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