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