blob: 4e95e4e716804659bfa4644d3b0788221b49e1bc [file] [log] [blame]
Willie Ruan2635a852013-03-17 16:33:42 -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/module.h>
17#include <linux/err.h>
18#include <linux/string.h>
19#include <linux/kernel.h>
20#include <linux/init.h>
21#include <linux/io.h>
22#include <linux/bitops.h>
23#include <linux/slab.h>
24#include <linux/of.h>
25#include <linux/of_device.h>
26#include <linux/platform_device.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/of_regulator.h>
29#include <linux/regulator/cpr-regulator.h>
30
31struct cpr_regulator {
32 struct regulator_desc rdesc;
33 struct regulator_dev *rdev;
34 bool enabled;
35 int corner;
36
37 /* Process voltage parameters */
38 phys_addr_t efuse_phys;
39 u32 num_efuse_bits;
40 u32 efuse_bit_pos[CPR_PVS_EFUSE_BITS_MAX];
41 u32 pvs_bin_process[CPR_PVS_EFUSE_BINS_MAX];
42 u32 pvs_corner_ceiling[NUM_APC_PVS][CPR_CORNER_MAX];
43 /* Process voltage variables */
44 u32 pvs_bin;
45 u32 pvs_process;
46 u32 *process_vmax;
47
48 /* APC voltage regulator */
49 struct regulator *vdd_apc;
50};
51
52static int cpr_regulator_is_enabled(struct regulator_dev *rdev)
53{
54 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
55
56 return cpr_vreg->enabled;
57}
58
59static int cpr_regulator_enable(struct regulator_dev *rdev)
60{
61 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
62 int rc;
63
64 rc = regulator_enable(cpr_vreg->vdd_apc);
65 if (!rc)
66 cpr_vreg->enabled = true;
67 return rc;
68}
69
70static int cpr_regulator_disable(struct regulator_dev *rdev)
71{
72 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
73 int rc;
74
75 rc = regulator_disable(cpr_vreg->vdd_apc);
76 if (!rc)
77 cpr_vreg->enabled = false;
78 return rc;
79}
80
81static int cpr_regulator_set_voltage(struct regulator_dev *rdev,
82 int min_uV, int max_uV, unsigned *selector)
83{
84 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
85 int rc;
86 int vdd_apc_min, vdd_apc_max;
87
88 vdd_apc_min = cpr_vreg->process_vmax[min_uV];
89 vdd_apc_max = cpr_vreg->process_vmax[CPR_CORNER_SUPER_TURBO];
90 rc = regulator_set_voltage(cpr_vreg->vdd_apc,
91 vdd_apc_min, vdd_apc_max);
92 if (!rc)
93 cpr_vreg->corner = min_uV;
94
95 pr_debug("set [corner:%d] = %d uV: rc=%d\n", min_uV, vdd_apc_min, rc);
96 return rc;
97}
98
99static int cpr_regulator_get_voltage(struct regulator_dev *rdev)
100{
101 struct cpr_regulator *cpr_vreg = rdev_get_drvdata(rdev);
102
103 return cpr_vreg->corner;
104}
105
106static struct regulator_ops cpr_corner_ops = {
107 .enable = cpr_regulator_enable,
108 .disable = cpr_regulator_disable,
109 .is_enabled = cpr_regulator_is_enabled,
110 .set_voltage = cpr_regulator_set_voltage,
111 .get_voltage = cpr_regulator_get_voltage,
112};
113
114static int __init cpr_regulator_pvs_init(struct cpr_regulator *cpr_vreg)
115{
116 void __iomem *efuse_base;
117 u32 efuse_bits;
118 int i, bit_pos;
119 u32 vmax;
120
121 efuse_base = ioremap(cpr_vreg->efuse_phys, 4);
122 if (!efuse_base) {
123 pr_err("Unable to map efuse_phys 0x%x\n",
124 cpr_vreg->efuse_phys);
125 return -EINVAL;
126 }
127
128 efuse_bits = readl_relaxed(efuse_base);
129
130 /* Construct PVS process # from the efuse bits */
131 for (i = 0; i < cpr_vreg->num_efuse_bits; i++) {
132 bit_pos = cpr_vreg->efuse_bit_pos[i];
133 cpr_vreg->pvs_bin |= (efuse_bits & BIT(bit_pos)) ? BIT(i) : 0;
134 }
135
136 cpr_vreg->pvs_process = cpr_vreg->pvs_bin_process[cpr_vreg->pvs_bin];
137 if (cpr_vreg->pvs_process >= NUM_APC_PVS)
138 cpr_vreg->pvs_process = APC_PVS_NO;
139
140 /* Use ceiling voltage of Turbo@Slow for all corners of APC_PVS_NO
141 but use SuperTurbo@Slow for its SuperTurbo */
142 vmax = cpr_vreg->pvs_corner_ceiling[APC_PVS_SLOW][CPR_CORNER_TURBO];
143 for (i = CPR_CORNER_SVS; i <= CPR_CORNER_TURBO; i++)
144 cpr_vreg->pvs_corner_ceiling[APC_PVS_NO][i] = vmax;
145 cpr_vreg->pvs_corner_ceiling[APC_PVS_NO][CPR_CORNER_SUPER_TURBO]
146 = cpr_vreg->pvs_corner_ceiling[APC_PVS_SLOW]
147 [CPR_CORNER_SUPER_TURBO];
148
149 cpr_vreg->process_vmax =
150 cpr_vreg->pvs_corner_ceiling[cpr_vreg->pvs_process];
151
152 iounmap(efuse_base);
153
154 pr_info("PVS Info: efuse_phys=0x%08X, n_bits=%d\n",
155 cpr_vreg->efuse_phys, cpr_vreg->num_efuse_bits);
156 pr_info("PVS Info: efuse=0x%08X, bin=%d, process=%d\n",
157 efuse_bits, cpr_vreg->pvs_bin, cpr_vreg->pvs_process);
158
159 return 0;
160}
161
162static int __init cpr_regulator_apc_init(struct platform_device *pdev,
163 struct cpr_regulator *cpr_vreg)
164{
165 cpr_vreg->vdd_apc = devm_regulator_get(&pdev->dev, "vdd-apc");
166 if (IS_ERR_OR_NULL(cpr_vreg->vdd_apc)) {
167 pr_err("devm_regulator_get: rc=%d\n",
168 (int)PTR_ERR(cpr_vreg->vdd_apc));
169 }
170
171 return PTR_RET(cpr_vreg->vdd_apc);
172}
173
174static void cpr_regulator_apc_exit(struct cpr_regulator *cpr_vreg)
175{
176 if (cpr_vreg->enabled)
177 regulator_disable(cpr_vreg->vdd_apc);
178}
179
180static int __init cpr_regulator_parse_dt(struct platform_device *pdev,
181 struct cpr_regulator *cpr_vreg)
182{
183 struct device_node *of_node = pdev->dev.of_node;
184 struct resource *res;
185 int rc;
186 size_t pvs_bins;
187
188 /* Parse process voltage parameters */
189 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
190 "efuse_phys");
191 if (!res || !res->start) {
192 pr_err("efuse_phys missing: res=%p\n", res);
193 return -EINVAL;
194 }
195 cpr_vreg->efuse_phys = res->start;
196
197 rc = of_property_read_u32(of_node, "qcom,num-efuse-bits",
198 &cpr_vreg->num_efuse_bits);
199 if (rc < 0) {
200 pr_err("num-efuse-bits missing: rc=%d\n", rc);
201 return rc;
202 }
203
204 if (cpr_vreg->num_efuse_bits == 0 ||
205 cpr_vreg->num_efuse_bits > CPR_PVS_EFUSE_BITS_MAX) {
206 pr_err("invalid num-efuse-bits : %d\n",
207 cpr_vreg->num_efuse_bits);
208 return -EINVAL;
209 }
210
211 rc = of_property_read_u32_array(of_node, "qcom,efuse-bit-pos",
212 cpr_vreg->efuse_bit_pos,
213 cpr_vreg->num_efuse_bits);
214 if (rc < 0) {
215 pr_err("efuse-bit-pos missing: rc=%d\n", rc);
216 return rc;
217 }
218
219 pvs_bins = 1 << cpr_vreg->num_efuse_bits;
220 rc = of_property_read_u32_array(of_node, "qcom,pvs-bin-process",
221 cpr_vreg->pvs_bin_process,
222 pvs_bins);
223 if (rc < 0) {
224 pr_err("pvs-bin-process missing: rc=%d\n", rc);
225 return rc;
226 }
227
228 rc = of_property_read_u32_array(of_node,
229 "qcom,pvs-corner-ceiling-slow",
230 &cpr_vreg->pvs_corner_ceiling[APC_PVS_SLOW][CPR_CORNER_SVS],
231 CPR_CORNER_MAX - CPR_CORNER_SVS);
232 if (rc < 0) {
233 pr_err("pvs-corner-ceiling-slow missing: rc=%d\n", rc);
234 return rc;
235 }
236
237 rc = of_property_read_u32_array(of_node,
238 "qcom,pvs-corner-ceiling-nom",
239 &cpr_vreg->pvs_corner_ceiling[APC_PVS_NOM][CPR_CORNER_SVS],
240 CPR_CORNER_MAX - CPR_CORNER_SVS);
241 if (rc < 0) {
242 pr_err("pvs-corner-ceiling-norm missing: rc=%d\n", rc);
243 return rc;
244 }
245
246 rc = of_property_read_u32_array(of_node,
247 "qcom,pvs-corner-ceiling-fast",
248 &cpr_vreg->pvs_corner_ceiling[APC_PVS_FAST][CPR_CORNER_SVS],
249 CPR_CORNER_MAX - CPR_CORNER_SVS);
250 if (rc < 0) {
251 pr_err("pvs-corner-ceiling-fast missing: rc=%d\n", rc);
252 return rc;
253 }
254
255 return 0;
256}
257
258static int __devinit cpr_regulator_probe(struct platform_device *pdev)
259{
260 struct cpr_regulator *cpr_vreg;
261 struct regulator_desc *rdesc;
262 struct regulator_init_data *init_data = pdev->dev.platform_data;
263 int rc;
264
265 if (!pdev->dev.of_node) {
266 pr_err("Device tree node is missing\n");
267 return -EINVAL;
268 }
269
270 init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node);
271 if (!init_data) {
272 pr_err("regulator init data is missing\n");
273 return -EINVAL;
274 } else {
275 init_data->constraints.input_uV
276 = init_data->constraints.max_uV;
277 init_data->constraints.valid_ops_mask
278 |= REGULATOR_CHANGE_VOLTAGE | REGULATOR_CHANGE_STATUS;
279 }
280
281 cpr_vreg = devm_kzalloc(&pdev->dev, sizeof(struct cpr_regulator),
282 GFP_KERNEL);
283 if (!cpr_vreg) {
284 pr_err("Can't allocate cpr_regulator memory\n");
285 return -ENOMEM;
286 }
287
288 rc = cpr_regulator_parse_dt(pdev, cpr_vreg);
289 if (rc) {
290 pr_err("Wrong DT parameter specified: rc=%d\n", rc);
291 return rc;
292 }
293
294 rc = cpr_regulator_pvs_init(cpr_vreg);
295 if (rc) {
296 pr_err("Initialize PVS wrong: rc=%d\n", rc);
297 return rc;
298 }
299
300 rc = cpr_regulator_apc_init(pdev, cpr_vreg);
301 if (rc) {
302 if (rc != -EPROBE_DEFER)
303 pr_err("Initialize APC wrong: rc=%d\n", rc);
304 return rc;
305 }
306
307 rdesc = &cpr_vreg->rdesc;
308 rdesc->owner = THIS_MODULE;
309 rdesc->type = REGULATOR_VOLTAGE;
310 rdesc->ops = &cpr_corner_ops;
311 rdesc->name = init_data->constraints.name;
312
313 cpr_vreg->rdev = regulator_register(rdesc, &pdev->dev, init_data,
314 cpr_vreg, pdev->dev.of_node);
315 if (IS_ERR(cpr_vreg->rdev)) {
316 rc = PTR_ERR(cpr_vreg->rdev);
317 pr_err("regulator_register failed: rc=%d\n", rc);
318
319 cpr_regulator_apc_exit(cpr_vreg);
320 return rc;
321 }
322
323 platform_set_drvdata(pdev, cpr_vreg);
324
325 pr_info("PVS [%d %d %d %d] uV\n",
326 cpr_vreg->process_vmax[CPR_CORNER_SVS],
327 cpr_vreg->process_vmax[CPR_CORNER_NORMAL],
328 cpr_vreg->process_vmax[CPR_CORNER_TURBO],
329 cpr_vreg->process_vmax[CPR_CORNER_SUPER_TURBO]);
330
331 return 0;
332}
333
334static int __devexit cpr_regulator_remove(struct platform_device *pdev)
335{
336 struct cpr_regulator *cpr_vreg;
337
338 cpr_vreg = platform_get_drvdata(pdev);
339 if (cpr_vreg) {
340 cpr_regulator_apc_exit(cpr_vreg);
341 regulator_unregister(cpr_vreg->rdev);
342 }
343
344 return 0;
345}
346
347static struct of_device_id cpr_regulator_match_table[] = {
348 { .compatible = CPR_REGULATOR_DRIVER_NAME, },
349 {}
350};
351
352static struct platform_driver cpr_regulator_driver = {
353 .driver = {
354 .name = CPR_REGULATOR_DRIVER_NAME,
355 .of_match_table = cpr_regulator_match_table,
356 .owner = THIS_MODULE,
357 },
358 .probe = cpr_regulator_probe,
359 .remove = __devexit_p(cpr_regulator_remove),
360};
361
362/**
363 * cpr_regulator_init() - register cpr-regulator driver
364 *
365 * This initialization function should be called in systems in which driver
366 * registration ordering must be controlled precisely.
367 */
368int __init cpr_regulator_init(void)
369{
370 static bool initialized;
371
372 if (initialized)
373 return 0;
374 else
375 initialized = true;
376
377 return platform_driver_register(&cpr_regulator_driver);
378}
379EXPORT_SYMBOL(cpr_regulator_init);
380
381static void __exit cpr_regulator_exit(void)
382{
383 platform_driver_unregister(&cpr_regulator_driver);
384}
385
386MODULE_DESCRIPTION("CPR regulator driver");
387MODULE_LICENSE("GPL v2");
388
389arch_initcall(cpr_regulator_init);
390module_exit(cpr_regulator_exit);