xref: /freebsd/sys/arm64/rockchip/rk_iodomain.c (revision b0d29bc47dba79f6f38e67eabadfb4b32ffd9390)
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_IO_VSEL		0x180
49 
50 struct rk_iodomain_supply {
51 	char		*name;
52 	uint32_t	bit;
53 };
54 
55 struct rk_iodomain_conf {
56 	struct rk_iodomain_supply	*supply;
57 	int				nsupply;
58 	uint32_t			grf_reg;
59 };
60 
61 struct rk_iodomain_softc {
62 	device_t			dev;
63 	struct syscon			*grf;
64 	phandle_t			node;
65 	struct rk_iodomain_conf		*conf;
66 };
67 
68 static struct rk_iodomain_supply rk3288_supply[] = {
69 	{"lcdc-supply", 0},
70 	{"dvp-supply", 1},
71 	{"flash0-supply", 2},
72 	{"flash1-supply", 3},
73 	{"wifi-supply", 4},
74 	{"bb-supply", 5},
75 	{"audio-supply", 6},
76 	{"sdcard-supply", 7},
77 	{"gpio30-supply", 8},
78 	{"gpio1830-supply", 9},
79 };
80 
81 static struct rk_iodomain_conf rk3288_conf = {
82 	.supply = rk3288_supply,
83 	.nsupply = nitems(rk3288_supply),
84 	.grf_reg = RK3288_GRF_IO_VSEL,
85 };
86 
87 static struct rk_iodomain_supply rk3399_supply[] = {
88 	{"bt656-supply", 0},
89 	{"audio-supply", 1},
90 	{"sdmmc-supply", 2},
91 	{"gpio1830-supply", 3},
92 };
93 
94 static struct rk_iodomain_conf rk3399_conf = {
95 	.supply = rk3399_supply,
96 	.nsupply = nitems(rk3399_supply),
97 	.grf_reg = RK3399_GRF_IO_VSEL,
98 };
99 
100 static struct rk_iodomain_supply rk3399_pmu_supply[] = {
101 	{"pmu1830-supply", 9},
102 };
103 
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,
108 };
109 
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},
114 	{NULL,             0}
115 };
116 
117 static void
118 rk_iodomain_set(struct rk_iodomain_softc *sc)
119 {
120 	regulator_t supply;
121 	uint32_t reg = 0;
122 	uint32_t mask = 0;
123 	int uvolt, i;
124 
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);
136 			} else
137 				device_printf(sc->dev, "Cannot get current "
138 				    "voltage for regulator %s\n",
139 				    sc->conf->supply[i].name);
140 		}
141 	}
142 
143 	SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask);
144 }
145 
146 static int
147 rk_iodomain_probe(device_t dev)
148 {
149 
150 	if (!ofw_bus_status_okay(dev))
151 		return (ENXIO);
152 
153 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
154 		return (ENXIO);
155 
156 	device_set_desc(dev, "RockChip IO Voltage Domain");
157 	return (BUS_PROBE_DEFAULT);
158 }
159 
160 static int
161 rk_iodomain_attach(device_t dev)
162 {
163 	struct rk_iodomain_softc *sc;
164 	int rv;
165 
166 	sc = device_get_softc(dev);
167 	sc->dev = dev;
168 	sc->node = ofw_bus_get_node(dev);
169 
170 	rv = syscon_get_handle_default(dev, &sc->grf);
171 	if (rv != 0) {
172 		device_printf(dev, "Cannot get grf handle\n");
173 		return (ENXIO);
174 	}
175 
176 	sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
177 	rk_iodomain_set(sc);
178 
179 	return (0);
180 }
181 
182 static int
183 rk_iodomain_detach(device_t dev)
184 {
185 
186 	return (0);
187 }
188 
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),
194 
195 	DEVMETHOD_END
196 };
197 
198 static driver_t rk_iodomain_driver = {
199 	"rk_iodomain",
200 	rk_iodomain_methods,
201 	sizeof(struct rk_iodomain_softc),
202 };
203 
204 static devclass_t rk_iodomain_devclass;
205 
206 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver,
207   rk_iodomain_devclass, 0, 0, BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
208