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 struct rockchip_iodomain;
56 struct rockchip_iodomain_soc_data {
58 const char *supply_names[MAX_SUPPLIES];
59 void (*init)(struct rockchip_iodomain *iod);
62 struct rockchip_iodomain_supply {
63 struct rockchip_iodomain *iod;
64 struct regulator *reg;
65 struct notifier_block nb;
69 struct rockchip_iodomain {
72 const struct rockchip_iodomain_soc_data *soc_data;
73 struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
76 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
79 struct rockchip_iodomain *iod = supply->iod;
84 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
87 /* apply hiword-mask */
88 val |= (BIT(supply->idx) << 16);
90 ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
92 dev_err(iod->dev, "Couldn't write to GRF\n");
97 static int rockchip_iodomain_notify(struct notifier_block *nb,
101 struct rockchip_iodomain_supply *supply =
102 container_of(nb, struct rockchip_iodomain_supply, nb);
107 * According to Rockchip it's important to keep the SoC IO domain
108 * higher than (or equal to) the external voltage. That means we need
109 * to change it before external voltage changes happen in the case
112 * Note that in the "pre" change we pick the max possible voltage that
113 * the regulator might end up at (the client requests a range and we
114 * don't know for certain the exact voltage). Right now we rely on the
115 * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
116 * request something like a max of 3.6V when they really want 3.3V.
117 * We could attempt to come up with better rules if this fails.
119 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
120 struct pre_voltage_change_data *pvc_data = data;
122 uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
123 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
124 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
125 uV = (unsigned long)data;
130 dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
132 if (uV > MAX_VOLTAGE_3_3) {
133 dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
135 if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
139 ret = rockchip_iodomain_write(supply, uV);
140 if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
143 dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
147 static void px30_iodomain_init(struct rockchip_iodomain *iod)
152 /* if no VCCIO6 supply we should leave things alone */
153 if (!iod->supplies[PX30_IO_VSEL_VCCIO6_SUPPLY_NUM].reg)
157 * set vccio6 iodomain to also use this framework
158 * instead of a special gpio.
160 val = PX30_IO_VSEL_VCCIO6_SRC | (PX30_IO_VSEL_VCCIO6_SRC << 16);
161 ret = regmap_write(iod->grf, PX30_IO_VSEL, val);
163 dev_warn(iod->dev, "couldn't update vccio6 ctrl\n");
166 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
171 /* if no flash supply we should leave things alone */
172 if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
176 * set flash0 iodomain to also use this framework
177 * instead of a special gpio.
179 val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
180 ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
182 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
185 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
190 /* if no vccio2 supply we should leave things alone */
191 if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
195 * set vccio2 iodomain to also use this framework
196 * instead of a special gpio.
198 val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
199 ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
201 dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
204 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
209 /* if no flash supply we should leave things alone */
210 if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
214 * set flash0 iodomain to also use this framework
215 * instead of a special gpio.
217 val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
218 ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
220 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
223 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
228 /* if no pmu io supply we should leave things alone */
229 if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
233 * set pmu io iodomain to also use this framework
234 * instead of a special gpio.
236 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
237 ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
239 dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
242 static const struct rockchip_iodomain_soc_data soc_data_px30 = {
254 .init = px30_iodomain_init,
257 static const struct rockchip_iodomain_soc_data soc_data_px30_pmu = {
280 * On the rk3188 the io-domains are handled by a shared register with the
281 * lower 8 bits being still being continuing drive-strength settings.
283 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
305 static const struct rockchip_iodomain_soc_data soc_data_rk3228 = {
315 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
318 "lcdc", /* LCDC_VDD */
319 "dvp", /* DVPIO_VDD */
320 "flash0", /* FLASH0_VDD (emmc) */
321 "flash1", /* FLASH1_VDD (sdio1) */
322 "wifi", /* APIO3_VDD (sdio0) */
323 "bb", /* APIO5_VDD */
324 "audio", /* APIO4_VDD */
325 "sdcard", /* SDMMC0_VDD (sdmmc) */
326 "gpio30", /* APIO1_VDD */
327 "gpio1830", /* APIO2_VDD */
329 .init = rk3288_iodomain_init,
332 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
343 .init = rk3328_iodomain_init,
346 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
350 "dvp", /* DVPIO_VDD */
351 "flash0", /* FLASH0_VDD (emmc) */
352 "wifi", /* APIO2_VDD (sdio0) */
354 "audio", /* APIO3_VDD */
355 "sdcard", /* SDMMC0_VDD (sdmmc) */
356 "gpio30", /* APIO1_VDD */
357 "gpio1830", /* APIO4_VDD (gpujtag) */
359 .init = rk3368_iodomain_init,
362 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
369 "pmu", /*PMU IO domain*/
370 "vop", /*LCDC IO domain*/
374 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
375 .grf_offset = 0xe640,
377 "bt656", /* APIO2_VDD */
378 "audio", /* APIO5_VDD */
379 "sdmmc", /* SDMMC0_VDD */
380 "gpio1830", /* APIO4_VDD */
384 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
396 "pmu1830", /* PMUIO2_VDD */
398 .init = rk3399_pmu_iodomain_init,
401 static const struct rockchip_iodomain_soc_data soc_data_rv1108 = {
424 static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu = {
431 static const struct of_device_id rockchip_iodomain_match[] = {
433 .compatible = "rockchip,px30-io-voltage-domain",
434 .data = (void *)&soc_data_px30
437 .compatible = "rockchip,px30-pmu-io-voltage-domain",
438 .data = (void *)&soc_data_px30_pmu
441 .compatible = "rockchip,rk3188-io-voltage-domain",
442 .data = &soc_data_rk3188
445 .compatible = "rockchip,rk3228-io-voltage-domain",
446 .data = &soc_data_rk3228
449 .compatible = "rockchip,rk3288-io-voltage-domain",
450 .data = &soc_data_rk3288
453 .compatible = "rockchip,rk3328-io-voltage-domain",
454 .data = &soc_data_rk3328
457 .compatible = "rockchip,rk3368-io-voltage-domain",
458 .data = &soc_data_rk3368
461 .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
462 .data = &soc_data_rk3368_pmu
465 .compatible = "rockchip,rk3399-io-voltage-domain",
466 .data = &soc_data_rk3399
469 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
470 .data = &soc_data_rk3399_pmu
473 .compatible = "rockchip,rv1108-io-voltage-domain",
474 .data = &soc_data_rv1108
477 .compatible = "rockchip,rv1108-pmu-io-voltage-domain",
478 .data = &soc_data_rv1108_pmu
482 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
484 static int rockchip_iodomain_probe(struct platform_device *pdev)
486 struct device_node *np = pdev->dev.of_node;
487 const struct of_device_id *match;
488 struct rockchip_iodomain *iod;
489 struct device *parent;
495 iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
499 iod->dev = &pdev->dev;
500 platform_set_drvdata(pdev, iod);
502 match = of_match_node(rockchip_iodomain_match, np);
503 iod->soc_data = match->data;
505 parent = pdev->dev.parent;
506 if (parent && parent->of_node) {
507 iod->grf = syscon_node_to_regmap(parent->of_node);
509 dev_dbg(&pdev->dev, "falling back to old binding\n");
510 iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
513 if (IS_ERR(iod->grf)) {
514 dev_err(&pdev->dev, "couldn't find grf regmap\n");
515 return PTR_ERR(iod->grf);
518 for (i = 0; i < MAX_SUPPLIES; i++) {
519 const char *supply_name = iod->soc_data->supply_names[i];
520 struct rockchip_iodomain_supply *supply = &iod->supplies[i];
521 struct regulator *reg;
527 reg = devm_regulator_get_optional(iod->dev, supply_name);
531 /* If a supply wasn't specified, that's OK */
534 else if (ret != -EPROBE_DEFER)
535 dev_err(iod->dev, "couldn't get regulator %s\n",
540 /* set initial correct value */
541 uV = regulator_get_voltage(reg);
543 /* must be a regulator we can get the voltage of */
545 dev_err(iod->dev, "Can't determine voltage: %s\n",
551 if (uV > MAX_VOLTAGE_3_3) {
553 "%d uV is too high. May damage SoC!\n",
559 /* setup our supply */
563 supply->nb.notifier_call = rockchip_iodomain_notify;
565 ret = rockchip_iodomain_write(supply, uV);
571 /* register regulator notifier */
572 ret = regulator_register_notifier(reg, &supply->nb);
575 "regulator notifier request failed\n");
581 if (iod->soc_data->init)
582 iod->soc_data->init(iod);
587 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
588 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
591 regulator_unregister_notifier(io_supply->reg,
598 static int rockchip_iodomain_remove(struct platform_device *pdev)
600 struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
603 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
604 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
607 regulator_unregister_notifier(io_supply->reg,
614 static struct platform_driver rockchip_iodomain_driver = {
615 .probe = rockchip_iodomain_probe,
616 .remove = rockchip_iodomain_remove,
618 .name = "rockchip-iodomain",
619 .of_match_table = rockchip_iodomain_match,
623 module_platform_driver(rockchip_iodomain_driver);
625 MODULE_DESCRIPTION("Rockchip IO-domain driver");
626 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
627 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
628 MODULE_LICENSE("GPL v2");