xref: /netbsd/sys/dev/i2c/tcakp.c (revision beecddb6)
1*beecddb6Sthorpej /* $NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $ */
245e54bd0Sjmcneill 
345e54bd0Sjmcneill /*-
445e54bd0Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
545e54bd0Sjmcneill  * All rights reserved.
645e54bd0Sjmcneill  *
745e54bd0Sjmcneill  * Redistribution and use in source and binary forms, with or without
845e54bd0Sjmcneill  * modification, are permitted provided that the following conditions
945e54bd0Sjmcneill  * are met:
1045e54bd0Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1145e54bd0Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1245e54bd0Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1345e54bd0Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1445e54bd0Sjmcneill  *    documentation and/or other materials provided with the distribution.
1545e54bd0Sjmcneill  *
1645e54bd0Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1745e54bd0Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1845e54bd0Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1945e54bd0Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2045e54bd0Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2145e54bd0Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2245e54bd0Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2345e54bd0Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2445e54bd0Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2545e54bd0Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2645e54bd0Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
2745e54bd0Sjmcneill  */
2845e54bd0Sjmcneill 
2945e54bd0Sjmcneill #include "opt_fdt.h"
3045e54bd0Sjmcneill 
3145e54bd0Sjmcneill #include <sys/cdefs.h>
32*beecddb6Sthorpej __KERNEL_RCSID(0, "$NetBSD: tcakp.c,v 1.17 2021/08/07 16:19:11 thorpej Exp $");
3345e54bd0Sjmcneill 
3445e54bd0Sjmcneill #include <sys/param.h>
3545e54bd0Sjmcneill #include <sys/systm.h>
3645e54bd0Sjmcneill #include <sys/kernel.h>
3745e54bd0Sjmcneill #include <sys/device.h>
3845e54bd0Sjmcneill #include <sys/conf.h>
3945e54bd0Sjmcneill #include <sys/bus.h>
4045e54bd0Sjmcneill #include <sys/kmem.h>
4145e54bd0Sjmcneill #include <sys/bitops.h>
4245e54bd0Sjmcneill 
4345e54bd0Sjmcneill #include <dev/i2c/i2cvar.h>
4445e54bd0Sjmcneill 
4545e54bd0Sjmcneill #include <dev/wscons/wsconsio.h>
4645e54bd0Sjmcneill #include <dev/wscons/wskbdvar.h>
4745e54bd0Sjmcneill #include <dev/wscons/wsksymdef.h>
4845e54bd0Sjmcneill #include <dev/wscons/wsksymvar.h>
49e1394683Sjmcneill #include <dev/wscons/linux_keymap.h>
5045e54bd0Sjmcneill 
5145e54bd0Sjmcneill #ifdef FDT
5245e54bd0Sjmcneill #include <dev/fdt/fdtvar.h>
5345e54bd0Sjmcneill #endif
5445e54bd0Sjmcneill 
5545e54bd0Sjmcneill #define	TCA_MAX_ROWS		8
5645e54bd0Sjmcneill #define	TCA_MAX_COLS		10
5745e54bd0Sjmcneill 
5845e54bd0Sjmcneill #define	TCA_CFG			0x01
5945e54bd0Sjmcneill #define	 CFG_AI			__BIT(7)
6045e54bd0Sjmcneill #define	 CFG_GPI_E_CFG		__BIT(6)
6145e54bd0Sjmcneill #define	 CFG_OVR_FLOW_M		__BIT(5)
6245e54bd0Sjmcneill #define	 CFG_INT_CFG		__BIT(4)
6345e54bd0Sjmcneill #define	 CFG_OVR_FLOW_IEN	__BIT(3)
6445e54bd0Sjmcneill #define	 CFG_K_LCK_IEN		__BIT(2)
6545e54bd0Sjmcneill #define	 CFG_GPI_IEN		__BIT(1)
6645e54bd0Sjmcneill #define	 CFG_KE_IEN		__BIT(0)
6745e54bd0Sjmcneill #define	TCA_INT_STAT		0x02
6845e54bd0Sjmcneill #define	 INT_STAT_CAD_INT	__BIT(4)
6945e54bd0Sjmcneill #define	 INT_STAT_OVR_FLOW_INT	__BIT(3)
7045e54bd0Sjmcneill #define	 INT_STAT_K_LCD_INT	__BIT(2)
7145e54bd0Sjmcneill #define	 INT_STAT_GPI_INT	__BIT(1)
7245e54bd0Sjmcneill #define	 INT_STAT_K_INT		__BIT(0)
7345e54bd0Sjmcneill #define	TCA_KEY_LCK_EC		0x03
7445e54bd0Sjmcneill #define	 KEY_LCK_EC_K_LCK_EN	__BIT(6)
7545e54bd0Sjmcneill #define	 KEY_LCK_EC_LCK2	__BIT(5)
7645e54bd0Sjmcneill #define	 KEY_LCK_EC_LCK1	__BIT(4)
7745e54bd0Sjmcneill #define	 KEY_LCK_EC_KEC		__BITS(3,0)
7845e54bd0Sjmcneill #define	TCA_EVENT(c)		(0x04 + (c) - 'A')
7945e54bd0Sjmcneill #define	 TCA_EVENT_STATE	__BIT(7)
8045e54bd0Sjmcneill #define	 TCA_EVENT_CODE		__BITS(6,0)
8145e54bd0Sjmcneill #define	TCA_KP_GPIO1		0x1d
8245e54bd0Sjmcneill #define	TCA_KP_GPIO2		0x1e
8345e54bd0Sjmcneill #define	TCA_KP_GPIO3		0x1f
8445e54bd0Sjmcneill #define	TCA_DEBOUNCE_DIS1	0x29
8545e54bd0Sjmcneill #define	TCA_DEBOUNCE_DIS2	0x2a
8645e54bd0Sjmcneill #define	TCA_DEBOUNCE_DIS3	0x2b
8745e54bd0Sjmcneill 
8845e54bd0Sjmcneill struct tcakp_softc {
8945e54bd0Sjmcneill 	device_t	sc_dev;
9045e54bd0Sjmcneill 	i2c_tag_t	sc_i2c;
9145e54bd0Sjmcneill 	i2c_addr_t	sc_addr;
9245e54bd0Sjmcneill 	int		sc_phandle;
9345e54bd0Sjmcneill 
9445e54bd0Sjmcneill 	u_int		sc_rows;
9545e54bd0Sjmcneill 	u_int		sc_cols;
9645e54bd0Sjmcneill 	bool		sc_autorepeat;
9745e54bd0Sjmcneill 	u_int		sc_row_shift;
9845e54bd0Sjmcneill 
9945e54bd0Sjmcneill 	uint16_t	sc_keymap[128];
10045e54bd0Sjmcneill 
10145e54bd0Sjmcneill 	void		*sc_ih;
1028f9bb8d1Sthorpej 	void		*sc_sih;
10345e54bd0Sjmcneill 
10445e54bd0Sjmcneill 	int		sc_enabled;
10545e54bd0Sjmcneill 	device_t	sc_wskbddev;
10645e54bd0Sjmcneill };
10745e54bd0Sjmcneill 
10845e54bd0Sjmcneill static int	tcakp_match(device_t, cfdata_t, void *);
10945e54bd0Sjmcneill static void	tcakp_attach(device_t, device_t, void *);
11045e54bd0Sjmcneill 
1118f9bb8d1Sthorpej static int	tcakp_i2c_lock(struct tcakp_softc *);
1128f9bb8d1Sthorpej static void	tcakp_i2c_unlock(struct tcakp_softc *);
1138f9bb8d1Sthorpej 
11445e54bd0Sjmcneill static int	tcakp_read(struct tcakp_softc *, uint8_t, uint8_t *);
11545e54bd0Sjmcneill static int	tcakp_write(struct tcakp_softc *, uint8_t, uint8_t);
11645e54bd0Sjmcneill 
11745e54bd0Sjmcneill CFATTACH_DECL_NEW(tcakp, sizeof(struct tcakp_softc),
11845e54bd0Sjmcneill     tcakp_match, tcakp_attach, NULL, NULL);
11945e54bd0Sjmcneill 
1200089a6b2Sthorpej static const struct device_compatible_entry compat_data[] = {
1213f1bcda8Sthorpej 	{ .compat = "ti,tca8418" },
12201632a17Sthorpej 	DEVICE_COMPAT_EOL
123b3c1046bSthorpej };
124b3c1046bSthorpej 
12545e54bd0Sjmcneill static u_int
tcakp_decode(struct tcakp_softc * sc,uint8_t code)12645e54bd0Sjmcneill tcakp_decode(struct tcakp_softc *sc, uint8_t code)
12745e54bd0Sjmcneill {
12845e54bd0Sjmcneill 	u_int row = code / TCA_MAX_COLS;
12945e54bd0Sjmcneill 	u_int col = code % TCA_MAX_COLS;
13045e54bd0Sjmcneill 	if (col == 0) {
13145e54bd0Sjmcneill 		row = row - 1;
13245e54bd0Sjmcneill 		col = TCA_MAX_COLS - 1;
13345e54bd0Sjmcneill 	} else {
13445e54bd0Sjmcneill 		col = col - 1;
13545e54bd0Sjmcneill 	}
13645e54bd0Sjmcneill 
13745e54bd0Sjmcneill 	return (row << sc->sc_row_shift) + col;
13845e54bd0Sjmcneill }
13945e54bd0Sjmcneill 
14045e54bd0Sjmcneill static int
tcakp_intr(void * priv)14145e54bd0Sjmcneill tcakp_intr(void *priv)
14245e54bd0Sjmcneill {
14345e54bd0Sjmcneill 	struct tcakp_softc * const sc = priv;
1448f9bb8d1Sthorpej 
1458f9bb8d1Sthorpej 	/*
1468f9bb8d1Sthorpej 	 * Schedule our soft interrupt handler.  We can't access the i2c
1478f9bb8d1Sthorpej 	 * from hard interrupt context, so just go ahead and claim the
1488f9bb8d1Sthorpej 	 * interrupt.
1498f9bb8d1Sthorpej 	 *
1508f9bb8d1Sthorpej 	 * XXX If we ever end up with an instance that uses
1518f9bb8d1Sthorpej 	 * level-sensitive interrupts, we will need to mask
1528f9bb8d1Sthorpej 	 * the interrupt source.
1538f9bb8d1Sthorpej 	 */
1548f9bb8d1Sthorpej 	softint_schedule(sc->sc_sih);
1558f9bb8d1Sthorpej 	return 1;
1568f9bb8d1Sthorpej }
1578f9bb8d1Sthorpej 
1588f9bb8d1Sthorpej static void
tcakp_softintr(void * priv)1598f9bb8d1Sthorpej tcakp_softintr(void *priv)
1608f9bb8d1Sthorpej {
1618f9bb8d1Sthorpej 	struct tcakp_softc * const sc = priv;
16245e54bd0Sjmcneill 	uint8_t stat, ev;
1638f9bb8d1Sthorpej 
1648f9bb8d1Sthorpej 	if (tcakp_i2c_lock(sc) != 0)
1658f9bb8d1Sthorpej 		return;
16645e54bd0Sjmcneill 
16745e54bd0Sjmcneill 	tcakp_read(sc, TCA_INT_STAT, &stat);
16845e54bd0Sjmcneill 	if (stat & INT_STAT_K_INT) {
16945e54bd0Sjmcneill 		tcakp_read(sc, TCA_EVENT('A'), &ev);
17045e54bd0Sjmcneill 		while (ev != 0) {
17145e54bd0Sjmcneill 			const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
17245e54bd0Sjmcneill 			const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
17345e54bd0Sjmcneill 
1748f9bb8d1Sthorpej 			tcakp_i2c_unlock(sc);
1758f9bb8d1Sthorpej 
17645e54bd0Sjmcneill 			/* Translate raw code to keymap index */
17745e54bd0Sjmcneill 			const u_int index = tcakp_decode(sc, code);
17845e54bd0Sjmcneill 
17945e54bd0Sjmcneill 			u_int type = pressed ? WSCONS_EVENT_KEY_DOWN :
18045e54bd0Sjmcneill 					       WSCONS_EVENT_KEY_UP;
181d501ef65Sjmcneill 			int key = linux_key_to_usb(sc->sc_keymap[index]);
18245e54bd0Sjmcneill 
18345e54bd0Sjmcneill 			if (sc->sc_wskbddev)
18445e54bd0Sjmcneill 				wskbd_input(sc->sc_wskbddev, type, key);
18545e54bd0Sjmcneill 
1868f9bb8d1Sthorpej 			if (tcakp_i2c_lock(sc) != 0)
1878f9bb8d1Sthorpej 				return;
18845e54bd0Sjmcneill 			tcakp_read(sc, TCA_EVENT('A'), &ev);
18945e54bd0Sjmcneill 		}
19045e54bd0Sjmcneill 	}
19145e54bd0Sjmcneill 	tcakp_write(sc, TCA_INT_STAT, stat);
1928f9bb8d1Sthorpej 	tcakp_i2c_unlock(sc);
19345e54bd0Sjmcneill }
19445e54bd0Sjmcneill 
19545e54bd0Sjmcneill static int
tcakp_init(struct tcakp_softc * sc)19645e54bd0Sjmcneill tcakp_init(struct tcakp_softc *sc)
19745e54bd0Sjmcneill {
19845e54bd0Sjmcneill 	uint32_t mask;
19945e54bd0Sjmcneill 	uint8_t val;
2008f9bb8d1Sthorpej 	int error;
20145e54bd0Sjmcneill 
20245e54bd0Sjmcneill 	if (sc->sc_rows == 0 || sc->sc_cols == 0) {
20345e54bd0Sjmcneill 		aprint_error_dev(sc->sc_dev, "not configured\n");
20445e54bd0Sjmcneill 		return ENXIO;
20545e54bd0Sjmcneill 	}
20645e54bd0Sjmcneill 
20745e54bd0Sjmcneill 	mask = __BITS(sc->sc_rows - 1, 0);
20845e54bd0Sjmcneill 	mask += __BITS(sc->sc_cols - 1, 0) << 8;
20945e54bd0Sjmcneill 
2108f9bb8d1Sthorpej 	error = tcakp_i2c_lock(sc);
2118f9bb8d1Sthorpej 	if (error)
2128f9bb8d1Sthorpej 		return error;
2138f9bb8d1Sthorpej 
21445e54bd0Sjmcneill 	/* Lock the keyboard */
21545e54bd0Sjmcneill 	tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
21645e54bd0Sjmcneill 	/* Select keyboard mode */
21745e54bd0Sjmcneill 	tcakp_write(sc, TCA_KP_GPIO1, (mask >> 0) & 0xff);
21845e54bd0Sjmcneill 	tcakp_write(sc, TCA_KP_GPIO2, (mask >> 8) & 0xff);
21945e54bd0Sjmcneill 	tcakp_write(sc, TCA_KP_GPIO3, (mask >> 16) & 0xff);
22045e54bd0Sjmcneill 	/* Disable debounce */
22145e54bd0Sjmcneill 	tcakp_write(sc, TCA_DEBOUNCE_DIS1, (mask >> 0) & 0xff);
22245e54bd0Sjmcneill 	tcakp_write(sc, TCA_DEBOUNCE_DIS2, (mask >> 8) & 0xff);
22345e54bd0Sjmcneill 	tcakp_write(sc, TCA_DEBOUNCE_DIS3, (mask >> 16) & 0xff);
22445e54bd0Sjmcneill 	/* Enable key event interrupts */
22545e54bd0Sjmcneill 	tcakp_write(sc, TCA_CFG, CFG_INT_CFG | CFG_KE_IEN);
22645e54bd0Sjmcneill 	/* Clear interrupts */
22745e54bd0Sjmcneill 	tcakp_read(sc, TCA_INT_STAT, &val);
22845e54bd0Sjmcneill 	tcakp_write(sc, TCA_INT_STAT, val);
22945e54bd0Sjmcneill 
2308f9bb8d1Sthorpej 	tcakp_i2c_unlock(sc);
2318f9bb8d1Sthorpej 
23245e54bd0Sjmcneill 	return 0;
23345e54bd0Sjmcneill }
23445e54bd0Sjmcneill 
23545e54bd0Sjmcneill static void
tcakp_configure_fdt(struct tcakp_softc * sc)23645e54bd0Sjmcneill tcakp_configure_fdt(struct tcakp_softc *sc)
23745e54bd0Sjmcneill {
23845e54bd0Sjmcneill 	const uint32_t *keymap;
23945e54bd0Sjmcneill 	int len;
24045e54bd0Sjmcneill 
24145e54bd0Sjmcneill 	of_getprop_uint32(sc->sc_phandle, "keypad,num-rows", &sc->sc_rows);
24245e54bd0Sjmcneill 	of_getprop_uint32(sc->sc_phandle, "keypad,num-columns", &sc->sc_cols);
24345e54bd0Sjmcneill 	sc->sc_autorepeat = of_getprop_bool(sc->sc_phandle, "keypad,autorepeat");
24445e54bd0Sjmcneill 
24545e54bd0Sjmcneill 	keymap = fdtbus_get_prop(sc->sc_phandle, "linux,keymap", &len);
24645e54bd0Sjmcneill 	if (keymap == NULL || len <= 0)
24745e54bd0Sjmcneill 		return;
24845e54bd0Sjmcneill 
24945e54bd0Sjmcneill 	sc->sc_row_shift = fls32(sc->sc_cols) - 1;
25045e54bd0Sjmcneill 	if (sc->sc_row_shift & (sc->sc_cols - 1))
25145e54bd0Sjmcneill 		sc->sc_row_shift++;
25245e54bd0Sjmcneill 
25345e54bd0Sjmcneill 	while (len >= 4) {
25445e54bd0Sjmcneill 		const uint32_t e = be32toh(*keymap);
25545e54bd0Sjmcneill 		const u_int row = (e >> 24) & 0xff;
25645e54bd0Sjmcneill 		const u_int col = (e >> 16) & 0xff;
25745e54bd0Sjmcneill 		const u_int index = (row << sc->sc_row_shift) + col;
25845e54bd0Sjmcneill 
25945e54bd0Sjmcneill 		sc->sc_keymap[index] = e & 0xffff;
26045e54bd0Sjmcneill 
26145e54bd0Sjmcneill 		len -= 4;
26245e54bd0Sjmcneill 		keymap++;
26345e54bd0Sjmcneill 	}
26445e54bd0Sjmcneill }
26545e54bd0Sjmcneill 
26645e54bd0Sjmcneill static int
tcakp_enable(void * v,int on)26745e54bd0Sjmcneill tcakp_enable(void *v, int on)
26845e54bd0Sjmcneill {
26945e54bd0Sjmcneill 	struct tcakp_softc * const sc = v;
2708f9bb8d1Sthorpej 	int error;
2718f9bb8d1Sthorpej 
2728f9bb8d1Sthorpej 	error = tcakp_i2c_lock(sc);
2738f9bb8d1Sthorpej 	if (error)
2748f9bb8d1Sthorpej 		return error;
27545e54bd0Sjmcneill 
27645e54bd0Sjmcneill 	if (on) {
27745e54bd0Sjmcneill 		/* Disable key lock */
27845e54bd0Sjmcneill 		tcakp_write(sc, TCA_KEY_LCK_EC, 0);
27945e54bd0Sjmcneill 	} else {
28045e54bd0Sjmcneill 		/* Enable key lock */
28145e54bd0Sjmcneill 		tcakp_write(sc, TCA_KEY_LCK_EC, KEY_LCK_EC_K_LCK_EN);
28245e54bd0Sjmcneill 	}
28345e54bd0Sjmcneill 
2848f9bb8d1Sthorpej 	tcakp_i2c_unlock(sc);
28545e54bd0Sjmcneill 	return 0;
28645e54bd0Sjmcneill }
28745e54bd0Sjmcneill 
28845e54bd0Sjmcneill static void
tcakp_set_leds(void * v,int leds)28945e54bd0Sjmcneill tcakp_set_leds(void *v, int leds)
29045e54bd0Sjmcneill {
29145e54bd0Sjmcneill }
29245e54bd0Sjmcneill 
29345e54bd0Sjmcneill static int
tcakp_ioctl(void * v,u_long cmd,void * data,int flag,lwp_t * l)29445e54bd0Sjmcneill tcakp_ioctl(void *v, u_long cmd, void *data, int flag, lwp_t *l)
29545e54bd0Sjmcneill {
29645e54bd0Sjmcneill 	switch (cmd) {
29745e54bd0Sjmcneill 	case WSKBDIO_GTYPE:
298d501ef65Sjmcneill 		*(int *)data = WSKBD_TYPE_USB;
29945e54bd0Sjmcneill 		return 0;
30045e54bd0Sjmcneill 	}
30145e54bd0Sjmcneill 
30245e54bd0Sjmcneill 	return EPASSTHROUGH;
30345e54bd0Sjmcneill }
30445e54bd0Sjmcneill 
30545e54bd0Sjmcneill static const struct wskbd_accessops tcakp_accessops = {
30645e54bd0Sjmcneill 	tcakp_enable,
30745e54bd0Sjmcneill 	tcakp_set_leds,
30845e54bd0Sjmcneill 	tcakp_ioctl,
30945e54bd0Sjmcneill };
31045e54bd0Sjmcneill 
31145e54bd0Sjmcneill #if notyet
31245e54bd0Sjmcneill static void
tcakp_cngetc(void * v,u_int * type,int * data)31345e54bd0Sjmcneill tcakp_cngetc(void *v, u_int *type, int *data)
31445e54bd0Sjmcneill {
31545e54bd0Sjmcneill 	struct tcakp_softc * const sc = v;
31645e54bd0Sjmcneill 	uint8_t ev = 0;
31745e54bd0Sjmcneill 
3188f9bb8d1Sthorpej 	/* XXX i2c bus acquire */
3198f9bb8d1Sthorpej 
32045e54bd0Sjmcneill 	do {
32145e54bd0Sjmcneill 		tcakp_read(sc, TCA_EVENT('A'), &ev);
32245e54bd0Sjmcneill 	} while (ev == 0);
32345e54bd0Sjmcneill 
32445e54bd0Sjmcneill 	const bool pressed = __SHIFTOUT(ev, TCA_EVENT_STATE);
32545e54bd0Sjmcneill 	const uint8_t code = __SHIFTOUT(ev, TCA_EVENT_CODE);
32645e54bd0Sjmcneill 	const u_int index = tcakp_decode(sc, code);
32745e54bd0Sjmcneill 
32845e54bd0Sjmcneill 	*type = pressed ? WSCONS_EVENT_KEY_DOWN :
32945e54bd0Sjmcneill 			  WSCONS_EVENT_KEY_UP;
33045e54bd0Sjmcneill 	*data = sc->sc_keymap[index];
3318f9bb8d1Sthorpej 
3328f9bb8d1Sthorpej 	/* XXX i2c bus release */
33345e54bd0Sjmcneill }
33445e54bd0Sjmcneill 
33545e54bd0Sjmcneill static void
tcakp_cnpollc(void * v,int on)33645e54bd0Sjmcneill tcakp_cnpollc(void *v, int on)
33745e54bd0Sjmcneill {
33845e54bd0Sjmcneill }
33945e54bd0Sjmcneill 
34045e54bd0Sjmcneill static void
tcakp_cnbell(void * v,u_int pitch,u_int period,u_int volume)34145e54bd0Sjmcneill tcakp_cnbell(void *v, u_int pitch, u_int period, u_int volume)
34245e54bd0Sjmcneill {
34345e54bd0Sjmcneill }
34445e54bd0Sjmcneill 
34545e54bd0Sjmcneill static const struct wskbd_consops tcakp_consops = {
34645e54bd0Sjmcneill 	tcakp_cngetc,
34745e54bd0Sjmcneill 	tcakp_cnpollc,
34845e54bd0Sjmcneill 	tcakp_cnbell,
34945e54bd0Sjmcneill };
35045e54bd0Sjmcneill #endif
35145e54bd0Sjmcneill 
352dc9bc474Sbouyer extern const struct wscons_keydesc hidkbd_keydesctab[];
35345e54bd0Sjmcneill static const struct wskbd_mapdata tcakp_keymapdata = {
354dc9bc474Sbouyer 	hidkbd_keydesctab,
35545e54bd0Sjmcneill 	KB_US,
35645e54bd0Sjmcneill };
35745e54bd0Sjmcneill 
35845e54bd0Sjmcneill static int
tcakp_match(device_t parent,cfdata_t match,void * aux)35945e54bd0Sjmcneill tcakp_match(device_t parent, cfdata_t match, void *aux)
36045e54bd0Sjmcneill {
36145e54bd0Sjmcneill 	struct i2c_attach_args *ia = aux;
36222de7409Sthorpej 	int match_result;
36345e54bd0Sjmcneill 
3640089a6b2Sthorpej 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
36522de7409Sthorpej 		return match_result;
36622de7409Sthorpej 
3673565906cSjmcneill 	if (ia->ia_addr == 0x34)
36822de7409Sthorpej 		return I2C_MATCH_ADDRESS_ONLY;
36922de7409Sthorpej 
3703565906cSjmcneill 	return 0;
37145e54bd0Sjmcneill }
37245e54bd0Sjmcneill 
37345e54bd0Sjmcneill static void
tcakp_attach(device_t parent,device_t self,void * aux)37445e54bd0Sjmcneill tcakp_attach(device_t parent, device_t self, void *aux)
37545e54bd0Sjmcneill {
37645e54bd0Sjmcneill 	struct tcakp_softc * const sc = device_private(self);
37745e54bd0Sjmcneill 	struct i2c_attach_args *ia = aux;
37845e54bd0Sjmcneill 	struct wskbddev_attach_args a;
37945e54bd0Sjmcneill 
38045e54bd0Sjmcneill 	sc->sc_dev = self;
38145e54bd0Sjmcneill 	sc->sc_i2c = ia->ia_tag;
38245e54bd0Sjmcneill 	sc->sc_addr = ia->ia_addr;
38345e54bd0Sjmcneill 	sc->sc_phandle = ia->ia_cookie;
38445e54bd0Sjmcneill 
38545e54bd0Sjmcneill 	aprint_naive("\n");
38645e54bd0Sjmcneill 	aprint_normal(": TCA8418\n");
38745e54bd0Sjmcneill 
38845e54bd0Sjmcneill #ifdef FDT
389792a92e0Sjmcneill 	sc->sc_ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_VM, 0,
390792a92e0Sjmcneill 	    tcakp_intr, sc);
3918f9bb8d1Sthorpej 	/*
3928f9bb8d1Sthorpej 	 * XXX This is an edge-sensitive interrupt, but we'd like to
3938f9bb8d1Sthorpej 	 * be able to check at run-time just to be sure.
3948f9bb8d1Sthorpej 	 */
3958f9bb8d1Sthorpej 	if (sc->sc_ih == NULL) {
3968f9bb8d1Sthorpej 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
3978f9bb8d1Sthorpej 		return;
3988f9bb8d1Sthorpej 	}
3998f9bb8d1Sthorpej 
4008f9bb8d1Sthorpej 	sc->sc_sih = softint_establish(SOFTINT_SERIAL, tcakp_softintr, sc);
4018f9bb8d1Sthorpej 	if (sc->sc_sih == NULL) {
4028f9bb8d1Sthorpej 		aprint_error_dev(sc->sc_dev,
4036a83c6e9Schristos 		    "unable to establish soft interrupt\n");
4048f9bb8d1Sthorpej 		return;
4058f9bb8d1Sthorpej 	}
40645e54bd0Sjmcneill 
40745e54bd0Sjmcneill 	tcakp_configure_fdt(sc);
40845e54bd0Sjmcneill #endif
40945e54bd0Sjmcneill 
41045e54bd0Sjmcneill 	if (tcakp_init(sc) != 0)
41145e54bd0Sjmcneill 		return;
41245e54bd0Sjmcneill 
41345e54bd0Sjmcneill 	memset(&a, 0, sizeof(a));
41445e54bd0Sjmcneill 	a.console = false;	/* XXX */
41545e54bd0Sjmcneill 	a.keymap = &tcakp_keymapdata;
41645e54bd0Sjmcneill 	a.accessops = &tcakp_accessops;
41745e54bd0Sjmcneill 	a.accesscookie = sc;
41845e54bd0Sjmcneill 
419*beecddb6Sthorpej 	sc->sc_wskbddev = config_found(self, &a, wskbddevprint, CFARGS_NONE);
42045e54bd0Sjmcneill }
42145e54bd0Sjmcneill 
42245e54bd0Sjmcneill static int
tcakp_i2c_lock(struct tcakp_softc * sc)4238f9bb8d1Sthorpej tcakp_i2c_lock(struct tcakp_softc *sc)
42445e54bd0Sjmcneill {
42539fde00fSjmcneill 	int error;
42639fde00fSjmcneill 
4278f9bb8d1Sthorpej 	error = iic_acquire_bus(sc->sc_i2c, 0);
4288f9bb8d1Sthorpej 	if (error) {
4298f9bb8d1Sthorpej 		aprint_error_dev(sc->sc_dev,
4308f9bb8d1Sthorpej 		    "unable to acquire bus lock (%d)\n", error);
4318f9bb8d1Sthorpej 	}
43239fde00fSjmcneill 	return error;
43345e54bd0Sjmcneill }
43445e54bd0Sjmcneill 
4358f9bb8d1Sthorpej static void
tcakp_i2c_unlock(struct tcakp_softc * sc)4368f9bb8d1Sthorpej tcakp_i2c_unlock(struct tcakp_softc *sc)
4378f9bb8d1Sthorpej {
4388f9bb8d1Sthorpej 	iic_release_bus(sc->sc_i2c, 0);
4398f9bb8d1Sthorpej }
4408f9bb8d1Sthorpej 
4418f9bb8d1Sthorpej static int
tcakp_read(struct tcakp_softc * sc,uint8_t reg,uint8_t * val)4428f9bb8d1Sthorpej tcakp_read(struct tcakp_softc *sc, uint8_t reg, uint8_t *val)
4438f9bb8d1Sthorpej {
4448f9bb8d1Sthorpej 	return iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr,
4458f9bb8d1Sthorpej 	    &reg, 1, val, 1, 0);
4468f9bb8d1Sthorpej }
4478f9bb8d1Sthorpej 
44845e54bd0Sjmcneill static int
tcakp_write(struct tcakp_softc * sc,uint8_t reg,uint8_t val)44945e54bd0Sjmcneill tcakp_write(struct tcakp_softc *sc, uint8_t reg, uint8_t val)
45045e54bd0Sjmcneill {
45145e54bd0Sjmcneill 	uint8_t buf[2] = { reg, val };
45239fde00fSjmcneill 
4538f9bb8d1Sthorpej 	return iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP,
4548f9bb8d1Sthorpej 	    sc->sc_addr, NULL, 0, buf, 2, 0);
45545e54bd0Sjmcneill }
456