regulator: regulator driver for Palmas series chips
[linux-2.6.git] / drivers / regulator / max8998.c
1 /*
2  * max8998.c - Voltage regulator driver for the Maxim 8998
3  *
4  *  Copyright (C) 2009-2010 Samsung Electronics
5  *  Kyungmin Park <kyungmin.park@samsung.com>
6  *  Marek Szyprowski <m.szyprowski@samsung.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
27 #include <linux/gpio.h>
28 #include <linux/slab.h>
29 #include <linux/interrupt.h>
30 #include <linux/mutex.h>
31 #include <linux/delay.h>
32 #include <linux/platform_device.h>
33 #include <linux/regulator/driver.h>
34 #include <linux/mfd/max8998.h>
35 #include <linux/mfd/max8998-private.h>
36
37 struct max8998_data {
38         struct device           *dev;
39         struct max8998_dev      *iodev;
40         int                     num_regulators;
41         struct regulator_dev    **rdev;
42         u8                      buck1_vol[4]; /* voltages for selection */
43         u8                      buck2_vol[2];
44         unsigned int            buck1_idx; /* index to last changed voltage */
45                                            /* value in a set */
46         unsigned int            buck2_idx;
47 };
48
49 struct voltage_map_desc {
50         int min;
51         int max;
52         int step;
53 };
54
55 /* Voltage maps */
56 static const struct voltage_map_desc ldo23_voltage_map_desc = {
57         .min = 800,     .step = 50,     .max = 1300,
58 };
59 static const struct voltage_map_desc ldo456711_voltage_map_desc = {
60         .min = 1600,    .step = 100,    .max = 3600,
61 };
62 static const struct voltage_map_desc ldo8_voltage_map_desc = {
63         .min = 3000,    .step = 100,    .max = 3600,
64 };
65 static const struct voltage_map_desc ldo9_voltage_map_desc = {
66         .min = 2800,    .step = 100,    .max = 3100,
67 };
68 static const struct voltage_map_desc ldo10_voltage_map_desc = {
69         .min = 950,     .step = 50,     .max = 1300,
70 };
71 static const struct voltage_map_desc ldo1213_voltage_map_desc = {
72         .min = 800,     .step = 100,    .max = 3300,
73 };
74 static const struct voltage_map_desc ldo1415_voltage_map_desc = {
75         .min = 1200,    .step = 100,    .max = 3300,
76 };
77 static const struct voltage_map_desc ldo1617_voltage_map_desc = {
78         .min = 1600,    .step = 100,    .max = 3600,
79 };
80 static const struct voltage_map_desc buck12_voltage_map_desc = {
81         .min = 750,     .step = 25,     .max = 1525,
82 };
83 static const struct voltage_map_desc buck3_voltage_map_desc = {
84         .min = 1600,    .step = 100,    .max = 3600,
85 };
86 static const struct voltage_map_desc buck4_voltage_map_desc = {
87         .min = 800,     .step = 100,    .max = 2300,
88 };
89
90 static const struct voltage_map_desc *ldo_voltage_map[] = {
91         NULL,
92         NULL,
93         &ldo23_voltage_map_desc,        /* LDO2 */
94         &ldo23_voltage_map_desc,        /* LDO3 */
95         &ldo456711_voltage_map_desc,    /* LDO4 */
96         &ldo456711_voltage_map_desc,    /* LDO5 */
97         &ldo456711_voltage_map_desc,    /* LDO6 */
98         &ldo456711_voltage_map_desc,    /* LDO7 */
99         &ldo8_voltage_map_desc,         /* LDO8 */
100         &ldo9_voltage_map_desc,         /* LDO9 */
101         &ldo10_voltage_map_desc,        /* LDO10 */
102         &ldo456711_voltage_map_desc,    /* LDO11 */
103         &ldo1213_voltage_map_desc,      /* LDO12 */
104         &ldo1213_voltage_map_desc,      /* LDO13 */
105         &ldo1415_voltage_map_desc,      /* LDO14 */
106         &ldo1415_voltage_map_desc,      /* LDO15 */
107         &ldo1617_voltage_map_desc,      /* LDO16 */
108         &ldo1617_voltage_map_desc,      /* LDO17 */
109         &buck12_voltage_map_desc,       /* BUCK1 */
110         &buck12_voltage_map_desc,       /* BUCK2 */
111         &buck3_voltage_map_desc,        /* BUCK3 */
112         &buck4_voltage_map_desc,        /* BUCK4 */
113 };
114
115 static int max8998_list_voltage(struct regulator_dev *rdev,
116                                 unsigned int selector)
117 {
118         const struct voltage_map_desc *desc;
119         int ldo = rdev_get_id(rdev);
120         int val;
121
122         if (ldo >= ARRAY_SIZE(ldo_voltage_map))
123                 return -EINVAL;
124
125         desc = ldo_voltage_map[ldo];
126         if (desc == NULL)
127                 return -EINVAL;
128
129         val = desc->min + desc->step * selector;
130         if (val > desc->max)
131                 return -EINVAL;
132
133         return val * 1000;
134 }
135
136 static int max8998_get_enable_register(struct regulator_dev *rdev,
137                                         int *reg, int *shift)
138 {
139         int ldo = rdev_get_id(rdev);
140
141         switch (ldo) {
142         case MAX8998_LDO2 ... MAX8998_LDO5:
143                 *reg = MAX8998_REG_ONOFF1;
144                 *shift = 3 - (ldo - MAX8998_LDO2);
145                 break;
146         case MAX8998_LDO6 ... MAX8998_LDO13:
147                 *reg = MAX8998_REG_ONOFF2;
148                 *shift = 7 - (ldo - MAX8998_LDO6);
149                 break;
150         case MAX8998_LDO14 ... MAX8998_LDO17:
151                 *reg = MAX8998_REG_ONOFF3;
152                 *shift = 7 - (ldo - MAX8998_LDO14);
153                 break;
154         case MAX8998_BUCK1 ... MAX8998_BUCK4:
155                 *reg = MAX8998_REG_ONOFF1;
156                 *shift = 7 - (ldo - MAX8998_BUCK1);
157                 break;
158         case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG:
159                 *reg = MAX8998_REG_ONOFF4;
160                 *shift = 7 - (ldo - MAX8998_EN32KHZ_AP);
161                 break;
162         case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2:
163                 *reg = MAX8998_REG_CHGR2;
164                 *shift = 7 - (ldo - MAX8998_ESAFEOUT1);
165                 break;
166         default:
167                 return -EINVAL;
168         }
169
170         return 0;
171 }
172
173 static int max8998_ldo_is_enabled(struct regulator_dev *rdev)
174 {
175         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
176         struct i2c_client *i2c = max8998->iodev->i2c;
177         int ret, reg, shift = 8;
178         u8 val;
179
180         ret = max8998_get_enable_register(rdev, &reg, &shift);
181         if (ret)
182                 return ret;
183
184         ret = max8998_read_reg(i2c, reg, &val);
185         if (ret)
186                 return ret;
187
188         return val & (1 << shift);
189 }
190
191 static int max8998_ldo_enable(struct regulator_dev *rdev)
192 {
193         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
194         struct i2c_client *i2c = max8998->iodev->i2c;
195         int reg, shift = 8, ret;
196
197         ret = max8998_get_enable_register(rdev, &reg, &shift);
198         if (ret)
199                 return ret;
200
201         return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift);
202 }
203
204 static int max8998_ldo_disable(struct regulator_dev *rdev)
205 {
206         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
207         struct i2c_client *i2c = max8998->iodev->i2c;
208         int reg, shift = 8, ret;
209
210         ret = max8998_get_enable_register(rdev, &reg, &shift);
211         if (ret)
212                 return ret;
213
214         return max8998_update_reg(i2c, reg, 0, 1<<shift);
215 }
216
217 static int max8998_get_voltage_register(struct regulator_dev *rdev,
218                                 int *_reg, int *_shift, int *_mask)
219 {
220         int ldo = rdev_get_id(rdev);
221         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
222         int reg, shift = 0, mask = 0xff;
223
224         switch (ldo) {
225         case MAX8998_LDO2 ... MAX8998_LDO3:
226                 reg = MAX8998_REG_LDO2_LDO3;
227                 mask = 0xf;
228                 if (ldo == MAX8998_LDO2)
229                         shift = 4;
230                 else
231                         shift = 0;
232                 break;
233         case MAX8998_LDO4 ... MAX8998_LDO7:
234                 reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4);
235                 break;
236         case MAX8998_LDO8 ... MAX8998_LDO9:
237                 reg = MAX8998_REG_LDO8_LDO9;
238                 mask = 0xf;
239                 if (ldo == MAX8998_LDO8)
240                         shift = 4;
241                 else
242                         shift = 0;
243                 break;
244         case MAX8998_LDO10 ... MAX8998_LDO11:
245                 reg = MAX8998_REG_LDO10_LDO11;
246                 if (ldo == MAX8998_LDO10) {
247                         shift = 5;
248                         mask = 0x7;
249                 } else {
250                         shift = 0;
251                         mask = 0x1f;
252                 }
253                 break;
254         case MAX8998_LDO12 ... MAX8998_LDO17:
255                 reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12);
256                 break;
257         case MAX8998_BUCK1:
258                 reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx;
259                 break;
260         case MAX8998_BUCK2:
261                 reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx;
262                 break;
263         case MAX8998_BUCK3:
264                 reg = MAX8998_REG_BUCK3;
265                 break;
266         case MAX8998_BUCK4:
267                 reg = MAX8998_REG_BUCK4;
268                 break;
269         default:
270                 return -EINVAL;
271         }
272
273         *_reg = reg;
274         *_shift = shift;
275         *_mask = mask;
276
277         return 0;
278 }
279
280 static int max8998_get_voltage(struct regulator_dev *rdev)
281 {
282         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
283         struct i2c_client *i2c = max8998->iodev->i2c;
284         int reg, shift = 0, mask, ret;
285         u8 val;
286
287         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
288         if (ret)
289                 return ret;
290
291         ret = max8998_read_reg(i2c, reg, &val);
292         if (ret)
293                 return ret;
294
295         val >>= shift;
296         val &= mask;
297
298         return max8998_list_voltage(rdev, val);
299 }
300
301 static int max8998_set_voltage_ldo(struct regulator_dev *rdev,
302                                    int min_uV, int max_uV, unsigned *selector)
303 {
304         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
305         struct i2c_client *i2c = max8998->iodev->i2c;
306         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
307         const struct voltage_map_desc *desc;
308         int ldo = rdev_get_id(rdev);
309         int reg, shift = 0, mask, ret;
310         int i = 0;
311
312         if (ldo >= ARRAY_SIZE(ldo_voltage_map))
313                 return -EINVAL;
314
315         desc = ldo_voltage_map[ldo];
316         if (desc == NULL)
317                 return -EINVAL;
318
319         if (max_vol < desc->min || min_vol > desc->max)
320                 return -EINVAL;
321
322         while (desc->min + desc->step*i < min_vol &&
323                desc->min + desc->step*i < desc->max)
324                 i++;
325
326         if (desc->min + desc->step*i > max_vol)
327                 return -EINVAL;
328
329         *selector = i;
330
331         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
332         if (ret)
333                 return ret;
334
335         ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
336
337         return ret;
338 }
339
340 static inline void buck1_gpio_set(int gpio1, int gpio2, int v)
341 {
342         gpio_set_value(gpio1, v & 0x1);
343         gpio_set_value(gpio2, (v >> 1) & 0x1);
344 }
345
346 static inline void buck2_gpio_set(int gpio, int v)
347 {
348         gpio_set_value(gpio, v & 0x1);
349 }
350
351 static int max8998_set_voltage_buck(struct regulator_dev *rdev,
352                                     int min_uV, int max_uV, unsigned *selector)
353 {
354         struct max8998_data *max8998 = rdev_get_drvdata(rdev);
355         struct max8998_platform_data *pdata =
356                 dev_get_platdata(max8998->iodev->dev);
357         struct i2c_client *i2c = max8998->iodev->i2c;
358         int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
359         const struct voltage_map_desc *desc;
360         int buck = rdev_get_id(rdev);
361         int reg, shift = 0, mask, ret;
362         int difference = 0, i = 0, j = 0, previous_vol = 0;
363         u8 val = 0;
364         static u8 buck1_last_val;
365
366         if (buck >= ARRAY_SIZE(ldo_voltage_map))
367                 return -EINVAL;
368
369         desc = ldo_voltage_map[buck];
370
371         if (desc == NULL)
372                 return -EINVAL;
373
374         if (max_vol < desc->min || min_vol > desc->max)
375                 return -EINVAL;
376
377         while (desc->min + desc->step*i < min_vol &&
378                desc->min + desc->step*i < desc->max)
379                 i++;
380
381         if (desc->min + desc->step*i > max_vol)
382                 return -EINVAL;
383
384         *selector = i;
385
386         ret = max8998_get_voltage_register(rdev, &reg, &shift, &mask);
387         if (ret)
388                 return ret;
389
390         previous_vol = max8998_get_voltage(rdev);
391
392         /* Check if voltage needs to be changed */
393         /* if previous_voltage equal new voltage, return */
394         if (previous_vol == max8998_list_voltage(rdev, i)) {
395                 dev_dbg(max8998->dev, "No voltage change, old:%d, new:%d\n",
396                         previous_vol, max8998_list_voltage(rdev, i));
397                 return ret;
398         }
399
400         switch (buck) {
401         case MAX8998_BUCK1:
402                 dev_dbg(max8998->dev,
403                         "BUCK1, i:%d, buck1_vol1:%d, buck1_vol2:%d\n"
404                         "buck1_vol3:%d, buck1_vol4:%d\n",
405                         i, max8998->buck1_vol[0], max8998->buck1_vol[1],
406                         max8998->buck1_vol[2], max8998->buck1_vol[3]);
407
408                 if (gpio_is_valid(pdata->buck1_set1) &&
409                     gpio_is_valid(pdata->buck1_set2)) {
410
411                         /* check if requested voltage */
412                         /* value is already defined */
413                         for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) {
414                                 if (max8998->buck1_vol[j] == i) {
415                                         max8998->buck1_idx = j;
416                                         buck1_gpio_set(pdata->buck1_set1,
417                                                        pdata->buck1_set2, j);
418                                         goto buck1_exit;
419                                 }
420                         }
421
422                         if (pdata->buck_voltage_lock)
423                                 return -EINVAL;
424
425                         /* no predefine regulator found */
426                         max8998->buck1_idx = (buck1_last_val % 2) + 2;
427                         dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n",
428                                 max8998->buck1_idx);
429                         max8998->buck1_vol[max8998->buck1_idx] = i;
430                         ret = max8998_get_voltage_register(rdev, &reg,
431                                                            &shift,
432                                                            &mask);
433                         ret = max8998_write_reg(i2c, reg, i);
434                         buck1_gpio_set(pdata->buck1_set1,
435                                        pdata->buck1_set2, max8998->buck1_idx);
436                         buck1_last_val++;
437 buck1_exit:
438                         dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n",
439                                 i2c->name, gpio_get_value(pdata->buck1_set1),
440                                 gpio_get_value(pdata->buck1_set2));
441                         break;
442                 } else {
443                         ret = max8998_write_reg(i2c, reg, i);
444                 }
445                 break;
446
447         case MAX8998_BUCK2:
448                 dev_dbg(max8998->dev,
449                         "BUCK2, i:%d buck2_vol1:%d, buck2_vol2:%d\n"
450                         , i, max8998->buck2_vol[0], max8998->buck2_vol[1]);
451                 if (gpio_is_valid(pdata->buck2_set3)) {
452
453                         /* check if requested voltage */
454                         /* value is already defined */
455                         for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) {
456                                 if (max8998->buck2_vol[j] == i) {
457                                         max8998->buck2_idx = j;
458                                         buck2_gpio_set(pdata->buck2_set3, j);
459                                         goto buck2_exit;
460                                 }
461                         }
462
463                         if (pdata->buck_voltage_lock)
464                                 return -EINVAL;
465
466                         max8998_get_voltage_register(rdev,
467                                         &reg, &shift, &mask);
468                         ret = max8998_write_reg(i2c, reg, i);
469                         max8998->buck2_vol[max8998->buck2_idx] = i;
470                         buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx);
471 buck2_exit:
472                         dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name,
473                                 gpio_get_value(pdata->buck2_set3));
474                 } else {
475                         ret = max8998_write_reg(i2c, reg, i);
476                 }
477                 break;
478
479         case MAX8998_BUCK3:
480         case MAX8998_BUCK4:
481                 ret = max8998_update_reg(i2c, reg, i<<shift, mask<<shift);
482                 break;
483         }
484
485         /* Voltage stabilization */
486         max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val);
487
488         /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */
489         /* MAX8998 has ENRAMP bit implemented, so test it*/
490         if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP))
491                 return ret;
492
493         difference = desc->min + desc->step*i - previous_vol/1000;
494         if (difference > 0)
495                 udelay(difference / ((val & 0x0f) + 1));
496
497         return ret;
498 }
499
500 static struct regulator_ops max8998_ldo_ops = {
501         .list_voltage           = max8998_list_voltage,
502         .is_enabled             = max8998_ldo_is_enabled,
503         .enable                 = max8998_ldo_enable,
504         .disable                = max8998_ldo_disable,
505         .get_voltage            = max8998_get_voltage,
506         .set_voltage            = max8998_set_voltage_ldo,
507         .set_suspend_enable     = max8998_ldo_enable,
508         .set_suspend_disable    = max8998_ldo_disable,
509 };
510
511 static struct regulator_ops max8998_buck_ops = {
512         .list_voltage           = max8998_list_voltage,
513         .is_enabled             = max8998_ldo_is_enabled,
514         .enable                 = max8998_ldo_enable,
515         .disable                = max8998_ldo_disable,
516         .get_voltage            = max8998_get_voltage,
517         .set_voltage            = max8998_set_voltage_buck,
518         .set_suspend_enable     = max8998_ldo_enable,
519         .set_suspend_disable    = max8998_ldo_disable,
520 };
521
522 static struct regulator_ops max8998_others_ops = {
523         .is_enabled             = max8998_ldo_is_enabled,
524         .enable                 = max8998_ldo_enable,
525         .disable                = max8998_ldo_disable,
526         .set_suspend_enable     = max8998_ldo_enable,
527         .set_suspend_disable    = max8998_ldo_disable,
528 };
529
530 static struct regulator_desc regulators[] = {
531         {
532                 .name           = "LDO2",
533                 .id             = MAX8998_LDO2,
534                 .ops            = &max8998_ldo_ops,
535                 .type           = REGULATOR_VOLTAGE,
536                 .owner          = THIS_MODULE,
537         }, {
538                 .name           = "LDO3",
539                 .id             = MAX8998_LDO3,
540                 .ops            = &max8998_ldo_ops,
541                 .type           = REGULATOR_VOLTAGE,
542                 .owner          = THIS_MODULE,
543         }, {
544                 .name           = "LDO4",
545                 .id             = MAX8998_LDO4,
546                 .ops            = &max8998_ldo_ops,
547                 .type           = REGULATOR_VOLTAGE,
548                 .owner          = THIS_MODULE,
549         }, {
550                 .name           = "LDO5",
551                 .id             = MAX8998_LDO5,
552                 .ops            = &max8998_ldo_ops,
553                 .type           = REGULATOR_VOLTAGE,
554                 .owner          = THIS_MODULE,
555         }, {
556                 .name           = "LDO6",
557                 .id             = MAX8998_LDO6,
558                 .ops            = &max8998_ldo_ops,
559                 .type           = REGULATOR_VOLTAGE,
560                 .owner          = THIS_MODULE,
561         }, {
562                 .name           = "LDO7",
563                 .id             = MAX8998_LDO7,
564                 .ops            = &max8998_ldo_ops,
565                 .type           = REGULATOR_VOLTAGE,
566                 .owner          = THIS_MODULE,
567         }, {
568                 .name           = "LDO8",
569                 .id             = MAX8998_LDO8,
570                 .ops            = &max8998_ldo_ops,
571                 .type           = REGULATOR_VOLTAGE,
572                 .owner          = THIS_MODULE,
573         }, {
574                 .name           = "LDO9",
575                 .id             = MAX8998_LDO9,
576                 .ops            = &max8998_ldo_ops,
577                 .type           = REGULATOR_VOLTAGE,
578                 .owner          = THIS_MODULE,
579         }, {
580                 .name           = "LDO10",
581                 .id             = MAX8998_LDO10,
582                 .ops            = &max8998_ldo_ops,
583                 .type           = REGULATOR_VOLTAGE,
584                 .owner          = THIS_MODULE,
585         }, {
586                 .name           = "LDO11",
587                 .id             = MAX8998_LDO11,
588                 .ops            = &max8998_ldo_ops,
589                 .type           = REGULATOR_VOLTAGE,
590                 .owner          = THIS_MODULE,
591         }, {
592                 .name           = "LDO12",
593                 .id             = MAX8998_LDO12,
594                 .ops            = &max8998_ldo_ops,
595                 .type           = REGULATOR_VOLTAGE,
596                 .owner          = THIS_MODULE,
597         }, {
598                 .name           = "LDO13",
599                 .id             = MAX8998_LDO13,
600                 .ops            = &max8998_ldo_ops,
601                 .type           = REGULATOR_VOLTAGE,
602                 .owner          = THIS_MODULE,
603         }, {
604                 .name           = "LDO14",
605                 .id             = MAX8998_LDO14,
606                 .ops            = &max8998_ldo_ops,
607                 .type           = REGULATOR_VOLTAGE,
608                 .owner          = THIS_MODULE,
609         }, {
610                 .name           = "LDO15",
611                 .id             = MAX8998_LDO15,
612                 .ops            = &max8998_ldo_ops,
613                 .type           = REGULATOR_VOLTAGE,
614                 .owner          = THIS_MODULE,
615         }, {
616                 .name           = "LDO16",
617                 .id             = MAX8998_LDO16,
618                 .ops            = &max8998_ldo_ops,
619                 .type           = REGULATOR_VOLTAGE,
620                 .owner          = THIS_MODULE,
621         }, {
622                 .name           = "LDO17",
623                 .id             = MAX8998_LDO17,
624                 .ops            = &max8998_ldo_ops,
625                 .type           = REGULATOR_VOLTAGE,
626                 .owner          = THIS_MODULE,
627         }, {
628                 .name           = "BUCK1",
629                 .id             = MAX8998_BUCK1,
630                 .ops            = &max8998_buck_ops,
631                 .type           = REGULATOR_VOLTAGE,
632                 .owner          = THIS_MODULE,
633         }, {
634                 .name           = "BUCK2",
635                 .id             = MAX8998_BUCK2,
636                 .ops            = &max8998_buck_ops,
637                 .type           = REGULATOR_VOLTAGE,
638                 .owner          = THIS_MODULE,
639         }, {
640                 .name           = "BUCK3",
641                 .id             = MAX8998_BUCK3,
642                 .ops            = &max8998_buck_ops,
643                 .type           = REGULATOR_VOLTAGE,
644                 .owner          = THIS_MODULE,
645         }, {
646                 .name           = "BUCK4",
647                 .id             = MAX8998_BUCK4,
648                 .ops            = &max8998_buck_ops,
649                 .type           = REGULATOR_VOLTAGE,
650                 .owner          = THIS_MODULE,
651         }, {
652                 .name           = "EN32KHz AP",
653                 .id             = MAX8998_EN32KHZ_AP,
654                 .ops            = &max8998_others_ops,
655                 .type           = REGULATOR_VOLTAGE,
656                 .owner          = THIS_MODULE,
657         }, {
658                 .name           = "EN32KHz CP",
659                 .id             = MAX8998_EN32KHZ_CP,
660                 .ops            = &max8998_others_ops,
661                 .type           = REGULATOR_VOLTAGE,
662                 .owner          = THIS_MODULE,
663         }, {
664                 .name           = "ENVICHG",
665                 .id             = MAX8998_ENVICHG,
666                 .ops            = &max8998_others_ops,
667                 .type           = REGULATOR_VOLTAGE,
668                 .owner          = THIS_MODULE,
669         }, {
670                 .name           = "ESAFEOUT1",
671                 .id             = MAX8998_ESAFEOUT1,
672                 .ops            = &max8998_others_ops,
673                 .type           = REGULATOR_VOLTAGE,
674                 .owner          = THIS_MODULE,
675         }, {
676                 .name           = "ESAFEOUT2",
677                 .id             = MAX8998_ESAFEOUT2,
678                 .ops            = &max8998_others_ops,
679                 .type           = REGULATOR_VOLTAGE,
680                 .owner          = THIS_MODULE,
681         }
682 };
683
684 static __devinit int max8998_pmic_probe(struct platform_device *pdev)
685 {
686         struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent);
687         struct max8998_platform_data *pdata = dev_get_platdata(iodev->dev);
688         struct regulator_dev **rdev;
689         struct max8998_data *max8998;
690         struct i2c_client *i2c;
691         int i, ret, size;
692
693         if (!pdata) {
694                 dev_err(pdev->dev.parent, "No platform init data supplied\n");
695                 return -ENODEV;
696         }
697
698         max8998 = kzalloc(sizeof(struct max8998_data), GFP_KERNEL);
699         if (!max8998)
700                 return -ENOMEM;
701
702         size = sizeof(struct regulator_dev *) * pdata->num_regulators;
703         max8998->rdev = kzalloc(size, GFP_KERNEL);
704         if (!max8998->rdev) {
705                 kfree(max8998);
706                 return -ENOMEM;
707         }
708
709         rdev = max8998->rdev;
710         max8998->dev = &pdev->dev;
711         max8998->iodev = iodev;
712         max8998->num_regulators = pdata->num_regulators;
713         platform_set_drvdata(pdev, max8998);
714         i2c = max8998->iodev->i2c;
715
716         max8998->buck1_idx = pdata->buck1_default_idx;
717         max8998->buck2_idx = pdata->buck2_default_idx;
718
719         /* NOTE: */
720         /* For unused GPIO NOT marked as -1 (thereof equal to 0)  WARN_ON */
721         /* will be displayed */
722
723         /* Check if MAX8998 voltage selection GPIOs are defined */
724         if (gpio_is_valid(pdata->buck1_set1) &&
725             gpio_is_valid(pdata->buck1_set2)) {
726                 /* Check if SET1 is not equal to 0 */
727                 if (!pdata->buck1_set1) {
728                         printk(KERN_ERR "MAX8998 SET1 GPIO defined as 0 !\n");
729                         WARN_ON(!pdata->buck1_set1);
730                         ret = -EIO;
731                         goto err_free_mem;
732                 }
733                 /* Check if SET2 is not equal to 0 */
734                 if (!pdata->buck1_set2) {
735                         printk(KERN_ERR "MAX8998 SET2 GPIO defined as 0 !\n");
736                         WARN_ON(!pdata->buck1_set2);
737                         ret = -EIO;
738                         goto err_free_mem;
739                 }
740
741                 gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1");
742                 gpio_direction_output(pdata->buck1_set1,
743                                       max8998->buck1_idx & 0x1);
744
745
746                 gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2");
747                 gpio_direction_output(pdata->buck1_set2,
748                                       (max8998->buck1_idx >> 1) & 0x1);
749                 /* Set predefined value for BUCK1 register 1 */
750                 i = 0;
751                 while (buck12_voltage_map_desc.min +
752                        buck12_voltage_map_desc.step*i
753                        < (pdata->buck1_voltage1 / 1000))
754                         i++;
755                 max8998->buck1_vol[0] = i;
756                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE1, i);
757                 if (ret)
758                         goto err_free_mem;
759
760                 /* Set predefined value for BUCK1 register 2 */
761                 i = 0;
762                 while (buck12_voltage_map_desc.min +
763                        buck12_voltage_map_desc.step*i
764                        < (pdata->buck1_voltage2 / 1000))
765                         i++;
766
767                 max8998->buck1_vol[1] = i;
768                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE2, i);
769                 if (ret)
770                         goto err_free_mem;
771
772                 /* Set predefined value for BUCK1 register 3 */
773                 i = 0;
774                 while (buck12_voltage_map_desc.min +
775                        buck12_voltage_map_desc.step*i
776                        < (pdata->buck1_voltage3 / 1000))
777                         i++;
778
779                 max8998->buck1_vol[2] = i;
780                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE3, i);
781                 if (ret)
782                         goto err_free_mem;
783
784                 /* Set predefined value for BUCK1 register 4 */
785                 i = 0;
786                 while (buck12_voltage_map_desc.min +
787                        buck12_voltage_map_desc.step*i
788                        < (pdata->buck1_voltage4 / 1000))
789                         i++;
790
791                 max8998->buck1_vol[3] = i;
792                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK1_VOLTAGE4, i);
793                 if (ret)
794                         goto err_free_mem;
795
796         }
797
798         if (gpio_is_valid(pdata->buck2_set3)) {
799                 /* Check if SET3 is not equal to 0 */
800                 if (!pdata->buck2_set3) {
801                         printk(KERN_ERR "MAX8998 SET3 GPIO defined as 0 !\n");
802                         WARN_ON(!pdata->buck2_set3);
803                         ret = -EIO;
804                         goto err_free_mem;
805                 }
806                 gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3");
807                 gpio_direction_output(pdata->buck2_set3,
808                                       max8998->buck2_idx & 0x1);
809
810                 /* BUCK2 register 1 */
811                 i = 0;
812                 while (buck12_voltage_map_desc.min +
813                        buck12_voltage_map_desc.step*i
814                        < (pdata->buck2_voltage1 / 1000))
815                         i++;
816                 max8998->buck2_vol[0] = i;
817                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE1, i);
818                 if (ret)
819                         goto err_free_mem;
820
821                 /* BUCK2 register 2 */
822                 i = 0;
823                 while (buck12_voltage_map_desc.min +
824                        buck12_voltage_map_desc.step*i
825                        < (pdata->buck2_voltage2 / 1000))
826                         i++;
827                 max8998->buck2_vol[1] = i;
828                 ret = max8998_write_reg(i2c, MAX8998_REG_BUCK2_VOLTAGE2, i);
829                 if (ret)
830                         goto err_free_mem;
831         }
832
833         for (i = 0; i < pdata->num_regulators; i++) {
834                 const struct voltage_map_desc *desc;
835                 int id = pdata->regulators[i].id;
836                 int index = id - MAX8998_LDO2;
837
838                 desc = ldo_voltage_map[id];
839                 if (desc && regulators[index].ops != &max8998_others_ops) {
840                         int count = (desc->max - desc->min) / desc->step + 1;
841                         regulators[index].n_voltages = count;
842                 }
843                 rdev[i] = regulator_register(&regulators[index], max8998->dev,
844                                 pdata->regulators[i].initdata, max8998, NULL);
845                 if (IS_ERR(rdev[i])) {
846                         ret = PTR_ERR(rdev[i]);
847                         dev_err(max8998->dev, "regulator init failed\n");
848                         rdev[i] = NULL;
849                         goto err;
850                 }
851         }
852
853
854         return 0;
855 err:
856         for (i = 0; i < max8998->num_regulators; i++)
857                 if (rdev[i])
858                         regulator_unregister(rdev[i]);
859
860 err_free_mem:
861         kfree(max8998->rdev);
862         kfree(max8998);
863
864         return ret;
865 }
866
867 static int __devexit max8998_pmic_remove(struct platform_device *pdev)
868 {
869         struct max8998_data *max8998 = platform_get_drvdata(pdev);
870         struct regulator_dev **rdev = max8998->rdev;
871         int i;
872
873         for (i = 0; i < max8998->num_regulators; i++)
874                 if (rdev[i])
875                         regulator_unregister(rdev[i]);
876
877         kfree(max8998->rdev);
878         kfree(max8998);
879
880         return 0;
881 }
882
883 static const struct platform_device_id max8998_pmic_id[] = {
884         { "max8998-pmic", TYPE_MAX8998 },
885         { "lp3974-pmic", TYPE_LP3974 },
886         { }
887 };
888 MODULE_DEVICE_TABLE(platform, max8998_pmic_id);
889
890 static struct platform_driver max8998_pmic_driver = {
891         .driver = {
892                 .name = "max8998-pmic",
893                 .owner = THIS_MODULE,
894         },
895         .probe = max8998_pmic_probe,
896         .remove = __devexit_p(max8998_pmic_remove),
897         .id_table = max8998_pmic_id,
898 };
899
900 static int __init max8998_pmic_init(void)
901 {
902         return platform_driver_register(&max8998_pmic_driver);
903 }
904 subsys_initcall(max8998_pmic_init);
905
906 static void __exit max8998_pmic_cleanup(void)
907 {
908         platform_driver_unregister(&max8998_pmic_driver);
909 }
910 module_exit(max8998_pmic_cleanup);
911
912 MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver");
913 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
914 MODULE_LICENSE("GPL");