Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch/arm/nvidia Add driver for Tegra HDMI CEC controller.



details:   https://anonhg.NetBSD.org/src/rev/07923a072c9f
branches:  trunk
changeset: 339637:07923a072c9f
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sat Aug 01 21:20:11 2015 +0000

description:
Add driver for Tegra HDMI CEC controller.

diffstat:

 sys/arch/arm/nvidia/files.tegra    |    7 +-
 sys/arch/arm/nvidia/tegra_car.c    |   16 +-
 sys/arch/arm/nvidia/tegra_cec.c    |  408 +++++++++++++++++++++++++++++++++++++
 sys/arch/arm/nvidia/tegra_cecreg.h |   90 ++++++++
 sys/arch/arm/nvidia/tegra_intr.h   |    3 +-
 sys/arch/arm/nvidia/tegra_io.c     |    6 +-
 sys/arch/arm/nvidia/tegra_reg.h    |    4 +-
 sys/arch/arm/nvidia/tegra_var.h    |    3 +-
 8 files changed, 529 insertions(+), 8 deletions(-)

diffs (truncated from 643 to 300 lines):

diff -r 89f0523da478 -r 07923a072c9f sys/arch/arm/nvidia/files.tegra
--- a/sys/arch/arm/nvidia/files.tegra   Sat Aug 01 21:19:24 2015 +0000
+++ b/sys/arch/arm/nvidia/files.tegra   Sat Aug 01 21:20:11 2015 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.tegra,v 1.14 2015/05/30 13:25:55 jmcneill Exp $
+#      $NetBSD: files.tegra,v 1.15 2015/08/01 21:20:11 jmcneill Exp $
 #
 # Configuration info for NVIDIA Tegra ARM Peripherals
 #
@@ -111,6 +111,11 @@
 attach tegrahdmi at tegraio with tegra_hdmi
 file   arch/arm/nvidia/tegra_hdmi.c            tegra_hdmi
 
+# HDMI CEC
+device tegracec: hdmicecbus
+attach tegracec at tegraio with tegra_cec
+file   arch/arm/nvidia/tegra_cec.c             tegra_cec
+
 # Console parameters
 defparam opt_tegra.h                   CONADDR
 defparam opt_tegra.h                   CONSPEED
diff -r 89f0523da478 -r 07923a072c9f sys/arch/arm/nvidia/tegra_car.c
--- a/sys/arch/arm/nvidia/tegra_car.c   Sat Aug 01 21:19:24 2015 +0000
+++ b/sys/arch/arm/nvidia/tegra_car.c   Sat Aug 01 21:20:11 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: tegra_car.c,v 1.25 2015/07/29 14:30:18 jmcneill Exp $ */
+/* $NetBSD: tegra_car.c,v 1.26 2015/08/01 21:20:11 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -29,7 +29,7 @@
 #include "locators.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: tegra_car.c,v 1.25 2015/07/29 14:30:18 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: tegra_car.c,v 1.26 2015/08/01 21:20:11 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -685,6 +685,18 @@
 }
 
 void
+tegra_car_periph_cec_enable(void)
+{
+       bus_space_tag_t bst;
+       bus_space_handle_t bsh;
+
+       tegra_car_get_bs(&bst, &bsh);
+
+       bus_space_write_4(bst, bsh, CAR_CLK_ENB_W_SET_REG, CAR_DEV_W_CEC);
+       bus_space_write_4(bst, bsh, CAR_RST_DEV_W_CLR_REG, CAR_DEV_W_CEC);
+}
+
+void
 tegra_car_hdmi_enable(u_int rate)
 {
        bus_space_tag_t bst;
diff -r 89f0523da478 -r 07923a072c9f sys/arch/arm/nvidia/tegra_cec.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/nvidia/tegra_cec.c   Sat Aug 01 21:20:11 2015 +0000
@@ -0,0 +1,408 @@
+/* $NetBSD: tegra_cec.c,v 1.1 2015/08/01 21:20:11 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "locators.h"
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: tegra_cec.c,v 1.1 2015/08/01 21:20:11 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+#include <sys/poll.h>
+#include <sys/select.h>
+
+#include <dev/hdmicec/hdmicecio.h>
+#include <dev/hdmicec/hdmicec_if.h>
+
+#include <arm/nvidia/tegra_var.h>
+#include <arm/nvidia/tegra_pmcreg.h>
+#include <arm/nvidia/tegra_cecreg.h>
+
+#define CEC_VENDORID_NVIDIA    0x00044b
+
+static int     tegra_cec_match(device_t, cfdata_t, void *);
+static void    tegra_cec_attach(device_t, device_t, void *);
+
+static int     tegra_cec_intr(void *);
+
+struct tegra_cec_softc {
+       device_t                sc_dev;
+       bus_space_tag_t         sc_bst;
+       bus_space_handle_t      sc_bsh;
+       void                    *sc_ih;
+
+       kmutex_t                sc_lock;
+       kcondvar_t              sc_cv;
+
+       const char              *sc_hdmidevname;
+       device_t                sc_cecdev;
+
+       struct selinfo          sc_selinfo;
+
+       uint8_t                 sc_rxbuf[16];
+       int                     sc_rxlen;
+       bool                    sc_rxdone;
+
+       uint8_t                 sc_txbuf[16];
+       int                     sc_txlen;
+       int                     sc_txcur;
+       int                     sc_txerr;
+       bool                    sc_txdone;
+};
+
+static void    tegra_cec_reset(struct tegra_cec_softc *);
+
+static int     tegra_cec_open(void *, int);
+static void    tegra_cec_close(void *);
+static int     tegra_cec_ioctl(void *, u_long, void *, int, lwp_t *);
+static int     tegra_cec_send(void *, const uint8_t *, size_t);
+static ssize_t tegra_cec_recv(void *, uint8_t *, size_t);
+static int     tegra_cec_poll(void *, int, lwp_t *);
+
+static const struct hdmicec_hw_if tegra_cec_hw_if = {
+       .open = tegra_cec_open,
+       .close = tegra_cec_close,
+       .ioctl = tegra_cec_ioctl,
+       .send = tegra_cec_send,
+       .recv = tegra_cec_recv,
+       .poll = tegra_cec_poll,
+};
+
+CFATTACH_DECL_NEW(tegra_cec, sizeof(struct tegra_cec_softc),
+       tegra_cec_match, tegra_cec_attach, NULL, NULL);
+
+#define CEC_READ(sc, reg)                      \
+    bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
+#define CEC_WRITE(sc, reg, val)                        \
+    bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
+#define CEC_SET_CLEAR(sc, reg, set, clr)       \
+    tegra_reg_set_clear((sc)->sc_bst, (sc)->sc_bsh, (reg), (set), (clr))
+
+static int
+tegra_cec_match(device_t parent, cfdata_t cf, void *aux)
+{
+       return 1;
+}
+
+static void
+tegra_cec_attach(device_t parent, device_t self, void *aux)
+{
+       struct tegra_cec_softc * const sc = device_private(self);
+       struct tegraio_attach_args * const tio = aux;
+       const struct tegra_locators * const loc = &tio->tio_loc;
+       prop_dictionary_t prop = device_properties(self);
+       struct hdmicec_attach_args caa;
+
+       sc->sc_dev = self;
+       sc->sc_bst = tio->tio_bst;
+       bus_space_subregion(tio->tio_bst, tio->tio_bsh,
+           loc->loc_offset, loc->loc_size, &sc->sc_bsh);
+       mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
+       cv_init(&sc->sc_cv, "tegracec");
+       selinit(&sc->sc_selinfo);
+
+       aprint_naive("\n");
+       aprint_normal(": HDMI CEC\n");
+
+       sc->sc_ih = intr_establish(loc->loc_intr, IPL_VM, IST_LEVEL|IST_MPSAFE,
+           tegra_cec_intr, sc);
+       if (sc->sc_ih == NULL) {
+               aprint_error_dev(self, "couldn't establish interrupt %d\n",
+                   loc->loc_intr);
+               return;
+       }
+       aprint_normal_dev(self, "interrupting on irq %d\n", loc->loc_intr);
+
+       prop_dictionary_get_cstring_nocopy(prop, "hdmi-device",
+           &sc->sc_hdmidevname);
+
+       tegra_car_periph_cec_enable();
+
+       CEC_WRITE(sc, CEC_SW_CONTROL_REG, 0);
+       CEC_WRITE(sc, CEC_INPUT_FILTER_REG, 0);
+       CEC_WRITE(sc, CEC_HW_CONTROL_REG, 0);
+       CEC_WRITE(sc, CEC_INT_MASK_REG, 0);
+       CEC_WRITE(sc, CEC_INT_STAT_REG, 0xffffffff);
+
+       memset(&caa, 0, sizeof(caa));
+       caa.priv = sc;
+       caa.hwif = &tegra_cec_hw_if;
+       sc->sc_cecdev = config_found(self, &caa, NULL);
+}
+
+static int
+tegra_cec_intr(void *priv)
+{
+       struct tegra_cec_softc * const sc = priv;
+       uint32_t val;
+       int handled = 0;
+
+       mutex_enter(&sc->sc_lock);
+       const uint32_t int_stat = CEC_READ(sc, CEC_INT_STAT_REG);
+
+       if (int_stat & CEC_INT_RX_REGISTER_FULL) {
+               val = CEC_READ(sc, CEC_RX_REGISTER_REG);
+               sc->sc_rxbuf[sc->sc_rxlen++] =
+                   __SHIFTOUT(val, CEC_RX_REGISTER_DATA);
+               if ((val & CEC_RX_REGISTER_EOM) != 0 ||
+                   sc->sc_rxlen == 16) {
+                       CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
+                           CEC_INT_RX_REGISTER_FULL);
+                       sc->sc_rxdone = true;
+                       cv_broadcast(&sc->sc_cv);
+                       selnotify(&sc->sc_selinfo, POLLIN|POLLRDNORM,
+                           NOTE_SUBMIT);
+               }
+               CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_RX_REGISTER_FULL);
+               ++handled;
+       }
+
+       if (int_stat & CEC_INT_TX_REGISTER_EMPTY) {
+               if (sc->sc_txcur < sc->sc_txlen) {
+                       const uint8_t destination = sc->sc_txbuf[0] & 0xf;
+                       val = __SHIFTIN(sc->sc_txbuf[sc->sc_txcur],
+                           CEC_TX_REGISTER_DATA);
+                       if (sc->sc_txcur == 0)
+                               val |= CEC_TX_REGISTER_GENERATE_START_BIT;
+                       if (sc->sc_txcur == sc->sc_txlen - 1)
+                               val |= CEC_TX_REGISTER_EOM;
+                       if (destination == 0xf)
+                               val |= CEC_TX_REGISTER_ADDRESS_MODE;
+
+                       CEC_WRITE(sc, CEC_TX_REGISTER_REG, val);
+                       CEC_WRITE(sc, CEC_INT_STAT_REG,
+                           CEC_INT_TX_REGISTER_EMPTY);
+                       ++sc->sc_txcur;
+               } else {
+                       CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
+                           CEC_INT_TX_REGISTER_EMPTY);
+               }
+               ++handled;
+       }
+
+       if (int_stat & CEC_INT_TX_FRAME_TRANSMITTED) {
+               CEC_SET_CLEAR(sc, CEC_INT_MASK_REG, 0,
+                   CEC_INT_TX_FRAME_TRANSMITTED |
+                   CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
+               CEC_WRITE(sc, CEC_INT_STAT_REG, CEC_INT_TX_FRAME_TRANSMITTED);
+               if (int_stat & CEC_INT_TX_FRAME_OR_BLOCK_NAKD) {
+                       CEC_WRITE(sc, CEC_INT_STAT_REG,
+                           CEC_INT_TX_FRAME_OR_BLOCK_NAKD);
+                       sc->sc_txerr = ECONNREFUSED;
+                       tegra_cec_reset(sc);
+               }
+               sc->sc_txdone = true;
+               cv_broadcast(&sc->sc_cv);
+               ++handled;
+       }
+
+       if (int_stat & CEC_INT_TX_REGISTER_UNDERRUN) {
+               tegra_cec_reset(sc);
+               cv_broadcast(&sc->sc_cv);
+               ++handled;
+       }
+
+       mutex_exit(&sc->sc_lock);
+
+       return handled;



Home | Main Index | Thread Index | Old Index