as3722.c revision 1.2 1 1.2 jmcneill /* $NetBSD: as3722.c,v 1.2 2015/11/21 10:56:40 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.1 jmcneill #include <sys/cdefs.h>
30 1.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: as3722.c,v 1.2 2015/11/21 10:56:40 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/kernel.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/conf.h>
37 1.1 jmcneill #include <sys/bus.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.2 jmcneill #include <sys/wdog.h>
40 1.2 jmcneill
41 1.2 jmcneill #include <dev/sysmon/sysmonvar.h>
42 1.1 jmcneill
43 1.1 jmcneill #include <dev/i2c/i2cvar.h>
44 1.1 jmcneill #include <dev/i2c/as3722.h>
45 1.1 jmcneill
46 1.2 jmcneill #define AS3722_GPIO0_CTRL_REG 0x08
47 1.2 jmcneill #define AS3722_GPIO0_CTRL_INVERT __BIT(7)
48 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF __BITS(6,3)
49 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF_GPIO 0
50 1.2 jmcneill #define AS3722_GPIO0_CTRL_IOSF_WATCHDOG 9
51 1.2 jmcneill #define AS3722_GPIO0_CTRL_MODE __BITS(2,0)
52 1.2 jmcneill #define AS3722_GPIO0_CTRL_MODE_PULLDOWN 5
53 1.2 jmcneill
54 1.1 jmcneill #define AS3722_RESET_CTRL_REG 0x36
55 1.1 jmcneill #define AS3722_RESET_CTRL_POWER_OFF __BIT(1)
56 1.1 jmcneill
57 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_REG 0x38
58 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_MODE __BITS(2,1)
59 1.2 jmcneill #define AS3722_WATCHDOG_CTRL_ON __BIT(0)
60 1.2 jmcneill
61 1.2 jmcneill #define AS3722_WATCHDOG_TIMER_REG 0x46
62 1.2 jmcneill #define AS3722_WATCHDOG_TIMER_TIMER __BITS(6,0)
63 1.2 jmcneill
64 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_REG 0x48
65 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_PWM_DIV __BITS(7,6)
66 1.2 jmcneill #define AS3722_WATCHDOG_SIGNAL_SW_SIG __BIT(0)
67 1.2 jmcneill
68 1.1 jmcneill #define AS3722_ASIC_ID1_REG 0x90
69 1.1 jmcneill #define AS3722_ASIC_ID2_REG 0x91
70 1.1 jmcneill
71 1.1 jmcneill struct as3722_softc {
72 1.1 jmcneill device_t sc_dev;
73 1.1 jmcneill i2c_tag_t sc_i2c;
74 1.1 jmcneill i2c_addr_t sc_addr;
75 1.2 jmcneill
76 1.2 jmcneill struct sysmon_wdog sc_smw;
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.2 jmcneill #define AS3722_WATCHDOG_DEFAULT_PERIOD 10
80 1.2 jmcneill
81 1.1 jmcneill static int as3722_match(device_t, cfdata_t, void *);
82 1.1 jmcneill static void as3722_attach(device_t, device_t, void *);
83 1.1 jmcneill
84 1.2 jmcneill static int as3722_wdt_setmode(struct sysmon_wdog *);
85 1.2 jmcneill static int as3722_wdt_tickle(struct sysmon_wdog *);
86 1.2 jmcneill
87 1.1 jmcneill static int as3722_read(struct as3722_softc *, uint8_t, uint8_t *, int);
88 1.1 jmcneill static int as3722_write(struct as3722_softc *, uint8_t, uint8_t, int);
89 1.2 jmcneill static int as3722_set_clear(struct as3722_softc *, uint8_t, uint8_t,
90 1.2 jmcneill uint8_t, int);
91 1.1 jmcneill
92 1.1 jmcneill CFATTACH_DECL_NEW(as3722pmic, sizeof(struct as3722_softc),
93 1.1 jmcneill as3722_match, as3722_attach, NULL, NULL);
94 1.1 jmcneill
95 1.1 jmcneill static int
96 1.1 jmcneill as3722_match(device_t parent, cfdata_t match, void *aux)
97 1.1 jmcneill {
98 1.1 jmcneill struct i2c_attach_args *ia = aux;
99 1.1 jmcneill uint8_t reg, id1;
100 1.1 jmcneill int error;
101 1.1 jmcneill
102 1.1 jmcneill iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
103 1.1 jmcneill reg = AS3722_ASIC_ID1_REG;
104 1.1 jmcneill error = iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
105 1.1 jmcneill ®, 1, &id1, 1, I2C_F_POLL);
106 1.1 jmcneill iic_release_bus(ia->ia_tag, I2C_F_POLL);
107 1.1 jmcneill
108 1.1 jmcneill if (error == 0 && id1 == 0x0c)
109 1.1 jmcneill return 1;
110 1.1 jmcneill
111 1.1 jmcneill return 0;
112 1.1 jmcneill }
113 1.1 jmcneill
114 1.1 jmcneill static void
115 1.1 jmcneill as3722_attach(device_t parent, device_t self, void *aux)
116 1.1 jmcneill {
117 1.1 jmcneill struct as3722_softc * const sc = device_private(self);
118 1.1 jmcneill struct i2c_attach_args *ia = aux;
119 1.2 jmcneill int error;
120 1.1 jmcneill
121 1.1 jmcneill sc->sc_dev = self;
122 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
123 1.1 jmcneill sc->sc_addr = ia->ia_addr;
124 1.1 jmcneill
125 1.1 jmcneill aprint_naive("\n");
126 1.1 jmcneill aprint_normal(": AMS AS3822\n");
127 1.2 jmcneill
128 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
129 1.2 jmcneill error = as3722_write(sc, AS3722_GPIO0_CTRL_REG,
130 1.2 jmcneill __SHIFTIN(AS3722_GPIO0_CTRL_IOSF_GPIO,
131 1.2 jmcneill AS3722_GPIO0_CTRL_IOSF) |
132 1.2 jmcneill __SHIFTIN(AS3722_GPIO0_CTRL_MODE_PULLDOWN,
133 1.2 jmcneill AS3722_GPIO0_CTRL_MODE),
134 1.2 jmcneill I2C_F_POLL);
135 1.2 jmcneill error += as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
136 1.2 jmcneill __SHIFTIN(1, AS3722_WATCHDOG_CTRL_MODE), 0, I2C_F_POLL);
137 1.2 jmcneill iic_release_bus(sc->sc_i2c, I2C_F_POLL);
138 1.2 jmcneill
139 1.2 jmcneill if (error)
140 1.2 jmcneill aprint_error_dev(self, "couldn't setup watchdog\n");
141 1.2 jmcneill
142 1.2 jmcneill sc->sc_smw.smw_name = device_xname(self);
143 1.2 jmcneill sc->sc_smw.smw_cookie = sc;
144 1.2 jmcneill sc->sc_smw.smw_setmode = as3722_wdt_setmode;
145 1.2 jmcneill sc->sc_smw.smw_tickle = as3722_wdt_tickle;
146 1.2 jmcneill sc->sc_smw.smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
147 1.2 jmcneill
148 1.2 jmcneill aprint_normal_dev(self, "default watchdog period is %u seconds\n",
149 1.2 jmcneill sc->sc_smw.smw_period);
150 1.2 jmcneill
151 1.2 jmcneill if (sysmon_wdog_register(&sc->sc_smw) != 0)
152 1.2 jmcneill aprint_error_dev(self, "couldn't register with sysmon\n");
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static int
156 1.1 jmcneill as3722_read(struct as3722_softc *sc, uint8_t reg, uint8_t *val, int flags)
157 1.1 jmcneill {
158 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
159 1.1 jmcneill ®, 1, val, 1, flags);
160 1.1 jmcneill }
161 1.1 jmcneill
162 1.1 jmcneill static int
163 1.1 jmcneill as3722_write(struct as3722_softc *sc, uint8_t reg, uint8_t val, int flags)
164 1.1 jmcneill {
165 1.1 jmcneill uint8_t buf[2] = { reg, val };
166 1.1 jmcneill return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
167 1.1 jmcneill NULL, 0, buf, 2, flags);
168 1.1 jmcneill }
169 1.1 jmcneill
170 1.2 jmcneill static int
171 1.2 jmcneill as3722_set_clear(struct as3722_softc *sc, uint8_t reg, uint8_t set,
172 1.2 jmcneill uint8_t clr, int flags)
173 1.2 jmcneill {
174 1.2 jmcneill uint8_t old, new;
175 1.2 jmcneill int error;
176 1.2 jmcneill
177 1.2 jmcneill error = as3722_read(sc, reg, &old, flags);
178 1.2 jmcneill if (error) {
179 1.2 jmcneill return error;
180 1.2 jmcneill }
181 1.2 jmcneill new = set | (old & ~clr);
182 1.2 jmcneill
183 1.2 jmcneill return as3722_write(sc, reg, new, flags);
184 1.2 jmcneill }
185 1.2 jmcneill
186 1.2 jmcneill static int
187 1.2 jmcneill as3722_wdt_setmode(struct sysmon_wdog *smw)
188 1.2 jmcneill {
189 1.2 jmcneill struct as3722_softc * const sc = smw->smw_cookie;
190 1.2 jmcneill int error;
191 1.2 jmcneill
192 1.2 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
193 1.2 jmcneill
194 1.2 jmcneill if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
195 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
196 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
197 1.2 jmcneill 0, AS3722_WATCHDOG_CTRL_ON, flags);
198 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
199 1.2 jmcneill return error;
200 1.2 jmcneill }
201 1.2 jmcneill
202 1.2 jmcneill if (smw->smw_period == WDOG_PERIOD_DEFAULT) {
203 1.2 jmcneill smw->smw_period = AS3722_WATCHDOG_DEFAULT_PERIOD;
204 1.2 jmcneill }
205 1.2 jmcneill if (smw->smw_period < 1 || smw->smw_period > 128) {
206 1.2 jmcneill return EINVAL;
207 1.2 jmcneill }
208 1.2 jmcneill sc->sc_smw.smw_period = smw->smw_period;
209 1.2 jmcneill
210 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
211 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_TIMER_REG,
212 1.2 jmcneill __SHIFTIN(sc->sc_smw.smw_period - 1, AS3722_WATCHDOG_TIMER_TIMER),
213 1.2 jmcneill AS3722_WATCHDOG_TIMER_TIMER, flags);
214 1.2 jmcneill if (error == 0) {
215 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_CTRL_REG,
216 1.2 jmcneill AS3722_WATCHDOG_CTRL_ON, 0, flags);
217 1.2 jmcneill }
218 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
219 1.2 jmcneill
220 1.2 jmcneill return error;
221 1.2 jmcneill }
222 1.2 jmcneill
223 1.2 jmcneill static int
224 1.2 jmcneill as3722_wdt_tickle(struct sysmon_wdog *smw)
225 1.2 jmcneill {
226 1.2 jmcneill struct as3722_softc * const sc = smw->smw_cookie;
227 1.2 jmcneill int error;
228 1.2 jmcneill
229 1.2 jmcneill const int flags = (cold ? I2C_F_POLL : 0);
230 1.2 jmcneill
231 1.2 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
232 1.2 jmcneill error = as3722_set_clear(sc, AS3722_WATCHDOG_SIGNAL_REG,
233 1.2 jmcneill AS3722_WATCHDOG_SIGNAL_SW_SIG, 0, flags);
234 1.2 jmcneill iic_release_bus(sc->sc_i2c, flags);
235 1.2 jmcneill
236 1.2 jmcneill return error;
237 1.2 jmcneill }
238 1.2 jmcneill
239 1.1 jmcneill int
240 1.1 jmcneill as3722_poweroff(device_t dev)
241 1.1 jmcneill {
242 1.1 jmcneill struct as3722_softc * const sc = device_private(dev);
243 1.1 jmcneill int error;
244 1.1 jmcneill
245 1.1 jmcneill const int flags = I2C_F_POLL;
246 1.1 jmcneill
247 1.1 jmcneill iic_acquire_bus(sc->sc_i2c, flags);
248 1.1 jmcneill error = as3722_write(sc, AS3722_RESET_CTRL_REG,
249 1.1 jmcneill AS3722_RESET_CTRL_POWER_OFF, flags);
250 1.1 jmcneill iic_release_bus(sc->sc_i2c, flags);
251 1.1 jmcneill
252 1.1 jmcneill return error;
253 1.1 jmcneill }
254