tcagpio.c revision 1.6 1 /* $NetBSD: tcagpio.c,v 1.6 2021/01/17 21:42:35 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tcagpio.c,v 1.6 2021/01/17 21:42:35 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/conf.h>
37 #include <sys/bus.h>
38 #include <sys/kmem.h>
39 #include <sys/gpio.h>
40
41 #include <dev/i2c/i2cvar.h>
42
43 #include <dev/fdt/fdtvar.h>
44
45 #define PORT_BANK(pin) (pin / 8)
46 #define PORT_BIT(pin) __BIT(pin % 8)
47
48 #define PORT_IN(pin) (0x00 + PORT_BANK(pin))
49 #define PORT_OUT(pin) (0x02 + PORT_BANK(pin))
50 #define PORT_POLARITY(pin) (0x04 + PORT_BANK(pin))
51 #define PORT_CFG(pin) (0x06 + PORT_BANK(pin))
52
53 #define TCA_PIN_COUNT 16
54
55 struct tcagpio_softc {
56 device_t sc_dev;
57 i2c_tag_t sc_i2c;
58 i2c_addr_t sc_addr;
59 int sc_phandle;
60
61 int sc_npins;
62 };
63
64 struct tcagpio_pin {
65 struct tcagpio_softc *pin_sc;
66 int pin_num;
67 int pin_flags;
68 bool pin_actlo;
69 };
70
71 static const struct device_compatible_entry compat_data[] = {
72 { .compat = "ti,tca9539" },
73
74 { 0 }
75 };
76
77 static uint8_t
78 tcagpio_read(struct tcagpio_softc *sc, uint8_t reg, int flags)
79 {
80 uint8_t val = 0;
81 int error;
82
83 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
84 ®, 1, &val, 1, flags);
85 if (error != 0)
86 aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error);
87
88 return val;
89 }
90
91 static void
92 tcagpio_write(struct tcagpio_softc *sc, uint8_t reg, uint8_t val, int flags)
93 {
94 uint8_t buf[2];
95 int error;
96
97 buf[0] = reg;
98 buf[1] = val;
99
100 error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
101 NULL, 0, buf, 2, flags);
102 if (error != 0)
103 aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error);
104 }
105
106 #define I2C_READ(sc, reg) tcagpio_read((sc), (reg), 0)
107 #define I2C_WRITE(sc, reg, val) tcagpio_write((sc), (reg), (val), 0)
108 #define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, 0)
109 #define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, 0)
110
111 static int
112 tcagpio_gpio_config(struct tcagpio_softc *sc, int pin, int flags)
113 {
114 uint16_t gpio;
115
116 KASSERT(pin >= 0 && pin < sc->sc_npins);
117
118 gpio = I2C_READ(sc, PORT_CFG(pin));
119
120 switch (flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) {
121 case GPIO_PIN_INPUT:
122 gpio |= PORT_BIT(pin);
123 break;
124 case GPIO_PIN_OUTPUT:
125 gpio &= ~PORT_BIT(pin);
126 break;
127 default:
128 return EINVAL;
129 }
130
131 I2C_WRITE(sc, PORT_CFG(pin), gpio);
132
133 return 0;
134 }
135
136 static void *
137 tcagpio_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
138 {
139 struct tcagpio_softc * const sc = device_private(dev);
140 struct tcagpio_pin *gpin;
141 const u_int *gpio = data;
142 int error;
143
144 if (len != 12)
145 return NULL;
146
147 const uint8_t pin = be32toh(gpio[1]) & 0xff;
148 const bool actlo = (be32toh(gpio[2]) & __BIT(0)) != 0;
149
150 if (pin >= sc->sc_npins)
151 return NULL;
152
153 I2C_LOCK(sc);
154 error = tcagpio_gpio_config(sc, pin, flags);
155 I2C_UNLOCK(sc);
156
157 if (error != 0) {
158 device_printf(dev, "bad pin %d config %#x\n", pin, flags);
159 return NULL;
160 }
161
162 gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
163 gpin->pin_sc = sc;
164 gpin->pin_num = pin;
165 gpin->pin_flags = flags;
166 gpin->pin_actlo = actlo;
167
168 return gpin;
169 }
170
171 static void
172 tcagpio_gpio_release(device_t dev, void *priv)
173 {
174 struct tcagpio_softc * const sc = device_private(dev);
175 struct tcagpio_pin *gpin = priv;
176
177 I2C_LOCK(sc);
178 tcagpio_gpio_config(sc, gpin->pin_num, GPIO_PIN_INPUT);
179 I2C_UNLOCK(sc);
180
181 kmem_free(gpin, sizeof(*gpin));
182 }
183
184 static int
185 tcagpio_gpio_read(device_t dev, void *priv, bool raw)
186 {
187 struct tcagpio_softc * const sc = device_private(dev);
188 struct tcagpio_pin *gpin = priv;
189 uint8_t gpio;
190 int val;
191
192 I2C_LOCK(sc);
193 gpio = I2C_READ(sc, PORT_IN(gpin->pin_num));
194 I2C_UNLOCK(sc);
195
196 val = __SHIFTOUT(gpio, PORT_BIT(gpin->pin_num));
197 if (!raw && gpin->pin_actlo)
198 val = !val;
199
200 #ifdef TCAGPIO_DEBUG
201 device_printf(sc->sc_dev, "GPIO%d rd %02x (%d %d)\n",
202 gpin->pin_num, gpio,
203 (int)__SHIFTOUT(gpio, PORT_BIT(gpin->pin_num)), val);
204 #endif
205
206 return val;
207 }
208
209 static void
210 tcagpio_gpio_write(device_t dev, void *priv, int val, bool raw)
211 {
212 struct tcagpio_softc * const sc = device_private(dev);
213 struct tcagpio_pin *gpin = priv;
214 uint8_t gpio;
215
216 if (!raw && gpin->pin_actlo)
217 val = !val;
218
219 I2C_LOCK(sc);
220 gpio = I2C_READ(sc, PORT_OUT(gpin->pin_num));
221 gpio &= ~PORT_BIT(gpin->pin_num);
222 gpio |= __SHIFTIN(val, PORT_BIT(gpin->pin_num));
223 #ifdef TCAGPIO_DEBUG
224 device_printf(sc->sc_dev, "GPIO%d wr %02x -> %02x\n",
225 gpin->pin_num,
226 I2C_READ(sc, PORT_OUT(gpin->pin_num)), gpio);
227 #endif
228 I2C_WRITE(sc, PORT_OUT(gpin->pin_num), gpio);
229 I2C_UNLOCK(sc);
230 }
231
232 static struct fdtbus_gpio_controller_func tcagpio_gpio_funcs = {
233 .acquire = tcagpio_gpio_acquire,
234 .release = tcagpio_gpio_release,
235 .read = tcagpio_gpio_read,
236 .write = tcagpio_gpio_write,
237 };
238
239 static void
240 tcagpio_gpio_attach(struct tcagpio_softc *sc)
241 {
242 fdtbus_register_gpio_controller(sc->sc_dev, sc->sc_phandle,
243 &tcagpio_gpio_funcs);
244 }
245
246 static int
247 tcagpio_match(device_t parent, cfdata_t match, void *aux)
248 {
249 struct i2c_attach_args *ia = aux;
250 int match_result;
251
252 if (iic_use_direct_match(ia, match, compat_data, &match_result))
253 return match_result;
254
255 return 0;
256 }
257
258 static void
259 tcagpio_attach(device_t parent, device_t self, void *aux)
260 {
261 struct tcagpio_softc * const sc = device_private(self);
262 struct i2c_attach_args *ia = aux;
263
264 sc->sc_dev = self;
265 sc->sc_i2c = ia->ia_tag;
266 sc->sc_addr = ia->ia_addr;
267 sc->sc_phandle = ia->ia_cookie;
268 sc->sc_npins = TCA_PIN_COUNT;
269
270 aprint_naive("\n");
271 aprint_normal(": I2C/SMBus I/O Expander\n");
272
273 tcagpio_gpio_attach(sc);
274 }
275
276 CFATTACH_DECL_NEW(tcagpio, sizeof(struct tcagpio_softc),
277 tcagpio_match, tcagpio_attach, NULL, NULL);
278