regulator: tps6591x: supporting init state of output through board
[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/init.h>
26 #include <linux/err.h>
27 #include <linux/slab.h>
28 #include <linux/platform_device.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/regulator/tps6591x-regulator.h>
32 #include <linux/mfd/tps6591x.h>
33
34 /* supply control and voltage setting  */
35 #define TPS6591X_VIO_ADD                0x20
36 #define TPS6591X_VDD1_ADD               0x21
37 #define TPS6591X_VDD1_OP_ADD            0x22
38 #define TPS6591X_VDD1_SR_ADD            0x23
39 #define TPS6591X_VDD2_ADD               0x24
40 #define TPS6591X_VDD2_OP_ADD            0x25
41 #define TPS6591X_VDD2_SR_ADD            0x26
42 #define TPS6591X_VDDCTRL_ADD            0x27
43 #define TPS6591X_VDDCTRL_OP_ADD         0x28
44 #define TPS6591X_VDDCTRL_SR_ADD         0x29
45 #define TPS6591X_LDO1_ADD               0x30
46 #define TPS6591X_LDO2_ADD               0x31
47 #define TPS6591X_LDO3_ADD               0x37
48 #define TPS6591X_LDO4_ADD               0x36
49 #define TPS6591X_LDO5_ADD               0x32
50 #define TPS6591X_LDO6_ADD               0x35
51 #define TPS6591X_LDO7_ADD               0x34
52 #define TPS6591X_LDO8_ADD               0x33
53 #define TPS6591X_INVALID_ADD            0xFF
54
55 struct tps6591x_register_info {
56         unsigned char addr;
57         unsigned char nbits;
58         unsigned char shift_bits;
59 };
60
61 enum {
62         supply_type_none = 0x0,
63         supply_type_single_reg,
64         supply_type_sr_op_reg
65 };
66
67 struct tps6591x_regulator {
68         struct regulator_desc desc;
69         int supply_type;
70
71         struct tps6591x_register_info supply_reg;
72         struct tps6591x_register_info op_reg;
73         struct tps6591x_register_info sr_reg;
74
75         int *voltages;
76 };
77
78 static inline struct device *to_tps6591x_dev(struct regulator_dev *rdev)
79 {
80         return rdev_get_dev(rdev)->parent->parent;
81 }
82
83 static int __tps6591x_vio_set_voltage(struct device *parent,
84                                       struct tps6591x_regulator *ri,
85                                       int min_uV, int max_uV)
86 {
87         int uV;
88         uint8_t mask;
89         uint8_t val;
90
91         for (val = 0; val < ri->desc.n_voltages; val++) {
92                 uV = ri->voltages[val] * 1000;
93
94                 /* use the first in-range value */
95                 if (min_uV <= uV && uV <= max_uV) {
96
97                         val <<= ri->supply_reg.shift_bits;
98                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
99                                         ri->supply_reg.shift_bits;
100
101                         return tps6591x_update(parent, ri->supply_reg.addr,
102                                         val, mask);
103                 }
104         }
105
106         return -EINVAL;
107 }
108
109 static int tps6591x_vio_set_voltage(struct regulator_dev *rdev,
110                                     int min_uV, int max_uV)
111 {
112         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
113         struct device *parent = to_tps6591x_dev(rdev);
114
115         return __tps6591x_vio_set_voltage(parent, ri, min_uV, max_uV);
116 }
117
118 static int tps6591x_vio_get_voltage(struct regulator_dev *rdev)
119 {
120         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
121         struct device *parent = to_tps6591x_dev(rdev);
122         uint8_t val, mask;
123         int ret;
124
125         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
126         if (ret)
127                 return ret;
128
129         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
130         val = (val & mask) >> ri->supply_reg.shift_bits;
131
132         if (val >= ri->desc.n_voltages)
133                 BUG();
134
135         return ri->voltages[val] * 1000;
136 }
137
138
139 static int tps6591x_ldo_list_voltage(struct regulator_dev *rdev,
140                                      unsigned selector)
141 {
142         struct tps6591x_regulator *info = rdev_get_drvdata(rdev);
143
144         return info->voltages[selector] * 1000;
145 }
146
147 static int __tps6591x_ldo1_set_voltage(struct device *parent,
148                                       struct tps6591x_regulator *ri,
149                                       int min_uV, int max_uV)
150 {
151         int val, uV;
152         uint8_t mask;
153
154         for (val = 0; val < ri->desc.n_voltages; val++) {
155                 uV = ri->voltages[val] * 1000;
156
157                 /* use the first in-range value */
158                 if (min_uV <= uV && uV <= max_uV) {
159                         val += 4;
160                         val <<= ri->supply_reg.shift_bits;
161                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
162                                         ri->supply_reg.shift_bits;
163
164                         return tps6591x_update(parent, ri->supply_reg.addr,
165                                         val, mask);
166                 }
167         }
168
169         return -EINVAL;
170 }
171
172 static int tps6591x_ldo1_set_voltage(struct regulator_dev *rdev,
173                                     int min_uV, int max_uV)
174 {
175         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
176         struct device *parent = to_tps6591x_dev(rdev);
177
178         return __tps6591x_ldo1_set_voltage(parent, ri, min_uV, max_uV);
179 }
180
181 static int tps6591x_ldo1_get_voltage(struct regulator_dev *rdev)
182 {
183         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
184         struct device *parent = to_tps6591x_dev(rdev);
185         uint8_t val, mask;
186         int ret;
187
188         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
189         if (ret)
190                 return ret;
191
192         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
193         val = (val & mask) >> ri->supply_reg.shift_bits;
194
195         if (val < 4)
196                 return 1000 * 1000;
197         else if (val > 0x32)
198                 return 3300 * 1000;
199         else
200                 val -= 4;
201         if (val >= ri->desc.n_voltages)
202                 BUG();
203
204         return ri->voltages[val] * 1000;
205 }
206
207 static int __tps6591x_ldo3_set_voltage(struct device *parent,
208                 struct tps6591x_regulator *ri, int min_uV, int max_uV)
209 {
210         int val, uV;
211         uint8_t mask;
212
213         for (val = 0; val < ri->desc.n_voltages; val++) {
214                 uV = ri->voltages[val] * 1000;
215
216                 /* use the first in-range value */
217                 if (min_uV <= uV && uV <= max_uV) {
218                         val += 2;
219                         val <<= ri->supply_reg.shift_bits;
220                         mask = ((1 << ri->supply_reg.nbits) - 1) <<
221                                                 ri->supply_reg.shift_bits;
222
223                         return tps6591x_update(parent, ri->supply_reg.addr,
224                                         val, mask);
225                 }
226         }
227
228         return -EINVAL;
229 }
230
231 static int tps6591x_ldo3_set_voltage(struct regulator_dev *rdev,
232                                     int min_uV, int max_uV)
233 {
234         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
235         struct device *parent = to_tps6591x_dev(rdev);
236
237         return __tps6591x_ldo3_set_voltage(parent, ri, min_uV, max_uV);
238 }
239
240 static int tps6591x_ldo3_get_voltage(struct regulator_dev *rdev)
241 {
242         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
243         struct device *parent = to_tps6591x_dev(rdev);
244         uint8_t val, mask;
245         int ret;
246
247         ret = tps6591x_read(parent, ri->supply_reg.addr, &val);
248         if (ret)
249                 return ret;
250
251         mask = ((1 << ri->supply_reg.nbits) - 1) << ri->supply_reg.shift_bits;
252         val = (val & mask) >> ri->supply_reg.shift_bits;
253
254         if (val < 2)
255                 return 1000 * 1000;
256         else if (val > 0x19)
257                 return 3300 * 1000;
258         else
259                 val -= 2;
260         if (val >= ri->desc.n_voltages)
261                 BUG();
262
263         return ri->voltages[val] * 1000;
264 }
265
266 static int __tps6591x_vdd_set_voltage(struct device *parent,
267                                       struct tps6591x_regulator *ri,
268                                       int min_uV, int max_uV)
269 {
270         int val, uV, ret;
271         uint8_t mask, reg_val;
272
273         for (val = 0; val < ri->desc.n_voltages; val++) {
274                 uV = ri->voltages[val] * 1000;
275
276                 /* use the first in-range value */
277                 if (min_uV <= uV && uV <= max_uV) {
278                         ret = tps6591x_read(parent, ri->op_reg.addr, &reg_val);
279                         if (ret)
280                                 return ret;
281                         val += 3;
282                         if (reg_val & 0x80) {
283                                 val <<= ri->sr_reg.shift_bits;
284                                 mask = ((1 << ri->sr_reg.nbits) - 1)
285                                         << ri->sr_reg.shift_bits;
286                                 return tps6591x_update(parent,
287                                         ri->sr_reg.addr, val, mask);
288                         } else {
289                                 val <<= ri->op_reg.shift_bits;
290                                 mask = ((1 << ri->op_reg.nbits) - 1)
291                                         << ri->op_reg.shift_bits;
292                                 return tps6591x_update(parent,
293                                         ri->op_reg.addr, val, mask);
294                         }
295                 }
296         }
297
298         return -EINVAL;
299 }
300
301 static int tps6591x_vdd_set_voltage(struct regulator_dev *rdev,
302                                     int min_uV, int max_uV)
303 {
304         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
305         struct device *parent = to_tps6591x_dev(rdev);
306
307         return __tps6591x_vdd_set_voltage(parent, ri, min_uV, max_uV);
308 }
309
310 static int tps6591x_vdd_get_voltage(struct regulator_dev *rdev)
311 {
312         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
313         struct device *parent = to_tps6591x_dev(rdev);
314         uint8_t op_val, sr_val, val;
315         int ret;
316
317         ret = tps6591x_read(parent, ri->op_reg.addr, &op_val);
318         if (ret)
319                 return ret;
320
321         ret = tps6591x_read(parent, ri->sr_reg.addr, &sr_val);
322         if (ret)
323                 return ret;
324
325         val = (op_val & 0x80) ? (sr_val & 0x7F) : (op_val & 0x7F);
326
327         if (!val)
328                 return 0;
329         else if (val < 0x3)
330                 return 600 * 1000;
331         else if (val > 0x4B)
332                 return 1500 * 1000;
333         else
334                 val -= 3;
335
336         if (val >= ri->desc.n_voltages)
337                 BUG();
338
339         return ri->voltages[val] * 1000;
340 }
341
342 static int tps6591x_regulator_enable(struct regulator_dev *rdev)
343 {
344         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
345         struct device *parent = to_tps6591x_dev(rdev);
346
347         return tps6591x_set_bits(parent, ri->supply_reg.addr, 0x1);
348 }
349
350 static int tps6591x_regulator_disable(struct regulator_dev *rdev)
351 {
352         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
353         struct device *parent = to_tps6591x_dev(rdev);
354
355         return tps6591x_clr_bits(parent, ri->supply_reg.addr, 0x1);
356 }
357
358 static int tps6591x_regulator_is_enabled(struct regulator_dev *rdev)
359 {
360         struct tps6591x_regulator *ri = rdev_get_drvdata(rdev);
361         struct device *parent = to_tps6591x_dev(rdev);
362         uint8_t reg_val;
363         int ret;
364
365         ret = tps6591x_read(parent, ri->supply_reg.addr, &reg_val);
366         if (ret)
367                 return ret;
368
369         return !!(reg_val & 0x1);
370 }
371
372 static struct regulator_ops tps6591x_regulator_vio_ops = {
373         .list_voltage = tps6591x_ldo_list_voltage,
374         .get_voltage = tps6591x_vio_get_voltage,
375         .set_voltage = tps6591x_vio_set_voltage,
376
377         .is_enabled = tps6591x_regulator_is_enabled,
378         .enable = tps6591x_regulator_enable,
379         .disable = tps6591x_regulator_disable,
380 };
381
382 static struct regulator_ops tps6591x_regulator_ldo1_ops = {
383         .list_voltage = tps6591x_ldo_list_voltage,
384         .get_voltage = tps6591x_ldo1_get_voltage,
385         .set_voltage = tps6591x_ldo1_set_voltage,
386
387         .is_enabled = tps6591x_regulator_is_enabled,
388         .enable = tps6591x_regulator_enable,
389         .disable = tps6591x_regulator_disable,
390 };
391
392 static struct regulator_ops tps6591x_regulator_ldo3_ops = {
393         .list_voltage = tps6591x_ldo_list_voltage,
394         .get_voltage = tps6591x_ldo3_get_voltage,
395         .set_voltage = tps6591x_ldo3_set_voltage,
396
397         .is_enabled = tps6591x_regulator_is_enabled,
398         .enable = tps6591x_regulator_enable,
399         .disable = tps6591x_regulator_disable,
400 };
401
402 static struct regulator_ops tps6591x_regulator_vdd_ops = {
403         .list_voltage = tps6591x_ldo_list_voltage,
404         .get_voltage = tps6591x_vdd_get_voltage,
405         .set_voltage = tps6591x_vdd_set_voltage,
406
407         .is_enabled = tps6591x_regulator_is_enabled,
408         .enable = tps6591x_regulator_enable,
409         .disable = tps6591x_regulator_disable,
410 };
411
412 static int tps6591x_vio_voltages[] = {
413         1500, 1800, 2500, 3300,
414 };
415
416 /* SEL[7:2]=000100:1000mV --> 110010:3300mV */
417 static int tps6591x_ldo124_voltages[] = {
418         1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350, 1400, 1450,
419         1500, 1550, 1600, 1650, 1700, 1750, 1800, 1850, 1900, 1950,
420         2000, 2050, 2100, 2150, 2200, 2250, 2300, 2350, 2400, 2450,
421         2500, 2550, 2600, 2650, 2700, 2750, 2800, 2850, 2900, 2950,
422         3000, 3050, 3100, 3150, 3200, 3250, 3300,
423 };
424
425 /* SEL[6:2]=00010:1000mv --> 11001:3300mV */
426 static int tps6591x_ldo35678_voltages[] = {
427         1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900,
428         2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900,
429         3000, 3100, 3200, 3300,
430 };
431
432 static int tps6591x_vdd_voltages[] = {
433         600, 612, 625, 637, 650, 662, 675, 687, 700, 712, 725, 737,
434         750, 762, 775, 787, 800, 812, 825, 837, 850, 862, 875, 887,
435         900, 912, 925, 937, 950, 962, 975, 987, 1000, 1012, 1025,
436         1037, 1050, 1062, 1075, 1087, 1100, 1112, 1125, 1137, 1150,
437         1162, 1175, 1187, 1200, 1212, 1225, 1237, 1250, 1262, 1275,
438         1287, 1300, 1312, 1325, 1337, 1350, 1362, 1375, 1387, 1400,
439         1412, 1425, 1437, 1450, 1462, 1475, 1487, 1500,
440 };
441
442 #define TPS6591X_REGULATOR(_id, vdata, _ops, s_addr, s_nbits, s_shift,  \
443                         s_type, op_addr, op_nbits, op_shift, sr_addr,   \
444                         sr_nbits, sr_shift)                             \
445         .desc   = {                                                     \
446                 .name   = tps6591x_rails(_id),                          \
447                 .ops    = &tps6591x_regulator_##_ops,                   \
448                 .type   = REGULATOR_VOLTAGE,                            \
449                 .id     = TPS6591X_ID_##_id,                            \
450                 .n_voltages = ARRAY_SIZE(tps6591x_##vdata##_voltages),  \
451                 .owner  = THIS_MODULE,                                  \
452         },                                                              \
453         .supply_type    = supply_type_##s_type,                         \
454         .supply_reg     = {                                             \
455                 .addr   = TPS6591X_##s_addr##_ADD,                      \
456                 .nbits  = s_nbits,                                      \
457                 .shift_bits = s_shift,                                  \
458         },                                                              \
459         .op_reg         = {                                             \
460                 .addr   = TPS6591X_##op_addr##_ADD,                     \
461                 .nbits  = op_nbits,                                     \
462                 .shift_bits = op_shift,                                 \
463         },                                                              \
464         .sr_reg         = {                                             \
465                 .addr   = TPS6591X_##sr_addr##_ADD,                     \
466                 .nbits  = sr_nbits,                                     \
467                 .shift_bits = sr_shift,                                 \
468         },                                                              \
469         .voltages       = tps6591x_##vdata##_voltages,
470
471 #define TPS6591X_VIO(_id, vdata, s_addr, s_nbits, s_shift, s_type)      \
472 {                                                                       \
473         TPS6591X_REGULATOR(_id, vdata, vio_ops, s_addr, s_nbits,        \
474                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0)  \
475 }
476
477 #define TPS6591X_LDO1(_id, vdata, s_addr, s_nbits, s_shift, s_type)     \
478 {                                                                       \
479         TPS6591X_REGULATOR(_id, vdata, ldo1_ops, s_addr, s_nbits,       \
480                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0)  \
481 }
482
483 #define TPS6591X_LDO3(_id, vdata, s_addr, s_nbits, s_shift, s_type)     \
484 {                                                                       \
485         TPS6591X_REGULATOR(_id, vdata, ldo3_ops, s_addr, s_nbits,       \
486                         s_shift, s_type, INVALID, 0, 0, INVALID, 0, 0)  \
487 }
488
489 #define TPS6591X_VDD(_id, vdata, s_addr, s_nbits, s_shift, s_type,      \
490                         op_addr, op_nbits, op_shift, sr_addr, sr_nbits, \
491                         sr_shift)                                       \
492 {                                                                       \
493         TPS6591X_REGULATOR(_id, vdata, vdd_ops, s_addr, s_nbits,        \
494                         s_shift, s_type, op_addr, op_nbits, op_shift,   \
495                         sr_addr, sr_nbits, sr_shift)                    \
496 }
497
498 static struct tps6591x_regulator tps6591x_regulator[] = {
499         TPS6591X_VIO(VIO, vio, VIO, 2, 2, single_reg),
500         TPS6591X_LDO1(LDO_1, ldo124, LDO1, 6, 2, single_reg),
501         TPS6591X_LDO1(LDO_2, ldo124, LDO2, 6, 2, single_reg),
502         TPS6591X_LDO3(LDO_3, ldo35678, LDO3, 5, 2, single_reg),
503         TPS6591X_LDO1(LDO_4, ldo124, LDO4, 6, 2, single_reg),
504         TPS6591X_LDO3(LDO_5, ldo35678, LDO5, 5, 2, single_reg),
505         TPS6591X_LDO3(LDO_6, ldo35678, LDO6, 5, 2, single_reg),
506         TPS6591X_LDO3(LDO_7, ldo35678, LDO7, 5, 2, single_reg),
507         TPS6591X_LDO3(LDO_8, ldo35678, LDO8, 5, 2, single_reg),
508         TPS6591X_VDD(VDD_1, vdd, VDD1, 2, 0, sr_op_reg, VDD1_OP,
509                 7, 0, VDD1_SR, 7, 0),
510         TPS6591X_VDD(VDD_2, vdd, VDD2, 2, 0, sr_op_reg, VDD2_OP,
511                 7, 0, VDD2_SR, 7, 0),
512         TPS6591X_VDD(VDDCTRL, vdd, VDDCTRL, 2, 0, sr_op_reg,
513                 VDDCTRL_OP, 7, 0, VDDCTRL_SR, 7, 0),
514 };
515
516 static inline int tps6591x_regulator_preinit(struct device *parent,
517                 struct tps6591x_regulator *ri,
518                 struct tps6591x_regulator_platform_data *tps6591x_pdata)
519 {
520         int ret;
521
522         if (!tps6591x_pdata->init_apply)
523                 return 0;
524
525         if (tps6591x_pdata->init_uV >= 0) {
526                 switch (ri->desc.id) {
527                 case TPS6591X_ID_VIO:
528                         ret = __tps6591x_vio_set_voltage(parent, ri,
529                                         tps6591x_pdata->init_uV,
530                                         tps6591x_pdata->init_uV);
531                         break;
532
533                 case TPS6591X_ID_LDO_1:
534                 case TPS6591X_ID_LDO_2:
535                 case TPS6591X_ID_LDO_4:
536                         ret = __tps6591x_ldo1_set_voltage(parent, ri,
537                                         tps6591x_pdata->init_uV,
538                                         tps6591x_pdata->init_uV);
539                         break;
540
541                 case TPS6591X_ID_LDO_3:
542                 case TPS6591X_ID_LDO_5:
543                 case TPS6591X_ID_LDO_6:
544                 case TPS6591X_ID_LDO_7:
545                 case TPS6591X_ID_LDO_8:
546                         ret = __tps6591x_ldo3_set_voltage(parent, ri,
547                                         tps6591x_pdata->init_uV,
548                                         tps6591x_pdata->init_uV);
549                         break;
550
551                 case TPS6591X_ID_VDD_1:
552                 case TPS6591X_ID_VDD_2:
553                 case TPS6591X_ID_VDDCTRL:
554                         ret = __tps6591x_vdd_set_voltage(parent, ri,
555                                         tps6591x_pdata->init_uV,
556                                         tps6591x_pdata->init_uV);
557                         break;
558
559                 default:
560                         ret = -EINVAL;
561                         break;
562                 }
563                 if (ret < 0) {
564                         pr_err("Not able to initialize voltage %d for rail "
565                                 "%d err %d\n", tps6591x_pdata->init_uV,
566                                 ri->desc.id, ret);
567                         return ret;
568                 }
569         }
570
571         if (tps6591x_pdata->init_enable)
572                 ret = tps6591x_set_bits(parent, ri->supply_reg.addr, 0x1);
573         else
574                 ret = tps6591x_clr_bits(parent, ri->supply_reg.addr, 0x1);
575
576         if (ret < 0)
577                 pr_err("Not able to %s rail %d err %d\n",
578                         (tps6591x_pdata->init_enable) ? "enable" : "disable",
579                         ri->desc.id, ret);
580         return ret;
581 }
582
583 static inline struct tps6591x_regulator *find_regulator_info(int id)
584 {
585         struct tps6591x_regulator *ri;
586         int i;
587
588         for (i = 0; i < ARRAY_SIZE(tps6591x_regulator); i++) {
589                 ri = &tps6591x_regulator[i];
590                 if (ri->desc.id == id)
591                         return ri;
592         }
593         return NULL;
594 }
595
596 static int __devinit tps6591x_regulator_probe(struct platform_device *pdev)
597 {
598         struct tps6591x_regulator *ri = NULL;
599         struct regulator_dev *rdev;
600         struct tps6591x_regulator_platform_data *tps_pdata;
601         int id = pdev->id;
602         int err;
603
604         dev_dbg(&pdev->dev, "Probing reulator %d\n", id);
605
606         ri = find_regulator_info(id);
607         if (ri == NULL) {
608                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
609                 return -EINVAL;
610         }
611         tps_pdata = pdev->dev.platform_data;
612
613         err = tps6591x_regulator_preinit(pdev->dev.parent, ri, tps_pdata);
614         if (err)
615                 return err;
616
617         rdev = regulator_register(&ri->desc, &pdev->dev,
618                                 &tps_pdata->regulator, ri);
619         if (IS_ERR_OR_NULL(rdev)) {
620                 dev_err(&pdev->dev, "failed to register regulator %s\n",
621                                 ri->desc.name);
622                 return PTR_ERR(rdev);
623         }
624
625         platform_set_drvdata(pdev, rdev);
626
627         return 0;
628 }
629
630 static int __devexit tps6591x_regulator_remove(struct platform_device *pdev)
631 {
632         struct regulator_dev *rdev = platform_get_drvdata(pdev);
633
634         regulator_unregister(rdev);
635         return 0;
636 }
637
638 static struct platform_driver tps6591x_regulator_driver = {
639         .driver = {
640                 .name   = "tps6591x-regulator",
641                 .owner  = THIS_MODULE,
642         },
643         .probe          = tps6591x_regulator_probe,
644         .remove         = __devexit_p(tps6591x_regulator_remove),
645 };
646
647 static int __init tps6591x_regulator_init(void)
648 {
649         return platform_driver_register(&tps6591x_regulator_driver);
650 }
651 subsys_initcall(tps6591x_regulator_init);
652
653 static void __exit tps6591x_regulator_exit(void)
654 {
655         platform_driver_unregister(&tps6591x_regulator_driver);
656 }
657 module_exit(tps6591x_regulator_exit);
658
659 MODULE_LICENSE("GPL");
660 MODULE_DESCRIPTION("Regulator Driver for TI TPS6591X PMIC");
661 MODULE_ALIAS("platform:tps6591x-regulator");