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