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