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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/* $NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $ */
/*-
* Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
* 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.
*/
#include "opt_fdt.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kmem.h>
#include <sys/bitops.h>
#include <dev/i2c/i2cvar.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wskbdvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/wscons/linux_keymap.h>
#ifdef FDT
#include <dev/fdt/fdtvar.h>
#endif
#define TCA_MAX_ROWS 8
#define TCA_MAX_COLS 10
#define TCA_CFG 0x01
#define CFG_AI __BIT(7)
#define CFG_GPI_E_CFG __BIT(6)
#define CFG_OVR_FLOW_M __BIT(5)
#define CFG_INT_CFG __BIT(4)
#define CFG_OVR_FLOW_IEN __BIT(3)
#define CFG_K_LCK_IEN __BIT(2)
#define CFG_GPI_IEN __BIT(1)
#define CFG_KE_IEN __BIT(0)
#define TCA_INT_STAT 0x02
#define INT_STAT_CAD_INT __BIT(4)
#define INT_STAT_OVR_FLOW_INT __BIT(3)
#define INT_STAT_K_LCD_INT __BIT(2)
#define INT_STAT_GPI_INT __BIT(1)
#define INT_STAT_K_INT __BIT(0)
#define TCA_KEY_LCK_EC 0x03
#define KEY_LCK_EC_K_LCK_EN __BIT(6)
#define KEY_LCK_EC_LCK2 __BIT(5)
#define KEY_LCK_EC_LCK1 __BIT(4)
#define KEY_LCK_EC_KEC __BITS(3,0)
#define TCA_EVENT(c) (0x04 + (c) - 'A')
#define TCA_EVENT_STATE __BIT(7)
#define TCA_EVENT_CODE __BITS(6,0)
#define TCA_KP_GPIO1 0x1d
#define TCA_KP_GPIO2 0x1e
#define TCA_KP_GPIO3 0x1f
#define TCA_DEBOUNCE_DIS1 0x29
#define TCA_DEBOUNCE_DIS2 0x2a
#define TCA_DEBOUNCE_DIS3 0x2b
struct tcakp_softc {
device_t sc_dev;
i2c_tag_t sc_i2c;
i2c_addr_t sc_addr;
int sc_phandle;
u_int sc_rows;
u_int sc_cols;
bool sc_autorepeat;
u_int sc_row_shift;
uint16_t sc_keymap[128];
void *sc_ih;
void *sc_sih;
int sc_enabled;
device_t sc_wskbddev;
};
static int tcakp_match(device_t, cfdata_t, void *);
static void tcakp_attach(device_t, device_t, void *);
static int tcakp_i2c_lock(struct tcakp_softc *);
static void tcakp_i2c_unlock(struct tcakp_softc *);
static int tcakp_read(struct tcakp_softc *, uint8_t, uint8_t *);
static int tcakp_write(struct tcakp_softc *, uint8_t, uint8_t);
CFATTACH_DECL_NEW(tcakp, sizeof(struct tcakp_softc),
tcakp_match, tcakp_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
{ .compat = "ti,tca8418" },
DEVICE_COMPAT_EOL
};
static u_int
tcakp_decode(struct tcakp_softc *sc, uint8_t code)
{
u_int row = code / TCA_MAX_COLS;
u_int col = code % TCA_MAX_COLS;
if (col == 0) {
row = row - 1;
col = TCA_MAX_COLS - 1;
} else {
col = col - 1;
}
return (row << sc->sc_row_shift) + col;
}
static int
tcakp_intr(void *priv)
{
struct tcakp_softc * const sc = priv;
/*
* Schedule our soft interrupt handler. We can't access the i2c
* from hard interrupt context, so just go ahead and claim the
* interrupt.
*
* XXX If we ever end up with an instance that uses
* level-sensitive interrupts, we will need to mask
* the interrupt source.
*/
softint_schedule(sc->sc_sih);
return 1;
}
static void
tcakp_softintr(void *priv)
{
struct tcakp_softc * const sc = priv;
uint8_t stat, ev;
if (tcakp_i2c_lock(sc) != 0)
return;
tcakp_read(sc, TCA_INT_STAT, &stat);
if (stat & INT_STAT_K_INT) {
tcakp_read(sc, TCA_EVENT('A'), &ev);
while (ev != 0) {
const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
tcakp_i2c_unlock(sc);
/* Translate raw code to keymap index */
const u_int index = tcakp_decode(sc, code);
u_int type = pressed ? WSCONS_EVENT_KEY_DOWN :
WSCONS_EVENT_KEY_UP;
int key = linux_key_to_usb(sc->sc_keymap[index]);
if (sc->sc_wskbddev)
wskbd_input(sc->sc_wskbddev, type, key);
if (tcakp_i2c_lock(sc) != 0)
return;
tcakp_read(sc, TCA_EVENT('A'), &ev);
}
}
tcakp_write(sc, TCA_INT_STAT, stat);
tcakp_i2c_unlock(sc);
}
static int
tcakp_init(struct tcakp_softc *sc)
{
uint32_t mask;
uint8_t val;
int error;
if (sc->sc_rows == 0 || sc->sc_cols == 0) {
aprint_error_dev(sc->sc_dev, "not configured\n");
return ENXIO;
}
mask = __BITS(sc->sc_rows - 1, 0);
mask += __BITS(sc->sc_cols - 1, 0) << 8;
error = tcakp_i2c_lock(sc);
if (error)
return error;
/* Lock the keyboard */
tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
/* Select keyboard mode */
tcakp_write(sc, TCA_KP_GPIO1, (mask >> 0) & 0xff);
tcakp_write(sc, TCA_KP_GPIO2, (mask >> 8) & 0xff);
tcakp_write(sc, TCA_KP_GPIO3, (mask >> 16) & 0xff);
/* Disable debounce */
tcakp_write(sc, TCA_DEBOUNCE_DIS1, (mask >> 0) & 0xff);
tcakp_write(sc, TCA_DEBOUNCE_DIS2, (mask >> 8) & 0xff);
tcakp_write(sc, TCA_DEBOUNCE_DIS3, (mask >> 16) & 0xff);
/* Enable key event interrupts */
tcakp_write(sc, TCA_CFG, CFG_INT_CFG | CFG_KE_IEN);
/* Clear interrupts */
tcakp_read(sc, TCA_INT_STAT, &val);
tcakp_write(sc, TCA_INT_STAT, val);
tcakp_i2c_unlock(sc);
return 0;
}
static void
tcakp_configure_fdt(struct tcakp_softc *sc)
{
const uint32_t *keymap;
int len;
of_getprop_uint32(sc->sc_phandle, "keypad,num-rows", &sc->sc_rows);
of_getprop_uint32(sc->sc_phandle, "keypad,num-columns", &sc->sc_cols);
sc->sc_autorepeat = of_getprop_bool(sc->sc_phandle, "keypad,autorepeat");
keymap = fdtbus_get_prop(sc->sc_phandle, "linux,keymap", &len);
if (keymap == NULL || len <= 0)
return;
sc->sc_row_shift = fls32(sc->sc_cols) - 1;
if (sc->sc_row_shift & (sc->sc_cols - 1))
sc->sc_row_shift++;
while (len >= 4) {
const uint32_t e = be32toh(*keymap);
const u_int row = (e >> 24) & 0xff;
const u_int col = (e >> 16) & 0xff;
const u_int index = (row << sc->sc_row_shift) + col;
sc->sc_keymap[index] = e & 0xffff;
len -= 4;
keymap++;
}
}
static int
tcakp_enable(void *v, int on)
{
struct tcakp_softc * const sc = v;
int error;
error = tcakp_i2c_lock(sc);
if (error)
return error;
if (on) {
/* Disable key lock */
tcakp_write(sc, TCA_KEY_LCK_EC, 0);
} else {
/* Enable key lock */
tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
}
tcakp_i2c_unlock(sc);
return 0;
}
static void
tcakp_set_leds(void *v, int leds)
{
}
static int
tcakp_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
{
switch (cmd) {
case WSKBDIO_GTYPE:
*(int *)data = WSKBD_TYPE_USB;
return 0;
}
return EPASSTHROUGH;
}
static const struct wskbd_accessops tcakp_accessops = {
tcakp_enable,
tcakp_set_leds,
tcakp_ioctl,
};
#if notyet
static void
tcakp_cngetc(void *v, u_int *type, int *data)
{
struct tcakp_softc * const sc = v;
uint8_t ev = 0;
/* XXX i2c bus acquire */
do {
tcakp_read(sc, TCA_EVENT('A'), &ev);
} while (ev == 0);
const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
const u_int index = tcakp_decode(sc, code);
*type = pressed ? WSCONS_EVENT_KEY_DOWN :
WSCONS_EVENT_KEY_UP;
*data = sc->sc_keymap[index];
/* XXX i2c bus release */
}
static void
tcakp_cnpollc(void *v, int on)
{
}
static void
tcakp_cnbell(void *v, u_int pitch, u_int period, u_int volume)
{
}
static const struct wskbd_consops tcakp_consops = {
tcakp_cngetc,
tcakp_cnpollc,
tcakp_cnbell,
};
#endif
extern const struct wscons_keydesc hidkbd_keydesctab[];
static const struct wskbd_mapdata tcakp_keymapdata = {
hidkbd_keydesctab,
KB_US,
};
static int
tcakp_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;
if (ia->ia_addr == 0x34)
return I2C_MATCH_ADDRESS_ONLY;
return 0;
}
static void
tcakp_attach(device_t parent, device_t self, void *aux)
{
struct tcakp_softc * const sc = device_private(self);
struct i2c_attach_args *ia = aux;
struct wskbddev_attach_args a;
sc->sc_dev = self;
sc->sc_i2c = ia->ia_tag;
sc->sc_addr = ia->ia_addr;
sc->sc_phandle = ia->ia_cookie;
aprint_naive("\n");
aprint_normal(": TCA8418\n");
#ifdef FDT
sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, 0,
tcakp_intr, sc);
/*
* XXX This is an edge-sensitive interrupt, but we'd like to
* be able to check at run-time just to be sure.
*/
if (sc->sc_ih == NULL) {
aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
return;
}
sc->sc_sih = softint_establish(SOFTINT_SERIAL, tcakp_softintr, sc);
if (sc->sc_sih == NULL) {
aprint_error_dev(sc->sc_dev,
"unable to establish soft interrupt\n");
return;
}
tcakp_configure_fdt(sc);
#endif
if (tcakp_init(sc) != 0)
return;
memset(&a, 0, sizeof(a));
a.console = false; /* XXX */
a.keymap = &tcakp_keymapdata;
a.accessops = &tcakp_accessops;
a.accesscookie = sc;
sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
}
static int
tcakp_i2c_lock(struct tcakp_softc *sc)
{
int error;
error = iic_acquire_bus(sc->sc_i2c, 0);
if (error) {
aprint_error_dev(sc->sc_dev,
"unable to acquire bus lock (%d)\n", error);
}
return error;
}
static void
tcakp_i2c_unlock(struct tcakp_softc *sc)
{
iic_release_bus(sc->sc_i2c, 0);
}
static int
tcakp_read(struct tcakp_softc *sc, uint8_t reg, uint8_t *val)
{
return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
®, 1, val, 1, 0);
}
static int
tcakp_write(struct tcakp_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t buf[2] = { reg, val };
return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
sc->sc_addr, NULL, 0, buf, 2, 0);
}
|