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