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