Merge tag 'bcachefs-2024-09-09' of git://evilpiepirate.org/bcachefs
[linux-block.git] / drivers / regulator / lp8755.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
b59320cc
DJ
2/*
3 * LP8755 High Performance Power Management Unit : System Interface Driver
4 * (based on rev. 0.26)
5 * Copyright 2012 Texas Instruments
6 *
7 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
b59320cc
DJ
8 */
9
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>
b59320cc 16#include <linux/regmap.h>
b59320cc
DJ
17#include <linux/uaccess.h>
18#include <linux/regulator/driver.h>
19#include <linux/regulator/machine.h>
20#include <linux/platform_data/lp8755.h>
21
22#define LP8755_REG_BUCK0 0x00
23#define LP8755_REG_BUCK1 0x03
24#define LP8755_REG_BUCK2 0x04
25#define LP8755_REG_BUCK3 0x01
26#define LP8755_REG_BUCK4 0x05
27#define LP8755_REG_BUCK5 0x02
28#define LP8755_REG_MAX 0xFF
29
30#define LP8755_BUCK_EN_M BIT(7)
31#define LP8755_BUCK_LINEAR_OUT_MAX 0x76
32#define LP8755_BUCK_VOUT_M 0x7F
33
b59320cc
DJ
34struct lp8755_mphase {
35 int nreg;
36 int buck_num[LP8755_BUCK_MAX];
37};
38
39struct lp8755_chip {
40 struct device *dev;
41 struct regmap *regmap;
42 struct lp8755_platform_data *pdata;
43
44 int irq;
45 unsigned int irqmask;
46
47 int mphase;
48 struct regulator_dev *rdev[LP8755_BUCK_MAX];
49};
50
b59320cc
DJ
51static int lp8755_buck_enable_time(struct regulator_dev *rdev)
52{
53 int ret;
54 unsigned int regval;
55 enum lp8755_bucks id = rdev_get_id(rdev);
b59320cc 56
4cf12735 57 ret = regmap_read(rdev->regmap, 0x12 + id, &regval);
b59320cc 58 if (ret < 0) {
4cf12735 59 dev_err(&rdev->dev, "i2c access error %s\n", __func__);
b59320cc
DJ
60 return ret;
61 }
62 return (regval & 0xff) * 100;
63}
64
65static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
66{
67 int ret;
68 unsigned int regbval = 0x0;
69 enum lp8755_bucks id = rdev_get_id(rdev);
70 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
71
72 switch (mode) {
73 case REGULATOR_MODE_FAST:
74 /* forced pwm mode */
75 regbval = (0x01 << id);
76 break;
77 case REGULATOR_MODE_NORMAL:
78 /* enable automatic pwm/pfm mode */
4cf12735 79 ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x00);
b59320cc
DJ
80 if (ret < 0)
81 goto err_i2c;
82 break;
83 case REGULATOR_MODE_IDLE:
84 /* enable automatic pwm/pfm/lppfm mode */
4cf12735 85 ret = regmap_update_bits(rdev->regmap, 0x08 + id, 0x20, 0x20);
b59320cc
DJ
86 if (ret < 0)
87 goto err_i2c;
88
4cf12735 89 ret = regmap_update_bits(rdev->regmap, 0x10, 0x01, 0x01);
b59320cc
DJ
90 if (ret < 0)
91 goto err_i2c;
92 break;
93 default:
94 dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
95 /* forced pwm mode */
96 regbval = (0x01 << id);
97 }
98
4cf12735 99 ret = regmap_update_bits(rdev->regmap, 0x06, 0x01 << id, regbval);
b59320cc
DJ
100 if (ret < 0)
101 goto err_i2c;
102 return ret;
103err_i2c:
4cf12735 104 dev_err(&rdev->dev, "i2c access error %s\n", __func__);
b59320cc
DJ
105 return ret;
106}
107
108static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
109{
110 int ret;
111 unsigned int regval;
112 enum lp8755_bucks id = rdev_get_id(rdev);
b59320cc 113
4cf12735 114 ret = regmap_read(rdev->regmap, 0x06, &regval);
b59320cc
DJ
115 if (ret < 0)
116 goto err_i2c;
117
118 /* mode fast means forced pwm mode */
119 if (regval & (0x01 << id))
120 return REGULATOR_MODE_FAST;
121
4cf12735 122 ret = regmap_read(rdev->regmap, 0x08 + id, &regval);
b59320cc
DJ
123 if (ret < 0)
124 goto err_i2c;
125
126 /* mode idle means automatic pwm/pfm/lppfm mode */
127 if (regval & 0x20)
128 return REGULATOR_MODE_IDLE;
129
130 /* mode normal means automatic pwm/pfm mode */
131 return REGULATOR_MODE_NORMAL;
132
133err_i2c:
4cf12735 134 dev_err(&rdev->dev, "i2c access error %s\n", __func__);
b59320cc
DJ
135 return 0;
136}
137
fbd168cd
AL
138static const unsigned int lp8755_buck_ramp_table[] = {
139 30000, 15000, 7500, 3800, 1900, 940, 470, 230
140};
b59320cc 141
d42797a4 142static const struct regulator_ops lp8755_buck_ops = {
56a942e9 143 .map_voltage = regulator_map_voltage_linear,
b59320cc
DJ
144 .list_voltage = regulator_list_voltage_linear,
145 .set_voltage_sel = regulator_set_voltage_sel_regmap,
146 .get_voltage_sel = regulator_get_voltage_sel_regmap,
147 .enable = regulator_enable_regmap,
148 .disable = regulator_disable_regmap,
149 .is_enabled = regulator_is_enabled_regmap,
150 .enable_time = lp8755_buck_enable_time,
151 .set_mode = lp8755_buck_set_mode,
152 .get_mode = lp8755_buck_get_mode,
fbd168cd 153 .set_ramp_delay = regulator_set_ramp_delay_regmap,
b59320cc
DJ
154};
155
156#define lp8755_rail(_id) "lp8755_buck"#_id
157#define lp8755_buck_init(_id)\
158{\
159 .constraints = {\
160 .name = lp8755_rail(_id),\
161 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
162 .min_uV = 500000,\
163 .max_uV = 1675000,\
164 },\
165}
166
167static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
510799ea
AL
168 [LP8755_BUCK0] = lp8755_buck_init(0),
169 [LP8755_BUCK1] = lp8755_buck_init(1),
170 [LP8755_BUCK2] = lp8755_buck_init(2),
171 [LP8755_BUCK3] = lp8755_buck_init(3),
172 [LP8755_BUCK4] = lp8755_buck_init(4),
173 [LP8755_BUCK5] = lp8755_buck_init(5),
b59320cc
DJ
174};
175
176static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
510799ea
AL
177 { 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
178 { 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
179 LP8755_BUCK4, LP8755_BUCK5 } },
180 { 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
181 LP8755_BUCK5} },
182 { 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
183 { 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
184 { 2, { LP8755_BUCK0, LP8755_BUCK5} },
185 { 1, { LP8755_BUCK0} },
186 { 2, { LP8755_BUCK0, LP8755_BUCK3} },
187 { 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
b59320cc
DJ
188};
189
190static int lp8755_init_data(struct lp8755_chip *pchip)
191{
192 unsigned int regval;
193 int ret, icnt, buck_num;
194 struct lp8755_platform_data *pdata = pchip->pdata;
195
196 /* read back muti-phase configuration */
4cf12735 197 ret = regmap_read(pchip->regmap, 0x3D, &regval);
b59320cc
DJ
198 if (ret < 0)
199 goto out_i2c_error;
cad877ef 200 pchip->mphase = regval & 0x0F;
b59320cc
DJ
201
202 /* set default data based on multi-phase config */
203 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
204 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
205 pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
206 }
207 return ret;
208
209out_i2c_error:
80aec6f5 210 dev_err(pchip->dev, "i2c access error %s\n", __func__);
b59320cc
DJ
211 return ret;
212}
213
214#define lp8755_buck_desc(_id)\
215{\
216 .name = lp8755_rail(_id),\
217 .id = LP8755_BUCK##_id,\
218 .ops = &lp8755_buck_ops,\
219 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
220 .uV_step = 10000,\
221 .min_uV = 500000,\
222 .type = REGULATOR_VOLTAGE,\
223 .owner = THIS_MODULE,\
224 .enable_reg = LP8755_REG_BUCK##_id,\
225 .enable_mask = LP8755_BUCK_EN_M,\
226 .vsel_reg = LP8755_REG_BUCK##_id,\
227 .vsel_mask = LP8755_BUCK_VOUT_M,\
fbd168cd
AL
228 .ramp_reg = (LP8755_BUCK##_id) + 0x7,\
229 .ramp_mask = 0x7,\
230 .ramp_delay_table = lp8755_buck_ramp_table,\
231 .n_ramp_values = ARRAY_SIZE(lp8755_buck_ramp_table),\
b59320cc
DJ
232}
233
367e90d1 234static const struct regulator_desc lp8755_regulators[] = {
b59320cc
DJ
235 lp8755_buck_desc(0),
236 lp8755_buck_desc(1),
237 lp8755_buck_desc(2),
238 lp8755_buck_desc(3),
239 lp8755_buck_desc(4),
240 lp8755_buck_desc(5),
241};
242
243static int lp8755_regulator_init(struct lp8755_chip *pchip)
244{
245 int ret, icnt, buck_num;
246 struct lp8755_platform_data *pdata = pchip->pdata;
247 struct regulator_config rconfig = { };
248
249 rconfig.regmap = pchip->regmap;
250 rconfig.dev = pchip->dev;
251 rconfig.driver_data = pchip;
252
253 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
254 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
255 rconfig.init_data = pdata->buck_data[buck_num];
256 rconfig.of_node = pchip->dev->of_node;
257 pchip->rdev[buck_num] =
57135250
HS
258 devm_regulator_register(pchip->dev,
259 &lp8755_regulators[buck_num], &rconfig);
b59320cc
DJ
260 if (IS_ERR(pchip->rdev[buck_num])) {
261 ret = PTR_ERR(pchip->rdev[buck_num]);
a1a41ab4
AL
262 pchip->rdev[buck_num] = NULL;
263 dev_err(pchip->dev, "regulator init failed: buck %d\n",
264 buck_num);
57135250 265 return ret;
b59320cc
DJ
266 }
267 }
268
269 return 0;
b59320cc
DJ
270}
271
272static irqreturn_t lp8755_irq_handler(int irq, void *data)
273{
274 int ret, icnt;
275 unsigned int flag0, flag1;
276 struct lp8755_chip *pchip = data;
277
278 /* read flag0 register */
4cf12735 279 ret = regmap_read(pchip->regmap, 0x0D, &flag0);
b59320cc
DJ
280 if (ret < 0)
281 goto err_i2c;
282 /* clear flag register to pull up int. pin */
4cf12735 283 ret = regmap_write(pchip->regmap, 0x0D, 0x00);
b59320cc
DJ
284 if (ret < 0)
285 goto err_i2c;
286
287 /* sent power fault detection event to specific regulator */
1200c60b 288 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
b59320cc
DJ
289 if ((flag0 & (0x4 << icnt))
290 && (pchip->irqmask & (0x04 << icnt))
89b2758c 291 && (pchip->rdev[icnt] != NULL)) {
b59320cc
DJ
292 regulator_notifier_call_chain(pchip->rdev[icnt],
293 LP8755_EVENT_PWR_FAULT,
294 NULL);
89b2758c 295 }
b59320cc
DJ
296
297 /* read flag1 register */
4cf12735 298 ret = regmap_read(pchip->regmap, 0x0E, &flag1);
b59320cc
DJ
299 if (ret < 0)
300 goto err_i2c;
301 /* clear flag register to pull up int. pin */
4cf12735 302 ret = regmap_write(pchip->regmap, 0x0E, 0x00);
b59320cc
DJ
303 if (ret < 0)
304 goto err_i2c;
305
48f1b4ef 306 /* send OCP event to all regulator devices */
b59320cc
DJ
307 if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
308 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
89b2758c 309 if (pchip->rdev[icnt] != NULL) {
b59320cc
DJ
310 regulator_notifier_call_chain(pchip->rdev[icnt],
311 LP8755_EVENT_OCP,
312 NULL);
89b2758c 313 }
b59320cc 314
48f1b4ef 315 /* send OVP event to all regulator devices */
b59320cc
DJ
316 if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
317 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
89b2758c 318 if (pchip->rdev[icnt] != NULL) {
b59320cc
DJ
319 regulator_notifier_call_chain(pchip->rdev[icnt],
320 LP8755_EVENT_OVP,
321 NULL);
89b2758c 322 }
b59320cc
DJ
323 return IRQ_HANDLED;
324
325err_i2c:
80aec6f5 326 dev_err(pchip->dev, "i2c access error %s\n", __func__);
b59320cc
DJ
327 return IRQ_NONE;
328}
329
330static int lp8755_int_config(struct lp8755_chip *pchip)
331{
332 int ret;
333 unsigned int regval;
334
335 if (pchip->irq == 0) {
336 dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
337 return 0;
338 }
339
4cf12735 340 ret = regmap_read(pchip->regmap, 0x0F, &regval);
840499aa 341 if (ret < 0) {
80aec6f5 342 dev_err(pchip->dev, "i2c access error %s\n", __func__);
b59320cc 343 return ret;
840499aa 344 }
b59320cc 345
840499aa
AL
346 pchip->irqmask = regval;
347 return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL,
348 lp8755_irq_handler,
349 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
350 "lp8755-irq", pchip);
b59320cc
DJ
351}
352
353static const struct regmap_config lp8755_regmap = {
354 .reg_bits = 8,
355 .val_bits = 8,
356 .max_register = LP8755_REG_MAX,
357};
358
cb28f74b 359static int lp8755_probe(struct i2c_client *client)
b59320cc
DJ
360{
361 int ret, icnt;
362 struct lp8755_chip *pchip;
dff91d0b 363 struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev);
b59320cc
DJ
364
365 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
366 dev_err(&client->dev, "i2c functionality check fail.\n");
367 return -EOPNOTSUPP;
368 }
369
370 pchip = devm_kzalloc(&client->dev,
371 sizeof(struct lp8755_chip), GFP_KERNEL);
372 if (!pchip)
373 return -ENOMEM;
374
375 pchip->dev = &client->dev;
376 pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
377 if (IS_ERR(pchip->regmap)) {
378 ret = PTR_ERR(pchip->regmap);
379 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
380 return ret;
381 }
382 i2c_set_clientdata(client, pchip);
383
384 if (pdata != NULL) {
385 pchip->pdata = pdata;
386 pchip->mphase = pdata->mphase;
387 } else {
388 pchip->pdata = devm_kzalloc(pchip->dev,
389 sizeof(struct lp8755_platform_data),
390 GFP_KERNEL);
391 if (!pchip->pdata)
392 return -ENOMEM;
393 ret = lp8755_init_data(pchip);
240a5291
AL
394 if (ret < 0) {
395 dev_err(&client->dev, "fail to initialize chip\n");
396 return ret;
397 }
b59320cc
DJ
398 }
399
400 ret = lp8755_regulator_init(pchip);
240a5291
AL
401 if (ret < 0) {
402 dev_err(&client->dev, "fail to initialize regulators\n");
57135250 403 goto err;
240a5291 404 }
b59320cc
DJ
405
406 pchip->irq = client->irq;
407 ret = lp8755_int_config(pchip);
240a5291
AL
408 if (ret < 0) {
409 dev_err(&client->dev, "fail to irq config\n");
57135250 410 goto err;
240a5291 411 }
b59320cc
DJ
412
413 return ret;
414
57135250 415err:
b59320cc 416 /* output disable */
1200c60b 417 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
4cf12735 418 regmap_write(pchip->regmap, icnt, 0x00);
b59320cc 419
b59320cc
DJ
420 return ret;
421}
422
ed5c2f5f 423static void lp8755_remove(struct i2c_client *client)
b59320cc
DJ
424{
425 int icnt;
426 struct lp8755_chip *pchip = i2c_get_clientdata(client);
427
1200c60b 428 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
4cf12735 429 regmap_write(pchip->regmap, icnt, 0x00);
b59320cc
DJ
430}
431
432static const struct i2c_device_id lp8755_id[] = {
aea07a98 433 { LP8755_NAME },
b59320cc
DJ
434 {}
435};
436
437MODULE_DEVICE_TABLE(i2c, lp8755_id);
438
439static struct i2c_driver lp8755_i2c_driver = {
440 .driver = {
441 .name = LP8755_NAME,
259b93b2 442 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
b59320cc 443 },
964e1865 444 .probe = lp8755_probe,
a1a41ab4 445 .remove = lp8755_remove,
b59320cc
DJ
446 .id_table = lp8755_id,
447};
448
449static int __init lp8755_init(void)
450{
451 return i2c_add_driver(&lp8755_i2c_driver);
452}
453
454subsys_initcall(lp8755_init);
455
456static void __exit lp8755_exit(void)
457{
458 i2c_del_driver(&lp8755_i2c_driver);
459}
460
461module_exit(lp8755_exit);
462
463MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
464MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
465MODULE_LICENSE("GPL v2");