1 /*
2  * TI LP8788 MFD - buck regulator driver
3  *
4  * Copyright 2012 Texas Instruments
5  *
6  * Author: Milo(Woogyom) Kim <milo.kim@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/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/mfd/lp8788.h>
20 #include <linux/gpio.h>
21 
22 /* register address */
23 #define LP8788_EN_BUCK			0x0C
24 #define LP8788_BUCK_DVS_SEL		0x1D
25 #define LP8788_BUCK1_VOUT0		0x1E
26 #define LP8788_BUCK1_VOUT1		0x1F
27 #define LP8788_BUCK1_VOUT2		0x20
28 #define LP8788_BUCK1_VOUT3		0x21
29 #define LP8788_BUCK2_VOUT0		0x22
30 #define LP8788_BUCK2_VOUT1		0x23
31 #define LP8788_BUCK2_VOUT2		0x24
32 #define LP8788_BUCK2_VOUT3		0x25
33 #define LP8788_BUCK3_VOUT		0x26
34 #define LP8788_BUCK4_VOUT		0x27
35 #define LP8788_BUCK1_TIMESTEP		0x28
36 #define LP8788_BUCK_PWM			0x2D
37 
38 /* mask/shift bits */
39 #define LP8788_EN_BUCK1_M		BIT(0)	/* Addr 0Ch */
40 #define LP8788_EN_BUCK2_M		BIT(1)
41 #define LP8788_EN_BUCK3_M		BIT(2)
42 #define LP8788_EN_BUCK4_M		BIT(3)
43 #define LP8788_BUCK1_DVS_SEL_M		0x04	/* Addr 1Dh */
44 #define LP8788_BUCK1_DVS_M		0x03
45 #define LP8788_BUCK1_DVS_S		0
46 #define LP8788_BUCK2_DVS_SEL_M		0x40
47 #define LP8788_BUCK2_DVS_M		0x30
48 #define LP8788_BUCK2_DVS_S		4
49 #define LP8788_BUCK1_DVS_I2C		BIT(2)
50 #define LP8788_BUCK2_DVS_I2C		BIT(6)
51 #define LP8788_BUCK1_DVS_PIN		(0 << 2)
52 #define LP8788_BUCK2_DVS_PIN		(0 << 6)
53 #define LP8788_VOUT_M			0x1F	/* Addr 1Eh ~ 27h */
54 #define LP8788_STARTUP_TIME_M		0xF8	/* Addr 28h ~ 2Bh */
55 #define LP8788_STARTUP_TIME_S		3
56 #define LP8788_FPWM_BUCK1_M		BIT(0)	/* Addr 2Dh */
57 #define LP8788_FPWM_BUCK1_S		0
58 #define LP8788_FPWM_BUCK2_M		BIT(1)
59 #define LP8788_FPWM_BUCK2_S		1
60 #define LP8788_FPWM_BUCK3_M		BIT(2)
61 #define LP8788_FPWM_BUCK3_S		2
62 #define LP8788_FPWM_BUCK4_M		BIT(3)
63 #define LP8788_FPWM_BUCK4_S		3
64 
65 #define INVALID_ADDR			0xFF
66 #define LP8788_FORCE_PWM		1
67 #define LP8788_AUTO_PWM			0
68 #define PIN_LOW				0
69 #define PIN_HIGH			1
70 #define ENABLE_TIME_USEC		32
71 
72 #define BUCK_FPWM_MASK(x)		(1 << (x))
73 #define BUCK_FPWM_SHIFT(x)		(x)
74 
75 enum lp8788_dvs_state {
76 	DVS_LOW  = GPIOF_OUT_INIT_LOW,
77 	DVS_HIGH = GPIOF_OUT_INIT_HIGH,
78 };
79 
80 enum lp8788_dvs_mode {
81 	REGISTER,
82 	EXTPIN,
83 };
84 
85 enum lp8788_buck_id {
86 	BUCK1,
87 	BUCK2,
88 	BUCK3,
89 	BUCK4,
90 };
91 
92 struct lp8788_buck {
93 	struct lp8788 *lp;
94 	struct regulator_dev *regulator;
95 	void *dvs;
96 };
97 
98 /* BUCK 1 ~ 4 voltage ranges */
99 static const struct regulator_linear_range buck_volt_ranges[] = {
100 	REGULATOR_LINEAR_RANGE(500000, 0, 0, 0),
101 	REGULATOR_LINEAR_RANGE(800000, 1, 25, 50000),
102 };
103 
104 static void lp8788_buck1_set_dvs(struct lp8788_buck *buck)
105 {
106 	struct lp8788_buck1_dvs *dvs = (struct lp8788_buck1_dvs *)buck->dvs;
107 	enum lp8788_dvs_state pinstate;
108 
109 	if (!dvs)
110 		return;
111 
112 	pinstate = dvs->vsel == DVS_SEL_V0 ? DVS_LOW : DVS_HIGH;
113 	if (gpio_is_valid(dvs->gpio))
114 		gpio_set_value(dvs->gpio, pinstate);
115 }
116 
117 static void lp8788_buck2_set_dvs(struct lp8788_buck *buck)
118 {
119 	struct lp8788_buck2_dvs *dvs = (struct lp8788_buck2_dvs *)buck->dvs;
120 	enum lp8788_dvs_state pin1, pin2;
121 
122 	if (!dvs)
123 		return;
124 
125 	switch (dvs->vsel) {
126 	case DVS_SEL_V0:
127 		pin1 = DVS_LOW;
128 		pin2 = DVS_LOW;
129 		break;
130 	case DVS_SEL_V1:
131 		pin1 = DVS_HIGH;
132 		pin2 = DVS_LOW;
133 		break;
134 	case DVS_SEL_V2:
135 		pin1 = DVS_LOW;
136 		pin2 = DVS_HIGH;
137 		break;
138 	case DVS_SEL_V3:
139 		pin1 = DVS_HIGH;
140 		pin2 = DVS_HIGH;
141 		break;
142 	default:
143 		return;
144 	}
145 
146 	if (gpio_is_valid(dvs->gpio[0]))
147 		gpio_set_value(dvs->gpio[0], pin1);
148 
149 	if (gpio_is_valid(dvs->gpio[1]))
150 		gpio_set_value(dvs->gpio[1], pin2);
151 }
152 
153 static void lp8788_set_dvs(struct lp8788_buck *buck, enum lp8788_buck_id id)
154 {
155 	switch (id) {
156 	case BUCK1:
157 		lp8788_buck1_set_dvs(buck);
158 		break;
159 	case BUCK2:
160 		lp8788_buck2_set_dvs(buck);
161 		break;
162 	default:
163 		break;
164 	}
165 }
166 
167 static enum lp8788_dvs_mode
168 lp8788_get_buck_dvs_ctrl_mode(struct lp8788_buck *buck, enum lp8788_buck_id id)
169 {
170 	u8 val, mask;
171 
172 	switch (id) {
173 	case BUCK1:
174 		mask = LP8788_BUCK1_DVS_SEL_M;
175 		break;
176 	case BUCK2:
177 		mask = LP8788_BUCK2_DVS_SEL_M;
178 		break;
179 	default:
180 		return REGISTER;
181 	}
182 
183 	lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
184 
185 	return val & mask ? REGISTER : EXTPIN;
186 }
187 
188 static bool lp8788_is_valid_buck_addr(u8 addr)
189 {
190 	switch (addr) {
191 	case LP8788_BUCK1_VOUT0:
192 	case LP8788_BUCK1_VOUT1:
193 	case LP8788_BUCK1_VOUT2:
194 	case LP8788_BUCK1_VOUT3:
195 	case LP8788_BUCK2_VOUT0:
196 	case LP8788_BUCK2_VOUT1:
197 	case LP8788_BUCK2_VOUT2:
198 	case LP8788_BUCK2_VOUT3:
199 		return true;
200 	default:
201 		return false;
202 	}
203 }
204 
205 static u8 lp8788_select_buck_vout_addr(struct lp8788_buck *buck,
206 					enum lp8788_buck_id id)
207 {
208 	enum lp8788_dvs_mode mode = lp8788_get_buck_dvs_ctrl_mode(buck, id);
209 	struct lp8788_buck1_dvs *b1_dvs;
210 	struct lp8788_buck2_dvs *b2_dvs;
211 	u8 val, idx, addr;
212 	int pin1, pin2;
213 
214 	switch (id) {
215 	case BUCK1:
216 		if (mode == EXTPIN) {
217 			b1_dvs = (struct lp8788_buck1_dvs *)buck->dvs;
218 			if (!b1_dvs)
219 				goto err;
220 
221 			idx = gpio_get_value(b1_dvs->gpio) ? 1 : 0;
222 		} else {
223 			lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
224 			idx = (val & LP8788_BUCK1_DVS_M) >> LP8788_BUCK1_DVS_S;
225 		}
226 		addr = LP8788_BUCK1_VOUT0 + idx;
227 		break;
228 	case BUCK2:
229 		if (mode == EXTPIN) {
230 			b2_dvs = (struct lp8788_buck2_dvs *)buck->dvs;
231 			if (!b2_dvs)
232 				goto err;
233 
234 			pin1 = gpio_get_value(b2_dvs->gpio[0]);
235 			pin2 = gpio_get_value(b2_dvs->gpio[1]);
236 
237 			if (pin1 == PIN_LOW && pin2 == PIN_LOW)
238 				idx = 0;
239 			else if (pin1 == PIN_LOW && pin2 == PIN_HIGH)
240 				idx = 2;
241 			else if (pin1 == PIN_HIGH && pin2 == PIN_LOW)
242 				idx = 1;
243 			else
244 				idx = 3;
245 		} else {
246 			lp8788_read_byte(buck->lp, LP8788_BUCK_DVS_SEL, &val);
247 			idx = (val & LP8788_BUCK2_DVS_M) >> LP8788_BUCK2_DVS_S;
248 		}
249 		addr = LP8788_BUCK2_VOUT0 + idx;
250 		break;
251 	default:
252 		goto err;
253 	}
254 
255 	return addr;
256 err:
257 	return INVALID_ADDR;
258 }
259 
260 static int lp8788_buck12_set_voltage_sel(struct regulator_dev *rdev,
261 					unsigned selector)
262 {
263 	struct lp8788_buck *buck = rdev_get_drvdata(rdev);
264 	enum lp8788_buck_id id = rdev_get_id(rdev);
265 	u8 addr;
266 
267 	if (buck->dvs)
268 		lp8788_set_dvs(buck, id);
269 
270 	addr = lp8788_select_buck_vout_addr(buck, id);
271 	if (!lp8788_is_valid_buck_addr(addr))
272 		return -EINVAL;
273 
274 	return lp8788_update_bits(buck->lp, addr, LP8788_VOUT_M, selector);
275 }
276 
277 static int lp8788_buck12_get_voltage_sel(struct regulator_dev *rdev)
278 {
279 	struct lp8788_buck *buck = rdev_get_drvdata(rdev);
280 	enum lp8788_buck_id id = rdev_get_id(rdev);
281 	int ret;
282 	u8 val, addr;
283 
284 	addr = lp8788_select_buck_vout_addr(buck, id);
285 	if (!lp8788_is_valid_buck_addr(addr))
286 		return -EINVAL;
287 
288 	ret = lp8788_read_byte(buck->lp, addr, &val);
289 	if (ret)
290 		return ret;
291 
292 	return val & LP8788_VOUT_M;
293 }
294 
295 static int lp8788_buck_enable_time(struct regulator_dev *rdev)
296 {
297 	struct lp8788_buck *buck = rdev_get_drvdata(rdev);
298 	enum lp8788_buck_id id = rdev_get_id(rdev);
299 	u8 val, addr = LP8788_BUCK1_TIMESTEP + id;
300 
301 	if (lp8788_read_byte(buck->lp, addr, &val))
302 		return -EINVAL;
303 
304 	val = (val & LP8788_STARTUP_TIME_M) >> LP8788_STARTUP_TIME_S;
305 
306 	return ENABLE_TIME_USEC * val;
307 }
308 
309 static int lp8788_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
310 {
311 	struct lp8788_buck *buck = rdev_get_drvdata(rdev);
312 	enum lp8788_buck_id id = rdev_get_id(rdev);
313 	u8 mask, val;
314 
315 	mask = BUCK_FPWM_MASK(id);
316 	switch (mode) {
317 	case REGULATOR_MODE_FAST:
318 		val = LP8788_FORCE_PWM << BUCK_FPWM_SHIFT(id);
319 		break;
320 	case REGULATOR_MODE_NORMAL:
321 		val = LP8788_AUTO_PWM << BUCK_FPWM_SHIFT(id);
322 		break;
323 	default:
324 		return -EINVAL;
325 	}
326 
327 	return lp8788_update_bits(buck->lp, LP8788_BUCK_PWM, mask, val);
328 }
329 
330 static unsigned int lp8788_buck_get_mode(struct regulator_dev *rdev)
331 {
332 	struct lp8788_buck *buck = rdev_get_drvdata(rdev);
333 	enum lp8788_buck_id id = rdev_get_id(rdev);
334 	u8 val;
335 	int ret;
336 
337 	ret = lp8788_read_byte(buck->lp, LP8788_BUCK_PWM, &val);
338 	if (ret)
339 		return ret;
340 
341 	return val & BUCK_FPWM_MASK(id) ?
342 				REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
343 }
344 
345 static const struct regulator_ops lp8788_buck12_ops = {
346 	.list_voltage = regulator_list_voltage_linear_range,
347 	.map_voltage = regulator_map_voltage_linear_range,
348 	.set_voltage_sel = lp8788_buck12_set_voltage_sel,
349 	.get_voltage_sel = lp8788_buck12_get_voltage_sel,
350 	.enable = regulator_enable_regmap,
351 	.disable = regulator_disable_regmap,
352 	.is_enabled = regulator_is_enabled_regmap,
353 	.enable_time = lp8788_buck_enable_time,
354 	.set_mode = lp8788_buck_set_mode,
355 	.get_mode = lp8788_buck_get_mode,
356 };
357 
358 static const struct regulator_ops lp8788_buck34_ops = {
359 	.list_voltage = regulator_list_voltage_linear_range,
360 	.map_voltage = regulator_map_voltage_linear_range,
361 	.set_voltage_sel = regulator_set_voltage_sel_regmap,
362 	.get_voltage_sel = regulator_get_voltage_sel_regmap,
363 	.enable = regulator_enable_regmap,
364 	.disable = regulator_disable_regmap,
365 	.is_enabled = regulator_is_enabled_regmap,
366 	.enable_time = lp8788_buck_enable_time,
367 	.set_mode = lp8788_buck_set_mode,
368 	.get_mode = lp8788_buck_get_mode,
369 };
370 
371 static const struct regulator_desc lp8788_buck_desc[] = {
372 	{
373 		.name = "buck1",
374 		.id = BUCK1,
375 		.ops = &lp8788_buck12_ops,
376 		.n_voltages = 26,
377 		.linear_ranges = buck_volt_ranges,
378 		.n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
379 		.type = REGULATOR_VOLTAGE,
380 		.owner = THIS_MODULE,
381 		.enable_reg = LP8788_EN_BUCK,
382 		.enable_mask = LP8788_EN_BUCK1_M,
383 	},
384 	{
385 		.name = "buck2",
386 		.id = BUCK2,
387 		.ops = &lp8788_buck12_ops,
388 		.n_voltages = 26,
389 		.linear_ranges = buck_volt_ranges,
390 		.n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
391 		.type = REGULATOR_VOLTAGE,
392 		.owner = THIS_MODULE,
393 		.enable_reg = LP8788_EN_BUCK,
394 		.enable_mask = LP8788_EN_BUCK2_M,
395 	},
396 	{
397 		.name = "buck3",
398 		.id = BUCK3,
399 		.ops = &lp8788_buck34_ops,
400 		.n_voltages = 26,
401 		.linear_ranges = buck_volt_ranges,
402 		.n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
403 		.type = REGULATOR_VOLTAGE,
404 		.owner = THIS_MODULE,
405 		.vsel_reg = LP8788_BUCK3_VOUT,
406 		.vsel_mask = LP8788_VOUT_M,
407 		.enable_reg = LP8788_EN_BUCK,
408 		.enable_mask = LP8788_EN_BUCK3_M,
409 	},
410 	{
411 		.name = "buck4",
412 		.id = BUCK4,
413 		.ops = &lp8788_buck34_ops,
414 		.n_voltages = 26,
415 		.linear_ranges = buck_volt_ranges,
416 		.n_linear_ranges = ARRAY_SIZE(buck_volt_ranges),
417 		.type = REGULATOR_VOLTAGE,
418 		.owner = THIS_MODULE,
419 		.vsel_reg = LP8788_BUCK4_VOUT,
420 		.vsel_mask = LP8788_VOUT_M,
421 		.enable_reg = LP8788_EN_BUCK,
422 		.enable_mask = LP8788_EN_BUCK4_M,
423 	},
424 };
425 
426 static int lp8788_dvs_gpio_request(struct platform_device *pdev,
427 				struct lp8788_buck *buck,
428 				enum lp8788_buck_id id)
429 {
430 	struct lp8788_platform_data *pdata = buck->lp->pdata;
431 	char *b1_name = "LP8788_B1_DVS";
432 	char *b2_name[] = { "LP8788_B2_DVS1", "LP8788_B2_DVS2" };
433 	int i, gpio, ret;
434 
435 	switch (id) {
436 	case BUCK1:
437 		gpio = pdata->buck1_dvs->gpio;
438 		ret = devm_gpio_request_one(&pdev->dev, gpio, DVS_LOW,
439 					    b1_name);
440 		if (ret)
441 			return ret;
442 
443 		buck->dvs = pdata->buck1_dvs;
444 		break;
445 	case BUCK2:
446 		for (i = 0; i < LP8788_NUM_BUCK2_DVS; i++) {
447 			gpio = pdata->buck2_dvs->gpio[i];
448 			ret = devm_gpio_request_one(&pdev->dev, gpio,
449 						    DVS_LOW, b2_name[i]);
450 			if (ret)
451 				return ret;
452 		}
453 		buck->dvs = pdata->buck2_dvs;
454 		break;
455 	default:
456 		break;
457 	}
458 
459 	return 0;
460 }
461 
462 static int lp8788_init_dvs(struct platform_device *pdev,
463 			struct lp8788_buck *buck, enum lp8788_buck_id id)
464 {
465 	struct lp8788_platform_data *pdata = buck->lp->pdata;
466 	u8 mask[] = { LP8788_BUCK1_DVS_SEL_M, LP8788_BUCK2_DVS_SEL_M };
467 	u8 val[]  = { LP8788_BUCK1_DVS_PIN, LP8788_BUCK2_DVS_PIN };
468 	u8 default_dvs_mode[] = { LP8788_BUCK1_DVS_I2C, LP8788_BUCK2_DVS_I2C };
469 
470 	/* no dvs for buck3, 4 */
471 	if (id > BUCK2)
472 		return 0;
473 
474 	/* no dvs platform data, then dvs will be selected by I2C registers */
475 	if (!pdata)
476 		goto set_default_dvs_mode;
477 
478 	if ((id == BUCK1 && !pdata->buck1_dvs) ||
479 		(id == BUCK2 && !pdata->buck2_dvs))
480 		goto set_default_dvs_mode;
481 
482 	if (lp8788_dvs_gpio_request(pdev, buck, id))
483 		goto set_default_dvs_mode;
484 
485 	return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
486 				val[id]);
487 
488 set_default_dvs_mode:
489 	return lp8788_update_bits(buck->lp, LP8788_BUCK_DVS_SEL, mask[id],
490 				  default_dvs_mode[id]);
491 }
492 
493 static int lp8788_buck_probe(struct platform_device *pdev)
494 {
495 	struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
496 	int id = pdev->id;
497 	struct lp8788_buck *buck;
498 	struct regulator_config cfg = { };
499 	struct regulator_dev *rdev;
500 	int ret;
501 
502 	if (id >= LP8788_NUM_BUCKS)
503 		return -EINVAL;
504 
505 	buck = devm_kzalloc(&pdev->dev, sizeof(struct lp8788_buck), GFP_KERNEL);
506 	if (!buck)
507 		return -ENOMEM;
508 
509 	buck->lp = lp;
510 
511 	ret = lp8788_init_dvs(pdev, buck, id);
512 	if (ret)
513 		return ret;
514 
515 	cfg.dev = pdev->dev.parent;
516 	cfg.init_data = lp->pdata ? lp->pdata->buck_data[id] : NULL;
517 	cfg.driver_data = buck;
518 	cfg.regmap = lp->regmap;
519 
520 	rdev = devm_regulator_register(&pdev->dev, &lp8788_buck_desc[id], &cfg);
521 	if (IS_ERR(rdev)) {
522 		ret = PTR_ERR(rdev);
523 		dev_err(&pdev->dev, "BUCK%d regulator register err = %d\n",
524 				id + 1, ret);
525 		return ret;
526 	}
527 
528 	buck->regulator = rdev;
529 	platform_set_drvdata(pdev, buck);
530 
531 	return 0;
532 }
533 
534 static struct platform_driver lp8788_buck_driver = {
535 	.probe = lp8788_buck_probe,
536 	.driver = {
537 		.name = LP8788_DEV_BUCK,
538 	},
539 };
540 
541 static int __init lp8788_buck_init(void)
542 {
543 	return platform_driver_register(&lp8788_buck_driver);
544 }
545 subsys_initcall(lp8788_buck_init);
546 
547 static void __exit lp8788_buck_exit(void)
548 {
549 	platform_driver_unregister(&lp8788_buck_driver);
550 }
551 module_exit(lp8788_buck_exit);
552 
553 MODULE_DESCRIPTION("TI LP8788 BUCK Driver");
554 MODULE_AUTHOR("Milo Kim");
555 MODULE_LICENSE("GPL");
556 MODULE_ALIAS("platform:lp8788-buck");
557