Phonet: factor common code to send control messages
[linux/fpc-iii.git] / drivers / regulator / max8925-regulator.c
blob8ae147549c6aabb2c65b9fd60f8aec8bb6f67a26
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/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>
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 */
26 struct max8925_regulator_info {
27 struct regulator_desc desc;
28 struct regulator_dev *regulator;
29 struct i2c_client *i2c;
30 struct max8925_chip *chip;
32 int min_uV;
33 int max_uV;
34 int step_uV;
35 int vol_reg;
36 int vol_shift;
37 int vol_nbits;
38 int enable_bit;
39 int enable_reg;
42 static inline int check_range(struct max8925_regulator_info *info,
43 int min_uV, int max_uV)
45 if (min_uV < info->min_uV || min_uV > info->max_uV)
46 return -EINVAL;
48 return 0;
51 static int max8925_list_voltage(struct regulator_dev *rdev, unsigned index)
53 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
54 return info->min_uV + index * info->step_uV;
57 static int max8925_set_voltage(struct regulator_dev *rdev,
58 int min_uV, int max_uV, unsigned int *selector)
60 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
61 unsigned char data, mask;
63 if (check_range(info, min_uV, max_uV)) {
64 dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
65 min_uV, max_uV);
66 return -EINVAL;
68 data = (min_uV - info->min_uV + info->step_uV - 1) / info->step_uV;
69 *selector = data;
70 data <<= info->vol_shift;
71 mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
73 return max8925_set_bits(info->i2c, info->vol_reg, mask, data);
76 static int max8925_get_voltage(struct regulator_dev *rdev)
78 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
79 unsigned char data, mask;
80 int ret;
82 ret = max8925_reg_read(info->i2c, info->vol_reg);
83 if (ret < 0)
84 return ret;
85 mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
86 data = (ret & mask) >> info->vol_shift;
88 return max8925_list_voltage(rdev, data);
91 static int max8925_enable(struct regulator_dev *rdev)
93 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
95 return max8925_set_bits(info->i2c, info->enable_reg,
96 1 << info->enable_bit,
97 1 << info->enable_bit);
100 static int max8925_disable(struct regulator_dev *rdev)
102 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
104 return max8925_set_bits(info->i2c, info->enable_reg,
105 1 << info->enable_bit, 0);
108 static int max8925_is_enabled(struct regulator_dev *rdev)
110 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
111 int ret;
113 ret = max8925_reg_read(info->i2c, info->enable_reg);
114 if (ret < 0)
115 return ret;
117 return ret & (1 << info->enable_bit);
120 static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
122 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
123 unsigned char data, mask;
125 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
126 return -EINVAL;
128 data = (uV - SD1_DVM_VMIN + SD1_DVM_STEP - 1) / SD1_DVM_STEP;
129 data <<= SD1_DVM_SHIFT;
130 mask = 3 << SD1_DVM_SHIFT;
132 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
135 static int max8925_set_dvm_enable(struct regulator_dev *rdev)
137 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
139 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
140 1 << SD1_DVM_EN);
143 static int max8925_set_dvm_disable(struct regulator_dev *rdev)
145 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
147 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
150 static struct regulator_ops max8925_regulator_sdv_ops = {
151 .set_voltage = max8925_set_voltage,
152 .get_voltage = max8925_get_voltage,
153 .enable = max8925_enable,
154 .disable = max8925_disable,
155 .is_enabled = max8925_is_enabled,
156 .set_suspend_voltage = max8925_set_dvm_voltage,
157 .set_suspend_enable = max8925_set_dvm_enable,
158 .set_suspend_disable = max8925_set_dvm_disable,
161 static struct regulator_ops max8925_regulator_ldo_ops = {
162 .set_voltage = max8925_set_voltage,
163 .get_voltage = max8925_get_voltage,
164 .enable = max8925_enable,
165 .disable = max8925_disable,
166 .is_enabled = max8925_is_enabled,
169 #define MAX8925_SDV(_id, min, max, step) \
171 .desc = { \
172 .name = "SDV" #_id, \
173 .ops = &max8925_regulator_sdv_ops, \
174 .type = REGULATOR_VOLTAGE, \
175 .id = MAX8925_ID_SD##_id, \
176 .owner = THIS_MODULE, \
177 }, \
178 .min_uV = min * 1000, \
179 .max_uV = max * 1000, \
180 .step_uV = step * 1000, \
181 .vol_reg = MAX8925_SDV##_id, \
182 .vol_shift = 0, \
183 .vol_nbits = 6, \
184 .enable_reg = MAX8925_SDCTL##_id, \
185 .enable_bit = 0, \
188 #define MAX8925_LDO(_id, min, max, step) \
190 .desc = { \
191 .name = "LDO" #_id, \
192 .ops = &max8925_regulator_ldo_ops, \
193 .type = REGULATOR_VOLTAGE, \
194 .id = MAX8925_ID_LDO##_id, \
195 .owner = THIS_MODULE, \
196 }, \
197 .min_uV = min * 1000, \
198 .max_uV = max * 1000, \
199 .step_uV = step * 1000, \
200 .vol_reg = MAX8925_LDOVOUT##_id, \
201 .vol_shift = 0, \
202 .vol_nbits = 6, \
203 .enable_reg = MAX8925_LDOCTL##_id, \
204 .enable_bit = 0, \
207 static struct max8925_regulator_info max8925_regulator_info[] = {
208 MAX8925_SDV(1, 637.5, 1425, 12.5),
209 MAX8925_SDV(2, 650, 2225, 25),
210 MAX8925_SDV(3, 750, 3900, 50),
212 MAX8925_LDO(1, 750, 3900, 50),
213 MAX8925_LDO(2, 650, 2250, 25),
214 MAX8925_LDO(3, 650, 2250, 25),
215 MAX8925_LDO(4, 750, 3900, 50),
216 MAX8925_LDO(5, 750, 3900, 50),
217 MAX8925_LDO(6, 750, 3900, 50),
218 MAX8925_LDO(7, 750, 3900, 50),
219 MAX8925_LDO(8, 750, 3900, 50),
220 MAX8925_LDO(9, 750, 3900, 50),
221 MAX8925_LDO(10, 750, 3900, 50),
222 MAX8925_LDO(11, 750, 3900, 50),
223 MAX8925_LDO(12, 750, 3900, 50),
224 MAX8925_LDO(13, 750, 3900, 50),
225 MAX8925_LDO(14, 750, 3900, 50),
226 MAX8925_LDO(15, 750, 3900, 50),
227 MAX8925_LDO(16, 750, 3900, 50),
228 MAX8925_LDO(17, 650, 2250, 25),
229 MAX8925_LDO(18, 650, 2250, 25),
230 MAX8925_LDO(19, 750, 3900, 50),
231 MAX8925_LDO(20, 750, 3900, 50),
234 static struct max8925_regulator_info * __devinit find_regulator_info(int id)
236 struct max8925_regulator_info *ri;
237 int i;
239 for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
240 ri = &max8925_regulator_info[i];
241 if (ri->desc.id == id)
242 return ri;
244 return NULL;
247 static int __devinit max8925_regulator_probe(struct platform_device *pdev)
249 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
250 struct max8925_platform_data *pdata = chip->dev->platform_data;
251 struct max8925_regulator_info *ri;
252 struct regulator_dev *rdev;
254 ri = find_regulator_info(pdev->id);
255 if (ri == NULL) {
256 dev_err(&pdev->dev, "invalid regulator ID specified\n");
257 return -EINVAL;
259 ri->i2c = chip->i2c;
260 ri->chip = chip;
262 rdev = regulator_register(&ri->desc, &pdev->dev,
263 pdata->regulator[pdev->id], ri);
264 if (IS_ERR(rdev)) {
265 dev_err(&pdev->dev, "failed to register regulator %s\n",
266 ri->desc.name);
267 return PTR_ERR(rdev);
270 platform_set_drvdata(pdev, rdev);
271 return 0;
274 static int __devexit max8925_regulator_remove(struct platform_device *pdev)
276 struct regulator_dev *rdev = platform_get_drvdata(pdev);
278 platform_set_drvdata(pdev, NULL);
279 regulator_unregister(rdev);
281 return 0;
284 static struct platform_driver max8925_regulator_driver = {
285 .driver = {
286 .name = "max8925-regulator",
287 .owner = THIS_MODULE,
289 .probe = max8925_regulator_probe,
290 .remove = __devexit_p(max8925_regulator_remove),
293 static int __init max8925_regulator_init(void)
295 return platform_driver_register(&max8925_regulator_driver);
297 subsys_initcall(max8925_regulator_init);
299 static void __exit max8925_regulator_exit(void)
301 platform_driver_unregister(&max8925_regulator_driver);
303 module_exit(max8925_regulator_exit);
305 MODULE_LICENSE("GPL");
306 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
307 MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
308 MODULE_ALIAS("platform:max8925-regulator");