Home | History | Annotate | Line # | Download | only in nvidia
tegra_cec.c revision 1.8
      1  1.8   thorpej /* $NetBSD: tegra_cec.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015 Jared D. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.8   thorpej __KERNEL_RCSID(0, "$NetBSD: tegra_cec.c,v 1.8 2021/01/27 03:10:19 thorpej Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/mutex.h>
     39  1.1  jmcneill #include <sys/condvar.h>
     40  1.1  jmcneill #include <sys/poll.h>
     41  1.1  jmcneill #include <sys/select.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/hdmicec/hdmicecio.h>
     44  1.1  jmcneill #include <dev/hdmicec/hdmicec_if.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/nvidia/tegra_var.h>
     47  1.1  jmcneill #include <arm/nvidia/tegra_pmcreg.h>
     48  1.1  jmcneill #include <arm/nvidia/tegra_cecreg.h>
     49  1.1  jmcneill 
     50  1.2  jmcneill #include <dev/fdt/fdtvar.h>
     51  1.2  jmcneill 
     52  1.1  jmcneill #define CEC_VENDORID_NVIDIA	0x00044b
     53  1.1  jmcneill 
     54  1.1  jmcneill static int	tegra_cec_match(device_t, cfdata_t, void *);
     55  1.1  jmcneill static void	tegra_cec_attach(device_t, device_t, void *);
     56  1.1  jmcneill 
     57  1.1  jmcneill static int	tegra_cec_intr(void *);
     58  1.1  jmcneill 
     59  1.1  jmcneill struct tegra_cec_softc {
     60  1.1  jmcneill 	device_t		sc_dev;
     61  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     62  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     63  1.1  jmcneill 	void			*sc_ih;
     64  1.3  jmcneill 	struct clk		*sc_clk;
     65  1.3  jmcneill 	struct fdtbus_reset	*sc_rst;
     66  1.1  jmcneill 
     67  1.1  jmcneill 	kmutex_t		sc_lock;
     68  1.1  jmcneill 	kcondvar_t		sc_cv;
     69  1.1  jmcneill 
     70  1.1  jmcneill 	const char		*sc_hdmidevname;
     71  1.1  jmcneill 	device_t		sc_cecdev;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	struct selinfo		sc_selinfo;
     74  1.1  jmcneill 
     75  1.1  jmcneill 	uint8_t			sc_rxbuf[16];
     76  1.1  jmcneill 	int			sc_rxlen;
     77  1.1  jmcneill 	bool			sc_rxdone;
     78  1.1  jmcneill 
     79  1.1  jmcneill 	uint8_t			sc_txbuf[16];
     80  1.1  jmcneill 	int			sc_txlen;
     81  1.1  jmcneill 	int			sc_txcur;
     82  1.1  jmcneill 	int			sc_txerr;
     83  1.1  jmcneill 	bool			sc_txdone;
     84  1.1  jmcneill };
     85  1.1  jmcneill 
     86  1.1  jmcneill static void	tegra_cec_reset(struct tegra_cec_softc *);
     87  1.1  jmcneill 
     88  1.1  jmcneill static int	tegra_cec_open(void *, int);
     89  1.1  jmcneill static void	tegra_cec_close(void *);
     90  1.1  jmcneill static int	tegra_cec_ioctl(void *, u_long, void *, int, lwp_t *);
     91  1.1  jmcneill static int	tegra_cec_send(void *, const uint8_t *, size_t);
     92  1.1  jmcneill static ssize_t	tegra_cec_recv(void *, uint8_t *, size_t);
     93  1.1  jmcneill static int	tegra_cec_poll(void *, int, lwp_t *);
     94  1.1  jmcneill 
     95  1.1  jmcneill static const struct hdmicec_hw_if tegra_cec_hw_if = {
     96  1.1  jmcneill 	.open = tegra_cec_open,
     97  1.1  jmcneill 	.close = tegra_cec_close,
     98  1.1  jmcneill 	.ioctl = tegra_cec_ioctl,
     99  1.1  jmcneill 	.send = tegra_cec_send,
    100  1.1  jmcneill 	.recv = tegra_cec_recv,
    101  1.1  jmcneill 	.poll = tegra_cec_poll,
    102  1.1  jmcneill };
    103  1.1  jmcneill 
    104  1.1  jmcneill CFATTACH_DECL_NEW(tegra_cec, sizeof(struct tegra_cec_softc),
    105  1.1  jmcneill 	tegra_cec_match, tegra_cec_attach, NULL, NULL);
    106  1.1  jmcneill 
    107  1.1  jmcneill #define CEC_READ(sc, reg)			\
    108  1.1  jmcneill     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    109  1.1  jmcneill #define CEC_WRITE(sc, reg, val)			\
    110  1.1  jmcneill     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    111  1.1  jmcneill #define CEC_SET_CLEAR(sc, reg, set, clr)	\
    112  1.1  jmcneill     tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
    113  1.1  jmcneill 
    114  1.8   thorpej static const struct device_compatible_entry compat_data[] = {
    115  1.8   thorpej 	{ .compat = "nvidia,tegra124-cec" },
    116  1.8   thorpej 	DEVICE_COMPAT_EOL
    117  1.8   thorpej };
    118  1.8   thorpej 
    119  1.1  jmcneill static int
    120  1.1  jmcneill tegra_cec_match(device_t parent, cfdata_t cf, void *aux)
    121  1.1  jmcneill {
    122  1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    123  1.2  jmcneill 
    124  1.8   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    125  1.1  jmcneill }
    126  1.1  jmcneill 
    127  1.1  jmcneill static void
    128  1.1  jmcneill tegra_cec_attach(device_t parent, device_t self, void *aux)
    129  1.1  jmcneill {
    130  1.1  jmcneill 	struct tegra_cec_softc * const sc = device_private(self);
    131  1.2  jmcneill 	struct fdt_attach_args * const faa = aux;
    132  1.1  jmcneill 	prop_dictionary_t prop = device_properties(self);
    133  1.1  jmcneill 	struct hdmicec_attach_args caa;
    134  1.2  jmcneill 	char intrstr[128];
    135  1.2  jmcneill 	bus_addr_t addr;
    136  1.2  jmcneill 	bus_size_t size;
    137  1.2  jmcneill 	int error;
    138  1.2  jmcneill 
    139  1.2  jmcneill 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
    140  1.2  jmcneill 		aprint_error(": couldn't get registers\n");
    141  1.2  jmcneill 		return;
    142  1.2  jmcneill 	}
    143  1.3  jmcneill 	sc->sc_clk = fdtbus_clock_get(faa->faa_phandle, "cec");
    144  1.3  jmcneill 	if (sc->sc_clk == NULL) {
    145  1.3  jmcneill 		aprint_error(": couldn't get clock cec\n");
    146  1.3  jmcneill 		return;
    147  1.3  jmcneill 	}
    148  1.3  jmcneill 	sc->sc_rst = fdtbus_reset_get(faa->faa_phandle, "cec");
    149  1.3  jmcneill 	if (sc->sc_rst == NULL) {
    150  1.3  jmcneill 		aprint_error(": couldn't get reset cec\n");
    151  1.3  jmcneill 		return;
    152  1.3  jmcneill 	}
    153  1.1  jmcneill 
    154  1.1  jmcneill 	sc->sc_dev = self;
    155  1.2  jmcneill 	sc->sc_bst = faa->faa_bst;
    156  1.2  jmcneill 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
    157  1.2  jmcneill 	if (error) {
    158  1.6     skrll 		aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error);
    159  1.2  jmcneill 		return;
    160  1.2  jmcneill 	}
    161  1.1  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    162  1.1  jmcneill 	cv_init(&sc->sc_cv, "tegracec");
    163  1.1  jmcneill 	selinit(&sc->sc_selinfo);
    164  1.1  jmcneill 
    165  1.1  jmcneill 	aprint_naive("\n");
    166  1.1  jmcneill 	aprint_normal(": HDMI CEC\n");
    167  1.1  jmcneill 
    168  1.2  jmcneill 	if (!fdtbus_intr_str(faa->faa_phandle, 0, intrstr, sizeof(intrstr))) {
    169  1.2  jmcneill 		aprint_error_dev(self, "failed to decode interrupt\n");
    170  1.2  jmcneill 		return;
    171  1.2  jmcneill 	}
    172  1.2  jmcneill 
    173  1.7  jmcneill 	sc->sc_ih = fdtbus_intr_establish_xname(faa->faa_phandle, 0, IPL_VM,
    174  1.7  jmcneill 	    FDT_INTR_MPSAFE, tegra_cec_intr, sc, device_xname(self));
    175  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    176  1.2  jmcneill 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    177  1.2  jmcneill 		    intrstr);
    178  1.1  jmcneill 		return;
    179  1.1  jmcneill 	}
    180  1.2  jmcneill 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    181  1.1  jmcneill 
    182  1.1  jmcneill 	prop_dictionary_get_cstring_nocopy(prop, "hdmi-device",
    183  1.1  jmcneill 	    &sc->sc_hdmidevname);
    184  1.1  jmcneill 
    185  1.3  jmcneill 	fdtbus_reset_assert(sc->sc_rst);
    186  1.3  jmcneill 	error = clk_enable(sc->sc_clk);
    187  1.3  jmcneill 	if (error) {
    188  1.3  jmcneill 		aprint_error_dev(self, "couldn't enable cec: %d\n", error);
    189  1.3  jmcneill 		return;
    190  1.3  jmcneill 	}
    191  1.3  jmcneill 	fdtbus_reset_deassert(sc->sc_rst);
    192  1.1  jmcneill 
    193  1.1  jmcneill 	CEC_WRITE(sc, CEC_SW_CONTROL_REG, 0);
    194  1.1  jmcneill 	CEC_WRITE(sc, CEC_INPUT_FILTER_REG, 0);
    195  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    196  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
    197  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    198  1.1  jmcneill 
    199  1.1  jmcneill 	memset(&caa, 0, sizeof(caa));
    200  1.1  jmcneill 	caa.priv = sc;
    201  1.1  jmcneill 	caa.hwif = &tegra_cec_hw_if;
    202  1.1  jmcneill 	sc->sc_cecdev = config_found(self, &caa, NULL);
    203  1.1  jmcneill }
    204  1.1  jmcneill 
    205  1.1  jmcneill static int
    206  1.1  jmcneill tegra_cec_intr(void *priv)
    207  1.1  jmcneill {
    208  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    209  1.1  jmcneill 	uint32_t val;
    210  1.1  jmcneill 	int handled = 0;
    211  1.1  jmcneill 
    212  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    213  1.1  jmcneill 	const uint32_t int_stat = CEC_READ(sc, CEC_INT_STAT_REG);
    214  1.1  jmcneill 
    215  1.1  jmcneill 	if (int_stat & CEC_INT_RX_REGISTER_FULL) {
    216  1.1  jmcneill 		val = CEC_READ(sc, CEC_RX_REGISTER_REG);
    217  1.1  jmcneill 		sc->sc_rxbuf[sc->sc_rxlen++] =
    218  1.1  jmcneill 		    __SHIFTOUT(val, CEC_RX_REGISTER_DATA);
    219  1.1  jmcneill 		if ((val & CEC_RX_REGISTER_EOM) != 0 ||
    220  1.1  jmcneill 		    sc->sc_rxlen == 16) {
    221  1.1  jmcneill 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    222  1.1  jmcneill 			    CEC_INT_RX_REGISTER_FULL);
    223  1.1  jmcneill 			sc->sc_rxdone = true;
    224  1.1  jmcneill 			cv_broadcast(&sc->sc_cv);
    225  1.1  jmcneill 			selnotify(&sc->sc_selinfo, POLLIN|POLLRDNORM,
    226  1.1  jmcneill 			    NOTE_SUBMIT);
    227  1.1  jmcneill 		}
    228  1.1  jmcneill 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_RX_REGISTER_FULL);
    229  1.1  jmcneill 		++handled;
    230  1.1  jmcneill 	}
    231  1.1  jmcneill 
    232  1.1  jmcneill 	if (int_stat & CEC_INT_TX_REGISTER_EMPTY) {
    233  1.1  jmcneill 		if (sc->sc_txcur < sc->sc_txlen) {
    234  1.1  jmcneill 			const uint8_t destination = sc->sc_txbuf[0] & 0xf;
    235  1.1  jmcneill 			val = __SHIFTIN(sc->sc_txbuf[sc->sc_txcur],
    236  1.1  jmcneill 			    CEC_TX_REGISTER_DATA);
    237  1.1  jmcneill 			if (sc->sc_txcur == 0)
    238  1.1  jmcneill 				val |= CEC_TX_REGISTER_GENERATE_START_BIT;
    239  1.1  jmcneill 			if (sc->sc_txcur == sc->sc_txlen - 1)
    240  1.1  jmcneill 				val |= CEC_TX_REGISTER_EOM;
    241  1.1  jmcneill 			if (destination == 0xf)
    242  1.1  jmcneill 				val |= CEC_TX_REGISTER_ADDRESS_MODE;
    243  1.1  jmcneill 
    244  1.1  jmcneill 			CEC_WRITE(sc, CEC_TX_REGISTER_REG, val);
    245  1.1  jmcneill 			CEC_WRITE(sc, CEC_INT_STAT_REG,
    246  1.1  jmcneill 			    CEC_INT_TX_REGISTER_EMPTY);
    247  1.1  jmcneill 			++sc->sc_txcur;
    248  1.1  jmcneill 		} else {
    249  1.1  jmcneill 			CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    250  1.1  jmcneill 			    CEC_INT_TX_REGISTER_EMPTY);
    251  1.1  jmcneill 		}
    252  1.1  jmcneill 		++handled;
    253  1.1  jmcneill 	}
    254  1.1  jmcneill 
    255  1.1  jmcneill 	if (int_stat & CEC_INT_TX_FRAME_TRANSMITTED) {
    256  1.1  jmcneill 		CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
    257  1.1  jmcneill 		    CEC_INT_TX_FRAME_TRANSMITTED |
    258  1.1  jmcneill 		    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
    259  1.1  jmcneill 		CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_TX_FRAME_TRANSMITTED);
    260  1.1  jmcneill 		if (int_stat & CEC_INT_TX_FRAME_OR_BLOCK_NAKD) {
    261  1.1  jmcneill 			CEC_WRITE(sc, CEC_INT_STAT_REG,
    262  1.1  jmcneill 			    CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
    263  1.1  jmcneill 			sc->sc_txerr = ECONNREFUSED;
    264  1.1  jmcneill 			tegra_cec_reset(sc);
    265  1.1  jmcneill 		}
    266  1.1  jmcneill 		sc->sc_txdone = true;
    267  1.1  jmcneill 		cv_broadcast(&sc->sc_cv);
    268  1.1  jmcneill 		++handled;
    269  1.1  jmcneill 	}
    270  1.1  jmcneill 
    271  1.1  jmcneill 	if (int_stat & CEC_INT_TX_REGISTER_UNDERRUN) {
    272  1.1  jmcneill 		tegra_cec_reset(sc);
    273  1.1  jmcneill 		cv_broadcast(&sc->sc_cv);
    274  1.1  jmcneill 		++handled;
    275  1.1  jmcneill 	}
    276  1.1  jmcneill 
    277  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    278  1.1  jmcneill 
    279  1.1  jmcneill 	return handled;
    280  1.1  jmcneill }
    281  1.1  jmcneill 
    282  1.1  jmcneill static void
    283  1.1  jmcneill tegra_cec_reset(struct tegra_cec_softc *sc)
    284  1.1  jmcneill {
    285  1.1  jmcneill 	uint32_t val;
    286  1.1  jmcneill 
    287  1.1  jmcneill 	KASSERT(mutex_owned(&sc->sc_lock));
    288  1.1  jmcneill 
    289  1.1  jmcneill 	val = CEC_READ(sc, CEC_HW_CONTROL_REG);
    290  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    291  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    292  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, val);
    293  1.1  jmcneill }
    294  1.1  jmcneill 
    295  1.1  jmcneill static int
    296  1.1  jmcneill tegra_cec_open(void *priv, int flag)
    297  1.1  jmcneill {
    298  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    299  1.1  jmcneill 
    300  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    301  1.1  jmcneill 	sc->sc_rxlen = 0;
    302  1.1  jmcneill 	sc->sc_rxdone = false;
    303  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, CEC_INT_RX_REGISTER_FULL);
    304  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, CEC_HW_CONTROL_TX_RX_MODE);
    305  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    306  1.1  jmcneill 
    307  1.1  jmcneill 	return 0;
    308  1.1  jmcneill }
    309  1.1  jmcneill 
    310  1.1  jmcneill static void
    311  1.1  jmcneill tegra_cec_close(void *priv)
    312  1.1  jmcneill {
    313  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    314  1.1  jmcneill 
    315  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    316  1.1  jmcneill 	CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
    317  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
    318  1.1  jmcneill 	CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
    319  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    320  1.1  jmcneill }
    321  1.1  jmcneill 
    322  1.1  jmcneill static int
    323  1.1  jmcneill tegra_cec_get_phys_addr(struct tegra_cec_softc *sc, uint16_t *phys_addr)
    324  1.1  jmcneill {
    325  1.1  jmcneill 	device_t hdmidev;
    326  1.1  jmcneill 
    327  1.1  jmcneill 	if (sc->sc_hdmidevname == NULL)
    328  1.1  jmcneill 		return EIO;
    329  1.1  jmcneill 	hdmidev = device_find_by_xname(sc->sc_hdmidevname);
    330  1.1  jmcneill 	if (hdmidev == NULL)
    331  1.1  jmcneill 		return ENXIO;
    332  1.1  jmcneill 
    333  1.1  jmcneill 	const prop_dictionary_t prop = device_properties(hdmidev);
    334  1.1  jmcneill 	if (!prop_dictionary_get_uint16(prop, "physical-address", phys_addr))
    335  1.1  jmcneill 		return ENOTCONN;
    336  1.1  jmcneill 
    337  1.1  jmcneill 	return 0;
    338  1.1  jmcneill }
    339  1.1  jmcneill 
    340  1.1  jmcneill static int
    341  1.1  jmcneill tegra_cec_ioctl(void *priv, u_long cmd, void *data, int flag, lwp_t *l)
    342  1.1  jmcneill {
    343  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    344  1.1  jmcneill 	uint32_t val;
    345  1.1  jmcneill 
    346  1.1  jmcneill 	switch (cmd) {
    347  1.1  jmcneill 	case CEC_GET_PHYS_ADDR:
    348  1.1  jmcneill 		return tegra_cec_get_phys_addr(sc, data);
    349  1.1  jmcneill 	case CEC_GET_LOG_ADDRS:
    350  1.1  jmcneill 		val = CEC_READ(sc, CEC_HW_CONTROL_REG);
    351  1.1  jmcneill 		*(uint16_t *)data =
    352  1.1  jmcneill 		    __SHIFTOUT(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
    353  1.1  jmcneill 		return 0;
    354  1.1  jmcneill 	case CEC_SET_LOG_ADDRS:
    355  1.1  jmcneill 		val = *(uint16_t *)data & 0x7fff;
    356  1.1  jmcneill 		CEC_SET_CLEAR(sc, CEC_HW_CONTROL_REG,
    357  1.1  jmcneill 		    __SHIFTIN(val, CEC_HW_CONTROL_RX_LOGICAL_ADDRS),
    358  1.1  jmcneill 		    CEC_HW_CONTROL_RX_LOGICAL_ADDRS);
    359  1.1  jmcneill 		return 0;
    360  1.1  jmcneill 	case CEC_GET_VENDOR_ID:
    361  1.1  jmcneill 		*(uint32_t *)data = CEC_VENDORID_NVIDIA;
    362  1.1  jmcneill 		return 0;
    363  1.1  jmcneill 	default:
    364  1.1  jmcneill 		return EINVAL;
    365  1.1  jmcneill 	}
    366  1.1  jmcneill }
    367  1.1  jmcneill 
    368  1.1  jmcneill static int
    369  1.1  jmcneill tegra_cec_send(void *priv, const uint8_t *data, size_t len)
    370  1.1  jmcneill {
    371  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    372  1.1  jmcneill 	int error = 0;
    373  1.1  jmcneill 
    374  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    375  1.1  jmcneill 
    376  1.1  jmcneill 	sc->sc_txdone = false;
    377  1.1  jmcneill 	sc->sc_txcur = 0;
    378  1.1  jmcneill 	sc->sc_txerr = 0;
    379  1.1  jmcneill 	memcpy(sc->sc_txbuf, data, len);
    380  1.1  jmcneill 	sc->sc_txlen = len;
    381  1.1  jmcneill 
    382  1.1  jmcneill 	CEC_SET_CLEAR(sc, CEC_INT_MASK_REG,
    383  1.1  jmcneill 	    CEC_INT_TX_REGISTER_EMPTY |
    384  1.1  jmcneill 	    CEC_INT_TX_FRAME_TRANSMITTED |
    385  1.1  jmcneill 	    CEC_INT_TX_FRAME_OR_BLOCK_NAKD, 0);
    386  1.1  jmcneill 
    387  1.1  jmcneill 	while (sc->sc_txdone == false) {
    388  1.1  jmcneill 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
    389  1.1  jmcneill 		if (error)
    390  1.1  jmcneill 			break;
    391  1.1  jmcneill 	}
    392  1.1  jmcneill 
    393  1.1  jmcneill 	if (sc->sc_txdone)
    394  1.1  jmcneill 		error = sc->sc_txerr;
    395  1.1  jmcneill 
    396  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    397  1.1  jmcneill 
    398  1.1  jmcneill 	return error;
    399  1.1  jmcneill }
    400  1.1  jmcneill 
    401  1.1  jmcneill static ssize_t
    402  1.1  jmcneill tegra_cec_recv(void *priv, uint8_t *data, size_t len)
    403  1.1  jmcneill {
    404  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    405  1.1  jmcneill 	ssize_t alen = -1;
    406  1.1  jmcneill 	int error = 0;
    407  1.1  jmcneill 
    408  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    409  1.1  jmcneill 
    410  1.1  jmcneill 	while (sc->sc_rxdone == false) {
    411  1.1  jmcneill 		error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock, hz);
    412  1.1  jmcneill 		if (error)
    413  1.1  jmcneill 			break;
    414  1.1  jmcneill 	}
    415  1.1  jmcneill 
    416  1.1  jmcneill 	if (sc->sc_rxdone) {
    417  1.1  jmcneill 		memcpy(data, sc->sc_rxbuf, sc->sc_rxlen);
    418  1.1  jmcneill 		alen = sc->sc_rxlen;
    419  1.1  jmcneill 		sc->sc_rxlen = 0;
    420  1.1  jmcneill 		sc->sc_rxdone = false;
    421  1.1  jmcneill 	}
    422  1.1  jmcneill 
    423  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    424  1.1  jmcneill 
    425  1.1  jmcneill 	return alen;
    426  1.1  jmcneill }
    427  1.1  jmcneill 
    428  1.1  jmcneill static int
    429  1.1  jmcneill tegra_cec_poll(void *priv, int events, lwp_t *l)
    430  1.1  jmcneill {
    431  1.1  jmcneill 	struct tegra_cec_softc * const sc = priv;
    432  1.1  jmcneill 	int revents;
    433  1.1  jmcneill 
    434  1.1  jmcneill 	revents = events & (POLLOUT | POLLWRNORM);
    435  1.1  jmcneill 
    436  1.1  jmcneill 	if ((events & (POLLIN | POLLRDNORM)) == 0)
    437  1.1  jmcneill 		return revents;
    438  1.1  jmcneill 
    439  1.1  jmcneill 	mutex_enter(&sc->sc_lock);
    440  1.1  jmcneill 	if (sc->sc_rxdone) {
    441  1.1  jmcneill 		revents = (events & (POLLIN | POLLRDNORM));
    442  1.1  jmcneill 	} else {
    443  1.1  jmcneill 		selrecord(l, &sc->sc_selinfo);
    444  1.1  jmcneill 		revents = 0;
    445  1.1  jmcneill 	}
    446  1.1  jmcneill 	mutex_exit(&sc->sc_lock);
    447  1.1  jmcneill 
    448  1.1  jmcneill 	return revents;
    449  1.1  jmcneill }
    450