regulator: palmas: implement sleep mode configuration
[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_STATUS_MASK;
299         reg >>= PALMAS_SMPS12_CTRL_STATUS_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 reg;
1033         unsigned int addr;
1034         int ret;
1035         int i;
1036
1037         addr = palmas_regs_info[PALMAS_REG_SMPS10].ctrl_addr;
1038
1039         ret = palmas_smps_write(palmas, addr, 0x00);
1040         if (ret < 0) {
1041                 dev_err(palmas->dev, "Error in disabling smps10 boost\n");
1042                 return;
1043         }
1044
1045 }
1046
1047 static void palmas_enable_smps10_boost(struct palmas *palmas)
1048 {
1049         unsigned int reg;
1050         unsigned int addr;
1051         int ret;
1052         int i;
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         int i;
1078
1079         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1080
1081         ret = palmas_ldo_read(palmas, addr, &reg);
1082         if (ret) {
1083                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1084                 return;
1085         }
1086
1087         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1088         ret = palmas_ldo_write(palmas, addr, reg);
1089         if (ret < 0) {
1090                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
1091                 return;
1092         }
1093         /*
1094          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1095          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1096          * and can be set from 0.45 to 1.65 V.
1097          */
1098         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1099         ret = palmas_ldo_read(palmas, addr, &reg);
1100         if (ret) {
1101                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1102                 return;
1103         }
1104
1105         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1106         ret = palmas_ldo_write(palmas, addr, reg);
1107         if (ret < 0) {
1108                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1109                 return;
1110         }
1111
1112         /*
1113          * Errata ES1.0, 2,0 and 2.1
1114          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1115          * when tracking is disabled, SW has to enabe Pull-Down.
1116          */
1117         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1118                 addr = PALMAS_LDO_PD_CTRL1;
1119                 ret = palmas_ldo_read(palmas, addr, &reg);
1120                 if (ret < 0) {
1121                         dev_err(palmas->dev,
1122                                 "Error in reading pulldown control reg\n");
1123                         return;
1124                 }
1125                 reg &= ~PALMAS_LDO_PD_CTRL1_LDO8;
1126                 ret = palmas_ldo_write(palmas, addr, reg);
1127                 if (ret < 0) {
1128                         dev_err(palmas->dev,
1129                                 "Error in setting pulldown control reg\n");
1130                         return;
1131                 }
1132         }
1133
1134         return;
1135 }
1136
1137 static void palmas_disable_ldo8_track(struct palmas *palmas)
1138 {
1139         unsigned int reg;
1140         unsigned int addr;
1141         int ret;
1142
1143         /*
1144          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
1145          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
1146          * and can be set from 0.45 to 1.65 V.
1147          */
1148         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
1149         ret = palmas_ldo_read(palmas, addr, &reg);
1150         if (ret) {
1151                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
1152                 return;
1153         }
1154
1155         reg = (reg >> 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
1156         ret = palmas_ldo_write(palmas, addr, reg);
1157         if (ret < 0) {
1158                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
1159                 return;
1160         }
1161
1162         /* Disable the tracking mode */
1163         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
1164         ret = palmas_ldo_read(palmas, addr, &reg);
1165         if (ret) {
1166                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
1167                 return;
1168         }
1169         reg &= ~PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
1170         ret = palmas_ldo_write(palmas, addr, reg);
1171         if (ret < 0) {
1172                 dev_err(palmas->dev, "Error in disabling tracking mode\n");
1173                 return;
1174         }
1175
1176         /*
1177          * Errata ES1.0, 2,0 and 2.1
1178          * When Tracking is enbled, it need to disable Pull-Down for LDO8 and
1179          * when tracking is disabled, SW has to enabe Pull-Down.
1180          */
1181         if (palmas_is_es_version_or_less(palmas, 2, 1)) {
1182                 addr = PALMAS_LDO_PD_CTRL1;
1183                 ret = palmas_ldo_read(palmas, addr, &reg);
1184                 if (ret < 0) {
1185                         dev_err(palmas->dev,
1186                                 "Error in reading pulldown control reg\n");
1187                         return;
1188                 }
1189                 reg |= PALMAS_LDO_PD_CTRL1_LDO8;
1190                 ret = palmas_ldo_write(palmas, addr, reg);
1191                 if (ret < 0) {
1192                         dev_err(palmas->dev,
1193                                 "Error in setting pulldown control reg\n");
1194                         return;
1195                 }
1196         }
1197
1198         return;
1199 }
1200
1201 static void palmas_dvfs_init(struct palmas *palmas,
1202                         struct palmas_pmic_platform_data *pdata)
1203 {
1204         int slave;
1205         struct palmas_dvfs_init_data *dvfs_idata = pdata->dvfs_init_data;
1206         int data_size = pdata->dvfs_init_data_size;
1207         unsigned int reg, addr;
1208         int ret;
1209         int sleep_id;
1210         int i;
1211
1212         if (!dvfs_idata || !data_size)
1213                 return;
1214
1215         slave = PALMAS_BASE_TO_SLAVE(PALMAS_DVFS_BASE);
1216         for (i = 0; i < data_size; i++) {
1217                 struct palmas_dvfs_init_data *dvfs_pd =  &dvfs_idata[i];
1218
1219                 sleep_id = palmas_regs_info[dvfs_pd->reg_id].sleep_id;
1220                 if (!dvfs_pd->en_pwm)
1221                         continue;
1222
1223                 ret = palmas_ext_power_req_config(palmas, sleep_id,
1224                                 dvfs_pd->ext_ctrl, true);
1225                 if (ret < 0) {
1226                         dev_err(palmas->dev,
1227                                         "Error in configuring external control\n");
1228                         goto err;
1229                 }
1230
1231                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1232                                 (PALMAS_SMPS_DVFS1_CTRL) + i*3);
1233                 reg =  (1 << PALMAS_SMPS_DVFS1_ENABLE_SHIFT);
1234                 if (dvfs_pd->step_20mV)
1235                         reg |= (1 << PALMAS_SMPS_DVFS1_OFFSET_STEP_SHIFT);
1236
1237                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1238                 if (ret)
1239                         goto err;
1240
1241                 addr = PALMAS_BASE_TO_REG(PALMAS_DVFS_BASE,
1242                                 (PALMAS_SMPS_DVFS1_VOLTAGE_MAX) + i*3);
1243                 if (!(dvfs_pd->max_voltage_uV >= DVFS_BASE_VOLTAGE_UV &&
1244                         dvfs_pd->max_voltage_uV <= DVFS_MAX_VOLTAGE_UV))
1245                         goto err;
1246
1247                 reg = DIV_ROUND_UP((dvfs_pd->max_voltage_uV -
1248                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1249                 ret = regmap_write(palmas->regmap[slave], addr, reg);
1250                 if (ret)
1251                         goto err;
1252
1253                 addr = palmas_regs_info[dvfs_pd->reg_id].fvsel_addr;
1254                 reg = (1 << PALMAS_SMPS12_FORCE_CMD_SHIFT);
1255                 reg |= DIV_ROUND_UP((dvfs_pd->base_voltage_uV -
1256                         DVFS_BASE_VOLTAGE_UV), DVFS_VOLTAGE_STEP_UV) + 6;
1257                 ret = palmas_smps_write(palmas, addr, reg);
1258                 if (ret)
1259                         goto  err;
1260
1261         }
1262
1263         return;
1264 err:
1265         dev_err(palmas->dev, "Failed to initilize cl dvfs(%d)", i);
1266         return;
1267 }
1268
1269 static __devinit int palmas_probe(struct platform_device *pdev)
1270 {
1271         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
1272         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
1273         struct regulator_dev *rdev;
1274         struct palmas_pmic *pmic;
1275         struct palmas_reg_init *reg_init;
1276         struct regulator_init_data *reg_data;
1277         int id = 0, ret;
1278         unsigned int addr, reg;
1279
1280         if (!pdata)
1281                 return -EINVAL;
1282         if (!pdata->reg_data)
1283                 return -EINVAL;
1284
1285         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
1286         if (!pmic)
1287                 return -ENOMEM;
1288
1289         pmic->dev = &pdev->dev;
1290         pmic->palmas = palmas;
1291         palmas->pmic = pmic;
1292         platform_set_drvdata(pdev, pmic);
1293
1294         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
1295         if (ret)
1296                 goto err_unregister_regulator;
1297
1298         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
1299                 pmic->smps123 = 1;
1300
1301         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
1302                 pmic->smps457 = 1;
1303
1304         for (id = 0; id < PALMAS_REG_LDO1; id++) {
1305                 bool ramp_delay_support = false;
1306
1307                 reg_data = pdata->reg_data[id];
1308
1309                 /*
1310                  * Miss out regulators which are not available due
1311                  * to slaving configurations.
1312                  */
1313                 switch (id) {
1314                 case PALMAS_REG_SMPS12:
1315                 case PALMAS_REG_SMPS3:
1316                         if (pmic->smps123)
1317                                 continue;
1318                         if (id == PALMAS_REG_SMPS12)
1319                                 ramp_delay_support = true;
1320                         break;
1321                 case PALMAS_REG_SMPS123:
1322                         if (!pmic->smps123)
1323                                 continue;
1324                         ramp_delay_support = true;
1325                         break;
1326                 case PALMAS_REG_SMPS45:
1327                 case PALMAS_REG_SMPS7:
1328                         if (pmic->smps457)
1329                                 continue;
1330                         if (id == PALMAS_REG_SMPS45)
1331                                 ramp_delay_support = true;
1332                         break;
1333                 case PALMAS_REG_SMPS457:
1334                         if (!pmic->smps457)
1335                                 continue;
1336                         ramp_delay_support = true;
1337                         break;
1338                 }
1339                 if ((id == PALMAS_REG_SMPS6) && (id == PALMAS_REG_SMPS8))
1340                                 ramp_delay_support = true;
1341
1342                 if (ramp_delay_support) {
1343                         addr = palmas_regs_info[id].tstep_addr;
1344                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1345                         if (ret < 0) {
1346                                 dev_err(&pdev->dev,
1347                                         "reading TSTEP reg failed: %d\n", ret);
1348                                 goto err_unregister_regulator;
1349                         }
1350                         pmic->desc[id].ramp_delay =
1351                                         palmas_smps_ramp_delay[reg & 0x3];
1352                         pmic->ramp_delay[id] = pmic->desc[id].ramp_delay;
1353                 }
1354
1355                 /* Register the regulators */
1356                 pmic->desc[id].name = palmas_regs_info[id].name;
1357                 pmic->desc[id].id = id;
1358
1359                 if (id != PALMAS_REG_SMPS10) {
1360                         pmic->desc[id].ops = &palmas_ops_smps;
1361                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1362                 } else {
1363                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1364                         pmic->desc[id].ops = &palmas_ops_smps10;
1365                         pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
1366                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1367                         pmic->desc[id].enable_reg = PALMAS_SMPS10_STATUS;
1368                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1369                 }
1370
1371                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1372                 pmic->desc[id].owner = THIS_MODULE;
1373
1374                 /* Initialise sleep/init values from platform data */
1375                 if (pdata && pdata->reg_init) {
1376                         reg_init = pdata->reg_init[id];
1377                         if (reg_init) {
1378                                 ret = palmas_smps_init(palmas, id, reg_init);
1379                                 if (ret)
1380                                         goto err_unregister_regulator;
1381                         }
1382                 }
1383
1384                 /*
1385                  * read and store the RANGE bit for later use
1386                  * This must be done before regulator is probed otherwise
1387                  * we error in probe with unsuportable ranges.
1388                  * Read the smps mode for later use.
1389                  */
1390                 if (id != PALMAS_REG_SMPS10) {
1391                         addr = palmas_regs_info[id].vsel_addr;
1392
1393                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1394                         if (ret)
1395                                 goto err_unregister_regulator;
1396                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1397                                 pmic->range[id] = 1;
1398
1399                         /* Read the smps mode for later use. */
1400                         addr = palmas_regs_info[id].ctrl_addr;
1401                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1402                         if (ret)
1403                                 goto err_unregister_regulator;
1404                         pmic->current_mode_reg[id] = reg &
1405                                         PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
1406                 }
1407
1408                 rdev = regulator_register(&pmic->desc[id],
1409                         palmas->dev, reg_data, pmic, NULL);
1410
1411                 if (IS_ERR(rdev)) {
1412                         dev_err(&pdev->dev,
1413                                 "failed to register %s regulator\n",
1414                                 pdev->name);
1415                         ret = PTR_ERR(rdev);
1416                         goto err_unregister_regulator;
1417                 }
1418
1419                 /* Save regulator for cleanup */
1420                 pmic->rdev[id] = rdev;
1421         }
1422
1423         /* Start this loop from the id left from previous loop */
1424         for (; id < PALMAS_NUM_REGS; id++) {
1425
1426                 reg_data = pdata->reg_data[id];
1427                 /* Miss out regulators which are not available due
1428                  * to alternate functions.
1429                  */
1430
1431                 /* Register the regulators */
1432                 pmic->desc[id].name = palmas_regs_info[id].name;
1433                 pmic->desc[id].id = id;
1434                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1435                 pmic->desc[id].owner = THIS_MODULE;
1436
1437                 if (id < PALMAS_REG_REGEN1) {
1438                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1439                         pmic->desc[id].ops = &palmas_ops_ldo;
1440                         pmic->desc[id].enable_reg =
1441                                         palmas_regs_info[id].ctrl_addr;
1442                         pmic->desc[id].enable_mask =
1443                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1444                 } else {
1445                         pmic->desc[id].n_voltages = 1;
1446                         pmic->desc[id].ops = &palmas_ops_extreg;
1447                 }
1448
1449                 rdev = regulator_register(&pmic->desc[id],
1450                         palmas->dev, reg_data, pmic, NULL);
1451
1452                 if (IS_ERR(rdev)) {
1453                         dev_err(&pdev->dev,
1454                                 "failed to register %s regulator\n",
1455                                 pdev->name);
1456                         ret = PTR_ERR(rdev);
1457                         goto err_unregister_regulator;
1458                 }
1459
1460                 /* Save regulator for cleanup */
1461                 pmic->rdev[id] = rdev;
1462
1463                 /* Initialise sleep/init values from platform data */
1464                 if (pdata->reg_init) {
1465                         reg_init = pdata->reg_init[id];
1466                         if (reg_init) {
1467                                 if (id < PALMAS_REG_REGEN1)
1468                                         ret = palmas_ldo_init(palmas, id,
1469                                                                 reg_init);
1470                                 else
1471                                         ret = palmas_extreg_init(palmas, id,
1472                                                                 reg_init);
1473                                 if (ret)
1474                                         goto err_unregister_regulator;
1475                         }
1476                 }
1477         }
1478
1479         /* Check if LDO8 is in tracking mode or not */
1480         if (pdata->enable_ldo8_tracking)
1481                 palmas_enable_ldo8_track(palmas);
1482
1483         palmas_dvfs_init(palmas, pdata);
1484         return 0;
1485
1486 err_unregister_regulator:
1487         while (--id >= 0)
1488                 regulator_unregister(pmic->rdev[id]);
1489         kfree(pmic->rdev);
1490         kfree(pmic->desc);
1491         kfree(pmic);
1492         return ret;
1493 }
1494
1495 static int __devexit palmas_remove(struct platform_device *pdev)
1496 {
1497         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1498         int id;
1499
1500         for (id = 0; id < PALMAS_NUM_REGS; id++)
1501                 regulator_unregister(pmic->rdev[id]);
1502
1503         kfree(pmic->rdev);
1504         kfree(pmic->desc);
1505         kfree(pmic);
1506         return 0;
1507 }
1508
1509 #ifdef CONFIG_PM_SLEEP
1510 static int palmas_suspend(struct device *dev)
1511 {
1512         struct palmas *palmas = dev_get_drvdata(dev->parent);
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                 palmas_disable_smps10_boost(palmas);
1521         return 0;
1522 }
1523
1524 static int palmas_resume(struct device *dev)
1525 {
1526         struct palmas *palmas = dev_get_drvdata(dev->parent);
1527         struct palmas_pmic_platform_data *pdata = dev_get_platdata(dev);
1528
1529         /* Check if LDO8 is in tracking mode disable in suspend or not */
1530         if (pdata->enable_ldo8_tracking && pdata->disabe_ldo8_tracking_suspend)
1531                 palmas_enable_ldo8_track(palmas);
1532
1533         if (pdata->disable_smps10_boost_suspend)
1534                 palmas_enable_smps10_boost(palmas);
1535         return 0;
1536 }
1537 #endif
1538 static const struct dev_pm_ops palmas_pm_ops = {
1539         SET_SYSTEM_SLEEP_PM_OPS(palmas_suspend, palmas_resume)
1540 };
1541
1542
1543 static struct platform_driver palmas_driver = {
1544         .driver = {
1545                 .name = "palmas-pmic",
1546                 .owner = THIS_MODULE,
1547                 .pm     = &palmas_pm_ops,
1548         },
1549         .probe = palmas_probe,
1550         .remove = __devexit_p(palmas_remove),
1551 };
1552
1553 static int __init palmas_init(void)
1554 {
1555         return platform_driver_register(&palmas_driver);
1556 }
1557 subsys_initcall(palmas_init);
1558
1559 static void __exit palmas_exit(void)
1560 {
1561         platform_driver_unregister(&palmas_driver);
1562 }
1563 module_exit(palmas_exit);
1564
1565 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1566 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1567 MODULE_LICENSE("GPL");
1568 MODULE_ALIAS("platform:palmas-pmic");