as3722.c revision 1.7 1 1.7 jmcneill /* $NetBSD: as3722.c,v 1.7 2017/04/22 21:48:56 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.6 jmcneill #include "opt_fdt.h"
30 1.6 jmcneill
31 1.1 jmcneill #include <sys/cdefs.h>
32 1.7 jmcneill __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.7 2017/04/22 21:48:56 jmcneill Exp $");
33 1.1 jmcneill
34 1.1 jmcneill #include <sys/param.h>
35 1.1 jmcneill #include <sys/systm.h>
36 1.1 jmcneill #include <sys/kernel.h>
37 1.1 jmcneill #include <sys/device.h>
38 1.1 jmcneill #include <sys/conf.h>
39 1.1 jmcneill #include <sys/bus.h>
40 1.1 jmcneill #include <sys/kmem.h>
41 1.2 jmcneill #include <sys/wdog.h>
42 1.2 jmcneill
43 1.6 jmcneill #include <dev/clock_subr.h>
44 1.6 jmcneill
45 1.2 jmcneill #include <dev/sysmon/sysmonvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/i2c/i2cvar.h>
48 1.1 jmcneill #include <dev/i2c/as3722.h>
49 1.1 jmcneill
50 1.6 jmcneill #ifdef FDT
51 1.6 jmcneill #include <dev/fdt/fdtvar.h>
52 1.6 jmcneill #endif
53 1.6 jmcneill
54 1.6 jmcneill #define AS3722_START_YEAR 2000
55 1.6 jmcneill
56 1.2 jmcneill #define AS3722_GPIO0_CTRL_REG 0x08
57 1.2 jmcneill #define AS3722_GPIO0_CTRL_INVERT __BIT(7)
58 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF __BITS(6,3)
59 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF_GPIO 0
60 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG 9
61 1.2 jmcneill #define AS3722_GPIO0_CTRL_MODE __BITS(2,0)
62 1.2 jmcneill #define AS3722_GPIO0_CTRL_MODE_PULLDOWN 5
63 1.2 jmcneill
64 1.7 jmcneill #define AS3722_LDO6_VOLTAGE_REG 0x16
65 1.7 jmcneill
66 1.1 jmcneill #define AS3722_RESET_CTRL_REG 0x36
67 1.1 jmcneill #define AS3722_RESET_CTRL_POWER_OFF __BIT(1)
68 1.3 jmcneill #define AS3722_RESET_CTRL_FORCE_RESET __BIT(0)
69 1.1 jmcneill
70 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_REG 0x38
71 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_MODE __BITS(2,1)
72 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_ON __BIT(0)
73 1.2 jmcneill
74 1.2 jmcneill #define AS3722_WATCHDOG_TIMER_REG 0x46
75 1.2 jmcneill #define AS3722_WATCHDOG_TIMER_TIMER __BITS(6,0)
76 1.2 jmcneill
77 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_REG 0x48
78 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_PWM_DIV __BITS(7,6)
79 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_SW_SIG __BIT(0)
80 1.2 jmcneill
81 1.7 jmcneill #define AS3722_LDOCONTROL0_REG 0x4e
82 1.7 jmcneill
83 1.6 jmcneill #define AS3722_RTC_CONTROL_REG 0x60
84 1.6 jmcneill #define AS3722_RTC_CONTROL_RTC_ON __BIT(2)
85 1.6 jmcneill
86 1.6 jmcneill #define AS3722_RTC_SECOND_REG 0x61
87 1.6 jmcneill #define AS3722_RTC_MINUTE_REG 0x62
88 1.6 jmcneill #define AS3722_RTC_HOUR_REG 0x63
89 1.6 jmcneill #define AS3722_RTC_DAY_REG 0x64
90 1.6 jmcneill #define AS3722_RTC_MONTH_REG 0x65
91 1.6 jmcneill #define AS3722_RTC_YEAR_REG 0x66
92 1.6 jmcneill #define AS3722_RTC_ACCESS_REG 0x6f
93 1.6 jmcneill
94 1.1 jmcneill #define AS3722_ASIC_ID1_REG 0x90
95 1.1 jmcneill #define AS3722_ASIC_ID2_REG 0x91
96 1.1 jmcneill
97 1.1 jmcneill struct as3722_softc {
98 1.1 jmcneill device_t sc_dev;
99 1.1 jmcneill i2c_tag_t sc_i2c;
100 1.1 jmcneill i2c_addr_t sc_addr;
101 1.6 jmcneill int sc_phandle;
102 1.2 jmcneill
103 1.2 jmcneill struct sysmon_wdog sc_smw;
104 1.6 jmcneill struct todr_chip_handle sc_todr;
105 1.1 jmcneill };
106 1.1 jmcneill
107 1.7 jmcneill #ifdef FDT
108 1.7 jmcneill static const struct as3722regdef {
109 1.7 jmcneill const char *name;
110 1.7 jmcneill u_int vsel_reg;
111 1.7 jmcneill u_int vsel_mask;
112 1.7 jmcneill u_int enable_reg;
113 1.7 jmcneill u_int enable_mask;
114 1.7 jmcneill u_int n_voltages;
115 1.7 jmcneill } as3722regdefs[] = {
116 1.7 jmcneill { .name = "ldo6",
117 1.7 jmcneill .vsel_reg = AS3722_LDO6_VOLTAGE_REG,
118 1.7 jmcneill .vsel_mask = 0x7f,
119 1.7 jmcneill .enable_reg = AS3722_LDOCONTROL0_REG,
120 1.7 jmcneill .enable_mask = 0x40,
121 1.7 jmcneill .n_voltages = 0x80 },
122 1.7 jmcneill };
123 1.7 jmcneill
124 1.7 jmcneill struct as3722reg_softc {
125 1.7 jmcneill device_t sc_dev;
126 1.7 jmcneill int sc_phandle;
127 1.7 jmcneill const struct as3722regdef *sc_regdef;
128 1.7 jmcneill };
129 1.7 jmcneill
130 1.7 jmcneill struct as3722reg_attach_args {
131 1.7 jmcneill const struct as3722regdef *reg_def;
132 1.7 jmcneill int reg_phandle;
133 1.7 jmcneill };
134 1.7 jmcneill #endif
135 1.7 jmcneill
136 1.2 jmcneill #define AS3722_WATCHDOG_DEFAULT_PERIOD 10
137 1.2 jmcneill
138 1.1 jmcneill static int as3722_match(device_t, cfdata_t, void *);
139 1.1 jmcneill static void as3722_attach(device_t, device_t, void *);
140 1.1 jmcneill
141 1.6 jmcneill static void as3722_wdt_attach(struct as3722_softc *);
142 1.2 jmcneill static int as3722_wdt_setmode(struct sysmon_wdog *);
143 1.2 jmcneill static int as3722_wdt_tickle(struct sysmon_wdog *);
144 1.2 jmcneill
145 1.6 jmcneill static void as3722_rtc_attach(struct as3722_softc *);
146 1.6 jmcneill static int as3722_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
147 1.6 jmcneill static int as3722_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
148 1.6 jmcneill
149 1.7 jmcneill #ifdef FDT
150 1.7 jmcneill static void as3722_regulator_attach(struct as3722_softc *);
151 1.7 jmcneill static int as3722reg_match(device_t, cfdata_t, void *);
152 1.7 jmcneill static void as3722reg_attach(device_t, device_t, void *);
153 1.7 jmcneill
154 1.7 jmcneill static int as3722reg_acquire(device_t);
155 1.7 jmcneill static void as3722reg_release(device_t);
156 1.7 jmcneill static int as3722reg_enable(device_t, bool);
157 1.7 jmcneill static int as3722reg_set_voltage(device_t, u_int, u_int);
158 1.7 jmcneill static int as3722reg_get_voltage(device_t, u_int *);
159 1.7 jmcneill
160 1.7 jmcneill static struct fdtbus_regulator_controller_func as3722reg_funcs = {
161 1.7 jmcneill .acquire = as3722reg_acquire,
162 1.7 jmcneill .release = as3722reg_release,
163 1.7 jmcneill .enable = as3722reg_enable,
164 1.7 jmcneill .set_voltage = as3722reg_set_voltage,
165 1.7 jmcneill .get_voltage = as3722reg_get_voltage,
166 1.7 jmcneill };
167 1.7 jmcneill #endif
168 1.7 jmcneill
169 1.1 jmcneill static int as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
170 1.1 jmcneill static int as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
171 1.2 jmcneill static int as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
172 1.2 jmcneill uint8_t, int);
173 1.1 jmcneill
174 1.1 jmcneill CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
175 1.1 jmcneill as3722_match, as3722_attach, NULL, NULL);
176 1.1 jmcneill
177 1.7 jmcneill #ifdef FDT
178 1.7 jmcneill CFATTACH_DECL_NEW(as3722reg, sizeof(struct as3722reg_softc),
179 1.7 jmcneill as3722reg_match, as3722reg_attach, NULL, NULL);
180 1.7 jmcneill #endif
181 1.7 jmcneill
182 1.4 jmcneill static const char * as3722_compats[] = {
183 1.4 jmcneill "ams,as3722",
184 1.4 jmcneill NULL
185 1.4 jmcneill };
186 1.4 jmcneill
187 1.1 jmcneill static int
188 1.1 jmcneill as3722_match(device_t parent, cfdata_t match, void *aux)
189 1.1 jmcneill {
190 1.1 jmcneill struct i2c_attach_args *ia = aux;
191 1.1 jmcneill uint8_t reg, id1;
192 1.1 jmcneill int error;
193 1.1 jmcneill
194 1.4 jmcneill if (ia->ia_name == NULL) {
195 1.4 jmcneill iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
196 1.4 jmcneill reg = AS3722_ASIC_ID1_REG;
197 1.4 jmcneill error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
198 1.4 jmcneill ®, 1, &id1, 1, I2C_F_POLL);
199 1.4 jmcneill iic_release_bus(ia->ia_tag, I2C_F_POLL);
200 1.4 jmcneill
201 1.4 jmcneill if (error == 0 && id1 == 0x0c)
202 1.4 jmcneill return 1;
203 1.4 jmcneill
204 1.4 jmcneill return 0;
205 1.4 jmcneill } else {
206 1.4 jmcneill return iic_compat_match(ia, as3722_compats);
207 1.4 jmcneill }
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill static void
211 1.1 jmcneill as3722_attach(device_t parent, device_t self, void *aux)
212 1.1 jmcneill {
213 1.1 jmcneill struct as3722_softc * const sc = device_private(self);
214 1.1 jmcneill struct i2c_attach_args *ia = aux;
215 1.1 jmcneill
216 1.1 jmcneill sc->sc_dev = self;
217 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
218 1.1 jmcneill sc->sc_addr = ia->ia_addr;
219 1.6 jmcneill sc->sc_phandle = ia->ia_cookie;
220 1.1 jmcneill
221 1.1 jmcneill aprint_naive("\n");
222 1.5 jakllsch aprint_normal(": AMS AS3722\n");
223 1.2 jmcneill
224 1.6 jmcneill as3722_wdt_attach(sc);
225 1.6 jmcneill as3722_rtc_attach(sc);
226 1.7 jmcneill #ifdef FDT
227 1.7 jmcneill as3722_regulator_attach(sc);
228 1.7 jmcneill #endif
229 1.6 jmcneill }
230 1.6 jmcneill
231 1.6 jmcneill static void
232 1.6 jmcneill as3722_wdt_attach(struct as3722_softc *sc)
233 1.6 jmcneill {
234 1.6 jmcneill int error;
235 1.6 jmcneill
236 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
237 1.2 jmcneill error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
238 1.2 jmcneill __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
239 1.2 jmcneill AS3722_GPIO0_CTRL_IOSF) |
240 1.2 jmcneill __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
241 1.2 jmcneill AS3722_GPIO0_CTRL_MODE),
242 1.2 jmcneill I2C_F_POLL);
243 1.2 jmcneill error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
244 1.2 jmcneill __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
245 1.2 jmcneill iic_release_bus(sc->sc_i2c, I2C_F_POLL);
246 1.2 jmcneill
247 1.6 jmcneill if (error) {
248 1.6 jmcneill aprint_error_dev(sc->sc_dev, "couldn't setup watchdog\n");
249 1.6 jmcneill return;
250 1.6 jmcneill }
251 1.2 jmcneill
252 1.6 jmcneill sc->sc_smw.smw_name = device_xname(sc->sc_dev);
253 1.2 jmcneill sc->sc_smw.smw_cookie = sc;
254 1.2 jmcneill sc->sc_smw.smw_setmode = as3722_wdt_setmode;
255 1.2 jmcneill sc->sc_smw.smw_tickle = as3722_wdt_tickle;
256 1.2 jmcneill sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
257 1.2 jmcneill
258 1.6 jmcneill aprint_normal_dev(sc->sc_dev, "default watchdog period is %u seconds\n",
259 1.2 jmcneill sc->sc_smw.smw_period);
260 1.2 jmcneill
261 1.2 jmcneill if (sysmon_wdog_register(&sc->sc_smw) != 0)
262 1.6 jmcneill aprint_error_dev(sc->sc_dev, "couldn't register with sysmon\n");
263 1.6 jmcneill }
264 1.6 jmcneill
265 1.6 jmcneill static void
266 1.6 jmcneill as3722_rtc_attach(struct as3722_softc *sc)
267 1.6 jmcneill {
268 1.6 jmcneill int error;
269 1.6 jmcneill
270 1.6 jmcneill iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
271 1.6 jmcneill error = as3722_set_clear(sc, AS3722_RTC_CONTROL_REG,
272 1.6 jmcneill AS3722_RTC_CONTROL_RTC_ON, 0, I2C_F_POLL);
273 1.6 jmcneill iic_release_bus(sc->sc_i2c, I2C_F_POLL);
274 1.6 jmcneill
275 1.6 jmcneill if (error) {
276 1.6 jmcneill aprint_error_dev(sc->sc_dev, "couldn't setup RTC\n");
277 1.6 jmcneill return;
278 1.6 jmcneill }
279 1.6 jmcneill
280 1.6 jmcneill sc->sc_todr.todr_gettime_ymdhms = as3722_rtc_gettime;
281 1.6 jmcneill sc->sc_todr.todr_settime_ymdhms = as3722_rtc_settime;
282 1.6 jmcneill sc->sc_todr.cookie = sc;
283 1.6 jmcneill #ifdef FDT
284 1.6 jmcneill fdtbus_todr_attach(sc->sc_dev, sc->sc_phandle, &sc->sc_todr);
285 1.6 jmcneill #else
286 1.6 jmcneill todr_attach(&sc->sc_todr);
287 1.6 jmcneill #endif
288 1.1 jmcneill }
289 1.1 jmcneill
290 1.1 jmcneill static int
291 1.1 jmcneill as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
292 1.1 jmcneill {
293 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
294 1.1 jmcneill ®, 1, val, 1, flags);
295 1.1 jmcneill }
296 1.1 jmcneill
297 1.1 jmcneill static int
298 1.1 jmcneill as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
299 1.1 jmcneill {
300 1.1 jmcneill uint8_t buf[2] = { reg, val };
301 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
302 1.1 jmcneill NULL, 0, buf, 2, flags);
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.2 jmcneill static int
306 1.2 jmcneill as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
307 1.2 jmcneill uint8_t clr, int flags)
308 1.2 jmcneill {
309 1.2 jmcneill uint8_t old, new;
310 1.2 jmcneill int error;
311 1.2 jmcneill
312 1.2 jmcneill error = as3722_read(sc, reg, &old, flags);
313 1.2 jmcneill if (error) {
314 1.2 jmcneill return error;
315 1.2 jmcneill }
316 1.2 jmcneill new = set | (old & ~clr);
317 1.2 jmcneill
318 1.2 jmcneill return as3722_write(sc, reg, new, flags);
319 1.2 jmcneill }
320 1.2 jmcneill
321 1.2 jmcneill static int
322 1.2 jmcneill as3722_wdt_setmode(struct sysmon_wdog *smw)
323 1.2 jmcneill {
324 1.2 jmcneill struct as3722_softc * const sc = smw->smw_cookie;
325 1.2 jmcneill int error;
326 1.2 jmcneill
327 1.2 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
328 1.2 jmcneill
329 1.2 jmcneill if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
330 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
331 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
332 1.2 jmcneill 0, AS3722_WATCHDOG_CTRL_ON, flags);
333 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
334 1.2 jmcneill return error;
335 1.2 jmcneill }
336 1.2 jmcneill
337 1.2 jmcneill if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
338 1.2 jmcneill smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
339 1.2 jmcneill }
340 1.2 jmcneill if (smw->smw_period < 1 || smw->smw_period > 128) {
341 1.2 jmcneill return EINVAL;
342 1.2 jmcneill }
343 1.2 jmcneill sc->sc_smw.smw_period = smw->smw_period;
344 1.2 jmcneill
345 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
346 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
347 1.2 jmcneill __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
348 1.2 jmcneill AS3722_WATCHDOG_TIMER_TIMER, flags);
349 1.2 jmcneill if (error == 0) {
350 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
351 1.2 jmcneill AS3722_WATCHDOG_CTRL_ON, 0, flags);
352 1.2 jmcneill }
353 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
354 1.2 jmcneill
355 1.2 jmcneill return error;
356 1.2 jmcneill }
357 1.2 jmcneill
358 1.2 jmcneill static int
359 1.2 jmcneill as3722_wdt_tickle(struct sysmon_wdog *smw)
360 1.2 jmcneill {
361 1.2 jmcneill struct as3722_softc * const sc = smw->smw_cookie;
362 1.2 jmcneill int error;
363 1.2 jmcneill
364 1.2 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
365 1.2 jmcneill
366 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
367 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
368 1.2 jmcneill AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
369 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
370 1.2 jmcneill
371 1.2 jmcneill return error;
372 1.2 jmcneill }
373 1.2 jmcneill
374 1.6 jmcneill static int
375 1.6 jmcneill as3722_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
376 1.6 jmcneill {
377 1.6 jmcneill struct as3722_softc * const sc = tch->cookie;
378 1.6 jmcneill uint8_t buf[6];
379 1.6 jmcneill int error = 0;
380 1.6 jmcneill
381 1.6 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
382 1.6 jmcneill
383 1.6 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
384 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_SECOND_REG, &buf[0], flags);
385 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_MINUTE_REG, &buf[1], flags);
386 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_HOUR_REG, &buf[2], flags);
387 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_DAY_REG, &buf[3], flags);
388 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_MONTH_REG, &buf[4], flags);
389 1.6 jmcneill error += as3722_read(sc, AS3722_RTC_YEAR_REG, &buf[5], flags);
390 1.6 jmcneill iic_release_bus(sc->sc_i2c, flags);
391 1.6 jmcneill
392 1.6 jmcneill if (error)
393 1.6 jmcneill return error;
394 1.6 jmcneill
395 1.6 jmcneill dt->dt_sec = bcdtobin(buf[0] & 0x7f);
396 1.6 jmcneill dt->dt_min = bcdtobin(buf[1] & 0x7f);
397 1.6 jmcneill dt->dt_hour = bcdtobin(buf[2] & 0x3f);
398 1.6 jmcneill dt->dt_day = bcdtobin(buf[3] & 0x3f);
399 1.6 jmcneill dt->dt_mon = bcdtobin(buf[4] & 0x1f) - 1;
400 1.6 jmcneill dt->dt_year = AS3722_START_YEAR + bcdtobin(buf[5] & 0x7f);
401 1.6 jmcneill dt->dt_wday = 0;
402 1.6 jmcneill
403 1.6 jmcneill return 0;
404 1.6 jmcneill }
405 1.6 jmcneill
406 1.6 jmcneill static int
407 1.6 jmcneill as3722_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
408 1.6 jmcneill {
409 1.6 jmcneill struct as3722_softc * const sc = tch->cookie;
410 1.6 jmcneill uint8_t buf[6];
411 1.6 jmcneill int error = 0;
412 1.6 jmcneill
413 1.6 jmcneill if (dt->dt_year < AS3722_START_YEAR)
414 1.6 jmcneill return EINVAL;
415 1.6 jmcneill
416 1.6 jmcneill buf[0] = bintobcd(dt->dt_sec) & 0x7f;
417 1.6 jmcneill buf[1] = bintobcd(dt->dt_min) & 0x7f;
418 1.6 jmcneill buf[2] = bintobcd(dt->dt_hour) & 0x3f;
419 1.6 jmcneill buf[3] = bintobcd(dt->dt_day) & 0x3f;
420 1.6 jmcneill buf[4] = bintobcd(dt->dt_mon + 1) & 0x1f;
421 1.6 jmcneill buf[5] = bintobcd(dt->dt_year - AS3722_START_YEAR) & 0x7f;
422 1.6 jmcneill
423 1.6 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
424 1.6 jmcneill
425 1.6 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
426 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_SECOND_REG, buf[0], flags);
427 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_MINUTE_REG, buf[1], flags);
428 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_HOUR_REG, buf[2], flags);
429 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_DAY_REG, buf[3], flags);
430 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_MONTH_REG, buf[4], flags);
431 1.6 jmcneill error += as3722_write(sc, AS3722_RTC_YEAR_REG, buf[5], flags);
432 1.6 jmcneill iic_release_bus(sc->sc_i2c, flags);
433 1.6 jmcneill
434 1.6 jmcneill return error;
435 1.6 jmcneill }
436 1.6 jmcneill
437 1.7 jmcneill #ifdef FDT
438 1.7 jmcneill static void
439 1.7 jmcneill as3722_regulator_attach(struct as3722_softc *sc)
440 1.7 jmcneill {
441 1.7 jmcneill struct as3722reg_attach_args raa;
442 1.7 jmcneill int phandle, child;
443 1.7 jmcneill
444 1.7 jmcneill phandle = of_find_firstchild_byname(sc->sc_phandle, "regulators");
445 1.7 jmcneill if (phandle <= 0)
446 1.7 jmcneill return;
447 1.7 jmcneill
448 1.7 jmcneill for (int i = 0; i < __arraycount(as3722regdefs); i++) {
449 1.7 jmcneill const struct as3722regdef *regdef = &as3722regdefs[i];
450 1.7 jmcneill child = of_find_firstchild_byname(phandle, regdef->name);
451 1.7 jmcneill if (child <= 0)
452 1.7 jmcneill continue;
453 1.7 jmcneill raa.reg_def = regdef;
454 1.7 jmcneill raa.reg_phandle = child;
455 1.7 jmcneill config_found(sc->sc_dev, &raa, NULL);
456 1.7 jmcneill }
457 1.7 jmcneill }
458 1.7 jmcneill
459 1.7 jmcneill static int
460 1.7 jmcneill as3722reg_match(device_t parent, cfdata_t match, void *aux)
461 1.7 jmcneill {
462 1.7 jmcneill return 1;
463 1.7 jmcneill }
464 1.7 jmcneill
465 1.7 jmcneill static void
466 1.7 jmcneill as3722reg_attach(device_t parent, device_t self, void *aux)
467 1.7 jmcneill {
468 1.7 jmcneill struct as3722reg_softc *sc = device_private(self);
469 1.7 jmcneill struct as3722reg_attach_args *raa = aux;
470 1.7 jmcneill char *name = NULL;
471 1.7 jmcneill int len;
472 1.7 jmcneill
473 1.7 jmcneill sc->sc_dev = self;
474 1.7 jmcneill sc->sc_phandle = raa->reg_phandle;
475 1.7 jmcneill sc->sc_regdef = raa->reg_def;
476 1.7 jmcneill
477 1.7 jmcneill fdtbus_register_regulator_controller(self, sc->sc_phandle,
478 1.7 jmcneill &as3722reg_funcs);
479 1.7 jmcneill
480 1.7 jmcneill len = OF_getproplen(sc->sc_phandle, "regulator-name");
481 1.7 jmcneill if (len > 0) {
482 1.7 jmcneill name = kmem_zalloc(len, KM_SLEEP);
483 1.7 jmcneill OF_getprop(sc->sc_phandle, "regulator-name", name, len);
484 1.7 jmcneill }
485 1.7 jmcneill
486 1.7 jmcneill aprint_naive("\n");
487 1.7 jmcneill if (name)
488 1.7 jmcneill aprint_normal(": %s\n", name);
489 1.7 jmcneill else
490 1.7 jmcneill aprint_normal("\n");
491 1.7 jmcneill
492 1.7 jmcneill if (name)
493 1.7 jmcneill kmem_free(name, len);
494 1.7 jmcneill }
495 1.7 jmcneill
496 1.7 jmcneill static int
497 1.7 jmcneill as3722reg_acquire(device_t dev)
498 1.7 jmcneill {
499 1.7 jmcneill return 0;
500 1.7 jmcneill }
501 1.7 jmcneill
502 1.7 jmcneill static void
503 1.7 jmcneill as3722reg_release(device_t dev)
504 1.7 jmcneill {
505 1.7 jmcneill }
506 1.7 jmcneill
507 1.7 jmcneill static int
508 1.7 jmcneill as3722reg_enable(device_t dev, bool enable)
509 1.7 jmcneill {
510 1.7 jmcneill struct as3722reg_softc *sc = device_private(dev);
511 1.7 jmcneill struct as3722_softc *asc = device_private(device_parent(dev));
512 1.7 jmcneill const struct as3722regdef *regdef = sc->sc_regdef;
513 1.7 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
514 1.7 jmcneill int error;
515 1.7 jmcneill
516 1.7 jmcneill iic_acquire_bus(asc->sc_i2c, flags);
517 1.7 jmcneill if (enable)
518 1.7 jmcneill error = as3722_set_clear(asc, regdef->enable_reg,
519 1.7 jmcneill regdef->enable_mask, 0, flags);
520 1.7 jmcneill else
521 1.7 jmcneill error = as3722_set_clear(asc, regdef->enable_reg,
522 1.7 jmcneill 0, regdef->enable_mask, flags);
523 1.7 jmcneill iic_release_bus(asc->sc_i2c, flags);
524 1.7 jmcneill
525 1.7 jmcneill return error;
526 1.7 jmcneill }
527 1.7 jmcneill
528 1.7 jmcneill static int
529 1.7 jmcneill as3722reg_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol)
530 1.7 jmcneill {
531 1.7 jmcneill struct as3722reg_softc *sc = device_private(dev);
532 1.7 jmcneill struct as3722_softc *asc = device_private(device_parent(dev));
533 1.7 jmcneill const struct as3722regdef *regdef = sc->sc_regdef;
534 1.7 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
535 1.7 jmcneill uint8_t set_v = 0x00;
536 1.7 jmcneill u_int uvol;
537 1.7 jmcneill int error;
538 1.7 jmcneill
539 1.7 jmcneill for (uint8_t v = 0x01; v <= 0x24; v++) {
540 1.7 jmcneill uvol = 800000 + (v * 25000);
541 1.7 jmcneill if (uvol >= min_uvol && uvol <= max_uvol) {
542 1.7 jmcneill set_v = v;
543 1.7 jmcneill goto done;
544 1.7 jmcneill }
545 1.7 jmcneill }
546 1.7 jmcneill for (uint8_t v = 0x40; v <= 0x7f; v++) {
547 1.7 jmcneill uvol = 1725000 + ((v - 0x40) * 25000);
548 1.7 jmcneill if (uvol >= min_uvol && uvol <= max_uvol) {
549 1.7 jmcneill set_v = v;
550 1.7 jmcneill goto done;
551 1.7 jmcneill }
552 1.7 jmcneill }
553 1.7 jmcneill if (set_v == 0)
554 1.7 jmcneill return ERANGE;
555 1.7 jmcneill
556 1.7 jmcneill done:
557 1.7 jmcneill iic_acquire_bus(asc->sc_i2c, flags);
558 1.7 jmcneill error = as3722_set_clear(asc, regdef->vsel_reg, set_v,
559 1.7 jmcneill regdef->vsel_mask, flags);
560 1.7 jmcneill iic_release_bus(asc->sc_i2c, flags);
561 1.7 jmcneill
562 1.7 jmcneill return error;
563 1.7 jmcneill }
564 1.7 jmcneill
565 1.7 jmcneill static int
566 1.7 jmcneill as3722reg_get_voltage(device_t dev, u_int *puvol)
567 1.7 jmcneill {
568 1.7 jmcneill struct as3722reg_softc *sc = device_private(dev);
569 1.7 jmcneill struct as3722_softc *asc = device_private(device_parent(dev));
570 1.7 jmcneill const struct as3722regdef *regdef = sc->sc_regdef;
571 1.7 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
572 1.7 jmcneill uint8_t v;
573 1.7 jmcneill int error;
574 1.7 jmcneill
575 1.7 jmcneill iic_acquire_bus(asc->sc_i2c, flags);
576 1.7 jmcneill error = as3722_read(asc, regdef->vsel_reg, &v, flags);
577 1.7 jmcneill iic_release_bus(asc->sc_i2c, flags);
578 1.7 jmcneill if (error != 0)
579 1.7 jmcneill return error;
580 1.7 jmcneill
581 1.7 jmcneill v &= regdef->vsel_mask;
582 1.7 jmcneill
583 1.7 jmcneill if (v == 0)
584 1.7 jmcneill *puvol = 0; /* LDO off */
585 1.7 jmcneill else if (v >= 0x01 && v <= 0x24)
586 1.7 jmcneill *puvol = 800000 + (v * 25000);
587 1.7 jmcneill else if (v >= 0x40 && v <= 0x7f)
588 1.7 jmcneill *puvol = 1725000 + ((v - 0x40) * 25000);
589 1.7 jmcneill else
590 1.7 jmcneill return EINVAL;
591 1.7 jmcneill
592 1.7 jmcneill return 0;
593 1.7 jmcneill }
594 1.7 jmcneill #endif
595 1.7 jmcneill
596 1.1 jmcneill int
597 1.1 jmcneill as3722_poweroff(device_t dev)
598 1.1 jmcneill {
599 1.1 jmcneill struct as3722_softc * const sc = device_private(dev);
600 1.1 jmcneill int error;
601 1.1 jmcneill
602 1.1 jmcneill const int flags = I2C_F_POLL;
603 1.1 jmcneill
604 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
605 1.1 jmcneill error = as3722_write(sc, AS3722_RESET_CTRL_REG,
606 1.1 jmcneill AS3722_RESET_CTRL_POWER_OFF, flags);
607 1.1 jmcneill iic_release_bus(sc->sc_i2c, flags);
608 1.1 jmcneill
609 1.1 jmcneill return error;
610 1.1 jmcneill }
611 1.3 jmcneill
612 1.3 jmcneill int
613 1.3 jmcneill as3722_reboot(device_t dev)
614 1.3 jmcneill {
615 1.3 jmcneill struct as3722_softc * const sc = device_private(dev);
616 1.3 jmcneill int error;
617 1.3 jmcneill
618 1.3 jmcneill const int flags = I2C_F_POLL;
619 1.3 jmcneill
620 1.3 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
621 1.3 jmcneill error = as3722_write(sc, AS3722_RESET_CTRL_REG,
622 1.3 jmcneill AS3722_RESET_CTRL_FORCE_RESET, flags);
623 1.3 jmcneill iic_release_bus(sc->sc_i2c, flags);
624 1.3 jmcneill
625 1.3 jmcneill return error;
626 1.3 jmcneill }
627