blob: 7c2c556e16e5dcffe8286a4def1bc70cb613b7b7 [file] [log] [blame]
Matt Wagantalld1af38e2011-08-06 01:38:02 -07001/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 * MSM architecture clock driver
3 *
4 * Copyright (C) 2007 Google, Inc.
Pankaj Kumarc9136b32012-01-02 18:46:13 +05305 * Copyright (c) 2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07006 * Author: San Mehat <san@android.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/version.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/errno.h>
23#include <linux/string.h>
24#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/cpufreq.h>
27#include <linux/mutex.h>
28#include <linux/io.h>
29#include <linux/sort.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <mach/board.h>
31#include <mach/msm_iomap.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/socinfo.h>
Taniya Dasc43e6872012-03-21 16:41:14 +053033#include <asm/mach-types.h>
34#include <asm/cpu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#include "smd_private.h"
37#include "acpuclock.h"
38
39#define A11S_CLK_CNTL_ADDR (MSM_CSR_BASE + 0x100)
40#define A11S_CLK_SEL_ADDR (MSM_CSR_BASE + 0x104)
41#define A11S_VDD_SVS_PLEVEL_ADDR (MSM_CSR_BASE + 0x124)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070043
Matt Wagantall6d9ebee2011-08-26 12:15:24 -070044#define POWER_COLLAPSE_KHZ 19200
45
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070046/* Max CPU frequency allowed by hardware while in standby waiting for an irq. */
47#define MAX_WAIT_FOR_IRQ_KHZ 128000
48
Pankaj Kumar3912c982011-12-07 16:59:03 +053049/**
50 * enum - For acpuclock PLL IDs
51 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052enum {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053 ACPU_PLL_0 = 0,
54 ACPU_PLL_1,
55 ACPU_PLL_2,
56 ACPU_PLL_3,
57 ACPU_PLL_4,
Pankaj Kumar0249bed2012-03-08 15:20:54 +053058 ACPU_PLL_TCXO,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070059 ACPU_PLL_END,
60};
61
Pankaj Kumar3912c982011-12-07 16:59:03 +053062struct acpu_clk_src {
63 struct clk *clk;
64 const char *name;
65};
66
67static struct acpu_clk_src pll_clk[ACPU_PLL_END] = {
68 [ACPU_PLL_0] = { .name = "pll0_clk" },
69 [ACPU_PLL_1] = { .name = "pll1_clk" },
70 [ACPU_PLL_2] = { .name = "pll2_clk" },
71 [ACPU_PLL_4] = { .name = "pll4_clk" },
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072};
73
74struct clock_state {
75 struct clkctl_acpu_speed *current_speed;
76 struct mutex lock;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070077 uint32_t max_speed_delta_khz;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078 struct clk *ebi1_clk;
79};
80
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081struct clkctl_acpu_speed {
82 unsigned int use_for_scaling;
83 unsigned int a11clk_khz;
84 int pll;
85 unsigned int a11clk_src_sel;
86 unsigned int a11clk_src_div;
87 unsigned int ahbclk_khz;
88 unsigned int ahbclk_div;
89 int vdd;
90 unsigned int axiclk_khz;
Taniya Dasc43e6872012-03-21 16:41:14 +053091 unsigned long lpj; /* loops_per_jiffy */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092 /* Pointers in acpu_freq_tbl[] for max up/down steppings. */
93 struct clkctl_acpu_speed *down[ACPU_PLL_END];
94 struct clkctl_acpu_speed *up[ACPU_PLL_END];
95};
96
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097static struct clock_state drv_state = { 0 };
98static struct clkctl_acpu_speed *acpu_freq_tbl;
99
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100/*
101 * ACPU freq tables used for different PLLs frequency combinations. The
102 * correct table is selected during init.
103 *
104 * Table stepping up/down entries are calculated during boot to choose the
105 * largest frequency jump that's less than max_speed_delta_khz on each PLL.
106 */
107
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530108/* 7627 with GSM capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_0[] = {
110 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
111 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
112 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
113 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 61440 },
114 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530115 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
116 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
117 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
118 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530119 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120};
121
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530122/* 7627 with CDMA capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700123static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_0[] = {
124 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
125 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
126 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
127 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
128 { 0, 200000, ACPU_PLL_2, 2, 5, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530129 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
130 { 0, 400000, ACPU_PLL_2, 2, 2, 133333, 2, 5, 160000 },
131 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
132 { 1, 600000, ACPU_PLL_2, 2, 1, 200000, 2, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530133 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134};
135
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530136/* 7627 with GSM capable modem - PLL2 @ 800 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_800_pll4_0[] = {
138 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 30720 },
139 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 61440 },
140 { 1, 122880, ACPU_PLL_1, 1, 1, 61440, 1, 3, 61440 },
141 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 61440 },
142 { 1, 245760, ACPU_PLL_1, 1, 0, 122880, 1, 4, 61440 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530143 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
144 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
145 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
146 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148};
149
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530150/* 7627 with CDMA capable modem - PLL2 @ 800 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_800_pll4_0[] = {
152 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 19200, 0, 0, 24576 },
153 { 1, 98304, ACPU_PLL_1, 1, 1, 98304, 0, 3, 49152 },
154 { 0, 120000, ACPU_PLL_0, 4, 7, 60000, 1, 3, 49152 },
155 { 1, 196608, ACPU_PLL_1, 1, 0, 65536, 2, 4, 98304 },
156 { 0, 200000, ACPU_PLL_2, 2, 3, 66667, 2, 4, 98304 },
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530157 { 1, 320000, ACPU_PLL_0, 4, 2, 160000, 1, 5, 160000 },
158 { 0, 400000, ACPU_PLL_2, 2, 1, 133333, 2, 5, 160000 },
159 { 1, 480000, ACPU_PLL_0, 4, 1, 160000, 2, 6, 160000 },
160 { 1, 800000, ACPU_PLL_2, 2, 0, 200000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530161 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162};
163
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530164/* 7627a PLL2 @ 1200MHz with GSM capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530166 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
167 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
168 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
169 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530170 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530171 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
172 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
173 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530174 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530175 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530176 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177};
178
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530179/* 7627a PLL2 @ 1200MHz with CDMA capable modem */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_800[] = {
Trilok Soni7d6c8652011-07-14 15:35:07 +0530181 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
182 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
183 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
184 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
Trilok Soniabb750b2011-07-13 16:47:18 +0530185 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
186 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
187 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
188 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530189 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni7d6c8652011-07-14 15:35:07 +0530190 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530191 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192};
193
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530194/* 7627aa PLL4 @ 1008MHz with GSM capable modem */
Trilok Sonif597e242011-06-06 12:37:16 +0530195static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_1008[] = {
196 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
197 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
198 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
199 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530200 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Sonif597e242011-06-06 12:37:16 +0530201 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
202 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530203 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
204 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Sonif597e242011-06-06 12:37:16 +0530205 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530206 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Sonif597e242011-06-06 12:37:16 +0530207};
208
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530209/* 7627aa PLL4 @ 1008MHz with CDMA capable modem */
Trilok Sonid7b05e52011-08-17 18:09:08 +0530210static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_1008[] = {
211 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
212 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
213 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
214 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530215 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Sonid7b05e52011-08-17 18:09:08 +0530216 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
217 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530218 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
219 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Sonid7b05e52011-08-17 18:09:08 +0530220 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530221 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Sonid7b05e52011-08-17 18:09:08 +0530222};
223
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530224/* 8625 PLL4 @ 1209MHz with GSM capable modem */
225static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_1209[] = {
226 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
227 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
228 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
229 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
230 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
231 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530232 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
233 { 0, 604800, ACPU_PLL_4, 6, 1, 75600, 3, 6, 160000 },
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530234 { 1, 1209600, ACPU_PLL_4, 6, 0, 151200, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530235 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530236};
237
238/* 8625 PLL4 @ 1209MHz with CDMA capable modem */
239static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_1209[] = {
240 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
241 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
242 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
243 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
244 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
245 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530246 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
247 { 0, 604800, ACPU_PLL_4, 6, 1, 75600, 3, 6, 160000 },
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530248 { 1, 1209600, ACPU_PLL_4, 6, 0, 151200, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530249 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530250};
251
Trilok Soni48631722012-05-17 20:56:42 +0530252/* 8625 PLL4 @ 1152MHz with GSM capable modem */
253static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_pll4_1152[] = {
254 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
255 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
256 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
257 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
258 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
259 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
260 { 0, 576000, ACPU_PLL_4, 6, 1, 72000, 3, 6, 160000 },
261 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
262 { 1, 1152000, ACPU_PLL_4, 6, 0, 144000, 3, 7, 200000},
263 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
264};
265
266/* 8625 PLL4 @ 1115MHz with CDMA capable modem */
267static struct clkctl_acpu_speed pll0_960_pll1_196_pll2_1200_pll4_1152[] = {
268 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
269 { 0, 65536, ACPU_PLL_1, 1, 3, 8192, 3, 1, 49152 },
270 { 1, 98304, ACPU_PLL_1, 1, 1, 12288, 3, 2, 49152 },
271 { 1, 196608, ACPU_PLL_1, 1, 0, 24576, 3, 3, 98304 },
272 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
273 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
274 { 0, 576000, ACPU_PLL_4, 6, 1, 72000, 3, 6, 160000 },
275 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
276 { 1, 1152000, ACPU_PLL_4, 6, 0, 144000, 3, 7, 200000},
277 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
278};
279
280
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530281/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530282static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_25a[] = {
Trilok Soni54d35c42011-07-14 17:47:50 +0530283 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
284 { 0, 61440, ACPU_PLL_1, 1, 3, 7680, 3, 1, 61440 },
285 { 1, 122880, ACPU_PLL_1, 1, 1, 15360, 3, 2, 61440 },
286 { 1, 245760, ACPU_PLL_1, 1, 0, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530287 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530288 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530289 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530290 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
291 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530292 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni54d35c42011-07-14 17:47:50 +0530293};
294
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530295/* 7627a PLL2 @ 1200MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530296static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_800[] = {
297 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
298 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
299 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
300 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530301 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530302 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
303 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
304 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530305 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530306 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530307 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530308};
309
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530310/* 7627a PLL2 @ 1200MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530311static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_800[] = {
312 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
313 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
314 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
315 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
316 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
317 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
318 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
319 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530320 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530321 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530322 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530323};
324
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530325/* 7627aa PLL4 @ 1008MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530326static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_1008[] = {
327 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
328 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
329 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
330 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530331 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530332 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
333 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530334 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
335 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530336 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530337 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530338};
339
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530340/* 7627aa PLL4 @ 1008MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530341static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_1008[] = {
342 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
343 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
344 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
345 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530346 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530347 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
348 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530349 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
350 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530351 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530352 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530353};
354
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530355/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530356static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_25a[] = {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530357 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
358 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
359 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
360 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530361 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530362 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530363 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530364 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
365 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530366 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, {0, 0, 0, 0}, {0, 0, 0, 0} }
Trilok Soni9bb022c2011-10-31 18:25:19 +0530367};
368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700369#define PLL_CONFIG(m0, m1, m2, m4) { \
Pankaj Kumar3912c982011-12-07 16:59:03 +0530370 m0, m1, m2, m4, \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700371 pll0_##m0##_pll1_##m1##_pll2_##m2##_pll4_##m4 \
372}
373
374struct pll_freq_tbl_map {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530375 unsigned int pll0_rate;
376 unsigned int pll1_rate;
377 unsigned int pll2_rate;
378 unsigned int pll4_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 struct clkctl_acpu_speed *tbl;
380};
381
382static struct pll_freq_tbl_map acpu_freq_tbl_list[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700383 PLL_CONFIG(960, 196, 1200, 0),
384 PLL_CONFIG(960, 245, 1200, 0),
385 PLL_CONFIG(960, 196, 800, 0),
386 PLL_CONFIG(960, 245, 800, 0),
387 PLL_CONFIG(960, 245, 1200, 800),
388 PLL_CONFIG(960, 196, 1200, 800),
Trilok Sonif597e242011-06-06 12:37:16 +0530389 PLL_CONFIG(960, 245, 1200, 1008),
Trilok Sonid7b05e52011-08-17 18:09:08 +0530390 PLL_CONFIG(960, 196, 1200, 1008),
Trilok Soni9bb022c2011-10-31 18:25:19 +0530391 PLL_CONFIG(960, 737, 1200, 800),
392 PLL_CONFIG(960, 589, 1200, 800),
393 PLL_CONFIG(960, 737, 1200, 1008),
394 PLL_CONFIG(960, 589, 1200, 1008),
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530395 PLL_CONFIG(960, 245, 1200, 1209),
396 PLL_CONFIG(960, 196, 1200, 1209),
Trilok Soni48631722012-05-17 20:56:42 +0530397 PLL_CONFIG(960, 245, 1200, 1152),
398 PLL_CONFIG(960, 196, 1200, 1152),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399 { 0, 0, 0, 0, 0 }
400};
401
402#ifdef CONFIG_CPU_FREQ_MSM
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530403static struct cpufreq_frequency_table freq_table[NR_CPUS][20];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404
405static void __init cpufreq_table_init(void)
406{
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530407 int cpu;
408 for_each_possible_cpu(cpu) {
409 unsigned int i, freq_cnt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530411 /* Construct the freq_table table from acpu_freq_tbl since
412 * the freq_table values need to match frequencies specified
413 * in acpu_freq_tbl and acpu_freq_tbl needs to be fixed up
414 * during init.
415 */
416 for (i = 0; acpu_freq_tbl[i].a11clk_khz != 0
417 && freq_cnt < ARRAY_SIZE(*freq_table)-1; i++) {
418 if (acpu_freq_tbl[i].use_for_scaling) {
419 freq_table[cpu][freq_cnt].index = freq_cnt;
420 freq_table[cpu][freq_cnt].frequency
421 = acpu_freq_tbl[i].a11clk_khz;
422 freq_cnt++;
423 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700424 }
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530425
426 /* freq_table not big enough to store all usable freqs. */
427 BUG_ON(acpu_freq_tbl[i].a11clk_khz != 0);
428
429 freq_table[cpu][freq_cnt].index = freq_cnt;
430 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
431 /* Register table with CPUFreq. */
432 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
433 pr_info("CPU%d: %d scaling frequencies supported.\n",
434 cpu, freq_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700436}
437#endif
438
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439static int acpuclk_set_vdd_level(int vdd)
440{
441 uint32_t current_vdd;
442
Pankaj Kumar9406a3b2011-12-23 18:07:15 +0530443 /*
444 * NOTE: v1.0 of 7x27a/7x25a chip doesn't have working
445 * VDD switching support.
446 */
447 if ((cpu_is_msm7x27a() || cpu_is_msm7x25a()) &&
448 (SOCINFO_VERSION_MINOR(socinfo_get_version()) < 1))
449 return 0;
450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700451 current_vdd = readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
452
453 pr_debug("Switching VDD from %u mV -> %d mV\n",
454 current_vdd, vdd);
455
456 writel_relaxed((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
457 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700458 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700459 if ((readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
460 pr_err("VDD set failed\n");
461 return -EIO;
462 }
463
464 pr_debug("VDD switched\n");
465
466 return 0;
467}
468
469/* Set proper dividers for the given clock speed. */
470static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s)
471{
472 uint32_t reg_clkctl, reg_clksel, clk_div, src_sel;
473
474 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
475
476 /* AHB_CLK_DIV */
477 clk_div = (reg_clksel >> 1) & 0x03;
478 /* CLK_SEL_SRC1NO */
479 src_sel = reg_clksel & 1;
480
481 /*
482 * If the new clock divider is higher than the previous, then
483 * program the divider before switching the clock
484 */
485 if (hunt_s->ahbclk_div > clk_div) {
486 reg_clksel &= ~(0x3 << 1);
487 reg_clksel |= (hunt_s->ahbclk_div << 1);
488 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
489 }
490
491 /* Program clock source and divider */
492 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
493 reg_clkctl &= ~(0xFF << (8 * src_sel));
494 reg_clkctl |= hunt_s->a11clk_src_sel << (4 + 8 * src_sel);
495 reg_clkctl |= hunt_s->a11clk_src_div << (0 + 8 * src_sel);
496 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
497
498 /* Program clock source selection */
499 reg_clksel ^= 1;
500 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
501
Pankaj Kumard66a919a2012-04-11 19:35:38 +0530502 /* Wait for the clock switch to complete */
503 mb();
504 udelay(50);
505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /*
507 * If the new clock divider is lower than the previous, then
508 * program the divider after switching the clock
509 */
510 if (hunt_s->ahbclk_div < clk_div) {
511 reg_clksel &= ~(0x3 << 1);
512 reg_clksel |= (hunt_s->ahbclk_div << 1);
513 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
514 }
515}
516
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530517static int acpuclk_7627_set_rate(int cpu, unsigned long rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700518 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700519{
520 uint32_t reg_clkctl;
521 struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
522 int res, rc = 0;
523 unsigned int plls_enabled = 0, pll;
524
525 if (reason == SETRATE_CPUFREQ)
526 mutex_lock(&drv_state.lock);
527
528 strt_s = cur_s = drv_state.current_speed;
529
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700530 WARN_ONCE(cur_s == NULL, "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 if (cur_s == NULL) {
532 rc = -ENOENT;
533 goto out;
534 }
535
536 if (rate == cur_s->a11clk_khz)
537 goto out;
538
539 for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
540 if (tgt_s->a11clk_khz == rate)
541 break;
542 }
543
544 if (tgt_s->a11clk_khz == 0) {
545 rc = -EINVAL;
546 goto out;
547 }
548
549 /* Choose the highest speed at or below 'rate' with same PLL. */
550 if (reason != SETRATE_CPUFREQ
551 && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
552 while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
553 tgt_s--;
554 }
555
556 if (strt_s->pll != ACPU_PLL_TCXO)
557 plls_enabled |= 1 << strt_s->pll;
558
559 if (reason == SETRATE_CPUFREQ) {
560 if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
Trilok Soni57c07782012-05-07 16:52:16 +0530561 rc = clk_enable(pll_clk[tgt_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700562 if (rc < 0) {
563 pr_err("PLL%d enable failed (%d)\n",
564 tgt_s->pll, rc);
565 goto out;
566 }
567 plls_enabled |= 1 << tgt_s->pll;
568 }
569 }
570 /* Need to do this when coming out of power collapse since some modem
571 * firmwares reset the VDD when the application processor enters power
572 * collapse. */
573 if (reason == SETRATE_CPUFREQ || reason == SETRATE_PC) {
574 /* Increase VDD if needed. */
575 if (tgt_s->vdd > cur_s->vdd) {
576 rc = acpuclk_set_vdd_level(tgt_s->vdd);
577 if (rc < 0) {
578 pr_err("Unable to switch ACPU vdd (%d)\n", rc);
579 goto out;
580 }
581 }
582 }
583
584 /* Set wait states for CPU inbetween frequency changes */
585 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
586 reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
587 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
588
589 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
590 strt_s->a11clk_khz, tgt_s->a11clk_khz);
591
592 while (cur_s != tgt_s) {
593 /*
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530594 * Always jump to target freq if within max_speed_delta_khz,
595 * regardless of PLL. If differnece is greater, use the
596 * predefined steppings in the table.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700597 */
598 int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
599 if (d > drv_state.max_speed_delta_khz) {
600
601 if (tgt_s->a11clk_khz > cur_s->a11clk_khz) {
602 /* Step up: jump to target PLL as early as
603 * possible so indexing using TCXO (up[-1])
604 * never occurs. */
605 if (likely(cur_s->up[tgt_s->pll]))
606 cur_s = cur_s->up[tgt_s->pll];
607 else
608 cur_s = cur_s->up[cur_s->pll];
609 } else {
610 /* Step down: stay on current PLL as long as
611 * possible so indexing using TCXO (down[-1])
612 * never occurs. */
613 if (likely(cur_s->down[cur_s->pll]))
614 cur_s = cur_s->down[cur_s->pll];
615 else
616 cur_s = cur_s->down[tgt_s->pll];
617 }
618
619 if (cur_s == NULL) { /* This should not happen. */
620 pr_err("No stepping frequencies found. "
621 "strt_s:%u tgt_s:%u\n",
622 strt_s->a11clk_khz, tgt_s->a11clk_khz);
623 rc = -EINVAL;
624 goto out;
625 }
626
627 } else {
628 cur_s = tgt_s;
629 }
630
631 pr_debug("STEP khz = %u, pll = %d\n",
632 cur_s->a11clk_khz, cur_s->pll);
633
634 if (cur_s->pll != ACPU_PLL_TCXO
635 && !(plls_enabled & (1 << cur_s->pll))) {
Trilok Soni57c07782012-05-07 16:52:16 +0530636 rc = clk_enable(pll_clk[cur_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 if (rc < 0) {
638 pr_err("PLL%d enable failed (%d)\n",
639 cur_s->pll, rc);
640 goto out;
641 }
642 plls_enabled |= 1 << cur_s->pll;
643 }
644
645 acpuclk_set_div(cur_s);
646 drv_state.current_speed = cur_s;
Taniya Dasc43e6872012-03-21 16:41:14 +0530647 /* Re-adjust lpj for the new clock speed. */
648#ifdef CONFIG_SMP
649 for_each_possible_cpu(cpu) {
650 per_cpu(cpu_data, cpu).loops_per_jiffy =
651 cur_s->lpj;
652 }
653#endif
654 /* Adjust the global one */
655 loops_per_jiffy = cur_s->lpj;
656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 }
658
659 /* Nothing else to do for SWFI. */
660 if (reason == SETRATE_SWFI)
661 goto out;
662
663 /* Change the AXI bus frequency if we can. */
664 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
665 res = clk_set_rate(drv_state.ebi1_clk,
666 tgt_s->axiclk_khz * 1000);
667 if (res < 0)
668 pr_warning("Setting AXI min rate failed (%d)\n", res);
669 }
670
671 /* Disable PLLs we are not using anymore. */
672 if (tgt_s->pll != ACPU_PLL_TCXO)
673 plls_enabled &= ~(1 << tgt_s->pll);
674 for (pll = ACPU_PLL_0; pll < ACPU_PLL_END; pll++)
Pankaj Kumar3912c982011-12-07 16:59:03 +0530675 if (plls_enabled & (1 << pll))
Trilok Soni57c07782012-05-07 16:52:16 +0530676 clk_disable(pll_clk[pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677
678 /* Nothing else to do for power collapse. */
679 if (reason == SETRATE_PC)
680 goto out;
681
682 /* Drop VDD level if we can. */
683 if (tgt_s->vdd < strt_s->vdd) {
684 res = acpuclk_set_vdd_level(tgt_s->vdd);
685 if (res < 0)
686 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
687 }
688
689 pr_debug("ACPU speed change complete\n");
690out:
691 if (reason == SETRATE_CPUFREQ)
692 mutex_unlock(&drv_state.lock);
693 return rc;
694}
695
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700696static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700697{
698 struct clkctl_acpu_speed *speed;
Trilok Soni7d6c8652011-07-14 15:35:07 +0530699 uint32_t div, sel, reg_clksel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 int res;
701
702 /*
Trilok Soni57c07782012-05-07 16:52:16 +0530703 * Prepare all the PLLs because we enable/disable them
704 * from atomic context and can't always ensure they're
705 * all prepared in non-atomic context. Same goes for
706 * ebi1_acpu_clk.
707 */
708 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_0].clk));
709 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_1].clk));
710 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_2].clk));
711 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_4].clk));
712 BUG_ON(clk_prepare(drv_state.ebi1_clk));
713
714 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 * Determine the rate of ACPU clock
716 */
717
718 if (!(readl_relaxed(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
719 /* CLK_SRC0_SEL */
720 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
721 /* CLK_SRC0_DIV */
722 div = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
723 } else {
724 /* CLK_SRC1_SEL */
725 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
726 /* CLK_SRC1_DIV */
727 div = readl_relaxed(A11S_CLK_CNTL_ADDR) & 0x0f;
728 }
729
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
731 if (speed->a11clk_src_sel == sel
732 && (speed->a11clk_src_div == div))
733 break;
734 }
735 if (speed->a11clk_khz == 0) {
736 pr_err("Error - ACPU clock reports invalid speed\n");
737 return;
738 }
739
740 drv_state.current_speed = speed;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530741 if (speed->pll != ACPU_PLL_TCXO) {
Trilok Soni57c07782012-05-07 16:52:16 +0530742 if (clk_enable(pll_clk[speed->pll].clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743 pr_warning("Failed to vote for boot PLL\n");
Pankaj Kumar3912c982011-12-07 16:59:03 +0530744 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700745
Trilok Soni7d6c8652011-07-14 15:35:07 +0530746 /* Fix div2 to 2 for 7x27/5a(aa) targets */
747 if (!cpu_is_msm7x27()) {
748 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
749 reg_clksel &= ~(0x3 << 14);
750 reg_clksel |= (0x1 << 14);
751 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
752 }
753
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
755 if (res < 0)
756 pr_warning("Setting AXI min rate failed (%d)\n", res);
Trilok Soni57c07782012-05-07 16:52:16 +0530757 res = clk_enable(drv_state.ebi1_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700758 if (res < 0)
759 pr_warning("Enabling AXI clock failed (%d)\n", res);
760
761 pr_info("ACPU running at %d KHz\n", speed->a11clk_khz);
762}
763
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530764static unsigned long acpuclk_7627_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765{
766 WARN_ONCE(drv_state.current_speed == NULL,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700767 "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700768 if (drv_state.current_speed)
769 return drv_state.current_speed->a11clk_khz;
770 else
771 return 0;
772}
773
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774/*----------------------------------------------------------------------------
775 * Clock driver initialization
776 *---------------------------------------------------------------------------*/
Pankaj Kumar3912c982011-12-07 16:59:03 +0530777#define MHZ 1000000
778static void __init select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779{
Pankaj Kumar3912c982011-12-07 16:59:03 +0530780 unsigned long pll_mhz[ACPU_PLL_END];
781 struct pll_freq_tbl_map *t;
782 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700783
Pankaj Kumar3912c982011-12-07 16:59:03 +0530784 /* Get PLL clocks */
785 for (i = 0; i < ACPU_PLL_END; i++) {
786 if (pll_clk[i].name) {
787 pll_clk[i].clk = clk_get_sys("acpu", pll_clk[i].name);
788 if (IS_ERR(pll_clk[i].clk)) {
789 pll_mhz[i] = 0;
790 continue;
791 }
792 /* Get PLL's Rate */
793 pll_mhz[i] = clk_get_rate(pll_clk[i].clk)/MHZ;
794 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795 }
796
Pankaj Kumar3912c982011-12-07 16:59:03 +0530797 /*
798 * For the pll configuration used in acpuclock table e.g.
799 * pll0_960_pll1_245_pll2_1200" is same for 7627 and
800 * 7625a (as pll0,pll1,pll2) having same rates, but frequency
801 * table is different for both targets.
802 *
803 * Hence below for loop will not be able to select correct
804 * table based on PLL rates as rates are same. Hence we need
805 * to add this cpu check for selecting the correct acpuclock table.
806 */
Trilok Soni54d35c42011-07-14 17:47:50 +0530807 if (cpu_is_msm7x25a()) {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530808 if (pll_mhz[ACPU_PLL_1] == 245) {
Trilok Soni54d35c42011-07-14 17:47:50 +0530809 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530810 pll0_960_pll1_245_pll2_1200_25a;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530811 } else if (pll_mhz[ACPU_PLL_1] == 737) {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530812 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530813 pll0_960_pll1_737_pll2_1200_25a;
Trilok Soni54d35c42011-07-14 17:47:50 +0530814 }
815 } else {
816 /* Select the right table to use. */
Pankaj Kumar3912c982011-12-07 16:59:03 +0530817 for (t = acpu_freq_tbl_list; t->tbl != 0; t++) {
818 if (t->pll0_rate == pll_mhz[ACPU_PLL_0]
819 && t->pll1_rate == pll_mhz[ACPU_PLL_1]
820 && t->pll2_rate == pll_mhz[ACPU_PLL_2]
821 && t->pll4_rate == pll_mhz[ACPU_PLL_4]) {
822 acpu_freq_tbl = t->tbl;
Trilok Soni54d35c42011-07-14 17:47:50 +0530823 break;
824 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 }
826 }
827
828 if (acpu_freq_tbl == NULL) {
829 pr_crit("Unknown PLL configuration!\n");
830 BUG();
831 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832}
833
834/*
835 * Hardware requires the CPU to be dropped to less than MAX_WAIT_FOR_IRQ_KHZ
836 * before entering a wait for irq low-power mode. Find a suitable rate.
837 */
838static unsigned long __init find_wait_for_irq_khz(void)
839{
840 unsigned long found_khz = 0;
841 int i;
842
843 for (i = 0; acpu_freq_tbl[i].a11clk_khz &&
844 acpu_freq_tbl[i].a11clk_khz <= MAX_WAIT_FOR_IRQ_KHZ; i++)
845 found_khz = acpu_freq_tbl[i].a11clk_khz;
846
847 return found_khz;
848}
849
Taniya Dasc43e6872012-03-21 16:41:14 +0530850static void __init lpj_init(void)
851{
852 int i = 0, cpu;
853 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
854 unsigned long loops;
855
856 for_each_possible_cpu(cpu) {
857#ifdef CONFIG_SMP
858 loops = per_cpu(cpu_data, cpu).loops_per_jiffy;
859#else
860 loops = loops_per_jiffy;
861#endif
862 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
863 acpu_freq_tbl[i].lpj = cpufreq_scale(
864 loops,
865 base_clk->a11clk_khz,
866 acpu_freq_tbl[i].a11clk_khz);
867 }
868 }
869}
870
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871static void __init precompute_stepping(void)
872{
873 int i, step_idx;
874
875#define cur_freq acpu_freq_tbl[i].a11clk_khz
876#define step_freq acpu_freq_tbl[step_idx].a11clk_khz
877#define cur_pll acpu_freq_tbl[i].pll
878#define step_pll acpu_freq_tbl[step_idx].pll
879
880 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
881
882 /* Calculate max "up" step for each destination PLL */
883 step_idx = i + 1;
884 while (step_freq && (step_freq - cur_freq)
885 <= drv_state.max_speed_delta_khz) {
886 acpu_freq_tbl[i].up[step_pll] =
887 &acpu_freq_tbl[step_idx];
888 step_idx++;
889 }
890 if (step_idx == (i + 1) && step_freq) {
891 pr_crit("Delta between freqs %u KHz and %u KHz is"
892 " too high!\n", cur_freq, step_freq);
893 BUG();
894 }
895
896 /* Calculate max "down" step for each destination PLL */
897 step_idx = i - 1;
898 while (step_idx >= 0 && (cur_freq - step_freq)
899 <= drv_state.max_speed_delta_khz) {
900 acpu_freq_tbl[i].down[step_pll] =
901 &acpu_freq_tbl[step_idx];
902 step_idx--;
903 }
904 if (step_idx == (i - 1) && i > 0) {
905 pr_crit("Delta between freqs %u KHz and %u KHz is"
906 " too high!\n", cur_freq, step_freq);
907 BUG();
908 }
909 }
910}
911
912static void __init print_acpu_freq_tbl(void)
913{
914 struct clkctl_acpu_speed *t;
915 short down_idx[ACPU_PLL_END];
916 short up_idx[ACPU_PLL_END];
917 int i, j;
918
919#define FREQ_IDX(freq_ptr) (freq_ptr - acpu_freq_tbl)
920 pr_info("Id CPU-KHz PLL DIV AHB-KHz ADIV AXI-KHz "
921 "D0 D1 D2 D4 U0 U1 U2 U4\n");
922
923 t = &acpu_freq_tbl[0];
924 for (i = 0; t->a11clk_khz != 0; i++) {
925
926 for (j = 0; j < ACPU_PLL_END; j++) {
927 down_idx[j] = t->down[j] ? FREQ_IDX(t->down[j]) : -1;
928 up_idx[j] = t->up[j] ? FREQ_IDX(t->up[j]) : -1;
929 }
930
931 pr_info("%2d %7d %3d %3d %7d %4d %7d "
932 "%2d %2d %2d %2d %2d %2d %2d %2d\n",
933 i, t->a11clk_khz, t->pll, t->a11clk_src_div + 1,
934 t->ahbclk_khz, t->ahbclk_div + 1, t->axiclk_khz,
935 down_idx[0], down_idx[1], down_idx[2], down_idx[4],
936 up_idx[0], up_idx[1], up_idx[2], up_idx[4]);
937
938 t++;
939 }
940}
941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700942
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530943static struct acpuclk_data acpuclk_7627_data = {
944 .set_rate = acpuclk_7627_set_rate,
945 .get_rate = acpuclk_7627_get_rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700946 .power_collapse_khz = POWER_COLLAPSE_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700947 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700948};
949
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530950static int __init acpuclk_7627_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700952 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953
954 drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
955 BUG_ON(IS_ERR(drv_state.ebi1_clk));
956
957 mutex_init(&drv_state.lock);
Matt Wagantallec57f062011-08-16 23:54:46 -0700958 drv_state.max_speed_delta_khz = soc_data->max_speed_delta_khz;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530959 select_freq_plan();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530960 acpuclk_7627_data.wait_for_irq_khz = find_wait_for_irq_khz();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 precompute_stepping();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700962 acpuclk_hw_init();
Taniya Dasc43e6872012-03-21 16:41:14 +0530963 lpj_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 print_acpu_freq_tbl();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530965 acpuclk_register(&acpuclk_7627_data);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700966
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967#ifdef CONFIG_CPU_FREQ_MSM
968 cpufreq_table_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700969#endif
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700970 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971}
Matt Wagantallec57f062011-08-16 23:54:46 -0700972
Matt Wagantallec57f062011-08-16 23:54:46 -0700973struct acpuclk_soc_data acpuclk_7x27_soc_data __initdata = {
974 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530975 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700976};
977
978struct acpuclk_soc_data acpuclk_7x27a_soc_data __initdata = {
979 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530980 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700981};
982
983struct acpuclk_soc_data acpuclk_7x27aa_soc_data __initdata = {
984 .max_speed_delta_khz = 504000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530985 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700986};
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530987
988struct acpuclk_soc_data acpuclk_8625_soc_data __initdata = {
989 /* TODO: Need to update speed delta from H/w Team */
990 .max_speed_delta_khz = 604800,
991 .init = acpuclk_7627_init,
992};