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