xref: /openbmc/linux/drivers/regulator/lp8755.c (revision cad877ef)
1 /*
2  * LP8755 High Performance Power Management Unit : System Interface Driver
3  * (based on rev. 0.26)
4  * Copyright 2012 Texas Instruments
5  *
6  * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.com>
7  *
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.
11  *
12  */
13 
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>
27 
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
35 
36 #define LP8755_BUCK_EN_M	BIT(7)
37 #define LP8755_BUCK_LINEAR_OUT_MAX	0x76
38 #define LP8755_BUCK_VOUT_M	0x7F
39 
40 enum bucks {
41 	BUCK0 = 0,
42 	BUCK1,
43 	BUCK2,
44 	BUCK3,
45 	BUCK4,
46 	BUCK5,
47 };
48 
49 struct lp8755_mphase {
50 	int nreg;
51 	int buck_num[LP8755_BUCK_MAX];
52 };
53 
54 struct lp8755_chip {
55 	struct device *dev;
56 	struct regmap *regmap;
57 	struct lp8755_platform_data *pdata;
58 
59 	int irq;
60 	unsigned int irqmask;
61 
62 	int mphase;
63 	struct regulator_dev *rdev[LP8755_BUCK_MAX];
64 };
65 
66 /**
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
71  */
72 static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
73 		       unsigned int *val)
74 {
75 	return regmap_read(pchip->regmap, reg, val);
76 }
77 
78 /**
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
83  */
84 static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
85 			unsigned int val)
86 {
87 	return regmap_write(pchip->regmap, reg, val);
88 }
89 
90 /**
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
96  */
97 static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
98 			      unsigned int mask, unsigned int val)
99 {
100 	return regmap_update_bits(pchip->regmap, reg, mask, val);
101 }
102 
103 static int lp8755_buck_enable_time(struct regulator_dev *rdev)
104 {
105 	int ret;
106 	unsigned int regval;
107 	enum lp8755_bucks id = rdev_get_id(rdev);
108 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
109 
110 	ret = lp8755_read(pchip, 0x12 + id, &regval);
111 	if (ret < 0) {
112 		dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
113 		return ret;
114 	}
115 	return (regval & 0xff) * 100;
116 }
117 
118 static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
119 {
120 	int ret;
121 	unsigned int regbval = 0x0;
122 	enum lp8755_bucks id = rdev_get_id(rdev);
123 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
124 
125 	switch (mode) {
126 	case REGULATOR_MODE_FAST:
127 		/* forced pwm mode */
128 		regbval = (0x01 << id);
129 		break;
130 	case REGULATOR_MODE_NORMAL:
131 		/* enable automatic pwm/pfm mode */
132 		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
133 		if (ret < 0)
134 			goto err_i2c;
135 		break;
136 	case REGULATOR_MODE_IDLE:
137 		/* enable automatic pwm/pfm/lppfm mode */
138 		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
139 		if (ret < 0)
140 			goto err_i2c;
141 
142 		ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
143 		if (ret < 0)
144 			goto err_i2c;
145 		break;
146 	default:
147 		dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
148 		/* forced pwm mode */
149 		regbval = (0x01 << id);
150 	}
151 
152 	ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
153 	if (ret < 0)
154 		goto err_i2c;
155 	return ret;
156 err_i2c:
157 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
158 	return ret;
159 }
160 
161 static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
162 {
163 	int ret;
164 	unsigned int regval;
165 	enum lp8755_bucks id = rdev_get_id(rdev);
166 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
167 
168 	ret = lp8755_read(pchip, 0x06, &regval);
169 	if (ret < 0)
170 		goto err_i2c;
171 
172 	/* mode fast means forced pwm mode */
173 	if (regval & (0x01 << id))
174 		return REGULATOR_MODE_FAST;
175 
176 	ret = lp8755_read(pchip, 0x08 + id, &regval);
177 	if (ret < 0)
178 		goto err_i2c;
179 
180 	/* mode idle means automatic pwm/pfm/lppfm mode */
181 	if (regval & 0x20)
182 		return REGULATOR_MODE_IDLE;
183 
184 	/* mode normal means automatic pwm/pfm mode */
185 	return REGULATOR_MODE_NORMAL;
186 
187 err_i2c:
188 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
189 	return 0;
190 }
191 
192 static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
193 {
194 	int ret;
195 	unsigned int regval = 0x00;
196 	enum lp8755_bucks id = rdev_get_id(rdev);
197 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
198 
199 	/* uV/us */
200 	switch (ramp) {
201 	case 0 ... 230:
202 		regval = 0x07;
203 		break;
204 	case 231 ... 470:
205 		regval = 0x06;
206 		break;
207 	case 471 ... 940:
208 		regval = 0x05;
209 		break;
210 	case 941 ... 1900:
211 		regval = 0x04;
212 		break;
213 	case 1901 ... 3800:
214 		regval = 0x03;
215 		break;
216 	case 3801 ... 7500:
217 		regval = 0x02;
218 		break;
219 	case 7501 ... 15000:
220 		regval = 0x01;
221 		break;
222 	case 15001 ... 30000:
223 		regval = 0x00;
224 		break;
225 	default:
226 		dev_err(pchip->dev,
227 			"Not supported ramp value %d %s\n", ramp, __func__);
228 		return -EINVAL;
229 	}
230 
231 	ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
232 	if (ret < 0)
233 		goto err_i2c;
234 	return ret;
235 err_i2c:
236 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
237 	return ret;
238 }
239 
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,
251 };
252 
253 #define lp8755_rail(_id) "lp8755_buck"#_id
254 #define lp8755_buck_init(_id)\
255 {\
256 	.constraints = {\
257 		.name = lp8755_rail(_id),\
258 		.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
259 		.min_uV = 500000,\
260 		.max_uV = 1675000,\
261 	},\
262 }
263 
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),
271 };
272 
273 static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
274 	{3, {BUCK0, BUCK3, BUCK5}
275 	 },
276 	{6, {BUCK0, BUCK1, BUCK2, BUCK3, BUCK4, BUCK5}
277 	 },
278 	{5, {BUCK0, BUCK2, BUCK3, BUCK4, BUCK5}
279 	 },
280 	{4, {BUCK0, BUCK3, BUCK4, BUCK5}
281 	 },
282 	{3, {BUCK0, BUCK4, BUCK5}
283 	 },
284 	{2, {BUCK0, BUCK5}
285 	 },
286 	{1, {BUCK0}
287 	 },
288 	{2, {BUCK0, BUCK3}
289 	 },
290 	{4, {BUCK0, BUCK2, BUCK3, BUCK5}
291 	 },
292 };
293 
294 static int lp8755_init_data(struct lp8755_chip *pchip)
295 {
296 	unsigned int regval;
297 	int ret, icnt, buck_num;
298 	struct lp8755_platform_data *pdata = pchip->pdata;
299 
300 	/* read back  muti-phase configuration */
301 	ret = lp8755_read(pchip, 0x3D, &regval);
302 	if (ret < 0)
303 		goto out_i2c_error;
304 	pchip->mphase = regval & 0x0F;
305 
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];
310 	}
311 	return ret;
312 
313 out_i2c_error:
314 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
315 	return ret;
316 }
317 
318 #define lp8755_buck_desc(_id)\
319 {\
320 	.name = lp8755_rail(_id),\
321 	.id   = LP8755_BUCK##_id,\
322 	.ops  = &lp8755_buck_ops,\
323 	.n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
324 	.uV_step = 10000,\
325 	.min_uV = 500000,\
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,\
332 }
333 
334 static struct regulator_desc lp8755_regulators[] = {
335 	lp8755_buck_desc(0),
336 	lp8755_buck_desc(1),
337 	lp8755_buck_desc(2),
338 	lp8755_buck_desc(3),
339 	lp8755_buck_desc(4),
340 	lp8755_buck_desc(5),
341 };
342 
343 static int lp8755_regulator_init(struct lp8755_chip *pchip)
344 {
345 	int ret, icnt, buck_num;
346 	struct lp8755_platform_data *pdata = pchip->pdata;
347 	struct regulator_config rconfig = { };
348 
349 	rconfig.regmap = pchip->regmap;
350 	rconfig.dev = pchip->dev;
351 	rconfig.driver_data = pchip;
352 
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",
363 				buck_num);
364 			goto err_buck;
365 		}
366 	}
367 
368 	return 0;
369 
370 err_buck:
371 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
372 		regulator_unregister(pchip->rdev[icnt]);
373 	return ret;
374 }
375 
376 static irqreturn_t lp8755_irq_handler(int irq, void *data)
377 {
378 	int ret, icnt;
379 	unsigned int flag0, flag1;
380 	struct lp8755_chip *pchip = data;
381 
382 	/* read flag0 register */
383 	ret = lp8755_read(pchip, 0x0D, &flag0);
384 	if (ret < 0)
385 		goto err_i2c;
386 	/* clear flag register to pull up int. pin */
387 	ret = lp8755_write(pchip, 0x0D, 0x00);
388 	if (ret < 0)
389 		goto err_i2c;
390 
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,
398 						      NULL);
399 
400 	/* read flag1 register */
401 	ret = lp8755_read(pchip, 0x0E, &flag1);
402 	if (ret < 0)
403 		goto err_i2c;
404 	/* clear flag register to pull up int. pin */
405 	ret = lp8755_write(pchip, 0x0E, 0x00);
406 	if (ret < 0)
407 		goto err_i2c;
408 
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],
414 							      LP8755_EVENT_OCP,
415 							      NULL);
416 
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],
422 							      LP8755_EVENT_OVP,
423 							      NULL);
424 	return IRQ_HANDLED;
425 
426 err_i2c:
427 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
428 	return IRQ_NONE;
429 }
430 
431 static int lp8755_int_config(struct lp8755_chip *pchip)
432 {
433 	int ret;
434 	unsigned int regval;
435 
436 	if (pchip->irq == 0) {
437 		dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
438 		return 0;
439 	}
440 
441 	ret = lp8755_read(pchip, 0x0F, &regval);
442 	if (ret < 0)
443 		goto err_i2c;
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);
448 	if (ret)
449 		return ret;
450 
451 	return ret;
452 
453 err_i2c:
454 	dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
455 	return ret;
456 }
457 
458 static const struct regmap_config lp8755_regmap = {
459 	.reg_bits = 8,
460 	.val_bits = 8,
461 	.max_register = LP8755_REG_MAX,
462 };
463 
464 static int lp8755_probe(struct i2c_client *client,
465 			const struct i2c_device_id *id)
466 {
467 	int ret, icnt;
468 	struct lp8755_chip *pchip;
469 	struct lp8755_platform_data *pdata = client->dev.platform_data;
470 
471 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
472 		dev_err(&client->dev, "i2c functionality check fail.\n");
473 		return -EOPNOTSUPP;
474 	}
475 
476 	pchip = devm_kzalloc(&client->dev,
477 			     sizeof(struct lp8755_chip), GFP_KERNEL);
478 	if (!pchip)
479 		return -ENOMEM;
480 
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);
486 		return ret;
487 	}
488 	i2c_set_clientdata(client, pchip);
489 
490 	if (pdata != NULL) {
491 		pchip->pdata = pdata;
492 		pchip->mphase = pdata->mphase;
493 	} else {
494 		pchip->pdata = devm_kzalloc(pchip->dev,
495 					    sizeof(struct lp8755_platform_data),
496 					    GFP_KERNEL);
497 		if (!pchip->pdata)
498 			return -ENOMEM;
499 		ret = lp8755_init_data(pchip);
500 		if (ret < 0)
501 			goto err_chip_init;
502 	}
503 
504 	ret = lp8755_regulator_init(pchip);
505 	if (ret < 0)
506 		goto err_regulator;
507 
508 	pchip->irq = client->irq;
509 	ret = lp8755_int_config(pchip);
510 	if (ret < 0)
511 		goto err_irq;
512 
513 	return ret;
514 
515 err_irq:
516 	dev_err(&client->dev, "fail to irq config\n");
517 
518 	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
519 		regulator_unregister(pchip->rdev[icnt]);
520 
521 err_regulator:
522 	dev_err(&client->dev, "fail to initialize regulators\n");
523 	/* output disable */
524 	for (icnt = 0; icnt < 0x06; icnt++)
525 		lp8755_write(pchip, icnt, 0x00);
526 
527 err_chip_init:
528 	dev_err(&client->dev, "fail to initialize chip\n");
529 	return ret;
530 }
531 
532 static int lp8755_remove(struct i2c_client *client)
533 {
534 	int icnt;
535 	struct lp8755_chip *pchip = i2c_get_clientdata(client);
536 
537 	for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
538 		regulator_unregister(pchip->rdev[icnt]);
539 
540 	for (icnt = 0; icnt < 0x06; icnt++)
541 		lp8755_write(pchip, icnt, 0x00);
542 
543 	if (pchip->irq != 0)
544 		free_irq(pchip->irq, pchip);
545 
546 	return 0;
547 }
548 
549 static const struct i2c_device_id lp8755_id[] = {
550 	{LP8755_NAME, 0},
551 	{}
552 };
553 
554 MODULE_DEVICE_TABLE(i2c, lp8755_id);
555 
556 static struct i2c_driver lp8755_i2c_driver = {
557 	.driver = {
558 		   .name = LP8755_NAME,
559 		   },
560 	.probe = lp8755_probe,
561 	.remove = lp8755_remove,
562 	.id_table = lp8755_id,
563 };
564 
565 static int __init lp8755_init(void)
566 {
567 	return i2c_add_driver(&lp8755_i2c_driver);
568 }
569 
570 subsys_initcall(lp8755_init);
571 
572 static void __exit lp8755_exit(void)
573 {
574 	i2c_del_driver(&lp8755_i2c_driver);
575 }
576 
577 module_exit(lp8755_exit);
578 
579 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
580 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
581 MODULE_LICENSE("GPL v2");
582