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