regulator: core: support of supply name through desc
[linux-2.6.git] / drivers / regulator / tps65912-regulator.c
1 /*
2  * tps65912.c  --  TI tps65912
3  *
4  * Copyright 2011 Texas Instruments Inc.
5  *
6  * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under  the terms of the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the License, or (at your
11  *  option) any later version.
12  *
13  * This driver is based on wm8350 implementation.
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/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/gpio.h>
26 #include <linux/mfd/tps65912.h>
27
28 /* DCDC's */
29 #define TPS65912_REG_DCDC1      0
30 #define TPS65912_REG_DCDC2      1
31 #define TPS65912_REG_DCDC3      2
32 #define TPS65912_REG_DCDC4      3
33
34 /* LDOs */
35 #define TPS65912_REG_LDO1       4
36 #define TPS65912_REG_LDO2       5
37 #define TPS65912_REG_LDO3       6
38 #define TPS65912_REG_LDO4       7
39 #define TPS65912_REG_LDO5       8
40 #define TPS65912_REG_LDO6       9
41 #define TPS65912_REG_LDO7       10
42 #define TPS65912_REG_LDO8       11
43 #define TPS65912_REG_LDO9       12
44 #define TPS65912_REG_LDO10      13
45
46 /* Number of step-down converters available */
47 #define TPS65912_NUM_DCDC       4
48
49 /* Number of LDO voltage regulators  available */
50 #define TPS65912_NUM_LDO        10
51
52 /* Number of total regulators available */
53 #define TPS65912_NUM_REGULATOR          (TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
54
55 #define TPS65912_REG_ENABLED    0x80
56 #define OP_SELREG_MASK          0x40
57 #define OP_SELREG_SHIFT         6
58
59 struct tps_info {
60         const char *name;
61 };
62
63 static struct tps_info tps65912_regs[] = {
64         {
65                 .name = "DCDC1",
66         },
67         {
68                 .name = "DCDC2",
69         },
70         {
71                 .name = "DCDC3",
72         },
73         {
74                 .name = "DCDC4",
75         },
76         {
77                 .name = "LDO1",
78         },
79         {
80                 .name = "LDO2",
81         },
82         {
83                 .name = "LDO3",
84         },
85         {
86                 .name = "LDO4",
87         },
88         {
89                 .name = "LDO5",
90         },
91         {
92                 .name = "LDO6",
93         },
94         {
95                 .name = "LDO7",
96         },
97         {
98                 .name = "LDO8",
99         },
100         {
101                 .name = "LDO9",
102         },
103         {
104                 .name = "LDO10",
105         },
106 };
107
108 struct tps65912_reg {
109         struct regulator_desc desc[TPS65912_NUM_REGULATOR];
110         struct tps65912 *mfd;
111         struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
112         struct tps_info *info[TPS65912_NUM_REGULATOR];
113         /* for read/write access */
114         struct mutex io_lock;
115         int mode;
116         int (*get_ctrl_reg)(int);
117         int dcdc_range[TPS65912_NUM_DCDC];
118         int pwm_mode_reg;
119         int eco_reg;
120 };
121
122 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
123 {
124         struct tps65912 *mfd = pmic->mfd;
125         int range;
126
127         switch (id) {
128         case TPS65912_REG_DCDC1:
129                 range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
130                 break;
131         case TPS65912_REG_DCDC2:
132                 range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
133                 break;
134         case TPS65912_REG_DCDC3:
135                 range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
136                 break;
137         case TPS65912_REG_DCDC4:
138                 range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
139                 break;
140         default:
141                 return 0;
142         }
143
144         if (range >= 0)
145                 range = (range & DCDC_LIMIT_RANGE_MASK)
146                         >> DCDC_LIMIT_RANGE_SHIFT;
147
148         pmic->dcdc_range[id] = range;
149         return range;
150 }
151
152 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
153 {
154         unsigned long uv;
155
156         uv = ((vsel * 12500) + 500000);
157         return uv;
158 }
159
160 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
161 {
162         unsigned long uv;
163
164          uv = ((vsel * 12500) + 700000);
165         return uv;
166 }
167
168 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
169 {
170         unsigned long uv;
171
172         uv = ((vsel * 25000) + 500000);
173         return uv;
174 }
175
176 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
177 {
178         unsigned long uv;
179
180         if (vsel == 0x3f)
181                 uv = 3800000;
182         else
183                 uv = ((vsel * 50000) + 500000);
184
185         return uv;
186 }
187
188 static unsigned long tps65912_vsel_to_uv_ldo(u8 vsel)
189 {
190         unsigned long uv = 0;
191
192         if (vsel <= 32)
193                 uv = ((vsel * 25000) + 800000);
194         else if (vsel > 32 && vsel <= 60)
195                 uv = (((vsel - 32) * 50000) + 1600000);
196         else if (vsel > 60)
197                 uv = (((vsel - 60) * 100000) + 3000000);
198
199         return uv;
200 }
201
202 static int tps65912_get_ctrl_register(int id)
203 {
204         if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
205                 return id * 3 + TPS65912_DCDC1_AVS;
206         else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
207                 return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
208         else
209                 return -EINVAL;
210 }
211
212 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
213 {
214         struct tps65912 *mfd = pmic->mfd;
215         int opvsel;
216         u8 reg = 0;
217
218         if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
219                 opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
220                 if (opvsel & OP_SELREG_MASK)
221                         reg = id * 3 + TPS65912_DCDC1_AVS;
222                 else
223                         reg = id * 3 + TPS65912_DCDC1_OP;
224         } else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
225                 reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
226         } else {
227                 return -EINVAL;
228         }
229
230         return reg;
231 }
232
233 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
234 {
235         switch (id) {
236         case TPS65912_REG_DCDC1:
237                 pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
238                 pmic->eco_reg = TPS65912_DCDC1_AVS;
239                 break;
240         case TPS65912_REG_DCDC2:
241                 pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
242                 pmic->eco_reg = TPS65912_DCDC2_AVS;
243                 break;
244         case TPS65912_REG_DCDC3:
245                 pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
246                 pmic->eco_reg = TPS65912_DCDC3_AVS;
247                 break;
248         case TPS65912_REG_DCDC4:
249                 pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
250                 pmic->eco_reg = TPS65912_DCDC4_AVS;
251                 break;
252         default:
253                 return -EINVAL;
254         }
255
256         return 0;
257 }
258
259 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
260 {
261         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
262         struct tps65912 *mfd = pmic->mfd;
263         int reg, value, id = rdev_get_id(dev);
264
265         if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
266                 return -EINVAL;
267
268         reg = pmic->get_ctrl_reg(id);
269         if (reg < 0)
270                 return reg;
271
272         value = tps65912_reg_read(mfd, reg);
273         if (value < 0)
274                 return value;
275
276         return value & TPS65912_REG_ENABLED;
277 }
278
279 static int tps65912_reg_enable(struct regulator_dev *dev)
280 {
281         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
282         struct tps65912 *mfd = pmic->mfd;
283         int id = rdev_get_id(dev);
284         int reg;
285
286         if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
287                 return -EINVAL;
288
289         reg = pmic->get_ctrl_reg(id);
290         if (reg < 0)
291                 return reg;
292
293         return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
294 }
295
296 static int tps65912_reg_disable(struct regulator_dev *dev)
297 {
298         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
299         struct tps65912 *mfd = pmic->mfd;
300         int id = rdev_get_id(dev), reg;
301
302         reg = pmic->get_ctrl_reg(id);
303         if (reg < 0)
304                 return reg;
305
306         return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
307 }
308
309 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
310 {
311         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
312         struct tps65912 *mfd = pmic->mfd;
313         int pwm_mode, eco, id = rdev_get_id(dev);
314
315         tps65912_get_mode_regiters(pmic, id);
316
317         pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
318         eco = tps65912_reg_read(mfd, pmic->eco_reg);
319
320         pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
321         eco &= DCDC_AVS_ECO_MASK;
322
323         switch (mode) {
324         case REGULATOR_MODE_FAST:
325                 /* Verify if mode alredy set */
326                 if (pwm_mode && !eco)
327                         break;
328                 tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
329                 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
330                 break;
331         case REGULATOR_MODE_NORMAL:
332         case REGULATOR_MODE_IDLE:
333                 if (!pwm_mode && !eco)
334                         break;
335                 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
336                 tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
337                 break;
338         case REGULATOR_MODE_STANDBY:
339                 if (!pwm_mode && eco)
340                         break;
341                 tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
342                 tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
343                 break;
344         default:
345                 return -EINVAL;
346         }
347
348         return 0;
349 }
350
351 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
352 {
353         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
354         struct tps65912 *mfd = pmic->mfd;
355         int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
356
357         tps65912_get_mode_regiters(pmic, id);
358
359         pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
360         eco = tps65912_reg_read(mfd, pmic->eco_reg);
361
362         pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
363         eco &= DCDC_AVS_ECO_MASK;
364
365         if (pwm_mode && !eco)
366                 mode = REGULATOR_MODE_FAST;
367         else if (!pwm_mode && !eco)
368                 mode = REGULATOR_MODE_NORMAL;
369         else if (!pwm_mode && eco)
370                 mode = REGULATOR_MODE_STANDBY;
371
372         return mode;
373 }
374
375 static int tps65912_list_voltage_dcdc(struct regulator_dev *dev,
376                                         unsigned selector)
377 {
378         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
379         int range, voltage = 0, id = rdev_get_id(dev);
380
381         if (id > TPS65912_REG_DCDC4)
382                 return -EINVAL;
383
384         range = pmic->dcdc_range[id];
385
386         switch (range) {
387         case 0:
388                 /* 0.5 - 1.2875V in 12.5mV steps */
389                 voltage = tps65912_vsel_to_uv_range0(selector);
390                 break;
391         case 1:
392                 /* 0.7 - 1.4875V in 12.5mV steps */
393                 voltage = tps65912_vsel_to_uv_range1(selector);
394                 break;
395         case 2:
396                 /* 0.5 - 2.075V in 25mV steps */
397                 voltage = tps65912_vsel_to_uv_range2(selector);
398                 break;
399         case 3:
400                 /* 0.5 - 3.8V in 50mV steps */
401                 voltage = tps65912_vsel_to_uv_range3(selector);
402                 break;
403         }
404         return voltage;
405 }
406
407 static int tps65912_get_voltage_dcdc(struct regulator_dev *dev)
408 {
409         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
410         struct tps65912 *mfd = pmic->mfd;
411         int id = rdev_get_id(dev);
412         int opvsel = 0, avsel = 0, sr, vsel;
413
414         switch (id) {
415         case TPS65912_REG_DCDC1:
416                 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC1_OP);
417                 avsel = tps65912_reg_read(mfd, TPS65912_DCDC1_AVS);
418                 break;
419         case TPS65912_REG_DCDC2:
420                 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC2_OP);
421                 avsel = tps65912_reg_read(mfd, TPS65912_DCDC2_AVS);
422                 break;
423         case TPS65912_REG_DCDC3:
424                 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC3_OP);
425                 avsel = tps65912_reg_read(mfd, TPS65912_DCDC3_AVS);
426                 break;
427         case TPS65912_REG_DCDC4:
428                 opvsel = tps65912_reg_read(mfd, TPS65912_DCDC4_OP);
429                 avsel = tps65912_reg_read(mfd, TPS65912_DCDC4_AVS);
430                 break;
431         default:
432                 return -EINVAL;
433         }
434
435         sr = (opvsel & OP_SELREG_MASK) >> OP_SELREG_SHIFT;
436         if (sr)
437                 vsel = avsel;
438         else
439                 vsel = opvsel;
440         vsel &= 0x3F;
441
442         return tps65912_list_voltage_dcdc(dev, vsel);
443 }
444
445 static int tps65912_set_voltage_dcdc_sel(struct regulator_dev *dev,
446                                          unsigned selector)
447 {
448         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
449         struct tps65912 *mfd = pmic->mfd;
450         int id = rdev_get_id(dev);
451         int reg, vsel;
452
453         reg = tps65912_get_sel_register(pmic, id);
454         if (reg < 0)
455                 return reg;
456
457         vsel = tps65912_reg_read(mfd, reg);
458         vsel &= 0x3F;
459
460         return tps65912_list_voltage_dcdc(dev, vsel);
461 }
462
463 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
464                                          unsigned selector)
465 {
466         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
467         struct tps65912 *mfd = pmic->mfd;
468         int id = rdev_get_id(dev);
469         int value;
470         u8 reg;
471
472         reg = tps65912_get_sel_register(pmic, id);
473         value = tps65912_reg_read(mfd, reg);
474         value &= 0xC0;
475         return tps65912_reg_write(mfd, reg, selector | value);
476 }
477
478 static int tps65912_get_voltage_ldo(struct regulator_dev *dev)
479 {
480         struct tps65912_reg *pmic = rdev_get_drvdata(dev);
481         struct tps65912 *mfd = pmic->mfd;
482         int id = rdev_get_id(dev);
483         int vsel = 0;
484         u8 reg;
485
486         reg = tps65912_get_sel_register(pmic, id);
487         vsel = tps65912_reg_read(mfd, reg);
488         vsel &= 0x3F;
489
490         return tps65912_vsel_to_uv_ldo(vsel);
491 }
492
493 static int tps65912_list_voltage_ldo(struct regulator_dev *dev,
494                                         unsigned selector)
495 {
496         int ldo = rdev_get_id(dev);
497
498         if (ldo < TPS65912_REG_LDO1 || ldo > TPS65912_REG_LDO10)
499                 return -EINVAL;
500
501         return tps65912_vsel_to_uv_ldo(selector);
502 }
503
504 /* Operations permitted on DCDCx */
505 static struct regulator_ops tps65912_ops_dcdc = {
506         .is_enabled = tps65912_reg_is_enabled,
507         .enable = tps65912_reg_enable,
508         .disable = tps65912_reg_disable,
509         .set_mode = tps65912_set_mode,
510         .get_mode = tps65912_get_mode,
511         .get_voltage = tps65912_get_voltage_dcdc,
512         .set_voltage_sel = tps65912_set_voltage_dcdc_sel,
513         .list_voltage = tps65912_list_voltage_dcdc,
514 };
515
516 /* Operations permitted on LDOx */
517 static struct regulator_ops tps65912_ops_ldo = {
518         .is_enabled = tps65912_reg_is_enabled,
519         .enable = tps65912_reg_enable,
520         .disable = tps65912_reg_disable,
521         .get_voltage = tps65912_get_voltage_ldo,
522         .set_voltage_sel = tps65912_set_voltage_sel,
523         .list_voltage = tps65912_list_voltage_ldo,
524 };
525
526 static __devinit int tps65912_probe(struct platform_device *pdev)
527 {
528         struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
529         struct tps_info *info;
530         struct regulator_init_data *reg_data;
531         struct regulator_dev *rdev;
532         struct tps65912_reg *pmic;
533         struct tps65912_board *pmic_plat_data;
534         int i, err;
535
536         pmic_plat_data = dev_get_platdata(tps65912->dev);
537         if (!pmic_plat_data)
538                 return -EINVAL;
539
540         reg_data = pmic_plat_data->tps65912_pmic_init_data;
541
542         pmic = kzalloc(sizeof(*pmic), GFP_KERNEL);
543         if (!pmic)
544                 return -ENOMEM;
545
546         mutex_init(&pmic->io_lock);
547         pmic->mfd = tps65912;
548         platform_set_drvdata(pdev, pmic);
549
550         pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
551         info = tps65912_regs;
552
553         for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
554                 int range = 0;
555                 /* Register the regulators */
556                 pmic->info[i] = info;
557
558                 pmic->desc[i].name = info->name;
559                 pmic->desc[i].id = i;
560                 pmic->desc[i].n_voltages = 64;
561                 pmic->desc[i].ops = (i > TPS65912_REG_DCDC4 ?
562                         &tps65912_ops_ldo : &tps65912_ops_dcdc);
563                 pmic->desc[i].type = REGULATOR_VOLTAGE;
564                 pmic->desc[i].owner = THIS_MODULE;
565                 range = tps65912_get_range(pmic, i);
566                 rdev = regulator_register(&pmic->desc[i],
567                                         tps65912->dev, reg_data, pmic, NULL);
568                 if (IS_ERR(rdev)) {
569                         dev_err(tps65912->dev,
570                                 "failed to register %s regulator\n",
571                                 pdev->name);
572                         err = PTR_ERR(rdev);
573                         goto err;
574                 }
575
576                 /* Save regulator for cleanup */
577                 pmic->rdev[i] = rdev;
578         }
579         return 0;
580
581 err:
582         while (--i >= 0)
583                 regulator_unregister(pmic->rdev[i]);
584
585         kfree(pmic);
586         return err;
587 }
588
589 static int __devexit tps65912_remove(struct platform_device *pdev)
590 {
591         struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
592         int i;
593
594         for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
595                 regulator_unregister(tps65912_reg->rdev[i]);
596
597         kfree(tps65912_reg);
598         return 0;
599 }
600
601 static struct platform_driver tps65912_driver = {
602         .driver = {
603                 .name = "tps65912-pmic",
604                 .owner = THIS_MODULE,
605         },
606         .probe = tps65912_probe,
607         .remove = __devexit_p(tps65912_remove),
608 };
609
610 static int __init tps65912_init(void)
611 {
612         return platform_driver_register(&tps65912_driver);
613 }
614 subsys_initcall(tps65912_init);
615
616 static void __exit tps65912_cleanup(void)
617 {
618         platform_driver_unregister(&tps65912_driver);
619 }
620 module_exit(tps65912_cleanup);
621
622 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
623 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
624 MODULE_LICENSE("GPL v2");
625 MODULE_ALIAS("platform:tps65912-pmic");