regulator: pass additional of_node to regulator_register()
[linux-2.6.git] / drivers / regulator / max8925-regulator.c
1 /*
2  * Regulators driver for Maxim max8925
3  *
4  * Copyright (C) 2009 Marvell International Ltd.
5  *      Haojian Zhuang <haojian.zhuang@marvell.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/i2c.h>
16 #include <linux/platform_device.h>
17 #include <linux/regulator/driver.h>
18 #include <linux/regulator/machine.h>
19 #include <linux/mfd/max8925.h>
20
21 #define SD1_DVM_VMIN            850000
22 #define SD1_DVM_VMAX            1000000
23 #define SD1_DVM_STEP            50000
24 #define SD1_DVM_SHIFT           5               /* SDCTL1 bit5 */
25 #define SD1_DVM_EN              6               /* SDV1 bit 6 */
26
27 /* bit definitions in SD & LDO control registers */
28 #define OUT_ENABLE              0x1f            /* Power U/D sequence as I2C */
29 #define OUT_DISABLE             0x1e            /* Power U/D sequence as I2C */
30
31 struct max8925_regulator_info {
32         struct regulator_desc   desc;
33         struct regulator_dev    *regulator;
34         struct i2c_client       *i2c;
35         struct max8925_chip     *chip;
36
37         int     min_uV;
38         int     max_uV;
39         int     step_uV;
40         int     vol_reg;
41         int     vol_shift;
42         int     vol_nbits;
43         int     enable_bit;
44         int     enable_reg;
45 };
46
47 static inline int check_range(struct max8925_regulator_info *info,
48                               int min_uV, int max_uV)
49 {
50         if (min_uV < info->min_uV || min_uV > info->max_uV)
51                 return -EINVAL;
52
53         return 0;
54 }
55
56 static int max8925_list_voltage(struct regulator_dev *rdev, unsigned index)
57 {
58         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
59         return info->min_uV + index * info->step_uV;
60 }
61
62 static int max8925_set_voltage(struct regulator_dev *rdev,
63                                int min_uV, int max_uV, unsigned int *selector)
64 {
65         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
66         unsigned char data, mask;
67
68         if (check_range(info, min_uV, max_uV)) {
69                 dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
70                         min_uV, max_uV);
71                 return -EINVAL;
72         }
73         data = (min_uV - info->min_uV + info->step_uV - 1) / info->step_uV;
74         *selector = data;
75         data <<= info->vol_shift;
76         mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
77
78         return max8925_set_bits(info->i2c, info->vol_reg, mask, data);
79 }
80
81 static int max8925_get_voltage(struct regulator_dev *rdev)
82 {
83         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
84         unsigned char data, mask;
85         int ret;
86
87         ret = max8925_reg_read(info->i2c, info->vol_reg);
88         if (ret < 0)
89                 return ret;
90         mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
91         data = (ret & mask) >> info->vol_shift;
92
93         return max8925_list_voltage(rdev, data);
94 }
95
96 static int max8925_enable(struct regulator_dev *rdev)
97 {
98         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
99
100         return max8925_set_bits(info->i2c, info->enable_reg,
101                                 OUT_ENABLE << info->enable_bit,
102                                 OUT_ENABLE << info->enable_bit);
103 }
104
105 static int max8925_disable(struct regulator_dev *rdev)
106 {
107         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
108
109         return max8925_set_bits(info->i2c, info->enable_reg,
110                                 OUT_ENABLE << info->enable_bit,
111                                 OUT_DISABLE << info->enable_bit);
112 }
113
114 static int max8925_is_enabled(struct regulator_dev *rdev)
115 {
116         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
117         int ret;
118
119         ret = max8925_reg_read(info->i2c, info->enable_reg);
120         if (ret < 0)
121                 return ret;
122
123         return ret & (1 << info->enable_bit);
124 }
125
126 static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
127 {
128         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
129         unsigned char data, mask;
130
131         if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
132                 return -EINVAL;
133
134         data = (uV - SD1_DVM_VMIN + SD1_DVM_STEP - 1) / SD1_DVM_STEP;
135         data <<= SD1_DVM_SHIFT;
136         mask = 3 << SD1_DVM_SHIFT;
137
138         return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
139 }
140
141 static int max8925_set_dvm_enable(struct regulator_dev *rdev)
142 {
143         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
144
145         return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
146                                 1 << SD1_DVM_EN);
147 }
148
149 static int max8925_set_dvm_disable(struct regulator_dev *rdev)
150 {
151         struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
152
153         return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
154 }
155
156 static struct regulator_ops max8925_regulator_sdv_ops = {
157         .set_voltage            = max8925_set_voltage,
158         .get_voltage            = max8925_get_voltage,
159         .enable                 = max8925_enable,
160         .disable                = max8925_disable,
161         .is_enabled             = max8925_is_enabled,
162         .set_suspend_voltage    = max8925_set_dvm_voltage,
163         .set_suspend_enable     = max8925_set_dvm_enable,
164         .set_suspend_disable    = max8925_set_dvm_disable,
165 };
166
167 static struct regulator_ops max8925_regulator_ldo_ops = {
168         .set_voltage            = max8925_set_voltage,
169         .get_voltage            = max8925_get_voltage,
170         .enable                 = max8925_enable,
171         .disable                = max8925_disable,
172         .is_enabled             = max8925_is_enabled,
173 };
174
175 #define MAX8925_SDV(_id, min, max, step)                        \
176 {                                                               \
177         .desc   = {                                             \
178                 .name   = "SDV" #_id,                           \
179                 .ops    = &max8925_regulator_sdv_ops,           \
180                 .type   = REGULATOR_VOLTAGE,                    \
181                 .id     = MAX8925_ID_SD##_id,                   \
182                 .owner  = THIS_MODULE,                          \
183         },                                                      \
184         .min_uV         = min * 1000,                           \
185         .max_uV         = max * 1000,                           \
186         .step_uV        = step * 1000,                          \
187         .vol_reg        = MAX8925_SDV##_id,                     \
188         .vol_shift      = 0,                                    \
189         .vol_nbits      = 6,                                    \
190         .enable_reg     = MAX8925_SDCTL##_id,                   \
191         .enable_bit     = 0,                                    \
192 }
193
194 #define MAX8925_LDO(_id, min, max, step)                        \
195 {                                                               \
196         .desc   = {                                             \
197                 .name   = "LDO" #_id,                           \
198                 .ops    = &max8925_regulator_ldo_ops,           \
199                 .type   = REGULATOR_VOLTAGE,                    \
200                 .id     = MAX8925_ID_LDO##_id,                  \
201                 .owner  = THIS_MODULE,                          \
202         },                                                      \
203         .min_uV         = min * 1000,                           \
204         .max_uV         = max * 1000,                           \
205         .step_uV        = step * 1000,                          \
206         .vol_reg        = MAX8925_LDOVOUT##_id,                 \
207         .vol_shift      = 0,                                    \
208         .vol_nbits      = 6,                                    \
209         .enable_reg     = MAX8925_LDOCTL##_id,                  \
210         .enable_bit     = 0,                                    \
211 }
212
213 static struct max8925_regulator_info max8925_regulator_info[] = {
214         MAX8925_SDV(1, 637.5, 1425, 12.5),
215         MAX8925_SDV(2,   650, 2225,   25),
216         MAX8925_SDV(3,   750, 3900,   50),
217
218         MAX8925_LDO(1,  750, 3900, 50),
219         MAX8925_LDO(2,  650, 2250, 25),
220         MAX8925_LDO(3,  650, 2250, 25),
221         MAX8925_LDO(4,  750, 3900, 50),
222         MAX8925_LDO(5,  750, 3900, 50),
223         MAX8925_LDO(6,  750, 3900, 50),
224         MAX8925_LDO(7,  750, 3900, 50),
225         MAX8925_LDO(8,  750, 3900, 50),
226         MAX8925_LDO(9,  750, 3900, 50),
227         MAX8925_LDO(10, 750, 3900, 50),
228         MAX8925_LDO(11, 750, 3900, 50),
229         MAX8925_LDO(12, 750, 3900, 50),
230         MAX8925_LDO(13, 750, 3900, 50),
231         MAX8925_LDO(14, 750, 3900, 50),
232         MAX8925_LDO(15, 750, 3900, 50),
233         MAX8925_LDO(16, 750, 3900, 50),
234         MAX8925_LDO(17, 650, 2250, 25),
235         MAX8925_LDO(18, 650, 2250, 25),
236         MAX8925_LDO(19, 750, 3900, 50),
237         MAX8925_LDO(20, 750, 3900, 50),
238 };
239
240 static struct max8925_regulator_info * __devinit find_regulator_info(int id)
241 {
242         struct max8925_regulator_info *ri;
243         int i;
244
245         for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
246                 ri = &max8925_regulator_info[i];
247                 if (ri->desc.id == id)
248                         return ri;
249         }
250         return NULL;
251 }
252
253 static int __devinit max8925_regulator_probe(struct platform_device *pdev)
254 {
255         struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
256         struct max8925_platform_data *pdata = chip->dev->platform_data;
257         struct max8925_regulator_info *ri;
258         struct regulator_dev *rdev;
259
260         ri = find_regulator_info(pdev->id);
261         if (ri == NULL) {
262                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
263                 return -EINVAL;
264         }
265         ri->i2c = chip->i2c;
266         ri->chip = chip;
267
268         rdev = regulator_register(&ri->desc, &pdev->dev,
269                                   pdata->regulator[pdev->id], ri, NULL);
270         if (IS_ERR(rdev)) {
271                 dev_err(&pdev->dev, "failed to register regulator %s\n",
272                                 ri->desc.name);
273                 return PTR_ERR(rdev);
274         }
275
276         platform_set_drvdata(pdev, rdev);
277         return 0;
278 }
279
280 static int __devexit max8925_regulator_remove(struct platform_device *pdev)
281 {
282         struct regulator_dev *rdev = platform_get_drvdata(pdev);
283
284         platform_set_drvdata(pdev, NULL);
285         regulator_unregister(rdev);
286
287         return 0;
288 }
289
290 static struct platform_driver max8925_regulator_driver = {
291         .driver         = {
292                 .name   = "max8925-regulator",
293                 .owner  = THIS_MODULE,
294         },
295         .probe          = max8925_regulator_probe,
296         .remove         = __devexit_p(max8925_regulator_remove),
297 };
298
299 static int __init max8925_regulator_init(void)
300 {
301         return platform_driver_register(&max8925_regulator_driver);
302 }
303 subsys_initcall(max8925_regulator_init);
304
305 static void __exit max8925_regulator_exit(void)
306 {
307         platform_driver_unregister(&max8925_regulator_driver);
308 }
309 module_exit(max8925_regulator_exit);
310
311 MODULE_LICENSE("GPL");
312 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
313 MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
314 MODULE_ALIAS("platform:max8925-regulator");
315