ARM: tegra: cardhu: Enable VDD_CPU external control
[linux-2.6.git] / drivers / regulator / tps6591x-regulator.c
1 /*
2  * driver/regulator/tps6591x-regulator.c
3  *
4  * Regulator driver for TI TPS6591x PMIC family
5  *
6  * Copyright (C) 2011 NVIDIA Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  *
22  */
23
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/regulator/tps6591x-regulator.h>
32 #include <linux/mfd/tps6591x.h>
33
34 /* supply control and voltage setting  */
35 #define TPS6591X_VIO_ADD                0x20
36 #define TPS6591X_VDD1_ADD               0x21
37 #define TPS6591X_VDD1_OP_ADD            0x22
38 #define TPS6591X_VDD1_SR_ADD            0x23
39 #define TPS6591X_VDD2_ADD               0x24
40 #define TPS6591X_VDD2_OP_ADD            0x25
41 #define TPS6591X_VDD2_SR_ADD            0x26
42 #define TPS6591X_VDDCTRL_ADD            0x27
43 #define TPS6591X_VDDCTRL_OP_ADD         0x28
44 #define TPS6591X_VDDCTRL_SR_ADD         0x29
45 #define TPS6591X_LDO1_ADD               0x30
46 #define TPS6591X_LDO2_ADD               0x31
47 #define TPS6591X_LDO3_ADD               0x37
48 #define TPS6591X_LDO4_ADD               0x36
49 #define TPS6591X_LDO5_ADD               0x32
50 #define TPS6591X_LDO6_ADD               0x35
51 #define TPS6591X_LDO7_ADD               0x34
52 #define TPS6591X_LDO8_ADD               0x33
53 #define TPS6591X_EN1_LDO_ADD            0x45
54 #define TPS6591X_EN1_SMPS_ADD           0x46
55 #define TPS6591X_EN2_LDO_ADD            0x47
56 #define TPS6591X_EN2_SMPS_ADD           0x48
57 #define TPS6591X_INVALID_ADD            0xFF
58
59 #define EN1_EN2_OFFSET                  2
60
61 struct tps6591x_register_info {
62         unsigned char addr;
63         unsigned char nbits;
64         unsigned char shift_bits;
65 };
66
67 enum {
68         supply_type_none = 0x0,
69         supply_type_single_reg,
70         supply_type_sr_op_reg
71 };
72
73 struct tps6591x_regulator {
74         struct regulator_desc desc;
75         int supply_type;
76
77         struct tps6591x_register_info supply_reg;
78         struct tps6591x_register_info op_reg;
79         struct tps6591x_register_info sr_reg;
80         struct tps6591x_register_info en1_reg;
81
82         int *voltages;
83 };
84
85 static inline struct device *to_tps6591x_dev(struct regulator_dev *rdev)
86 {
87         return rdev_get_dev(rdev)->parent->parent;
88 }
89
90 static int __tps6591x_ext_control_set(struct device *parent,
91                                       struct tps6591x_regulator *ri,
92                                       enum tps6591x_ext_control ectrl)
93 {
94         int ret;
95         uint8_t mask, reg_val, addr;
96
97         /* For regulator that has separate operational and sleep register make
98            sure that operational is used and clear sleep register to turn
99            regulator off when external control is inactive */
100         if (ri->supply_type == supply_type_sr_op_reg) {
101                 ret = tps6591x_read(parent, ri->op_reg.addr, &reg_val);
102                 if (ret)
103                         return ret;
104
105                 if (reg_val & 0x80) {   /* boot has used sr - switch to op */
106                         ret = tps6591x_read(parent, ri->sr_reg.addr, &reg_val);
107                         if (ret)
108                                 return ret;
109
110                         mask = ((1 << ri->sr_reg.nbits) - 1)
111                                 << ri->sr_reg.shift_bits;
112                         reg_val &= mask;
113                         ret = tps6591x_write(parent, ri->op_reg.addr, reg_val);
114                         if (ret)
115                                 return ret;
116                 }
117                 ret = tps6591x_write(parent, ri->sr_reg.addr, 0);
118                 if (ret)
119                         return ret;
120         }
121
122         switch (ectrl) {
123         case EXT_CTRL_EN1:
124                 addr = ri->en1_reg.addr;
125                 break;
126         case EXT_CTRL_EN2:
127                 addr = ri->en1_reg.addr + EN1_EN2_OFFSET;
128                 break;
129         default:
130                 return -EINVAL;
131         }
132         mask = ((1 << ri->en1_reg.nbits) - 1) << ri->en1_reg.shift_bits;
133         return tps6591x_update(parent, addr, mask, mask);
134 }
135
136 static int __tps6591x_vio_set_voltage(struct device *parent,
137                                       struct tps6591x_regulator *ri,
138                                       int min_uV, int max_uV)
139 {
140         int uV;
141         uint8_t mask;
142         uint8_t val;
143
144         for (val = 0; val < ri->desc.n_voltages; val++) {
145                 uV = ri->voltages[val] * 1000;
146
147                 /* use the first in-range value */
148                 if (min_uV <= uV && uV <= max_uV) {
149
150                         val <<= ri->supply_reg.shift_bits;
151                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
152                                         ri->supply_reg.shift_bits;
153
154                         return tps6591x_update(parent, ri->supply_reg.addr,
155                                         val, mask);
156                 }
157         }
158
159         return -EINVAL;
160 }
161
162 static int tps6591x_vio_set_voltage(struct regulator_dev *rdev,
163                                     int min_uV, int max_uV)
164 {
165         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
166         struct device *parent = to_tps6591x_dev(rdev);
167
168         return __tps6591x_vio_set_voltage(parent, ri, min_uV, max_uV);
169 }
170
171 static int tps6591x_vio_get_voltage(struct regulator_dev *rdev)
172 {
173         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
174         struct device *parent = to_tps6591x_dev(rdev);
175         uint8_t val, mask;
176         int ret;
177
178         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
179         if (ret)
180                 return ret;
181
182         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
183         val = (val & mask) >> ri->supply_reg.shift_bits;
184
185         if (val >= ri->desc.n_voltages)
186                 BUG();
187
188         return ri->voltages[val] * 1000;
189 }
190
191
192 static int tps6591x_ldo_list_voltage(struct regulator_dev *rdev,
193                                      unsigned selector)
194 {
195         struct tps6591x_regulator *info = rdev_get_drvdata(rdev);
196
197         return info->voltages[selector] * 1000;
198 }
199
200 static int __tps6591x_ldo1_set_voltage(struct device *parent,
201                                       struct tps6591x_regulator *ri,
202                                       int min_uV, int max_uV)
203 {
204         int val, uV;
205         uint8_t mask;
206
207         for (val = 0; val < ri->desc.n_voltages; val++) {
208                 uV = ri->voltages[val] * 1000;
209
210                 /* use the first in-range value */
211                 if (min_uV <= uV && uV <= max_uV) {
212                         val += 4;
213                         val <<= ri->supply_reg.shift_bits;
214                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
215                                         ri->supply_reg.shift_bits;
216
217                         return tps6591x_update(parent, ri->supply_reg.addr,
218                                         val, mask);
219                 }
220         }
221
222         return -EINVAL;
223 }
224
225 static int tps6591x_ldo1_set_voltage(struct regulator_dev *rdev,
226                                     int min_uV, int max_uV)
227 {
228         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
229         struct device *parent = to_tps6591x_dev(rdev);
230
231         return __tps6591x_ldo1_set_voltage(parent, ri, min_uV, max_uV);
232 }
233
234 static int tps6591x_ldo1_get_voltage(struct regulator_dev *rdev)
235 {
236         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
237         struct device *parent = to_tps6591x_dev(rdev);
238         uint8_t val, mask;
239         int ret;
240
241         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
242         if (ret)
243                 return ret;
244
245         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
246         val = (val & mask) >> ri->supply_reg.shift_bits;
247
248         if (val < 4)
249                 return 1000 * 1000;
250         else if (val > 0x32)
251                 return 3300 * 1000;
252         else
253                 val -= 4;
254         if (val >= ri->desc.n_voltages)
255                 BUG();
256
257         return ri->voltages[val] * 1000;
258 }
259
260 static int __tps6591x_ldo3_set_voltage(struct device *parent,
261                 struct tps6591x_regulator *ri, int min_uV, int max_uV)
262 {
263         int val, uV;
264         uint8_t mask;
265
266         for (val = 0; val < ri->desc.n_voltages; val++) {
267                 uV = ri->voltages[val] * 1000;
268
269                 /* use the first in-range value */
270                 if (min_uV <= uV && uV <= max_uV) {
271                         val += 2;
272                         val <<= ri->supply_reg.shift_bits;
273                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
274                                                 ri->supply_reg.shift_bits;
275
276                         return tps6591x_update(parent, ri->supply_reg.addr,
277                                         val, mask);
278                 }
279         }
280
281         return -EINVAL;
282 }
283
284 static int tps6591x_ldo3_set_voltage(struct regulator_dev *rdev,
285                                     int min_uV, int max_uV)
286 {
287         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
288         struct device *parent = to_tps6591x_dev(rdev);
289
290         return __tps6591x_ldo3_set_voltage(parent, ri, min_uV, max_uV);
291 }
292
293 static int tps6591x_ldo3_get_voltage(struct regulator_dev *rdev)
294 {
295         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
296         struct device *parent = to_tps6591x_dev(rdev);
297         uint8_t val, mask;
298         int ret;
299
300         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
301         if (ret)
302                 return ret;
303
304         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
305         val = (val & mask) >> ri->supply_reg.shift_bits;
306
307         if (val < 2)
308                 return 1000 * 1000;
309         else if (val > 0x19)
310                 return 3300 * 1000;
311         else
312                 val -= 2;
313         if (val >= ri->desc.n_voltages)
314                 BUG();
315
316         return ri->voltages[val] * 1000;
317 }
318
319 static int __tps6591x_vdd_set_voltage(struct device *parent,
320                                       struct tps6591x_regulator *ri,
321                                       int min_uV, int max_uV)
322 {
323         int val, uV, ret;
324         uint8_t mask, reg_val;
325
326         for (val = 0; val < ri->desc.n_voltages; val++) {
327                 uV = ri->voltages[val] * 1000;
328
329                 /* use the first in-range value */
330                 if (min_uV <= uV && uV <= max_uV) {
331                         ret = tps6591x_read(parent, ri->op_reg.addr, &reg_val);
332                         if (ret)
333                                 return ret;
334                         val += 3;
335                         if (reg_val & 0x80) {
336                                 val <<= ri->sr_reg.shift_bits;
337                                 mask = ((1 << ri->sr_reg.nbits) - 1)
338                                         << ri->sr_reg.shift_bits;
339                                 return tps6591x_update(parent,
340                                         ri->sr_reg.addr, val, mask);
341                         } else {
342                                 val <<= ri->op_reg.shift_bits;
343                                 mask = ((1 << ri->op_reg.nbits) - 1)
344                                         << ri->op_reg.shift_bits;
345                                 return tps6591x_update(parent,
346                                         ri->op_reg.addr, val, mask);
347                         }
348                 }
349         }
350
351         return -EINVAL;
352 }
353
354 static int tps6591x_vdd_set_voltage(struct regulator_dev *rdev,
355                                     int min_uV, int max_uV)
356 {
357         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
358         struct device *parent = to_tps6591x_dev(rdev);
359
360         return __tps6591x_vdd_set_voltage(parent, ri, min_uV, max_uV);
361 }
362
363 static int tps6591x_vdd_get_voltage(struct regulator_dev *rdev)
364 {
365         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
366         struct device *parent = to_tps6591x_dev(rdev);
367         uint8_t op_val, sr_val, val;
368         int ret;
369
370         ret = tps6591x_read(parent, ri->op_reg.addr, &op_val);
371         if (ret)
372                 return ret;
373
374         ret = tps6591x_read(parent, ri->sr_reg.addr, &sr_val);
375         if (ret)
376                 return ret;
377
378         val = (op_val & 0x80) ? (sr_val & 0x7F) : (op_val & 0x7F);
379
380         if (!val)
381                 return 0;
382         else if (val < 0x3)
383                 return 600 * 1000;
384         else if (val > 0x4B)
385                 return 1500 * 1000;
386         else
387                 val -= 3;
388
389         if (val >= ri->desc.n_voltages)
390                 BUG();
391
392         return ri->voltages[val] * 1000;
393 }
394
395 static int tps6591x_regulator_enable(struct regulator_dev *rdev)
396 {
397         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
398         struct device *parent = to_tps6591x_dev(rdev);
399
400         return tps6591x_set_bits(parent, ri->supply_reg.addr, 0x1);
401 }
402
403 static int tps6591x_regulator_disable(struct regulator_dev *rdev)
404 {
405         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
406         struct device *parent = to_tps6591x_dev(rdev);
407
408         return tps6591x_clr_bits(parent, ri->supply_reg.addr, 0x1);
409 }
410
411 static int tps6591x_regulator_is_enabled(struct regulator_dev *rdev)
412 {
413         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
414         struct device *parent = to_tps6591x_dev(rdev);
415         uint8_t reg_val;
416         int ret;
417
418         ret = tps6591x_read(parent, ri->supply_reg.addr, &reg_val);
419         if (ret)
420                 return ret;
421
422         return !!(reg_val & 0x1);
423 }
424
425 static struct regulator_ops tps6591x_regulator_vio_ops = {
426         .list_voltage = tps6591x_ldo_list_voltage,
427         .get_voltage = tps6591x_vio_get_voltage,
428         .set_voltage = tps6591x_vio_set_voltage,
429
430         .is_enabled = tps6591x_regulator_is_enabled,
431         .enable = tps6591x_regulator_enable,
432         .disable = tps6591x_regulator_disable,
433 };
434
435 static struct regulator_ops tps6591x_regulator_ldo1_ops = {
436         .list_voltage = tps6591x_ldo_list_voltage,
437         .get_voltage = tps6591x_ldo1_get_voltage,
438         .set_voltage = tps6591x_ldo1_set_voltage,
439
440         .is_enabled = tps6591x_regulator_is_enabled,
441         .enable = tps6591x_regulator_enable,
442         .disable = tps6591x_regulator_disable,
443 };
444
445 static struct regulator_ops tps6591x_regulator_ldo3_ops = {
446         .list_voltage = tps6591x_ldo_list_voltage,
447         .get_voltage = tps6591x_ldo3_get_voltage,
448         .set_voltage = tps6591x_ldo3_set_voltage,
449
450         .is_enabled = tps6591x_regulator_is_enabled,
451         .enable = tps6591x_regulator_enable,
452         .disable = tps6591x_regulator_disable,
453 };
454
455 static struct regulator_ops tps6591x_regulator_vdd_ops = {
456         .list_voltage = tps6591x_ldo_list_voltage,
457         .get_voltage = tps6591x_vdd_get_voltage,
458         .set_voltage = tps6591x_vdd_set_voltage,
459
460         .is_enabled = tps6591x_regulator_is_enabled,
461         .enable = tps6591x_regulator_enable,
462         .disable = tps6591x_regulator_disable,
463 };
464
465 static int tps6591x_vio_voltages[] = {
466         1500, 1800, 2500, 3300,
467 };
468
469 /* SEL[7:2]=000100:1000mV --> 110010:3300mV */
470 static int tps6591x_ldo124_voltages[] = {
471         1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450,
472         1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950,
473         2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450,
474         2500, 2550, 2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950,
475         3000, 3050, 3100, 3150, 3200, 3250, 3300,
476 };
477
478 /* SEL[6:2]=00010:1000mv --> 11001:3300mV */
479 static int tps6591x_ldo35678_voltages[] = {
480         1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900,
481         2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900,
482         3000, 3100, 3200, 3300,
483 };
484
485 static int tps6591x_vdd_voltages[] = {
486         600, 612, 625, 637, 650, 662, 675, 687, 700, 712, 725, 737,
487         750, 762, 775, 787, 800, 812, 825, 837, 850, 862, 875, 887,
488         900, 912, 925, 937, 950, 962, 975, 987, 1000, 1012, 1025,
489         1037, 1050, 1062, 1075, 1087, 1100, 1112, 1125, 1137, 1150,
490         1162, 1175, 1187, 1200, 1212, 1225, 1237, 1250, 1262, 1275,
491         1287, 1300, 1312, 1325, 1337, 1350, 1362, 1375, 1387, 1400,
492         1412, 1425, 1437, 1450, 1462, 1475, 1487, 1500,
493 };
494
495 #define TPS6591X_REGULATOR(_id, vdata, _ops, s_addr, s_nbits, s_shift,  \
496                         s_type, op_addr, op_nbits, op_shift, sr_addr,   \
497                         sr_nbits, sr_shift, en1_addr, en1_shift)        \
498         .desc   = {                                                     \
499                 .name   = tps6591x_rails(_id),                          \
500                 .ops    = &tps6591x_regulator_##_ops,                   \
501                 .type   = REGULATOR_VOLTAGE,                            \
502                 .id     = TPS6591X_ID_##_id,                            \
503                 .n_voltages = ARRAY_SIZE(tps6591x_##vdata##_voltages),  \
504                 .owner  = THIS_MODULE,                                  \
505         },                                                              \
506         .supply_type    = supply_type_##s_type,                         \
507         .supply_reg     = {                                             \
508                 .addr   = TPS6591X_##s_addr##_ADD,                      \
509                 .nbits  = s_nbits,                                      \
510                 .shift_bits = s_shift,                                  \
511         },                                                              \
512         .op_reg         = {                                             \
513                 .addr   = TPS6591X_##op_addr##_ADD,                     \
514                 .nbits  = op_nbits,                                     \
515                 .shift_bits = op_shift,                                 \
516         },                                                              \
517         .sr_reg         = {                                             \
518                 .addr   = TPS6591X_##sr_addr##_ADD,                     \
519                 .nbits  = sr_nbits,                                     \
520                 .shift_bits = sr_shift,                                 \
521         },                                                              \
522         .en1_reg        = {                                             \
523                 .addr   = TPS6591X_##en1_addr##_ADD,                    \
524                 .nbits  = 1,                                            \
525                 .shift_bits = en1_shift,                                \
526         },                                                              \
527         .voltages       = tps6591x_##vdata##_voltages,
528
529 #define TPS6591X_VIO(_id, vdata, s_addr, s_nbits, s_shift, s_type,      \
530                         en1_shift)                                      \
531 {                                                                       \
532         TPS6591X_REGULATOR(_id, vdata, vio_ops, s_addr, s_nbits,        \
533                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0,  \
534                         EN1_SMPS, en1_shift)                            \
535 }
536
537 #define TPS6591X_LDO1(_id, vdata, s_addr, s_nbits, s_shift, s_type,     \
538                         en1_shift)                                      \
539 {                                                                       \
540         TPS6591X_REGULATOR(_id, vdata, ldo1_ops, s_addr, s_nbits,       \
541                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0,  \
542                         EN1_LDO, en1_shift)                             \
543 }
544
545 #define TPS6591X_LDO3(_id, vdata, s_addr, s_nbits, s_shift, s_type,     \
546                         en1_shift)                                      \
547 {                                                                       \
548         TPS6591X_REGULATOR(_id, vdata, ldo3_ops, s_addr, s_nbits,       \
549                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0,  \
550                         EN1_LDO, en1_shift)                             \
551 }
552
553 #define TPS6591X_VDD(_id, vdata, s_addr, s_nbits, s_shift, s_type,      \
554                         op_addr, op_nbits, op_shift, sr_addr, sr_nbits, \
555                         sr_shift, en1_shift)                            \
556 {                                                                       \
557         TPS6591X_REGULATOR(_id, vdata, vdd_ops, s_addr, s_nbits,        \
558                         s_shift, s_type, op_addr, op_nbits, op_shift,   \
559                         sr_addr, sr_nbits, sr_shift, EN1_SMPS,          \
560                         en1_shift)                                      \
561 }
562
563 static struct tps6591x_regulator tps6591x_regulator[] = {
564         TPS6591X_VIO(VIO, vio, VIO, 2, 2, single_reg, 0),
565         TPS6591X_LDO1(LDO_1, ldo124, LDO1, 6, 2, single_reg, 1),
566         TPS6591X_LDO1(LDO_2, ldo124, LDO2, 6, 2, single_reg, 2),
567         TPS6591X_LDO3(LDO_3, ldo35678, LDO3, 5, 2, single_reg, 7),
568         TPS6591X_LDO1(LDO_4, ldo124, LDO4, 6, 2, single_reg, 6),
569         TPS6591X_LDO3(LDO_5, ldo35678, LDO5, 5, 2, single_reg, 3),
570         TPS6591X_LDO3(LDO_6, ldo35678, LDO6, 5, 2, single_reg, 0),
571         TPS6591X_LDO3(LDO_7, ldo35678, LDO7, 5, 2, single_reg, 5),
572         TPS6591X_LDO3(LDO_8, ldo35678, LDO8, 5, 2, single_reg, 4),
573         TPS6591X_VDD(VDD_1, vdd, VDD1, 2, 0, sr_op_reg, VDD1_OP,
574                 7, 0, VDD1_SR, 7, 0, 1),
575         TPS6591X_VDD(VDD_2, vdd, VDD2, 2, 0, sr_op_reg, VDD2_OP,
576                 7, 0, VDD2_SR, 7, 0, 2),
577         TPS6591X_VDD(VDDCTRL, vdd, VDDCTRL, 2, 0, sr_op_reg,
578                 VDDCTRL_OP, 7, 0, VDDCTRL_SR, 7, 0, 3),
579 };
580
581 static inline int tps6591x_regulator_preinit(struct device *parent,
582                 struct tps6591x_regulator *ri,
583                 struct tps6591x_regulator_platform_data *tps6591x_pdata)
584 {
585         int ret;
586
587         if (tps6591x_pdata->ectrl != EXT_CTRL_NONE) {
588                 ret = __tps6591x_ext_control_set(
589                         parent, ri, tps6591x_pdata->ectrl);
590                 if (ret < 0) {
591                         pr_err("Not able to configure external control %d"
592                                " for rail %d err %d\n", tps6591x_pdata->ectrl,
593                                ri->desc.id, ret);
594                         return ret;
595                 }
596         }
597
598         if (!tps6591x_pdata->init_apply)
599                 return 0;
600
601         if (tps6591x_pdata->init_uV >= 0) {
602                 switch (ri->desc.id) {
603                 case TPS6591X_ID_VIO:
604                         ret = __tps6591x_vio_set_voltage(parent, ri,
605                                         tps6591x_pdata->init_uV,
606                                         tps6591x_pdata->init_uV);
607                         break;
608
609                 case TPS6591X_ID_LDO_1:
610                 case TPS6591X_ID_LDO_2:
611                 case TPS6591X_ID_LDO_4:
612                         ret = __tps6591x_ldo1_set_voltage(parent, ri,
613                                         tps6591x_pdata->init_uV,
614                                         tps6591x_pdata->init_uV);
615                         break;
616
617                 case TPS6591X_ID_LDO_3:
618                 case TPS6591X_ID_LDO_5:
619                 case TPS6591X_ID_LDO_6:
620                 case TPS6591X_ID_LDO_7:
621                 case TPS6591X_ID_LDO_8:
622                         ret = __tps6591x_ldo3_set_voltage(parent, ri,
623                                         tps6591x_pdata->init_uV,
624                                         tps6591x_pdata->init_uV);
625                         break;
626
627                 case TPS6591X_ID_VDD_1:
628                 case TPS6591X_ID_VDD_2:
629                 case TPS6591X_ID_VDDCTRL:
630                         ret = __tps6591x_vdd_set_voltage(parent, ri,
631                                         tps6591x_pdata->init_uV,
632                                         tps6591x_pdata->init_uV);
633                         break;
634
635                 default:
636                         ret = -EINVAL;
637                         break;
638                 }
639                 if (ret < 0) {
640                         pr_err("Not able to initialize voltage %d for rail "
641                                 "%d err %d\n", tps6591x_pdata->init_uV,
642                                 ri->desc.id, ret);
643                         return ret;
644                 }
645         }
646
647         if (tps6591x_pdata->init_enable)
648                 ret = tps6591x_set_bits(parent, ri->supply_reg.addr, 0x1);
649         else
650                 ret = tps6591x_clr_bits(parent, ri->supply_reg.addr, 0x1);
651
652         if (ret < 0)
653                 pr_err("Not able to %s rail %d err %d\n",
654                         (tps6591x_pdata->init_enable) ? "enable" : "disable",
655                         ri->desc.id, ret);
656         return ret;
657 }
658
659 static inline struct tps6591x_regulator *find_regulator_info(int id)
660 {
661         struct tps6591x_regulator *ri;
662         int i;
663
664         for (i = 0; i < ARRAY_SIZE(tps6591x_regulator); i++) {
665                 ri = &tps6591x_regulator[i];
666                 if (ri->desc.id == id)
667                         return ri;
668         }
669         return NULL;
670 }
671
672 static int __devinit tps6591x_regulator_probe(struct platform_device *pdev)
673 {
674         struct tps6591x_regulator *ri = NULL;
675         struct regulator_dev *rdev;
676         struct tps6591x_regulator_platform_data *tps_pdata;
677         int id = pdev->id;
678         int err;
679
680         dev_dbg(&pdev->dev, "Probing reulator %d\n", id);
681
682         ri = find_regulator_info(id);
683         if (ri == NULL) {
684                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
685                 return -EINVAL;
686         }
687         tps_pdata = pdev->dev.platform_data;
688
689         err = tps6591x_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
690         if (err)
691                 return err;
692
693         rdev = regulator_register(&ri->desc, &pdev->dev,
694                                 &tps_pdata->regulator, ri);
695         if (IS_ERR_OR_NULL(rdev)) {
696                 dev_err(&pdev->dev, "failed to register regulator %s\n",
697                                 ri->desc.name);
698                 return PTR_ERR(rdev);
699         }
700
701         platform_set_drvdata(pdev, rdev);
702
703         return 0;
704 }
705
706 static int __devexit tps6591x_regulator_remove(struct platform_device *pdev)
707 {
708         struct regulator_dev *rdev = platform_get_drvdata(pdev);
709
710         regulator_unregister(rdev);
711         return 0;
712 }
713
714 static struct platform_driver tps6591x_regulator_driver = {
715         .driver = {
716                 .name   = "tps6591x-regulator",
717                 .owner  = THIS_MODULE,
718         },
719         .probe          = tps6591x_regulator_probe,
720         .remove         = __devexit_p(tps6591x_regulator_remove),
721 };
722
723 static int __init tps6591x_regulator_init(void)
724 {
725         return platform_driver_register(&tps6591x_regulator_driver);
726 }
727 subsys_initcall(tps6591x_regulator_init);
728
729 static void __exit tps6591x_regulator_exit(void)
730 {
731         platform_driver_unregister(&tps6591x_regulator_driver);
732 }
733 module_exit(tps6591x_regulator_exit);
734
735 MODULE_LICENSE("GPL");
736 MODULE_DESCRIPTION("Regulator Driver for TI TPS6591X PMIC");
737 MODULE_ALIAS("platform:tps6591x-regulator");