xref: /freebsd/sys/arm64/rockchip/rk_iodomain.c (revision 8cc087a1eee9ec1ca9f7ac1e63ad51bdb5a682eb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 
41 #include <dev/extres/syscon/syscon.h>
42 #include <dev/extres/regulator/regulator.h>
43 
44 #include "syscon_if.h"
45 
46 #define	RK3288_GRF_IO_VSEL		0x380
47 #define	RK3399_GRF_IO_VSEL		0xe640
48 #define	RK3399_PMUGRF_SOC_CON0		0x180
49 
50 struct rk_iodomain_supply {
51 	char		*name;
52 	uint32_t	bit;
53 };
54 
55 struct rk_iodomain_softc;
56 
57 struct rk_iodomain_conf {
58 	struct rk_iodomain_supply	*supply;
59 	int				nsupply;
60 	uint32_t			grf_reg;
61 	void				(*init)(struct rk_iodomain_softc *sc);
62 };
63 
64 struct rk_iodomain_softc {
65 	device_t			dev;
66 	struct syscon			*grf;
67 	phandle_t			node;
68 	struct rk_iodomain_conf		*conf;
69 };
70 
71 static struct rk_iodomain_supply rk3288_supply[] = {
72 	{"lcdc-supply", 0},
73 	{"dvp-supply", 1},
74 	{"flash0-supply", 2},
75 	{"flash1-supply", 3},
76 	{"wifi-supply", 4},
77 	{"bb-supply", 5},
78 	{"audio-supply", 6},
79 	{"sdcard-supply", 7},
80 	{"gpio30-supply", 8},
81 	{"gpio1830-supply", 9},
82 };
83 
84 static struct rk_iodomain_conf rk3288_conf = {
85 	.supply = rk3288_supply,
86 	.nsupply = nitems(rk3288_supply),
87 	.grf_reg = RK3288_GRF_IO_VSEL,
88 };
89 
90 static struct rk_iodomain_supply rk3399_supply[] = {
91 	{"bt656-supply", 0},
92 	{"audio-supply", 1},
93 	{"sdmmc-supply", 2},
94 	{"gpio1830-supply", 3},
95 };
96 
97 static struct rk_iodomain_conf rk3399_conf = {
98 	.supply = rk3399_supply,
99 	.nsupply = nitems(rk3399_supply),
100 	.grf_reg = RK3399_GRF_IO_VSEL,
101 };
102 
103 static struct rk_iodomain_supply rk3399_pmu_supply[] = {
104 	{"pmu1830-supply", 9},
105 };
106 
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,
113 };
114 
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},
119 	{NULL,             0}
120 };
121 
122 static void
123 rk3399_pmu_init(struct rk_iodomain_softc *sc)
124 {
125 
126 	SYSCON_WRITE_4(sc->grf, RK3399_PMUGRF_SOC_CON0,
127 	    (1 << 8) | (1 << (8 + 16)));	/* set pmu1830_volsel */
128 }
129 
130 static void
131 rk_iodomain_set(struct rk_iodomain_softc *sc)
132 {
133 	regulator_t supply;
134 	uint32_t reg = 0;
135 	uint32_t mask = 0;
136 	int uvolt, i;
137 
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);
149 			} else
150 				device_printf(sc->dev, "Cannot get current "
151 				    "voltage for regulator %s\n",
152 				    sc->conf->supply[i].name);
153 		}
154 	}
155 
156 	SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask);
157 	if (sc->conf->init != NULL)
158 		 sc->conf->init(sc);
159 }
160 
161 static int
162 rk_iodomain_probe(device_t dev)
163 {
164 
165 	if (!ofw_bus_status_okay(dev))
166 		return (ENXIO);
167 
168 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
169 		return (ENXIO);
170 
171 	device_set_desc(dev, "RockChip IO Voltage Domain");
172 	return (BUS_PROBE_DEFAULT);
173 }
174 
175 static int
176 rk_iodomain_attach(device_t dev)
177 {
178 	struct rk_iodomain_softc *sc;
179 	int rv;
180 
181 	sc = device_get_softc(dev);
182 	sc->dev = dev;
183 	sc->node = ofw_bus_get_node(dev);
184 
185 	rv = syscon_get_handle_default(dev, &sc->grf);
186 	if (rv != 0) {
187 		device_printf(dev, "Cannot get grf handle\n");
188 		return (ENXIO);
189 	}
190 
191 	sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
192 	rk_iodomain_set(sc);
193 
194 	return (0);
195 }
196 
197 static int
198 rk_iodomain_detach(device_t dev)
199 {
200 
201 	return (0);
202 }
203 
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),
209 
210 	DEVMETHOD_END
211 };
212 
213 static driver_t rk_iodomain_driver = {
214 	"rk_iodomain",
215 	rk_iodomain_methods,
216 	sizeof(struct rk_iodomain_softc),
217 };
218 
219 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver, 0, 0,
220     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
221