2 * Rockchip IO Voltage Domain driver
4 * Copyright 2014 MundoReader S.L.
5 * Copyright 2014 Google, Inc.
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/err.h>
20 #include <linux/mfd/syscon.h>
22 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
26 #define MAX_SUPPLIES 16
29 * The max voltage for 1.8V and 3.3V come from the Rockchip datasheet under
30 * "Recommended Operating Conditions" for "Digital GPIO". When the typical
31 * is 3.3V the max is 3.6V. When the typical is 1.8V the max is 1.98V.
33 * They are used like this:
34 * - If the voltage on a rail is above the "1.8" voltage (1.98V) we'll tell the
36 * - If the voltage on a rail is above the "3.3" voltage (3.6V) we'll consider
37 * that to be an error.
39 #define MAX_VOLTAGE_1_8 1980000
40 #define MAX_VOLTAGE_3_3 3600000
42 #define RK3288_SOC_CON2 0x24c
43 #define RK3288_SOC_CON2_FLASH0 BIT(7)
44 #define RK3288_SOC_FLASH_SUPPLY_NUM 2
46 #define RK3328_SOC_CON4 0x410
47 #define RK3328_SOC_CON4_VCCIO2 BIT(7)
48 #define RK3328_SOC_VCCIO2_SUPPLY_NUM 1
50 #define RK3366_SOC_CON6 0x418
51 #define RK3366_SOC_CON6_FLASH0 BIT(14)
52 #define RK3366_SOC_FLASH_SUPPLY_NUM 2
54 #define RK3368_SOC_CON15 0x43c
55 #define RK3368_SOC_CON15_FLASH0 BIT(14)
56 #define RK3368_SOC_FLASH_SUPPLY_NUM 2
58 #define RK3399_PMUGRF_CON0 0x180
59 #define RK3399_PMUGRF_CON0_VSEL BIT(8)
60 #define RK3399_PMUGRF_VSEL_SUPPLY_NUM 9
62 struct rockchip_iodomain;
65 * @supplies: voltage settings matching the register bits.
67 struct rockchip_iodomain_soc_data {
69 const char *supply_names[MAX_SUPPLIES];
70 void (*init)(struct rockchip_iodomain *iod);
73 struct rockchip_iodomain_supply {
74 struct rockchip_iodomain *iod;
75 struct regulator *reg;
76 struct notifier_block nb;
80 struct rockchip_iodomain {
83 struct rockchip_iodomain_soc_data *soc_data;
84 struct rockchip_iodomain_supply supplies[MAX_SUPPLIES];
87 static int rockchip_iodomain_write(struct rockchip_iodomain_supply *supply,
90 struct rockchip_iodomain *iod = supply->iod;
95 val = (uV > MAX_VOLTAGE_1_8) ? 0 : 1;
98 /* apply hiword-mask */
99 val |= (BIT(supply->idx) << 16);
101 ret = regmap_write(iod->grf, iod->soc_data->grf_offset, val);
103 dev_err(iod->dev, "Couldn't write to GRF\n");
108 static int rockchip_iodomain_notify(struct notifier_block *nb,
112 struct rockchip_iodomain_supply *supply =
113 container_of(nb, struct rockchip_iodomain_supply, nb);
118 * According to Rockchip it's important to keep the SoC IO domain
119 * higher than (or equal to) the external voltage. That means we need
120 * to change it before external voltage changes happen in the case
123 * Note that in the "pre" change we pick the max possible voltage that
124 * the regulator might end up at (the client requests a range and we
125 * don't know for certain the exact voltage). Right now we rely on the
126 * slop in MAX_VOLTAGE_1_8 and MAX_VOLTAGE_3_3 to save us if clients
127 * request something like a max of 3.6V when they really want 3.3V.
128 * We could attempt to come up with better rules if this fails.
130 if (event & REGULATOR_EVENT_PRE_VOLTAGE_CHANGE) {
131 struct pre_voltage_change_data *pvc_data = data;
133 uV = max_t(unsigned long, pvc_data->old_uV, pvc_data->max_uV);
134 } else if (event & (REGULATOR_EVENT_VOLTAGE_CHANGE |
135 REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE)) {
136 uV = (unsigned long)data;
141 dev_dbg(supply->iod->dev, "Setting to %d\n", uV);
143 if (uV > MAX_VOLTAGE_3_3) {
144 dev_err(supply->iod->dev, "Voltage too high: %d\n", uV);
146 if (event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
150 ret = rockchip_iodomain_write(supply, uV);
151 if (ret && event == REGULATOR_EVENT_PRE_VOLTAGE_CHANGE)
154 dev_info(supply->iod->dev, "Setting to %d done\n", uV);
158 static void rk3288_iodomain_init(struct rockchip_iodomain *iod)
163 /* if no flash supply we should leave things alone */
164 if (!iod->supplies[RK3288_SOC_FLASH_SUPPLY_NUM].reg)
168 * set flash0 iodomain to also use this framework
169 * instead of a special gpio.
171 val = RK3288_SOC_CON2_FLASH0 | (RK3288_SOC_CON2_FLASH0 << 16);
172 ret = regmap_write(iod->grf, RK3288_SOC_CON2, val);
174 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
177 static void rk3328_iodomain_init(struct rockchip_iodomain *iod)
182 /* if no vccio2 supply we should leave things alone */
183 if (!iod->supplies[RK3328_SOC_VCCIO2_SUPPLY_NUM].reg)
187 * set vccio2 iodomain to also use this framework
188 * instead of a special gpio.
190 val = RK3328_SOC_CON4_VCCIO2 | (RK3328_SOC_CON4_VCCIO2 << 16);
191 ret = regmap_write(iod->grf, RK3328_SOC_CON4, val);
193 dev_warn(iod->dev, "couldn't update vccio2 vsel ctrl\n");
196 static void rk3366_iodomain_init(struct rockchip_iodomain *iod)
201 /* if no flash supply we should leave things alone */
202 if (!iod->supplies[RK3366_SOC_FLASH_SUPPLY_NUM].reg)
206 * set flash0 iodomain to also use this framework
207 * instead of a special gpio.
209 val = RK3366_SOC_CON6_FLASH0 | (RK3366_SOC_CON6_FLASH0 << 16);
210 ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
212 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
215 static void rk3368_iodomain_init(struct rockchip_iodomain *iod)
220 /* if no flash supply we should leave things alone */
221 if (!iod->supplies[RK3368_SOC_FLASH_SUPPLY_NUM].reg)
225 * set flash0 iodomain to also use this framework
226 * instead of a special gpio.
228 val = RK3368_SOC_CON15_FLASH0 | (RK3368_SOC_CON15_FLASH0 << 16);
229 ret = regmap_write(iod->grf, RK3368_SOC_CON15, val);
231 dev_warn(iod->dev, "couldn't update flash0 ctrl\n");
234 static void rk3399_pmu_iodomain_init(struct rockchip_iodomain *iod)
239 /* if no pmu io supply we should leave things alone */
240 if (!iod->supplies[RK3399_PMUGRF_VSEL_SUPPLY_NUM].reg)
244 * set pmu io iodomain to also use this framework
245 * instead of a special gpio.
247 val = RK3399_PMUGRF_CON0_VSEL | (RK3399_PMUGRF_CON0_VSEL << 16);
248 ret = regmap_write(iod->grf, RK3399_PMUGRF_CON0, val);
250 dev_warn(iod->dev, "couldn't update pmu io iodomain ctrl\n");
254 * On the rk3188 the io-domains are handled by a shared register with the
255 * lower 8 bits being still being continuing drive-strength settings.
257 static const struct rockchip_iodomain_soc_data soc_data_rk3188 = {
279 static const struct rockchip_iodomain_soc_data soc_data_rk3288 = {
282 "lcdc", /* LCDC_VDD */
283 "dvp", /* DVPIO_VDD */
284 "flash0", /* FLASH0_VDD (emmc) */
285 "flash1", /* FLASH1_VDD (sdio1) */
286 "wifi", /* APIO3_VDD (sdio0) */
287 "bb", /* APIO5_VDD */
288 "audio", /* APIO4_VDD */
289 "sdcard", /* SDMMC0_VDD (sdmmc) */
290 "gpio30", /* APIO1_VDD */
291 "gpio1830", /* APIO2_VDD */
293 .init = rk3288_iodomain_init,
296 static const struct rockchip_iodomain_soc_data soc_data_rk3328 = {
307 .init = rk3328_iodomain_init,
310 static const struct rockchip_iodomain_soc_data soc_data_rk3366 = {
313 "lcdc", /* LCDC_IOVDD */
314 "dvpts", /* DVP_IOVDD */
315 "flash", /* FLASH_IOVDD (emmc) */
316 "wifibt", /* APIO1_IOVDD */
318 "audio", /* AUDIO_IODVDD */
319 "sdcard", /* SDMMC_IOVDD (sdmmc) */
320 "tphdsor", /* APIO2_IOVDD */
322 .init = rk3366_iodomain_init,
325 static const struct rockchip_iodomain_soc_data soc_data_rk3368 = {
329 "dvp", /* DVPIO_VDD */
330 "flash0", /* FLASH0_VDD (emmc) */
331 "wifi", /* APIO2_VDD (sdio0) */
333 "audio", /* APIO3_VDD */
334 "sdcard", /* SDMMC0_VDD (sdmmc) */
335 "gpio30", /* APIO1_VDD */
336 "gpio1830", /* APIO4_VDD (gpujtag) */
338 .init = rk3368_iodomain_init,
341 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu = {
348 "pmu", /*PMU IO domain*/
349 "vop", /*LCDC IO domain*/
353 static const struct rockchip_iodomain_soc_data soc_data_rk3399 = {
354 .grf_offset = 0xe640,
356 "bt656", /* APIO2_VDD */
357 "audio", /* APIO5_VDD */
358 "sdmmc", /* SDMMC0_VDD */
359 "gpio1830", /* APIO4_VDD */
363 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu = {
375 "pmu1830", /* PMUIO2_VDD */
377 .init = rk3399_pmu_iodomain_init,
380 static const struct of_device_id rockchip_iodomain_match[] = {
382 .compatible = "rockchip,rk3188-io-voltage-domain",
383 .data = (void *)&soc_data_rk3188
386 .compatible = "rockchip,rk3288-io-voltage-domain",
387 .data = (void *)&soc_data_rk3288
390 .compatible = "rockchip,rk3328-io-voltage-domain",
391 .data = (void *)&soc_data_rk3328
394 .compatible = "rockchip,rk3366-io-voltage-domain",
395 .data = (void *)&soc_data_rk3366
398 .compatible = "rockchip,rk3368-io-voltage-domain",
399 .data = (void *)&soc_data_rk3368
402 .compatible = "rockchip,rk3368-pmu-io-voltage-domain",
403 .data = (void *)&soc_data_rk3368_pmu
406 .compatible = "rockchip,rk3399-io-voltage-domain",
407 .data = (void *)&soc_data_rk3399
410 .compatible = "rockchip,rk3399-pmu-io-voltage-domain",
411 .data = (void *)&soc_data_rk3399_pmu
415 MODULE_DEVICE_TABLE(of, rockchip_iodomain_match);
417 static int rockchip_iodomain_probe(struct platform_device *pdev)
419 struct device_node *np = pdev->dev.of_node;
420 const struct of_device_id *match;
421 struct rockchip_iodomain *iod;
422 struct device *parent;
428 iod = devm_kzalloc(&pdev->dev, sizeof(*iod), GFP_KERNEL);
432 iod->dev = &pdev->dev;
433 platform_set_drvdata(pdev, iod);
435 match = of_match_node(rockchip_iodomain_match, np);
436 iod->soc_data = (struct rockchip_iodomain_soc_data *)match->data;
438 parent = pdev->dev.parent;
439 if (parent && parent->of_node) {
440 iod->grf = syscon_node_to_regmap(parent->of_node);
442 dev_dbg(&pdev->dev, "falling back to old binding\n");
443 iod->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
446 if (IS_ERR(iod->grf)) {
447 dev_err(&pdev->dev, "couldn't find grf regmap\n");
448 return PTR_ERR(iod->grf);
451 for (i = 0; i < MAX_SUPPLIES; i++) {
452 const char *supply_name = iod->soc_data->supply_names[i];
453 struct rockchip_iodomain_supply *supply = &iod->supplies[i];
454 struct regulator *reg;
460 reg = devm_regulator_get_optional(iod->dev, supply_name);
464 /* If a supply wasn't specified, that's OK */
467 else if (ret != -EPROBE_DEFER)
468 dev_err(iod->dev, "couldn't get regulator %s\n",
473 /* set initial correct value */
474 uV = regulator_get_voltage(reg);
476 /* must be a regulator we can get the voltage of */
478 dev_err(iod->dev, "Can't determine voltage: %s\n",
483 if (uV > MAX_VOLTAGE_3_3) {
485 "%d uV is too high. May damage SoC!\n",
491 /* setup our supply */
495 supply->nb.notifier_call = rockchip_iodomain_notify;
497 ret = rockchip_iodomain_write(supply, uV);
503 /* register regulator notifier */
504 ret = regulator_register_notifier(reg, &supply->nb);
507 "regulator notifier request failed\n");
513 if (iod->soc_data->init)
514 iod->soc_data->init(iod);
519 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
520 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
523 regulator_unregister_notifier(io_supply->reg,
530 static int rockchip_iodomain_remove(struct platform_device *pdev)
532 struct rockchip_iodomain *iod = platform_get_drvdata(pdev);
535 for (i = MAX_SUPPLIES - 1; i >= 0; i--) {
536 struct rockchip_iodomain_supply *io_supply = &iod->supplies[i];
539 regulator_unregister_notifier(io_supply->reg,
546 static struct platform_driver rockchip_iodomain_driver = {
547 .probe = rockchip_iodomain_probe,
548 .remove = rockchip_iodomain_remove,
550 .name = "rockchip-iodomain",
551 .of_match_table = rockchip_iodomain_match,
555 module_platform_driver(rockchip_iodomain_driver);
557 MODULE_DESCRIPTION("Rockchip IO-domain driver");
558 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
559 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
560 MODULE_LICENSE("GPL v2");