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 PX30_IO_VSEL 0x180
43 #define PX30_IO_VSEL_VCCIO6_SRC BIT(0)
44 #define PX30_IO_VSEL_VCCIO6_SUPPLY_NUM 1
46 #define RK3288_SOC_CON2 0x24c
47 #define RK3288_SOC_CON2_FLASH0 BIT(7)
48 #define RK3288_SOC_FLASH_SUPPLY_NUM 2
50 #define RK3328_SOC_CON4 0x410
51 #define RK3328_SOC_CON4_VCCIO2 BIT(7)
52 #define RK3328_SOC_VCCIO2_SUPPLY_NUM 1
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 const 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_dbg(supply
->iod
->dev
, "Setting to %d done\n", uV
);
158 static void px30_iodomain_init(struct rockchip_iodomain
*iod
)
163 /* if no VCCIO0 supply we should leave things alone */
164 if (!iod
->supplies
[PX30_IO_VSEL_VCCIO6_SUPPLY_NUM
].reg
)
168 * set vccio0 iodomain to also use this framework
169 * instead of a special gpio.
171 val
= PX30_IO_VSEL_VCCIO6_SRC
| (PX30_IO_VSEL_VCCIO6_SRC
<< 16);
172 ret
= regmap_write(iod
->grf
, PX30_IO_VSEL
, val
);
174 dev_warn(iod
->dev
, "couldn't update vccio0 ctrl\n");
177 static void rk3288_iodomain_init(struct rockchip_iodomain
*iod
)
182 /* if no flash supply we should leave things alone */
183 if (!iod
->supplies
[RK3288_SOC_FLASH_SUPPLY_NUM
].reg
)
187 * set flash0 iodomain to also use this framework
188 * instead of a special gpio.
190 val
= RK3288_SOC_CON2_FLASH0
| (RK3288_SOC_CON2_FLASH0
<< 16);
191 ret
= regmap_write(iod
->grf
, RK3288_SOC_CON2
, val
);
193 dev_warn(iod
->dev
, "couldn't update flash0 ctrl\n");
196 static void rk3328_iodomain_init(struct rockchip_iodomain
*iod
)
201 /* if no vccio2 supply we should leave things alone */
202 if (!iod
->supplies
[RK3328_SOC_VCCIO2_SUPPLY_NUM
].reg
)
206 * set vccio2 iodomain to also use this framework
207 * instead of a special gpio.
209 val
= RK3328_SOC_CON4_VCCIO2
| (RK3328_SOC_CON4_VCCIO2
<< 16);
210 ret
= regmap_write(iod
->grf
, RK3328_SOC_CON4
, val
);
212 dev_warn(iod
->dev
, "couldn't update vccio2 vsel 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");
253 static const struct rockchip_iodomain_soc_data soc_data_px30
= {
265 .init
= px30_iodomain_init
,
268 static const struct rockchip_iodomain_soc_data soc_data_px30_pmu
= {
291 * On the rk3188 the io-domains are handled by a shared register with the
292 * lower 8 bits being still being continuing drive-strength settings.
294 static const struct rockchip_iodomain_soc_data soc_data_rk3188
= {
316 static const struct rockchip_iodomain_soc_data soc_data_rk3228
= {
326 static const struct rockchip_iodomain_soc_data soc_data_rk3288
= {
329 "lcdc", /* LCDC_VDD */
330 "dvp", /* DVPIO_VDD */
331 "flash0", /* FLASH0_VDD (emmc) */
332 "flash1", /* FLASH1_VDD (sdio1) */
333 "wifi", /* APIO3_VDD (sdio0) */
334 "bb", /* APIO5_VDD */
335 "audio", /* APIO4_VDD */
336 "sdcard", /* SDMMC0_VDD (sdmmc) */
337 "gpio30", /* APIO1_VDD */
338 "gpio1830", /* APIO2_VDD */
340 .init
= rk3288_iodomain_init
,
343 static const struct rockchip_iodomain_soc_data soc_data_rk3328
= {
354 .init
= rk3328_iodomain_init
,
357 static const struct rockchip_iodomain_soc_data soc_data_rk3368
= {
361 "dvp", /* DVPIO_VDD */
362 "flash0", /* FLASH0_VDD (emmc) */
363 "wifi", /* APIO2_VDD (sdio0) */
365 "audio", /* APIO3_VDD */
366 "sdcard", /* SDMMC0_VDD (sdmmc) */
367 "gpio30", /* APIO1_VDD */
368 "gpio1830", /* APIO4_VDD (gpujtag) */
370 .init
= rk3368_iodomain_init
,
373 static const struct rockchip_iodomain_soc_data soc_data_rk3368_pmu
= {
380 "pmu", /*PMU IO domain*/
381 "vop", /*LCDC IO domain*/
385 static const struct rockchip_iodomain_soc_data soc_data_rk3399
= {
386 .grf_offset
= 0xe640,
388 "bt656", /* APIO2_VDD */
389 "audio", /* APIO5_VDD */
390 "sdmmc", /* SDMMC0_VDD */
391 "gpio1830", /* APIO4_VDD */
395 static const struct rockchip_iodomain_soc_data soc_data_rk3399_pmu
= {
407 "pmu1830", /* PMUIO2_VDD */
409 .init
= rk3399_pmu_iodomain_init
,
412 static const struct rockchip_iodomain_soc_data soc_data_rv1108
= {
435 static const struct rockchip_iodomain_soc_data soc_data_rv1108_pmu
= {
442 static const struct of_device_id rockchip_iodomain_match
[] = {
444 .compatible
= "rockchip,px30-io-voltage-domain",
445 .data
= (void *)&soc_data_px30
448 .compatible
= "rockchip,px30-pmu-io-voltage-domain",
449 .data
= (void *)&soc_data_px30_pmu
452 .compatible
= "rockchip,rk3188-io-voltage-domain",
453 .data
= &soc_data_rk3188
456 .compatible
= "rockchip,rk3228-io-voltage-domain",
457 .data
= &soc_data_rk3228
460 .compatible
= "rockchip,rk3288-io-voltage-domain",
461 .data
= &soc_data_rk3288
464 .compatible
= "rockchip,rk3328-io-voltage-domain",
465 .data
= &soc_data_rk3328
468 .compatible
= "rockchip,rk3368-io-voltage-domain",
469 .data
= &soc_data_rk3368
472 .compatible
= "rockchip,rk3368-pmu-io-voltage-domain",
473 .data
= &soc_data_rk3368_pmu
476 .compatible
= "rockchip,rk3399-io-voltage-domain",
477 .data
= &soc_data_rk3399
480 .compatible
= "rockchip,rk3399-pmu-io-voltage-domain",
481 .data
= &soc_data_rk3399_pmu
484 .compatible
= "rockchip,rv1108-io-voltage-domain",
485 .data
= &soc_data_rv1108
488 .compatible
= "rockchip,rv1108-pmu-io-voltage-domain",
489 .data
= &soc_data_rv1108_pmu
493 MODULE_DEVICE_TABLE(of
, rockchip_iodomain_match
);
495 static int rockchip_iodomain_probe(struct platform_device
*pdev
)
497 struct device_node
*np
= pdev
->dev
.of_node
;
498 const struct of_device_id
*match
;
499 struct rockchip_iodomain
*iod
;
500 struct device
*parent
;
506 iod
= devm_kzalloc(&pdev
->dev
, sizeof(*iod
), GFP_KERNEL
);
510 iod
->dev
= &pdev
->dev
;
511 platform_set_drvdata(pdev
, iod
);
513 match
= of_match_node(rockchip_iodomain_match
, np
);
514 iod
->soc_data
= match
->data
;
516 parent
= pdev
->dev
.parent
;
517 if (parent
&& parent
->of_node
) {
518 iod
->grf
= syscon_node_to_regmap(parent
->of_node
);
520 dev_dbg(&pdev
->dev
, "falling back to old binding\n");
521 iod
->grf
= syscon_regmap_lookup_by_phandle(np
, "rockchip,grf");
524 if (IS_ERR(iod
->grf
)) {
525 dev_err(&pdev
->dev
, "couldn't find grf regmap\n");
526 return PTR_ERR(iod
->grf
);
529 for (i
= 0; i
< MAX_SUPPLIES
; i
++) {
530 const char *supply_name
= iod
->soc_data
->supply_names
[i
];
531 struct rockchip_iodomain_supply
*supply
= &iod
->supplies
[i
];
532 struct regulator
*reg
;
538 reg
= devm_regulator_get_optional(iod
->dev
, supply_name
);
542 /* If a supply wasn't specified, that's OK */
545 else if (ret
!= -EPROBE_DEFER
)
546 dev_err(iod
->dev
, "couldn't get regulator %s\n",
551 /* set initial correct value */
552 uV
= regulator_get_voltage(reg
);
554 /* must be a regulator we can get the voltage of */
556 dev_err(iod
->dev
, "Can't determine voltage: %s\n",
561 if (uV
> MAX_VOLTAGE_3_3
) {
563 "%d uV is too high. May damage SoC!\n",
569 /* setup our supply */
573 supply
->nb
.notifier_call
= rockchip_iodomain_notify
;
575 ret
= rockchip_iodomain_write(supply
, uV
);
581 /* register regulator notifier */
582 ret
= regulator_register_notifier(reg
, &supply
->nb
);
585 "regulator notifier request failed\n");
591 if (iod
->soc_data
->init
)
592 iod
->soc_data
->init(iod
);
597 for (i
= MAX_SUPPLIES
- 1; i
>= 0; i
--) {
598 struct rockchip_iodomain_supply
*io_supply
= &iod
->supplies
[i
];
601 regulator_unregister_notifier(io_supply
->reg
,
608 static int rockchip_iodomain_remove(struct platform_device
*pdev
)
610 struct rockchip_iodomain
*iod
= platform_get_drvdata(pdev
);
613 for (i
= MAX_SUPPLIES
- 1; i
>= 0; i
--) {
614 struct rockchip_iodomain_supply
*io_supply
= &iod
->supplies
[i
];
617 regulator_unregister_notifier(io_supply
->reg
,
624 static struct platform_driver rockchip_iodomain_driver
= {
625 .probe
= rockchip_iodomain_probe
,
626 .remove
= rockchip_iodomain_remove
,
628 .name
= "rockchip-iodomain",
629 .of_match_table
= rockchip_iodomain_match
,
633 module_platform_driver(rockchip_iodomain_driver
);
635 MODULE_DESCRIPTION("Rockchip IO-domain driver");
636 MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
637 MODULE_AUTHOR("Doug Anderson <dianders@chromium.org>");
638 MODULE_LICENSE("GPL v2");