Merge branch 'master' into for-2.6.33
[linux-2.6/next.git] / drivers / regulator / lp3971.c
blob7803a320543b1da4f9da2d64ed33e0f108b2f634
1 /*
2 * Regulator driver for National Semiconductors LP3971 PMIC chip
4 * Copyright (C) 2009 Samsung Electronics
5 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
7 * Based on wm8350.c
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include <linux/bug.h>
16 #include <linux/err.h>
17 #include <linux/i2c.h>
18 #include <linux/kernel.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/lp3971.h>
22 struct lp3971 {
23 struct device *dev;
24 struct mutex io_lock;
25 struct i2c_client *i2c;
26 int num_regulators;
27 struct regulator_dev **rdev;
30 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
31 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
33 #define LP3971_SYS_CONTROL1_REG 0x07
35 /* System control register 1 initial value,
36 bits 4 and 5 are EPROM programmable */
37 #define SYS_CONTROL1_INIT_VAL 0x40
38 #define SYS_CONTROL1_INIT_MASK 0xCF
40 #define LP3971_BUCK_VOL_ENABLE_REG 0x10
41 #define LP3971_BUCK_VOL_CHANGE_REG 0x20
43 /* Voltage control registers shift:
44 LP3971_BUCK1 -> 0
45 LP3971_BUCK2 -> 4
46 LP3971_BUCK3 -> 6
48 #define BUCK_VOL_CHANGE_SHIFT(x) (((1 << x) & ~0x01) << 1)
49 #define BUCK_VOL_CHANGE_FLAG_GO 0x01
50 #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
51 #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
53 #define LP3971_BUCK1_BASE 0x23
54 #define LP3971_BUCK2_BASE 0x29
55 #define LP3971_BUCK3_BASE 0x32
57 const static int buck_base_addr[] = {
58 LP3971_BUCK1_BASE,
59 LP3971_BUCK2_BASE,
60 LP3971_BUCK3_BASE,
63 #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
64 #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
66 const static int buck_voltage_map[] = {
67 0, 800, 850, 900, 950, 1000, 1050, 1100,
68 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
69 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
70 3000, 3300,
73 #define BUCK_TARGET_VOL_MASK 0x3f
74 #define BUCK_TARGET_VOL_MIN_IDX 0x01
75 #define BUCK_TARGET_VOL_MAX_IDX 0x19
77 #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
79 #define LP3971_LDO_ENABLE_REG 0x12
80 #define LP3971_LDO_VOL_CONTR_BASE 0x39
82 /* Voltage control registers:
83 LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
84 LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
85 LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
86 LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
87 LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
89 #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
91 /* Voltage control registers shift:
92 LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
93 LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
94 LP3971_LDO5 -> 0
96 #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
97 #define LDO_VOL_CONTR_MASK 0x0f
99 const static int ldo45_voltage_map[] = {
100 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
101 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
104 const static int ldo123_voltage_map[] = {
105 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
106 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
109 const static int *ldo_voltage_map[] = {
110 ldo123_voltage_map, /* LDO1 */
111 ldo123_voltage_map, /* LDO2 */
112 ldo123_voltage_map, /* LDO3 */
113 ldo45_voltage_map, /* LDO4 */
114 ldo45_voltage_map, /* LDO5 */
117 #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)])
119 #define LDO_VOL_MIN_IDX 0x00
120 #define LDO_VOL_MAX_IDX 0x0f
122 static int lp3971_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
124 int ldo = rdev_get_id(dev) - LP3971_LDO1;
125 return 1000 * LDO_VOL_VALUE_MAP(ldo)[index];
128 static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
130 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
131 int ldo = rdev_get_id(dev) - LP3971_LDO1;
132 u16 mask = 1 << (1 + ldo);
133 u16 val;
135 val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
136 return (val & mask) != 0;
139 static int lp3971_ldo_enable(struct regulator_dev *dev)
141 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
142 int ldo = rdev_get_id(dev) - LP3971_LDO1;
143 u16 mask = 1 << (1 + ldo);
145 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
148 static int lp3971_ldo_disable(struct regulator_dev *dev)
150 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
151 int ldo = rdev_get_id(dev) - LP3971_LDO1;
152 u16 mask = 1 << (1 + ldo);
154 return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
157 static int lp3971_ldo_get_voltage(struct regulator_dev *dev)
159 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
160 int ldo = rdev_get_id(dev) - LP3971_LDO1;
161 u16 val, reg;
163 reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
164 val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
166 return 1000 * LDO_VOL_VALUE_MAP(ldo)[val];
169 static int lp3971_ldo_set_voltage(struct regulator_dev *dev,
170 int min_uV, int max_uV)
172 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
173 int ldo = rdev_get_id(dev) - LP3971_LDO1;
174 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
175 const int *vol_map = LDO_VOL_VALUE_MAP(ldo);
176 u16 val;
178 if (min_vol < vol_map[LDO_VOL_MIN_IDX] ||
179 min_vol > vol_map[LDO_VOL_MAX_IDX])
180 return -EINVAL;
182 for (val = LDO_VOL_MIN_IDX; val <= LDO_VOL_MAX_IDX; val++)
183 if (vol_map[val] >= min_vol)
184 break;
186 if (vol_map[val] > max_vol)
187 return -EINVAL;
189 return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
190 LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo), val);
193 static struct regulator_ops lp3971_ldo_ops = {
194 .list_voltage = lp3971_ldo_list_voltage,
195 .is_enabled = lp3971_ldo_is_enabled,
196 .enable = lp3971_ldo_enable,
197 .disable = lp3971_ldo_disable,
198 .get_voltage = lp3971_ldo_get_voltage,
199 .set_voltage = lp3971_ldo_set_voltage,
202 static int lp3971_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
204 return 1000 * buck_voltage_map[index];
207 static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
209 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
210 int buck = rdev_get_id(dev) - LP3971_DCDC1;
211 u16 mask = 1 << (buck * 2);
212 u16 val;
214 val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
215 return (val & mask) != 0;
218 static int lp3971_dcdc_enable(struct regulator_dev *dev)
220 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
221 int buck = rdev_get_id(dev) - LP3971_DCDC1;
222 u16 mask = 1 << (buck * 2);
224 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
227 static int lp3971_dcdc_disable(struct regulator_dev *dev)
229 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
230 int buck = rdev_get_id(dev) - LP3971_DCDC1;
231 u16 mask = 1 << (buck * 2);
233 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
236 static int lp3971_dcdc_get_voltage(struct regulator_dev *dev)
238 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
239 int buck = rdev_get_id(dev) - LP3971_DCDC1;
240 u16 reg;
241 int val;
243 reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
244 reg &= BUCK_TARGET_VOL_MASK;
246 if (reg <= BUCK_TARGET_VOL_MAX_IDX)
247 val = 1000 * buck_voltage_map[reg];
248 else {
249 val = 0;
250 dev_warn(&dev->dev, "chip reported incorrect voltage value.\n");
253 return val;
256 static int lp3971_dcdc_set_voltage(struct regulator_dev *dev,
257 int min_uV, int max_uV)
259 struct lp3971 *lp3971 = rdev_get_drvdata(dev);
260 int buck = rdev_get_id(dev) - LP3971_DCDC1;
261 int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
262 const int *vol_map = buck_voltage_map;
263 u16 val;
264 int ret;
266 if (min_vol < vol_map[BUCK_TARGET_VOL_MIN_IDX] ||
267 min_vol > vol_map[BUCK_TARGET_VOL_MAX_IDX])
268 return -EINVAL;
270 for (val = BUCK_TARGET_VOL_MIN_IDX; val <= BUCK_TARGET_VOL_MAX_IDX;
271 val++)
272 if (vol_map[val] >= min_vol)
273 break;
275 if (vol_map[val] > max_vol)
276 return -EINVAL;
278 ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
279 BUCK_TARGET_VOL_MASK, val);
280 if (ret)
281 return ret;
283 ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
284 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
285 BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
286 if (ret)
287 return ret;
289 return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
290 BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
291 0 << BUCK_VOL_CHANGE_SHIFT(buck));
294 static struct regulator_ops lp3971_dcdc_ops = {
295 .list_voltage = lp3971_dcdc_list_voltage,
296 .is_enabled = lp3971_dcdc_is_enabled,
297 .enable = lp3971_dcdc_enable,
298 .disable = lp3971_dcdc_disable,
299 .get_voltage = lp3971_dcdc_get_voltage,
300 .set_voltage = lp3971_dcdc_set_voltage,
303 static struct regulator_desc regulators[] = {
305 .name = "LDO1",
306 .id = LP3971_LDO1,
307 .ops = &lp3971_ldo_ops,
308 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
309 .type = REGULATOR_VOLTAGE,
310 .owner = THIS_MODULE,
313 .name = "LDO2",
314 .id = LP3971_LDO2,
315 .ops = &lp3971_ldo_ops,
316 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
317 .type = REGULATOR_VOLTAGE,
318 .owner = THIS_MODULE,
321 .name = "LDO3",
322 .id = LP3971_LDO3,
323 .ops = &lp3971_ldo_ops,
324 .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
325 .type = REGULATOR_VOLTAGE,
326 .owner = THIS_MODULE,
329 .name = "LDO4",
330 .id = LP3971_LDO4,
331 .ops = &lp3971_ldo_ops,
332 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
333 .type = REGULATOR_VOLTAGE,
334 .owner = THIS_MODULE,
337 .name = "LDO5",
338 .id = LP3971_LDO5,
339 .ops = &lp3971_ldo_ops,
340 .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
341 .type = REGULATOR_VOLTAGE,
342 .owner = THIS_MODULE,
345 .name = "DCDC1",
346 .id = LP3971_DCDC1,
347 .ops = &lp3971_dcdc_ops,
348 .n_voltages = ARRAY_SIZE(buck_voltage_map),
349 .type = REGULATOR_VOLTAGE,
350 .owner = THIS_MODULE,
353 .name = "DCDC2",
354 .id = LP3971_DCDC2,
355 .ops = &lp3971_dcdc_ops,
356 .n_voltages = ARRAY_SIZE(buck_voltage_map),
357 .type = REGULATOR_VOLTAGE,
358 .owner = THIS_MODULE,
361 .name = "DCDC3",
362 .id = LP3971_DCDC3,
363 .ops = &lp3971_dcdc_ops,
364 .n_voltages = ARRAY_SIZE(buck_voltage_map),
365 .type = REGULATOR_VOLTAGE,
366 .owner = THIS_MODULE,
370 static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
371 u16 *dest)
373 int ret;
375 if (count != 1)
376 return -EIO;
377 ret = i2c_smbus_read_byte_data(i2c, reg);
378 if (ret < 0 || count != 1)
379 return -EIO;
381 *dest = ret;
382 return 0;
385 static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
386 const u16 *src)
388 int ret;
390 if (count != 1)
391 return -EIO;
392 ret = i2c_smbus_write_byte_data(i2c, reg, *src);
393 if (ret >= 0)
394 return 0;
396 return ret;
399 static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
401 u16 val = 0;
403 mutex_lock(&lp3971->io_lock);
405 lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
407 dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
408 (unsigned)val&0xff);
410 mutex_unlock(&lp3971->io_lock);
412 return val & 0xff;
415 static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
417 u16 tmp;
418 int ret;
420 mutex_lock(&lp3971->io_lock);
422 ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
423 tmp = (tmp & ~mask) | val;
424 if (ret == 0) {
425 ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
426 dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
427 (unsigned)val&0xff);
429 mutex_unlock(&lp3971->io_lock);
431 return ret;
434 static int setup_regulators(struct lp3971 *lp3971,
435 struct lp3971_platform_data *pdata)
437 int i, err;
438 int num_regulators = pdata->num_regulators;
439 lp3971->num_regulators = num_regulators;
440 lp3971->rdev = kzalloc(sizeof(struct regulator_dev *) * num_regulators,
441 GFP_KERNEL);
443 /* Instantiate the regulators */
444 for (i = 0; i < num_regulators; i++) {
445 int id = pdata->regulators[i].id;
446 lp3971->rdev[i] = regulator_register(&regulators[id],
447 lp3971->dev, pdata->regulators[i].initdata, lp3971);
449 err = IS_ERR(lp3971->rdev[i]);
450 if (err) {
451 dev_err(lp3971->dev, "regulator init failed: %d\n",
452 err);
453 goto error;
457 return 0;
458 error:
459 for (i = 0; i < num_regulators; i++)
460 if (lp3971->rdev[i])
461 regulator_unregister(lp3971->rdev[i]);
462 kfree(lp3971->rdev);
463 lp3971->rdev = NULL;
464 return err;
467 static int __devinit lp3971_i2c_probe(struct i2c_client *i2c,
468 const struct i2c_device_id *id)
470 struct lp3971 *lp3971;
471 struct lp3971_platform_data *pdata = i2c->dev.platform_data;
472 int ret;
473 u16 val;
475 lp3971 = kzalloc(sizeof(struct lp3971), GFP_KERNEL);
476 if (lp3971 == NULL) {
477 ret = -ENOMEM;
478 goto err;
481 lp3971->i2c = i2c;
482 lp3971->dev = &i2c->dev;
483 i2c_set_clientdata(i2c, lp3971);
485 mutex_init(&lp3971->io_lock);
487 /* Detect LP3971 */
488 ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
489 if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
490 ret = -ENODEV;
491 if (ret < 0) {
492 dev_err(&i2c->dev, "failed to detect device\n");
493 goto err_detect;
496 if (pdata) {
497 ret = setup_regulators(lp3971, pdata);
498 if (ret < 0)
499 goto err_detect;
500 } else
501 dev_warn(lp3971->dev, "No platform init data supplied\n");
503 return 0;
505 err_detect:
506 i2c_set_clientdata(i2c, NULL);
507 kfree(lp3971);
508 err:
509 return ret;
512 static int __devexit lp3971_i2c_remove(struct i2c_client *i2c)
514 struct lp3971 *lp3971 = i2c_get_clientdata(i2c);
515 int i;
516 for (i = 0; i < lp3971->num_regulators; i++)
517 if (lp3971->rdev[i])
518 regulator_unregister(lp3971->rdev[i]);
519 kfree(lp3971->rdev);
520 i2c_set_clientdata(i2c, NULL);
521 kfree(lp3971);
523 return 0;
526 static const struct i2c_device_id lp3971_i2c_id[] = {
527 { "lp3971", 0 },
530 MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
532 static struct i2c_driver lp3971_i2c_driver = {
533 .driver = {
534 .name = "LP3971",
535 .owner = THIS_MODULE,
537 .probe = lp3971_i2c_probe,
538 .remove = __devexit_p(lp3971_i2c_remove),
539 .id_table = lp3971_i2c_id,
542 static int __init lp3971_module_init(void)
544 int ret;
546 ret = i2c_add_driver(&lp3971_i2c_driver);
547 if (ret != 0)
548 pr_err("Failed to register I2C driver: %d\n", ret);
550 return ret;
552 module_init(lp3971_module_init);
554 static void __exit lp3971_module_exit(void)
556 i2c_del_driver(&lp3971_i2c_driver);
558 module_exit(lp3971_module_exit);
560 MODULE_LICENSE("GPL");
561 MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
562 MODULE_DESCRIPTION("LP3971 PMIC driver");