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