blob: d54a27c9912188f472705ade364574e63a90780e [file] [log] [blame]
Sekhar Nori6601b802009-09-22 21:14:00 +05301/*
2 * CPU frequency scaling for DaVinci
3 *
4 * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
5 *
6 * Based on linux/arch/arm/plat-omap/cpu-omap.c. Original Copyright follows:
7 *
8 * Copyright (C) 2005 Nokia Corporation
9 * Written by Tony Lindgren <tony@atomide.com>
10 *
11 * Based on cpu-sa1110.c, Copyright (C) 2001 Russell King
12 *
13 * Copyright (C) 2007-2008 Texas Instruments, Inc.
14 * Updated to support OMAP3
15 * Rajendra Nayak <rnayak@ti.com>
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License version 2 as
19 * published by the Free Software Foundation.
20 */
21#include <linux/types.h>
22#include <linux/cpufreq.h>
23#include <linux/init.h>
24#include <linux/err.h>
25#include <linux/clk.h>
26#include <linux/platform_device.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040027#include <linux/export.h>
Sekhar Nori6601b802009-09-22 21:14:00 +053028
29#include <mach/hardware.h>
30#include <mach/cpufreq.h>
31#include <mach/common.h>
32
Sekhar Nori6601b802009-09-22 21:14:00 +053033struct davinci_cpufreq {
34 struct device *dev;
35 struct clk *armclk;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053036 struct clk *asyncclk;
37 unsigned long asyncrate;
Sekhar Nori6601b802009-09-22 21:14:00 +053038};
39static struct davinci_cpufreq cpufreq;
40
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053041static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
Sekhar Nori6601b802009-09-22 21:14:00 +053042{
Sekhar Nori6601b802009-09-22 21:14:00 +053043 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
44 struct clk *armclk = cpufreq.armclk;
Viresh Kumard4019f02013-08-14 19:38:24 +053045 unsigned int old_freq, new_freq;
46 int ret = 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053047
Viresh Kumar652ed952014-01-09 20:38:43 +053048 old_freq = policy->cur;
Viresh Kumard4019f02013-08-14 19:38:24 +053049 new_freq = pdata->freq_table[idx].frequency;
Sekhar Nori6601b802009-09-22 21:14:00 +053050
51 /* if moving to higher frequency, up the voltage beforehand */
Viresh Kumard4019f02013-08-14 19:38:24 +053052 if (pdata->set_voltage && new_freq > old_freq) {
Sekhar Norifca97b32010-07-20 16:46:48 +053053 ret = pdata->set_voltage(idx);
54 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053055 return ret;
Sekhar Norifca97b32010-07-20 16:46:48 +053056 }
Sekhar Nori6601b802009-09-22 21:14:00 +053057
Bartosz Golaszewskib4088172016-12-07 16:22:18 +010058 ret = clk_set_rate(armclk, new_freq * 1000);
Sekhar Norifca97b32010-07-20 16:46:48 +053059 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053060 return ret;
Sekhar Nori6601b802009-09-22 21:14:00 +053061
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053062 if (cpufreq.asyncclk) {
63 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
64 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053065 return ret;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053066 }
67
Sekhar Nori6601b802009-09-22 21:14:00 +053068 /* if moving to lower freq, lower the voltage after lowering freq */
Viresh Kumard4019f02013-08-14 19:38:24 +053069 if (pdata->set_voltage && new_freq < old_freq)
Sekhar Nori6601b802009-09-22 21:14:00 +053070 pdata->set_voltage(idx);
71
Viresh Kumard4019f02013-08-14 19:38:24 +053072 return 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053073}
74
Axel Lin079db592011-02-28 15:51:33 +053075static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +053076{
77 int result = 0;
78 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
79 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
80
81 if (policy->cpu != 0)
82 return -EINVAL;
83
Sekhar Nori13d5e272009-10-22 15:12:16 +053084 /* Finish platform specific initialization */
85 if (pdata->init) {
86 result = pdata->init();
87 if (result)
88 return result;
89 }
90
Viresh Kumar652ed952014-01-09 20:38:43 +053091 policy->clk = cpufreq.armclk;
92
Sekhar Nori6601b802009-09-22 21:14:00 +053093 /*
94 * Time measurement across the target() function yields ~1500-1800us
95 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030096 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +053097 * to pre/post change notification list.
98 */
Viresh Kumaraf8c4cf2013-10-03 20:29:11 +053099 return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
Sekhar Nori6601b802009-09-22 21:14:00 +0530100}
101
Sekhar Nori6601b802009-09-22 21:14:00 +0530102static struct cpufreq_driver davinci_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530103 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Viresh Kumar9d4de292016-06-03 10:58:52 +0530104 .verify = cpufreq_generic_frequency_table_verify,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530105 .target_index = davinci_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530106 .get = cpufreq_generic_get,
Sekhar Nori6601b802009-09-22 21:14:00 +0530107 .init = davinci_cpu_init,
Sekhar Nori6601b802009-09-22 21:14:00 +0530108 .name = "davinci",
Viresh Kumar39d0c362013-10-03 20:28:02 +0530109 .attr = cpufreq_generic_attr,
Sekhar Nori6601b802009-09-22 21:14:00 +0530110};
111
112static int __init davinci_cpufreq_probe(struct platform_device *pdev)
113{
114 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530115 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530116
117 if (!pdata)
118 return -EINVAL;
119 if (!pdata->freq_table)
120 return -EINVAL;
121
122 cpufreq.dev = &pdev->dev;
123
124 cpufreq.armclk = clk_get(NULL, "arm");
125 if (IS_ERR(cpufreq.armclk)) {
126 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
127 return PTR_ERR(cpufreq.armclk);
128 }
129
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530130 asyncclk = clk_get(cpufreq.dev, "async");
131 if (!IS_ERR(asyncclk)) {
132 cpufreq.asyncclk = asyncclk;
133 cpufreq.asyncrate = clk_get_rate(asyncclk);
134 }
135
Sekhar Nori6601b802009-09-22 21:14:00 +0530136 return cpufreq_register_driver(&davinci_driver);
137}
138
139static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
140{
141 clk_put(cpufreq.armclk);
142
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530143 if (cpufreq.asyncclk)
144 clk_put(cpufreq.asyncclk);
145
Sekhar Nori6601b802009-09-22 21:14:00 +0530146 return cpufreq_unregister_driver(&davinci_driver);
147}
148
149static struct platform_driver davinci_cpufreq_driver = {
150 .driver = {
151 .name = "cpufreq-davinci",
Sekhar Nori6601b802009-09-22 21:14:00 +0530152 },
153 .remove = __exit_p(davinci_cpufreq_remove),
154};
155
Shawn Guo3aa3e842012-04-26 09:45:39 +0800156int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530157{
158 return platform_driver_probe(&davinci_cpufreq_driver,
159 davinci_cpufreq_probe);
160}
Sekhar Nori6601b802009-09-22 21:14:00 +0530161