regulator: palmas: implement errata for ES1.0,ES2.0 and ES2.1
[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 #define PALMA_SMPS10_VSEL       BIT(3)
27 #define PALMA_SMPS10_BOOST_EN   BIT(2)
28 #define PALMA_SMPS10_BYPASS_EN  BIT(1)
29 #define PALMA_SMPS10_SWITCH_EN  BIT(0)
30
31 struct regs_info {
32         char    *name;
33         u8      vsel_addr;
34         u8      ctrl_addr;
35         u8      tstep_addr;
36         u8      fvsel_addr;
37         int     sleep_id;
38 };
39
40 static const struct regs_info palmas_regs_info[] = {
41         {
42                 .name           = "SMPS12",
43                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
44                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
45                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
46                 .fvsel_addr     = PALMAS_SMPS12_FORCE,
47                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
48         },
49         {
50                 .name           = "SMPS123",
51                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
52                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
53                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
54                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
55         },
56         {
57                 .name           = "SMPS3",
58                 .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
59                 .ctrl_addr      = PALMAS_SMPS3_CTRL,
60                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS3,
61         },
62         {
63                 .name           = "SMPS45",
64                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
65                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
66                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
67                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
68         },
69         {
70                 .name           = "SMPS457",
71                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
72                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
73                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
74                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
75         },
76         {
77                 .name           = "SMPS6",
78                 .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
79                 .ctrl_addr      = PALMAS_SMPS6_CTRL,
80                 .tstep_addr     = PALMAS_SMPS6_TSTEP,
81                 .fvsel_addr     = PALMAS_SMPS6_FORCE,
82                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS6,
83         },
84         {
85                 .name           = "SMPS7",
86                 .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
87                 .ctrl_addr      = PALMAS_SMPS7_CTRL,
88                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS7,
89         },
90         {
91                 .name           = "SMPS8",
92                 .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
93                 .ctrl_addr      = PALMAS_SMPS8_CTRL,
94                 .tstep_addr     = PALMAS_SMPS8_TSTEP,
95                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS8,
96         },
97         {
98                 .name           = "SMPS9",
99                 .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
100                 .ctrl_addr      = PALMAS_SMPS9_CTRL,
101                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS9,
102         },
103         {
104                 .name           = "SMPS10",
105                 .ctrl_addr      = PALMAS_SMPS10_CTRL,
106                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS10,
107         },
108         {
109                 .name           = "LDO1",
110                 .vsel_addr      = PALMAS_LDO1_VOLTAGE,
111                 .ctrl_addr      = PALMAS_LDO1_CTRL,
112                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO1,
113         },
114         {
115                 .name           = "LDO2",
116                 .vsel_addr      = PALMAS_LDO2_VOLTAGE,
117                 .ctrl_addr      = PALMAS_LDO2_CTRL,
118                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO2,
119         },
120         {
121                 .name           = "LDO3",
122                 .vsel_addr      = PALMAS_LDO3_VOLTAGE,
123                 .ctrl_addr      = PALMAS_LDO3_CTRL,
124                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO3,
125         },
126         {
127                 .name           = "LDO4",
128                 .vsel_addr      = PALMAS_LDO4_VOLTAGE,
129                 .ctrl_addr      = PALMAS_LDO4_CTRL,
130                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO4,
131         },
132         {
133                 .name           = "LDO5",
134                 .vsel_addr      = PALMAS_LDO5_VOLTAGE,
135                 .ctrl_addr      = PALMAS_LDO5_CTRL,
136                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO5,
137         },
138         {
139                 .name           = "LDO6",
140                 .vsel_addr      = PALMAS_LDO6_VOLTAGE,
141                 .ctrl_addr      = PALMAS_LDO6_CTRL,
142                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO6,
143         },
144         {
145                 .name           = "LDO7",
146                 .vsel_addr      = PALMAS_LDO7_VOLTAGE,
147                 .ctrl_addr      = PALMAS_LDO7_CTRL,
148                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO7,
149         },
150         {
151                 .name           = "LDO8",
152                 .vsel_addr      = PALMAS_LDO8_VOLTAGE,
153                 .ctrl_addr      = PALMAS_LDO8_CTRL,
154                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO8,
155         },
156         {
157                 .name           = "LDO9",
158                 .vsel_addr      = PALMAS_LDO9_VOLTAGE,
159                 .ctrl_addr      = PALMAS_LDO9_CTRL,
160                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO9,
161         },
162         {
163                 .name           = "LDOLN",
164                 .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
165                 .ctrl_addr      = PALMAS_LDOLN_CTRL,
166                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOLN,
167         },
168         {
169                 .name           = "LDOUSB",
170                 .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
171                 .ctrl_addr      = PALMAS_LDOUSB_CTRL,
172                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOUSB,
173         },
174         {
175                 .name           = "REGEN1",
176                 .ctrl_addr      = PALMAS_REGEN1_CTRL,
177                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN1,
178         },
179         {
180                 .name           = "REGEN2",
181                 .ctrl_addr      = PALMAS_REGEN2_CTRL,
182                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN2,
183         },
184         {
185                 .name           = "REGEN3",
186                 .ctrl_addr      = PALMAS_REGEN3_CTRL,
187                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN3,
188         },
189         {
190                 .name           = "SYSEN1",
191                 .ctrl_addr      = PALMAS_SYSEN1_CTRL,
192                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN1,
193         },
194         {
195                 .name           = "SYSEN2",
196                 .ctrl_addr      = PALMAS_SYSEN2_CTRL,
197                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN2,
198         },
199 };
200
201 static unsigned int palmas_smps_ramp_delay[4] = {0, 10000, 5000, 2500};
202
203 #define SMPS_CTRL_MODE_OFF              0x00
204 #define SMPS_CTRL_MODE_ON               0x01
205 #define SMPS_CTRL_MODE_ECO              0x02
206 #define SMPS_CTRL_MODE_PWM              0x03
207
208 /* These values are derived from the data sheet. And are the number of steps
209  * where there is a voltage change, the ranges at beginning and end of register
210  * max/min values where there are no change are ommitted.
211  *
212  * So they are basically (maxV-minV)/stepV
213  */
214 #define PALMAS_SMPS_NUM_VOLTAGES        116
215 #define PALMAS_SMPS10_NUM_VOLTAGES      2
216 #define PALMAS_LDO_NUM_VOLTAGES         50
217
218 #define SMPS10_VSEL                     (1<<3)
219 #define SMPS10_BOOST_EN                 (1<<2)
220 #define SMPS10_BYPASS_EN                (1<<1)
221 #define SMPS10_SWITCH_EN                (1<<0)
222
223 #define REGULATOR_SLAVE                 0
224
225 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
226                 unsigned int *dest)
227 {
228         unsigned int addr;
229
230         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
231
232         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
233 }
234
235 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
236                 unsigned int value)
237 {
238         unsigned int addr;
239
240         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
241
242         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
243 }
244
245 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
246                 unsigned int *dest)
247 {
248         unsigned int addr;
249
250         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
251
252         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
253 }
254
255 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
256                 unsigned int value)
257 {
258         unsigned int addr;
259
260         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
261
262         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
263 }
264
265 static int palmas_resource_read(struct palmas *palmas, unsigned int reg,
266                 unsigned int *dest)
267 {
268         unsigned int addr;
269
270         addr = PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE, reg);
271
272         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
273 }
274
275 static int palmas_resource_write(struct palmas *palmas, unsigned int reg,
276                 unsigned int value)
277 {
278         unsigned int addr;
279
280         addr = PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE, reg);
281
282         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
283 }
284
285 static int palmas_is_enabled_smps(struct regulator_dev *dev)
286 {
287         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
288         int id = rdev_get_id(dev);
289         unsigned int reg;
290
291         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
292
293         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
294         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
295
296         return !!(reg);
297 }
298
299 static int palmas_enable_smps(struct regulator_dev *dev)
300 {
301         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
302         int id = rdev_get_id(dev);
303         unsigned int reg;
304
305         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
306
307         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
308         reg |= SMPS_CTRL_MODE_ON;
309
310         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
311
312         return 0;
313 }
314
315 static int palmas_disable_smps(struct regulator_dev *dev)
316 {
317         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
318         int id = rdev_get_id(dev);
319         unsigned int reg;
320
321         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
322
323         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
324
325         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
326
327         return 0;
328 }
329
330
331 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
332 {
333         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
334         int id = rdev_get_id(dev);
335         unsigned int reg;
336
337         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
338         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
339
340         switch (mode) {
341         case REGULATOR_MODE_NORMAL:
342                 reg |= SMPS_CTRL_MODE_ON;
343                 break;
344         case REGULATOR_MODE_IDLE:
345                 reg |= SMPS_CTRL_MODE_ECO;
346                 break;
347         case REGULATOR_MODE_FAST:
348                 reg |= SMPS_CTRL_MODE_PWM;
349                 break;
350         default:
351                 return -EINVAL;
352         }
353         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
354
355         return 0;
356 }
357
358 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
359 {
360         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
361         int id = rdev_get_id(dev);
362         unsigned int reg;
363
364         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
365         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
366         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
367
368         switch (reg) {
369         case SMPS_CTRL_MODE_ON:
370                 return REGULATOR_MODE_NORMAL;
371         case SMPS_CTRL_MODE_ECO:
372                 return REGULATOR_MODE_IDLE;
373         case SMPS_CTRL_MODE_PWM:
374                 return REGULATOR_MODE_FAST;
375         }
376
377         return 0;
378 }
379
380 static int palmas_list_voltage_smps(struct regulator_dev *dev,
381                                         unsigned selector)
382 {
383         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
384         int id = rdev_get_id(dev);
385         int mult = 1;
386
387         if (!selector)
388                 return 0;
389
390         /* Read the multiplier set in VSEL register to return
391          * the correct voltage.
392          */
393         if (pmic->range[id])
394                 mult = 2;
395
396         /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
397          * as defined in data sheet. RANGE is either x1 or x2
398          */
399         return  (490000 + (selector * 10000)) * mult;
400 }
401
402 static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
403 {
404         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
405         int id = rdev_get_id(dev);
406         int selector;
407         unsigned int reg;
408         unsigned int addr;
409
410         addr = palmas_regs_info[id].vsel_addr;
411
412         palmas_smps_read(pmic->palmas, addr, &reg);
413
414         selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
415
416         /* Adjust selector to match list_voltage ranges */
417         if ((selector > 0) && (selector < 6))
418                 selector = 6;
419         if (!selector)
420                 selector = 5;
421         if (selector > 121)
422                 selector = 121;
423         selector -= 5;
424
425         return selector;
426 }
427
428 static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
429                 unsigned selector)
430 {
431         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
432         int id = rdev_get_id(dev);
433         unsigned int reg = 0;
434         unsigned int addr;
435
436         addr = palmas_regs_info[id].vsel_addr;
437
438         /* Make sure we don't change the value of RANGE */
439         if (pmic->range[id])
440                 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
441
442         /* Adjust the linux selector into range used in VSEL register */
443         if (selector)
444                 reg |= selector + 5;
445
446         palmas_smps_write(pmic->palmas, addr, reg);
447
448         return 0;
449 }
450
451 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev *rdev,
452         unsigned int old_selector, unsigned int new_selector)
453 {
454         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
455         int id = rdev_get_id(rdev);
456         int old_uV, new_uV;
457         unsigned int ramp_delay = pmic->ramp_delay[id];
458
459         /* ES2.1, have the 1.5X slower slew rate than configured */
460         if (palmas_is_es_version_or_less(pmic->palmas, 2, 1))
461                 ramp_delay = (ramp_delay * 15)/10;
462
463         if (!ramp_delay)
464                 return 0;
465
466         old_uV = palmas_list_voltage_smps(rdev, old_selector);
467         if (old_uV < 0)
468                 return old_uV;
469
470         new_uV = palmas_list_voltage_smps(rdev, new_selector);
471         if (new_uV < 0)
472                 return new_uV;
473
474         return DIV_ROUND_UP(abs(old_uV - new_uV), ramp_delay);
475 }
476
477 static int palmas_smps_set_ramp_delay(struct regulator_dev *rdev,
478                 int ramp_delay)
479 {
480         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
481         int id = rdev_get_id(rdev);
482         unsigned int reg = 0;
483         unsigned int addr = palmas_regs_info[id].tstep_addr;
484         int ret;
485
486         if (ramp_delay <= 0)
487                 reg = 0;
488         else if (ramp_delay < 2500)
489                 reg = 3;
490         else if (ramp_delay < 5000)
491                 reg = 2;
492         else
493                 reg = 1;
494
495         ret = palmas_smps_write(pmic->palmas, addr, reg);
496         if (ret < 0) {
497                 dev_err(pmic->palmas->dev, "TSTEP write failed: %d\n", ret);
498                 return ret;
499         }
500
501         pmic->ramp_delay[id] = palmas_smps_ramp_delay[reg];
502         return ret;
503 }
504
505 static struct regulator_ops palmas_ops_smps = {
506         .is_enabled             = palmas_is_enabled_smps,
507         .enable                 = palmas_enable_smps,
508         .disable                = palmas_disable_smps,
509         .set_mode               = palmas_set_mode_smps,
510         .get_mode               = palmas_get_mode_smps,
511         .get_voltage_sel        = palmas_get_voltage_smps_sel,
512         .set_voltage_sel        = palmas_set_voltage_smps_sel,
513         .list_voltage           = palmas_list_voltage_smps,
514         .set_voltage_time_sel   = palma_smps_set_voltage_smps_time_sel,
515         .set_ramp_delay         = palmas_smps_set_ramp_delay,
516 };
517
518 static int palmas_is_enabled_smps10(struct regulator_dev *dev)
519 {
520         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
521         unsigned int reg;
522         int ret;
523
524         ret = palmas_smps_read(pmic->palmas, PALMAS_SMPS10_STATUS, &reg);
525         if (ret < 0) {
526                 dev_err(pmic->palmas->dev,
527                         "Error in reading smps10 status reg\n");
528                 return ret;
529         }
530
531         if (reg & PALMA_SMPS10_SWITCH_EN)
532                 return 1;
533
534         return 0;
535 }
536
537 static int palmas_enable_smps10(struct regulator_dev *dev)
538 {
539         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
540         int id = rdev_get_id(dev);
541         unsigned int reg;
542         int ret;
543
544         ret = palmas_smps_read(pmic->palmas,
545                                 palmas_regs_info[id].ctrl_addr, &reg);
546         if (ret < 0) {
547                 dev_err(pmic->palmas->dev,
548                         "Error in reading smps10 control reg\n");
549                 return ret;
550         }
551
552         reg |= PALMA_SMPS10_SWITCH_EN;
553         ret = palmas_smps_write(pmic->palmas,
554                                 palmas_regs_info[id].ctrl_addr, reg);
555         if (ret < 0)
556                 dev_err(pmic->palmas->dev,
557                         "Error in writing smps10 control reg\n");
558         return ret;
559 }
560
561 static int palmas_disable_smps10(struct regulator_dev *dev)
562 {
563         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
564         int id = rdev_get_id(dev);
565         unsigned int reg;
566         int ret;
567
568         ret = palmas_smps_read(pmic->palmas,
569                                 palmas_regs_info[id].ctrl_addr, &reg);
570         if (ret < 0) {
571                 dev_err(pmic->palmas->dev,
572                         "Error in reading smps10 control reg\n");
573                 return ret;
574         }
575
576         reg &= ~PALMA_SMPS10_SWITCH_EN;
577         ret = palmas_smps_write(pmic->palmas,
578                                 palmas_regs_info[id].ctrl_addr, reg);
579         if (ret < 0)
580                 dev_err(pmic->palmas->dev,
581                         "Error in writing smps10 control reg\n");
582         return ret;
583 }
584
585 static int palmas_get_voltage_smps10_sel(struct regulator_dev *dev)
586 {
587         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
588         int id = rdev_get_id(dev);
589         unsigned int reg;
590         int ret;
591
592         ret = palmas_smps_read(pmic->palmas,
593                                 palmas_regs_info[id].ctrl_addr, &reg);
594         if (ret < 0) {
595                 dev_err(pmic->palmas->dev,
596                         "Error in reading smps10 control reg\n");
597                 return ret;
598         }
599
600         if (reg & PALMA_SMPS10_VSEL)
601                 return 1;
602
603         return 0;
604 }
605
606 static int palmas_set_voltage_smps10_sel(struct regulator_dev *dev,
607                 unsigned selector)
608 {
609         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
610         int id = rdev_get_id(dev);
611         unsigned int reg;
612         int ret;
613
614         ret = palmas_smps_read(pmic->palmas,
615                                 palmas_regs_info[id].ctrl_addr, &reg);
616         if (ret < 0) {
617                 dev_err(pmic->palmas->dev,
618                         "Error in reading smps10 control reg\n");
619                 return ret;
620         }
621
622         if (selector)
623                 reg |= PALMA_SMPS10_VSEL;
624         else
625                 reg &= ~PALMA_SMPS10_VSEL;
626
627         /* Enable boost mode */
628         reg |= PALMA_SMPS10_BOOST_EN;
629
630         ret = palmas_smps_write(pmic->palmas,
631                                 palmas_regs_info[id].ctrl_addr, reg);
632         if (ret < 0)
633                 dev_err(pmic->palmas->dev,
634                         "Error in writing smps10 control reg\n");
635         return ret;
636 }
637
638 static int palmas_list_voltage_smps10(struct regulator_dev *dev,
639                                         unsigned selector)
640 {
641         return 3750000 + (selector * 1250000);
642 }
643
644 static struct regulator_ops palmas_ops_smps10 = {
645         .is_enabled             = palmas_is_enabled_smps10,
646         .enable                 = palmas_enable_smps10,
647         .disable                = palmas_disable_smps10,
648         .get_voltage_sel        = palmas_get_voltage_smps10_sel,
649         .set_voltage_sel        = palmas_set_voltage_smps10_sel,
650         .list_voltage           = palmas_list_voltage_smps10,
651 };
652
653 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
654 {
655         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
656         int id = rdev_get_id(dev);
657         unsigned int reg;
658
659         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
660
661         reg &= PALMAS_LDO1_CTRL_STATUS;
662
663         return !!(reg);
664 }
665
666 static int palmas_enable_ldo(struct regulator_dev *dev)
667 {
668         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
669         int id = rdev_get_id(dev);
670         unsigned int reg;
671
672         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
673
674         reg |= SMPS_CTRL_MODE_ON;
675
676         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
677
678         return 0;
679 }
680
681 static int palmas_disable_ldo(struct regulator_dev *dev)
682 {
683         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
684         int id = rdev_get_id(dev);
685         unsigned int reg;
686
687         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
688
689         reg &= ~SMPS_CTRL_MODE_ON;
690
691         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
692
693         return 0;
694 }
695
696 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
697                                         unsigned selector)
698 {
699         if (!selector)
700                 return 0;
701
702         /* voltage is 0.85V + (selector * 0.05v) */
703         return  850000 + (selector * 50000);
704 }
705
706 static int palmas_get_voltage_ldo_sel(struct regulator_dev *dev)
707 {
708         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
709         int id = rdev_get_id(dev);
710         int selector;
711         unsigned int reg;
712         unsigned int addr;
713
714         addr = palmas_regs_info[id].vsel_addr;
715
716         palmas_ldo_read(pmic->palmas, addr, &reg);
717
718         selector = reg & PALMAS_LDO1_VOLTAGE_VSEL_MASK;
719
720         /* Adjust selector to match list_voltage ranges */
721         if (selector > 49)
722                 selector = 49;
723
724         return selector;
725 }
726
727 static int palmas_set_voltage_ldo_sel(struct regulator_dev *dev,
728                 unsigned selector)
729 {
730         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
731         int id = rdev_get_id(dev);
732         unsigned int reg = 0;
733         unsigned int addr;
734
735         addr = palmas_regs_info[id].vsel_addr;
736
737         reg = selector;
738
739         palmas_ldo_write(pmic->palmas, addr, reg);
740
741         return 0;
742 }
743
744 static struct regulator_ops palmas_ops_ldo = {
745         .is_enabled             = palmas_is_enabled_ldo,
746         .enable                 = palmas_enable_ldo,
747         .disable                = palmas_disable_ldo,
748         .get_voltage_sel        = palmas_get_voltage_ldo_sel,
749         .set_voltage_sel        = palmas_set_voltage_ldo_sel,
750         .list_voltage           = palmas_list_voltage_ldo,
751 };
752
753 static int palmas_is_enabled_extreg(struct regulator_dev *dev)
754 {
755         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
756         int id = rdev_get_id(dev);
757         unsigned int reg;
758         int ret;
759
760         ret = palmas_resource_read(pmic->palmas,
761                         palmas_regs_info[id].ctrl_addr, &reg);
762         reg &= PALMAS_REGEN1_CTRL_STATUS;
763         if (ret < 0)
764                 return ret;
765
766         return !!(reg);
767 }
768
769 static int palmas_enable_extreg(struct regulator_dev *dev)
770 {
771         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
772         int id = rdev_get_id(dev);
773         unsigned int reg;
774         int ret;
775
776         ret = palmas_resource_read(pmic->palmas,
777                         palmas_regs_info[id].ctrl_addr, &reg);
778         if (ret < 0)
779                 return ret;
780
781         reg |= PALMAS_REGEN1_CTRL_MODE_ACTIVE;
782         ret = palmas_resource_write(pmic->palmas,
783                         palmas_regs_info[id].ctrl_addr, reg);
784         return ret;
785 }
786
787 static int palmas_disable_extreg(struct regulator_dev *dev)
788 {
789         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
790         int id = rdev_get_id(dev);
791         unsigned int reg;
792         int ret;
793
794         ret = palmas_resource_read(pmic->palmas,
795                         palmas_regs_info[id].ctrl_addr, &reg);
796         if (ret < 0)
797                 return ret;
798
799         reg &= ~PALMAS_REGEN1_CTRL_MODE_ACTIVE;
800         ret = palmas_resource_write(pmic->palmas,
801                         palmas_regs_info[id].ctrl_addr, reg);
802         return ret;
803 }
804
805 static int palmas_getvoltage_extreg(struct regulator_dev *rdev)
806 {
807         return 4300 * 1000;
808 }
809
810
811 static struct regulator_ops palmas_ops_extreg = {
812         .is_enabled             = palmas_is_enabled_extreg,
813         .enable                 = palmas_enable_extreg,
814         .disable                = palmas_disable_extreg,
815         .get_voltage            = palmas_getvoltage_extreg,
816 };
817
818 /*
819  * setup the hardware based sleep configuration of the SMPS/LDO regulators
820  * from the platform data. This is different to the software based control
821  * supported by the regulator framework as it is controlled by toggling
822  * pins on the PMIC such as PREQ, SYSEN, ...
823  */
824 static int palmas_smps_init(struct palmas *palmas, int id,
825                 struct palmas_reg_init *reg_init)
826 {
827         unsigned int reg;
828         unsigned int addr;
829         int ret;
830
831         addr = palmas_regs_info[id].ctrl_addr;
832
833         ret = palmas_smps_read(palmas, addr, &reg);
834         if (ret)
835                 return ret;
836
837         if (id != PALMAS_REG_SMPS10) {
838                 if (reg_init->warm_reset)
839                         reg |= PALMAS_SMPS12_CTRL_WR_S;
840
841                 if (reg_init->roof_floor)
842                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
843
844                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
845                 if (reg_init->mode_sleep)
846                         reg |= reg_init->mode_sleep <<
847                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
848         } else {
849                 if (reg_init->mode_sleep) {
850                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
851                         reg |= reg_init->mode_sleep <<
852                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
853                 }
854
855         }
856         ret = palmas_smps_write(palmas, addr, reg);
857         if (ret)
858                 return ret;
859
860         if ((id != PALMAS_REG_SMPS10) && reg_init->roof_floor) {
861                 int sleep_id = palmas_regs_info[id].sleep_id;
862                 ret = palmas_ext_power_req_config(palmas, sleep_id,
863                                         reg_init->roof_floor, true);
864                 if (ret < 0) {
865                         dev_err(palmas->dev,
866                                 "Error in configuring external control\n");
867                         return ret;
868                 }
869
870                 if (id == PALMAS_REG_SMPS123) {
871                         ret = palmas_ext_power_req_config(palmas,
872                                         PALMAS_SLEEP_REQSTR_ID_SMPS3,
873                                         reg_init->roof_floor, true);
874                         if (ret < 0) {
875                                 dev_err(palmas->dev,
876                                         "Error in configuring ext control\n");
877                                 return ret;
878                         }
879                 }
880         }
881
882         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
883                 addr = palmas_regs_info[id].tstep_addr;
884
885                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
886
887                 ret = palmas_smps_write(palmas, addr, reg);
888                 if (ret)
889                         return ret;
890         }
891
892         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
893                 addr = palmas_regs_info[id].vsel_addr;
894
895                 reg = reg_init->vsel;
896
897                 ret = palmas_smps_write(palmas, addr, reg);
898                 if (ret)
899                         return ret;
900         }
901
902
903         return 0;
904 }
905
906 static int palmas_ldo_init(struct palmas *palmas, int id,
907                 struct palmas_reg_init *reg_init)
908 {
909         unsigned int reg;
910         unsigned int addr;
911         int ret;
912
913         addr = palmas_regs_info[id].ctrl_addr;
914
915         ret = palmas_ldo_read(palmas, addr, &reg);
916         if (ret)
917                 return ret;
918
919         if (reg_init->warm_reset)
920                 reg |= PALMAS_LDO1_CTRL_WR_S;
921
922         if (reg_init->mode_sleep)
923                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
924         else
925                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
926
927         ret = palmas_ldo_write(palmas, addr, reg);
928         if (ret)
929                 return ret;
930
931         if (reg_init->roof_floor) {
932                 int sleep_id = palmas_regs_info[id].sleep_id;
933
934                 ret = palmas_ext_power_req_config(palmas, sleep_id,
935                         reg_init->roof_floor, true);
936                 if (ret < 0) {
937                         dev_err(palmas->dev,
938                                 "Error in configuring external control\n");
939                         return ret;
940                 }
941         }
942
943         return 0;
944 }
945
946 static int palmas_extreg_init(struct palmas *palmas, int id,
947                 struct palmas_reg_init *reg_init)
948 {
949         unsigned int reg;
950         unsigned int addr;
951         int ret;
952
953         addr = palmas_regs_info[id].ctrl_addr;
954
955         ret = palmas_resource_read(palmas, addr, &reg);
956         if (ret)
957                 return ret;
958
959         if (reg_init->mode_sleep)
960                 reg |= PALMAS_REGEN1_CTRL_MODE_SLEEP;
961         else
962                 reg &= ~PALMAS_REGEN1_CTRL_MODE_SLEEP;
963
964         ret = palmas_resource_write(palmas, addr, reg);
965         if (ret < 0)
966                 return ret;
967
968         if (reg_init->roof_floor) {
969                 int sleep_id = palmas_regs_info[id].sleep_id;
970
971                 ret = palmas_ext_power_req_config(palmas, sleep_id,
972                         reg_init->roof_floor, true);
973                 if (ret < 0) {
974                         dev_err(palmas->dev,
975                                 "Error in configuring external control\n");
976                         return ret;
977                 }
978         }
979         return 0;
980 }
981
982 static void palmas_disable_smps10_boost(struct palmas *palmas)
983 {
984         unsigned int reg;
985         unsigned int addr;
986         int ret;
987         int i;
988
989         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
990
991         ret = palmas_smps_write(palmas, addr, 0x00);
992         if (ret < 0) {
993                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
994                 return;
995         }
996
997 }
998
999 static void palmas_enable_smps10_boost(struct palmas *palmas)
1000 {
1001         unsigned int reg;
1002         unsigned int addr;
1003         int ret;
1004         int i;
1005
1006         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1007
1008         ret = palmas_smps_read(palmas, addr, &reg);
1009         if (ret) {
1010                 dev_err(palmas->dev, "Error in reading smps10 control reg\n");
1011                 return;
1012         }
1013
1014         reg |= PALMA_SMPS10_VSEL;
1015         reg |= PALMA_SMPS10_BOOST_EN;
1016
1017         ret = palmas_smps_write(palmas, addr, reg);
1018         if (ret < 0) {
1019                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1020                 return;
1021         }
1022 }
1023
1024 static void palmas_enable_ldo8_track(struct palmas *palmas)
1025 {
1026         unsigned int reg;
1027         unsigned int addr;
1028         int ret;
1029         int i;
1030
1031         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1032
1033         ret = palmas_ldo_read(palmas, addr, &reg);
1034         if (ret) {
1035                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1036                 return;
1037         }
1038
1039         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1040         ret = palmas_ldo_write(palmas, addr, reg);
1041         if (ret < 0) {
1042                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
1043                 return;
1044         }
1045         /*
1046          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1047          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1048          * and can be set from 0.45 to 1.65 V.
1049          */
1050         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1051         ret = palmas_ldo_read(palmas, addr, &reg);
1052         if (ret) {
1053                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1054                 return;
1055         }
1056
1057         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1058         ret = palmas_ldo_write(palmas, addr, reg);
1059         if (ret < 0) {
1060                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1061                 return;
1062         }
1063
1064         /*
1065          * Errata ES1.0, 2,0 and 2.1
1066          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1067          * when tracking is disabled, SW has to enabe Pull-Down.
1068          */
1069         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1070                 addr = PALMAS_LDO_PD_CTRL1;
1071                 ret = palmas_ldo_read(palmas, addr, &reg);
1072                 if (ret < 0) {
1073                         dev_err(palmas->dev,
1074                                 "Error in reading pulldown control reg\n");
1075                         return;
1076                 }
1077                 reg &= ~PALMAS_LDO_PD_CTRL1_LDO8;
1078                 ret = palmas_ldo_write(palmas, addr, reg);
1079                 if (ret < 0) {
1080                         dev_err(palmas->dev,
1081                                 "Error in setting pulldown control reg\n");
1082                         return;
1083                 }
1084         }
1085
1086         return;
1087 }
1088
1089 static void palmas_disable_ldo8_track(struct palmas *palmas)
1090 {
1091         unsigned int reg;
1092         unsigned int addr;
1093         int ret;
1094
1095         /*
1096          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1097          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1098          * and can be set from 0.45 to 1.65 V.
1099          */
1100         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1101         ret = palmas_ldo_read(palmas, addr, &reg);
1102         if (ret) {
1103                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1104                 return;
1105         }
1106
1107         reg = (reg >> 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1108         ret = palmas_ldo_write(palmas, addr, reg);
1109         if (ret < 0) {
1110                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1111                 return;
1112         }
1113
1114         /* Disable the tracking mode */
1115         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1116         ret = palmas_ldo_read(palmas, addr, &reg);
1117         if (ret) {
1118                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1119                 return;
1120         }
1121         reg &= ~PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1122         ret = palmas_ldo_write(palmas, addr, reg);
1123         if (ret < 0) {
1124                 dev_err(palmas->dev, "Error in disabling tracking mode\n");
1125                 return;
1126         }
1127
1128         /*
1129          * Errata ES1.0, 2,0 and 2.1
1130          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1131          * when tracking is disabled, SW has to enabe Pull-Down.
1132          */
1133         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1134                 addr = PALMAS_LDO_PD_CTRL1;
1135                 ret = palmas_ldo_read(palmas, addr, &reg);
1136                 if (ret < 0) {
1137                         dev_err(palmas->dev,
1138                                 "Error in reading pulldown control reg\n");
1139                         return;
1140                 }
1141                 reg |= PALMAS_LDO_PD_CTRL1_LDO8;
1142                 ret = palmas_ldo_write(palmas, addr, reg);
1143                 if (ret < 0) {
1144                         dev_err(palmas->dev,
1145                                 "Error in setting pulldown control reg\n");
1146                         return;
1147                 }
1148         }
1149
1150         return;
1151 }
1152
1153 static void palmas_dvfs_init(struct palmas *palmas,
1154                         struct palmas_pmic_platform_data *pdata)
1155 {
1156         int slave;
1157         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1158         int data_size = pdata->dvfs_init_data_size;
1159         unsigned int reg, addr;
1160         int ret;
1161         int sleep_id;
1162         int i;
1163
1164         if (!dvfs_idata || !data_size)
1165                 return;
1166
1167         slave = PALMAS_BASE_TO_SLAVE(PALMAS_DVFS_BASE);
1168         for (i = 0; i < data_size; i++) {
1169                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1170
1171                 sleep_id = palmas_regs_info[dvfs_pd->reg_id].sleep_id;
1172                 if (!dvfs_pd->en_pwm)
1173                         continue;
1174
1175                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1176                                 dvfs_pd->ext_ctrl, true);
1177                 if (ret < 0) {
1178                         dev_err(palmas->dev,
1179                                         "Error in configuring external control\n");
1180                         goto err;
1181                 }
1182
1183                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1184                                 (PALMAS_SMPS_DVFS1_CTRL) + i*3);
1185                 reg =  (1 << PALMAS_SMPS_DVFS1_ENABLE_SHIFT);
1186                 if (dvfs_pd->step_20mV)
1187                         reg |= (1 << PALMAS_SMPS_DVFS1_OFFSET_STEP_SHIFT);
1188
1189                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1190                 if (ret)
1191                         goto err;
1192
1193                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1194                                 (PALMAS_SMPS_DVFS1_VOLTAGE_MAX) + i*3);
1195                 if (!(dvfs_pd->max_voltage_uV >= DVFS_BASE_VOLTAGE_UV &&
1196                         dvfs_pd->max_voltage_uV <= DVFS_MAX_VOLTAGE_UV))
1197                         goto err;
1198
1199                 reg = DIV_ROUND_UP((dvfs_pd->max_voltage_uV -
1200                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1201                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1202                 if (ret)
1203                         goto err;
1204
1205                 addr = palmas_regs_info[dvfs_pd->reg_id].fvsel_addr;
1206                 reg = (1 << PALMAS_SMPS12_FORCE_CMD_SHIFT);
1207                 reg |= DIV_ROUND_UP((dvfs_pd->base_voltage_uV -
1208                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1209                 ret = palmas_smps_write(palmas, addr, reg);
1210                 if (ret)
1211                         goto  err;
1212
1213         }
1214
1215         return;
1216 err:
1217         dev_err(palmas->dev, "Failed to initilize cl dvfs(%d)", i);
1218         return;
1219 }
1220
1221 static __devinit int palmas_probe(struct platform_device *pdev)
1222 {
1223         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
1224         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
1225         struct regulator_dev *rdev;
1226         struct palmas_pmic *pmic;
1227         struct palmas_reg_init *reg_init;
1228         struct regulator_init_data *reg_data;
1229         int id = 0, ret;
1230         unsigned int addr, reg;
1231
1232         if (!pdata)
1233                 return -EINVAL;
1234         if (!pdata->reg_data)
1235                 return -EINVAL;
1236
1237         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1238         if (!pmic)
1239                 return -ENOMEM;
1240
1241         pmic->dev = &pdev->dev;
1242         pmic->palmas = palmas;
1243         palmas->pmic = pmic;
1244         platform_set_drvdata(pdev, pmic);
1245
1246         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
1247         if (ret)
1248                 goto err_unregister_regulator;
1249
1250         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
1251                 pmic->smps123 = 1;
1252
1253         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
1254                 pmic->smps457 = 1;
1255
1256         for (id = 0; id < PALMAS_REG_LDO1; id++) {
1257                 bool ramp_delay_support = false;
1258
1259                 reg_data = pdata->reg_data[id];
1260
1261                 /*
1262                  * Miss out regulators which are not available due
1263                  * to slaving configurations.
1264                  */
1265                 switch (id) {
1266                 case PALMAS_REG_SMPS12:
1267                 case PALMAS_REG_SMPS3:
1268                         if (pmic->smps123)
1269                                 continue;
1270                         if (id == PALMAS_REG_SMPS12)
1271                                 ramp_delay_support = true;
1272                         break;
1273                 case PALMAS_REG_SMPS123:
1274                         if (!pmic->smps123)
1275                                 continue;
1276                         ramp_delay_support = true;
1277                         break;
1278                 case PALMAS_REG_SMPS45:
1279                 case PALMAS_REG_SMPS7:
1280                         if (pmic->smps457)
1281                                 continue;
1282                         if (id == PALMAS_REG_SMPS45)
1283                                 ramp_delay_support = true;
1284                         break;
1285                 case PALMAS_REG_SMPS457:
1286                         if (!pmic->smps457)
1287                                 continue;
1288                         ramp_delay_support = true;
1289                         break;
1290                 }
1291                 if ((id == PALMAS_REG_SMPS6) && (id == PALMAS_REG_SMPS8))
1292                                 ramp_delay_support = true;
1293
1294                 if (ramp_delay_support) {
1295                         addr = palmas_regs_info[id].tstep_addr;
1296                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1297                         if (ret < 0) {
1298                                 dev_err(&pdev->dev,
1299                                         "reading TSTEP reg failed: %d\n", ret);
1300                                 goto err_unregister_regulator;
1301                         }
1302                         pmic->desc[id].ramp_delay =
1303                                         palmas_smps_ramp_delay[reg & 0x3];
1304                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
1305                 }
1306
1307                 /* Register the regulators */
1308                 pmic->desc[id].name = palmas_regs_info[id].name;
1309                 pmic->desc[id].id = id;
1310
1311                 if (id != PALMAS_REG_SMPS10) {
1312                         pmic->desc[id].ops = &palmas_ops_smps;
1313                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1314                 } else {
1315                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1316                         pmic->desc[id].ops = &palmas_ops_smps10;
1317                         pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
1318                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1319                         pmic->desc[id].enable_reg = PALMAS_SMPS10_STATUS;
1320                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1321                 }
1322
1323                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1324                 pmic->desc[id].owner = THIS_MODULE;
1325
1326                 /* Initialise sleep/init values from platform data */
1327                 if (pdata && pdata->reg_init) {
1328                         reg_init = pdata->reg_init[id];
1329                         if (reg_init) {
1330                                 ret = palmas_smps_init(palmas, id, reg_init);
1331                                 if (ret)
1332                                         goto err_unregister_regulator;
1333                         }
1334                 }
1335
1336                 /*
1337                  * read and store the RANGE bit for later use
1338                  * This must be done before regulator is probed otherwise
1339                  * we error in probe with unsuportable ranges.
1340                  */
1341                 if (id != PALMAS_REG_SMPS10) {
1342                         addr = palmas_regs_info[id].vsel_addr;
1343
1344                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1345                         if (ret)
1346                                 goto err_unregister_regulator;
1347                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1348                                 pmic->range[id] = 1;
1349                 }
1350
1351                 rdev = regulator_register(&pmic->desc[id],
1352                         palmas->dev, reg_data, pmic, NULL);
1353
1354                 if (IS_ERR(rdev)) {
1355                         dev_err(&pdev->dev,
1356                                 "failed to register %s regulator\n",
1357                                 pdev->name);
1358                         ret = PTR_ERR(rdev);
1359                         goto err_unregister_regulator;
1360                 }
1361
1362                 /* Save regulator for cleanup */
1363                 pmic->rdev[id] = rdev;
1364         }
1365
1366         /* Start this loop from the id left from previous loop */
1367         for (; id < PALMAS_NUM_REGS; id++) {
1368
1369                 reg_data = pdata->reg_data[id];
1370                 /* Miss out regulators which are not available due
1371                  * to alternate functions.
1372                  */
1373
1374                 /* Register the regulators */
1375                 pmic->desc[id].name = palmas_regs_info[id].name;
1376                 pmic->desc[id].id = id;
1377                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1378                 pmic->desc[id].owner = THIS_MODULE;
1379
1380                 if (id < PALMAS_REG_REGEN1) {
1381                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1382                         pmic->desc[id].ops = &palmas_ops_ldo;
1383                         pmic->desc[id].enable_reg =
1384                                         palmas_regs_info[id].ctrl_addr;
1385                         pmic->desc[id].enable_mask =
1386                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1387                 } else {
1388                         pmic->desc[id].n_voltages = 1;
1389                         pmic->desc[id].ops = &palmas_ops_extreg;
1390                 }
1391
1392                 rdev = regulator_register(&pmic->desc[id],
1393                         palmas->dev, reg_data, pmic, NULL);
1394
1395                 if (IS_ERR(rdev)) {
1396                         dev_err(&pdev->dev,
1397                                 "failed to register %s regulator\n",
1398                                 pdev->name);
1399                         ret = PTR_ERR(rdev);
1400                         goto err_unregister_regulator;
1401                 }
1402
1403                 /* Save regulator for cleanup */
1404                 pmic->rdev[id] = rdev;
1405
1406                 /* Initialise sleep/init values from platform data */
1407                 if (pdata->reg_init) {
1408                         reg_init = pdata->reg_init[id];
1409                         if (reg_init) {
1410                                 if (id < PALMAS_REG_REGEN1)
1411                                         ret = palmas_ldo_init(palmas, id,
1412                                                                 reg_init);
1413                                 else
1414                                         ret = palmas_extreg_init(palmas, id,
1415                                                                 reg_init);
1416                                 if (ret)
1417                                         goto err_unregister_regulator;
1418                         }
1419                 }
1420         }
1421
1422         /* Check if LDO8 is in tracking mode or not */
1423         if (pdata->enable_ldo8_tracking)
1424                 palmas_enable_ldo8_track(palmas);
1425
1426         palmas_dvfs_init(palmas, pdata);
1427         return 0;
1428
1429 err_unregister_regulator:
1430         while (--id >= 0)
1431                 regulator_unregister(pmic->rdev[id]);
1432         kfree(pmic->rdev);
1433         kfree(pmic->desc);
1434         kfree(pmic);
1435         return ret;
1436 }
1437
1438 static int __devexit palmas_remove(struct platform_device *pdev)
1439 {
1440         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1441         int id;
1442
1443         for (id = 0; id < PALMAS_NUM_REGS; id++)
1444                 regulator_unregister(pmic->rdev[id]);
1445
1446         kfree(pmic->rdev);
1447         kfree(pmic->desc);
1448         kfree(pmic);
1449         return 0;
1450 }
1451
1452 #ifdef CONFIG_PM_SLEEP
1453 static int palmas_suspend(struct device *dev)
1454 {
1455         struct palmas *palmas = dev_get_drvdata(dev->parent);
1456         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1457
1458         /* Check if LDO8 is in tracking mode disable in suspend or not */
1459         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1460                 palmas_disable_ldo8_track(palmas);
1461
1462         if (pdata->disable_smps10_boost_suspend)
1463                 palmas_disable_smps10_boost(palmas);
1464         return 0;
1465 }
1466
1467 static int palmas_resume(struct device *dev)
1468 {
1469         struct palmas *palmas = dev_get_drvdata(dev->parent);
1470         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1471
1472         /* Check if LDO8 is in tracking mode disable in suspend or not */
1473         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1474                 palmas_enable_ldo8_track(palmas);
1475
1476         if (pdata->disable_smps10_boost_suspend)
1477                 palmas_enable_smps10_boost(palmas);
1478         return 0;
1479 }
1480 #endif
1481 static const struct dev_pm_ops palmas_pm_ops = {
1482         SET_SYSTEM_SLEEP_PM_OPS(palmas_suspend, palmas_resume)
1483 };
1484
1485
1486 static struct platform_driver palmas_driver = {
1487         .driver = {
1488                 .name = "palmas-pmic",
1489                 .owner = THIS_MODULE,
1490                 .pm     = &palmas_pm_ops,
1491         },
1492         .probe = palmas_probe,
1493         .remove = __devexit_p(palmas_remove),
1494 };
1495
1496 static int __init palmas_init(void)
1497 {
1498         return platform_driver_register(&palmas_driver);
1499 }
1500 subsys_initcall(palmas_init);
1501
1502 static void __exit palmas_exit(void)
1503 {
1504         platform_driver_unregister(&palmas_driver);
1505 }
1506 module_exit(palmas_exit);
1507
1508 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1509 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1510 MODULE_LICENSE("GPL");
1511 MODULE_ALIAS("platform:palmas-pmic");