Merge tag 'dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / regulator / palmas-regulator.c
1 /*
2  * Driver for Regulator part of Palmas PMIC Chips
3  *
4  * Copyright 2011-2012 Texas Instruments Inc.
5  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under  the terms of the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the License, or (at your
11  *  option) any later version.
12  *
13  */
14
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/slab.h>
23 #include <linux/regmap.h>
24 #include <linux/mfd/palmas.h>
25
26 struct regs_info {
27         char    *name;
28         u8      vsel_addr;
29         u8      ctrl_addr;
30         u8      tstep_addr;
31 };
32
33 static const struct regs_info palmas_regs_info[] = {
34         {
35                 .name           = "SMPS12",
36                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
37                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
38                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
39         },
40         {
41                 .name           = "SMPS123",
42                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
43                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
44                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
45         },
46         {
47                 .name           = "SMPS3",
48                 .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
49                 .ctrl_addr      = PALMAS_SMPS3_CTRL,
50         },
51         {
52                 .name           = "SMPS45",
53                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
54                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
55                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
56         },
57         {
58                 .name           = "SMPS457",
59                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
60                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
61                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
62         },
63         {
64                 .name           = "SMPS6",
65                 .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
66                 .ctrl_addr      = PALMAS_SMPS6_CTRL,
67                 .tstep_addr     = PALMAS_SMPS6_TSTEP,
68         },
69         {
70                 .name           = "SMPS7",
71                 .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
72                 .ctrl_addr      = PALMAS_SMPS7_CTRL,
73         },
74         {
75                 .name           = "SMPS8",
76                 .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
77                 .ctrl_addr      = PALMAS_SMPS8_CTRL,
78                 .tstep_addr     = PALMAS_SMPS8_TSTEP,
79         },
80         {
81                 .name           = "SMPS9",
82                 .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
83                 .ctrl_addr      = PALMAS_SMPS9_CTRL,
84         },
85         {
86                 .name           = "SMPS10",
87         },
88         {
89                 .name           = "LDO1",
90                 .vsel_addr      = PALMAS_LDO1_VOLTAGE,
91                 .ctrl_addr      = PALMAS_LDO1_CTRL,
92         },
93         {
94                 .name           = "LDO2",
95                 .vsel_addr      = PALMAS_LDO2_VOLTAGE,
96                 .ctrl_addr      = PALMAS_LDO2_CTRL,
97         },
98         {
99                 .name           = "LDO3",
100                 .vsel_addr      = PALMAS_LDO3_VOLTAGE,
101                 .ctrl_addr      = PALMAS_LDO3_CTRL,
102         },
103         {
104                 .name           = "LDO4",
105                 .vsel_addr      = PALMAS_LDO4_VOLTAGE,
106                 .ctrl_addr      = PALMAS_LDO4_CTRL,
107         },
108         {
109                 .name           = "LDO5",
110                 .vsel_addr      = PALMAS_LDO5_VOLTAGE,
111                 .ctrl_addr      = PALMAS_LDO5_CTRL,
112         },
113         {
114                 .name           = "LDO6",
115                 .vsel_addr      = PALMAS_LDO6_VOLTAGE,
116                 .ctrl_addr      = PALMAS_LDO6_CTRL,
117         },
118         {
119                 .name           = "LDO7",
120                 .vsel_addr      = PALMAS_LDO7_VOLTAGE,
121                 .ctrl_addr      = PALMAS_LDO7_CTRL,
122         },
123         {
124                 .name           = "LDO8",
125                 .vsel_addr      = PALMAS_LDO8_VOLTAGE,
126                 .ctrl_addr      = PALMAS_LDO8_CTRL,
127         },
128         {
129                 .name           = "LDO9",
130                 .vsel_addr      = PALMAS_LDO9_VOLTAGE,
131                 .ctrl_addr      = PALMAS_LDO9_CTRL,
132         },
133         {
134                 .name           = "LDOLN",
135                 .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
136                 .ctrl_addr      = PALMAS_LDOLN_CTRL,
137         },
138         {
139                 .name           = "LDOUSB",
140                 .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
141                 .ctrl_addr      = PALMAS_LDOUSB_CTRL,
142         },
143 };
144
145 #define SMPS_CTRL_MODE_OFF              0x00
146 #define SMPS_CTRL_MODE_ON               0x01
147 #define SMPS_CTRL_MODE_ECO              0x02
148 #define SMPS_CTRL_MODE_PWM              0x03
149
150 /* These values are derived from the data sheet. And are the number of steps
151  * where there is a voltage change, the ranges at beginning and end of register
152  * max/min values where there are no change are ommitted.
153  *
154  * So they are basically (maxV-minV)/stepV
155  */
156 #define PALMAS_SMPS_NUM_VOLTAGES        116
157 #define PALMAS_SMPS10_NUM_VOLTAGES      2
158 #define PALMAS_LDO_NUM_VOLTAGES         50
159
160 #define SMPS10_VSEL                     (1<<3)
161 #define SMPS10_BOOST_EN                 (1<<2)
162 #define SMPS10_BYPASS_EN                (1<<1)
163 #define SMPS10_SWITCH_EN                (1<<0)
164
165 #define REGULATOR_SLAVE                 0
166
167 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
168                 unsigned int *dest)
169 {
170         unsigned int addr;
171
172         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
173
174         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
175 }
176
177 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
178                 unsigned int value)
179 {
180         unsigned int addr;
181
182         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
183
184         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
185 }
186
187 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
188                 unsigned int *dest)
189 {
190         unsigned int addr;
191
192         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
193
194         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
195 }
196
197 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
198                 unsigned int value)
199 {
200         unsigned int addr;
201
202         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
203
204         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
205 }
206
207 static int palmas_is_enabled_smps(struct regulator_dev *dev)
208 {
209         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
210         int id = rdev_get_id(dev);
211         unsigned int reg;
212
213         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
214
215         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
216         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
217
218         return !!(reg);
219 }
220
221 static int palmas_enable_smps(struct regulator_dev *dev)
222 {
223         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
224         int id = rdev_get_id(dev);
225         unsigned int reg;
226
227         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
228
229         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
230         reg |= SMPS_CTRL_MODE_ON;
231
232         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
233
234         return 0;
235 }
236
237 static int palmas_disable_smps(struct regulator_dev *dev)
238 {
239         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
240         int id = rdev_get_id(dev);
241         unsigned int reg;
242
243         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
244
245         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
246
247         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
248
249         return 0;
250 }
251
252
253 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
254 {
255         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
256         int id = rdev_get_id(dev);
257         unsigned int reg;
258
259         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
260         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
261
262         switch (mode) {
263         case REGULATOR_MODE_NORMAL:
264                 reg |= SMPS_CTRL_MODE_ON;
265                 break;
266         case REGULATOR_MODE_IDLE:
267                 reg |= SMPS_CTRL_MODE_ECO;
268                 break;
269         case REGULATOR_MODE_FAST:
270                 reg |= SMPS_CTRL_MODE_PWM;
271                 break;
272         default:
273                 return -EINVAL;
274         }
275         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
276
277         return 0;
278 }
279
280 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
281 {
282         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
283         int id = rdev_get_id(dev);
284         unsigned int reg;
285
286         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
287         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
288         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
289
290         switch (reg) {
291         case SMPS_CTRL_MODE_ON:
292                 return REGULATOR_MODE_NORMAL;
293         case SMPS_CTRL_MODE_ECO:
294                 return REGULATOR_MODE_IDLE;
295         case SMPS_CTRL_MODE_PWM:
296                 return REGULATOR_MODE_FAST;
297         }
298
299         return 0;
300 }
301
302 static int palmas_list_voltage_smps(struct regulator_dev *dev,
303                                         unsigned selector)
304 {
305         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
306         int id = rdev_get_id(dev);
307         int mult = 1;
308
309         if (!selector)
310                 return 0;
311
312         /* Read the multiplier set in VSEL register to return
313          * the correct voltage.
314          */
315         if (pmic->range[id])
316                 mult = 2;
317
318         /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
319          * as defined in data sheet. RANGE is either x1 or x2
320          */
321         return  (490000 + (selector * 10000)) * mult;
322 }
323
324 static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
325 {
326         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
327         int id = rdev_get_id(dev);
328         int selector;
329         unsigned int reg;
330         unsigned int addr;
331
332         addr = palmas_regs_info[id].vsel_addr;
333
334         palmas_smps_read(pmic->palmas, addr, &reg);
335
336         selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
337
338         /* Adjust selector to match list_voltage ranges */
339         if ((selector > 0) && (selector < 6))
340                 selector = 6;
341         if (!selector)
342                 selector = 5;
343         if (selector > 121)
344                 selector = 121;
345         selector -= 5;
346
347         return selector;
348 }
349
350 static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
351                 unsigned selector)
352 {
353         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
354         int id = rdev_get_id(dev);
355         unsigned int reg = 0;
356         unsigned int addr;
357
358         addr = palmas_regs_info[id].vsel_addr;
359
360         /* Make sure we don't change the value of RANGE */
361         if (pmic->range[id])
362                 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
363
364         /* Adjust the linux selector into range used in VSEL register */
365         if (selector)
366                 reg |= selector + 5;
367
368         palmas_smps_write(pmic->palmas, addr, reg);
369
370         return 0;
371 }
372
373 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
374                 int min_uV, int max_uV)
375 {
376         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
377         int id = rdev_get_id(rdev);
378         int ret, voltage;
379
380         if (min_uV == 0)
381                 return 0;
382
383         if (pmic->range[id]) { /* RANGE is x2 */
384                 if (min_uV < 1000000)
385                         min_uV = 1000000;
386                 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 1;
387         } else {                /* RANGE is x1 */
388                 if (min_uV < 500000)
389                         min_uV = 500000;
390                 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 1;
391         }
392
393         /* Map back into a voltage to verify we're still in bounds */
394         voltage = palmas_list_voltage_smps(rdev, ret);
395         if (voltage < min_uV || voltage > max_uV)
396                 return -EINVAL;
397
398         return ret;
399 }
400
401 static struct regulator_ops palmas_ops_smps = {
402         .is_enabled             = palmas_is_enabled_smps,
403         .enable                 = palmas_enable_smps,
404         .disable                = palmas_disable_smps,
405         .set_mode               = palmas_set_mode_smps,
406         .get_mode               = palmas_get_mode_smps,
407         .get_voltage_sel        = palmas_get_voltage_smps_sel,
408         .set_voltage_sel        = palmas_set_voltage_smps_sel,
409         .list_voltage           = palmas_list_voltage_smps,
410         .map_voltage            = palmas_map_voltage_smps,
411 };
412
413 static struct regulator_ops palmas_ops_smps10 = {
414         .is_enabled             = regulator_is_enabled_regmap,
415         .enable                 = regulator_enable_regmap,
416         .disable                = regulator_disable_regmap,
417         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
418         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
419         .list_voltage           = regulator_list_voltage_linear,
420         .map_voltage            = regulator_map_voltage_linear,
421 };
422
423 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
424 {
425         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
426         int id = rdev_get_id(dev);
427         unsigned int reg;
428
429         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
430
431         reg &= PALMAS_LDO1_CTRL_STATUS;
432
433         return !!(reg);
434 }
435
436 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
437                                         unsigned selector)
438 {
439         if (!selector)
440                 return 0;
441
442         /* voltage is 0.85V + (selector * 0.05v) */
443         return  850000 + (selector * 50000);
444 }
445
446 static int palmas_map_voltage_ldo(struct regulator_dev *rdev,
447                 int min_uV, int max_uV)
448 {
449         int ret, voltage;
450
451         if (min_uV == 0)
452                 return 0;
453
454         if (min_uV < 900000)
455                 min_uV = 900000;
456         ret = DIV_ROUND_UP(min_uV - 900000, 50000) + 1;
457
458         /* Map back into a voltage to verify we're still in bounds */
459         voltage = palmas_list_voltage_ldo(rdev, ret);
460         if (voltage < min_uV || voltage > max_uV)
461                 return -EINVAL;
462
463         return ret;
464 }
465
466 static struct regulator_ops palmas_ops_ldo = {
467         .is_enabled             = palmas_is_enabled_ldo,
468         .enable                 = regulator_enable_regmap,
469         .disable                = regulator_disable_regmap,
470         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
471         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
472         .list_voltage           = palmas_list_voltage_ldo,
473         .map_voltage            = palmas_map_voltage_ldo,
474 };
475
476 /*
477  * setup the hardware based sleep configuration of the SMPS/LDO regulators
478  * from the platform data. This is different to the software based control
479  * supported by the regulator framework as it is controlled by toggling
480  * pins on the PMIC such as PREQ, SYSEN, ...
481  */
482 static int palmas_smps_init(struct palmas *palmas, int id,
483                 struct palmas_reg_init *reg_init)
484 {
485         unsigned int reg;
486         unsigned int addr;
487         int ret;
488
489         addr = palmas_regs_info[id].ctrl_addr;
490
491         ret = palmas_smps_read(palmas, addr, &reg);
492         if (ret)
493                 return ret;
494
495         switch (id) {
496         case PALMAS_REG_SMPS10:
497                 if (reg_init->mode_sleep) {
498                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
499                         reg |= reg_init->mode_sleep <<
500                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
501                 }
502                 break;
503         default:
504                 if (reg_init->warm_reset)
505                         reg |= PALMAS_SMPS12_CTRL_WR_S;
506
507                 if (reg_init->roof_floor)
508                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
509
510                 if (reg_init->mode_sleep) {
511                         reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
512                         reg |= reg_init->mode_sleep <<
513                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
514                 }
515         }
516
517         ret = palmas_smps_write(palmas, addr, reg);
518         if (ret)
519                 return ret;
520
521         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
522                 addr = palmas_regs_info[id].tstep_addr;
523
524                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
525
526                 ret = palmas_smps_write(palmas, addr, reg);
527                 if (ret)
528                         return ret;
529         }
530
531         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
532                 addr = palmas_regs_info[id].vsel_addr;
533
534                 reg = reg_init->vsel;
535
536                 ret = palmas_smps_write(palmas, addr, reg);
537                 if (ret)
538                         return ret;
539         }
540
541
542         return 0;
543 }
544
545 static int palmas_ldo_init(struct palmas *palmas, int id,
546                 struct palmas_reg_init *reg_init)
547 {
548         unsigned int reg;
549         unsigned int addr;
550         int ret;
551
552         addr = palmas_regs_info[id].ctrl_addr;
553
554         ret = palmas_ldo_read(palmas, addr, &reg);
555         if (ret)
556                 return ret;
557
558         if (reg_init->warm_reset)
559                 reg |= PALMAS_LDO1_CTRL_WR_S;
560
561         if (reg_init->mode_sleep)
562                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
563
564         ret = palmas_ldo_write(palmas, addr, reg);
565         if (ret)
566                 return ret;
567
568         return 0;
569 }
570
571 static __devinit int palmas_probe(struct platform_device *pdev)
572 {
573         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
574         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
575         struct regulator_dev *rdev;
576         struct regulator_config config = { };
577         struct palmas_pmic *pmic;
578         struct palmas_reg_init *reg_init;
579         int id = 0, ret;
580         unsigned int addr, reg;
581
582         if (!pdata)
583                 return -EINVAL;
584         if (!pdata->reg_data)
585                 return -EINVAL;
586
587         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
588         if (!pmic)
589                 return -ENOMEM;
590
591         pmic->dev = &pdev->dev;
592         pmic->palmas = palmas;
593         palmas->pmic = pmic;
594         platform_set_drvdata(pdev, pmic);
595
596         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
597         if (ret)
598                 return ret;
599
600         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
601                 pmic->smps123 = 1;
602
603         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
604                 pmic->smps457 = 1;
605
606         config.regmap = palmas->regmap[REGULATOR_SLAVE];
607         config.dev = &pdev->dev;
608         config.driver_data = pmic;
609
610         for (id = 0; id < PALMAS_REG_LDO1; id++) {
611
612                 /*
613                  * Miss out regulators which are not available due
614                  * to slaving configurations.
615                  */
616                 switch (id) {
617                 case PALMAS_REG_SMPS12:
618                 case PALMAS_REG_SMPS3:
619                         if (pmic->smps123)
620                                 continue;
621                         break;
622                 case PALMAS_REG_SMPS123:
623                         if (!pmic->smps123)
624                                 continue;
625                         break;
626                 case PALMAS_REG_SMPS45:
627                 case PALMAS_REG_SMPS7:
628                         if (pmic->smps457)
629                                 continue;
630                         break;
631                 case PALMAS_REG_SMPS457:
632                         if (!pmic->smps457)
633                                 continue;
634                 }
635
636                 /* Register the regulators */
637                 pmic->desc[id].name = palmas_regs_info[id].name;
638                 pmic->desc[id].id = id;
639
640                 switch (id) {
641                 case PALMAS_REG_SMPS10:
642                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
643                         pmic->desc[id].ops = &palmas_ops_smps10;
644                         pmic->desc[id].vsel_reg =
645                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
646                                                         PALMAS_SMPS10_CTRL);
647                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
648                         pmic->desc[id].enable_reg =
649                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
650                                                         PALMAS_SMPS10_STATUS);
651                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
652                         pmic->desc[id].min_uV = 3750000;
653                         pmic->desc[id].uV_step = 1250000;
654                         break;
655                 default:
656                         pmic->desc[id].ops = &palmas_ops_smps;
657                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
658                 }
659
660                 pmic->desc[id].type = REGULATOR_VOLTAGE;
661                 pmic->desc[id].owner = THIS_MODULE;
662
663                 /* Initialise sleep/init values from platform data */
664                 if (pdata && pdata->reg_init) {
665                         reg_init = pdata->reg_init[id];
666                         if (reg_init) {
667                                 ret = palmas_smps_init(palmas, id, reg_init);
668                                 if (ret)
669                                         goto err_unregister_regulator;
670                         }
671                 }
672
673                 /*
674                  * read and store the RANGE bit for later use
675                  * This must be done before regulator is probed otherwise
676                  * we error in probe with unsuportable ranges.
677                  */
678                 if (id != PALMAS_REG_SMPS10) {
679                         addr = palmas_regs_info[id].vsel_addr;
680
681                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
682                         if (ret)
683                                 goto err_unregister_regulator;
684                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
685                                 pmic->range[id] = 1;
686                 }
687
688                 if (pdata && pdata->reg_data)
689                         config.init_data = pdata->reg_data[id];
690                 else
691                         config.init_data = NULL;
692
693                 rdev = regulator_register(&pmic->desc[id], &config);
694                 if (IS_ERR(rdev)) {
695                         dev_err(&pdev->dev,
696                                 "failed to register %s regulator\n",
697                                 pdev->name);
698                         ret = PTR_ERR(rdev);
699                         goto err_unregister_regulator;
700                 }
701
702                 /* Save regulator for cleanup */
703                 pmic->rdev[id] = rdev;
704         }
705
706         /* Start this loop from the id left from previous loop */
707         for (; id < PALMAS_NUM_REGS; id++) {
708
709                 /* Miss out regulators which are not available due
710                  * to alternate functions.
711                  */
712
713                 /* Register the regulators */
714                 pmic->desc[id].name = palmas_regs_info[id].name;
715                 pmic->desc[id].id = id;
716                 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
717
718                 pmic->desc[id].ops = &palmas_ops_ldo;
719
720                 pmic->desc[id].type = REGULATOR_VOLTAGE;
721                 pmic->desc[id].owner = THIS_MODULE;
722                 pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
723                                                 palmas_regs_info[id].vsel_addr);
724                 pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
725                 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
726                                                 palmas_regs_info[id].ctrl_addr);
727                 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
728
729                 if (pdata && pdata->reg_data)
730                         config.init_data = pdata->reg_data[id];
731                 else
732                         config.init_data = NULL;
733
734                 rdev = regulator_register(&pmic->desc[id], &config);
735                 if (IS_ERR(rdev)) {
736                         dev_err(&pdev->dev,
737                                 "failed to register %s regulator\n",
738                                 pdev->name);
739                         ret = PTR_ERR(rdev);
740                         goto err_unregister_regulator;
741                 }
742
743                 /* Save regulator for cleanup */
744                 pmic->rdev[id] = rdev;
745
746                 /* Initialise sleep/init values from platform data */
747                 if (pdata->reg_init) {
748                         reg_init = pdata->reg_init[id];
749                         if (reg_init) {
750                                 ret = palmas_ldo_init(palmas, id, reg_init);
751                                 if (ret) {
752                                         regulator_unregister(pmic->rdev[id]);
753                                         goto err_unregister_regulator;
754                                 }
755                         }
756                 }
757         }
758
759         return 0;
760
761 err_unregister_regulator:
762         while (--id >= 0)
763                 regulator_unregister(pmic->rdev[id]);
764         return ret;
765 }
766
767 static int __devexit palmas_remove(struct platform_device *pdev)
768 {
769         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
770         int id;
771
772         for (id = 0; id < PALMAS_NUM_REGS; id++)
773                 regulator_unregister(pmic->rdev[id]);
774         return 0;
775 }
776
777 static struct platform_driver palmas_driver = {
778         .driver = {
779                 .name = "palmas-pmic",
780                 .owner = THIS_MODULE,
781         },
782         .probe = palmas_probe,
783         .remove = __devexit_p(palmas_remove),
784 };
785
786 static int __init palmas_init(void)
787 {
788         return platform_driver_register(&palmas_driver);
789 }
790 subsys_initcall(palmas_init);
791
792 static void __exit palmas_exit(void)
793 {
794         platform_driver_unregister(&palmas_driver);
795 }
796 module_exit(palmas_exit);
797
798 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
799 MODULE_DESCRIPTION("Palmas voltage regulator driver");
800 MODULE_LICENSE("GPL");
801 MODULE_ALIAS("platform:palmas-pmic");