0b1163dd51daf19ea008af57acd7a9e75e7d51ef
[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         int     sleep_id;
37 };
38
39 static const struct regs_info palmas_regs_info[] = {
40         {
41                 .name           = "SMPS12",
42                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
43                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
44                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
45                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
46         },
47         {
48                 .name           = "SMPS123",
49                 .vsel_addr      = PALMAS_SMPS12_VOLTAGE,
50                 .ctrl_addr      = PALMAS_SMPS12_CTRL,
51                 .tstep_addr     = PALMAS_SMPS12_TSTEP,
52                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS12,
53         },
54         {
55                 .name           = "SMPS3",
56                 .vsel_addr      = PALMAS_SMPS3_VOLTAGE,
57                 .ctrl_addr      = PALMAS_SMPS3_CTRL,
58                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS3,
59         },
60         {
61                 .name           = "SMPS45",
62                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
63                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
64                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
65                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
66         },
67         {
68                 .name           = "SMPS457",
69                 .vsel_addr      = PALMAS_SMPS45_VOLTAGE,
70                 .ctrl_addr      = PALMAS_SMPS45_CTRL,
71                 .tstep_addr     = PALMAS_SMPS45_TSTEP,
72                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS45,
73         },
74         {
75                 .name           = "SMPS6",
76                 .vsel_addr      = PALMAS_SMPS6_VOLTAGE,
77                 .ctrl_addr      = PALMAS_SMPS6_CTRL,
78                 .tstep_addr     = PALMAS_SMPS6_TSTEP,
79                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS6,
80         },
81         {
82                 .name           = "SMPS7",
83                 .vsel_addr      = PALMAS_SMPS7_VOLTAGE,
84                 .ctrl_addr      = PALMAS_SMPS7_CTRL,
85                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS7,
86         },
87         {
88                 .name           = "SMPS8",
89                 .vsel_addr      = PALMAS_SMPS8_VOLTAGE,
90                 .ctrl_addr      = PALMAS_SMPS8_CTRL,
91                 .tstep_addr     = PALMAS_SMPS8_TSTEP,
92                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS8,
93         },
94         {
95                 .name           = "SMPS9",
96                 .vsel_addr      = PALMAS_SMPS9_VOLTAGE,
97                 .ctrl_addr      = PALMAS_SMPS9_CTRL,
98                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS9,
99         },
100         {
101                 .name           = "SMPS10",
102                 .ctrl_addr      = PALMAS_SMPS10_CTRL,
103                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SMPS10,
104         },
105         {
106                 .name           = "LDO1",
107                 .vsel_addr      = PALMAS_LDO1_VOLTAGE,
108                 .ctrl_addr      = PALMAS_LDO1_CTRL,
109                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO1,
110         },
111         {
112                 .name           = "LDO2",
113                 .vsel_addr      = PALMAS_LDO2_VOLTAGE,
114                 .ctrl_addr      = PALMAS_LDO2_CTRL,
115                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO2,
116         },
117         {
118                 .name           = "LDO3",
119                 .vsel_addr      = PALMAS_LDO3_VOLTAGE,
120                 .ctrl_addr      = PALMAS_LDO3_CTRL,
121                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO3,
122         },
123         {
124                 .name           = "LDO4",
125                 .vsel_addr      = PALMAS_LDO4_VOLTAGE,
126                 .ctrl_addr      = PALMAS_LDO4_CTRL,
127                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO4,
128         },
129         {
130                 .name           = "LDO5",
131                 .vsel_addr      = PALMAS_LDO5_VOLTAGE,
132                 .ctrl_addr      = PALMAS_LDO5_CTRL,
133                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO5,
134         },
135         {
136                 .name           = "LDO6",
137                 .vsel_addr      = PALMAS_LDO6_VOLTAGE,
138                 .ctrl_addr      = PALMAS_LDO6_CTRL,
139                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO6,
140         },
141         {
142                 .name           = "LDO7",
143                 .vsel_addr      = PALMAS_LDO7_VOLTAGE,
144                 .ctrl_addr      = PALMAS_LDO7_CTRL,
145                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO7,
146         },
147         {
148                 .name           = "LDO8",
149                 .vsel_addr      = PALMAS_LDO8_VOLTAGE,
150                 .ctrl_addr      = PALMAS_LDO8_CTRL,
151                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO8,
152         },
153         {
154                 .name           = "LDO9",
155                 .vsel_addr      = PALMAS_LDO9_VOLTAGE,
156                 .ctrl_addr      = PALMAS_LDO9_CTRL,
157                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDO9,
158         },
159         {
160                 .name           = "LDOLN",
161                 .vsel_addr      = PALMAS_LDOLN_VOLTAGE,
162                 .ctrl_addr      = PALMAS_LDOLN_CTRL,
163                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOLN,
164         },
165         {
166                 .name           = "LDOUSB",
167                 .vsel_addr      = PALMAS_LDOUSB_VOLTAGE,
168                 .ctrl_addr      = PALMAS_LDOUSB_CTRL,
169                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_LDOUSB,
170         },
171         {
172                 .name           = "REGEN1",
173                 .ctrl_addr      = PALMAS_REGEN1_CTRL,
174                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN1,
175         },
176         {
177                 .name           = "REGEN2",
178                 .ctrl_addr      = PALMAS_REGEN2_CTRL,
179                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN2,
180         },
181         {
182                 .name           = "REGEN3",
183                 .ctrl_addr      = PALMAS_REGEN3_CTRL,
184                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_REGEN3,
185         },
186         {
187                 .name           = "SYSEN1",
188                 .ctrl_addr      = PALMAS_SYSEN1_CTRL,
189                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN1,
190         },
191         {
192                 .name           = "SYSEN2",
193                 .ctrl_addr      = PALMAS_SYSEN2_CTRL,
194                 .sleep_id       = PALMAS_SLEEP_REQSTR_ID_SYSEN2,
195         },
196 };
197
198 #define SMPS_CTRL_MODE_OFF              0x00
199 #define SMPS_CTRL_MODE_ON               0x01
200 #define SMPS_CTRL_MODE_ECO              0x02
201 #define SMPS_CTRL_MODE_PWM              0x03
202
203 /* These values are derived from the data sheet. And are the number of steps
204  * where there is a voltage change, the ranges at beginning and end of register
205  * max/min values where there are no change are ommitted.
206  *
207  * So they are basically (maxV-minV)/stepV
208  */
209 #define PALMAS_SMPS_NUM_VOLTAGES        116
210 #define PALMAS_SMPS10_NUM_VOLTAGES      2
211 #define PALMAS_LDO_NUM_VOLTAGES         50
212
213 #define SMPS10_VSEL                     (1<<3)
214 #define SMPS10_BOOST_EN                 (1<<2)
215 #define SMPS10_BYPASS_EN                (1<<1)
216 #define SMPS10_SWITCH_EN                (1<<0)
217
218 #define REGULATOR_SLAVE                 0
219
220 static int palmas_smps_read(struct palmas *palmas, unsigned int reg,
221                 unsigned int *dest)
222 {
223         unsigned int addr;
224
225         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
226
227         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
228 }
229
230 static int palmas_smps_write(struct palmas *palmas, unsigned int reg,
231                 unsigned int value)
232 {
233         unsigned int addr;
234
235         addr = PALMAS_BASE_TO_REG(PALMAS_SMPS_BASE, reg);
236
237         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
238 }
239
240 static int palmas_ldo_read(struct palmas *palmas, unsigned int reg,
241                 unsigned int *dest)
242 {
243         unsigned int addr;
244
245         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
246
247         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
248 }
249
250 static int palmas_ldo_write(struct palmas *palmas, unsigned int reg,
251                 unsigned int value)
252 {
253         unsigned int addr;
254
255         addr = PALMAS_BASE_TO_REG(PALMAS_LDO_BASE, reg);
256
257         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
258 }
259
260 static int palmas_resource_read(struct palmas *palmas, unsigned int reg,
261                 unsigned int *dest)
262 {
263         unsigned int addr;
264
265         addr = PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE, reg);
266
267         return regmap_read(palmas->regmap[REGULATOR_SLAVE], addr, dest);
268 }
269
270 static int palmas_resource_write(struct palmas *palmas, unsigned int reg,
271                 unsigned int value)
272 {
273         unsigned int addr;
274
275         addr = PALMAS_BASE_TO_REG(PALMAS_RESOURCE_BASE, reg);
276
277         return regmap_write(palmas->regmap[REGULATOR_SLAVE], addr, value);
278 }
279
280 static int palmas_is_enabled_smps(struct regulator_dev *dev)
281 {
282         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
283         int id = rdev_get_id(dev);
284         unsigned int reg;
285
286         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
287
288         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
289         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
290
291         return !!(reg);
292 }
293
294 static int palmas_enable_smps(struct regulator_dev *dev)
295 {
296         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
297         int id = rdev_get_id(dev);
298         unsigned int reg;
299
300         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
301
302         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
303         reg |= SMPS_CTRL_MODE_ON;
304
305         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
306
307         return 0;
308 }
309
310 static int palmas_disable_smps(struct regulator_dev *dev)
311 {
312         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
313         int id = rdev_get_id(dev);
314         unsigned int reg;
315
316         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
317
318         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
319
320         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
321
322         return 0;
323 }
324
325
326 static int palmas_set_mode_smps(struct regulator_dev *dev, unsigned int mode)
327 {
328         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
329         int id = rdev_get_id(dev);
330         unsigned int reg;
331
332         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
333         reg &= ~PALMAS_SMPS12_CTRL_MODE_ACTIVE_MASK;
334
335         switch (mode) {
336         case REGULATOR_MODE_NORMAL:
337                 reg |= SMPS_CTRL_MODE_ON;
338                 break;
339         case REGULATOR_MODE_IDLE:
340                 reg |= SMPS_CTRL_MODE_ECO;
341                 break;
342         case REGULATOR_MODE_FAST:
343                 reg |= SMPS_CTRL_MODE_PWM;
344                 break;
345         default:
346                 return -EINVAL;
347         }
348         palmas_smps_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
349
350         return 0;
351 }
352
353 static unsigned int palmas_get_mode_smps(struct regulator_dev *dev)
354 {
355         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
356         int id = rdev_get_id(dev);
357         unsigned int reg;
358
359         palmas_smps_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
360         reg &= PALMAS_SMPS12_CTRL_STATUS_MASK;
361         reg >>= PALMAS_SMPS12_CTRL_STATUS_SHIFT;
362
363         switch (reg) {
364         case SMPS_CTRL_MODE_ON:
365                 return REGULATOR_MODE_NORMAL;
366         case SMPS_CTRL_MODE_ECO:
367                 return REGULATOR_MODE_IDLE;
368         case SMPS_CTRL_MODE_PWM:
369                 return REGULATOR_MODE_FAST;
370         }
371
372         return 0;
373 }
374
375 static int palmas_list_voltage_smps(struct regulator_dev *dev,
376                                         unsigned selector)
377 {
378         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
379         int id = rdev_get_id(dev);
380         int mult = 1;
381
382         if (!selector)
383                 return 0;
384
385         /* Read the multiplier set in VSEL register to return
386          * the correct voltage.
387          */
388         if (pmic->range[id])
389                 mult = 2;
390
391         /* Voltage is (0.49V + (selector * 0.01V)) * RANGE
392          * as defined in data sheet. RANGE is either x1 or x2
393          */
394         return  (490000 + (selector * 10000)) * mult;
395 }
396
397 static int palmas_get_voltage_smps_sel(struct regulator_dev *dev)
398 {
399         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
400         int id = rdev_get_id(dev);
401         int selector;
402         unsigned int reg;
403         unsigned int addr;
404
405         addr = palmas_regs_info[id].vsel_addr;
406
407         palmas_smps_read(pmic->palmas, addr, &reg);
408
409         selector = reg & PALMAS_SMPS12_VOLTAGE_VSEL_MASK;
410
411         /* Adjust selector to match list_voltage ranges */
412         if ((selector > 0) && (selector < 6))
413                 selector = 6;
414         if (!selector)
415                 selector = 5;
416         if (selector > 121)
417                 selector = 121;
418         selector -= 5;
419
420         return selector;
421 }
422
423 static int palmas_set_voltage_smps_sel(struct regulator_dev *dev,
424                 unsigned selector)
425 {
426         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
427         int id = rdev_get_id(dev);
428         unsigned int reg = 0;
429         unsigned int addr;
430
431         addr = palmas_regs_info[id].vsel_addr;
432
433         /* Make sure we don't change the value of RANGE */
434         if (pmic->range[id])
435                 reg |= PALMAS_SMPS12_VOLTAGE_RANGE;
436
437         /* Adjust the linux selector into range used in VSEL register */
438         if (selector)
439                 reg |= selector + 5;
440
441         palmas_smps_write(pmic->palmas, addr, reg);
442
443         return 0;
444 }
445
446 static struct regulator_ops palmas_ops_smps = {
447         .is_enabled             = palmas_is_enabled_smps,
448         .enable                 = palmas_enable_smps,
449         .disable                = palmas_disable_smps,
450         .set_mode               = palmas_set_mode_smps,
451         .get_mode               = palmas_get_mode_smps,
452         .get_voltage_sel        = palmas_get_voltage_smps_sel,
453         .set_voltage_sel        = palmas_set_voltage_smps_sel,
454         .list_voltage           = palmas_list_voltage_smps,
455 };
456
457 static int palmas_is_enabled_smps10(struct regulator_dev *dev)
458 {
459         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
460         unsigned int reg;
461         int ret;
462
463         ret = palmas_smps_read(pmic->palmas, PALMAS_SMPS10_STATUS, &reg);
464         if (ret < 0) {
465                 dev_err(pmic->palmas->dev,
466                         "Error in reading smps10 status reg\n");
467                 return ret;
468         }
469
470         if (reg & PALMA_SMPS10_SWITCH_EN)
471                 return 1;
472
473         return 0;
474 }
475
476 static int palmas_enable_smps10(struct regulator_dev *dev)
477 {
478         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
479         int id = rdev_get_id(dev);
480         unsigned int reg;
481         int ret;
482
483         ret = palmas_smps_read(pmic->palmas,
484                                 palmas_regs_info[id].ctrl_addr, &reg);
485         if (ret < 0) {
486                 dev_err(pmic->palmas->dev,
487                         "Error in reading smps10 control reg\n");
488                 return ret;
489         }
490
491         reg |= PALMA_SMPS10_SWITCH_EN;
492         ret = palmas_smps_write(pmic->palmas,
493                                 palmas_regs_info[id].ctrl_addr, reg);
494         if (ret < 0)
495                 dev_err(pmic->palmas->dev,
496                         "Error in writing smps10 control reg\n");
497         return ret;
498 }
499
500 static int palmas_disable_smps10(struct regulator_dev *dev)
501 {
502         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
503         int id = rdev_get_id(dev);
504         unsigned int reg;
505         int ret;
506
507         ret = palmas_smps_read(pmic->palmas,
508                                 palmas_regs_info[id].ctrl_addr, &reg);
509         if (ret < 0) {
510                 dev_err(pmic->palmas->dev,
511                         "Error in reading smps10 control reg\n");
512                 return ret;
513         }
514
515         reg &= ~PALMA_SMPS10_SWITCH_EN;
516         ret = palmas_smps_write(pmic->palmas,
517                                 palmas_regs_info[id].ctrl_addr, reg);
518         if (ret < 0)
519                 dev_err(pmic->palmas->dev,
520                         "Error in writing smps10 control reg\n");
521         return ret;
522 }
523
524 static int palmas_get_voltage_smps10_sel(struct regulator_dev *dev)
525 {
526         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
527         int id = rdev_get_id(dev);
528         unsigned int reg;
529         int ret;
530
531         ret = palmas_smps_read(pmic->palmas,
532                                 palmas_regs_info[id].ctrl_addr, &reg);
533         if (ret < 0) {
534                 dev_err(pmic->palmas->dev,
535                         "Error in reading smps10 control reg\n");
536                 return ret;
537         }
538
539         if (reg & PALMA_SMPS10_VSEL)
540                 return 1;
541
542         return 0;
543 }
544
545 static int palmas_set_voltage_smps10_sel(struct regulator_dev *dev,
546                 unsigned selector)
547 {
548         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
549         int id = rdev_get_id(dev);
550         unsigned int reg;
551         int ret;
552
553         ret = palmas_smps_read(pmic->palmas,
554                                 palmas_regs_info[id].ctrl_addr, &reg);
555         if (ret < 0) {
556                 dev_err(pmic->palmas->dev,
557                         "Error in reading smps10 control reg\n");
558                 return ret;
559         }
560
561         if (selector)
562                 reg |= PALMA_SMPS10_VSEL;
563         else
564                 reg &= ~PALMA_SMPS10_VSEL;
565
566         /* Enable boost mode */
567         reg |= PALMA_SMPS10_BOOST_EN;
568
569         ret = palmas_smps_write(pmic->palmas,
570                                 palmas_regs_info[id].ctrl_addr, reg);
571         if (ret < 0)
572                 dev_err(pmic->palmas->dev,
573                         "Error in writing smps10 control reg\n");
574         return ret;
575 }
576
577 static int palmas_list_voltage_smps10(struct regulator_dev *dev,
578                                         unsigned selector)
579 {
580         return 3750000 + (selector * 1250000);
581 }
582
583 static struct regulator_ops palmas_ops_smps10 = {
584         .is_enabled             = palmas_is_enabled_smps10,
585         .enable                 = palmas_enable_smps10,
586         .disable                = palmas_disable_smps10,
587         .get_voltage_sel        = palmas_get_voltage_smps10_sel,
588         .set_voltage_sel        = palmas_set_voltage_smps10_sel,
589         .list_voltage           = palmas_list_voltage_smps10,
590 };
591
592 static int palmas_is_enabled_ldo(struct regulator_dev *dev)
593 {
594         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
595         int id = rdev_get_id(dev);
596         unsigned int reg;
597
598         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
599
600         reg &= PALMAS_LDO1_CTRL_STATUS;
601
602         return !!(reg);
603 }
604
605 static int palmas_enable_ldo(struct regulator_dev *dev)
606 {
607         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
608         int id = rdev_get_id(dev);
609         unsigned int reg;
610
611         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
612
613         reg |= SMPS_CTRL_MODE_ON;
614
615         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
616
617         return 0;
618 }
619
620 static int palmas_disable_ldo(struct regulator_dev *dev)
621 {
622         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
623         int id = rdev_get_id(dev);
624         unsigned int reg;
625
626         palmas_ldo_read(pmic->palmas, palmas_regs_info[id].ctrl_addr, &reg);
627
628         reg &= ~SMPS_CTRL_MODE_ON;
629
630         palmas_ldo_write(pmic->palmas, palmas_regs_info[id].ctrl_addr, reg);
631
632         return 0;
633 }
634
635 static int palmas_list_voltage_ldo(struct regulator_dev *dev,
636                                         unsigned selector)
637 {
638         if (!selector)
639                 return 0;
640
641         /* voltage is 0.85V + (selector * 0.05v) */
642         return  850000 + (selector * 50000);
643 }
644
645 static int palmas_get_voltage_ldo_sel(struct regulator_dev *dev)
646 {
647         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
648         int id = rdev_get_id(dev);
649         int selector;
650         unsigned int reg;
651         unsigned int addr;
652
653         addr = palmas_regs_info[id].vsel_addr;
654
655         palmas_ldo_read(pmic->palmas, addr, &reg);
656
657         selector = reg & PALMAS_LDO1_VOLTAGE_VSEL_MASK;
658
659         /* Adjust selector to match list_voltage ranges */
660         if (selector > 49)
661                 selector = 49;
662
663         return selector;
664 }
665
666 static int palmas_set_voltage_ldo_sel(struct regulator_dev *dev,
667                 unsigned selector)
668 {
669         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
670         int id = rdev_get_id(dev);
671         unsigned int reg = 0;
672         unsigned int addr;
673
674         addr = palmas_regs_info[id].vsel_addr;
675
676         reg = selector;
677
678         palmas_ldo_write(pmic->palmas, addr, reg);
679
680         return 0;
681 }
682
683 static struct regulator_ops palmas_ops_ldo = {
684         .is_enabled             = palmas_is_enabled_ldo,
685         .enable                 = palmas_enable_ldo,
686         .disable                = palmas_disable_ldo,
687         .get_voltage_sel        = palmas_get_voltage_ldo_sel,
688         .set_voltage_sel        = palmas_set_voltage_ldo_sel,
689         .list_voltage           = palmas_list_voltage_ldo,
690 };
691
692 static int palmas_is_enabled_extreg(struct regulator_dev *dev)
693 {
694         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
695         int id = rdev_get_id(dev);
696         unsigned int reg;
697         int ret;
698
699         ret = palmas_resource_read(pmic->palmas,
700                         palmas_regs_info[id].ctrl_addr, &reg);
701         reg &= PALMAS_REGEN1_CTRL_STATUS;
702         if (ret < 0)
703                 return ret;
704
705         return !!(reg);
706 }
707
708 static int palmas_enable_extreg(struct regulator_dev *dev)
709 {
710         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
711         int id = rdev_get_id(dev);
712         unsigned int reg;
713         int ret;
714
715         ret = palmas_resource_read(pmic->palmas,
716                         palmas_regs_info[id].ctrl_addr, &reg);
717         if (ret < 0)
718                 return ret;
719
720         reg |= PALMAS_REGEN1_CTRL_MODE_ACTIVE;
721         ret = palmas_resource_write(pmic->palmas,
722                         palmas_regs_info[id].ctrl_addr, reg);
723         return ret;
724 }
725
726 static int palmas_disable_extreg(struct regulator_dev *dev)
727 {
728         struct palmas_pmic *pmic = rdev_get_drvdata(dev);
729         int id = rdev_get_id(dev);
730         unsigned int reg;
731         int ret;
732
733         ret = palmas_resource_read(pmic->palmas,
734                         palmas_regs_info[id].ctrl_addr, &reg);
735         if (ret < 0)
736                 return ret;
737
738         reg &= ~PALMAS_REGEN1_CTRL_MODE_ACTIVE;
739         ret = palmas_resource_write(pmic->palmas,
740                         palmas_regs_info[id].ctrl_addr, reg);
741         return ret;
742 }
743
744 static int palmas_getvoltage_extreg(struct regulator_dev *rdev)
745 {
746         return 4300 * 1000;
747 }
748
749
750 static struct regulator_ops palmas_ops_extreg = {
751         .is_enabled             = palmas_is_enabled_extreg,
752         .enable                 = palmas_enable_extreg,
753         .disable                = palmas_disable_extreg,
754         .get_voltage            = palmas_getvoltage_extreg,
755 };
756
757 /*
758  * setup the hardware based sleep configuration of the SMPS/LDO regulators
759  * from the platform data. This is different to the software based control
760  * supported by the regulator framework as it is controlled by toggling
761  * pins on the PMIC such as PREQ, SYSEN, ...
762  */
763 static int palmas_smps_init(struct palmas *palmas, int id,
764                 struct palmas_reg_init *reg_init)
765 {
766         unsigned int reg;
767         unsigned int addr;
768         int ret;
769
770         addr = palmas_regs_info[id].ctrl_addr;
771
772         ret = palmas_smps_read(palmas, addr, &reg);
773         if (ret)
774                 return ret;
775
776         if (id != PALMAS_REG_SMPS10) {
777                 if (reg_init->warm_reset)
778                         reg |= PALMAS_SMPS12_CTRL_WR_S;
779
780                 if (reg_init->roof_floor)
781                         reg |= PALMAS_SMPS12_CTRL_ROOF_FLOOR_EN;
782
783                 reg &= ~PALMAS_SMPS12_CTRL_MODE_SLEEP_MASK;
784                 if (reg_init->mode_sleep)
785                         reg |= reg_init->mode_sleep <<
786                                         PALMAS_SMPS12_CTRL_MODE_SLEEP_SHIFT;
787         } else {
788                 if (reg_init->mode_sleep) {
789                         reg &= ~PALMAS_SMPS10_CTRL_MODE_SLEEP_MASK;
790                         reg |= reg_init->mode_sleep <<
791                                         PALMAS_SMPS10_CTRL_MODE_SLEEP_SHIFT;
792                 }
793
794         }
795         ret = palmas_smps_write(palmas, addr, reg);
796         if (ret)
797                 return ret;
798
799         if ((id != PALMAS_REG_SMPS10) && reg_init->roof_floor) {
800                 int sleep_id = palmas_regs_info[id].sleep_id;
801                 ret = palmas_ext_power_req_config(palmas, sleep_id,
802                                         reg_init->roof_floor, true);
803                 if (ret < 0) {
804                         dev_err(palmas->dev,
805                                 "Error in configuring external control\n");
806                         return ret;
807                 }
808         }
809
810         if (palmas_regs_info[id].tstep_addr && reg_init->tstep) {
811                 addr = palmas_regs_info[id].tstep_addr;
812
813                 reg = reg_init->tstep & PALMAS_SMPS12_TSTEP_TSTEP_MASK;
814
815                 ret = palmas_smps_write(palmas, addr, reg);
816                 if (ret)
817                         return ret;
818         }
819
820         if (palmas_regs_info[id].vsel_addr && reg_init->vsel) {
821                 addr = palmas_regs_info[id].vsel_addr;
822
823                 reg = reg_init->vsel;
824
825                 ret = palmas_smps_write(palmas, addr, reg);
826                 if (ret)
827                         return ret;
828         }
829
830
831         return 0;
832 }
833
834 static int palmas_ldo_init(struct palmas *palmas, int id,
835                 struct palmas_reg_init *reg_init)
836 {
837         unsigned int reg;
838         unsigned int addr;
839         int ret;
840
841         addr = palmas_regs_info[id].ctrl_addr;
842
843         ret = palmas_ldo_read(palmas, addr, &reg);
844         if (ret)
845                 return ret;
846
847         if (reg_init->warm_reset)
848                 reg |= PALMAS_LDO1_CTRL_WR_S;
849
850         if (reg_init->mode_sleep)
851                 reg |= PALMAS_LDO1_CTRL_MODE_SLEEP;
852         else
853                 reg &= ~PALMAS_LDO1_CTRL_MODE_SLEEP;
854
855         ret = palmas_ldo_write(palmas, addr, reg);
856         if (ret)
857                 return ret;
858
859         if (reg_init->roof_floor) {
860                 int sleep_id = palmas_regs_info[id].sleep_id;
861
862                 ret = palmas_ext_power_req_config(palmas, sleep_id,
863                         reg_init->roof_floor, true);
864                 if (ret < 0) {
865                         dev_err(palmas->dev,
866                                 "Error in configuring external control\n");
867                         return ret;
868                 }
869         }
870
871         return 0;
872 }
873
874 static int palmas_extreg_init(struct palmas *palmas, int id,
875                 struct palmas_reg_init *reg_init)
876 {
877         unsigned int reg;
878         unsigned int addr;
879         int ret;
880
881         addr = palmas_regs_info[id].ctrl_addr;
882
883         ret = palmas_resource_read(palmas, addr, &reg);
884         if (ret)
885                 return ret;
886
887         if (reg_init->mode_sleep)
888                 reg |= PALMAS_REGEN1_CTRL_MODE_SLEEP;
889         else
890                 reg &= ~PALMAS_REGEN1_CTRL_MODE_SLEEP;
891
892         ret = palmas_resource_write(palmas, addr, reg);
893         if (ret < 0)
894                 return ret;
895
896         if (reg_init->roof_floor) {
897                 int sleep_id = palmas_regs_info[id].sleep_id;
898
899                 ret = palmas_ext_power_req_config(palmas, sleep_id,
900                         reg_init->roof_floor, true);
901                 if (ret < 0) {
902                         dev_err(palmas->dev,
903                                 "Error in configuring external control\n");
904                         return ret;
905                 }
906         }
907         return 0;
908 }
909
910 static void palmas_enable_ldo8_track(struct palmas *palmas)
911 {
912         unsigned int reg;
913         unsigned int addr;
914         int ret;
915
916         addr = palmas_regs_info[PALMAS_REG_LDO8].ctrl_addr;
917
918         ret = palmas_ldo_read(palmas, addr, &reg);
919         if (ret) {
920                 dev_err(palmas->dev, "Error in reading ldo8 control reg\n");
921                 return;
922         }
923
924         reg |= PALMAS_LDO8_CTRL_LDO_TRACKING_EN;
925         ret = palmas_ldo_write(palmas, addr, reg);
926         if (ret < 0) {
927                 dev_err(palmas->dev, "Error in enabling tracking mode\n");
928                 return;
929         }
930         /*
931          * When SMPS4&5 is set to off and LDO8 tracking is enabled, the LDO8
932          * output is defined by the LDO8_VOLTAGE.VSEL register divided by two,
933          * and can be set from 0.45 to 1.65 V.
934          */
935         addr = palmas_regs_info[PALMAS_REG_LDO8].vsel_addr;
936         ret = palmas_ldo_read(palmas, addr, &reg);
937         if (ret) {
938                 dev_err(palmas->dev, "Error in reading ldo8 voltage reg\n");
939                 return;
940         }
941
942         reg = (reg << 1) & PALMAS_LDO8_VOLTAGE_VSEL_MASK;
943         ret = palmas_ldo_write(palmas, addr, reg);
944         if (ret < 0)
945                 dev_err(palmas->dev, "Error in setting ldo8 voltage reg\n");
946
947         return;
948 }
949
950 static __devinit int palmas_probe(struct platform_device *pdev)
951 {
952         struct palmas *palmas = dev_get_drvdata(pdev->dev.parent);
953         struct palmas_pmic_platform_data *pdata = pdev->dev.platform_data;
954         struct regulator_dev *rdev;
955         struct palmas_pmic *pmic;
956         struct palmas_reg_init *reg_init;
957         struct regulator_init_data *reg_data;
958         int id = 0, ret;
959         unsigned int addr, reg;
960
961         if (!pdata)
962                 return -EINVAL;
963         if (!pdata->reg_data)
964                 return -EINVAL;
965
966         pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
967         if (!pmic)
968                 return -ENOMEM;
969
970         pmic->dev = &pdev->dev;
971         pmic->palmas = palmas;
972         palmas->pmic = pmic;
973         platform_set_drvdata(pdev, pmic);
974
975         ret = palmas_smps_read(palmas, PALMAS_SMPS_CTRL, &reg);
976         if (ret)
977                 goto err_unregister_regulator;
978
979         if (reg & PALMAS_SMPS_CTRL_SMPS12_SMPS123_EN)
980                 pmic->smps123 = 1;
981
982         if (reg & PALMAS_SMPS_CTRL_SMPS45_SMPS457_EN)
983                 pmic->smps457 = 1;
984
985         for (id = 0; id < PALMAS_REG_LDO1; id++) {
986                 reg_data = pdata->reg_data[id];
987
988                 /*
989                  * Miss out regulators which are not available due
990                  * to slaving configurations.
991                  */
992                 switch (id) {
993                 case PALMAS_REG_SMPS12:
994                 case PALMAS_REG_SMPS3:
995                         if (pmic->smps123)
996                                 continue;
997                         break;
998                 case PALMAS_REG_SMPS123:
999                         if (!pmic->smps123)
1000                                 continue;
1001                         break;
1002                 case PALMAS_REG_SMPS45:
1003                 case PALMAS_REG_SMPS7:
1004                         if (pmic->smps457)
1005                                 continue;
1006                         break;
1007                 case PALMAS_REG_SMPS457:
1008                         if (!pmic->smps457)
1009                                 continue;
1010                 }
1011
1012                 /* Register the regulators */
1013                 pmic->desc[id].name = palmas_regs_info[id].name;
1014                 pmic->desc[id].id = id;
1015
1016                 if (id != PALMAS_REG_SMPS10) {
1017                         pmic->desc[id].ops = &palmas_ops_smps;
1018                         pmic->desc[id].n_voltages = PALMAS_SMPS_NUM_VOLTAGES;
1019                 } else {
1020                         pmic->desc[id].n_voltages = PALMAS_SMPS10_NUM_VOLTAGES;
1021                         pmic->desc[id].ops = &palmas_ops_smps10;
1022                         pmic->desc[id].vsel_reg = PALMAS_SMPS10_CTRL;
1023                         pmic->desc[id].vsel_mask = SMPS10_VSEL;
1024                         pmic->desc[id].enable_reg = PALMAS_SMPS10_STATUS;
1025                         pmic->desc[id].enable_mask = SMPS10_BOOST_EN;
1026                 }
1027
1028                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1029                 pmic->desc[id].owner = THIS_MODULE;
1030
1031                 /* Initialise sleep/init values from platform data */
1032                 if (pdata && pdata->reg_init) {
1033                         reg_init = pdata->reg_init[id];
1034                         if (reg_init) {
1035                                 ret = palmas_smps_init(palmas, id, reg_init);
1036                                 if (ret)
1037                                         goto err_unregister_regulator;
1038                         }
1039                 }
1040
1041                 /*
1042                  * read and store the RANGE bit for later use
1043                  * This must be done before regulator is probed otherwise
1044                  * we error in probe with unsuportable ranges.
1045                  */
1046                 if (id != PALMAS_REG_SMPS10) {
1047                         addr = palmas_regs_info[id].vsel_addr;
1048
1049                         ret = palmas_smps_read(pmic->palmas, addr, &reg);
1050                         if (ret)
1051                                 goto err_unregister_regulator;
1052                         if (reg & PALMAS_SMPS12_VOLTAGE_RANGE)
1053                                 pmic->range[id] = 1;
1054                 }
1055
1056                 rdev = regulator_register(&pmic->desc[id],
1057                         palmas->dev, reg_data, pmic, NULL);
1058
1059                 if (IS_ERR(rdev)) {
1060                         dev_err(&pdev->dev,
1061                                 "failed to register %s regulator\n",
1062                                 pdev->name);
1063                         ret = PTR_ERR(rdev);
1064                         goto err_unregister_regulator;
1065                 }
1066
1067                 /* Save regulator for cleanup */
1068                 pmic->rdev[id] = rdev;
1069         }
1070
1071         /* Start this loop from the id left from previous loop */
1072         for (; id < PALMAS_NUM_REGS; id++) {
1073
1074                 reg_data = pdata->reg_data[id];
1075                 /* Miss out regulators which are not available due
1076                  * to alternate functions.
1077                  */
1078
1079                 /* Register the regulators */
1080                 pmic->desc[id].name = palmas_regs_info[id].name;
1081                 pmic->desc[id].id = id;
1082                 pmic->desc[id].type = REGULATOR_VOLTAGE;
1083                 pmic->desc[id].owner = THIS_MODULE;
1084
1085                 if (id < PALMAS_REG_REGEN1) {
1086                         pmic->desc[id].n_voltages = PALMAS_LDO_NUM_VOLTAGES;
1087                         pmic->desc[id].ops = &palmas_ops_ldo;
1088                         pmic->desc[id].enable_reg =
1089                                         palmas_regs_info[id].ctrl_addr;
1090                         pmic->desc[id].enable_mask =
1091                                         PALMAS_LDO1_CTRL_MODE_ACTIVE;
1092                 } else {
1093                         pmic->desc[id].n_voltages = 1;
1094                         pmic->desc[id].ops = &palmas_ops_extreg;
1095                 }
1096
1097                 rdev = regulator_register(&pmic->desc[id],
1098                         palmas->dev, reg_data, pmic, NULL);
1099
1100                 if (IS_ERR(rdev)) {
1101                         dev_err(&pdev->dev,
1102                                 "failed to register %s regulator\n",
1103                                 pdev->name);
1104                         ret = PTR_ERR(rdev);
1105                         goto err_unregister_regulator;
1106                 }
1107
1108                 /* Save regulator for cleanup */
1109                 pmic->rdev[id] = rdev;
1110
1111                 /* Initialise sleep/init values from platform data */
1112                 if (pdata->reg_init) {
1113                         reg_init = pdata->reg_init[id];
1114                         if (reg_init) {
1115                                 if (id < PALMAS_REG_REGEN1)
1116                                         ret = palmas_ldo_init(palmas, id,
1117                                                                 reg_init);
1118                                 else
1119                                         ret = palmas_extreg_init(palmas, id,
1120                                                                 reg_init);
1121                                 if (ret)
1122                                         goto err_unregister_regulator;
1123                         }
1124                 }
1125         }
1126
1127         /* Check if LDO8 is in tracking mode or not */
1128         if (pdata->enable_ldo8_tracking)
1129                 palmas_enable_ldo8_track(palmas);
1130
1131         return 0;
1132
1133 err_unregister_regulator:
1134         while (--id >= 0)
1135                 regulator_unregister(pmic->rdev[id]);
1136         kfree(pmic->rdev);
1137         kfree(pmic->desc);
1138         kfree(pmic);
1139         return ret;
1140 }
1141
1142 static int __devexit palmas_remove(struct platform_device *pdev)
1143 {
1144         struct palmas_pmic *pmic = platform_get_drvdata(pdev);
1145         int id;
1146
1147         for (id = 0; id < PALMAS_NUM_REGS; id++)
1148                 regulator_unregister(pmic->rdev[id]);
1149
1150         kfree(pmic->rdev);
1151         kfree(pmic->desc);
1152         kfree(pmic);
1153         return 0;
1154 }
1155
1156 static struct platform_driver palmas_driver = {
1157         .driver = {
1158                 .name = "palmas-pmic",
1159                 .owner = THIS_MODULE,
1160         },
1161         .probe = palmas_probe,
1162         .remove = __devexit_p(palmas_remove),
1163 };
1164
1165 static int __init palmas_init(void)
1166 {
1167         return platform_driver_register(&palmas_driver);
1168 }
1169 subsys_initcall(palmas_init);
1170
1171 static void __exit palmas_exit(void)
1172 {
1173         platform_driver_unregister(&palmas_driver);
1174 }
1175 module_exit(palmas_exit);
1176
1177 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1178 MODULE_DESCRIPTION("Palmas voltage regulator driver");
1179 MODULE_LICENSE("GPL");
1180 MODULE_ALIAS("platform:palmas-pmic");