1 /* $NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill@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 "opt_fdt.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/conf.h>
39 #include <sys/bus.h>
40 #include <sys/kmem.h>
41 #include <sys/bitops.h>
42
43 #include <dev/i2c/i2cvar.h>
44
45 #include <dev/wscons/wsconsio.h>
46 #include <dev/wscons/wskbdvar.h>
47 #include <dev/wscons/wsksymdef.h>
48 #include <dev/wscons/wsksymvar.h>
49 #include <dev/wscons/linux_keymap.h>
50
51 #ifdef FDT
52 #include <dev/fdt/fdtvar.h>
53 #endif
54
55 #define TCA_MAX_ROWS 8
56 #define TCA_MAX_COLS 10
57
58 #define TCA_CFG 0x01
59 #define CFG_AI __BIT(7)
60 #define CFG_GPI_E_CFG __BIT(6)
61 #define CFG_OVR_FLOW_M __BIT(5)
62 #define CFG_INT_CFG __BIT(4)
63 #define CFG_OVR_FLOW_IEN __BIT(3)
64 #define CFG_K_LCK_IEN __BIT(2)
65 #define CFG_GPI_IEN __BIT(1)
66 #define CFG_KE_IEN __BIT(0)
67 #define TCA_INT_STAT 0x02
68 #define INT_STAT_CAD_INT __BIT(4)
69 #define INT_STAT_OVR_FLOW_INT __BIT(3)
70 #define INT_STAT_K_LCD_INT __BIT(2)
71 #define INT_STAT_GPI_INT __BIT(1)
72 #define INT_STAT_K_INT __BIT(0)
73 #define TCA_KEY_LCK_EC 0x03
74 #define KEY_LCK_EC_K_LCK_EN __BIT(6)
75 #define KEY_LCK_EC_LCK2 __BIT(5)
76 #define KEY_LCK_EC_LCK1 __BIT(4)
77 #define KEY_LCK_EC_KEC __BITS(3,0)
78 #define TCA_EVENT(c) (0x04 + (c) - 'A')
79 #define TCA_EVENT_STATE __BIT(7)
80 #define TCA_EVENT_CODE __BITS(6,0)
81 #define TCA_KP_GPIO1 0x1d
82 #define TCA_KP_GPIO2 0x1e
83 #define TCA_KP_GPIO3 0x1f
84 #define TCA_DEBOUNCE_DIS1 0x29
85 #define TCA_DEBOUNCE_DIS2 0x2a
86 #define TCA_DEBOUNCE_DIS3 0x2b
87
88 struct tcakp_softc {
89 device_t sc_dev;
90 i2c_tag_t sc_i2c;
91 i2c_addr_t sc_addr;
92 int sc_phandle;
93
94 u_int sc_rows;
95 u_int sc_cols;
96 bool sc_autorepeat;
97 u_int sc_row_shift;
98
99 uint16_t sc_keymap[128];
100
101 void *sc_ih;
102 void *sc_sih;
103
104 int sc_enabled;
105 device_t sc_wskbddev;
106 };
107
108 static int tcakp_match(device_t, cfdata_t, void *);
109 static void tcakp_attach(device_t, device_t, void *);
110
111 static int tcakp_i2c_lock(struct tcakp_softc *);
112 static void tcakp_i2c_unlock(struct tcakp_softc *);
113
114 static int tcakp_read(struct tcakp_softc *, uint8_t, uint8_t *);
115 static int tcakp_write(struct tcakp_softc *, uint8_t, uint8_t);
116
117 CFATTACH_DECL_NEW(tcakp, sizeof(struct tcakp_softc),
118 tcakp_match, tcakp_attach, NULL, NULL);
119
120 static const struct device_compatible_entry compat_data[] = {
121 { .compat = "ti,tca8418" },
122 DEVICE_COMPAT_EOL
123 };
124
125 static u_int
tcakp_decode(struct tcakp_softc * sc,uint8_t code)126 tcakp_decode(struct tcakp_softc *sc, uint8_t code)
127 {
128 u_int row = code / TCA_MAX_COLS;
129 u_int col = code % TCA_MAX_COLS;
130 if (col == 0) {
131 row = row - 1;
132 col = TCA_MAX_COLS - 1;
133 } else {
134 col = col - 1;
135 }
136
137 return (row << sc->sc_row_shift) + col;
138 }
139
140 static int
tcakp_intr(void * priv)141 tcakp_intr(void *priv)
142 {
143 struct tcakp_softc * const sc = priv;
144
145 /*
146 * Schedule our soft interrupt handler. We can't access the i2c
147 * from hard interrupt context, so just go ahead and claim the
148 * interrupt.
149 *
150 * XXX If we ever end up with an instance that uses
151 * level-sensitive interrupts, we will need to mask
152 * the interrupt source.
153 */
154 softint_schedule(sc->sc_sih);
155 return 1;
156 }
157
158 static void
tcakp_softintr(void * priv)159 tcakp_softintr(void *priv)
160 {
161 struct tcakp_softc * const sc = priv;
162 uint8_t stat, ev;
163
164 if (tcakp_i2c_lock(sc) != 0)
165 return;
166
167 tcakp_read(sc, TCA_INT_STAT, &stat);
168 if (stat & INT_STAT_K_INT) {
169 tcakp_read(sc, TCA_EVENT('A'), &ev);
170 while (ev != 0) {
171 const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
172 const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
173
174 tcakp_i2c_unlock(sc);
175
176 /* Translate raw code to keymap index */
177 const u_int index = tcakp_decode(sc, code);
178
179 u_int type = pressed ? WSCONS_EVENT_KEY_DOWN :
180 WSCONS_EVENT_KEY_UP;
181 int key = linux_key_to_usb(sc->sc_keymap[index]);
182
183 if (sc->sc_wskbddev)
184 wskbd_input(sc->sc_wskbddev, type, key);
185
186 if (tcakp_i2c_lock(sc) != 0)
187 return;
188 tcakp_read(sc, TCA_EVENT('A'), &ev);
189 }
190 }
191 tcakp_write(sc, TCA_INT_STAT, stat);
192 tcakp_i2c_unlock(sc);
193 }
194
195 static int
tcakp_init(struct tcakp_softc * sc)196 tcakp_init(struct tcakp_softc *sc)
197 {
198 uint32_t mask;
199 uint8_t val;
200 int error;
201
202 if (sc->sc_rows == 0 || sc->sc_cols == 0) {
203 aprint_error_dev(sc->sc_dev, "not configured\n");
204 return ENXIO;
205 }
206
207 mask = __BITS(sc->sc_rows - 1, 0);
208 mask += __BITS(sc->sc_cols - 1, 0) << 8;
209
210 error = tcakp_i2c_lock(sc);
211 if (error)
212 return error;
213
214 /* Lock the keyboard */
215 tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
216 /* Select keyboard mode */
217 tcakp_write(sc, TCA_KP_GPIO1, (mask >> 0) & 0xff);
218 tcakp_write(sc, TCA_KP_GPIO2, (mask >> 8) & 0xff);
219 tcakp_write(sc, TCA_KP_GPIO3, (mask >> 16) & 0xff);
220 /* Disable debounce */
221 tcakp_write(sc, TCA_DEBOUNCE_DIS1, (mask >> 0) & 0xff);
222 tcakp_write(sc, TCA_DEBOUNCE_DIS2, (mask >> 8) & 0xff);
223 tcakp_write(sc, TCA_DEBOUNCE_DIS3, (mask >> 16) & 0xff);
224 /* Enable key event interrupts */
225 tcakp_write(sc, TCA_CFG, CFG_INT_CFG | CFG_KE_IEN);
226 /* Clear interrupts */
227 tcakp_read(sc, TCA_INT_STAT, &val);
228 tcakp_write(sc, TCA_INT_STAT, val);
229
230 tcakp_i2c_unlock(sc);
231
232 return 0;
233 }
234
235 static void
tcakp_configure_fdt(struct tcakp_softc * sc)236 tcakp_configure_fdt(struct tcakp_softc *sc)
237 {
238 const uint32_t *keymap;
239 int len;
240
241 of_getprop_uint32(sc->sc_phandle, "keypad,num-rows", &sc->sc_rows);
242 of_getprop_uint32(sc->sc_phandle, "keypad,num-columns", &sc->sc_cols);
243 sc->sc_autorepeat = of_getprop_bool(sc->sc_phandle, "keypad,autorepeat");
244
245 keymap = fdtbus_get_prop(sc->sc_phandle, "linux,keymap", &len);
246 if (keymap == NULL || len <= 0)
247 return;
248
249 sc->sc_row_shift = fls32(sc->sc_cols) - 1;
250 if (sc->sc_row_shift & (sc->sc_cols - 1))
251 sc->sc_row_shift++;
252
253 while (len >= 4) {
254 const uint32_t e = be32toh(*keymap);
255 const u_int row = (e >> 24) & 0xff;
256 const u_int col = (e >> 16) & 0xff;
257 const u_int index = (row << sc->sc_row_shift) + col;
258
259 sc->sc_keymap[index] = e & 0xffff;
260
261 len -= 4;
262 keymap++;
263 }
264 }
265
266 static int
tcakp_enable(void * v,int on)267 tcakp_enable(void *v, int on)
268 {
269 struct tcakp_softc * const sc = v;
270 int error;
271
272 error = tcakp_i2c_lock(sc);
273 if (error)
274 return error;
275
276 if (on) {
277 /* Disable key lock */
278 tcakp_write(sc, TCA_KEY_LCK_EC, 0);
279 } else {
280 /* Enable key lock */
281 tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
282 }
283
284 tcakp_i2c_unlock(sc);
285 return 0;
286 }
287
288 static void
tcakp_set_leds(void * v,int leds)289 tcakp_set_leds(void *v, int leds)
290 {
291 }
292
293 static int
tcakp_ioctl(void * v,u_long cmd,void * data,int flag,lwp_t * l)294 tcakp_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
295 {
296 switch (cmd) {
297 case WSKBDIO_GTYPE:
298 *(int *)data = WSKBD_TYPE_USB;
299 return 0;
300 }
301
302 return EPASSTHROUGH;
303 }
304
305 static const struct wskbd_accessops tcakp_accessops = {
306 tcakp_enable,
307 tcakp_set_leds,
308 tcakp_ioctl,
309 };
310
311 #if notyet
312 static void
tcakp_cngetc(void * v,u_int * type,int * data)313 tcakp_cngetc(void *v, u_int *type, int *data)
314 {
315 struct tcakp_softc * const sc = v;
316 uint8_t ev = 0;
317
318 /* XXX i2c bus acquire */
319
320 do {
321 tcakp_read(sc, TCA_EVENT('A'), &ev);
322 } while (ev == 0);
323
324 const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
325 const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
326 const u_int index = tcakp_decode(sc, code);
327
328 *type = pressed ? WSCONS_EVENT_KEY_DOWN :
329 WSCONS_EVENT_KEY_UP;
330 *data = sc->sc_keymap[index];
331
332 /* XXX i2c bus release */
333 }
334
335 static void
tcakp_cnpollc(void * v,int on)336 tcakp_cnpollc(void *v, int on)
337 {
338 }
339
340 static void
tcakp_cnbell(void * v,u_int pitch,u_int period,u_int volume)341 tcakp_cnbell(void *v, u_int pitch, u_int period, u_int volume)
342 {
343 }
344
345 static const struct wskbd_consops tcakp_consops = {
346 tcakp_cngetc,
347 tcakp_cnpollc,
348 tcakp_cnbell,
349 };
350 #endif
351
352 extern const struct wscons_keydesc hidkbd_keydesctab[];
353 static const struct wskbd_mapdata tcakp_keymapdata = {
354 hidkbd_keydesctab,
355 KB_US,
356 };
357
358 static int
tcakp_match(device_t parent,cfdata_t match,void * aux)359 tcakp_match(device_t parent, cfdata_t match, void *aux)
360 {
361 struct i2c_attach_args *ia = aux;
362 int match_result;
363
364 if (iic_use_direct_match(ia, match, compat_data, &match_result))
365 return match_result;
366
367 if (ia->ia_addr == 0x34)
368 return I2C_MATCH_ADDRESS_ONLY;
369
370 return 0;
371 }
372
373 static void
tcakp_attach(device_t parent,device_t self,void * aux)374 tcakp_attach(device_t parent, device_t self, void *aux)
375 {
376 struct tcakp_softc * const sc = device_private(self);
377 struct i2c_attach_args *ia = aux;
378 struct wskbddev_attach_args a;
379
380 sc->sc_dev = self;
381 sc->sc_i2c = ia->ia_tag;
382 sc->sc_addr = ia->ia_addr;
383 sc->sc_phandle = ia->ia_cookie;
384
385 aprint_naive("\n");
386 aprint_normal(": TCA8418\n");
387
388 #ifdef FDT
389 sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, 0,
390 tcakp_intr, sc);
391 /*
392 * XXX This is an edge-sensitive interrupt, but we'd like to
393 * be able to check at run-time just to be sure.
394 */
395 if (sc->sc_ih == NULL) {
396 aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
397 return;
398 }
399
400 sc->sc_sih = softint_establish(SOFTINT_SERIAL, tcakp_softintr, sc);
401 if (sc->sc_sih == NULL) {
402 aprint_error_dev(sc->sc_dev,
403 "unable to establish soft interrupt\n");
404 return;
405 }
406
407 tcakp_configure_fdt(sc);
408 #endif
409
410 if (tcakp_init(sc) != 0)
411 return;
412
413 memset(&a, 0, sizeof(a));
414 a.console = false; /* XXX */
415 a.keymap = &tcakp_keymapdata;
416 a.accessops = &tcakp_accessops;
417 a.accesscookie = sc;
418
419 sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
420 }
421
422 static int
tcakp_i2c_lock(struct tcakp_softc * sc)423 tcakp_i2c_lock(struct tcakp_softc *sc)
424 {
425 int error;
426
427 error = iic_acquire_bus(sc->sc_i2c, 0);
428 if (error) {
429 aprint_error_dev(sc->sc_dev,
430 "unable to acquire bus lock (%d)\n", error);
431 }
432 return error;
433 }
434
435 static void
tcakp_i2c_unlock(struct tcakp_softc * sc)436 tcakp_i2c_unlock(struct tcakp_softc *sc)
437 {
438 iic_release_bus(sc->sc_i2c, 0);
439 }
440
441 static int
tcakp_read(struct tcakp_softc * sc,uint8_t reg,uint8_t * val)442 tcakp_read(struct tcakp_softc *sc, uint8_t reg, uint8_t *val)
443 {
444 return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
445 ®, 1, val, 1, 0);
446 }
447
448 static int
tcakp_write(struct tcakp_softc * sc,uint8_t reg,uint8_t val)449 tcakp_write(struct tcakp_softc *sc, uint8_t reg, uint8_t val)
450 {
451 uint8_t buf[2] = { reg, val };
452
453 return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
454 sc->sc_addr, NULL, 0, buf, 2, 0);
455 }
456