xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk808.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2018-2021 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 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/bus.h>
31 #include <sys/clock.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 #include <sys/rman.h>
36 #include <machine/bus.h>
37 
38 #include <dev/iicbus/iiconf.h>
39 #include <dev/iicbus/iicbus.h>
40 
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include <dev/iicbus/pmic/rockchip/rk808reg.h>
45 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
46 
47 static struct ofw_compat_data compat_data[] = {
48 	{"rockchip,rk808", RK808},
49 	{NULL,             0}
50 };
51 
52 static struct rk8xx_regdef rk808_regdefs[] = {
53 	{
54 		.id = RK808_BUCK1,
55 		.name = "DCDC_REG1",
56 		.enable_reg = RK808_DCDC_EN,
57 		.enable_mask = 0x1,
58 		.voltage_reg = RK808_BUCK1_ON_VSEL,
59 		.voltage_mask = 0x3F,
60 		.voltage_min = 712500,
61 		.voltage_max = 1500000,
62 		.voltage_step = 12500,
63 		.voltage_nstep = 64,
64 	},
65 	{
66 		.id = RK808_BUCK2,
67 		.name = "DCDC_REG2",
68 		.enable_reg = RK808_DCDC_EN,
69 		.enable_mask = 0x2,
70 		.voltage_reg = RK808_BUCK2_ON_VSEL,
71 		.voltage_mask = 0x3F,
72 		.voltage_min = 712500,
73 		.voltage_max = 1500000,
74 		.voltage_step = 12500,
75 		.voltage_nstep = 64,
76 	},
77 	{
78 		/* BUCK3 voltage is calculated based on external resistor */
79 		.id = RK808_BUCK3,
80 		.name = "DCDC_REG3",
81 		.enable_reg = RK808_DCDC_EN,
82 		.enable_mask = 0x4,
83 	},
84 	{
85 		.id = RK808_BUCK4,
86 		.name = "DCDC_REG4",
87 		.enable_reg = RK808_DCDC_EN,
88 		.enable_mask = 0x8,
89 		.voltage_reg = RK808_BUCK4_ON_VSEL,
90 		.voltage_mask = 0xF,
91 		.voltage_min = 1800000,
92 		.voltage_max = 3300000,
93 		.voltage_step = 100000,
94 		.voltage_nstep = 16,
95 	},
96 	{
97 		.id = RK808_LDO1,
98 		.name = "LDO_REG1",
99 		.enable_reg = RK808_LDO_EN,
100 		.enable_mask = 0x1,
101 		.voltage_reg = RK808_LDO1_ON_VSEL,
102 		.voltage_mask = 0x1F,
103 		.voltage_min = 1800000,
104 		.voltage_max = 3400000,
105 		.voltage_step = 100000,
106 		.voltage_nstep = 17,
107 	},
108 	{
109 		.id = RK808_LDO2,
110 		.name = "LDO_REG2",
111 		.enable_reg = RK808_LDO_EN,
112 		.enable_mask = 0x2,
113 		.voltage_reg = RK808_LDO2_ON_VSEL,
114 		.voltage_mask = 0x1F,
115 		.voltage_min = 1800000,
116 		.voltage_max = 3400000,
117 		.voltage_step = 100000,
118 		.voltage_nstep = 17,
119 	},
120 	{
121 		.id = RK808_LDO3,
122 		.name = "LDO_REG3",
123 		.enable_reg = RK808_LDO_EN,
124 		.enable_mask = 0x4,
125 		.voltage_reg = RK808_LDO3_ON_VSEL,
126 		.voltage_mask = 0xF,
127 		.voltage_min = 800000,
128 		.voltage_max = 2500000,
129 		.voltage_step = 100000,
130 		.voltage_nstep = 18,
131 	},
132 	{
133 		.id = RK808_LDO4,
134 		.name = "LDO_REG4",
135 		.enable_reg = RK808_LDO_EN,
136 		.enable_mask = 0x8,
137 		.voltage_reg = RK808_LDO4_ON_VSEL,
138 		.voltage_mask = 0x1F,
139 		.voltage_min = 1800000,
140 		.voltage_max = 3400000,
141 		.voltage_step = 100000,
142 		.voltage_nstep = 17,
143 	},
144 	{
145 		.id = RK808_LDO5,
146 		.name = "LDO_REG5",
147 		.enable_reg = RK808_LDO_EN,
148 		.enable_mask = 0x10,
149 		.voltage_reg = RK808_LDO5_ON_VSEL,
150 		.voltage_mask = 0x1F,
151 		.voltage_min = 1800000,
152 		.voltage_max = 3400000,
153 		.voltage_step = 100000,
154 		.voltage_nstep = 17,
155 	},
156 	{
157 		.id = RK808_LDO6,
158 		.name = "LDO_REG6",
159 		.enable_reg = RK808_LDO_EN,
160 		.enable_mask = 0x20,
161 		.voltage_reg = RK808_LDO6_ON_VSEL,
162 		.voltage_mask = 0x1F,
163 		.voltage_min = 800000,
164 		.voltage_max = 2500000,
165 		.voltage_step = 100000,
166 		.voltage_nstep = 18,
167 	},
168 	{
169 		.id = RK808_LDO7,
170 		.name = "LDO_REG7",
171 		.enable_reg = RK808_LDO_EN,
172 		.enable_mask = 0x40,
173 		.voltage_reg = RK808_LDO7_ON_VSEL,
174 		.voltage_mask = 0x1F,
175 		.voltage_min = 800000,
176 		.voltage_max = 2500000,
177 		.voltage_step = 100000,
178 		.voltage_nstep = 18,
179 	},
180 	{
181 		.id = RK808_LDO8,
182 		.name = "LDO_REG8",
183 		.enable_reg = RK808_LDO_EN,
184 		.enable_mask = 0x80,
185 		.voltage_reg = RK808_LDO8_ON_VSEL,
186 		.voltage_mask = 0x1F,
187 		.voltage_min = 1800000,
188 		.voltage_max = 3400000,
189 		.voltage_step = 100000,
190 		.voltage_nstep = 17,
191 	},
192 	{
193 		.id = RK808_SWITCH1,
194 		.name = "SWITCH_REG1",
195 		.enable_reg = RK808_DCDC_EN,
196 		.enable_mask = 0x20,
197 		.voltage_min = 3000000,
198 		.voltage_max = 3000000,
199 	},
200 	{
201 		.id = RK808_SWITCH2,
202 		.name = "SWITCH_REG2",
203 		.enable_reg = RK808_DCDC_EN,
204 		.enable_mask = 0x40,
205 		.voltage_min = 3000000,
206 		.voltage_max = 3000000,
207 	},
208 };
209 
210 static int
211 rk808_probe(device_t dev)
212 {
213 	if (!ofw_bus_status_okay(dev))
214 		return (ENXIO);
215 
216 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
217 		return (ENXIO);
218 
219 	device_set_desc(dev, "RockChip RK808 PMIC");
220 	return (BUS_PROBE_DEFAULT);
221 }
222 
223 static int
224 rk808_attach(device_t dev)
225 {
226 	struct rk8xx_softc *sc;
227 
228 	sc = device_get_softc(dev);
229 	sc->dev = dev;
230 
231 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
232 
233 	sc->regdefs = rk808_regdefs;
234 	sc->nregs = nitems(rk808_regdefs);
235 	sc->rtc_regs.secs = RK808_RTC_SECS;
236 	sc->rtc_regs.secs_mask = RK808_RTC_SECS_MASK;
237 	sc->rtc_regs.minutes = RK808_RTC_MINUTES;
238 	sc->rtc_regs.minutes_mask = RK808_RTC_MINUTES_MASK;
239 	sc->rtc_regs.hours = RK808_RTC_HOURS;
240 	sc->rtc_regs.hours_mask = RK808_RTC_HOURS_MASK;
241 	sc->rtc_regs.days = RK808_RTC_DAYS;
242 	sc->rtc_regs.days_mask = RK808_RTC_DAYS_MASK;
243 	sc->rtc_regs.months = RK808_RTC_MONTHS;
244 	sc->rtc_regs.months_mask = RK808_RTC_MONTHS_MASK;
245 	sc->rtc_regs.years = RK808_RTC_YEARS;
246 	sc->rtc_regs.weeks = RK808_RTC_WEEKS_MASK;
247 	sc->rtc_regs.ctrl = RK808_RTC_CTRL;
248 	sc->rtc_regs.ctrl_stop_mask = RK808_RTC_CTRL_STOP;
249 	sc->rtc_regs.ctrl_ampm_mask = RK808_RTC_AMPM_MODE;
250 	sc->rtc_regs.ctrl_gettime_mask = RK808_RTC_GET_TIME;
251 	sc->rtc_regs.ctrl_readsel_mask = RK808_RTC_READSEL;
252 	sc->dev_ctrl.dev_ctrl_reg = RK808_DEV_CTRL;
253 	sc->dev_ctrl.pwr_off_mask = RK808_DEV_CTRL_OFF;
254 
255 	return (rk8xx_attach(sc));
256 }
257 
258 static device_method_t rk808_methods[] = {
259 	DEVMETHOD(device_probe,		rk808_probe),
260 	DEVMETHOD(device_attach,	rk808_attach),
261 
262 	DEVMETHOD_END
263 };
264 
265 DEFINE_CLASS_1(rk808_pmu, rk808_driver, rk808_methods,
266     sizeof(struct rk8xx_softc), rk8xx_driver);
267 
268 EARLY_DRIVER_MODULE(rk808_pmu, iicbus, rk808_driver, 0, 0,
269     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
270 EARLY_DRIVER_MODULE(iicbus, rk808_pmu, iicbus_driver, 0, 0,
271     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
272 MODULE_DEPEND(rk808_pmu, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
273 MODULE_VERSION(rk808_pmu, 1);
274