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_SOC_CON0 0x180
50 struct rk_iodomain_supply {
55 struct rk_iodomain_softc;
57 struct rk_iodomain_conf {
58 struct rk_iodomain_supply *supply;
61 void (*init)(struct rk_iodomain_softc *sc);
64 struct rk_iodomain_softc {
68 struct rk_iodomain_conf *conf;
71 static struct rk_iodomain_supply rk3288_supply[] = {
81 {"gpio1830-supply", 9},
84 static struct rk_iodomain_conf rk3288_conf = {
85 .supply = rk3288_supply,
86 .nsupply = nitems(rk3288_supply),
87 .grf_reg = RK3288_GRF_IO_VSEL,
90 static struct rk_iodomain_supply rk3399_supply[] = {
94 {"gpio1830-supply", 3},
97 static struct rk_iodomain_conf rk3399_conf = {
98 .supply = rk3399_supply,
99 .nsupply = nitems(rk3399_supply),
100 .grf_reg = RK3399_GRF_IO_VSEL,
103 static struct rk_iodomain_supply rk3399_pmu_supply[] = {
104 {"pmu1830-supply", 9},
107 static void rk3399_pmu_init(struct rk_iodomain_softc *sc);
108 static struct rk_iodomain_conf rk3399_pmu_conf = {
109 .supply = rk3399_pmu_supply,
110 .nsupply = nitems(rk3399_pmu_supply),
111 .grf_reg = RK3399_PMUGRF_SOC_CON0,
112 .init = rk3399_pmu_init,
115 static struct ofw_compat_data compat_data[] = {
116 {"rockchip,rk3288-io-voltage-domain", (uintptr_t)&rk3288_conf},
117 {"rockchip,rk3399-io-voltage-domain", (uintptr_t)&rk3399_conf},
118 {"rockchip,rk3399-pmu-io-voltage-domain", (uintptr_t)&rk3399_pmu_conf},
123 rk3399_pmu_init(struct rk_iodomain_softc *sc)
126 SYSCON_WRITE_4(sc->grf, RK3399_PMUGRF_SOC_CON0,
127 (1 << 8) | (1 << (8 + 16))); /* set pmu1830_volsel */
131 rk_iodomain_set(struct rk_iodomain_softc *sc)
138 for (i = 0; i < sc->conf->nsupply; i++) {
139 mask |= (1 << sc->conf->supply[i].bit) << 16;
140 if (regulator_get_by_ofw_property(sc->dev, sc->node,
141 sc->conf->supply[i].name, &supply) == 0) {
142 if (regulator_get_voltage(supply, &uvolt) == 0) {
143 if (uvolt == 1800000)
144 reg |= (1 << sc->conf->supply[i].bit);
145 else if (uvolt != 3000000)
146 device_printf(sc->dev,
147 "%s regulator is at %duV, ignoring\n",
148 sc->conf->supply[i].name, uvolt);
150 device_printf(sc->dev, "Cannot get current "
151 "voltage for regulator %s\n",
152 sc->conf->supply[i].name);
156 SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask);
157 if (sc->conf->init != NULL)
162 rk_iodomain_probe(device_t dev)
165 if (!ofw_bus_status_okay(dev))
168 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
171 device_set_desc(dev, "RockChip IO Voltage Domain");
172 return (BUS_PROBE_DEFAULT);
176 rk_iodomain_attach(device_t dev)
178 struct rk_iodomain_softc *sc;
181 sc = device_get_softc(dev);
183 sc->node = ofw_bus_get_node(dev);
185 rv = syscon_get_handle_default(dev, &sc->grf);
187 device_printf(dev, "Cannot get grf handle\n");
191 sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
198 rk_iodomain_detach(device_t dev)
204 static device_method_t rk_iodomain_methods[] = {
205 /* Device interface */
206 DEVMETHOD(device_probe, rk_iodomain_probe),
207 DEVMETHOD(device_attach, rk_iodomain_attach),
208 DEVMETHOD(device_detach, rk_iodomain_detach),
213 static driver_t rk_iodomain_driver = {
216 sizeof(struct rk_iodomain_softc),
219 static devclass_t rk_iodomain_devclass;
221 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver,
222 rk_iodomain_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);