Avoid beyond bounds copy while caching ACL
[zen-stable.git] / drivers / regulator / max8925-regulator.c
blobcc290d37c463c3059b19cd9f061b430802f8c491
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 LDO control registers */
28 #define LDO_SEQ_I2C 0x7 /* Power U/D by i2c */
29 #define LDO_SEQ_MASK 0x7 /* Power U/D sequence mask */
30 #define LDO_SEQ_SHIFT 2 /* Power U/D sequence offset */
31 #define LDO_I2C_EN 0x1 /* Enable by i2c */
32 #define LDO_I2C_EN_MASK 0x1 /* Enable mask by i2c */
33 #define LDO_I2C_EN_SHIFT 0 /* Enable offset by i2c */
35 struct max8925_regulator_info {
36 struct regulator_desc desc;
37 struct regulator_dev *regulator;
38 struct i2c_client *i2c;
39 struct max8925_chip *chip;
41 int min_uV;
42 int max_uV;
43 int step_uV;
44 int vol_reg;
45 int vol_shift;
46 int vol_nbits;
47 int enable_reg;
50 static inline int check_range(struct max8925_regulator_info *info,
51 int min_uV, int max_uV)
53 if (min_uV < info->min_uV || min_uV > info->max_uV)
54 return -EINVAL;
56 return 0;
59 static int max8925_list_voltage(struct regulator_dev *rdev, unsigned index)
61 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
62 return info->min_uV + index * info->step_uV;
65 static int max8925_set_voltage(struct regulator_dev *rdev,
66 int min_uV, int max_uV, unsigned int *selector)
68 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
69 unsigned char data, mask;
71 if (check_range(info, min_uV, max_uV)) {
72 dev_err(info->chip->dev, "invalid voltage range (%d, %d) uV\n",
73 min_uV, max_uV);
74 return -EINVAL;
76 data = (min_uV - info->min_uV + info->step_uV - 1) / info->step_uV;
77 *selector = data;
78 data <<= info->vol_shift;
79 mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
81 return max8925_set_bits(info->i2c, info->vol_reg, mask, data);
84 static int max8925_get_voltage(struct regulator_dev *rdev)
86 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
87 unsigned char data, mask;
88 int ret;
90 ret = max8925_reg_read(info->i2c, info->vol_reg);
91 if (ret < 0)
92 return ret;
93 mask = ((1 << info->vol_nbits) - 1) << info->vol_shift;
94 data = (ret & mask) >> info->vol_shift;
96 return max8925_list_voltage(rdev, data);
99 static int max8925_enable(struct regulator_dev *rdev)
101 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
103 return max8925_set_bits(info->i2c, info->enable_reg,
104 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
105 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
106 LDO_SEQ_I2C << LDO_SEQ_SHIFT |
107 LDO_I2C_EN << LDO_I2C_EN_SHIFT);
110 static int max8925_disable(struct regulator_dev *rdev)
112 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
114 return max8925_set_bits(info->i2c, info->enable_reg,
115 LDO_SEQ_MASK << LDO_SEQ_SHIFT |
116 LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT,
117 LDO_SEQ_I2C << LDO_SEQ_SHIFT);
120 static int max8925_is_enabled(struct regulator_dev *rdev)
122 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
123 int ldo_seq, ret;
125 ret = max8925_reg_read(info->i2c, info->enable_reg);
126 if (ret < 0)
127 return ret;
128 ldo_seq = (ret >> LDO_SEQ_SHIFT) & LDO_SEQ_MASK;
129 if (ldo_seq != LDO_SEQ_I2C)
130 return 1;
131 else
132 return ret & (LDO_I2C_EN_MASK << LDO_I2C_EN_SHIFT);
135 static int max8925_set_dvm_voltage(struct regulator_dev *rdev, int uV)
137 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
138 unsigned char data, mask;
140 if (uV < SD1_DVM_VMIN || uV > SD1_DVM_VMAX)
141 return -EINVAL;
143 data = (uV - SD1_DVM_VMIN + SD1_DVM_STEP - 1) / SD1_DVM_STEP;
144 data <<= SD1_DVM_SHIFT;
145 mask = 3 << SD1_DVM_SHIFT;
147 return max8925_set_bits(info->i2c, info->enable_reg, mask, data);
150 static int max8925_set_dvm_enable(struct regulator_dev *rdev)
152 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
154 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN,
155 1 << SD1_DVM_EN);
158 static int max8925_set_dvm_disable(struct regulator_dev *rdev)
160 struct max8925_regulator_info *info = rdev_get_drvdata(rdev);
162 return max8925_set_bits(info->i2c, info->vol_reg, 1 << SD1_DVM_EN, 0);
165 static struct regulator_ops max8925_regulator_sdv_ops = {
166 .set_voltage = max8925_set_voltage,
167 .get_voltage = max8925_get_voltage,
168 .enable = max8925_enable,
169 .disable = max8925_disable,
170 .is_enabled = max8925_is_enabled,
171 .set_suspend_voltage = max8925_set_dvm_voltage,
172 .set_suspend_enable = max8925_set_dvm_enable,
173 .set_suspend_disable = max8925_set_dvm_disable,
176 static struct regulator_ops max8925_regulator_ldo_ops = {
177 .set_voltage = max8925_set_voltage,
178 .get_voltage = max8925_get_voltage,
179 .enable = max8925_enable,
180 .disable = max8925_disable,
181 .is_enabled = max8925_is_enabled,
184 #define MAX8925_SDV(_id, min, max, step) \
186 .desc = { \
187 .name = "SDV" #_id, \
188 .ops = &max8925_regulator_sdv_ops, \
189 .type = REGULATOR_VOLTAGE, \
190 .id = MAX8925_ID_SD##_id, \
191 .owner = THIS_MODULE, \
192 }, \
193 .min_uV = min * 1000, \
194 .max_uV = max * 1000, \
195 .step_uV = step * 1000, \
196 .vol_reg = MAX8925_SDV##_id, \
197 .vol_shift = 0, \
198 .vol_nbits = 6, \
199 .enable_reg = MAX8925_SDCTL##_id, \
202 #define MAX8925_LDO(_id, min, max, step) \
204 .desc = { \
205 .name = "LDO" #_id, \
206 .ops = &max8925_regulator_ldo_ops, \
207 .type = REGULATOR_VOLTAGE, \
208 .id = MAX8925_ID_LDO##_id, \
209 .owner = THIS_MODULE, \
210 }, \
211 .min_uV = min * 1000, \
212 .max_uV = max * 1000, \
213 .step_uV = step * 1000, \
214 .vol_reg = MAX8925_LDOVOUT##_id, \
215 .vol_shift = 0, \
216 .vol_nbits = 6, \
217 .enable_reg = MAX8925_LDOCTL##_id, \
220 static struct max8925_regulator_info max8925_regulator_info[] = {
221 MAX8925_SDV(1, 637.5, 1425, 12.5),
222 MAX8925_SDV(2, 650, 2225, 25),
223 MAX8925_SDV(3, 750, 3900, 50),
225 MAX8925_LDO(1, 750, 3900, 50),
226 MAX8925_LDO(2, 650, 2250, 25),
227 MAX8925_LDO(3, 650, 2250, 25),
228 MAX8925_LDO(4, 750, 3900, 50),
229 MAX8925_LDO(5, 750, 3900, 50),
230 MAX8925_LDO(6, 750, 3900, 50),
231 MAX8925_LDO(7, 750, 3900, 50),
232 MAX8925_LDO(8, 750, 3900, 50),
233 MAX8925_LDO(9, 750, 3900, 50),
234 MAX8925_LDO(10, 750, 3900, 50),
235 MAX8925_LDO(11, 750, 3900, 50),
236 MAX8925_LDO(12, 750, 3900, 50),
237 MAX8925_LDO(13, 750, 3900, 50),
238 MAX8925_LDO(14, 750, 3900, 50),
239 MAX8925_LDO(15, 750, 3900, 50),
240 MAX8925_LDO(16, 750, 3900, 50),
241 MAX8925_LDO(17, 650, 2250, 25),
242 MAX8925_LDO(18, 650, 2250, 25),
243 MAX8925_LDO(19, 750, 3900, 50),
244 MAX8925_LDO(20, 750, 3900, 50),
247 static struct max8925_regulator_info * __devinit find_regulator_info(int id)
249 struct max8925_regulator_info *ri;
250 int i;
252 for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
253 ri = &max8925_regulator_info[i];
254 if (ri->desc.id == id)
255 return ri;
257 return NULL;
260 static int __devinit max8925_regulator_probe(struct platform_device *pdev)
262 struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent);
263 struct max8925_platform_data *pdata = chip->dev->platform_data;
264 struct max8925_regulator_info *ri;
265 struct regulator_dev *rdev;
267 ri = find_regulator_info(pdev->id);
268 if (ri == NULL) {
269 dev_err(&pdev->dev, "invalid regulator ID specified\n");
270 return -EINVAL;
272 ri->i2c = chip->i2c;
273 ri->chip = chip;
275 rdev = regulator_register(&ri->desc, &pdev->dev,
276 pdata->regulator[pdev->id], ri, NULL);
277 if (IS_ERR(rdev)) {
278 dev_err(&pdev->dev, "failed to register regulator %s\n",
279 ri->desc.name);
280 return PTR_ERR(rdev);
283 platform_set_drvdata(pdev, rdev);
284 return 0;
287 static int __devexit max8925_regulator_remove(struct platform_device *pdev)
289 struct regulator_dev *rdev = platform_get_drvdata(pdev);
291 platform_set_drvdata(pdev, NULL);
292 regulator_unregister(rdev);
294 return 0;
297 static struct platform_driver max8925_regulator_driver = {
298 .driver = {
299 .name = "max8925-regulator",
300 .owner = THIS_MODULE,
302 .probe = max8925_regulator_probe,
303 .remove = __devexit_p(max8925_regulator_remove),
306 static int __init max8925_regulator_init(void)
308 return platform_driver_register(&max8925_regulator_driver);
310 subsys_initcall(max8925_regulator_init);
312 static void __exit max8925_regulator_exit(void)
314 platform_driver_unregister(&max8925_regulator_driver);
316 module_exit(max8925_regulator_exit);
318 MODULE_LICENSE("GPL");
319 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
320 MODULE_DESCRIPTION("Regulator Driver for Maxim 8925 PMIC");
321 MODULE_ALIAS("platform:max8925-regulator");