max8907c regulator: fix unit error
[linux-2.6.git] / drivers / regulator / max8907c-regulator.c
1 /*
2  * max8907c-regulator.c -- support regulators in max8907c
3  *
4  * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/err.h>
15 #include <linux/platform_device.h>
16 #include <linux/regulator/driver.h>
17 #include <linux/mfd/max8907c.h>
18 #include <linux/regulator/max8907c-regulator.h>
19
20 #define MAX8907C_II2RR_VERSION_MASK     0xF0
21 #define MAX8907C_II2RR_VERSION_REV_A    0x00
22 #define MAX8907C_II2RR_VERSION_REV_B    0x10
23 #define MAX8907C_II2RR_VERSION_REV_C    0x30
24
25 #define MAX8907C_REGULATOR_CNT (ARRAY_SIZE(max8907c_regulators))
26
27 struct max8907c_regulator_info {
28         u32 min_uV;
29         u32 max_uV;
30         u32 step_uV;
31         u8 reg_base;
32         struct regulator_desc desc;
33 };
34
35 #define REG_LDO(ids, base, min, max, step) \
36         { \
37                 .min_uV = (min), \
38                 .max_uV = (max), \
39                 .step_uV = (step), \
40                 .reg_base = (base), \
41                 .desc = { \
42                         .name = #ids, \
43                         .id = MAX8907C_##ids, \
44                         .n_voltages = ((max) - (min)) / (step) + 1, \
45                         .ops = &max8907c_ldo_ops, \
46                         .type = REGULATOR_VOLTAGE, \
47                         .owner = THIS_MODULE, \
48                 }, \
49         }
50
51 #define REG_FIXED(ids, voltage) \
52         { \
53                 .min_uV = (voltage), \
54                 .max_uV = (voltage), \
55                 .desc = { \
56                         .name = #ids, \
57                         .id = MAX8907C_##ids, \
58                         .n_voltages = 1, \
59                         .ops = &max8907c_fixed_ops, \
60                         .type = REGULATOR_VOLTAGE, \
61                         .owner = THIS_MODULE, \
62                 }, \
63         }
64
65 #define REG_OUT5V(ids, base, voltage) \
66         { \
67                 .min_uV = (voltage), \
68                 .max_uV = (voltage), \
69                 .reg_base = (base), \
70                 .desc = { \
71                         .name = #ids, \
72                         .id = MAX8907C_##ids, \
73                         .n_voltages = 1, \
74                         .ops = &max8907c_out5v_ops, \
75                         .type = REGULATOR_VOLTAGE, \
76                         .owner = THIS_MODULE, \
77                 }, \
78         }
79
80 #define REG_BBAT(ids, base, min, max, step) \
81         { \
82                 .min_uV = (min), \
83                 .max_uV = (max), \
84                 .step_uV = (step), \
85                 .reg_base = (base), \
86                 .desc = { \
87                         .name = #ids, \
88                         .id = MAX8907C_##ids, \
89                         .n_voltages = ((max) - (min)) / (step) + 1, \
90                         .ops = &max8907c_bbat_ops, \
91                         .type = REGULATOR_VOLTAGE, \
92                         .owner = THIS_MODULE, \
93                 }, \
94         }
95
96 #define REG_WLED(ids, base, voltage) \
97         { \
98                 .min_uV = (voltage), \
99                 .max_uV = (voltage), \
100                 .reg_base = (base), \
101                 .desc = { \
102                         .name = #ids, \
103                         .id = MAX8907C_##ids, \
104                         .n_voltages = 1, \
105                         .ops = &max8907c_wled_ops, \
106                         .type = REGULATOR_CURRENT, \
107                         .owner = THIS_MODULE, \
108                 }, \
109         }
110
111 #define LDO_750_50(id, base) REG_LDO(id, (base), 750000, 3900000, 50000)
112 #define LDO_650_25(id, base) REG_LDO(id, (base), 650000, 2225000, 25000)
113
114 static int max8907c_regulator_list_voltage(struct regulator_dev *dev,
115                                            unsigned index);
116 static int max8907c_regulator_ldo_set_voltage(struct regulator_dev *dev,
117                                               int min_uV, int max_uV);
118 static int max8907c_regulator_bbat_set_voltage(struct regulator_dev *dev,
119                                                int min_uV, int max_uV);
120 static int max8907c_regulator_ldo_get_voltage(struct regulator_dev *dev);
121 static int max8907c_regulator_fixed_get_voltage(struct regulator_dev *dev);
122 static int max8907c_regulator_bbat_get_voltage(struct regulator_dev *dev);
123 static int max8907c_regulator_wled_set_current_limit(struct regulator_dev *dev,
124                                                      int min_uA, int max_uA);
125 static int max8907c_regulator_wled_get_current_limit(struct regulator_dev *dev);
126 static int max8907c_regulator_ldo_enable(struct regulator_dev *dev);
127 static int max8907c_regulator_out5v_enable(struct regulator_dev *dev);
128 static int max8907c_regulator_ldo_disable(struct regulator_dev *dev);
129 static int max8907c_regulator_out5v_disable(struct regulator_dev *dev);
130 static int max8907c_regulator_ldo_is_enabled(struct regulator_dev *dev);
131 static int max8907c_regulator_out5v_is_enabled(struct regulator_dev *dev);
132
133 static struct regulator_ops max8907c_ldo_ops = {
134         .list_voltage = max8907c_regulator_list_voltage,
135         .set_voltage = max8907c_regulator_ldo_set_voltage,
136         .get_voltage = max8907c_regulator_ldo_get_voltage,
137         .enable = max8907c_regulator_ldo_enable,
138         .disable = max8907c_regulator_ldo_disable,
139         .is_enabled = max8907c_regulator_ldo_is_enabled,
140 };
141
142 static struct regulator_ops max8907c_fixed_ops = {
143         .list_voltage = max8907c_regulator_list_voltage,
144         .get_voltage = max8907c_regulator_fixed_get_voltage,
145 };
146
147 static struct regulator_ops max8907c_out5v_ops = {
148         .list_voltage = max8907c_regulator_list_voltage,
149         .get_voltage = max8907c_regulator_fixed_get_voltage,
150         .enable = max8907c_regulator_out5v_enable,
151         .disable = max8907c_regulator_out5v_disable,
152         .is_enabled = max8907c_regulator_out5v_is_enabled,
153 };
154
155 static struct regulator_ops max8907c_bbat_ops = {
156         .list_voltage = max8907c_regulator_list_voltage,
157         .set_voltage = max8907c_regulator_bbat_set_voltage,
158         .get_voltage = max8907c_regulator_bbat_get_voltage,
159 };
160
161 static struct regulator_ops max8907c_wled_ops = {
162         .list_voltage = max8907c_regulator_list_voltage,
163         .set_current_limit = max8907c_regulator_wled_set_current_limit,
164         .get_current_limit = max8907c_regulator_wled_get_current_limit,
165         .get_voltage = max8907c_regulator_fixed_get_voltage,
166 };
167
168 static struct max8907c_regulator_info max8907c_regulators[] = {
169         REG_LDO(SD1, MAX8907C_REG_SDCTL1, 650000, 2225000, 25000),
170         REG_LDO(SD2, MAX8907C_REG_SDCTL2, 637500, 1425000, 12500),
171         REG_LDO(SD3, MAX8907C_REG_SDCTL3, 750000, 3900000, 50000),
172         LDO_750_50(LDO1, MAX8907C_REG_LDOCTL1),
173         LDO_650_25(LDO2, MAX8907C_REG_LDOCTL2),
174         LDO_650_25(LDO3, MAX8907C_REG_LDOCTL3),
175         LDO_750_50(LDO4, MAX8907C_REG_LDOCTL4),
176         LDO_750_50(LDO5, MAX8907C_REG_LDOCTL5),
177         LDO_750_50(LDO6, MAX8907C_REG_LDOCTL6),
178         LDO_750_50(LDO7, MAX8907C_REG_LDOCTL7),
179         LDO_750_50(LDO8, MAX8907C_REG_LDOCTL8),
180         LDO_750_50(LDO9, MAX8907C_REG_LDOCTL9),
181         LDO_750_50(LDO10, MAX8907C_REG_LDOCTL10),
182         LDO_750_50(LDO11, MAX8907C_REG_LDOCTL11),
183         LDO_750_50(LDO12, MAX8907C_REG_LDOCTL12),
184         LDO_750_50(LDO13, MAX8907C_REG_LDOCTL13),
185         LDO_750_50(LDO14, MAX8907C_REG_LDOCTL14),
186         LDO_750_50(LDO15, MAX8907C_REG_LDOCTL15),
187         LDO_750_50(LDO16, MAX8907C_REG_LDOCTL16),
188         LDO_650_25(LDO17, MAX8907C_REG_LDOCTL17),
189         LDO_650_25(LDO18, MAX8907C_REG_LDOCTL18),
190         LDO_750_50(LDO19, MAX8907C_REG_LDOCTL19),
191         LDO_750_50(LDO20, MAX8907C_REG_LDOCTL20),
192         REG_OUT5V(OUT5V, MAX8907C_REG_OUT5VEN, 5000000),
193         REG_OUT5V(OUT33V, MAX8907C_REG_OUT33VEN, 3300000),
194         REG_BBAT(BBAT, MAX8907C_REG_BBAT_CNFG, 2400000, 3000000, 200000),
195         REG_FIXED(SDBY, 1200000),
196         REG_FIXED(VRTC, 3300000),
197         REG_WLED(WLED, MAX8907C_REG_ILED_CNTL, 0),
198 };
199
200 static int max8907c_regulator_list_voltage(struct regulator_dev *rdev,
201                                            unsigned index)
202 {
203         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
204
205         return info->min_uV + info->step_uV * index;
206 }
207
208 static int max8907c_regulator_ldo_set_voltage(struct regulator_dev *rdev,
209                                               int min_uV, int max_uV)
210 {
211         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
212         struct device *parent = rdev_get_dev(rdev)->parent->parent;
213         int val;
214
215         if (min_uV < info->min_uV || max_uV > info->max_uV)
216                 return -EDOM;
217
218         val = (min_uV - info->min_uV) / info->step_uV;
219
220         return max8907c_reg_write(parent, info->reg_base + MAX8907C_VOUT, val);
221 }
222
223 static int max8907c_regulator_bbat_set_voltage(struct regulator_dev *rdev,
224                                                int min_uV, int max_uV)
225 {
226         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
227         struct device *parent = rdev_get_dev(rdev)->parent->parent;
228         int val;
229
230         if (min_uV < info->min_uV || max_uV > info->max_uV)
231                 return -EDOM;
232
233         val = (min_uV - info->min_uV) / info->step_uV;
234
235         return max8907c_set_bits(parent, info->reg_base, MAX8907C_MASK_VBBATTCV,
236                                  val);
237 }
238
239 static int max8907c_regulator_ldo_get_voltage(struct regulator_dev *rdev)
240 {
241         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
242         struct device *parent = rdev_get_dev(rdev)->parent->parent;
243         int val;
244
245         val = max8907c_reg_read(parent, info->reg_base + MAX8907C_VOUT);
246         return val * info->step_uV + info->min_uV;
247 }
248
249 static int max8907c_regulator_fixed_get_voltage(struct regulator_dev *rdev)
250 {
251         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
252
253         return info->min_uV;
254 }
255
256 static int max8907c_regulator_bbat_get_voltage(struct regulator_dev *rdev)
257 {
258         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
259         struct device *parent = rdev_get_dev(rdev)->parent->parent;
260         int val;
261
262         val =
263             max8907c_reg_read(parent, info->reg_base) & MAX8907C_MASK_VBBATTCV;
264         return val * info->step_uV + info->min_uV;
265 }
266
267 static int max8907c_regulator_wled_set_current_limit(struct regulator_dev *rdev,
268                                                      int min_uA, int max_uA)
269 {
270         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
271         struct device *parent = rdev_get_dev(rdev)->parent->parent;
272
273         if (min_uA > 25500)
274                 return -EDOM;
275
276         return max8907c_reg_write(parent, info->reg_base, min_uA / 100);
277 }
278
279 static int max8907c_regulator_wled_get_current_limit(struct regulator_dev *rdev)
280 {
281         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
282         struct device *parent = rdev_get_dev(rdev)->parent->parent;
283         int val;
284
285         val = max8907c_reg_read(parent, info->reg_base);
286         return val * 100;
287 }
288
289 static int max8907c_regulator_ldo_enable(struct regulator_dev *rdev)
290 {
291         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
292         struct device *parent = rdev_get_dev(rdev)->parent->parent;
293
294         return max8907c_set_bits(parent, info->reg_base + MAX8907C_CTL,
295                                  MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ,
296                                  MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ);
297 }
298
299 static int max8907c_regulator_out5v_enable(struct regulator_dev *rdev)
300 {
301         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
302         struct device *parent = rdev_get_dev(rdev)->parent->parent;
303
304         return max8907c_set_bits(parent, info->reg_base,
305                                  MAX8907C_MASK_OUT5V_VINEN |
306                                  MAX8907C_MASK_OUT5V_ENSRC |
307                                  MAX8907C_MASK_OUT5V_EN,
308                                  MAX8907C_MASK_OUT5V_ENSRC |
309                                  MAX8907C_MASK_OUT5V_EN);
310 }
311
312 static int max8907c_regulator_ldo_disable(struct regulator_dev *rdev)
313 {
314         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
315         struct device *parent = rdev_get_dev(rdev)->parent->parent;
316
317         return max8907c_set_bits(parent, info->reg_base + MAX8907C_CTL,
318                                  MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ,
319                                  MAX8907C_MASK_LDO_SEQ);
320 }
321
322 static int max8907c_regulator_out5v_disable(struct regulator_dev *rdev)
323 {
324         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
325         struct device *parent = rdev_get_dev(rdev)->parent->parent;
326
327         return max8907c_set_bits(parent, info->reg_base,
328                                  MAX8907C_MASK_OUT5V_VINEN |
329                                  MAX8907C_MASK_OUT5V_ENSRC |
330                                  MAX8907C_MASK_OUT5V_EN,
331                                  MAX8907C_MASK_OUT5V_ENSRC);
332 }
333
334 static int max8907c_regulator_ldo_is_enabled(struct regulator_dev *rdev)
335 {
336         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
337         struct device *parent = rdev_get_dev(rdev)->parent->parent;
338         int val;
339
340         val = max8907c_reg_read(parent, info->reg_base + MAX8907C_CTL);
341         if (val < 0)
342                 return -EDOM;
343
344         return (val & MAX8907C_MASK_LDO_EN) || !(val & MAX8907C_MASK_LDO_SEQ);
345 }
346
347 static int max8907c_regulator_out5v_is_enabled(struct regulator_dev *rdev)
348 {
349         const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev);
350         struct device *parent = rdev_get_dev(rdev)->parent->parent;
351         int val;
352
353         val = max8907c_reg_read(parent, info->reg_base);
354         if (val < 0)
355                 return -EDOM;
356
357         if ((val &
358              (MAX8907C_MASK_OUT5V_VINEN | MAX8907C_MASK_OUT5V_ENSRC |
359               MAX8907C_MASK_OUT5V_EN))
360             == MAX8907C_MASK_OUT5V_ENSRC)
361                 return 1;
362
363         return 0;
364 }
365
366 static int max8907c_regulator_probe(struct platform_device *pdev)
367 {
368         struct max8907c_regulator_info *info;
369         struct regulator_dev *rdev;
370         u8 version;
371
372         /* Backwards compatibility with max8907b, SD1 uses different voltages */
373         version = max8907c_reg_read(pdev->dev.parent, MAX8907C_REG_II2RR);
374         if ((version & MAX8907C_II2RR_VERSION_MASK) == MAX8907C_II2RR_VERSION_REV_B) {
375                 max8907c_regulators[MAX8907C_SD1].min_uV = 637500;
376                 max8907c_regulators[MAX8907C_SD1].max_uV = 1425000;
377                 max8907c_regulators[MAX8907C_SD1].step_uV = 12500;
378         }
379
380         info = &max8907c_regulators[pdev->id];
381
382         rdev = regulator_register(&info->desc,
383                                   &pdev->dev, pdev->dev.platform_data, info);
384         if (IS_ERR(rdev)) {
385                 dev_err(&pdev->dev, "Cannot register regulator \"%s\", %ld\n",
386                         info->desc.name, PTR_ERR(rdev));
387                 goto error;
388         }
389
390         platform_set_drvdata(pdev, rdev);
391         return 0;
392
393 error:
394         return PTR_ERR(rdev);
395 }
396
397 static int max8907c_regulator_remove(struct platform_device *pdev)
398 {
399         struct regulator_dev *rdev = platform_get_drvdata(pdev);
400
401         regulator_unregister(rdev);
402         return 0;
403 }
404
405 static struct platform_driver max8907c_regulator_driver = {
406         .driver = {
407                    .name = "max8907c-regulator",
408                    .owner = THIS_MODULE,
409                    },
410         .probe = max8907c_regulator_probe,
411         .remove = __devexit_p(max8907c_regulator_remove),
412 };
413
414 static int __init max8907c_regulator_init(void)
415 {
416         return platform_driver_register(&max8907c_regulator_driver);
417 }
418
419 subsys_initcall(max8907c_regulator_init);
420
421 static void __exit max8907c_reg_exit(void)
422 {
423         platform_driver_unregister(&max8907c_regulator_driver);
424 }
425
426 module_exit(max8907c_reg_exit);
427
428 MODULE_DESCRIPTION("MAX8907C regulator driver");
429 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
430 MODULE_LICENSE("GPL");