pcf8574.c revision 1.2.2.1 1 /* $NetBSD: pcf8574.c,v 1.2.2.1 2020/12/14 14:38:06 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.2.2.1 2020/12/14 14:38:06 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
47 #include <dev/i2c/i2cvar.h>
48 #include <dev/led.h>
49
50 #ifdef PCF8574_DEBUG
51 #define DPRINTF printf
52 #else
53 #define DPRINTF if (0) printf
54 #endif
55
56 struct pcf8574_led {
57 void *cookie;
58 struct led_device *led;
59 uint8_t mask, v_on, v_off;
60 };
61
62 #define PCF8574_NPINS 8
63 struct pcf8574_softc {
64 device_t sc_dev;
65 i2c_tag_t sc_tag;
66 i2c_addr_t sc_addr;
67 uint8_t sc_state;
68 uint8_t sc_mask;
69
70 int sc_nleds;
71 struct pcf8574_led sc_leds[PCF8574_NPINS];
72
73 struct sysmon_envsys *sc_sme;
74 envsys_data_t sc_sensor[PCF8574_NPINS];
75 int sc_pin_sensor[PCF8574_NPINS];
76 int sc_pin_active[PCF8574_NPINS];
77
78 #ifdef PCF8574_DEBUG
79 callout_t sc_timer;
80 #endif
81 };
82
83 static int pcf8574_match(device_t, cfdata_t, void *);
84 static void pcf8574_attach(device_t, device_t, void *);
85 static int pcf8574_detach(device_t, int);
86
87 static int pcf8574_read(struct pcf8574_softc *sc, uint8_t *val);
88 static int pcf8574_write(struct pcf8574_softc *sc, uint8_t val);
89 static void pcf8574_attach_led(
90 struct pcf8574_softc *, char *, int, int, int);
91 void pcf8574_refresh(struct sysmon_envsys *, envsys_data_t *);
92 int pcf8574_get_led(void *);
93 void pcf8574_set_led(void *, int);
94
95 #ifdef PCF8574_DEBUG
96 static void pcf8574_timeout(void *);
97 #endif
98
99 CFATTACH_DECL_NEW(pcf8574io, sizeof(struct pcf8574_softc),
100 pcf8574_match, pcf8574_attach, pcf8574_detach, NULL);
101
102 static const struct device_compatible_entry compat_data[] = {
103 { "i2c-pcf8574", 0 },
104 { NULL, 0 }
105 };
106
107 static int
108 pcf8574_match(device_t parent, cfdata_t cf, void *aux)
109 {
110 struct i2c_attach_args *ia = aux;
111 int match_result;
112
113 if (iic_use_direct_match(ia, cf, compat_data, &match_result))
114 return match_result;
115
116 /* We don't support indirect matches */
117 return 0;
118 }
119
120 static void
121 pcf8574_attach(device_t parent, device_t self, void *aux)
122 {
123 struct pcf8574_softc *sc = device_private(self);
124 struct i2c_attach_args *ia = aux;
125 prop_dictionary_t dict = device_properties(self);
126 prop_array_t pins;
127 prop_dictionary_t pin;
128 int i, num, def, envc = 0;
129 char name[32];
130 const char *nptr = NULL, *spptr;
131 bool ok = TRUE, act, sysmon = FALSE;
132
133 sc->sc_tag = ia->ia_tag;
134 sc->sc_addr = ia->ia_addr;
135 sc->sc_dev = self;
136
137 /*
138 * The PCF8574 requires input pins to be written with the value 1,
139 * and then read. Assume that all pins are input initially.
140 * We'll use the mask when we write, because we have to write a
141 * value for every pin, and we don't want to change input pins.
142 */
143 sc->sc_mask = 0xff;
144
145 /* Try a read, and fail if this component isn't present */
146 if (pcf8574_read(sc, &sc->sc_state)) {
147 aprint_normal(": read failed\n");
148 return;
149 }
150
151 #ifdef PCF8574_DEBUG
152 aprint_normal(": GPIO: state = 0x%02x\n", sc->sc_state);
153
154 callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
155 callout_reset(&sc->sc_timer, hz*30, pcf8574_timeout, sc);
156 #else
157 aprint_normal(": GPIO\n");
158 #endif
159
160 pins = prop_dictionary_get(dict, "pins");
161 if (pins == NULL)
162 return;
163
164 for (i = 0; i < prop_array_count(pins); i++) {
165 pin = prop_array_get(pins, i);
166 ok &= prop_dictionary_get_cstring_nocopy(pin, "name", &nptr);
167 ok &= prop_dictionary_get_uint32(pin, "pin", &num);
168 ok &= prop_dictionary_get_bool(pin, "active_high", &act);
169 /* optional default state */
170 def = -1;
171 prop_dictionary_get_int32(pin, "default_state", &def);
172 if (!ok)
173 continue;
174 /* Extract pin type from the name */
175 spptr = strstr(nptr, " ");
176 if (spptr == NULL)
177 continue;
178 spptr += 1;
179 strncpy(name, spptr, 31);
180 sc->sc_pin_active[i] = act;
181 if (!strncmp(nptr, "LED ", 4)) {
182 sc->sc_mask &= ~(1 << num);
183 pcf8574_attach_led(sc, name, num, act, def);
184 }
185 if (!strncmp(nptr, "INDICATOR ", 4)) {
186 if (!sysmon) {
187 sc->sc_sme = sysmon_envsys_create();
188 sysmon = TRUE;
189 }
190 /* envsys sensor # to pin # mapping */
191 sc->sc_pin_sensor[envc] = num;
192 sc->sc_sensor[i].state = ENVSYS_SINVALID;
193 sc->sc_sensor[i].units = ENVSYS_INDICATOR;
194 strlcpy(sc->sc_sensor[i].desc, name,
195 sizeof(sc->sc_sensor[i].desc));
196 if (sysmon_envsys_sensor_attach(sc->sc_sme,
197 &sc->sc_sensor[i])) {
198 sysmon_envsys_destroy(sc->sc_sme);
199 sc->sc_sme = NULL;
200 aprint_error_dev(self,
201 "unable to attach pin %d at sysmon\n", i);
202 return;
203 }
204 DPRINTF("%s: added indicator: pin %d sensor %d (%s)\n",
205 device_xname(sc->sc_dev), num, envc, name);
206 envc++;
207 }
208 }
209
210 if (sysmon) {
211 sc->sc_sme->sme_name = device_xname(self);
212 sc->sc_sme->sme_cookie = sc;
213 sc->sc_sme->sme_refresh = pcf8574_refresh;
214 if (sysmon_envsys_register(sc->sc_sme)) {
215 aprint_error_dev(self,
216 "unable to register with sysmon\n");
217 sysmon_envsys_destroy(sc->sc_sme);
218 sc->sc_sme = NULL;
219 return;
220 }
221 }
222 }
223
224 static int
225 pcf8574_detach(device_t self, int flags)
226 {
227 struct pcf8574_softc *sc = device_private(self);
228 int i;
229
230 if (sc->sc_sme != NULL) {
231 sysmon_envsys_unregister(sc->sc_sme);
232 sc->sc_sme = NULL;
233 }
234
235 for (i = 0; i < sc->sc_nleds; i++)
236 led_detach(sc->sc_leds[i].led);
237
238 #ifdef PCF8574_DEBUG
239 callout_halt(&sc->sc_timer, NULL);
240 callout_destroy(&sc->sc_timer);
241 #endif
242 return 0;
243 }
244
245 static int
246 pcf8574_read(struct pcf8574_softc *sc, uint8_t *val)
247 {
248 int err = 0;
249
250 if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
251 return err;
252 err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
253 sc->sc_addr, NULL, 0, val, 1, 0);
254 iic_release_bus(sc->sc_tag, 0);
255 return err;
256 }
257
258 static int
259 pcf8574_write(struct pcf8574_softc *sc, uint8_t val)
260 {
261 int err = 0;
262
263 if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
264 return err;
265 err = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
266 &val, 1, NULL, 0, 0);
267 iic_release_bus(sc->sc_tag, 0);
268 return err;
269 }
270
271 static void
272 pcf8574_attach_led(struct pcf8574_softc *sc, char *n, int pin, int act, int def)
273 {
274 struct pcf8574_led *l;
275
276 l = &sc->sc_leds[sc->sc_nleds];
277 l->cookie = sc;
278 l->mask = 1 << pin;
279 l->v_on = act ? l->mask : 0;
280 l->v_off = act ? 0 : l->mask;
281 led_attach(n, l, pcf8574_get_led, pcf8574_set_led);
282 if (def != -1)
283 pcf8574_set_led(l, def);
284 DPRINTF("%s: attached LED: %02x %02x %02x def %d\n",
285 device_xname(sc->sc_dev), l->mask, l->v_on, l->v_off, def);
286 sc->sc_nleds++;
287 }
288
289 void
290 pcf8574_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
291 {
292 struct pcf8574_softc *sc = sme->sme_cookie;
293 int pin = sc->sc_pin_sensor[edata->sensor];
294 int act = sc->sc_pin_active[pin];
295
296 pcf8574_read(sc, &sc->sc_state);
297 if (act)
298 edata->value_cur = sc->sc_state & 1 << pin ? TRUE : FALSE;
299 else
300 edata->value_cur = sc->sc_state & 1 << pin ? FALSE : TRUE;
301 edata->state = ENVSYS_SVALID;
302 }
303
304 int
305 pcf8574_get_led(void *cookie)
306 {
307 struct pcf8574_led *l = cookie;
308 struct pcf8574_softc *sc = l->cookie;
309
310 return ((sc->sc_state & l->mask) == l->v_on);
311 }
312
313 void
314 pcf8574_set_led(void *cookie, int val)
315 {
316 struct pcf8574_led *l = cookie;
317 struct pcf8574_softc *sc = l->cookie;
318 uint32_t newstate;
319
320 newstate = sc->sc_state & ~l->mask;
321 newstate |= val ? l->v_on : l->v_off;
322 DPRINTF("%s: set LED: %02x -> %02x, %02x %02x %02x\n",
323 device_xname(sc->sc_dev), sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
324 if (newstate != sc->sc_state) {
325 pcf8574_write(sc, newstate | sc->sc_mask);
326 pcf8574_read(sc, &sc->sc_state);
327 }
328 }
329
330 #ifdef PCF8574_DEBUG
331 static void
332 pcf8574_timeout(void *v)
333 {
334 struct pcf8574_softc *sc = v;
335 uint8_t val;
336
337 pcf8574_read(sc, &val);
338 if (val != sc->sc_state)
339 aprint_normal_dev(sc->sc_dev,
340 "status change: 0x%02x > 0x%02x\n", sc->sc_state, val);
341 sc->sc_state = val;
342
343 callout_reset(&sc->sc_timer, hz*60, pcf8574_timeout, sc);
344 }
345 #endif
346