xref: /linux/drivers/regulator/lp8755.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
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/uaccess.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
25 #include <linux/platform_data/lp8755.h>
26 
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
34 
35 #define LP8755_BUCK_EN_M	BIT(7)
36 #define LP8755_BUCK_LINEAR_OUT_MAX	0x76
37 #define LP8755_BUCK_VOUT_M	0x7F
38 
39 struct lp8755_mphase {
40 	int nreg;
41 	int buck_num[LP8755_BUCK_MAX];
42 };
43 
44 struct lp8755_chip {
45 	struct device *dev;
46 	struct regmap *regmap;
47 	struct lp8755_platform_data *pdata;
48 
49 	int irq;
50 	unsigned int irqmask;
51 
52 	int mphase;
53 	struct regulator_dev *rdev[LP8755_BUCK_MAX];
54 };
55 
56 /**
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
61  */
62 static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
63 		       unsigned int *val)
64 {
65 	return regmap_read(pchip->regmap, reg, val);
66 }
67 
68 /**
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
73  */
74 static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
75 			unsigned int val)
76 {
77 	return regmap_write(pchip->regmap, reg, val);
78 }
79 
80 /**
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
86  */
87 static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
88 			      unsigned int mask, unsigned int val)
89 {
90 	return regmap_update_bits(pchip->regmap, reg, mask, val);
91 }
92 
93 static int lp8755_buck_enable_time(struct regulator_dev *rdev)
94 {
95 	int ret;
96 	unsigned int regval;
97 	enum lp8755_bucks id = rdev_get_id(rdev);
98 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
99 
100 	ret = lp8755_read(pchip, 0x12 + id, &regval);
101 	if (ret < 0) {
102 		dev_err(pchip->dev, "i2c access error %s\n", __func__);
103 		return ret;
104 	}
105 	return (regval & 0xff) * 100;
106 }
107 
108 static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
109 {
110 	int ret;
111 	unsigned int regbval = 0x0;
112 	enum lp8755_bucks id = rdev_get_id(rdev);
113 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
114 
115 	switch (mode) {
116 	case REGULATOR_MODE_FAST:
117 		/* forced pwm mode */
118 		regbval = (0x01 << id);
119 		break;
120 	case REGULATOR_MODE_NORMAL:
121 		/* enable automatic pwm/pfm mode */
122 		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
123 		if (ret < 0)
124 			goto err_i2c;
125 		break;
126 	case REGULATOR_MODE_IDLE:
127 		/* enable automatic pwm/pfm/lppfm mode */
128 		ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
129 		if (ret < 0)
130 			goto err_i2c;
131 
132 		ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
133 		if (ret < 0)
134 			goto err_i2c;
135 		break;
136 	default:
137 		dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
138 		/* forced pwm mode */
139 		regbval = (0x01 << id);
140 	}
141 
142 	ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
143 	if (ret < 0)
144 		goto err_i2c;
145 	return ret;
146 err_i2c:
147 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
148 	return ret;
149 }
150 
151 static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
152 {
153 	int ret;
154 	unsigned int regval;
155 	enum lp8755_bucks id = rdev_get_id(rdev);
156 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
157 
158 	ret = lp8755_read(pchip, 0x06, &regval);
159 	if (ret < 0)
160 		goto err_i2c;
161 
162 	/* mode fast means forced pwm mode */
163 	if (regval & (0x01 << id))
164 		return REGULATOR_MODE_FAST;
165 
166 	ret = lp8755_read(pchip, 0x08 + id, &regval);
167 	if (ret < 0)
168 		goto err_i2c;
169 
170 	/* mode idle means automatic pwm/pfm/lppfm mode */
171 	if (regval & 0x20)
172 		return REGULATOR_MODE_IDLE;
173 
174 	/* mode normal means automatic pwm/pfm mode */
175 	return REGULATOR_MODE_NORMAL;
176 
177 err_i2c:
178 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
179 	return 0;
180 }
181 
182 static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
183 {
184 	int ret;
185 	unsigned int regval = 0x00;
186 	enum lp8755_bucks id = rdev_get_id(rdev);
187 	struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
188 
189 	/* uV/us */
190 	switch (ramp) {
191 	case 0 ... 230:
192 		regval = 0x07;
193 		break;
194 	case 231 ... 470:
195 		regval = 0x06;
196 		break;
197 	case 471 ... 940:
198 		regval = 0x05;
199 		break;
200 	case 941 ... 1900:
201 		regval = 0x04;
202 		break;
203 	case 1901 ... 3800:
204 		regval = 0x03;
205 		break;
206 	case 3801 ... 7500:
207 		regval = 0x02;
208 		break;
209 	case 7501 ... 15000:
210 		regval = 0x01;
211 		break;
212 	case 15001 ... 30000:
213 		regval = 0x00;
214 		break;
215 	default:
216 		dev_err(pchip->dev,
217 			"Not supported ramp value %d %s\n", ramp, __func__);
218 		return -EINVAL;
219 	}
220 
221 	ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
222 	if (ret < 0)
223 		goto err_i2c;
224 	return ret;
225 err_i2c:
226 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
227 	return ret;
228 }
229 
230 static const struct regulator_ops lp8755_buck_ops = {
231 	.map_voltage = regulator_map_voltage_linear,
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,
242 };
243 
244 #define lp8755_rail(_id) "lp8755_buck"#_id
245 #define lp8755_buck_init(_id)\
246 {\
247 	.constraints = {\
248 		.name = lp8755_rail(_id),\
249 		.valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
250 		.min_uV = 500000,\
251 		.max_uV = 1675000,\
252 	},\
253 }
254 
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),
262 };
263 
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,
269 	       LP8755_BUCK5} },
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} },
276 };
277 
278 static int lp8755_init_data(struct lp8755_chip *pchip)
279 {
280 	unsigned int regval;
281 	int ret, icnt, buck_num;
282 	struct lp8755_platform_data *pdata = pchip->pdata;
283 
284 	/* read back  muti-phase configuration */
285 	ret = lp8755_read(pchip, 0x3D, &regval);
286 	if (ret < 0)
287 		goto out_i2c_error;
288 	pchip->mphase = regval & 0x0F;
289 
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];
294 	}
295 	return ret;
296 
297 out_i2c_error:
298 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
299 	return ret;
300 }
301 
302 #define lp8755_buck_desc(_id)\
303 {\
304 	.name = lp8755_rail(_id),\
305 	.id   = LP8755_BUCK##_id,\
306 	.ops  = &lp8755_buck_ops,\
307 	.n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
308 	.uV_step = 10000,\
309 	.min_uV = 500000,\
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,\
316 }
317 
318 static const struct regulator_desc lp8755_regulators[] = {
319 	lp8755_buck_desc(0),
320 	lp8755_buck_desc(1),
321 	lp8755_buck_desc(2),
322 	lp8755_buck_desc(3),
323 	lp8755_buck_desc(4),
324 	lp8755_buck_desc(5),
325 };
326 
327 static int lp8755_regulator_init(struct lp8755_chip *pchip)
328 {
329 	int ret, icnt, buck_num;
330 	struct lp8755_platform_data *pdata = pchip->pdata;
331 	struct regulator_config rconfig = { };
332 
333 	rconfig.regmap = pchip->regmap;
334 	rconfig.dev = pchip->dev;
335 	rconfig.driver_data = pchip;
336 
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 		    devm_regulator_register(pchip->dev,
343 				    &lp8755_regulators[buck_num], &rconfig);
344 		if (IS_ERR(pchip->rdev[buck_num])) {
345 			ret = PTR_ERR(pchip->rdev[buck_num]);
346 			pchip->rdev[buck_num] = NULL;
347 			dev_err(pchip->dev, "regulator init failed: buck %d\n",
348 				buck_num);
349 			return ret;
350 		}
351 	}
352 
353 	return 0;
354 }
355 
356 static irqreturn_t lp8755_irq_handler(int irq, void *data)
357 {
358 	int ret, icnt;
359 	unsigned int flag0, flag1;
360 	struct lp8755_chip *pchip = data;
361 
362 	/* read flag0 register */
363 	ret = lp8755_read(pchip, 0x0D, &flag0);
364 	if (ret < 0)
365 		goto err_i2c;
366 	/* clear flag register to pull up int. pin */
367 	ret = lp8755_write(pchip, 0x0D, 0x00);
368 	if (ret < 0)
369 		goto err_i2c;
370 
371 	/* sent power fault detection event to specific regulator */
372 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
373 		if ((flag0 & (0x4 << icnt))
374 		    && (pchip->irqmask & (0x04 << icnt))
375 		    && (pchip->rdev[icnt] != NULL)) {
376 			regulator_lock(pchip->rdev[icnt]);
377 			regulator_notifier_call_chain(pchip->rdev[icnt],
378 						      LP8755_EVENT_PWR_FAULT,
379 						      NULL);
380 			regulator_unlock(pchip->rdev[icnt]);
381 		}
382 
383 	/* read flag1 register */
384 	ret = lp8755_read(pchip, 0x0E, &flag1);
385 	if (ret < 0)
386 		goto err_i2c;
387 	/* clear flag register to pull up int. pin */
388 	ret = lp8755_write(pchip, 0x0E, 0x00);
389 	if (ret < 0)
390 		goto err_i2c;
391 
392 	/* send OCP event to all regulator 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_lock(pchip->rdev[icnt]);
397 				regulator_notifier_call_chain(pchip->rdev[icnt],
398 							      LP8755_EVENT_OCP,
399 							      NULL);
400 				regulator_unlock(pchip->rdev[icnt]);
401 			}
402 
403 	/* send OVP event to all regulator devices */
404 	if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
405 		for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
406 			if (pchip->rdev[icnt] != NULL) {
407 				regulator_lock(pchip->rdev[icnt]);
408 				regulator_notifier_call_chain(pchip->rdev[icnt],
409 							      LP8755_EVENT_OVP,
410 							      NULL);
411 				regulator_unlock(pchip->rdev[icnt]);
412 			}
413 	return IRQ_HANDLED;
414 
415 err_i2c:
416 	dev_err(pchip->dev, "i2c access error %s\n", __func__);
417 	return IRQ_NONE;
418 }
419 
420 static int lp8755_int_config(struct lp8755_chip *pchip)
421 {
422 	int ret;
423 	unsigned int regval;
424 
425 	if (pchip->irq == 0) {
426 		dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
427 		return 0;
428 	}
429 
430 	ret = lp8755_read(pchip, 0x0F, &regval);
431 	if (ret < 0) {
432 		dev_err(pchip->dev, "i2c access error %s\n", __func__);
433 		return ret;
434 	}
435 
436 	pchip->irqmask = regval;
437 	return devm_request_threaded_irq(pchip->dev, pchip->irq, NULL,
438 					 lp8755_irq_handler,
439 					 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
440 					 "lp8755-irq", pchip);
441 }
442 
443 static const struct regmap_config lp8755_regmap = {
444 	.reg_bits = 8,
445 	.val_bits = 8,
446 	.max_register = LP8755_REG_MAX,
447 };
448 
449 static int lp8755_probe(struct i2c_client *client,
450 			const struct i2c_device_id *id)
451 {
452 	int ret, icnt;
453 	struct lp8755_chip *pchip;
454 	struct lp8755_platform_data *pdata = dev_get_platdata(&client->dev);
455 
456 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
457 		dev_err(&client->dev, "i2c functionality check fail.\n");
458 		return -EOPNOTSUPP;
459 	}
460 
461 	pchip = devm_kzalloc(&client->dev,
462 			     sizeof(struct lp8755_chip), GFP_KERNEL);
463 	if (!pchip)
464 		return -ENOMEM;
465 
466 	pchip->dev = &client->dev;
467 	pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
468 	if (IS_ERR(pchip->regmap)) {
469 		ret = PTR_ERR(pchip->regmap);
470 		dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
471 		return ret;
472 	}
473 	i2c_set_clientdata(client, pchip);
474 
475 	if (pdata != NULL) {
476 		pchip->pdata = pdata;
477 		pchip->mphase = pdata->mphase;
478 	} else {
479 		pchip->pdata = devm_kzalloc(pchip->dev,
480 					    sizeof(struct lp8755_platform_data),
481 					    GFP_KERNEL);
482 		if (!pchip->pdata)
483 			return -ENOMEM;
484 		ret = lp8755_init_data(pchip);
485 		if (ret < 0) {
486 			dev_err(&client->dev, "fail to initialize chip\n");
487 			return ret;
488 		}
489 	}
490 
491 	ret = lp8755_regulator_init(pchip);
492 	if (ret < 0) {
493 		dev_err(&client->dev, "fail to initialize regulators\n");
494 		goto err;
495 	}
496 
497 	pchip->irq = client->irq;
498 	ret = lp8755_int_config(pchip);
499 	if (ret < 0) {
500 		dev_err(&client->dev, "fail to irq config\n");
501 		goto err;
502 	}
503 
504 	return ret;
505 
506 err:
507 	/* output disable */
508 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
509 		lp8755_write(pchip, icnt, 0x00);
510 
511 	return ret;
512 }
513 
514 static int lp8755_remove(struct i2c_client *client)
515 {
516 	int icnt;
517 	struct lp8755_chip *pchip = i2c_get_clientdata(client);
518 
519 	for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
520 		lp8755_write(pchip, icnt, 0x00);
521 
522 	return 0;
523 }
524 
525 static const struct i2c_device_id lp8755_id[] = {
526 	{LP8755_NAME, 0},
527 	{}
528 };
529 
530 MODULE_DEVICE_TABLE(i2c, lp8755_id);
531 
532 static struct i2c_driver lp8755_i2c_driver = {
533 	.driver = {
534 		   .name = LP8755_NAME,
535 		   },
536 	.probe = lp8755_probe,
537 	.remove = lp8755_remove,
538 	.id_table = lp8755_id,
539 };
540 
541 static int __init lp8755_init(void)
542 {
543 	return i2c_add_driver(&lp8755_i2c_driver);
544 }
545 
546 subsys_initcall(lp8755_init);
547 
548 static void __exit lp8755_exit(void)
549 {
550 	i2c_del_driver(&lp8755_i2c_driver);
551 }
552 
553 module_exit(lp8755_exit);
554 
555 MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
556 MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
557 MODULE_LICENSE("GPL v2");
558