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