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;
57 * @supplies: voltage settings matching the register bits.
59 struct rockchip_iodomain_soc_data {
61 const char *supply_names[MAX_SUPPLIES];
62 void (*init)(struct rockchip_iodomain *iod);
65 struct rockchip_iodomain_supply {
66 struct rockchip_iodomain *iod;
67 struct regulator *reg;
68 struct notifier_block nb;
72 struct rockchip_iodomain {
75 const struct rockchip_iodomain_soc_data *soc_data;
76 struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
79 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
82 struct rockchip_iodomain *iod = supply->iod;
87 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
90 /* apply hiword-mask */
91 val |= (BIT(supply->idx) << 16);
93 ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
95 dev_err(iod->dev, "Couldn't write to GRF\n");
100 static int rockchip_iodomain_notify(struct notifier_block *nb,
104 struct rockchip_iodomain_supply *supply =
105 container_of(nb, struct rockchip_iodomain_supply, nb);
110 * According to Rockchip it's important to keep the SoC IO domain
111 * higher than (or equal to) the external voltage. That means we need
112 * to change it before external voltage changes happen in the case
115 * Note that in the "pre" change we pick the max possible voltage that
116 * the regulator might end up at (the client requests a range and we
117 * don't know for certain the exact voltage). Right now we rely on the
118 * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
119 * request something like a max of 3.6V when they really want 3.3V.
120 * We could attempt to come up with better rules if this fails.
122 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
123 struct pre_voltage_change_data *pvc_data = data;
125 uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
126 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
127 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
128 uV = (unsigned long)data;
133 dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
135 if (uV > MAX_VOLTAGE_3_3) {
136 dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
138 if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
142 ret = rockchip_iodomain_write(supply, uV);
143 if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
146 dev_dbg(supply->iod->dev, "Setting to %d done\n", uV);
150 static void px30_iodomain_init(struct rockchip_iodomain *iod)
155 /* if no VCCIO6 supply we should leave things alone */
156 if (!iod->supplies[PX30_IO_VSEL_VCCIO6_SUPPLY_NUM].reg)
160 * set vccio6 iodomain to also use this framework
161 * instead of a special gpio.
163 val = PX30_IO_VSEL_VCCIO6_SRC | (PX30_IO_VSEL_VCCIO6_SRC << 16);
164 ret = regmap_write(iod->grf, PX30_IO_VSEL, val);
166 dev_warn(iod->dev, "couldn't update vccio6 ctrl\n");
169 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
174 /* if no flash supply we should leave things alone */
175 if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
179 * set flash0 iodomain to also use this framework
180 * instead of a special gpio.
182 val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
183 ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
185 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
188 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
193 /* if no vccio2 supply we should leave things alone */
194 if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
198 * set vccio2 iodomain to also use this framework
199 * instead of a special gpio.
201 val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
202 ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
204 dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
207 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
212 /* if no flash supply we should leave things alone */
213 if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
217 * set flash0 iodomain to also use this framework
218 * instead of a special gpio.
220 val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
221 ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
223 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
226 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
231 /* if no pmu io supply we should leave things alone */
232 if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
236 * set pmu io iodomain to also use this framework
237 * instead of a special gpio.
239 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
240 ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
242 dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
245 static const struct rockchip_iodomain_soc_data soc_data_px30 = {
257 .init = px30_iodomain_init,
260 static const struct rockchip_iodomain_soc_data soc_data_px30_pmu = {
283 * On the rk3188 the io-domains are handled by a shared register with the
284 * lower 8 bits being still being continuing drive-strength settings.
286 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
308 static const struct rockchip_iodomain_soc_data soc_data_rk3228 = {
318 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
321 "lcdc", /* LCDC_VDD */
322 "dvp", /* DVPIO_VDD */
323 "flash0", /* FLASH0_VDD (emmc) */
324 "flash1", /* FLASH1_VDD (sdio1) */
325 "wifi", /* APIO3_VDD (sdio0) */
326 "bb", /* APIO5_VDD */
327 "audio", /* APIO4_VDD */
328 "sdcard", /* SDMMC0_VDD (sdmmc) */
329 "gpio30", /* APIO1_VDD */
330 "gpio1830", /* APIO2_VDD */
332 .init = rk3288_iodomain_init,
335 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
346 .init = rk3328_iodomain_init,
349 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
353 "dvp", /* DVPIO_VDD */
354 "flash0", /* FLASH0_VDD (emmc) */
355 "wifi", /* APIO2_VDD (sdio0) */
357 "audio", /* APIO3_VDD */
358 "sdcard", /* SDMMC0_VDD (sdmmc) */
359 "gpio30", /* APIO1_VDD */
360 "gpio1830", /* APIO4_VDD (gpujtag) */
362 .init = rk3368_iodomain_init,
365 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
372 "pmu", /*PMU IO domain*/
373 "vop", /*LCDC IO domain*/
377 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
378 .grf_offset = 0xe640,
380 "bt656", /* APIO2_VDD */
381 "audio", /* APIO5_VDD */
382 "sdmmc", /* SDMMC0_VDD */
383 "gpio1830", /* APIO4_VDD */
387 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
399 "pmu1830", /* PMUIO2_VDD */
401 .init = rk3399_pmu_iodomain_init,
404 static const struct rockchip_iodomain_soc_data soc_data_rv1108 = {
427 static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu = {
434 static const struct of_device_id rockchip_iodomain_match[] = {
436 .compatible = "rockchip,px30-io-voltage-domain",
437 .data = (void *)&soc_data_px30
440 .compatible = "rockchip,px30-pmu-io-voltage-domain",
441 .data = (void *)&soc_data_px30_pmu
444 .compatible = "rockchip,rk3188-io-voltage-domain",
445 .data = &soc_data_rk3188
448 .compatible = "rockchip,rk3228-io-voltage-domain",
449 .data = &soc_data_rk3228
452 .compatible = "rockchip,rk3288-io-voltage-domain",
453 .data = &soc_data_rk3288
456 .compatible = "rockchip,rk3328-io-voltage-domain",
457 .data = &soc_data_rk3328
460 .compatible = "rockchip,rk3368-io-voltage-domain",
461 .data = &soc_data_rk3368
464 .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
465 .data = &soc_data_rk3368_pmu
468 .compatible = "rockchip,rk3399-io-voltage-domain",
469 .data = &soc_data_rk3399
472 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
473 .data = &soc_data_rk3399_pmu
476 .compatible = "rockchip,rv1108-io-voltage-domain",
477 .data = &soc_data_rv1108
480 .compatible = "rockchip,rv1108-pmu-io-voltage-domain",
481 .data = &soc_data_rv1108_pmu
485 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
487 static int rockchip_iodomain_probe(struct platform_device *pdev)
489 struct device_node *np = pdev->dev.of_node;
490 const struct of_device_id *match;
491 struct rockchip_iodomain *iod;
492 struct device *parent;
498 iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
502 iod->dev = &pdev->dev;
503 platform_set_drvdata(pdev, iod);
505 match = of_match_node(rockchip_iodomain_match, np);
506 iod->soc_data = match->data;
508 parent = pdev->dev.parent;
509 if (parent && parent->of_node) {
510 iod->grf = syscon_node_to_regmap(parent->of_node);
512 dev_dbg(&pdev->dev, "falling back to old binding\n");
513 iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
516 if (IS_ERR(iod->grf)) {
517 dev_err(&pdev->dev, "couldn't find grf regmap\n");
518 return PTR_ERR(iod->grf);
521 for (i = 0; i < MAX_SUPPLIES; i++) {
522 const char *supply_name = iod->soc_data->supply_names[i];
523 struct rockchip_iodomain_supply *supply = &iod->supplies[i];
524 struct regulator *reg;
530 reg = devm_regulator_get_optional(iod->dev, supply_name);
534 /* If a supply wasn't specified, that's OK */
537 else if (ret != -EPROBE_DEFER)
538 dev_err(iod->dev, "couldn't get regulator %s\n",
543 /* set initial correct value */
544 uV = regulator_get_voltage(reg);
546 /* must be a regulator we can get the voltage of */
548 dev_err(iod->dev, "Can't determine voltage: %s\n",
554 if (uV > MAX_VOLTAGE_3_3) {
556 "%d uV is too high. May damage SoC!\n",
562 /* setup our supply */
566 supply->nb.notifier_call = rockchip_iodomain_notify;
568 ret = rockchip_iodomain_write(supply, uV);
574 /* register regulator notifier */
575 ret = regulator_register_notifier(reg, &supply->nb);
578 "regulator notifier request failed\n");
584 if (iod->soc_data->init)
585 iod->soc_data->init(iod);
590 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
591 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
594 regulator_unregister_notifier(io_supply->reg,
601 static int rockchip_iodomain_remove(struct platform_device *pdev)
603 struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
606 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
607 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
610 regulator_unregister_notifier(io_supply->reg,
617 static struct platform_driver rockchip_iodomain_driver = {
618 .probe = rockchip_iodomain_probe,
619 .remove = rockchip_iodomain_remove,
621 .name = "rockchip-iodomain",
622 .of_match_table = rockchip_iodomain_match,
626 module_platform_driver(rockchip_iodomain_driver);
628 MODULE_DESCRIPTION("Rockchip IO-domain driver");
629 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
630 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
631 MODULE_LICENSE("GPL v2");