regulator: s5m8767: Define symbol for buck control mask
[deliverable/linux.git] / drivers / regulator / s5m8767.c
CommitLineData
9767ec7f
SK
1/*
2 * s5m8767.c
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd
5 * http://www.samsung.com
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 */
13
14#include <linux/bug.h>
9767ec7f
SK
15#include <linux/err.h>
16#include <linux/gpio.h>
26aec009 17#include <linux/of_gpio.h>
9767ec7f
SK
18#include <linux/slab.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/regulator/driver.h>
22#include <linux/regulator/machine.h>
54227bcf
SK
23#include <linux/mfd/samsung/core.h>
24#include <linux/mfd/samsung/s5m8767.h>
26aec009
ADK
25#include <linux/regulator/of_regulator.h>
26
27#define S5M8767_OPMODE_NORMAL_MODE 0x1
9767ec7f
SK
28
29struct s5m8767_info {
30 struct device *dev;
63063bfb 31 struct sec_pmic_dev *iodev;
9767ec7f
SK
32 int num_regulators;
33 struct regulator_dev **rdev;
63063bfb 34 struct sec_opmode_data *opmode;
9767ec7f
SK
35
36 int ramp_delay;
37 bool buck2_ramp;
38 bool buck3_ramp;
39 bool buck4_ramp;
40
41 bool buck2_gpiodvs;
42 bool buck3_gpiodvs;
43 bool buck4_gpiodvs;
44 u8 buck2_vol[8];
45 u8 buck3_vol[8];
46 u8 buck4_vol[8];
47 int buck_gpios[3];
c848bc85 48 int buck_ds[3];
9767ec7f
SK
49 int buck_gpioindex;
50};
51
63063bfb 52struct sec_voltage_desc {
9767ec7f
SK
53 int max;
54 int min;
55 int step;
56};
57
63063bfb 58static const struct sec_voltage_desc buck_voltage_val1 = {
9767ec7f
SK
59 .max = 2225000,
60 .min = 650000,
61 .step = 6250,
62};
63
63063bfb 64static const struct sec_voltage_desc buck_voltage_val2 = {
9767ec7f
SK
65 .max = 1600000,
66 .min = 600000,
67 .step = 6250,
68};
69
63063bfb 70static const struct sec_voltage_desc buck_voltage_val3 = {
9767ec7f
SK
71 .max = 3000000,
72 .min = 750000,
73 .step = 12500,
74};
75
63063bfb 76static const struct sec_voltage_desc ldo_voltage_val1 = {
9767ec7f
SK
77 .max = 3950000,
78 .min = 800000,
79 .step = 50000,
80};
81
63063bfb 82static const struct sec_voltage_desc ldo_voltage_val2 = {
9767ec7f
SK
83 .max = 2375000,
84 .min = 800000,
85 .step = 25000,
86};
87
63063bfb 88static const struct sec_voltage_desc *reg_voltage_map[] = {
9767ec7f
SK
89 [S5M8767_LDO1] = &ldo_voltage_val2,
90 [S5M8767_LDO2] = &ldo_voltage_val2,
91 [S5M8767_LDO3] = &ldo_voltage_val1,
92 [S5M8767_LDO4] = &ldo_voltage_val1,
93 [S5M8767_LDO5] = &ldo_voltage_val1,
94 [S5M8767_LDO6] = &ldo_voltage_val2,
95 [S5M8767_LDO7] = &ldo_voltage_val2,
96 [S5M8767_LDO8] = &ldo_voltage_val2,
97 [S5M8767_LDO9] = &ldo_voltage_val1,
98 [S5M8767_LDO10] = &ldo_voltage_val1,
99 [S5M8767_LDO11] = &ldo_voltage_val1,
100 [S5M8767_LDO12] = &ldo_voltage_val1,
101 [S5M8767_LDO13] = &ldo_voltage_val1,
102 [S5M8767_LDO14] = &ldo_voltage_val1,
103 [S5M8767_LDO15] = &ldo_voltage_val2,
104 [S5M8767_LDO16] = &ldo_voltage_val1,
105 [S5M8767_LDO17] = &ldo_voltage_val1,
106 [S5M8767_LDO18] = &ldo_voltage_val1,
107 [S5M8767_LDO19] = &ldo_voltage_val1,
108 [S5M8767_LDO20] = &ldo_voltage_val1,
109 [S5M8767_LDO21] = &ldo_voltage_val1,
110 [S5M8767_LDO22] = &ldo_voltage_val1,
111 [S5M8767_LDO23] = &ldo_voltage_val1,
112 [S5M8767_LDO24] = &ldo_voltage_val1,
113 [S5M8767_LDO25] = &ldo_voltage_val1,
114 [S5M8767_LDO26] = &ldo_voltage_val1,
115 [S5M8767_LDO27] = &ldo_voltage_val1,
116 [S5M8767_LDO28] = &ldo_voltage_val1,
117 [S5M8767_BUCK1] = &buck_voltage_val1,
118 [S5M8767_BUCK2] = &buck_voltage_val2,
119 [S5M8767_BUCK3] = &buck_voltage_val2,
120 [S5M8767_BUCK4] = &buck_voltage_val2,
121 [S5M8767_BUCK5] = &buck_voltage_val1,
122 [S5M8767_BUCK6] = &buck_voltage_val1,
123 [S5M8767_BUCK7] = NULL,
124 [S5M8767_BUCK8] = NULL,
125 [S5M8767_BUCK9] = &buck_voltage_val3,
126};
127
5ceba7ba 128static unsigned int s5m8767_opmode_reg[][4] = {
7e44bb83
SK
129 /* {OFF, ON, LOWPOWER, SUSPEND} */
130 /* LDO1 ... LDO28 */
131 {0x0, 0x3, 0x2, 0x1}, /* LDO1 */
132 {0x0, 0x3, 0x2, 0x1},
133 {0x0, 0x3, 0x2, 0x1},
134 {0x0, 0x0, 0x0, 0x0},
135 {0x0, 0x3, 0x2, 0x1}, /* LDO5 */
136 {0x0, 0x3, 0x2, 0x1},
137 {0x0, 0x3, 0x2, 0x1},
138 {0x0, 0x3, 0x2, 0x1},
139 {0x0, 0x3, 0x2, 0x1},
140 {0x0, 0x3, 0x2, 0x1}, /* LDO10 */
141 {0x0, 0x3, 0x2, 0x1},
142 {0x0, 0x3, 0x2, 0x1},
143 {0x0, 0x3, 0x2, 0x1},
144 {0x0, 0x3, 0x2, 0x1},
145 {0x0, 0x3, 0x2, 0x1}, /* LDO15 */
146 {0x0, 0x3, 0x2, 0x1},
147 {0x0, 0x3, 0x2, 0x1},
148 {0x0, 0x0, 0x0, 0x0},
149 {0x0, 0x3, 0x2, 0x1},
150 {0x0, 0x3, 0x2, 0x1}, /* LDO20 */
151 {0x0, 0x3, 0x2, 0x1},
152 {0x0, 0x3, 0x2, 0x1},
153 {0x0, 0x0, 0x0, 0x0},
154 {0x0, 0x3, 0x2, 0x1},
155 {0x0, 0x3, 0x2, 0x1}, /* LDO25 */
156 {0x0, 0x3, 0x2, 0x1},
157 {0x0, 0x3, 0x2, 0x1},
158 {0x0, 0x3, 0x2, 0x1}, /* LDO28 */
159
160 /* BUCK1 ... BUCK9 */
161 {0x0, 0x3, 0x1, 0x1}, /* BUCK1 */
162 {0x0, 0x3, 0x1, 0x1},
163 {0x0, 0x3, 0x1, 0x1},
164 {0x0, 0x3, 0x1, 0x1},
165 {0x0, 0x3, 0x2, 0x1}, /* BUCK5 */
166 {0x0, 0x3, 0x1, 0x1},
167 {0x0, 0x3, 0x1, 0x1},
168 {0x0, 0x3, 0x1, 0x1},
169 {0x0, 0x3, 0x1, 0x1}, /* BUCK9 */
170};
171
172static int s5m8767_get_register(struct regulator_dev *rdev, int *reg,
173 int *enable_ctrl)
9767ec7f 174{
9bb096ff 175 int i, reg_id = rdev_get_id(rdev);
7e44bb83
SK
176 unsigned int mode;
177 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
9767ec7f
SK
178
179 switch (reg_id) {
180 case S5M8767_LDO1 ... S5M8767_LDO2:
181 *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
182 break;
183 case S5M8767_LDO3 ... S5M8767_LDO28:
184 *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
185 break;
186 case S5M8767_BUCK1:
187 *reg = S5M8767_REG_BUCK1CTRL1;
188 break;
189 case S5M8767_BUCK2 ... S5M8767_BUCK4:
190 *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
191 break;
192 case S5M8767_BUCK5:
193 *reg = S5M8767_REG_BUCK5CTRL1;
194 break;
195 case S5M8767_BUCK6 ... S5M8767_BUCK9:
196 *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
197 break;
198 default:
199 return -EINVAL;
200 }
201
9bb096ff
ADK
202 for (i = 0; i < s5m8767->num_regulators; i++) {
203 if (s5m8767->opmode[i].id == reg_id) {
204 mode = s5m8767->opmode[i].mode;
205 break;
206 }
207 }
208
209 if (i < s5m8767->num_regulators)
210 *enable_ctrl =
211 s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT;
212
9767ec7f
SK
213 return 0;
214}
215
216static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
217{
218 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
219 int ret, reg;
79b53d19 220 int enable_ctrl;
3ef30398 221 unsigned int val;
9767ec7f 222
7e44bb83 223 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
9767ec7f
SK
224 if (ret == -EINVAL)
225 return 1;
226 else if (ret)
227 return ret;
228
63063bfb 229 ret = sec_reg_read(s5m8767->iodev, reg, &val);
9767ec7f
SK
230 if (ret)
231 return ret;
232
79b53d19 233 return (val & S5M8767_ENCTRL_MASK) == enable_ctrl;
9767ec7f
SK
234}
235
236static int s5m8767_reg_enable(struct regulator_dev *rdev)
237{
238 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
239 int ret, reg;
79b53d19 240 int enable_ctrl;
9767ec7f 241
7e44bb83 242 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
9767ec7f
SK
243 if (ret)
244 return ret;
245
79b53d19
KK
246 return sec_reg_update(s5m8767->iodev, reg, enable_ctrl,
247 S5M8767_ENCTRL_MASK);
9767ec7f
SK
248}
249
250static int s5m8767_reg_disable(struct regulator_dev *rdev)
251{
252 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
253 int ret, reg;
79b53d19 254 int mask = S5M8767_ENCTRL_MASK, enable_ctrl;
9767ec7f 255
7e44bb83 256 ret = s5m8767_get_register(rdev, &reg, &enable_ctrl);
9767ec7f
SK
257 if (ret)
258 return ret;
259
63063bfb 260 return sec_reg_update(s5m8767->iodev, reg, ~mask, mask);
9767ec7f
SK
261}
262
31a932e1 263static int s5m8767_get_vsel_reg(int reg_id, struct s5m8767_info *s5m8767)
9767ec7f 264{
9767ec7f
SK
265 int reg;
266
267 switch (reg_id) {
268 case S5M8767_LDO1 ... S5M8767_LDO2:
269 reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
270 break;
271 case S5M8767_LDO3 ... S5M8767_LDO28:
272 reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
273 break;
274 case S5M8767_BUCK1:
275 reg = S5M8767_REG_BUCK1CTRL2;
276 break;
277 case S5M8767_BUCK2:
da130ab2 278 reg = S5M8767_REG_BUCK2DVS1;
0a41685f
AL
279 if (s5m8767->buck2_gpiodvs)
280 reg += s5m8767->buck_gpioindex;
9767ec7f
SK
281 break;
282 case S5M8767_BUCK3:
da130ab2 283 reg = S5M8767_REG_BUCK3DVS1;
0a41685f
AL
284 if (s5m8767->buck3_gpiodvs)
285 reg += s5m8767->buck_gpioindex;
9767ec7f
SK
286 break;
287 case S5M8767_BUCK4:
da130ab2 288 reg = S5M8767_REG_BUCK4DVS1;
0a41685f
AL
289 if (s5m8767->buck4_gpiodvs)
290 reg += s5m8767->buck_gpioindex;
9767ec7f
SK
291 break;
292 case S5M8767_BUCK5:
293 reg = S5M8767_REG_BUCK5CTRL2;
294 break;
295 case S5M8767_BUCK6 ... S5M8767_BUCK9:
296 reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
297 break;
298 default:
299 return -EINVAL;
300 }
301
31a932e1 302 return reg;
9767ec7f
SK
303}
304
854f73ec
AL
305static int s5m8767_convert_voltage_to_sel(const struct sec_voltage_desc *desc,
306 int min_vol)
9767ec7f 307{
5b5e977c 308 int selector = 0;
9767ec7f
SK
309
310 if (desc == NULL)
311 return -EINVAL;
312
854f73ec 313 if (min_vol > desc->max)
9767ec7f
SK
314 return -EINVAL;
315
94e85a3c
AL
316 if (min_vol < desc->min)
317 min_vol = desc->min;
318
319 selector = DIV_ROUND_UP(min_vol - desc->min, desc->step);
9767ec7f 320
854f73ec 321 if (desc->min + desc->step * selector > desc->max)
9767ec7f
SK
322 return -EINVAL;
323
5b5e977c 324 return selector;
9767ec7f
SK
325}
326
df2643cf 327static inline int s5m8767_set_high(struct s5m8767_info *s5m8767)
321d2aba
AL
328{
329 int temp_index = s5m8767->buck_gpioindex;
330
331 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
332 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
333 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
df2643cf
SK
334
335 return 0;
321d2aba
AL
336}
337
df2643cf 338static inline int s5m8767_set_low(struct s5m8767_info *s5m8767)
321d2aba
AL
339{
340 int temp_index = s5m8767->buck_gpioindex;
341
342 gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
343 gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
344 gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
df2643cf
SK
345
346 return 0;
321d2aba
AL
347}
348
df2643cf
SK
349static int s5m8767_set_voltage_sel(struct regulator_dev *rdev,
350 unsigned selector)
9767ec7f
SK
351{
352 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
20a14b84 353 int reg_id = rdev_get_id(rdev);
31a932e1 354 int old_index, index = 0;
321d2aba 355 u8 *buck234_vol = NULL;
9767ec7f
SK
356
357 switch (reg_id) {
358 case S5M8767_LDO1 ... S5M8767_LDO28:
9767ec7f
SK
359 break;
360 case S5M8767_BUCK1 ... S5M8767_BUCK6:
321d2aba
AL
361 if (reg_id == S5M8767_BUCK2 && s5m8767->buck2_gpiodvs)
362 buck234_vol = &s5m8767->buck2_vol[0];
363 else if (reg_id == S5M8767_BUCK3 && s5m8767->buck3_gpiodvs)
364 buck234_vol = &s5m8767->buck3_vol[0];
365 else if (reg_id == S5M8767_BUCK4 && s5m8767->buck4_gpiodvs)
366 buck234_vol = &s5m8767->buck4_vol[0];
9767ec7f
SK
367 break;
368 case S5M8767_BUCK7 ... S5M8767_BUCK8:
369 return -EINVAL;
370 case S5M8767_BUCK9:
9767ec7f
SK
371 break;
372 default:
373 return -EINVAL;
374 }
375
321d2aba
AL
376 /* buck234_vol != NULL means to control buck234 voltage via DVS GPIO */
377 if (buck234_vol) {
df2643cf 378 while (*buck234_vol != selector) {
321d2aba
AL
379 buck234_vol++;
380 index++;
381 }
382 old_index = s5m8767->buck_gpioindex;
383 s5m8767->buck_gpioindex = index;
384
385 if (index > old_index)
df2643cf 386 return s5m8767_set_high(s5m8767);
321d2aba 387 else
df2643cf 388 return s5m8767_set_low(s5m8767);
321d2aba 389 } else {
31a932e1 390 return regulator_set_voltage_sel_regmap(rdev, selector);
321d2aba 391 }
9767ec7f
SK
392}
393
9767ec7f
SK
394static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
395 unsigned int old_sel,
396 unsigned int new_sel)
397{
398 struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
63063bfb 399 const struct sec_voltage_desc *desc;
20a14b84 400 int reg_id = rdev_get_id(rdev);
9767ec7f 401
9767ec7f
SK
402 desc = reg_voltage_map[reg_id];
403
9d88fc0b 404 if ((old_sel < new_sel) && s5m8767->ramp_delay)
89e0f0e4 405 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
0f8b9c77 406 s5m8767->ramp_delay * 1000);
89e0f0e4 407 return 0;
9767ec7f
SK
408}
409
d35aad0c 410static struct regulator_ops s5m8767_ops = {
e2eb169b 411 .list_voltage = regulator_list_voltage_linear,
9767ec7f
SK
412 .is_enabled = s5m8767_reg_is_enabled,
413 .enable = s5m8767_reg_enable,
414 .disable = s5m8767_reg_disable,
31a932e1 415 .get_voltage_sel = regulator_get_voltage_sel_regmap,
df2643cf 416 .set_voltage_sel = s5m8767_set_voltage_sel,
9767ec7f
SK
417 .set_voltage_time_sel = s5m8767_set_voltage_time_sel,
418};
419
e2eb169b
AL
420static struct regulator_ops s5m8767_buck78_ops = {
421 .is_enabled = s5m8767_reg_is_enabled,
422 .enable = s5m8767_reg_enable,
423 .disable = s5m8767_reg_disable,
424};
425
65896e73
AL
426#define s5m8767_regulator_desc(_name) { \
427 .name = #_name, \
428 .id = S5M8767_##_name, \
429 .ops = &s5m8767_ops, \
9767ec7f
SK
430 .type = REGULATOR_VOLTAGE, \
431 .owner = THIS_MODULE, \
432}
433
e2eb169b
AL
434#define s5m8767_regulator_buck78_desc(_name) { \
435 .name = #_name, \
436 .id = S5M8767_##_name, \
437 .ops = &s5m8767_buck78_ops, \
438 .type = REGULATOR_VOLTAGE, \
439 .owner = THIS_MODULE, \
440}
441
9767ec7f 442static struct regulator_desc regulators[] = {
65896e73
AL
443 s5m8767_regulator_desc(LDO1),
444 s5m8767_regulator_desc(LDO2),
445 s5m8767_regulator_desc(LDO3),
446 s5m8767_regulator_desc(LDO4),
447 s5m8767_regulator_desc(LDO5),
448 s5m8767_regulator_desc(LDO6),
449 s5m8767_regulator_desc(LDO7),
450 s5m8767_regulator_desc(LDO8),
451 s5m8767_regulator_desc(LDO9),
452 s5m8767_regulator_desc(LDO10),
453 s5m8767_regulator_desc(LDO11),
454 s5m8767_regulator_desc(LDO12),
455 s5m8767_regulator_desc(LDO13),
456 s5m8767_regulator_desc(LDO14),
457 s5m8767_regulator_desc(LDO15),
458 s5m8767_regulator_desc(LDO16),
459 s5m8767_regulator_desc(LDO17),
460 s5m8767_regulator_desc(LDO18),
461 s5m8767_regulator_desc(LDO19),
462 s5m8767_regulator_desc(LDO20),
463 s5m8767_regulator_desc(LDO21),
464 s5m8767_regulator_desc(LDO22),
465 s5m8767_regulator_desc(LDO23),
466 s5m8767_regulator_desc(LDO24),
467 s5m8767_regulator_desc(LDO25),
468 s5m8767_regulator_desc(LDO26),
469 s5m8767_regulator_desc(LDO27),
470 s5m8767_regulator_desc(LDO28),
471 s5m8767_regulator_desc(BUCK1),
472 s5m8767_regulator_desc(BUCK2),
473 s5m8767_regulator_desc(BUCK3),
474 s5m8767_regulator_desc(BUCK4),
475 s5m8767_regulator_desc(BUCK5),
476 s5m8767_regulator_desc(BUCK6),
e2eb169b
AL
477 s5m8767_regulator_buck78_desc(BUCK7),
478 s5m8767_regulator_buck78_desc(BUCK8),
65896e73 479 s5m8767_regulator_desc(BUCK9),
9767ec7f
SK
480};
481
26aec009
ADK
482#ifdef CONFIG_OF
483static int s5m8767_pmic_dt_parse_dvs_gpio(struct sec_pmic_dev *iodev,
484 struct sec_platform_data *pdata,
485 struct device_node *pmic_np)
486{
487 int i, gpio;
488
489 for (i = 0; i < 3; i++) {
490 gpio = of_get_named_gpio(pmic_np,
491 "s5m8767,pmic-buck-dvs-gpios", i);
492 if (!gpio_is_valid(gpio)) {
493 dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
494 return -EINVAL;
495 }
496 pdata->buck_gpios[i] = gpio;
497 }
498 return 0;
499}
500
501static int s5m8767_pmic_dt_parse_ds_gpio(struct sec_pmic_dev *iodev,
502 struct sec_platform_data *pdata,
503 struct device_node *pmic_np)
504{
505 int i, gpio;
506
507 for (i = 0; i < 3; i++) {
508 gpio = of_get_named_gpio(pmic_np,
509 "s5m8767,pmic-buck-ds-gpios", i);
510 if (!gpio_is_valid(gpio)) {
511 dev_err(iodev->dev, "invalid gpio[%d]: %d\n", i, gpio);
512 return -EINVAL;
513 }
514 pdata->buck_ds[i] = gpio;
515 }
516 return 0;
517}
518
cbb0ed49 519static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
26aec009
ADK
520 struct sec_platform_data *pdata)
521{
cbb0ed49 522 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
26aec009
ADK
523 struct device_node *pmic_np, *regulators_np, *reg_np;
524 struct sec_regulator_data *rdata;
525 struct sec_opmode_data *rmode;
04f9f068 526 unsigned int i, dvs_voltage_nr = 8, ret;
26aec009
ADK
527
528 pmic_np = iodev->dev->of_node;
529 if (!pmic_np) {
530 dev_err(iodev->dev, "could not find pmic sub-node\n");
531 return -ENODEV;
532 }
533
534 regulators_np = of_find_node_by_name(pmic_np, "regulators");
535 if (!regulators_np) {
536 dev_err(iodev->dev, "could not find regulators sub-node\n");
537 return -EINVAL;
538 }
539
540 /* count the number of regulators to be supported in pmic */
1f91b6f6 541 pdata->num_regulators = of_get_child_count(regulators_np);
26aec009 542
cbb0ed49 543 rdata = devm_kzalloc(&pdev->dev, sizeof(*rdata) *
26aec009
ADK
544 pdata->num_regulators, GFP_KERNEL);
545 if (!rdata) {
546 dev_err(iodev->dev,
547 "could not allocate memory for regulator data\n");
548 return -ENOMEM;
549 }
550
cbb0ed49 551 rmode = devm_kzalloc(&pdev->dev, sizeof(*rmode) *
26aec009 552 pdata->num_regulators, GFP_KERNEL);
720a9717 553 if (!rmode) {
26aec009
ADK
554 dev_err(iodev->dev,
555 "could not allocate memory for regulator mode\n");
556 return -ENOMEM;
557 }
558
559 pdata->regulators = rdata;
560 pdata->opmode = rmode;
561 for_each_child_of_node(regulators_np, reg_np) {
562 for (i = 0; i < ARRAY_SIZE(regulators); i++)
563 if (!of_node_cmp(reg_np->name, regulators[i].name))
564 break;
565
566 if (i == ARRAY_SIZE(regulators)) {
567 dev_warn(iodev->dev,
568 "don't know how to configure regulator %s\n",
569 reg_np->name);
570 continue;
571 }
572
573 rdata->id = i;
574 rdata->initdata = of_get_regulator_init_data(
cbb0ed49 575 &pdev->dev, reg_np);
26aec009
ADK
576 rdata->reg_node = reg_np;
577 rdata++;
578 rmode->id = i;
579 if (of_property_read_u32(reg_np, "op_mode",
580 &rmode->mode)) {
581 dev_warn(iodev->dev,
582 "no op_mode property property at %s\n",
583 reg_np->full_name);
584
585 rmode->mode = S5M8767_OPMODE_NORMAL_MODE;
586 }
587 rmode++;
588 }
589
04f9f068 590 if (of_get_property(pmic_np, "s5m8767,pmic-buck2-uses-gpio-dvs", NULL)) {
26aec009
ADK
591 pdata->buck2_gpiodvs = true;
592
04f9f068
CC
593 if (of_property_read_u32_array(pmic_np,
594 "s5m8767,pmic-buck2-dvs-voltage",
595 pdata->buck2_voltage, dvs_voltage_nr)) {
596 dev_err(iodev->dev, "buck2 voltages not specified\n");
597 return -EINVAL;
598 }
599 }
600
601 if (of_get_property(pmic_np, "s5m8767,pmic-buck3-uses-gpio-dvs", NULL)) {
26aec009
ADK
602 pdata->buck3_gpiodvs = true;
603
04f9f068
CC
604 if (of_property_read_u32_array(pmic_np,
605 "s5m8767,pmic-buck3-dvs-voltage",
606 pdata->buck3_voltage, dvs_voltage_nr)) {
607 dev_err(iodev->dev, "buck3 voltages not specified\n");
608 return -EINVAL;
609 }
610 }
611
612 if (of_get_property(pmic_np, "s5m8767,pmic-buck4-uses-gpio-dvs", NULL)) {
26aec009
ADK
613 pdata->buck4_gpiodvs = true;
614
04f9f068
CC
615 if (of_property_read_u32_array(pmic_np,
616 "s5m8767,pmic-buck4-dvs-voltage",
617 pdata->buck4_voltage, dvs_voltage_nr)) {
618 dev_err(iodev->dev, "buck4 voltages not specified\n");
619 return -EINVAL;
620 }
621 }
622
26aec009
ADK
623 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
624 pdata->buck4_gpiodvs) {
625 ret = s5m8767_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np);
626 if (ret)
627 return -EINVAL;
628
629 if (of_property_read_u32(pmic_np,
630 "s5m8767,pmic-buck-default-dvs-idx",
631 &pdata->buck_default_idx)) {
632 pdata->buck_default_idx = 0;
633 } else {
634 if (pdata->buck_default_idx >= 8) {
635 pdata->buck_default_idx = 0;
636 dev_info(iodev->dev,
637 "invalid value for default dvs index, use 0\n");
638 }
639 }
26aec009
ADK
640 }
641
642 ret = s5m8767_pmic_dt_parse_ds_gpio(iodev, pdata, pmic_np);
643 if (ret)
644 return -EINVAL;
645
033054e8
CC
646 if (of_get_property(pmic_np, "s5m8767,pmic-buck2-ramp-enable", NULL))
647 pdata->buck2_ramp_enable = true;
26aec009 648
033054e8
CC
649 if (of_get_property(pmic_np, "s5m8767,pmic-buck3-ramp-enable", NULL))
650 pdata->buck3_ramp_enable = true;
26aec009 651
033054e8
CC
652 if (of_get_property(pmic_np, "s5m8767,pmic-buck4-ramp-enable", NULL))
653 pdata->buck4_ramp_enable = true;
654
655 if (pdata->buck2_ramp_enable || pdata->buck3_ramp_enable
656 || pdata->buck4_ramp_enable) {
657 if (of_property_read_u32(pmic_np, "s5m8767,pmic-buck-ramp-delay",
658 &pdata->buck_ramp_delay))
659 pdata->buck_ramp_delay = 0;
26aec009
ADK
660 }
661
662 return 0;
663}
664#else
cbb0ed49 665static int s5m8767_pmic_dt_parse_pdata(struct platform_device *pdev,
26aec009
ADK
666 struct sec_platform_data *pdata)
667{
668 return 0;
669}
670#endif /* CONFIG_OF */
671
a5023574 672static int s5m8767_pmic_probe(struct platform_device *pdev)
9767ec7f 673{
63063bfb 674 struct sec_pmic_dev *iodev = dev_get_drvdata(pdev->dev.parent);
26aec009 675 struct sec_platform_data *pdata = iodev->pdata;
c172708d 676 struct regulator_config config = { };
9767ec7f
SK
677 struct regulator_dev **rdev;
678 struct s5m8767_info *s5m8767;
c848bc85 679 int i, ret, size, buck_init;
9767ec7f 680
e81d7bc8
AL
681 if (!pdata) {
682 dev_err(pdev->dev.parent, "Platform data not supplied\n");
683 return -ENODEV;
684 }
685
26aec009 686 if (iodev->dev->of_node) {
cbb0ed49 687 ret = s5m8767_pmic_dt_parse_pdata(pdev, pdata);
26aec009
ADK
688 if (ret)
689 return ret;
690 }
691
6c4efe24
AL
692 if (pdata->buck2_gpiodvs) {
693 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
694 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
695 return -EINVAL;
696 }
697 }
698
699 if (pdata->buck3_gpiodvs) {
700 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
701 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
702 return -EINVAL;
703 }
704 }
705
706 if (pdata->buck4_gpiodvs) {
707 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
708 dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
709 return -EINVAL;
710 }
711 }
712
9767ec7f
SK
713 s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
714 GFP_KERNEL);
715 if (!s5m8767)
716 return -ENOMEM;
717
718 size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2);
719 s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
720 if (!s5m8767->rdev)
721 return -ENOMEM;
722
723 rdev = s5m8767->rdev;
724 s5m8767->dev = &pdev->dev;
725 s5m8767->iodev = iodev;
9bb096ff 726 s5m8767->num_regulators = pdata->num_regulators;
9767ec7f 727 platform_set_drvdata(pdev, s5m8767);
9767ec7f
SK
728
729 s5m8767->buck_gpioindex = pdata->buck_default_idx;
730 s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
731 s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
732 s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
733 s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
734 s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
735 s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
c848bc85
SK
736 s5m8767->buck_ds[0] = pdata->buck_ds[0];
737 s5m8767->buck_ds[1] = pdata->buck_ds[1];
738 s5m8767->buck_ds[2] = pdata->buck_ds[2];
739
9767ec7f
SK
740 s5m8767->ramp_delay = pdata->buck_ramp_delay;
741 s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
742 s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
743 s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
7e44bb83 744 s5m8767->opmode = pdata->opmode;
9767ec7f 745
c848bc85 746 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
854f73ec 747 pdata->buck2_init);
c848bc85 748
938e05bf 749 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS2, buck_init);
c848bc85
SK
750
751 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
854f73ec 752 pdata->buck3_init);
c848bc85 753
938e05bf 754 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS2, buck_init);
c848bc85
SK
755
756 buck_init = s5m8767_convert_voltage_to_sel(&buck_voltage_val2,
854f73ec 757 pdata->buck4_init);
c848bc85 758
938e05bf 759 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS2, buck_init);
c848bc85 760
9767ec7f
SK
761 for (i = 0; i < 8; i++) {
762 if (s5m8767->buck2_gpiodvs) {
763 s5m8767->buck2_vol[i] =
5b5e977c 764 s5m8767_convert_voltage_to_sel(
9767ec7f 765 &buck_voltage_val2,
854f73ec 766 pdata->buck2_voltage[i]);
9767ec7f
SK
767 }
768
769 if (s5m8767->buck3_gpiodvs) {
770 s5m8767->buck3_vol[i] =
5b5e977c 771 s5m8767_convert_voltage_to_sel(
9767ec7f 772 &buck_voltage_val2,
854f73ec 773 pdata->buck3_voltage[i]);
9767ec7f
SK
774 }
775
776 if (s5m8767->buck4_gpiodvs) {
777 s5m8767->buck4_vol[i] =
5b5e977c 778 s5m8767_convert_voltage_to_sel(
9767ec7f 779 &buck_voltage_val2,
854f73ec 780 pdata->buck4_voltage[i]);
9767ec7f
SK
781 }
782 }
783
76c854d1
ADK
784 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
785 pdata->buck4_gpiodvs) {
786
787 if (!gpio_is_valid(pdata->buck_gpios[0]) ||
788 !gpio_is_valid(pdata->buck_gpios[1]) ||
789 !gpio_is_valid(pdata->buck_gpios[2])) {
790 dev_err(&pdev->dev, "GPIO NOT VALID\n");
791 return -EINVAL;
792 }
793
5febb3c9
AL
794 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[0],
795 "S5M8767 SET1");
796 if (ret)
797 return ret;
798
799 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[1],
800 "S5M8767 SET2");
801 if (ret)
802 return ret;
803
804 ret = devm_gpio_request(&pdev->dev, pdata->buck_gpios[2],
805 "S5M8767 SET3");
806 if (ret)
807 return ret;
808
c848bc85
SK
809 /* SET1 GPIO */
810 gpio_direction_output(pdata->buck_gpios[0],
811 (s5m8767->buck_gpioindex >> 2) & 0x1);
812 /* SET2 GPIO */
813 gpio_direction_output(pdata->buck_gpios[1],
814 (s5m8767->buck_gpioindex >> 1) & 0x1);
815 /* SET3 GPIO */
816 gpio_direction_output(pdata->buck_gpios[2],
817 (s5m8767->buck_gpioindex >> 0) & 0x1);
9767ec7f
SK
818 }
819
5febb3c9
AL
820 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[0], "S5M8767 DS2");
821 if (ret)
822 return ret;
c848bc85 823
5febb3c9
AL
824 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[1], "S5M8767 DS3");
825 if (ret)
826 return ret;
c848bc85 827
5febb3c9
AL
828 ret = devm_gpio_request(&pdev->dev, pdata->buck_ds[2], "S5M8767 DS4");
829 if (ret)
830 return ret;
c848bc85
SK
831
832 /* DS2 GPIO */
833 gpio_direction_output(pdata->buck_ds[0], 0x0);
834 /* DS3 GPIO */
835 gpio_direction_output(pdata->buck_ds[1], 0x0);
836 /* DS4 GPIO */
837 gpio_direction_output(pdata->buck_ds[2], 0x0);
838
839 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
840 pdata->buck4_gpiodvs) {
3e701cdf 841 sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
c848bc85
SK
842 (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1),
843 1 << 1);
3e701cdf 844 sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
c848bc85
SK
845 (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1),
846 1 << 1);
3e701cdf 847 sec_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
c848bc85
SK
848 (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1),
849 1 << 1);
850 }
9767ec7f
SK
851
852 /* Initialize GPIO DVS registers */
853 for (i = 0; i < 8; i++) {
854 if (s5m8767->buck2_gpiodvs) {
63063bfb 855 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
9767ec7f
SK
856 s5m8767->buck2_vol[i]);
857 }
858
859 if (s5m8767->buck3_gpiodvs) {
63063bfb 860 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
9767ec7f
SK
861 s5m8767->buck3_vol[i]);
862 }
863
864 if (s5m8767->buck4_gpiodvs) {
63063bfb 865 sec_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
9767ec7f
SK
866 s5m8767->buck4_vol[i]);
867 }
868 }
9767ec7f
SK
869
870 if (s5m8767->buck2_ramp)
63063bfb 871 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
9767ec7f
SK
872
873 if (s5m8767->buck3_ramp)
63063bfb 874 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
9767ec7f
SK
875
876 if (s5m8767->buck4_ramp)
63063bfb 877 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
9767ec7f
SK
878
879 if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
880 || s5m8767->buck4_ramp) {
881 switch (s5m8767->ramp_delay) {
1af142c6 882 case 5:
63063bfb 883 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
1af142c6
SK
884 0x40, 0xf0);
885 break;
886 case 10:
3e701cdf 887 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
1af142c6 888 0x90, 0xf0);
047ec220 889 break;
9767ec7f 890 case 25:
63063bfb 891 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
9767ec7f 892 0xd0, 0xf0);
047ec220 893 break;
9767ec7f 894 case 50:
63063bfb 895 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
9767ec7f 896 0xe0, 0xf0);
047ec220 897 break;
9767ec7f 898 case 100:
63063bfb 899 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
9767ec7f 900 0xf0, 0xf0);
047ec220 901 break;
9767ec7f 902 default:
63063bfb 903 sec_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
9767ec7f
SK
904 0x90, 0xf0);
905 }
906 }
907
908 for (i = 0; i < pdata->num_regulators; i++) {
63063bfb 909 const struct sec_voltage_desc *desc;
9767ec7f
SK
910 int id = pdata->regulators[i].id;
911
912 desc = reg_voltage_map[id];
e2eb169b 913 if (desc) {
9767ec7f
SK
914 regulators[id].n_voltages =
915 (desc->max - desc->min) / desc->step + 1;
e2eb169b
AL
916 regulators[id].min_uV = desc->min;
917 regulators[id].uV_step = desc->step;
31a932e1
AL
918 regulators[id].vsel_reg =
919 s5m8767_get_vsel_reg(id, s5m8767);
920 if (id < S5M8767_BUCK1)
921 regulators[id].vsel_mask = 0x3f;
922 else
923 regulators[id].vsel_mask = 0xff;
e2eb169b 924 }
9767ec7f 925
c172708d
MB
926 config.dev = s5m8767->dev;
927 config.init_data = pdata->regulators[i].initdata;
928 config.driver_data = s5m8767;
31a932e1 929 config.regmap = iodev->regmap;
26aec009 930 config.of_node = pdata->regulators[i].reg_node;
c172708d 931
f0db475d
MB
932 rdev[i] = devm_regulator_register(&pdev->dev, &regulators[id],
933 &config);
9767ec7f
SK
934 if (IS_ERR(rdev[i])) {
935 ret = PTR_ERR(rdev[i]);
936 dev_err(s5m8767->dev, "regulator init failed for %d\n",
937 id);
f0db475d 938 return ret;
9767ec7f
SK
939 }
940 }
941
9767ec7f
SK
942 return 0;
943}
944
945static const struct platform_device_id s5m8767_pmic_id[] = {
946 { "s5m8767-pmic", 0},
947 { },
948};
949MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
950
951static struct platform_driver s5m8767_pmic_driver = {
952 .driver = {
953 .name = "s5m8767-pmic",
954 .owner = THIS_MODULE,
955 },
956 .probe = s5m8767_pmic_probe,
9767ec7f
SK
957 .id_table = s5m8767_pmic_id,
958};
959
960static int __init s5m8767_pmic_init(void)
961{
962 return platform_driver_register(&s5m8767_pmic_driver);
963}
964subsys_initcall(s5m8767_pmic_init);
965
966static void __exit s5m8767_pmic_exit(void)
967{
968 platform_driver_unregister(&s5m8767_pmic_driver);
969}
970module_exit(s5m8767_pmic_exit);
971
972/* Module information */
973MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
974MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
975MODULE_LICENSE("GPL");
This page took 0.159072 seconds and 5 git commands to generate.