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