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 |
Lee Jones | 0baf066 | 2012-11-28 11:27:44 +0100 | [diff] [blame] | 3 | * Copyright (C) ST-Ericsson SA 2010-2012 |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 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> |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 9 | */ |
Lee Jones | 0baf066 | 2012-11-28 11:27:44 +0100 | [diff] [blame] | 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> |
Ulf Hansson | 78e30d1 | 2012-10-10 13:42:29 +0200 | [diff] [blame] | 17 | #include <linux/clk.h> |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 18 | |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 19 | static struct cpufreq_frequency_table *freq_table; |
Ulf Hansson | 78e30d1 | 2012-10-10 13:42:29 +0200 | [diff] [blame] | 20 | static struct clk *armss_clk; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 21 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 22 | static int dbx500_cpufreq_target(struct cpufreq_policy *policy, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 23 | unsigned int index) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 24 | { |
| 25 | struct cpufreq_freqs freqs; |
Jonas Aaberg | 9291cf9 | 2012-11-28 11:27:43 +0100 | [diff] [blame] | 26 | int ret; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 27 | |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 28 | freqs.old = policy->cur; |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 29 | freqs.new = freq_table[index].frequency; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 30 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 31 | /* pre-change notification */ |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 32 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 33 | |
Ulf Hansson | 78e30d1 | 2012-10-10 13:42:29 +0200 | [diff] [blame] | 34 | /* update armss clk frequency */ |
Jonas Aaberg | 9291cf9 | 2012-11-28 11:27:43 +0100 | [diff] [blame] | 35 | ret = clk_set_rate(armss_clk, freqs.new * 1000); |
| 36 | |
| 37 | if (ret) { |
| 38 | pr_err("dbx500-cpufreq: Failed to set armss_clk to %d Hz: error %d\n", |
| 39 | freqs.new * 1000, ret); |
Viresh Kumar | 803c126 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 40 | freqs.new = freqs.old; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Linus Walleij | 72b2fd5 | 2011-05-15 19:19:51 +0200 | [diff] [blame] | 43 | /* post change notification */ |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 44 | cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 45 | |
Viresh Kumar | 803c126 | 2013-06-19 11:18:20 +0530 | [diff] [blame] | 46 | return ret; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 49 | static unsigned int dbx500_cpufreq_getspeed(unsigned int cpu) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 50 | { |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 51 | int i = 0; |
Ulf Hansson | 78e30d1 | 2012-10-10 13:42:29 +0200 | [diff] [blame] | 52 | unsigned long freq = clk_get_rate(armss_clk) / 1000; |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 53 | |
Mats Fagerstrom | c778966 | 2013-04-11 23:23:57 +0200 | [diff] [blame] | 54 | /* The value is rounded to closest frequency in the defined table. */ |
| 55 | while (freq_table[i + 1].frequency != CPUFREQ_TABLE_END) { |
| 56 | if (freq < freq_table[i].frequency + |
| 57 | (freq_table[i + 1].frequency - freq_table[i].frequency) / 2) |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 58 | return freq_table[i].frequency; |
| 59 | i++; |
| 60 | } |
| 61 | |
Mats Fagerstrom | c778966 | 2013-04-11 23:23:57 +0200 | [diff] [blame] | 62 | return freq_table[i].frequency; |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Paul Gortmaker | 2760984 | 2013-06-19 13:54:04 -0400 | [diff] [blame] | 65 | static int dbx500_cpufreq_init(struct cpufreq_policy *policy) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 66 | { |
Viresh Kumar | 2b3dc76 | 2013-10-03 20:29:12 +0530 | [diff] [blame] | 67 | return cpufreq_generic_init(policy, freq_table, 20 * 1000); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 70 | static struct cpufreq_driver dbx500_cpufreq_driver = { |
Fabio Baltieri | ec66912 | 2012-12-04 11:10:45 +0100 | [diff] [blame] | 71 | .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS, |
Viresh Kumar | 47150e9 | 2013-10-03 20:28:03 +0530 | [diff] [blame] | 72 | .verify = cpufreq_generic_frequency_table_verify, |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 73 | .target_index = dbx500_cpufreq_target, |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 74 | .get = dbx500_cpufreq_getspeed, |
| 75 | .init = dbx500_cpufreq_init, |
| 76 | .name = "DBX500", |
Viresh Kumar | 47150e9 | 2013-10-03 20:28:03 +0530 | [diff] [blame] | 77 | .attr = cpufreq_generic_attr, |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 78 | }; |
| 79 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 80 | static int dbx500_cpufreq_probe(struct platform_device *pdev) |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 81 | { |
Ulf Hansson | 3e27996 | 2012-12-10 16:25:40 +0100 | [diff] [blame] | 82 | int i = 0; |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 83 | |
Ulf Hansson | 3e27996 | 2012-12-10 16:25:40 +0100 | [diff] [blame] | 84 | freq_table = dev_get_platdata(&pdev->dev); |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 85 | if (!freq_table) { |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 86 | pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n"); |
Ulf Hansson | fdb4446 | 2012-10-10 13:42:25 +0200 | [diff] [blame] | 87 | return -ENODEV; |
| 88 | } |
| 89 | |
Ulf Hansson | 3e27996 | 2012-12-10 16:25:40 +0100 | [diff] [blame] | 90 | armss_clk = clk_get(&pdev->dev, "armss"); |
| 91 | if (IS_ERR(armss_clk)) { |
Jonas Aaberg | 9291cf9 | 2012-11-28 11:27:43 +0100 | [diff] [blame] | 92 | pr_err("dbx500-cpufreq: Failed to get armss clk\n"); |
Ulf Hansson | 3e27996 | 2012-12-10 16:25:40 +0100 | [diff] [blame] | 93 | return PTR_ERR(armss_clk); |
| 94 | } |
| 95 | |
Jonas Aaberg | 9291cf9 | 2012-11-28 11:27:43 +0100 | [diff] [blame] | 96 | pr_info("dbx500-cpufreq: Available frequencies:\n"); |
Ulf Hansson | 3e27996 | 2012-12-10 16:25:40 +0100 | [diff] [blame] | 97 | while (freq_table[i].frequency != CPUFREQ_TABLE_END) { |
| 98 | pr_info(" %d Mhz\n", freq_table[i].frequency/1000); |
| 99 | i++; |
| 100 | } |
| 101 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 102 | return cpufreq_register_driver(&dbx500_cpufreq_driver); |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 103 | } |
| 104 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 105 | static struct platform_driver dbx500_cpufreq_plat_driver = { |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 106 | .driver = { |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 107 | .name = "cpufreq-ux500", |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 108 | .owner = THIS_MODULE, |
| 109 | }, |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 110 | .probe = dbx500_cpufreq_probe, |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 113 | static int __init dbx500_cpufreq_register(void) |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 114 | { |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 115 | return platform_driver_register(&dbx500_cpufreq_plat_driver); |
Martin Persson | 7c1a70e | 2010-12-08 15:13:42 +0100 | [diff] [blame] | 116 | } |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 117 | device_initcall(dbx500_cpufreq_register); |
Ulf Hansson | b468944 | 2012-10-10 13:42:24 +0200 | [diff] [blame] | 118 | |
| 119 | MODULE_LICENSE("GPL v2"); |
Lee Jones | edb10c1 | 2012-12-10 16:25:38 +0100 | [diff] [blame] | 120 | MODULE_DESCRIPTION("cpufreq driver for DBX500"); |