b543df1d28647d4c3fbcab45367a21b8722b9638
[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         if (pmic->current_mode_reg[id])
309                 reg |= pmic->current_mode_reg[id];
310         else
311                 reg |= SMPS_CTRL_MODE_ON;
312
313         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
314
315         return 0;
316 }
317
318 static int palmas_disable_smps(struct regulator_dev *dev)
319 {
320         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
321         int id = rdev_get_id(dev);
322         unsigned int reg;
323
324         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
325
326         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
327
328         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
329
330         return 0;
331 }
332
333 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
334 {
335         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
336         int id = rdev_get_id(dev);
337         unsigned int reg;
338         int avoid_update = 0;
339
340         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
341         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
342
343         if (reg == SMPS_CTRL_MODE_OFF)
344                 avoid_update = 1;
345
346         switch (mode) {
347         case REGULATOR_MODE_NORMAL:
348                 reg |= SMPS_CTRL_MODE_ON;
349                 break;
350         case REGULATOR_MODE_IDLE:
351                 reg |= SMPS_CTRL_MODE_ECO;
352                 break;
353         case REGULATOR_MODE_FAST:
354                 reg |= SMPS_CTRL_MODE_PWM;
355                 break;
356         case REGULATOR_MODE_STANDBY:
357                 reg |= SMPS_CTRL_MODE_OFF;
358                 break;
359         default:
360                 return -EINVAL;
361         }
362         pmic->current_mode_reg[id] = reg & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
363         if (!avoid_update)
364                 palmas_smps_write(pmic->palmas,
365                         palmas_regs_info[id].ctrl_addr, reg);
366         return 0;
367 }
368
369 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
370 {
371         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
372         int id = rdev_get_id(dev);
373         unsigned int reg;
374
375         reg = pmic->current_mode_reg[id] & PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
376
377         switch (reg) {
378         case SMPS_CTRL_MODE_ON:
379                 return REGULATOR_MODE_NORMAL;
380         case SMPS_CTRL_MODE_ECO:
381                 return REGULATOR_MODE_IDLE;
382         case SMPS_CTRL_MODE_PWM:
383                 return REGULATOR_MODE_FAST;
384         case SMPS_CTRL_MODE_OFF:
385                 return REGULATOR_MODE_STANDBY;
386         }
387
388         return 0;
389 }
390
391 static int palmas_list_voltage_smps(struct regulator_dev *dev,
392                                         unsigned selector)
393 {
394         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
395         int id = rdev_get_id(dev);
396         int mult = 1;
397
398         if (!selector)
399                 return 0;
400
401         /* Read the multiplier set in VSEL register to return
402          * the correct voltage.
403          */
404         if (pmic->range[id])
405                 mult = 2;
406
407         /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
408          * as defined in data sheet. RANGE is either x1 or x2
409          */
410         return  (490000 + (selector * 10000)) * mult;
411 }
412
413 static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
414 {
415         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
416         int id = rdev_get_id(dev);
417         int selector;
418         unsigned int reg;
419         unsigned int addr;
420
421         addr = palmas_regs_info[id].vsel_addr;
422
423         palmas_smps_read(pmic->palmas, addr, &reg);
424
425         selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
426
427         /* Adjust selector to match list_voltage ranges */
428         if ((selector > 0) && (selector < 6))
429                 selector = 6;
430         if (!selector)
431                 selector = 5;
432         if (selector > 121)
433                 selector = 121;
434         selector -= 5;
435
436         return selector;
437 }
438
439 static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
440                 unsigned selector)
441 {
442         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
443         int id = rdev_get_id(dev);
444         unsigned int reg = 0;
445         unsigned int addr;
446
447         addr = palmas_regs_info[id].vsel_addr;
448
449         /* Make sure we don't change the value of RANGE */
450         if (pmic->range[id])
451                 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
452
453         /* Adjust the linux selector into range used in VSEL register */
454         if (selector)
455                 reg |= selector + 5;
456
457         palmas_smps_write(pmic->palmas, addr, reg);
458
459         return 0;
460 }
461
462 static int palma_smps_set_voltage_smps_time_sel(struct regulator_dev *rdev,
463         unsigned int old_selector, unsigned int new_selector)
464 {
465         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
466         int id = rdev_get_id(rdev);
467         int old_uV, new_uV;
468         unsigned int ramp_delay = pmic->ramp_delay[id];
469
470         /* ES2.1, have the 1.5X slower slew rate than configured */
471         if (palmas_is_es_version_or_less(pmic->palmas, 2, 1))
472                 ramp_delay = (ramp_delay * 15)/10;
473
474         if (!ramp_delay)
475                 return 0;
476
477         old_uV = palmas_list_voltage_smps(rdev, old_selector);
478         if (old_uV < 0)
479                 return old_uV;
480
481         new_uV = palmas_list_voltage_smps(rdev, new_selector);
482         if (new_uV < 0)
483                 return new_uV;
484
485         return DIV_ROUND_UP(abs(old_uV - new_uV), ramp_delay);
486 }
487
488 static int palmas_smps_set_ramp_delay(struct regulator_dev *rdev,
489                 int ramp_delay)
490 {
491         struct palmas_pmic *pmic = rdev_get_drvdata(rdev);
492         int id = rdev_get_id(rdev);
493         unsigned int reg = 0;
494         unsigned int addr = palmas_regs_info[id].tstep_addr;
495         int ret;
496
497         if (ramp_delay <= 0)
498                 reg = 0;
499         else if (ramp_delay < 2500)
500                 reg = 3;
501         else if (ramp_delay < 5000)
502                 reg = 2;
503         else
504                 reg = 1;
505
506         ret = palmas_smps_write(pmic->palmas, addr, reg);
507         if (ret < 0) {
508                 dev_err(pmic->palmas->dev, "TSTEP write failed: %d\n", ret);
509                 return ret;
510         }
511
512         pmic->ramp_delay[id] = palmas_smps_ramp_delay[reg];
513         return ret;
514 }
515
516 static struct regulator_ops palmas_ops_smps = {
517         .is_enabled             = palmas_is_enabled_smps,
518         .enable                 = palmas_enable_smps,
519         .disable                = palmas_disable_smps,
520         .set_mode               = palmas_set_mode_smps,
521         .get_mode               = palmas_get_mode_smps,
522         .get_voltage_sel        = palmas_get_voltage_smps_sel,
523         .set_voltage_sel        = palmas_set_voltage_smps_sel,
524         .list_voltage           = palmas_list_voltage_smps,
525         .set_voltage_time_sel   = palma_smps_set_voltage_smps_time_sel,
526         .set_ramp_delay         = palmas_smps_set_ramp_delay,
527 };
528
529 static int palmas_is_enabled_smps10(struct regulator_dev *dev)
530 {
531         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
532         unsigned int reg;
533         int ret;
534
535         ret = palmas_smps_read(pmic->palmas, PALMAS_SMPS10_STATUS, &reg);
536         if (ret < 0) {
537                 dev_err(pmic->palmas->dev,
538                         "Error in reading smps10 status reg\n");
539                 return ret;
540         }
541
542         if (reg & PALMA_SMPS10_SWITCH_EN)
543                 return 1;
544
545         return 0;
546 }
547
548 static int palmas_enable_smps10(struct regulator_dev *dev)
549 {
550         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
551         int id = rdev_get_id(dev);
552         unsigned int reg;
553         int ret;
554
555         ret = palmas_smps_read(pmic->palmas,
556                                 palmas_regs_info[id].ctrl_addr, &reg);
557         if (ret < 0) {
558                 dev_err(pmic->palmas->dev,
559                         "Error in reading smps10 control reg\n");
560                 return ret;
561         }
562
563         reg |= PALMA_SMPS10_SWITCH_EN;
564         ret = palmas_smps_write(pmic->palmas,
565                                 palmas_regs_info[id].ctrl_addr, reg);
566         if (ret < 0)
567                 dev_err(pmic->palmas->dev,
568                         "Error in writing smps10 control reg\n");
569         return ret;
570 }
571
572 static int palmas_disable_smps10(struct regulator_dev *dev)
573 {
574         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
575         int id = rdev_get_id(dev);
576         unsigned int reg;
577         int ret;
578
579         ret = palmas_smps_read(pmic->palmas,
580                                 palmas_regs_info[id].ctrl_addr, &reg);
581         if (ret < 0) {
582                 dev_err(pmic->palmas->dev,
583                         "Error in reading smps10 control reg\n");
584                 return ret;
585         }
586
587         reg &= ~PALMA_SMPS10_SWITCH_EN;
588         ret = palmas_smps_write(pmic->palmas,
589                                 palmas_regs_info[id].ctrl_addr, reg);
590         if (ret < 0)
591                 dev_err(pmic->palmas->dev,
592                         "Error in writing smps10 control reg\n");
593         return ret;
594 }
595
596 static int palmas_get_voltage_smps10_sel(struct regulator_dev *dev)
597 {
598         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
599         int id = rdev_get_id(dev);
600         unsigned int reg;
601         int ret;
602
603         ret = palmas_smps_read(pmic->palmas,
604                                 palmas_regs_info[id].ctrl_addr, &reg);
605         if (ret < 0) {
606                 dev_err(pmic->palmas->dev,
607                         "Error in reading smps10 control reg\n");
608                 return ret;
609         }
610
611         if (reg & PALMA_SMPS10_VSEL)
612                 return 1;
613
614         return 0;
615 }
616
617 static int palmas_set_voltage_smps10_sel(struct regulator_dev *dev,
618                 unsigned selector)
619 {
620         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
621         int id = rdev_get_id(dev);
622         unsigned int reg;
623         int ret;
624
625         ret = palmas_smps_read(pmic->palmas,
626                                 palmas_regs_info[id].ctrl_addr, &reg);
627         if (ret < 0) {
628                 dev_err(pmic->palmas->dev,
629                         "Error in reading smps10 control reg\n");
630                 return ret;
631         }
632
633         if (selector)
634                 reg |= PALMA_SMPS10_VSEL;
635         else
636                 reg &= ~PALMA_SMPS10_VSEL;
637
638         /* Enable boost mode */
639         reg |= PALMA_SMPS10_BOOST_EN;
640
641         ret = palmas_smps_write(pmic->palmas,
642                                 palmas_regs_info[id].ctrl_addr, reg);
643         if (ret < 0)
644                 dev_err(pmic->palmas->dev,
645                         "Error in writing smps10 control reg\n");
646         return ret;
647 }
648
649 static int palmas_list_voltage_smps10(struct regulator_dev *dev,
650                                         unsigned selector)
651 {
652         return 3750000 + (selector * 1250000);
653 }
654
655 static struct regulator_ops palmas_ops_smps10 = {
656         .is_enabled             = palmas_is_enabled_smps10,
657         .enable                 = palmas_enable_smps10,
658         .disable                = palmas_disable_smps10,
659         .get_voltage_sel        = palmas_get_voltage_smps10_sel,
660         .set_voltage_sel        = palmas_set_voltage_smps10_sel,
661         .list_voltage           = palmas_list_voltage_smps10,
662 };
663
664 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
665 {
666         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
667         int id = rdev_get_id(dev);
668         unsigned int reg;
669
670         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
671
672         reg &= PALMAS_LDO1_CTRL_STATUS;
673
674         return !!(reg);
675 }
676
677 static int palmas_enable_ldo(struct regulator_dev *dev)
678 {
679         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
680         int id = rdev_get_id(dev);
681         unsigned int reg;
682
683         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
684
685         reg |= SMPS_CTRL_MODE_ON;
686
687         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
688
689         return 0;
690 }
691
692 static int palmas_disable_ldo(struct regulator_dev *dev)
693 {
694         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
695         int id = rdev_get_id(dev);
696         unsigned int reg;
697
698         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
699
700         reg &= ~SMPS_CTRL_MODE_ON;
701
702         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
703
704         return 0;
705 }
706
707 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
708                                         unsigned selector)
709 {
710         if (!selector)
711                 return 0;
712
713         /* voltage is 0.85V + (selector * 0.05v) */
714         return  850000 + (selector * 50000);
715 }
716
717 static int palmas_get_voltage_ldo_sel(struct regulator_dev *dev)
718 {
719         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
720         int id = rdev_get_id(dev);
721         int selector;
722         unsigned int reg;
723         unsigned int addr;
724
725         addr = palmas_regs_info[id].vsel_addr;
726
727         palmas_ldo_read(pmic->palmas, addr, &reg);
728
729         selector = reg & PALMAS_LDO1_VOLTAGE_VSEL_MASK;
730
731         /* Adjust selector to match list_voltage ranges */
732         if (selector > 49)
733                 selector = 49;
734
735         return selector;
736 }
737
738 static int palmas_set_voltage_ldo_sel(struct regulator_dev *dev,
739                 unsigned selector)
740 {
741         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
742         int id = rdev_get_id(dev);
743         unsigned int reg = 0;
744         unsigned int addr;
745
746         addr = palmas_regs_info[id].vsel_addr;
747
748         reg = selector;
749
750         palmas_ldo_write(pmic->palmas, addr, reg);
751
752         return 0;
753 }
754
755 static struct regulator_ops palmas_ops_ldo = {
756         .is_enabled             = palmas_is_enabled_ldo,
757         .enable                 = palmas_enable_ldo,
758         .disable                = palmas_disable_ldo,
759         .get_voltage_sel        = palmas_get_voltage_ldo_sel,
760         .set_voltage_sel        = palmas_set_voltage_ldo_sel,
761         .list_voltage           = palmas_list_voltage_ldo,
762 };
763
764 static int palmas_is_enabled_extreg(struct regulator_dev *dev)
765 {
766         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
767         int id = rdev_get_id(dev);
768         unsigned int reg;
769         int ret;
770
771         ret = palmas_resource_read(pmic->palmas,
772                         palmas_regs_info[id].ctrl_addr, &reg);
773         reg &= PALMAS_REGEN1_CTRL_STATUS;
774         if (ret < 0)
775                 return ret;
776
777         return !!(reg);
778 }
779
780 static int palmas_enable_extreg(struct regulator_dev *dev)
781 {
782         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
783         int id = rdev_get_id(dev);
784         unsigned int reg;
785         int ret;
786
787         ret = palmas_resource_read(pmic->palmas,
788                         palmas_regs_info[id].ctrl_addr, &reg);
789         if (ret < 0)
790                 return ret;
791
792         reg |= PALMAS_REGEN1_CTRL_MODE_ACTIVE;
793         ret = palmas_resource_write(pmic->palmas,
794                         palmas_regs_info[id].ctrl_addr, reg);
795         return ret;
796 }
797
798 static int palmas_disable_extreg(struct regulator_dev *dev)
799 {
800         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
801         int id = rdev_get_id(dev);
802         unsigned int reg;
803         int ret;
804
805         ret = palmas_resource_read(pmic->palmas,
806                         palmas_regs_info[id].ctrl_addr, &reg);
807         if (ret < 0)
808                 return ret;
809
810         reg &= ~PALMAS_REGEN1_CTRL_MODE_ACTIVE;
811         ret = palmas_resource_write(pmic->palmas,
812                         palmas_regs_info[id].ctrl_addr, reg);
813         return ret;
814 }
815
816 static int palmas_getvoltage_extreg(struct regulator_dev *rdev)
817 {
818         return 4300 * 1000;
819 }
820
821
822 static struct regulator_ops palmas_ops_extreg = {
823         .is_enabled             = palmas_is_enabled_extreg,
824         .enable                 = palmas_enable_extreg,
825         .disable                = palmas_disable_extreg,
826         .get_voltage            = palmas_getvoltage_extreg,
827 };
828
829 /*
830  * setup the hardware based sleep configuration of the SMPS/LDO regulators
831  * from the platform data. This is different to the software based control
832  * supported by the regulator framework as it is controlled by toggling
833  * pins on the PMIC such as PREQ, SYSEN, ...
834  */
835 static int palmas_smps_init(struct palmas *palmas, int id,
836                 struct palmas_reg_init *reg_init)
837 {
838         unsigned int reg;
839         unsigned int addr;
840         int ret;
841
842         addr = palmas_regs_info[id].ctrl_addr;
843
844         ret = palmas_smps_read(palmas, addr, &reg);
845         if (ret)
846                 return ret;
847
848         if (id != PALMAS_REG_SMPS10) {
849                 if (reg_init->warm_reset)
850                         reg |= PALMAS_SMPS12_CTRL_WR_S;
851
852                 if (reg_init->roof_floor)
853                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
854
855                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
856                 if (reg_init->mode_sleep)
857                         reg |= reg_init->mode_sleep <<
858                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
859         } else {
860                 if (reg_init->mode_sleep) {
861                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
862                         reg |= reg_init->mode_sleep <<
863                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
864                 }
865
866         }
867         ret = palmas_smps_write(palmas, addr, reg);
868         if (ret)
869                 return ret;
870
871         if ((id != PALMAS_REG_SMPS10) && reg_init->roof_floor) {
872                 int sleep_id = palmas_regs_info[id].sleep_id;
873                 ret = palmas_ext_power_req_config(palmas, sleep_id,
874                                         reg_init->roof_floor, true);
875                 if (ret < 0) {
876                         dev_err(palmas->dev,
877                                 "Error in configuring external control\n");
878                         return ret;
879                 }
880
881                 if (id == PALMAS_REG_SMPS123) {
882                         ret = palmas_ext_power_req_config(palmas,
883                                         PALMAS_SLEEP_REQSTR_ID_SMPS3,
884                                         reg_init->roof_floor, true);
885                         if (ret < 0) {
886                                 dev_err(palmas->dev,
887                                         "Error in configuring ext control\n");
888                                 return ret;
889                         }
890                 }
891         }
892
893         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
894                 addr = palmas_regs_info[id].tstep_addr;
895
896                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
897
898                 ret = palmas_smps_write(palmas, addr, reg);
899                 if (ret)
900                         return ret;
901         }
902
903         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
904                 addr = palmas_regs_info[id].vsel_addr;
905
906                 reg = reg_init->vsel;
907
908                 ret = palmas_smps_write(palmas, addr, reg);
909                 if (ret)
910                         return ret;
911         }
912
913
914         return 0;
915 }
916
917 static int palmas_ldo_init(struct palmas *palmas, int id,
918                 struct palmas_reg_init *reg_init)
919 {
920         unsigned int reg;
921         unsigned int addr;
922         int ret;
923
924         addr = palmas_regs_info[id].ctrl_addr;
925
926         ret = palmas_ldo_read(palmas, addr, &reg);
927         if (ret)
928                 return ret;
929
930         if (reg_init->warm_reset)
931                 reg |= PALMAS_LDO1_CTRL_WR_S;
932
933         if (reg_init->mode_sleep)
934                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
935         else
936                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
937
938         ret = palmas_ldo_write(palmas, addr, reg);
939         if (ret)
940                 return ret;
941
942         if (reg_init->roof_floor) {
943                 int sleep_id = palmas_regs_info[id].sleep_id;
944
945                 ret = palmas_ext_power_req_config(palmas, sleep_id,
946                         reg_init->roof_floor, true);
947                 if (ret < 0) {
948                         dev_err(palmas->dev,
949                                 "Error in configuring external control\n");
950                         return ret;
951                 }
952         }
953
954         return 0;
955 }
956
957 static int palmas_extreg_init(struct palmas *palmas, int id,
958                 struct palmas_reg_init *reg_init)
959 {
960         unsigned int reg;
961         unsigned int addr;
962         int ret;
963
964         addr = palmas_regs_info[id].ctrl_addr;
965
966         ret = palmas_resource_read(palmas, addr, &reg);
967         if (ret)
968                 return ret;
969
970         if (reg_init->mode_sleep)
971                 reg |= PALMAS_REGEN1_CTRL_MODE_SLEEP;
972         else
973                 reg &= ~PALMAS_REGEN1_CTRL_MODE_SLEEP;
974
975         ret = palmas_resource_write(palmas, addr, reg);
976         if (ret < 0)
977                 return ret;
978
979         if (reg_init->roof_floor) {
980                 int sleep_id = palmas_regs_info[id].sleep_id;
981
982                 ret = palmas_ext_power_req_config(palmas, sleep_id,
983                         reg_init->roof_floor, true);
984                 if (ret < 0) {
985                         dev_err(palmas->dev,
986                                 "Error in configuring external control\n");
987                         return ret;
988                 }
989         }
990         return 0;
991 }
992
993 static void palmas_disable_smps10_boost(struct palmas *palmas)
994 {
995         unsigned int reg;
996         unsigned int addr;
997         int ret;
998         int i;
999
1000         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1001
1002         ret = palmas_smps_write(palmas, addr, 0x00);
1003         if (ret < 0) {
1004                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1005                 return;
1006         }
1007
1008 }
1009
1010 static void palmas_enable_smps10_boost(struct palmas *palmas)
1011 {
1012         unsigned int reg;
1013         unsigned int addr;
1014         int ret;
1015         int i;
1016
1017         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1018
1019         ret = palmas_smps_read(palmas, addr, &reg);
1020         if (ret) {
1021                 dev_err(palmas->dev, "Error in reading smps10 control reg\n");
1022                 return;
1023         }
1024
1025         reg |= PALMA_SMPS10_VSEL;
1026         reg |= PALMA_SMPS10_BOOST_EN;
1027
1028         ret = palmas_smps_write(palmas, addr, reg);
1029         if (ret < 0) {
1030                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1031                 return;
1032         }
1033 }
1034
1035 static void palmas_enable_ldo8_track(struct palmas *palmas)
1036 {
1037         unsigned int reg;
1038         unsigned int addr;
1039         int ret;
1040         int i;
1041
1042         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1043
1044         ret = palmas_ldo_read(palmas, addr, &reg);
1045         if (ret) {
1046                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1047                 return;
1048         }
1049
1050         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1051         ret = palmas_ldo_write(palmas, addr, reg);
1052         if (ret < 0) {
1053                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
1054                 return;
1055         }
1056         /*
1057          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1058          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1059          * and can be set from 0.45 to 1.65 V.
1060          */
1061         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1062         ret = palmas_ldo_read(palmas, addr, &reg);
1063         if (ret) {
1064                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1065                 return;
1066         }
1067
1068         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1069         ret = palmas_ldo_write(palmas, addr, reg);
1070         if (ret < 0) {
1071                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1072                 return;
1073         }
1074
1075         /*
1076          * Errata ES1.0, 2,0 and 2.1
1077          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1078          * when tracking is disabled, SW has to enabe Pull-Down.
1079          */
1080         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1081                 addr = PALMAS_LDO_PD_CTRL1;
1082                 ret = palmas_ldo_read(palmas, addr, &reg);
1083                 if (ret < 0) {
1084                         dev_err(palmas->dev,
1085                                 "Error in reading pulldown control reg\n");
1086                         return;
1087                 }
1088                 reg &= ~PALMAS_LDO_PD_CTRL1_LDO8;
1089                 ret = palmas_ldo_write(palmas, addr, reg);
1090                 if (ret < 0) {
1091                         dev_err(palmas->dev,
1092                                 "Error in setting pulldown control reg\n");
1093                         return;
1094                 }
1095         }
1096
1097         return;
1098 }
1099
1100 static void palmas_disable_ldo8_track(struct palmas *palmas)
1101 {
1102         unsigned int reg;
1103         unsigned int addr;
1104         int ret;
1105
1106         /*
1107          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1108          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1109          * and can be set from 0.45 to 1.65 V.
1110          */
1111         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1112         ret = palmas_ldo_read(palmas, addr, &reg);
1113         if (ret) {
1114                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1115                 return;
1116         }
1117
1118         reg = (reg >> 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1119         ret = palmas_ldo_write(palmas, addr, reg);
1120         if (ret < 0) {
1121                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1122                 return;
1123         }
1124
1125         /* Disable the tracking mode */
1126         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1127         ret = palmas_ldo_read(palmas, addr, &reg);
1128         if (ret) {
1129                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1130                 return;
1131         }
1132         reg &= ~PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1133         ret = palmas_ldo_write(palmas, addr, reg);
1134         if (ret < 0) {
1135                 dev_err(palmas->dev, "Error in disabling tracking mode\n");
1136                 return;
1137         }
1138
1139         /*
1140          * Errata ES1.0, 2,0 and 2.1
1141          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1142          * when tracking is disabled, SW has to enabe Pull-Down.
1143          */
1144         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1145                 addr = PALMAS_LDO_PD_CTRL1;
1146                 ret = palmas_ldo_read(palmas, addr, &reg);
1147                 if (ret < 0) {
1148                         dev_err(palmas->dev,
1149                                 "Error in reading pulldown control reg\n");
1150                         return;
1151                 }
1152                 reg |= PALMAS_LDO_PD_CTRL1_LDO8;
1153                 ret = palmas_ldo_write(palmas, addr, reg);
1154                 if (ret < 0) {
1155                         dev_err(palmas->dev,
1156                                 "Error in setting pulldown control reg\n");
1157                         return;
1158                 }
1159         }
1160
1161         return;
1162 }
1163
1164 static void palmas_dvfs_init(struct palmas *palmas,
1165                         struct palmas_pmic_platform_data *pdata)
1166 {
1167         int slave;
1168         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1169         int data_size = pdata->dvfs_init_data_size;
1170         unsigned int reg, addr;
1171         int ret;
1172         int sleep_id;
1173         int i;
1174
1175         if (!dvfs_idata || !data_size)
1176                 return;
1177
1178         slave = PALMAS_BASE_TO_SLAVE(PALMAS_DVFS_BASE);
1179         for (i = 0; i < data_size; i++) {
1180                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1181
1182                 sleep_id = palmas_regs_info[dvfs_pd->reg_id].sleep_id;
1183                 if (!dvfs_pd->en_pwm)
1184                         continue;
1185
1186                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1187                                 dvfs_pd->ext_ctrl, true);
1188                 if (ret < 0) {
1189                         dev_err(palmas->dev,
1190                                         "Error in configuring external control\n");
1191                         goto err;
1192                 }
1193
1194                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1195                                 (PALMAS_SMPS_DVFS1_CTRL) + i*3);
1196                 reg =  (1 << PALMAS_SMPS_DVFS1_ENABLE_SHIFT);
1197                 if (dvfs_pd->step_20mV)
1198                         reg |= (1 << PALMAS_SMPS_DVFS1_OFFSET_STEP_SHIFT);
1199
1200                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1201                 if (ret)
1202                         goto err;
1203
1204                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1205                                 (PALMAS_SMPS_DVFS1_VOLTAGE_MAX) + i*3);
1206                 if (!(dvfs_pd->max_voltage_uV >= DVFS_BASE_VOLTAGE_UV &&
1207                         dvfs_pd->max_voltage_uV <= DVFS_MAX_VOLTAGE_UV))
1208                         goto err;
1209
1210                 reg = DIV_ROUND_UP((dvfs_pd->max_voltage_uV -
1211                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1212                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1213                 if (ret)
1214                         goto err;
1215
1216                 addr = palmas_regs_info[dvfs_pd->reg_id].fvsel_addr;
1217                 reg = (1 << PALMAS_SMPS12_FORCE_CMD_SHIFT);
1218                 reg |= DIV_ROUND_UP((dvfs_pd->base_voltage_uV -
1219                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1220                 ret = palmas_smps_write(palmas, addr, reg);
1221                 if (ret)
1222                         goto  err;
1223
1224         }
1225
1226         return;
1227 err:
1228         dev_err(palmas->dev, "Failed to initilize cl dvfs(%d)", i);
1229         return;
1230 }
1231
1232 static __devinit int palmas_probe(struct platform_device *pdev)
1233 {
1234         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
1235         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
1236         struct regulator_dev *rdev;
1237         struct palmas_pmic *pmic;
1238         struct palmas_reg_init *reg_init;
1239         struct regulator_init_data *reg_data;
1240         int id = 0, ret;
1241         unsigned int addr, reg;
1242
1243         if (!pdata)
1244                 return -EINVAL;
1245         if (!pdata->reg_data)
1246                 return -EINVAL;
1247
1248         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1249         if (!pmic)
1250                 return -ENOMEM;
1251
1252         pmic->dev = &pdev->dev;
1253         pmic->palmas = palmas;
1254         palmas->pmic = pmic;
1255         platform_set_drvdata(pdev, pmic);
1256
1257         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
1258         if (ret)
1259                 goto err_unregister_regulator;
1260
1261         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
1262                 pmic->smps123 = 1;
1263
1264         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
1265                 pmic->smps457 = 1;
1266
1267         for (id = 0; id < PALMAS_REG_LDO1; id++) {
1268                 bool ramp_delay_support = false;
1269
1270                 reg_data = pdata->reg_data[id];
1271
1272                 /*
1273                  * Miss out regulators which are not available due
1274                  * to slaving configurations.
1275                  */
1276                 switch (id) {
1277                 case PALMAS_REG_SMPS12:
1278                 case PALMAS_REG_SMPS3:
1279                         if (pmic->smps123)
1280                                 continue;
1281                         if (id == PALMAS_REG_SMPS12)
1282                                 ramp_delay_support = true;
1283                         break;
1284                 case PALMAS_REG_SMPS123:
1285                         if (!pmic->smps123)
1286                                 continue;
1287                         ramp_delay_support = true;
1288                         break;
1289                 case PALMAS_REG_SMPS45:
1290                 case PALMAS_REG_SMPS7:
1291                         if (pmic->smps457)
1292                                 continue;
1293                         if (id == PALMAS_REG_SMPS45)
1294                                 ramp_delay_support = true;
1295                         break;
1296                 case PALMAS_REG_SMPS457:
1297                         if (!pmic->smps457)
1298                                 continue;
1299                         ramp_delay_support = true;
1300                         break;
1301                 }
1302                 if ((id == PALMAS_REG_SMPS6) && (id == PALMAS_REG_SMPS8))
1303                                 ramp_delay_support = true;
1304
1305                 if (ramp_delay_support) {
1306                         addr = palmas_regs_info[id].tstep_addr;
1307                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1308                         if (ret < 0) {
1309                                 dev_err(&pdev->dev,
1310                                         "reading TSTEP reg failed: %d\n", ret);
1311                                 goto err_unregister_regulator;
1312                         }
1313                         pmic->desc[id].ramp_delay =
1314                                         palmas_smps_ramp_delay[reg & 0x3];
1315                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
1316                 }
1317
1318                 /* Register the regulators */
1319                 pmic->desc[id].name = palmas_regs_info[id].name;
1320                 pmic->desc[id].id = id;
1321
1322                 if (id != PALMAS_REG_SMPS10) {
1323                         pmic->desc[id].ops = &palmas_ops_smps;
1324                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1325                 } else {
1326                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1327                         pmic->desc[id].ops = &palmas_ops_smps10;
1328                         pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
1329                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1330                         pmic->desc[id].enable_reg = PALMAS_SMPS10_STATUS;
1331                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1332                 }
1333
1334                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1335                 pmic->desc[id].owner = THIS_MODULE;
1336
1337                 /* Initialise sleep/init values from platform data */
1338                 if (pdata && pdata->reg_init) {
1339                         reg_init = pdata->reg_init[id];
1340                         if (reg_init) {
1341                                 ret = palmas_smps_init(palmas, id, reg_init);
1342                                 if (ret)
1343                                         goto err_unregister_regulator;
1344                         }
1345                 }
1346
1347                 /*
1348                  * read and store the RANGE bit for later use
1349                  * This must be done before regulator is probed otherwise
1350                  * we error in probe with unsuportable ranges.
1351                  * Read the smps mode for later use.
1352                  */
1353                 if (id != PALMAS_REG_SMPS10) {
1354                         addr = palmas_regs_info[id].vsel_addr;
1355
1356                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1357                         if (ret)
1358                                 goto err_unregister_regulator;
1359                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1360                                 pmic->range[id] = 1;
1361
1362                         /* Read the smps mode for later use. */
1363                         addr = palmas_regs_info[id].ctrl_addr;
1364                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1365                         if (ret)
1366                                 goto err_unregister_regulator;
1367                         pmic->current_mode_reg[id] = reg &
1368                                         PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
1369                 }
1370
1371                 rdev = regulator_register(&pmic->desc[id],
1372                         palmas->dev, reg_data, pmic, NULL);
1373
1374                 if (IS_ERR(rdev)) {
1375                         dev_err(&pdev->dev,
1376                                 "failed to register %s regulator\n",
1377                                 pdev->name);
1378                         ret = PTR_ERR(rdev);
1379                         goto err_unregister_regulator;
1380                 }
1381
1382                 /* Save regulator for cleanup */
1383                 pmic->rdev[id] = rdev;
1384         }
1385
1386         /* Start this loop from the id left from previous loop */
1387         for (; id < PALMAS_NUM_REGS; id++) {
1388
1389                 reg_data = pdata->reg_data[id];
1390                 /* Miss out regulators which are not available due
1391                  * to alternate functions.
1392                  */
1393
1394                 /* Register the regulators */
1395                 pmic->desc[id].name = palmas_regs_info[id].name;
1396                 pmic->desc[id].id = id;
1397                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1398                 pmic->desc[id].owner = THIS_MODULE;
1399
1400                 if (id < PALMAS_REG_REGEN1) {
1401                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1402                         pmic->desc[id].ops = &palmas_ops_ldo;
1403                         pmic->desc[id].enable_reg =
1404                                         palmas_regs_info[id].ctrl_addr;
1405                         pmic->desc[id].enable_mask =
1406                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1407                 } else {
1408                         pmic->desc[id].n_voltages = 1;
1409                         pmic->desc[id].ops = &palmas_ops_extreg;
1410                 }
1411
1412                 rdev = regulator_register(&pmic->desc[id],
1413                         palmas->dev, reg_data, pmic, NULL);
1414
1415                 if (IS_ERR(rdev)) {
1416                         dev_err(&pdev->dev,
1417                                 "failed to register %s regulator\n",
1418                                 pdev->name);
1419                         ret = PTR_ERR(rdev);
1420                         goto err_unregister_regulator;
1421                 }
1422
1423                 /* Save regulator for cleanup */
1424                 pmic->rdev[id] = rdev;
1425
1426                 /* Initialise sleep/init values from platform data */
1427                 if (pdata->reg_init) {
1428                         reg_init = pdata->reg_init[id];
1429                         if (reg_init) {
1430                                 if (id < PALMAS_REG_REGEN1)
1431                                         ret = palmas_ldo_init(palmas, id,
1432                                                                 reg_init);
1433                                 else
1434                                         ret = palmas_extreg_init(palmas, id,
1435                                                                 reg_init);
1436                                 if (ret)
1437                                         goto err_unregister_regulator;
1438                         }
1439                 }
1440         }
1441
1442         /* Check if LDO8 is in tracking mode or not */
1443         if (pdata->enable_ldo8_tracking)
1444                 palmas_enable_ldo8_track(palmas);
1445
1446         palmas_dvfs_init(palmas, pdata);
1447         return 0;
1448
1449 err_unregister_regulator:
1450         while (--id >= 0)
1451                 regulator_unregister(pmic->rdev[id]);
1452         kfree(pmic->rdev);
1453         kfree(pmic->desc);
1454         kfree(pmic);
1455         return ret;
1456 }
1457
1458 static int __devexit palmas_remove(struct platform_device *pdev)
1459 {
1460         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1461         int id;
1462
1463         for (id = 0; id < PALMAS_NUM_REGS; id++)
1464                 regulator_unregister(pmic->rdev[id]);
1465
1466         kfree(pmic->rdev);
1467         kfree(pmic->desc);
1468         kfree(pmic);
1469         return 0;
1470 }
1471
1472 #ifdef CONFIG_PM_SLEEP
1473 static int palmas_suspend(struct device *dev)
1474 {
1475         struct palmas *palmas = dev_get_drvdata(dev->parent);
1476         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1477
1478         /* Check if LDO8 is in tracking mode disable in suspend or not */
1479         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1480                 palmas_disable_ldo8_track(palmas);
1481
1482         if (pdata->disable_smps10_boost_suspend)
1483                 palmas_disable_smps10_boost(palmas);
1484         return 0;
1485 }
1486
1487 static int palmas_resume(struct device *dev)
1488 {
1489         struct palmas *palmas = dev_get_drvdata(dev->parent);
1490         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1491
1492         /* Check if LDO8 is in tracking mode disable in suspend or not */
1493         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1494                 palmas_enable_ldo8_track(palmas);
1495
1496         if (pdata->disable_smps10_boost_suspend)
1497                 palmas_enable_smps10_boost(palmas);
1498         return 0;
1499 }
1500 #endif
1501 static const struct dev_pm_ops palmas_pm_ops = {
1502         SET_SYSTEM_SLEEP_PM_OPS(palmas_suspend, palmas_resume)
1503 };
1504
1505
1506 static struct platform_driver palmas_driver = {
1507         .driver = {
1508                 .name = "palmas-pmic",
1509                 .owner = THIS_MODULE,
1510                 .pm     = &palmas_pm_ops,
1511         },
1512         .probe = palmas_probe,
1513         .remove = __devexit_p(palmas_remove),
1514 };
1515
1516 static int __init palmas_init(void)
1517 {
1518         return platform_driver_register(&palmas_driver);
1519 }
1520 subsys_initcall(palmas_init);
1521
1522 static void __exit palmas_exit(void)
1523 {
1524         platform_driver_unregister(&palmas_driver);
1525 }
1526 module_exit(palmas_exit);
1527
1528 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1529 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1530 MODULE_LICENSE("GPL");
1531 MODULE_ALIAS("platform:palmas-pmic");