Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 2 | /* |
| 3 | * CPU frequency scaling for DaVinci |
| 4 | * |
Alexander A. Klimov | 8479eb8 | 2020-07-13 12:55:02 +0200 | [diff] [blame] | 5 | * Copyright (C) 2009 Texas Instruments Incorporated - https://www.ti.com/ |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 6 | * |
| 7 | * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows: |
| 8 | * |
| 9 | * Copyright (C) 2005 Nokia Corporation |
| 10 | * Written by Tony Lindgren <tony@atomide.com> |
| 11 | * |
| 12 | * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King |
| 13 | * |
| 14 | * Copyright (C) 2007-2008 Texas Instruments, Inc. |
| 15 | * Updated to support OMAP3 |
| 16 | * Rajendra Nayak <rnayak@ti.com> |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 17 | */ |
| 18 | #include <linux/types.h> |
| 19 | #include <linux/cpufreq.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/clk.h> |
Bartosz Golaszewski | 40b46b3 | 2019-02-14 18:05:05 +0100 | [diff] [blame] | 23 | #include <linux/platform_data/davinci-cpufreq.h> |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Paul Gortmaker | dc28094 | 2011-07-31 16:17:29 -0400 | [diff] [blame] | 25 | #include <linux/export.h> |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 26 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 27 | struct davinci_cpufreq { |
| 28 | struct device *dev; |
| 29 | struct clk *armclk; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 30 | struct clk *asyncclk; |
| 31 | unsigned long asyncrate; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 32 | }; |
| 33 | static struct davinci_cpufreq cpufreq; |
| 34 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 35 | static int davinci_target(struct cpufreq_policy *policy, unsigned int idx) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 36 | { |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 37 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 38 | struct clk *armclk = cpufreq.armclk; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 39 | unsigned int old_freq, new_freq; |
| 40 | int ret = 0; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 41 | |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 42 | old_freq = policy->cur; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 43 | new_freq = pdata->freq_table[idx].frequency; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 44 | |
| 45 | /* if moving to higher frequency, up the voltage beforehand */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 46 | if (pdata->set_voltage && new_freq > old_freq) { |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 47 | ret = pdata->set_voltage(idx); |
| 48 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 49 | return ret; |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 50 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 51 | |
Bartosz Golaszewski | b4088173 | 2016-12-07 16:22:18 +0100 | [diff] [blame] | 52 | ret = clk_set_rate(armclk, new_freq * 1000); |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 53 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 54 | return ret; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 55 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 56 | if (cpufreq.asyncclk) { |
| 57 | ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate); |
| 58 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 59 | return ret; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 60 | } |
| 61 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 62 | /* if moving to lower freq, lower the voltage after lowering freq */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 63 | if (pdata->set_voltage && new_freq < old_freq) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 64 | pdata->set_voltage(idx); |
| 65 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 66 | return 0; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 67 | } |
| 68 | |
Axel Lin | 079db59 | 2011-02-28 15:51:33 +0530 | [diff] [blame] | 69 | static int davinci_cpu_init(struct cpufreq_policy *policy) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 70 | { |
| 71 | int result = 0; |
| 72 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 73 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 74 | |
| 75 | if (policy->cpu != 0) |
| 76 | return -EINVAL; |
| 77 | |
Sekhar Nori | 13d5e27 | 2009-10-22 15:12:16 +0530 | [diff] [blame] | 78 | /* Finish platform specific initialization */ |
| 79 | if (pdata->init) { |
| 80 | result = pdata->init(); |
| 81 | if (result) |
| 82 | return result; |
| 83 | } |
| 84 | |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 85 | policy->clk = cpufreq.armclk; |
| 86 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 87 | /* |
| 88 | * Time measurement across the target() function yields ~1500-1800us |
| 89 | * time taken with no drivers on notification list. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 90 | * Setting the latency to 2000 us to accommodate addition of drivers |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 91 | * to pre/post change notification list. |
| 92 | */ |
Viresh Kumar | c4dcc8a | 2019-07-16 09:36:08 +0530 | [diff] [blame] | 93 | cpufreq_generic_init(policy, freq_table, 2000 * 1000); |
| 94 | return 0; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 95 | } |
| 96 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 97 | static struct cpufreq_driver davinci_driver = { |
Viresh Kumar | ae6b427 | 2013-12-03 11:20:45 +0530 | [diff] [blame] | 98 | .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK, |
Viresh Kumar | 9d4de29 | 2016-06-03 10:58:52 +0530 | [diff] [blame] | 99 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 100 | .target_index = davinci_target, |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 101 | .get = cpufreq_generic_get, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 102 | .init = davinci_cpu_init, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 103 | .name = "davinci", |
Viresh Kumar | 39d0c36 | 2013-10-03 20:28:02 +0530 | [diff] [blame] | 104 | .attr = cpufreq_generic_attr, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) |
| 108 | { |
| 109 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 110 | struct clk *asyncclk; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 111 | |
| 112 | if (!pdata) |
| 113 | return -EINVAL; |
| 114 | if (!pdata->freq_table) |
| 115 | return -EINVAL; |
| 116 | |
| 117 | cpufreq.dev = &pdev->dev; |
| 118 | |
| 119 | cpufreq.armclk = clk_get(NULL, "arm"); |
| 120 | if (IS_ERR(cpufreq.armclk)) { |
| 121 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); |
| 122 | return PTR_ERR(cpufreq.armclk); |
| 123 | } |
| 124 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 125 | asyncclk = clk_get(cpufreq.dev, "async"); |
| 126 | if (!IS_ERR(asyncclk)) { |
| 127 | cpufreq.asyncclk = asyncclk; |
| 128 | cpufreq.asyncrate = clk_get_rate(asyncclk); |
| 129 | } |
| 130 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 131 | return cpufreq_register_driver(&davinci_driver); |
| 132 | } |
| 133 | |
| 134 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) |
| 135 | { |
| 136 | clk_put(cpufreq.armclk); |
| 137 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 138 | if (cpufreq.asyncclk) |
| 139 | clk_put(cpufreq.asyncclk); |
| 140 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 141 | return cpufreq_unregister_driver(&davinci_driver); |
| 142 | } |
| 143 | |
| 144 | static struct platform_driver davinci_cpufreq_driver = { |
| 145 | .driver = { |
| 146 | .name = "cpufreq-davinci", |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 147 | }, |
| 148 | .remove = __exit_p(davinci_cpufreq_remove), |
| 149 | }; |
| 150 | |
Shawn Guo | 3aa3e84 | 2012-04-26 09:45:39 +0800 | [diff] [blame] | 151 | int __init davinci_cpufreq_init(void) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 152 | { |
| 153 | return platform_driver_probe(&davinci_cpufreq_driver, |
| 154 | davinci_cpufreq_probe); |
| 155 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 156 | |