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/uaccess.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/platform_data/lp8755.h>
27 #define LP8755_REG_BUCK0 0x00
28 #define LP8755_REG_BUCK1 0x03
29 #define LP8755_REG_BUCK2 0x04
30 #define LP8755_REG_BUCK3 0x01
31 #define LP8755_REG_BUCK4 0x05
32 #define LP8755_REG_BUCK5 0x02
33 #define LP8755_REG_MAX 0xFF
35 #define LP8755_BUCK_EN_M BIT(7)
36 #define LP8755_BUCK_LINEAR_OUT_MAX 0x76
37 #define LP8755_BUCK_VOUT_M 0x7F
39 struct lp8755_mphase {
41 int buck_num[LP8755_BUCK_MAX];
46 struct regmap *regmap;
47 struct lp8755_platform_data *pdata;
53 struct regulator_dev *rdev[LP8755_BUCK_MAX];
57 *lp8755_read : read a single register value from lp8755.
58 *@pchip : device to read from
59 *@reg : register to read from
60 *@val : pointer to store read value
62 static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
65 return regmap_read(pchip->regmap, reg, val);
69 *lp8755_write : write a single register value to lp8755.
70 *@pchip : device to write to
71 *@reg : register to write to
72 *@val : value to be written
74 static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
77 return regmap_write(pchip->regmap, reg, val);
81 *lp8755_update_bits : set the values of bit fields in lp8755 register.
82 *@pchip : device to read from
83 *@reg : register to update
84 *@mask : bitmask to be changed
85 *@val : value for bitmask
87 static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
88 unsigned int mask, unsigned int val)
90 return regmap_update_bits(pchip->regmap, reg, mask, val);
93 static int lp8755_buck_enable_time(struct regulator_dev *rdev)
97 enum lp8755_bucks id = rdev_get_id(rdev);
98 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
100 ret = lp8755_read(pchip, 0x12 + id, ®val);
102 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
105 return (regval & 0xff) * 100;
108 static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
111 unsigned int regbval = 0x0;
112 enum lp8755_bucks id = rdev_get_id(rdev);
113 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
116 case REGULATOR_MODE_FAST:
117 /* forced pwm mode */
118 regbval = (0x01 << id);
120 case REGULATOR_MODE_NORMAL:
121 /* enable automatic pwm/pfm mode */
122 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
126 case REGULATOR_MODE_IDLE:
127 /* enable automatic pwm/pfm/lppfm mode */
128 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
132 ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
137 dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
138 /* forced pwm mode */
139 regbval = (0x01 << id);
142 ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
147 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
151 static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
155 enum lp8755_bucks id = rdev_get_id(rdev);
156 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
158 ret = lp8755_read(pchip, 0x06, ®val);
162 /* mode fast means forced pwm mode */
163 if (regval & (0x01 << id))
164 return REGULATOR_MODE_FAST;
166 ret = lp8755_read(pchip, 0x08 + id, ®val);
170 /* mode idle means automatic pwm/pfm/lppfm mode */
172 return REGULATOR_MODE_IDLE;
174 /* mode normal means automatic pwm/pfm mode */
175 return REGULATOR_MODE_NORMAL;
178 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
182 static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
185 unsigned int regval = 0x00;
186 enum lp8755_bucks id = rdev_get_id(rdev);
187 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
212 case 15001 ... 30000:
217 "Not supported ramp value %d %s\n", ramp, __func__);
221 ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
226 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
230 static struct regulator_ops lp8755_buck_ops = {
231 .list_voltage = regulator_list_voltage_linear,
232 .set_voltage_sel = regulator_set_voltage_sel_regmap,
233 .get_voltage_sel = regulator_get_voltage_sel_regmap,
234 .enable = regulator_enable_regmap,
235 .disable = regulator_disable_regmap,
236 .is_enabled = regulator_is_enabled_regmap,
237 .enable_time = lp8755_buck_enable_time,
238 .set_mode = lp8755_buck_set_mode,
239 .get_mode = lp8755_buck_get_mode,
240 .set_ramp_delay = lp8755_buck_set_ramp,
243 #define lp8755_rail(_id) "lp8755_buck"#_id
244 #define lp8755_buck_init(_id)\
247 .name = lp8755_rail(_id),\
248 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
254 static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
255 [LP8755_BUCK0] = lp8755_buck_init(0),
256 [LP8755_BUCK1] = lp8755_buck_init(1),
257 [LP8755_BUCK2] = lp8755_buck_init(2),
258 [LP8755_BUCK3] = lp8755_buck_init(3),
259 [LP8755_BUCK4] = lp8755_buck_init(4),
260 [LP8755_BUCK5] = lp8755_buck_init(5),
263 static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
264 { 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
265 { 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
266 LP8755_BUCK4, LP8755_BUCK5 } },
267 { 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
269 { 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
270 { 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
271 { 2, { LP8755_BUCK0, LP8755_BUCK5} },
272 { 1, { LP8755_BUCK0} },
273 { 2, { LP8755_BUCK0, LP8755_BUCK3} },
274 { 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
277 static int lp8755_init_data(struct lp8755_chip *pchip)
280 int ret, icnt, buck_num;
281 struct lp8755_platform_data *pdata = pchip->pdata;
283 /* read back muti-phase configuration */
284 ret = lp8755_read(pchip, 0x3D, ®val);
287 pchip->mphase = regval & 0x0F;
289 /* set default data based on multi-phase config */
290 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
291 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
292 pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
297 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
301 #define lp8755_buck_desc(_id)\
303 .name = lp8755_rail(_id),\
304 .id = LP8755_BUCK##_id,\
305 .ops = &lp8755_buck_ops,\
306 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
309 .type = REGULATOR_VOLTAGE,\
310 .owner = THIS_MODULE,\
311 .enable_reg = LP8755_REG_BUCK##_id,\
312 .enable_mask = LP8755_BUCK_EN_M,\
313 .vsel_reg = LP8755_REG_BUCK##_id,\
314 .vsel_mask = LP8755_BUCK_VOUT_M,\
317 static struct regulator_desc lp8755_regulators[] = {
326 static int lp8755_regulator_init(struct lp8755_chip *pchip)
328 int ret, icnt, buck_num;
329 struct lp8755_platform_data *pdata = pchip->pdata;
330 struct regulator_config rconfig = { };
332 rconfig.regmap = pchip->regmap;
333 rconfig.dev = pchip->dev;
334 rconfig.driver_data = pchip;
336 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
337 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
338 rconfig.init_data = pdata->buck_data[buck_num];
339 rconfig.of_node = pchip->dev->of_node;
340 pchip->rdev[buck_num] =
341 regulator_register(&lp8755_regulators[buck_num], &rconfig);
342 if (IS_ERR(pchip->rdev[buck_num])) {
343 ret = PTR_ERR(pchip->rdev[buck_num]);
344 pchip->rdev[buck_num] = NULL;
345 dev_err(pchip->dev, "regulator init failed: buck %d\n",
354 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
355 regulator_unregister(pchip->rdev[icnt]);
359 static irqreturn_t lp8755_irq_handler(int irq, void *data)
362 unsigned int flag0, flag1;
363 struct lp8755_chip *pchip = data;
365 /* read flag0 register */
366 ret = lp8755_read(pchip, 0x0D, &flag0);
369 /* clear flag register to pull up int. pin */
370 ret = lp8755_write(pchip, 0x0D, 0x00);
374 /* sent power fault detection event to specific regulator */
375 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
376 if ((flag0 & (0x4 << icnt))
377 && (pchip->irqmask & (0x04 << icnt))
378 && (pchip->rdev[icnt] != NULL))
379 regulator_notifier_call_chain(pchip->rdev[icnt],
380 LP8755_EVENT_PWR_FAULT,
383 /* read flag1 register */
384 ret = lp8755_read(pchip, 0x0E, &flag1);
387 /* clear flag register to pull up int. pin */
388 ret = lp8755_write(pchip, 0x0E, 0x00);
392 /* send OCP event to all regualtor devices */
393 if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
394 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
395 if (pchip->rdev[icnt] != NULL)
396 regulator_notifier_call_chain(pchip->rdev[icnt],
400 /* send OVP event to all regualtor devices */
401 if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
402 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
403 if (pchip->rdev[icnt] != NULL)
404 regulator_notifier_call_chain(pchip->rdev[icnt],
410 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
414 static int lp8755_int_config(struct lp8755_chip *pchip)
419 if (pchip->irq == 0) {
420 dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
424 ret = lp8755_read(pchip, 0x0F, ®val);
427 pchip->irqmask = regval;
428 ret = request_threaded_irq(pchip->irq, NULL, lp8755_irq_handler,
429 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
430 "lp8755-irq", pchip);
437 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
441 static const struct regmap_config lp8755_regmap = {
444 .max_register = LP8755_REG_MAX,
447 static int lp8755_probe(struct i2c_client *client,
448 const struct i2c_device_id *id)
451 struct lp8755_chip *pchip;
452 struct lp8755_platform_data *pdata = client->dev.platform_data;
454 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
455 dev_err(&client->dev, "i2c functionality check fail.\n");
459 pchip = devm_kzalloc(&client->dev,
460 sizeof(struct lp8755_chip), GFP_KERNEL);
464 pchip->dev = &client->dev;
465 pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
466 if (IS_ERR(pchip->regmap)) {
467 ret = PTR_ERR(pchip->regmap);
468 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
471 i2c_set_clientdata(client, pchip);
474 pchip->pdata = pdata;
475 pchip->mphase = pdata->mphase;
477 pchip->pdata = devm_kzalloc(pchip->dev,
478 sizeof(struct lp8755_platform_data),
482 ret = lp8755_init_data(pchip);
484 dev_err(&client->dev, "fail to initialize chip\n");
489 ret = lp8755_regulator_init(pchip);
491 dev_err(&client->dev, "fail to initialize regulators\n");
495 pchip->irq = client->irq;
496 ret = lp8755_int_config(pchip);
498 dev_err(&client->dev, "fail to irq config\n");
505 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
506 regulator_unregister(pchip->rdev[icnt]);
510 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
511 lp8755_write(pchip, icnt, 0x00);
516 static int lp8755_remove(struct i2c_client *client)
519 struct lp8755_chip *pchip = i2c_get_clientdata(client);
521 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
522 regulator_unregister(pchip->rdev[icnt]);
524 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
525 lp8755_write(pchip, icnt, 0x00);
528 free_irq(pchip->irq, pchip);
533 static const struct i2c_device_id lp8755_id[] = {
538 MODULE_DEVICE_TABLE(i2c, lp8755_id);
540 static struct i2c_driver lp8755_i2c_driver = {
544 .probe = lp8755_probe,
545 .remove = lp8755_remove,
546 .id_table = lp8755_id,
549 static int __init lp8755_init(void)
551 return i2c_add_driver(&lp8755_i2c_driver);
554 subsys_initcall(lp8755_init);
556 static void __exit lp8755_exit(void)
558 i2c_del_driver(&lp8755_i2c_driver);
561 module_exit(lp8755_exit);
563 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
564 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
565 MODULE_LICENSE("GPL v2");