blob: 0e67ab96321a7aa999802de779d6a65c16a468b0 [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{
Ulf Hansson78e30d12012-10-10 13:42:29 +020025 /* update armss clk frequency */
Viresh Kumard4019f02013-08-14 19:38:24 +053026 return clk_set_rate(armss_clk, freq_table[index].frequency * 1000);
Martin Persson7c1a70e2010-12-08 15:13:42 +010027}
28
Lee Jonesedb10c12012-12-10 16:25:38 +010029static unsigned int dbx500_cpufreq_getspeed(unsigned int cpu)
Martin Persson7c1a70e2010-12-08 15:13:42 +010030{
Ulf Hanssonfdb44462012-10-10 13:42:25 +020031 int i = 0;
Ulf Hansson78e30d12012-10-10 13:42:29 +020032 unsigned long freq = clk_get_rate(armss_clk) / 1000;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020033
Mats Fagerstromc7789662013-04-11 23:23:57 +020034 /* The value is rounded to closest frequency in the defined table. */
35 while (freq_table[i + 1].frequency != CPUFREQ_TABLE_END) {
36 if (freq < freq_table[i].frequency +
37 (freq_table[i + 1].frequency - freq_table[i].frequency) / 2)
Ulf Hanssonfdb44462012-10-10 13:42:25 +020038 return freq_table[i].frequency;
39 i++;
40 }
41
Mats Fagerstromc7789662013-04-11 23:23:57 +020042 return freq_table[i].frequency;
Martin Persson7c1a70e2010-12-08 15:13:42 +010043}
44
Paul Gortmaker27609842013-06-19 13:54:04 -040045static int dbx500_cpufreq_init(struct cpufreq_policy *policy)
Martin Persson7c1a70e2010-12-08 15:13:42 +010046{
Viresh Kumar2b3dc762013-10-03 20:29:12 +053047 return cpufreq_generic_init(policy, freq_table, 20 * 1000);
Martin Persson7c1a70e2010-12-08 15:13:42 +010048}
49
Lee Jonesedb10c12012-12-10 16:25:38 +010050static struct cpufreq_driver dbx500_cpufreq_driver = {
Fabio Baltieriec669122012-12-04 11:10:45 +010051 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS,
Viresh Kumar47150e92013-10-03 20:28:03 +053052 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053053 .target_index = dbx500_cpufreq_target,
Lee Jonesedb10c12012-12-10 16:25:38 +010054 .get = dbx500_cpufreq_getspeed,
55 .init = dbx500_cpufreq_init,
56 .name = "DBX500",
Viresh Kumar47150e92013-10-03 20:28:03 +053057 .attr = cpufreq_generic_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +010058};
59
Lee Jonesedb10c12012-12-10 16:25:38 +010060static int dbx500_cpufreq_probe(struct platform_device *pdev)
Ulf Hanssonb4689442012-10-10 13:42:24 +020061{
Ulf Hansson3e279962012-12-10 16:25:40 +010062 int i = 0;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020063
Ulf Hansson3e279962012-12-10 16:25:40 +010064 freq_table = dev_get_platdata(&pdev->dev);
Ulf Hanssonfdb44462012-10-10 13:42:25 +020065 if (!freq_table) {
Lee Jonesedb10c12012-12-10 16:25:38 +010066 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
Ulf Hanssonfdb44462012-10-10 13:42:25 +020067 return -ENODEV;
68 }
69
Ulf Hansson3e279962012-12-10 16:25:40 +010070 armss_clk = clk_get(&pdev->dev, "armss");
71 if (IS_ERR(armss_clk)) {
Jonas Aaberg9291cf92012-11-28 11:27:43 +010072 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010073 return PTR_ERR(armss_clk);
74 }
75
Jonas Aaberg9291cf92012-11-28 11:27:43 +010076 pr_info("dbx500-cpufreq: Available frequencies:\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010077 while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
78 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
79 i++;
80 }
81
Lee Jonesedb10c12012-12-10 16:25:38 +010082 return cpufreq_register_driver(&dbx500_cpufreq_driver);
Ulf Hanssonb4689442012-10-10 13:42:24 +020083}
84
Lee Jonesedb10c12012-12-10 16:25:38 +010085static struct platform_driver dbx500_cpufreq_plat_driver = {
Ulf Hanssonb4689442012-10-10 13:42:24 +020086 .driver = {
Lee Jonesedb10c12012-12-10 16:25:38 +010087 .name = "cpufreq-ux500",
Ulf Hanssonb4689442012-10-10 13:42:24 +020088 .owner = THIS_MODULE,
89 },
Lee Jonesedb10c12012-12-10 16:25:38 +010090 .probe = dbx500_cpufreq_probe,
Ulf Hanssonb4689442012-10-10 13:42:24 +020091};
92
Lee Jonesedb10c12012-12-10 16:25:38 +010093static int __init dbx500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +010094{
Lee Jonesedb10c12012-12-10 16:25:38 +010095 return platform_driver_register(&dbx500_cpufreq_plat_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +010096}
Lee Jonesedb10c12012-12-10 16:25:38 +010097device_initcall(dbx500_cpufreq_register);
Ulf Hanssonb4689442012-10-10 13:42:24 +020098
99MODULE_LICENSE("GPL v2");
Lee Jonesedb10c12012-12-10 16:25:38 +0100100MODULE_DESCRIPTION("cpufreq driver for DBX500");