regulator: tps6586x: fix LDO0 voltage setting
[linux-2.6.git] / drivers / regulator / tps6586x-regulator.c
1 /*
2  * Regulator driver for TI TPS6586x
3  *
4  * Copyright (C) 2010 Compulab Ltd.
5  * Author: Mike Rapoport <mike@compulab.co.il>
6  *
7  * Based on da903x
8  * Copyright (C) 2006-2008 Marvell International Ltd.
9  * Copyright (C) 2008 Compulab Ltd.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
24 #include <linux/mfd/tps6586x.h>
25 #include <linux/delay.h>
26
27 /* supply control and voltage setting  */
28 #define TPS6586X_SUPPLYENA      0x10
29 #define TPS6586X_SUPPLYENB      0x11
30 #define TPS6586X_SUPPLYENC      0x12
31 #define TPS6586X_SUPPLYEND      0x13
32 #define TPS6586X_SUPPLYENE      0x14
33 #define TPS6586X_VCC1           0x20
34 #define TPS6586X_VCC2           0x21
35 #define TPS6586X_SM1V1          0x23
36 #define TPS6586X_SM1V2          0x24
37 #define TPS6586X_SM1SL          0x25
38 #define TPS6586X_SM0V1          0x26
39 #define TPS6586X_SM0V2          0x27
40 #define TPS6586X_SM0SL          0x28
41 #define TPS6586X_LDO2AV1        0x29
42 #define TPS6586X_LDO2AV2        0x2A
43 #define TPS6586X_LDO2BV1        0x2F
44 #define TPS6586X_LDO2BV2        0x30
45 #define TPS6586X_LDO4V1         0x32
46 #define TPS6586X_LDO4V2         0x33
47
48 /* converter settings  */
49 #define TPS6586X_SUPPLYV1       0x41
50 #define TPS6586X_SUPPLYV2       0x42
51 #define TPS6586X_SUPPLYV3       0x43
52 #define TPS6586X_SUPPLYV4       0x44
53 #define TPS6586X_SUPPLYV5       0x45
54 #define TPS6586X_SUPPLYV6       0x46
55 #define TPS6586X_SMODE1         0x47
56 #define TPS6586X_SMODE2         0x48
57
58 struct tps6586x_regulator {
59         struct regulator_desc desc;
60
61         int volt_reg;
62         int volt_shift;
63         int volt_nbits;
64         int enable_bit[2];
65         int enable_reg[2];
66         int *voltages;
67         int delay; /* delay in us for regulator to stabilize */
68
69         /* for DVM regulators */
70         int go_reg;
71         int go_bit;
72 };
73
74 static inline struct device *to_tps6586x_dev(struct regulator_dev *rdev)
75 {
76         return rdev_get_dev(rdev)->parent->parent;
77 }
78
79 static int tps6586x_ldo_list_voltage(struct regulator_dev *rdev,
80                                      unsigned selector)
81 {
82         struct tps6586x_regulator *info = rdev_get_drvdata(rdev);
83
84         return info->voltages[selector] * 1000;
85 }
86
87
88 static int __tps6586x_ldo_set_voltage(struct device *parent,
89                                       struct tps6586x_regulator *ri,
90                                       int min_uV, int max_uV,
91                                       unsigned *selector)
92 {
93         int val, uV;
94         uint8_t mask;
95
96         for (val = 0; val < ri->desc.n_voltages; val++) {
97                 uV = ri->voltages[val] * 1000;
98
99                 /* use the first in-range value */
100                 if (min_uV <= uV && uV <= max_uV) {
101
102                         *selector = val;
103
104                         val <<= ri->volt_shift;
105                         mask = ((1 << ri->volt_nbits) - 1) << ri->volt_shift;
106
107                         return tps6586x_update(parent, ri->volt_reg, val, mask);
108                 }
109         }
110
111         return -EINVAL;
112 }
113
114 static int tps6586x_ldo_set_voltage(struct regulator_dev *rdev,
115                                     int min_uV, int max_uV, unsigned *selector)
116 {
117         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
118         struct device *parent = to_tps6586x_dev(rdev);
119
120         return __tps6586x_ldo_set_voltage(parent, ri, min_uV, max_uV,
121                                           selector);
122 }
123
124 static int tps6586x_ldo_get_voltage(struct regulator_dev *rdev)
125 {
126         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
127         struct device *parent = to_tps6586x_dev(rdev);
128         uint8_t val, mask;
129         int ret;
130
131         ret = tps6586x_read(parent, ri->volt_reg, &val);
132         if (ret)
133                 return ret;
134
135         mask = ((1 << ri->volt_nbits) - 1) << ri->volt_shift;
136         val = (val & mask) >> ri->volt_shift;
137
138         if (val >= ri->desc.n_voltages)
139                 BUG();
140
141         return ri->voltages[val] * 1000;
142 }
143
144 static int tps6586x_dvm_set_voltage(struct regulator_dev *rdev,
145                                     int min_uV, int max_uV, unsigned *selector)
146 {
147         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
148         struct device *parent = to_tps6586x_dev(rdev);
149         int ret;
150
151         ret = __tps6586x_ldo_set_voltage(parent, ri, min_uV, max_uV,
152                                          selector);
153         if (ret)
154                 return ret;
155
156         return tps6586x_set_bits(parent, ri->go_reg, 1 << ri->go_bit);
157 }
158
159 static int tps6586x_regulator_enable(struct regulator_dev *rdev)
160 {
161         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
162         struct device *parent = to_tps6586x_dev(rdev);
163
164         return tps6586x_set_bits(parent, ri->enable_reg[0],
165                                  1 << ri->enable_bit[0]);
166 }
167
168 static int tps6586x_regulator_disable(struct regulator_dev *rdev)
169 {
170         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
171         struct device *parent = to_tps6586x_dev(rdev);
172
173         return tps6586x_clr_bits(parent, ri->enable_reg[0],
174                                  1 << ri->enable_bit[0]);
175 }
176
177 static int tps6586x_regulator_is_enabled(struct regulator_dev *rdev)
178 {
179         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
180         struct device *parent = to_tps6586x_dev(rdev);
181         uint8_t reg_val;
182         int ret;
183
184         ret = tps6586x_read(parent, ri->enable_reg[0], &reg_val);
185         if (ret)
186                 return ret;
187
188         return !!(reg_val & (1 << ri->enable_bit[0]));
189 }
190
191 static int tps6586x_regulator_enable_time(struct regulator_dev *rdev)
192 {
193         struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
194
195         return ri->delay;
196 }
197
198 static struct regulator_ops tps6586x_regulator_ldo_ops = {
199         .list_voltage = tps6586x_ldo_list_voltage,
200         .get_voltage = tps6586x_ldo_get_voltage,
201         .set_voltage = tps6586x_ldo_set_voltage,
202         .enable_time = tps6586x_regulator_enable_time,
203         .is_enabled = tps6586x_regulator_is_enabled,
204         .enable = tps6586x_regulator_enable,
205         .disable = tps6586x_regulator_disable,
206 };
207
208 static struct regulator_ops tps6586x_regulator_dvm_ops = {
209         .list_voltage = tps6586x_ldo_list_voltage,
210         .get_voltage = tps6586x_ldo_get_voltage,
211         .set_voltage = tps6586x_dvm_set_voltage,
212         .enable_time = tps6586x_regulator_enable_time,
213         .is_enabled = tps6586x_regulator_is_enabled,
214         .enable = tps6586x_regulator_enable,
215         .disable = tps6586x_regulator_disable,
216 };
217
218 static int tps6586x_ldo_voltages[] = {
219         1250, 1500, 1800, 2500, 2700, 2850, 3100, 3300,
220 };
221
222 static int tps6586x_ldo0_voltages[] = {
223         1200, 1500, 1800, 2500, 2700, 2850, 3100, 3300,
224 };
225
226 static int tps6586x_ldo4_voltages[] = {
227         1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
228         1900, 1925, 1950, 1975, 2000, 2025, 2050, 2075,
229         2100, 2125, 2150, 2175, 2200, 2225, 2250, 2275,
230         2300, 2325, 2350, 2375, 2400, 2425, 2450, 2475,
231 };
232
233 static int tps6586x_sm2_voltages[] = {
234         3000, 3050, 3100, 3150, 3200, 3250, 3300, 3350,
235         3400, 3450, 3500, 3550, 3600, 3650, 3700, 3750,
236         3800, 3850, 3900, 3950, 4000, 4050, 4100, 4150,
237         4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550,
238 };
239
240 static int tps6586x_dvm_voltages[] = {
241          725,  750,  775,  800,  825,  850,  875,  900,
242          925,  950,  975, 1000, 1025, 1050, 1075, 1100,
243         1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
244         1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
245 };
246
247 #define TPS6586X_REGULATOR(_id, vdata, _ops, vreg, shift, nbits,        \
248                            ereg0, ebit0, ereg1, ebit1, en_time)         \
249         .desc   = {                                                     \
250                 .name   = "REG-" #_id,                                  \
251                 .ops    = &tps6586x_regulator_##_ops,                   \
252                 .type   = REGULATOR_VOLTAGE,                            \
253                 .id     = TPS6586X_ID_##_id,                            \
254                 .n_voltages = ARRAY_SIZE(tps6586x_##vdata##_voltages),  \
255                 .owner  = THIS_MODULE,                                  \
256         },                                                              \
257         .volt_reg       = TPS6586X_##vreg,                              \
258         .volt_shift     = (shift),                                      \
259         .volt_nbits     = (nbits),                                      \
260         .enable_reg[0]  = TPS6586X_SUPPLY##ereg0,                       \
261         .enable_bit[0]  = (ebit0),                                      \
262         .enable_reg[1]  = TPS6586X_SUPPLY##ereg1,                       \
263         .enable_bit[1]  = (ebit1),                                      \
264         .voltages       = tps6586x_##vdata##_voltages,                  \
265         .delay          = en_time,
266
267 #define TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit)                      \
268         .go_reg = TPS6586X_##goreg,                                     \
269         .go_bit = (gobit),
270
271 #define TPS6586X_LDO(_id, vdata, vreg, shift, nbits,                    \
272                      ereg0, ebit0, ereg1, ebit1, en_time)               \
273 {                                                                       \
274         TPS6586X_REGULATOR(_id, vdata, ldo_ops, vreg, shift, nbits,     \
275                            ereg0, ebit0, ereg1, ebit1, en_time)         \
276 }
277
278 #define TPS6586X_DVM(_id, vdata, vreg, shift, nbits,                    \
279                      ereg0, ebit0, ereg1, ebit1, goreg, gobit, en_time) \
280 {                                                                       \
281         TPS6586X_REGULATOR(_id, vdata, dvm_ops, vreg, shift, nbits,     \
282                            ereg0, ebit0, ereg1, ebit1, en_time)         \
283         TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit)                      \
284 }
285
286 static struct tps6586x_regulator tps6586x_regulator[] = {
287         TPS6586X_LDO(LDO_0, ldo0, SUPPLYV1, 5, 3, ENC, 0, END, 0, 4000),
288         TPS6586X_LDO(LDO_1, dvm, SUPPLYV1, 0, 5, ENC, 1, END, 1, 4000),
289         TPS6586X_LDO(LDO_3, ldo, SUPPLYV4, 0, 3, ENC, 2, END, 2, 3000),
290         TPS6586X_LDO(LDO_5, ldo, SUPPLYV6, 0, 3, ENE, 6, ENE, 6, 3000),
291         TPS6586X_LDO(LDO_6, ldo, SUPPLYV3, 0, 3, ENC, 4, END, 4, 15000),
292         TPS6586X_LDO(LDO_7, ldo, SUPPLYV3, 3, 3, ENC, 5, END, 5, 15000),
293         TPS6586X_LDO(LDO_8, ldo, SUPPLYV2, 5, 3, ENC, 6, END, 6, 15000),
294         TPS6586X_LDO(LDO_9, ldo, SUPPLYV6, 3, 3, ENE, 7, ENE, 7, 3000),
295         TPS6586X_LDO(LDO_RTC, ldo, SUPPLYV4, 3, 3, V4, 7, V4, 7, 0),
296         TPS6586X_LDO(SM_2, sm2, SUPPLYV2, 0, 5, ENC, 7, END, 7, 0),
297
298         TPS6586X_DVM(LDO_2, dvm, LDO2BV1, 0, 5, ENA, 3, ENB, 3, VCC2, 6, 3000),
299         TPS6586X_DVM(SM_0, dvm, SM0V1, 0, 5, ENA, 1, ENB, 1, VCC1, 2, 4000),
300         TPS6586X_DVM(SM_1, dvm, SM1V1, 0, 5, ENA, 0, ENB, 0, VCC1, 0, 4000),
301         TPS6586X_DVM(LDO_4, ldo4, LDO4V1, 0, 5, ENC, 3, END, 3, VCC1, 6, 15000),
302 };
303
304 /*
305  * TPS6586X has 2 enable bits that are OR'ed to determine the actual
306  * regulator state. Clearing one of this bits allows switching
307  * regulator on and of with single register write.
308  */
309 static inline int tps6586x_regulator_preinit(struct device *parent,
310                                              struct tps6586x_regulator *ri)
311 {
312         uint8_t val1, val2;
313         int ret;
314
315         if (ri->enable_reg[0] == ri->enable_reg[1] &&
316             ri->enable_bit[0] == ri->enable_bit[1])
317                         return 0;
318
319         ret = tps6586x_read(parent, ri->enable_reg[0], &val1);
320         if (ret)
321                 return ret;
322
323         ret = tps6586x_read(parent, ri->enable_reg[1], &val2);
324         if (ret)
325                 return ret;
326
327         if (!(val2 & (1 << ri->enable_bit[1])))
328                 return 0;
329
330         /*
331          * The regulator is on, but it's enabled with the bit we don't
332          * want to use, so we switch the enable bits
333          */
334         if (!(val1 & (1 << ri->enable_bit[0]))) {
335                 ret = tps6586x_set_bits(parent, ri->enable_reg[0],
336                                         1 << ri->enable_bit[0]);
337                 if (ret)
338                         return ret;
339         }
340
341         return tps6586x_clr_bits(parent, ri->enable_reg[1],
342                                  1 << ri->enable_bit[1]);
343 }
344
345 static int tps6586x_regulator_set_slew_rate(struct platform_device *pdev)
346 {
347         struct device *parent = pdev->dev.parent;
348         struct regulator_init_data *p = pdev->dev.platform_data;
349         struct tps6586x_settings *setting = p->driver_data;
350         uint8_t reg;
351
352         if (setting == NULL)
353                 return 0;
354
355         if (!(setting->slew_rate & TPS6586X_SLEW_RATE_SET))
356                 return 0;
357
358         /* only SM0 and SM1 can have the slew rate settings */
359         switch (pdev->id) {
360         case TPS6586X_ID_SM_0:
361                 reg = TPS6586X_SM0SL;
362                 break;
363         case TPS6586X_ID_SM_1:
364                 reg = TPS6586X_SM1SL;
365                 break;
366         default:
367                 dev_warn(&pdev->dev, "Only SM0/SM1 can set slew rate\n");
368                 return -EINVAL;
369         }
370
371         return tps6586x_write(parent, reg,
372                         setting->slew_rate & TPS6586X_SLEW_RATE_MASK);
373 }
374
375 static inline int tps6586x_regulator_set_pwm_mode(struct platform_device *pdev)
376 {
377         struct device *parent = pdev->dev.parent;
378         struct regulator_init_data *p = pdev->dev.platform_data;
379         struct tps6586x_settings *setting = p->driver_data;
380         int ret = 0;
381         uint8_t mask;
382
383         if (setting == NULL)
384                 return 0;
385
386         switch (pdev->id) {
387         case TPS6586X_ID_SM_0:
388                 mask = 1 << SM0_PWM_BIT;
389                 break;
390         case TPS6586X_ID_SM_1:
391                 mask = 1 << SM1_PWM_BIT;
392                 break;
393         case TPS6586X_ID_SM_2:
394                 mask = 1 << SM2_PWM_BIT;
395                 break;
396         default:
397                 /* not all regulators have PWM/PFM option */
398                 return 0;
399         }
400
401         if (setting->sm_pwm_mode == PWM_ONLY)
402                 ret = tps6586x_set_bits(parent, TPS6586X_SMODE1, mask);
403         else if (setting->sm_pwm_mode == AUTO_PWM_PFM)
404                 ret = tps6586x_clr_bits(parent, TPS6586X_SMODE1, mask);
405
406         return ret;
407 }
408 static inline struct tps6586x_regulator *find_regulator_info(int id)
409 {
410         struct tps6586x_regulator *ri;
411         int i;
412
413         for (i = 0; i < ARRAY_SIZE(tps6586x_regulator); i++) {
414                 ri = &tps6586x_regulator[i];
415                 if (ri->desc.id == id)
416                         return ri;
417         }
418         return NULL;
419 }
420
421 static int __devinit tps6586x_regulator_probe(struct platform_device *pdev)
422 {
423         struct tps6586x_regulator *ri = NULL;
424         struct regulator_dev *rdev;
425         int id = pdev->id;
426         int err;
427
428         dev_dbg(&pdev->dev, "Probing reulator %d\n", id);
429
430         ri = find_regulator_info(id);
431         if (ri == NULL) {
432                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
433                 return -EINVAL;
434         }
435
436         err = tps6586x_regulator_preinit(pdev->dev.parent, ri);
437         if (err)
438                 return err;
439
440         rdev = regulator_register(&ri->desc, &pdev->dev,
441                                   pdev->dev.platform_data, ri, NULL);
442         if (IS_ERR(rdev)) {
443                 dev_err(&pdev->dev, "failed to register regulator %s\n",
444                                 ri->desc.name);
445                 return PTR_ERR(rdev);
446         }
447
448         platform_set_drvdata(pdev, rdev);
449
450         err = tps6586x_regulator_set_slew_rate(pdev);
451         if (err)
452                 return err;
453
454         return tps6586x_regulator_set_pwm_mode(pdev);
455 }
456
457 static int __devexit tps6586x_regulator_remove(struct platform_device *pdev)
458 {
459         struct regulator_dev *rdev = platform_get_drvdata(pdev);
460
461         regulator_unregister(rdev);
462         return 0;
463 }
464
465 static struct platform_driver tps6586x_regulator_driver = {
466         .driver = {
467                 .name   = "tps6586x-regulator",
468                 .owner  = THIS_MODULE,
469         },
470         .probe          = tps6586x_regulator_probe,
471         .remove         = __devexit_p(tps6586x_regulator_remove),
472 };
473
474 static int __init tps6586x_regulator_init(void)
475 {
476         return platform_driver_register(&tps6586x_regulator_driver);
477 }
478 subsys_initcall(tps6586x_regulator_init);
479
480 static void __exit tps6586x_regulator_exit(void)
481 {
482         platform_driver_unregister(&tps6586x_regulator_driver);
483 }
484 module_exit(tps6586x_regulator_exit);
485
486 MODULE_LICENSE("GPL");
487 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
488 MODULE_DESCRIPTION("Regulator Driver for TI TPS6586X PMIC");
489 MODULE_ALIAS("platform:tps6586x-regulator");