IMA: maintain i_readcount in the VFS layer
[linux/fpc-iii.git] / drivers / regulator / max8660.c
blobc570e6eb0db2c5ab5e4d86776e7063e6a10c9179
1 /*
2 * max8660.c -- Voltage regulation for the Maxim 8660/8661
4 * based on max1586.c and wm8400-regulator.c
6 * Copyright (C) 2009 Wolfram Sang, Pengutronix e.K.
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
21 * Some info:
23 * Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX8660-MAX8661.pdf
25 * This chip is a bit nasty because it is a write-only device. Thus, the driver
26 * uses shadow registers to keep track of its values. The main problem appears
27 * to be the initialization: When Linux boots up, we cannot know if the chip is
28 * in the default state or not, so we would have to pass such information in
29 * platform_data. As this adds a bit of complexity to the driver, this is left
30 * out for now until it is really needed.
32 * [A|S|M]DTV1 registers are currently not used, but [A|S|M]DTV2.
34 * If the driver is feature complete, it might be worth to check if one set of
35 * functions for V3-V7 is sufficient. For maximum flexibility during
36 * development, they are separated for now.
40 #include <linux/module.h>
41 #include <linux/err.h>
42 #include <linux/i2c.h>
43 #include <linux/platform_device.h>
44 #include <linux/regulator/driver.h>
45 #include <linux/slab.h>
46 #include <linux/regulator/max8660.h>
48 #define MAX8660_DCDC_MIN_UV 725000
49 #define MAX8660_DCDC_MAX_UV 1800000
50 #define MAX8660_DCDC_STEP 25000
51 #define MAX8660_DCDC_MAX_SEL 0x2b
53 #define MAX8660_LDO5_MIN_UV 1700000
54 #define MAX8660_LDO5_MAX_UV 2000000
55 #define MAX8660_LDO5_STEP 25000
56 #define MAX8660_LDO5_MAX_SEL 0x0c
58 #define MAX8660_LDO67_MIN_UV 1800000
59 #define MAX8660_LDO67_MAX_UV 3300000
60 #define MAX8660_LDO67_STEP 100000
61 #define MAX8660_LDO67_MAX_SEL 0x0f
63 enum {
64 MAX8660_OVER1,
65 MAX8660_OVER2,
66 MAX8660_VCC1,
67 MAX8660_ADTV1,
68 MAX8660_ADTV2,
69 MAX8660_SDTV1,
70 MAX8660_SDTV2,
71 MAX8660_MDTV1,
72 MAX8660_MDTV2,
73 MAX8660_L12VCR,
74 MAX8660_FPWM,
75 MAX8660_N_REGS, /* not a real register */
78 struct max8660 {
79 struct i2c_client *client;
80 u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */
81 struct regulator_dev *rdev[];
84 static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val)
86 static const u8 max8660_addresses[MAX8660_N_REGS] =
87 { 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 };
89 int ret;
90 u8 reg_val = (max8660->shadow_regs[reg] & mask) | val;
91 dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n",
92 max8660_addresses[reg], reg_val);
94 ret = i2c_smbus_write_byte_data(max8660->client,
95 max8660_addresses[reg], reg_val);
96 if (ret == 0)
97 max8660->shadow_regs[reg] = reg_val;
99 return ret;
104 * DCDC functions
107 static int max8660_dcdc_is_enabled(struct regulator_dev *rdev)
109 struct max8660 *max8660 = rdev_get_drvdata(rdev);
110 u8 val = max8660->shadow_regs[MAX8660_OVER1];
111 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
112 return !!(val & mask);
115 static int max8660_dcdc_enable(struct regulator_dev *rdev)
117 struct max8660 *max8660 = rdev_get_drvdata(rdev);
118 u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4;
119 return max8660_write(max8660, MAX8660_OVER1, 0xff, bit);
122 static int max8660_dcdc_disable(struct regulator_dev *rdev)
124 struct max8660 *max8660 = rdev_get_drvdata(rdev);
125 u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4;
126 return max8660_write(max8660, MAX8660_OVER1, mask, 0);
129 static int max8660_dcdc_list(struct regulator_dev *rdev, unsigned selector)
131 if (selector > MAX8660_DCDC_MAX_SEL)
132 return -EINVAL;
133 return MAX8660_DCDC_MIN_UV + selector * MAX8660_DCDC_STEP;
136 static int max8660_dcdc_get(struct regulator_dev *rdev)
138 struct max8660 *max8660 = rdev_get_drvdata(rdev);
139 u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
140 u8 selector = max8660->shadow_regs[reg];
141 return MAX8660_DCDC_MIN_UV + selector * MAX8660_DCDC_STEP;
144 static int max8660_dcdc_set(struct regulator_dev *rdev, int min_uV, int max_uV)
146 struct max8660 *max8660 = rdev_get_drvdata(rdev);
147 u8 reg, selector, bits;
148 int ret;
150 if (min_uV < MAX8660_DCDC_MIN_UV || min_uV > MAX8660_DCDC_MAX_UV)
151 return -EINVAL;
152 if (max_uV < MAX8660_DCDC_MIN_UV || max_uV > MAX8660_DCDC_MAX_UV)
153 return -EINVAL;
155 selector = (min_uV - (MAX8660_DCDC_MIN_UV - MAX8660_DCDC_STEP + 1))
156 / MAX8660_DCDC_STEP;
158 ret = max8660_dcdc_list(rdev, selector);
159 if (ret < 0 || ret > max_uV)
160 return -EINVAL;
162 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2;
163 ret = max8660_write(max8660, reg, 0, selector);
164 if (ret)
165 return ret;
167 /* Select target voltage register and activate regulation */
168 bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30;
169 return max8660_write(max8660, MAX8660_VCC1, 0xff, bits);
172 static struct regulator_ops max8660_dcdc_ops = {
173 .is_enabled = max8660_dcdc_is_enabled,
174 .list_voltage = max8660_dcdc_list,
175 .set_voltage = max8660_dcdc_set,
176 .get_voltage = max8660_dcdc_get,
181 * LDO5 functions
184 static int max8660_ldo5_list(struct regulator_dev *rdev, unsigned selector)
186 if (selector > MAX8660_LDO5_MAX_SEL)
187 return -EINVAL;
188 return MAX8660_LDO5_MIN_UV + selector * MAX8660_LDO5_STEP;
191 static int max8660_ldo5_get(struct regulator_dev *rdev)
193 struct max8660 *max8660 = rdev_get_drvdata(rdev);
194 u8 selector = max8660->shadow_regs[MAX8660_MDTV2];
196 return MAX8660_LDO5_MIN_UV + selector * MAX8660_LDO5_STEP;
199 static int max8660_ldo5_set(struct regulator_dev *rdev, int min_uV, int max_uV)
201 struct max8660 *max8660 = rdev_get_drvdata(rdev);
202 u8 selector;
203 int ret;
205 if (min_uV < MAX8660_LDO5_MIN_UV || min_uV > MAX8660_LDO5_MAX_UV)
206 return -EINVAL;
207 if (max_uV < MAX8660_LDO5_MIN_UV || max_uV > MAX8660_LDO5_MAX_UV)
208 return -EINVAL;
210 selector = (min_uV - (MAX8660_LDO5_MIN_UV - MAX8660_LDO5_STEP + 1))
211 / MAX8660_LDO5_STEP;
212 ret = max8660_ldo5_list(rdev, selector);
213 if (ret < 0 || ret > max_uV)
214 return -EINVAL;
216 ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector);
217 if (ret)
218 return ret;
220 /* Select target voltage register and activate regulation */
221 return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0);
224 static struct regulator_ops max8660_ldo5_ops = {
225 .list_voltage = max8660_ldo5_list,
226 .set_voltage = max8660_ldo5_set,
227 .get_voltage = max8660_ldo5_get,
232 * LDO67 functions
235 static int max8660_ldo67_is_enabled(struct regulator_dev *rdev)
237 struct max8660 *max8660 = rdev_get_drvdata(rdev);
238 u8 val = max8660->shadow_regs[MAX8660_OVER2];
239 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
240 return !!(val & mask);
243 static int max8660_ldo67_enable(struct regulator_dev *rdev)
245 struct max8660 *max8660 = rdev_get_drvdata(rdev);
246 u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4;
247 return max8660_write(max8660, MAX8660_OVER2, 0xff, bit);
250 static int max8660_ldo67_disable(struct regulator_dev *rdev)
252 struct max8660 *max8660 = rdev_get_drvdata(rdev);
253 u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4;
254 return max8660_write(max8660, MAX8660_OVER2, mask, 0);
257 static int max8660_ldo67_list(struct regulator_dev *rdev, unsigned selector)
259 if (selector > MAX8660_LDO67_MAX_SEL)
260 return -EINVAL;
261 return MAX8660_LDO67_MIN_UV + selector * MAX8660_LDO67_STEP;
264 static int max8660_ldo67_get(struct regulator_dev *rdev)
266 struct max8660 *max8660 = rdev_get_drvdata(rdev);
267 u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4;
268 u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf;
270 return MAX8660_LDO67_MIN_UV + selector * MAX8660_LDO67_STEP;
273 static int max8660_ldo67_set(struct regulator_dev *rdev, int min_uV, int max_uV)
275 struct max8660 *max8660 = rdev_get_drvdata(rdev);
276 u8 selector;
277 int ret;
279 if (min_uV < MAX8660_LDO67_MIN_UV || min_uV > MAX8660_LDO67_MAX_UV)
280 return -EINVAL;
281 if (max_uV < MAX8660_LDO67_MIN_UV || max_uV > MAX8660_LDO67_MAX_UV)
282 return -EINVAL;
284 selector = (min_uV - (MAX8660_LDO67_MIN_UV - MAX8660_LDO67_STEP + 1))
285 / MAX8660_LDO67_STEP;
287 ret = max8660_ldo67_list(rdev, selector);
288 if (ret < 0 || ret > max_uV)
289 return -EINVAL;
291 if (rdev_get_id(rdev) == MAX8660_V6)
292 return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector);
293 else
294 return max8660_write(max8660, MAX8660_L12VCR, 0x0f, selector << 4);
297 static struct regulator_ops max8660_ldo67_ops = {
298 .is_enabled = max8660_ldo67_is_enabled,
299 .enable = max8660_ldo67_enable,
300 .disable = max8660_ldo67_disable,
301 .list_voltage = max8660_ldo67_list,
302 .get_voltage = max8660_ldo67_get,
303 .set_voltage = max8660_ldo67_set,
306 static struct regulator_desc max8660_reg[] = {
308 .name = "V3(DCDC)",
309 .id = MAX8660_V3,
310 .ops = &max8660_dcdc_ops,
311 .type = REGULATOR_VOLTAGE,
312 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
313 .owner = THIS_MODULE,
316 .name = "V4(DCDC)",
317 .id = MAX8660_V4,
318 .ops = &max8660_dcdc_ops,
319 .type = REGULATOR_VOLTAGE,
320 .n_voltages = MAX8660_DCDC_MAX_SEL + 1,
321 .owner = THIS_MODULE,
324 .name = "V5(LDO)",
325 .id = MAX8660_V5,
326 .ops = &max8660_ldo5_ops,
327 .type = REGULATOR_VOLTAGE,
328 .n_voltages = MAX8660_LDO5_MAX_SEL + 1,
329 .owner = THIS_MODULE,
332 .name = "V6(LDO)",
333 .id = MAX8660_V6,
334 .ops = &max8660_ldo67_ops,
335 .type = REGULATOR_VOLTAGE,
336 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
337 .owner = THIS_MODULE,
340 .name = "V7(LDO)",
341 .id = MAX8660_V7,
342 .ops = &max8660_ldo67_ops,
343 .type = REGULATOR_VOLTAGE,
344 .n_voltages = MAX8660_LDO67_MAX_SEL + 1,
345 .owner = THIS_MODULE,
349 static int __devinit max8660_probe(struct i2c_client *client,
350 const struct i2c_device_id *i2c_id)
352 struct regulator_dev **rdev;
353 struct max8660_platform_data *pdata = client->dev.platform_data;
354 struct max8660 *max8660;
355 int boot_on, i, id, ret = -EINVAL;
357 if (pdata->num_subdevs > MAX8660_V_END) {
358 dev_err(&client->dev, "Too many regulators found!\n");
359 goto out;
362 max8660 = kzalloc(sizeof(struct max8660) +
363 sizeof(struct regulator_dev *) * MAX8660_V_END,
364 GFP_KERNEL);
365 if (!max8660) {
366 ret = -ENOMEM;
367 goto out;
370 max8660->client = client;
371 rdev = max8660->rdev;
373 if (pdata->en34_is_high) {
374 /* Simulate always on */
375 max8660->shadow_regs[MAX8660_OVER1] = 5;
376 } else {
377 /* Otherwise devices can be toggled via software */
378 max8660_dcdc_ops.enable = max8660_dcdc_enable;
379 max8660_dcdc_ops.disable = max8660_dcdc_disable;
383 * First, set up shadow registers to prevent glitches. As some
384 * registers are shared between regulators, everything must be properly
385 * set up for all regulators in advance.
387 max8660->shadow_regs[MAX8660_ADTV1] =
388 max8660->shadow_regs[MAX8660_ADTV2] =
389 max8660->shadow_regs[MAX8660_SDTV1] =
390 max8660->shadow_regs[MAX8660_SDTV2] = 0x1b;
391 max8660->shadow_regs[MAX8660_MDTV1] =
392 max8660->shadow_regs[MAX8660_MDTV2] = 0x04;
394 for (i = 0; i < pdata->num_subdevs; i++) {
396 if (!pdata->subdevs[i].platform_data)
397 goto err_free;
399 boot_on = pdata->subdevs[i].platform_data->constraints.boot_on;
401 switch (pdata->subdevs[i].id) {
402 case MAX8660_V3:
403 if (boot_on)
404 max8660->shadow_regs[MAX8660_OVER1] |= 1;
405 break;
407 case MAX8660_V4:
408 if (boot_on)
409 max8660->shadow_regs[MAX8660_OVER1] |= 4;
410 break;
412 case MAX8660_V5:
413 break;
415 case MAX8660_V6:
416 if (boot_on)
417 max8660->shadow_regs[MAX8660_OVER2] |= 2;
418 break;
420 case MAX8660_V7:
421 if (!strcmp(i2c_id->name, "max8661")) {
422 dev_err(&client->dev, "Regulator not on this chip!\n");
423 goto err_free;
426 if (boot_on)
427 max8660->shadow_regs[MAX8660_OVER2] |= 4;
428 break;
430 default:
431 dev_err(&client->dev, "invalid regulator %s\n",
432 pdata->subdevs[i].name);
433 goto err_free;
437 /* Finally register devices */
438 for (i = 0; i < pdata->num_subdevs; i++) {
440 id = pdata->subdevs[i].id;
442 rdev[i] = regulator_register(&max8660_reg[id], &client->dev,
443 pdata->subdevs[i].platform_data,
444 max8660);
445 if (IS_ERR(rdev[i])) {
446 ret = PTR_ERR(rdev[i]);
447 dev_err(&client->dev, "failed to register %s\n",
448 max8660_reg[id].name);
449 goto err_unregister;
453 i2c_set_clientdata(client, max8660);
454 dev_info(&client->dev, "Maxim 8660/8661 regulator driver loaded\n");
455 return 0;
457 err_unregister:
458 while (--i >= 0)
459 regulator_unregister(rdev[i]);
460 err_free:
461 kfree(max8660);
462 out:
463 return ret;
466 static int __devexit max8660_remove(struct i2c_client *client)
468 struct max8660 *max8660 = i2c_get_clientdata(client);
469 int i;
471 for (i = 0; i < MAX8660_V_END; i++)
472 if (max8660->rdev[i])
473 regulator_unregister(max8660->rdev[i]);
474 kfree(max8660);
476 return 0;
479 static const struct i2c_device_id max8660_id[] = {
480 { "max8660", 0 },
481 { "max8661", 0 },
484 MODULE_DEVICE_TABLE(i2c, max8660_id);
486 static struct i2c_driver max8660_driver = {
487 .probe = max8660_probe,
488 .remove = __devexit_p(max8660_remove),
489 .driver = {
490 .name = "max8660",
491 .owner = THIS_MODULE,
493 .id_table = max8660_id,
496 static int __init max8660_init(void)
498 return i2c_add_driver(&max8660_driver);
500 subsys_initcall(max8660_init);
502 static void __exit max8660_exit(void)
504 i2c_del_driver(&max8660_driver);
506 module_exit(max8660_exit);
508 /* Module information */
509 MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver");
510 MODULE_AUTHOR("Wolfram Sang");
511 MODULE_LICENSE("GPL v2");