pcf8574.c revision 1.7 1 /* $NetBSD: pcf8574.c,v 1.7 2021/01/17 21:42:35 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Julian Coleman.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * A driver for Philips Semiconductor (NXP) PCF8574/PCF857A GPIO's.
34 * Uses device properties to connect pins to the appropriate subsystem.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: pcf8574.c,v 1.7 2021/01/17 21:42:35 thorpej Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/kernel.h>
44
45 #include <dev/sysmon/sysmonvar.h>
46 #include <dev/sysmon/sysmon_taskq.h>
47
48 #include <dev/i2c/i2cvar.h>
49 #include <dev/led.h>
50
51 #ifdef PCF8574_DEBUG
52 #define DPRINTF printf
53 #else
54 #define DPRINTF if (0) printf
55 #endif
56
57 struct pcf8574_led {
58 void *cookie;
59 struct led_device *led;
60 uint8_t mask, v_on, v_off;
61 };
62
63 struct pcf8574_pin {
64 int pin_sensor;
65 int pin_active;
66 char pin_desc[ENVSYS_DESCLEN];
67 };
68
69 #define PCF8574_NPINS 8
70 struct pcf8574_softc {
71 device_t sc_dev;
72 i2c_tag_t sc_tag;
73 i2c_addr_t sc_addr;
74 uint8_t sc_state;
75 uint8_t sc_mask;
76
77 uint8_t sc_alert_mask;
78 #define PCF8574_DEFAULT_TIMER 60
79 int sc_callout_time;
80 callout_t sc_timer;
81
82 int sc_nleds;
83 struct pcf8574_led sc_leds[PCF8574_NPINS];
84 struct pcf8574_pin sc_pins[PCF8574_NPINS];
85
86 struct sysmon_envsys *sc_sme;
87 envsys_data_t sc_sensor[PCF8574_NPINS];
88 };
89
90 static int pcf8574_match(device_t, cfdata_t, void *);
91 static void pcf8574_attach(device_t, device_t, void *);
92 static int pcf8574_detach(device_t, int);
93
94 static int pcf8574_read(struct pcf8574_softc *sc, uint8_t *);
95 static int pcf8574_write(struct pcf8574_softc *sc, uint8_t);
96 static void pcf8574_attach_led(
97 struct pcf8574_softc *, char *, int, int, int);
98 static int pcf8574_attach_sysmon(
99 struct pcf8574_softc *, char *, int, int, int);
100 void pcf8574_refresh(struct sysmon_envsys *, envsys_data_t *);
101 int pcf8574_get_led(void *);
102 void pcf8574_set_led(void *, int);
103 static void pcf8574_timeout(void *);
104 static void pcf8574_check(void *);
105 static void pcf8574_check_alert(struct pcf8574_softc *, uint8_t, uint8_t);
106
107 CFATTACH_DECL_NEW(pcf8574io, sizeof(struct pcf8574_softc),
108 pcf8574_match, pcf8574_attach, pcf8574_detach, NULL);
109
110 static const struct device_compatible_entry compat_data[] = {
111 { .compat = "i2c-pcf8574" },
112
113 { 0 }
114 };
115
116 static int
117 pcf8574_match(device_t parent, cfdata_t cf, void *aux)
118 {
119 struct i2c_attach_args *ia = aux;
120 int match_result;
121
122 if (iic_use_direct_match(ia, cf, compat_data, &match_result))
123 return match_result;
124
125 /* We don't support indirect matches */
126 return 0;
127 }
128
129 static void
130 pcf8574_attach(device_t parent, device_t self, void *aux)
131 {
132 struct pcf8574_softc *sc = device_private(self);
133 struct i2c_attach_args *ia = aux;
134 prop_dictionary_t dict = device_properties(self);
135 prop_array_t pins;
136 prop_dictionary_t pin;
137 int i, num, def, envc = 0;
138 char name[32];
139 const char *nptr = NULL, *spptr;
140 bool ok = TRUE, act;
141
142 sc->sc_tag = ia->ia_tag;
143 sc->sc_addr = ia->ia_addr;
144 sc->sc_dev = self;
145
146 sc->sc_sme = NULL;
147 #ifdef PCF8574_DEBUG
148 sc->sc_callout_time = 60; /* watch for changes when debugging */
149 #else
150 sc->sc_callout_time = 0;
151 #endif
152
153 /*
154 * The PCF8574 requires input pins to be written with the value 1,
155 * and then read. Assume that all pins are input initially.
156 * We'll use the mask when we write, because we have to write a
157 * value for every pin, and we don't want to change input pins.
158 */
159 sc->sc_mask = 0xff;
160
161 /* Try a read, and fail if this component isn't present */
162 if (pcf8574_read(sc, &sc->sc_state)) {
163 aprint_normal(": read failed\n");
164 return;
165 }
166
167 #ifdef PCF8574_DEBUG
168 aprint_normal(": GPIO: state = 0x%02x\n", sc->sc_state);
169 #else
170 aprint_normal(": GPIO\n");
171 #endif
172
173 pins = prop_dictionary_get(dict, "pins");
174 if (pins == NULL)
175 return;
176
177 for (i = 0; i < prop_array_count(pins); i++) {
178 pin = prop_array_get(pins, i);
179 ok &= prop_dictionary_get_cstring_nocopy(pin, "name", &nptr);
180 ok &= prop_dictionary_get_uint32(pin, "pin", &num);
181 ok &= prop_dictionary_get_bool(pin, "active_high", &act);
182 /* optional default state */
183 def = -1;
184 prop_dictionary_get_int32(pin, "default_state", &def);
185 if (!ok)
186 continue;
187 /* Extract pin type from the name */
188 spptr = strstr(nptr, " ");
189 if (spptr == NULL)
190 continue;
191 spptr += 1;
192 strncpy(name, spptr, 31);
193 sc->sc_pins[i].pin_active = act;
194 if (!strncmp(nptr, "LED ", 4)) {
195 sc->sc_mask &= ~(1 << num);
196 pcf8574_attach_led(sc, name, num, act, def);
197 }
198 if (!strncmp(nptr, "INDICATOR ", 10)) {
199 if (pcf8574_attach_sysmon(sc, name, envc, num, act))
200 return;
201 envc++;
202 }
203 if (!strncmp(nptr, "ALERT ", 6)) {
204 u_int64_t alert_time;
205
206 if (pcf8574_attach_sysmon(sc, name, envc, num, act))
207 return;
208
209 /* Adjust our timeout if the alert times out. */
210 if (def != -1)
211 alert_time = def / 2;
212 else
213 alert_time = PCF8574_DEFAULT_TIMER;
214
215 if (sc->sc_callout_time == 0 ||
216 alert_time < sc->sc_callout_time)
217 sc->sc_callout_time = alert_time;
218
219 sc->sc_alert_mask |= (1 << num);
220 envc++;
221 }
222 }
223
224 if (sc->sc_sme != NULL) {
225 sc->sc_sme->sme_name = device_xname(self);
226 sc->sc_sme->sme_cookie = sc;
227 sc->sc_sme->sme_refresh = pcf8574_refresh;
228 if (sysmon_envsys_register(sc->sc_sme)) {
229 aprint_error_dev(self,
230 "unable to register with sysmon\n");
231 sysmon_envsys_destroy(sc->sc_sme);
232 sc->sc_sme = NULL;
233 return;
234 }
235 }
236
237 if (sc->sc_callout_time) {
238 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
239 callout_reset(&sc->sc_timer, hz * sc->sc_callout_time,
240 pcf8574_timeout, sc);
241 }
242 }
243
244 static int
245 pcf8574_detach(device_t self, int flags)
246 {
247 struct pcf8574_softc *sc = device_private(self);
248 int i;
249
250 if (sc->sc_callout_time) {
251 callout_halt(&sc->sc_timer, NULL);
252 callout_destroy(&sc->sc_timer);
253 sc->sc_callout_time = 0;
254 }
255
256 if (sc->sc_sme != NULL) {
257 sysmon_envsys_unregister(sc->sc_sme);
258 sc->sc_sme = NULL;
259 }
260
261 for (i = 0; i < sc->sc_nleds; i++)
262 led_detach(sc->sc_leds[i].led);
263
264 return 0;
265 }
266
267 static int
268 pcf8574_read(struct pcf8574_softc *sc, uint8_t *val)
269 {
270 int err = 0;
271
272 if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
273 return err;
274 err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
275 sc->sc_addr, NULL, 0, val, 1, 0);
276 iic_release_bus(sc->sc_tag, 0);
277 return err;
278 }
279
280 static int
281 pcf8574_write(struct pcf8574_softc *sc, uint8_t val)
282 {
283 int err = 0;
284
285 if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
286 return err;
287 err = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
288 &val, 1, NULL, 0, 0);
289 iic_release_bus(sc->sc_tag, 0);
290 return err;
291 }
292
293 static void
294 pcf8574_attach_led(struct pcf8574_softc *sc, char *name, int pin, int act,
295 int def)
296 {
297 struct pcf8574_led *l;
298
299 l = &sc->sc_leds[sc->sc_nleds];
300 l->cookie = sc;
301 l->mask = 1 << pin;
302 l->v_on = act ? l->mask : 0;
303 l->v_off = act ? 0 : l->mask;
304 led_attach(name, l, pcf8574_get_led, pcf8574_set_led);
305 if (def != -1)
306 pcf8574_set_led(l, def);
307 DPRINTF("%s: attached LED: %02x %02x %02x def %d\n",
308 device_xname(sc->sc_dev), l->mask, l->v_on, l->v_off, def);
309 sc->sc_nleds++;
310 }
311
312 static int
313 pcf8574_attach_sysmon(struct pcf8574_softc *sc, char *name, int envc, int pin,
314 int act)
315 {
316 int ret;
317
318 if (sc->sc_sme == NULL) {
319 sc->sc_sme = sysmon_envsys_create();
320 sc->sc_sme->sme_events_timeout = 0;
321 }
322
323 strlcpy(sc->sc_pins[pin].pin_desc, name,
324 sizeof(sc->sc_pins[pin].pin_desc));
325 /* envsys sensor # to pin # mapping */
326 sc->sc_pins[envc].pin_sensor = pin;
327 sc->sc_sensor[envc].state = ENVSYS_SINVALID;
328 sc->sc_sensor[envc].units = ENVSYS_INDICATOR;
329 strlcpy(sc->sc_sensor[envc].desc, name,
330 sizeof(sc->sc_sensor[envc].desc));
331 ret = sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[envc]);
332 if (ret) {
333 sysmon_envsys_destroy(sc->sc_sme);
334 sc->sc_sme = NULL;
335 aprint_error_dev(sc->sc_dev,
336 "unable to attach pin %d at sysmon\n", pin);
337 return ret;
338 }
339 DPRINTF("%s: added sysmon: pin %d sensor %d (%s)\n",
340 device_xname(sc->sc_dev), pin, envc, name);
341 return 0;
342 }
343
344 void
345 pcf8574_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
346 {
347 struct pcf8574_softc *sc = sme->sme_cookie;
348 int pin = sc->sc_pins[edata->sensor].pin_sensor;
349 int act = sc->sc_pins[pin].pin_active;
350 u_int8_t prev_state = sc->sc_state;
351
352 pcf8574_read(sc, &sc->sc_state);
353 if (act)
354 edata->value_cur = sc->sc_state & 1 << pin ? TRUE : FALSE;
355 else
356 edata->value_cur = sc->sc_state & 1 << pin ? FALSE : TRUE;
357 edata->state = ENVSYS_SVALID;
358
359 /* We read all the pins, so check for alerts on any pin now */
360 if (sc->sc_state != prev_state) {
361 DPRINTF("%s: (refresh) status change: 0x%02x > 0x%02x\n",
362 device_xname(sc->sc_dev), prev_state, sc->sc_state);
363 pcf8574_check_alert(sc, prev_state, sc->sc_state);
364 }
365 }
366
367 int
368 pcf8574_get_led(void *cookie)
369 {
370 struct pcf8574_led *l = cookie;
371 struct pcf8574_softc *sc = l->cookie;
372
373 return ((sc->sc_state & l->mask) == l->v_on);
374 }
375
376 void
377 pcf8574_set_led(void *cookie, int val)
378 {
379 struct pcf8574_led *l = cookie;
380 struct pcf8574_softc *sc = l->cookie;
381 uint32_t newstate;
382
383 newstate = sc->sc_state & ~l->mask;
384 newstate |= val ? l->v_on : l->v_off;
385 DPRINTF("%s: set LED: %02x -> %02x, %02x %02x %02x\n",
386 device_xname(sc->sc_dev), sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
387 if (newstate != sc->sc_state) {
388 pcf8574_write(sc, newstate | sc->sc_mask);
389 pcf8574_read(sc, &sc->sc_state);
390 }
391 }
392
393 static void
394 pcf8574_timeout(void *v)
395 {
396 struct pcf8574_softc *sc = v;
397
398 sysmon_task_queue_sched(0, pcf8574_check, sc);
399 callout_reset(&sc->sc_timer, hz * sc->sc_callout_time,
400 pcf8574_timeout, sc);
401 }
402
403 static void
404 pcf8574_check(void *v)
405 {
406 struct pcf8574_softc *sc = v;
407 uint8_t prev_state = sc->sc_state;
408
409 pcf8574_read(sc, &sc->sc_state);
410 if (sc->sc_state != prev_state) {
411 DPRINTF("%s: (check) status change: 0x%02x > 0x%02x\n",
412 device_xname(sc->sc_dev), prev_state, sc->sc_state);
413 pcf8574_check_alert(sc, prev_state, sc->sc_state);
414 }
415 }
416
417
418 static void
419 pcf8574_check_alert(struct pcf8574_softc *sc, uint8_t prev_state,
420 uint8_t curr_state)
421 {
422 int i;
423 uint8_t pin_chg;
424
425 for (i = 0; i < PCF8574_NPINS; i++) {
426 pin_chg = (sc->sc_state & 1 << i) ^ (prev_state & 1 << i);
427 if (pin_chg & sc->sc_alert_mask) {
428 if (sc->sc_pins[i].pin_active)
429 printf("%s: Alert: %s = %s\n",
430 device_xname(sc->sc_dev),
431 sc->sc_pins[i].pin_desc,
432 sc->sc_state & 1 << i ? "True" : "False");
433 else
434 printf("%s: Alert: %s = %s\n",
435 device_xname(sc->sc_dev),
436 sc->sc_pins[i].pin_desc,
437 sc->sc_state & 1 << i ? "False" : "True");
438 }
439 }
440 }
441