cwfg.c revision 1.6 1 1.6 thorpej /* $NetBSD: cwfg.c,v 1.6 2025/09/17 13:42:42 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2020 Jared 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.6 thorpej __KERNEL_RCSID(0, "$NetBSD: cwfg.c,v 1.6 2025/09/17 13:42:42 thorpej 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.1 jmcneill
40 1.1 jmcneill #include <dev/i2c/i2cvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/sysmon/sysmonvar.h>
43 1.1 jmcneill #include <dev/sysmon/sysmon_taskq.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/fdt/fdtvar.h>
46 1.1 jmcneill
47 1.1 jmcneill #define VERSION_REG 0x00
48 1.1 jmcneill #define VCELL_HI_REG 0x02
49 1.1 jmcneill #define VCELL_HI __BITS(5,0)
50 1.1 jmcneill #define VCELL_LO_REG 0x03
51 1.1 jmcneill #define VCELL_LO __BITS(7,0)
52 1.1 jmcneill #define SOC_HI_REG 0x04
53 1.1 jmcneill #define SOC_LO_REG 0x05
54 1.1 jmcneill #define RTT_ALRT_HI_REG 0x06
55 1.1 jmcneill #define RTT_ALRT __BIT(7)
56 1.1 jmcneill #define RTT_HI __BITS(4,0)
57 1.1 jmcneill #define RTT_ALRT_LO_REG 0x07
58 1.1 jmcneill #define RTT_LO __BITS(7,0)
59 1.1 jmcneill #define CONFIG_REG 0x08
60 1.1 jmcneill #define CONFIG_ATHD __BITS(7,3)
61 1.1 jmcneill #define CONFIG_UFG __BIT(1)
62 1.1 jmcneill #define MODE_REG 0x0a
63 1.1 jmcneill #define MODE_SLEEP __BITS(7,6)
64 1.1 jmcneill #define MODE_SLEEP_WAKE 0x0
65 1.1 jmcneill #define MODE_SLEEP_SLEEP 0x3
66 1.1 jmcneill #define MODE_QSTRT __BITS(5,4)
67 1.1 jmcneill #define MODE_POR __BITS(3,0)
68 1.1 jmcneill #define BATINFO_REG(n) (0x10 + (n))
69 1.1 jmcneill
70 1.1 jmcneill #define VCELL_STEP 312
71 1.1 jmcneill #define VCELL_DIV 1024
72 1.1 jmcneill #define BATINFO_SIZE 64
73 1.1 jmcneill #define RESET_COUNT 30
74 1.1 jmcneill #define RESET_DELAY 100000
75 1.1 jmcneill
76 1.1 jmcneill enum cwfg_sensor {
77 1.1 jmcneill CWFG_SENSOR_VCELL,
78 1.1 jmcneill CWFG_SENSOR_SOC,
79 1.1 jmcneill CWFG_SENSOR_RTT,
80 1.1 jmcneill CWFG_NSENSORS
81 1.1 jmcneill };
82 1.1 jmcneill
83 1.1 jmcneill struct cwfg_softc {
84 1.1 jmcneill device_t sc_dev;
85 1.1 jmcneill i2c_tag_t sc_i2c;
86 1.1 jmcneill i2c_addr_t sc_addr;
87 1.1 jmcneill int sc_phandle;
88 1.1 jmcneill
89 1.1 jmcneill uint8_t sc_batinfo[BATINFO_SIZE];
90 1.1 jmcneill
91 1.1 jmcneill u_int sc_alert_level;
92 1.1 jmcneill u_int sc_monitor_interval;
93 1.1 jmcneill u_int sc_design_capacity;
94 1.1 jmcneill
95 1.1 jmcneill struct sysmon_envsys *sc_sme;
96 1.1 jmcneill
97 1.1 jmcneill envsys_data_t sc_sensor[CWFG_NSENSORS];
98 1.1 jmcneill };
99 1.1 jmcneill
100 1.1 jmcneill #define CWFG_MONITOR_INTERVAL_DEFAULT 8
101 1.1 jmcneill #define CWFG_DESIGN_CAPACITY_DEFAULT 2000
102 1.1 jmcneill #define CWFG_ALERT_LEVEL_DEFAULT 0
103 1.1 jmcneill
104 1.1 jmcneill static const struct device_compatible_entry compat_data[] = {
105 1.5 jmcneill { .compat = "cellwise,cw2015" },
106 1.5 jmcneill { .compat = "cellwise,cw201x" }, /* DTCOMPAT */
107 1.4 thorpej DEVICE_COMPAT_EOL
108 1.1 jmcneill };
109 1.1 jmcneill
110 1.1 jmcneill static int
111 1.1 jmcneill cwfg_lock(struct cwfg_softc *sc)
112 1.1 jmcneill {
113 1.1 jmcneill return iic_acquire_bus(sc->sc_i2c, 0);
114 1.1 jmcneill }
115 1.1 jmcneill
116 1.1 jmcneill static void
117 1.1 jmcneill cwfg_unlock(struct cwfg_softc *sc)
118 1.1 jmcneill {
119 1.1 jmcneill iic_release_bus(sc->sc_i2c, 0);
120 1.1 jmcneill }
121 1.1 jmcneill
122 1.1 jmcneill static int
123 1.1 jmcneill cwfg_read(struct cwfg_softc *sc, uint8_t reg, uint8_t *val)
124 1.1 jmcneill {
125 1.1 jmcneill return iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill static int
129 1.1 jmcneill cwfg_write(struct cwfg_softc *sc, uint8_t reg, uint8_t val)
130 1.1 jmcneill {
131 1.1 jmcneill return iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr, reg, val, 0);
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill static void
135 1.1 jmcneill cwfg_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *e)
136 1.1 jmcneill {
137 1.1 jmcneill struct cwfg_softc *sc = sme->sme_cookie;
138 1.1 jmcneill u_int vcell, rtt, tmp;
139 1.1 jmcneill uint8_t val;
140 1.1 jmcneill int error, n;
141 1.1 jmcneill
142 1.1 jmcneill e->state = ENVSYS_SINVALID;
143 1.1 jmcneill
144 1.1 jmcneill if ((error = cwfg_lock(sc)) != 0)
145 1.1 jmcneill return;
146 1.1 jmcneill
147 1.1 jmcneill switch (e->private) {
148 1.1 jmcneill case CWFG_SENSOR_VCELL:
149 1.1 jmcneill /* Take the average of three readings */
150 1.1 jmcneill vcell = 0;
151 1.1 jmcneill for (n = 0; n < 3; n++) {
152 1.1 jmcneill if ((error = cwfg_read(sc, VCELL_HI_REG, &val)) != 0)
153 1.1 jmcneill goto done;
154 1.1 jmcneill tmp = __SHIFTOUT(val, VCELL_HI) << 8;
155 1.1 jmcneill if ((error = cwfg_read(sc, VCELL_LO_REG, &val)) != 0)
156 1.1 jmcneill goto done;
157 1.1 jmcneill tmp |= __SHIFTOUT(val, VCELL_LO);
158 1.1 jmcneill vcell += tmp;
159 1.1 jmcneill }
160 1.1 jmcneill vcell /= 3;
161 1.1 jmcneill
162 1.1 jmcneill e->state = ENVSYS_SVALID;
163 1.1 jmcneill e->value_cur = ((vcell * VCELL_STEP) / VCELL_DIV) * 1000;
164 1.1 jmcneill break;
165 1.1 jmcneill
166 1.1 jmcneill case CWFG_SENSOR_SOC:
167 1.1 jmcneill if ((error = cwfg_read(sc, SOC_HI_REG, &val)) != 0)
168 1.1 jmcneill goto done;
169 1.1 jmcneill
170 1.1 jmcneill if (val != 0xff) {
171 1.1 jmcneill e->state = ENVSYS_SVALID;
172 1.1 jmcneill e->value_cur = val; /* batt % */
173 1.1 jmcneill }
174 1.1 jmcneill break;
175 1.1 jmcneill
176 1.1 jmcneill case CWFG_SENSOR_RTT:
177 1.1 jmcneill if ((error = cwfg_read(sc, RTT_ALRT_HI_REG, &val)) != 0)
178 1.1 jmcneill goto done;
179 1.1 jmcneill rtt = __SHIFTOUT(val, RTT_HI) << 8;
180 1.1 jmcneill if ((error = cwfg_read(sc, RTT_ALRT_LO_REG, &val)) != 0)
181 1.1 jmcneill goto done;
182 1.1 jmcneill rtt |= __SHIFTOUT(val, RTT_LO);
183 1.1 jmcneill
184 1.1 jmcneill if (rtt != 0x1fff) {
185 1.1 jmcneill e->state = ENVSYS_SVALID;
186 1.1 jmcneill e->value_cur = rtt; /* minutes */
187 1.1 jmcneill }
188 1.1 jmcneill break;
189 1.1 jmcneill }
190 1.1 jmcneill
191 1.1 jmcneill done:
192 1.1 jmcneill cwfg_unlock(sc);
193 1.1 jmcneill }
194 1.1 jmcneill
195 1.1 jmcneill static void
196 1.1 jmcneill cwfg_attach_battery(struct cwfg_softc *sc)
197 1.1 jmcneill {
198 1.1 jmcneill envsys_data_t *e;
199 1.1 jmcneill
200 1.1 jmcneill /* Cell voltage */
201 1.1 jmcneill e = &sc->sc_sensor[CWFG_SENSOR_VCELL];
202 1.1 jmcneill e->private = CWFG_SENSOR_VCELL;
203 1.1 jmcneill e->units = ENVSYS_SVOLTS_DC;
204 1.1 jmcneill e->state = ENVSYS_SINVALID;
205 1.1 jmcneill strlcpy(e->desc, "battery voltage", sizeof(e->desc));
206 1.1 jmcneill sysmon_envsys_sensor_attach(sc->sc_sme, e);
207 1.1 jmcneill
208 1.1 jmcneill /* State of charge */
209 1.1 jmcneill e = &sc->sc_sensor[CWFG_SENSOR_SOC];
210 1.1 jmcneill e->private = CWFG_SENSOR_SOC;
211 1.1 jmcneill e->units = ENVSYS_INTEGER;
212 1.1 jmcneill e->state = ENVSYS_SINVALID;
213 1.1 jmcneill e->flags = ENVSYS_FPERCENT;
214 1.1 jmcneill strlcpy(e->desc, "battery percent", sizeof(e->desc));
215 1.1 jmcneill sysmon_envsys_sensor_attach(sc->sc_sme, e);
216 1.1 jmcneill
217 1.1 jmcneill /* Remaining run time */
218 1.1 jmcneill e = &sc->sc_sensor[CWFG_SENSOR_RTT];
219 1.1 jmcneill e->private = CWFG_SENSOR_RTT;
220 1.1 jmcneill e->units = ENVSYS_INTEGER;
221 1.1 jmcneill e->state = ENVSYS_SINVALID;
222 1.1 jmcneill strlcpy(e->desc, "battery remaining minutes", sizeof(e->desc));
223 1.1 jmcneill sysmon_envsys_sensor_attach(sc->sc_sme, e);
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill static void
227 1.1 jmcneill cwfg_attach_sensors(struct cwfg_softc *sc)
228 1.1 jmcneill {
229 1.1 jmcneill sc->sc_sme = sysmon_envsys_create();
230 1.1 jmcneill sc->sc_sme->sme_name = device_xname(sc->sc_dev);
231 1.1 jmcneill sc->sc_sme->sme_cookie = sc;
232 1.1 jmcneill sc->sc_sme->sme_refresh = cwfg_sensor_refresh;
233 1.1 jmcneill sc->sc_sme->sme_events_timeout = sc->sc_monitor_interval;
234 1.1 jmcneill sc->sc_sme->sme_class = SME_CLASS_BATTERY;
235 1.1 jmcneill sc->sc_sme->sme_flags = SME_INIT_REFRESH;
236 1.1 jmcneill
237 1.1 jmcneill cwfg_attach_battery(sc);
238 1.1 jmcneill
239 1.1 jmcneill sysmon_envsys_register(sc->sc_sme);
240 1.1 jmcneill }
241 1.1 jmcneill
242 1.1 jmcneill static int
243 1.1 jmcneill cwfg_set_config(struct cwfg_softc *sc)
244 1.1 jmcneill {
245 1.1 jmcneill u_int alert_level;
246 1.1 jmcneill bool need_update;
247 1.1 jmcneill uint8_t config, mode, val;
248 1.1 jmcneill int error, n;
249 1.1 jmcneill
250 1.1 jmcneill /* Read current config */
251 1.1 jmcneill if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
252 1.1 jmcneill return error;
253 1.1 jmcneill
254 1.1 jmcneill /* Update alert level, if necessary */
255 1.1 jmcneill alert_level = __SHIFTOUT(config, CONFIG_ATHD);
256 1.1 jmcneill if (alert_level != sc->sc_alert_level) {
257 1.1 jmcneill config &= ~CONFIG_ATHD;
258 1.1 jmcneill config |= __SHIFTIN(sc->sc_alert_level, CONFIG_ATHD);
259 1.1 jmcneill if ((error = cwfg_write(sc, CONFIG_REG, config)) != 0)
260 1.1 jmcneill return error;
261 1.1 jmcneill }
262 1.1 jmcneill
263 1.1 jmcneill /* Re-read current config */
264 1.1 jmcneill if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
265 1.1 jmcneill return error;
266 1.1 jmcneill
267 1.1 jmcneill /*
268 1.1 jmcneill * We need to upload a battery profile if either the UFG flag
269 1.1 jmcneill * is unset, or the current battery profile differs from the
270 1.1 jmcneill * one in the DT.
271 1.1 jmcneill */
272 1.1 jmcneill need_update = (config & CONFIG_UFG) == 0;
273 1.1 jmcneill if (need_update == false) {
274 1.1 jmcneill for (n = 0; n < BATINFO_SIZE; n++) {
275 1.1 jmcneill if ((error = cwfg_read(sc, BATINFO_REG(n), &val)) != 0)
276 1.1 jmcneill return error;
277 1.1 jmcneill if (sc->sc_batinfo[n] != val) {
278 1.1 jmcneill need_update = true;
279 1.1 jmcneill break;
280 1.1 jmcneill }
281 1.1 jmcneill }
282 1.1 jmcneill }
283 1.1 jmcneill if (need_update == false)
284 1.1 jmcneill return 0;
285 1.1 jmcneill
286 1.1 jmcneill aprint_verbose_dev(sc->sc_dev, "updating battery profile\n");
287 1.1 jmcneill
288 1.1 jmcneill /* Update battery profile */
289 1.1 jmcneill for (n = 0; n < BATINFO_SIZE; n++) {
290 1.1 jmcneill val = sc->sc_batinfo[n];
291 1.1 jmcneill if ((error = cwfg_write(sc, BATINFO_REG(n), val)) != 0)
292 1.1 jmcneill return error;
293 1.1 jmcneill }
294 1.1 jmcneill
295 1.1 jmcneill /* Set UFG flag to switch to new profile */
296 1.1 jmcneill if ((error = cwfg_read(sc, CONFIG_REG, &config)) != 0)
297 1.1 jmcneill return error;
298 1.1 jmcneill config |= CONFIG_UFG;
299 1.1 jmcneill if ((error = cwfg_write(sc, CONFIG_REG, config)) != 0)
300 1.1 jmcneill return error;
301 1.1 jmcneill
302 1.1 jmcneill /* Restart the IC with new profile */
303 1.1 jmcneill if ((error = cwfg_read(sc, MODE_REG, &mode)) != 0)
304 1.1 jmcneill return error;
305 1.1 jmcneill mode |= MODE_POR;
306 1.1 jmcneill if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
307 1.1 jmcneill return error;
308 1.1 jmcneill delay(20000);
309 1.1 jmcneill mode &= ~MODE_POR;
310 1.1 jmcneill if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
311 1.1 jmcneill return error;
312 1.1 jmcneill
313 1.1 jmcneill return error;
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill static int
317 1.1 jmcneill cwfg_init(struct cwfg_softc *sc)
318 1.1 jmcneill {
319 1.1 jmcneill uint8_t mode, soc;
320 1.1 jmcneill int error, retry;
321 1.1 jmcneill
322 1.1 jmcneill cwfg_lock(sc);
323 1.1 jmcneill
324 1.1 jmcneill /* If the device is in sleep mode, wake it up */
325 1.1 jmcneill if ((error = cwfg_read(sc, MODE_REG, &mode)) != 0)
326 1.1 jmcneill goto done;
327 1.1 jmcneill if (__SHIFTOUT(mode, MODE_SLEEP) == MODE_SLEEP_SLEEP) {
328 1.1 jmcneill mode &= ~MODE_SLEEP;
329 1.1 jmcneill mode |= __SHIFTIN(MODE_SLEEP_WAKE, MODE_SLEEP);
330 1.1 jmcneill if ((error = cwfg_write(sc, MODE_REG, mode)) != 0)
331 1.1 jmcneill goto done;
332 1.1 jmcneill }
333 1.1 jmcneill
334 1.1 jmcneill /* Load battery profile */
335 1.1 jmcneill if ((error = cwfg_set_config(sc)) != 0)
336 1.1 jmcneill goto done;
337 1.1 jmcneill
338 1.1 jmcneill /* Wait for chip to become ready */
339 1.1 jmcneill for (retry = RESET_COUNT; retry > 0; retry--) {
340 1.1 jmcneill if ((error = cwfg_read(sc, SOC_HI_REG, &soc)) != 0)
341 1.1 jmcneill goto done;
342 1.1 jmcneill if (soc != 0xff)
343 1.1 jmcneill break;
344 1.1 jmcneill delay(RESET_DELAY);
345 1.1 jmcneill }
346 1.1 jmcneill if (retry == 0) {
347 1.1 jmcneill aprint_error_dev(sc->sc_dev,
348 1.1 jmcneill "WARNING: timeout waiting for chip ready\n");
349 1.1 jmcneill }
350 1.1 jmcneill
351 1.1 jmcneill done:
352 1.1 jmcneill cwfg_unlock(sc);
353 1.1 jmcneill
354 1.1 jmcneill return error;
355 1.1 jmcneill }
356 1.1 jmcneill
357 1.1 jmcneill static int
358 1.1 jmcneill cwfg_parse_resources(struct cwfg_softc *sc)
359 1.1 jmcneill {
360 1.1 jmcneill const u_int *batinfo;
361 1.5 jmcneill u_int val;
362 1.1 jmcneill int len = 0, n;
363 1.1 jmcneill
364 1.1 jmcneill batinfo = fdtbus_get_prop(sc->sc_phandle,
365 1.5 jmcneill "cellwise,battery-profile", &len);
366 1.5 jmcneill if (batinfo == NULL) {
367 1.5 jmcneill /* DTCOMPAT */
368 1.5 jmcneill batinfo = fdtbus_get_prop(sc->sc_phandle,
369 1.5 jmcneill "cellwise,bat-config-info", &len);
370 1.5 jmcneill }
371 1.1 jmcneill switch (len) {
372 1.1 jmcneill case BATINFO_SIZE:
373 1.1 jmcneill memcpy(sc->sc_batinfo, batinfo, BATINFO_SIZE);
374 1.1 jmcneill break;
375 1.1 jmcneill case BATINFO_SIZE * 4:
376 1.1 jmcneill for (n = 0; n < BATINFO_SIZE; n++)
377 1.1 jmcneill sc->sc_batinfo[n] = be32toh(batinfo[n]);
378 1.1 jmcneill break;
379 1.1 jmcneill default:
380 1.1 jmcneill aprint_error_dev(sc->sc_dev,
381 1.1 jmcneill "missing or invalid battery info\n");
382 1.1 jmcneill return EINVAL;
383 1.1 jmcneill }
384 1.1 jmcneill
385 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle,
386 1.5 jmcneill "cellwise,monitor-interval-ms", &val) == 0) {
387 1.5 jmcneill sc->sc_monitor_interval = howmany(val, 1000);
388 1.5 jmcneill } else if (of_getprop_uint32(sc->sc_phandle,
389 1.5 jmcneill "cellwise,monitor-interval", &val) == 0) {
390 1.5 jmcneill /* DTCOMPAT */
391 1.5 jmcneill sc->sc_monitor_interval = val;
392 1.5 jmcneill } else {
393 1.1 jmcneill sc->sc_monitor_interval = CWFG_MONITOR_INTERVAL_DEFAULT;
394 1.1 jmcneill }
395 1.1 jmcneill
396 1.5 jmcneill const int bphandle = fdtbus_get_phandle(sc->sc_phandle, "monitored-battery");
397 1.5 jmcneill if (bphandle != -1 && of_getprop_uint32(bphandle,
398 1.5 jmcneill "charge-full-design-microamp-hours", &val) == 0) {
399 1.5 jmcneill sc->sc_design_capacity = howmany(val, 1000);
400 1.5 jmcneill } else if (of_getprop_uint32(sc->sc_phandle,
401 1.5 jmcneill "cellwise,design-capacity", &val) == 0) {
402 1.5 jmcneill /* DTCOMPAT */
403 1.5 jmcneill sc->sc_design_capacity = val;
404 1.5 jmcneill } else {
405 1.1 jmcneill sc->sc_design_capacity = CWFG_DESIGN_CAPACITY_DEFAULT;
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill if (of_getprop_uint32(sc->sc_phandle,
409 1.1 jmcneill "cellwise,alert-level", &sc->sc_alert_level) != 0) {
410 1.1 jmcneill sc->sc_alert_level = CWFG_ALERT_LEVEL_DEFAULT;
411 1.1 jmcneill }
412 1.1 jmcneill
413 1.1 jmcneill return 0;
414 1.1 jmcneill }
415 1.1 jmcneill
416 1.1 jmcneill static int
417 1.1 jmcneill cwfg_match(device_t parent, cfdata_t match, void *aux)
418 1.1 jmcneill {
419 1.1 jmcneill struct i2c_attach_args *ia = aux;
420 1.1 jmcneill int match_result;
421 1.1 jmcneill
422 1.1 jmcneill if (iic_use_direct_match(ia, match, compat_data, &match_result))
423 1.1 jmcneill return match_result;
424 1.1 jmcneill
425 1.1 jmcneill /* This device is direct-config only. */
426 1.1 jmcneill
427 1.1 jmcneill return 0;
428 1.1 jmcneill }
429 1.1 jmcneill
430 1.1 jmcneill static void
431 1.1 jmcneill cwfg_attach(device_t parent, device_t self, void *aux)
432 1.1 jmcneill {
433 1.1 jmcneill struct cwfg_softc *sc = device_private(self);
434 1.1 jmcneill struct i2c_attach_args *ia = aux;
435 1.1 jmcneill uint8_t ver;
436 1.1 jmcneill int error;
437 1.1 jmcneill
438 1.1 jmcneill sc->sc_dev = self;
439 1.1 jmcneill sc->sc_i2c = ia->ia_tag;
440 1.1 jmcneill sc->sc_addr = ia->ia_addr;
441 1.6 thorpej sc->sc_phandle = devhandle_to_of(device_handle(self));
442 1.1 jmcneill
443 1.1 jmcneill cwfg_lock(sc);
444 1.1 jmcneill error = cwfg_read(sc, VERSION_REG, &ver);
445 1.1 jmcneill cwfg_unlock(sc);
446 1.1 jmcneill
447 1.1 jmcneill if (error != 0) {
448 1.1 jmcneill aprint_error(": device not responding, error = %d\n", error);
449 1.1 jmcneill return;
450 1.1 jmcneill }
451 1.1 jmcneill
452 1.1 jmcneill aprint_naive("\n");
453 1.1 jmcneill aprint_normal(": CellWise CW2015 Fuel Gauge IC (ver. 0x%02x)\n", ver);
454 1.1 jmcneill
455 1.1 jmcneill if (cwfg_parse_resources(sc) != 0) {
456 1.1 jmcneill aprint_error_dev(self, "failed to parse resources\n");
457 1.1 jmcneill return;
458 1.1 jmcneill }
459 1.1 jmcneill
460 1.1 jmcneill if (cwfg_init(sc) != 0) {
461 1.1 jmcneill aprint_error_dev(self, "failed to initialize device\n");
462 1.1 jmcneill return;
463 1.1 jmcneill }
464 1.1 jmcneill
465 1.1 jmcneill cwfg_attach_sensors(sc);
466 1.1 jmcneill }
467 1.1 jmcneill
468 1.1 jmcneill CFATTACH_DECL_NEW(cwfg, sizeof(struct cwfg_softc),
469 1.1 jmcneill cwfg_match, cwfg_attach, NULL, NULL);
470