Merge remote-tracking branch 'regulator/topic/palmas' into regulator-next
[~shefty/rdma-dev.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         /* Read the multiplier set in VSEL register to return
313          * the correct voltage.
314          */
315         if (pmic->range[id])
316                 mult = 2;
317
318         if (selector == 0)
319                 return 0;
320         else if (selector < 6)
321                 return 500000 * mult;
322         else
323                 /* Voltage is linear mapping starting from selector 6,
324                  * volt = (0.49V + ((selector - 5) * 0.01V)) * RANGE
325                  * RANGE is either x1 or x2
326                  */
327                 return (490000 + ((selector - 5) * 10000)) * mult;
328 }
329
330 static int palmas_map_voltage_smps(struct regulator_dev *rdev,
331                 int min_uV, int max_uV)
332 {
333         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
334         int id = rdev_get_id(rdev);
335         int ret, voltage;
336
337         if (min_uV == 0)
338                 return 0;
339
340         if (pmic->range[id]) { /* RANGE is x2 */
341                 if (min_uV < 1000000)
342                         min_uV = 1000000;
343                 ret = DIV_ROUND_UP(min_uV - 1000000, 20000) + 6;
344         } else {                /* RANGE is x1 */
345                 if (min_uV < 500000)
346                         min_uV = 500000;
347                 ret = DIV_ROUND_UP(min_uV - 500000, 10000) + 6;
348         }
349
350         /* Map back into a voltage to verify we're still in bounds */
351         voltage = palmas_list_voltage_smps(rdev, ret);
352         if (voltage < min_uV || voltage > max_uV)
353                 return -EINVAL;
354
355         return ret;
356 }
357
358 static struct regulator_ops palmas_ops_smps = {
359         .is_enabled             = palmas_is_enabled_smps,
360         .enable                 = palmas_enable_smps,
361         .disable                = palmas_disable_smps,
362         .set_mode               = palmas_set_mode_smps,
363         .get_mode               = palmas_get_mode_smps,
364         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
365         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
366         .list_voltage           = palmas_list_voltage_smps,
367         .map_voltage            = palmas_map_voltage_smps,
368 };
369
370 static struct regulator_ops palmas_ops_smps10 = {
371         .is_enabled             = regulator_is_enabled_regmap,
372         .enable                 = regulator_enable_regmap,
373         .disable                = regulator_disable_regmap,
374         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
375         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
376         .list_voltage           = regulator_list_voltage_linear,
377         .map_voltage            = regulator_map_voltage_linear,
378 };
379
380 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
381 {
382         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
383         int id = rdev_get_id(dev);
384         unsigned int reg;
385
386         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
387
388         reg &= PALMAS_LDO1_CTRL_STATUS;
389
390         return !!(reg);
391 }
392
393 static struct regulator_ops palmas_ops_ldo = {
394         .is_enabled             = palmas_is_enabled_ldo,
395         .enable                 = regulator_enable_regmap,
396         .disable                = regulator_disable_regmap,
397         .get_voltage_sel        = regulator_get_voltage_sel_regmap,
398         .set_voltage_sel        = regulator_set_voltage_sel_regmap,
399         .list_voltage           = regulator_list_voltage_linear,
400         .map_voltage            = regulator_map_voltage_linear,
401 };
402
403 /*
404  * setup the hardware based sleep configuration of the SMPS/LDO regulators
405  * from the platform data. This is different to the software based control
406  * supported by the regulator framework as it is controlled by toggling
407  * pins on the PMIC such as PREQ, SYSEN, ...
408  */
409 static int palmas_smps_init(struct palmas *palmas, int id,
410                 struct palmas_reg_init *reg_init)
411 {
412         unsigned int reg;
413         unsigned int addr;
414         int ret;
415
416         addr = palmas_regs_info[id].ctrl_addr;
417
418         ret = palmas_smps_read(palmas, addr, &reg);
419         if (ret)
420                 return ret;
421
422         switch (id) {
423         case PALMAS_REG_SMPS10:
424                 if (reg_init->mode_sleep) {
425                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
426                         reg |= reg_init->mode_sleep <<
427                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
428                 }
429                 break;
430         default:
431                 if (reg_init->warm_reset)
432                         reg |= PALMAS_SMPS12_CTRL_WR_S;
433
434                 if (reg_init->roof_floor)
435                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
436
437                 if (reg_init->mode_sleep) {
438                         reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
439                         reg |= reg_init->mode_sleep <<
440                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
441                 }
442         }
443
444         ret = palmas_smps_write(palmas, addr, reg);
445         if (ret)
446                 return ret;
447
448         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
449                 addr = palmas_regs_info[id].tstep_addr;
450
451                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
452
453                 ret = palmas_smps_write(palmas, addr, reg);
454                 if (ret)
455                         return ret;
456         }
457
458         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
459                 addr = palmas_regs_info[id].vsel_addr;
460
461                 reg = reg_init->vsel;
462
463                 ret = palmas_smps_write(palmas, addr, reg);
464                 if (ret)
465                         return ret;
466         }
467
468
469         return 0;
470 }
471
472 static int palmas_ldo_init(struct palmas *palmas, int id,
473                 struct palmas_reg_init *reg_init)
474 {
475         unsigned int reg;
476         unsigned int addr;
477         int ret;
478
479         addr = palmas_regs_info[id].ctrl_addr;
480
481         ret = palmas_ldo_read(palmas, addr, &reg);
482         if (ret)
483                 return ret;
484
485         if (reg_init->warm_reset)
486                 reg |= PALMAS_LDO1_CTRL_WR_S;
487
488         if (reg_init->mode_sleep)
489                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
490
491         ret = palmas_ldo_write(palmas, addr, reg);
492         if (ret)
493                 return ret;
494
495         return 0;
496 }
497
498 static struct of_regulator_match palmas_matches[] = {
499         { .name = "smps12", },
500         { .name = "smps123", },
501         { .name = "smps3", },
502         { .name = "smps45", },
503         { .name = "smps457", },
504         { .name = "smps6", },
505         { .name = "smps7", },
506         { .name = "smps8", },
507         { .name = "smps9", },
508         { .name = "smps10", },
509         { .name = "ldo1", },
510         { .name = "ldo2", },
511         { .name = "ldo3", },
512         { .name = "ldo4", },
513         { .name = "ldo5", },
514         { .name = "ldo6", },
515         { .name = "ldo7", },
516         { .name = "ldo8", },
517         { .name = "ldo9", },
518         { .name = "ldoln", },
519         { .name = "ldousb", },
520 };
521
522 static void palmas_dt_to_pdata(struct device *dev,
523                 struct device_node *node,
524                 struct palmas_pmic_platform_data *pdata)
525 {
526         struct device_node *regulators;
527         u32 prop;
528         int idx, ret;
529
530         node = of_node_get(node);
531         regulators = of_find_node_by_name(node, "regulators");
532         if (!regulators) {
533                 dev_info(dev, "regulator node not found\n");
534                 return;
535         }
536
537         ret = of_regulator_match(dev, regulators, palmas_matches,
538                         PALMAS_NUM_REGS);
539         of_node_put(regulators);
540         if (ret < 0) {
541                 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
542                 return;
543         }
544
545         for (idx = 0; idx < PALMAS_NUM_REGS; idx++) {
546                 if (!palmas_matches[idx].init_data ||
547                                 !palmas_matches[idx].of_node)
548                         continue;
549
550                 pdata->reg_data[idx] = palmas_matches[idx].init_data;
551
552                 pdata->reg_init[idx] = devm_kzalloc(dev,
553                                 sizeof(struct palmas_reg_init), GFP_KERNEL);
554
555                 ret = of_property_read_u32(palmas_matches[idx].of_node,
556                                 "ti,warm_reset", &prop);
557                 if (!ret)
558                         pdata->reg_init[idx]->warm_reset = prop;
559
560                 ret = of_property_read_u32(palmas_matches[idx].of_node,
561                                 "ti,roof_floor", &prop);
562                 if (!ret)
563                         pdata->reg_init[idx]->roof_floor = prop;
564
565                 ret = of_property_read_u32(palmas_matches[idx].of_node,
566                                 "ti,mode_sleep", &prop);
567                 if (!ret)
568                         pdata->reg_init[idx]->mode_sleep = prop;
569
570                 ret = of_property_read_u32(palmas_matches[idx].of_node,
571                                 "ti,tstep", &prop);
572                 if (!ret)
573                         pdata->reg_init[idx]->tstep = prop;
574
575                 ret = of_property_read_u32(palmas_matches[idx].of_node,
576                                 "ti,vsel", &prop);
577                 if (!ret)
578                         pdata->reg_init[idx]->vsel = prop;
579         }
580
581         ret = of_property_read_u32(node, "ti,ldo6_vibrator", &prop);
582         if (!ret)
583                 pdata->ldo6_vibrator = prop;
584 }
585
586
587 static int palmas_probe(struct platform_device *pdev)
588 {
589         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
590         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
591         struct device_node *node = pdev->dev.of_node;
592         struct regulator_dev *rdev;
593         struct regulator_config config = { };
594         struct palmas_pmic *pmic;
595         struct palmas_reg_init *reg_init;
596         int id = 0, ret;
597         unsigned int addr, reg;
598
599         if (node && !pdata) {
600                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
601
602                 if (!pdata)
603                         return -ENOMEM;
604
605                 palmas_dt_to_pdata(&pdev->dev, node, pdata);
606         }
607
608         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
609         if (!pmic)
610                 return -ENOMEM;
611
612         pmic->dev = &pdev->dev;
613         pmic->palmas = palmas;
614         palmas->pmic = pmic;
615         platform_set_drvdata(pdev, pmic);
616
617         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
618         if (ret)
619                 return ret;
620
621         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
622                 pmic->smps123 = 1;
623
624         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
625                 pmic->smps457 = 1;
626
627         config.regmap = palmas->regmap[REGULATOR_SLAVE];
628         config.dev = &pdev->dev;
629         config.driver_data = pmic;
630
631         for (id = 0; id < PALMAS_REG_LDO1; id++) {
632
633                 /*
634                  * Miss out regulators which are not available due
635                  * to slaving configurations.
636                  */
637                 switch (id) {
638                 case PALMAS_REG_SMPS12:
639                 case PALMAS_REG_SMPS3:
640                         if (pmic->smps123)
641                                 continue;
642                         break;
643                 case PALMAS_REG_SMPS123:
644                         if (!pmic->smps123)
645                                 continue;
646                         break;
647                 case PALMAS_REG_SMPS45:
648                 case PALMAS_REG_SMPS7:
649                         if (pmic->smps457)
650                                 continue;
651                         break;
652                 case PALMAS_REG_SMPS457:
653                         if (!pmic->smps457)
654                                 continue;
655                 }
656
657                 /* Initialise sleep/init values from platform data */
658                 if (pdata && pdata->reg_init[id]) {
659                         reg_init = pdata->reg_init[id];
660                         ret = palmas_smps_init(palmas, id, reg_init);
661                         if (ret)
662                                 goto err_unregister_regulator;
663                 }
664
665                 /* Register the regulators */
666                 pmic->desc[id].name = palmas_regs_info[id].name;
667                 pmic->desc[id].id = id;
668
669                 switch (id) {
670                 case PALMAS_REG_SMPS10:
671                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
672                         pmic->desc[id].ops = &palmas_ops_smps10;
673                         pmic->desc[id].vsel_reg =
674                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
675                                                         PALMAS_SMPS10_CTRL);
676                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
677                         pmic->desc[id].enable_reg =
678                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
679                                                         PALMAS_SMPS10_STATUS);
680                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
681                         pmic->desc[id].min_uV = 3750000;
682                         pmic->desc[id].uV_step = 1250000;
683                         break;
684                 default:
685                         /*
686                          * Read and store the RANGE bit for later use
687                          * This must be done before regulator is probed,
688                          * otherwise we error in probe with unsupportable ranges.
689                          */
690                         addr = palmas_regs_info[id].vsel_addr;
691
692                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
693                         if (ret)
694                                 goto err_unregister_regulator;
695                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
696                                 pmic->range[id] = 1;
697
698                         pmic->desc[id].ops = &palmas_ops_smps;
699                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
700                         pmic->desc[id].vsel_reg =
701                                         PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE,
702                                                 palmas_regs_info[id].vsel_addr);
703                         pmic->desc[id].vsel_mask =
704                                         PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
705                 }
706
707                 pmic->desc[id].type = REGULATOR_VOLTAGE;
708                 pmic->desc[id].owner = THIS_MODULE;
709
710                 if (pdata)
711                         config.init_data = pdata->reg_data[id];
712                 else
713                         config.init_data = NULL;
714
715                 config.of_node = palmas_matches[id].of_node;
716
717                 rdev = regulator_register(&pmic->desc[id], &config);
718                 if (IS_ERR(rdev)) {
719                         dev_err(&pdev->dev,
720                                 "failed to register %s regulator\n",
721                                 pdev->name);
722                         ret = PTR_ERR(rdev);
723                         goto err_unregister_regulator;
724                 }
725
726                 /* Save regulator for cleanup */
727                 pmic->rdev[id] = rdev;
728         }
729
730         /* Start this loop from the id left from previous loop */
731         for (; id < PALMAS_NUM_REGS; id++) {
732
733                 /* Miss out regulators which are not available due
734                  * to alternate functions.
735                  */
736
737                 /* Register the regulators */
738                 pmic->desc[id].name = palmas_regs_info[id].name;
739                 pmic->desc[id].id = id;
740                 pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
741
742                 pmic->desc[id].ops = &palmas_ops_ldo;
743
744                 pmic->desc[id].type = REGULATOR_VOLTAGE;
745                 pmic->desc[id].owner = THIS_MODULE;
746                 pmic->desc[id].min_uV = 900000;
747                 pmic->desc[id].uV_step = 50000;
748                 pmic->desc[id].linear_min_sel = 1;
749                 pmic->desc[id].vsel_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
750                                                 palmas_regs_info[id].vsel_addr);
751                 pmic->desc[id].vsel_mask = PALMAS_LDO1_VOLTAGE_VSEL_MASK;
752                 pmic->desc[id].enable_reg = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE,
753                                                 palmas_regs_info[id].ctrl_addr);
754                 pmic->desc[id].enable_mask = PALMAS_LDO1_CTRL_MODE_ACTIVE;
755
756                 if (pdata)
757                         config.init_data = pdata->reg_data[id];
758                 else
759                         config.init_data = NULL;
760
761                 config.of_node = palmas_matches[id].of_node;
762
763                 rdev = regulator_register(&pmic->desc[id], &config);
764                 if (IS_ERR(rdev)) {
765                         dev_err(&pdev->dev,
766                                 "failed to register %s regulator\n",
767                                 pdev->name);
768                         ret = PTR_ERR(rdev);
769                         goto err_unregister_regulator;
770                 }
771
772                 /* Save regulator for cleanup */
773                 pmic->rdev[id] = rdev;
774
775                 /* Initialise sleep/init values from platform data */
776                 if (pdata) {
777                         reg_init = pdata->reg_init[id];
778                         if (reg_init) {
779                                 ret = palmas_ldo_init(palmas, id, reg_init);
780                                 if (ret) {
781                                         regulator_unregister(pmic->rdev[id]);
782                                         goto err_unregister_regulator;
783                                 }
784                         }
785                 }
786         }
787
788         return 0;
789
790 err_unregister_regulator:
791         while (--id >= 0)
792                 regulator_unregister(pmic->rdev[id]);
793         return ret;
794 }
795
796 static int palmas_remove(struct platform_device *pdev)
797 {
798         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
799         int id;
800
801         for (id = 0; id < PALMAS_NUM_REGS; id++)
802                 regulator_unregister(pmic->rdev[id]);
803         return 0;
804 }
805
806 static struct of_device_id of_palmas_match_tbl[] = {
807         { .compatible = "ti,palmas-pmic", },
808         { /* end */ }
809 };
810
811 static struct platform_driver palmas_driver = {
812         .driver = {
813                 .name = "palmas-pmic",
814                 .of_match_table = of_palmas_match_tbl,
815                 .owner = THIS_MODULE,
816         },
817         .probe = palmas_probe,
818         .remove = palmas_remove,
819 };
820
821 static int __init palmas_init(void)
822 {
823         return platform_driver_register(&palmas_driver);
824 }
825 subsys_initcall(palmas_init);
826
827 static void __exit palmas_exit(void)
828 {
829         platform_driver_unregister(&palmas_driver);
830 }
831 module_exit(palmas_exit);
832
833 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
834 MODULE_DESCRIPTION("Palmas voltage regulator driver");
835 MODULE_LICENSE("GPL");
836 MODULE_ALIAS("platform:palmas-pmic");
837 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);