Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / regulator / max8925-regulator.c
blobcc9ec0e0327183bb58aa81a69343905e2765db02
1 /*
2 * Regulators driver for Maxim max8925
4 * Copyright (C) 2009 Marvell International Ltd.
5 * Haojian Zhuang <haojian.zhuang@marvell.com>
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.
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>
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 */
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 */
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;
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;
47 static inline int check_range(struct max8925_regulator_info *info,
48 int min_uV, int max_uV)
50 if (min_uV < info->min_uV || min_uV > info->max_uV)
51 return -EINVAL;
53 return 0;
56 static int max8925_list_voltage(struct regulator_dev *rdev, unsigned index)
58 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
59 return info->min_uV + index * info->step_uV;
62 static int max8925_set_voltage(struct regulator_dev *rdev,
63 int min_uV, int max_uV, unsigned int *selector)
65 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
66 unsigned char data, mask;
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;
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;
78 return max8925_set_bits(info->i2c, info->vol_reg, mask, data);
81 static int max8925_get_voltage(struct regulator_dev *rdev)
83 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
84 unsigned char data, mask;
85 int ret;
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;
93 return max8925_list_voltage(rdev, data);
96 static int max8925_enable(struct regulator_dev *rdev)
98 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
100 return max8925_set_bits(info->i2c, info->enable_reg,
101 OUT_ENABLE << info->enable_bit,
102 OUT_ENABLE << info->enable_bit);
105 static int max8925_disable(struct regulator_dev *rdev)
107 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
109 return max8925_set_bits(info->i2c, info->enable_reg,
110 OUT_ENABLE << info->enable_bit,
111 OUT_DISABLE << info->enable_bit);
114 static int max8925_is_enabled(struct regulator_dev *rdev)
116 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
117 int ret;
119 ret = max8925_reg_read(info->i2c, info->enable_reg);
120 if (ret < 0)
121 return ret;
123 return ret & (1 << info->enable_bit);
126 static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
128 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
129 unsigned char data, mask;
131 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
132 return -EINVAL;
134 data = (uV - SD1_DVM_VMIN + SD1_DVM_STEP - 1) / SD1_DVM_STEP;
135 data <<= SD1_DVM_SHIFT;
136 mask = 3 << SD1_DVM_SHIFT;
138 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
141 static int max8925_set_dvm_enable(struct regulator_dev *rdev)
143 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
145 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
146 1 << SD1_DVM_EN);
149 static int max8925_set_dvm_disable(struct regulator_dev *rdev)
151 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
153 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
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,
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,
175 #define MAX8925_SDV(_id, min, max, step) \
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, \
194 #define MAX8925_LDO(_id, min, max, step) \
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, \
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),
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),
240 static struct max8925_regulator_info * __devinit find_regulator_info(int id)
242 struct max8925_regulator_info *ri;
243 int i;
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;
250 return NULL;
253 static int __devinit max8925_regulator_probe(struct platform_device *pdev)
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;
260 ri = find_regulator_info(pdev->id);
261 if (ri == NULL) {
262 dev_err(&pdev->dev, "invalid regulator ID specified\n");
263 return -EINVAL;
265 ri->i2c = chip->i2c;
266 ri->chip = chip;
268 rdev = regulator_register(&ri->desc, &pdev->dev,
269 pdata->regulator[pdev->id], ri);
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);
276 platform_set_drvdata(pdev, rdev);
277 return 0;
280 static int __devexit max8925_regulator_remove(struct platform_device *pdev)
282 struct regulator_dev *rdev = platform_get_drvdata(pdev);
284 platform_set_drvdata(pdev, NULL);
285 regulator_unregister(rdev);
287 return 0;
290 static struct platform_driver max8925_regulator_driver = {
291 .driver = {
292 .name = "max8925-regulator",
293 .owner = THIS_MODULE,
295 .probe = max8925_regulator_probe,
296 .remove = __devexit_p(max8925_regulator_remove),
299 static int __init max8925_regulator_init(void)
301 return platform_driver_register(&max8925_regulator_driver);
303 subsys_initcall(max8925_regulator_init);
305 static void __exit max8925_regulator_exit(void)
307 platform_driver_unregister(&max8925_regulator_driver);
309 module_exit(max8925_regulator_exit);
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");