Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 1 | /* |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 2 | * Copyright (C) STMicroelectronics 2009 |
| 3 | * Copyright (C) ST-Ericsson SA 2010 |
| 4 | * |
| 5 | * License Terms: GNU General Public License v2 |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 6 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> |
| 7 | * Author: Martin Persson <martin.persson@stericsson.com> |
| 8 | * Author: Jonas Aaberg <jonas.aberg@stericsson.com> |
| 9 | * |
| 10 | */ |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame^] | 11 | #include <linux/module.h> |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/cpufreq.h> |
| 14 | #include <linux/delay.h> |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame^] | 16 | #include <linux/platform_device.h> |
Mattias Nilsson | 73180f8 | 2011-08-12 10:28:10 +0200 | [diff] [blame] | 17 | #include <linux/mfd/dbx500-prcmu.h> |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 18 | #include <mach/id.h> |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 19 | |
| 20 | static struct cpufreq_frequency_table freq_table[] = { |
| 21 | [0] = { |
| 22 | .index = 0, |
Linus Walleij | 6283e32 | 2011-09-02 08:52:10 +0200 | [diff] [blame] | 23 | .frequency = 200000, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 24 | }, |
| 25 | [1] = { |
| 26 | .index = 1, |
Daniel Willerud | c72fe85 | 2012-01-13 16:20:03 +0100 | [diff] [blame] | 27 | .frequency = 400000, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 28 | }, |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 29 | [2] = { |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 30 | .index = 2, |
Daniel Willerud | c72fe85 | 2012-01-13 16:20:03 +0100 | [diff] [blame] | 31 | .frequency = 800000, |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 32 | }, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 33 | [3] = { |
Linus Walleij | 6283e32 | 2011-09-02 08:52:10 +0200 | [diff] [blame] | 34 | /* Used for MAX_OPP, if available */ |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 35 | .index = 3, |
| 36 | .frequency = CPUFREQ_TABLE_END, |
| 37 | }, |
Linus Walleij | 6283e32 | 2011-09-02 08:52:10 +0200 | [diff] [blame] | 38 | [4] = { |
| 39 | .index = 4, |
| 40 | .frequency = CPUFREQ_TABLE_END, |
| 41 | }, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 44 | static enum arm_opp idx2opp[] = { |
Linus Walleij | 6283e32 | 2011-09-02 08:52:10 +0200 | [diff] [blame] | 45 | ARM_EXTCLK, |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 46 | ARM_50_OPP, |
| 47 | ARM_100_OPP, |
| 48 | ARM_MAX_OPP |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 51 | static struct freq_attr *db8500_cpufreq_attr[] = { |
| 52 | &cpufreq_freq_attr_scaling_available_freqs, |
| 53 | NULL, |
| 54 | }; |
| 55 | |
| 56 | static int db8500_cpufreq_verify_speed(struct cpufreq_policy *policy) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 57 | { |
| 58 | return cpufreq_frequency_table_verify(policy, freq_table); |
| 59 | } |
| 60 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 61 | static int db8500_cpufreq_target(struct cpufreq_policy *policy, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 62 | unsigned int target_freq, |
| 63 | unsigned int relation) |
| 64 | { |
| 65 | struct cpufreq_freqs freqs; |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 66 | unsigned int idx; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 67 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 68 | /* scale the target frequency to one of the extremes supported */ |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 69 | if (target_freq < policy->cpuinfo.min_freq) |
| 70 | target_freq = policy->cpuinfo.min_freq; |
| 71 | if (target_freq > policy->cpuinfo.max_freq) |
| 72 | target_freq = policy->cpuinfo.max_freq; |
| 73 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 74 | /* Lookup the next frequency */ |
| 75 | if (cpufreq_frequency_table_target |
| 76 | (policy, freq_table, target_freq, relation, &idx)) { |
| 77 | return -EINVAL; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | freqs.old = policy->cur; |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 81 | freqs.new = freq_table[idx].frequency; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 82 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 83 | if (freqs.old == freqs.new) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 84 | return 0; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 85 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 86 | /* pre-change notification */ |
Vincent Guittot | 8efd072 | 2011-08-25 08:31:20 +0200 | [diff] [blame] | 87 | for_each_cpu(freqs.cpu, policy->cpus) |
| 88 | cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 89 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 90 | /* request the PRCM unit for opp change */ |
| 91 | if (prcmu_set_arm_opp(idx2opp[idx])) { |
| 92 | pr_err("db8500-cpufreq: Failed to set OPP level\n"); |
| 93 | return -EINVAL; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 96 | /* post change notification */ |
Vincent Guittot | 8efd072 | 2011-08-25 08:31:20 +0200 | [diff] [blame] | 97 | for_each_cpu(freqs.cpu, policy->cpus) |
| 98 | cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 99 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 100 | return 0; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 103 | static unsigned int db8500_cpufreq_getspeed(unsigned int cpu) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 104 | { |
| 105 | int i; |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 106 | /* request the prcm to get the current ARM opp */ |
| 107 | for (i = 0; prcmu_get_arm_opp() != idx2opp[i]; i++) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 108 | ; |
| 109 | return freq_table[i].frequency; |
| 110 | } |
| 111 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 112 | static int __cpuinit db8500_cpufreq_init(struct cpufreq_policy *policy) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 113 | { |
Axel Lin | eb0b38a | 2011-11-04 20:04:41 +0800 | [diff] [blame] | 114 | int i, res; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 115 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 116 | BUILD_BUG_ON(ARRAY_SIZE(idx2opp) + 1 != ARRAY_SIZE(freq_table)); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 117 | |
Daniel Willerud | c72fe85 | 2012-01-13 16:20:03 +0100 | [diff] [blame] | 118 | if (prcmu_has_arm_maxopp()) |
| 119 | freq_table[3].frequency = 1000000; |
| 120 | |
Linus Walleij | 6283e32 | 2011-09-02 08:52:10 +0200 | [diff] [blame] | 121 | pr_info("db8500-cpufreq : Available frequencies:\n"); |
Axel Lin | eb0b38a | 2011-11-04 20:04:41 +0800 | [diff] [blame] | 122 | for (i = 0; freq_table[i].frequency != CPUFREQ_TABLE_END; i++) |
| 123 | pr_info(" %d Mhz\n", freq_table[i].frequency/1000); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 124 | |
| 125 | /* get policy fields based on the table */ |
| 126 | res = cpufreq_frequency_table_cpuinfo(policy, freq_table); |
| 127 | if (!res) |
| 128 | cpufreq_frequency_table_get_attr(freq_table, policy->cpu); |
| 129 | else { |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 130 | pr_err("db8500-cpufreq : Failed to read policy table\n"); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 131 | return res; |
| 132 | } |
| 133 | |
| 134 | policy->min = policy->cpuinfo.min_freq; |
| 135 | policy->max = policy->cpuinfo.max_freq; |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 136 | policy->cur = db8500_cpufreq_getspeed(policy->cpu); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 137 | policy->governor = CPUFREQ_DEFAULT_GOVERNOR; |
| 138 | |
| 139 | /* |
| 140 | * FIXME : Need to take time measurement across the target() |
| 141 | * function with no/some/all drivers in the notification |
| 142 | * list. |
| 143 | */ |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 144 | policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */ |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 145 | |
| 146 | /* policy sharing between dual CPUs */ |
Rusty Russell | 88d8cd5 | 2012-03-29 15:38:30 +1030 | [diff] [blame] | 147 | cpumask_copy(policy->cpus, cpu_present_mask); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 148 | |
| 149 | policy->shared_type = CPUFREQ_SHARED_TYPE_ALL; |
| 150 | |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 154 | static struct cpufreq_driver db8500_cpufreq_driver = { |
| 155 | .flags = CPUFREQ_STICKY, |
| 156 | .verify = db8500_cpufreq_verify_speed, |
| 157 | .target = db8500_cpufreq_target, |
| 158 | .get = db8500_cpufreq_getspeed, |
| 159 | .init = db8500_cpufreq_init, |
| 160 | .name = "DB8500", |
| 161 | .attr = db8500_cpufreq_attr, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 162 | }; |
| 163 | |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame^] | 164 | static int db8500_cpufreq_probe(struct platform_device *pdev) |
| 165 | { |
| 166 | return cpufreq_register_driver(&db8500_cpufreq_driver); |
| 167 | } |
| 168 | |
| 169 | static struct platform_driver db8500_cpufreq_plat_driver = { |
| 170 | .driver = { |
| 171 | .name = "cpufreq-u8500", |
| 172 | .owner = THIS_MODULE, |
| 173 | }, |
| 174 | .probe = db8500_cpufreq_probe, |
| 175 | }; |
| 176 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 177 | static int __init db8500_cpufreq_register(void) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 178 | { |
Linus Walleij | bc71c09 | 2012-01-23 11:54:44 +0100 | [diff] [blame] | 179 | if (!cpu_is_u8500_family()) |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 180 | return -ENODEV; |
| 181 | |
| 182 | pr_info("cpufreq for DB8500 started\n"); |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame^] | 183 | return platform_driver_register(&db8500_cpufreq_plat_driver); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 184 | } |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 185 | device_initcall(db8500_cpufreq_register); |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame^] | 186 | |
| 187 | MODULE_LICENSE("GPL v2"); |
| 188 | MODULE_DESCRIPTION("cpufreq driver for DB8500"); |