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