xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk805.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/clock.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/reboot.h>
38 #include <sys/mutex.h>
39 #include <sys/rman.h>
40 #include <machine/bus.h>
41 
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44 
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 
48 #include <dev/iicbus/pmic/rockchip/rk805reg.h>
49 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
50 
51 static struct ofw_compat_data compat_data[] = {
52 	{"rockchip,rk805", RK805},
53 	{NULL,             0}
54 };
55 
56 static struct rk8xx_regdef rk805_regdefs[] = {
57 	{
58 		.id = RK805_BUCK1,
59 		.name = "DCDC_REG1",
60 		.enable_reg = RK805_DCDC_EN,
61 		.enable_mask = 0x11,
62 		.voltage_reg = RK805_BUCK1_ON_VSEL,
63 		.voltage_mask = 0x3F,
64 		.voltage_min = 712500,
65 		.voltage_max = 1450000,
66 		.voltage_step = 12500,
67 		.voltage_nstep = 64,
68 	},
69 	{
70 		.id = RK805_BUCK2,
71 		.name = "DCDC_REG2",
72 		.enable_reg = RK805_DCDC_EN,
73 		.enable_mask = 0x22,
74 		.voltage_reg = RK805_BUCK2_ON_VSEL,
75 		.voltage_mask = 0x3F,
76 		.voltage_min = 712500,
77 		.voltage_max = 1450000,
78 		.voltage_step = 12500,
79 		.voltage_nstep = 64,
80 	},
81 	{
82 		.id = RK805_BUCK3,
83 		.name = "DCDC_REG3",
84 		.enable_reg = RK805_DCDC_EN,
85 		.enable_mask = 0x44,
86 	},
87 	{
88 		.id = RK805_BUCK4,
89 		.name = "DCDC_REG4",
90 		.enable_reg = RK805_DCDC_EN,
91 		.enable_mask = 0x88,
92 		.voltage_reg = RK805_BUCK4_ON_VSEL,
93 		.voltage_mask = 0x3F,
94 		.voltage_min = 800000,
95 		.voltage_max = 3500000,
96 		.voltage_step = 100000,
97 		.voltage_nstep = 28,
98 	},
99 	{
100 		.id = RK805_LDO1,
101 		.name = "LDO_REG1",
102 		.enable_reg = RK805_LDO_EN,
103 		.enable_mask = 0x11,
104 		.voltage_reg = RK805_LDO1_ON_VSEL,
105 		.voltage_mask = 0x1F,
106 		.voltage_min = 800000,
107 		.voltage_max = 3400000,
108 		.voltage_step = 100000,
109 		.voltage_nstep = 27,
110 	},
111 	{
112 		.id = RK805_LDO2,
113 		.name = "LDO_REG2",
114 		.enable_reg = RK805_LDO_EN,
115 		.enable_mask = 0x22,
116 		.voltage_reg = RK805_LDO2_ON_VSEL,
117 		.voltage_mask = 0x1F,
118 		.voltage_min = 800000,
119 		.voltage_max = 3400000,
120 		.voltage_step = 100000,
121 		.voltage_nstep = 27,
122 	},
123 	{
124 		.id = RK805_LDO3,
125 		.name = "LDO_REG3",
126 		.enable_reg = RK805_LDO_EN,
127 		.enable_mask = 0x44,
128 		.voltage_reg = RK805_LDO3_ON_VSEL,
129 		.voltage_mask = 0x1F,
130 		.voltage_min = 800000,
131 		.voltage_max = 3400000,
132 		.voltage_step = 100000,
133 		.voltage_nstep = 27,
134 	},
135 };
136 
137 static int
138 rk805_probe(device_t dev)
139 {
140 	if (!ofw_bus_status_okay(dev))
141 		return (ENXIO);
142 
143 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
144 		return (ENXIO);
145 
146 	device_set_desc(dev, "RockChip RK805 PMIC");
147 	return (BUS_PROBE_DEFAULT);
148 }
149 
150 static int
151 rk805_attach(device_t dev)
152 {
153 	struct rk8xx_softc *sc;
154 
155 	sc = device_get_softc(dev);
156 	sc->dev = dev;
157 
158 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
159 
160 	sc->regdefs = rk805_regdefs;
161 	sc->nregs = nitems(rk805_regdefs);
162 	sc->rtc_regs.secs = RK805_RTC_SECS;
163 	sc->rtc_regs.secs_mask = RK805_RTC_SECS_MASK;
164 	sc->rtc_regs.minutes = RK805_RTC_MINUTES;
165 	sc->rtc_regs.minutes_mask = RK805_RTC_MINUTES_MASK;
166 	sc->rtc_regs.hours = RK805_RTC_HOURS;
167 	sc->rtc_regs.hours_mask = RK805_RTC_HOURS_MASK;
168 	sc->rtc_regs.days = RK805_RTC_DAYS;
169 	sc->rtc_regs.days_mask = RK805_RTC_DAYS_MASK;
170 	sc->rtc_regs.months = RK805_RTC_MONTHS;
171 	sc->rtc_regs.months_mask = RK805_RTC_MONTHS_MASK;
172 	sc->rtc_regs.years = RK805_RTC_YEARS;
173 	sc->rtc_regs.weeks = RK805_RTC_WEEKS_MASK;
174 	sc->rtc_regs.ctrl = RK805_RTC_CTRL;
175 	sc->rtc_regs.ctrl_stop_mask = RK805_RTC_CTRL_STOP;
176 	sc->rtc_regs.ctrl_ampm_mask = RK805_RTC_AMPM_MODE;
177 	sc->rtc_regs.ctrl_gettime_mask = RK805_RTC_GET_TIME;
178 	sc->rtc_regs.ctrl_readsel_mask = RK805_RTC_READSEL;
179 	sc->dev_ctrl.dev_ctrl_reg = RK805_DEV_CTRL;
180 	sc->dev_ctrl.pwr_off_mask = RK805_DEV_CTRL_OFF;
181 
182 	return (rk8xx_attach(sc));
183 }
184 
185 static device_method_t rk805_methods[] = {
186 	DEVMETHOD(device_probe,		rk805_probe),
187 	DEVMETHOD(device_attach,	rk805_attach),
188 
189 	DEVMETHOD_END
190 };
191 
192 DEFINE_CLASS_1(rk805_pmu, rk805_driver, rk805_methods,
193     sizeof(struct rk8xx_softc), rk8xx_driver);
194 
195 EARLY_DRIVER_MODULE(rk805_pmu, iicbus, rk805_driver, 0, 0,
196     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
197 EARLY_DRIVER_MODULE(iicbus, rk805_pmu, iicbus_driver, 0, 0,
198     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
199 MODULE_DEPEND(rk805_pmu, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
200 MODULE_VERSION(rk805_pmu, 1);
201