1 /*
2  * Split TWL6030 logic from twl-regulator.c:
3  * Copyright (C) 2008 David Brownell
4  *
5  * Copyright (C) 2016 Nicolae Rosia <nicolae.rosia@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12 
13 #include <linux/module.h>
14 #include <linux/string.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/of.h>
20 #include <linux/of_device.h>
21 #include <linux/regulator/driver.h>
22 #include <linux/regulator/machine.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/mfd/twl.h>
25 #include <linux/delay.h>
26 
27 struct twlreg_info {
28 	/* start of regulator's PM_RECEIVER control register bank */
29 	u8			base;
30 
31 	/* twl resource ID, for resource control state machine */
32 	u8			id;
33 
34 	/* chip constraints on regulator behavior */
35 	u16			min_mV;
36 
37 	u8			flags;
38 
39 	/* used by regulator core */
40 	struct regulator_desc	desc;
41 
42 	/* chip specific features */
43 	unsigned long		features;
44 
45 	/* data passed from board for external get/set voltage */
46 	void			*data;
47 };
48 
49 
50 /* LDO control registers ... offset is from the base of its register bank.
51  * The first three registers of all power resource banks help hardware to
52  * manage the various resource groups.
53  */
54 /* Common offset in TWL4030/6030 */
55 #define VREG_GRP		0
56 /* TWL6030 register offsets */
57 #define VREG_TRANS		1
58 #define VREG_STATE		2
59 #define VREG_VOLTAGE		3
60 #define VREG_VOLTAGE_SMPS	4
61 /* TWL6030 Misc register offsets */
62 #define VREG_BC_ALL		1
63 #define VREG_BC_REF		2
64 #define VREG_BC_PROC		3
65 #define VREG_BC_CLK_RST		4
66 
67 /* TWL6030 LDO register values for CFG_STATE */
68 #define TWL6030_CFG_STATE_OFF	0x00
69 #define TWL6030_CFG_STATE_ON	0x01
70 #define TWL6030_CFG_STATE_OFF2	0x02
71 #define TWL6030_CFG_STATE_SLEEP	0x03
72 #define TWL6030_CFG_STATE_GRP_SHIFT	5
73 #define TWL6030_CFG_STATE_APP_SHIFT	2
74 #define TWL6030_CFG_STATE_MASK		0x03
75 #define TWL6030_CFG_STATE_APP_MASK	(0x03 << TWL6030_CFG_STATE_APP_SHIFT)
76 #define TWL6030_CFG_STATE_APP(v)	(((v) & TWL6030_CFG_STATE_APP_MASK) >>\
77 						TWL6030_CFG_STATE_APP_SHIFT)
78 
79 /* Flags for SMPS Voltage reading */
80 #define SMPS_OFFSET_EN		BIT(0)
81 #define SMPS_EXTENDED_EN	BIT(1)
82 
83 /* twl6032 SMPS EPROM values */
84 #define TWL6030_SMPS_OFFSET		0xB0
85 #define TWL6030_SMPS_MULT		0xB3
86 #define SMPS_MULTOFFSET_SMPS4	BIT(0)
87 #define SMPS_MULTOFFSET_VIO	BIT(1)
88 #define SMPS_MULTOFFSET_SMPS3	BIT(6)
89 
90 static inline int
twlreg_read(struct twlreg_info * info,unsigned slave_subgp,unsigned offset)91 twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
92 {
93 	u8 value;
94 	int status;
95 
96 	status = twl_i2c_read_u8(slave_subgp,
97 			&value, info->base + offset);
98 	return (status < 0) ? status : value;
99 }
100 
101 static inline int
twlreg_write(struct twlreg_info * info,unsigned slave_subgp,unsigned offset,u8 value)102 twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
103 						 u8 value)
104 {
105 	return twl_i2c_write_u8(slave_subgp,
106 			value, info->base + offset);
107 }
108 
109 /* generic power resource operations, which work on all regulators */
twlreg_grp(struct regulator_dev * rdev)110 static int twlreg_grp(struct regulator_dev *rdev)
111 {
112 	return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
113 								 VREG_GRP);
114 }
115 
116 /*
117  * Enable/disable regulators by joining/leaving the P1 (processor) group.
118  * We assume nobody else is updating the DEV_GRP registers.
119  */
120 /* definition for 6030 family */
121 #define P3_GRP_6030	BIT(2)		/* secondary processor, modem, etc */
122 #define P2_GRP_6030	BIT(1)		/* "peripherals" */
123 #define P1_GRP_6030	BIT(0)		/* CPU/Linux */
124 
twl6030reg_is_enabled(struct regulator_dev * rdev)125 static int twl6030reg_is_enabled(struct regulator_dev *rdev)
126 {
127 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
128 	int			grp = 0, val;
129 
130 	if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
131 		grp = twlreg_grp(rdev);
132 		if (grp < 0)
133 			return grp;
134 		grp &= P1_GRP_6030;
135 		val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
136 		val = TWL6030_CFG_STATE_APP(val);
137 	} else {
138 		val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
139 		val &= TWL6030_CFG_STATE_MASK;
140 		grp = 1;
141 	}
142 
143 	return grp && (val == TWL6030_CFG_STATE_ON);
144 }
145 
146 #define PB_I2C_BUSY	BIT(0)
147 #define PB_I2C_BWEN	BIT(1)
148 
149 
twl6030reg_enable(struct regulator_dev * rdev)150 static int twl6030reg_enable(struct regulator_dev *rdev)
151 {
152 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
153 	int			grp = 0;
154 	int			ret;
155 
156 	if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
157 		grp = twlreg_grp(rdev);
158 	if (grp < 0)
159 		return grp;
160 
161 	ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
162 			grp << TWL6030_CFG_STATE_GRP_SHIFT |
163 			TWL6030_CFG_STATE_ON);
164 	return ret;
165 }
166 
twl6030reg_disable(struct regulator_dev * rdev)167 static int twl6030reg_disable(struct regulator_dev *rdev)
168 {
169 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
170 	int			grp = 0;
171 	int			ret;
172 
173 	if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
174 		grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
175 
176 	/* For 6030, set the off state for all grps enabled */
177 	ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
178 			(grp) << TWL6030_CFG_STATE_GRP_SHIFT |
179 			TWL6030_CFG_STATE_OFF);
180 
181 	return ret;
182 }
183 
twl6030reg_get_status(struct regulator_dev * rdev)184 static int twl6030reg_get_status(struct regulator_dev *rdev)
185 {
186 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
187 	int			val;
188 
189 	val = twlreg_grp(rdev);
190 	if (val < 0)
191 		return val;
192 
193 	val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
194 
195 	if (info->features & TWL6032_SUBCLASS)
196 		val &= TWL6030_CFG_STATE_MASK;
197 	else
198 		val = TWL6030_CFG_STATE_APP(val);
199 
200 	switch (val) {
201 	case TWL6030_CFG_STATE_ON:
202 		return REGULATOR_STATUS_NORMAL;
203 
204 	case TWL6030_CFG_STATE_SLEEP:
205 		return REGULATOR_STATUS_STANDBY;
206 
207 	case TWL6030_CFG_STATE_OFF:
208 	case TWL6030_CFG_STATE_OFF2:
209 	default:
210 		break;
211 	}
212 
213 	return REGULATOR_STATUS_OFF;
214 }
215 
twl6030reg_set_mode(struct regulator_dev * rdev,unsigned mode)216 static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
217 {
218 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
219 	int grp = 0;
220 	int val;
221 
222 	if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
223 		grp = twlreg_grp(rdev);
224 
225 	if (grp < 0)
226 		return grp;
227 
228 	/* Compose the state register settings */
229 	val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
230 	/* We can only set the mode through state machine commands... */
231 	switch (mode) {
232 	case REGULATOR_MODE_NORMAL:
233 		val |= TWL6030_CFG_STATE_ON;
234 		break;
235 	case REGULATOR_MODE_STANDBY:
236 		val |= TWL6030_CFG_STATE_SLEEP;
237 		break;
238 
239 	default:
240 		return -EINVAL;
241 	}
242 
243 	return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
244 }
245 
twl6030coresmps_set_voltage(struct regulator_dev * rdev,int min_uV,int max_uV,unsigned * selector)246 static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
247 	int max_uV, unsigned *selector)
248 {
249 	return -ENODEV;
250 }
251 
twl6030coresmps_get_voltage(struct regulator_dev * rdev)252 static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
253 {
254 	return -ENODEV;
255 }
256 
257 static struct regulator_ops twl6030coresmps_ops = {
258 	.set_voltage	= twl6030coresmps_set_voltage,
259 	.get_voltage	= twl6030coresmps_get_voltage,
260 };
261 
twl6030ldo_list_voltage(struct regulator_dev * rdev,unsigned sel)262 static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned sel)
263 {
264 	struct twlreg_info *info = rdev_get_drvdata(rdev);
265 
266 	switch (sel) {
267 	case 0:
268 		return 0;
269 	case 1 ... 24:
270 		/* Linear mapping from 00000001 to 00011000:
271 		 * Absolute voltage value = 1.0 V + 0.1 V × (sel – 00000001)
272 		 */
273 		return (info->min_mV + 100 * (sel - 1)) * 1000;
274 	case 25 ... 30:
275 		return -EINVAL;
276 	case 31:
277 		return 2750000;
278 	default:
279 		return -EINVAL;
280 	}
281 }
282 
283 static int
twl6030ldo_set_voltage_sel(struct regulator_dev * rdev,unsigned selector)284 twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
285 {
286 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
287 
288 	return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
289 			    selector);
290 }
291 
twl6030ldo_get_voltage_sel(struct regulator_dev * rdev)292 static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
293 {
294 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
295 	int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
296 
297 	return vsel;
298 }
299 
300 static struct regulator_ops twl6030ldo_ops = {
301 	.list_voltage	= twl6030ldo_list_voltage,
302 
303 	.set_voltage_sel = twl6030ldo_set_voltage_sel,
304 	.get_voltage_sel = twl6030ldo_get_voltage_sel,
305 
306 	.enable		= twl6030reg_enable,
307 	.disable	= twl6030reg_disable,
308 	.is_enabled	= twl6030reg_is_enabled,
309 
310 	.set_mode	= twl6030reg_set_mode,
311 
312 	.get_status	= twl6030reg_get_status,
313 };
314 
315 static struct regulator_ops twl6030fixed_ops = {
316 	.list_voltage	= regulator_list_voltage_linear,
317 
318 	.enable		= twl6030reg_enable,
319 	.disable	= twl6030reg_disable,
320 	.is_enabled	= twl6030reg_is_enabled,
321 
322 	.set_mode	= twl6030reg_set_mode,
323 
324 	.get_status	= twl6030reg_get_status,
325 };
326 
327 /*
328  * SMPS status and control
329  */
330 
twl6030smps_list_voltage(struct regulator_dev * rdev,unsigned index)331 static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
332 {
333 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
334 
335 	int voltage = 0;
336 
337 	switch (info->flags) {
338 	case SMPS_OFFSET_EN:
339 		voltage = 100000;
340 		/* fall through */
341 	case 0:
342 		switch (index) {
343 		case 0:
344 			voltage = 0;
345 			break;
346 		case 58:
347 			voltage = 1350 * 1000;
348 			break;
349 		case 59:
350 			voltage = 1500 * 1000;
351 			break;
352 		case 60:
353 			voltage = 1800 * 1000;
354 			break;
355 		case 61:
356 			voltage = 1900 * 1000;
357 			break;
358 		case 62:
359 			voltage = 2100 * 1000;
360 			break;
361 		default:
362 			voltage += (600000 + (12500 * (index - 1)));
363 		}
364 		break;
365 	case SMPS_EXTENDED_EN:
366 		switch (index) {
367 		case 0:
368 			voltage = 0;
369 			break;
370 		case 58:
371 			voltage = 2084 * 1000;
372 			break;
373 		case 59:
374 			voltage = 2315 * 1000;
375 			break;
376 		case 60:
377 			voltage = 2778 * 1000;
378 			break;
379 		case 61:
380 			voltage = 2932 * 1000;
381 			break;
382 		case 62:
383 			voltage = 3241 * 1000;
384 			break;
385 		default:
386 			voltage = (1852000 + (38600 * (index - 1)));
387 		}
388 		break;
389 	case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
390 		switch (index) {
391 		case 0:
392 			voltage = 0;
393 			break;
394 		case 58:
395 			voltage = 4167 * 1000;
396 			break;
397 		case 59:
398 			voltage = 2315 * 1000;
399 			break;
400 		case 60:
401 			voltage = 2778 * 1000;
402 			break;
403 		case 61:
404 			voltage = 2932 * 1000;
405 			break;
406 		case 62:
407 			voltage = 3241 * 1000;
408 			break;
409 		default:
410 			voltage = (2161000 + (38600 * (index - 1)));
411 		}
412 		break;
413 	}
414 
415 	return voltage;
416 }
417 
twl6030smps_map_voltage(struct regulator_dev * rdev,int min_uV,int max_uV)418 static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
419 				   int max_uV)
420 {
421 	struct twlreg_info *info = rdev_get_drvdata(rdev);
422 	int vsel = 0;
423 
424 	switch (info->flags) {
425 	case 0:
426 		if (min_uV == 0)
427 			vsel = 0;
428 		else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
429 			vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
430 			vsel++;
431 		}
432 		/* Values 1..57 for vsel are linear and can be calculated
433 		 * values 58..62 are non linear.
434 		 */
435 		else if ((min_uV > 1900000) && (min_uV <= 2100000))
436 			vsel = 62;
437 		else if ((min_uV > 1800000) && (min_uV <= 1900000))
438 			vsel = 61;
439 		else if ((min_uV > 1500000) && (min_uV <= 1800000))
440 			vsel = 60;
441 		else if ((min_uV > 1350000) && (min_uV <= 1500000))
442 			vsel = 59;
443 		else if ((min_uV > 1300000) && (min_uV <= 1350000))
444 			vsel = 58;
445 		else
446 			return -EINVAL;
447 		break;
448 	case SMPS_OFFSET_EN:
449 		if (min_uV == 0)
450 			vsel = 0;
451 		else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
452 			vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
453 			vsel++;
454 		}
455 		/* Values 1..57 for vsel are linear and can be calculated
456 		 * values 58..62 are non linear.
457 		 */
458 		else if ((min_uV > 1900000) && (min_uV <= 2100000))
459 			vsel = 62;
460 		else if ((min_uV > 1800000) && (min_uV <= 1900000))
461 			vsel = 61;
462 		else if ((min_uV > 1500000) && (min_uV <= 1800000))
463 			vsel = 60;
464 		else if ((min_uV > 1350000) && (min_uV <= 1500000))
465 			vsel = 59;
466 		else
467 			return -EINVAL;
468 		break;
469 	case SMPS_EXTENDED_EN:
470 		if (min_uV == 0) {
471 			vsel = 0;
472 		} else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
473 			vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
474 			vsel++;
475 		}
476 		break;
477 	case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
478 		if (min_uV == 0) {
479 			vsel = 0;
480 		} else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
481 			vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
482 			vsel++;
483 		}
484 		break;
485 	}
486 
487 	return vsel;
488 }
489 
twl6030smps_set_voltage_sel(struct regulator_dev * rdev,unsigned int selector)490 static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
491 				       unsigned int selector)
492 {
493 	struct twlreg_info *info = rdev_get_drvdata(rdev);
494 
495 	return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
496 			    selector);
497 }
498 
twl6030smps_get_voltage_sel(struct regulator_dev * rdev)499 static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
500 {
501 	struct twlreg_info	*info = rdev_get_drvdata(rdev);
502 
503 	return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
504 }
505 
506 static struct regulator_ops twlsmps_ops = {
507 	.list_voltage		= twl6030smps_list_voltage,
508 	.map_voltage		= twl6030smps_map_voltage,
509 
510 	.set_voltage_sel	= twl6030smps_set_voltage_sel,
511 	.get_voltage_sel	= twl6030smps_get_voltage_sel,
512 
513 	.enable			= twl6030reg_enable,
514 	.disable		= twl6030reg_disable,
515 	.is_enabled		= twl6030reg_is_enabled,
516 
517 	.set_mode		= twl6030reg_set_mode,
518 
519 	.get_status		= twl6030reg_get_status,
520 };
521 
522 /*----------------------------------------------------------------------*/
523 
524 #define TWL6030_ADJUSTABLE_SMPS(label) \
525 static const struct twlreg_info TWL6030_INFO_##label = { \
526 	.desc = { \
527 		.name = #label, \
528 		.id = TWL6030_REG_##label, \
529 		.ops = &twl6030coresmps_ops, \
530 		.type = REGULATOR_VOLTAGE, \
531 		.owner = THIS_MODULE, \
532 		}, \
533 	}
534 
535 #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts) \
536 static const struct twlreg_info TWL6030_INFO_##label = { \
537 	.base = offset, \
538 	.min_mV = min_mVolts, \
539 	.desc = { \
540 		.name = #label, \
541 		.id = TWL6030_REG_##label, \
542 		.n_voltages = 32, \
543 		.ops = &twl6030ldo_ops, \
544 		.type = REGULATOR_VOLTAGE, \
545 		.owner = THIS_MODULE, \
546 		}, \
547 	}
548 
549 #define TWL6032_ADJUSTABLE_LDO(label, offset, min_mVolts) \
550 static const struct twlreg_info TWL6032_INFO_##label = { \
551 	.base = offset, \
552 	.min_mV = min_mVolts, \
553 	.desc = { \
554 		.name = #label, \
555 		.id = TWL6032_REG_##label, \
556 		.n_voltages = 32, \
557 		.ops = &twl6030ldo_ops, \
558 		.type = REGULATOR_VOLTAGE, \
559 		.owner = THIS_MODULE, \
560 		}, \
561 	}
562 
563 #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
564 static const struct twlreg_info TWLFIXED_INFO_##label = { \
565 	.base = offset, \
566 	.id = 0, \
567 	.min_mV = mVolts, \
568 	.desc = { \
569 		.name = #label, \
570 		.id = TWL6030##_REG_##label, \
571 		.n_voltages = 1, \
572 		.ops = &twl6030fixed_ops, \
573 		.type = REGULATOR_VOLTAGE, \
574 		.owner = THIS_MODULE, \
575 		.min_uV = mVolts * 1000, \
576 		.enable_time = turnon_delay, \
577 		.of_map_mode = NULL, \
578 		}, \
579 	}
580 
581 #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
582 static const struct twlreg_info TWLSMPS_INFO_##label = { \
583 	.base = offset, \
584 	.min_mV = 600, \
585 	.desc = { \
586 		.name = #label, \
587 		.id = TWL6032_REG_##label, \
588 		.n_voltages = 63, \
589 		.ops = &twlsmps_ops, \
590 		.type = REGULATOR_VOLTAGE, \
591 		.owner = THIS_MODULE, \
592 		}, \
593 	}
594 
595 /* VUSBCP is managed *only* by the USB subchip */
596 /* 6030 REG with base as PMC Slave Misc : 0x0030 */
597 /* Turnon-delay and remap configuration values for 6030 are not
598    verified since the specification is not public */
599 TWL6030_ADJUSTABLE_SMPS(VDD1);
600 TWL6030_ADJUSTABLE_SMPS(VDD2);
601 TWL6030_ADJUSTABLE_SMPS(VDD3);
602 TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000);
603 TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000);
604 TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000);
605 TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000);
606 TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000);
607 TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000);
608 /* 6025 are renamed compared to 6030 versions */
609 TWL6032_ADJUSTABLE_LDO(LDO2, 0x54, 1000);
610 TWL6032_ADJUSTABLE_LDO(LDO4, 0x58, 1000);
611 TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c, 1000);
612 TWL6032_ADJUSTABLE_LDO(LDO5, 0x68, 1000);
613 TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c, 1000);
614 TWL6032_ADJUSTABLE_LDO(LDO7, 0x74, 1000);
615 TWL6032_ADJUSTABLE_LDO(LDO6, 0x60, 1000);
616 TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64, 1000);
617 TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000);
618 TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
619 TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
620 TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
621 TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
622 TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
623 TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
624 TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
625 TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
626 TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
627 
twl_get_smps_offset(void)628 static u8 twl_get_smps_offset(void)
629 {
630 	u8 value;
631 
632 	twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
633 			TWL6030_SMPS_OFFSET);
634 	return value;
635 }
636 
twl_get_smps_mult(void)637 static u8 twl_get_smps_mult(void)
638 {
639 	u8 value;
640 
641 	twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
642 			TWL6030_SMPS_MULT);
643 	return value;
644 }
645 
646 #define TWL_OF_MATCH(comp, family, label) \
647 	{ \
648 		.compatible = comp, \
649 		.data = &family##_INFO_##label, \
650 	}
651 
652 #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
653 #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
654 #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
655 #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
656 
657 static const struct of_device_id twl_of_match[] = {
658 	TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
659 	TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
660 	TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
661 	TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
662 	TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
663 	TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
664 	TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
665 	TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
666 	TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
667 	TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
668 	TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
669 	TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
670 	TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
671 	TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
672 	TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
673 	TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
674 	TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
675 	TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
676 	TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
677 	TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
678 	TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
679 	TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
680 	TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
681 	TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
682 	TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
683 	TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
684 	TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
685 	{},
686 };
687 MODULE_DEVICE_TABLE(of, twl_of_match);
688 
twlreg_probe(struct platform_device * pdev)689 static int twlreg_probe(struct platform_device *pdev)
690 {
691 	int id;
692 	struct twlreg_info		*info;
693 	const struct twlreg_info	*template;
694 	struct regulator_init_data	*initdata;
695 	struct regulation_constraints	*c;
696 	struct regulator_dev		*rdev;
697 	const struct of_device_id	*match;
698 	struct regulator_config		config = { };
699 
700 	match = of_match_device(twl_of_match, &pdev->dev);
701 	if (!match)
702 		return -ENODEV;
703 
704 	template = match->data;
705 	if (!template)
706 		return -ENODEV;
707 
708 	id = template->desc.id;
709 	initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
710 						&template->desc);
711 	if (!initdata)
712 		return -EINVAL;
713 
714 	info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
715 	if (!info)
716 		return -ENOMEM;
717 
718 	/* Constrain board-specific capabilities according to what
719 	 * this driver and the chip itself can actually do.
720 	 */
721 	c = &initdata->constraints;
722 	c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
723 	c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
724 				| REGULATOR_CHANGE_MODE
725 				| REGULATOR_CHANGE_STATUS;
726 
727 	switch (id) {
728 	case TWL6032_REG_SMPS3:
729 		if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
730 			info->flags |= SMPS_EXTENDED_EN;
731 		if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
732 			info->flags |= SMPS_OFFSET_EN;
733 		break;
734 	case TWL6032_REG_SMPS4:
735 		if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
736 			info->flags |= SMPS_EXTENDED_EN;
737 		if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
738 			info->flags |= SMPS_OFFSET_EN;
739 		break;
740 	case TWL6032_REG_VIO:
741 		if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
742 			info->flags |= SMPS_EXTENDED_EN;
743 		if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
744 			info->flags |= SMPS_OFFSET_EN;
745 		break;
746 	}
747 
748 	config.dev = &pdev->dev;
749 	config.init_data = initdata;
750 	config.driver_data = info;
751 	config.of_node = pdev->dev.of_node;
752 
753 	rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
754 	if (IS_ERR(rdev)) {
755 		dev_err(&pdev->dev, "can't register %s, %ld\n",
756 				info->desc.name, PTR_ERR(rdev));
757 		return PTR_ERR(rdev);
758 	}
759 	platform_set_drvdata(pdev, rdev);
760 
761 	/* NOTE:  many regulators support short-circuit IRQs (presentable
762 	 * as REGULATOR_OVER_CURRENT notifications?) configured via:
763 	 *  - SC_CONFIG
764 	 *  - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
765 	 *  - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
766 	 *  - IT_CONFIG
767 	 */
768 
769 	return 0;
770 }
771 
772 MODULE_ALIAS("platform:twl6030_reg");
773 
774 static struct platform_driver twlreg_driver = {
775 	.probe		= twlreg_probe,
776 	/* NOTE: short name, to work around driver model truncation of
777 	 * "twl_regulator.12" (and friends) to "twl_regulator.1".
778 	 */
779 	.driver  = {
780 		.name  = "twl6030_reg",
781 		.of_match_table = of_match_ptr(twl_of_match),
782 	},
783 };
784 
twlreg_init(void)785 static int __init twlreg_init(void)
786 {
787 	return platform_driver_register(&twlreg_driver);
788 }
789 subsys_initcall(twlreg_init);
790 
twlreg_exit(void)791 static void __exit twlreg_exit(void)
792 {
793 	platform_driver_unregister(&twlreg_driver);
794 }
795 module_exit(twlreg_exit)
796 
797 MODULE_DESCRIPTION("TWL6030 regulator driver");
798 MODULE_LICENSE("GPL");
799