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