tcakp.c revision 1.1 1 /* $NetBSD: tcakp.c,v 1.1 2017/08/26 21:02:35 jmcneill 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 "opt_fdt.h"
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: tcakp.c,v 1.1 2017/08/26 21:02:35 jmcneill 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
50 /* XXX keymap */
51 #include <dev/pckbport/wskbdmap_mfii.c>
52
53 #ifdef FDT
54 #include <dev/fdt/fdtvar.h>
55 #endif
56
57 #define TCA_MAX_ROWS 8
58 #define TCA_MAX_COLS 10
59
60 #define TCA_CFG 0x01
61 #define CFG_AI __BIT(7)
62 #define CFG_GPI_E_CFG __BIT(6)
63 #define CFG_OVR_FLOW_M __BIT(5)
64 #define CFG_INT_CFG __BIT(4)
65 #define CFG_OVR_FLOW_IEN __BIT(3)
66 #define CFG_K_LCK_IEN __BIT(2)
67 #define CFG_GPI_IEN __BIT(1)
68 #define CFG_KE_IEN __BIT(0)
69 #define TCA_INT_STAT 0x02
70 #define INT_STAT_CAD_INT __BIT(4)
71 #define INT_STAT_OVR_FLOW_INT __BIT(3)
72 #define INT_STAT_K_LCD_INT __BIT(2)
73 #define INT_STAT_GPI_INT __BIT(1)
74 #define INT_STAT_K_INT __BIT(0)
75 #define TCA_KEY_LCK_EC 0x03
76 #define KEY_LCK_EC_K_LCK_EN __BIT(6)
77 #define KEY_LCK_EC_LCK2 __BIT(5)
78 #define KEY_LCK_EC_LCK1 __BIT(4)
79 #define KEY_LCK_EC_KEC __BITS(3,0)
80 #define TCA_EVENT(c) (0x04 + (c) - 'A')
81 #define TCA_EVENT_STATE __BIT(7)
82 #define TCA_EVENT_CODE __BITS(6,0)
83 #define TCA_KP_GPIO1 0x1d
84 #define TCA_KP_GPIO2 0x1e
85 #define TCA_KP_GPIO3 0x1f
86 #define TCA_DEBOUNCE_DIS1 0x29
87 #define TCA_DEBOUNCE_DIS2 0x2a
88 #define TCA_DEBOUNCE_DIS3 0x2b
89
90 #ifdef FDT
91 #define KC(n) KS_KEYCODE(n)
92 static const keysym_t tcakp_linux_event_keydesc[] = {
93 KC(1),
94 };
95 #undef KC
96 #endif
97
98 struct tcakp_softc {
99 device_t sc_dev;
100 i2c_tag_t sc_i2c;
101 i2c_addr_t sc_addr;
102 int sc_phandle;
103
104 u_int sc_rows;
105 u_int sc_cols;
106 bool sc_autorepeat;
107 u_int sc_row_shift;
108
109 uint16_t sc_keymap[128];
110
111 void *sc_ih;
112
113 int sc_enabled;
114 device_t sc_wskbddev;
115 };
116
117 static int tcakp_match(device_t, cfdata_t, void *);
118 static void tcakp_attach(device_t, device_t, void *);
119
120 static int tcakp_read(struct tcakp_softc *, uint8_t, uint8_t *);
121 static int tcakp_write(struct tcakp_softc *, uint8_t, uint8_t);
122
123 CFATTACH_DECL_NEW(tcakp, sizeof(struct tcakp_softc),
124 tcakp_match, tcakp_attach, NULL, NULL);
125
126 static const char * tcakp_compats[] = {
127 "ti,tca8418",
128 NULL
129 };
130
131 static u_int
132 tcakp_decode(struct tcakp_softc *sc, uint8_t code)
133 {
134 u_int row = code / TCA_MAX_COLS;
135 u_int col = code % TCA_MAX_COLS;
136 if (col == 0) {
137 row = row - 1;
138 col = TCA_MAX_COLS - 1;
139 } else {
140 col = col - 1;
141 }
142
143 return (row << sc->sc_row_shift) + col;
144 }
145
146 static int
147 tcakp_intr(void *priv)
148 {
149 struct tcakp_softc * const sc = priv;
150 uint8_t stat, ev;
151 int ret = 0;
152
153 tcakp_read(sc, TCA_INT_STAT, &stat);
154 if (stat & INT_STAT_K_INT) {
155 tcakp_read(sc, TCA_EVENT('A'), &ev);
156 while (ev != 0) {
157 const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
158 const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
159
160 /* Translate raw code to keymap index */
161 const u_int index = tcakp_decode(sc, code);
162
163 u_int type = pressed ? WSCONS_EVENT_KEY_DOWN :
164 WSCONS_EVENT_KEY_UP;
165 int key = sc->sc_keymap[index];
166
167 if (sc->sc_wskbddev)
168 wskbd_input(sc->sc_wskbddev, type, key);
169
170 tcakp_read(sc, TCA_EVENT('A'), &ev);
171 }
172 ret = 1;
173 }
174 tcakp_write(sc, TCA_INT_STAT, stat);
175
176 return ret;
177 }
178
179 static int
180 tcakp_init(struct tcakp_softc *sc)
181 {
182 uint32_t mask;
183 uint8_t val;
184
185 if (sc->sc_rows == 0 || sc->sc_cols == 0) {
186 aprint_error_dev(sc->sc_dev, "not configured\n");
187 return ENXIO;
188 }
189
190 mask = __BITS(sc->sc_rows - 1, 0);
191 mask += __BITS(sc->sc_cols - 1, 0) << 8;
192
193 /* Lock the keyboard */
194 tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
195 /* Select keyboard mode */
196 tcakp_write(sc, TCA_KP_GPIO1, (mask >> 0) & 0xff);
197 tcakp_write(sc, TCA_KP_GPIO2, (mask >> 8) & 0xff);
198 tcakp_write(sc, TCA_KP_GPIO3, (mask >> 16) & 0xff);
199 /* Disable debounce */
200 tcakp_write(sc, TCA_DEBOUNCE_DIS1, (mask >> 0) & 0xff);
201 tcakp_write(sc, TCA_DEBOUNCE_DIS2, (mask >> 8) & 0xff);
202 tcakp_write(sc, TCA_DEBOUNCE_DIS3, (mask >> 16) & 0xff);
203 /* Enable key event interrupts */
204 tcakp_write(sc, TCA_CFG, CFG_INT_CFG | CFG_KE_IEN);
205 /* Clear interrupts */
206 tcakp_read(sc, TCA_INT_STAT, &val);
207 tcakp_write(sc, TCA_INT_STAT, val);
208
209 return 0;
210 }
211
212 static void
213 tcakp_configure_fdt(struct tcakp_softc *sc)
214 {
215 const uint32_t *keymap;
216 int len;
217
218 of_getprop_uint32(sc->sc_phandle, "keypad,num-rows", &sc->sc_rows);
219 of_getprop_uint32(sc->sc_phandle, "keypad,num-columns", &sc->sc_cols);
220 sc->sc_autorepeat = of_getprop_bool(sc->sc_phandle, "keypad,autorepeat");
221
222 keymap = fdtbus_get_prop(sc->sc_phandle, "linux,keymap", &len);
223 if (keymap == NULL || len <= 0)
224 return;
225
226 sc->sc_row_shift = fls32(sc->sc_cols) - 1;
227 if (sc->sc_row_shift & (sc->sc_cols - 1))
228 sc->sc_row_shift++;
229
230 while (len >= 4) {
231 const uint32_t e = be32toh(*keymap);
232 const u_int row = (e >> 24) & 0xff;
233 const u_int col = (e >> 16) & 0xff;
234 const u_int index = (row << sc->sc_row_shift) + col;
235
236 sc->sc_keymap[index] = e & 0xffff;
237
238 len -= 4;
239 keymap++;
240 }
241 }
242
243 static int
244 tcakp_enable(void *v, int on)
245 {
246 struct tcakp_softc * const sc = v;
247
248 if (on) {
249 /* Disable key lock */
250 tcakp_write(sc, TCA_KEY_LCK_EC, 0);
251 } else {
252 /* Enable key lock */
253 tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
254 }
255
256 return 0;
257 }
258
259 static void
260 tcakp_set_leds(void *v, int leds)
261 {
262 }
263
264 static int
265 tcakp_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
266 {
267 switch (cmd) {
268 case WSKBDIO_GTYPE:
269 *(int *)data = WSKBD_TYPE_TCAKP;
270 return 0;
271 }
272
273 return EPASSTHROUGH;
274 }
275
276 static const struct wskbd_accessops tcakp_accessops = {
277 tcakp_enable,
278 tcakp_set_leds,
279 tcakp_ioctl,
280 };
281
282 #if notyet
283 static void
284 tcakp_cngetc(void *v, u_int *type, int *data)
285 {
286 struct tcakp_softc * const sc = v;
287 uint8_t ev = 0;
288
289 do {
290 tcakp_read(sc, TCA_EVENT('A'), &ev);
291 } while (ev == 0);
292
293 const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
294 const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
295 const u_int index = tcakp_decode(sc, code);
296
297 *type = pressed ? WSCONS_EVENT_KEY_DOWN :
298 WSCONS_EVENT_KEY_UP;
299 *data = sc->sc_keymap[index];
300 }
301
302 static void
303 tcakp_cnpollc(void *v, int on)
304 {
305 }
306
307 static void
308 tcakp_cnbell(void *v, u_int pitch, u_int period, u_int volume)
309 {
310 }
311
312 static const struct wskbd_consops tcakp_consops = {
313 tcakp_cngetc,
314 tcakp_cnpollc,
315 tcakp_cnbell,
316 };
317 #endif
318
319 static const struct wskbd_mapdata tcakp_keymapdata = {
320 pckbd_keydesctab,
321 KB_US,
322 };
323
324 static int
325 tcakp_match(device_t parent, cfdata_t match, void *aux)
326 {
327 struct i2c_attach_args *ia = aux;
328
329 if (ia->ia_name == NULL)
330 return 1;
331 else
332 return iic_compat_match(ia, tcakp_compats);
333 }
334
335 static void
336 tcakp_attach(device_t parent, device_t self, void *aux)
337 {
338 struct tcakp_softc * const sc = device_private(self);
339 struct i2c_attach_args *ia = aux;
340 struct wskbddev_attach_args a;
341
342 sc->sc_dev = self;
343 sc->sc_i2c = ia->ia_tag;
344 sc->sc_addr = ia->ia_addr;
345 sc->sc_phandle = ia->ia_cookie;
346
347 aprint_naive("\n");
348 aprint_normal(": TCA8418\n");
349
350 #ifdef FDT
351 sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM,
352 FDT_INTR_MPSAFE, tcakp_intr, sc);
353
354 tcakp_configure_fdt(sc);
355 #endif
356
357 if (tcakp_init(sc) != 0)
358 return;
359
360 memset(&a, 0, sizeof(a));
361 a.console = false; /* XXX */
362 a.keymap = &tcakp_keymapdata;
363 a.accessops = &tcakp_accessops;
364 a.accesscookie = sc;
365
366 sc->sc_wskbddev = config_found_ia(self, "wskbddev", &a, wskbddevprint);
367 }
368
369 static int
370 tcakp_read(struct tcakp_softc *sc, uint8_t reg, uint8_t *val)
371 {
372 return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
373 ®, 1, val, 1, I2C_F_POLL);
374 }
375
376 static int
377 tcakp_write(struct tcakp_softc *sc, uint8_t reg, uint8_t val)
378 {
379 uint8_t buf[2] = { reg, val };
380 return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr,
381 NULL, 0, buf, 2, I2C_F_POLL);
382 }
383