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