1 // SPDX-License-Identifier: GPL-2.0-only
3 * LP8755 High Performance Power Management Unit : System Interface Driver
5 * Copyright 2012 Texas Instruments
7 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/i2c.h>
13 #include <linux/err.h>
14 #include <linux/irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/gpio.h>
17 #include <linux/regmap.h>
18 #include <linux/uaccess.h>
19 #include <linux/regulator/driver.h>
20 #include <linux/regulator/machine.h>
21 #include <linux/platform_data/lp8755.h>
23 #define LP8755_REG_BUCK0 0x00
24 #define LP8755_REG_BUCK1 0x03
25 #define LP8755_REG_BUCK2 0x04
26 #define LP8755_REG_BUCK3 0x01
27 #define LP8755_REG_BUCK4 0x05
28 #define LP8755_REG_BUCK5 0x02
29 #define LP8755_REG_MAX 0xFF
31 #define LP8755_BUCK_EN_M BIT(7)
32 #define LP8755_BUCK_LINEAR_OUT_MAX 0x76
33 #define LP8755_BUCK_VOUT_M 0x7F
35 struct lp8755_mphase
{
37 int buck_num
[LP8755_BUCK_MAX
];
42 struct regmap
*regmap
;
43 struct lp8755_platform_data
*pdata
;
49 struct regulator_dev
*rdev
[LP8755_BUCK_MAX
];
53 *lp8755_read : read a single register value from lp8755.
54 *@pchip : device to read from
55 *@reg : register to read from
56 *@val : pointer to store read value
58 static int lp8755_read(struct lp8755_chip
*pchip
, unsigned int reg
,
61 return regmap_read(pchip
->regmap
, reg
, val
);
65 *lp8755_write : write a single register value to lp8755.
66 *@pchip : device to write to
67 *@reg : register to write to
68 *@val : value to be written
70 static int lp8755_write(struct lp8755_chip
*pchip
, unsigned int reg
,
73 return regmap_write(pchip
->regmap
, reg
, val
);
77 *lp8755_update_bits : set the values of bit fields in lp8755 register.
78 *@pchip : device to read from
79 *@reg : register to update
80 *@mask : bitmask to be changed
81 *@val : value for bitmask
83 static int lp8755_update_bits(struct lp8755_chip
*pchip
, unsigned int reg
,
84 unsigned int mask
, unsigned int val
)
86 return regmap_update_bits(pchip
->regmap
, reg
, mask
, val
);
89 static int lp8755_buck_enable_time(struct regulator_dev
*rdev
)
93 enum lp8755_bucks id
= rdev_get_id(rdev
);
94 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
96 ret
= lp8755_read(pchip
, 0x12 + id
, ®val
);
98 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
101 return (regval
& 0xff) * 100;
104 static int lp8755_buck_set_mode(struct regulator_dev
*rdev
, unsigned int mode
)
107 unsigned int regbval
= 0x0;
108 enum lp8755_bucks id
= rdev_get_id(rdev
);
109 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
112 case REGULATOR_MODE_FAST
:
113 /* forced pwm mode */
114 regbval
= (0x01 << id
);
116 case REGULATOR_MODE_NORMAL
:
117 /* enable automatic pwm/pfm mode */
118 ret
= lp8755_update_bits(pchip
, 0x08 + id
, 0x20, 0x00);
122 case REGULATOR_MODE_IDLE
:
123 /* enable automatic pwm/pfm/lppfm mode */
124 ret
= lp8755_update_bits(pchip
, 0x08 + id
, 0x20, 0x20);
128 ret
= lp8755_update_bits(pchip
, 0x10, 0x01, 0x01);
133 dev_err(pchip
->dev
, "Not supported buck mode %s\n", __func__
);
134 /* forced pwm mode */
135 regbval
= (0x01 << id
);
138 ret
= lp8755_update_bits(pchip
, 0x06, 0x01 << id
, regbval
);
143 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
147 static unsigned int lp8755_buck_get_mode(struct regulator_dev
*rdev
)
151 enum lp8755_bucks id
= rdev_get_id(rdev
);
152 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
154 ret
= lp8755_read(pchip
, 0x06, ®val
);
158 /* mode fast means forced pwm mode */
159 if (regval
& (0x01 << id
))
160 return REGULATOR_MODE_FAST
;
162 ret
= lp8755_read(pchip
, 0x08 + id
, ®val
);
166 /* mode idle means automatic pwm/pfm/lppfm mode */
168 return REGULATOR_MODE_IDLE
;
170 /* mode normal means automatic pwm/pfm mode */
171 return REGULATOR_MODE_NORMAL
;
174 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
178 static int lp8755_buck_set_ramp(struct regulator_dev
*rdev
, int ramp
)
181 unsigned int regval
= 0x00;
182 enum lp8755_bucks id
= rdev_get_id(rdev
);
183 struct lp8755_chip
*pchip
= rdev_get_drvdata(rdev
);
208 case 15001 ... 30000:
213 "Not supported ramp value %d %s\n", ramp
, __func__
);
217 ret
= lp8755_update_bits(pchip
, 0x07 + id
, 0x07, regval
);
222 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
226 static const struct regulator_ops lp8755_buck_ops
= {
227 .map_voltage
= regulator_map_voltage_linear
,
228 .list_voltage
= regulator_list_voltage_linear
,
229 .set_voltage_sel
= regulator_set_voltage_sel_regmap
,
230 .get_voltage_sel
= regulator_get_voltage_sel_regmap
,
231 .enable
= regulator_enable_regmap
,
232 .disable
= regulator_disable_regmap
,
233 .is_enabled
= regulator_is_enabled_regmap
,
234 .enable_time
= lp8755_buck_enable_time
,
235 .set_mode
= lp8755_buck_set_mode
,
236 .get_mode
= lp8755_buck_get_mode
,
237 .set_ramp_delay
= lp8755_buck_set_ramp
,
240 #define lp8755_rail(_id) "lp8755_buck"#_id
241 #define lp8755_buck_init(_id)\
244 .name = lp8755_rail(_id),\
245 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
251 static struct regulator_init_data lp8755_reg_default
[LP8755_BUCK_MAX
] = {
252 [LP8755_BUCK0
] = lp8755_buck_init(0),
253 [LP8755_BUCK1
] = lp8755_buck_init(1),
254 [LP8755_BUCK2
] = lp8755_buck_init(2),
255 [LP8755_BUCK3
] = lp8755_buck_init(3),
256 [LP8755_BUCK4
] = lp8755_buck_init(4),
257 [LP8755_BUCK5
] = lp8755_buck_init(5),
260 static const struct lp8755_mphase mphase_buck
[MPHASE_CONF_MAX
] = {
261 { 3, { LP8755_BUCK0
, LP8755_BUCK3
, LP8755_BUCK5
} },
262 { 6, { LP8755_BUCK0
, LP8755_BUCK1
, LP8755_BUCK2
, LP8755_BUCK3
,
263 LP8755_BUCK4
, LP8755_BUCK5
} },
264 { 5, { LP8755_BUCK0
, LP8755_BUCK2
, LP8755_BUCK3
, LP8755_BUCK4
,
266 { 4, { LP8755_BUCK0
, LP8755_BUCK3
, LP8755_BUCK4
, LP8755_BUCK5
} },
267 { 3, { LP8755_BUCK0
, LP8755_BUCK4
, LP8755_BUCK5
} },
268 { 2, { LP8755_BUCK0
, LP8755_BUCK5
} },
269 { 1, { LP8755_BUCK0
} },
270 { 2, { LP8755_BUCK0
, LP8755_BUCK3
} },
271 { 4, { LP8755_BUCK0
, LP8755_BUCK2
, LP8755_BUCK3
, LP8755_BUCK5
} },
274 static int lp8755_init_data(struct lp8755_chip
*pchip
)
277 int ret
, icnt
, buck_num
;
278 struct lp8755_platform_data
*pdata
= pchip
->pdata
;
280 /* read back muti-phase configuration */
281 ret
= lp8755_read(pchip
, 0x3D, ®val
);
284 pchip
->mphase
= regval
& 0x0F;
286 /* set default data based on multi-phase config */
287 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++) {
288 buck_num
= mphase_buck
[pchip
->mphase
].buck_num
[icnt
];
289 pdata
->buck_data
[buck_num
] = &lp8755_reg_default
[buck_num
];
294 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
298 #define lp8755_buck_desc(_id)\
300 .name = lp8755_rail(_id),\
301 .id = LP8755_BUCK##_id,\
302 .ops = &lp8755_buck_ops,\
303 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
306 .type = REGULATOR_VOLTAGE,\
307 .owner = THIS_MODULE,\
308 .enable_reg = LP8755_REG_BUCK##_id,\
309 .enable_mask = LP8755_BUCK_EN_M,\
310 .vsel_reg = LP8755_REG_BUCK##_id,\
311 .vsel_mask = LP8755_BUCK_VOUT_M,\
314 static const struct regulator_desc lp8755_regulators
[] = {
323 static int lp8755_regulator_init(struct lp8755_chip
*pchip
)
325 int ret
, icnt
, buck_num
;
326 struct lp8755_platform_data
*pdata
= pchip
->pdata
;
327 struct regulator_config rconfig
= { };
329 rconfig
.regmap
= pchip
->regmap
;
330 rconfig
.dev
= pchip
->dev
;
331 rconfig
.driver_data
= pchip
;
333 for (icnt
= 0; icnt
< mphase_buck
[pchip
->mphase
].nreg
; icnt
++) {
334 buck_num
= mphase_buck
[pchip
->mphase
].buck_num
[icnt
];
335 rconfig
.init_data
= pdata
->buck_data
[buck_num
];
336 rconfig
.of_node
= pchip
->dev
->of_node
;
337 pchip
->rdev
[buck_num
] =
338 devm_regulator_register(pchip
->dev
,
339 &lp8755_regulators
[buck_num
], &rconfig
);
340 if (IS_ERR(pchip
->rdev
[buck_num
])) {
341 ret
= PTR_ERR(pchip
->rdev
[buck_num
]);
342 pchip
->rdev
[buck_num
] = NULL
;
343 dev_err(pchip
->dev
, "regulator init failed: buck %d\n",
352 static irqreturn_t
lp8755_irq_handler(int irq
, void *data
)
355 unsigned int flag0
, flag1
;
356 struct lp8755_chip
*pchip
= data
;
358 /* read flag0 register */
359 ret
= lp8755_read(pchip
, 0x0D, &flag0
);
362 /* clear flag register to pull up int. pin */
363 ret
= lp8755_write(pchip
, 0x0D, 0x00);
367 /* sent power fault detection event to specific regulator */
368 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
369 if ((flag0
& (0x4 << icnt
))
370 && (pchip
->irqmask
& (0x04 << icnt
))
371 && (pchip
->rdev
[icnt
] != NULL
)) {
372 regulator_lock(pchip
->rdev
[icnt
]);
373 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
374 LP8755_EVENT_PWR_FAULT
,
376 regulator_unlock(pchip
->rdev
[icnt
]);
379 /* read flag1 register */
380 ret
= lp8755_read(pchip
, 0x0E, &flag1
);
383 /* clear flag register to pull up int. pin */
384 ret
= lp8755_write(pchip
, 0x0E, 0x00);
388 /* send OCP event to all regulator devices */
389 if ((flag1
& 0x01) && (pchip
->irqmask
& 0x01))
390 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
391 if (pchip
->rdev
[icnt
] != NULL
) {
392 regulator_lock(pchip
->rdev
[icnt
]);
393 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
396 regulator_unlock(pchip
->rdev
[icnt
]);
399 /* send OVP event to all regulator devices */
400 if ((flag1
& 0x02) && (pchip
->irqmask
& 0x02))
401 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
402 if (pchip
->rdev
[icnt
] != NULL
) {
403 regulator_lock(pchip
->rdev
[icnt
]);
404 regulator_notifier_call_chain(pchip
->rdev
[icnt
],
407 regulator_unlock(pchip
->rdev
[icnt
]);
412 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
416 static int lp8755_int_config(struct lp8755_chip
*pchip
)
421 if (pchip
->irq
== 0) {
422 dev_warn(pchip
->dev
, "not use interrupt : %s\n", __func__
);
426 ret
= lp8755_read(pchip
, 0x0F, ®val
);
428 dev_err(pchip
->dev
, "i2c access error %s\n", __func__
);
432 pchip
->irqmask
= regval
;
433 return devm_request_threaded_irq(pchip
->dev
, pchip
->irq
, NULL
,
435 IRQF_TRIGGER_FALLING
| IRQF_ONESHOT
,
436 "lp8755-irq", pchip
);
439 static const struct regmap_config lp8755_regmap
= {
442 .max_register
= LP8755_REG_MAX
,
445 static int lp8755_probe(struct i2c_client
*client
,
446 const struct i2c_device_id
*id
)
449 struct lp8755_chip
*pchip
;
450 struct lp8755_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
452 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
453 dev_err(&client
->dev
, "i2c functionality check fail.\n");
457 pchip
= devm_kzalloc(&client
->dev
,
458 sizeof(struct lp8755_chip
), GFP_KERNEL
);
462 pchip
->dev
= &client
->dev
;
463 pchip
->regmap
= devm_regmap_init_i2c(client
, &lp8755_regmap
);
464 if (IS_ERR(pchip
->regmap
)) {
465 ret
= PTR_ERR(pchip
->regmap
);
466 dev_err(&client
->dev
, "fail to allocate regmap %d\n", ret
);
469 i2c_set_clientdata(client
, pchip
);
472 pchip
->pdata
= pdata
;
473 pchip
->mphase
= pdata
->mphase
;
475 pchip
->pdata
= devm_kzalloc(pchip
->dev
,
476 sizeof(struct lp8755_platform_data
),
480 ret
= lp8755_init_data(pchip
);
482 dev_err(&client
->dev
, "fail to initialize chip\n");
487 ret
= lp8755_regulator_init(pchip
);
489 dev_err(&client
->dev
, "fail to initialize regulators\n");
493 pchip
->irq
= client
->irq
;
494 ret
= lp8755_int_config(pchip
);
496 dev_err(&client
->dev
, "fail to irq config\n");
504 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
505 lp8755_write(pchip
, icnt
, 0x00);
510 static int lp8755_remove(struct i2c_client
*client
)
513 struct lp8755_chip
*pchip
= i2c_get_clientdata(client
);
515 for (icnt
= 0; icnt
< LP8755_BUCK_MAX
; icnt
++)
516 lp8755_write(pchip
, icnt
, 0x00);
521 static const struct i2c_device_id lp8755_id
[] = {
526 MODULE_DEVICE_TABLE(i2c
, lp8755_id
);
528 static struct i2c_driver lp8755_i2c_driver
= {
532 .probe
= lp8755_probe
,
533 .remove
= lp8755_remove
,
534 .id_table
= lp8755_id
,
537 static int __init
lp8755_init(void)
539 return i2c_add_driver(&lp8755_i2c_driver
);
542 subsys_initcall(lp8755_init
);
544 static void __exit
lp8755_exit(void)
546 i2c_del_driver(&lp8755_i2c_driver
);
549 module_exit(lp8755_exit
);
551 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
552 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
553 MODULE_LICENSE("GPL v2");