332608273be4c8815f72662c2c6f1a48413223c1
[cascardo/linux.git] / drivers / regulator / s5m8767.c
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>
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/gpio.h>
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>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/mfd/s5m87xx/s5m-core.h>
25 #include <linux/mfd/s5m87xx/s5m-pmic.h>
26
27 #define S5M8767_OPMODE_MASK     0x3
28 #define S5M8767_OPMODE_SHIFT    6
29 #define S5M8767_OPMODE_SM       (S5M8767_OPMODE_MASK << S5M8767_OPMODE_SHIFT)
30
31 struct s5m8767_info {
32         struct device *dev;
33         struct s5m87xx_dev *iodev;
34         int num_regulators;
35         struct regulator_dev **rdev;
36
37         int ramp_delay;
38         bool buck2_ramp;
39         bool buck3_ramp;
40         bool buck4_ramp;
41
42         bool buck2_gpiodvs;
43         bool buck3_gpiodvs;
44         bool buck4_gpiodvs;
45         u8 buck2_vol[8];
46         u8 buck3_vol[8];
47         u8 buck4_vol[8];
48         int buck_gpios[3];
49         int buck_gpioindex;
50 };
51
52 struct s5m_voltage_desc {
53         int max;
54         int min;
55         int step;
56 };
57
58 static const struct s5m_voltage_desc buck_voltage_val1 = {
59         .max = 2225000,
60         .min =  650000,
61         .step =   6250,
62 };
63
64 static const struct s5m_voltage_desc buck_voltage_val2 = {
65         .max = 1600000,
66         .min =  600000,
67         .step =   6250,
68 };
69
70 static const struct s5m_voltage_desc buck_voltage_val3 = {
71         .max = 3000000,
72         .min =  750000,
73         .step =  12500,
74 };
75
76 static const struct s5m_voltage_desc ldo_voltage_val1 = {
77         .max = 3950000,
78         .min =  800000,
79         .step =  50000,
80 };
81
82 static const struct s5m_voltage_desc ldo_voltage_val2 = {
83         .max = 2375000,
84         .min =  800000,
85         .step =  25000,
86 };
87
88 static const struct s5m_voltage_desc *reg_voltage_map[] = {
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         [S5M8767_EN32KHZ_AP] = NULL,
127         [S5M8767_EN32KHZ_CP] = NULL,
128         [S5M8767_EN32KHZ_BT] = NULL,
129 };
130
131 static int s5m8767_list_voltage(struct regulator_dev *rdev,
132                                 unsigned int selector)
133 {
134         const struct s5m_voltage_desc *desc;
135         int reg_id = rdev_get_id(rdev);
136         int val;
137
138         if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0)
139                 return -EINVAL;
140
141         desc = reg_voltage_map[reg_id];
142         if (desc == NULL)
143                 return -EINVAL;
144
145         val = desc->min + desc->step * selector;
146         if (val > desc->max)
147                 return -EINVAL;
148
149         return val;
150 }
151
152 static int s5m8767_get_register(struct regulator_dev *rdev,
153                                 int *reg, int *mask, int *pattern)
154 {
155         int reg_id = rdev_get_id(rdev);
156         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
157         int i;
158         int op_mode = 0x3;
159         for (i = 0; i < s5m8767->iodev->pdata->num_regulators; i++)
160                 if (reg_id == s5m8767->iodev->pdata->regulators[i].id)
161                         break;
162
163         if (i == s5m8767->iodev->pdata->num_regulators) {
164                 dev_err(s5m8767->iodev->dev, "No matching regulators\n");
165                 return -ENODEV;
166         }
167 #ifdef CONFIG_OF
168         op_mode = s5m8767->iodev->pdata->regulators[i].reg_op_mode;
169 #endif
170         *pattern = op_mode << S5M8767_OPMODE_SHIFT;
171         *mask = S5M8767_OPMODE_SM;
172
173         switch (reg_id) {
174         case S5M8767_LDO1 ... S5M8767_LDO2:
175                 *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
176                 break;
177         case S5M8767_LDO3 ... S5M8767_LDO28:
178                 *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
179                 break;
180         case S5M8767_BUCK1:
181                 *reg = S5M8767_REG_BUCK1CTRL1;
182                 break;
183         case S5M8767_BUCK2 ... S5M8767_BUCK4:
184                 *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9;
185                 break;
186         case S5M8767_BUCK5:
187                 *reg = S5M8767_REG_BUCK5CTRL1;
188                 break;
189         case S5M8767_BUCK6 ... S5M8767_BUCK9:
190                 *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2;
191                 break;
192         case S5M8767_EN32KHZ_AP...S5M8767_EN32KHZ_BT:
193                 *reg = S5M8767_REG_CTRL1;
194                 *pattern = 1 << (reg_id - S5M8767_EN32KHZ_AP);
195                 *mask = 1 << (reg_id - S5M8767_EN32KHZ_AP);
196                 break;
197         default:
198                 return -EINVAL;
199         }
200
201         return 0;
202 }
203
204 static int s5m8767_reg_is_enabled(struct regulator_dev *rdev)
205 {
206         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
207         int ret, reg, mask, pattern;
208         unsigned int val;
209
210         ret = s5m8767_get_register(rdev, &reg, &mask, &pattern);
211         if (ret == -EINVAL)
212                 return 1;
213         else if (ret)
214                 return ret;
215
216         ret = s5m_reg_read(s5m8767->iodev, reg, &val);
217         if (ret)
218                 return ret;
219
220         return (val & mask) == pattern;
221 }
222
223 static int s5m8767_reg_enable(struct regulator_dev *rdev)
224 {
225         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
226         int ret, reg, mask, pattern;
227
228         ret = s5m8767_get_register(rdev, &reg, &mask, &pattern);
229         if (ret)
230                 return ret;
231
232         return s5m_reg_update(s5m8767->iodev, reg, pattern, mask);
233 }
234
235 static int s5m8767_reg_disable(struct regulator_dev *rdev)
236 {
237         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
238         int ret, reg, mask, pattern;
239
240         ret = s5m8767_get_register(rdev, &reg, &mask, &pattern);
241         if (ret)
242                 return ret;
243
244         return s5m_reg_update(s5m8767->iodev, reg, ~mask, mask);
245 }
246
247 static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg)
248 {
249         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
250         int reg_id = rdev_get_id(rdev);
251         int reg;
252
253         switch (reg_id) {
254         case S5M8767_LDO1 ... S5M8767_LDO2:
255                 reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1);
256                 break;
257         case S5M8767_LDO3 ... S5M8767_LDO28:
258                 reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3);
259                 break;
260         case S5M8767_BUCK1:
261                 reg = S5M8767_REG_BUCK1CTRL2;
262                 break;
263         case S5M8767_BUCK2:
264                 reg = S5M8767_REG_BUCK2DVS1;
265                 if (s5m8767->buck2_gpiodvs)
266                         reg += s5m8767->buck_gpioindex;
267                 break;
268         case S5M8767_BUCK3:
269                 reg = S5M8767_REG_BUCK3DVS1;
270                 if (s5m8767->buck3_gpiodvs)
271                         reg += s5m8767->buck_gpioindex;
272                 break;
273         case S5M8767_BUCK4:
274                 reg = S5M8767_REG_BUCK4DVS1;
275                 if (s5m8767->buck4_gpiodvs)
276                         reg += s5m8767->buck_gpioindex;
277                 break;
278         case S5M8767_BUCK5:
279                 reg = S5M8767_REG_BUCK5CTRL2;
280                 break;
281         case S5M8767_BUCK6 ... S5M8767_BUCK9:
282                 reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2;
283                 break;
284         default:
285                 return -EINVAL;
286         }
287
288         *_reg = reg;
289
290         return 0;
291 }
292
293 static int s5m8767_get_voltage_sel(struct regulator_dev *rdev)
294 {
295         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
296         int reg, mask, ret;
297         int reg_id = rdev_get_id(rdev);
298         unsigned int val;
299
300         ret = s5m8767_get_voltage_register(rdev, &reg);
301         if (ret)
302                 return ret;
303
304         mask = (reg_id < S5M8767_BUCK1) ? 0x3f : 0xff;
305
306         ret = s5m_reg_read(s5m8767->iodev, reg, &val);
307         if (ret)
308                 return ret;
309
310         val &= mask;
311
312         return val;
313 }
314
315 static int s5m8767_convert_voltage_to_sel(
316                 const struct s5m_voltage_desc *desc,
317                 int min_vol, int max_vol)
318 {
319         int selector = 0;
320
321         if (desc == NULL)
322                 return -EINVAL;
323
324         if (max_vol < desc->min || min_vol > desc->max)
325                 return -EINVAL;
326
327         selector = (min_vol - desc->min) / desc->step;
328
329         if (desc->min + desc->step * selector > max_vol)
330                 return -EINVAL;
331
332         return selector;
333 }
334
335 static int s5m8767_set_voltage(struct regulator_dev *rdev,
336                                 int min_uV, int max_uV, unsigned *selector)
337 {
338         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
339         const struct s5m_voltage_desc *desc;
340         int reg_id = rdev_get_id(rdev);
341         int sel, reg, mask, ret;
342         unsigned int val;
343
344         switch (reg_id) {
345         case S5M8767_LDO1 ... S5M8767_LDO28:
346                 mask = 0x3f;
347                 break;
348         case S5M8767_BUCK1 ... S5M8767_BUCK6:
349                 mask = 0xff;
350                 break;
351         case S5M8767_BUCK7 ... S5M8767_BUCK8:
352                 return -EINVAL;
353         case S5M8767_BUCK9:
354                 mask = 0xff;
355                 break;
356         default:
357                 return -EINVAL;
358         }
359
360         desc = reg_voltage_map[reg_id];
361
362         sel = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
363         if (sel < 0)
364                 return sel;
365
366         ret = s5m8767_get_voltage_register(rdev, &reg);
367         if (ret)
368                 return ret;
369
370         s5m_reg_read(s5m8767->iodev, reg, &val);
371         val &= ~mask;
372         val |= sel;
373
374         ret = s5m_reg_write(s5m8767->iodev, reg, val);
375         *selector = sel;
376
377         return ret;
378 }
379
380 static inline void s5m8767_set_high(struct s5m8767_info *s5m8767)
381 {
382         int temp_index = s5m8767->buck_gpioindex;
383
384         gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
385         gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
386         gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
387 }
388
389 static inline void s5m8767_set_low(struct s5m8767_info *s5m8767)
390 {
391         int temp_index = s5m8767->buck_gpioindex;
392
393         gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1);
394         gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1);
395         gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1);
396 }
397
398 static int s5m8767_set_voltage_buck(struct regulator_dev *rdev,
399                                     int min_uV, int max_uV, unsigned *selector)
400 {
401         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
402         int reg_id = rdev_get_id(rdev);
403         const struct s5m_voltage_desc *desc;
404         int new_val, old_val, i = 0;
405
406         if (reg_id < S5M8767_BUCK1 || reg_id > S5M8767_BUCK6)
407                 return -EINVAL;
408
409         switch (reg_id) {
410         case S5M8767_BUCK1:
411                 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
412         case S5M8767_BUCK2 ... S5M8767_BUCK4:
413                 break;
414         case S5M8767_BUCK5 ... S5M8767_BUCK6:
415                 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
416         case S5M8767_BUCK9:
417                 return s5m8767_set_voltage(rdev, min_uV, max_uV, selector);
418         }
419
420         desc = reg_voltage_map[reg_id];
421         new_val = s5m8767_convert_voltage_to_sel(desc, min_uV, max_uV);
422         if (new_val < 0)
423                 return new_val;
424
425         switch (reg_id) {
426         case S5M8767_BUCK2:
427                 if (s5m8767->buck2_gpiodvs) {
428                         while (s5m8767->buck2_vol[i] != new_val)
429                                 i++;
430                 } else
431                         return s5m8767_set_voltage(rdev, min_uV,
432                                                    max_uV, selector);
433                 break;
434         case S5M8767_BUCK3:
435                 if (s5m8767->buck3_gpiodvs) {
436                         while (s5m8767->buck3_vol[i] != new_val)
437                                 i++;
438                 } else
439                         return s5m8767_set_voltage(rdev, min_uV,
440                                                    max_uV, selector);
441                 break;
442         case S5M8767_BUCK4:
443                 if (s5m8767->buck3_gpiodvs) {
444                         while (s5m8767->buck4_vol[i] != new_val)
445                                 i++;
446                 } else
447                         return s5m8767_set_voltage(rdev, min_uV,
448                                                    max_uV, selector);
449                 break;
450         }
451
452         old_val = s5m8767->buck_gpioindex;
453         s5m8767->buck_gpioindex = i;
454
455         if (i > old_val)
456                 s5m8767_set_high(s5m8767);
457         else
458                 s5m8767_set_low(s5m8767);
459
460         *selector = new_val;
461         return 0;
462 }
463
464 static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev,
465                                              unsigned int old_sel,
466                                              unsigned int new_sel)
467 {
468         struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev);
469         const struct s5m_voltage_desc *desc;
470         int reg_id = rdev_get_id(rdev);
471
472         desc = reg_voltage_map[reg_id];
473
474         if (old_sel < new_sel)
475                 return DIV_ROUND_UP(desc->step * (new_sel - old_sel),
476                                         s5m8767->ramp_delay * 1000);
477         return 0;
478 }
479
480 static struct regulator_ops s5m8767_ldo_ops = {
481         .list_voltage           = s5m8767_list_voltage,
482         .is_enabled             = s5m8767_reg_is_enabled,
483         .enable                 = s5m8767_reg_enable,
484         .disable                = s5m8767_reg_disable,
485         .get_voltage_sel        = s5m8767_get_voltage_sel,
486         .set_voltage            = s5m8767_set_voltage,
487         .set_voltage_time_sel   = s5m8767_set_voltage_time_sel,
488 };
489
490 static struct regulator_ops s5m8767_buck_ops = {
491         .list_voltage           = s5m8767_list_voltage,
492         .is_enabled             = s5m8767_reg_is_enabled,
493         .enable                 = s5m8767_reg_enable,
494         .disable                = s5m8767_reg_disable,
495         .get_voltage_sel        = s5m8767_get_voltage_sel,
496         .set_voltage            = s5m8767_set_voltage_buck,
497         .set_voltage_time_sel   = s5m8767_set_voltage_time_sel,
498 };
499
500 static struct regulator_ops s5m8767_32k_ops = {
501         .list_voltage           = s5m8767_list_voltage,
502         .is_enabled             = s5m8767_reg_is_enabled,
503         .enable                 = s5m8767_reg_enable,
504         .disable                = s5m8767_reg_disable,
505 };
506
507 #define regulator_desc_ldo(num)         {       \
508         .name           = "LDO"#num,            \
509         .id             = S5M8767_LDO##num,     \
510         .ops            = &s5m8767_ldo_ops,     \
511         .type           = REGULATOR_VOLTAGE,    \
512         .owner          = THIS_MODULE,          \
513 }
514 #define regulator_desc_buck(num)        {       \
515         .name           = "BUCK"#num,           \
516         .id             = S5M8767_BUCK##num,    \
517         .ops            = &s5m8767_buck_ops,    \
518         .type           = REGULATOR_VOLTAGE,    \
519         .owner          = THIS_MODULE,          \
520 }
521 #define regulator_desc_32khz(x) {                       \
522         .name           = "EN32KHZ_" __stringify(x),    \
523         .id             = S5M8767_EN32KHZ_##x,          \
524         .ops            = &s5m8767_32k_ops,             \
525         .type           = REGULATOR_VOLTAGE,            \
526         .owner          = THIS_MODULE,                  \
527 }
528
529 static struct regulator_desc regulators[] = {
530         regulator_desc_ldo(1),
531         regulator_desc_ldo(2),
532         regulator_desc_ldo(3),
533         regulator_desc_ldo(4),
534         regulator_desc_ldo(5),
535         regulator_desc_ldo(6),
536         regulator_desc_ldo(7),
537         regulator_desc_ldo(8),
538         regulator_desc_ldo(9),
539         regulator_desc_ldo(10),
540         regulator_desc_ldo(11),
541         regulator_desc_ldo(12),
542         regulator_desc_ldo(13),
543         regulator_desc_ldo(14),
544         regulator_desc_ldo(15),
545         regulator_desc_ldo(16),
546         regulator_desc_ldo(17),
547         regulator_desc_ldo(18),
548         regulator_desc_ldo(19),
549         regulator_desc_ldo(20),
550         regulator_desc_ldo(21),
551         regulator_desc_ldo(22),
552         regulator_desc_ldo(23),
553         regulator_desc_ldo(24),
554         regulator_desc_ldo(25),
555         regulator_desc_ldo(26),
556         regulator_desc_ldo(27),
557         regulator_desc_ldo(28),
558         regulator_desc_buck(1),
559         regulator_desc_buck(2),
560         regulator_desc_buck(3),
561         regulator_desc_buck(4),
562         regulator_desc_buck(5),
563         regulator_desc_buck(6),
564         regulator_desc_buck(7),
565         regulator_desc_buck(8),
566         regulator_desc_buck(9),
567         regulator_desc_32khz(AP),
568         regulator_desc_32khz(CP),
569         regulator_desc_32khz(BT),
570 };
571
572 #ifdef CONFIG_OF
573 static int s5m8767_pmic_dt_parse_pdata(struct s5m87xx_dev *iodev,
574                                         struct s5m_platform_data *pdata)
575 {
576         struct device_node *pmic_np, *regulators_np, *reg_np;
577         struct s5m_regulator_data *rdata;
578         unsigned int i;
579
580         pmic_np = iodev->dev->of_node;
581         if (!pmic_np) {
582                 dev_err(iodev->dev, "could not find pmic sub-node\n");
583                 return -ENODEV;
584         }
585
586         regulators_np = of_find_node_by_name(pmic_np, "voltage-regulators");
587         if (!regulators_np) {
588                 dev_err(iodev->dev, "could not find regulators sub-node\n");
589                 return -EINVAL;
590         }
591
592         /* count the number of regulators to be supported in pmic */
593         pdata->num_regulators = 0;
594         for_each_child_of_node(regulators_np, reg_np)
595                 pdata->num_regulators++;
596
597         rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) *
598                                 pdata->num_regulators, GFP_KERNEL);
599         if (!rdata) {
600                 dev_err(iodev->dev,
601                         "could not allocate memory for regulator data\n");
602                 return -ENOMEM;
603         }
604
605         pdata->regulators = rdata;
606         for_each_child_of_node(regulators_np, reg_np) {
607                 for (i = 0; i < ARRAY_SIZE(regulators); i++)
608                         if (!of_node_cmp(reg_np->name, regulators[i].name))
609                                 break;
610
611                 if (i == ARRAY_SIZE(regulators)) {
612                         dev_warn(iodev->dev,
613                                 "No configuration data for regulator %s\n",
614                                 reg_np->name);
615                         continue;
616                 }
617
618                 rdata->id = i;
619                 rdata->initdata = of_get_regulator_init_data(
620                                                 iodev->dev, reg_np);
621                 rdata->reg_node = reg_np;
622                 if (of_property_read_u32(reg_np, "reg_op_mode",
623                                 &rdata->reg_op_mode)) {
624                         dev_warn(iodev->dev, "no op_mode property property at %s\n",
625                                 reg_np->full_name);
626                         /*
627                          * Set operating mode to NORMAL "ON" as default. The
628                          * 32KHz clocks are being turned on and kept on by
629                          * default, so the below mode setting does not impact
630                          * it.
631                          */
632                         rdata->reg_op_mode = S5M8767_OPMODE_MASK;
633                 }
634                 rdata++;
635         }
636
637         if (!of_property_read_u32(pmic_np, "s5m8767,buck_ramp_delay", &i))
638                 pdata->buck_ramp_delay = i & 0xf;
639
640         if (of_get_property(pmic_np, "s5m8767,buck2_ramp_enable", NULL))
641                 pdata->buck2_ramp_enable = 1;
642
643         if (of_get_property(pmic_np, "s5m8767,buck3_ramp_enable", NULL))
644                 pdata->buck3_ramp_enable = 1;
645
646         if (of_get_property(pmic_np, "s5m8767,buck4_ramp_enable", NULL))
647                 pdata->buck4_ramp_enable = 1;
648
649         return 0;
650 }
651 #else
652 static int s5m8767_pmic_dt_parse_pdata(struct s5m87xx_dev *iodev,
653                                         struct s5m_platform_data *pdata)
654 {
655         return 0;
656 }
657 #endif  /* CONFIG_OF */
658
659 static __devinit int s5m8767_pmic_probe(struct platform_device *pdev)
660 {
661         struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent);
662         struct s5m_platform_data *pdata = iodev->pdata;
663         struct regulator_dev **rdev;
664         struct s5m8767_info *s5m8767;
665         int i, ret, size;
666
667         if (!pdata) {
668                 dev_err(pdev->dev.parent, "Platform data not supplied\n");
669                 return -ENODEV;
670         }
671
672         if (iodev->dev->of_node) {
673                 ret = s5m8767_pmic_dt_parse_pdata(iodev, pdata);
674                 if (ret)
675                         return ret;
676         }
677
678         if (pdata->buck2_gpiodvs) {
679                 if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) {
680                         dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
681                         return -EINVAL;
682                 }
683         }
684
685         if (pdata->buck3_gpiodvs) {
686                 if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) {
687                         dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
688                         return -EINVAL;
689                 }
690         }
691
692         if (pdata->buck4_gpiodvs) {
693                 if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) {
694                         dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n");
695                         return -EINVAL;
696                 }
697         }
698
699         s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info),
700                                 GFP_KERNEL);
701         if (!s5m8767)
702                 return -ENOMEM;
703
704         if (!pdata->num_regulators)
705                 pdata->num_regulators = S5M8767_REG_MAX - 2;
706
707         size = sizeof(struct regulator_dev *) * pdata->num_regulators;
708         s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
709         if (!s5m8767->rdev)
710                 return -ENOMEM;
711
712         rdev = s5m8767->rdev;
713         s5m8767->dev = &pdev->dev;
714         s5m8767->iodev = iodev;
715         s5m8767->num_regulators = pdata->num_regulators;
716         platform_set_drvdata(pdev, s5m8767);
717
718         s5m8767->buck_gpioindex = pdata->buck_default_idx;
719         s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs;
720         s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs;
721         s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs;
722         s5m8767->buck_gpios[0] = pdata->buck_gpios[0];
723         s5m8767->buck_gpios[1] = pdata->buck_gpios[1];
724         s5m8767->buck_gpios[2] = pdata->buck_gpios[2];
725         s5m8767->ramp_delay = pdata->buck_ramp_delay;
726         s5m8767->buck2_ramp = pdata->buck2_ramp_enable;
727         s5m8767->buck3_ramp = pdata->buck3_ramp_enable;
728         s5m8767->buck4_ramp = pdata->buck4_ramp_enable;
729
730         for (i = 0; i < 8; i++) {
731                 if (s5m8767->buck2_gpiodvs) {
732                         s5m8767->buck2_vol[i] =
733                                 s5m8767_convert_voltage_to_sel(
734                                                 &buck_voltage_val2,
735                                                 pdata->buck2_voltage[i],
736                                                 pdata->buck2_voltage[i] +
737                                                 buck_voltage_val2.step);
738                 }
739
740                 if (s5m8767->buck3_gpiodvs) {
741                         s5m8767->buck3_vol[i] =
742                                 s5m8767_convert_voltage_to_sel(
743                                                 &buck_voltage_val2,
744                                                 pdata->buck3_voltage[i],
745                                                 pdata->buck3_voltage[i] +
746                                                 buck_voltage_val2.step);
747                 }
748
749                 if (s5m8767->buck4_gpiodvs) {
750                         s5m8767->buck4_vol[i] =
751                                 s5m8767_convert_voltage_to_sel(
752                                                 &buck_voltage_val2,
753                                                 pdata->buck4_voltage[i],
754                                                 pdata->buck4_voltage[i] +
755                                                 buck_voltage_val2.step);
756                 }
757         }
758
759         if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs ||
760                 pdata->buck4_gpiodvs) {
761                 if (gpio_is_valid(pdata->buck_gpios[0]) &&
762                         gpio_is_valid(pdata->buck_gpios[1]) &&
763                         gpio_is_valid(pdata->buck_gpios[2])) {
764                         ret = gpio_request(pdata->buck_gpios[0],
765                                                 "S5M8767 SET1");
766                         if (ret == -EBUSY)
767                                 dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n");
768
769                         ret = gpio_request(pdata->buck_gpios[1],
770                                            "S5M8767 SET2");
771                         if (ret == -EBUSY)
772                                 dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n");
773
774                         ret = gpio_request(pdata->buck_gpios[2],
775                                            "S5M8767 SET3");
776                         if (ret == -EBUSY)
777                                 dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n");
778                         /* SET1 GPIO */
779                         gpio_direction_output(pdata->buck_gpios[0],
780                                         (s5m8767->buck_gpioindex >> 2) & 0x1);
781                         /* SET2 GPIO */
782                         gpio_direction_output(pdata->buck_gpios[1],
783                                         (s5m8767->buck_gpioindex >> 1) & 0x1);
784                         /* SET3 GPIO */
785                         gpio_direction_output(pdata->buck_gpios[2],
786                                         (s5m8767->buck_gpioindex >> 0) & 0x1);
787                         ret = 0;
788                 } else {
789                         dev_err(&pdev->dev, "GPIO NOT VALID\n");
790                         ret = -EINVAL;
791                         return ret;
792                 }
793         }
794
795         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL,
796                         (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
797         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL,
798                         (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
799         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL,
800                         (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1);
801
802         /* Initialize GPIO DVS registers */
803         for (i = 0; i < 8; i++) {
804                 if (s5m8767->buck2_gpiodvs) {
805                         s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i,
806                                            s5m8767->buck2_vol[i]);
807                 }
808
809                 if (s5m8767->buck3_gpiodvs) {
810                         s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i,
811                                            s5m8767->buck3_vol[i]);
812                 }
813
814                 if (s5m8767->buck4_gpiodvs) {
815                         s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i,
816                                            s5m8767->buck4_vol[i]);
817                 }
818         }
819         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff);
820         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff);
821         s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff);
822
823         if (s5m8767->buck2_ramp)
824                 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08);
825
826         if (s5m8767->buck3_ramp)
827                 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04);
828
829         if (s5m8767->buck4_ramp)
830                 s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02);
831
832         if (s5m8767->buck2_ramp || s5m8767->buck3_ramp
833                 || s5m8767->buck4_ramp) {
834                 switch (s5m8767->ramp_delay) {
835                 case 15:
836                         s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
837                                         0xc0, 0xf0);
838                         break;
839                 case 25:
840                         s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
841                                         0xd0, 0xf0);
842                         break;
843                 case 50:
844                         s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
845                                         0xe0, 0xf0);
846                         break;
847                 case 100:
848                         s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
849                                         0xf0, 0xf0);
850                         break;
851                 default:
852                         s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP,
853                                         0x90, 0xf0);
854                 }
855         }
856
857         for (i = 0; i < pdata->num_regulators; i++) {
858                 const struct s5m_voltage_desc *desc;
859                 int id = pdata->regulators[i].id;
860
861                 desc = reg_voltage_map[id];
862                 if (desc)
863                         regulators[id].n_voltages =
864                                 (desc->max - desc->min) / desc->step + 1;
865
866                 rdev[i] = regulator_register(&regulators[id], s5m8767->dev,
867                                              pdata->regulators[i].initdata,
868                                              s5m8767,
869                                              pdata->regulators[i].reg_node);
870                 if (IS_ERR(rdev[i])) {
871                         ret = PTR_ERR(rdev[i]);
872                         dev_err(s5m8767->dev, "regulator init failed for %d\n",
873                                         id);
874                         rdev[i] = NULL;
875                         goto err;
876                 }
877         }
878
879         return 0;
880 err:
881         for (i = 0; i < s5m8767->num_regulators; i++)
882                 if (rdev[i])
883                         regulator_unregister(rdev[i]);
884
885         return ret;
886 }
887
888 static int __devexit s5m8767_pmic_remove(struct platform_device *pdev)
889 {
890         struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev);
891         struct regulator_dev **rdev = s5m8767->rdev;
892         int i;
893
894         for (i = 0; i < s5m8767->num_regulators; i++)
895                 if (rdev[i])
896                         regulator_unregister(rdev[i]);
897
898         return 0;
899 }
900
901 static const struct platform_device_id s5m8767_pmic_id[] = {
902         { "s5m8767-pmic", 0},
903         { },
904 };
905 MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id);
906
907 static struct platform_driver s5m8767_pmic_driver = {
908         .driver = {
909                 .name = "s5m8767-pmic",
910                 .owner = THIS_MODULE,
911         },
912         .probe = s5m8767_pmic_probe,
913         .remove = __devexit_p(s5m8767_pmic_remove),
914         .id_table = s5m8767_pmic_id,
915 };
916
917 static int __init s5m8767_pmic_init(void)
918 {
919         return platform_driver_register(&s5m8767_pmic_driver);
920 }
921 subsys_initcall(s5m8767_pmic_init);
922
923 static void __exit s5m8767_pmic_exit(void)
924 {
925         platform_driver_unregister(&s5m8767_pmic_driver);
926 }
927 module_exit(s5m8767_pmic_exit);
928
929 /* Module information */
930 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
931 MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver");
932 MODULE_LICENSE("GPL");