]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - sitara-epos/sitara-epos-kernel.git/blob - drivers/regulator/tps65910-regulator.c
regulator: TPS65910: VDD1 error handling
[sitara-epos/sitara-epos-kernel.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  */
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>
28 #define TPS65910_SUPPLY_STATE_ENABLED   0x1
30 /* supported VIO voltages in milivolts */
31 static const u16 VIO_VSEL_table[] = {
32         1500, 1800, 2500, 3300,
33 };
35 /* VSEL tables for TPS65910 specific LDOs and dcdc's */
37 /* supported VDD3 voltages in milivolts */
38 static const u16 VDD3_VSEL_table[] = {
39         5000,
40 };
42 /* supported VDIG1 voltages in milivolts */
43 static const u16 VDIG1_VSEL_table[] = {
44         1200, 1500, 1800, 2700,
45 };
47 /* supported VDIG2 voltages in milivolts */
48 static const u16 VDIG2_VSEL_table[] = {
49         1000, 1100, 1200, 1800,
50 };
52 /* supported VPLL voltages in milivolts */
53 static const u16 VPLL_VSEL_table[] = {
54         1000, 1100, 1800, 2500,
55 };
57 /* supported VDAC voltages in milivolts */
58 static const u16 VDAC_VSEL_table[] = {
59         1800, 2600, 2800, 2850,
60 };
62 /* supported VAUX1 voltages in milivolts */
63 static const u16 VAUX1_VSEL_table[] = {
64         1800, 2500, 2800, 2850,
65 };
67 /* supported VAUX2 voltages in milivolts */
68 static const u16 VAUX2_VSEL_table[] = {
69         1800, 2800, 2900, 3300,
70 };
72 /* supported VAUX33 voltages in milivolts */
73 static const u16 VAUX33_VSEL_table[] = {
74         1800, 2000, 2800, 3300,
75 };
77 /* supported VMMC voltages in milivolts */
78 static const u16 VMMC_VSEL_table[] = {
79         1800, 2800, 3000, 3300,
80 };
82 struct tps_info {
83         const char *name;
84         unsigned min_uV;
85         unsigned max_uV;
86         u8 table_len;
87         const u16 *table;
88 };
90 static struct tps_info tps65910_regs[] = {
91         {
92                 .name = "VRTC",
93         },
94         {
95                 .name = "VIO",
96                 .min_uV = 1500000,
97                 .max_uV = 3300000,
98                 .table_len = ARRAY_SIZE(VIO_VSEL_table),
99                 .table = VIO_VSEL_table,
100         },
101         {
102                 .name = "VDD1",
103                 .min_uV = 600000,
104                 .max_uV = 4500000,
105         },
106         {
107                 .name = "VDD2",
108                 .min_uV = 600000,
109                 .max_uV = 4500000,
110         },
111         {
112                 .name = "VDD3",
113                 .min_uV = 5000000,
114                 .max_uV = 5000000,
115                 .table_len = ARRAY_SIZE(VDD3_VSEL_table),
116                 .table = VDD3_VSEL_table,
117         },
118         {
119                 .name = "VDIG1",
120                 .min_uV = 1200000,
121                 .max_uV = 2700000,
122                 .table_len = ARRAY_SIZE(VDIG1_VSEL_table),
123                 .table = VDIG1_VSEL_table,
124         },
125         {
126                 .name = "VDIG2",
127                 .min_uV = 1000000,
128                 .max_uV = 1800000,
129                 .table_len = ARRAY_SIZE(VDIG2_VSEL_table),
130                 .table = VDIG2_VSEL_table,
131         },
132         {
133                 .name = "VPLL",
134                 .min_uV = 1000000,
135                 .max_uV = 2500000,
136                 .table_len = ARRAY_SIZE(VPLL_VSEL_table),
137                 .table = VPLL_VSEL_table,
138         },
139         {
140                 .name = "VDAC",
141                 .min_uV = 1800000,
142                 .max_uV = 2850000,
143                 .table_len = ARRAY_SIZE(VDAC_VSEL_table),
144                 .table = VDAC_VSEL_table,
145         },
146         {
147                 .name = "VAUX1",
148                 .min_uV = 1800000,
149                 .max_uV = 2850000,
150                 .table_len = ARRAY_SIZE(VAUX1_VSEL_table),
151                 .table = VAUX1_VSEL_table,
152         },
153         {
154                 .name = "VAUX2",
155                 .min_uV = 1800000,
156                 .max_uV = 3300000,
157                 .table_len = ARRAY_SIZE(VAUX2_VSEL_table),
158                 .table = VAUX2_VSEL_table,
159         },
160         {
161                 .name = "VAUX33",
162                 .min_uV = 1800000,
163                 .max_uV = 3300000,
164                 .table_len = ARRAY_SIZE(VAUX33_VSEL_table),
165                 .table = VAUX33_VSEL_table,
166         },
167         {
168                 .name = "VMMC",
169                 .min_uV = 1800000,
170                 .max_uV = 3300000,
171                 .table_len = ARRAY_SIZE(VMMC_VSEL_table),
172                 .table = VMMC_VSEL_table,
173         },
174 };
176 static struct tps_info tps65911_regs[] = {
177         {
178                 .name = "VIO",
179                 .min_uV = 1500000,
180                 .max_uV = 3300000,
181                 .table_len = ARRAY_SIZE(VIO_VSEL_table),
182                 .table = VIO_VSEL_table,
183         },
184         {
185                 .name = "VDD1",
186                 .min_uV = 600000,
187                 .max_uV = 4500000,
188         },
189         {
190                 .name = "VDD2",
191                 .min_uV = 600000,
192                 .max_uV = 4500000,
193         },
194         {
195                 .name = "VDDCTRL",
196                 .min_uV = 600000,
197                 .max_uV = 1400000,
198         },
199         {
200                 .name = "LDO1",
201                 .min_uV = 1000000,
202                 .max_uV = 3300000,
203         },
204         {
205                 .name = "LDO2",
206                 .min_uV = 1000000,
207                 .max_uV = 3300000,
208         },
209         {
210                 .name = "LDO3",
211                 .min_uV = 1000000,
212                 .max_uV = 3300000,
213         },
214         {
215                 .name = "LDO4",
216                 .min_uV = 1000000,
217                 .max_uV = 3300000,
218         },
219         {
220                 .name = "LDO5",
221                 .min_uV = 1000000,
222                 .max_uV = 3300000,
223         },
224         {
225                 .name = "LDO6",
226                 .min_uV = 1000000,
227                 .max_uV = 3300000,
228         },
229         {
230                 .name = "LDO7",
231                 .min_uV = 1000000,
232                 .max_uV = 3300000,
233         },
234         {
235                 .name = "LDO8",
236                 .min_uV = 1000000,
237                 .max_uV = 3300000,
238         },
239 };
241 struct tps65910_reg {
242         struct regulator_desc *desc;
243         struct tps65910 *mfd;
244         struct regulator_dev **rdev;
245         struct tps_info **info;
246         struct mutex mutex;
247         int num_regulators;
248         int mode;
249         int  (*get_ctrl_reg)(int);
250 };
252 static inline int tps65910_read(struct tps65910_reg *pmic, u8 reg)
254         u8 val;
255         int err;
257         err = pmic->mfd->read(pmic->mfd, reg, 1, &val);
258         if (err)
259                 return err;
261         return val;
264 static inline int tps65910_write(struct tps65910_reg *pmic, u8 reg, u8 val)
266         return pmic->mfd->write(pmic->mfd, reg, 1, &val);
269 static int tps65910_modify_bits(struct tps65910_reg *pmic, u8 reg,
270                                         u8 set_mask, u8 clear_mask)
272         int err, data;
274         mutex_lock(&pmic->mutex);
276         data = tps65910_read(pmic, reg);
277         if (data < 0) {
278                 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
279                 err = data;
280                 goto out;
281         }
283         data &= ~clear_mask;
284         data |= set_mask;
285         err = tps65910_write(pmic, reg, data);
286         if (err)
287                 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
289 out:
290         mutex_unlock(&pmic->mutex);
291         return err;
294 static int tps65910_reg_read(struct tps65910_reg *pmic, u8 reg)
296         int data;
298         mutex_lock(&pmic->mutex);
300         data = tps65910_read(pmic, reg);
301         if (data < 0)
302                 dev_err(pmic->mfd->dev, "Read from reg 0x%x failed\n", reg);
304         mutex_unlock(&pmic->mutex);
305         return data;
308 static int tps65910_reg_write(struct tps65910_reg *pmic, u8 reg, u8 val)
310         int err;
312         mutex_lock(&pmic->mutex);
314         err = tps65910_write(pmic, reg, val);
315         if (err < 0)
316                 dev_err(pmic->mfd->dev, "Write for reg 0x%x failed\n", reg);
318         mutex_unlock(&pmic->mutex);
319         return err;
322 static int tps65910_get_ctrl_register(int id)
324         switch (id) {
325         case TPS65910_REG_VRTC:
326                 return TPS65910_VRTC;
327         case TPS65910_REG_VIO:
328                 return TPS65910_VIO;
329         case TPS65910_REG_VDD1:
330                 return TPS65910_VDD1;
331         case TPS65910_REG_VDD2:
332                 return TPS65910_VDD2;
333         case TPS65910_REG_VDD3:
334                 return TPS65910_VDD3;
335         case TPS65910_REG_VDIG1:
336                 return TPS65910_VDIG1;
337         case TPS65910_REG_VDIG2:
338                 return TPS65910_VDIG2;
339         case TPS65910_REG_VPLL:
340                 return TPS65910_VPLL;
341         case TPS65910_REG_VDAC:
342                 return TPS65910_VDAC;
343         case TPS65910_REG_VAUX1:
344                 return TPS65910_VAUX1;
345         case TPS65910_REG_VAUX2:
346                 return TPS65910_VAUX2;
347         case TPS65910_REG_VAUX33:
348                 return TPS65910_VAUX33;
349         case TPS65910_REG_VMMC:
350                 return TPS65910_VMMC;
351         default:
352                 return -EINVAL;
353         }
356 static int tps65911_get_ctrl_register(int id)
358         switch (id) {
359         case TPS65910_REG_VRTC:
360                 return TPS65910_VRTC;
361         case TPS65910_REG_VIO:
362                 return TPS65910_VIO;
363         case TPS65910_REG_VDD1:
364                 return TPS65910_VDD1;
365         case TPS65910_REG_VDD2:
366                 return TPS65910_VDD2;
367         case TPS65911_REG_VDDCTRL:
368                 return TPS65911_VDDCTRL;
369         case TPS65911_REG_LDO1:
370                 return TPS65911_LDO1;
371         case TPS65911_REG_LDO2:
372                 return TPS65911_LDO2;
373         case TPS65911_REG_LDO3:
374                 return TPS65911_LDO3;
375         case TPS65911_REG_LDO4:
376                 return TPS65911_LDO4;
377         case TPS65911_REG_LDO5:
378                 return TPS65911_LDO5;
379         case TPS65911_REG_LDO6:
380                 return TPS65911_LDO6;
381         case TPS65911_REG_LDO7:
382                 return TPS65911_LDO7;
383         case TPS65911_REG_LDO8:
384                 return TPS65911_LDO8;
385         default:
386                 return -EINVAL;
387         }
390 static int tps65910_is_enabled(struct regulator_dev *dev)
392         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
393         int reg, value, id = rdev_get_id(dev);
395         reg = pmic->get_ctrl_reg(id);
396         if (reg < 0)
397                 return reg;
399         value = tps65910_reg_read(pmic, reg);
400         if (value < 0)
401                 return value;
403         return value & TPS65910_SUPPLY_STATE_ENABLED;
406 static int tps65910_enable(struct regulator_dev *dev)
408         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
409         struct tps65910 *mfd = pmic->mfd;
410         int reg, id = rdev_get_id(dev);
412         reg = pmic->get_ctrl_reg(id);
413         if (reg < 0)
414                 return reg;
416         return tps65910_set_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
419 static int tps65910_disable(struct regulator_dev *dev)
421         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
422         struct tps65910 *mfd = pmic->mfd;
423         int reg, id = rdev_get_id(dev);
425         reg = pmic->get_ctrl_reg(id);
426         if (reg < 0)
427                 return reg;
429         return tps65910_clear_bits(mfd, reg, TPS65910_SUPPLY_STATE_ENABLED);
433 static int tps65910_set_mode(struct regulator_dev *dev, unsigned int mode)
435         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
436         struct tps65910 *mfd = pmic->mfd;
437         int reg, value, id = rdev_get_id(dev);
439         reg = pmic->get_ctrl_reg(id);
440         if (reg < 0)
441                 return reg;
443         switch (mode) {
444         case REGULATOR_MODE_NORMAL:
445                 return tps65910_modify_bits(pmic, reg, LDO_ST_ON_BIT,
446                                                         LDO_ST_MODE_BIT);
447         case REGULATOR_MODE_IDLE:
448                 value = LDO_ST_ON_BIT | LDO_ST_MODE_BIT;
449                 return tps65910_set_bits(mfd, reg, value);
450         case REGULATOR_MODE_STANDBY:
451                 return tps65910_clear_bits(mfd, reg, LDO_ST_ON_BIT);
452         }
454         return -EINVAL;
457 static unsigned int tps65910_get_mode(struct regulator_dev *dev)
459         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
460         int reg, value, id = rdev_get_id(dev);
462         reg = pmic->get_ctrl_reg(id);
463         if (reg < 0)
464                 return reg;
466         value = tps65910_reg_read(pmic, reg);
467         if (value < 0)
468                 return value;
470         if (value & LDO_ST_ON_BIT)
471                 return REGULATOR_MODE_STANDBY;
472         else if (value & LDO_ST_MODE_BIT)
473                 return REGULATOR_MODE_IDLE;
474         else
475                 return REGULATOR_MODE_NORMAL;
478 static int tps65910_get_voltage_dcdc(struct regulator_dev *dev)
480         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
481         int id = rdev_get_id(dev), voltage = 0;
482         int opvsel = 0, srvsel = 0, vselmax = 0, mult = 0, sr = 0;
484         switch (id) {
485         case TPS65910_REG_VDD1:
486                 opvsel = tps65910_reg_read(pmic, TPS65910_VDD1_OP);
487                 if (opvsel < 0)
488                         return opvsel;
489                 mult = tps65910_reg_read(pmic, TPS65910_VDD1);
490                 if (mult < 0)
491                         return mult;
492                 mult = (mult & VDD1_VGAIN_SEL_MASK) >> VDD1_VGAIN_SEL_SHIFT;
493                 srvsel = tps65910_reg_read(pmic, TPS65910_VDD1_SR);
494                 if (srvsel < 0)
495                         return srvsel;
496                 sr = opvsel & VDD1_OP_CMD_MASK;
497                 opvsel &= VDD1_OP_SEL_MASK;
498                 srvsel &= VDD1_SR_SEL_MASK;
499                 vselmax = 75;
500                 break;
501         case TPS65910_REG_VDD2:
502                 opvsel = tps65910_reg_read(pmic, TPS65910_VDD2_OP);
503                 mult = tps65910_reg_read(pmic, TPS65910_VDD2);
504                 mult = (mult & VDD2_VGAIN_SEL_MASK) >> VDD2_VGAIN_SEL_SHIFT;
505                 srvsel = tps65910_reg_read(pmic, TPS65910_VDD2_SR);
506                 sr = opvsel & VDD2_OP_CMD_MASK;
507                 opvsel &= VDD2_OP_SEL_MASK;
508                 srvsel &= VDD2_SR_SEL_MASK;
509                 vselmax = 75;
510                 break;
511         case TPS65911_REG_VDDCTRL:
512                 opvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_OP);
513                 srvsel = tps65910_reg_read(pmic, TPS65911_VDDCTRL_SR);
514                 sr = opvsel & VDDCTRL_OP_CMD_MASK;
515                 opvsel &= VDDCTRL_OP_SEL_MASK;
516                 srvsel &= VDDCTRL_SR_SEL_MASK;
517                 vselmax = 64;
518                 break;
519         }
521         /* multiplier 0 == 1 but 2,3 normal */
522         if (!mult)
523                 mult=1;
525         if (sr) {
526                 /* normalise to valid range */
527                 if (srvsel < 3)
528                         srvsel = 3;
529                 if (srvsel > vselmax)
530                         srvsel = vselmax;
531                 srvsel -= 3;
533                 voltage = (srvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
534         } else {
536                 /* normalise to valid range*/
537                 if (opvsel < 3)
538                         opvsel = 3;
539                 if (opvsel > vselmax)
540                         opvsel = vselmax;
541                 opvsel -= 3;
543                 voltage = (opvsel * VDD1_2_OFFSET + VDD1_2_MIN_VOLT) * 100;
544         }
546         voltage *= mult;
548         return voltage;
551 static int tps65910_get_voltage(struct regulator_dev *dev)
553         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
554         int reg, value, id = rdev_get_id(dev), voltage = 0;
556         reg = pmic->get_ctrl_reg(id);
557         if (reg < 0)
558                 return reg;
560         value = tps65910_reg_read(pmic, reg);
561         if (value < 0)
562                 return value;
564         switch (id) {
565         case TPS65910_REG_VIO:
566         case TPS65910_REG_VDIG1:
567         case TPS65910_REG_VDIG2:
568         case TPS65910_REG_VPLL:
569         case TPS65910_REG_VDAC:
570         case TPS65910_REG_VAUX1:
571         case TPS65910_REG_VAUX2:
572         case TPS65910_REG_VAUX33:
573         case TPS65910_REG_VMMC:
574                 value &= LDO_SEL_MASK;
575                 value >>= LDO_SEL_SHIFT;
576                 break;
577         default:
578                 return -EINVAL;
579         }
581         voltage = pmic->info[id]->table[value] * 1000;
583         return voltage;
586 static int tps65910_get_voltage_vdd3(struct regulator_dev *dev)
588         return 5 * 1000 * 1000;
591 static int tps65911_get_voltage(struct regulator_dev *dev)
593         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
594         int step_mv, id = rdev_get_id(dev);
595         u8 value, reg;
597         reg = pmic->get_ctrl_reg(id);
599         value = tps65910_reg_read(pmic, reg);
601         switch (id) {
602         case TPS65911_REG_LDO1:
603         case TPS65911_REG_LDO2:
604         case TPS65911_REG_LDO4:
605                 value &= LDO1_SEL_MASK;
606                 value >>= LDO_SEL_SHIFT;
607                 /* The first 5 values of the selector correspond to 1V */
608                 if (value < 5)
609                         value = 0;
610                 else
611                         value -= 4;
613                 step_mv = 50;
614                 break;
615         case TPS65911_REG_LDO3:
616         case TPS65911_REG_LDO5:
617         case TPS65911_REG_LDO6:
618         case TPS65911_REG_LDO7:
619         case TPS65911_REG_LDO8:
620                 value &= LDO3_SEL_MASK;
621                 value >>= LDO_SEL_SHIFT;
622                 /* The first 3 values of the selector correspond to 1V */
623                 if (value < 3)
624                         value = 0;
625                 else
626                         value -= 2;
628                 step_mv = 100;
629                 break;
630         case TPS65910_REG_VIO:
631                 return pmic->info[id]->table[value] * 1000;
632                 break;
633         default:
634                 return -EINVAL;
635         }
637         return (LDO_MIN_VOLT + value * step_mv) * 1000;
640 static int tps65910_set_voltage_dcdc(struct regulator_dev *dev,
641                                 unsigned selector)
643         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
644         int id = rdev_get_id(dev), vsel;
645         int dcdc_mult = 0;
646         int ret = 0;
648         switch (id) {
649         case TPS65910_REG_VDD1:
650                 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
651                 if (dcdc_mult == 1)
652                         dcdc_mult--;
653                 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
655                 ret = tps65910_modify_bits(pmic, TPS65910_VDD1,
656                                 (dcdc_mult << VDD1_VGAIN_SEL_SHIFT),
657                                                 VDD1_VGAIN_SEL_MASK);
658                 if (!ret)
659                         ret = tps65910_reg_write(pmic, TPS65910_VDD1_OP, vsel);
660                 break;
661         case TPS65910_REG_VDD2:
662                 dcdc_mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
663                 if (dcdc_mult == 1)
664                         dcdc_mult--;
665                 vsel = (selector % VDD1_2_NUM_VOLT_FINE) + 3;
667                 tps65910_modify_bits(pmic, TPS65910_VDD2,
668                                 (dcdc_mult << VDD2_VGAIN_SEL_SHIFT),
669                                                 VDD1_VGAIN_SEL_MASK);
670                 tps65910_reg_write(pmic, TPS65910_VDD2_OP, vsel);
671                 break;
672         case TPS65911_REG_VDDCTRL:
673                 vsel = selector;
674                 tps65910_reg_write(pmic, TPS65911_VDDCTRL_OP, vsel);
675         }
677         return ret;
680 static int tps65910_set_voltage(struct regulator_dev *dev, unsigned selector)
682         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
683         int reg, id = rdev_get_id(dev);
685         reg = pmic->get_ctrl_reg(id);
686         if (reg < 0)
687                 return reg;
689         switch (id) {
690         case TPS65910_REG_VIO:
691         case TPS65910_REG_VDIG1:
692         case TPS65910_REG_VDIG2:
693         case TPS65910_REG_VPLL:
694         case TPS65910_REG_VDAC:
695         case TPS65910_REG_VAUX1:
696         case TPS65910_REG_VAUX2:
697         case TPS65910_REG_VAUX33:
698         case TPS65910_REG_VMMC:
699                 return tps65910_modify_bits(pmic, reg,
700                                 (selector << LDO_SEL_SHIFT), LDO_SEL_MASK);
701         }
703         return -EINVAL;
706 static int tps65911_set_voltage(struct regulator_dev *dev, unsigned selector)
708         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
709         int reg, id = rdev_get_id(dev);
711         reg = pmic->get_ctrl_reg(id);
712         if (reg < 0)
713                 return reg;
715         switch (id) {
716         case TPS65911_REG_LDO1:
717         case TPS65911_REG_LDO2:
718         case TPS65911_REG_LDO4:
719                 return tps65910_modify_bits(pmic, reg,
720                                 (selector << LDO_SEL_SHIFT), LDO1_SEL_MASK);
721         case TPS65911_REG_LDO3:
722         case TPS65911_REG_LDO5:
723         case TPS65911_REG_LDO6:
724         case TPS65911_REG_LDO7:
725         case TPS65911_REG_LDO8:
726         case TPS65910_REG_VIO:
727                 return tps65910_modify_bits(pmic, reg,
728                                 (selector << LDO_SEL_SHIFT), LDO3_SEL_MASK);
729         }
731         return -EINVAL;
735 static int tps65910_list_voltage_dcdc(struct regulator_dev *dev,
736                                         unsigned selector)
738         int volt, mult = 1, id = rdev_get_id(dev);
740         switch (id) {
741         case TPS65910_REG_VDD1:
742         case TPS65910_REG_VDD2:
743                 mult = (selector / VDD1_2_NUM_VOLT_FINE) + 1;
744                 volt = VDD1_2_MIN_VOLT +
745                                 (selector % VDD1_2_NUM_VOLT_FINE) * VDD1_2_OFFSET;
746                 break;
747         case TPS65911_REG_VDDCTRL:
748                 volt = VDDCTRL_MIN_VOLT + (selector * VDDCTRL_OFFSET);
749                 break;
750         default:
751                 BUG();
752                 return -EINVAL;
753         }
755         return  volt * 100 * mult;
758 static int tps65910_list_voltage(struct regulator_dev *dev,
759                                         unsigned selector)
761         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
762         int id = rdev_get_id(dev), voltage;
764         if (id < TPS65910_REG_VIO || id > TPS65910_REG_VMMC)
765                 return -EINVAL;
767         if (selector >= pmic->info[id]->table_len)
768                 return -EINVAL;
769         else
770                 voltage = pmic->info[id]->table[selector] * 1000;
772         return voltage;
775 static int tps65911_list_voltage(struct regulator_dev *dev, unsigned selector)
777         struct tps65910_reg *pmic = rdev_get_drvdata(dev);
778         int step_mv = 0, id = rdev_get_id(dev);
780         switch(id) {
781         case TPS65911_REG_LDO1:
782         case TPS65911_REG_LDO2:
783         case TPS65911_REG_LDO4:
784                 /* The first 5 values of the selector correspond to 1V */
785                 if (selector < 5)
786                         selector = 0;
787                 else
788                         selector -= 4;
790                 step_mv = 50;
791                 break;
792         case TPS65911_REG_LDO3:
793         case TPS65911_REG_LDO5:
794         case TPS65911_REG_LDO6:
795         case TPS65911_REG_LDO7:
796         case TPS65911_REG_LDO8:
797                 /* The first 3 values of the selector correspond to 1V */
798                 if (selector < 3)
799                         selector = 0;
800                 else
801                         selector -= 2;
803                 step_mv = 100;
804                 break;
805         case TPS65910_REG_VIO:
806                 return pmic->info[id]->table[selector] * 1000;
807         default:
808                 return -EINVAL;
809         }
811         return (LDO_MIN_VOLT + selector * step_mv) * 1000;
814 /* Regulator ops (except VRTC) */
815 static struct regulator_ops tps65910_ops_dcdc = {
816         .is_enabled             = tps65910_is_enabled,
817         .enable                 = tps65910_enable,
818         .disable                = tps65910_disable,
819         .set_mode               = tps65910_set_mode,
820         .get_mode               = tps65910_get_mode,
821         .get_voltage            = tps65910_get_voltage_dcdc,
822         .set_voltage_sel        = tps65910_set_voltage_dcdc,
823         .list_voltage           = tps65910_list_voltage_dcdc,
824 };
826 static struct regulator_ops tps65910_ops_vdd3 = {
827         .is_enabled             = tps65910_is_enabled,
828         .enable                 = tps65910_enable,
829         .disable                = tps65910_disable,
830         .set_mode               = tps65910_set_mode,
831         .get_mode               = tps65910_get_mode,
832         .get_voltage            = tps65910_get_voltage_vdd3,
833         .list_voltage           = tps65910_list_voltage,
834 };
836 static struct regulator_ops tps65910_ops = {
837         .is_enabled             = tps65910_is_enabled,
838         .enable                 = tps65910_enable,
839         .disable                = tps65910_disable,
840         .set_mode               = tps65910_set_mode,
841         .get_mode               = tps65910_get_mode,
842         .get_voltage            = tps65910_get_voltage,
843         .set_voltage_sel        = tps65910_set_voltage,
844         .list_voltage           = tps65910_list_voltage,
845 };
847 static struct regulator_ops tps65911_ops = {
848         .is_enabled             = tps65910_is_enabled,
849         .enable                 = tps65910_enable,
850         .disable                = tps65910_disable,
851         .set_mode               = tps65910_set_mode,
852         .get_mode               = tps65910_get_mode,
853         .get_voltage            = tps65911_get_voltage,
854         .set_voltage_sel        = tps65911_set_voltage,
855         .list_voltage           = tps65911_list_voltage,
856 };
858 static __devinit int tps65910_probe(struct platform_device *pdev)
860         struct tps65910 *tps65910 = dev_get_drvdata(pdev->dev.parent);
861         struct tps_info *info;
862         struct regulator_init_data *reg_data;
863         struct regulator_dev *rdev;
864         struct tps65910_reg *pmic;
865         struct tps65910_board *pmic_plat_data;
866         int i, err;
868         pmic_plat_data = dev_get_platdata(tps65910->dev);
869         if (!pmic_plat_data)
870                 return -EINVAL;
872         pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
873         if (!pmic)
874                 return -ENOMEM;
876         mutex_init(&pmic->mutex);
877         pmic->mfd = tps65910;
878         platform_set_drvdata(pdev, pmic);
880         /* Give control of all register to control port */
881         tps65910_set_bits(pmic->mfd, TPS65910_DEVCTRL,
882                                 DEVCTRL_SR_CTL_I2C_SEL_MASK);
884         switch(tps65910_chip_id(tps65910)) {
885         case TPS65910:
886                 pmic->get_ctrl_reg = &tps65910_get_ctrl_register;
887                 pmic->num_regulators = ARRAY_SIZE(tps65910_regs);
888                 info = tps65910_regs;
889                 break;
890         case TPS65911:
891                 pmic->get_ctrl_reg = &tps65911_get_ctrl_register;
892                 pmic->num_regulators = ARRAY_SIZE(tps65911_regs);
893                 info = tps65911_regs;
894                 break;
895         default:
896                 pr_err("Invalid tps chip version\n");
897                 kfree(pmic);
898                 return -ENODEV;
899         }
901         pmic->desc = kcalloc(pmic->num_regulators,
902                         sizeof(struct regulator_desc), GFP_KERNEL);
903         if (!pmic->desc) {
904                 err = -ENOMEM;
905                 goto err_free_pmic;
906         }
908         pmic->info = kcalloc(pmic->num_regulators,
909                         sizeof(struct tps_info *), GFP_KERNEL);
910         if (!pmic->info) {
911                 err = -ENOMEM;
912                 goto err_free_desc;
913         }
915         pmic->rdev = kcalloc(pmic->num_regulators,
916                         sizeof(struct regulator_dev *), GFP_KERNEL);
917         if (!pmic->rdev) {
918                 err = -ENOMEM;
919                 goto err_free_info;
920         }
922         for (i = 0; i < pmic->num_regulators && i < TPS65910_NUM_REGS;
923                         i++, info++) {
925                 reg_data = pmic_plat_data->tps65910_pmic_init_data[i];
927                 /* Regulator API handles empty constraints but not NULL
928                  * constraints */
929                 if (!reg_data)
930                         continue;
932                 /* Register the regulators */
933                 pmic->info[i] = info;
935                 pmic->desc[i].name = info->name;
936                 pmic->desc[i].id = i;
937                 pmic->desc[i].n_voltages = info->table_len;
939                 if (i == TPS65910_REG_VDD1 || i == TPS65910_REG_VDD2) {
940                         pmic->desc[i].ops = &tps65910_ops_dcdc;
941                         pmic->desc[i].n_voltages = VDD1_2_NUM_VOLT_FINE *
942                                                         VDD1_2_NUM_VOLT_COARSE;
943                 } else if (i == TPS65910_REG_VDD3) {
944                         if (tps65910_chip_id(tps65910) == TPS65910)
945                                 pmic->desc[i].ops = &tps65910_ops_vdd3;
946                         else
947                                 pmic->desc[i].ops = &tps65910_ops_dcdc;
948                 } else {
949                         if (tps65910_chip_id(tps65910) == TPS65910)
950                                 pmic->desc[i].ops = &tps65910_ops;
951                         else
952                                 pmic->desc[i].ops = &tps65911_ops;
953                 }
955                 pmic->desc[i].type = REGULATOR_VOLTAGE;
956                 pmic->desc[i].owner = THIS_MODULE;
958                 rdev = regulator_register(&pmic->desc[i],
959                                 tps65910->dev, reg_data, pmic);
960                 if (IS_ERR(rdev)) {
961                         dev_err(tps65910->dev,
962                                 "failed to register %s regulator\n",
963                                 pdev->name);
964                         err = PTR_ERR(rdev);
965                         goto err_unregister_regulator;
966                 }
968                 /* Save regulator for cleanup */
969                 pmic->rdev[i] = rdev;
970         }
971         return 0;
973 err_unregister_regulator:
974         while (--i >= 0)
975                 regulator_unregister(pmic->rdev[i]);
976         kfree(pmic->rdev);
977 err_free_info:
978         kfree(pmic->info);
979 err_free_desc:
980         kfree(pmic->desc);
981 err_free_pmic:
982         kfree(pmic);
983         return err;
986 static int __devexit tps65910_remove(struct platform_device *pdev)
988         struct tps65910_reg *pmic = platform_get_drvdata(pdev);
989         int i;
991         for (i = 0; i < pmic->num_regulators; i++)
992                 regulator_unregister(pmic->rdev[i]);
994         kfree(pmic->rdev);
995         kfree(pmic->info);
996         kfree(pmic->desc);
997         kfree(pmic);
998         return 0;
1001 static struct platform_driver tps65910_driver = {
1002         .driver = {
1003                 .name = "tps65910-pmic",
1004                 .owner = THIS_MODULE,
1005         },
1006         .probe = tps65910_probe,
1007         .remove = __devexit_p(tps65910_remove),
1008 };
1010 static int __init tps65910_init(void)
1012         return platform_driver_register(&tps65910_driver);
1014 subsys_initcall(tps65910_init);
1016 static void __exit tps65910_cleanup(void)
1018         platform_driver_unregister(&tps65910_driver);
1020 module_exit(tps65910_cleanup);
1022 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
1023 MODULE_DESCRIPTION("TPS6507x voltage regulator driver");
1024 MODULE_LICENSE("GPL v2");
1025 MODULE_ALIAS("platform:tps65910-pmic");