xref: /OK3568_Linux_fs/kernel/drivers/soc/rockchip/io-domain.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Rockchip IO Voltage Domain driver
4  *
5  * Copyright 2014 MundoReader S.L.
6  * Copyright 2014 Google, Inc.
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/of.h>
18 #include <linux/of_device.h>
19 #include <linux/regulator/of_regulator.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22 #include <linux/rockchip/cpu.h>
23 #include "../../regulator/internal.h"
24 
25 #define MAX_SUPPLIES		16
26 
27 /*
28  * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
29  * "Recommended Operating Conditions" for "Digital GPIO".   When the typical
30  * is 3.3V the max is 3.6V.  When the typical is 1.8V the max is 1.98V.
31  *
32  * They are used like this:
33  * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
34  *   SoC we're at 3.3.
35  * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
36  *   that to be an error.
37  */
38 #define MAX_VOLTAGE_1_8		1980000
39 #define MAX_VOLTAGE_3_3		3600000
40 
41 #define PX30_IO_VSEL			0x180
42 #define PX30_IO_VSEL_VCCIO6_SRC		BIT(0)
43 #define PX30_IO_VSEL_VCCIO6_SUPPLY_NUM	1
44 
45 #define RK3288_SOC_CON2			0x24c
46 #define RK3288_SOC_CON2_FLASH0		BIT(7)
47 #define RK3288_SOC_FLASH_SUPPLY_NUM	2
48 
49 #define RK3308_SOC_CON0			0x300
50 #define RK3308_SOC_CON0_VCCIO3		BIT(8)
51 #define RK3308_SOC_VCCIO3_SUPPLY_NUM	3
52 
53 #define RK3328_SOC_CON4			0x410
54 #define RK3328_SOC_CON4_VCCIO2		BIT(7)
55 #define RK3328_SOC_VCCIO2_SUPPLY_NUM	1
56 
57 #define RK3368_SOC_CON15		0x43c
58 #define RK3368_SOC_CON15_FLASH0		BIT(14)
59 #define RK3368_SOC_FLASH_SUPPLY_NUM	2
60 
61 #define RK3399_PMUGRF_CON0		0x180
62 #define RK3399_PMUGRF_CON0_VSEL		BIT(8)
63 #define RK3399_PMUGRF_VSEL_SUPPLY_NUM	9
64 
65 #define RK3568_PMU_GRF_IO_VSEL0		(0x0140)
66 #define RK3568_PMU_GRF_IO_VSEL1		(0x0144)
67 #define RK3568_PMU_GRF_IO_VSEL2		(0x0148)
68 
69 struct rockchip_iodomain;
70 
71 struct rockchip_iodomain_supply {
72 	struct rockchip_iodomain *iod;
73 	struct regulator *reg;
74 	struct notifier_block nb;
75 	int idx;
76 };
77 
78 struct rockchip_iodomain_soc_data {
79 	int grf_offset;
80 	const char *supply_names[MAX_SUPPLIES];
81 	void (*init)(struct rockchip_iodomain *iod);
82 	int (*write)(struct rockchip_iodomain_supply *supply, int uV);
83 };
84 
85 struct rockchip_iodomain {
86 	struct device *dev;
87 	struct regmap *grf;
88 	const struct rockchip_iodomain_soc_data *soc_data;
89 	struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
90 	int (*write)(struct rockchip_iodomain_supply *supply, int uV);
91 };
92 
rk3568_iodomain_write(struct rockchip_iodomain_supply * supply,int uV)93 static int rk3568_iodomain_write(struct rockchip_iodomain_supply *supply, int uV)
94 {
95 	struct rockchip_iodomain *iod = supply->iod;
96 	u32 is_3v3 = uV > MAX_VOLTAGE_1_8;
97 	u32 val0, val1;
98 	int b;
99 
100 	switch (supply->idx) {
101 	case 0: /* pmuio1 */
102 		break;
103 	case 1: /* pmuio2 */
104 		b = supply->idx;
105 		val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
106 		b = supply->idx + 4;
107 		val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
108 
109 		regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL2, val0);
110 		regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL2, val1);
111 		break;
112 	case 3: /* vccio2 */
113 		break;
114 	case 2: /* vccio1 */
115 	case 4: /* vccio3 */
116 	case 5: /* vccio4 */
117 	case 6: /* vccio5 */
118 	case 7: /* vccio6 */
119 	case 8: /* vccio7 */
120 		b = supply->idx - 1;
121 		val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
122 		val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
123 
124 		regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL0, val0);
125 		regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL1, val1);
126 		break;
127 	default:
128 		return -EINVAL;
129 	};
130 
131 	return 0;
132 }
133 
rockchip_iodomain_write(struct rockchip_iodomain_supply * supply,int uV)134 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
135 				   int uV)
136 {
137 	struct rockchip_iodomain *iod = supply->iod;
138 	u32 val;
139 	int ret;
140 
141 	/* set value bit */
142 	val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
143 	val <<= supply->idx;
144 
145 	/* apply hiword-mask */
146 	val |= (BIT(supply->idx) << 16);
147 
148 	ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
149 	if (ret)
150 		dev_err(iod->dev, "Couldn't write to GRF\n");
151 
152 	return ret;
153 }
154 
rockchip_iodomain_notify(struct notifier_block * nb,unsigned long event,void * data)155 static int rockchip_iodomain_notify(struct notifier_block *nb,
156 				    unsigned long event,
157 				    void *data)
158 {
159 	struct rockchip_iodomain_supply *supply =
160 			container_of(nb, struct rockchip_iodomain_supply, nb);
161 	int uV;
162 	int ret;
163 
164 	/*
165 	 * According to Rockchip it's important to keep the SoC IO domain
166 	 * higher than (or equal to) the external voltage.  That means we need
167 	 * to change it before external voltage changes happen in the case
168 	 * of an increase.
169 	 *
170 	 * Note that in the "pre" change we pick the max possible voltage that
171 	 * the regulator might end up at (the client requests a range and we
172 	 * don't know for certain the exact voltage).  Right now we rely on the
173 	 * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
174 	 * request something like a max of 3.6V when they really want 3.3V.
175 	 * We could attempt to come up with better rules if this fails.
176 	 */
177 	if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
178 		struct pre_voltage_change_data *pvc_data = data;
179 
180 		uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
181 	} else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
182 			    REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
183 		uV = (unsigned long)data;
184 	} else {
185 		return NOTIFY_OK;
186 	}
187 
188 	dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
189 
190 	if (uV > MAX_VOLTAGE_3_3) {
191 		dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
192 
193 		if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
194 			return NOTIFY_BAD;
195 	}
196 
197 	ret = supply->iod->write(supply, uV);
198 	if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
199 		return NOTIFY_BAD;
200 
201 	dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
202 	return NOTIFY_OK;
203 }
204 
px30_iodomain_init(struct rockchip_iodomain * iod)205 static void px30_iodomain_init(struct rockchip_iodomain *iod)
206 {
207 	int ret;
208 	u32 val;
209 
210 	/* if no VCCIO6 supply we should leave things alone */
211 	if (!iod->supplies[PX30_IO_VSEL_VCCIO6_SUPPLY_NUM].reg)
212 		return;
213 
214 	/*
215 	 * set vccio6 iodomain to also use this framework
216 	 * instead of a special gpio.
217 	 */
218 	val = PX30_IO_VSEL_VCCIO6_SRC | (PX30_IO_VSEL_VCCIO6_SRC << 16);
219 	ret = regmap_write(iod->grf, PX30_IO_VSEL, val);
220 	if (ret < 0)
221 		dev_warn(iod->dev, "couldn't update vccio6 ctrl\n");
222 }
223 
rk3288_iodomain_init(struct rockchip_iodomain * iod)224 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
225 {
226 	int ret;
227 	u32 val;
228 
229 	/* if no flash supply we should leave things alone */
230 	if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
231 		return;
232 
233 	/*
234 	 * set flash0 iodomain to also use this framework
235 	 * instead of a special gpio.
236 	 */
237 	val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
238 	ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
239 	if (ret < 0)
240 		dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
241 }
242 
rk3308_iodomain_init(struct rockchip_iodomain * iod)243 static void rk3308_iodomain_init(struct rockchip_iodomain *iod)
244 {
245 	int ret;
246 	u32 val;
247 
248 	/* if no vccio3 supply we should leave things alone */
249 	if (!iod->supplies[RK3308_SOC_VCCIO3_SUPPLY_NUM].reg)
250 		return;
251 
252 	/*
253 	 * set vccio3 iodomain to also use this framework
254 	 * instead of a special gpio.
255 	 */
256 	val = RK3308_SOC_CON0_VCCIO3 | (RK3308_SOC_CON0_VCCIO3 << 16);
257 	ret = regmap_write(iod->grf, RK3308_SOC_CON0, val);
258 	if (ret < 0)
259 		dev_warn(iod->dev, "couldn't update vccio3 vsel ctrl\n");
260 }
261 
rk3328_iodomain_init(struct rockchip_iodomain * iod)262 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
263 {
264 	int ret;
265 	u32 val;
266 
267 	/* if no vccio2 supply we should leave things alone */
268 	if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
269 		return;
270 
271 	/*
272 	 * set vccio2 iodomain to also use this framework
273 	 * instead of a special gpio.
274 	 */
275 	val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
276 	ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
277 	if (ret < 0)
278 		dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
279 }
280 
rk3368_iodomain_init(struct rockchip_iodomain * iod)281 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
282 {
283 	int ret;
284 	u32 val;
285 
286 	/* if no flash supply we should leave things alone */
287 	if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
288 		return;
289 
290 	/*
291 	 * set flash0 iodomain to also use this framework
292 	 * instead of a special gpio.
293 	 */
294 	val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
295 	ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
296 	if (ret < 0)
297 		dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
298 }
299 
rk3399_pmu_iodomain_init(struct rockchip_iodomain * iod)300 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
301 {
302 	int ret;
303 	u32 val;
304 
305 	/* if no pmu io supply we should leave things alone */
306 	if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
307 		return;
308 
309 	/*
310 	 * set pmu io iodomain to also use this framework
311 	 * instead of a special gpio.
312 	 */
313 	val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
314 	ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
315 	if (ret < 0)
316 		dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
317 }
318 
319 static const struct rockchip_iodomain_soc_data soc_data_px30 = {
320 	.grf_offset = 0x180,
321 	.supply_names = {
322 		NULL,
323 		"vccio6",
324 		"vccio1",
325 		"vccio2",
326 		"vccio3",
327 		"vccio4",
328 		"vccio5",
329 		"vccio-oscgpi",
330 	},
331 	.init = px30_iodomain_init,
332 };
333 
334 static const struct rockchip_iodomain_soc_data soc_data_px30_pmu = {
335 	.grf_offset = 0x100,
336 	.supply_names = {
337 		NULL,
338 		NULL,
339 		NULL,
340 		NULL,
341 		NULL,
342 		NULL,
343 		NULL,
344 		NULL,
345 		NULL,
346 		NULL,
347 		NULL,
348 		NULL,
349 		NULL,
350 		NULL,
351 		"pmuio1",
352 		"pmuio2",
353 	},
354 };
355 
356 /*
357  * On the rk3188 the io-domains are handled by a shared register with the
358  * lower 8 bits being still being continuing drive-strength settings.
359  */
360 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
361 	.grf_offset = 0x104,
362 	.supply_names = {
363 		NULL,
364 		NULL,
365 		NULL,
366 		NULL,
367 		NULL,
368 		NULL,
369 		NULL,
370 		NULL,
371 		"ap0",
372 		"ap1",
373 		"cif",
374 		"flash",
375 		"vccio0",
376 		"vccio1",
377 		"lcdc0",
378 		"lcdc1",
379 	},
380 };
381 
382 static const struct rockchip_iodomain_soc_data soc_data_rk3228 = {
383 	.grf_offset = 0x418,
384 	.supply_names = {
385 		"vccio1",
386 		"vccio2",
387 		"vccio3",
388 		"vccio4",
389 	},
390 };
391 
392 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
393 	.grf_offset = 0x380,
394 	.supply_names = {
395 		"lcdc",		/* LCDC_VDD */
396 		"dvp",		/* DVPIO_VDD */
397 		"flash0",	/* FLASH0_VDD (emmc) */
398 		"flash1",	/* FLASH1_VDD (sdio1) */
399 		"wifi",		/* APIO3_VDD  (sdio0) */
400 		"bb",		/* APIO5_VDD */
401 		"audio",	/* APIO4_VDD */
402 		"sdcard",	/* SDMMC0_VDD (sdmmc) */
403 		"gpio30",	/* APIO1_VDD */
404 		"gpio1830",	/* APIO2_VDD */
405 	},
406 	.init = rk3288_iodomain_init,
407 };
408 
409 static const struct rockchip_iodomain_soc_data soc_data_rk3308 = {
410 	.grf_offset = 0x300,
411 	.supply_names = {
412 		"vccio0",
413 		"vccio1",
414 		"vccio2",
415 		"vccio3",
416 		"vccio4",
417 		"vccio5",
418 	},
419 	.init = rk3308_iodomain_init,
420 };
421 
422 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
423 	.grf_offset = 0x410,
424 	.supply_names = {
425 		"vccio1",
426 		"vccio2",
427 		"vccio3",
428 		"vccio4",
429 		"vccio5",
430 		"vccio6",
431 		"pmuio",
432 	},
433 	.init = rk3328_iodomain_init,
434 };
435 
436 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
437 	.grf_offset = 0x900,
438 	.supply_names = {
439 		NULL,		/* reserved */
440 		"dvp",		/* DVPIO_VDD */
441 		"flash0",	/* FLASH0_VDD (emmc) */
442 		"wifi",		/* APIO2_VDD (sdio0) */
443 		NULL,
444 		"audio",	/* APIO3_VDD */
445 		"sdcard",	/* SDMMC0_VDD (sdmmc) */
446 		"gpio30",	/* APIO1_VDD */
447 		"gpio1830",	/* APIO4_VDD (gpujtag) */
448 	},
449 	.init = rk3368_iodomain_init,
450 };
451 
452 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
453 	.grf_offset = 0x100,
454 	.supply_names = {
455 		NULL,
456 		NULL,
457 		NULL,
458 		NULL,
459 		"pmu",	        /*PMU IO domain*/
460 		"vop",	        /*LCDC IO domain*/
461 	},
462 };
463 
464 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
465 	.grf_offset = 0xe640,
466 	.supply_names = {
467 		"bt656",		/* APIO2_VDD */
468 		"audio",		/* APIO5_VDD */
469 		"sdmmc",		/* SDMMC0_VDD */
470 		"gpio1830",		/* APIO4_VDD */
471 	},
472 };
473 
474 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
475 	.grf_offset = 0x180,
476 	.supply_names = {
477 		NULL,
478 		NULL,
479 		NULL,
480 		NULL,
481 		NULL,
482 		NULL,
483 		NULL,
484 		NULL,
485 		NULL,
486 		"pmu1830",		/* PMUIO2_VDD */
487 	},
488 	.init = rk3399_pmu_iodomain_init,
489 };
490 
491 static const struct rockchip_iodomain_soc_data soc_data_rk3568_pmu = {
492 	.grf_offset = 0x140,
493 	.supply_names = {
494 		"pmuio1",
495 		"pmuio2",
496 		"vccio1",
497 		"vccio2",
498 		"vccio3",
499 		"vccio4",
500 		"vccio5",
501 		"vccio6",
502 		"vccio7",
503 	},
504 	.write = rk3568_iodomain_write,
505 };
506 
507 static const struct rockchip_iodomain_soc_data soc_data_rv1108 = {
508 	.grf_offset = 0x404,
509 	.supply_names = {
510 		NULL,
511 		NULL,
512 		NULL,
513 		NULL,
514 		NULL,
515 		NULL,
516 		NULL,
517 		NULL,
518 		NULL,
519 		NULL,
520 		NULL,
521 		"vccio1",
522 		"vccio2",
523 		"vccio3",
524 		"vccio5",
525 		"vccio6",
526 	},
527 
528 };
529 
530 static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu = {
531 	.grf_offset = 0x104,
532 	.supply_names = {
533 		"pmu",
534 	},
535 };
536 
537 static const struct rockchip_iodomain_soc_data soc_data_rv1126_pmu = {
538 	.grf_offset = 0x140,
539 	.supply_names = {
540 		NULL,
541 		"vccio1",
542 		"vccio2",
543 		"vccio3",
544 		"vccio4",
545 		"vccio5",
546 		"vccio6",
547 		"vccio7",
548 		"pmuio0",
549 		"pmuio1",
550 	},
551 };
552 
553 static const struct of_device_id rockchip_iodomain_match[] = {
554 #ifdef CONFIG_CPU_PX30
555 	{
556 		.compatible = "rockchip,px30-io-voltage-domain",
557 		.data = (void *)&soc_data_px30
558 	},
559 	{
560 		.compatible = "rockchip,px30-pmu-io-voltage-domain",
561 		.data = (void *)&soc_data_px30_pmu
562 	},
563 #endif
564 #ifdef CONFIG_CPU_RK3188
565 	{
566 		.compatible = "rockchip,rk3188-io-voltage-domain",
567 		.data = &soc_data_rk3188
568 	},
569 #endif
570 #ifdef CONFIG_CPU_RK322X
571 	{
572 		.compatible = "rockchip,rk3228-io-voltage-domain",
573 		.data = &soc_data_rk3228
574 	},
575 #endif
576 #ifdef CONFIG_CPU_RK3288
577 	{
578 		.compatible = "rockchip,rk3288-io-voltage-domain",
579 		.data = &soc_data_rk3288
580 	},
581 #endif
582 #ifdef CONFIG_CPU_RK3308
583 	{
584 		.compatible = "rockchip,rk3308-io-voltage-domain",
585 		.data = &soc_data_rk3308
586 	},
587 #endif
588 #ifdef CONFIG_CPU_RK3328
589 	{
590 		.compatible = "rockchip,rk3328-io-voltage-domain",
591 		.data = &soc_data_rk3328
592 	},
593 #endif
594 #ifdef CONFIG_CPU_RK3368
595 	{
596 		.compatible = "rockchip,rk3368-io-voltage-domain",
597 		.data = &soc_data_rk3368
598 	},
599 	{
600 		.compatible = "rockchip,rk3368-pmu-io-voltage-domain",
601 		.data = &soc_data_rk3368_pmu
602 	},
603 #endif
604 #ifdef CONFIG_CPU_RK3399
605 	{
606 		.compatible = "rockchip,rk3399-io-voltage-domain",
607 		.data = &soc_data_rk3399
608 	},
609 	{
610 		.compatible = "rockchip,rk3399-pmu-io-voltage-domain",
611 		.data = &soc_data_rk3399_pmu
612 	},
613 #endif
614 #ifdef CONFIG_CPU_RK3568
615 	{
616 		.compatible = "rockchip,rk3568-pmu-io-voltage-domain",
617 		.data = &soc_data_rk3568_pmu
618 	},
619 #endif
620 #ifdef CONFIG_CPU_RV1108
621 	{
622 		.compatible = "rockchip,rv1108-io-voltage-domain",
623 		.data = &soc_data_rv1108
624 	},
625 	{
626 		.compatible = "rockchip,rv1108-pmu-io-voltage-domain",
627 		.data = &soc_data_rv1108_pmu
628 	},
629 #endif
630 #ifdef CONFIG_CPU_RV1126
631 	{
632 		.compatible = "rockchip,rv1126-pmu-io-voltage-domain",
633 		.data = &soc_data_rv1126_pmu
634 	},
635 #endif
636 	{ /* sentinel */ },
637 };
638 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
639 
640 #ifndef MODULE
rdev_get_name(struct regulator_dev * rdev)641 static const char *rdev_get_name(struct regulator_dev *rdev)
642 {
643 	if (rdev->constraints && rdev->constraints->name)
644 		return rdev->constraints->name;
645 	else if (rdev->desc->name)
646 		return rdev->desc->name;
647 	else
648 		return "";
649 }
650 
of_get_child_regulator(struct device_node * parent,const char * prop_name)651 static struct device_node *of_get_child_regulator(struct device_node *parent,
652 						  const char *prop_name)
653 {
654 	struct device_node *regnode = NULL;
655 	struct device_node *child = NULL;
656 
657 	for_each_child_of_node(parent, child) {
658 		regnode = of_parse_phandle(child, prop_name, 0);
659 
660 		if (!regnode) {
661 			regnode = of_get_child_regulator(child, prop_name);
662 			if (regnode)
663 				return regnode;
664 		} else {
665 			return regnode;
666 		}
667 	}
668 	return NULL;
669 }
670 
of_get_regulator(struct device * dev,const char * supply)671 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
672 {
673 	struct device_node *regnode = NULL;
674 	char prop_name[256];
675 
676 	dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
677 
678 	snprintf(prop_name, sizeof(prop_name), "%s-supply", supply);
679 	regnode = of_parse_phandle(dev->of_node, prop_name, 0);
680 
681 	if (!regnode) {
682 		regnode = of_get_child_regulator(dev->of_node, prop_name);
683 		if (regnode)
684 			return regnode;
685 
686 		dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
687 				prop_name, dev->of_node);
688 		return NULL;
689 	}
690 	return regnode;
691 }
692 
rockchip_iodomain_dump(const struct platform_device * pdev,struct rockchip_iodomain_supply * supply)693 static void rockchip_iodomain_dump(const struct platform_device *pdev,
694 				   struct rockchip_iodomain_supply *supply)
695 {
696 	struct rockchip_iodomain *iod = supply->iod;
697 	const char *name = iod->soc_data->supply_names[supply->idx];
698 	struct device *dev = iod->dev;
699 	struct device_node *node;
700 	struct regulator_dev *r = NULL;
701 
702 	node = of_get_regulator(dev, name);
703 	if (node) {
704 		r = of_find_regulator_by_node(node);
705 		if (!IS_ERR_OR_NULL(r))
706 			dev_info(&pdev->dev, "%s(%d uV) supplied by %s\n",
707 				name, regulator_get_voltage(supply->reg),
708 				rdev_get_name(r));
709 	}
710 }
711 #else
712 static inline void
rockchip_iodomain_dump(const struct platform_device * pdev,struct rockchip_iodomain_supply * supply)713 rockchip_iodomain_dump(const struct platform_device *pdev,
714 		       struct rockchip_iodomain_supply *supply)
715 {
716 }
717 #endif
718 
rv1126_iodomain_notify(struct notifier_block * nb,unsigned long event,void * data)719 static int rv1126_iodomain_notify(struct notifier_block *nb,
720 				  unsigned long event,
721 				  void *data)
722 {
723 	struct rockchip_iodomain_supply *supply =
724 			container_of(nb, struct rockchip_iodomain_supply, nb);
725 	int uV;
726 	int ret;
727 
728 	if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
729 		struct pre_voltage_change_data *pvc_data = data;
730 
731 		uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
732 	} else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
733 			    REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
734 		uV = (unsigned long)data;
735 	} else if (event & REGULATOR_EVENT_DISABLE) {
736 		uV = MAX_VOLTAGE_3_3;
737 	} else if (event & REGULATOR_EVENT_ENABLE) {
738 		if (!data)
739 			return NOTIFY_BAD;
740 
741 		uV = (unsigned long)data;
742 	} else {
743 		return NOTIFY_OK;
744 	}
745 
746 	if (uV <= 0) {
747 		dev_err(supply->iod->dev, "Voltage invalid: %d\n", uV);
748 		return NOTIFY_BAD;
749 	}
750 
751 	dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
752 
753 	if (uV > MAX_VOLTAGE_3_3) {
754 		dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
755 
756 		if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
757 			return NOTIFY_BAD;
758 	}
759 
760 	ret = supply->iod->write(supply, uV);
761 	if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
762 		return NOTIFY_BAD;
763 
764 	dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
765 	return NOTIFY_OK;
766 }
767 
rockchip_iodomain_probe(struct platform_device * pdev)768 static int rockchip_iodomain_probe(struct platform_device *pdev)
769 {
770 	struct device_node *np = pdev->dev.of_node;
771 	const struct of_device_id *match;
772 	struct rockchip_iodomain *iod;
773 	struct device *parent;
774 	int i, ret = 0;
775 
776 	if (!np)
777 		return -ENODEV;
778 
779 	iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
780 	if (!iod)
781 		return -ENOMEM;
782 
783 	iod->dev = &pdev->dev;
784 	platform_set_drvdata(pdev, iod);
785 
786 	match = of_match_node(rockchip_iodomain_match, np);
787 	iod->soc_data = match->data;
788 
789 	if (iod->soc_data->write)
790 		iod->write = iod->soc_data->write;
791 	else
792 		iod->write = rockchip_iodomain_write;
793 
794 	parent = pdev->dev.parent;
795 	if (parent && parent->of_node) {
796 		iod->grf = syscon_node_to_regmap(parent->of_node);
797 	} else {
798 		dev_dbg(&pdev->dev, "falling back to old binding\n");
799 		iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
800 	}
801 
802 	if (IS_ERR(iod->grf)) {
803 		dev_err(&pdev->dev, "couldn't find grf regmap\n");
804 		return PTR_ERR(iod->grf);
805 	}
806 
807 	for (i = 0; i < MAX_SUPPLIES; i++) {
808 		const char *supply_name = iod->soc_data->supply_names[i];
809 		struct rockchip_iodomain_supply *supply = &iod->supplies[i];
810 		struct regulator *reg;
811 		int uV;
812 
813 		if (!supply_name)
814 			continue;
815 
816 		/* PX30s pmuio1 not support 1v8 mode switch. */
817 		if (soc_is_px30s() && (!strcmp(supply_name, "pmuio1")))
818 			continue;
819 
820 		reg = devm_regulator_get_optional(iod->dev, supply_name);
821 		if (IS_ERR(reg)) {
822 			ret = PTR_ERR(reg);
823 
824 			/* If a supply wasn't specified, that's OK */
825 			if (ret == -ENODEV)
826 				continue;
827 			else if (ret != -EPROBE_DEFER)
828 				dev_err(iod->dev, "couldn't get regulator %s\n",
829 					supply_name);
830 			goto unreg_notify;
831 		}
832 
833 		/* set initial correct value */
834 		uV = regulator_get_voltage(reg);
835 
836 		/* must be a regulator we can get the voltage of */
837 		if (uV < 0) {
838 			dev_err(iod->dev, "Can't determine voltage: %s\n",
839 				supply_name);
840 			ret = uV;
841 			goto unreg_notify;
842 		}
843 
844 		if (uV > MAX_VOLTAGE_3_3) {
845 			dev_crit(iod->dev,
846 				 "%d uV is too high. May damage SoC!\n",
847 				 uV);
848 			ret = -EINVAL;
849 			goto unreg_notify;
850 		}
851 
852 		/* setup our supply */
853 		supply->idx = i;
854 		supply->iod = iod;
855 		supply->reg = reg;
856 		supply->nb.notifier_call = rockchip_iodomain_notify;
857 		if (IS_ENABLED(CONFIG_CPU_RV1126))
858 			supply->nb.notifier_call = rv1126_iodomain_notify;
859 
860 		ret = iod->write(supply, uV);
861 		if (ret) {
862 			supply->reg = NULL;
863 			goto unreg_notify;
864 		}
865 
866 		/* register regulator notifier */
867 		ret = regulator_register_notifier(reg, &supply->nb);
868 		if (ret) {
869 			dev_err(&pdev->dev,
870 				"regulator notifier request failed\n");
871 			supply->reg = NULL;
872 			goto unreg_notify;
873 		}
874 
875 		rockchip_iodomain_dump(pdev, supply);
876 	}
877 
878 	if (iod->soc_data->init)
879 		iod->soc_data->init(iod);
880 
881 	return 0;
882 
883 unreg_notify:
884 	for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
885 		struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
886 
887 		if (io_supply->reg)
888 			regulator_unregister_notifier(io_supply->reg,
889 						      &io_supply->nb);
890 	}
891 
892 	return ret;
893 }
894 
rockchip_iodomain_remove(struct platform_device * pdev)895 static int rockchip_iodomain_remove(struct platform_device *pdev)
896 {
897 	struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
898 	int i;
899 
900 	for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
901 		struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
902 
903 		if (io_supply->reg)
904 			regulator_unregister_notifier(io_supply->reg,
905 						      &io_supply->nb);
906 	}
907 
908 	return 0;
909 }
910 
911 static struct platform_driver rockchip_iodomain_driver = {
912 	.probe   = rockchip_iodomain_probe,
913 	.remove  = rockchip_iodomain_remove,
914 	.driver  = {
915 		.name  = "rockchip-iodomain",
916 		.of_match_table = rockchip_iodomain_match,
917 	},
918 };
919 
rockchip_iodomain_driver_init(void)920 static int __init rockchip_iodomain_driver_init(void)
921 {
922 	return platform_driver_register(&rockchip_iodomain_driver);
923 }
924 fs_initcall(rockchip_iodomain_driver_init);
925 
rockchip_iodomain_driver_exit(void)926 static void __exit rockchip_iodomain_driver_exit(void)
927 {
928 	platform_driver_unregister(&rockchip_iodomain_driver);
929 }
930 module_exit(rockchip_iodomain_driver_exit);
931 
932 MODULE_DESCRIPTION("Rockchip IO-domain driver");
933 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
934 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
935 MODULE_LICENSE("GPL v2");
936