1 // SPDX-License-Identifier: GPL-2.0-only
3 * Rockchip IO Voltage Domain driver
5 * Copyright 2014 MundoReader S.L.
6 * Copyright 2014 Google, Inc.
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/mfd/syscon.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/regulator/consumer.h>
18 #define MAX_SUPPLIES 16
21 * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
22 * "Recommended Operating Conditions" for "Digital GPIO". When the typical
23 * is 3.3V the max is 3.6V. When the typical is 1.8V the max is 1.98V.
25 * They are used like this:
26 * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
28 * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
29 * that to be an error.
31 #define MAX_VOLTAGE_1_8 1980000
32 #define MAX_VOLTAGE_3_3 3600000
34 #define PX30_IO_VSEL 0x180
35 #define PX30_IO_VSEL_VCCIO6_SRC BIT(0)
36 #define PX30_IO_VSEL_VCCIO6_SUPPLY_NUM 1
38 #define RK3288_SOC_CON2 0x24c
39 #define RK3288_SOC_CON2_FLASH0 BIT(7)
40 #define RK3288_SOC_FLASH_SUPPLY_NUM 2
42 #define RK3328_SOC_CON4 0x410
43 #define RK3328_SOC_CON4_VCCIO2 BIT(7)
44 #define RK3328_SOC_VCCIO2_SUPPLY_NUM 1
46 #define RK3368_SOC_CON15 0x43c
47 #define RK3368_SOC_CON15_FLASH0 BIT(14)
48 #define RK3368_SOC_FLASH_SUPPLY_NUM 2
50 #define RK3399_PMUGRF_CON0 0x180
51 #define RK3399_PMUGRF_CON0_VSEL BIT(8)
52 #define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
54 #define RK3568_PMU_GRF_IO_VSEL0 (0x0140)
55 #define RK3568_PMU_GRF_IO_VSEL1 (0x0144)
56 #define RK3568_PMU_GRF_IO_VSEL2 (0x0148)
58 struct rockchip_iodomain;
60 struct rockchip_iodomain_supply {
61 struct rockchip_iodomain *iod;
62 struct regulator *reg;
63 struct notifier_block nb;
67 struct rockchip_iodomain_soc_data {
69 const char *supply_names[MAX_SUPPLIES];
70 void (*init)(struct rockchip_iodomain *iod);
71 int (*write)(struct rockchip_iodomain_supply *supply, int uV);
74 struct rockchip_iodomain {
77 const struct rockchip_iodomain_soc_data *soc_data;
78 struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
79 int (*write)(struct rockchip_iodomain_supply *supply, int uV);
82 static int rk3568_iodomain_write(struct rockchip_iodomain_supply *supply, int uV)
84 struct rockchip_iodomain *iod = supply->iod;
85 u32 is_3v3 = uV > MAX_VOLTAGE_1_8;
89 switch (supply->idx) {
94 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
96 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
98 regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL2, val0);
99 regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL2, val1);
110 val0 = BIT(16 + b) | (is_3v3 ? 0 : BIT(b));
111 val1 = BIT(16 + b) | (is_3v3 ? BIT(b) : 0);
113 regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL0, val0);
114 regmap_write(iod->grf, RK3568_PMU_GRF_IO_VSEL1, val1);
123 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
126 struct rockchip_iodomain *iod = supply->iod;
131 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
134 /* apply hiword-mask */
135 val |= (BIT(supply->idx) << 16);
137 ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
139 dev_err(iod->dev, "Couldn't write to GRF\n");
144 static int rockchip_iodomain_notify(struct notifier_block *nb,
148 struct rockchip_iodomain_supply *supply =
149 container_of(nb, struct rockchip_iodomain_supply, nb);
154 * According to Rockchip it's important to keep the SoC IO domain
155 * higher than (or equal to) the external voltage. That means we need
156 * to change it before external voltage changes happen in the case
159 * Note that in the "pre" change we pick the max possible voltage that
160 * the regulator might end up at (the client requests a range and we
161 * don't know for certain the exact voltage). Right now we rely on the
162 * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
163 * request something like a max of 3.6V when they really want 3.3V.
164 * We could attempt to come up with better rules if this fails.
166 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
167 struct pre_voltage_change_data *pvc_data = data;
169 uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
170 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
171 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
172 uV = (unsigned long)data;
177 dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
179 if (uV > MAX_VOLTAGE_3_3) {
180 dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
182 if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
186 ret = supply->iod->write(supply, uV);
187 if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
190 dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
194 static void px30_iodomain_init(struct rockchip_iodomain *iod)
199 /* if no VCCIO6 supply we should leave things alone */
200 if (!iod->supplies[PX30_IO_VSEL_VCCIO6_SUPPLY_NUM].reg)
204 * set vccio6 iodomain to also use this framework
205 * instead of a special gpio.
207 val = PX30_IO_VSEL_VCCIO6_SRC | (PX30_IO_VSEL_VCCIO6_SRC << 16);
208 ret = regmap_write(iod->grf, PX30_IO_VSEL, val);
210 dev_warn(iod->dev, "couldn't update vccio6 ctrl\n");
213 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
218 /* if no flash supply we should leave things alone */
219 if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
223 * set flash0 iodomain to also use this framework
224 * instead of a special gpio.
226 val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
227 ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
229 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
232 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
237 /* if no vccio2 supply we should leave things alone */
238 if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
242 * set vccio2 iodomain to also use this framework
243 * instead of a special gpio.
245 val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
246 ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
248 dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
251 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
256 /* if no flash supply we should leave things alone */
257 if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
261 * set flash0 iodomain to also use this framework
262 * instead of a special gpio.
264 val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
265 ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
267 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
270 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
275 /* if no pmu io supply we should leave things alone */
276 if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
280 * set pmu io iodomain to also use this framework
281 * instead of a special gpio.
283 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
284 ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
286 dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
289 static const struct rockchip_iodomain_soc_data soc_data_px30 = {
301 .init = px30_iodomain_init,
304 static const struct rockchip_iodomain_soc_data soc_data_px30_pmu = {
327 * On the rk3188 the io-domains are handled by a shared register with the
328 * lower 8 bits being still being continuing drive-strength settings.
330 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
352 static const struct rockchip_iodomain_soc_data soc_data_rk3228 = {
362 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
365 "lcdc", /* LCDC_VDD */
366 "dvp", /* DVPIO_VDD */
367 "flash0", /* FLASH0_VDD (emmc) */
368 "flash1", /* FLASH1_VDD (sdio1) */
369 "wifi", /* APIO3_VDD (sdio0) */
370 "bb", /* APIO5_VDD */
371 "audio", /* APIO4_VDD */
372 "sdcard", /* SDMMC0_VDD (sdmmc) */
373 "gpio30", /* APIO1_VDD */
374 "gpio1830", /* APIO2_VDD */
376 .init = rk3288_iodomain_init,
379 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
390 .init = rk3328_iodomain_init,
393 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
397 "dvp", /* DVPIO_VDD */
398 "flash0", /* FLASH0_VDD (emmc) */
399 "wifi", /* APIO2_VDD (sdio0) */
401 "audio", /* APIO3_VDD */
402 "sdcard", /* SDMMC0_VDD (sdmmc) */
403 "gpio30", /* APIO1_VDD */
404 "gpio1830", /* APIO4_VDD (gpujtag) */
406 .init = rk3368_iodomain_init,
409 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
416 "pmu", /*PMU IO domain*/
417 "vop", /*LCDC IO domain*/
421 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
422 .grf_offset = 0xe640,
424 "bt656", /* APIO2_VDD */
425 "audio", /* APIO5_VDD */
426 "sdmmc", /* SDMMC0_VDD */
427 "gpio1830", /* APIO4_VDD */
431 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
443 "pmu1830", /* PMUIO2_VDD */
445 .init = rk3399_pmu_iodomain_init,
448 static const struct rockchip_iodomain_soc_data soc_data_rk3568_pmu = {
461 .write = rk3568_iodomain_write,
464 static const struct rockchip_iodomain_soc_data soc_data_rv1108 = {
487 static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu = {
494 static const struct rockchip_iodomain_soc_data soc_data_rv1126_pmu = {
510 static const struct of_device_id rockchip_iodomain_match[] = {
512 .compatible = "rockchip,px30-io-voltage-domain",
513 .data = (void *)&soc_data_px30
516 .compatible = "rockchip,px30-pmu-io-voltage-domain",
517 .data = (void *)&soc_data_px30_pmu
520 .compatible = "rockchip,rk3188-io-voltage-domain",
521 .data = &soc_data_rk3188
524 .compatible = "rockchip,rk3228-io-voltage-domain",
525 .data = &soc_data_rk3228
528 .compatible = "rockchip,rk3288-io-voltage-domain",
529 .data = &soc_data_rk3288
532 .compatible = "rockchip,rk3328-io-voltage-domain",
533 .data = &soc_data_rk3328
536 .compatible = "rockchip,rk3368-io-voltage-domain",
537 .data = &soc_data_rk3368
540 .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
541 .data = &soc_data_rk3368_pmu
544 .compatible = "rockchip,rk3399-io-voltage-domain",
545 .data = &soc_data_rk3399
548 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
549 .data = &soc_data_rk3399_pmu
552 .compatible = "rockchip,rk3568-pmu-io-voltage-domain",
553 .data = &soc_data_rk3568_pmu
556 .compatible = "rockchip,rv1108-io-voltage-domain",
557 .data = &soc_data_rv1108
560 .compatible = "rockchip,rv1108-pmu-io-voltage-domain",
561 .data = &soc_data_rv1108_pmu
564 .compatible = "rockchip,rv1126-pmu-io-voltage-domain",
565 .data = &soc_data_rv1126_pmu
569 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
571 static int rockchip_iodomain_probe(struct platform_device *pdev)
573 struct device_node *np = pdev->dev.of_node;
574 const struct of_device_id *match;
575 struct rockchip_iodomain *iod;
576 struct device *parent;
582 iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
586 iod->dev = &pdev->dev;
587 platform_set_drvdata(pdev, iod);
589 match = of_match_node(rockchip_iodomain_match, np);
590 iod->soc_data = match->data;
592 if (iod->soc_data->write)
593 iod->write = iod->soc_data->write;
595 iod->write = rockchip_iodomain_write;
597 parent = pdev->dev.parent;
598 if (parent && parent->of_node) {
599 iod->grf = syscon_node_to_regmap(parent->of_node);
601 dev_dbg(&pdev->dev, "falling back to old binding\n");
602 iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
605 if (IS_ERR(iod->grf)) {
606 dev_err(&pdev->dev, "couldn't find grf regmap\n");
607 return PTR_ERR(iod->grf);
610 for (i = 0; i < MAX_SUPPLIES; i++) {
611 const char *supply_name = iod->soc_data->supply_names[i];
612 struct rockchip_iodomain_supply *supply = &iod->supplies[i];
613 struct regulator *reg;
619 reg = devm_regulator_get_optional(iod->dev, supply_name);
623 /* If a supply wasn't specified, that's OK */
626 else if (ret != -EPROBE_DEFER)
627 dev_err(iod->dev, "couldn't get regulator %s\n",
632 /* set initial correct value */
633 uV = regulator_get_voltage(reg);
635 /* must be a regulator we can get the voltage of */
637 dev_err(iod->dev, "Can't determine voltage: %s\n",
643 if (uV > MAX_VOLTAGE_3_3) {
645 "%d uV is too high. May damage SoC!\n",
651 /* setup our supply */
655 supply->nb.notifier_call = rockchip_iodomain_notify;
657 ret = iod->write(supply, uV);
663 /* register regulator notifier */
664 ret = regulator_register_notifier(reg, &supply->nb);
667 "regulator notifier request failed\n");
673 if (iod->soc_data->init)
674 iod->soc_data->init(iod);
679 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
680 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
683 regulator_unregister_notifier(io_supply->reg,
690 static int rockchip_iodomain_remove(struct platform_device *pdev)
692 struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
695 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
696 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
699 regulator_unregister_notifier(io_supply->reg,
706 static struct platform_driver rockchip_iodomain_driver = {
707 .probe = rockchip_iodomain_probe,
708 .remove = rockchip_iodomain_remove,
710 .name = "rockchip-iodomain",
711 .of_match_table = rockchip_iodomain_match,
715 module_platform_driver(rockchip_iodomain_driver);
717 MODULE_DESCRIPTION("Rockchip IO-domain driver");
720 MODULE_LICENSE("GPL v2");