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
|
/* $OpenBSD: pca9532.c,v 1.2 2006/06/17 23:00:47 drahn Exp $ */
/*
* Copyright (c) 2006 Dale Rahn <drahn@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kernel.h>
#include <sys/fcntl.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/gpio.h>
#include <dev/i2c/i2cvar.h>
#include <dev/gpio/gpiovar.h>
#include "gpio.h"
/* driver for PCA 9532 */
#define PCALED_ADDR 0x60
#define PCALED_GPIO_NPINS 16
struct pcaled_softc {
struct device sc_dev;
i2c_tag_t sc_tag;
int sc_addr;
struct gpio_chipset_tag sc_gpio_gc;
struct gpio_pin sc_gpio_pin[PCALED_GPIO_NPINS];
};
int pcaled_match(struct device *, void *, void *);
void pcaled_attach(struct device *, struct device *, void *);
int pcaled_gpio_pin_read(void *arg, int pin);
void pcaled_gpio_pin_write (void *arg, int pin, int value);
void pcaled_gpio_pin_ctl (void *arg, int pin, int flags);
struct cfattach pcaled_ca = {
sizeof(struct pcaled_softc), pcaled_match, pcaled_attach
};
struct cfdriver pcaled_cd = {
NULL, "pcaled", DV_DULL
};
int
pcaled_match(struct device *parent, void *v, void *arg)
{
struct i2c_attach_args *ia = arg;
int ok = 0;
uint8_t cmd, data;
if (ia->ia_addr != PCALED_ADDR)
return (0);
/* attempt to read input register 0 */
iic_acquire_bus(ia->ia_tag, I2C_F_POLL);
cmd = 0;
if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail;
cmd = 9;
if (iic_exec(ia->ia_tag, I2C_OP_READ_WITH_STOP, ia->ia_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail;
ok = 1;
fail:
iic_release_bus(ia->ia_tag, I2C_F_POLL);
return (ok);
}
void
pcaled_attach(struct device *parent, struct device *self, void *arg)
{
struct pcaled_softc *sc = (void *)self;
struct i2c_attach_args *ia = arg;
struct gpiobus_attach_args gba;
int i;
uint8_t cmd, data;
sc->sc_tag = ia->ia_tag;
sc->sc_addr = ia->ia_addr;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
for (i = 0; i < PCALED_GPIO_NPINS; i++) {
sc->sc_gpio_pin[i].pin_num = i;
sc->sc_gpio_pin[i].pin_caps = GPIO_PIN_INOUT;
if (i < 8)
cmd = 0;
else
cmd = 1;
if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail; /* XXX */
sc->sc_gpio_pin[i].pin_state = (data >> (i & 3)) & 1;
}
sc->sc_gpio_gc.gp_cookie = sc;
sc->sc_gpio_gc.gp_pin_read = pcaled_gpio_pin_read;
sc->sc_gpio_gc.gp_pin_write = pcaled_gpio_pin_write;
sc->sc_gpio_gc.gp_pin_ctl = pcaled_gpio_pin_ctl;
printf(": PCA9532 LED controller\n");
gba.gba_name = "gpio";
gba.gba_gc = &sc->sc_gpio_gc;
gba.gba_pins = sc->sc_gpio_pin;
gba.gba_npins = PCALED_GPIO_NPINS;
#if NGPIO > 0
config_found(&sc->sc_dev, &gba, gpiobus_print);
#endif
fail:
iic_release_bus(sc->sc_tag, I2C_F_POLL);
}
int
pcaled_gpio_pin_read(void *arg, int pin)
{
struct pcaled_softc *sc = arg;
iic_acquire_bus(sc->sc_tag, I2C_F_POLL);
uint8_t cmd, data;
if (pin < 8)
cmd = 0;
else
cmd = 1;
if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail; /* XXX */
fail:
iic_release_bus(sc->sc_tag, I2C_F_POLL);
return (data >> (pin & 3)) & 1;
}
void
pcaled_gpio_pin_write (void *arg, int pin, int value)
{
struct pcaled_softc *sc = arg;
uint8_t cmd, data;
if (pin < 4)
cmd = 6;
else if (pin < 8)
cmd = 7;
else if (pin < 12)
cmd = 8;
else
cmd = 9;
if (iic_exec(sc->sc_tag, I2C_OP_READ_WITH_STOP, sc->sc_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail; /* XXX */
data &= ~(0x3 << (2*(pin & 3)));
data |= (value << (2*(pin & 3)));
if (iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
&cmd, 1, &data, 1, I2C_F_POLL))
goto fail; /* XXX */
fail:
iic_release_bus(sc->sc_tag, I2C_F_POLL);
}
void
pcaled_gpio_pin_ctl (void *arg, int pin, int flags)
{
/* XXX all pins are inout */
}
|