blob: 99311d4e862faac0aa03b226cbecd4b84d2db4ca [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
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530252/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530253static struct clkctl_acpu_speed pll0_960_pll1_245_pll2_1200_25a[] = {
Trilok Soni54d35c42011-07-14 17:47:50 +0530254 { 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 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530258 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530259 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530260 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni54d35c42011-07-14 17:47:50 +0530261 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
262 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530263 { 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 +0530264};
265
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530266/* 7627a PLL2 @ 1200MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530267static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_800[] = {
268 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
269 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
270 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
271 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530272 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530273 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
274 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 122880 },
275 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530276 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530277 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530278 { 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 +0530279};
280
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530281/* 7627a PLL2 @ 1200MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530282static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_800[] = {
283 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
284 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
285 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
286 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
287 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 120000 },
288 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 120000 },
289 { 0, 400000, ACPU_PLL_4, 6, 1, 50000, 3, 4, 120000 },
290 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 120000 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530291 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530292 { 1, 800000, ACPU_PLL_4, 6, 0, 100000, 3, 7, 200000 },
Taniya Dasc43e6872012-03-21 16:41:14 +0530293 { 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 +0530294};
295
Pankaj Kumar714c5cc2012-01-05 13:14:38 +0530296/* 7627aa PLL4 @ 1008MHz with GSM capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530297static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_pll4_1008[] = {
298 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
299 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
300 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
301 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530302 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530303 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
304 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530305 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
306 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530307 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530308 { 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 +0530309};
310
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530311/* 7627aa PLL4 @ 1008MHz with CDMA capable modem */
Trilok Soni9bb022c2011-10-31 18:25:19 +0530312static struct clkctl_acpu_speed pll0_960_pll1_589_pll2_1200_pll4_1008[] = {
313 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 24576 },
314 { 0, 65536, ACPU_PLL_1, 1, 8, 8192, 3, 1, 49152 },
315 { 1, 98304, ACPU_PLL_1, 1, 5, 12288, 3, 2, 49152 },
316 { 1, 196608, ACPU_PLL_1, 1, 2, 24576, 3, 3, 98304 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530317 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530318 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
319 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
Pankaj Kumar501e14e2012-04-10 14:51:41 +0530320 { 0, 504000, ACPU_PLL_4, 6, 1, 63000, 3, 6, 160000 },
321 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 160000 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530322 { 1, 1008000, ACPU_PLL_4, 6, 0, 126000, 3, 7, 200000},
Taniya Dasc43e6872012-03-21 16:41:14 +0530323 { 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 +0530324};
325
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530326/* 7625a PLL2 @ 1200MHz with GSM capable modem */
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530327static struct clkctl_acpu_speed pll0_960_pll1_737_pll2_1200_25a[] = {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530328 { 0, 19200, ACPU_PLL_TCXO, 0, 0, 2400, 3, 0, 30720 },
329 { 0, 61440, ACPU_PLL_1, 1, 11, 7680, 3, 1, 61440 },
330 { 1, 122880, ACPU_PLL_1, 1, 5, 15360, 3, 2, 61440 },
331 { 1, 245760, ACPU_PLL_1, 1, 2, 30720, 3, 3, 61440 },
Pankaj Kumar2764fe72012-02-23 00:53:28 +0530332 { 0, 300000, ACPU_PLL_2, 2, 3, 37500, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530333 { 1, 320000, ACPU_PLL_0, 4, 2, 40000, 3, 4, 122880 },
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530334 { 0, 400000, ACPU_PLL_2, 2, 2, 50000, 3, 4, 122880 },
Trilok Soni9bb022c2011-10-31 18:25:19 +0530335 { 1, 480000, ACPU_PLL_0, 4, 1, 60000, 3, 5, 122880 },
336 { 1, 600000, ACPU_PLL_2, 2, 1, 75000, 3, 6, 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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700340#define PLL_CONFIG(m0, m1, m2, m4) { \
Pankaj Kumar3912c982011-12-07 16:59:03 +0530341 m0, m1, m2, m4, \
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 pll0_##m0##_pll1_##m1##_pll2_##m2##_pll4_##m4 \
343}
344
345struct pll_freq_tbl_map {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530346 unsigned int pll0_rate;
347 unsigned int pll1_rate;
348 unsigned int pll2_rate;
349 unsigned int pll4_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700350 struct clkctl_acpu_speed *tbl;
351};
352
353static struct pll_freq_tbl_map acpu_freq_tbl_list[] = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700354 PLL_CONFIG(960, 196, 1200, 0),
355 PLL_CONFIG(960, 245, 1200, 0),
356 PLL_CONFIG(960, 196, 800, 0),
357 PLL_CONFIG(960, 245, 800, 0),
358 PLL_CONFIG(960, 245, 1200, 800),
359 PLL_CONFIG(960, 196, 1200, 800),
Trilok Sonif597e242011-06-06 12:37:16 +0530360 PLL_CONFIG(960, 245, 1200, 1008),
Trilok Sonid7b05e52011-08-17 18:09:08 +0530361 PLL_CONFIG(960, 196, 1200, 1008),
Trilok Soni9bb022c2011-10-31 18:25:19 +0530362 PLL_CONFIG(960, 737, 1200, 800),
363 PLL_CONFIG(960, 589, 1200, 800),
364 PLL_CONFIG(960, 737, 1200, 1008),
365 PLL_CONFIG(960, 589, 1200, 1008),
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530366 PLL_CONFIG(960, 245, 1200, 1209),
367 PLL_CONFIG(960, 196, 1200, 1209),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368 { 0, 0, 0, 0, 0 }
369};
370
371#ifdef CONFIG_CPU_FREQ_MSM
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530372static struct cpufreq_frequency_table freq_table[NR_CPUS][20];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373
374static void __init cpufreq_table_init(void)
375{
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530376 int cpu;
377 for_each_possible_cpu(cpu) {
378 unsigned int i, freq_cnt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530380 /* Construct the freq_table table from acpu_freq_tbl since
381 * the freq_table values need to match frequencies specified
382 * in acpu_freq_tbl and acpu_freq_tbl needs to be fixed up
383 * during init.
384 */
385 for (i = 0; acpu_freq_tbl[i].a11clk_khz != 0
386 && freq_cnt < ARRAY_SIZE(*freq_table)-1; i++) {
387 if (acpu_freq_tbl[i].use_for_scaling) {
388 freq_table[cpu][freq_cnt].index = freq_cnt;
389 freq_table[cpu][freq_cnt].frequency
390 = acpu_freq_tbl[i].a11clk_khz;
391 freq_cnt++;
392 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 }
Pankaj Kumar873bc7a2011-12-21 17:25:44 +0530394
395 /* freq_table not big enough to store all usable freqs. */
396 BUG_ON(acpu_freq_tbl[i].a11clk_khz != 0);
397
398 freq_table[cpu][freq_cnt].index = freq_cnt;
399 freq_table[cpu][freq_cnt].frequency = CPUFREQ_TABLE_END;
400 /* Register table with CPUFreq. */
401 cpufreq_frequency_table_get_attr(freq_table[cpu], cpu);
402 pr_info("CPU%d: %d scaling frequencies supported.\n",
403 cpu, freq_cnt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700404 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700405}
406#endif
407
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700408static int acpuclk_set_vdd_level(int vdd)
409{
410 uint32_t current_vdd;
411
Pankaj Kumar9406a3b2011-12-23 18:07:15 +0530412 /*
413 * NOTE: v1.0 of 7x27a/7x25a chip doesn't have working
414 * VDD switching support.
415 */
416 if ((cpu_is_msm7x27a() || cpu_is_msm7x25a()) &&
417 (SOCINFO_VERSION_MINOR(socinfo_get_version()) < 1))
418 return 0;
419
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700420 current_vdd = readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x07;
421
422 pr_debug("Switching VDD from %u mV -> %d mV\n",
423 current_vdd, vdd);
424
425 writel_relaxed((1 << 7) | (vdd << 3), A11S_VDD_SVS_PLEVEL_ADDR);
426 mb();
Matt Wagantallec57f062011-08-16 23:54:46 -0700427 udelay(62);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700428 if ((readl_relaxed(A11S_VDD_SVS_PLEVEL_ADDR) & 0x7) != vdd) {
429 pr_err("VDD set failed\n");
430 return -EIO;
431 }
432
433 pr_debug("VDD switched\n");
434
435 return 0;
436}
437
438/* Set proper dividers for the given clock speed. */
439static void acpuclk_set_div(const struct clkctl_acpu_speed *hunt_s)
440{
441 uint32_t reg_clkctl, reg_clksel, clk_div, src_sel;
442
443 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
444
445 /* AHB_CLK_DIV */
446 clk_div = (reg_clksel >> 1) & 0x03;
447 /* CLK_SEL_SRC1NO */
448 src_sel = reg_clksel & 1;
449
450 /*
451 * If the new clock divider is higher than the previous, then
452 * program the divider before switching the clock
453 */
454 if (hunt_s->ahbclk_div > clk_div) {
455 reg_clksel &= ~(0x3 << 1);
456 reg_clksel |= (hunt_s->ahbclk_div << 1);
457 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
458 }
459
460 /* Program clock source and divider */
461 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
462 reg_clkctl &= ~(0xFF << (8 * src_sel));
463 reg_clkctl |= hunt_s->a11clk_src_sel << (4 + 8 * src_sel);
464 reg_clkctl |= hunt_s->a11clk_src_div << (0 + 8 * src_sel);
465 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
466
467 /* Program clock source selection */
468 reg_clksel ^= 1;
469 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
470
Pankaj Kumard66a9192012-04-11 19:35:38 +0530471 /* Wait for the clock switch to complete */
472 mb();
473 udelay(50);
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475 /*
476 * If the new clock divider is lower than the previous, then
477 * program the divider after switching the clock
478 */
479 if (hunt_s->ahbclk_div < clk_div) {
480 reg_clksel &= ~(0x3 << 1);
481 reg_clksel |= (hunt_s->ahbclk_div << 1);
482 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
483 }
484}
485
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530486static int acpuclk_7627_set_rate(int cpu, unsigned long rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700487 enum setrate_reason reason)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488{
489 uint32_t reg_clkctl;
490 struct clkctl_acpu_speed *cur_s, *tgt_s, *strt_s;
491 int res, rc = 0;
492 unsigned int plls_enabled = 0, pll;
493
494 if (reason == SETRATE_CPUFREQ)
495 mutex_lock(&drv_state.lock);
496
497 strt_s = cur_s = drv_state.current_speed;
498
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700499 WARN_ONCE(cur_s == NULL, "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700500 if (cur_s == NULL) {
501 rc = -ENOENT;
502 goto out;
503 }
504
505 if (rate == cur_s->a11clk_khz)
506 goto out;
507
508 for (tgt_s = acpu_freq_tbl; tgt_s->a11clk_khz != 0; tgt_s++) {
509 if (tgt_s->a11clk_khz == rate)
510 break;
511 }
512
513 if (tgt_s->a11clk_khz == 0) {
514 rc = -EINVAL;
515 goto out;
516 }
517
518 /* Choose the highest speed at or below 'rate' with same PLL. */
519 if (reason != SETRATE_CPUFREQ
520 && tgt_s->a11clk_khz < cur_s->a11clk_khz) {
521 while (tgt_s->pll != ACPU_PLL_TCXO && tgt_s->pll != cur_s->pll)
522 tgt_s--;
523 }
524
525 if (strt_s->pll != ACPU_PLL_TCXO)
526 plls_enabled |= 1 << strt_s->pll;
527
528 if (reason == SETRATE_CPUFREQ) {
529 if (strt_s->pll != tgt_s->pll && tgt_s->pll != ACPU_PLL_TCXO) {
Trilok Soni57c07782012-05-07 16:52:16 +0530530 rc = clk_enable(pll_clk[tgt_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700531 if (rc < 0) {
532 pr_err("PLL%d enable failed (%d)\n",
533 tgt_s->pll, rc);
534 goto out;
535 }
536 plls_enabled |= 1 << tgt_s->pll;
537 }
538 }
539 /* Need to do this when coming out of power collapse since some modem
540 * firmwares reset the VDD when the application processor enters power
541 * collapse. */
542 if (reason == SETRATE_CPUFREQ || reason == SETRATE_PC) {
543 /* Increase VDD if needed. */
544 if (tgt_s->vdd > cur_s->vdd) {
545 rc = acpuclk_set_vdd_level(tgt_s->vdd);
546 if (rc < 0) {
547 pr_err("Unable to switch ACPU vdd (%d)\n", rc);
548 goto out;
549 }
550 }
551 }
552
553 /* Set wait states for CPU inbetween frequency changes */
554 reg_clkctl = readl_relaxed(A11S_CLK_CNTL_ADDR);
555 reg_clkctl |= (100 << 16); /* set WT_ST_CNT */
556 writel_relaxed(reg_clkctl, A11S_CLK_CNTL_ADDR);
557
558 pr_debug("Switching from ACPU rate %u KHz -> %u KHz\n",
559 strt_s->a11clk_khz, tgt_s->a11clk_khz);
560
561 while (cur_s != tgt_s) {
562 /*
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530563 * Always jump to target freq if within max_speed_delta_khz,
564 * regardless of PLL. If differnece is greater, use the
565 * predefined steppings in the table.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 */
567 int d = abs((int)(cur_s->a11clk_khz - tgt_s->a11clk_khz));
568 if (d > drv_state.max_speed_delta_khz) {
569
570 if (tgt_s->a11clk_khz > cur_s->a11clk_khz) {
571 /* Step up: jump to target PLL as early as
572 * possible so indexing using TCXO (up[-1])
573 * never occurs. */
574 if (likely(cur_s->up[tgt_s->pll]))
575 cur_s = cur_s->up[tgt_s->pll];
576 else
577 cur_s = cur_s->up[cur_s->pll];
578 } else {
579 /* Step down: stay on current PLL as long as
580 * possible so indexing using TCXO (down[-1])
581 * never occurs. */
582 if (likely(cur_s->down[cur_s->pll]))
583 cur_s = cur_s->down[cur_s->pll];
584 else
585 cur_s = cur_s->down[tgt_s->pll];
586 }
587
588 if (cur_s == NULL) { /* This should not happen. */
589 pr_err("No stepping frequencies found. "
590 "strt_s:%u tgt_s:%u\n",
591 strt_s->a11clk_khz, tgt_s->a11clk_khz);
592 rc = -EINVAL;
593 goto out;
594 }
595
596 } else {
597 cur_s = tgt_s;
598 }
599
600 pr_debug("STEP khz = %u, pll = %d\n",
601 cur_s->a11clk_khz, cur_s->pll);
602
603 if (cur_s->pll != ACPU_PLL_TCXO
604 && !(plls_enabled & (1 << cur_s->pll))) {
Trilok Soni57c07782012-05-07 16:52:16 +0530605 rc = clk_enable(pll_clk[cur_s->pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 if (rc < 0) {
607 pr_err("PLL%d enable failed (%d)\n",
608 cur_s->pll, rc);
609 goto out;
610 }
611 plls_enabled |= 1 << cur_s->pll;
612 }
613
614 acpuclk_set_div(cur_s);
615 drv_state.current_speed = cur_s;
Taniya Dasc43e6872012-03-21 16:41:14 +0530616 /* Re-adjust lpj for the new clock speed. */
617#ifdef CONFIG_SMP
618 for_each_possible_cpu(cpu) {
619 per_cpu(cpu_data, cpu).loops_per_jiffy =
620 cur_s->lpj;
621 }
622#endif
623 /* Adjust the global one */
624 loops_per_jiffy = cur_s->lpj;
625
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626 }
627
628 /* Nothing else to do for SWFI. */
629 if (reason == SETRATE_SWFI)
630 goto out;
631
632 /* Change the AXI bus frequency if we can. */
633 if (strt_s->axiclk_khz != tgt_s->axiclk_khz) {
634 res = clk_set_rate(drv_state.ebi1_clk,
635 tgt_s->axiclk_khz * 1000);
636 if (res < 0)
637 pr_warning("Setting AXI min rate failed (%d)\n", res);
638 }
639
640 /* Disable PLLs we are not using anymore. */
641 if (tgt_s->pll != ACPU_PLL_TCXO)
642 plls_enabled &= ~(1 << tgt_s->pll);
643 for (pll = ACPU_PLL_0; pll < ACPU_PLL_END; pll++)
Pankaj Kumar3912c982011-12-07 16:59:03 +0530644 if (plls_enabled & (1 << pll))
Trilok Soni57c07782012-05-07 16:52:16 +0530645 clk_disable(pll_clk[pll].clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646
647 /* Nothing else to do for power collapse. */
648 if (reason == SETRATE_PC)
649 goto out;
650
651 /* Drop VDD level if we can. */
652 if (tgt_s->vdd < strt_s->vdd) {
653 res = acpuclk_set_vdd_level(tgt_s->vdd);
654 if (res < 0)
655 pr_warning("Unable to drop ACPU vdd (%d)\n", res);
656 }
657
658 pr_debug("ACPU speed change complete\n");
659out:
660 if (reason == SETRATE_CPUFREQ)
661 mutex_unlock(&drv_state.lock);
662 return rc;
663}
664
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700665static void __init acpuclk_hw_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666{
667 struct clkctl_acpu_speed *speed;
Trilok Soni7d6c8652011-07-14 15:35:07 +0530668 uint32_t div, sel, reg_clksel;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 int res;
670
671 /*
Trilok Soni57c07782012-05-07 16:52:16 +0530672 * Prepare all the PLLs because we enable/disable them
673 * from atomic context and can't always ensure they're
674 * all prepared in non-atomic context. Same goes for
675 * ebi1_acpu_clk.
676 */
677 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_0].clk));
678 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_1].clk));
679 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_2].clk));
680 BUG_ON(clk_prepare(pll_clk[ACPU_PLL_4].clk));
681 BUG_ON(clk_prepare(drv_state.ebi1_clk));
682
683 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700684 * Determine the rate of ACPU clock
685 */
686
687 if (!(readl_relaxed(A11S_CLK_SEL_ADDR) & 0x01)) { /* CLK_SEL_SRC1N0 */
688 /* CLK_SRC0_SEL */
689 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 12) & 0x7;
690 /* CLK_SRC0_DIV */
691 div = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 8) & 0x0f;
692 } else {
693 /* CLK_SRC1_SEL */
694 sel = (readl_relaxed(A11S_CLK_CNTL_ADDR) >> 4) & 0x07;
695 /* CLK_SRC1_DIV */
696 div = readl_relaxed(A11S_CLK_CNTL_ADDR) & 0x0f;
697 }
698
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 for (speed = acpu_freq_tbl; speed->a11clk_khz != 0; speed++) {
700 if (speed->a11clk_src_sel == sel
701 && (speed->a11clk_src_div == div))
702 break;
703 }
704 if (speed->a11clk_khz == 0) {
705 pr_err("Error - ACPU clock reports invalid speed\n");
706 return;
707 }
708
709 drv_state.current_speed = speed;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530710 if (speed->pll != ACPU_PLL_TCXO) {
Trilok Soni57c07782012-05-07 16:52:16 +0530711 if (clk_enable(pll_clk[speed->pll].clk))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700712 pr_warning("Failed to vote for boot PLL\n");
Pankaj Kumar3912c982011-12-07 16:59:03 +0530713 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714
Trilok Soni7d6c8652011-07-14 15:35:07 +0530715 /* Fix div2 to 2 for 7x27/5a(aa) targets */
716 if (!cpu_is_msm7x27()) {
717 reg_clksel = readl_relaxed(A11S_CLK_SEL_ADDR);
718 reg_clksel &= ~(0x3 << 14);
719 reg_clksel |= (0x1 << 14);
720 writel_relaxed(reg_clksel, A11S_CLK_SEL_ADDR);
721 }
722
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 res = clk_set_rate(drv_state.ebi1_clk, speed->axiclk_khz * 1000);
724 if (res < 0)
725 pr_warning("Setting AXI min rate failed (%d)\n", res);
Trilok Soni57c07782012-05-07 16:52:16 +0530726 res = clk_enable(drv_state.ebi1_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700727 if (res < 0)
728 pr_warning("Enabling AXI clock failed (%d)\n", res);
729
730 pr_info("ACPU running at %d KHz\n", speed->a11clk_khz);
731}
732
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530733static unsigned long acpuclk_7627_get_rate(int cpu)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734{
735 WARN_ONCE(drv_state.current_speed == NULL,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700736 "%s: not initialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 if (drv_state.current_speed)
738 return drv_state.current_speed->a11clk_khz;
739 else
740 return 0;
741}
742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700743/*----------------------------------------------------------------------------
744 * Clock driver initialization
745 *---------------------------------------------------------------------------*/
Pankaj Kumar3912c982011-12-07 16:59:03 +0530746#define MHZ 1000000
747static void __init select_freq_plan(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748{
Pankaj Kumar3912c982011-12-07 16:59:03 +0530749 unsigned long pll_mhz[ACPU_PLL_END];
750 struct pll_freq_tbl_map *t;
751 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752
Pankaj Kumar3912c982011-12-07 16:59:03 +0530753 /* Get PLL clocks */
754 for (i = 0; i < ACPU_PLL_END; i++) {
755 if (pll_clk[i].name) {
756 pll_clk[i].clk = clk_get_sys("acpu", pll_clk[i].name);
757 if (IS_ERR(pll_clk[i].clk)) {
758 pll_mhz[i] = 0;
759 continue;
760 }
761 /* Get PLL's Rate */
762 pll_mhz[i] = clk_get_rate(pll_clk[i].clk)/MHZ;
763 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 }
765
Pankaj Kumar3912c982011-12-07 16:59:03 +0530766 /*
767 * For the pll configuration used in acpuclock table e.g.
768 * pll0_960_pll1_245_pll2_1200" is same for 7627 and
769 * 7625a (as pll0,pll1,pll2) having same rates, but frequency
770 * table is different for both targets.
771 *
772 * Hence below for loop will not be able to select correct
773 * table based on PLL rates as rates are same. Hence we need
774 * to add this cpu check for selecting the correct acpuclock table.
775 */
Trilok Soni54d35c42011-07-14 17:47:50 +0530776 if (cpu_is_msm7x25a()) {
Pankaj Kumar3912c982011-12-07 16:59:03 +0530777 if (pll_mhz[ACPU_PLL_1] == 245) {
Trilok Soni54d35c42011-07-14 17:47:50 +0530778 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530779 pll0_960_pll1_245_pll2_1200_25a;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530780 } else if (pll_mhz[ACPU_PLL_1] == 737) {
Trilok Soni9bb022c2011-10-31 18:25:19 +0530781 acpu_freq_tbl =
Pankaj Kumarc9136b32012-01-02 18:46:13 +0530782 pll0_960_pll1_737_pll2_1200_25a;
Trilok Soni54d35c42011-07-14 17:47:50 +0530783 }
784 } else {
785 /* Select the right table to use. */
Pankaj Kumar3912c982011-12-07 16:59:03 +0530786 for (t = acpu_freq_tbl_list; t->tbl != 0; t++) {
787 if (t->pll0_rate == pll_mhz[ACPU_PLL_0]
788 && t->pll1_rate == pll_mhz[ACPU_PLL_1]
789 && t->pll2_rate == pll_mhz[ACPU_PLL_2]
790 && t->pll4_rate == pll_mhz[ACPU_PLL_4]) {
791 acpu_freq_tbl = t->tbl;
Trilok Soni54d35c42011-07-14 17:47:50 +0530792 break;
793 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700794 }
795 }
796
797 if (acpu_freq_tbl == NULL) {
798 pr_crit("Unknown PLL configuration!\n");
799 BUG();
800 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700801}
802
803/*
804 * Hardware requires the CPU to be dropped to less than MAX_WAIT_FOR_IRQ_KHZ
805 * before entering a wait for irq low-power mode. Find a suitable rate.
806 */
807static unsigned long __init find_wait_for_irq_khz(void)
808{
809 unsigned long found_khz = 0;
810 int i;
811
812 for (i = 0; acpu_freq_tbl[i].a11clk_khz &&
813 acpu_freq_tbl[i].a11clk_khz <= MAX_WAIT_FOR_IRQ_KHZ; i++)
814 found_khz = acpu_freq_tbl[i].a11clk_khz;
815
816 return found_khz;
817}
818
Taniya Dasc43e6872012-03-21 16:41:14 +0530819static void __init lpj_init(void)
820{
821 int i = 0, cpu;
822 const struct clkctl_acpu_speed *base_clk = drv_state.current_speed;
823 unsigned long loops;
824
825 for_each_possible_cpu(cpu) {
826#ifdef CONFIG_SMP
827 loops = per_cpu(cpu_data, cpu).loops_per_jiffy;
828#else
829 loops = loops_per_jiffy;
830#endif
831 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
832 acpu_freq_tbl[i].lpj = cpufreq_scale(
833 loops,
834 base_clk->a11clk_khz,
835 acpu_freq_tbl[i].a11clk_khz);
836 }
837 }
838}
839
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700840static void __init precompute_stepping(void)
841{
842 int i, step_idx;
843
844#define cur_freq acpu_freq_tbl[i].a11clk_khz
845#define step_freq acpu_freq_tbl[step_idx].a11clk_khz
846#define cur_pll acpu_freq_tbl[i].pll
847#define step_pll acpu_freq_tbl[step_idx].pll
848
849 for (i = 0; acpu_freq_tbl[i].a11clk_khz; i++) {
850
851 /* Calculate max "up" step for each destination PLL */
852 step_idx = i + 1;
853 while (step_freq && (step_freq - cur_freq)
854 <= drv_state.max_speed_delta_khz) {
855 acpu_freq_tbl[i].up[step_pll] =
856 &acpu_freq_tbl[step_idx];
857 step_idx++;
858 }
859 if (step_idx == (i + 1) && step_freq) {
860 pr_crit("Delta between freqs %u KHz and %u KHz is"
861 " too high!\n", cur_freq, step_freq);
862 BUG();
863 }
864
865 /* Calculate max "down" step for each destination PLL */
866 step_idx = i - 1;
867 while (step_idx >= 0 && (cur_freq - step_freq)
868 <= drv_state.max_speed_delta_khz) {
869 acpu_freq_tbl[i].down[step_pll] =
870 &acpu_freq_tbl[step_idx];
871 step_idx--;
872 }
873 if (step_idx == (i - 1) && i > 0) {
874 pr_crit("Delta between freqs %u KHz and %u KHz is"
875 " too high!\n", cur_freq, step_freq);
876 BUG();
877 }
878 }
879}
880
881static void __init print_acpu_freq_tbl(void)
882{
883 struct clkctl_acpu_speed *t;
884 short down_idx[ACPU_PLL_END];
885 short up_idx[ACPU_PLL_END];
886 int i, j;
887
888#define FREQ_IDX(freq_ptr) (freq_ptr - acpu_freq_tbl)
889 pr_info("Id CPU-KHz PLL DIV AHB-KHz ADIV AXI-KHz "
890 "D0 D1 D2 D4 U0 U1 U2 U4\n");
891
892 t = &acpu_freq_tbl[0];
893 for (i = 0; t->a11clk_khz != 0; i++) {
894
895 for (j = 0; j < ACPU_PLL_END; j++) {
896 down_idx[j] = t->down[j] ? FREQ_IDX(t->down[j]) : -1;
897 up_idx[j] = t->up[j] ? FREQ_IDX(t->up[j]) : -1;
898 }
899
900 pr_info("%2d %7d %3d %3d %7d %4d %7d "
901 "%2d %2d %2d %2d %2d %2d %2d %2d\n",
902 i, t->a11clk_khz, t->pll, t->a11clk_src_div + 1,
903 t->ahbclk_khz, t->ahbclk_div + 1, t->axiclk_khz,
904 down_idx[0], down_idx[1], down_idx[2], down_idx[4],
905 up_idx[0], up_idx[1], up_idx[2], up_idx[4]);
906
907 t++;
908 }
909}
910
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530912static struct acpuclk_data acpuclk_7627_data = {
913 .set_rate = acpuclk_7627_set_rate,
914 .get_rate = acpuclk_7627_get_rate,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700915 .power_collapse_khz = POWER_COLLAPSE_KHZ,
Matt Wagantallec57f062011-08-16 23:54:46 -0700916 .switch_time_us = 50,
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700917};
918
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530919static int __init acpuclk_7627_init(struct acpuclk_soc_data *soc_data)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920{
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700921 pr_info("%s()\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922
923 drv_state.ebi1_clk = clk_get(NULL, "ebi1_acpu_clk");
924 BUG_ON(IS_ERR(drv_state.ebi1_clk));
925
926 mutex_init(&drv_state.lock);
Matt Wagantallec57f062011-08-16 23:54:46 -0700927 drv_state.max_speed_delta_khz = soc_data->max_speed_delta_khz;
Pankaj Kumar3912c982011-12-07 16:59:03 +0530928 select_freq_plan();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530929 acpuclk_7627_data.wait_for_irq_khz = find_wait_for_irq_khz();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 precompute_stepping();
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700931 acpuclk_hw_init();
Taniya Dasc43e6872012-03-21 16:41:14 +0530932 lpj_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 print_acpu_freq_tbl();
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530934 acpuclk_register(&acpuclk_7627_data);
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700935
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936#ifdef CONFIG_CPU_FREQ_MSM
937 cpufreq_table_init();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938#endif
Matt Wagantall6d9ebee2011-08-26 12:15:24 -0700939 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940}
Matt Wagantallec57f062011-08-16 23:54:46 -0700941
Matt Wagantallec57f062011-08-16 23:54:46 -0700942struct acpuclk_soc_data acpuclk_7x27_soc_data __initdata = {
943 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530944 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700945};
946
947struct acpuclk_soc_data acpuclk_7x27a_soc_data __initdata = {
948 .max_speed_delta_khz = 400000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530949 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700950};
951
952struct acpuclk_soc_data acpuclk_7x27aa_soc_data __initdata = {
953 .max_speed_delta_khz = 504000,
Pankaj Kumar6e66f372011-12-05 14:41:58 +0530954 .init = acpuclk_7627_init,
Matt Wagantallec57f062011-08-16 23:54:46 -0700955};
Pankaj Kumar50c705c2012-01-10 12:02:07 +0530956
957struct acpuclk_soc_data acpuclk_8625_soc_data __initdata = {
958 /* TODO: Need to update speed delta from H/w Team */
959 .max_speed_delta_khz = 604800,
960 .init = acpuclk_7627_init,
961};