xref: /freebsd-13-stable/sys/arm64/rockchip/rk_iodomain.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
38 
39 #include <dev/extres/syscon/syscon.h>
40 #include <dev/extres/regulator/regulator.h>
41 
42 #include "syscon_if.h"
43 
44 #define	RK3288_GRF_IO_VSEL		0x380
45 #define	RK3399_GRF_IO_VSEL		0xe640
46 #define	RK3399_PMUGRF_SOC_CON0		0x180
47 
48 struct rk_iodomain_supply {
49 	char		*name;
50 	uint32_t	bit;
51 };
52 
53 struct rk_iodomain_softc;
54 
55 struct rk_iodomain_conf {
56 	struct rk_iodomain_supply	*supply;
57 	int				nsupply;
58 	uint32_t			grf_reg;
59 	void				(*init)(struct rk_iodomain_softc *sc);
60 };
61 
62 struct rk_iodomain_softc {
63 	device_t			dev;
64 	struct syscon			*grf;
65 	phandle_t			node;
66 	struct rk_iodomain_conf		*conf;
67 };
68 
69 static struct rk_iodomain_supply rk3288_supply[] = {
70 	{"lcdc-supply", 0},
71 	{"dvp-supply", 1},
72 	{"flash0-supply", 2},
73 	{"flash1-supply", 3},
74 	{"wifi-supply", 4},
75 	{"bb-supply", 5},
76 	{"audio-supply", 6},
77 	{"sdcard-supply", 7},
78 	{"gpio30-supply", 8},
79 	{"gpio1830-supply", 9},
80 };
81 
82 static struct rk_iodomain_conf rk3288_conf = {
83 	.supply = rk3288_supply,
84 	.nsupply = nitems(rk3288_supply),
85 	.grf_reg = RK3288_GRF_IO_VSEL,
86 };
87 
88 static struct rk_iodomain_supply rk3399_supply[] = {
89 	{"bt656-supply", 0},
90 	{"audio-supply", 1},
91 	{"sdmmc-supply", 2},
92 	{"gpio1830-supply", 3},
93 };
94 
95 static struct rk_iodomain_conf rk3399_conf = {
96 	.supply = rk3399_supply,
97 	.nsupply = nitems(rk3399_supply),
98 	.grf_reg = RK3399_GRF_IO_VSEL,
99 };
100 
101 static struct rk_iodomain_supply rk3399_pmu_supply[] = {
102 	{"pmu1830-supply", 9},
103 };
104 
105 static void rk3399_pmu_init(struct rk_iodomain_softc *sc);
106 static struct rk_iodomain_conf rk3399_pmu_conf = {
107 	.supply = rk3399_pmu_supply,
108 	.nsupply = nitems(rk3399_pmu_supply),
109 	.grf_reg = RK3399_PMUGRF_SOC_CON0,
110 	.init = rk3399_pmu_init,
111 };
112 
113 static struct ofw_compat_data compat_data[] = {
114 	{"rockchip,rk3288-io-voltage-domain", (uintptr_t)&rk3288_conf},
115 	{"rockchip,rk3399-io-voltage-domain", (uintptr_t)&rk3399_conf},
116 	{"rockchip,rk3399-pmu-io-voltage-domain", (uintptr_t)&rk3399_pmu_conf},
117 	{NULL,             0}
118 };
119 
120 static void
rk3399_pmu_init(struct rk_iodomain_softc * sc)121 rk3399_pmu_init(struct rk_iodomain_softc *sc)
122 {
123 
124 	SYSCON_WRITE_4(sc->grf, RK3399_PMUGRF_SOC_CON0,
125 	    (1 << 8) | (1 << (8 + 16)));	/* set pmu1830_volsel */
126 }
127 
128 static void
rk_iodomain_set(struct rk_iodomain_softc * sc)129 rk_iodomain_set(struct rk_iodomain_softc *sc)
130 {
131 	regulator_t supply;
132 	uint32_t reg = 0;
133 	uint32_t mask = 0;
134 	int uvolt, i;
135 
136 	for (i = 0; i < sc->conf->nsupply; i++) {
137 		mask |= (1 << sc->conf->supply[i].bit) << 16;
138 		if (regulator_get_by_ofw_property(sc->dev, sc->node,
139 		    sc->conf->supply[i].name, &supply) == 0) {
140 			if (regulator_get_voltage(supply, &uvolt) == 0) {
141 				if (uvolt == 1800000)
142 					reg |= (1 << sc->conf->supply[i].bit);
143 				else if (uvolt != 3000000)
144 					device_printf(sc->dev,
145 					  "%s regulator is at %duV, ignoring\n",
146 					  sc->conf->supply[i].name, uvolt);
147 			} else
148 				device_printf(sc->dev, "Cannot get current "
149 				    "voltage for regulator %s\n",
150 				    sc->conf->supply[i].name);
151 		}
152 	}
153 
154 	SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask);
155 	if (sc->conf->init != NULL)
156 		 sc->conf->init(sc);
157 }
158 
159 static int
rk_iodomain_probe(device_t dev)160 rk_iodomain_probe(device_t dev)
161 {
162 
163 	if (!ofw_bus_status_okay(dev))
164 		return (ENXIO);
165 
166 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
167 		return (ENXIO);
168 
169 	device_set_desc(dev, "RockChip IO Voltage Domain");
170 	return (BUS_PROBE_DEFAULT);
171 }
172 
173 static int
rk_iodomain_attach(device_t dev)174 rk_iodomain_attach(device_t dev)
175 {
176 	struct rk_iodomain_softc *sc;
177 	int rv;
178 
179 	sc = device_get_softc(dev);
180 	sc->dev = dev;
181 	sc->node = ofw_bus_get_node(dev);
182 
183 	rv = syscon_get_handle_default(dev, &sc->grf);
184 	if (rv != 0) {
185 		device_printf(dev, "Cannot get grf handle\n");
186 		return (ENXIO);
187 	}
188 
189 	sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
190 	rk_iodomain_set(sc);
191 
192 	return (0);
193 }
194 
195 static int
rk_iodomain_detach(device_t dev)196 rk_iodomain_detach(device_t dev)
197 {
198 
199 	return (0);
200 }
201 
202 static device_method_t rk_iodomain_methods[] = {
203 	/* Device interface */
204 	DEVMETHOD(device_probe,		rk_iodomain_probe),
205 	DEVMETHOD(device_attach,	rk_iodomain_attach),
206 	DEVMETHOD(device_detach,	rk_iodomain_detach),
207 
208 	DEVMETHOD_END
209 };
210 
211 static driver_t rk_iodomain_driver = {
212 	"rk_iodomain",
213 	rk_iodomain_methods,
214 	sizeof(struct rk_iodomain_softc),
215 };
216 
217 static devclass_t rk_iodomain_devclass;
218 
219 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver,
220   rk_iodomain_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
221