regulator: tps65910: Provide settling time for enabling rails
[linux-2.6.git] / drivers / regulator / tps65910-regulator.c
1 /*
2  * tps65910.c  --  TI tps65910
3  *
4  * Copyright 2010 Texas Instruments Inc.
5  *
6  * Author: Graeme Gregory <gg@slimlogic.co.uk>
7  * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
8  *
9  *  This program is free software; you can redistribute it and/or modify it
10  *  under  the terms of the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/tps65910.h>
27
28 #define TPS65910_SUPPLY_STATE_ENABLED   0x1
29 #define EXT_SLEEP_CONTROL (TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1 |       \
30                         TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2 |          \
31                         TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3 |          \
32                         TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
33
34 /* supported VIO voltages in milivolts */
35 static const u16 VIO_VSEL_table[] = {
36         1500, 1800, 2500, 3300,
37 };
38
39 /* VSEL tables for TPS65910 specific LDOs and dcdc's */
40
41 /* supported VDD3 voltages in milivolts */
42 static const u16 VDD3_VSEL_table[] = {
43         5000,
44 };
45
46 /* supported VDIG1 voltages in milivolts */
47 static const u16 VDIG1_VSEL_table[] = {
48         1200, 1500, 1800, 2700,
49 };
50
51 /* supported VDIG2 voltages in milivolts */
52 static const u16 VDIG2_VSEL_table[] = {
53         1000, 1100, 1200, 1800,
54 };
55
56 /* supported VPLL voltages in milivolts */
57 static const u16 VPLL_VSEL_table[] = {
58         1000, 1100, 1800, 2500,
59 };
60
61 /* supported VDAC voltages in milivolts */
62 static const u16 VDAC_VSEL_table[] = {
63         1800, 2600, 2800, 2850,
64 };
65
66 /* supported VAUX1 voltages in milivolts */
67 static const u16 VAUX1_VSEL_table[] = {
68         1800, 2500, 2800, 2850,
69 };
70
71 /* supported VAUX2 voltages in milivolts */
72 static const u16 VAUX2_VSEL_table[] = {
73         1800, 2800, 2900, 3300,
74 };
75
76 /* supported VAUX33 voltages in milivolts */
77 static const u16 VAUX33_VSEL_table[] = {
78         1800, 2000, 2800, 3300,
79 };
80
81 /* supported VMMC voltages in milivolts */
82 static const u16 VMMC_VSEL_table[] = {
83         1800, 2800, 3000, 3300,
84 };
85
86 struct tps_info {
87         const char *name;
88         unsigned min_uV;
89         unsigned max_uV;
90         u8 n_voltages;
91         const u16 *voltage_table;
92         int enable_time_us;
93 };
94
95 static struct tps_info tps65910_regs[] = {
96         {
97                 .name = "VRTC",
98                 .enable_time_us = 2200,
99         },
100         {
101                 .name = "VIO",
102                 .min_uV = 1500000,
103                 .max_uV = 3300000,
104                 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
105                 .voltage_table = VIO_VSEL_table,
106                 .enable_time_us = 350,
107         },
108         {
109                 .name = "VDD1",
110                 .min_uV = 600000,
111                 .max_uV = 4500000,
112                 .enable_time_us = 350,
113         },
114         {
115                 .name = "VDD2",
116                 .min_uV = 600000,
117                 .max_uV = 4500000,
118                 .enable_time_us = 350,
119         },
120         {
121                 .name = "VDD3",
122                 .min_uV = 5000000,
123                 .max_uV = 5000000,
124                 .n_voltages = ARRAY_SIZE(VDD3_VSEL_table),
125                 .voltage_table = VDD3_VSEL_table,
126                 .enable_time_us = 200,
127         },
128         {
129                 .name = "VDIG1",
130                 .min_uV = 1200000,
131                 .max_uV = 2700000,
132                 .n_voltages = ARRAY_SIZE(VDIG1_VSEL_table),
133                 .voltage_table = VDIG1_VSEL_table,
134                 .enable_time_us = 100,
135         },
136         {
137                 .name = "VDIG2",
138                 .min_uV = 1000000,
139                 .max_uV = 1800000,
140                 .n_voltages = ARRAY_SIZE(VDIG2_VSEL_table),
141                 .voltage_table = VDIG2_VSEL_table,
142                 .enable_time_us = 100,
143         },
144         {
145                 .name = "VPLL",
146                 .min_uV = 1000000,
147                 .max_uV = 2500000,
148                 .n_voltages = ARRAY_SIZE(VPLL_VSEL_table),
149                 .voltage_table = VPLL_VSEL_table,
150                 .enable_time_us = 100,
151         },
152         {
153                 .name = "VDAC",
154                 .min_uV = 1800000,
155                 .max_uV = 2850000,
156                 .n_voltages = ARRAY_SIZE(VDAC_VSEL_table),
157                 .voltage_table = VDAC_VSEL_table,
158                 .enable_time_us = 100,
159         },
160         {
161                 .name = "VAUX1",
162                 .min_uV = 1800000,
163                 .max_uV = 2850000,
164                 .n_voltages = ARRAY_SIZE(VAUX1_VSEL_table),
165                 .voltage_table = VAUX1_VSEL_table,
166                 .enable_time_us = 100,
167         },
168         {
169                 .name = "VAUX2",
170                 .min_uV = 1800000,
171                 .max_uV = 3300000,
172                 .n_voltages = ARRAY_SIZE(VAUX2_VSEL_table),
173                 .voltage_table = VAUX2_VSEL_table,
174                 .enable_time_us = 100,
175         },
176         {
177                 .name = "VAUX33",
178                 .min_uV = 1800000,
179                 .max_uV = 3300000,
180                 .n_voltages = ARRAY_SIZE(VAUX33_VSEL_table),
181                 .voltage_table = VAUX33_VSEL_table,
182                 .enable_time_us = 100,
183         },
184         {
185                 .name = "VMMC",
186                 .min_uV = 1800000,
187                 .max_uV = 3300000,
188                 .n_voltages = ARRAY_SIZE(VMMC_VSEL_table),
189                 .voltage_table = VMMC_VSEL_table,
190                 .enable_time_us = 100,
191         },
192 };
193
194 static struct tps_info tps65911_regs[] = {
195         {
196                 .name = "VRTC",
197                 .enable_time_us = 2200,
198         },
199         {
200                 .name = "VIO",
201                 .min_uV = 1500000,
202                 .max_uV = 3300000,
203                 .n_voltages = ARRAY_SIZE(VIO_VSEL_table),
204                 .voltage_table = VIO_VSEL_table,
205                 .enable_time_us = 350,
206         },
207         {
208                 .name = "VDD1",
209                 .min_uV = 600000,
210                 .max_uV = 4500000,
211                 .n_voltages = 73,
212                 .enable_time_us = 350,
213         },
214         {
215                 .name = "VDD2",
216                 .min_uV = 600000,
217                 .max_uV = 4500000,
218                 .n_voltages = 73,
219                 .enable_time_us = 350,
220         },
221         {
222                 .name = "VDDCTRL",
223                 .min_uV = 600000,
224                 .max_uV = 1400000,
225                 .n_voltages = 65,
226                 .enable_time_us = 900,
227         },
228         {
229                 .name = "LDO1",
230                 .min_uV = 1000000,
231                 .max_uV = 3300000,
232                 .n_voltages = 47,
233                 .enable_time_us = 420,
234         },
235         {
236                 .name = "LDO2",
237                 .min_uV = 1000000,
238                 .max_uV = 3300000,
239                 .n_voltages = 47,
240                 .enable_time_us = 420,
241         },
242         {
243                 .name = "LDO3",
244                 .min_uV = 1000000,
245                 .max_uV = 3300000,
246                 .n_voltages = 24,
247                 .enable_time_us = 230,
248         },
249         {
250                 .name = "LDO4",
251                 .min_uV = 1000000,
252                 .max_uV = 3300000,
253                 .n_voltages = 47,
254                 .enable_time_us = 230,
255         },
256         {
257                 .name = "LDO5",
258                 .min_uV = 1000000,
259                 .max_uV = 3300000,
260                 .n_voltages = 24,
261                 .enable_time_us = 230,
262         },
263         {
264                 .name = "LDO6",
265                 .min_uV = 1000000,
266                 .max_uV = 3300000,
267                 .n_voltages = 24,
268                 .enable_time_us = 230,
269         },
270         {
271                 .name = "LDO7",
272                 .min_uV = 1000000,
273                 .max_uV = 3300000,
274                 .n_voltages = 24,
275                 .enable_time_us = 230,
276         },
277         {
278                 .name = "LDO8",
279                 .min_uV = 1000000,
280                 .max_uV = 3300000,
281                 .n_voltages = 24,
282                 .enable_time_us = 230,
283         },
284 };
285
286 #define EXT_CONTROL_REG_BITS(id, regs_offs, bits) (((regs_offs) << 8) | (bits))
287 static unsigned int tps65910_ext_sleep_control[] = {
288         0,
289         EXT_CONTROL_REG_BITS(VIO,    1, 0),
290         EXT_CONTROL_REG_BITS(VDD1,   1, 1),
291         EXT_CONTROL_REG_BITS(VDD2,   1, 2),
292         EXT_CONTROL_REG_BITS(VDD3,   1, 3),
293         EXT_CONTROL_REG_BITS(VDIG1,  0, 1),
294         EXT_CONTROL_REG_BITS(VDIG2,  0, 2),
295         EXT_CONTROL_REG_BITS(VPLL,   0, 6),
296         EXT_CONTROL_REG_BITS(VDAC,   0, 7),
297         EXT_CONTROL_REG_BITS(VAUX1,  0, 3),
298         EXT_CONTROL_REG_BITS(VAUX2,  0, 4),
299         EXT_CONTROL_REG_BITS(VAUX33, 0, 5),
300         EXT_CONTROL_REG_BITS(VMMC,   0, 0),
301 };
302
303 static unsigned int tps65911_ext_sleep_control[] = {
304         0,
305         EXT_CONTROL_REG_BITS(VIO,     1, 0),
306         EXT_CONTROL_REG_BITS(VDD1,    1, 1),
307         EXT_CONTROL_REG_BITS(VDD2,    1, 2),
308         EXT_CONTROL_REG_BITS(VDDCTRL, 1, 3),
309         EXT_CONTROL_REG_BITS(LDO1,    0, 1),
310         EXT_CONTROL_REG_BITS(LDO2,    0, 2),
311         EXT_CONTROL_REG_BITS(LDO3,    0, 7),
312         EXT_CONTROL_REG_BITS(LDO4,    0, 6),
313         EXT_CONTROL_REG_BITS(LDO5,    0, 3),
314         EXT_CONTROL_REG_BITS(LDO6,    0, 0),
315         EXT_CONTROL_REG_BITS(LDO7,    0, 5),
316         EXT_CONTROL_REG_BITS(LDO8,    0, 4),
317 };
318
319 struct tps65910_reg {
320         struct regulator_desc *desc;
321         struct tps65910 *mfd;
322         struct regulator_dev **rdev;
323         struct tps_info **info;
324         struct mutex mutex;
325         int num_regulators;
326         int mode;
327         int  (*get_ctrl_reg)(int);
328         unsigned int *ext_sleep_control;
329         unsigned int board_ext_control[TPS65910_NUM_REGS];
330 };
331
332 static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
333 {
334         u8 val;
335         int err;
336
337         err = pmic->mfd->read(pmic->mfd, reg, 1, &val);
338         if (err)
339                 return err;
340
341         return val;
342 }
343
344 static inline int tps65910_write(struct tps65910_reg *pmic, u8 reg, u8 val)
345 {
346         return pmic->mfd->write(pmic->mfd, reg, 1, &val);
347 }
348
349 static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
350                                         u8 set_mask, u8 clear_mask)
351 {
352         int err, data;
353
354         mutex_lock(&pmic->mutex);
355
356         data = tps65910_read(pmic, reg);
357         if (data < 0) {
358                 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
359                 err = data;
360                 goto out;
361         }
362
363         data &= ~clear_mask;
364         data |= set_mask;
365         err = tps65910_write(pmic, reg, data);
366         if (err)
367                 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
368
369 out:
370         mutex_unlock(&pmic->mutex);
371         return err;
372 }
373
374 static int tps65910_reg_read(struct tps65910_reg *pmic, u8 reg)
375 {
376         int data;
377
378         mutex_lock(&pmic->mutex);
379
380         data = tps65910_read(pmic, reg);
381         if (data < 0)
382                 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
383
384         mutex_unlock(&pmic->mutex);
385         return data;
386 }
387
388 static int tps65910_reg_write(struct tps65910_reg *pmic, u8 reg, u8 val)
389 {
390         int err;
391
392         mutex_lock(&pmic->mutex);
393
394         err = tps65910_write(pmic, reg, val);
395         if (err < 0)
396                 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
397
398         mutex_unlock(&pmic->mutex);
399         return err;
400 }
401
402 static int tps65910_get_ctrl_register(int id)
403 {
404         switch (id) {
405         case TPS65910_REG_VRTC:
406                 return TPS65910_VRTC;
407         case TPS65910_REG_VIO:
408                 return TPS65910_VIO;
409         case TPS65910_REG_VDD1:
410                 return TPS65910_VDD1;
411         case TPS65910_REG_VDD2:
412                 return TPS65910_VDD2;
413         case TPS65910_REG_VDD3:
414                 return TPS65910_VDD3;
415         case TPS65910_REG_VDIG1:
416                 return TPS65910_VDIG1;
417         case TPS65910_REG_VDIG2:
418                 return TPS65910_VDIG2;
419         case TPS65910_REG_VPLL:
420                 return TPS65910_VPLL;
421         case TPS65910_REG_VDAC:
422                 return TPS65910_VDAC;
423         case TPS65910_REG_VAUX1:
424                 return TPS65910_VAUX1;
425         case TPS65910_REG_VAUX2:
426                 return TPS65910_VAUX2;
427         case TPS65910_REG_VAUX33:
428                 return TPS65910_VAUX33;
429         case TPS65910_REG_VMMC:
430                 return TPS65910_VMMC;
431         default:
432                 return -EINVAL;
433         }
434 }
435
436 static int tps65911_get_ctrl_register(int id)
437 {
438         switch (id) {
439         case TPS65910_REG_VRTC:
440                 return TPS65910_VRTC;
441         case TPS65910_REG_VIO:
442                 return TPS65910_VIO;
443         case TPS65910_REG_VDD1:
444                 return TPS65910_VDD1;
445         case TPS65910_REG_VDD2:
446                 return TPS65910_VDD2;
447         case TPS65911_REG_VDDCTRL:
448                 return TPS65911_VDDCTRL;
449         case TPS65911_REG_LDO1:
450                 return TPS65911_LDO1;
451         case TPS65911_REG_LDO2:
452                 return TPS65911_LDO2;
453         case TPS65911_REG_LDO3:
454                 return TPS65911_LDO3;
455         case TPS65911_REG_LDO4:
456                 return TPS65911_LDO4;
457         case TPS65911_REG_LDO5:
458                 return TPS65911_LDO5;
459         case TPS65911_REG_LDO6:
460                 return TPS65911_LDO6;
461         case TPS65911_REG_LDO7:
462                 return TPS65911_LDO7;
463         case TPS65911_REG_LDO8:
464                 return TPS65911_LDO8;
465         default:
466                 return -EINVAL;
467         }
468 }
469
470 static int tps65910_is_enabled(struct regulator_dev *dev)
471 {
472         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
473         int reg, value, id = rdev_get_id(dev);
474
475         reg = pmic->get_ctrl_reg(id);
476         if (reg < 0)
477                 return reg;
478
479         value = tps65910_reg_read(pmic, reg);
480         if (value < 0)
481                 return value;
482
483         return value & TPS65910_SUPPLY_STATE_ENABLED;
484 }
485
486 static int tps65910_enable(struct regulator_dev *dev)
487 {
488         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
489         struct tps65910 *mfd = pmic->mfd;
490         int reg, id = rdev_get_id(dev);
491
492         reg = pmic->get_ctrl_reg(id);
493         if (reg < 0)
494                 return reg;
495
496         return tps65910_set_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
497 }
498
499 static int tps65910_disable(struct regulator_dev *dev)
500 {
501         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
502         struct tps65910 *mfd = pmic->mfd;
503         int reg, id = rdev_get_id(dev);
504
505         reg = pmic->get_ctrl_reg(id);
506         if (reg < 0)
507                 return reg;
508
509         return tps65910_clear_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
510 }
511
512 static int tps65910_enable_time(struct regulator_dev *dev)
513 {
514         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
515         int id = rdev_get_id(dev);
516         return pmic->info[id]->enable_time_us;
517 }
518
519 static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
520 {
521         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
522         struct tps65910 *mfd = pmic->mfd;
523         int reg, value, id = rdev_get_id(dev);
524
525         reg = pmic->get_ctrl_reg(id);
526         if (reg < 0)
527                 return reg;
528
529         switch (mode) {
530         case REGULATOR_MODE_NORMAL:
531                 return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
532                                                         LDO_ST_MODE_BIT);
533         case REGULATOR_MODE_IDLE:
534                 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
535                 return tps65910_set_bits(mfd, reg, value);
536         case REGULATOR_MODE_STANDBY:
537                 return tps65910_clear_bits(mfd, reg, LDO_ST_ON_BIT);
538         }
539
540         return -EINVAL;
541 }
542
543 static unsigned int tps65910_get_mode(struct regulator_dev *dev)
544 {
545         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
546         int reg, value, id = rdev_get_id(dev);
547
548         reg = pmic->get_ctrl_reg(id);
549         if (reg < 0)
550                 return reg;
551
552         value = tps65910_reg_read(pmic, reg);
553         if (value < 0)
554                 return value;
555
556         if (!(value & LDO_ST_ON_BIT))
557                 return REGULATOR_MODE_STANDBY;
558         else if (value & LDO_ST_MODE_BIT)
559                 return REGULATOR_MODE_IDLE;
560         else
561                 return REGULATOR_MODE_NORMAL;
562 }
563
564 static int tps65910_get_voltage_dcdc(struct regulator_dev *dev)
565 {
566         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
567         int id = rdev_get_id(dev), voltage = 0;
568         int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
569
570         switch (id) {
571         case TPS65910_REG_VDD1:
572                 opvsel = tps65910_reg_read(pmic, TPS65910_VDD1_OP);
573                 mult = tps65910_reg_read(pmic, TPS65910_VDD1);
574                 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
575                 srvsel = tps65910_reg_read(pmic, TPS65910_VDD1_SR);
576                 sr = opvsel & VDD1_OP_CMD_MASK;
577                 opvsel &= VDD1_OP_SEL_MASK;
578                 srvsel &= VDD1_SR_SEL_MASK;
579                 vselmax = 75;
580                 break;
581         case TPS65910_REG_VDD2:
582                 opvsel = tps65910_reg_read(pmic, TPS65910_VDD2_OP);
583                 mult = tps65910_reg_read(pmic, TPS65910_VDD2);
584                 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
585                 srvsel = tps65910_reg_read(pmic, TPS65910_VDD2_SR);
586                 sr = opvsel & VDD2_OP_CMD_MASK;
587                 opvsel &= VDD2_OP_SEL_MASK;
588                 srvsel &= VDD2_SR_SEL_MASK;
589                 vselmax = 75;
590                 break;
591         case TPS65911_REG_VDDCTRL:
592                 opvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_OP);
593                 srvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_SR);
594                 sr = opvsel & VDDCTRL_OP_CMD_MASK;
595                 opvsel &= VDDCTRL_OP_SEL_MASK;
596                 srvsel &= VDDCTRL_SR_SEL_MASK;
597                 vselmax = 64;
598                 break;
599         }
600
601         /* multiplier 0 == 1 but 2,3 normal */
602         if (!mult)
603                 mult=1;
604
605         if (sr) {
606                 /* normalise to valid range */
607                 if (srvsel < 3)
608                         srvsel = 3;
609                 if (srvsel > vselmax)
610                         srvsel = vselmax;
611                 srvsel -= 3;
612
613                 voltage = (srvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
614         } else {
615
616                 /* normalise to valid range*/
617                 if (opvsel < 3)
618                         opvsel = 3;
619                 if (opvsel > vselmax)
620                         opvsel = vselmax;
621                 opvsel -= 3;
622
623                 voltage = (opvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
624         }
625
626         voltage *= mult;
627
628         return voltage;
629 }
630
631 static int tps65910_get_voltage(struct regulator_dev *dev)
632 {
633         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
634         int reg, value, id = rdev_get_id(dev), voltage = 0;
635
636         reg = pmic->get_ctrl_reg(id);
637         if (reg < 0)
638                 return reg;
639
640         value = tps65910_reg_read(pmic, reg);
641         if (value < 0)
642                 return value;
643
644         switch (id) {
645         case TPS65910_REG_VIO:
646         case TPS65910_REG_VDIG1:
647         case TPS65910_REG_VDIG2:
648         case TPS65910_REG_VPLL:
649         case TPS65910_REG_VDAC:
650         case TPS65910_REG_VAUX1:
651         case TPS65910_REG_VAUX2:
652         case TPS65910_REG_VAUX33:
653         case TPS65910_REG_VMMC:
654                 value &= LDO_SEL_MASK;
655                 value >>= LDO_SEL_SHIFT;
656                 break;
657         default:
658                 return -EINVAL;
659         }
660
661         voltage = pmic->info[id]->voltage_table[value] * 1000;
662
663         return voltage;
664 }
665
666 static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
667 {
668         return 5 * 1000 * 1000;
669 }
670
671 static int tps65911_get_voltage(struct regulator_dev *dev)
672 {
673         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
674         int step_mv, id = rdev_get_id(dev);
675         u8 value, reg;
676
677         reg = pmic->get_ctrl_reg(id);
678
679         value = tps65910_reg_read(pmic, reg);
680
681         switch (id) {
682         case TPS65911_REG_LDO1:
683         case TPS65911_REG_LDO2:
684         case TPS65911_REG_LDO4:
685                 value &= LDO1_SEL_MASK;
686                 value >>= LDO_SEL_SHIFT;
687                 /* The first 5 values of the selector correspond to 1V */
688                 if (value < 5)
689                         value = 0;
690                 else
691                         value -= 4;
692
693                 step_mv = 50;
694                 break;
695         case TPS65911_REG_LDO3:
696         case TPS65911_REG_LDO5:
697         case TPS65911_REG_LDO6:
698         case TPS65911_REG_LDO7:
699         case TPS65911_REG_LDO8:
700                 value &= LDO3_SEL_MASK;
701                 value >>= LDO_SEL_SHIFT;
702                 /* The first 3 values of the selector correspond to 1V */
703                 if (value < 3)
704                         value = 0;
705                 else
706                         value -= 2;
707
708                 step_mv = 100;
709                 break;
710         case TPS65910_REG_VIO:
711                 value &= LDO_SEL_MASK;
712                 value >>= LDO_SEL_SHIFT;
713                 return pmic->info[id]->voltage_table[value] * 1000;
714         default:
715                 return -EINVAL;
716         }
717
718         return (LDO_MIN_VOLT + value * step_mv) * 1000;
719 }
720
721 static int tps65910_set_voltage_dcdc_sel(struct regulator_dev *dev,
722                                          unsigned selector)
723 {
724         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
725         int id = rdev_get_id(dev), vsel;
726         int dcdc_mult = 0;
727
728         switch (id) {
729         case TPS65910_REG_VDD1:
730                 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
731                 if (dcdc_mult == 1)
732                         dcdc_mult--;
733                 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
734
735                 tps65910_modify_bits(pmic, TPS65910_VDD1,
736                                 (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
737                                                 VDD1_VGAIN_SEL_MASK);
738                 tps65910_reg_write(pmic, TPS65910_VDD1_OP, vsel);
739                 break;
740         case TPS65910_REG_VDD2:
741                 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
742                 if (dcdc_mult == 1)
743                         dcdc_mult--;
744                 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
745
746                 tps65910_modify_bits(pmic, TPS65910_VDD2,
747                                 (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
748                                                 VDD1_VGAIN_SEL_MASK);
749                 tps65910_reg_write(pmic, TPS65910_VDD2_OP, vsel);
750                 break;
751         case TPS65911_REG_VDDCTRL:
752                 vsel = selector + 3;
753                 tps65910_reg_write(pmic, TPS65911_VDDCTRL_OP, vsel);
754         }
755
756         return 0;
757 }
758
759 static int tps65910_set_voltage_sel(struct regulator_dev *dev,
760                                     unsigned selector)
761 {
762         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
763         int reg, id = rdev_get_id(dev);
764
765         reg = pmic->get_ctrl_reg(id);
766         if (reg < 0)
767                 return reg;
768
769         switch (id) {
770         case TPS65910_REG_VIO:
771         case TPS65910_REG_VDIG1:
772         case TPS65910_REG_VDIG2:
773         case TPS65910_REG_VPLL:
774         case TPS65910_REG_VDAC:
775         case TPS65910_REG_VAUX1:
776         case TPS65910_REG_VAUX2:
777         case TPS65910_REG_VAUX33:
778         case TPS65910_REG_VMMC:
779                 return tps65910_modify_bits(pmic, reg,
780                                 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
781         }
782
783         return -EINVAL;
784 }
785
786 static int tps65911_set_voltage_sel(struct regulator_dev *dev,
787                                     unsigned selector)
788 {
789         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
790         int reg, id = rdev_get_id(dev);
791
792         reg = pmic->get_ctrl_reg(id);
793         if (reg < 0)
794                 return reg;
795
796         switch (id) {
797         case TPS65911_REG_LDO1:
798         case TPS65911_REG_LDO2:
799         case TPS65911_REG_LDO4:
800                 return tps65910_modify_bits(pmic, reg,
801                                 (selector << LDO_SEL_SHIFT), LDO1_SEL_MASK);
802         case TPS65911_REG_LDO3:
803         case TPS65911_REG_LDO5:
804         case TPS65911_REG_LDO6:
805         case TPS65911_REG_LDO7:
806         case TPS65911_REG_LDO8:
807                 return tps65910_modify_bits(pmic, reg,
808                                 (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
809         case TPS65910_REG_VIO:
810                 return tps65910_modify_bits(pmic, reg,
811                                 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
812         }
813
814         return -EINVAL;
815 }
816
817
818 static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
819                                         unsigned selector)
820 {
821         int volt, mult = 1, id = rdev_get_id(dev);
822
823         switch (id) {
824         case TPS65910_REG_VDD1:
825         case TPS65910_REG_VDD2:
826                 mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
827                 volt = VDD1_2_MIN_VOLT +
828                                 (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
829                 break;
830         case TPS65911_REG_VDDCTRL:
831                 volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
832                 break;
833         default:
834                 BUG();
835                 return -EINVAL;
836         }
837
838         return  volt * 100 * mult;
839 }
840
841 static int tps65910_list_voltage(struct regulator_dev *dev,
842                                         unsigned selector)
843 {
844         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
845         int id = rdev_get_id(dev), voltage;
846
847         if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
848                 return -EINVAL;
849
850         if (selector >= pmic->info[id]->n_voltages)
851                 return -EINVAL;
852         else
853                 voltage = pmic->info[id]->voltage_table[selector] * 1000;
854
855         return voltage;
856 }
857
858 static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
859 {
860         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
861         int step_mv = 0, id = rdev_get_id(dev);
862
863         switch(id) {
864         case TPS65911_REG_LDO1:
865         case TPS65911_REG_LDO2:
866         case TPS65911_REG_LDO4:
867                 /* The first 5 values of the selector correspond to 1V */
868                 if (selector < 5)
869                         selector = 0;
870                 else
871                         selector -= 4;
872
873                 step_mv = 50;
874                 break;
875         case TPS65911_REG_LDO3:
876         case TPS65911_REG_LDO5:
877         case TPS65911_REG_LDO6:
878         case TPS65911_REG_LDO7:
879         case TPS65911_REG_LDO8:
880                 /* The first 3 values of the selector correspond to 1V */
881                 if (selector < 3)
882                         selector = 0;
883                 else
884                         selector -= 2;
885
886                 step_mv = 100;
887                 break;
888         case TPS65910_REG_VIO:
889                 return pmic->info[id]->voltage_table[selector] * 1000;
890         default:
891                 return -EINVAL;
892         }
893
894         return (LDO_MIN_VOLT + selector * step_mv) * 1000;
895 }
896
897 /* Regulator ops (except VRTC) */
898 static struct regulator_ops tps65910_ops_dcdc = {
899         .is_enabled             = tps65910_is_enabled,
900         .enable                 = tps65910_enable,
901         .disable                = tps65910_disable,
902         .enable_time            = tps65910_enable_time,
903         .set_mode               = tps65910_set_mode,
904         .get_mode               = tps65910_get_mode,
905         .get_voltage            = tps65910_get_voltage_dcdc,
906         .set_voltage_sel        = tps65910_set_voltage_dcdc_sel,
907         .list_voltage           = tps65910_list_voltage_dcdc,
908 };
909
910 static struct regulator_ops tps65910_ops_vdd3 = {
911         .is_enabled             = tps65910_is_enabled,
912         .enable                 = tps65910_enable,
913         .disable                = tps65910_disable,
914         .enable_time            = tps65910_enable_time,
915         .set_mode               = tps65910_set_mode,
916         .get_mode               = tps65910_get_mode,
917         .get_voltage            = tps65910_get_voltage_vdd3,
918         .list_voltage           = tps65910_list_voltage,
919 };
920
921 static struct regulator_ops tps65910_ops = {
922         .is_enabled             = tps65910_is_enabled,
923         .enable                 = tps65910_enable,
924         .disable                = tps65910_disable,
925         .enable_time            = tps65910_enable_time,
926         .set_mode               = tps65910_set_mode,
927         .get_mode               = tps65910_get_mode,
928         .get_voltage            = tps65910_get_voltage,
929         .set_voltage_sel        = tps65910_set_voltage_sel,
930         .list_voltage           = tps65910_list_voltage,
931 };
932
933 static struct regulator_ops tps65911_ops = {
934         .is_enabled             = tps65910_is_enabled,
935         .enable                 = tps65910_enable,
936         .disable                = tps65910_disable,
937         .enable_time            = tps65910_enable_time,
938         .set_mode               = tps65910_set_mode,
939         .get_mode               = tps65910_get_mode,
940         .get_voltage            = tps65911_get_voltage,
941         .set_voltage_sel        = tps65911_set_voltage_sel,
942         .list_voltage           = tps65911_list_voltage,
943 };
944
945 static int tps65910_set_ext_sleep_config(struct tps65910_reg *pmic,
946                 int id, int ext_sleep_config)
947 {
948         struct tps65910 *mfd = pmic->mfd;
949         u8 regoffs = (pmic->ext_sleep_control[id] >> 8) & 0xFF;
950         u8 bit_pos = (1 << pmic->ext_sleep_control[id] & 0xFF);
951         int ret;
952
953         /*
954          * Regulator can not be control from multiple external input EN1, EN2
955          * and EN3 together.
956          */
957         if (ext_sleep_config & EXT_SLEEP_CONTROL) {
958                 int en_count;
959                 en_count = ((ext_sleep_config &
960                                 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1) != 0);
961                 en_count += ((ext_sleep_config &
962                                 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2) != 0);
963                 en_count += ((ext_sleep_config &
964                                 TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3) != 0);
965                 en_count += ((ext_sleep_config &
966                                 TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP) != 0);
967                 if (en_count > 1) {
968                         dev_err(mfd->dev,
969                                 "External sleep control flag is not proper\n");
970                         return -EINVAL;
971                 }
972         }
973
974         pmic->board_ext_control[id] = ext_sleep_config;
975
976         /* External EN1 control */
977         if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN1)
978                 ret = tps65910_set_bits(mfd,
979                                 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
980         else
981                 ret = tps65910_clear_bits(mfd,
982                                 TPS65910_EN1_LDO_ASS + regoffs, bit_pos);
983         if (ret < 0) {
984                 dev_err(mfd->dev,
985                         "Error in configuring external control EN1\n");
986                 return ret;
987         }
988
989         /* External EN2 control */
990         if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN2)
991                 ret = tps65910_set_bits(mfd,
992                                 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
993         else
994                 ret = tps65910_clear_bits(mfd,
995                                 TPS65910_EN2_LDO_ASS + regoffs, bit_pos);
996         if (ret < 0) {
997                 dev_err(mfd->dev,
998                         "Error in configuring external control EN2\n");
999                 return ret;
1000         }
1001
1002         /* External EN3 control for TPS65910 LDO only */
1003         if ((tps65910_chip_id(mfd) == TPS65910) &&
1004                         (id >= TPS65910_REG_VDIG1)) {
1005                 if (ext_sleep_config & TPS65910_SLEEP_CONTROL_EXT_INPUT_EN3)
1006                         ret = tps65910_set_bits(mfd,
1007                                 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
1008                 else
1009                         ret = tps65910_clear_bits(mfd,
1010                                 TPS65910_EN3_LDO_ASS + regoffs, bit_pos);
1011                 if (ret < 0) {
1012                         dev_err(mfd->dev,
1013                                 "Error in configuring external control EN3\n");
1014                         return ret;
1015                 }
1016         }
1017
1018         /* Return if no external control is selected */
1019         if (!(ext_sleep_config & EXT_SLEEP_CONTROL)) {
1020                 /* Clear all sleep controls */
1021                 ret = tps65910_clear_bits(mfd,
1022                         TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
1023                 if (!ret)
1024                         ret = tps65910_clear_bits(mfd,
1025                                 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
1026                 if (ret < 0)
1027                         dev_err(mfd->dev,
1028                                 "Error in configuring SLEEP register\n");
1029                 return ret;
1030         }
1031
1032         /*
1033          * For regulator that has separate operational and sleep register make
1034          * sure that operational is used and clear sleep register to turn
1035          * regulator off when external control is inactive
1036          */
1037         if ((id == TPS65910_REG_VDD1) ||
1038                 (id == TPS65910_REG_VDD2) ||
1039                         ((id == TPS65911_REG_VDDCTRL) &&
1040                                 (tps65910_chip_id(mfd) == TPS65911))) {
1041                 int op_reg_add = pmic->get_ctrl_reg(id) + 1;
1042                 int sr_reg_add = pmic->get_ctrl_reg(id) + 2;
1043                 int opvsel = tps65910_reg_read(pmic, op_reg_add);
1044                 int srvsel = tps65910_reg_read(pmic, sr_reg_add);
1045                 if (opvsel & VDD1_OP_CMD_MASK) {
1046                         u8 reg_val = srvsel & VDD1_OP_SEL_MASK;
1047                         ret = tps65910_reg_write(pmic, op_reg_add, reg_val);
1048                         if (ret < 0) {
1049                                 dev_err(mfd->dev,
1050                                         "Error in configuring op register\n");
1051                                 return ret;
1052                         }
1053                 }
1054                 ret = tps65910_reg_write(pmic, sr_reg_add, 0);
1055                 if (ret < 0) {
1056                         dev_err(mfd->dev, "Error in settting sr register\n");
1057                         return ret;
1058                 }
1059         }
1060
1061         ret = tps65910_clear_bits(mfd,
1062                         TPS65910_SLEEP_KEEP_LDO_ON + regoffs, bit_pos);
1063         if (!ret) {
1064                 if (ext_sleep_config & TPS65911_SLEEP_CONTROL_EXT_INPUT_SLEEP)
1065                         ret = tps65910_set_bits(mfd,
1066                                 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
1067                 else
1068                         ret = tps65910_clear_bits(mfd,
1069                                 TPS65910_SLEEP_SET_LDO_OFF + regoffs, bit_pos);
1070         }
1071         if (ret < 0)
1072                 dev_err(mfd->dev,
1073                         "Error in configuring SLEEP register\n");
1074
1075         return ret;
1076 }
1077
1078 static __devinit int tps65910_probe(struct platform_device *pdev)
1079 {
1080         struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
1081         struct tps_info *info;
1082         struct regulator_init_data *reg_data;
1083         struct regulator_dev *rdev;
1084         struct tps65910_reg *pmic;
1085         struct tps65910_board *pmic_plat_data;
1086         int i, err;
1087
1088         pmic_plat_data = dev_get_platdata(tps65910->dev);
1089         if (!pmic_plat_data)
1090                 return -EINVAL;
1091
1092         pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
1093         if (!pmic)
1094                 return -ENOMEM;
1095
1096         mutex_init(&pmic->mutex);
1097         pmic->mfd = tps65910;
1098         platform_set_drvdata(pdev, pmic);
1099
1100         /* Give control of all register to control port */
1101         tps65910_set_bits(pmic->mfd, TPS65910_DEVCTRL,
1102                                 DEVCTRL_SR_CTL_I2C_SEL_MASK);
1103
1104         switch(tps65910_chip_id(tps65910)) {
1105         case TPS65910:
1106                 pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
1107                 pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
1108                 pmic->ext_sleep_control = tps65910_ext_sleep_control;
1109                 info = tps65910_regs;
1110                 break;
1111         case TPS65911:
1112                 pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
1113                 pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
1114                 pmic->ext_sleep_control = tps65911_ext_sleep_control;
1115                 info = tps65911_regs;
1116                 break;
1117         default:
1118                 pr_err("Invalid tps chip version\n");
1119                 kfree(pmic);
1120                 return -ENODEV;
1121         }
1122
1123         pmic->desc = kcalloc(pmic->num_regulators,
1124                         sizeof(struct regulator_desc), GFP_KERNEL);
1125         if (!pmic->desc) {
1126                 err = -ENOMEM;
1127                 goto err_free_pmic;
1128         }
1129
1130         pmic->info = kcalloc(pmic->num_regulators,
1131                         sizeof(struct tps_info *), GFP_KERNEL);
1132         if (!pmic->info) {
1133                 err = -ENOMEM;
1134                 goto err_free_desc;
1135         }
1136
1137         pmic->rdev = kcalloc(pmic->num_regulators,
1138                         sizeof(struct regulator_dev *), GFP_KERNEL);
1139         if (!pmic->rdev) {
1140                 err = -ENOMEM;
1141                 goto err_free_info;
1142         }
1143
1144         for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
1145                         i++, info++) {
1146
1147                 reg_data = pmic_plat_data->tps65910_pmic_init_data[i];
1148
1149                 /* Regulator API handles empty constraints but not NULL
1150                  * constraints */
1151                 if (!reg_data)
1152                         continue;
1153
1154                 /* Register the regulators */
1155                 pmic->info[i] = info;
1156
1157                 pmic->desc[i].name = info->name;
1158                 pmic->desc[i].id = i;
1159                 pmic->desc[i].n_voltages = info->n_voltages;
1160
1161                 if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
1162                         pmic->desc[i].ops = &tps65910_ops_dcdc;
1163                         pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
1164                                                         VDD1_2_NUM_VOLT_COARSE;
1165                 } else if (i == TPS65910_REG_VDD3) {
1166                         if (tps65910_chip_id(tps65910) == TPS65910)
1167                                 pmic->desc[i].ops = &tps65910_ops_vdd3;
1168                         else
1169                                 pmic->desc[i].ops = &tps65910_ops_dcdc;
1170                 } else {
1171                         if (tps65910_chip_id(tps65910) == TPS65910)
1172                                 pmic->desc[i].ops = &tps65910_ops;
1173                         else
1174                                 pmic->desc[i].ops = &tps65911_ops;
1175                 }
1176
1177                 err = tps65910_set_ext_sleep_config(pmic, i,
1178                                 pmic_plat_data->regulator_ext_sleep_control[i]);
1179                 /*
1180                  * Failing on regulator for configuring externally control
1181                  * is not a serious issue, just throw warning.
1182                  */
1183                 if (err < 0)
1184                         dev_warn(tps65910->dev,
1185                                 "Failed to initialise ext control config\n");
1186
1187                 pmic->desc[i].type = REGULATOR_VOLTAGE;
1188                 pmic->desc[i].owner = THIS_MODULE;
1189
1190                 rdev = regulator_register(&pmic->desc[i],
1191                                 tps65910->dev, reg_data, pmic);
1192                 if (IS_ERR(rdev)) {
1193                         dev_err(tps65910->dev,
1194                                 "failed to register %s regulator\n",
1195                                 pdev->name);
1196                         err = PTR_ERR(rdev);
1197                         goto err_unregister_regulator;
1198                 }
1199
1200                 /* Save regulator for cleanup */
1201                 pmic->rdev[i] = rdev;
1202         }
1203         return 0;
1204
1205 err_unregister_regulator:
1206         while (--i >= 0)
1207                 regulator_unregister(pmic->rdev[i]);
1208         kfree(pmic->rdev);
1209 err_free_info:
1210         kfree(pmic->info);
1211 err_free_desc:
1212         kfree(pmic->desc);
1213 err_free_pmic:
1214         kfree(pmic);
1215         return err;
1216 }
1217
1218 static int __devexit tps65910_remove(struct platform_device *pdev)
1219 {
1220         struct tps65910_reg *pmic = platform_get_drvdata(pdev);
1221         int i;
1222
1223         for (i = 0; i < pmic->num_regulators; i++)
1224                 regulator_unregister(pmic->rdev[i]);
1225
1226         kfree(pmic->rdev);
1227         kfree(pmic->info);
1228         kfree(pmic->desc);
1229         kfree(pmic);
1230         return 0;
1231 }
1232
1233 static void tps65910_shutdown(struct platform_device *pdev)
1234 {
1235         struct tps65910_reg *pmic = platform_get_drvdata(pdev);
1236         int i;
1237
1238         /*
1239          * Before bootloader jumps to kernel, it makes sure that required
1240          * external control signals are in desired state so that given rails
1241          * can be configure accordingly.
1242          * If rails are configured to be controlled from external control
1243          * then before shutting down/rebooting the system, the external
1244          * control configuration need to be remove from the rails so that
1245          * its output will be available as per register programming even
1246          * if external controls are removed. This is require when the POR
1247          * value of the control signals are not in active state and before
1248          * bootloader initializes it, the system requires the rail output
1249          * to be active for booting.
1250          */
1251         for (i = 0; i < pmic->num_regulators; i++) {
1252                 int err;
1253                 if (!pmic->rdev[i])
1254                         continue;
1255
1256                 err = tps65910_set_ext_sleep_config(pmic, i, 0);
1257                 if (err < 0)
1258                         dev_err(&pdev->dev,
1259                                 "Error in clearing external control\n");
1260         }
1261 }
1262
1263 static struct platform_driver tps65910_driver = {
1264         .driver = {
1265                 .name = "tps65910-pmic",
1266                 .owner = THIS_MODULE,
1267         },
1268         .probe = tps65910_probe,
1269         .remove = __devexit_p(tps65910_remove),
1270         .shutdown = tps65910_shutdown,
1271 };
1272
1273 static int __init tps65910_init(void)
1274 {
1275         return platform_driver_register(&tps65910_driver);
1276 }
1277 subsys_initcall(tps65910_init);
1278
1279 static void __exit tps65910_cleanup(void)
1280 {
1281         platform_driver_unregister(&tps65910_driver);
1282 }
1283 module_exit(tps65910_cleanup);
1284
1285 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1286 MODULE_DESCRIPTION("TPS65910/TPS65911 voltage regulator driver");
1287 MODULE_LICENSE("GPL v2");
1288 MODULE_ALIAS("platform:tps65910-pmic");