blob: e6206dd77d473122f39f7f3181da8d0173ea2c7b [file] [log] [blame]
Matt Wagantall44f672e2011-09-07 20:31:16 -07001/*
2 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#define pr_fmt(fmt) "%s: " fmt, __func__
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/delay.h>
20#include <linux/mutex.h>
21#include <linux/spinlock.h>
22#include <linux/errno.h>
23#include <linux/cpufreq.h>
24#include <linux/clk.h>
25
26#include <asm/cpu.h>
27
28#include <mach/board.h>
29#include <mach/msm_iomap.h>
Matt Wagantall095760f2011-10-18 15:21:36 -070030#include <mach/msm_bus.h>
31#include <mach/msm_bus_board.h>
Matt Wagantallb1be5662011-10-18 13:28:58 -070032#include <mach/rpm-regulator.h>
Matt Wagantall44f672e2011-09-07 20:31:16 -070033
34#include "acpuclock.h"
35
36#define REG_CLKSEL_0 (MSM_APCS_GLB_BASE + 0x08)
37#define REG_CLKDIV_0 (MSM_APCS_GLB_BASE + 0x0C)
38#define REG_CLKSEL_1 (MSM_APCS_GLB_BASE + 0x10)
39#define REG_CLKDIV_1 (MSM_APCS_GLB_BASE + 0x14)
40#define REG_CLKOUTSEL (MSM_APCS_GLB_BASE + 0x18)
41
Matt Wagantallb1be5662011-10-18 13:28:58 -070042#define MAX_VDD_CPU 1150000
43#define MAX_VDD_MEM 1150000
44
Matt Wagantall44f672e2011-09-07 20:31:16 -070045enum clk_src {
46 SRC_CXO,
47 SRC_PLL0,
48 SRC_PLL8,
49 SRC_PLL9,
50 NUM_SRC,
51};
52
53struct src_clock {
54 struct clk *clk;
55 const char *name;
56};
57
58static struct src_clock clocks[NUM_SRC] = {
59 [SRC_CXO].name = "cxo",
60 [SRC_PLL0].name = "pll0",
61 [SRC_PLL8].name = "pll8",
62 [SRC_PLL9].name = "pll9",
63};
64
65struct clkctl_acpu_speed {
66 bool use_for_scaling;
67 unsigned int khz;
68 int src;
69 unsigned int src_sel;
70 unsigned int src_div;
Matt Wagantallb1be5662011-10-18 13:28:58 -070071 unsigned int vdd_cpu;
72 unsigned int vdd_mem;
Matt Wagantall095760f2011-10-18 15:21:36 -070073 unsigned int bw_level;
Matt Wagantall44f672e2011-09-07 20:31:16 -070074};
75
76struct acpuclk_state {
77 struct mutex lock;
78 struct clkctl_acpu_speed *current_speed;
79};
80
81static struct acpuclk_state drv_state = {
82 .current_speed = &(struct clkctl_acpu_speed){ 0 },
83};
84
Matt Wagantall095760f2011-10-18 15:21:36 -070085/* Instantaneous bandwidth requests in MB/s. */
86#define BW_MBPS(_bw) \
87 { \
88 .vectors = &(struct msm_bus_vectors){ \
89 .src = MSM_BUS_MASTER_AMPSS_M0, \
90 .dst = MSM_BUS_SLAVE_EBI_CH0, \
91 .ib = (_bw) * 1000000UL, \
92 .ab = 0, \
93 }, \
94 .num_paths = 1, \
95 }
96static struct msm_bus_paths bw_level_tbl[] = {
97 [0] = BW_MBPS(152), /* At least 19 MHz on bus. */
98 [1] = BW_MBPS(368), /* At least 46 MHz on bus. */
99 [2] = BW_MBPS(552), /* At least 69 MHz on bus. */
100 [3] = BW_MBPS(736), /* At least 92 MHz on bus. */
101 [4] = BW_MBPS(1064), /* At least 133 MHz on bus. */
102};
103
104static struct msm_bus_scale_pdata bus_client_pdata = {
105 .usecase = bw_level_tbl,
106 .num_usecases = ARRAY_SIZE(bw_level_tbl),
107 .active_only = 1,
108 .name = "acpuclock",
109};
110
111static uint32_t bus_perf_client;
112
Matt Wagantall44f672e2011-09-07 20:31:16 -0700113static struct clkctl_acpu_speed acpu_freq_tbl[] = {
Matt Wagantall095760f2011-10-18 15:21:36 -0700114 { 0, 19200, SRC_CXO, 0, 0, 950000, 1050000, 0 },
115 { 1, 138000, SRC_PLL0, 6, 1, 950000, 1050000, 2 },
116 { 1, 276000, SRC_PLL0, 6, 0, 1050000, 1050000, 2 },
117 { 1, 384000, SRC_PLL8, 3, 0, 1150000, 1150000, 4 },
118 { 1, 440000, SRC_PLL9, 2, 0, 1150000, 1150000, 4 },
Matt Wagantall44f672e2011-09-07 20:31:16 -0700119 { 0 }
120};
121
122static void select_clk_source_div(struct clkctl_acpu_speed *s)
123{
124 static void * __iomem const sel_reg[] = {REG_CLKSEL_0, REG_CLKSEL_1};
125 static void * __iomem const div_reg[] = {REG_CLKDIV_0, REG_CLKDIV_1};
126 uint32_t next_bank;
127
128 next_bank = !(readl_relaxed(REG_CLKOUTSEL) & 1);
129 writel_relaxed(s->src_sel, sel_reg[next_bank]);
130 writel_relaxed(s->src_div, div_reg[next_bank]);
131 writel_relaxed(next_bank, REG_CLKOUTSEL);
132
133 /* Wait for switch to complete. */
134 mb();
135 udelay(1);
136}
137
Matt Wagantall095760f2011-10-18 15:21:36 -0700138/* Update the bus bandwidth request. */
139static void set_bus_bw(unsigned int bw)
140{
141 int ret;
142
143 /* Bounds check. */
144 if (bw >= ARRAY_SIZE(bw_level_tbl)) {
145 pr_err("invalid bandwidth request (%d)\n", bw);
146 return;
147 }
148
149 /* Update bandwidth if request has changed. This may sleep. */
150 ret = msm_bus_scale_client_update_request(bus_perf_client, bw);
151 if (ret)
152 pr_err("bandwidth request failed (%d)\n", ret);
153
154 return;
155}
156
Matt Wagantallb1be5662011-10-18 13:28:58 -0700157/* Apply any per-cpu voltage increases. */
158static int increase_vdd(unsigned int vdd_cpu, unsigned int vdd_mem)
159{
160 int rc = 0;
161
162 /*
163 * Increase vdd_mem active-set before vdd_cpu.
164 * vdd_mem should be >= vdd_cpu.
165 */
166 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8018_L9, RPM_VREG_VOTER1,
167 vdd_mem, MAX_VDD_MEM, 0);
168 if (rc) {
169 pr_err("vdd_mem increase failed (%d)\n", rc);
170 return rc;
171 }
172
173 rc = rpm_vreg_set_voltage(RPM_VREG_ID_PM8018_S1, RPM_VREG_VOTER1,
174 vdd_cpu, MAX_VDD_CPU, 0);
175 if (rc)
176 pr_err("vdd_cpu increase failed (%d)\n", rc);
177
178 return rc;
179}
180
181/* Apply any per-cpu voltage decreases. */
182static void decrease_vdd(unsigned int vdd_cpu, unsigned int vdd_mem)
183{
184 int ret;
185
186 /* Update CPU voltage. */
187 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8018_S1, RPM_VREG_VOTER1,
188 vdd_cpu, MAX_VDD_CPU, 0);
189 if (ret) {
190 pr_err("vdd_cpu decrease failed (%d)\n", ret);
191 return;
192 }
193
194 /*
195 * Decrease vdd_mem active-set after vdd_cpu.
196 * vdd_mem should be >= vdd_cpu.
197 */
198 ret = rpm_vreg_set_voltage(RPM_VREG_ID_PM8018_L9, RPM_VREG_VOTER1,
199 vdd_mem, MAX_VDD_MEM, 0);
200 if (ret)
201 pr_err("vdd_mem decrease failed (%d)\n", ret);
202}
203
Matt Wagantall44f672e2011-09-07 20:31:16 -0700204static int acpuclk_9615_set_rate(int cpu, unsigned long rate,
205 enum setrate_reason reason)
206{
207 struct clkctl_acpu_speed *tgt_s, *strt_s;
208 int rc = 0;
209
210 if (reason == SETRATE_CPUFREQ)
211 mutex_lock(&drv_state.lock);
212
213 strt_s = drv_state.current_speed;
214
215 /* Return early if rate didn't change. */
216 if (rate == strt_s->khz)
217 goto out;
218
219 /* Find target frequency. */
220 for (tgt_s = acpu_freq_tbl; tgt_s->khz != 0; tgt_s++)
221 if (tgt_s->khz == rate)
222 break;
223 if (tgt_s->khz == 0) {
224 rc = -EINVAL;
225 goto out;
226 }
227
Matt Wagantallb1be5662011-10-18 13:28:58 -0700228 /* Increase VDD levels if needed. */
229 if ((reason == SETRATE_CPUFREQ || reason == SETRATE_INIT)
230 && (tgt_s->khz > strt_s->khz)) {
231 rc = increase_vdd(tgt_s->vdd_cpu, tgt_s->vdd_mem);
232 if (rc)
233 goto out;
234 }
235
Matt Wagantallf5d64072011-10-13 14:15:19 -0700236 pr_debug("Switching from CPU rate %u KHz -> %u KHz\n",
Matt Wagantall44f672e2011-09-07 20:31:16 -0700237 strt_s->khz, tgt_s->khz);
238
239 /* Switch CPU speed. */
240 clk_enable(clocks[tgt_s->src].clk);
241 select_clk_source_div(tgt_s);
242 clk_disable(clocks[strt_s->src].clk);
243
244 drv_state.current_speed = tgt_s;
Matt Wagantallf5d64072011-10-13 14:15:19 -0700245 pr_debug("CPU speed change complete\n");
Matt Wagantall44f672e2011-09-07 20:31:16 -0700246
Matt Wagantallb1be5662011-10-18 13:28:58 -0700247 /* Nothing else to do for SWFI or power-collapse. */
248 if (reason == SETRATE_SWFI || reason == SETRATE_PC)
249 goto out;
250
Matt Wagantall095760f2011-10-18 15:21:36 -0700251 /* Update bus bandwith request. */
252 set_bus_bw(tgt_s->bw_level);
253
Matt Wagantallb1be5662011-10-18 13:28:58 -0700254 /* Drop VDD levels if we can. */
255 if (tgt_s->khz < strt_s->khz)
256 decrease_vdd(tgt_s->vdd_cpu, tgt_s->vdd_mem);
257
Matt Wagantall44f672e2011-09-07 20:31:16 -0700258out:
259 if (reason == SETRATE_CPUFREQ)
260 mutex_unlock(&drv_state.lock);
261 return rc;
262}
263
264static unsigned long acpuclk_9615_get_rate(int cpu)
265{
266 return drv_state.current_speed->khz;
267}
268
269#ifdef CONFIG_CPU_FREQ_MSM
270static struct cpufreq_frequency_table freq_table[30];
271
272static void __init cpufreq_table_init(void)
273{
274 int i, freq_cnt = 0;
275
276 /* Construct the freq_table tables from acpu_freq_tbl. */
277 for (i = 0; acpu_freq_tbl[i].khz != 0
278 && freq_cnt < ARRAY_SIZE(freq_table); i++) {
279 if (acpu_freq_tbl[i].use_for_scaling) {
280 freq_table[freq_cnt].index = freq_cnt;
281 freq_table[freq_cnt].frequency
282 = acpu_freq_tbl[i].khz;
283 freq_cnt++;
284 }
285 }
286 /* freq_table not big enough to store all usable freqs. */
287 BUG_ON(acpu_freq_tbl[i].khz != 0);
288
289 freq_table[freq_cnt].index = freq_cnt;
290 freq_table[freq_cnt].frequency = CPUFREQ_TABLE_END;
291
292 pr_info("CPU: %d scaling frequencies supported.\n", freq_cnt);
293
294 /* Register table with CPUFreq. */
295 cpufreq_frequency_table_get_attr(freq_table, smp_processor_id());
296}
297#else
298static void __init cpufreq_table_init(void) {}
299#endif
300
301static struct acpuclk_data acpuclk_9615_data = {
302 .set_rate = acpuclk_9615_set_rate,
303 .get_rate = acpuclk_9615_get_rate,
304 .power_collapse_khz = 19200,
305 .wait_for_irq_khz = 19200,
306};
307
308static int __init acpuclk_9615_init(struct acpuclk_soc_data *soc_data)
309{
310 unsigned long max_cpu_khz = 0;
311 int i;
312
313 mutex_init(&drv_state.lock);
Matt Wagantall095760f2011-10-18 15:21:36 -0700314
315 bus_perf_client = msm_bus_scale_register_client(&bus_client_pdata);
316 if (!bus_perf_client) {
317 pr_err("Unable to register bus client\n");
318 BUG();
319 }
320
Matt Wagantall44f672e2011-09-07 20:31:16 -0700321 for (i = 0; i < NUM_SRC; i++) {
322 if (clocks[i].name) {
323 clocks[i].clk = clk_get_sys(NULL, clocks[i].name);
324 BUG_ON(IS_ERR(clocks[i].clk));
325 }
326 }
327
328 /* Improve boot time by ramping up CPU immediately. */
329 for (i = 0; acpu_freq_tbl[i].khz != 0; i++)
330 max_cpu_khz = acpu_freq_tbl[i].khz;
331 acpuclk_9615_set_rate(smp_processor_id(), max_cpu_khz, SETRATE_INIT);
332
333 acpuclk_register(&acpuclk_9615_data);
334 cpufreq_table_init();
335
336 return 0;
337}
338
339struct acpuclk_soc_data acpuclk_9615_soc_data __initdata = {
340 .init = acpuclk_9615_init,
341};