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