blob: ee83b4876420b776229100fa70363e18909d2c21 [file] [log] [blame]
Sachin Kamatf1879272014-03-14 17:21:58 +05301/*
2 * Copyright (c) 2013 Samsung Electronics Co., Ltd
3 * http://www.samsung.com
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 */
11
12#include <linux/bug.h>
13#include <linux/err.h>
14#include <linux/gpio.h>
15#include <linux/slab.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/regmap.h>
19#include <linux/platform_device.h>
20#include <linux/regulator/driver.h>
21#include <linux/regulator/machine.h>
22#include <linux/regulator/of_regulator.h>
23#include <linux/mfd/samsung/core.h>
24#include <linux/mfd/samsung/s2mpa01.h>
25
26#define S2MPA01_REGULATOR_CNT ARRAY_SIZE(regulators)
27
28struct s2mpa01_info {
29 int ramp_delay24;
30 int ramp_delay3;
31 int ramp_delay5;
32 int ramp_delay16;
33 int ramp_delay7;
34 int ramp_delay8910;
35};
36
37static int get_ramp_delay(int ramp_delay)
38{
39 unsigned char cnt = 0;
40
41 ramp_delay /= 6250;
42
43 while (true) {
44 ramp_delay = ramp_delay >> 1;
45 if (ramp_delay == 0)
46 break;
47 cnt++;
48 }
49
50 if (cnt > 3)
51 cnt = 3;
52
53 return cnt;
54}
55
56static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
57 unsigned int old_selector,
58 unsigned int new_selector)
59{
60 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
61 unsigned int ramp_delay = 0;
62 int old_volt, new_volt;
63
Krzysztof Kozlowski06080322014-05-06 08:37:38 +020064 switch (rdev_get_id(rdev)) {
Sachin Kamatf1879272014-03-14 17:21:58 +053065 case S2MPA01_BUCK2:
66 case S2MPA01_BUCK4:
67 ramp_delay = s2mpa01->ramp_delay24;
68 break;
69 case S2MPA01_BUCK3:
70 ramp_delay = s2mpa01->ramp_delay3;
71 break;
72 case S2MPA01_BUCK5:
73 ramp_delay = s2mpa01->ramp_delay5;
74 break;
75 case S2MPA01_BUCK1:
76 case S2MPA01_BUCK6:
77 ramp_delay = s2mpa01->ramp_delay16;
78 break;
79 case S2MPA01_BUCK7:
80 ramp_delay = s2mpa01->ramp_delay7;
81 break;
82 case S2MPA01_BUCK8:
83 case S2MPA01_BUCK9:
84 case S2MPA01_BUCK10:
85 ramp_delay = s2mpa01->ramp_delay8910;
86 break;
87 }
88
89 if (ramp_delay == 0)
90 ramp_delay = rdev->desc->ramp_delay;
91
92 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
93 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
94
95 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
96}
97
98static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
99{
100 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
101 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
102 unsigned int ramp_enable = 1, enable_shift = 0;
103 int ret;
104
Krzysztof Kozlowski06080322014-05-06 08:37:38 +0200105 switch (rdev_get_id(rdev)) {
Sachin Kamatf1879272014-03-14 17:21:58 +0530106 case S2MPA01_BUCK1:
107 enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
108 if (!ramp_delay) {
109 ramp_enable = 0;
110 break;
111 }
112
113 if (ramp_delay > s2mpa01->ramp_delay16)
114 s2mpa01->ramp_delay16 = ramp_delay;
115 else
116 ramp_delay = s2mpa01->ramp_delay16;
117
118 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
Sachin Kamatf1879272014-03-14 17:21:58 +0530119 break;
120 case S2MPA01_BUCK2:
121 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
122 if (!ramp_delay) {
123 ramp_enable = 0;
124 break;
125 }
126
127 if (ramp_delay > s2mpa01->ramp_delay24)
128 s2mpa01->ramp_delay24 = ramp_delay;
129 else
130 ramp_delay = s2mpa01->ramp_delay24;
131
132 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
133 ramp_reg = S2MPA01_REG_RAMP1;
134 break;
135 case S2MPA01_BUCK3:
136 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
137 if (!ramp_delay) {
138 ramp_enable = 0;
139 break;
140 }
141
142 s2mpa01->ramp_delay3 = ramp_delay;
143 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
144 ramp_reg = S2MPA01_REG_RAMP1;
145 break;
146 case S2MPA01_BUCK4:
147 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
148 if (!ramp_delay) {
149 ramp_enable = 0;
150 break;
151 }
152
153 if (ramp_delay > s2mpa01->ramp_delay24)
154 s2mpa01->ramp_delay24 = ramp_delay;
155 else
156 ramp_delay = s2mpa01->ramp_delay24;
157
158 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
159 ramp_reg = S2MPA01_REG_RAMP1;
160 break;
161 case S2MPA01_BUCK5:
162 s2mpa01->ramp_delay5 = ramp_delay;
163 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
164 break;
165 case S2MPA01_BUCK6:
166 if (ramp_delay > s2mpa01->ramp_delay16)
167 s2mpa01->ramp_delay16 = ramp_delay;
168 else
169 ramp_delay = s2mpa01->ramp_delay16;
170
171 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
172 break;
173 case S2MPA01_BUCK7:
174 s2mpa01->ramp_delay7 = ramp_delay;
175 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
176 break;
177 case S2MPA01_BUCK8:
178 case S2MPA01_BUCK9:
179 case S2MPA01_BUCK10:
180 if (ramp_delay > s2mpa01->ramp_delay8910)
181 s2mpa01->ramp_delay8910 = ramp_delay;
182 else
183 ramp_delay = s2mpa01->ramp_delay8910;
184
185 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
186 break;
187 default:
188 return 0;
189 }
190
191 if (!ramp_enable)
192 goto ramp_disable;
193
Krzysztof Kozlowski51e2fc02014-05-06 08:37:37 +0200194 /* Ramp delay can be enabled/disabled only for buck[1234] */
195 if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
196 rdev_get_id(rdev) <= S2MPA01_BUCK4) {
197 ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
198 1 << enable_shift, 1 << enable_shift);
199 if (ret) {
200 dev_err(&rdev->dev, "failed to enable ramp rate\n");
201 return ret;
202 }
Sachin Kamatf1879272014-03-14 17:21:58 +0530203 }
204
205 ramp_val = get_ramp_delay(ramp_delay);
206
207 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
208 ramp_val << ramp_shift);
209
210ramp_disable:
211 return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
212 1 << enable_shift, 0);
213}
214
215static struct regulator_ops s2mpa01_ldo_ops = {
216 .list_voltage = regulator_list_voltage_linear,
217 .map_voltage = regulator_map_voltage_linear,
218 .is_enabled = regulator_is_enabled_regmap,
219 .enable = regulator_enable_regmap,
220 .disable = regulator_disable_regmap,
221 .get_voltage_sel = regulator_get_voltage_sel_regmap,
222 .set_voltage_sel = regulator_set_voltage_sel_regmap,
223 .set_voltage_time_sel = regulator_set_voltage_time_sel,
224};
225
226static struct regulator_ops s2mpa01_buck_ops = {
227 .list_voltage = regulator_list_voltage_linear,
228 .map_voltage = regulator_map_voltage_linear,
229 .is_enabled = regulator_is_enabled_regmap,
230 .enable = regulator_enable_regmap,
231 .disable = regulator_disable_regmap,
232 .get_voltage_sel = regulator_get_voltage_sel_regmap,
233 .set_voltage_sel = regulator_set_voltage_sel_regmap,
234 .set_voltage_time_sel = s2mpa01_regulator_set_voltage_time_sel,
235 .set_ramp_delay = s2mpa01_set_ramp_delay,
236};
237
238#define regulator_desc_ldo1(num) { \
239 .name = "LDO"#num, \
240 .id = S2MPA01_LDO##num, \
241 .ops = &s2mpa01_ldo_ops, \
242 .type = REGULATOR_VOLTAGE, \
243 .owner = THIS_MODULE, \
244 .min_uV = S2MPA01_LDO_MIN, \
245 .uV_step = S2MPA01_LDO_STEP1, \
246 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
247 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
248 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
249 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
250 .enable_mask = S2MPA01_ENABLE_MASK \
251}
252#define regulator_desc_ldo2(num) { \
253 .name = "LDO"#num, \
254 .id = S2MPA01_LDO##num, \
255 .ops = &s2mpa01_ldo_ops, \
256 .type = REGULATOR_VOLTAGE, \
257 .owner = THIS_MODULE, \
258 .min_uV = S2MPA01_LDO_MIN, \
259 .uV_step = S2MPA01_LDO_STEP2, \
260 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
261 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
262 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
263 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
264 .enable_mask = S2MPA01_ENABLE_MASK \
265}
266
267#define regulator_desc_buck1_4(num) { \
268 .name = "BUCK"#num, \
269 .id = S2MPA01_BUCK##num, \
270 .ops = &s2mpa01_buck_ops, \
271 .type = REGULATOR_VOLTAGE, \
272 .owner = THIS_MODULE, \
273 .min_uV = S2MPA01_BUCK_MIN1, \
274 .uV_step = S2MPA01_BUCK_STEP1, \
275 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
276 .ramp_delay = S2MPA01_RAMP_DELAY, \
277 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
278 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
279 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
280 .enable_mask = S2MPA01_ENABLE_MASK \
281}
282
283#define regulator_desc_buck5 { \
284 .name = "BUCK5", \
285 .id = S2MPA01_BUCK5, \
286 .ops = &s2mpa01_buck_ops, \
287 .type = REGULATOR_VOLTAGE, \
288 .owner = THIS_MODULE, \
289 .min_uV = S2MPA01_BUCK_MIN2, \
290 .uV_step = S2MPA01_BUCK_STEP1, \
291 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
292 .ramp_delay = S2MPA01_RAMP_DELAY, \
293 .vsel_reg = S2MPA01_REG_B5CTRL2, \
294 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
295 .enable_reg = S2MPA01_REG_B5CTRL1, \
296 .enable_mask = S2MPA01_ENABLE_MASK \
297}
298
299#define regulator_desc_buck6_7(num) { \
300 .name = "BUCK"#num, \
301 .id = S2MPA01_BUCK##num, \
302 .ops = &s2mpa01_buck_ops, \
303 .type = REGULATOR_VOLTAGE, \
304 .owner = THIS_MODULE, \
305 .min_uV = S2MPA01_BUCK_MIN1, \
306 .uV_step = S2MPA01_BUCK_STEP1, \
307 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
308 .ramp_delay = S2MPA01_RAMP_DELAY, \
309 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
310 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
311 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
312 .enable_mask = S2MPA01_ENABLE_MASK \
313}
314
315#define regulator_desc_buck8 { \
316 .name = "BUCK8", \
317 .id = S2MPA01_BUCK8, \
318 .ops = &s2mpa01_buck_ops, \
319 .type = REGULATOR_VOLTAGE, \
320 .owner = THIS_MODULE, \
321 .min_uV = S2MPA01_BUCK_MIN2, \
322 .uV_step = S2MPA01_BUCK_STEP2, \
323 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
324 .ramp_delay = S2MPA01_RAMP_DELAY, \
325 .vsel_reg = S2MPA01_REG_B8CTRL2, \
326 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
327 .enable_reg = S2MPA01_REG_B8CTRL1, \
328 .enable_mask = S2MPA01_ENABLE_MASK \
329}
330
331#define regulator_desc_buck9 { \
332 .name = "BUCK9", \
333 .id = S2MPA01_BUCK9, \
334 .ops = &s2mpa01_buck_ops, \
335 .type = REGULATOR_VOLTAGE, \
336 .owner = THIS_MODULE, \
337 .min_uV = S2MPA01_BUCK_MIN4, \
338 .uV_step = S2MPA01_BUCK_STEP2, \
339 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
340 .ramp_delay = S2MPA01_RAMP_DELAY, \
341 .vsel_reg = S2MPA01_REG_B9CTRL2, \
342 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
343 .enable_reg = S2MPA01_REG_B9CTRL1, \
344 .enable_mask = S2MPA01_ENABLE_MASK \
345}
346
347#define regulator_desc_buck10 { \
348 .name = "BUCK10", \
349 .id = S2MPA01_BUCK10, \
350 .ops = &s2mpa01_buck_ops, \
351 .type = REGULATOR_VOLTAGE, \
352 .owner = THIS_MODULE, \
353 .min_uV = S2MPA01_BUCK_MIN3, \
354 .uV_step = S2MPA01_BUCK_STEP2, \
355 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
356 .ramp_delay = S2MPA01_RAMP_DELAY, \
357 .vsel_reg = S2MPA01_REG_B10CTRL2, \
358 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
359 .enable_reg = S2MPA01_REG_B10CTRL1, \
360 .enable_mask = S2MPA01_ENABLE_MASK \
361}
362
363static struct regulator_desc regulators[] = {
364 regulator_desc_ldo2(1),
365 regulator_desc_ldo1(2),
366 regulator_desc_ldo1(3),
367 regulator_desc_ldo1(4),
368 regulator_desc_ldo1(5),
369 regulator_desc_ldo2(6),
370 regulator_desc_ldo1(7),
371 regulator_desc_ldo1(8),
372 regulator_desc_ldo1(9),
373 regulator_desc_ldo1(10),
374 regulator_desc_ldo2(11),
375 regulator_desc_ldo1(12),
376 regulator_desc_ldo1(13),
377 regulator_desc_ldo1(14),
378 regulator_desc_ldo1(15),
379 regulator_desc_ldo1(16),
380 regulator_desc_ldo1(17),
381 regulator_desc_ldo1(18),
382 regulator_desc_ldo1(19),
383 regulator_desc_ldo1(20),
384 regulator_desc_ldo1(21),
385 regulator_desc_ldo2(22),
386 regulator_desc_ldo2(23),
387 regulator_desc_ldo1(24),
388 regulator_desc_ldo1(25),
389 regulator_desc_ldo1(26),
390 regulator_desc_buck1_4(1),
391 regulator_desc_buck1_4(2),
392 regulator_desc_buck1_4(3),
393 regulator_desc_buck1_4(4),
394 regulator_desc_buck5,
395 regulator_desc_buck6_7(6),
396 regulator_desc_buck6_7(7),
397 regulator_desc_buck8,
398 regulator_desc_buck9,
399 regulator_desc_buck10,
400};
401
402static int s2mpa01_pmic_probe(struct platform_device *pdev)
403{
404 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
405 struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
406 struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX];
407 struct device_node *reg_np = NULL;
408 struct regulator_config config = { };
409 struct s2mpa01_info *s2mpa01;
410 int i;
411
412 s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
413 if (!s2mpa01)
414 return -ENOMEM;
415
416 for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
417 rdata[i].name = regulators[i].name;
418
419 if (iodev->dev->of_node) {
420 reg_np = of_get_child_by_name(iodev->dev->of_node,
421 "regulators");
422 if (!reg_np) {
423 dev_err(&pdev->dev,
424 "could not find regulators sub-node\n");
425 return -EINVAL;
426 }
427
428 of_regulator_match(&pdev->dev, reg_np, rdata,
429 S2MPA01_REGULATOR_MAX);
430 of_node_put(reg_np);
431 }
432
433 platform_set_drvdata(pdev, s2mpa01);
434
435 config.dev = &pdev->dev;
436 config.regmap = iodev->regmap_pmic;
437 config.driver_data = s2mpa01;
438
439 for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
440 struct regulator_dev *rdev;
441 if (pdata)
442 config.init_data = pdata->regulators[i].initdata;
443 else
444 config.init_data = rdata[i].init_data;
445
446 if (reg_np)
447 config.of_node = rdata[i].of_node;
448
449 rdev = devm_regulator_register(&pdev->dev,
450 &regulators[i], &config);
451 if (IS_ERR(rdev)) {
452 dev_err(&pdev->dev, "regulator init failed for %d\n",
453 i);
454 return PTR_ERR(rdev);
455 }
456 }
457
458 return 0;
459}
460
461static const struct platform_device_id s2mpa01_pmic_id[] = {
462 { "s2mpa01-pmic", 0},
463 { },
464};
465MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
466
467static struct platform_driver s2mpa01_pmic_driver = {
468 .driver = {
469 .name = "s2mpa01-pmic",
470 .owner = THIS_MODULE,
471 },
472 .probe = s2mpa01_pmic_probe,
473 .id_table = s2mpa01_pmic_id,
474};
475
476module_platform_driver(s2mpa01_pmic_driver);
477
478/* Module information */
479MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
480MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
481MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
482MODULE_LICENSE("GPL");