Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * CPU frequency scaling for DaVinci |
| 3 | * |
| 4 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * |
| 6 | * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows: |
| 7 | * |
| 8 | * Copyright (C) 2005 Nokia Corporation |
| 9 | * Written by Tony Lindgren <tony@atomide.com> |
| 10 | * |
| 11 | * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King |
| 12 | * |
| 13 | * Copyright (C) 2007-2008 Texas Instruments, Inc. |
| 14 | * Updated to support OMAP3 |
| 15 | * Rajendra Nayak <rnayak@ti.com> |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify |
| 18 | * it under the terms of the GNU General Public License version 2 as |
| 19 | * published by the Free Software Foundation. |
| 20 | */ |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/cpufreq.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/err.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/platform_device.h> |
Paul Gortmaker | dc28094 | 2011-07-31 16:17:29 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 28 | |
| 29 | #include <mach/hardware.h> |
| 30 | #include <mach/cpufreq.h> |
| 31 | #include <mach/common.h> |
| 32 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 33 | struct davinci_cpufreq { |
| 34 | struct device *dev; |
| 35 | struct clk *armclk; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 36 | struct clk *asyncclk; |
| 37 | unsigned long asyncrate; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 38 | }; |
| 39 | static struct davinci_cpufreq cpufreq; |
| 40 | |
| 41 | static int davinci_verify_speed(struct cpufreq_policy *policy) |
| 42 | { |
| 43 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 44 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 45 | struct clk *armclk = cpufreq.armclk; |
| 46 | |
| 47 | if (freq_table) |
| 48 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 49 | |
| 50 | if (policy->cpu) |
| 51 | return -EINVAL; |
| 52 | |
| 53 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 54 | policy->cpuinfo.max_freq); |
| 55 | |
| 56 | policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000; |
| 57 | policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000; |
| 58 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 59 | policy->cpuinfo.max_freq); |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static unsigned int davinci_getspeed(unsigned int cpu) |
| 64 | { |
| 65 | if (cpu) |
| 66 | return 0; |
| 67 | |
| 68 | return clk_get_rate(cpufreq.armclk) / 1000; |
| 69 | } |
| 70 | |
| 71 | static int davinci_target(struct cpufreq_policy *policy, |
| 72 | unsigned int target_freq, unsigned int relation) |
| 73 | { |
| 74 | int ret = 0; |
| 75 | unsigned int idx; |
| 76 | struct cpufreq_freqs freqs; |
| 77 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 78 | struct clk *armclk = cpufreq.armclk; |
| 79 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 80 | freqs.old = davinci_getspeed(0); |
| 81 | freqs.new = clk_round_rate(armclk, target_freq * 1000) / 1000; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 82 | |
| 83 | if (freqs.old == freqs.new) |
| 84 | return ret; |
| 85 | |
Uwe Kleine-König | d870df6 | 2012-03-08 20:36:44 +0100 | [diff] [blame] | 86 | dev_dbg(cpufreq.dev, "transition: %u --> %u\n", freqs.old, freqs.new); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 87 | |
| 88 | ret = cpufreq_frequency_table_target(policy, pdata->freq_table, |
| 89 | freqs.new, relation, &idx); |
| 90 | if (ret) |
| 91 | return -EINVAL; |
| 92 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 93 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 94 | |
| 95 | /* if moving to higher frequency, up the voltage beforehand */ |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 96 | if (pdata->set_voltage && freqs.new > freqs.old) { |
| 97 | ret = pdata->set_voltage(idx); |
| 98 | if (ret) |
| 99 | goto out; |
| 100 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 101 | |
| 102 | ret = clk_set_rate(armclk, idx); |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 103 | if (ret) |
| 104 | goto out; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 105 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 106 | if (cpufreq.asyncclk) { |
| 107 | ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate); |
| 108 | if (ret) |
| 109 | goto out; |
| 110 | } |
| 111 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 112 | /* if moving to lower freq, lower the voltage after lowering freq */ |
| 113 | if (pdata->set_voltage && freqs.new < freqs.old) |
| 114 | pdata->set_voltage(idx); |
| 115 | |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 116 | out: |
Viresh Kumar | f20b97d | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 117 | if (ret) |
| 118 | freqs.new = freqs.old; |
| 119 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 120 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 121 | |
| 122 | return ret; |
| 123 | } |
| 124 | |
Axel Lin | 079db59 | 2011-02-28 15:51:33 +0530 | [diff] [blame] | 125 | static int davinci_cpu_init(struct cpufreq_policy *policy) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 126 | { |
| 127 | int result = 0; |
| 128 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 129 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 130 | |
| 131 | if (policy->cpu != 0) |
| 132 | return -EINVAL; |
| 133 | |
Sekhar Nori | 13d5e27 | 2009-10-22 15:12:16 +0530 | [diff] [blame] | 134 | /* Finish platform specific initialization */ |
| 135 | if (pdata->init) { |
| 136 | result = pdata->init(); |
| 137 | if (result) |
| 138 | return result; |
| 139 | } |
| 140 | |
Viresh Kumar | eb2f50f | 2013-04-01 12:57:48 +0000 | [diff] [blame] | 141 | policy->cur = davinci_getspeed(0); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 142 | |
Viresh Kumar | eb2f50f | 2013-04-01 12:57:48 +0000 | [diff] [blame] | 143 | result = cpufreq_frequency_table_cpuinfo(policy, freq_table); |
| 144 | if (result) { |
| 145 | pr_err("%s: cpufreq_frequency_table_cpuinfo() failed", |
| 146 | __func__); |
| 147 | return result; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 148 | } |
| 149 | |
Viresh Kumar | eb2f50f | 2013-04-01 12:57:48 +0000 | [diff] [blame] | 150 | cpufreq_frequency_table_get_attr(freq_table, policy->cpu); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Time measurement across the target() function yields ~1500-1800us |
| 154 | * time taken with no drivers on notification list. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 155 | * Setting the latency to 2000 us to accommodate addition of drivers |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 156 | * to pre/post change notification list. |
| 157 | */ |
| 158 | policy->cpuinfo.transition_latency = 2000 * 1000; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int davinci_cpu_exit(struct cpufreq_policy *policy) |
| 163 | { |
| 164 | cpufreq_frequency_table_put_attr(policy->cpu); |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static struct freq_attr *davinci_cpufreq_attr[] = { |
| 169 | &cpufreq_freq_attr_scaling_available_freqs, |
| 170 | NULL, |
| 171 | }; |
| 172 | |
| 173 | static struct cpufreq_driver davinci_driver = { |
| 174 | .flags = CPUFREQ_STICKY, |
| 175 | .verify = davinci_verify_speed, |
| 176 | .target = davinci_target, |
| 177 | .get = davinci_getspeed, |
| 178 | .init = davinci_cpu_init, |
| 179 | .exit = davinci_cpu_exit, |
| 180 | .name = "davinci", |
| 181 | .attr = davinci_cpufreq_attr, |
| 182 | }; |
| 183 | |
| 184 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) |
| 185 | { |
| 186 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 187 | struct clk *asyncclk; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 188 | |
| 189 | if (!pdata) |
| 190 | return -EINVAL; |
| 191 | if (!pdata->freq_table) |
| 192 | return -EINVAL; |
| 193 | |
| 194 | cpufreq.dev = &pdev->dev; |
| 195 | |
| 196 | cpufreq.armclk = clk_get(NULL, "arm"); |
| 197 | if (IS_ERR(cpufreq.armclk)) { |
| 198 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); |
| 199 | return PTR_ERR(cpufreq.armclk); |
| 200 | } |
| 201 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 202 | asyncclk = clk_get(cpufreq.dev, "async"); |
| 203 | if (!IS_ERR(asyncclk)) { |
| 204 | cpufreq.asyncclk = asyncclk; |
| 205 | cpufreq.asyncrate = clk_get_rate(asyncclk); |
| 206 | } |
| 207 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 208 | return cpufreq_register_driver(&davinci_driver); |
| 209 | } |
| 210 | |
| 211 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) |
| 212 | { |
| 213 | clk_put(cpufreq.armclk); |
| 214 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 215 | if (cpufreq.asyncclk) |
| 216 | clk_put(cpufreq.asyncclk); |
| 217 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 218 | return cpufreq_unregister_driver(&davinci_driver); |
| 219 | } |
| 220 | |
| 221 | static struct platform_driver davinci_cpufreq_driver = { |
| 222 | .driver = { |
| 223 | .name = "cpufreq-davinci", |
| 224 | .owner = THIS_MODULE, |
| 225 | }, |
| 226 | .remove = __exit_p(davinci_cpufreq_remove), |
| 227 | }; |
| 228 | |
Shawn Guo | 3aa3e84 | 2012-04-26 09:45:39 +0800 | [diff] [blame] | 229 | int __init davinci_cpufreq_init(void) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 230 | { |
| 231 | return platform_driver_probe(&davinci_cpufreq_driver, |
| 232 | davinci_cpufreq_probe); |
| 233 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 234 | |