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 | |
Viresh Kumar | be49e34 | 2013-10-02 14:13:19 +0530 | [diff] [blame] | 53 | cpufreq_verify_within_cpu_limits(policy); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 54 | policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000; |
| 55 | policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000; |
| 56 | cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq, |
| 57 | policy->cpuinfo.max_freq); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static unsigned int davinci_getspeed(unsigned int cpu) |
| 62 | { |
| 63 | if (cpu) |
| 64 | return 0; |
| 65 | |
| 66 | return clk_get_rate(cpufreq.armclk) / 1000; |
| 67 | } |
| 68 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 69 | static int davinci_target(struct cpufreq_policy *policy, unsigned int idx) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 70 | { |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 71 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 72 | struct clk *armclk = cpufreq.armclk; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 73 | unsigned int old_freq, new_freq; |
| 74 | int ret = 0; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 75 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 76 | old_freq = davinci_getspeed(0); |
| 77 | new_freq = pdata->freq_table[idx].frequency; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 78 | |
| 79 | /* if moving to higher frequency, up the voltage beforehand */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 80 | if (pdata->set_voltage && new_freq > old_freq) { |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 81 | ret = pdata->set_voltage(idx); |
| 82 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 83 | return ret; |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 84 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 85 | |
| 86 | ret = clk_set_rate(armclk, idx); |
Sekhar Nori | fca97b3 | 2010-07-20 16:46:48 +0530 | [diff] [blame] | 87 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 88 | return ret; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 89 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 90 | if (cpufreq.asyncclk) { |
| 91 | ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate); |
| 92 | if (ret) |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 93 | return ret; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 94 | } |
| 95 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 96 | /* if moving to lower freq, lower the voltage after lowering freq */ |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 97 | if (pdata->set_voltage && new_freq < old_freq) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 98 | pdata->set_voltage(idx); |
| 99 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 100 | return 0; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 101 | } |
| 102 | |
Axel Lin | 079db59 | 2011-02-28 15:51:33 +0530 | [diff] [blame] | 103 | static int davinci_cpu_init(struct cpufreq_policy *policy) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 104 | { |
| 105 | int result = 0; |
| 106 | struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data; |
| 107 | struct cpufreq_frequency_table *freq_table = pdata->freq_table; |
| 108 | |
| 109 | if (policy->cpu != 0) |
| 110 | return -EINVAL; |
| 111 | |
Sekhar Nori | 13d5e27 | 2009-10-22 15:12:16 +0530 | [diff] [blame] | 112 | /* Finish platform specific initialization */ |
| 113 | if (pdata->init) { |
| 114 | result = pdata->init(); |
| 115 | if (result) |
| 116 | return result; |
| 117 | } |
| 118 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 119 | /* |
| 120 | * Time measurement across the target() function yields ~1500-1800us |
| 121 | * time taken with no drivers on notification list. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 122 | * Setting the latency to 2000 us to accommodate addition of drivers |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 123 | * to pre/post change notification list. |
| 124 | */ |
Viresh Kumar | af8c4cf | 2013-10-03 20:29:11 +0530 | [diff] [blame] | 125 | return cpufreq_generic_init(policy, freq_table, 2000 * 1000); |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 126 | } |
| 127 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 128 | static struct cpufreq_driver davinci_driver = { |
| 129 | .flags = CPUFREQ_STICKY, |
| 130 | .verify = davinci_verify_speed, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 131 | .target_index = davinci_target, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 132 | .get = davinci_getspeed, |
| 133 | .init = davinci_cpu_init, |
Viresh Kumar | 39d0c36 | 2013-10-03 20:28:02 +0530 | [diff] [blame] | 134 | .exit = cpufreq_generic_exit, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 135 | .name = "davinci", |
Viresh Kumar | 39d0c36 | 2013-10-03 20:28:02 +0530 | [diff] [blame] | 136 | .attr = cpufreq_generic_attr, |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | static int __init davinci_cpufreq_probe(struct platform_device *pdev) |
| 140 | { |
| 141 | struct davinci_cpufreq_config *pdata = pdev->dev.platform_data; |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 142 | struct clk *asyncclk; |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 143 | |
| 144 | if (!pdata) |
| 145 | return -EINVAL; |
| 146 | if (!pdata->freq_table) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | cpufreq.dev = &pdev->dev; |
| 150 | |
| 151 | cpufreq.armclk = clk_get(NULL, "arm"); |
| 152 | if (IS_ERR(cpufreq.armclk)) { |
| 153 | dev_err(cpufreq.dev, "Unable to get ARM clock\n"); |
| 154 | return PTR_ERR(cpufreq.armclk); |
| 155 | } |
| 156 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 157 | asyncclk = clk_get(cpufreq.dev, "async"); |
| 158 | if (!IS_ERR(asyncclk)) { |
| 159 | cpufreq.asyncclk = asyncclk; |
| 160 | cpufreq.asyncrate = clk_get_rate(asyncclk); |
| 161 | } |
| 162 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 163 | return cpufreq_register_driver(&davinci_driver); |
| 164 | } |
| 165 | |
| 166 | static int __exit davinci_cpufreq_remove(struct platform_device *pdev) |
| 167 | { |
| 168 | clk_put(cpufreq.armclk); |
| 169 | |
Sekhar Nori | 30a2c5d | 2010-07-20 16:46:50 +0530 | [diff] [blame] | 170 | if (cpufreq.asyncclk) |
| 171 | clk_put(cpufreq.asyncclk); |
| 172 | |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 173 | return cpufreq_unregister_driver(&davinci_driver); |
| 174 | } |
| 175 | |
| 176 | static struct platform_driver davinci_cpufreq_driver = { |
| 177 | .driver = { |
| 178 | .name = "cpufreq-davinci", |
| 179 | .owner = THIS_MODULE, |
| 180 | }, |
| 181 | .remove = __exit_p(davinci_cpufreq_remove), |
| 182 | }; |
| 183 | |
Shawn Guo | 3aa3e84 | 2012-04-26 09:45:39 +0800 | [diff] [blame] | 184 | int __init davinci_cpufreq_init(void) |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 185 | { |
| 186 | return platform_driver_probe(&davinci_cpufreq_driver, |
| 187 | davinci_cpufreq_probe); |
| 188 | } |
Sekhar Nori | 6601b80 | 2009-09-22 21:14:00 +0530 | [diff] [blame] | 189 | |