as3722.c revision 1.4.2.3 1 1.4.2.3 skrll /* $NetBSD: as3722.c,v 1.4.2.3 2016/10/05 20:55:41 skrll Exp $ */
2 1.4.2.2 skrll
3 1.4.2.2 skrll /*-
4 1.4.2.2 skrll * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.4.2.2 skrll * All rights reserved.
6 1.4.2.2 skrll *
7 1.4.2.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.4.2.2 skrll * modification, are permitted provided that the following conditions
9 1.4.2.2 skrll * are met:
10 1.4.2.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.4.2.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.4.2.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.4.2.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.4.2.2 skrll * documentation and/or other materials provided with the distribution.
15 1.4.2.2 skrll *
16 1.4.2.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.4.2.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.4.2.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.4.2.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.4.2.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.4.2.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.4.2.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.4.2.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.4.2.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.4.2.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.4.2.2 skrll * POSSIBILITY OF SUCH DAMAGE.
27 1.4.2.2 skrll */
28 1.4.2.2 skrll
29 1.4.2.2 skrll #include <sys/cdefs.h>
30 1.4.2.3 skrll __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.4.2.3 2016/10/05 20:55:41 skrll Exp $");
31 1.4.2.2 skrll
32 1.4.2.2 skrll #include <sys/param.h>
33 1.4.2.2 skrll #include <sys/systm.h>
34 1.4.2.2 skrll #include <sys/kernel.h>
35 1.4.2.2 skrll #include <sys/device.h>
36 1.4.2.2 skrll #include <sys/conf.h>
37 1.4.2.2 skrll #include <sys/bus.h>
38 1.4.2.2 skrll #include <sys/kmem.h>
39 1.4.2.2 skrll #include <sys/wdog.h>
40 1.4.2.2 skrll
41 1.4.2.2 skrll #include <dev/sysmon/sysmonvar.h>
42 1.4.2.2 skrll
43 1.4.2.2 skrll #include <dev/i2c/i2cvar.h>
44 1.4.2.2 skrll #include <dev/i2c/as3722.h>
45 1.4.2.2 skrll
46 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_REG 0x08
47 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_INVERT __BIT(7)
48 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_IOSF __BITS(6,3)
49 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_IOSF_GPIO 0
50 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG 9
51 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_MODE __BITS(2,0)
52 1.4.2.2 skrll #define AS3722_GPIO0_CTRL_MODE_PULLDOWN 5
53 1.4.2.2 skrll
54 1.4.2.2 skrll #define AS3722_RESET_CTRL_REG 0x36
55 1.4.2.2 skrll #define AS3722_RESET_CTRL_POWER_OFF __BIT(1)
56 1.4.2.2 skrll #define AS3722_RESET_CTRL_FORCE_RESET __BIT(0)
57 1.4.2.2 skrll
58 1.4.2.2 skrll #define AS3722_WATCHDOG_CTRL_REG 0x38
59 1.4.2.2 skrll #define AS3722_WATCHDOG_CTRL_MODE __BITS(2,1)
60 1.4.2.2 skrll #define AS3722_WATCHDOG_CTRL_ON __BIT(0)
61 1.4.2.2 skrll
62 1.4.2.2 skrll #define AS3722_WATCHDOG_TIMER_REG 0x46
63 1.4.2.2 skrll #define AS3722_WATCHDOG_TIMER_TIMER __BITS(6,0)
64 1.4.2.2 skrll
65 1.4.2.2 skrll #define AS3722_WATCHDOG_SIGNAL_REG 0x48
66 1.4.2.2 skrll #define AS3722_WATCHDOG_SIGNAL_PWM_DIV __BITS(7,6)
67 1.4.2.2 skrll #define AS3722_WATCHDOG_SIGNAL_SW_SIG __BIT(0)
68 1.4.2.2 skrll
69 1.4.2.2 skrll #define AS3722_ASIC_ID1_REG 0x90
70 1.4.2.2 skrll #define AS3722_ASIC_ID2_REG 0x91
71 1.4.2.2 skrll
72 1.4.2.2 skrll struct as3722_softc {
73 1.4.2.2 skrll device_t sc_dev;
74 1.4.2.2 skrll i2c_tag_t sc_i2c;
75 1.4.2.2 skrll i2c_addr_t sc_addr;
76 1.4.2.2 skrll
77 1.4.2.2 skrll struct sysmon_wdog sc_smw;
78 1.4.2.2 skrll };
79 1.4.2.2 skrll
80 1.4.2.2 skrll #define AS3722_WATCHDOG_DEFAULT_PERIOD 10
81 1.4.2.2 skrll
82 1.4.2.2 skrll static int as3722_match(device_t, cfdata_t, void *);
83 1.4.2.2 skrll static void as3722_attach(device_t, device_t, void *);
84 1.4.2.2 skrll
85 1.4.2.2 skrll static int as3722_wdt_setmode(struct sysmon_wdog *);
86 1.4.2.2 skrll static int as3722_wdt_tickle(struct sysmon_wdog *);
87 1.4.2.2 skrll
88 1.4.2.2 skrll static int as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
89 1.4.2.2 skrll static int as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
90 1.4.2.2 skrll static int as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
91 1.4.2.2 skrll uint8_t, int);
92 1.4.2.2 skrll
93 1.4.2.2 skrll CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
94 1.4.2.2 skrll as3722_match, as3722_attach, NULL, NULL);
95 1.4.2.2 skrll
96 1.4.2.2 skrll static const char * as3722_compats[] = {
97 1.4.2.2 skrll "ams,as3722",
98 1.4.2.2 skrll NULL
99 1.4.2.2 skrll };
100 1.4.2.2 skrll
101 1.4.2.2 skrll static int
102 1.4.2.2 skrll as3722_match(device_t parent, cfdata_t match, void *aux)
103 1.4.2.2 skrll {
104 1.4.2.2 skrll struct i2c_attach_args *ia = aux;
105 1.4.2.2 skrll uint8_t reg, id1;
106 1.4.2.2 skrll int error;
107 1.4.2.2 skrll
108 1.4.2.2 skrll if (ia->ia_name == NULL) {
109 1.4.2.2 skrll iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
110 1.4.2.2 skrll reg = AS3722_ASIC_ID1_REG;
111 1.4.2.2 skrll error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
112 1.4.2.2 skrll ®, 1, &id1, 1, I2C_F_POLL);
113 1.4.2.2 skrll iic_release_bus(ia->ia_tag, I2C_F_POLL);
114 1.4.2.2 skrll
115 1.4.2.2 skrll if (error == 0 && id1 == 0x0c)
116 1.4.2.2 skrll return 1;
117 1.4.2.2 skrll
118 1.4.2.2 skrll return 0;
119 1.4.2.2 skrll } else {
120 1.4.2.2 skrll return iic_compat_match(ia, as3722_compats);
121 1.4.2.2 skrll }
122 1.4.2.2 skrll }
123 1.4.2.2 skrll
124 1.4.2.2 skrll static void
125 1.4.2.2 skrll as3722_attach(device_t parent, device_t self, void *aux)
126 1.4.2.2 skrll {
127 1.4.2.2 skrll struct as3722_softc * const sc = device_private(self);
128 1.4.2.2 skrll struct i2c_attach_args *ia = aux;
129 1.4.2.2 skrll int error;
130 1.4.2.2 skrll
131 1.4.2.2 skrll sc->sc_dev = self;
132 1.4.2.2 skrll sc->sc_i2c = ia->ia_tag;
133 1.4.2.2 skrll sc->sc_addr = ia->ia_addr;
134 1.4.2.2 skrll
135 1.4.2.2 skrll aprint_naive("\n");
136 1.4.2.3 skrll aprint_normal(": AMS AS3722\n");
137 1.4.2.2 skrll
138 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
139 1.4.2.2 skrll error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
140 1.4.2.2 skrll __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
141 1.4.2.2 skrll AS3722_GPIO0_CTRL_IOSF) |
142 1.4.2.2 skrll __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
143 1.4.2.2 skrll AS3722_GPIO0_CTRL_MODE),
144 1.4.2.2 skrll I2C_F_POLL);
145 1.4.2.2 skrll error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
146 1.4.2.2 skrll __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
147 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, I2C_F_POLL);
148 1.4.2.2 skrll
149 1.4.2.2 skrll if (error)
150 1.4.2.2 skrll aprint_error_dev(self, "couldn't setup watchdog\n");
151 1.4.2.2 skrll
152 1.4.2.2 skrll sc->sc_smw.smw_name = device_xname(self);
153 1.4.2.2 skrll sc->sc_smw.smw_cookie = sc;
154 1.4.2.2 skrll sc->sc_smw.smw_setmode = as3722_wdt_setmode;
155 1.4.2.2 skrll sc->sc_smw.smw_tickle = as3722_wdt_tickle;
156 1.4.2.2 skrll sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
157 1.4.2.2 skrll
158 1.4.2.2 skrll aprint_normal_dev(self, "default watchdog period is %u seconds\n",
159 1.4.2.2 skrll sc->sc_smw.smw_period);
160 1.4.2.2 skrll
161 1.4.2.2 skrll if (sysmon_wdog_register(&sc->sc_smw) != 0)
162 1.4.2.2 skrll aprint_error_dev(self, "couldn't register with sysmon\n");
163 1.4.2.2 skrll }
164 1.4.2.2 skrll
165 1.4.2.2 skrll static int
166 1.4.2.2 skrll as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
167 1.4.2.2 skrll {
168 1.4.2.2 skrll return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
169 1.4.2.2 skrll ®, 1, val, 1, flags);
170 1.4.2.2 skrll }
171 1.4.2.2 skrll
172 1.4.2.2 skrll static int
173 1.4.2.2 skrll as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
174 1.4.2.2 skrll {
175 1.4.2.2 skrll uint8_t buf[2] = { reg, val };
176 1.4.2.2 skrll return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
177 1.4.2.2 skrll NULL, 0, buf, 2, flags);
178 1.4.2.2 skrll }
179 1.4.2.2 skrll
180 1.4.2.2 skrll static int
181 1.4.2.2 skrll as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
182 1.4.2.2 skrll uint8_t clr, int flags)
183 1.4.2.2 skrll {
184 1.4.2.2 skrll uint8_t old, new;
185 1.4.2.2 skrll int error;
186 1.4.2.2 skrll
187 1.4.2.2 skrll error = as3722_read(sc, reg, &old, flags);
188 1.4.2.2 skrll if (error) {
189 1.4.2.2 skrll return error;
190 1.4.2.2 skrll }
191 1.4.2.2 skrll new = set | (old & ~clr);
192 1.4.2.2 skrll
193 1.4.2.2 skrll return as3722_write(sc, reg, new, flags);
194 1.4.2.2 skrll }
195 1.4.2.2 skrll
196 1.4.2.2 skrll static int
197 1.4.2.2 skrll as3722_wdt_setmode(struct sysmon_wdog *smw)
198 1.4.2.2 skrll {
199 1.4.2.2 skrll struct as3722_softc * const sc = smw->smw_cookie;
200 1.4.2.2 skrll int error;
201 1.4.2.2 skrll
202 1.4.2.2 skrll const int flags = (cold ? I2C_F_POLL : 0);
203 1.4.2.2 skrll
204 1.4.2.2 skrll if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
205 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, flags);
206 1.4.2.2 skrll error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
207 1.4.2.2 skrll 0, AS3722_WATCHDOG_CTRL_ON, flags);
208 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, flags);
209 1.4.2.2 skrll return error;
210 1.4.2.2 skrll }
211 1.4.2.2 skrll
212 1.4.2.2 skrll if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
213 1.4.2.2 skrll smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
214 1.4.2.2 skrll }
215 1.4.2.2 skrll if (smw->smw_period < 1 || smw->smw_period > 128) {
216 1.4.2.2 skrll return EINVAL;
217 1.4.2.2 skrll }
218 1.4.2.2 skrll sc->sc_smw.smw_period = smw->smw_period;
219 1.4.2.2 skrll
220 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, flags);
221 1.4.2.2 skrll error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
222 1.4.2.2 skrll __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
223 1.4.2.2 skrll AS3722_WATCHDOG_TIMER_TIMER, flags);
224 1.4.2.2 skrll if (error == 0) {
225 1.4.2.2 skrll error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
226 1.4.2.2 skrll AS3722_WATCHDOG_CTRL_ON, 0, flags);
227 1.4.2.2 skrll }
228 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, flags);
229 1.4.2.2 skrll
230 1.4.2.2 skrll return error;
231 1.4.2.2 skrll }
232 1.4.2.2 skrll
233 1.4.2.2 skrll static int
234 1.4.2.2 skrll as3722_wdt_tickle(struct sysmon_wdog *smw)
235 1.4.2.2 skrll {
236 1.4.2.2 skrll struct as3722_softc * const sc = smw->smw_cookie;
237 1.4.2.2 skrll int error;
238 1.4.2.2 skrll
239 1.4.2.2 skrll const int flags = (cold ? I2C_F_POLL : 0);
240 1.4.2.2 skrll
241 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, flags);
242 1.4.2.2 skrll error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
243 1.4.2.2 skrll AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
244 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, flags);
245 1.4.2.2 skrll
246 1.4.2.2 skrll return error;
247 1.4.2.2 skrll }
248 1.4.2.2 skrll
249 1.4.2.2 skrll int
250 1.4.2.2 skrll as3722_poweroff(device_t dev)
251 1.4.2.2 skrll {
252 1.4.2.2 skrll struct as3722_softc * const sc = device_private(dev);
253 1.4.2.2 skrll int error;
254 1.4.2.2 skrll
255 1.4.2.2 skrll const int flags = I2C_F_POLL;
256 1.4.2.2 skrll
257 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, flags);
258 1.4.2.2 skrll error = as3722_write(sc, AS3722_RESET_CTRL_REG,
259 1.4.2.2 skrll AS3722_RESET_CTRL_POWER_OFF, flags);
260 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, flags);
261 1.4.2.2 skrll
262 1.4.2.2 skrll return error;
263 1.4.2.2 skrll }
264 1.4.2.2 skrll
265 1.4.2.2 skrll int
266 1.4.2.2 skrll as3722_reboot(device_t dev)
267 1.4.2.2 skrll {
268 1.4.2.2 skrll struct as3722_softc * const sc = device_private(dev);
269 1.4.2.2 skrll int error;
270 1.4.2.2 skrll
271 1.4.2.2 skrll const int flags = I2C_F_POLL;
272 1.4.2.2 skrll
273 1.4.2.2 skrll iic_acquire_bus(sc->sc_i2c, flags);
274 1.4.2.2 skrll error = as3722_write(sc, AS3722_RESET_CTRL_REG,
275 1.4.2.2 skrll AS3722_RESET_CTRL_FORCE_RESET, flags);
276 1.4.2.2 skrll iic_release_bus(sc->sc_i2c, flags);
277 1.4.2.2 skrll
278 1.4.2.2 skrll return error;
279 1.4.2.2 skrll }
280