blob: 238b16976be1c0204a8243eb499f1955c8032c76 [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 int dbx500_cpufreq_target(struct cpufreq_policy *policy,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053023 unsigned int index)
Martin Persson7c1a70e2010-12-08 15:13:42 +010024{
25 struct cpufreq_freqs freqs;
Jonas Aaberg9291cf92012-11-28 11:27:43 +010026 int ret;
Martin Persson7c1a70e2010-12-08 15:13:42 +010027
Martin Persson7c1a70e2010-12-08 15:13:42 +010028 freqs.old = policy->cur;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053029 freqs.new = freq_table[index].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010030
Linus Walleij72b2fd52011-05-15 19:19:51 +020031 /* pre-change notification */
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053032 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010033
Ulf Hansson78e30d12012-10-10 13:42:29 +020034 /* update armss clk frequency */
Jonas Aaberg9291cf92012-11-28 11:27:43 +010035 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 Kumar803c1262013-06-19 11:18:20 +053040 freqs.new = freqs.old;
Martin Persson7c1a70e2010-12-08 15:13:42 +010041 }
42
Linus Walleij72b2fd52011-05-15 19:19:51 +020043 /* post change notification */
Viresh Kumarb43a7ff2013-03-24 11:56:43 +053044 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Martin Persson7c1a70e2010-12-08 15:13:42 +010045
Viresh Kumar803c1262013-06-19 11:18:20 +053046 return ret;
Martin Persson7c1a70e2010-12-08 15:13:42 +010047}
48
Lee Jonesedb10c12012-12-10 16:25:38 +010049static unsigned int dbx500_cpufreq_getspeed(unsigned int cpu)
Martin Persson7c1a70e2010-12-08 15:13:42 +010050{
Ulf Hanssonfdb44462012-10-10 13:42:25 +020051 int i = 0;
Ulf Hansson78e30d12012-10-10 13:42:29 +020052 unsigned long freq = clk_get_rate(armss_clk) / 1000;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020053
Mats Fagerstromc7789662013-04-11 23:23:57 +020054 /* 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 Hanssonfdb44462012-10-10 13:42:25 +020058 return freq_table[i].frequency;
59 i++;
60 }
61
Mats Fagerstromc7789662013-04-11 23:23:57 +020062 return freq_table[i].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010063}
64
Paul Gortmaker27609842013-06-19 13:54:04 -040065static int dbx500_cpufreq_init(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010066{
Viresh Kumar2b3dc762013-10-03 20:29:12 +053067 return cpufreq_generic_init(policy, freq_table, 20 * 1000);
Martin Persson7c1a70e2010-12-08 15:13:42 +010068}
69
Lee Jonesedb10c12012-12-10 16:25:38 +010070static struct cpufreq_driver dbx500_cpufreq_driver = {
Fabio Baltieriec669122012-12-04 11:10:45 +010071 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
Viresh Kumar47150e92013-10-03 20:28:03 +053072 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053073 .target_index = dbx500_cpufreq_target,
Lee Jonesedb10c12012-12-10 16:25:38 +010074 .get = dbx500_cpufreq_getspeed,
75 .init = dbx500_cpufreq_init,
76 .name = "DBX500",
Viresh Kumar47150e92013-10-03 20:28:03 +053077 .attr = cpufreq_generic_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +010078};
79
Lee Jonesedb10c12012-12-10 16:25:38 +010080static int dbx500_cpufreq_probe(struct platform_device *pdev)
Ulf Hanssonb4689442012-10-10 13:42:24 +020081{
Ulf Hansson3e279962012-12-10 16:25:40 +010082 int i = 0;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020083
Ulf Hansson3e279962012-12-10 16:25:40 +010084 freq_table = dev_get_platdata(&pdev->dev);
Ulf Hanssonfdb44462012-10-10 13:42:25 +020085 if (!freq_table) {
Lee Jonesedb10c12012-12-10 16:25:38 +010086 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
Ulf Hanssonfdb44462012-10-10 13:42:25 +020087 return -ENODEV;
88 }
89
Ulf Hansson3e279962012-12-10 16:25:40 +010090 armss_clk = clk_get(&pdev->dev, "armss");
91 if (IS_ERR(armss_clk)) {
Jonas Aaberg9291cf92012-11-28 11:27:43 +010092 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010093 return PTR_ERR(armss_clk);
94 }
95
Jonas Aaberg9291cf92012-11-28 11:27:43 +010096 pr_info("dbx500-cpufreq: Available frequencies:\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010097 while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
98 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
99 i++;
100 }
101
Lee Jonesedb10c12012-12-10 16:25:38 +0100102 return cpufreq_register_driver(&dbx500_cpufreq_driver);
Ulf Hanssonb4689442012-10-10 13:42:24 +0200103}
104
Lee Jonesedb10c12012-12-10 16:25:38 +0100105static struct platform_driver dbx500_cpufreq_plat_driver = {
Ulf Hanssonb4689442012-10-10 13:42:24 +0200106 .driver = {
Lee Jonesedb10c12012-12-10 16:25:38 +0100107 .name = "cpufreq-ux500",
Ulf Hanssonb4689442012-10-10 13:42:24 +0200108 .owner = THIS_MODULE,
109 },
Lee Jonesedb10c12012-12-10 16:25:38 +0100110 .probe = dbx500_cpufreq_probe,
Ulf Hanssonb4689442012-10-10 13:42:24 +0200111};
112
Lee Jonesedb10c12012-12-10 16:25:38 +0100113static int __init dbx500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +0100114{
Lee Jonesedb10c12012-12-10 16:25:38 +0100115 return platform_driver_register(&dbx500_cpufreq_plat_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +0100116}
Lee Jonesedb10c12012-12-10 16:25:38 +0100117device_initcall(dbx500_cpufreq_register);
Ulf Hanssonb4689442012-10-10 13:42:24 +0200118
119MODULE_LICENSE("GPL v2");
Lee Jonesedb10c12012-12-10 16:25:38 +0100120MODULE_DESCRIPTION("cpufreq driver for DBX500");