1 /*
2  * tps65912.c  --  TI tps65912
3  *
4  * Copyright 2011 Texas Instruments Inc.
5  *
6  * Author: Margarita Olaya Cabrera <magi@slimlogic.co.uk>
7  *
8  *  This program is free software; you can redistribute it and/or modify it
9  *  under  the terms of the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the License, or (at your
11  *  option) any later version.
12  *
13  * This driver is based on wm8350 implementation.
14  */
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/platform_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/mfd/tps65912.h>
26 
27 /* DCDC's */
28 #define TPS65912_REG_DCDC1	0
29 #define TPS65912_REG_DCDC2	1
30 #define TPS65912_REG_DCDC3	2
31 #define TPS65912_REG_DCDC4	3
32 
33 /* LDOs */
34 #define TPS65912_REG_LDO1	4
35 #define TPS65912_REG_LDO2	5
36 #define TPS65912_REG_LDO3	6
37 #define TPS65912_REG_LDO4	7
38 #define TPS65912_REG_LDO5	8
39 #define TPS65912_REG_LDO6	9
40 #define TPS65912_REG_LDO7	10
41 #define TPS65912_REG_LDO8	11
42 #define TPS65912_REG_LDO9	12
43 #define TPS65912_REG_LDO10	13
44 
45 /* Number of step-down converters available */
46 #define TPS65912_NUM_DCDC	4
47 
48 /* Number of LDO voltage regulators  available */
49 #define TPS65912_NUM_LDO	10
50 
51 /* Number of total regulators available */
52 #define TPS65912_NUM_REGULATOR		(TPS65912_NUM_DCDC + TPS65912_NUM_LDO)
53 
54 #define TPS65912_REG_ENABLED	0x80
55 #define OP_SELREG_MASK		0x40
56 #define OP_SELREG_SHIFT		6
57 
58 struct tps_info {
59 	const char *name;
60 };
61 
62 static struct tps_info tps65912_regs[] = {
63 	{
64 		.name = "DCDC1",
65 	},
66 	{
67 		.name = "DCDC2",
68 	},
69 	{
70 		.name = "DCDC3",
71 	},
72 	{
73 		.name = "DCDC4",
74 	},
75 	{
76 		.name = "LDO1",
77 	},
78 	{
79 		.name = "LDO2",
80 	},
81 	{
82 		.name = "LDO3",
83 	},
84 	{
85 		.name = "LDO4",
86 	},
87 	{
88 		.name = "LDO5",
89 	},
90 	{
91 		.name = "LDO6",
92 	},
93 	{
94 		.name = "LDO7",
95 	},
96 	{
97 		.name = "LDO8",
98 	},
99 	{
100 		.name = "LDO9",
101 	},
102 	{
103 		.name = "LDO10",
104 	},
105 };
106 
107 struct tps65912_reg {
108 	struct regulator_desc desc[TPS65912_NUM_REGULATOR];
109 	struct tps65912 *mfd;
110 	struct regulator_dev *rdev[TPS65912_NUM_REGULATOR];
111 	struct tps_info *info[TPS65912_NUM_REGULATOR];
112 	/* for read/write access */
113 	struct mutex io_lock;
114 	int mode;
115 	int (*get_ctrl_reg)(int);
116 	int dcdc_range[TPS65912_NUM_DCDC];
117 	int pwm_mode_reg;
118 	int eco_reg;
119 };
120 
121 static const struct regulator_linear_range tps65912_ldo_ranges[] = {
122 	{ .min_uV = 800000, .max_uV = 1600000, .min_sel =  0, .max_sel = 32,
123 	  .uV_step = 25000 },
124 	{ .min_uV = 1650000, .max_uV = 3000000, .min_sel = 33, .max_sel = 60,
125 	  .uV_step = 50000 },
126 	{ .min_uV = 3100000, .max_uV = 3300000, .min_sel = 61, .max_sel = 63,
127 	  .uV_step = 100000 },
128 };
129 
130 static int tps65912_get_range(struct tps65912_reg *pmic, int id)
131 {
132 	struct tps65912 *mfd = pmic->mfd;
133 	int range;
134 
135 	switch (id) {
136 	case TPS65912_REG_DCDC1:
137 		range = tps65912_reg_read(mfd, TPS65912_DCDC1_LIMIT);
138 		break;
139 	case TPS65912_REG_DCDC2:
140 		range = tps65912_reg_read(mfd, TPS65912_DCDC2_LIMIT);
141 		break;
142 	case TPS65912_REG_DCDC3:
143 		range = tps65912_reg_read(mfd, TPS65912_DCDC3_LIMIT);
144 		break;
145 	case TPS65912_REG_DCDC4:
146 		range = tps65912_reg_read(mfd, TPS65912_DCDC4_LIMIT);
147 		break;
148 	default:
149 		return 0;
150 	}
151 
152 	if (range >= 0)
153 		range = (range & DCDC_LIMIT_RANGE_MASK)
154 			>> DCDC_LIMIT_RANGE_SHIFT;
155 
156 	pmic->dcdc_range[id] = range;
157 	return range;
158 }
159 
160 static unsigned long tps65912_vsel_to_uv_range0(u8 vsel)
161 {
162 	unsigned long uv;
163 
164 	uv = ((vsel * 12500) + 500000);
165 	return uv;
166 }
167 
168 static unsigned long tps65912_vsel_to_uv_range1(u8 vsel)
169 {
170 	unsigned long uv;
171 
172 	 uv = ((vsel * 12500) + 700000);
173 	return uv;
174 }
175 
176 static unsigned long tps65912_vsel_to_uv_range2(u8 vsel)
177 {
178 	unsigned long uv;
179 
180 	uv = ((vsel * 25000) + 500000);
181 	return uv;
182 }
183 
184 static unsigned long tps65912_vsel_to_uv_range3(u8 vsel)
185 {
186 	unsigned long uv;
187 
188 	if (vsel == 0x3f)
189 		uv = 3800000;
190 	else
191 		uv = ((vsel * 50000) + 500000);
192 
193 	return uv;
194 }
195 
196 static int tps65912_get_ctrl_register(int id)
197 {
198 	if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4)
199 		return id * 3 + TPS65912_DCDC1_AVS;
200 	else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10)
201 		return id - TPS65912_REG_LDO5 + TPS65912_LDO5;
202 	else
203 		return -EINVAL;
204 }
205 
206 static int tps65912_get_sel_register(struct tps65912_reg *pmic, int id)
207 {
208 	struct tps65912 *mfd = pmic->mfd;
209 	int opvsel;
210 	u8 reg = 0;
211 
212 	if (id >= TPS65912_REG_DCDC1 && id <= TPS65912_REG_LDO4) {
213 		opvsel = tps65912_reg_read(mfd, id * 3 + TPS65912_DCDC1_OP);
214 		if (opvsel & OP_SELREG_MASK)
215 			reg = id * 3 + TPS65912_DCDC1_AVS;
216 		else
217 			reg = id * 3 + TPS65912_DCDC1_OP;
218 	} else if (id >= TPS65912_REG_LDO5 && id <= TPS65912_REG_LDO10) {
219 		reg = id - TPS65912_REG_LDO5 + TPS65912_LDO5;
220 	} else {
221 		return -EINVAL;
222 	}
223 
224 	return reg;
225 }
226 
227 static int tps65912_get_mode_regiters(struct tps65912_reg *pmic, int id)
228 {
229 	switch (id) {
230 	case TPS65912_REG_DCDC1:
231 		pmic->pwm_mode_reg = TPS65912_DCDC1_CTRL;
232 		pmic->eco_reg = TPS65912_DCDC1_AVS;
233 		break;
234 	case TPS65912_REG_DCDC2:
235 		pmic->pwm_mode_reg = TPS65912_DCDC2_CTRL;
236 		pmic->eco_reg = TPS65912_DCDC2_AVS;
237 		break;
238 	case TPS65912_REG_DCDC3:
239 		pmic->pwm_mode_reg = TPS65912_DCDC3_CTRL;
240 		pmic->eco_reg = TPS65912_DCDC3_AVS;
241 		break;
242 	case TPS65912_REG_DCDC4:
243 		pmic->pwm_mode_reg = TPS65912_DCDC4_CTRL;
244 		pmic->eco_reg = TPS65912_DCDC4_AVS;
245 		break;
246 	default:
247 		return -EINVAL;
248 	}
249 
250 	return 0;
251 }
252 
253 static int tps65912_reg_is_enabled(struct regulator_dev *dev)
254 {
255 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
256 	struct tps65912 *mfd = pmic->mfd;
257 	int reg, value, id = rdev_get_id(dev);
258 
259 	if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
260 		return -EINVAL;
261 
262 	reg = pmic->get_ctrl_reg(id);
263 	if (reg < 0)
264 		return reg;
265 
266 	value = tps65912_reg_read(mfd, reg);
267 	if (value < 0)
268 		return value;
269 
270 	return value & TPS65912_REG_ENABLED;
271 }
272 
273 static int tps65912_reg_enable(struct regulator_dev *dev)
274 {
275 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
276 	struct tps65912 *mfd = pmic->mfd;
277 	int id = rdev_get_id(dev);
278 	int reg;
279 
280 	if (id < TPS65912_REG_DCDC1 || id > TPS65912_REG_LDO10)
281 		return -EINVAL;
282 
283 	reg = pmic->get_ctrl_reg(id);
284 	if (reg < 0)
285 		return reg;
286 
287 	return tps65912_set_bits(mfd, reg, TPS65912_REG_ENABLED);
288 }
289 
290 static int tps65912_reg_disable(struct regulator_dev *dev)
291 {
292 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
293 	struct tps65912 *mfd = pmic->mfd;
294 	int id = rdev_get_id(dev), reg;
295 
296 	reg = pmic->get_ctrl_reg(id);
297 	if (reg < 0)
298 		return reg;
299 
300 	return tps65912_clear_bits(mfd, reg, TPS65912_REG_ENABLED);
301 }
302 
303 static int tps65912_set_mode(struct regulator_dev *dev, unsigned int mode)
304 {
305 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
306 	struct tps65912 *mfd = pmic->mfd;
307 	int pwm_mode, eco, id = rdev_get_id(dev);
308 
309 	tps65912_get_mode_regiters(pmic, id);
310 
311 	pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
312 	eco = tps65912_reg_read(mfd, pmic->eco_reg);
313 
314 	pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
315 	eco &= DCDC_AVS_ECO_MASK;
316 
317 	switch (mode) {
318 	case REGULATOR_MODE_FAST:
319 		/* Verify if mode alredy set */
320 		if (pwm_mode && !eco)
321 			break;
322 		tps65912_set_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
323 		tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
324 		break;
325 	case REGULATOR_MODE_NORMAL:
326 	case REGULATOR_MODE_IDLE:
327 		if (!pwm_mode && !eco)
328 			break;
329 		tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
330 		tps65912_clear_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
331 		break;
332 	case REGULATOR_MODE_STANDBY:
333 		if (!pwm_mode && eco)
334 			break;
335 		tps65912_clear_bits(mfd, pmic->pwm_mode_reg, DCDCCTRL_DCDC_MODE_MASK);
336 		tps65912_set_bits(mfd, pmic->eco_reg, DCDC_AVS_ECO_MASK);
337 		break;
338 	default:
339 		return -EINVAL;
340 	}
341 
342 	return 0;
343 }
344 
345 static unsigned int tps65912_get_mode(struct regulator_dev *dev)
346 {
347 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
348 	struct tps65912 *mfd = pmic->mfd;
349 	int pwm_mode, eco, mode = 0, id = rdev_get_id(dev);
350 
351 	tps65912_get_mode_regiters(pmic, id);
352 
353 	pwm_mode = tps65912_reg_read(mfd, pmic->pwm_mode_reg);
354 	eco = tps65912_reg_read(mfd, pmic->eco_reg);
355 
356 	pwm_mode &= DCDCCTRL_DCDC_MODE_MASK;
357 	eco &= DCDC_AVS_ECO_MASK;
358 
359 	if (pwm_mode && !eco)
360 		mode = REGULATOR_MODE_FAST;
361 	else if (!pwm_mode && !eco)
362 		mode = REGULATOR_MODE_NORMAL;
363 	else if (!pwm_mode && eco)
364 		mode = REGULATOR_MODE_STANDBY;
365 
366 	return mode;
367 }
368 
369 static int tps65912_list_voltage(struct regulator_dev *dev, unsigned selector)
370 {
371 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
372 	int range, voltage = 0, id = rdev_get_id(dev);
373 
374 	if (id > TPS65912_REG_DCDC4)
375 		return -EINVAL;
376 
377 	range = pmic->dcdc_range[id];
378 
379 	switch (range) {
380 	case 0:
381 		/* 0.5 - 1.2875V in 12.5mV steps */
382 		voltage = tps65912_vsel_to_uv_range0(selector);
383 		break;
384 	case 1:
385 		/* 0.7 - 1.4875V in 12.5mV steps */
386 		voltage = tps65912_vsel_to_uv_range1(selector);
387 		break;
388 	case 2:
389 		/* 0.5 - 2.075V in 25mV steps */
390 		voltage = tps65912_vsel_to_uv_range2(selector);
391 		break;
392 	case 3:
393 		/* 0.5 - 3.8V in 50mV steps */
394 		voltage = tps65912_vsel_to_uv_range3(selector);
395 		break;
396 	}
397 	return voltage;
398 }
399 
400 static int tps65912_get_voltage_sel(struct regulator_dev *dev)
401 {
402 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
403 	struct tps65912 *mfd = pmic->mfd;
404 	int id = rdev_get_id(dev);
405 	int reg, vsel;
406 
407 	reg = tps65912_get_sel_register(pmic, id);
408 	if (reg < 0)
409 		return reg;
410 
411 	vsel = tps65912_reg_read(mfd, reg);
412 	vsel &= 0x3F;
413 
414 	return vsel;
415 }
416 
417 static int tps65912_set_voltage_sel(struct regulator_dev *dev,
418 					 unsigned selector)
419 {
420 	struct tps65912_reg *pmic = rdev_get_drvdata(dev);
421 	struct tps65912 *mfd = pmic->mfd;
422 	int id = rdev_get_id(dev);
423 	int value;
424 	u8 reg;
425 
426 	reg = tps65912_get_sel_register(pmic, id);
427 	value = tps65912_reg_read(mfd, reg);
428 	value &= 0xC0;
429 	return tps65912_reg_write(mfd, reg, selector | value);
430 }
431 
432 /* Operations permitted on DCDCx */
433 static struct regulator_ops tps65912_ops_dcdc = {
434 	.is_enabled = tps65912_reg_is_enabled,
435 	.enable = tps65912_reg_enable,
436 	.disable = tps65912_reg_disable,
437 	.set_mode = tps65912_set_mode,
438 	.get_mode = tps65912_get_mode,
439 	.get_voltage_sel = tps65912_get_voltage_sel,
440 	.set_voltage_sel = tps65912_set_voltage_sel,
441 	.list_voltage = tps65912_list_voltage,
442 };
443 
444 /* Operations permitted on LDOx */
445 static struct regulator_ops tps65912_ops_ldo = {
446 	.is_enabled = tps65912_reg_is_enabled,
447 	.enable = tps65912_reg_enable,
448 	.disable = tps65912_reg_disable,
449 	.get_voltage_sel = tps65912_get_voltage_sel,
450 	.set_voltage_sel = tps65912_set_voltage_sel,
451 	.list_voltage = regulator_list_voltage_linear_range,
452 	.map_voltage = regulator_map_voltage_linear_range,
453 };
454 
455 static int tps65912_probe(struct platform_device *pdev)
456 {
457 	struct tps65912 *tps65912 = dev_get_drvdata(pdev->dev.parent);
458 	struct regulator_config config = { };
459 	struct tps_info *info;
460 	struct regulator_init_data *reg_data;
461 	struct regulator_dev *rdev;
462 	struct tps65912_reg *pmic;
463 	struct tps65912_board *pmic_plat_data;
464 	int i, err;
465 
466 	pmic_plat_data = dev_get_platdata(tps65912->dev);
467 	if (!pmic_plat_data)
468 		return -EINVAL;
469 
470 	reg_data = pmic_plat_data->tps65912_pmic_init_data;
471 
472 	pmic = devm_kzalloc(&pdev->dev, sizeof(*pmic), GFP_KERNEL);
473 	if (!pmic)
474 		return -ENOMEM;
475 
476 	mutex_init(&pmic->io_lock);
477 	pmic->mfd = tps65912;
478 	platform_set_drvdata(pdev, pmic);
479 
480 	pmic->get_ctrl_reg = &tps65912_get_ctrl_register;
481 	info = tps65912_regs;
482 
483 	for (i = 0; i < TPS65912_NUM_REGULATOR; i++, info++, reg_data++) {
484 		int range = 0;
485 		/* Register the regulators */
486 		pmic->info[i] = info;
487 
488 		pmic->desc[i].name = info->name;
489 		pmic->desc[i].id = i;
490 		pmic->desc[i].n_voltages = 64;
491 		if (i > TPS65912_REG_DCDC4) {
492 			pmic->desc[i].ops = &tps65912_ops_ldo;
493 			pmic->desc[i].linear_ranges = tps65912_ldo_ranges;
494 			pmic->desc[i].n_linear_ranges =
495 					ARRAY_SIZE(tps65912_ldo_ranges);
496 		} else {
497 			pmic->desc[i].ops = &tps65912_ops_dcdc;
498 		}
499 		pmic->desc[i].type = REGULATOR_VOLTAGE;
500 		pmic->desc[i].owner = THIS_MODULE;
501 		range = tps65912_get_range(pmic, i);
502 
503 		config.dev = tps65912->dev;
504 		config.init_data = reg_data;
505 		config.driver_data = pmic;
506 
507 		rdev = regulator_register(&pmic->desc[i], &config);
508 		if (IS_ERR(rdev)) {
509 			dev_err(tps65912->dev,
510 				"failed to register %s regulator\n",
511 				pdev->name);
512 			err = PTR_ERR(rdev);
513 			goto err;
514 		}
515 
516 		/* Save regulator for cleanup */
517 		pmic->rdev[i] = rdev;
518 	}
519 	return 0;
520 
521 err:
522 	while (--i >= 0)
523 		regulator_unregister(pmic->rdev[i]);
524 	return err;
525 }
526 
527 static int tps65912_remove(struct platform_device *pdev)
528 {
529 	struct tps65912_reg *tps65912_reg = platform_get_drvdata(pdev);
530 	int i;
531 
532 	for (i = 0; i < TPS65912_NUM_REGULATOR; i++)
533 		regulator_unregister(tps65912_reg->rdev[i]);
534 	return 0;
535 }
536 
537 static struct platform_driver tps65912_driver = {
538 	.driver = {
539 		.name = "tps65912-pmic",
540 		.owner = THIS_MODULE,
541 	},
542 	.probe = tps65912_probe,
543 	.remove = tps65912_remove,
544 };
545 
546 static int __init tps65912_init(void)
547 {
548 	return platform_driver_register(&tps65912_driver);
549 }
550 subsys_initcall(tps65912_init);
551 
552 static void __exit tps65912_cleanup(void)
553 {
554 	platform_driver_unregister(&tps65912_driver);
555 }
556 module_exit(tps65912_cleanup);
557 
558 MODULE_AUTHOR("Margarita Olaya Cabrera <magi@slimlogic.co.uk>");
559 MODULE_DESCRIPTION("TPS65912 voltage regulator driver");
560 MODULE_LICENSE("GPL v2");
561 MODULE_ALIAS("platform:tps65912-pmic");
562