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