Home | History | Annotate | Line # | Download | only in i2c
anxedp.c revision 1.1
      1 /* $NetBSD: anxedp.c,v 1.1 2019/02/03 13:17:12 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: anxedp.c,v 1.1 2019/02/03 13:17:12 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/conf.h>
     39 
     40 #include <dev/ic/dw_hdmi.h>
     41 
     42 #include <dev/i2c/i2cvar.h>
     43 #include <dev/i2c/ddcvar.h>
     44 #include <dev/i2c/ddcreg.h>
     45 #include <dev/videomode/videomode.h>
     46 #include <dev/videomode/edidvar.h>
     47 
     48 #include <dev/fdt/fdtvar.h>
     49 #include <dev/fdt/fdt_port.h>
     50 
     51 #include <drm/drmP.h>
     52 #include <drm/drm_crtc.h>
     53 #include <drm/drm_crtc_helper.h>
     54 #include <drm/drm_edid.h>
     55 
     56 #define	ANX_DP_AUX_CH_CTL_1	0xe5
     57 #define	 ANX_AUX_LENGTH		__BITS(7,4)
     58 #define	 ANX_AUX_TX_COMM	__BITS(3,0)
     59 #define	  ANX_AUX_TX_COMM_MOT	4
     60 #define	  ANX_AUX_TX_COMM_READ	1
     61 #define	ANX_DP_AUX_ADDR(n)	(0xe6 + (n))
     62 #define	ANX_DP_AUX_CH_CTL_2	0xe9
     63 #define	 ANX_ADDR_ONLY		__BIT(1)
     64 #define	 ANX_AUX_EN		__BIT(0)
     65 #define	ANX_BUF_DATA(n)		(0xf0 + (n))
     66 
     67 #define	ANX_DP_INT_STA		0xf7
     68 #define	 ANX_RPLY_RECEIV	__BIT(1)
     69 
     70 static const struct device_compatible_entry compat_data[] = {
     71 	{ "analogix,anx6345",		1 },
     72 	{ NULL }
     73 };
     74 
     75 struct anxedp_softc;
     76 
     77 struct anxedp_connector {
     78 	struct drm_connector	base;
     79 	struct anxedp_softc	*sc;
     80 };
     81 
     82 struct anxedp_softc {
     83 	device_t		sc_dev;
     84 	i2c_tag_t		sc_i2c;
     85 	i2c_addr_t		sc_addr;
     86 	int			sc_phandle;
     87 
     88 	struct anxedp_connector sc_connector;
     89 	struct drm_bridge	sc_bridge;
     90 
     91 	struct fdt_device_ports	sc_ports;
     92 	struct drm_display_mode	sc_curmode;
     93 };
     94 
     95 #define	to_anxedp_connector(x)	container_of(x, struct anxedp_connector, base)
     96 
     97 static uint8_t
     98 anxedp_read(struct anxedp_softc *sc, u_int off, uint8_t reg)
     99 {
    100 	uint8_t val;
    101 
    102 	if (iic_smbus_read_byte(sc->sc_i2c, sc->sc_addr + off, reg, &val, I2C_F_POLL) != 0)
    103 		val = 0xff;
    104 
    105 	return val;
    106 }
    107 
    108 static void
    109 anxedp_write(struct anxedp_softc *sc, u_int off, uint8_t reg, uint8_t val)
    110 {
    111 	(void)iic_smbus_write_byte(sc->sc_i2c, sc->sc_addr + off, reg, val, I2C_F_POLL);
    112 }
    113 
    114 static enum drm_connector_status
    115 anxedp_connector_detect(struct drm_connector *connector, bool force)
    116 {
    117 	return connector_status_connected;
    118 }
    119 
    120 static void
    121 anxedp_connector_destroy(struct drm_connector *connector)
    122 {
    123 	drm_connector_unregister(connector);
    124 	drm_connector_cleanup(connector);
    125 }
    126 
    127 static const struct drm_connector_funcs anxedp_connector_funcs = {
    128 	.dpms = drm_helper_connector_dpms,
    129 	.detect = anxedp_connector_detect,
    130 	.fill_modes = drm_helper_probe_single_connector_modes,
    131 	.destroy = anxedp_connector_destroy,
    132 };
    133 
    134 static int
    135 anxedp_aux_wait(struct anxedp_softc *sc)
    136 {
    137 	uint8_t val;
    138 	int retry;
    139 
    140 	for (retry = 1000; retry > 0; retry--) {
    141 		val = anxedp_read(sc, 0, ANX_DP_AUX_CH_CTL_2);
    142 		if ((val & ANX_AUX_EN) == 0)
    143 			break;
    144 		delay(100);
    145 	}
    146 	if (retry == 0) {
    147 		device_printf(sc->sc_dev, "aux transfer timeout\n");
    148 		return ETIMEDOUT;
    149 	}
    150 
    151 	for (retry = 1000; retry > 0; retry--) {
    152 		val = anxedp_read(sc, 1, ANX_DP_INT_STA);
    153 		if ((val & ANX_RPLY_RECEIV) != 0) {
    154 			anxedp_write(sc, 1, ANX_DP_INT_STA, val);
    155 			break;
    156 		}
    157 		delay(100);
    158 	}
    159 	if (retry == 0) {
    160 		device_printf(sc->sc_dev, "aux transfer timeout\n");
    161 		return ETIMEDOUT;
    162 	}
    163 
    164 	return 0;
    165 }
    166 
    167 static int
    168 anxedp_aux_transfer(struct anxedp_softc *sc, uint8_t comm, uint32_t addr,
    169     uint8_t *buf, int buflen)
    170 {
    171 	uint8_t ctrl[2];
    172 	int n, error;
    173 
    174 	ctrl[0] = __SHIFTIN(comm, ANX_AUX_TX_COMM);
    175 	if (buflen > 0)
    176 		ctrl[0] |= __SHIFTIN(buflen - 1, ANX_AUX_LENGTH);
    177 	ctrl[1] = ANX_AUX_EN;
    178 	if (buflen == 0)
    179 		ctrl[1] |= ANX_ADDR_ONLY;
    180 
    181 	if (comm != ANX_AUX_TX_COMM_READ) {
    182 		for (n = 0; n < buflen; n++)
    183 			anxedp_write(sc, 0, ANX_BUF_DATA(n), buf[n]);
    184 	}
    185 
    186 	anxedp_write(sc, 0, ANX_DP_AUX_ADDR(0), addr & 0xff);
    187 	anxedp_write(sc, 0, ANX_DP_AUX_ADDR(1), (addr >> 8) & 0xff);
    188 	anxedp_write(sc, 0, ANX_DP_AUX_ADDR(2), (addr >> 16) & 0xf);
    189 	anxedp_write(sc, 0, ANX_DP_AUX_CH_CTL_1, ctrl[0]);
    190 	anxedp_write(sc, 0, ANX_DP_AUX_CH_CTL_2, ctrl[1]);
    191 
    192 	error = anxedp_aux_wait(sc);
    193 	if (error != 0)
    194 		return error;
    195 
    196 	if (comm == ANX_AUX_TX_COMM_READ) {
    197 		for (n = 0; n < buflen; n++)
    198 			buf[n] = anxedp_read(sc, 0, ANX_BUF_DATA(n));
    199 	}
    200 
    201 	return 0;
    202 }
    203 
    204 static int
    205 anxedp_read_edid(struct anxedp_softc *sc, uint8_t *edid, int edidlen)
    206 {
    207 	int error;
    208 	uint8_t n;
    209 
    210 	for (n = 0; n < edidlen; n += 16) {
    211 		const int xferlen = MIN(edidlen - n, 16);
    212 
    213 		error = anxedp_aux_transfer(sc, ANX_AUX_TX_COMM_MOT, DDC_ADDR, &n, 1);
    214 		if (error != 0)
    215 			return error;
    216 
    217 		error = anxedp_aux_transfer(sc, ANX_AUX_TX_COMM_READ, DDC_ADDR, &edid[n], xferlen);
    218 		if (error != 0)
    219 			return error;
    220 	}
    221 
    222 	return 0;
    223 }
    224 
    225 static int
    226 anxedp_connector_get_modes(struct drm_connector *connector)
    227 {
    228 	struct anxedp_connector *anxedp_connector = to_anxedp_connector(connector);
    229 	struct anxedp_softc * const sc = anxedp_connector->sc;
    230 	char edid[EDID_LENGTH];
    231 	struct edid *pedid = NULL;
    232 	int error;
    233 
    234 	iic_acquire_bus(sc->sc_i2c, I2C_F_POLL);
    235 	error = anxedp_read_edid(sc, edid, sizeof(edid));
    236 	iic_release_bus(sc->sc_i2c, I2C_F_POLL);
    237 	if (error == 0)
    238 		pedid = (struct edid *)edid;
    239 
    240 	drm_mode_connector_update_edid_property(connector, pedid);
    241 	if (pedid == NULL)
    242 		return 0;
    243 
    244 	error = drm_add_edid_modes(connector, pedid);
    245 	drm_edid_to_eld(connector, pedid);
    246 
    247 	return error;
    248 }
    249 
    250 static struct drm_encoder *
    251 anxedp_connector_best_encoder(struct drm_connector *connector)
    252 {
    253 	int enc_id = connector->encoder_ids[0];
    254 	struct drm_mode_object *obj;
    255 	struct drm_encoder *encoder = NULL;
    256 
    257 	if (enc_id) {
    258 		obj = drm_mode_object_find(connector->dev, enc_id,
    259 		    DRM_MODE_OBJECT_ENCODER);
    260 		if (obj == NULL)
    261 			return NULL;
    262 		encoder = obj_to_encoder(obj);
    263 	}
    264 
    265 	return encoder;
    266 }
    267 
    268 static const struct drm_connector_helper_funcs anxedp_connector_helper_funcs = {
    269 	.get_modes = anxedp_connector_get_modes,
    270 	.best_encoder = anxedp_connector_best_encoder,
    271 };
    272 
    273 static int
    274 anxedp_bridge_attach(struct drm_bridge *bridge)
    275 {
    276 	struct anxedp_softc * const sc = bridge->driver_private;
    277 	struct anxedp_connector *anxedp_connector = &sc->sc_connector;
    278 	struct drm_connector *connector = &anxedp_connector->base;
    279 	int error;
    280 
    281 	anxedp_connector->sc = sc;
    282 
    283 	connector->polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT;
    284 	connector->interlace_allowed = 0;
    285 	connector->doublescan_allowed = 0;
    286 
    287 	drm_connector_init(bridge->dev, connector, &anxedp_connector_funcs,
    288 	    connector->connector_type);
    289 	drm_connector_helper_add(connector, &anxedp_connector_helper_funcs);
    290 
    291 	error = drm_mode_connector_attach_encoder(connector, bridge->encoder);
    292 	if (error != 0)
    293 		return error;
    294 
    295 	return drm_connector_register(connector);
    296 }
    297 
    298 static void
    299 anxedp_bridge_enable(struct drm_bridge *bridge)
    300 {
    301 }
    302 
    303 static void
    304 anxedp_bridge_pre_enable(struct drm_bridge *bridge)
    305 {
    306 }
    307 
    308 static void
    309 anxedp_bridge_disable(struct drm_bridge *bridge)
    310 {
    311 }
    312 
    313 static void
    314 anxedp_bridge_post_disable(struct drm_bridge *bridge)
    315 {
    316 }
    317 
    318 static void
    319 anxedp_bridge_mode_set(struct drm_bridge *bridge,
    320     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    321 {
    322 	struct anxedp_softc * const sc = bridge->driver_private;
    323 
    324 	sc->sc_curmode = *adjusted_mode;
    325 }
    326 
    327 static bool
    328 anxedp_bridge_mode_fixup(struct drm_bridge *bridge,
    329     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    330 {
    331 	return true;
    332 }
    333 
    334 static const struct drm_bridge_funcs anxedp_bridge_funcs = {
    335 	.attach = anxedp_bridge_attach,
    336 	.enable = anxedp_bridge_enable,
    337 	.pre_enable = anxedp_bridge_pre_enable,
    338 	.disable = anxedp_bridge_disable,
    339 	.post_disable = anxedp_bridge_post_disable,
    340 	.mode_set = anxedp_bridge_mode_set,
    341 	.mode_fixup = anxedp_bridge_mode_fixup,
    342 };
    343 
    344 static int
    345 anxedp_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    346 {
    347 	struct anxedp_softc * const sc = device_private(dev);
    348 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
    349 	struct drm_encoder *encoder;
    350 	struct drm_bridge *bridge;
    351 	int error;
    352 
    353 	if (!activate)
    354 		return EINVAL;
    355 
    356 	if (fdt_endpoint_port_index(ep) != 0)
    357 		return EINVAL;
    358 
    359 	switch (fdt_endpoint_type(in_ep)) {
    360 	case EP_DRM_ENCODER:
    361 		encoder = fdt_endpoint_get_data(in_ep);
    362 		break;
    363 	case EP_DRM_BRIDGE:
    364 		bridge = fdt_endpoint_get_data(in_ep);
    365 		encoder = bridge->encoder;
    366 		break;
    367 	default:
    368 		encoder = NULL;
    369 		break;
    370 	}
    371 
    372 	if (encoder == NULL)
    373 		return EINVAL;
    374 
    375 	sc->sc_connector.base.connector_type = DRM_MODE_CONNECTOR_eDP;
    376 
    377 	sc->sc_bridge.driver_private = sc;
    378 	sc->sc_bridge.funcs = &anxedp_bridge_funcs;
    379 	sc->sc_bridge.encoder = encoder;
    380 
    381 	error = drm_bridge_attach(encoder->dev, &sc->sc_bridge);
    382 	if (error != 0)
    383 		return EIO;
    384 
    385 	encoder->bridge = &sc->sc_bridge;
    386 
    387 	return 0;
    388 }
    389 
    390 static void *
    391 anxedp_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    392 {
    393 	struct anxedp_softc * const sc = device_private(dev);
    394 
    395 	return &sc->sc_bridge;
    396 }
    397 
    398 static int
    399 anxedp_match(device_t parent, cfdata_t match, void *aux)
    400 {
    401 	struct i2c_attach_args *ia = aux;
    402 	int match_result;
    403 
    404 	if (iic_use_direct_match(ia, match, compat_data, &match_result))
    405 		return match_result;
    406 
    407 	/* This device is direct-config only */
    408 
    409 	return 0;
    410 }
    411 
    412 static void
    413 anxedp_attach(device_t parent, device_t self, void *aux)
    414 {
    415 	struct anxedp_softc * const sc = device_private(self);
    416 	struct i2c_attach_args * const ia = aux;
    417 
    418 	sc->sc_dev = self;
    419 	sc->sc_i2c = ia->ia_tag;
    420 	sc->sc_addr = ia->ia_addr;
    421 	sc->sc_phandle = ia->ia_cookie;
    422 
    423 	aprint_naive("\n");
    424 	aprint_normal(": eDP TX\n");
    425 
    426 	sc->sc_ports.dp_ep_activate = anxedp_ep_activate;
    427 	sc->sc_ports.dp_ep_get_data = anxedp_ep_get_data;
    428 	fdt_ports_register(&sc->sc_ports, self, sc->sc_phandle, EP_DRM_BRIDGE);
    429 }
    430 
    431 CFATTACH_DECL_NEW(anxedp, sizeof(struct anxedp_softc),
    432     anxedp_match, anxedp_attach, NULL, NULL);
    433