2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/extres/syscon/syscon.h>
42 #include <dev/extres/regulator/regulator.h>
44 #include "syscon_if.h"
46 #define RK3288_GRF_IO_VSEL 0x380
47 #define RK3399_GRF_IO_VSEL 0xe640
48 #define RK3399_PMUGRF_IO_VSEL 0x180
50 struct rk_iodomain_supply {
55 struct rk_iodomain_conf {
56 struct rk_iodomain_supply *supply;
61 struct rk_iodomain_softc {
65 struct rk_iodomain_conf *conf;
68 static struct rk_iodomain_supply rk3288_supply[] = {
78 {"gpio1830-supply", 9},
81 static struct rk_iodomain_conf rk3288_conf = {
82 .supply = rk3288_supply,
83 .nsupply = nitems(rk3288_supply),
84 .grf_reg = RK3288_GRF_IO_VSEL,
87 static struct rk_iodomain_supply rk3399_supply[] = {
91 {"gpio1830-supply", 3},
94 static struct rk_iodomain_conf rk3399_conf = {
95 .supply = rk3399_supply,
96 .nsupply = nitems(rk3399_supply),
97 .grf_reg = RK3399_GRF_IO_VSEL,
100 static struct rk_iodomain_supply rk3399_pmu_supply[] = {
101 {"pmu1830-supply", 9},
104 static struct rk_iodomain_conf rk3399_pmu_conf = {
105 .supply = rk3399_pmu_supply,
106 .nsupply = nitems(rk3399_pmu_supply),
107 .grf_reg = RK3399_PMUGRF_IO_VSEL,
110 static struct ofw_compat_data compat_data[] = {
111 {"rockchip,rk3288-io-voltage-domain", (uintptr_t)&rk3288_conf},
112 {"rockchip,rk3399-io-voltage-domain", (uintptr_t)&rk3399_conf},
113 {"rockchip,rk3399-pmu-io-voltage-domain", (uintptr_t)&rk3399_pmu_conf},
118 rk_iodomain_set(struct rk_iodomain_softc *sc)
125 for (i = 0; i < sc->conf->nsupply; i++) {
126 mask |= (1 << sc->conf->supply[i].bit) << 16;
127 if (regulator_get_by_ofw_property(sc->dev, sc->node,
128 sc->conf->supply[i].name, &supply) == 0) {
129 if (regulator_get_voltage(supply, &uvolt) == 0) {
130 if (uvolt == 1800000)
131 reg |= (1 << sc->conf->supply[i].bit);
132 else if (uvolt != 3000000)
133 device_printf(sc->dev,
134 "%s regulator is at %duV, ignoring\n",
135 sc->conf->supply[i].name, uvolt);
137 device_printf(sc->dev, "Cannot get current "
138 "voltage for regulator %s\n",
139 sc->conf->supply[i].name);
143 SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask);
147 rk_iodomain_probe(device_t dev)
150 if (!ofw_bus_status_okay(dev))
153 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
156 device_set_desc(dev, "RockChip IO Voltage Domain");
157 return (BUS_PROBE_DEFAULT);
161 rk_iodomain_attach(device_t dev)
163 struct rk_iodomain_softc *sc;
166 sc = device_get_softc(dev);
168 sc->node = ofw_bus_get_node(dev);
170 rv = syscon_get_handle_default(dev, &sc->grf);
172 device_printf(dev, "Cannot get grf handle\n");
176 sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
183 rk_iodomain_detach(device_t dev)
189 static device_method_t rk_iodomain_methods[] = {
190 /* Device interface */
191 DEVMETHOD(device_probe, rk_iodomain_probe),
192 DEVMETHOD(device_attach, rk_iodomain_attach),
193 DEVMETHOD(device_detach, rk_iodomain_detach),
198 static driver_t rk_iodomain_driver = {
201 sizeof(struct rk_iodomain_softc),
204 static devclass_t rk_iodomain_devclass;
206 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver,
207 rk_iodomain_devclass, 0, 0, BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);