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