blob: 6ec6539ae0418895a4b54619e98eeac7d647159c [file] [log] [blame]
Martin Persson7c1a70e2010-12-08 15:13:42 +01001/*
Martin Persson7c1a70e2010-12-08 15:13:42 +01002 * Copyright (C) STMicroelectronics 2009
Lee Jones0baf0662012-11-28 11:27:44 +01003 * Copyright (C) ST-Ericsson SA 2010-2012
Martin Persson7c1a70e2010-12-08 15:13:42 +01004 *
5 * License Terms: GNU General Public License v2
Martin Persson7c1a70e2010-12-08 15:13:42 +01006 * Author: Sundar Iyer <sundar.iyer@stericsson.com>
7 * Author: Martin Persson <martin.persson@stericsson.com>
8 * Author: Jonas Aaberg <jonas.aberg@stericsson.com>
Martin Persson7c1a70e2010-12-08 15:13:42 +01009 */
Lee Jones0baf0662012-11-28 11:27:44 +010010
Ulf Hanssonb4689442012-10-10 13:42:24 +020011#include <linux/module.h>
Martin Persson7c1a70e2010-12-08 15:13:42 +010012#include <linux/kernel.h>
13#include <linux/cpufreq.h>
14#include <linux/delay.h>
Linus Walleij72b2fd52011-05-15 19:19:51 +020015#include <linux/slab.h>
Ulf Hanssonb4689442012-10-10 13:42:24 +020016#include <linux/platform_device.h>
Ulf Hansson78e30d12012-10-10 13:42:29 +020017#include <linux/clk.h>
Martin Persson7c1a70e2010-12-08 15:13:42 +010018
Ulf Hanssonfdb44462012-10-10 13:42:25 +020019static struct cpufreq_frequency_table *freq_table;
Ulf Hansson78e30d12012-10-10 13:42:29 +020020static struct clk *armss_clk;
Martin Persson7c1a70e2010-12-08 15:13:42 +010021
Lee Jonesedb10c12012-12-10 16:25:38 +010022static struct freq_attr *dbx500_cpufreq_attr[] = {
Linus Walleij72b2fd52011-05-15 19:19:51 +020023 &cpufreq_freq_attr_scaling_available_freqs,
24 NULL,
25};
26
Lee Jonesedb10c12012-12-10 16:25:38 +010027static int dbx500_cpufreq_verify_speed(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010028{
29 return cpufreq_frequency_table_verify(policy, freq_table);
30}
31
Lee Jonesedb10c12012-12-10 16:25:38 +010032static int dbx500_cpufreq_target(struct cpufreq_policy *policy,
Martin Persson7c1a70e2010-12-08 15:13:42 +010033 unsigned int target_freq,
34 unsigned int relation)
35{
36 struct cpufreq_freqs freqs;
Linus Walleij72b2fd52011-05-15 19:19:51 +020037 unsigned int idx;
Jonas Aaberg9291cf92012-11-28 11:27:43 +010038 int ret;
Martin Persson7c1a70e2010-12-08 15:13:42 +010039
Linus Walleij72b2fd52011-05-15 19:19:51 +020040 /* Lookup the next frequency */
Jonas Aaberg9291cf92012-11-28 11:27:43 +010041 if (cpufreq_frequency_table_target(policy, freq_table, target_freq,
42 relation, &idx))
Linus Walleij72b2fd52011-05-15 19:19:51 +020043 return -EINVAL;
Martin Persson7c1a70e2010-12-08 15:13:42 +010044
45 freqs.old = policy->cur;
Linus Walleij72b2fd52011-05-15 19:19:51 +020046 freqs.new = freq_table[idx].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010047
Linus Walleij72b2fd52011-05-15 19:19:51 +020048 if (freqs.old == freqs.new)
Martin Persson7c1a70e2010-12-08 15:13:42 +010049 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +010050
Linus Walleij72b2fd52011-05-15 19:19:51 +020051 /* pre-change notification */
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053052 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010053
Ulf Hansson78e30d12012-10-10 13:42:29 +020054 /* update armss clk frequency */
Jonas Aaberg9291cf92012-11-28 11:27:43 +010055 ret = clk_set_rate(armss_clk, freqs.new * 1000);
56
57 if (ret) {
58 pr_err("dbx500-cpufreq: Failed to set armss_clk to %d Hz: error %d\n",
59 freqs.new * 1000, ret);
60 return ret;
Martin Persson7c1a70e2010-12-08 15:13:42 +010061 }
62
Linus Walleij72b2fd52011-05-15 19:19:51 +020063 /* post change notification */
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053064 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010065
Linus Walleij72b2fd52011-05-15 19:19:51 +020066 return 0;
Martin Persson7c1a70e2010-12-08 15:13:42 +010067}
68
Lee Jonesedb10c12012-12-10 16:25:38 +010069static unsigned int dbx500_cpufreq_getspeed(unsigned int cpu)
Martin Persson7c1a70e2010-12-08 15:13:42 +010070{
Ulf Hanssonfdb44462012-10-10 13:42:25 +020071 int i = 0;
Ulf Hansson78e30d12012-10-10 13:42:29 +020072 unsigned long freq = clk_get_rate(armss_clk) / 1000;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020073
Mats Fagerstromc7789662013-04-11 23:23:57 +020074 /* The value is rounded to closest frequency in the defined table. */
75 while (freq_table[i + 1].frequency != CPUFREQ_TABLE_END) {
76 if (freq < freq_table[i].frequency +
77 (freq_table[i + 1].frequency - freq_table[i].frequency) / 2)
Ulf Hanssonfdb44462012-10-10 13:42:25 +020078 return freq_table[i].frequency;
79 i++;
80 }
81
Mats Fagerstromc7789662013-04-11 23:23:57 +020082 return freq_table[i].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010083}
84
Lee Jonesedb10c12012-12-10 16:25:38 +010085static int __cpuinit dbx500_cpufreq_init(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010086{
Ulf Hanssonfdb44462012-10-10 13:42:25 +020087 int res;
Daniel Willerudc72fe852012-01-13 16:20:03 +010088
Martin Persson7c1a70e2010-12-08 15:13:42 +010089 /* get policy fields based on the table */
90 res = cpufreq_frequency_table_cpuinfo(policy, freq_table);
91 if (!res)
92 cpufreq_frequency_table_get_attr(freq_table, policy->cpu);
93 else {
Jonas Aaberg9291cf92012-11-28 11:27:43 +010094 pr_err("dbx500-cpufreq: Failed to read policy table\n");
Martin Persson7c1a70e2010-12-08 15:13:42 +010095 return res;
96 }
97
98 policy->min = policy->cpuinfo.min_freq;
99 policy->max = policy->cpuinfo.max_freq;
Lee Jonesedb10c12012-12-10 16:25:38 +0100100 policy->cur = dbx500_cpufreq_getspeed(policy->cpu);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100101 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
102
103 /*
104 * FIXME : Need to take time measurement across the target()
105 * function with no/some/all drivers in the notification
106 * list.
107 */
Linus Walleij72b2fd52011-05-15 19:19:51 +0200108 policy->cpuinfo.transition_latency = 20 * 1000; /* in ns */
Martin Persson7c1a70e2010-12-08 15:13:42 +0100109
110 /* policy sharing between dual CPUs */
Viresh Kumar4c738d02013-02-01 06:40:01 +0000111 cpumask_setall(policy->cpus);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100112
Martin Persson7c1a70e2010-12-08 15:13:42 +0100113 return 0;
114}
115
Lee Jonesedb10c12012-12-10 16:25:38 +0100116static struct cpufreq_driver dbx500_cpufreq_driver = {
Fabio Baltieriec669122012-12-04 11:10:45 +0100117 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
Lee Jonesedb10c12012-12-10 16:25:38 +0100118 .verify = dbx500_cpufreq_verify_speed,
119 .target = dbx500_cpufreq_target,
120 .get = dbx500_cpufreq_getspeed,
121 .init = dbx500_cpufreq_init,
122 .name = "DBX500",
123 .attr = dbx500_cpufreq_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +0100124};
125
Lee Jonesedb10c12012-12-10 16:25:38 +0100126static int dbx500_cpufreq_probe(struct platform_device *pdev)
Ulf Hanssonb4689442012-10-10 13:42:24 +0200127{
Ulf Hansson3e279962012-12-10 16:25:40 +0100128 int i = 0;
Ulf Hanssonfdb44462012-10-10 13:42:25 +0200129
Ulf Hansson3e279962012-12-10 16:25:40 +0100130 freq_table = dev_get_platdata(&pdev->dev);
Ulf Hanssonfdb44462012-10-10 13:42:25 +0200131 if (!freq_table) {
Lee Jonesedb10c12012-12-10 16:25:38 +0100132 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
Ulf Hanssonfdb44462012-10-10 13:42:25 +0200133 return -ENODEV;
134 }
135
Ulf Hansson3e279962012-12-10 16:25:40 +0100136 armss_clk = clk_get(&pdev->dev, "armss");
137 if (IS_ERR(armss_clk)) {
Jonas Aaberg9291cf92012-11-28 11:27:43 +0100138 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
Ulf Hansson3e279962012-12-10 16:25:40 +0100139 return PTR_ERR(armss_clk);
140 }
141
Jonas Aaberg9291cf92012-11-28 11:27:43 +0100142 pr_info("dbx500-cpufreq: Available frequencies:\n");
Ulf Hansson3e279962012-12-10 16:25:40 +0100143 while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
144 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
145 i++;
146 }
147
Lee Jonesedb10c12012-12-10 16:25:38 +0100148 return cpufreq_register_driver(&dbx500_cpufreq_driver);
Ulf Hanssonb4689442012-10-10 13:42:24 +0200149}
150
Lee Jonesedb10c12012-12-10 16:25:38 +0100151static struct platform_driver dbx500_cpufreq_plat_driver = {
Ulf Hanssonb4689442012-10-10 13:42:24 +0200152 .driver = {
Lee Jonesedb10c12012-12-10 16:25:38 +0100153 .name = "cpufreq-ux500",
Ulf Hanssonb4689442012-10-10 13:42:24 +0200154 .owner = THIS_MODULE,
155 },
Lee Jonesedb10c12012-12-10 16:25:38 +0100156 .probe = dbx500_cpufreq_probe,
Ulf Hanssonb4689442012-10-10 13:42:24 +0200157};
158
Lee Jonesedb10c12012-12-10 16:25:38 +0100159static int __init dbx500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100160{
Lee Jonesedb10c12012-12-10 16:25:38 +0100161 return platform_driver_register(&dbx500_cpufreq_plat_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100162}
Lee Jonesedb10c12012-12-10 16:25:38 +0100163device_initcall(dbx500_cpufreq_register);
Ulf Hanssonb4689442012-10-10 13:42:24 +0200164
165MODULE_LICENSE("GPL v2");
Lee Jonesedb10c12012-12-10 16:25:38 +0100166MODULE_DESCRIPTION("cpufreq driver for DBX500");