blob: d09e4b6b0afa901f88ee4f4d06eb59353179311f [file] [log] [blame]
Patrick Dalyb12d7cc2013-09-23 12:52:08 -07001/*
2 * Copyright (c) 2013, The Linux Foundation. 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>
Patrick Dalyfb983b52013-08-06 18:22:15 -070017#include <linux/module.h>
18#include <linux/io.h>
Patrick Dalyb12d7cc2013-09-23 12:52:08 -070019#include <linux/err.h>
20#include <linux/clk.h>
Patrick Dalyfb983b52013-08-06 18:22:15 -070021#include <linux/mutex.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/regulator/consumer.h>
25#include <linux/of.h>
Patrick Dalyb12d7cc2013-09-23 12:52:08 -070026
27#include <mach/clock-generic.h>
Patrick Dalyfb983b52013-08-06 18:22:15 -070028#include "clock-local2.h"
Patrick Dalyb12d7cc2013-09-23 12:52:08 -070029
30#define UPDATE_CHECK_MAX_LOOPS 200
31
32struct cortex_reg_data {
33 u32 cmd_offset;
34 u32 update_mask;
35 u32 poll_mask;
36};
37
38#define DIV_REG(x) ((x)->base + (x)->div_offset)
39#define SRC_REG(x) ((x)->base + (x)->src_offset)
40#define CMD_REG(x) ((x)->base + \
41 ((struct cortex_reg_data *)(x)->priv)->cmd_offset)
42
43static int update_config(struct mux_div_clk *md)
44{
45 u32 regval, count;
46 struct cortex_reg_data *r = md->priv;
47
48 /* Update the configuration */
49 regval = readl_relaxed(CMD_REG(md));
50 regval |= r->update_mask;
51 writel_relaxed(regval, CMD_REG(md));
52
53 /* Wait for update to take effect */
54 for (count = UPDATE_CHECK_MAX_LOOPS; count > 0; count--) {
55 if (!(readl_relaxed(CMD_REG(md)) &
56 r->poll_mask))
57 return 0;
58 udelay(1);
59 }
60
61 CLK_WARN(&md->c, true, "didn't update its configuration.");
62
63 return -EINVAL;
64}
65
66static void cortex_get_config(struct mux_div_clk *md, u32 *src_sel, u32 *div)
67{
68 u32 regval;
69
70 regval = readl_relaxed(DIV_REG(md));
71 regval &= (md->div_mask << md->div_shift);
72 *div = regval >> md->div_shift;
73 *div = max((u32)1, (*div + 1) / 2);
74
75 regval = readl_relaxed(SRC_REG(md));
76 regval &= (md->src_mask << md->src_shift);
77 *src_sel = regval >> md->src_shift;
78}
79
80static int cortex_set_config(struct mux_div_clk *md, u32 src_sel, u32 div)
81{
82 u32 regval;
83
84 div = div ? ((2 * div) - 1) : 0;
85 regval = readl_relaxed(DIV_REG(md));
86 regval &= ~(md->div_mask << md->div_shift);
87 regval |= div << md->div_shift;
88 writel_relaxed(regval, DIV_REG(md));
89
90 regval = readl_relaxed(SRC_REG(md));
91 regval &= ~(md->src_mask << md->src_shift);
92 regval |= src_sel << md->src_shift;
93 writel_relaxed(regval, SRC_REG(md));
94
95 return update_config(md);
96}
97
98static int cortex_enable(struct mux_div_clk *md)
99{
100 u32 src_sel = parent_to_src_sel(md->parents, md->num_parents,
101 md->c.parent);
102 return cortex_set_config(md, src_sel, md->data.div);
103}
104
105static void cortex_disable(struct mux_div_clk *md)
106{
107 u32 src_sel = parent_to_src_sel(md->parents, md->num_parents,
108 md->safe_parent);
109 cortex_set_config(md, src_sel, md->safe_div);
110}
111
112static bool cortex_is_enabled(struct mux_div_clk *md)
113{
114 return true;
115}
116
117struct mux_div_ops cortex_mux_div_ops = {
118 .set_src_div = cortex_set_config,
119 .get_src_div = cortex_get_config,
120 .is_enabled = cortex_is_enabled,
121 .enable = cortex_enable,
122 .disable = cortex_disable,
123};
Patrick Dalyfb983b52013-08-06 18:22:15 -0700124
125static struct cortex_reg_data a7ssmux_priv = {
126 .cmd_offset = 0x0,
127 .update_mask = BIT(0),
128 .poll_mask = BIT(0),
129};
130
131DEFINE_VDD_REGS_INIT(vdd_cpu, 1);
132
133static struct mux_div_clk a7ssmux = {
134 .ops = &cortex_mux_div_ops,
135 .safe_freq = 300000000,
136 .data = {
137 .max_div = 8,
138 .min_div = 1,
139 },
140 .c = {
141 .dbg_name = "a7ssmux",
142 .ops = &clk_ops_mux_div_clk,
143 .vdd_class = &vdd_cpu,
144 CLK_INIT(a7ssmux.c),
145 },
146 .parents = (struct clk_src[8]) {},
147 .priv = &a7ssmux_priv,
148 .div_offset = 0x4,
149 .div_mask = BM(4, 0),
150 .div_shift = 0,
151 .src_offset = 0x4,
152 .src_mask = BM(10, 8) >> 8,
153 .src_shift = 8,
154};
155
156static struct clk_lookup clock_tbl_a7[] = {
157 CLK_LOOKUP("cpu0_clk", a7ssmux.c, "0.qcom,msm-cpufreq"),
158 CLK_LOOKUP("cpu0_clk", a7ssmux.c, "fe805664.qcom,pm-8x60"),
159};
160
161static int of_get_fmax_vdd_class(struct platform_device *pdev, struct clk *c,
162 char *prop_name)
163{
164 struct device_node *of = pdev->dev.of_node;
165 int prop_len, i;
166 struct clk_vdd_class *vdd = c->vdd_class;
167 u32 *array;
168
169 if (!of_find_property(of, prop_name, &prop_len)) {
170 dev_err(&pdev->dev, "missing %s\n", prop_name);
171 return -EINVAL;
172 }
173
174 prop_len /= sizeof(u32);
175 if (prop_len % 2) {
176 dev_err(&pdev->dev, "bad length %d\n", prop_len);
177 return -EINVAL;
178 }
179
180 prop_len /= 2;
181 vdd->level_votes = devm_kzalloc(&pdev->dev, prop_len * sizeof(int),
182 GFP_KERNEL);
183 if (!vdd->level_votes)
184 return -ENOMEM;
185
186 vdd->vdd_uv = devm_kzalloc(&pdev->dev, prop_len * sizeof(int),
187 GFP_KERNEL);
188 if (!vdd->vdd_uv)
189 return -ENOMEM;
190
191 c->fmax = devm_kzalloc(&pdev->dev, prop_len * sizeof(unsigned long),
192 GFP_KERNEL);
193 if (!c->fmax)
194 return -ENOMEM;
195
Prasad Sodagudi27ebf842013-12-30 11:26:40 +0530196 array = devm_kzalloc(&pdev->dev,
197 prop_len * sizeof(u32) * 2, GFP_KERNEL);
Patrick Dalyfb983b52013-08-06 18:22:15 -0700198 if (!array)
199 return -ENOMEM;
200
201 of_property_read_u32_array(of, prop_name, array, prop_len * 2);
202 for (i = 0; i < prop_len; i++) {
203 c->fmax[i] = array[2 * i];
204 vdd->vdd_uv[i] = array[2 * i + 1];
205 }
206
207 devm_kfree(&pdev->dev, array);
208 vdd->num_levels = prop_len;
209 vdd->cur_level = prop_len;
210 c->num_fmax = prop_len;
211 return 0;
212}
213
214static void get_speed_bin(struct platform_device *pdev, int *bin, int *version)
215{
216 struct resource *res;
217 void __iomem *base;
218 u32 pte_efuse, redundant_sel, valid;
219
220 *bin = 0;
221 *version = 0;
222
223 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "efuse");
224 if (!res) {
225 dev_info(&pdev->dev,
226 "No speed/PVS binning available. Defaulting to 0!\n");
227 return;
228 }
229
230 base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
231 if (!base) {
232 dev_warn(&pdev->dev,
233 "Unable to read efuse data. Defaulting to 0!\n");
234 return;
235 }
236
237 pte_efuse = readl_relaxed(base);
238 devm_iounmap(&pdev->dev, base);
239
240 redundant_sel = (pte_efuse >> 24) & 0x7;
241 *bin = pte_efuse & 0x7;
242 valid = (pte_efuse >> 3) & 0x1;
243 *version = (pte_efuse >> 4) & 0x3;
244
245 if (redundant_sel == 1)
246 *bin = (pte_efuse >> 27) & 0x7;
247
248 if (!valid) {
249 dev_info(&pdev->dev, "Speed bin not set. Defaulting to 0!\n");
250 *bin = 0;
251 } else {
252 dev_info(&pdev->dev, "Speed bin: %d\n", *bin);
253 }
254
255 dev_info(&pdev->dev, "PVS version: %d\n", *version);
256
257 return;
258}
259
260static int of_get_clk_src(struct platform_device *pdev, struct clk_src *parents)
261{
262 struct device_node *of = pdev->dev.of_node;
263 int num_parents, i, j, index;
264 struct clk *c;
265 char clk_name[] = "clk-x";
266
267 num_parents = of_property_count_strings(of, "clock-names");
268 if (num_parents <= 0 || num_parents > 8) {
269 dev_err(&pdev->dev, "missing clock-names\n");
270 return -EINVAL;
271 }
272
273 j = 0;
274 for (i = 0; i < 8; i++) {
275 snprintf(clk_name, ARRAY_SIZE(clk_name), "clk-%d", i);
276 index = of_property_match_string(of, "clock-names", clk_name);
277 if (IS_ERR_VALUE(index))
278 continue;
279
280 parents[j].sel = i;
281 parents[j].src = c = devm_clk_get(&pdev->dev, clk_name);
282 if (IS_ERR(c)) {
283 if (c != ERR_PTR(-EPROBE_DEFER))
284 dev_err(&pdev->dev, "clk_get: %s\n fail",
285 clk_name);
286 return PTR_ERR(c);
287 }
288 j++;
289 }
290
291 return num_parents;
292}
293
294static int clock_a7_probe(struct platform_device *pdev)
295{
296 struct resource *res;
Patrick Daly696ebba2013-11-20 19:57:49 -0800297 int speed_bin = 0, version = 0, rc;
Patrick Dalyfb983b52013-08-06 18:22:15 -0700298 unsigned long rate, aux_rate;
299 struct clk *aux_clk, *main_pll;
300 char prop_name[] = "qcom,speedX-bin-vX";
301
302 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rcg-base");
303 if (!res) {
304 dev_err(&pdev->dev, "missing rcg-base\n");
305 return -EINVAL;
306 }
307 a7ssmux.base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
308 if (!a7ssmux.base) {
309 dev_err(&pdev->dev, "ioremap failed for rcg-base\n");
310 return -ENOMEM;
311 }
312
313 vdd_cpu.regulator[0] = devm_regulator_get(&pdev->dev, "cpu-vdd");
314 if (IS_ERR(vdd_cpu.regulator[0])) {
315 if (PTR_ERR(vdd_cpu.regulator[0]) != -EPROBE_DEFER)
316 dev_err(&pdev->dev, "unable to get regulator\n");
317 return PTR_ERR(vdd_cpu.regulator[0]);
318 }
319
320 a7ssmux.num_parents = of_get_clk_src(pdev, a7ssmux.parents);
321 if (IS_ERR_VALUE(a7ssmux.num_parents))
322 return a7ssmux.num_parents;
323
324 get_speed_bin(pdev, &speed_bin, &version);
325
326 snprintf(prop_name, ARRAY_SIZE(prop_name),
327 "qcom,speed%d-bin-v%d", speed_bin, version);
328 rc = of_get_fmax_vdd_class(pdev, &a7ssmux.c, prop_name);
329 if (rc) {
330 /* Fall back to most conservative PVS table */
331 dev_err(&pdev->dev, "Unable to load voltage plan %s!\n",
332 prop_name);
333 rc = of_get_fmax_vdd_class(pdev, &a7ssmux.c,
334 "qcom,speed0-bin-v0");
335 if (rc) {
336 dev_err(&pdev->dev,
337 "Unable to load safe voltage plan\n");
338 return rc;
339 }
340 dev_info(&pdev->dev, "Safe voltage plan loaded.\n");
341 }
342
343 rc = msm_clock_register(clock_tbl_a7, ARRAY_SIZE(clock_tbl_a7));
344 if (rc) {
345 dev_err(&pdev->dev, "msm_clock_register failed\n");
346 return rc;
347 }
348
349 /* Force a PLL reconfiguration */
350 aux_clk = a7ssmux.parents[0].src;
351 main_pll = a7ssmux.parents[1].src;
352
353 aux_rate = clk_get_rate(aux_clk);
354 rate = clk_get_rate(&a7ssmux.c);
355 clk_set_rate(&a7ssmux.c, aux_rate);
356 clk_set_rate(main_pll, clk_round_rate(main_pll, 1));
357 clk_set_rate(&a7ssmux.c, rate);
358
359 /*
360 * We don't want the CPU clocks to be turned off at late init
361 * if CPUFREQ or HOTPLUG configs are disabled. So, bump up the
362 * refcount of these clocks. Any cpufreq/hotplug manager can assume
363 * that the clocks have already been prepared and enabled by the time
364 * they take over.
365 */
Patrick Daly696ebba2013-11-20 19:57:49 -0800366 WARN(clk_prepare_enable(&a7ssmux.c),
367 "Unable to turn on CPU clock");
Patrick Dalyfb983b52013-08-06 18:22:15 -0700368 return 0;
369}
370
371static struct of_device_id clock_a7_match_table[] = {
372 {.compatible = "qcom,clock-a7-8226"},
373 {}
374};
375
376static struct platform_driver clock_a7_driver = {
377 .driver = {
378 .name = "clock-a7",
379 .of_match_table = clock_a7_match_table,
380 .owner = THIS_MODULE,
381 },
382};
383
384static int __init clock_a7_init(void)
385{
386 return platform_driver_probe(&clock_a7_driver, clock_a7_probe);
387}
388device_initcall(clock_a7_init);