Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/i2c Add basic support for Analogix eDP transmitters....



details:   https://anonhg.NetBSD.org/src/rev/ecb35c88dfb1
branches:  trunk
changeset: 448463:ecb35c88dfb1
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun Feb 03 13:17:12 2019 +0000

description:
Add basic support for Analogix eDP transmitters. This driver doesn't
know how to mode set yet -- it only knows how to read EDID from a panel.

diffstat:

 sys/dev/i2c/anxedp.c  |  432 ++++++++++++++++++++++++++++++++++++++++++++++++++
 sys/dev/i2c/files.i2c |    7 +-
 2 files changed, 438 insertions(+), 1 deletions(-)

diffs (truncated from 454 to 300 lines):

diff -r 18460f38de58 -r ecb35c88dfb1 sys/dev/i2c/anxedp.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/i2c/anxedp.c      Sun Feb 03 13:17:12 2019 +0000
@@ -0,0 +1,432 @@
+/* $NetBSD: anxedp.c,v 1.1 2019/02/03 13:17:12 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2019 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 <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: anxedp.c,v 1.1 2019/02/03 13:17:12 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/conf.h>
+
+#include <dev/ic/dw_hdmi.h>
+
+#include <dev/i2c/i2cvar.h>
+#include <dev/i2c/ddcvar.h>
+#include <dev/i2c/ddcreg.h>
+#include <dev/videomode/videomode.h>
+#include <dev/videomode/edidvar.h>
+
+#include <dev/fdt/fdtvar.h>
+#include <dev/fdt/fdt_port.h>
+
+#include <drm/drmP.h>
+#include <drm/drm_crtc.h>
+#include <drm/drm_crtc_helper.h>
+#include <drm/drm_edid.h>
+
+#define        ANX_DP_AUX_CH_CTL_1     0xe5
+#define         ANX_AUX_LENGTH         __BITS(7,4)
+#define         ANX_AUX_TX_COMM        __BITS(3,0)
+#define          ANX_AUX_TX_COMM_MOT   4
+#define          ANX_AUX_TX_COMM_READ  1
+#define        ANX_DP_AUX_ADDR(n)      (0xe6 + (n))
+#define        ANX_DP_AUX_CH_CTL_2     0xe9
+#define         ANX_ADDR_ONLY          __BIT(1)
+#define         ANX_AUX_EN             __BIT(0)
+#define        ANX_BUF_DATA(n)         (0xf0 + (n))
+
+#define        ANX_DP_INT_STA          0xf7
+#define         ANX_RPLY_RECEIV        __BIT(1)
+
+static const struct device_compatible_entry compat_data[] = {
+       { "analogix,anx6345",           1 },
+       { NULL }
+};
+
+struct anxedp_softc;
+
+struct anxedp_connector {
+       struct drm_connector    base;
+       struct anxedp_softc     *sc;
+};
+
+struct anxedp_softc {
+       device_t                sc_dev;
+       i2c_tag_t               sc_i2c;
+       i2c_addr_t              sc_addr;
+       int                     sc_phandle;
+
+       struct anxedp_connector sc_connector;
+       struct drm_bridge       sc_bridge;
+
+       struct fdt_device_ports sc_ports;
+       struct drm_display_mode sc_curmode;
+};
+
+#define        to_anxedp_connector(x)  container_of(x, struct anxedp_connector, base)
+
+static uint8_t
+anxedp_read(struct anxedp_softc *sc, u_int off, uint8_t reg)
+{
+       uint8_t val;
+
+       if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr + off, reg, &val, I2C_F_POLL) != 0)
+               val = 0xff;
+
+       return val;
+}
+
+static void
+anxedp_write(struct anxedp_softc *sc, u_int off, uint8_t reg, uint8_t val)
+{
+       (void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr + off, reg, val, I2C_F_POLL);
+}
+
+static enum drm_connector_status
+anxedp_connector_detect(struct drm_connector *connector, bool force)
+{
+       return connector_status_connected;
+}
+
+static void
+anxedp_connector_destroy(struct drm_connector *connector)
+{
+       drm_connector_unregister(connector);
+       drm_connector_cleanup(connector);
+}
+
+static const struct drm_connector_funcs anxedp_connector_funcs = {
+       .dpms = drm_helper_connector_dpms,
+       .detect = anxedp_connector_detect,
+       .fill_modes = drm_helper_probe_single_connector_modes,
+       .destroy = anxedp_connector_destroy,
+};
+
+static int
+anxedp_aux_wait(struct anxedp_softc *sc)
+{
+       uint8_t val;
+       int retry;
+
+       for (retry = 1000; retry > 0; retry--) {
+               val = anxedp_read(sc, 0, ANX_DP_AUX_CH_CTL_2);
+               if ((val & ANX_AUX_EN) == 0)
+                       break;
+               delay(100);
+       }
+       if (retry == 0) {
+               device_printf(sc->sc_dev, "aux transfer timeout\n");
+               return ETIMEDOUT;
+       }
+
+       for (retry = 1000; retry > 0; retry--) {
+               val = anxedp_read(sc, 1, ANX_DP_INT_STA);
+               if ((val & ANX_RPLY_RECEIV) != 0) {
+                       anxedp_write(sc, 1, ANX_DP_INT_STA, val);
+                       break;
+               }
+               delay(100);
+       }
+       if (retry == 0) {
+               device_printf(sc->sc_dev, "aux transfer timeout\n");
+               return ETIMEDOUT;
+       }
+
+       return 0;
+}
+
+static int
+anxedp_aux_transfer(struct anxedp_softc *sc, uint8_t comm, uint32_t addr,
+    uint8_t *buf, int buflen)
+{
+       uint8_t ctrl[2];
+       int n, error;
+
+       ctrl[0] = __SHIFTIN(comm, ANX_AUX_TX_COMM);
+       if (buflen > 0)
+               ctrl[0] |= __SHIFTIN(buflen - 1, ANX_AUX_LENGTH);
+       ctrl[1] = ANX_AUX_EN;
+       if (buflen == 0)
+               ctrl[1] |= ANX_ADDR_ONLY;
+
+       if (comm != ANX_AUX_TX_COMM_READ) {
+               for (n = 0; n < buflen; n++)
+                       anxedp_write(sc, 0, ANX_BUF_DATA(n), buf[n]);
+       }
+
+       anxedp_write(sc, 0, ANX_DP_AUX_ADDR(0), addr & 0xff);
+       anxedp_write(sc, 0, ANX_DP_AUX_ADDR(1), (addr >> 8) & 0xff);
+       anxedp_write(sc, 0, ANX_DP_AUX_ADDR(2), (addr >> 16) & 0xf);
+       anxedp_write(sc, 0, ANX_DP_AUX_CH_CTL_1, ctrl[0]);
+       anxedp_write(sc, 0, ANX_DP_AUX_CH_CTL_2, ctrl[1]);
+
+       error = anxedp_aux_wait(sc);
+       if (error != 0)
+               return error;
+
+       if (comm == ANX_AUX_TX_COMM_READ) {
+               for (n = 0; n < buflen; n++)
+                       buf[n] = anxedp_read(sc, 0, ANX_BUF_DATA(n));
+       }
+
+       return 0;
+}
+
+static int
+anxedp_read_edid(struct anxedp_softc *sc, uint8_t *edid, int edidlen)
+{
+       int error;
+       uint8_t n;
+
+       for (n = 0; n < edidlen; n += 16) {
+               const int xferlen = MIN(edidlen - n, 16);
+
+               error = anxedp_aux_transfer(sc, ANX_AUX_TX_COMM_MOT, DDC_ADDR, &n, 1);
+               if (error != 0)
+                       return error;
+
+               error = anxedp_aux_transfer(sc, ANX_AUX_TX_COMM_READ, DDC_ADDR, &edid[n], xferlen);
+               if (error != 0)
+                       return error;
+       }
+
+       return 0;
+}
+
+static int
+anxedp_connector_get_modes(struct drm_connector *connector)
+{
+       struct anxedp_connector *anxedp_connector = to_anxedp_connector(connector);
+       struct anxedp_softc * const sc = anxedp_connector->sc;
+       char edid[EDID_LENGTH];
+       struct edid *pedid = NULL;
+       int error;
+
+       iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
+       error = anxedp_read_edid(sc, edid, sizeof(edid));
+       iic_release_bus(sc->sc_i2c, I2C_F_POLL);
+       if (error == 0)
+               pedid = (struct edid *)edid;
+
+       drm_mode_connector_update_edid_property(connector, pedid);
+       if (pedid == NULL)
+               return 0;
+
+       error = drm_add_edid_modes(connector, pedid);
+       drm_edid_to_eld(connector, pedid);
+
+       return error;
+}
+
+static struct drm_encoder *
+anxedp_connector_best_encoder(struct drm_connector *connector)
+{
+       int enc_id = connector->encoder_ids[0];
+       struct drm_mode_object *obj;
+       struct drm_encoder *encoder = NULL;
+
+       if (enc_id) {
+               obj = drm_mode_object_find(connector->dev, enc_id,
+                   DRM_MODE_OBJECT_ENCODER);
+               if (obj == NULL)
+                       return NULL;
+               encoder = obj_to_encoder(obj);
+       }
+
+       return encoder;
+}
+
+static const struct drm_connector_helper_funcs anxedp_connector_helper_funcs = {
+       .get_modes = anxedp_connector_get_modes,
+       .best_encoder = anxedp_connector_best_encoder,
+};
+
+static int
+anxedp_bridge_attach(struct drm_bridge *bridge)
+{
+       struct anxedp_softc * const sc = bridge->driver_private;
+       struct anxedp_connector *anxedp_connector = &sc->sc_connector;
+       struct drm_connector *connector = &anxedp_connector->base;
+       int error;
+
+       anxedp_connector->sc = sc;
+
+       connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
+       connector->interlace_allowed = 0;
+       connector->doublescan_allowed = 0;
+
+       drm_connector_init(bridge->dev, connector, &anxedp_connector_funcs,
+           connector->connector_type);
+       drm_connector_helper_add(connector, &anxedp_connector_helper_funcs);
+
+       error = drm_mode_connector_attach_encoder(connector, bridge->encoder);
+       if (error != 0)
+               return error;
+
+       return drm_connector_register(connector);
+}



Home | Main Index | Thread Index | Old Index