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