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