2 * LP8755 High Performance Power Management Unit : System Interface Driver
4 * Copyright 2012 Texas Instruments
6 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/i2c.h>
17 #include <linux/err.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/gpio.h>
21 #include <linux/regmap.h>
22 #include <linux/delay.h>
23 #include <linux/uaccess.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/machine.h>
26 #include <linux/platform_data/lp8755.h>
28 #define LP8755_REG_BUCK0 0x00
29 #define LP8755_REG_BUCK1 0x03
30 #define LP8755_REG_BUCK2 0x04
31 #define LP8755_REG_BUCK3 0x01
32 #define LP8755_REG_BUCK4 0x05
33 #define LP8755_REG_BUCK5 0x02
34 #define LP8755_REG_MAX 0xFF
36 #define LP8755_BUCK_EN_M BIT(7)
37 #define LP8755_BUCK_LINEAR_OUT_MAX 0x76
38 #define LP8755_BUCK_VOUT_M 0x7F
49 struct lp8755_mphase {
51 int buck_num[LP8755_BUCK_MAX];
56 struct regmap *regmap;
57 struct lp8755_platform_data *pdata;
63 struct regulator_dev *rdev[LP8755_BUCK_MAX];
67 *lp8755_read : read a single register value from lp8755.
68 *@pchip : device to read from
69 *@reg : register to read from
70 *@val : pointer to store read value
72 static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
75 return regmap_read(pchip->regmap, reg, val);
79 *lp8755_write : write a single register value to lp8755.
80 *@pchip : device to write to
81 *@reg : register to write to
82 *@val : value to be written
84 static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
87 return regmap_write(pchip->regmap, reg, val);
91 *lp8755_update_bits : set the values of bit fields in lp8755 register.
92 *@pchip : device to read from
93 *@reg : register to update
94 *@mask : bitmask to be changed
95 *@val : value for bitmask
97 static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
98 unsigned int mask, unsigned int val)
100 return regmap_update_bits(pchip->regmap, reg, mask, val);
103 static int lp8755_buck_enable_time(struct regulator_dev *rdev)
107 enum lp8755_bucks id = rdev_get_id(rdev);
108 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
110 ret = lp8755_read(pchip, 0x12 + id, ®val);
112 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
115 return (regval & 0xff) * 100;
118 static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
121 unsigned int regbval = 0x0;
122 enum lp8755_bucks id = rdev_get_id(rdev);
123 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
126 case REGULATOR_MODE_FAST:
127 /* forced pwm mode */
128 regbval = (0x01 << id);
130 case REGULATOR_MODE_NORMAL:
131 /* enable automatic pwm/pfm mode */
132 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
136 case REGULATOR_MODE_IDLE:
137 /* enable automatic pwm/pfm/lppfm mode */
138 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
142 ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
147 dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
148 /* forced pwm mode */
149 regbval = (0x01 << id);
152 ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
157 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
161 static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
165 enum lp8755_bucks id = rdev_get_id(rdev);
166 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
168 ret = lp8755_read(pchip, 0x06, ®val);
172 /* mode fast means forced pwm mode */
173 if (regval & (0x01 << id))
174 return REGULATOR_MODE_FAST;
176 ret = lp8755_read(pchip, 0x08 + id, ®val);
180 /* mode idle means automatic pwm/pfm/lppfm mode */
182 return REGULATOR_MODE_IDLE;
184 /* mode normal means automatic pwm/pfm mode */
185 return REGULATOR_MODE_NORMAL;
188 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
192 static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
195 unsigned int regval = 0x00;
196 enum lp8755_bucks id = rdev_get_id(rdev);
197 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
222 case 15001 ... 30000:
227 "Not supported ramp value %d %s\n", ramp, __func__);
231 ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
236 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
240 static struct regulator_ops lp8755_buck_ops = {
241 .list_voltage = regulator_list_voltage_linear,
242 .set_voltage_sel = regulator_set_voltage_sel_regmap,
243 .get_voltage_sel = regulator_get_voltage_sel_regmap,
244 .enable = regulator_enable_regmap,
245 .disable = regulator_disable_regmap,
246 .is_enabled = regulator_is_enabled_regmap,
247 .enable_time = lp8755_buck_enable_time,
248 .set_mode = lp8755_buck_set_mode,
249 .get_mode = lp8755_buck_get_mode,
250 .set_ramp_delay = lp8755_buck_set_ramp,
253 #define lp8755_rail(_id) "lp8755_buck"#_id
254 #define lp8755_buck_init(_id)\
257 .name = lp8755_rail(_id),\
258 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
264 static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
265 [BUCK0] = lp8755_buck_init(0),
266 [BUCK1] = lp8755_buck_init(1),
267 [BUCK2] = lp8755_buck_init(2),
268 [BUCK3] = lp8755_buck_init(3),
269 [BUCK4] = lp8755_buck_init(4),
270 [BUCK5] = lp8755_buck_init(5),
273 static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
274 {3, {BUCK0, BUCK3, BUCK5}
276 {6, {BUCK0, BUCK1, BUCK2, BUCK3, BUCK4, BUCK5}
278 {5, {BUCK0, BUCK2, BUCK3, BUCK4, BUCK5}
280 {4, {BUCK0, BUCK3, BUCK4, BUCK5}
282 {3, {BUCK0, BUCK4, BUCK5}
290 {4, {BUCK0, BUCK2, BUCK3, BUCK5}
294 static int lp8755_init_data(struct lp8755_chip *pchip)
297 int ret, icnt, buck_num;
298 struct lp8755_platform_data *pdata = pchip->pdata;
300 /* read back muti-phase configuration */
301 ret = lp8755_read(pchip, 0x3D, ®val);
304 pchip->mphase = regval & 0x0F;
306 /* set default data based on multi-phase config */
307 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
308 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
309 pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
314 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
318 #define lp8755_buck_desc(_id)\
320 .name = lp8755_rail(_id),\
321 .id = LP8755_BUCK##_id,\
322 .ops = &lp8755_buck_ops,\
323 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
326 .type = REGULATOR_VOLTAGE,\
327 .owner = THIS_MODULE,\
328 .enable_reg = LP8755_REG_BUCK##_id,\
329 .enable_mask = LP8755_BUCK_EN_M,\
330 .vsel_reg = LP8755_REG_BUCK##_id,\
331 .vsel_mask = LP8755_BUCK_VOUT_M,\
334 static struct regulator_desc lp8755_regulators[] = {
343 static int lp8755_regulator_init(struct lp8755_chip *pchip)
345 int ret, icnt, buck_num;
346 struct lp8755_platform_data *pdata = pchip->pdata;
347 struct regulator_config rconfig = { };
349 rconfig.regmap = pchip->regmap;
350 rconfig.dev = pchip->dev;
351 rconfig.driver_data = pchip;
353 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
354 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
355 rconfig.init_data = pdata->buck_data[buck_num];
356 rconfig.of_node = pchip->dev->of_node;
357 pchip->rdev[buck_num] =
358 regulator_register(&lp8755_regulators[buck_num], &rconfig);
359 if (IS_ERR(pchip->rdev[buck_num])) {
360 ret = PTR_ERR(pchip->rdev[buck_num]);
361 pchip->rdev[buck_num] = NULL;
362 dev_err(pchip->dev, "regulator init failed: buck %d\n",
371 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
372 regulator_unregister(pchip->rdev[icnt]);
376 static irqreturn_t lp8755_irq_handler(int irq, void *data)
379 unsigned int flag0, flag1;
380 struct lp8755_chip *pchip = data;
382 /* read flag0 register */
383 ret = lp8755_read(pchip, 0x0D, &flag0);
386 /* clear flag register to pull up int. pin */
387 ret = lp8755_write(pchip, 0x0D, 0x00);
391 /* sent power fault detection event to specific regulator */
392 for (icnt = 0; icnt < 6; icnt++)
393 if ((flag0 & (0x4 << icnt))
394 && (pchip->irqmask & (0x04 << icnt))
395 && (pchip->rdev[icnt] != NULL))
396 regulator_notifier_call_chain(pchip->rdev[icnt],
397 LP8755_EVENT_PWR_FAULT,
400 /* read flag1 register */
401 ret = lp8755_read(pchip, 0x0E, &flag1);
404 /* clear flag register to pull up int. pin */
405 ret = lp8755_write(pchip, 0x0E, 0x00);
409 /* send OCP event to all regualtor devices */
410 if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
411 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
412 if (pchip->rdev[icnt] != NULL)
413 regulator_notifier_call_chain(pchip->rdev[icnt],
417 /* send OVP event to all regualtor devices */
418 if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
419 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
420 if (pchip->rdev[icnt] != NULL)
421 regulator_notifier_call_chain(pchip->rdev[icnt],
427 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
431 static int lp8755_int_config(struct lp8755_chip *pchip)
436 if (pchip->irq == 0) {
437 dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
441 ret = lp8755_read(pchip, 0x0F, ®val);
444 pchip->irqmask = regval;
445 ret = request_threaded_irq(pchip->irq, NULL, lp8755_irq_handler,
446 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
447 "lp8755-irq", pchip);
454 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
458 static const struct regmap_config lp8755_regmap = {
461 .max_register = LP8755_REG_MAX,
464 static int lp8755_probe(struct i2c_client *client,
465 const struct i2c_device_id *id)
468 struct lp8755_chip *pchip;
469 struct lp8755_platform_data *pdata = client->dev.platform_data;
471 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
472 dev_err(&client->dev, "i2c functionality check fail.\n");
476 pchip = devm_kzalloc(&client->dev,
477 sizeof(struct lp8755_chip), GFP_KERNEL);
481 pchip->dev = &client->dev;
482 pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
483 if (IS_ERR(pchip->regmap)) {
484 ret = PTR_ERR(pchip->regmap);
485 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
488 i2c_set_clientdata(client, pchip);
491 pchip->pdata = pdata;
492 pchip->mphase = pdata->mphase;
494 pchip->pdata = devm_kzalloc(pchip->dev,
495 sizeof(struct lp8755_platform_data),
499 ret = lp8755_init_data(pchip);
504 ret = lp8755_regulator_init(pchip);
508 pchip->irq = client->irq;
509 ret = lp8755_int_config(pchip);
516 dev_err(&client->dev, "fail to irq config\n");
518 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
519 regulator_unregister(pchip->rdev[icnt]);
522 dev_err(&client->dev, "fail to initialize regulators\n");
524 for (icnt = 0; icnt < 0x06; icnt++)
525 lp8755_write(pchip, icnt, 0x00);
528 dev_err(&client->dev, "fail to initialize chip\n");
532 static int lp8755_remove(struct i2c_client *client)
535 struct lp8755_chip *pchip = i2c_get_clientdata(client);
537 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
538 regulator_unregister(pchip->rdev[icnt]);
540 for (icnt = 0; icnt < 0x06; icnt++)
541 lp8755_write(pchip, icnt, 0x00);
544 free_irq(pchip->irq, pchip);
549 static const struct i2c_device_id lp8755_id[] = {
554 MODULE_DEVICE_TABLE(i2c, lp8755_id);
556 static struct i2c_driver lp8755_i2c_driver = {
560 .probe = lp8755_probe,
561 .remove = lp8755_remove,
562 .id_table = lp8755_id,
565 static int __init lp8755_init(void)
567 return i2c_add_driver(&lp8755_i2c_driver);
570 subsys_initcall(lp8755_init);
572 static void __exit lp8755_exit(void)
574 i2c_del_driver(&lp8755_i2c_driver);
577 module_exit(lp8755_exit);
579 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
580 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
581 MODULE_LICENSE("GPL v2");