blob: 21d9898e000ca6b0727c9d6ed60a382b45d66365 [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 = {
Viresh Kumarae6b4272013-12-03 11:20:45 +053051 .flags = CPUFREQ_STICKY | CPUFREQ_CONST_LOOPS |
52 CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumar47150e92013-10-03 20:28:03 +053053 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053054 .target_index = dbx500_cpufreq_target,
Lee Jonesedb10c12012-12-10 16:25:38 +010055 .get = dbx500_cpufreq_getspeed,
56 .init = dbx500_cpufreq_init,
57 .name = "DBX500",
Viresh Kumar47150e92013-10-03 20:28:03 +053058 .attr = cpufreq_generic_attr,
Martin Persson7c1a70e2010-12-08 15:13:42 +010059};
60
Lee Jonesedb10c12012-12-10 16:25:38 +010061static int dbx500_cpufreq_probe(struct platform_device *pdev)
Ulf Hanssonb4689442012-10-10 13:42:24 +020062{
Ulf Hansson3e279962012-12-10 16:25:40 +010063 int i = 0;
Ulf Hanssonfdb44462012-10-10 13:42:25 +020064
Ulf Hansson3e279962012-12-10 16:25:40 +010065 freq_table = dev_get_platdata(&pdev->dev);
Ulf Hanssonfdb44462012-10-10 13:42:25 +020066 if (!freq_table) {
Lee Jonesedb10c12012-12-10 16:25:38 +010067 pr_err("dbx500-cpufreq: Failed to fetch cpufreq table\n");
Ulf Hanssonfdb44462012-10-10 13:42:25 +020068 return -ENODEV;
69 }
70
Ulf Hansson3e279962012-12-10 16:25:40 +010071 armss_clk = clk_get(&pdev->dev, "armss");
72 if (IS_ERR(armss_clk)) {
Jonas Aaberg9291cf92012-11-28 11:27:43 +010073 pr_err("dbx500-cpufreq: Failed to get armss clk\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010074 return PTR_ERR(armss_clk);
75 }
76
Jonas Aaberg9291cf92012-11-28 11:27:43 +010077 pr_info("dbx500-cpufreq: Available frequencies:\n");
Ulf Hansson3e279962012-12-10 16:25:40 +010078 while (freq_table[i].frequency != CPUFREQ_TABLE_END) {
79 pr_info(" %d Mhz\n", freq_table[i].frequency/1000);
80 i++;
81 }
82
Lee Jonesedb10c12012-12-10 16:25:38 +010083 return cpufreq_register_driver(&dbx500_cpufreq_driver);
Ulf Hanssonb4689442012-10-10 13:42:24 +020084}
85
Lee Jonesedb10c12012-12-10 16:25:38 +010086static struct platform_driver dbx500_cpufreq_plat_driver = {
Ulf Hanssonb4689442012-10-10 13:42:24 +020087 .driver = {
Lee Jonesedb10c12012-12-10 16:25:38 +010088 .name = "cpufreq-ux500",
Ulf Hanssonb4689442012-10-10 13:42:24 +020089 .owner = THIS_MODULE,
90 },
Lee Jonesedb10c12012-12-10 16:25:38 +010091 .probe = dbx500_cpufreq_probe,
Ulf Hanssonb4689442012-10-10 13:42:24 +020092};
93
Lee Jonesedb10c12012-12-10 16:25:38 +010094static int __init dbx500_cpufreq_register(void)
Martin Persson7c1a70e2010-12-08 15:13:42 +010095{
Lee Jonesedb10c12012-12-10 16:25:38 +010096 return platform_driver_register(&dbx500_cpufreq_plat_driver);
Martin Persson7c1a70e2010-12-08 15:13:42 +010097}
Lee Jonesedb10c12012-12-10 16:25:38 +010098device_initcall(dbx500_cpufreq_register);
Ulf Hanssonb4689442012-10-10 13:42:24 +020099
100MODULE_LICENSE("GPL v2");
Lee Jonesedb10c12012-12-10 16:25:38 +0100101MODULE_DESCRIPTION("cpufreq driver for DBX500");