regulator: da9052: fix bug in device tree iteration loop
[linux-3.10.git] / drivers / regulator / da9052-regulator.c
1 /*
2 * da9052-regulator.c: Regulator driver for DA9052
3 *
4 * Copyright(c) 2011 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
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 */
14
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #ifdef CONFIG_OF
23 #include <linux/of.h>
24 #include <linux/regulator/of_regulator.h>
25 #endif
26
27 #include <linux/mfd/da9052/da9052.h>
28 #include <linux/mfd/da9052/reg.h>
29 #include <linux/mfd/da9052/pdata.h>
30
31 /* Buck step size */
32 #define DA9052_BUCK_PERI_3uV_STEP               100000
33 #define DA9052_BUCK_PERI_REG_MAP_UPTO_3uV       24
34 #define DA9052_CONST_3uV                        3000000
35
36 #define DA9052_MIN_UA           0
37 #define DA9052_MAX_UA           3
38 #define DA9052_CURRENT_RANGE    4
39
40 /* Bit masks */
41 #define DA9052_BUCK_ILIM_MASK_EVEN      0x0c
42 #define DA9052_BUCK_ILIM_MASK_ODD       0xc0
43
44 /* DA9052 REGULATOR IDs */
45 #define DA9052_ID_BUCK1         0
46 #define DA9052_ID_BUCK2         1
47 #define DA9052_ID_BUCK3         2
48 #define DA9052_ID_BUCK4         3
49 #define DA9052_ID_LDO1          4
50 #define DA9052_ID_LDO2          5
51 #define DA9052_ID_LDO3          6
52 #define DA9052_ID_LDO4          7
53 #define DA9052_ID_LDO5          8
54 #define DA9052_ID_LDO6          9
55 #define DA9052_ID_LDO7          10
56 #define DA9052_ID_LDO8          11
57 #define DA9052_ID_LDO9          12
58 #define DA9052_ID_LDO10         13
59
60 static const u32 da9052_current_limits[3][4] = {
61         {700000, 800000, 1000000, 1200000},     /* DA9052-BC BUCKs */
62         {1600000, 2000000, 2400000, 3000000},   /* DA9053-AA/Bx BUCK-CORE */
63         {800000, 1000000, 1200000, 1500000},    /* DA9053-AA/Bx BUCK-PRO,
64                                                  * BUCK-MEM and BUCK-PERI
65                                                 */
66 };
67
68 struct da9052_regulator_info {
69         struct regulator_desc reg_desc;
70         int step_uV;
71         int min_uV;
72         int max_uV;
73         unsigned char volt_shift;
74         unsigned char en_bit;
75         unsigned char activate_bit;
76 };
77
78 struct da9052_regulator {
79         struct da9052 *da9052;
80         struct da9052_regulator_info *info;
81         struct regulator_dev *rdev;
82 };
83
84 static int verify_range(struct da9052_regulator_info *info,
85                          int min_uV, int max_uV)
86 {
87         if (min_uV > info->max_uV || max_uV < info->min_uV)
88                 return -EINVAL;
89
90         return 0;
91 }
92
93 static int da9052_regulator_enable(struct regulator_dev *rdev)
94 {
95         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
96         struct da9052_regulator_info *info = regulator->info;
97         int offset = rdev_get_id(rdev);
98
99         return da9052_reg_update(regulator->da9052,
100                                  DA9052_BUCKCORE_REG + offset,
101                                  1 << info->en_bit, 1 << info->en_bit);
102 }
103
104 static int da9052_regulator_disable(struct regulator_dev *rdev)
105 {
106         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
107         struct da9052_regulator_info *info = regulator->info;
108         int offset = rdev_get_id(rdev);
109
110         return da9052_reg_update(regulator->da9052,
111                                  DA9052_BUCKCORE_REG + offset,
112                                  1 << info->en_bit, 0);
113 }
114
115 static int da9052_regulator_is_enabled(struct regulator_dev *rdev)
116 {
117         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
118         struct da9052_regulator_info *info = regulator->info;
119         int offset = rdev_get_id(rdev);
120         int ret;
121
122         ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
123         if (ret < 0)
124                 return ret;
125
126         return ret & (1 << info->en_bit);
127 }
128
129 static int da9052_dcdc_get_current_limit(struct regulator_dev *rdev)
130 {
131         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
132         int offset = rdev_get_id(rdev);
133         int ret, row = 2;
134
135         ret = da9052_reg_read(regulator->da9052, DA9052_BUCKA_REG + offset/2);
136         if (ret < 0)
137                 return ret;
138
139         /* Determine the even or odd position of the buck current limit
140          * register field
141         */
142         if (offset % 2 == 0)
143                 ret = (ret & DA9052_BUCK_ILIM_MASK_EVEN) >> 2;
144         else
145                 ret = (ret & DA9052_BUCK_ILIM_MASK_ODD) >> 6;
146
147         /* Select the appropriate current limit range */
148         if (regulator->da9052->chip_id == DA9052)
149                 row = 0;
150         else if (offset == 0)
151                 row = 1;
152
153         return da9052_current_limits[row][ret];
154 }
155
156 static int da9052_dcdc_set_current_limit(struct regulator_dev *rdev, int min_uA,
157                                           int max_uA)
158 {
159         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
160         int offset = rdev_get_id(rdev);
161         int reg_val = 0;
162         int i, row = 2;
163
164         /* Select the appropriate current limit range */
165         if (regulator->da9052->chip_id == DA9052)
166                 row = 0;
167         else if (offset == 0)
168                 row = 1;
169
170         if (min_uA > da9052_current_limits[row][DA9052_MAX_UA] ||
171             max_uA < da9052_current_limits[row][DA9052_MIN_UA])
172                 return -EINVAL;
173
174         for (i = 0; i < DA9052_CURRENT_RANGE; i++) {
175                 if (min_uA <= da9052_current_limits[row][i]) {
176                         reg_val = i;
177                         break;
178                 }
179         }
180
181         /* Determine the even or odd position of the buck current limit
182          * register field
183         */
184         if (offset % 2 == 0)
185                 return da9052_reg_update(regulator->da9052,
186                                          DA9052_BUCKA_REG + offset/2,
187                                          DA9052_BUCK_ILIM_MASK_EVEN,
188                                          reg_val << 2);
189         else
190                 return da9052_reg_update(regulator->da9052,
191                                          DA9052_BUCKA_REG + offset/2,
192                                          DA9052_BUCK_ILIM_MASK_ODD,
193                                          reg_val << 6);
194 }
195
196 static int da9052_list_voltage(struct regulator_dev *rdev,
197                                 unsigned int selector)
198 {
199         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
200         struct da9052_regulator_info *info = regulator->info;
201         int id = rdev_get_id(rdev);
202         int volt_uV;
203
204         if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
205                 && (selector >= DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)) {
206                 volt_uV = ((DA9052_BUCK_PERI_REG_MAP_UPTO_3uV * info->step_uV)
207                           + info->min_uV);
208                 volt_uV += (selector - DA9052_BUCK_PERI_REG_MAP_UPTO_3uV)
209                                     * (DA9052_BUCK_PERI_3uV_STEP);
210         } else {
211                 volt_uV = (selector * info->step_uV) + info->min_uV;
212         }
213
214         if (volt_uV > info->max_uV)
215                 return -EINVAL;
216
217         return volt_uV;
218 }
219
220 static int da9052_regulator_set_voltage(struct regulator_dev *rdev,
221                                              int min_uV, int max_uV,
222                                              unsigned int *selector)
223 {
224         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
225         struct da9052_regulator_info *info = regulator->info;
226         int id = rdev_get_id(rdev);
227         int ret;
228
229         ret = verify_range(info, min_uV, max_uV);
230         if (ret < 0)
231                 return ret;
232
233         if (min_uV < info->min_uV)
234                 min_uV = info->min_uV;
235
236         if ((id == DA9052_ID_BUCK4) && (regulator->da9052->chip_id == DA9052)
237                 && (min_uV >= DA9052_CONST_3uV)) {
238                         *selector = DA9052_BUCK_PERI_REG_MAP_UPTO_3uV +
239                                     DIV_ROUND_UP(min_uV - DA9052_CONST_3uV,
240                                                  DA9052_BUCK_PERI_3uV_STEP);
241         } else {
242                 *selector = DIV_ROUND_UP(min_uV - info->min_uV, info->step_uV);
243         }
244
245         ret = da9052_list_voltage(rdev, *selector);
246         if (ret < 0)
247                 return ret;
248
249         ret = da9052_reg_update(regulator->da9052,
250                                  DA9052_BUCKCORE_REG + id,
251                                  (1 << info->volt_shift) - 1, *selector);
252         if (ret < 0)
253                 return ret;
254
255         /* Some LDOs and DCDCs are DVC controlled which requires enabling of
256          * the activate bit to implment the changes on the output.
257          */
258         switch (id) {
259         case DA9052_ID_BUCK1:
260         case DA9052_ID_BUCK2:
261         case DA9052_ID_BUCK3:
262         case DA9052_ID_LDO2:
263         case DA9052_ID_LDO3:
264                 ret = da9052_reg_update(regulator->da9052, DA9052_SUPPLY_REG,
265                                         info->activate_bit, info->activate_bit);
266                 break;
267         }
268
269         return ret;
270 }
271
272 static int da9052_get_regulator_voltage_sel(struct regulator_dev *rdev)
273 {
274         struct da9052_regulator *regulator = rdev_get_drvdata(rdev);
275         struct da9052_regulator_info *info = regulator->info;
276         int offset = rdev_get_id(rdev);
277         int ret;
278
279         ret = da9052_reg_read(regulator->da9052, DA9052_BUCKCORE_REG + offset);
280         if (ret < 0)
281                 return ret;
282
283         ret &= ((1 << info->volt_shift) - 1);
284
285         return ret;
286 }
287
288 static struct regulator_ops da9052_dcdc_ops = {
289         .set_voltage = da9052_regulator_set_voltage,
290         .get_current_limit = da9052_dcdc_get_current_limit,
291         .set_current_limit = da9052_dcdc_set_current_limit,
292
293         .list_voltage = da9052_list_voltage,
294         .get_voltage_sel = da9052_get_regulator_voltage_sel,
295         .is_enabled = da9052_regulator_is_enabled,
296         .enable = da9052_regulator_enable,
297         .disable = da9052_regulator_disable,
298 };
299
300 static struct regulator_ops da9052_ldo_ops = {
301         .set_voltage = da9052_regulator_set_voltage,
302
303         .list_voltage = da9052_list_voltage,
304         .get_voltage_sel = da9052_get_regulator_voltage_sel,
305         .is_enabled = da9052_regulator_is_enabled,
306         .enable = da9052_regulator_enable,
307         .disable = da9052_regulator_disable,
308 };
309
310 #define DA9052_LDO(_id, step, min, max, sbits, ebits, abits) \
311 {\
312         .reg_desc = {\
313                 .name = #_id,\
314                 .ops = &da9052_ldo_ops,\
315                 .type = REGULATOR_VOLTAGE,\
316                 .id = DA9052_ID_##_id,\
317                 .n_voltages = (max - min) / step + 1, \
318                 .owner = THIS_MODULE,\
319         },\
320         .min_uV = (min) * 1000,\
321         .max_uV = (max) * 1000,\
322         .step_uV = (step) * 1000,\
323         .volt_shift = (sbits),\
324         .en_bit = (ebits),\
325         .activate_bit = (abits),\
326 }
327
328 #define DA9052_DCDC(_id, step, min, max, sbits, ebits, abits) \
329 {\
330         .reg_desc = {\
331                 .name = #_id,\
332                 .ops = &da9052_dcdc_ops,\
333                 .type = REGULATOR_VOLTAGE,\
334                 .id = DA9052_ID_##_id,\
335                 .n_voltages = (max - min) / step + 1, \
336                 .owner = THIS_MODULE,\
337         },\
338         .min_uV = (min) * 1000,\
339         .max_uV = (max) * 1000,\
340         .step_uV = (step) * 1000,\
341         .volt_shift = (sbits),\
342         .en_bit = (ebits),\
343         .activate_bit = (abits),\
344 }
345
346 static struct da9052_regulator_info da9052_regulator_info[] = {
347         DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
348         DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
349         DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
350         DA9052_DCDC(BUCK4, 50, 1800, 3600, 5, 6, 0),
351         DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
352         DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
353         DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
354         DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
355         DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
356         DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
357         DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
358         DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
359         DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
360         DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
361 };
362
363 static struct da9052_regulator_info da9053_regulator_info[] = {
364         DA9052_DCDC(BUCK1, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBCOREGO),
365         DA9052_DCDC(BUCK2, 25, 500, 2075, 6, 6, DA9052_SUPPLY_VBPROGO),
366         DA9052_DCDC(BUCK3, 25, 925, 2500, 6, 6, DA9052_SUPPLY_VBMEMGO),
367         DA9052_DCDC(BUCK4, 25, 925, 2500, 6, 6, 0),
368         DA9052_LDO(LDO1, 50, 600, 1800, 5, 6, 0),
369         DA9052_LDO(LDO2, 25, 600, 1800, 6, 6, DA9052_SUPPLY_VLDO2GO),
370         DA9052_LDO(LDO3, 25, 1725, 3300, 6, 6, DA9052_SUPPLY_VLDO3GO),
371         DA9052_LDO(LDO4, 25, 1725, 3300, 6, 6, 0),
372         DA9052_LDO(LDO5, 50, 1200, 3600, 6, 6, 0),
373         DA9052_LDO(LDO6, 50, 1200, 3600, 6, 6, 0),
374         DA9052_LDO(LDO7, 50, 1200, 3600, 6, 6, 0),
375         DA9052_LDO(LDO8, 50, 1200, 3600, 6, 6, 0),
376         DA9052_LDO(LDO9, 50, 1250, 3650, 6, 6, 0),
377         DA9052_LDO(LDO10, 50, 1200, 3600, 6, 6, 0),
378 };
379
380 static inline struct da9052_regulator_info *find_regulator_info(u8 chip_id,
381                                                                  int id)
382 {
383         struct da9052_regulator_info *info;
384         int i;
385
386         switch (chip_id) {
387         case DA9052:
388                 for (i = 0; i < ARRAY_SIZE(da9052_regulator_info); i++) {
389                         info = &da9052_regulator_info[i];
390                         if (info->reg_desc.id == id)
391                                 return info;
392                 }
393                 break;
394         case DA9053_AA:
395         case DA9053_BA:
396         case DA9053_BB:
397                 for (i = 0; i < ARRAY_SIZE(da9053_regulator_info); i++) {
398                         info = &da9053_regulator_info[i];
399                         if (info->reg_desc.id == id)
400                                 return info;
401                 }
402                 break;
403         }
404
405         return NULL;
406 }
407
408 static int __devinit da9052_regulator_probe(struct platform_device *pdev)
409 {
410         struct regulator_config config = { };
411         struct da9052_regulator *regulator;
412         struct da9052 *da9052;
413         struct da9052_pdata *pdata;
414
415         regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9052_regulator),
416                                  GFP_KERNEL);
417         if (!regulator)
418                 return -ENOMEM;
419
420         da9052 = dev_get_drvdata(pdev->dev.parent);
421         pdata = da9052->dev->platform_data;
422         regulator->da9052 = da9052;
423
424         regulator->info = find_regulator_info(regulator->da9052->chip_id,
425                                               pdev->id);
426         if (regulator->info == NULL) {
427                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
428                 return -EINVAL;
429         }
430
431         config.dev = &pdev->dev;
432         config.driver_data = regulator;
433         if (pdata && pdata->regulators) {
434                 config.init_data = pdata->regulators[pdev->id];
435         } else {
436 #ifdef CONFIG_OF
437                 struct device_node *nproot = da9052->dev->of_node;
438                 struct device_node *np;
439
440                 if (!nproot)
441                         return -ENODEV;
442
443                 nproot = of_find_node_by_name(nproot, "regulators");
444                 if (!nproot)
445                         return -ENODEV;
446
447                 for (np = of_get_next_child(nproot, NULL); np;
448                      np = of_get_next_child(nproot, np)) {
449                         if (!of_node_cmp(np->name,
450                                          regulator->info->reg_desc.name)) {
451                                 config.init_data = of_get_regulator_init_data(
452                                         &pdev->dev, np);
453                                 break;
454                         }
455                 }
456 #endif
457         }
458
459         regulator->rdev = regulator_register(&regulator->info->reg_desc,
460                                              &config);
461         if (IS_ERR(regulator->rdev)) {
462                 dev_err(&pdev->dev, "failed to register regulator %s\n",
463                         regulator->info->reg_desc.name);
464                 return PTR_ERR(regulator->rdev);
465         }
466
467         platform_set_drvdata(pdev, regulator);
468
469         return 0;
470 }
471
472 static int __devexit da9052_regulator_remove(struct platform_device *pdev)
473 {
474         struct da9052_regulator *regulator = platform_get_drvdata(pdev);
475
476         regulator_unregister(regulator->rdev);
477         return 0;
478 }
479
480 static struct platform_driver da9052_regulator_driver = {
481         .probe = da9052_regulator_probe,
482         .remove = __devexit_p(da9052_regulator_remove),
483         .driver = {
484                 .name = "da9052-regulator",
485                 .owner = THIS_MODULE,
486         },
487 };
488
489 static int __init da9052_regulator_init(void)
490 {
491         return platform_driver_register(&da9052_regulator_driver);
492 }
493 subsys_initcall(da9052_regulator_init);
494
495 static void __exit da9052_regulator_exit(void)
496 {
497         platform_driver_unregister(&da9052_regulator_driver);
498 }
499 module_exit(da9052_regulator_exit);
500
501 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
502 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9052 PMIC");
503 MODULE_LICENSE("GPL");
504 MODULE_ALIAS("platform:da9052-regulator");