Home | History | Annotate | Line # | Download | only in sunxi
sunxi_hdmi.c revision 1.11
      1 /* $NetBSD: sunxi_hdmi.c,v 1.11 2021/01/18 02:35:49 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2014 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 "opt_ddb.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: sunxi_hdmi.c,v 1.11 2021/01/18 02:35:49 thorpej Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/kmem.h>
     39 #include <sys/systm.h>
     40 #include <sys/kernel.h>
     41 #include <sys/proc.h>
     42 #include <sys/mutex.h>
     43 #include <sys/kthread.h>
     44 
     45 #include <dev/fdt/fdtvar.h>
     46 #include <dev/fdt/fdt_port.h>
     47 
     48 #include <dev/i2c/i2cvar.h>
     49 #include <dev/i2c/ddcvar.h>
     50 #include <dev/i2c/ddcreg.h>
     51 #include <dev/videomode/videomode.h>
     52 #include <dev/videomode/edidvar.h>
     53 
     54 #include <arm/sunxi/sunxi_hdmireg.h>
     55 #include <arm/sunxi/sunxi_display.h>
     56 
     57 enum sunxi_hdmi_type {
     58 	HDMI_A10 = 1,
     59 	HDMI_A31,
     60 };
     61 
     62 struct sunxi_hdmi_softc {
     63 	device_t sc_dev;
     64 	int sc_phandle;
     65 	enum sunxi_hdmi_type sc_type;
     66 	bus_space_tag_t sc_bst;
     67 	bus_space_handle_t sc_bsh;
     68 	struct clk *sc_clk_ahb;
     69 	struct clk *sc_clk_mod;
     70 	struct clk *sc_clk_pll0;
     71 	struct clk *sc_clk_pll1;
     72 	void *sc_ih;
     73 	lwp_t *sc_thread;
     74 
     75 	struct i2c_controller sc_ic;
     76 	kmutex_t sc_exec_lock;
     77 
     78 	bool sc_display_connected;
     79 	char sc_display_vendor[16];
     80 	char sc_display_product[16];
     81 
     82 	u_int sc_display_mode;
     83 	u_int sc_current_display_mode;
     84 #define DISPLAY_MODE_AUTO	0
     85 #define DISPLAY_MODE_HDMI	1
     86 #define DISPLAY_MODE_DVI	2
     87 
     88 	kmutex_t sc_pwr_lock;
     89 	int	sc_pwr_refcount; /* reference who needs HDMI */
     90 
     91 	uint32_t sc_ver;
     92 	unsigned int sc_i2c_blklen;
     93 
     94 	struct fdt_device_ports sc_ports;
     95 	struct fdt_endpoint *sc_in_ep;
     96 	struct fdt_endpoint *sc_in_rep;
     97 	struct fdt_endpoint *sc_out_ep;
     98 };
     99 
    100 #define HDMI_READ(sc, reg)			\
    101     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    102 #define HDMI_WRITE(sc, reg, val)		\
    103     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val));
    104 
    105 #define HDMI_1_3_P(sc)	((sc)->sc_ver == 0x00010003)
    106 #define HDMI_1_4_P(sc)	((sc)->sc_ver == 0x00010004)
    107 
    108 static const struct device_compatible_entry compat_data[] = {
    109 	{ .compat = "allwinner,sun4i-a10-hdmi", .value = HDMI_A10},
    110 	{ .compat = "allwinner,sun7i-a20-hdmi", .value = HDMI_A10},
    111 
    112 	{ 0 }
    113 };
    114 
    115 static int	sunxi_hdmi_match(device_t, cfdata_t, void *);
    116 static void	sunxi_hdmi_attach(device_t, device_t, void *);
    117 static void	sunxi_hdmi_i2c_init(struct sunxi_hdmi_softc *);
    118 static int	sunxi_hdmi_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    119 				   size_t, void *, size_t, int);
    120 static int	sunxi_hdmi_i2c_xfer(void *, i2c_addr_t, uint8_t, uint8_t,
    121 				   size_t, int, int);
    122 static int	sunxi_hdmi_i2c_reset(struct sunxi_hdmi_softc *, int);
    123 
    124 static int	sunxi_hdmi_ep_activate(device_t, struct fdt_endpoint *, bool);
    125 static int	sunxi_hdmi_ep_enable(device_t, struct fdt_endpoint *, bool);
    126 static void	sunxi_hdmi_do_enable(struct sunxi_hdmi_softc *);
    127 static void	sunxi_hdmi_read_edid(struct sunxi_hdmi_softc *);
    128 static int	sunxi_hdmi_read_edid_block(struct sunxi_hdmi_softc *, uint8_t *,
    129 					  uint8_t);
    130 static u_int	sunxi_hdmi_get_display_mode(struct sunxi_hdmi_softc *,
    131 					   const struct edid_info *);
    132 static void	sunxi_hdmi_video_enable(struct sunxi_hdmi_softc *, bool);
    133 static void	sunxi_hdmi_set_videomode(struct sunxi_hdmi_softc *,
    134 					const struct videomode *, u_int);
    135 static void	sunxi_hdmi_set_audiomode(struct sunxi_hdmi_softc *,
    136 					const struct videomode *, u_int);
    137 static void	sunxi_hdmi_hpd(struct sunxi_hdmi_softc *);
    138 static void	sunxi_hdmi_thread(void *);
    139 static int	sunxi_hdmi_poweron(struct sunxi_hdmi_softc *, bool);
    140 #if 0
    141 static int	sunxi_hdmi_intr(void *);
    142 #endif
    143 
    144 #if defined(DDB)
    145 void		sunxi_hdmi_dump_regs(void);
    146 #endif
    147 
    148 CFATTACH_DECL_NEW(sunxi_hdmi, sizeof(struct sunxi_hdmi_softc),
    149 	sunxi_hdmi_match, sunxi_hdmi_attach, NULL, NULL);
    150 
    151 static int
    152 sunxi_hdmi_match(device_t parent, cfdata_t cf, void *aux)
    153 {
    154 	struct fdt_attach_args * const faa = aux;
    155 
    156 	return of_match_compat_data(faa->faa_phandle, compat_data);
    157 }
    158 
    159 static void
    160 sunxi_hdmi_attach(device_t parent, device_t self, void *aux)
    161 {
    162 	struct sunxi_hdmi_softc *sc = device_private(self);
    163 	struct fdt_attach_args * const faa = aux;
    164 	const int phandle = faa->faa_phandle;
    165 	bus_addr_t addr;
    166 	bus_size_t size;
    167 	uint32_t ver;
    168 
    169 	sc->sc_dev = self;
    170 	sc->sc_phandle = phandle;
    171 	sc->sc_bst = faa->faa_bst;
    172 
    173 	sc->sc_type =
    174 	    of_search_compatible(faa->faa_phandle, compat_data)->value;
    175 
    176 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    177 		aprint_error(": couldn't get registers\n");
    178 	}
    179 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    180 		aprint_error(": couldn't map registers\n");
    181 		return;
    182 	}
    183 
    184 	sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
    185 	sc->sc_clk_mod = fdtbus_clock_get(phandle, "mod");
    186 	sc->sc_clk_pll0 = fdtbus_clock_get(phandle, "pll-0");
    187 	sc->sc_clk_pll1 = fdtbus_clock_get(phandle, "pll-1");
    188 
    189 	if (sc->sc_clk_ahb == NULL || sc->sc_clk_mod == NULL
    190 	    || sc->sc_clk_pll0 == NULL || sc->sc_clk_pll1 == NULL) {
    191 		aprint_error(": couldn't get clocks\n");
    192 		aprint_debug_dev(self, "clk ahb %s mod %s pll-0 %s pll-1 %s\n",
    193 		    sc->sc_clk_ahb == NULL ? "missing" : "present",
    194 		    sc->sc_clk_mod == NULL ? "missing" : "present",
    195 		    sc->sc_clk_pll0 == NULL ? "missing" : "present",
    196 		    sc->sc_clk_pll1 == NULL ? "missing" : "present");
    197 		return;
    198 	}
    199 
    200 	if (clk_enable(sc->sc_clk_ahb) != 0) {
    201 		aprint_error(": couldn't enable ahb clock\n");
    202 		return;
    203 	}
    204 	ver = HDMI_READ(sc, SUNXI_HDMI_VERSION_ID_REG);
    205 
    206 	const int vmaj = __SHIFTOUT(ver, SUNXI_HDMI_VERSION_ID_H);
    207 	const int vmin = __SHIFTOUT(ver, SUNXI_HDMI_VERSION_ID_L);
    208 
    209 	aprint_naive("\n");
    210 	aprint_normal(": HDMI %d.%d\n", vmaj, vmin);
    211 
    212 	sc->sc_ver = ver;
    213 	sc->sc_i2c_blklen = 16;
    214 
    215 	sc->sc_ports.dp_ep_activate = sunxi_hdmi_ep_activate;
    216 	sc->sc_ports.dp_ep_enable = sunxi_hdmi_ep_enable;
    217 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_OTHER);
    218 
    219 	mutex_init(&sc->sc_pwr_lock, MUTEX_DEFAULT, IPL_NONE);
    220 	sunxi_hdmi_i2c_init(sc);
    221 }
    222 
    223 void
    224 sunxi_hdmi_doreset(void)
    225 {
    226 	device_t dev;
    227 	struct sunxi_hdmi_softc *sc;
    228 	int error;
    229 
    230 	for (int i = 0;;i++) {
    231 		dev = device_find_by_driver_unit("sunxihdmi", i);
    232 		if (dev == NULL)
    233 			return;
    234 		sc = device_private(dev);
    235 
    236 		error = clk_disable(sc->sc_clk_mod);
    237 		if (error) {
    238 			aprint_error_dev(dev, ": couldn't disable mod clock\n");
    239 			return;
    240 		}
    241 
    242 #if defined(SUNXI_HDMI_DEBUG)
    243 		sunxi_hdmi_dump_regs();
    244 #endif
    245 
    246 		/*
    247 		 * reset device, in case it has been setup by firmware in an
    248 		 * incompatible way
    249 		 */
    250 		for (int j = 0; j <= 0x500; j += 4) {
    251 			HDMI_WRITE(sc, j, 0);
    252 		}
    253 
    254 		if (clk_disable(sc->sc_clk_ahb) != 0) {
    255 			aprint_error_dev(dev, ": couldn't disable ahb clock\n");
    256 			return;
    257 		}
    258 	}
    259 }
    260 
    261 static void
    262 sunxi_hdmi_i2c_init(struct sunxi_hdmi_softc *sc)
    263 {
    264 	struct i2c_controller *ic = &sc->sc_ic;
    265 
    266 	mutex_init(&sc->sc_exec_lock, MUTEX_DEFAULT, IPL_NONE);
    267 
    268 	iic_tag_init(ic);
    269 	ic->ic_cookie = sc;
    270 	ic->ic_exec = sunxi_hdmi_i2c_exec;
    271 }
    272 
    273 static int
    274 sunxi_hdmi_i2c_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
    275     const void *cmdbuf, size_t cmdlen, void *buf, size_t len, int flags)
    276 {
    277 	struct sunxi_hdmi_softc *sc = priv;
    278 	uint8_t *pbuf;
    279 	uint8_t block;
    280 	int resid;
    281 	off_t off;
    282 	int err;
    283 
    284 	mutex_enter(&sc->sc_exec_lock);
    285 
    286 	KASSERT(op == I2C_OP_READ_WITH_STOP);
    287 	KASSERT(addr == DDC_ADDR);
    288 	KASSERT(cmdlen > 0);
    289 	KASSERT(buf != NULL);
    290 
    291 	err = sunxi_hdmi_i2c_reset(sc, flags);
    292 	if (err)
    293 		goto done;
    294 
    295 	block = *(const uint8_t *)cmdbuf;
    296 	off = (block & 1) ? 128 : 0;
    297 
    298 	pbuf = buf;
    299 	resid = len;
    300 	while (resid > 0) {
    301 		size_t blklen = uimin(resid, sc->sc_i2c_blklen);
    302 
    303 		err = sunxi_hdmi_i2c_xfer(sc, addr, block >> 1, off, blklen,
    304 		      SUNXI_HDMI_DDC_COMMAND_ACCESS_CMD_EOREAD, flags);
    305 		if (err)
    306 			goto done;
    307 
    308 		if (HDMI_1_3_P(sc)) {
    309 			bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh,
    310 			    SUNXI_HDMI_DDC_FIFO_ACCESS_REG, pbuf, blklen);
    311 		} else {
    312 			bus_space_read_multi_1(sc->sc_bst, sc->sc_bsh,
    313 			    SUNXI_A31_HDMI_DDC_FIFO_ACCESS_REG, pbuf, blklen);
    314 		}
    315 
    316 #ifdef SUNXI_HDMI_DEBUG
    317 		printf("off=%d:", (int)off);
    318 		for (int i = 0; i < blklen; i++)
    319 			printf(" %02x", pbuf[i]);
    320 		printf("\n");
    321 #endif
    322 
    323 		pbuf += blklen;
    324 		off += blklen;
    325 		resid -= blklen;
    326 	}
    327 
    328 done:
    329 	mutex_exit(&sc->sc_exec_lock);
    330 	return err;
    331 }
    332 
    333 static int
    334 sunxi_hdmi_i2c_xfer_1_3(void *priv, i2c_addr_t addr, uint8_t block, uint8_t reg,
    335     size_t len, int type, int flags)
    336 {
    337 	struct sunxi_hdmi_softc *sc = priv;
    338 	uint32_t val;
    339 	int retry;
    340 
    341 	val = HDMI_READ(sc, SUNXI_HDMI_DDC_CTRL_REG);
    342 	val &= ~SUNXI_HDMI_DDC_CTRL_FIFO_DIR;
    343 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_CTRL_REG, val);
    344 
    345 	val |= __SHIFTIN(block, SUNXI_HDMI_DDC_SLAVE_ADDR_0);
    346 	val |= __SHIFTIN(0x60, SUNXI_HDMI_DDC_SLAVE_ADDR_1);
    347 	val |= __SHIFTIN(reg, SUNXI_HDMI_DDC_SLAVE_ADDR_2);
    348 	val |= __SHIFTIN(addr, SUNXI_HDMI_DDC_SLAVE_ADDR_3);
    349 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_SLAVE_ADDR_REG, val);
    350 
    351 	val = HDMI_READ(sc, SUNXI_HDMI_DDC_FIFO_CTRL_REG);
    352 	val |= SUNXI_HDMI_DDC_FIFO_CTRL_ADDR_CLEAR;
    353 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_FIFO_CTRL_REG, val);
    354 
    355 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_BYTE_COUNTER_REG, len);
    356 
    357 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_COMMAND_REG, type);
    358 
    359 	val = HDMI_READ(sc, SUNXI_HDMI_DDC_CTRL_REG);
    360 	val |= SUNXI_HDMI_DDC_CTRL_ACCESS_CMD_START;
    361 	HDMI_WRITE(sc, SUNXI_HDMI_DDC_CTRL_REG, val);
    362 
    363 	retry = 1000;
    364 	while (--retry > 0) {
    365 		val = HDMI_READ(sc, SUNXI_HDMI_DDC_CTRL_REG);
    366 		if ((val & SUNXI_HDMI_DDC_CTRL_ACCESS_CMD_START) == 0)
    367 			break;
    368 		delay(1000);
    369 	}
    370 	if (retry == 0)
    371 		return ETIMEDOUT;
    372 
    373 	val = HDMI_READ(sc, SUNXI_HDMI_DDC_INT_STATUS_REG);
    374 	if ((val & SUNXI_HDMI_DDC_INT_STATUS_TRANSFER_COMPLETE) == 0) {
    375 		device_printf(sc->sc_dev, "xfer failed, status=%08x\n", val);
    376 		return EIO;
    377 	}
    378 
    379 	return 0;
    380 }
    381 
    382 static int
    383 sunxi_hdmi_i2c_xfer_1_4(void *priv, i2c_addr_t addr, uint8_t block, uint8_t reg,
    384     size_t len, int type, int flags)
    385 {
    386 	struct sunxi_hdmi_softc *sc = priv;
    387 	uint32_t val;
    388 	int retry;
    389 
    390 	val = HDMI_READ(sc, SUNXI_A31_HDMI_DDC_FIFO_CTRL_REG);
    391 	val |= SUNXI_A31_HDMI_DDC_FIFO_CTRL_RST;
    392 	HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_FIFO_CTRL_REG, val);
    393 
    394 	val = __SHIFTIN(block, SUNXI_A31_HDMI_DDC_SLAVE_ADDR_SEG_PTR);
    395 	val |= __SHIFTIN(0x60, SUNXI_A31_HDMI_DDC_SLAVE_ADDR_DDC_CMD);
    396 	val |= __SHIFTIN(reg, SUNXI_A31_HDMI_DDC_SLAVE_ADDR_OFF_ADR);
    397 	val |= __SHIFTIN(addr, SUNXI_A31_HDMI_DDC_SLAVE_ADDR_DEV_ADR);
    398 	HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_SLAVE_ADDR_REG, val);
    399 
    400 	HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_COMMAND_REG,
    401 	    __SHIFTIN(len, SUNXI_A31_HDMI_DDC_COMMAND_DTC) |
    402 	    __SHIFTIN(type, SUNXI_A31_HDMI_DDC_COMMAND_CMD));
    403 
    404 	val = HDMI_READ(sc, SUNXI_A31_HDMI_DDC_CTRL_REG);
    405 	val |= SUNXI_A31_HDMI_DDC_CTRL_ACCESS_CMD_START;
    406 	HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_CTRL_REG, val);
    407 
    408 	retry = 1000;
    409 	while (--retry > 0) {
    410 		val = HDMI_READ(sc, SUNXI_A31_HDMI_DDC_CTRL_REG);
    411 		if ((val & SUNXI_A31_HDMI_DDC_CTRL_ACCESS_CMD_START) == 0)
    412 			break;
    413 		if (flags & I2C_F_POLL)
    414 			delay(1000);
    415 		else
    416 			kpause("hdmiddc", false, mstohz(10), &sc->sc_exec_lock);
    417 	}
    418 	if (retry == 0)
    419 		return ETIMEDOUT;
    420 
    421 	return 0;
    422 }
    423 
    424 static int
    425 sunxi_hdmi_i2c_xfer(void *priv, i2c_addr_t addr, uint8_t block, uint8_t reg,
    426     size_t len, int type, int flags)
    427 {
    428 	struct sunxi_hdmi_softc *sc = priv;
    429 	int rv;
    430 
    431 	if (HDMI_1_3_P(sc)) {
    432 		rv = sunxi_hdmi_i2c_xfer_1_3(priv, addr, block, reg, len,
    433 		    type, flags);
    434 	} else {
    435 		rv = sunxi_hdmi_i2c_xfer_1_4(priv, addr, block, reg, len,
    436 		    type, flags);
    437 	}
    438 
    439 	return rv;
    440 }
    441 
    442 static int
    443 sunxi_hdmi_i2c_reset(struct sunxi_hdmi_softc *sc, int flags)
    444 {
    445 	uint32_t hpd, ctrl;
    446 
    447 	hpd = HDMI_READ(sc, SUNXI_HDMI_HPD_REG);
    448 	if ((hpd & SUNXI_HDMI_HPD_HOTPLUG_DET) == 0) {
    449 		device_printf(sc->sc_dev, "no device detected\n");
    450 		return ENODEV;	/* no device plugged in */
    451 	}
    452 
    453 	if (HDMI_1_3_P(sc)) {
    454 		HDMI_WRITE(sc, SUNXI_HDMI_DDC_FIFO_CTRL_REG, 0);
    455 		HDMI_WRITE(sc, SUNXI_HDMI_DDC_CTRL_REG,
    456 		    SUNXI_HDMI_DDC_CTRL_EN | SUNXI_HDMI_DDC_CTRL_SW_RST);
    457 
    458 		delay(1000);
    459 
    460 		ctrl = HDMI_READ(sc, SUNXI_HDMI_DDC_CTRL_REG);
    461 		if (ctrl & SUNXI_HDMI_DDC_CTRL_SW_RST) {
    462 			device_printf(sc->sc_dev, "reset failed (1.3)\n");
    463 			return EBUSY;
    464 		}
    465 
    466 		/* N=5,M=1 */
    467 		HDMI_WRITE(sc, SUNXI_HDMI_DDC_CLOCK_REG,
    468 		    __SHIFTIN(5, SUNXI_HDMI_DDC_CLOCK_N) |
    469 		    __SHIFTIN(1, SUNXI_HDMI_DDC_CLOCK_M));
    470 
    471 		HDMI_WRITE(sc, SUNXI_HDMI_DDC_DBG_REG, 0x300);
    472 	} else {
    473 		HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_CTRL_REG,
    474 		    SUNXI_A31_HDMI_DDC_CTRL_SW_RST);
    475 
    476 		/* N=1,M=12 */
    477 		HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_CLOCK_REG,
    478 		    __SHIFTIN(1, SUNXI_HDMI_DDC_CLOCK_N) |
    479 		    __SHIFTIN(12, SUNXI_HDMI_DDC_CLOCK_M));
    480 
    481 		HDMI_WRITE(sc, SUNXI_A31_HDMI_DDC_CTRL_REG,
    482 		    SUNXI_A31_HDMI_DDC_CTRL_SDA_PAD_EN |
    483 		    SUNXI_A31_HDMI_DDC_CTRL_SCL_PAD_EN |
    484 		    SUNXI_A31_HDMI_DDC_CTRL_EN);
    485 	}
    486 
    487 	return 0;
    488 }
    489 
    490 static int
    491 sunxi_hdmi_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    492 {
    493 	struct sunxi_hdmi_softc *sc = device_private(dev);
    494 	struct fdt_endpoint *in_ep, *out_ep;
    495 	int error;
    496 
    497 	/* our input is activated by tcon, we activate our output */
    498 	if (fdt_endpoint_port_index(ep) != SUNXI_PORT_INPUT) {
    499 		panic("sunxi_hdmi_ep_activate: port %d",
    500 		    fdt_endpoint_port_index(ep));
    501 	}
    502 
    503 	if (!activate)
    504 		return EOPNOTSUPP;
    505 
    506 	/* check that out other input is not active */
    507 	switch (fdt_endpoint_index(ep)) {
    508 	case 0:
    509 		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    510 		    SUNXI_PORT_INPUT, 1);
    511 		break;
    512 	case 1:
    513 		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    514 		    SUNXI_PORT_INPUT, 0);
    515 		break;
    516 	default:
    517 		in_ep = NULL;
    518 		panic("sunxi_hdmi_ep_activate: input index %d",
    519 		    fdt_endpoint_index(ep));
    520 	}
    521 	if (in_ep != NULL) {
    522 		if (fdt_endpoint_is_active(in_ep))
    523 			return EBUSY;
    524 	}
    525 	/* only one output */
    526 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    527 		   SUNXI_PORT_OUTPUT, 0);
    528 	if (out_ep == NULL) {
    529 		aprint_error_dev(dev, "no output endpoint\n");
    530 		return ENODEV;
    531 	}
    532 	error = fdt_endpoint_activate(out_ep, activate);
    533 	if (error == 0) {
    534 		sc->sc_in_ep = ep;
    535 		sc->sc_in_rep = fdt_endpoint_remote(ep);
    536 		sc->sc_out_ep = out_ep;
    537 		sunxi_hdmi_do_enable(sc);
    538 		return 0;
    539 	}
    540 	return error;
    541 }
    542 
    543 static int
    544 sunxi_hdmi_ep_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
    545 {
    546 	struct sunxi_hdmi_softc *sc = device_private(dev);
    547 	int error;
    548 
    549 	if (fdt_endpoint_port_index(ep) == SUNXI_PORT_INPUT) {
    550 		KASSERT(ep == sc->sc_in_ep);
    551 		if (sc->sc_thread == NULL) {
    552 			if (enable) {
    553 				delay(50000);
    554 				mutex_enter(&sc->sc_pwr_lock);
    555 				sunxi_hdmi_hpd(sc);
    556 				mutex_exit(&sc->sc_pwr_lock);
    557 				kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    558 				    sunxi_hdmi_thread, sc, &sc->sc_thread, "%s",
    559 				    device_xname(dev));
    560 			}
    561 			return 0;
    562 		} else {
    563 			mutex_enter(&sc->sc_pwr_lock);
    564 			error = sunxi_hdmi_poweron(sc, enable);
    565 			mutex_exit(&sc->sc_pwr_lock);
    566 			return error;
    567 		}
    568 	}
    569 	panic("sunxi_hdmi_ep_enable");
    570 }
    571 
    572 static void
    573 sunxi_hdmi_do_enable(struct sunxi_hdmi_softc *sc)
    574 {
    575 	/* complete attach */
    576 	struct clk *clk;
    577 	int error;
    578 	uint32_t dbg0_reg;
    579 
    580 	if (clk_enable(sc->sc_clk_ahb) != 0) {
    581 		aprint_error_dev(sc->sc_dev, "couldn't enable ahb clock\n");
    582 		return;
    583 	}
    584 	/* assume tcon0 uses pll3, tcon1 uses pll7 */
    585 	switch(fdt_endpoint_index(sc->sc_in_ep)) {
    586 	case 0:
    587 		clk = sc->sc_clk_pll0;
    588 		dbg0_reg = (0<<21);
    589 		break;
    590 	case 1:
    591 		clk = sc->sc_clk_pll1;
    592 		dbg0_reg = (1<<21);
    593 		break;
    594 	default:
    595 		panic("sunxi_hdmi pll");
    596 	}
    597 	error = clk_set_rate(clk, 270000000);
    598 	if (error) {
    599 		clk = clk_get_parent(clk);
    600 		/* probably because this is pllx2 */
    601 		error = clk_set_rate(clk, 270000000);
    602 	}
    603 	if (error) {
    604 		aprint_error_dev(sc->sc_dev, ": couldn't init pll clock\n");
    605 		return;
    606 	}
    607 	error = clk_set_parent(sc->sc_clk_mod, clk);
    608 	if (error) {
    609 		aprint_error_dev(sc->sc_dev, ": couldn't set mod clock parent\n");
    610 		return;
    611 	}
    612 	error = clk_enable(sc->sc_clk_mod);
    613 	if (error) {
    614 		aprint_error_dev(sc->sc_dev, ": couldn't enable mod clock\n");
    615 		return;
    616 	}
    617 	delay(1000);
    618 
    619 	HDMI_WRITE(sc, SUNXI_HDMI_CTRL_REG, SUNXI_HDMI_CTRL_MODULE_EN);
    620 	delay(1000);
    621 	if (sc->sc_type == HDMI_A10) {
    622 		HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL0_REG, 0xfe800000);
    623 		HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL1_REG, 0x00d8c830);
    624 	} else if (sc->sc_type == HDMI_A31) {
    625 		HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL0_REG, 0x7e80000f);
    626 		HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL1_REG, 0x01ded030);
    627 	}
    628 	HDMI_WRITE(sc, SUNXI_HDMI_PLL_DBG0_REG, dbg0_reg);
    629 	delay(1000);
    630 }
    631 
    632 #define EDID_BLOCK_SIZE 128
    633 
    634 static int
    635 sunxi_hdmi_read_edid_block(struct sunxi_hdmi_softc *sc, uint8_t *data,
    636     uint8_t block)
    637 {
    638 	i2c_tag_t tag = &sc->sc_ic;
    639 	uint8_t wbuf[2];
    640 	int error;
    641 
    642 	if ((error = iic_acquire_bus(tag, 0)) != 0)
    643 		return error;
    644 
    645 	wbuf[0] = block;	/* start address */
    646 
    647 	error = iic_exec(tag, I2C_OP_READ_WITH_STOP, DDC_ADDR, wbuf, 1,
    648 	    data, EDID_BLOCK_SIZE, 0);
    649 	iic_release_bus(tag, 0);
    650 	return error;
    651 }
    652 
    653 static void
    654 sunxi_hdmi_read_edid(struct sunxi_hdmi_softc *sc)
    655 {
    656 	const struct videomode *mode;
    657 	char *edid;
    658 	struct edid_info *eip;
    659 	int retry = 4;
    660 	u_int display_mode;
    661 
    662 	edid = kmem_zalloc(EDID_BLOCK_SIZE, KM_SLEEP);
    663 	eip = kmem_zalloc(sizeof(struct edid_info), KM_SLEEP);
    664 
    665 	while (--retry > 0) {
    666 		if (!sunxi_hdmi_read_edid_block(sc, edid, 0))
    667 			break;
    668 	}
    669 	if (retry == 0) {
    670 		device_printf(sc->sc_dev, "failed to read EDID\n");
    671 	} else {
    672 		if (edid_parse(edid, eip) != 0) {
    673 			device_printf(sc->sc_dev, "failed to parse EDID\n");
    674 		}
    675 #ifdef SUNXI_HDMI_DEBUG
    676 		else {
    677 			edid_print(eip);
    678 		}
    679 #endif
    680 	}
    681 
    682 	if (sc->sc_display_mode == DISPLAY_MODE_AUTO)
    683 		display_mode = sunxi_hdmi_get_display_mode(sc, eip);
    684 	else
    685 		display_mode = sc->sc_display_mode;
    686 
    687 	const char *forced = sc->sc_display_mode == DISPLAY_MODE_AUTO ?
    688 	    "auto-detected" : "forced";
    689 	device_printf(sc->sc_dev, "%s mode (%s)\n",
    690 	    display_mode == DISPLAY_MODE_HDMI ? "HDMI" : "DVI", forced);
    691 
    692 	strlcpy(sc->sc_display_vendor, eip->edid_vendorname,
    693 	    sizeof(sc->sc_display_vendor));
    694 	strlcpy(sc->sc_display_product, eip->edid_productname,
    695 	    sizeof(sc->sc_display_product));
    696 	sc->sc_current_display_mode = display_mode;
    697 
    698 	mode = eip->edid_preferred_mode;
    699 	if (mode == NULL)
    700 		mode = pick_mode_by_ref(640, 480, 60);
    701 
    702 	if (mode != NULL) {
    703 		sunxi_hdmi_video_enable(sc, false);
    704 		fdt_endpoint_enable(sc->sc_in_ep, false);
    705 		delay(20000);
    706 
    707 		sunxi_tcon1_set_videomode(
    708 		    fdt_endpoint_device(sc->sc_in_rep), mode);
    709 		sunxi_hdmi_set_videomode(sc, mode, display_mode);
    710 		sunxi_hdmi_set_audiomode(sc, mode, display_mode);
    711 		fdt_endpoint_enable(sc->sc_in_ep, true);
    712 		delay(20000);
    713 		sunxi_hdmi_video_enable(sc, true);
    714 	}
    715 	kmem_free(edid, EDID_BLOCK_SIZE);
    716 	kmem_free(eip, sizeof(struct edid_info));
    717 }
    718 
    719 static u_int
    720 sunxi_hdmi_get_display_mode(struct sunxi_hdmi_softc *sc,
    721     const struct edid_info *ei)
    722 {
    723 	char *edid;
    724 	bool found_hdmi = false;
    725 	unsigned int n, p;
    726 	edid = kmem_zalloc(EDID_BLOCK_SIZE, KM_SLEEP);
    727 
    728 	/*
    729 	 * Scan through extension blocks, looking for a CEA-861-D v3
    730 	 * block. If an HDMI Vendor-Specific Data Block (HDMI VSDB) is
    731 	 * found in that, assume HDMI mode.
    732 	 */
    733 	for (n = 1; n <= MIN(ei->edid_ext_block_count, 4); n++) {
    734 		if (sunxi_hdmi_read_edid_block(sc, edid, n)) {
    735 #ifdef SUNXI_HDMI_DEBUG
    736 			device_printf(sc->sc_dev,
    737 			    "Failed to read EDID block %d\n", n);
    738 #endif
    739 			break;
    740 		}
    741 
    742 #ifdef SUNXI_HDMI_DEBUG
    743 		device_printf(sc->sc_dev, "EDID block #%d:\n", n);
    744 #endif
    745 
    746 		const uint8_t tag = edid[0];
    747 		const uint8_t rev = edid[1];
    748 		const uint8_t off = edid[2];
    749 
    750 #ifdef SUNXI_HDMI_DEBUG
    751 		device_printf(sc->sc_dev, "  Tag %d, Revision %d, Offset %d\n",
    752 		    tag, rev, off);
    753 		device_printf(sc->sc_dev, "  Flags: 0x%02x\n", edid[3]);
    754 #endif
    755 
    756 		/* We are looking for a CEA-861-D tag (02h) with revision 3 */
    757 		if (tag != 0x02 || rev != 3)
    758 			continue;
    759 		/*
    760 		 * CEA data block collection starts at byte 4, so the
    761 		 * DTD blocks must start after it.
    762 		 */
    763 		if (off <= 4)
    764 			continue;
    765 
    766 		/* Parse the CEA data blocks */
    767 		for (p = 4; p < off;) {
    768 			const uint8_t btag = (edid[p] >> 5) & 0x7;
    769 			const uint8_t blen = edid[p] & 0x1f;
    770 
    771 #ifdef SUNXI_HDMI_DEBUG
    772 			device_printf(sc->sc_dev, "  CEA data block @ %d\n", p);
    773 			device_printf(sc->sc_dev, "    Tag %d, Length %d\n",
    774 			    btag, blen);
    775 #endif
    776 
    777 			/* Make sure the length is sane */
    778 			if (p + blen + 1 > off)
    779 				break;
    780 			/* Looking for a VSDB tag */
    781 			if (btag != 3)
    782 				goto next_block;
    783 			/* HDMI VSDB is at least 5 bytes long */
    784 			if (blen < 5)
    785 				goto next_block;
    786 
    787 #ifdef SUNXI_HDMI_DEBUG
    788 			device_printf(sc->sc_dev, "    ID: %02x%02x%02x\n",
    789 			    edid[p + 1], edid[p + 2], edid[p + 3]);
    790 #endif
    791 
    792 			/* HDMI 24-bit IEEE registration ID is 0x000C03 */
    793 			if (memcmp(&edid[p + 1], "\x03\x0c\x00", 3) == 0)
    794 				found_hdmi = true;
    795 
    796 next_block:
    797 			p += (1 + blen);
    798 		}
    799 	}
    800 
    801 	kmem_free(edid, EDID_BLOCK_SIZE);
    802 	return found_hdmi ? DISPLAY_MODE_HDMI : DISPLAY_MODE_DVI;
    803 }
    804 
    805 static void
    806 sunxi_hdmi_video_enable(struct sunxi_hdmi_softc *sc, bool enable)
    807 {
    808 	uint32_t val;
    809 
    810 	fdt_endpoint_enable(sc->sc_out_ep, enable);
    811 
    812 	val = HDMI_READ(sc, SUNXI_HDMI_VID_CTRL_REG);
    813 	val &= ~SUNXI_HDMI_VID_CTRL_SRC_SEL;
    814 #ifdef SUNXI_HDMI_CBGEN
    815 	val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_SRC_SEL_CBGEN,
    816 			 SUNXI_HDMI_VID_CTRL_SRC_SEL);
    817 #else
    818 	val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_SRC_SEL_RGB,
    819 			 SUNXI_HDMI_VID_CTRL_SRC_SEL);
    820 #endif
    821 	if (enable) {
    822 		val |= SUNXI_HDMI_VID_CTRL_VIDEO_EN;
    823 	} else {
    824 		val &= ~SUNXI_HDMI_VID_CTRL_VIDEO_EN;
    825 	}
    826 	HDMI_WRITE(sc, SUNXI_HDMI_VID_CTRL_REG, val);
    827 
    828 #if defined(SUNXI_HDMI_DEBUG)
    829 	sunxi_hdmi_dump_regs();
    830 #endif
    831 }
    832 
    833 static void
    834 sunxi_hdmi_set_videomode(struct sunxi_hdmi_softc *sc,
    835     const struct videomode *mode, u_int display_mode)
    836 {
    837 	uint32_t val;
    838 	const u_int dblscan_p = !!(mode->flags & VID_DBLSCAN);
    839 	const u_int interlace_p = !!(mode->flags & VID_INTERLACE);
    840 	const u_int phsync_p = !!(mode->flags & VID_PHSYNC);
    841 	const u_int pvsync_p = !!(mode->flags & VID_PVSYNC);
    842 	const u_int hfp = mode->hsync_start - mode->hdisplay;
    843 	const u_int hspw = mode->hsync_end - mode->hsync_start;
    844 	const u_int hbp = mode->htotal - mode->hsync_start;
    845 	const u_int vfp = mode->vsync_start - mode->vdisplay;
    846 	const u_int vspw = mode->vsync_end - mode->vsync_start;
    847 	const u_int vbp = mode->vtotal - mode->vsync_start;
    848 	struct clk *clk_pll;
    849 	int parent_rate;
    850 	int best_div, best_dbl, best_diff;
    851 	int target_rate = mode->dot_clock * 1000;
    852 
    853 #ifdef SUNXI_HDMI_DEBUG
    854 	device_printf(sc->sc_dev,
    855 	    "dblscan %d, interlace %d, phsync %d, pvsync %d\n",
    856 	    dblscan_p, interlace_p, phsync_p, pvsync_p);
    857 	device_printf(sc->sc_dev, "h: %u %u %u %u\n",
    858 	    mode->hdisplay, hbp, hfp, hspw);
    859 	device_printf(sc->sc_dev, "v: %u %u %u %u\n",
    860 	    mode->vdisplay, vbp, vfp, vspw);
    861 #endif
    862 
    863 	HDMI_WRITE(sc, SUNXI_HDMI_INT_STATUS_REG, 0xffffffff);
    864 
    865 	/* assume tcon0 uses pll3, tcon1 uses pll7 */
    866 	switch(fdt_endpoint_index(sc->sc_in_ep)) {
    867 	case 0:
    868 		clk_pll = sc->sc_clk_pll0;
    869 		break;
    870 	case 1:
    871 		clk_pll = sc->sc_clk_pll1;
    872 		break;
    873 	default:
    874 		panic("sunxi_hdmi pll");
    875 	}
    876 	parent_rate = clk_get_rate(clk_pll);
    877 	KASSERT(parent_rate > 0);
    878 	best_div = best_dbl = 0;
    879 	best_diff = INT_MAX;
    880 	for (int d = 2; d > 0 && best_diff != 0; d--) {
    881 		for (int m = 1; m <= 16 && best_diff != 0; m++) {
    882 			int cur_rate = parent_rate / m / d;
    883 			int diff = abs(target_rate - cur_rate);
    884 			if (diff >= 0 && diff < best_diff) {
    885 				best_diff = diff;
    886 				best_div = m;
    887 				best_dbl = d;
    888 			}
    889 		}
    890 	}
    891 
    892 #ifdef SUNXI_HDMI_DEBUG
    893 	device_printf(sc->sc_dev, "parent rate: %d\n", parent_rate);
    894 	device_printf(sc->sc_dev, "dot_clock: %d\n", mode->dot_clock);
    895 	device_printf(sc->sc_dev, "clkdiv: %d\n", best_div);
    896 	device_printf(sc->sc_dev, "clkdbl: %c\n", (best_dbl == 1) ? 'Y' : 'N');
    897 #endif
    898 
    899 	if (best_div == 0) {
    900 		device_printf(sc->sc_dev, "ERROR: TCON clk not configured\n");
    901 		return;
    902 	}
    903 
    904 	uint32_t pll_ctrl, pad_ctrl0, pad_ctrl1;
    905 	if (HDMI_1_4_P(sc)) {
    906 		pad_ctrl0 = 0x7e8000ff;
    907 		pad_ctrl1 = 0x01ded030;
    908 		pll_ctrl = 0xba48a308;
    909 		pll_ctrl |= __SHIFTIN(best_div - 1, SUNXI_HDMI_PLL_CTRL_PREDIV);
    910 	} else {
    911 		pad_ctrl0 = 0xfe800000;
    912 		pad_ctrl1 = 0x00d8c830;
    913 		pll_ctrl = 0xfa4ef708;
    914 		pll_ctrl |= __SHIFTIN(best_div, SUNXI_HDMI_PLL_CTRL_PREDIV);
    915 	}
    916 	if (best_dbl == 2)
    917 		pad_ctrl1 |= 0x40;
    918 
    919 	HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL0_REG, pad_ctrl0);
    920 	HDMI_WRITE(sc, SUNXI_HDMI_PAD_CTRL1_REG, pad_ctrl1);
    921 	HDMI_WRITE(sc, SUNXI_HDMI_PLL_CTRL_REG, pll_ctrl);
    922 	/* assume tcon0 uses pll3, tcon1 uses pll7 */
    923 	switch(fdt_endpoint_index(sc->sc_in_ep)) {
    924 	case 0:
    925 		HDMI_WRITE(sc, SUNXI_HDMI_PLL_DBG0_REG, (0<<21));
    926 		break;
    927 	case 1:
    928 		HDMI_WRITE(sc, SUNXI_HDMI_PLL_DBG0_REG, (1<<21));
    929 		break;
    930 	default:
    931 		panic("sunxi_hdmi pll");
    932 	}
    933 
    934 	val = HDMI_READ(sc, SUNXI_HDMI_VID_CTRL_REG);
    935 	val &= ~SUNXI_HDMI_VID_CTRL_HDMI_MODE;
    936 	if (display_mode == DISPLAY_MODE_DVI) {
    937 		val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_HDMI_MODE_DVI,
    938 				 SUNXI_HDMI_VID_CTRL_HDMI_MODE);
    939 	} else {
    940 		val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_HDMI_MODE_HDMI,
    941 				 SUNXI_HDMI_VID_CTRL_HDMI_MODE);
    942 	}
    943 	val &= ~SUNXI_HDMI_VID_CTRL_REPEATER_SEL;
    944 	if (dblscan_p) {
    945 		val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_REPEATER_SEL_2X,
    946 				 SUNXI_HDMI_VID_CTRL_REPEATER_SEL);
    947 	}
    948 	val &= ~SUNXI_HDMI_VID_CTRL_OUTPUT_FMT;
    949 	if (interlace_p) {
    950 		val |= __SHIFTIN(SUNXI_HDMI_VID_CTRL_OUTPUT_FMT_INTERLACE,
    951 				 SUNXI_HDMI_VID_CTRL_OUTPUT_FMT);
    952 	}
    953 	HDMI_WRITE(sc, SUNXI_HDMI_VID_CTRL_REG, val);
    954 
    955 	val = __SHIFTIN((mode->hdisplay << dblscan_p) - 1,
    956 			SUNXI_HDMI_VID_TIMING_0_ACT_H);
    957 	val |= __SHIFTIN(mode->vdisplay - 1,
    958 			 SUNXI_HDMI_VID_TIMING_0_ACT_V);
    959 	HDMI_WRITE(sc, SUNXI_HDMI_VID_TIMING_0_REG, val);
    960 
    961 	val = __SHIFTIN((hbp << dblscan_p) - 1,
    962 			SUNXI_HDMI_VID_TIMING_1_HBP);
    963 	val |= __SHIFTIN(vbp - 1,
    964 			 SUNXI_HDMI_VID_TIMING_1_VBP);
    965 	HDMI_WRITE(sc, SUNXI_HDMI_VID_TIMING_1_REG, val);
    966 
    967 	val = __SHIFTIN((hfp << dblscan_p) - 1,
    968 			SUNXI_HDMI_VID_TIMING_2_HFP);
    969 	val |= __SHIFTIN(vfp - 1,
    970 			 SUNXI_HDMI_VID_TIMING_2_VFP);
    971 	HDMI_WRITE(sc, SUNXI_HDMI_VID_TIMING_2_REG, val);
    972 
    973 	val = __SHIFTIN((hspw << dblscan_p) - 1,
    974 			SUNXI_HDMI_VID_TIMING_3_HSPW);
    975 	val |= __SHIFTIN(vspw - 1,
    976 			 SUNXI_HDMI_VID_TIMING_3_VSPW);
    977 	HDMI_WRITE(sc, SUNXI_HDMI_VID_TIMING_3_REG, val);
    978 
    979 	val = 0;
    980 	if (phsync_p) {
    981 		val |= SUNXI_HDMI_VID_TIMING_4_HSYNC_ACTIVE_SEL;
    982 	}
    983 	if (pvsync_p) {
    984 		val |= SUNXI_HDMI_VID_TIMING_4_VSYNC_ACTIVE_SEL;
    985 	}
    986 	val |= __SHIFTIN(SUNXI_HDMI_VID_TIMING_4_TX_CLOCK_NORMAL,
    987 			 SUNXI_HDMI_VID_TIMING_4_TX_CLOCK);
    988 	HDMI_WRITE(sc, SUNXI_HDMI_VID_TIMING_4_REG, val);
    989 
    990 	/* Packet control */
    991 	HDMI_WRITE(sc, SUNXI_HDMI_GP_PKT0_REG, 0);
    992 	HDMI_WRITE(sc, SUNXI_HDMI_GP_PKT1_REG, 0);
    993 	HDMI_WRITE(sc, SUNXI_HDMI_PKT_CTRL0_REG, 0x00005321);
    994 	HDMI_WRITE(sc, SUNXI_HDMI_PKT_CTRL1_REG, 0x0000000f);
    995 }
    996 
    997 static void
    998 sunxi_hdmi_set_audiomode(struct sunxi_hdmi_softc *sc,
    999     const struct videomode *mode, u_int display_mode)
   1000 {
   1001 	uint32_t cts, n, val;
   1002 
   1003 	/*
   1004 	 * Before changing audio parameters, disable and reset the
   1005 	 * audio module. Wait for the soft reset bit to clear before
   1006 	 * configuring the audio parameters.
   1007 	 */
   1008 	val = HDMI_READ(sc, SUNXI_HDMI_AUD_CTRL_REG);
   1009 	val &= ~SUNXI_HDMI_AUD_CTRL_EN;
   1010 	val |= SUNXI_HDMI_AUD_CTRL_RST;
   1011 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_CTRL_REG, val);
   1012 	do {
   1013 		val = HDMI_READ(sc, SUNXI_HDMI_AUD_CTRL_REG);
   1014 	} while (val & SUNXI_HDMI_AUD_CTRL_RST);
   1015 
   1016 	/* No audio support in DVI mode */
   1017 	if (display_mode != DISPLAY_MODE_HDMI) {
   1018 		return;
   1019 	}
   1020 
   1021 	/* DMA & FIFO control */
   1022 	val = HDMI_READ(sc, SUNXI_HDMI_ADMA_CTRL_REG);
   1023 	if (sc->sc_type == HDMI_A31) {
   1024 		val |= SUNXI_HDMI_ADMA_CTRL_SRC_DMA_MODE;	/* NDMA */
   1025 	} else {
   1026 		val &= ~SUNXI_HDMI_ADMA_CTRL_SRC_DMA_MODE;	/* DDMA */
   1027 	}
   1028 	val &= ~SUNXI_HDMI_ADMA_CTRL_SRC_DMA_SAMPLE_RATE;
   1029 	val &= ~SUNXI_HDMI_ADMA_CTRL_SRC_SAMPLE_LAYOUT;
   1030 	val &= ~SUNXI_HDMI_ADMA_CTRL_SRC_WORD_LEN;
   1031 	val &= ~SUNXI_HDMI_ADMA_CTRL_DATA_SEL;
   1032 	HDMI_WRITE(sc, SUNXI_HDMI_ADMA_CTRL_REG, val);
   1033 
   1034 	/* Audio format control */
   1035 	val = HDMI_READ(sc, SUNXI_HDMI_AUD_FMT_REG);
   1036 	val &= ~SUNXI_HDMI_AUD_FMT_SRC_SEL;
   1037 	val &= ~SUNXI_HDMI_AUD_FMT_SEL;
   1038 	val &= ~SUNXI_HDMI_AUD_FMT_DSD_FMT;
   1039 	val &= ~SUNXI_HDMI_AUD_FMT_LAYOUT;
   1040 	val &= ~SUNXI_HDMI_AUD_FMT_SRC_CH_CFG;
   1041 	val |= __SHIFTIN(1, SUNXI_HDMI_AUD_FMT_SRC_CH_CFG);
   1042 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_FMT_REG, val);
   1043 
   1044 	/* PCM control (channel map) */
   1045 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_PCM_CTRL_REG, 0x76543210);
   1046 
   1047 	/* Clock setup */
   1048 	n = 6144;	/* 48 kHz */
   1049 	cts = ((mode->dot_clock * 10) * (n / 128)) / 480;
   1050 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_CTS_REG, cts);
   1051 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_N_REG, n);
   1052 
   1053 	/* Audio PCM channel status 0 */
   1054 	val = __SHIFTIN(SUNXI_HDMI_AUD_CH_STATUS0_FS_FREQ_48,
   1055 			SUNXI_HDMI_AUD_CH_STATUS0_FS_FREQ);
   1056 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_CH_STATUS0_REG, val);
   1057 
   1058 	/* Audio PCM channel status 1 */
   1059 	val = HDMI_READ(sc, SUNXI_HDMI_AUD_CH_STATUS1_REG);
   1060 	val &= ~SUNXI_HDMI_AUD_CH_STATUS1_CGMS_A;
   1061 	val &= ~SUNXI_HDMI_AUD_CH_STATUS1_ORIGINAL_FS;
   1062 	val &= ~SUNXI_HDMI_AUD_CH_STATUS1_WORD_LEN;
   1063 	val |= __SHIFTIN(5, SUNXI_HDMI_AUD_CH_STATUS1_WORD_LEN);
   1064 	val |= SUNXI_HDMI_AUD_CH_STATUS1_WORD_LEN_MAX;
   1065 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_CH_STATUS1_REG, val);
   1066 
   1067 	/* Re-enable */
   1068 	val = HDMI_READ(sc, SUNXI_HDMI_AUD_CTRL_REG);
   1069 	val |= SUNXI_HDMI_AUD_CTRL_EN;
   1070 	HDMI_WRITE(sc, SUNXI_HDMI_AUD_CTRL_REG, val);
   1071 
   1072 #if defined(SUNXI_HDMI_DEBUG)
   1073 	sunxi_hdmi_dump_regs();
   1074 #endif
   1075 }
   1076 
   1077 static void
   1078 sunxi_hdmi_hpd(struct sunxi_hdmi_softc *sc)
   1079 {
   1080 	uint32_t hpd = HDMI_READ(sc, SUNXI_HDMI_HPD_REG);
   1081 	bool con = !!(hpd & SUNXI_HDMI_HPD_HOTPLUG_DET);
   1082 
   1083 	KASSERT(mutex_owned(&sc->sc_pwr_lock));
   1084 	if (sc->sc_display_connected == con)
   1085 		return;
   1086 
   1087 	if (con) {
   1088 		device_printf(sc->sc_dev, "display connected\n");
   1089 		sc->sc_pwr_refcount  = 1;
   1090 		sunxi_hdmi_read_edid(sc);
   1091 	} else {
   1092 		device_printf(sc->sc_dev, "display disconnected\n");
   1093 		sc->sc_pwr_refcount = 0;
   1094 		sunxi_hdmi_video_enable(sc, false);
   1095 		fdt_endpoint_enable(sc->sc_in_ep, false);
   1096 		sunxi_tcon1_set_videomode(
   1097 		    fdt_endpoint_device(sc->sc_in_rep), NULL);
   1098 	}
   1099 
   1100 	sc->sc_display_connected = con;
   1101 }
   1102 
   1103 static void
   1104 sunxi_hdmi_thread(void *priv)
   1105 {
   1106 	struct sunxi_hdmi_softc *sc = priv;
   1107 
   1108 	for (;;) {
   1109 		mutex_enter(&sc->sc_pwr_lock);
   1110 		sunxi_hdmi_hpd(sc);
   1111 		mutex_exit(&sc->sc_pwr_lock);
   1112 		kpause("hdmihotplug", false, mstohz(1000), NULL);
   1113 	}
   1114 }
   1115 
   1116 static int
   1117 sunxi_hdmi_poweron(struct sunxi_hdmi_softc *sc, bool enable)
   1118 {
   1119 	int error = 0;
   1120 	KASSERT(mutex_owned(&sc->sc_pwr_lock));
   1121 	if (!sc->sc_display_connected)
   1122 		return EOPNOTSUPP;
   1123 	if (enable) {
   1124 		KASSERT(sc->sc_pwr_refcount >= 0);
   1125 		if (sc->sc_pwr_refcount == 0) {
   1126 			error = fdt_endpoint_enable(sc->sc_in_ep, true);
   1127 			if (error)
   1128 				return error;
   1129 			sunxi_hdmi_video_enable(sc, true);
   1130 		}
   1131 		sc->sc_pwr_refcount++;
   1132 	} else {
   1133 		sc->sc_pwr_refcount--;
   1134 		KASSERT(sc->sc_pwr_refcount >= 0);
   1135 		if (sc->sc_pwr_refcount == 0) {
   1136 			sunxi_hdmi_video_enable(sc, false);
   1137 			error = fdt_endpoint_enable(sc->sc_in_ep, false);
   1138 		}
   1139 	}
   1140 	return error;
   1141 }
   1142 #if 0
   1143 static int
   1144 sunxi_hdmi_intr(void *priv)
   1145 {
   1146 	struct sunxi_hdmi_softc *sc = priv;
   1147 	uint32_t intsts;
   1148 
   1149 	intsts = HDMI_READ(sc, SUNXI_HDMI_INT_STATUS_REG);
   1150 	if (!(intsts & 0x73))
   1151 		return 0;
   1152 
   1153 	HDMI_WRITE(sc, SUNXI_HDMI_INT_STATUS_REG, intsts);
   1154 
   1155 	device_printf(sc->sc_dev, "INT_STATUS %08X\n", intsts);
   1156 
   1157 	return 1;
   1158 }
   1159 #endif
   1160 
   1161 #if 0 /* XXX audio */
   1162 void
   1163 sunxi_hdmi_get_info(struct sunxi_hdmi_info *info)
   1164 {
   1165 	struct sunxi_hdmi_softc *sc;
   1166 	device_t dev;
   1167 
   1168 	memset(info, 0, sizeof(*info));
   1169 
   1170 	dev = device_find_by_driver_unit("sunxihdmi", 0);
   1171 	if (dev == NULL) {
   1172 		info->display_connected = false;
   1173 		return;
   1174 	}
   1175 	sc = device_private(dev);
   1176 
   1177 	info->display_connected = sc->sc_display_connected;
   1178 	if (info->display_connected) {
   1179 		strlcpy(info->display_vendor, sc->sc_display_vendor,
   1180 		    sizeof(info->display_vendor));
   1181 		strlcpy(info->display_product, sc->sc_display_product,
   1182 		    sizeof(info->display_product));
   1183 		info->display_hdmimode =
   1184 		    sc->sc_current_display_mode == DISPLAY_MODE_HDMI;
   1185 	}
   1186 }
   1187 #endif
   1188 
   1189 #if defined(SUNXI_HDMI_DEBUG)
   1190 void
   1191 sunxi_hdmi_dump_regs(void)
   1192 {
   1193 	static const struct {
   1194 		const char *name;
   1195 		uint16_t reg;
   1196 	} regs[] = {
   1197 		{ "CTRL", SUNXI_HDMI_CTRL_REG },
   1198 		{ "INT_STATUS", SUNXI_HDMI_INT_STATUS_REG },
   1199 		{ "VID_CTRL", SUNXI_HDMI_VID_CTRL_REG },
   1200 		{ "VID_TIMING_0", SUNXI_HDMI_VID_TIMING_0_REG },
   1201 		{ "VID_TIMING_1", SUNXI_HDMI_VID_TIMING_1_REG },
   1202 		{ "VID_TIMING_2", SUNXI_HDMI_VID_TIMING_2_REG },
   1203 		{ "VID_TIMING_3", SUNXI_HDMI_VID_TIMING_3_REG },
   1204 		{ "VID_TIMING_4", SUNXI_HDMI_VID_TIMING_4_REG },
   1205 		{ "PAD_CTRL0", SUNXI_HDMI_PAD_CTRL0_REG },
   1206 		{ "PAD_CTRL1", SUNXI_HDMI_PAD_CTRL1_REG },
   1207 		{ "PLL_CTRL", SUNXI_HDMI_PLL_CTRL_REG },
   1208 		{ "PLL_DBG0", SUNXI_HDMI_PLL_DBG0_REG },
   1209 		{ "PLL_DBG1", SUNXI_HDMI_PLL_DBG1_REG },
   1210 	};
   1211 	struct sunxi_hdmi_softc *sc;
   1212 	device_t dev;
   1213 
   1214 	dev = device_find_by_driver_unit("sunxihdmi", 0);
   1215 	if (dev == NULL)
   1216 		return;
   1217 	sc = device_private(dev);
   1218 
   1219 	for (int i = 0; i < __arraycount(regs); i++) {
   1220 		printf("%s: 0x%08x\n", regs[i].name,
   1221 		    HDMI_READ(sc, regs[i].reg));
   1222 	}
   1223 }
   1224 #endif
   1225