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