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
40 struct lp8755_mphase
{
42 int buck_num
[LP8755_BUCK_MAX
];
47 struct regmap
*regmap
;
48 struct lp8755_platform_data
*pdata
;
54 struct regulator_dev
*rdev
[LP8755_BUCK_MAX
];
58 *lp8755_read : read a single register value from lp8755.
59 *@pchip : device to read from
60 *@reg : register to read from
61 *@val : pointer to store read value
63 static int lp8755_read(struct lp8755_chip
*pchip
, unsigned int reg
,
66 return regmap_read(pchip
->regmap
, reg
, val
);
70 *lp8755_write : write a single register value to lp8755.
71 *@pchip : device to write to
72 *@reg : register to write to
73 *@val : value to be written
75 static int lp8755_write(struct lp8755_chip
*pchip
, unsigned int reg
,
78 return regmap_write(pchip
->regmap
, reg
, val
);
82 *lp8755_update_bits : set the values of bit fields in lp8755 register.
83 *@pchip : device to read from
84 *@reg : register to update
85 *@mask : bitmask to be changed
86 *@val : value for bitmask
88 static int lp8755_update_bits(struct lp8755_chip
*pchip
, unsigned int reg
,
89 unsigned int mask
, unsigned int val
)
91 return regmap_update_bits(pchip
->regmap
, reg
, mask
, val
);
94 static int lp8755_buck_enable_time(struct regulator_dev
*rdev
)
98 enum lp8755_bucks id
= rdev_get_id(rdev
);
99 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
101 ret
= lp8755_read(pchip
, 0x12 + id
, ®val
);
103 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
106 return (regval
& 0xff) * 100;
109 static int lp8755_buck_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
112 unsigned int regbval
= 0x0;
113 enum lp8755_bucks id
= rdev_get_id(rdev
);
114 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
117 case REGULATOR_MODE_FAST
:
118 /* forced pwm mode */
119 regbval
= (0x01 << id
);
121 case REGULATOR_MODE_NORMAL
:
122 /* enable automatic pwm/pfm mode */
123 ret
= lp8755_update_bits(pchip
, 0x08 + id
, 0x20, 0x00);
127 case REGULATOR_MODE_IDLE
:
128 /* enable automatic pwm/pfm/lppfm mode */
129 ret
= lp8755_update_bits(pchip
, 0x08 + id
, 0x20, 0x20);
133 ret
= lp8755_update_bits(pchip
, 0x10, 0x01, 0x01);
138 dev_err(pchip
->dev
, "Not supported buck mode %s\n", __func__
);
139 /* forced pwm mode */
140 regbval
= (0x01 << id
);
143 ret
= lp8755_update_bits(pchip
, 0x06, 0x01 << id
, regbval
);
148 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
152 static unsigned int lp8755_buck_get_mode(struct regulator_dev
*rdev
)
156 enum lp8755_bucks id
= rdev_get_id(rdev
);
157 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
159 ret
= lp8755_read(pchip
, 0x06, ®val
);
163 /* mode fast means forced pwm mode */
164 if (regval
& (0x01 << id
))
165 return REGULATOR_MODE_FAST
;
167 ret
= lp8755_read(pchip
, 0x08 + id
, ®val
);
171 /* mode idle means automatic pwm/pfm/lppfm mode */
173 return REGULATOR_MODE_IDLE
;
175 /* mode normal means automatic pwm/pfm mode */
176 return REGULATOR_MODE_NORMAL
;
179 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
183 static int lp8755_buck_set_ramp(struct regulator_dev
*rdev
, int ramp
)
186 unsigned int regval
= 0x00;
187 enum lp8755_bucks id
= rdev_get_id(rdev
);
188 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
213 case 15001 ... 30000:
218 "Not supported ramp value %d %s\n", ramp
, __func__
);
222 ret
= lp8755_update_bits(pchip
, 0x07 + id
, 0x07, regval
);
227 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
231 static struct regulator_ops lp8755_buck_ops
= {
232 .list_voltage
= regulator_list_voltage_linear
,
233 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
234 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
235 .enable
= regulator_enable_regmap
,
236 .disable
= regulator_disable_regmap
,
237 .is_enabled
= regulator_is_enabled_regmap
,
238 .enable_time
= lp8755_buck_enable_time
,
239 .set_mode
= lp8755_buck_set_mode
,
240 .get_mode
= lp8755_buck_get_mode
,
241 .set_ramp_delay
= lp8755_buck_set_ramp
,
244 #define lp8755_rail(_id) "lp8755_buck"#_id
245 #define lp8755_buck_init(_id)\
248 .name = lp8755_rail(_id),\
249 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
255 static struct regulator_init_data lp8755_reg_default
[LP8755_BUCK_MAX
] = {
256 [LP8755_BUCK0
] = lp8755_buck_init(0),
257 [LP8755_BUCK1
] = lp8755_buck_init(1),
258 [LP8755_BUCK2
] = lp8755_buck_init(2),
259 [LP8755_BUCK3
] = lp8755_buck_init(3),
260 [LP8755_BUCK4
] = lp8755_buck_init(4),
261 [LP8755_BUCK5
] = lp8755_buck_init(5),
264 static const struct lp8755_mphase mphase_buck
[MPHASE_CONF_MAX
] = {
265 { 3, { LP8755_BUCK0
, LP8755_BUCK3
, LP8755_BUCK5
} },
266 { 6, { LP8755_BUCK0
, LP8755_BUCK1
, LP8755_BUCK2
, LP8755_BUCK3
,
267 LP8755_BUCK4
, LP8755_BUCK5
} },
268 { 5, { LP8755_BUCK0
, LP8755_BUCK2
, LP8755_BUCK3
, LP8755_BUCK4
,
270 { 4, { LP8755_BUCK0
, LP8755_BUCK3
, LP8755_BUCK4
, LP8755_BUCK5
} },
271 { 3, { LP8755_BUCK0
, LP8755_BUCK4
, LP8755_BUCK5
} },
272 { 2, { LP8755_BUCK0
, LP8755_BUCK5
} },
273 { 1, { LP8755_BUCK0
} },
274 { 2, { LP8755_BUCK0
, LP8755_BUCK3
} },
275 { 4, { LP8755_BUCK0
, LP8755_BUCK2
, LP8755_BUCK3
, LP8755_BUCK5
} },
278 static int lp8755_init_data(struct lp8755_chip
*pchip
)
281 int ret
, icnt
, buck_num
;
282 struct lp8755_platform_data
*pdata
= pchip
->pdata
;
284 /* read back muti-phase configuration */
285 ret
= lp8755_read(pchip
, 0x3D, ®val
);
288 pchip
->mphase
= regval
& 0x0F;
290 /* set default data based on multi-phase config */
291 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++) {
292 buck_num
= mphase_buck
[pchip
->mphase
].buck_num
[icnt
];
293 pdata
->buck_data
[buck_num
] = &lp8755_reg_default
[buck_num
];
298 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
302 #define lp8755_buck_desc(_id)\
304 .name = lp8755_rail(_id),\
305 .id = LP8755_BUCK##_id,\
306 .ops = &lp8755_buck_ops,\
307 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
310 .type = REGULATOR_VOLTAGE,\
311 .owner = THIS_MODULE,\
312 .enable_reg = LP8755_REG_BUCK##_id,\
313 .enable_mask = LP8755_BUCK_EN_M,\
314 .vsel_reg = LP8755_REG_BUCK##_id,\
315 .vsel_mask = LP8755_BUCK_VOUT_M,\
318 static struct regulator_desc lp8755_regulators
[] = {
327 static int lp8755_regulator_init(struct lp8755_chip
*pchip
)
329 int ret
, icnt
, buck_num
;
330 struct lp8755_platform_data
*pdata
= pchip
->pdata
;
331 struct regulator_config rconfig
= { };
333 rconfig
.regmap
= pchip
->regmap
;
334 rconfig
.dev
= pchip
->dev
;
335 rconfig
.driver_data
= pchip
;
337 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++) {
338 buck_num
= mphase_buck
[pchip
->mphase
].buck_num
[icnt
];
339 rconfig
.init_data
= pdata
->buck_data
[buck_num
];
340 rconfig
.of_node
= pchip
->dev
->of_node
;
341 pchip
->rdev
[buck_num
] =
342 regulator_register(&lp8755_regulators
[buck_num
], &rconfig
);
343 if (IS_ERR(pchip
->rdev
[buck_num
])) {
344 ret
= PTR_ERR(pchip
->rdev
[buck_num
]);
345 pchip
->rdev
[buck_num
] = NULL
;
346 dev_err(pchip
->dev
, "regulator init failed: buck %d\n",
355 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
356 regulator_unregister(pchip
->rdev
[icnt
]);
360 static irqreturn_t
lp8755_irq_handler(int irq
, void *data
)
363 unsigned int flag0
, flag1
;
364 struct lp8755_chip
*pchip
= data
;
366 /* read flag0 register */
367 ret
= lp8755_read(pchip
, 0x0D, &flag0
);
370 /* clear flag register to pull up int. pin */
371 ret
= lp8755_write(pchip
, 0x0D, 0x00);
375 /* sent power fault detection event to specific regulator */
376 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
377 if ((flag0
& (0x4 << icnt
))
378 && (pchip
->irqmask
& (0x04 << icnt
))
379 && (pchip
->rdev
[icnt
] != NULL
))
380 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
381 LP8755_EVENT_PWR_FAULT
,
384 /* read flag1 register */
385 ret
= lp8755_read(pchip
, 0x0E, &flag1
);
388 /* clear flag register to pull up int. pin */
389 ret
= lp8755_write(pchip
, 0x0E, 0x00);
393 /* send OCP event to all regualtor devices */
394 if ((flag1
& 0x01) && (pchip
->irqmask
& 0x01))
395 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
396 if (pchip
->rdev
[icnt
] != NULL
)
397 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
401 /* send OVP event to all regualtor devices */
402 if ((flag1
& 0x02) && (pchip
->irqmask
& 0x02))
403 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
404 if (pchip
->rdev
[icnt
] != NULL
)
405 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
411 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
415 static int lp8755_int_config(struct lp8755_chip
*pchip
)
420 if (pchip
->irq
== 0) {
421 dev_warn(pchip
->dev
, "not use interrupt : %s\n", __func__
);
425 ret
= lp8755_read(pchip
, 0x0F, ®val
);
428 pchip
->irqmask
= regval
;
429 ret
= request_threaded_irq(pchip
->irq
, NULL
, lp8755_irq_handler
,
430 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
431 "lp8755-irq", pchip
);
438 dev_err(pchip
->dev
, "i2c acceess error %s\n", __func__
);
442 static const struct regmap_config lp8755_regmap
= {
445 .max_register
= LP8755_REG_MAX
,
448 static int lp8755_probe(struct i2c_client
*client
,
449 const struct i2c_device_id
*id
)
452 struct lp8755_chip
*pchip
;
453 struct lp8755_platform_data
*pdata
= client
->dev
.platform_data
;
455 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
456 dev_err(&client
->dev
, "i2c functionality check fail.\n");
460 pchip
= devm_kzalloc(&client
->dev
,
461 sizeof(struct lp8755_chip
), GFP_KERNEL
);
465 pchip
->dev
= &client
->dev
;
466 pchip
->regmap
= devm_regmap_init_i2c(client
, &lp8755_regmap
);
467 if (IS_ERR(pchip
->regmap
)) {
468 ret
= PTR_ERR(pchip
->regmap
);
469 dev_err(&client
->dev
, "fail to allocate regmap %d\n", ret
);
472 i2c_set_clientdata(client
, pchip
);
475 pchip
->pdata
= pdata
;
476 pchip
->mphase
= pdata
->mphase
;
478 pchip
->pdata
= devm_kzalloc(pchip
->dev
,
479 sizeof(struct lp8755_platform_data
),
483 ret
= lp8755_init_data(pchip
);
485 dev_err(&client
->dev
, "fail to initialize chip\n");
490 ret
= lp8755_regulator_init(pchip
);
492 dev_err(&client
->dev
, "fail to initialize regulators\n");
496 pchip
->irq
= client
->irq
;
497 ret
= lp8755_int_config(pchip
);
499 dev_err(&client
->dev
, "fail to irq config\n");
506 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++)
507 regulator_unregister(pchip
->rdev
[icnt
]);
511 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
512 lp8755_write(pchip
, icnt
, 0x00);
517 static int lp8755_remove(struct i2c_client
*client
)
520 struct lp8755_chip
*pchip
= i2c_get_clientdata(client
);
522 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++)
523 regulator_unregister(pchip
->rdev
[icnt
]);
525 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
526 lp8755_write(pchip
, icnt
, 0x00);
529 free_irq(pchip
->irq
, pchip
);
534 static const struct i2c_device_id lp8755_id
[] = {
539 MODULE_DEVICE_TABLE(i2c
, lp8755_id
);
541 static struct i2c_driver lp8755_i2c_driver
= {
545 .probe
= lp8755_probe
,
546 .remove
= lp8755_remove
,
547 .id_table
= lp8755_id
,
550 static int __init
lp8755_init(void)
552 return i2c_add_driver(&lp8755_i2c_driver
);
555 subsys_initcall(lp8755_init
);
557 static void __exit
lp8755_exit(void)
559 i2c_del_driver(&lp8755_i2c_driver
);
562 module_exit(lp8755_exit
);
564 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
565 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
566 MODULE_LICENSE("GPL v2");