summaryrefslogtreecommitdiff
path: root/sys/dev/i2c/pcagpio.c
blob: 1ffd697ea6eebc7ada4c67ea835f49ce6ea52147 (plain)
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
/* $NetBSD: pcagpio.c,v 1.12 2022/02/12 03:24:35 riastradh Exp $ */

/*-
 * Copyright (c) 2020 Michael Lorenz
 * All rights reserved.
 *
 * 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 PCA9555 GPIO controllers
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pcagpio.c,v 1.12 2022/02/12 03:24:35 riastradh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#ifdef PCAGPIO_DEBUG
#include <sys/kernel.h>
#endif
#include <sys/conf.h>
#include <sys/bus.h>

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

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

/* commands */
#define PCAGPIO_INPUT	0x00	/* line status */
#define PCAGPIO_OUTPUT	0x01	/* output status */
#define PCAGPIO_REVERT	0x02	/* revert input if set */
#define PCAGPIO_CONFIG	0x03	/* input if set, output if not */

static int	pcagpio_match(device_t, cfdata_t, void *);
static void	pcagpio_attach(device_t, device_t, void *);
static int	pcagpio_detach(device_t, int);
#ifdef PCAGPIO_DEBUG
static void	pcagpio_timeout(void *);
#endif

/* we can only pass one cookie to led_attach() but we need several values... */
struct pcagpio_led {
	void *cookie;
	struct led_device *led;
	uint32_t mask, v_on, v_off;
};

struct pcagpio_softc {
	device_t	sc_dev;
	i2c_tag_t	sc_i2c;
	i2c_addr_t	sc_addr;

	int		sc_is_16bit;
	uint32_t	sc_state;
	struct pcagpio_led sc_leds[16];
	int		sc_nleds;

#ifdef PCAGPIO_DEBUG
	uint32_t	sc_dir, sc_in;
	callout_t	sc_timer;
#endif
};


static void 	pcagpio_writereg(struct pcagpio_softc *, int, uint32_t);
static uint32_t pcagpio_readreg(struct pcagpio_softc *, int);
static void	pcagpio_attach_led(
			struct pcagpio_softc *, char *, int, int, int);
static int	pcagpio_get(void *);
static void	pcagpio_set(void *, int);

CFATTACH_DECL_NEW(pcagpio, sizeof(struct pcagpio_softc),
    pcagpio_match, pcagpio_attach, pcagpio_detach, NULL);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "i2c-pca9555",	.value = 1 },
	{ .compat = "pca9555",		.value = 1 },
	{ .compat = "i2c-pca9556",	.value = 0 },
	{ .compat = "pca9556",		.value = 0 },
	DEVICE_COMPAT_EOL
};

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

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

	return 0;
}

#ifdef PCAGPIO_DEBUG
static void
printdir(char* name, uint32_t val, uint32_t mask, char letter)
{
	char flags[17], bits[17];
	uint32_t bit = 0x8000;
	int i;

	val &= mask;
	for (i = 0; i < 16; i++) {
		flags[i] = (mask & bit) ? letter : '-';
		bits[i] = (val & bit) ? 'X' : ' ';
		bit = bit >> 1;
	}
	flags[16] = 0;
	bits[16] = 0;
	printf("%s: dir: %s\n", name, flags);
	printf("%s: lvl: %s\n", name, bits);
}	
#endif

static void
pcagpio_attach(device_t parent, device_t self, void *aux)
{
	struct pcagpio_softc *sc = device_private(self);
	struct i2c_attach_args *ia = aux;
	const struct device_compatible_entry *dce;
	prop_dictionary_t dict = device_properties(self);
	prop_array_t pins;
	prop_dictionary_t pin;

	sc->sc_dev = self;
	sc->sc_i2c = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_nleds = 0;

	aprint_naive("\n");
	sc->sc_is_16bit = 0;
	if ((dce = iic_compatible_lookup(ia, compat_data)) != NULL)
		sc->sc_is_16bit = dce->value;

	aprint_normal(": %s\n", sc->sc_is_16bit ? "PCA9555" : "PCA9556");

	sc->sc_state = pcagpio_readreg(sc, PCAGPIO_OUTPUT);

#ifdef PCAGPIO_DEBUG
	uint32_t in, out;
	sc->sc_dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
	sc->sc_in = pcagpio_readreg(sc, PCAGPIO_INPUT);
	in = sc-> sc_in;
	out = sc->sc_state;

	out &= ~sc->sc_dir;
	in &= sc->sc_dir;

	printdir(device_xname(sc->sc_dev), in, sc->sc_dir, 'I');
	printdir(device_xname(sc->sc_dev), out, ~sc->sc_dir, 'O');

	callout_init(&sc->sc_timer, CALLOUT_MPSAFE);
	callout_reset(&sc->sc_timer, hz*20, pcagpio_timeout, sc);

#endif

	pins = prop_dictionary_get(dict, "pins");
	if (pins != NULL) {
		int i, num, def;
		char name[32];
		const char *spptr, *nptr;
		bool ok = TRUE, act;

		for (i = 0; i < prop_array_count(pins); i++) {
			nptr = NULL;
			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);
			if (!strncmp(nptr, "LED ", 4))
				pcagpio_attach_led(sc, name, num, act, def);
		}
	}
}

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

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

#ifdef PCAGPIO_DEBUG
	callout_halt(&sc->sc_timer, NULL);
	callout_destroy(&sc->sc_timer);
#endif

	return 0;
}

#ifdef PCAGPIO_DEBUG
static void
pcagpio_timeout(void *v)
{
	struct pcagpio_softc *sc = v;
	uint32_t out, dir, in, o_out, o_in;

	out = pcagpio_readreg(sc, PCAGPIO_OUTPUT);
	dir = pcagpio_readreg(sc, PCAGPIO_CONFIG);
	in = pcagpio_readreg(sc, PCAGPIO_INPUT);
	if (out != sc->sc_state || dir != sc->sc_dir || in != sc->sc_in) {
		aprint_normal_dev(sc->sc_dev, "status change\n");
		o_out = sc->sc_state;
		o_in = sc->sc_in;
		o_out &= ~sc->sc_dir;
		o_in &= sc->sc_dir;
		printdir(device_xname(sc->sc_dev), o_in, sc->sc_dir, 'I');
		printdir(device_xname(sc->sc_dev), o_out, ~sc->sc_dir, 'O');
		sc->sc_state = out;
		sc->sc_dir = dir;
		sc->sc_in = in;
		out &= ~sc->sc_dir;
		in &= sc->sc_dir;
		printdir(device_xname(sc->sc_dev), in, sc->sc_dir, 'I');
		printdir(device_xname(sc->sc_dev), out, ~sc->sc_dir, 'O');
	}
	callout_reset(&sc->sc_timer, hz*60, pcagpio_timeout, sc);
}
#endif

static void
pcagpio_writereg(struct pcagpio_softc *sc, int reg, uint32_t val)
{
	uint8_t cmd;

	iic_acquire_bus(sc->sc_i2c, 0);
	if (sc->sc_is_16bit) {
		uint16_t creg;
		cmd = reg << 1;
		creg = htole16(val);
		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
	} else {
		uint8_t creg;
		cmd = reg;
		creg = (uint8_t)val;
		iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
	}
	if (reg == PCAGPIO_OUTPUT) sc->sc_state = val;
	iic_release_bus(sc->sc_i2c, 0);
}		

static uint32_t pcagpio_readreg(struct pcagpio_softc *sc, int reg)
{
	uint8_t cmd;
	uint32_t ret;

	iic_acquire_bus(sc->sc_i2c, 0);
	if (sc->sc_is_16bit) {
		uint16_t creg;
		cmd = reg << 1;
		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
		    sc->sc_addr, &cmd, 1, &creg, 2, 0);
		ret = le16toh(creg);
	} else {
		uint8_t creg;
		cmd = reg;
		iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP,
		    sc->sc_addr, &cmd, 1, &creg, 1, 0);
		ret = creg;
	}
	iic_release_bus(sc->sc_i2c, 0);
	return ret;
}

static void
pcagpio_attach_led(struct pcagpio_softc *sc, char *n, int pin, int act, int def)
{
	struct pcagpio_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;
	l->led = led_attach(n, l, pcagpio_get, pcagpio_set);
	if (def != -1) pcagpio_set(l, def);
	DPRINTF("%s: %04x %04x %04x def %d\n",
	    __func__, l->mask, l->v_on, l->v_off, def);
	sc->sc_nleds++;
}

static int
pcagpio_get(void *cookie)
{
	struct pcagpio_led *l = cookie;
	struct pcagpio_softc *sc = l->cookie;

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

static void
pcagpio_set(void *cookie, int val)
{
	struct pcagpio_led *l = cookie;
	struct pcagpio_softc *sc = l->cookie;
	uint32_t newstate;	

	newstate = sc->sc_state & ~l->mask;
	newstate |= val ? l->v_on : l->v_off;
	DPRINTF("%s: %04x -> %04x, %04x %04x %04x\n", __func__,
	    sc->sc_state, newstate, l->mask, l->v_on, l->v_off);
	if (newstate != sc->sc_state)
		pcagpio_writereg(sc, PCAGPIO_OUTPUT, newstate);
}