blob: 48f0ca90743cca5cec830e6b2ce74fa9f7e8d0a8 [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 {
Arnd Bergmann33f06982017-01-25 23:20:59 +010029 struct of_regulator_match rdata[S2MPA01_REGULATOR_MAX];
Sachin Kamatf1879272014-03-14 17:21:58 +053030 int ramp_delay24;
31 int ramp_delay3;
32 int ramp_delay5;
33 int ramp_delay16;
34 int ramp_delay7;
35 int ramp_delay8910;
36};
37
38static int get_ramp_delay(int ramp_delay)
39{
40 unsigned char cnt = 0;
41
42 ramp_delay /= 6250;
43
44 while (true) {
45 ramp_delay = ramp_delay >> 1;
46 if (ramp_delay == 0)
47 break;
48 cnt++;
49 }
50
51 if (cnt > 3)
52 cnt = 3;
53
54 return cnt;
55}
56
57static int s2mpa01_regulator_set_voltage_time_sel(struct regulator_dev *rdev,
58 unsigned int old_selector,
59 unsigned int new_selector)
60{
61 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
62 unsigned int ramp_delay = 0;
63 int old_volt, new_volt;
64
Krzysztof Kozlowski06080322014-05-06 08:37:38 +020065 switch (rdev_get_id(rdev)) {
Sachin Kamatf1879272014-03-14 17:21:58 +053066 case S2MPA01_BUCK2:
67 case S2MPA01_BUCK4:
68 ramp_delay = s2mpa01->ramp_delay24;
69 break;
70 case S2MPA01_BUCK3:
71 ramp_delay = s2mpa01->ramp_delay3;
72 break;
73 case S2MPA01_BUCK5:
74 ramp_delay = s2mpa01->ramp_delay5;
75 break;
76 case S2MPA01_BUCK1:
77 case S2MPA01_BUCK6:
78 ramp_delay = s2mpa01->ramp_delay16;
79 break;
80 case S2MPA01_BUCK7:
81 ramp_delay = s2mpa01->ramp_delay7;
82 break;
83 case S2MPA01_BUCK8:
84 case S2MPA01_BUCK9:
85 case S2MPA01_BUCK10:
86 ramp_delay = s2mpa01->ramp_delay8910;
87 break;
88 }
89
90 if (ramp_delay == 0)
91 ramp_delay = rdev->desc->ramp_delay;
92
93 old_volt = rdev->desc->min_uV + (rdev->desc->uV_step * old_selector);
94 new_volt = rdev->desc->min_uV + (rdev->desc->uV_step * new_selector);
95
96 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
97}
98
99static int s2mpa01_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
100{
101 struct s2mpa01_info *s2mpa01 = rdev_get_drvdata(rdev);
102 unsigned int ramp_val, ramp_shift, ramp_reg = S2MPA01_REG_RAMP2;
103 unsigned int ramp_enable = 1, enable_shift = 0;
104 int ret;
105
Krzysztof Kozlowski06080322014-05-06 08:37:38 +0200106 switch (rdev_get_id(rdev)) {
Sachin Kamatf1879272014-03-14 17:21:58 +0530107 case S2MPA01_BUCK1:
108 enable_shift = S2MPA01_BUCK1_RAMP_EN_SHIFT;
109 if (!ramp_delay) {
110 ramp_enable = 0;
111 break;
112 }
113
114 if (ramp_delay > s2mpa01->ramp_delay16)
115 s2mpa01->ramp_delay16 = ramp_delay;
116 else
117 ramp_delay = s2mpa01->ramp_delay16;
118
119 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
Sachin Kamatf1879272014-03-14 17:21:58 +0530120 break;
121 case S2MPA01_BUCK2:
122 enable_shift = S2MPA01_BUCK2_RAMP_EN_SHIFT;
123 if (!ramp_delay) {
124 ramp_enable = 0;
125 break;
126 }
127
128 if (ramp_delay > s2mpa01->ramp_delay24)
129 s2mpa01->ramp_delay24 = ramp_delay;
130 else
131 ramp_delay = s2mpa01->ramp_delay24;
132
133 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
134 ramp_reg = S2MPA01_REG_RAMP1;
135 break;
136 case S2MPA01_BUCK3:
137 enable_shift = S2MPA01_BUCK3_RAMP_EN_SHIFT;
138 if (!ramp_delay) {
139 ramp_enable = 0;
140 break;
141 }
142
143 s2mpa01->ramp_delay3 = ramp_delay;
144 ramp_shift = S2MPA01_BUCK3_RAMP_SHIFT;
145 ramp_reg = S2MPA01_REG_RAMP1;
146 break;
147 case S2MPA01_BUCK4:
148 enable_shift = S2MPA01_BUCK4_RAMP_EN_SHIFT;
149 if (!ramp_delay) {
150 ramp_enable = 0;
151 break;
152 }
153
154 if (ramp_delay > s2mpa01->ramp_delay24)
155 s2mpa01->ramp_delay24 = ramp_delay;
156 else
157 ramp_delay = s2mpa01->ramp_delay24;
158
159 ramp_shift = S2MPA01_BUCK24_RAMP_SHIFT;
160 ramp_reg = S2MPA01_REG_RAMP1;
161 break;
162 case S2MPA01_BUCK5:
163 s2mpa01->ramp_delay5 = ramp_delay;
164 ramp_shift = S2MPA01_BUCK5_RAMP_SHIFT;
165 break;
166 case S2MPA01_BUCK6:
167 if (ramp_delay > s2mpa01->ramp_delay16)
168 s2mpa01->ramp_delay16 = ramp_delay;
169 else
170 ramp_delay = s2mpa01->ramp_delay16;
171
172 ramp_shift = S2MPA01_BUCK16_RAMP_SHIFT;
173 break;
174 case S2MPA01_BUCK7:
175 s2mpa01->ramp_delay7 = ramp_delay;
176 ramp_shift = S2MPA01_BUCK7_RAMP_SHIFT;
177 break;
178 case S2MPA01_BUCK8:
179 case S2MPA01_BUCK9:
180 case S2MPA01_BUCK10:
181 if (ramp_delay > s2mpa01->ramp_delay8910)
182 s2mpa01->ramp_delay8910 = ramp_delay;
183 else
184 ramp_delay = s2mpa01->ramp_delay8910;
185
186 ramp_shift = S2MPA01_BUCK8910_RAMP_SHIFT;
187 break;
188 default:
189 return 0;
190 }
191
192 if (!ramp_enable)
193 goto ramp_disable;
194
Krzysztof Kozlowski51e2fc02014-05-06 08:37:37 +0200195 /* Ramp delay can be enabled/disabled only for buck[1234] */
196 if (rdev_get_id(rdev) >= S2MPA01_BUCK1 &&
197 rdev_get_id(rdev) <= S2MPA01_BUCK4) {
198 ret = regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
199 1 << enable_shift, 1 << enable_shift);
200 if (ret) {
201 dev_err(&rdev->dev, "failed to enable ramp rate\n");
202 return ret;
203 }
Sachin Kamatf1879272014-03-14 17:21:58 +0530204 }
205
206 ramp_val = get_ramp_delay(ramp_delay);
207
208 return regmap_update_bits(rdev->regmap, ramp_reg, 0x3 << ramp_shift,
209 ramp_val << ramp_shift);
210
211ramp_disable:
212 return regmap_update_bits(rdev->regmap, S2MPA01_REG_RAMP1,
213 1 << enable_shift, 0);
214}
215
Krzysztof Kozlowskif465bf92017-03-11 21:01:21 +0200216static const struct regulator_ops s2mpa01_ldo_ops = {
Sachin Kamatf1879272014-03-14 17:21:58 +0530217 .list_voltage = regulator_list_voltage_linear,
218 .map_voltage = regulator_map_voltage_linear,
219 .is_enabled = regulator_is_enabled_regmap,
220 .enable = regulator_enable_regmap,
221 .disable = regulator_disable_regmap,
222 .get_voltage_sel = regulator_get_voltage_sel_regmap,
223 .set_voltage_sel = regulator_set_voltage_sel_regmap,
224 .set_voltage_time_sel = regulator_set_voltage_time_sel,
225};
226
Krzysztof Kozlowskif465bf92017-03-11 21:01:21 +0200227static const struct regulator_ops s2mpa01_buck_ops = {
Sachin Kamatf1879272014-03-14 17:21:58 +0530228 .list_voltage = regulator_list_voltage_linear,
229 .map_voltage = regulator_map_voltage_linear,
230 .is_enabled = regulator_is_enabled_regmap,
231 .enable = regulator_enable_regmap,
232 .disable = regulator_disable_regmap,
233 .get_voltage_sel = regulator_get_voltage_sel_regmap,
234 .set_voltage_sel = regulator_set_voltage_sel_regmap,
235 .set_voltage_time_sel = s2mpa01_regulator_set_voltage_time_sel,
236 .set_ramp_delay = s2mpa01_set_ramp_delay,
237};
238
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530239#define regulator_desc_ldo(num, step) { \
Sachin Kamatf1879272014-03-14 17:21:58 +0530240 .name = "LDO"#num, \
241 .id = S2MPA01_LDO##num, \
242 .ops = &s2mpa01_ldo_ops, \
243 .type = REGULATOR_VOLTAGE, \
244 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530245 .min_uV = MIN_800_MV, \
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530246 .uV_step = step, \
Sachin Kamatf1879272014-03-14 17:21:58 +0530247 .n_voltages = S2MPA01_LDO_N_VOLTAGES, \
248 .vsel_reg = S2MPA01_REG_L1CTRL + num - 1, \
249 .vsel_mask = S2MPA01_LDO_VSEL_MASK, \
250 .enable_reg = S2MPA01_REG_L1CTRL + num - 1, \
251 .enable_mask = S2MPA01_ENABLE_MASK \
252}
253
254#define regulator_desc_buck1_4(num) { \
255 .name = "BUCK"#num, \
256 .id = S2MPA01_BUCK##num, \
257 .ops = &s2mpa01_buck_ops, \
258 .type = REGULATOR_VOLTAGE, \
259 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530260 .min_uV = MIN_600_MV, \
261 .uV_step = STEP_6_25_MV, \
Sachin Kamatf1879272014-03-14 17:21:58 +0530262 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
263 .ramp_delay = S2MPA01_RAMP_DELAY, \
264 .vsel_reg = S2MPA01_REG_B1CTRL2 + (num - 1) * 2, \
265 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
266 .enable_reg = S2MPA01_REG_B1CTRL1 + (num - 1) * 2, \
267 .enable_mask = S2MPA01_ENABLE_MASK \
268}
269
270#define regulator_desc_buck5 { \
271 .name = "BUCK5", \
272 .id = S2MPA01_BUCK5, \
273 .ops = &s2mpa01_buck_ops, \
274 .type = REGULATOR_VOLTAGE, \
275 .owner = THIS_MODULE, \
Amit Daniel Kachhap0e4f4172014-07-15 16:32:51 +0530276 .min_uV = MIN_800_MV, \
277 .uV_step = STEP_6_25_MV, \
Sachin Kamatf1879272014-03-14 17:21:58 +0530278 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
279 .ramp_delay = S2MPA01_RAMP_DELAY, \
280 .vsel_reg = S2MPA01_REG_B5CTRL2, \
281 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
282 .enable_reg = S2MPA01_REG_B5CTRL1, \
283 .enable_mask = S2MPA01_ENABLE_MASK \
284}
285
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530286#define regulator_desc_buck6_10(num, min, step) { \
Sachin Kamatf1879272014-03-14 17:21:58 +0530287 .name = "BUCK"#num, \
288 .id = S2MPA01_BUCK##num, \
289 .ops = &s2mpa01_buck_ops, \
290 .type = REGULATOR_VOLTAGE, \
291 .owner = THIS_MODULE, \
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530292 .min_uV = min, \
293 .uV_step = step, \
Sachin Kamatf1879272014-03-14 17:21:58 +0530294 .n_voltages = S2MPA01_BUCK_N_VOLTAGES, \
295 .ramp_delay = S2MPA01_RAMP_DELAY, \
296 .vsel_reg = S2MPA01_REG_B6CTRL2 + (num - 6) * 2, \
297 .vsel_mask = S2MPA01_BUCK_VSEL_MASK, \
298 .enable_reg = S2MPA01_REG_B6CTRL1 + (num - 6) * 2, \
299 .enable_mask = S2MPA01_ENABLE_MASK \
300}
301
Krzysztof Kozlowski4b8e43f2014-10-27 16:44:14 +0100302static const struct regulator_desc regulators[] = {
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530303 regulator_desc_ldo(1, STEP_25_MV),
304 regulator_desc_ldo(2, STEP_50_MV),
305 regulator_desc_ldo(3, STEP_50_MV),
306 regulator_desc_ldo(4, STEP_50_MV),
307 regulator_desc_ldo(5, STEP_50_MV),
308 regulator_desc_ldo(6, STEP_25_MV),
309 regulator_desc_ldo(7, STEP_50_MV),
310 regulator_desc_ldo(8, STEP_50_MV),
311 regulator_desc_ldo(9, STEP_50_MV),
312 regulator_desc_ldo(10, STEP_50_MV),
313 regulator_desc_ldo(11, STEP_25_MV),
314 regulator_desc_ldo(12, STEP_50_MV),
315 regulator_desc_ldo(13, STEP_50_MV),
316 regulator_desc_ldo(14, STEP_50_MV),
317 regulator_desc_ldo(15, STEP_50_MV),
318 regulator_desc_ldo(16, STEP_50_MV),
319 regulator_desc_ldo(17, STEP_50_MV),
320 regulator_desc_ldo(18, STEP_50_MV),
321 regulator_desc_ldo(19, STEP_50_MV),
322 regulator_desc_ldo(20, STEP_50_MV),
323 regulator_desc_ldo(21, STEP_50_MV),
324 regulator_desc_ldo(22, STEP_25_MV),
325 regulator_desc_ldo(23, STEP_25_MV),
326 regulator_desc_ldo(24, STEP_50_MV),
327 regulator_desc_ldo(25, STEP_50_MV),
328 regulator_desc_ldo(26, STEP_50_MV),
Sachin Kamatf1879272014-03-14 17:21:58 +0530329 regulator_desc_buck1_4(1),
330 regulator_desc_buck1_4(2),
331 regulator_desc_buck1_4(3),
332 regulator_desc_buck1_4(4),
333 regulator_desc_buck5,
Amit Daniel Kachhapd264fd42014-07-15 16:32:52 +0530334 regulator_desc_buck6_10(6, MIN_600_MV, STEP_6_25_MV),
335 regulator_desc_buck6_10(7, MIN_600_MV, STEP_6_25_MV),
336 regulator_desc_buck6_10(8, MIN_800_MV, STEP_12_5_MV),
337 regulator_desc_buck6_10(9, MIN_1500_MV, STEP_12_5_MV),
338 regulator_desc_buck6_10(10, MIN_1000_MV, STEP_12_5_MV),
Sachin Kamatf1879272014-03-14 17:21:58 +0530339};
340
341static int s2mpa01_pmic_probe(struct platform_device *pdev)
342{
343 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
344 struct sec_platform_data *pdata = dev_get_platdata(iodev->dev);
Sachin Kamatf1879272014-03-14 17:21:58 +0530345 struct device_node *reg_np = NULL;
346 struct regulator_config config = { };
Arnd Bergmann33f06982017-01-25 23:20:59 +0100347 struct of_regulator_match *rdata;
Sachin Kamatf1879272014-03-14 17:21:58 +0530348 struct s2mpa01_info *s2mpa01;
349 int i;
350
351 s2mpa01 = devm_kzalloc(&pdev->dev, sizeof(*s2mpa01), GFP_KERNEL);
352 if (!s2mpa01)
353 return -ENOMEM;
354
Arnd Bergmann33f06982017-01-25 23:20:59 +0100355 rdata = s2mpa01->rdata;
Sachin Kamatf1879272014-03-14 17:21:58 +0530356 for (i = 0; i < S2MPA01_REGULATOR_CNT; i++)
357 rdata[i].name = regulators[i].name;
358
359 if (iodev->dev->of_node) {
360 reg_np = of_get_child_by_name(iodev->dev->of_node,
361 "regulators");
Krzysztof Kozlowski5339c342017-03-11 21:01:24 +0200362 if (!reg_np) {
363 dev_err(&pdev->dev,
364 "could not find regulators sub-node\n");
365 return -EINVAL;
366 }
Sachin Kamatf1879272014-03-14 17:21:58 +0530367
368 of_regulator_match(&pdev->dev, reg_np, rdata,
369 S2MPA01_REGULATOR_MAX);
370 of_node_put(reg_np);
371 }
372
373 platform_set_drvdata(pdev, s2mpa01);
374
375 config.dev = &pdev->dev;
376 config.regmap = iodev->regmap_pmic;
377 config.driver_data = s2mpa01;
378
379 for (i = 0; i < S2MPA01_REGULATOR_MAX; i++) {
380 struct regulator_dev *rdev;
381 if (pdata)
382 config.init_data = pdata->regulators[i].initdata;
383 else
384 config.init_data = rdata[i].init_data;
385
386 if (reg_np)
387 config.of_node = rdata[i].of_node;
388
389 rdev = devm_regulator_register(&pdev->dev,
390 &regulators[i], &config);
391 if (IS_ERR(rdev)) {
392 dev_err(&pdev->dev, "regulator init failed for %d\n",
393 i);
394 return PTR_ERR(rdev);
395 }
396 }
397
398 return 0;
399}
400
401static const struct platform_device_id s2mpa01_pmic_id[] = {
402 { "s2mpa01-pmic", 0},
403 { },
404};
405MODULE_DEVICE_TABLE(platform, s2mpa01_pmic_id);
406
407static struct platform_driver s2mpa01_pmic_driver = {
408 .driver = {
409 .name = "s2mpa01-pmic",
Sachin Kamatf1879272014-03-14 17:21:58 +0530410 },
411 .probe = s2mpa01_pmic_probe,
412 .id_table = s2mpa01_pmic_id,
413};
414
415module_platform_driver(s2mpa01_pmic_driver);
416
417/* Module information */
418MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
419MODULE_AUTHOR("Sachin Kamat <sachin.kamat@samsung.com>");
420MODULE_DESCRIPTION("SAMSUNG S2MPA01 Regulator Driver");
421MODULE_LICENSE("GPL");