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