blob: 28a16dc6e02ec8786e529ad176185846efcaf735 [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
41static int davinci_verify_speed(struct cpufreq_policy *policy)
42{
43 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
44 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
45 struct clk *armclk = cpufreq.armclk;
46
47 if (freq_table)
48 return cpufreq_frequency_table_verify(policy, freq_table);
49
50 if (policy->cpu)
51 return -EINVAL;
52
Viresh Kumarbe49e342013-10-02 14:13:19 +053053 cpufreq_verify_within_cpu_limits(policy);
Sekhar Nori6601b802009-09-22 21:14:00 +053054 policy->min = clk_round_rate(armclk, policy->min * 1000) / 1000;
55 policy->max = clk_round_rate(armclk, policy->max * 1000) / 1000;
56 cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
57 policy->cpuinfo.max_freq);
58 return 0;
59}
60
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053061static int davinci_target(struct cpufreq_policy *policy, unsigned int idx)
Sekhar Nori6601b802009-09-22 21:14:00 +053062{
Sekhar Nori6601b802009-09-22 21:14:00 +053063 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
64 struct clk *armclk = cpufreq.armclk;
Viresh Kumard4019f02013-08-14 19:38:24 +053065 unsigned int old_freq, new_freq;
66 int ret = 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053067
Viresh Kumar652ed952014-01-09 20:38:43 +053068 old_freq = policy->cur;
Viresh Kumard4019f02013-08-14 19:38:24 +053069 new_freq = pdata->freq_table[idx].frequency;
Sekhar Nori6601b802009-09-22 21:14:00 +053070
71 /* if moving to higher frequency, up the voltage beforehand */
Viresh Kumard4019f02013-08-14 19:38:24 +053072 if (pdata->set_voltage && new_freq > old_freq) {
Sekhar Norifca97b32010-07-20 16:46:48 +053073 ret = pdata->set_voltage(idx);
74 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053075 return ret;
Sekhar Norifca97b32010-07-20 16:46:48 +053076 }
Sekhar Nori6601b802009-09-22 21:14:00 +053077
78 ret = clk_set_rate(armclk, idx);
Sekhar Norifca97b32010-07-20 16:46:48 +053079 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053080 return ret;
Sekhar Nori6601b802009-09-22 21:14:00 +053081
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053082 if (cpufreq.asyncclk) {
83 ret = clk_set_rate(cpufreq.asyncclk, cpufreq.asyncrate);
84 if (ret)
Viresh Kumard4019f02013-08-14 19:38:24 +053085 return ret;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +053086 }
87
Sekhar Nori6601b802009-09-22 21:14:00 +053088 /* if moving to lower freq, lower the voltage after lowering freq */
Viresh Kumard4019f02013-08-14 19:38:24 +053089 if (pdata->set_voltage && new_freq < old_freq)
Sekhar Nori6601b802009-09-22 21:14:00 +053090 pdata->set_voltage(idx);
91
Viresh Kumard4019f02013-08-14 19:38:24 +053092 return 0;
Sekhar Nori6601b802009-09-22 21:14:00 +053093}
94
Axel Lin079db592011-02-28 15:51:33 +053095static int davinci_cpu_init(struct cpufreq_policy *policy)
Sekhar Nori6601b802009-09-22 21:14:00 +053096{
97 int result = 0;
98 struct davinci_cpufreq_config *pdata = cpufreq.dev->platform_data;
99 struct cpufreq_frequency_table *freq_table = pdata->freq_table;
100
101 if (policy->cpu != 0)
102 return -EINVAL;
103
Sekhar Nori13d5e272009-10-22 15:12:16 +0530104 /* Finish platform specific initialization */
105 if (pdata->init) {
106 result = pdata->init();
107 if (result)
108 return result;
109 }
110
Viresh Kumar652ed952014-01-09 20:38:43 +0530111 policy->clk = cpufreq.armclk;
112
Sekhar Nori6601b802009-09-22 21:14:00 +0530113 /*
114 * Time measurement across the target() function yields ~1500-1800us
115 * time taken with no drivers on notification list.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300116 * Setting the latency to 2000 us to accommodate addition of drivers
Sekhar Nori6601b802009-09-22 21:14:00 +0530117 * to pre/post change notification list.
118 */
Viresh Kumaraf8c4cf2013-10-03 20:29:11 +0530119 return cpufreq_generic_init(policy, freq_table, 2000 * 1000);
Sekhar Nori6601b802009-09-22 21:14:00 +0530120}
121
Sekhar Nori6601b802009-09-22 21:14:00 +0530122static struct cpufreq_driver davinci_driver = {
Viresh Kumarae6b4272013-12-03 11:20:45 +0530123 .flags = CPUFREQ_STICKY | CPUFREQ_NEED_INITIAL_FREQ_CHECK,
Sekhar Nori6601b802009-09-22 21:14:00 +0530124 .verify = davinci_verify_speed,
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530125 .target_index = davinci_target,
Viresh Kumar652ed952014-01-09 20:38:43 +0530126 .get = cpufreq_generic_get,
Sekhar Nori6601b802009-09-22 21:14:00 +0530127 .init = davinci_cpu_init,
Sekhar Nori6601b802009-09-22 21:14:00 +0530128 .name = "davinci",
Viresh Kumar39d0c362013-10-03 20:28:02 +0530129 .attr = cpufreq_generic_attr,
Sekhar Nori6601b802009-09-22 21:14:00 +0530130};
131
132static int __init davinci_cpufreq_probe(struct platform_device *pdev)
133{
134 struct davinci_cpufreq_config *pdata = pdev->dev.platform_data;
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530135 struct clk *asyncclk;
Sekhar Nori6601b802009-09-22 21:14:00 +0530136
137 if (!pdata)
138 return -EINVAL;
139 if (!pdata->freq_table)
140 return -EINVAL;
141
142 cpufreq.dev = &pdev->dev;
143
144 cpufreq.armclk = clk_get(NULL, "arm");
145 if (IS_ERR(cpufreq.armclk)) {
146 dev_err(cpufreq.dev, "Unable to get ARM clock\n");
147 return PTR_ERR(cpufreq.armclk);
148 }
149
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530150 asyncclk = clk_get(cpufreq.dev, "async");
151 if (!IS_ERR(asyncclk)) {
152 cpufreq.asyncclk = asyncclk;
153 cpufreq.asyncrate = clk_get_rate(asyncclk);
154 }
155
Sekhar Nori6601b802009-09-22 21:14:00 +0530156 return cpufreq_register_driver(&davinci_driver);
157}
158
159static int __exit davinci_cpufreq_remove(struct platform_device *pdev)
160{
161 clk_put(cpufreq.armclk);
162
Sekhar Nori30a2c5d2010-07-20 16:46:50 +0530163 if (cpufreq.asyncclk)
164 clk_put(cpufreq.asyncclk);
165
Sekhar Nori6601b802009-09-22 21:14:00 +0530166 return cpufreq_unregister_driver(&davinci_driver);
167}
168
169static struct platform_driver davinci_cpufreq_driver = {
170 .driver = {
171 .name = "cpufreq-davinci",
172 .owner = THIS_MODULE,
173 },
174 .remove = __exit_p(davinci_cpufreq_remove),
175};
176
Shawn Guo3aa3e842012-04-26 09:45:39 +0800177int __init davinci_cpufreq_init(void)
Sekhar Nori6601b802009-09-22 21:14:00 +0530178{
179 return platform_driver_probe(&davinci_cpufreq_driver,
180 davinci_cpufreq_probe);
181}
Sekhar Nori6601b802009-09-22 21:14:00 +0530182