4948659d0cd82736f2420d263e2a15fa885a122e
[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
607         pmic->smps10_regulator_enabled = true;
608         return ret;
609 }
610
611 static int palmas_disable_smps10(struct regulator_dev *dev)
612 {
613         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
614         int id = rdev_get_id(dev);
615         unsigned int reg;
616         int ret;
617
618         ret = palmas_smps_read(pmic->palmas,
619                                 palmas_regs_info[id].ctrl_addr, &reg);
620         if (ret < 0) {
621                 dev_err(pmic->palmas->dev,
622                         "Error in reading smps10 control reg\n");
623                 return ret;
624         }
625
626         reg &= ~PALMA_SMPS10_SWITCH_EN;
627         ret = palmas_smps_write(pmic->palmas,
628                                 palmas_regs_info[id].ctrl_addr, reg);
629         if (ret < 0)
630                 dev_err(pmic->palmas->dev,
631                         "Error in writing smps10 control reg\n");
632         pmic->smps10_regulator_enabled = false;
633         return ret;
634 }
635
636 static int palmas_get_voltage_smps10_sel(struct regulator_dev *dev)
637 {
638         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
639         int id = rdev_get_id(dev);
640         unsigned int reg;
641         int ret;
642
643         ret = palmas_smps_read(pmic->palmas,
644                                 palmas_regs_info[id].ctrl_addr, &reg);
645         if (ret < 0) {
646                 dev_err(pmic->palmas->dev,
647                         "Error in reading smps10 control reg\n");
648                 return ret;
649         }
650
651         if (reg & PALMA_SMPS10_VSEL)
652                 return 1;
653
654         return 0;
655 }
656
657 static int palmas_set_voltage_smps10_sel(struct regulator_dev *dev,
658                 unsigned selector)
659 {
660         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
661         int id = rdev_get_id(dev);
662         unsigned int reg;
663         int ret;
664
665         ret = palmas_smps_read(pmic->palmas,
666                                 palmas_regs_info[id].ctrl_addr, &reg);
667         if (ret < 0) {
668                 dev_err(pmic->palmas->dev,
669                         "Error in reading smps10 control reg\n");
670                 return ret;
671         }
672
673         if (selector)
674                 reg |= PALMA_SMPS10_VSEL;
675         else
676                 reg &= ~PALMA_SMPS10_VSEL;
677
678         /* Enable boost mode */
679         reg |= PALMA_SMPS10_BOOST_EN;
680
681         ret = palmas_smps_write(pmic->palmas,
682                                 palmas_regs_info[id].ctrl_addr, reg);
683         if (ret < 0)
684                 dev_err(pmic->palmas->dev,
685                         "Error in writing smps10 control reg\n");
686         return ret;
687 }
688
689 static int palmas_list_voltage_smps10(struct regulator_dev *dev,
690                                         unsigned selector)
691 {
692         return 3750000 + (selector * 1250000);
693 }
694
695 static struct regulator_ops palmas_ops_smps10 = {
696         .is_enabled             = palmas_is_enabled_smps10,
697         .enable                 = palmas_enable_smps10,
698         .disable                = palmas_disable_smps10,
699         .get_voltage_sel        = palmas_get_voltage_smps10_sel,
700         .set_voltage_sel        = palmas_set_voltage_smps10_sel,
701         .list_voltage           = palmas_list_voltage_smps10,
702 };
703
704 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
705 {
706         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
707         int id = rdev_get_id(dev);
708         unsigned int reg;
709
710         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
711
712         reg &= PALMAS_LDO1_CTRL_STATUS;
713
714         return !!(reg);
715 }
716
717 static int palmas_enable_ldo(struct regulator_dev *dev)
718 {
719         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
720         int id = rdev_get_id(dev);
721         unsigned int reg;
722
723         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
724
725         reg |= SMPS_CTRL_MODE_ON;
726
727         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
728
729         return 0;
730 }
731
732 static int palmas_disable_ldo(struct regulator_dev *dev)
733 {
734         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
735         int id = rdev_get_id(dev);
736         unsigned int reg;
737
738         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
739
740         reg &= ~SMPS_CTRL_MODE_ON;
741
742         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
743
744         return 0;
745 }
746
747 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
748                                         unsigned selector)
749 {
750         if (!selector)
751                 return 0;
752
753         /* voltage is 0.85V + (selector * 0.05v) */
754         return  850000 + (selector * 50000);
755 }
756
757 static int palmas_get_voltage_ldo_sel(struct regulator_dev *dev)
758 {
759         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
760         int id = rdev_get_id(dev);
761         int selector;
762         unsigned int reg;
763         unsigned int addr;
764
765         addr = palmas_regs_info[id].vsel_addr;
766
767         palmas_ldo_read(pmic->palmas, addr, &reg);
768
769         selector = reg & PALMAS_LDO1_VOLTAGE_VSEL_MASK;
770
771         /* Adjust selector to match list_voltage ranges */
772         if (selector > 49)
773                 selector = 49;
774
775         return selector;
776 }
777
778 static int palmas_set_voltage_ldo_sel(struct regulator_dev *dev,
779                 unsigned selector)
780 {
781         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
782         int id = rdev_get_id(dev);
783         unsigned int reg = 0;
784         unsigned int addr;
785
786         addr = palmas_regs_info[id].vsel_addr;
787
788         reg = selector;
789
790         palmas_ldo_write(pmic->palmas, addr, reg);
791
792         return 0;
793 }
794
795 static struct regulator_ops palmas_ops_ldo = {
796         .is_enabled             = palmas_is_enabled_ldo,
797         .enable                 = palmas_enable_ldo,
798         .disable                = palmas_disable_ldo,
799         .get_voltage_sel        = palmas_get_voltage_ldo_sel,
800         .set_voltage_sel        = palmas_set_voltage_ldo_sel,
801         .list_voltage           = palmas_list_voltage_ldo,
802 };
803
804 static int palmas_is_enabled_extreg(struct regulator_dev *dev)
805 {
806         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
807         int id = rdev_get_id(dev);
808         unsigned int reg;
809         int ret;
810
811         ret = palmas_resource_read(pmic->palmas,
812                         palmas_regs_info[id].ctrl_addr, &reg);
813         reg &= PALMAS_REGEN1_CTRL_STATUS;
814         if (ret < 0)
815                 return ret;
816
817         return !!(reg);
818 }
819
820 static int palmas_enable_extreg(struct regulator_dev *dev)
821 {
822         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
823         int id = rdev_get_id(dev);
824         unsigned int reg;
825         int ret;
826
827         ret = palmas_resource_read(pmic->palmas,
828                         palmas_regs_info[id].ctrl_addr, &reg);
829         if (ret < 0)
830                 return ret;
831
832         reg |= PALMAS_REGEN1_CTRL_MODE_ACTIVE;
833         ret = palmas_resource_write(pmic->palmas,
834                         palmas_regs_info[id].ctrl_addr, reg);
835         return ret;
836 }
837
838 static int palmas_disable_extreg(struct regulator_dev *dev)
839 {
840         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
841         int id = rdev_get_id(dev);
842         unsigned int reg;
843         int ret;
844
845         ret = palmas_resource_read(pmic->palmas,
846                         palmas_regs_info[id].ctrl_addr, &reg);
847         if (ret < 0)
848                 return ret;
849
850         reg &= ~PALMAS_REGEN1_CTRL_MODE_ACTIVE;
851         ret = palmas_resource_write(pmic->palmas,
852                         palmas_regs_info[id].ctrl_addr, reg);
853         return ret;
854 }
855
856 static int palmas_getvoltage_extreg(struct regulator_dev *rdev)
857 {
858         return 4300 * 1000;
859 }
860
861
862 static struct regulator_ops palmas_ops_extreg = {
863         .is_enabled             = palmas_is_enabled_extreg,
864         .enable                 = palmas_enable_extreg,
865         .disable                = palmas_disable_extreg,
866         .get_voltage            = palmas_getvoltage_extreg,
867 };
868
869 /*
870  * setup the hardware based sleep configuration of the SMPS/LDO regulators
871  * from the platform data. This is different to the software based control
872  * supported by the regulator framework as it is controlled by toggling
873  * pins on the PMIC such as PREQ, SYSEN, ...
874  */
875 static int palmas_smps_init(struct palmas *palmas, int id,
876                 struct palmas_reg_init *reg_init)
877 {
878         unsigned int reg;
879         unsigned int addr;
880         int ret;
881
882         addr = palmas_regs_info[id].ctrl_addr;
883
884         ret = palmas_smps_read(palmas, addr, &reg);
885         if (ret)
886                 return ret;
887
888         if (id != PALMAS_REG_SMPS10) {
889                 if (reg_init->warm_reset)
890                         reg |= PALMAS_SMPS12_CTRL_WR_S;
891
892                 if (reg_init->roof_floor)
893                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
894
895                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
896                 if (reg_init->mode_sleep)
897                         reg |= reg_init->mode_sleep <<
898                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
899         } else {
900                 if (reg_init->mode_sleep) {
901                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
902                         reg |= reg_init->mode_sleep <<
903                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
904                 }
905
906         }
907         ret = palmas_smps_write(palmas, addr, reg);
908         if (ret)
909                 return ret;
910
911         if ((id != PALMAS_REG_SMPS10) && reg_init->roof_floor) {
912                 int sleep_id = palmas_regs_info[id].sleep_id;
913                 ret = palmas_ext_power_req_config(palmas, sleep_id,
914                                         reg_init->roof_floor, true);
915                 if (ret < 0) {
916                         dev_err(palmas->dev,
917                                 "Error in configuring external control\n");
918                         return ret;
919                 }
920
921                 if (id == PALMAS_REG_SMPS123) {
922                         ret = palmas_ext_power_req_config(palmas,
923                                         PALMAS_SLEEP_REQSTR_ID_SMPS3,
924                                         reg_init->roof_floor, true);
925                         if (ret < 0) {
926                                 dev_err(palmas->dev,
927                                         "Error in configuring ext control\n");
928                                 return ret;
929                         }
930                 }
931         }
932
933         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
934                 addr = palmas_regs_info[id].tstep_addr;
935
936                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
937
938                 ret = palmas_smps_write(palmas, addr, reg);
939                 if (ret)
940                         return ret;
941         }
942
943         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
944                 addr = palmas_regs_info[id].vsel_addr;
945
946                 reg = reg_init->vsel;
947
948                 ret = palmas_smps_write(palmas, addr, reg);
949                 if (ret)
950                         return ret;
951         }
952
953
954         return 0;
955 }
956
957 static int palmas_ldo_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_ldo_read(palmas, addr, &reg);
967         if (ret)
968                 return ret;
969
970         if (reg_init->warm_reset)
971                 reg |= PALMAS_LDO1_CTRL_WR_S;
972
973         if (reg_init->mode_sleep)
974                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
975         else
976                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
977
978         ret = palmas_ldo_write(palmas, addr, reg);
979         if (ret)
980                 return ret;
981
982         if (reg_init->roof_floor) {
983                 int sleep_id = palmas_regs_info[id].sleep_id;
984
985                 ret = palmas_ext_power_req_config(palmas, sleep_id,
986                         reg_init->roof_floor, true);
987                 if (ret < 0) {
988                         dev_err(palmas->dev,
989                                 "Error in configuring external control\n");
990                         return ret;
991                 }
992         }
993
994         return 0;
995 }
996
997 static int palmas_extreg_init(struct palmas *palmas, int id,
998                 struct palmas_reg_init *reg_init)
999 {
1000         unsigned int reg;
1001         unsigned int addr;
1002         int ret;
1003
1004         addr = palmas_regs_info[id].ctrl_addr;
1005
1006         ret = palmas_resource_read(palmas, addr, &reg);
1007         if (ret)
1008                 return ret;
1009
1010         if (reg_init->mode_sleep)
1011                 reg |= PALMAS_REGEN1_CTRL_MODE_SLEEP;
1012         else
1013                 reg &= ~PALMAS_REGEN1_CTRL_MODE_SLEEP;
1014
1015         ret = palmas_resource_write(palmas, addr, reg);
1016         if (ret < 0)
1017                 return ret;
1018
1019         if (reg_init->roof_floor) {
1020                 int sleep_id = palmas_regs_info[id].sleep_id;
1021
1022                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1023                         reg_init->roof_floor, true);
1024                 if (ret < 0) {
1025                         dev_err(palmas->dev,
1026                                 "Error in configuring external control\n");
1027                         return ret;
1028                 }
1029         }
1030         return 0;
1031 }
1032
1033 static void palmas_disable_smps10_boost(struct palmas *palmas)
1034 {
1035         unsigned int addr;
1036         int ret;
1037
1038         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1039
1040         ret = palmas_smps_write(palmas, addr, 0x00);
1041         if (ret < 0) {
1042                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1043                 return;
1044         }
1045
1046 }
1047
1048 static void palmas_enable_smps10_boost(struct palmas *palmas)
1049 {
1050         unsigned int reg;
1051         unsigned int addr;
1052         int ret;
1053
1054         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1055
1056         ret = palmas_smps_read(palmas, addr, &reg);
1057         if (ret) {
1058                 dev_err(palmas->dev, "Error in reading smps10 control reg\n");
1059                 return;
1060         }
1061
1062         reg |= PALMA_SMPS10_VSEL;
1063         reg |= PALMA_SMPS10_BOOST_EN;
1064
1065         ret = palmas_smps_write(palmas, addr, reg);
1066         if (ret < 0) {
1067                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1068                 return;
1069         }
1070 }
1071
1072 static void palmas_enable_ldo8_track(struct palmas *palmas)
1073 {
1074         unsigned int reg;
1075         unsigned int addr;
1076         int ret;
1077
1078         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1079
1080         ret = palmas_ldo_read(palmas, addr, &reg);
1081         if (ret) {
1082                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1083                 return;
1084         }
1085
1086         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1087         ret = palmas_ldo_write(palmas, addr, reg);
1088         if (ret < 0) {
1089                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
1090                 return;
1091         }
1092         /*
1093          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1094          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1095          * and can be set from 0.45 to 1.65 V.
1096          */
1097         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1098         ret = palmas_ldo_read(palmas, addr, &reg);
1099         if (ret) {
1100                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1101                 return;
1102         }
1103
1104         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1105         ret = palmas_ldo_write(palmas, addr, reg);
1106         if (ret < 0) {
1107                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1108                 return;
1109         }
1110
1111         /*
1112          * Errata ES1.0, 2,0 and 2.1
1113          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1114          * when tracking is disabled, SW has to enabe Pull-Down.
1115          */
1116         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1117                 addr = PALMAS_LDO_PD_CTRL1;
1118                 ret = palmas_ldo_read(palmas, addr, &reg);
1119                 if (ret < 0) {
1120                         dev_err(palmas->dev,
1121                                 "Error in reading pulldown control reg\n");
1122                         return;
1123                 }
1124                 reg &= ~PALMAS_LDO_PD_CTRL1_LDO8;
1125                 ret = palmas_ldo_write(palmas, addr, reg);
1126                 if (ret < 0) {
1127                         dev_err(palmas->dev,
1128                                 "Error in setting pulldown control reg\n");
1129                         return;
1130                 }
1131         }
1132
1133         return;
1134 }
1135
1136 static void palmas_disable_ldo8_track(struct palmas *palmas)
1137 {
1138         unsigned int reg;
1139         unsigned int addr;
1140         int ret;
1141
1142         /*
1143          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1144          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1145          * and can be set from 0.45 to 1.65 V.
1146          */
1147         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1148         ret = palmas_ldo_read(palmas, addr, &reg);
1149         if (ret) {
1150                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1151                 return;
1152         }
1153
1154         reg = (reg >> 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1155         ret = palmas_ldo_write(palmas, addr, reg);
1156         if (ret < 0) {
1157                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1158                 return;
1159         }
1160
1161         /* Disable the tracking mode */
1162         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1163         ret = palmas_ldo_read(palmas, addr, &reg);
1164         if (ret) {
1165                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1166                 return;
1167         }
1168         reg &= ~PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1169         ret = palmas_ldo_write(palmas, addr, reg);
1170         if (ret < 0) {
1171                 dev_err(palmas->dev, "Error in disabling tracking mode\n");
1172                 return;
1173         }
1174
1175         /*
1176          * Errata ES1.0, 2,0 and 2.1
1177          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1178          * when tracking is disabled, SW has to enabe Pull-Down.
1179          */
1180         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1181                 addr = PALMAS_LDO_PD_CTRL1;
1182                 ret = palmas_ldo_read(palmas, addr, &reg);
1183                 if (ret < 0) {
1184                         dev_err(palmas->dev,
1185                                 "Error in reading pulldown control reg\n");
1186                         return;
1187                 }
1188                 reg |= PALMAS_LDO_PD_CTRL1_LDO8;
1189                 ret = palmas_ldo_write(palmas, addr, reg);
1190                 if (ret < 0) {
1191                         dev_err(palmas->dev,
1192                                 "Error in setting pulldown control reg\n");
1193                         return;
1194                 }
1195         }
1196
1197         return;
1198 }
1199
1200 static void palmas_dvfs_init(struct palmas *palmas,
1201                         struct palmas_pmic_platform_data *pdata)
1202 {
1203         int slave;
1204         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1205         int data_size = pdata->dvfs_init_data_size;
1206         unsigned int reg, addr;
1207         int ret;
1208         int sleep_id;
1209         int i;
1210
1211         if (!dvfs_idata || !data_size)
1212                 return;
1213
1214         slave = PALMAS_BASE_TO_SLAVE(PALMAS_DVFS_BASE);
1215         for (i = 0; i < data_size; i++) {
1216                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1217
1218                 sleep_id = palmas_regs_info[dvfs_pd->reg_id].sleep_id;
1219                 if (!dvfs_pd->en_pwm)
1220                         continue;
1221
1222                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1223                                 dvfs_pd->ext_ctrl, true);
1224                 if (ret < 0) {
1225                         dev_err(palmas->dev,
1226                                         "Error in configuring external control\n");
1227                         goto err;
1228                 }
1229
1230                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1231                                 (PALMAS_SMPS_DVFS1_CTRL) + i*3);
1232                 reg =  (1 << PALMAS_SMPS_DVFS1_ENABLE_SHIFT);
1233                 if (dvfs_pd->step_20mV)
1234                         reg |= (1 << PALMAS_SMPS_DVFS1_OFFSET_STEP_SHIFT);
1235
1236                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1237                 if (ret)
1238                         goto err;
1239
1240                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1241                                 (PALMAS_SMPS_DVFS1_VOLTAGE_MAX) + i*3);
1242                 if (!(dvfs_pd->max_voltage_uV >= DVFS_BASE_VOLTAGE_UV &&
1243                         dvfs_pd->max_voltage_uV <= DVFS_MAX_VOLTAGE_UV))
1244                         goto err;
1245
1246                 reg = DIV_ROUND_UP((dvfs_pd->max_voltage_uV -
1247                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1248                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1249                 if (ret)
1250                         goto err;
1251
1252                 addr = palmas_regs_info[dvfs_pd->reg_id].fvsel_addr;
1253                 reg = (1 << PALMAS_SMPS12_FORCE_CMD_SHIFT);
1254                 reg |= DIV_ROUND_UP((dvfs_pd->base_voltage_uV -
1255                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1256                 ret = palmas_smps_write(palmas, addr, reg);
1257                 if (ret)
1258                         goto  err;
1259
1260         }
1261
1262         return;
1263 err:
1264         dev_err(palmas->dev, "Failed to initilize cl dvfs(%d)", i);
1265         return;
1266 }
1267
1268 static __devinit int palmas_probe(struct platform_device *pdev)
1269 {
1270         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
1271         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
1272         struct regulator_dev *rdev;
1273         struct palmas_pmic *pmic;
1274         struct palmas_reg_init *reg_init;
1275         struct regulator_init_data *reg_data;
1276         int id = 0, ret;
1277         unsigned int addr, reg;
1278
1279         if (!pdata)
1280                 return -EINVAL;
1281         if (!pdata->reg_data)
1282                 return -EINVAL;
1283
1284         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1285         if (!pmic)
1286                 return -ENOMEM;
1287
1288         pmic->dev = &pdev->dev;
1289         pmic->palmas = palmas;
1290         palmas->pmic = pmic;
1291         platform_set_drvdata(pdev, pmic);
1292
1293         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
1294         if (ret)
1295                 goto err_unregister_regulator;
1296
1297         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
1298                 pmic->smps123 = 1;
1299
1300         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
1301                 pmic->smps457 = 1;
1302
1303         for (id = 0; id < PALMAS_REG_LDO1; id++) {
1304                 bool ramp_delay_support = false;
1305
1306                 reg_data = pdata->reg_data[id];
1307
1308                 /*
1309                  * Miss out regulators which are not available due
1310                  * to slaving configurations.
1311                  */
1312                 switch (id) {
1313                 case PALMAS_REG_SMPS12:
1314                 case PALMAS_REG_SMPS3:
1315                         if (pmic->smps123)
1316                                 continue;
1317                         if (id == PALMAS_REG_SMPS12)
1318                                 ramp_delay_support = true;
1319                         break;
1320                 case PALMAS_REG_SMPS123:
1321                         if (!pmic->smps123)
1322                                 continue;
1323                         ramp_delay_support = true;
1324                         break;
1325                 case PALMAS_REG_SMPS45:
1326                 case PALMAS_REG_SMPS7:
1327                         if (pmic->smps457)
1328                                 continue;
1329                         if (id == PALMAS_REG_SMPS45)
1330                                 ramp_delay_support = true;
1331                         break;
1332                 case PALMAS_REG_SMPS457:
1333                         if (!pmic->smps457)
1334                                 continue;
1335                         ramp_delay_support = true;
1336                         break;
1337                 }
1338                 if ((id == PALMAS_REG_SMPS6) && (id == PALMAS_REG_SMPS8))
1339                                 ramp_delay_support = true;
1340
1341                 if (ramp_delay_support) {
1342                         addr = palmas_regs_info[id].tstep_addr;
1343                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1344                         if (ret < 0) {
1345                                 dev_err(&pdev->dev,
1346                                         "reading TSTEP reg failed: %d\n", ret);
1347                                 goto err_unregister_regulator;
1348                         }
1349                         pmic->desc[id].ramp_delay =
1350                                         palmas_smps_ramp_delay[reg & 0x3];
1351                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
1352                 }
1353
1354                 /* Register the regulators */
1355                 pmic->desc[id].name = palmas_regs_info[id].name;
1356                 pmic->desc[id].id = id;
1357
1358                 if (id != PALMAS_REG_SMPS10) {
1359                         pmic->desc[id].ops = &palmas_ops_smps;
1360                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1361                 } else {
1362                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1363                         pmic->desc[id].ops = &palmas_ops_smps10;
1364                         pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
1365                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1366                         pmic->desc[id].enable_reg = PALMAS_SMPS10_STATUS;
1367                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1368                 }
1369
1370                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1371                 pmic->desc[id].owner = THIS_MODULE;
1372
1373                 /* Initialise sleep/init values from platform data */
1374                 if (pdata && pdata->reg_init) {
1375                         reg_init = pdata->reg_init[id];
1376                         if (reg_init) {
1377                                 ret = palmas_smps_init(palmas, id, reg_init);
1378                                 if (ret)
1379                                         goto err_unregister_regulator;
1380                         }
1381                 }
1382
1383                 /*
1384                  * read and store the RANGE bit for later use
1385                  * This must be done before regulator is probed otherwise
1386                  * we error in probe with unsuportable ranges.
1387                  * Read the smps mode for later use.
1388                  */
1389                 if (id != PALMAS_REG_SMPS10) {
1390                         addr = palmas_regs_info[id].vsel_addr;
1391
1392                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1393                         if (ret)
1394                                 goto err_unregister_regulator;
1395                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1396                                 pmic->range[id] = 1;
1397
1398                         /* Read the smps mode for later use. */
1399                         addr = palmas_regs_info[id].ctrl_addr;
1400                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1401                         if (ret)
1402                                 goto err_unregister_regulator;
1403                         pmic->current_mode_reg[id] = reg &
1404                                         PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
1405                 }
1406
1407                 rdev = regulator_register(&pmic->desc[id],
1408                         palmas->dev, reg_data, pmic, NULL);
1409
1410                 if (IS_ERR(rdev)) {
1411                         dev_err(&pdev->dev,
1412                                 "failed to register %s regulator\n",
1413                                 pdev->name);
1414                         ret = PTR_ERR(rdev);
1415                         goto err_unregister_regulator;
1416                 }
1417
1418                 /* Save regulator for cleanup */
1419                 pmic->rdev[id] = rdev;
1420         }
1421
1422         /* Start this loop from the id left from previous loop */
1423         for (; id < PALMAS_NUM_REGS; id++) {
1424
1425                 reg_data = pdata->reg_data[id];
1426                 /* Miss out regulators which are not available due
1427                  * to alternate functions.
1428                  */
1429
1430                 /* Register the regulators */
1431                 pmic->desc[id].name = palmas_regs_info[id].name;
1432                 pmic->desc[id].id = id;
1433                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1434                 pmic->desc[id].owner = THIS_MODULE;
1435
1436                 if (id < PALMAS_REG_REGEN1) {
1437                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1438                         pmic->desc[id].ops = &palmas_ops_ldo;
1439                         pmic->desc[id].enable_reg =
1440                                         palmas_regs_info[id].ctrl_addr;
1441                         pmic->desc[id].enable_mask =
1442                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1443                 } else {
1444                         pmic->desc[id].n_voltages = 1;
1445                         pmic->desc[id].ops = &palmas_ops_extreg;
1446                 }
1447
1448                 rdev = regulator_register(&pmic->desc[id],
1449                         palmas->dev, reg_data, pmic, NULL);
1450
1451                 if (IS_ERR(rdev)) {
1452                         dev_err(&pdev->dev,
1453                                 "failed to register %s regulator\n",
1454                                 pdev->name);
1455                         ret = PTR_ERR(rdev);
1456                         goto err_unregister_regulator;
1457                 }
1458
1459                 /* Save regulator for cleanup */
1460                 pmic->rdev[id] = rdev;
1461
1462                 /* Initialise sleep/init values from platform data */
1463                 if (pdata->reg_init) {
1464                         reg_init = pdata->reg_init[id];
1465                         if (reg_init) {
1466                                 if (id < PALMAS_REG_REGEN1)
1467                                         ret = palmas_ldo_init(palmas, id,
1468                                                                 reg_init);
1469                                 else
1470                                         ret = palmas_extreg_init(palmas, id,
1471                                                                 reg_init);
1472                                 if (ret)
1473                                         goto err_unregister_regulator;
1474                         }
1475                 }
1476         }
1477
1478         /* Check if LDO8 is in tracking mode or not */
1479         if (pdata->enable_ldo8_tracking)
1480                 palmas_enable_ldo8_track(palmas);
1481
1482         palmas_dvfs_init(palmas, pdata);
1483         return 0;
1484
1485 err_unregister_regulator:
1486         while (--id >= 0)
1487                 regulator_unregister(pmic->rdev[id]);
1488         kfree(pmic->rdev);
1489         kfree(pmic->desc);
1490         kfree(pmic);
1491         return ret;
1492 }
1493
1494 static int __devexit palmas_remove(struct platform_device *pdev)
1495 {
1496         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1497         int id;
1498
1499         for (id = 0; id < PALMAS_NUM_REGS; id++)
1500                 regulator_unregister(pmic->rdev[id]);
1501
1502         kfree(pmic->rdev);
1503         kfree(pmic->desc);
1504         kfree(pmic);
1505         return 0;
1506 }
1507
1508 #ifdef CONFIG_PM_SLEEP
1509 static int palmas_suspend(struct device *dev)
1510 {
1511         struct palmas *palmas = dev_get_drvdata(dev->parent);
1512         struct palmas_pmic *pmic = dev_get_drvdata(dev);
1513         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1514
1515         /* Check if LDO8 is in tracking mode disable in suspend or not */
1516         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1517                 palmas_disable_ldo8_track(palmas);
1518
1519         if (pdata->disable_smps10_boost_suspend &&
1520                         !pmic->smps10_regulator_enabled)
1521                 palmas_disable_smps10_boost(palmas);
1522         return 0;
1523 }
1524
1525 static int palmas_resume(struct device *dev)
1526 {
1527         struct palmas *palmas = dev_get_drvdata(dev->parent);
1528         struct palmas_pmic *pmic = dev_get_drvdata(dev);
1529         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1530
1531         /* Check if LDO8 is in tracking mode disable in suspend or not */
1532         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1533                 palmas_enable_ldo8_track(palmas);
1534
1535         if (pdata->disable_smps10_boost_suspend &&
1536                         !pmic->smps10_regulator_enabled)
1537                 palmas_enable_smps10_boost(palmas);
1538         return 0;
1539 }
1540 #endif
1541 static const struct dev_pm_ops palmas_pm_ops = {
1542         SET_SYSTEM_SLEEP_PM_OPS(palmas_suspend, palmas_resume)
1543 };
1544
1545
1546 static struct platform_driver palmas_driver = {
1547         .driver = {
1548                 .name = "palmas-pmic",
1549                 .owner = THIS_MODULE,
1550                 .pm     = &palmas_pm_ops,
1551         },
1552         .probe = palmas_probe,
1553         .remove = __devexit_p(palmas_remove),
1554 };
1555
1556 static int __init palmas_init(void)
1557 {
1558         return platform_driver_register(&palmas_driver);
1559 }
1560 subsys_initcall(palmas_init);
1561
1562 static void __exit palmas_exit(void)
1563 {
1564         platform_driver_unregister(&palmas_driver);
1565 }
1566 module_exit(palmas_exit);
1567
1568 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1569 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1570 MODULE_LICENSE("GPL");
1571 MODULE_ALIAS("platform:palmas-pmic");