Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* $NetBSD: pcf8574.c,v 1.10 2021/06/21 03:12:54 christos Exp $ */

/*-
 * Copyright (c) 2020 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Julian Coleman.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * A driver for Philips Semiconductor (NXP) PCF8574/PCF857A GPIO's.
 * Uses device properties to connect pins to the appropriate subsystem.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pcf8574.c,v 1.10 2021/06/21 03:12:54 christos Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>

#include <dev/sysmon/sysmonvar.h>
#include <dev/sysmon/sysmon_taskq.h>

#include <dev/i2c/i2cvar.h>
#include <dev/led.h>

#ifdef PCF8574_DEBUG
#define DPRINTF printf
#else
#define DPRINTF if (0) printf
#endif

struct pcf8574_led {
	void *cookie;
	struct led_device *led;
	uint8_t mask, v_on, v_off;
};

struct pcf8574_pin {
	int pin_sensor;
	int pin_active;
	char pin_desc[ENVSYS_DESCLEN];
};

#define PCF8574_NPINS	8
struct pcf8574_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_tag;
	i2c_addr_t	sc_addr;
	uint8_t		sc_state;
	uint8_t		sc_mask;

	uint8_t		sc_alert_mask;
#define	PCF8574_DEFAULT_TIMER	60
	int		sc_callout_time;
	callout_t	sc_timer;

	int		sc_nleds;
	struct pcf8574_led sc_leds[PCF8574_NPINS];
	struct pcf8574_pin sc_pins[PCF8574_NPINS];

	struct sysmon_envsys *sc_sme;
	envsys_data_t	sc_sensor[PCF8574_NPINS];
};

static int	pcf8574_match(device_t, cfdata_t, void *);
static void	pcf8574_attach(device_t, device_t, void *);
static int	pcf8574_detach(device_t, int);

static int	pcf8574_read(struct pcf8574_softc *sc, uint8_t *);
static int	pcf8574_write(struct pcf8574_softc *sc, uint8_t);
static void	pcf8574_attach_led(
			struct pcf8574_softc *, char *, int, int, int);
static int	pcf8574_attach_sysmon(
			struct pcf8574_softc *, char *, int, int, int);
void		pcf8574_refresh(struct sysmon_envsys *, envsys_data_t *);
int		pcf8574_get_led(void *);
void		pcf8574_set_led(void *, int);
static void	pcf8574_timeout(void *);
static void	pcf8574_check(void *);
static void	pcf8574_check_alert(struct pcf8574_softc *, uint8_t, uint8_t);

CFATTACH_DECL_NEW(pcf8574io, sizeof(struct pcf8574_softc),
	pcf8574_match, pcf8574_attach, pcf8574_detach, NULL);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "i2c-pcf8574" },
	DEVICE_COMPAT_EOL
};

static int
pcf8574_match(device_t parent, cfdata_t cf, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int match_result;

	if (iic_use_direct_match(ia, cf, compat_data, &match_result))
		return match_result;

	/* We don't support indirect matches */
	return 0;
}

static void
pcf8574_attach(device_t parent, device_t self, void *aux)
{
	struct pcf8574_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;
	prop_dictionary_t dict = device_properties(self);
	prop_array_t pins;
	prop_dictionary_t pin;
	int i, num, def, envc = 0;
	char name[32];
	const char *nptr = NULL, *spptr;
	bool ok = TRUE, act;

	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_dev = self;

	sc->sc_sme = NULL;
#ifdef PCF8574_DEBUG
	sc->sc_callout_time = 60;	/* watch for changes when debugging */
#else
	sc->sc_callout_time = 0;
#endif

	/*
	 * The PCF8574 requires input pins to be written with the value 1,
	 * and then read.  Assume that all pins are input initially.
	 * We'll use the mask when we write, because we have to write a
	 * value for every pin, and we don't want to change input pins.
	 */
	sc->sc_mask = 0xff;

	/* Try a read, and fail if this component isn't present */
	if (pcf8574_read(sc, &sc->sc_state)) {
		aprint_normal(": read failed\n");
		return;
	}

#ifdef PCF8574_DEBUG
	aprint_normal(": GPIO: state = 0x%02x\n", sc->sc_state);
#else
	aprint_normal(": GPIO\n");
#endif

	pins = prop_dictionary_get(dict, "pins");
	if (pins == NULL)
		return;

	for (i = 0; i < prop_array_count(pins); i++) {
		pin = prop_array_get(pins, i);
		ok &= prop_dictionary_get_string(pin, "name", &nptr);
		ok &= prop_dictionary_get_uint32(pin, "pin", &num);
		ok &= prop_dictionary_get_bool(pin, "active_high", &act);
		/* optional default state */
		def = -1;
		prop_dictionary_get_int32(pin, "default_state", &def);
		if (!ok)
			continue;
		/* Extract pin type from the name */
		spptr = strstr(nptr, " ");
		if (spptr == NULL)
			continue;
		spptr += 1;
		strncpy(name, spptr, 31);
		sc->sc_pins[i].pin_active = act;
		if (!strncmp(nptr, "LED ", 4)) {
			sc->sc_mask &= ~(1 << num);
			pcf8574_attach_led(sc, name, num, act, def);
		}
		if (!strncmp(nptr, "INDICATOR ", 10)) {
			if (pcf8574_attach_sysmon(sc, name, envc, num, act))
				return;
			envc++;
		}
		if (!strncmp(nptr, "ALERT ", 6)) {
			u_int64_t alert_time;

			if (pcf8574_attach_sysmon(sc, name, envc, num, act))
				return;

			/* Adjust our timeout if the alert times out. */
			if (def != -1)
				alert_time = def / 2;
			else
				alert_time = PCF8574_DEFAULT_TIMER;
			
			if (sc->sc_callout_time == 0 ||
			    alert_time < sc->sc_callout_time)
				sc->sc_callout_time = alert_time;

			sc->sc_alert_mask |= (1 << num);
			envc++;
		}
	}

	if (sc->sc_sme != NULL) {
		sc->sc_sme->sme_name = device_xname(self);
		sc->sc_sme->sme_cookie = sc;
		sc->sc_sme->sme_refresh = pcf8574_refresh;
		if (sysmon_envsys_register(sc->sc_sme)) {
			aprint_error_dev(self,
			    "unable to register with sysmon\n");
			sysmon_envsys_destroy(sc->sc_sme);
			sc->sc_sme = NULL;
			return;
		}
	}

	if (sc->sc_callout_time) {
		callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
		callout_reset(&sc->sc_timer, hz * sc->sc_callout_time,
		    pcf8574_timeout, sc);
	}
}

static int
pcf8574_detach(device_t self, int flags)
{
	struct pcf8574_softc *sc = device_private(self);
	int i;

	if (sc->sc_callout_time) {
		callout_halt(&sc->sc_timer, NULL);
		callout_destroy(&sc->sc_timer);
		sc->sc_callout_time = 0;
	}

	if (sc->sc_sme != NULL) {
		sysmon_envsys_unregister(sc->sc_sme);
		sc->sc_sme = NULL;
	}

	for (i = 0; i < sc->sc_nleds; i++)
		led_detach(sc->sc_leds[i].led);

	return 0;
}

static int
pcf8574_read(struct pcf8574_softc *sc, uint8_t *val)
{
	int err = 0;

	if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
		return err;
	err = iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP,
	    sc->sc_addr, NULL, 0, val, 1, 0);
	iic_release_bus(sc->sc_tag, 0);
	return err;
}

static int
pcf8574_write(struct pcf8574_softc *sc, uint8_t val)
{
	int err = 0;

	if ((err = iic_acquire_bus(sc->sc_tag, 0)) != 0)
		return err;
	err = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
	    &val, 1, NULL, 0, 0);
	iic_release_bus(sc->sc_tag, 0);
	return err;
}

static void
pcf8574_attach_led(struct pcf8574_softc *sc, char *name, int pin, int act,
	int def)
{
	struct pcf8574_led *l;

	l = &sc->sc_leds[sc->sc_nleds];
	l->cookie = sc;
	l->mask = 1 << pin;
	l->v_on = act ? l->mask : 0;
	l->v_off = act ? 0 : l->mask;
	led_attach(name, l, pcf8574_get_led, pcf8574_set_led);
	if (def != -1)
		pcf8574_set_led(l, def);
	DPRINTF("%s: attached LED: %02x %02x %02x def %d\n",
	    device_xname(sc->sc_dev), l->mask, l->v_on, l->v_off, def);
	sc->sc_nleds++;
}

static int
pcf8574_attach_sysmon(struct pcf8574_softc *sc, char *name, int envc, int pin,
	int act)
{
	int ret;

	if (sc->sc_sme == NULL) {
		sc->sc_sme = sysmon_envsys_create();
		sc->sc_sme->sme_events_timeout = 0;
	}

	strlcpy(sc->sc_pins[pin].pin_desc, name,
	    sizeof(sc->sc_pins[pin].pin_desc));
	/* envsys sensor # to pin # mapping */
	sc->sc_pins[envc].pin_sensor = pin;
	sc->sc_sensor[envc].state = ENVSYS_SINVALID;
	sc->sc_sensor[envc].units = ENVSYS_INDICATOR;
	strlcpy(sc->sc_sensor[envc].desc, name,
	    sizeof(sc->sc_sensor[envc].desc));
	ret = sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[envc]);
	if (ret) {
		sysmon_envsys_destroy(sc->sc_sme);
		sc->sc_sme = NULL;
		aprint_error_dev(sc->sc_dev,
		    "unable to attach pin %d at sysmon\n", pin);
		return ret;
	}
	DPRINTF("%s: added sysmon: pin %d sensor %d (%s)\n",
	    device_xname(sc->sc_dev), pin, envc, name);
	return 0;
}

void
pcf8574_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
{
	struct pcf8574_softc *sc = sme->sme_cookie;
	int pin = sc->sc_pins[edata->sensor].pin_sensor;
	int act = sc->sc_pins[pin].pin_active;
	u_int8_t prev_state = sc->sc_state;

	pcf8574_read(sc, &sc->sc_state);
	if (act)
		edata->value_cur = sc->sc_state & 1 << pin ? TRUE : FALSE;
	else
		edata->value_cur = sc->sc_state & 1 << pin ? FALSE : TRUE;
	edata->state = ENVSYS_SVALID;

	/* We read all the pins, so check for alerts on any pin now */
	if (sc->sc_state != prev_state) {
		DPRINTF("%s: (refresh) status change: 0x%02x > 0x%02x\n",
                    device_xname(sc->sc_dev), prev_state, sc->sc_state);
		pcf8574_check_alert(sc, prev_state, sc->sc_state);
	}
}

int
pcf8574_get_led(void *cookie)
{
	struct pcf8574_led *l = cookie;
	struct pcf8574_softc *sc = l->cookie;

	return ((sc->sc_state & l->mask) == l->v_on);
}

void
pcf8574_set_led(void *cookie, int val)
{
	struct pcf8574_led *l = cookie;
	struct pcf8574_softc *sc = l->cookie;
	uint32_t newstate;	

	newstate = sc->sc_state & ~l->mask;
	newstate |= val ? l->v_on : l->v_off;
	DPRINTF("%s: set LED: %02x -> %02x, %02x %02x %02x\n",
	    device_xname(sc->sc_dev), sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
	if (newstate != sc->sc_state) {
		pcf8574_write(sc, newstate | sc->sc_mask);
		pcf8574_read(sc, &sc->sc_state);
	}
}

static void
pcf8574_timeout(void *v)
{
	struct pcf8574_softc *sc = v;

	sysmon_task_queue_sched(0, pcf8574_check, sc);
	callout_reset(&sc->sc_timer, hz * sc->sc_callout_time,
	    pcf8574_timeout, sc);
}

static void
pcf8574_check(void *v)
{
	struct pcf8574_softc *sc = v;
	uint8_t prev_state = sc->sc_state;

	pcf8574_read(sc, &sc->sc_state);
	if (sc->sc_state != prev_state) {
		DPRINTF("%s: (check) status change: 0x%02x > 0x%02x\n",
		    device_xname(sc->sc_dev), prev_state, sc->sc_state);
		pcf8574_check_alert(sc, prev_state, sc->sc_state);
	}
}


static void
pcf8574_check_alert(struct pcf8574_softc *sc, uint8_t prev_state,
	uint8_t curr_state)
{
	int i;
	uint8_t pin_chg;

	for (i = 0; i < PCF8574_NPINS; i++) {
		pin_chg = (sc->sc_state & 1 << i) ^ (prev_state & 1 << i);
		if (pin_chg & sc->sc_alert_mask) {
			if (sc->sc_pins[i].pin_active)
				printf("%s: Alert: %s = %s\n",
				    device_xname(sc->sc_dev),
				    sc->sc_pins[i].pin_desc,
				    sc->sc_state & 1 << i ?  "True" : "False");
			else
				printf("%s: Alert: %s = %s\n",
				    device_xname(sc->sc_dev),
				    sc->sc_pins[i].pin_desc,
				    sc->sc_state & 1 << i ?  "False" : "True");
		}
	}
}