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