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