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