Home | History | Annotate | Line # | Download | only in sunxi
sunxi_tcon.c revision 1.7.14.1
      1  1.7.14.1  thorpej /* $NetBSD: sunxi_tcon.c,v 1.7.14.1 2021/04/03 22:28:19 thorpej Exp $ */
      2       1.1   bouyer 
      3       1.1   bouyer /*-
      4       1.1   bouyer  * Copyright (c) 2018 Manuel Bouyer <bouyer (at) antioche.eu.org>
      5       1.1   bouyer  * All rights reserved.
      6       1.1   bouyer  *
      7       1.1   bouyer  * Copyright (c) 2014 Jared D. McNeill <jmcneill (at) invisible.ca>
      8       1.1   bouyer  * All rights reserved.
      9       1.1   bouyer  *
     10       1.1   bouyer  * Redistribution and use in source and binary forms, with or without
     11       1.1   bouyer  * modification, are permitted provided that the following conditions
     12       1.1   bouyer  * are met:
     13       1.1   bouyer  * 1. Redistributions of source code must retain the above copyright
     14       1.1   bouyer  *    notice, this list of conditions and the following disclaimer.
     15       1.1   bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1   bouyer  *    notice, this list of conditions and the following disclaimer in the
     17       1.1   bouyer  *    documentation and/or other materials provided with the distribution.
     18       1.1   bouyer  *
     19       1.1   bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20       1.1   bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21       1.1   bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22       1.1   bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23       1.1   bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24       1.1   bouyer  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25       1.1   bouyer  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26       1.1   bouyer  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27       1.1   bouyer  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.1   bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.1   bouyer  * SUCH DAMAGE.
     30       1.1   bouyer  */
     31       1.1   bouyer 
     32       1.1   bouyer #include <sys/cdefs.h>
     33  1.7.14.1  thorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_tcon.c,v 1.7.14.1 2021/04/03 22:28:19 thorpej Exp $");
     34       1.1   bouyer 
     35       1.1   bouyer #include <sys/param.h>
     36       1.1   bouyer #include <sys/bus.h>
     37       1.1   bouyer #include <sys/device.h>
     38       1.1   bouyer #include <sys/intr.h>
     39       1.1   bouyer #include <sys/systm.h>
     40       1.1   bouyer #include <sys/kernel.h>
     41       1.1   bouyer #include <sys/mutex.h>
     42       1.1   bouyer #include <sys/condvar.h>
     43       1.1   bouyer 
     44       1.1   bouyer #include <dev/fdt/fdtvar.h>
     45       1.1   bouyer #include <dev/fdt/fdt_port.h>
     46       1.1   bouyer #include <dev/fdt/panel_fdt.h>
     47       1.1   bouyer 
     48       1.1   bouyer #include <dev/videomode/videomode.h>
     49       1.1   bouyer 
     50       1.1   bouyer #include <arm/sunxi/sunxi_tconreg.h>
     51       1.1   bouyer #include <arm/sunxi/sunxi_display.h>
     52       1.1   bouyer 
     53       1.1   bouyer #define DIVIDE(x,y)     (((x) + ((y) / 2)) / (y))
     54       1.1   bouyer 
     55       1.1   bouyer enum sunxi_tcon_type {
     56       1.1   bouyer 	TCON_A10 = 1,
     57       1.1   bouyer };
     58       1.1   bouyer 
     59       1.1   bouyer struct sunxi_tcon_softc {
     60       1.1   bouyer 	device_t sc_dev;
     61       1.1   bouyer 	enum sunxi_tcon_type sc_type;
     62       1.1   bouyer 	int sc_phandle;
     63       1.1   bouyer 	bus_space_tag_t sc_bst;
     64       1.1   bouyer 	bus_space_handle_t sc_bsh;
     65       1.1   bouyer 	struct clk *sc_clk_ahb;
     66       1.1   bouyer 	struct clk *sc_clk_ch0;
     67       1.1   bouyer 	struct clk *sc_clk_ch1;
     68       1.7   bouyer 	struct fdtbus_reset *sc_rst, *sc_lvds_rst;
     69       1.1   bouyer 	unsigned int sc_output_type;
     70       1.1   bouyer #define OUTPUT_HDMI 0
     71       1.1   bouyer #define OUTPUT_LVDS 1
     72       1.1   bouyer #define OUTPUT_VGA 2
     73       1.1   bouyer 	struct fdt_device_ports sc_ports;
     74       1.1   bouyer 	int sc_unit; /* tcon0 or tcon1 */
     75       1.1   bouyer 	struct fdt_endpoint *sc_in_ep;
     76       1.1   bouyer 	struct fdt_endpoint *sc_in_rep;
     77       1.1   bouyer 	struct fdt_endpoint *sc_out_ep;
     78       1.1   bouyer };
     79       1.1   bouyer 
     80       1.1   bouyer static bus_space_handle_t tcon_mux_bsh;
     81       1.1   bouyer static bool tcon_mux_inited = false;
     82       1.1   bouyer 
     83       1.1   bouyer static void sunxi_tcon_ep_connect(device_t, struct fdt_endpoint *, bool);
     84       1.1   bouyer static int  sunxi_tcon_ep_activate(device_t, struct fdt_endpoint *, bool);
     85       1.1   bouyer static int  sunxi_tcon_ep_enable(device_t, struct fdt_endpoint *, bool);
     86       1.1   bouyer static int  sunxi_tcon0_set_video(struct sunxi_tcon_softc *);
     87       1.1   bouyer static int  sunxi_tcon0_enable(struct sunxi_tcon_softc *, bool);
     88       1.1   bouyer static int  sunxi_tcon1_enable(struct sunxi_tcon_softc *, bool);
     89       1.1   bouyer void sunxi_tcon_dump_regs(int);
     90       1.1   bouyer 
     91       1.1   bouyer #define TCON_READ(sc, reg) \
     92       1.1   bouyer     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     93       1.1   bouyer #define TCON_WRITE(sc, reg, val) \
     94       1.1   bouyer     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     95       1.1   bouyer 
     96  1.7.14.1  thorpej static const struct device_compatible_entry compat_data[] = {
     97  1.7.14.1  thorpej 	{ .compat = "allwinner,sun4i-a10-tcon", .value = TCON_A10},
     98  1.7.14.1  thorpej 	{ .compat = "allwinner,sun7i-a20-tcon", .value = TCON_A10},
     99  1.7.14.1  thorpej 	DEVICE_COMPAT_EOL
    100       1.1   bouyer };
    101       1.1   bouyer 
    102       1.1   bouyer static int	sunxi_tcon_match(device_t, cfdata_t, void *);
    103       1.1   bouyer static void	sunxi_tcon_attach(device_t, device_t, void *);
    104       1.1   bouyer 
    105       1.1   bouyer CFATTACH_DECL_NEW(sunxi_tcon, sizeof(struct sunxi_tcon_softc),
    106       1.1   bouyer 	sunxi_tcon_match, sunxi_tcon_attach, NULL, NULL);
    107       1.1   bouyer 
    108       1.1   bouyer static int
    109       1.1   bouyer sunxi_tcon_match(device_t parent, cfdata_t cf, void *aux)
    110       1.1   bouyer {
    111       1.1   bouyer 	struct fdt_attach_args * const faa = aux;
    112       1.1   bouyer 
    113  1.7.14.1  thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    114       1.1   bouyer }
    115       1.1   bouyer 
    116       1.1   bouyer static void
    117       1.1   bouyer sunxi_tcon_attach(device_t parent, device_t self, void *aux)
    118       1.1   bouyer {
    119       1.1   bouyer 	struct sunxi_tcon_softc *sc = device_private(self);
    120       1.1   bouyer 	struct fdt_attach_args * const faa = aux;
    121       1.1   bouyer 	const int phandle = faa->faa_phandle;
    122       1.1   bouyer 	bus_addr_t addr;
    123       1.1   bouyer 	bus_size_t size;
    124       1.1   bouyer 
    125       1.1   bouyer 	sc->sc_dev = self;
    126       1.1   bouyer 	sc->sc_phandle = phandle;
    127       1.1   bouyer 	sc->sc_bst = faa->faa_bst;
    128       1.1   bouyer 
    129       1.1   bouyer 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    130       1.1   bouyer 		aprint_error(": couldn't get registers\n");
    131       1.1   bouyer 	}
    132       1.1   bouyer 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    133       1.1   bouyer 		aprint_error(": couldn't map registers\n");
    134       1.1   bouyer 		return;
    135       1.1   bouyer 	}
    136       1.1   bouyer 
    137       1.1   bouyer 	sc->sc_clk_ahb = fdtbus_clock_get(phandle, "ahb");
    138       1.1   bouyer 	sc->sc_clk_ch0 = fdtbus_clock_get(phandle, "tcon-ch0");
    139       1.1   bouyer 	sc->sc_clk_ch1 = fdtbus_clock_get(phandle, "tcon-ch1");
    140       1.1   bouyer 
    141       1.1   bouyer 	if (sc->sc_clk_ahb == NULL || sc->sc_clk_ch0 == NULL
    142       1.5   bouyer 	    || sc->sc_clk_ch1 == NULL) {
    143       1.1   bouyer 		aprint_error(": couldn't get clocks\n");
    144       1.1   bouyer 		aprint_debug_dev(self, "clk ahb %s tcon-ch0 %s tcon-ch1 %s\n",
    145       1.1   bouyer 		    sc->sc_clk_ahb == NULL ? "missing" : "present",
    146       1.1   bouyer 		    sc->sc_clk_ch0 == NULL ? "missing" : "present",
    147       1.1   bouyer 		    sc->sc_clk_ch1 == NULL ? "missing" : "present");
    148       1.1   bouyer 		return;
    149       1.1   bouyer 	}
    150       1.1   bouyer 
    151       1.7   bouyer 	sc->sc_rst = fdtbus_reset_get(phandle, "lcd");
    152       1.7   bouyer 	if (sc->sc_rst == NULL) {
    153       1.1   bouyer 		aprint_error(": couldn't get lcd reset\n");
    154       1.1   bouyer 		return;
    155       1.1   bouyer 	}
    156       1.1   bouyer 
    157       1.7   bouyer 	sc->sc_lvds_rst = fdtbus_reset_get(phandle, "lvds");
    158       1.1   bouyer 
    159  1.7.14.1  thorpej 	sc->sc_type =
    160  1.7.14.1  thorpej 	    of_compatible_lookup(faa->faa_phandle, compat_data)->value;
    161       1.1   bouyer 
    162       1.1   bouyer 	aprint_naive("\n");
    163       1.1   bouyer 	aprint_normal(": LCD/TV timing controller (%s)\n",
    164       1.1   bouyer 	    fdtbus_get_string(phandle, "name"));
    165       1.1   bouyer 
    166       1.1   bouyer 	sc->sc_unit = -1;
    167       1.1   bouyer 	sc->sc_ports.dp_ep_connect = sunxi_tcon_ep_connect;
    168       1.1   bouyer 	sc->sc_ports.dp_ep_activate = sunxi_tcon_ep_activate;
    169       1.1   bouyer 	sc->sc_ports.dp_ep_enable = sunxi_tcon_ep_enable;
    170       1.1   bouyer 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_OTHER);
    171       1.7   bouyer }
    172       1.1   bouyer 
    173       1.7   bouyer void
    174       1.7   bouyer sunxi_tcon_doreset(void)
    175       1.7   bouyer {
    176       1.7   bouyer 	device_t dev;
    177       1.7   bouyer 	struct sunxi_tcon_softc *sc;
    178       1.7   bouyer 	for (int i = 0;;i++) {
    179       1.7   bouyer 		dev = device_find_by_driver_unit("sunxitcon", i);
    180       1.7   bouyer 		if (dev == NULL)
    181       1.7   bouyer 			return;
    182       1.7   bouyer 		sc = device_private(dev);
    183       1.4   bouyer 
    184       1.4   bouyer 		if (clk_disable(sc->sc_clk_ahb) != 0) {
    185       1.7   bouyer 			aprint_error_dev(dev, ": couldn't disable ahb clock\n");
    186       1.7   bouyer 			return;
    187       1.7   bouyer 		}
    188       1.7   bouyer 		if (clk_disable(sc->sc_clk_ch0) != 0) {
    189       1.7   bouyer 			aprint_error_dev(dev, ": couldn't disable ch0 clock\n");
    190       1.7   bouyer 			return;
    191       1.7   bouyer 		}
    192       1.7   bouyer 
    193       1.7   bouyer 		if (clk_disable(sc->sc_clk_ch1) != 0) {
    194       1.7   bouyer 			aprint_error_dev(dev, ": couldn't disable ch1 clock\n");
    195       1.7   bouyer 			return;
    196       1.7   bouyer 		}
    197       1.7   bouyer 
    198       1.7   bouyer 		if (fdtbus_reset_assert(sc->sc_rst) != 0) {
    199       1.7   bouyer 			aprint_error_dev(dev, ": couldn't assert lcd reset\n");
    200       1.7   bouyer 			return;
    201       1.7   bouyer 		}
    202       1.7   bouyer 		if (sc->sc_lvds_rst != NULL) {
    203       1.7   bouyer 			if (fdtbus_reset_assert(sc->sc_lvds_rst) != 0) {
    204       1.7   bouyer 				aprint_error_dev(dev,
    205       1.7   bouyer 				    ": couldn't assert lvds reset\n");
    206       1.7   bouyer 				return;
    207       1.7   bouyer 			}
    208       1.7   bouyer 		}
    209       1.7   bouyer 		delay(1);
    210       1.7   bouyer 		if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
    211       1.7   bouyer 			aprint_error_dev(dev,
    212       1.7   bouyer 			    ": couldn't de-assert lcd reset\n");
    213       1.4   bouyer 			return;
    214       1.4   bouyer 		}
    215       1.7   bouyer 		if (sc->sc_lvds_rst != NULL) {
    216       1.7   bouyer 			if (fdtbus_reset_deassert(sc->sc_lvds_rst) != 0) {
    217       1.7   bouyer 				aprint_error_dev(dev,
    218       1.7   bouyer 				    ": couldn't de-assert lvds reset\n");
    219       1.7   bouyer 				return;
    220       1.7   bouyer 			}
    221       1.7   bouyer 		}
    222       1.7   bouyer 
    223       1.7   bouyer 		if (clk_enable(sc->sc_clk_ahb) != 0) {
    224       1.7   bouyer 			aprint_error_dev(dev, ": couldn't enable ahb clock\n");
    225       1.7   bouyer 			return;
    226       1.7   bouyer 		}
    227       1.7   bouyer 
    228       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GINT0_REG, 0);
    229       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
    230       1.7   bouyer 		    __SHIFTIN(0x20, SUNXI_TCON_GINT1_TCON0_LINENO));
    231       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_DCLK_REG, 0xf0000000);
    232       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, 0x0);
    233       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, 0);
    234       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0xffffffff);
    235       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, 0);
    236       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0xffffffff);
    237       1.7   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, 0);
    238       1.7   bouyer 
    239       1.7   bouyer 		/* clock needed for the mux in unit 0 */
    240       1.7   bouyer 		if (sc->sc_unit != 0) {
    241       1.7   bouyer 			if (clk_disable(sc->sc_clk_ahb) != 0) {
    242       1.7   bouyer 				aprint_error_dev(dev,
    243       1.7   bouyer 				    ": couldn't disable ahb clock\n");
    244       1.7   bouyer 				return;
    245       1.7   bouyer 			}
    246       1.7   bouyer 		}
    247       1.3   bouyer 	}
    248       1.1   bouyer }
    249       1.1   bouyer 
    250       1.1   bouyer static void
    251       1.1   bouyer sunxi_tcon_ep_connect(device_t self, struct fdt_endpoint *ep, bool connect)
    252       1.1   bouyer {
    253       1.1   bouyer 	struct sunxi_tcon_softc *sc = device_private(self);
    254       1.1   bouyer 	struct fdt_endpoint *rep = fdt_endpoint_remote(ep);
    255       1.1   bouyer 	int rep_idx = fdt_endpoint_index(rep);
    256       1.1   bouyer 
    257       1.1   bouyer 	KASSERT(device_is_a(self, "sunxitcon"));
    258       1.1   bouyer 	if (!connect) {
    259       1.1   bouyer 		aprint_error_dev(self, "endpoint disconnect not supported\n");
    260       1.1   bouyer 		return;
    261       1.1   bouyer 	}
    262       1.1   bouyer 
    263       1.1   bouyer 	if (fdt_endpoint_port_index(ep) == 0) {
    264       1.1   bouyer 		bool do_print = (sc->sc_unit == -1);
    265       1.1   bouyer 		/*
    266       1.1   bouyer 		 * one of our input endpoints has been connected.
    267       1.1   bouyer 		 * the remote id is our unit number
    268       1.1   bouyer 		 */
    269       1.1   bouyer 		if (sc->sc_unit != -1 && rep_idx != -1 &&
    270       1.1   bouyer 		    sc->sc_unit != rep_idx) {
    271       1.1   bouyer 			aprint_error_dev(self, ": remote id %d doens't match"
    272       1.1   bouyer 			    " discovered unit number %d\n",
    273       1.1   bouyer 			    rep_idx, sc->sc_unit);
    274       1.1   bouyer 			return;
    275       1.1   bouyer 		}
    276       1.1   bouyer 		if (!device_is_a(fdt_endpoint_device(rep), "sunxidebe")) {
    277       1.1   bouyer 			aprint_error_dev(self,
    278       1.1   bouyer 			    ": input %d connected to unknown device\n",
    279       1.1   bouyer 			    fdt_endpoint_index(ep));
    280       1.1   bouyer 			return;
    281       1.1   bouyer 		}
    282       1.1   bouyer 
    283       1.1   bouyer 		if (rep_idx != -1)
    284       1.1   bouyer 			sc->sc_unit = rep_idx;
    285       1.1   bouyer 		else {
    286       1.1   bouyer 			/* assume only one tcon */
    287       1.1   bouyer 			sc->sc_unit = 0;
    288       1.1   bouyer 		}
    289       1.1   bouyer 		if (do_print)
    290       1.1   bouyer 			aprint_verbose_dev(self, "tcon unit %d\n", sc->sc_unit);
    291       1.1   bouyer 		if (!tcon_mux_inited && sc->sc_unit == 0) {
    292       1.1   bouyer 			/* the mux register is only in LCD0 */
    293       1.4   bouyer 			if (clk_enable(sc->sc_clk_ahb) != 0) {
    294       1.4   bouyer 				aprint_error_dev(self,
    295       1.4   bouyer 				    "couldn't enable ahb clock\n");
    296       1.4   bouyer 				return;
    297       1.4   bouyer 			}
    298       1.1   bouyer 			bus_space_subregion(sc->sc_bst, sc->sc_bsh,
    299       1.1   bouyer 			    SUNXI_TCON_MUX_CTL_REG, 4, &tcon_mux_bsh);
    300       1.1   bouyer 			tcon_mux_inited = true;
    301       1.4   bouyer 			bus_space_write_4(sc->sc_bst, tcon_mux_bsh, 0,
    302       1.4   bouyer 			    __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_CLOSE,
    303       1.4   bouyer 			    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC));
    304       1.1   bouyer 		}
    305       1.1   bouyer 	} else if (fdt_endpoint_port_index(ep) == 1) {
    306       1.1   bouyer 		device_t rep_dev = fdt_endpoint_device(rep);
    307       1.1   bouyer 		switch(fdt_endpoint_index(ep)) {
    308       1.1   bouyer 		case 0:
    309       1.1   bouyer 			break;
    310       1.1   bouyer 		case 1:
    311       1.1   bouyer 			if (!device_is_a(rep_dev, "sunxihdmi")) {
    312       1.1   bouyer 				aprint_error_dev(self,
    313       1.1   bouyer 				    ": output 1 connected to unknown device\n");
    314       1.1   bouyer 				return;
    315       1.1   bouyer 			}
    316       1.1   bouyer 			break;
    317       1.1   bouyer 		default:
    318       1.1   bouyer 			break;
    319       1.1   bouyer 		}
    320       1.1   bouyer 	}
    321       1.1   bouyer }
    322       1.1   bouyer 
    323       1.1   bouyer static int
    324       1.1   bouyer sunxi_tcon_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    325       1.1   bouyer {
    326       1.1   bouyer 	struct sunxi_tcon_softc *sc = device_private(dev);
    327       1.1   bouyer 	struct fdt_endpoint *in_ep, *out_ep;
    328       1.1   bouyer 	int outi;
    329       1.1   bouyer 	int error = ENODEV;
    330       1.1   bouyer 
    331       1.1   bouyer 	KASSERT(device_is_a(dev, "sunxitcon"));
    332       1.1   bouyer 	/* our input is activated by debe, we activate our output */
    333       1.1   bouyer 	if (fdt_endpoint_port_index(ep) != SUNXI_PORT_INPUT) {
    334       1.1   bouyer 		panic("sunxi_tcon_ep_activate: port %d",
    335       1.1   bouyer 		    fdt_endpoint_port_index(ep));
    336       1.1   bouyer 	}
    337       1.1   bouyer 
    338       1.1   bouyer 	if (!activate)
    339       1.1   bouyer 		return EOPNOTSUPP;
    340       1.1   bouyer 
    341       1.3   bouyer 	if (clk_enable(sc->sc_clk_ahb) != 0) {
    342       1.3   bouyer 		aprint_error_dev(dev, "couldn't enable ahb clock\n");
    343       1.3   bouyer 		return EIO;
    344       1.3   bouyer 	}
    345       1.1   bouyer 	sc->sc_in_ep = ep;
    346       1.1   bouyer 	sc->sc_in_rep = fdt_endpoint_remote(ep);
    347       1.1   bouyer 	/* check that our other input is not active */
    348       1.1   bouyer 	switch (fdt_endpoint_index(ep)) {
    349       1.1   bouyer 	case 0:
    350       1.1   bouyer 		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    351       1.1   bouyer 		    SUNXI_PORT_INPUT, 1);
    352       1.1   bouyer 		break;
    353       1.1   bouyer 	case 1:
    354       1.1   bouyer 		in_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    355       1.1   bouyer 		    SUNXI_PORT_INPUT, 0);
    356       1.1   bouyer 		break;
    357       1.1   bouyer 	default:
    358       1.1   bouyer 		in_ep = NULL;
    359       1.1   bouyer 		panic("sunxi_tcon_ep_activate: input index %d",
    360       1.1   bouyer 		    fdt_endpoint_index(ep));
    361       1.1   bouyer 	}
    362       1.1   bouyer 	if (in_ep != NULL) {
    363       1.1   bouyer 		if (fdt_endpoint_is_active(in_ep))
    364       1.1   bouyer 			return EBUSY;
    365       1.1   bouyer 	}
    366       1.1   bouyer 	/* try output 0 (RGB/LVDS) first, then ouput 1 (HDMI) if it fails */
    367       1.1   bouyer 	for (outi = 0; outi < 2; outi++) {
    368       1.1   bouyer 		out_ep = fdt_endpoint_get_from_index(&sc->sc_ports,
    369       1.1   bouyer 		    SUNXI_PORT_OUTPUT, outi);
    370       1.1   bouyer 		if (out_ep == NULL)
    371       1.1   bouyer 			continue;
    372       1.1   bouyer 		error = fdt_endpoint_activate(out_ep, activate);
    373       1.1   bouyer 		if (error == 0) {
    374       1.1   bouyer 			struct fdt_endpoint *rep = fdt_endpoint_remote(out_ep);
    375       1.1   bouyer 			aprint_verbose_dev(dev, "output to %s\n",
    376       1.1   bouyer 			    device_xname(fdt_endpoint_device(rep)));
    377       1.1   bouyer 			sc->sc_out_ep = out_ep;
    378       1.1   bouyer 			if (outi == 0)
    379       1.1   bouyer 				return sunxi_tcon0_set_video(sc);
    380       1.4   bouyer 			/* XXX should check VGA here */
    381       1.4   bouyer 			sc->sc_output_type = OUTPUT_HDMI;
    382       1.1   bouyer 			return 0;
    383       1.1   bouyer 		}
    384       1.1   bouyer 	}
    385       1.1   bouyer 	if (out_ep == NULL) {
    386       1.1   bouyer 		aprint_error_dev(dev, "no output endpoint\n");
    387       1.1   bouyer 		return ENODEV;
    388       1.1   bouyer 	}
    389       1.1   bouyer 	return error;
    390       1.1   bouyer }
    391       1.1   bouyer 
    392       1.1   bouyer static int
    393       1.1   bouyer sunxi_tcon_ep_enable(device_t dev, struct fdt_endpoint *ep, bool enable)
    394       1.1   bouyer {
    395       1.1   bouyer 	struct sunxi_tcon_softc *sc = device_private(dev);
    396       1.1   bouyer 	int error;
    397       1.1   bouyer 	KASSERT(device_is_a(dev, "sunxitcon"));
    398       1.1   bouyer 	switch (fdt_endpoint_port_index(ep)) {
    399       1.1   bouyer 	case SUNXI_PORT_INPUT:
    400       1.1   bouyer 		KASSERT(ep == sc->sc_in_ep);
    401       1.1   bouyer 		if (fdt_endpoint_index(sc->sc_out_ep) == 0) {
    402       1.1   bouyer 			/* tcon0 active */
    403       1.1   bouyer 			return sunxi_tcon0_enable(sc, enable);
    404       1.1   bouyer 		}
    405       1.1   bouyer 		/* propagate to our output, it will get back to us */
    406       1.1   bouyer 		return fdt_endpoint_enable(sc->sc_out_ep, enable);
    407       1.1   bouyer 	case SUNXI_PORT_OUTPUT:
    408       1.1   bouyer 		KASSERT(ep == sc->sc_out_ep);
    409       1.1   bouyer 		switch (fdt_endpoint_index(ep)) {
    410       1.1   bouyer 		case 0:
    411       1.1   bouyer 			panic("sunxi_tcon0_ep_enable");
    412       1.1   bouyer 		case 1:
    413       1.1   bouyer 			error = sunxi_tcon1_enable(sc, enable);
    414       1.1   bouyer 			break;
    415       1.1   bouyer 		default:
    416       1.1   bouyer 			panic("sunxi_tcon_ep_enable ep %d",
    417       1.1   bouyer 			    fdt_endpoint_index(ep));
    418       1.1   bouyer 
    419       1.1   bouyer 		}
    420       1.1   bouyer 		break;
    421       1.1   bouyer 	default:
    422       1.1   bouyer 		panic("sunxi_tcon_ep_enable port %d", fdt_endpoint_port_index(ep));
    423       1.1   bouyer 	}
    424       1.1   bouyer #if defined(SUNXI_TCON_DEBUG)
    425       1.1   bouyer 	sunxi_tcon_dump_regs(device_unit(dev));
    426       1.1   bouyer #endif
    427       1.1   bouyer 	return error;
    428       1.1   bouyer }
    429       1.1   bouyer 
    430       1.1   bouyer static int
    431       1.1   bouyer sunxi_tcon0_set_video(struct sunxi_tcon_softc *sc)
    432       1.1   bouyer {
    433       1.1   bouyer 	const struct fdt_panel * panel;
    434       1.1   bouyer 	int32_t lcd_x, lcd_y;
    435       1.1   bouyer 	int32_t lcd_hbp, lcd_ht, lcd_vbp, lcd_vt;
    436       1.1   bouyer 	int32_t lcd_hspw, lcd_vspw, lcd_io_cfg0;
    437       1.1   bouyer 	uint32_t vblk, start_delay;
    438       1.1   bouyer 	uint32_t val;
    439       1.1   bouyer 	uint32_t best_div;
    440       1.1   bouyer 	int best_diff, best_clk_freq, clk_freq, lcd_dclk_freq;
    441       1.1   bouyer 	bool dualchan = false;
    442       1.1   bouyer 	static struct videomode mode;
    443       1.1   bouyer 	int error;
    444       1.1   bouyer 
    445       1.1   bouyer 	panel = fdt_endpoint_get_data(fdt_endpoint_remote(sc->sc_out_ep));
    446       1.1   bouyer 	KASSERT(panel != NULL);
    447       1.1   bouyer 	KASSERT(panel->panel_type == PANEL_DUAL_LVDS ||
    448       1.1   bouyer 	    panel->panel_type == PANEL_LVDS);
    449       1.4   bouyer 	sc->sc_output_type = OUTPUT_LVDS;
    450       1.1   bouyer 
    451       1.1   bouyer 	lcd_x = panel->panel_timing.hactive;
    452       1.1   bouyer 	lcd_y = panel->panel_timing.vactive;
    453       1.1   bouyer 
    454       1.1   bouyer 	lcd_dclk_freq = panel->panel_timing.clock_freq;
    455       1.1   bouyer 
    456       1.1   bouyer 	lcd_hbp = panel->panel_timing.hback_porch;
    457       1.1   bouyer 	lcd_hspw = panel->panel_timing.hsync_len;
    458       1.1   bouyer 	lcd_ht = panel->panel_timing.hfront_porch + lcd_hspw + lcd_x + lcd_hbp;
    459       1.1   bouyer 
    460       1.1   bouyer 	lcd_vbp = panel->panel_timing.vback_porch;
    461       1.1   bouyer 	lcd_vspw = panel->panel_timing.vsync_len;
    462       1.1   bouyer 	lcd_vt = panel->panel_timing.vfront_porch + lcd_vspw + lcd_y + lcd_vbp;
    463       1.1   bouyer 
    464       1.1   bouyer 	lcd_io_cfg0 = 0x10000000; /* XXX */
    465       1.1   bouyer 
    466       1.1   bouyer 	if (panel->panel_type == PANEL_DUAL_LVDS)
    467       1.1   bouyer 		dualchan = true;
    468       1.1   bouyer 
    469       1.1   bouyer 	vblk = lcd_vt - lcd_y;
    470       1.1   bouyer 	start_delay = (vblk >= 32) ? 30 : (vblk - 2);
    471       1.1   bouyer 
    472       1.1   bouyer 	if (lcd_dclk_freq > 150000000) /* hardware limit ? */
    473       1.1   bouyer 		lcd_dclk_freq = 150000000;
    474       1.1   bouyer 
    475       1.1   bouyer 	best_diff = INT_MAX;
    476       1.1   bouyer 	best_div = 0;
    477       1.1   bouyer 	best_clk_freq = 0;
    478       1.1   bouyer 	for (u_int div = 7; div <= 15; div++) {
    479       1.1   bouyer 		int dot_freq, diff;
    480       1.1   bouyer 		clk_freq = clk_round_rate(sc->sc_clk_ch0, lcd_dclk_freq * div);
    481       1.1   bouyer 		if (clk_freq == 0)
    482       1.1   bouyer 			continue;
    483       1.1   bouyer 		dot_freq = clk_freq / div;
    484       1.1   bouyer 		diff = abs(lcd_dclk_freq - dot_freq);
    485       1.1   bouyer 		if (best_diff > diff) {
    486       1.1   bouyer 			best_diff = diff;
    487       1.1   bouyer 			best_div = div;
    488       1.1   bouyer 			best_clk_freq = clk_freq;
    489       1.1   bouyer 			if (diff == 0)
    490       1.1   bouyer 				break;
    491       1.1   bouyer 		}
    492       1.1   bouyer 	}
    493       1.1   bouyer 	if (best_clk_freq == 0) {
    494       1.1   bouyer 		device_printf(sc->sc_dev,
    495       1.1   bouyer 		    ": failed to find params for dot clock %d\n",
    496       1.1   bouyer 		    lcd_dclk_freq);
    497       1.1   bouyer 		return EINVAL;
    498       1.1   bouyer 	}
    499       1.1   bouyer 
    500       1.1   bouyer 	error = clk_set_rate(sc->sc_clk_ch0, best_clk_freq);
    501       1.1   bouyer 	if (error) {
    502       1.1   bouyer 		device_printf(sc->sc_dev,
    503       1.1   bouyer 		    ": failed to set ch0 clock to %d for %d: %d\n",
    504       1.1   bouyer 		    best_clk_freq, lcd_dclk_freq, error);
    505       1.1   bouyer 		panic("tcon0 set clk");
    506       1.1   bouyer 	}
    507       1.1   bouyer 	error = clk_enable(sc->sc_clk_ch0);
    508       1.1   bouyer 	if (error) {
    509       1.1   bouyer 		device_printf(sc->sc_dev,
    510       1.1   bouyer 		    ": failed to enable ch0 clock: %d\n", error);
    511       1.1   bouyer 		return EIO;
    512       1.1   bouyer 	}
    513       1.1   bouyer 
    514       1.1   bouyer 	val = __SHIFTIN(start_delay, SUNXI_TCONx_CTL_START_DELAY);
    515       1.1   bouyer 	/*
    516       1.1   bouyer 	 * the DE selector selects the primary DEBE for this tcon:
    517       1.1   bouyer 	 * 0 selects debe0 for tcon0 and debe1 for tcon1
    518       1.1   bouyer 	 */
    519       1.1   bouyer 	val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_DE0,
    520       1.1   bouyer 			 SUNXI_TCONx_CTL_SRC_SEL);
    521       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);
    522       1.1   bouyer 
    523       1.1   bouyer 	val =  (lcd_x - 1) << 16 |  (lcd_y - 1);
    524       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_BASIC0_REG, val);
    525       1.1   bouyer 	val = (lcd_ht - 1) << 16 | (lcd_hbp - 1);
    526       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_BASIC1_REG, val);
    527       1.1   bouyer 	val = (lcd_vt * 2) << 16 | (lcd_vbp - 1);
    528       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_BASIC2_REG, val);
    529       1.1   bouyer 	val =  ((lcd_hspw > 0) ? (lcd_hspw - 1) : 0) << 16;
    530       1.1   bouyer 	val |= ((lcd_vspw > 0) ? (lcd_vspw - 1) : 0);
    531       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_BASIC3_REG, val);
    532       1.1   bouyer 
    533       1.1   bouyer 	val = 0;
    534       1.1   bouyer 	if (dualchan)
    535       1.1   bouyer 		val |= SUNXI_TCON0_LVDS_IF_DUALCHAN;
    536       1.1   bouyer 	if (panel->panel_lvds_format == LVDS_JEIDA_24)
    537       1.1   bouyer 		val |= SUNXI_TCON0_LVDS_IF_MODE_JEIDA;
    538       1.1   bouyer 	if (panel->panel_lvds_format == LVDS_JEIDA_18)
    539       1.1   bouyer 		val |= SUNXI_TCON0_LVDS_IF_18BITS;
    540       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);
    541       1.1   bouyer 
    542       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_IO_POL_REG, lcd_io_cfg0);
    543       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0);
    544       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
    545       1.1   bouyer 	    __SHIFTIN(start_delay + 2, SUNXI_TCON_GINT1_TCON0_LINENO));
    546       1.1   bouyer 
    547       1.1   bouyer 	val = 0xf0000000;
    548       1.1   bouyer 	val &= ~SUNXI_TCON0_DCLK_DIV;
    549       1.1   bouyer 	val |= __SHIFTIN(best_div, SUNXI_TCON0_DCLK_DIV);
    550       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON0_DCLK_REG, val);
    551       1.1   bouyer 
    552       1.1   bouyer 	mode.dot_clock = lcd_dclk_freq;
    553       1.1   bouyer 	mode.hdisplay = lcd_x;
    554       1.1   bouyer 	mode.hsync_start = lcd_ht - lcd_hbp;
    555       1.1   bouyer 	mode.hsync_end = lcd_hspw + mode.hsync_start;
    556       1.1   bouyer 	mode.htotal = lcd_ht;
    557       1.1   bouyer 	mode.vdisplay = lcd_y;
    558       1.1   bouyer 	mode.vsync_start = lcd_vt - lcd_vbp;
    559       1.1   bouyer 	mode.vsync_end = lcd_vspw + mode.vsync_start;
    560       1.1   bouyer 	mode.vtotal = lcd_vt;
    561       1.1   bouyer 	mode.flags = 0;
    562       1.1   bouyer 	mode.name = NULL;
    563       1.1   bouyer 
    564       1.1   bouyer 	sunxi_debe_set_videomode(fdt_endpoint_device(sc->sc_in_rep), &mode);
    565       1.1   bouyer 
    566       1.1   bouyer 	/* XXX
    567       1.1   bouyer 	 * magic values here from linux. these are not documented
    568       1.1   bouyer 	 * in the A20 user manual, and other Allwiner LVDS-capable SoC
    569       1.1   bouyer 	 * documentation don't make sense with these values
    570       1.1   bouyer 	 */
    571       1.1   bouyer 	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
    572       1.1   bouyer 	val |= 0x3F310000;
    573       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
    574       1.1   bouyer 	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
    575       1.1   bouyer 	val |= 1 << 22;
    576       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
    577       1.1   bouyer 	delay(2);
    578       1.1   bouyer 	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA1);
    579       1.1   bouyer 	val |= (0x1f << 26 | 0x1f << 10);
    580       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA1, val);
    581       1.1   bouyer 	delay(2);
    582       1.1   bouyer 	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA1);
    583       1.1   bouyer 	val |= (0x1f << 16 | 0x1f << 0);
    584       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA1, val);
    585       1.1   bouyer 	val = TCON_READ(sc, SUNXI_TCON_LVDS_ANA0);
    586       1.1   bouyer 	val |= 1 << 22;
    587       1.1   bouyer 	TCON_WRITE(sc, SUNXI_TCON_LVDS_ANA0, val);
    588       1.1   bouyer 	return 0;
    589       1.1   bouyer }
    590       1.1   bouyer 
    591       1.1   bouyer static int
    592       1.1   bouyer sunxi_tcon0_enable(struct sunxi_tcon_softc *sc, bool enable)
    593       1.1   bouyer {
    594       1.1   bouyer 	uint32_t val;
    595       1.1   bouyer 	int error;
    596       1.1   bouyer 
    597       1.1   bouyer 	/* turn on/off backlight and lcd  */
    598       1.1   bouyer 	error = fdt_endpoint_enable(sc->sc_out_ep, enable);
    599       1.1   bouyer 	if (error)
    600       1.1   bouyer 		return error;
    601       1.1   bouyer 
    602       1.1   bouyer 	/* and finally disable or enable the tcon */
    603       1.1   bouyer 	error = fdt_endpoint_enable(sc->sc_in_ep, enable);
    604       1.1   bouyer 	if (error)
    605       1.1   bouyer 		return error;
    606       1.1   bouyer 	delay(20000);
    607       1.1   bouyer 	if (enable) {
    608       1.3   bouyer 		if ((error = clk_enable(sc->sc_clk_ch0)) != 0) {
    609       1.3   bouyer 			device_printf(sc->sc_dev,
    610       1.3   bouyer 			    ": couldn't enable ch0 clock\n");
    611       1.3   bouyer 			return error;
    612       1.3   bouyer 		}
    613       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
    614       1.1   bouyer 		val |= SUNXI_TCON_GCTL_EN;
    615       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
    616       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON0_CTL_REG);
    617       1.1   bouyer 		val |= SUNXI_TCONx_CTL_EN;
    618       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);
    619       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON0_LVDS_IF_REG);
    620       1.1   bouyer 		val |= SUNXI_TCON0_LVDS_IF_EN;
    621       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);
    622       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0);
    623       1.1   bouyer 	} else {
    624       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_IO_TRI_REG, 0xffffffff);
    625       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON0_LVDS_IF_REG);
    626       1.1   bouyer 		val &= ~SUNXI_TCON0_LVDS_IF_EN;
    627       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_LVDS_IF_REG, val);
    628       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON0_CTL_REG);
    629       1.1   bouyer 		val &= ~SUNXI_TCONx_CTL_EN;
    630       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON0_CTL_REG, val);
    631       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
    632       1.1   bouyer 		val &= ~SUNXI_TCON_GCTL_EN;
    633       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
    634       1.3   bouyer 		if ((error = clk_disable(sc->sc_clk_ch0)) != 0) {
    635       1.3   bouyer 			device_printf(sc->sc_dev,
    636       1.3   bouyer 			    ": couldn't disable ch0 clock\n");
    637       1.3   bouyer 			return error;
    638       1.3   bouyer 		}
    639       1.1   bouyer 	}
    640       1.1   bouyer #ifdef SUNXI_TCON_DEBUG
    641       1.1   bouyer 	sunxi_tcon_dump_regs(device_unit(sc->sc_dev));
    642       1.1   bouyer #endif
    643       1.1   bouyer 	return 0;
    644       1.1   bouyer }
    645       1.1   bouyer 
    646       1.1   bouyer static int
    647       1.1   bouyer sunxi_tcon1_enable(struct sunxi_tcon_softc *sc, bool enable)
    648       1.1   bouyer {
    649       1.1   bouyer 	uint32_t val;
    650       1.3   bouyer 	int error;
    651       1.1   bouyer 
    652       1.1   bouyer 	KASSERT((sc->sc_output_type == OUTPUT_HDMI) ||
    653       1.1   bouyer 		    (sc->sc_output_type == OUTPUT_VGA));
    654       1.1   bouyer 
    655       1.1   bouyer 	fdt_endpoint_enable(sc->sc_in_ep, enable);
    656       1.1   bouyer 	delay(20000);
    657       1.1   bouyer 	if (enable) {
    658       1.3   bouyer 		if ((error = clk_enable(sc->sc_clk_ch1)) != 0) {
    659       1.3   bouyer 			device_printf(sc->sc_dev,
    660       1.3   bouyer 			    ": couldn't enable ch1 clock\n");
    661       1.3   bouyer 			return error;
    662       1.3   bouyer 		}
    663       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
    664       1.1   bouyer 		val |= SUNXI_TCON_GCTL_EN;
    665       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
    666       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
    667       1.1   bouyer 		val |= SUNXI_TCONx_CTL_EN;
    668       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
    669       1.1   bouyer 		if (sc->sc_output_type == OUTPUT_VGA) {
    670       1.1   bouyer 			TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0x0cffffff);
    671       1.1   bouyer 		} else
    672       1.1   bouyer 			TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0);
    673       1.1   bouyer 	} else {
    674       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_IO_TRI_REG, 0xffffffff);
    675       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
    676       1.1   bouyer 		val &= ~SUNXI_TCONx_CTL_EN;
    677       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
    678       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
    679       1.1   bouyer 		val &= ~SUNXI_TCON_GCTL_EN;
    680       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
    681       1.3   bouyer 		if ((error = clk_disable(sc->sc_clk_ch1)) != 0) {
    682       1.3   bouyer 			device_printf(sc->sc_dev,
    683       1.3   bouyer 			    ": couldn't disable ch1 clock\n");
    684       1.3   bouyer 			return error;
    685       1.3   bouyer 		}
    686       1.1   bouyer 	}
    687       1.1   bouyer 
    688       1.1   bouyer 	KASSERT(tcon_mux_inited);
    689       1.1   bouyer 	val = bus_space_read_4(sc->sc_bst, tcon_mux_bsh, 0);
    690       1.1   bouyer #ifdef SUNXI_TCON_DEBUG
    691       1.1   bouyer 	printf("sunxi_tcon1_enable(%d) %d val 0x%x", sc->sc_unit, enable, val);
    692       1.1   bouyer #endif
    693       1.1   bouyer 	val &= ~ SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC;
    694       1.1   bouyer 	switch(sc->sc_unit) {
    695       1.1   bouyer 	case 0:
    696       1.1   bouyer 		val |= __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_LCDC0_TCON1,
    697       1.1   bouyer 		    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC);
    698       1.1   bouyer 		break;
    699       1.1   bouyer 	case 1:
    700       1.1   bouyer 		val |= __SHIFTIN(SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC_LCDC1_TCON1,
    701       1.1   bouyer 		    SUNXI_TCON_MUX_CTL_HDMI_OUTPUT_SRC);
    702       1.1   bouyer 		break;
    703       1.1   bouyer 	default:
    704       1.1   bouyer 		panic("tcon: invalid unid %d\n", sc->sc_unit);
    705       1.1   bouyer 	}
    706       1.1   bouyer #ifdef SUNXI_TCON_DEBUG
    707       1.1   bouyer 	printf(" -> 0x%x", val);
    708       1.1   bouyer #endif
    709       1.1   bouyer 	bus_space_write_4(sc->sc_bst, tcon_mux_bsh, 0, val);
    710       1.1   bouyer #ifdef SUNXI_TCON_DEBUG
    711       1.1   bouyer 	printf(": 0x%" PRIxBSH " 0x%" PRIxBSH " 0x%x 0x%x\n", sc->sc_bsh,
    712       1.1   bouyer 	    tcon_mux_bsh, bus_space_read_4(sc->sc_bst, tcon_mux_bsh, 0),
    713       1.1   bouyer 	    TCON_READ(sc, SUNXI_TCON_MUX_CTL_REG));
    714       1.1   bouyer #endif
    715       1.1   bouyer 	return 0;
    716       1.1   bouyer }
    717       1.1   bouyer 
    718       1.1   bouyer void
    719       1.1   bouyer sunxi_tcon1_set_videomode(device_t dev, const struct videomode *mode)
    720       1.1   bouyer {
    721       1.1   bouyer 	struct sunxi_tcon_softc *sc = device_private(dev);
    722       1.1   bouyer 	uint32_t val;
    723       1.1   bouyer 	int error;
    724       1.1   bouyer 
    725       1.1   bouyer 	KASSERT(device_is_a(dev, "sunxitcon"));
    726       1.1   bouyer 	KASSERT((sc->sc_output_type == OUTPUT_HDMI) ||
    727       1.1   bouyer 		    (sc->sc_output_type == OUTPUT_VGA));
    728       1.1   bouyer 
    729       1.1   bouyer 	sunxi_debe_set_videomode(fdt_endpoint_device(sc->sc_in_rep), mode);
    730       1.1   bouyer 	if (mode) {
    731       1.1   bouyer 		const u_int interlace_p = !!(mode->flags & VID_INTERLACE);
    732       1.1   bouyer 		const u_int phsync_p = !!(mode->flags & VID_PHSYNC);
    733       1.1   bouyer 		const u_int pvsync_p = !!(mode->flags & VID_PVSYNC);
    734       1.1   bouyer 		const u_int hspw = mode->hsync_end - mode->hsync_start;
    735       1.1   bouyer 		const u_int hbp = mode->htotal - mode->hsync_start;
    736       1.1   bouyer 		const u_int vspw = mode->vsync_end - mode->vsync_start;
    737       1.1   bouyer 		const u_int vbp = mode->vtotal - mode->vsync_start;
    738       1.1   bouyer 		const u_int vblank_len =
    739       1.1   bouyer 		    ((mode->vtotal << interlace_p) >> 1) - mode->vdisplay - 2;
    740       1.1   bouyer 		const u_int start_delay =
    741       1.1   bouyer 		    vblank_len >= 32 ? 30 : vblank_len - 2;
    742       1.1   bouyer 
    743       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON_GCTL_REG);
    744       1.1   bouyer 		val |= SUNXI_TCON_GCTL_IO_MAP_SEL;
    745       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GCTL_REG, val);
    746       1.1   bouyer 
    747       1.1   bouyer 		/* enable */
    748       1.1   bouyer 		val = SUNXI_TCONx_CTL_EN;
    749       1.1   bouyer 		if (interlace_p)
    750       1.1   bouyer 			val |= SUNXI_TCONx_CTL_INTERLACE_EN;
    751       1.1   bouyer 		val |= __SHIFTIN(start_delay, SUNXI_TCONx_CTL_START_DELAY);
    752       1.1   bouyer #ifdef SUNXI_TCON1_BLUEDATA
    753       1.1   bouyer 		val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_BLUEDATA,
    754       1.1   bouyer 				 SUNXI_TCONx_CTL_SRC_SEL);
    755       1.1   bouyer #else
    756       1.1   bouyer 		/*
    757       1.1   bouyer 		 * the DE selector selects the primary DEBE for this tcon:
    758       1.1   bouyer 		 * 0 selects debe0 for tcon0 and debe1 for tcon1
    759       1.1   bouyer 		 */
    760       1.1   bouyer 		val |= __SHIFTIN(SUNXI_TCONx_CTL_SRC_SEL_DE0,
    761       1.1   bouyer 				 SUNXI_TCONx_CTL_SRC_SEL);
    762       1.1   bouyer #endif
    763       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
    764       1.1   bouyer 
    765       1.1   bouyer 		/* Source width/height */
    766       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC0_REG,
    767       1.1   bouyer 		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    768       1.1   bouyer 		/* Scaler width/height */
    769       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC1_REG,
    770       1.1   bouyer 		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    771       1.1   bouyer 		/* Output width/height */
    772       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC2_REG,
    773       1.1   bouyer 		    ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    774       1.1   bouyer 		/* Horizontal total + back porch */
    775       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC3_REG,
    776       1.1   bouyer 		    ((mode->htotal - 1) << 16) | (hbp - 1));
    777       1.1   bouyer 		/* Vertical total + back porch */
    778       1.1   bouyer 		u_int vtotal = mode->vtotal * 2;
    779       1.1   bouyer 		if (interlace_p) {
    780       1.1   bouyer 			u_int framerate =
    781       1.1   bouyer 			    DIVIDE(DIVIDE(mode->dot_clock * 1000, mode->htotal),
    782       1.1   bouyer 			    mode->vtotal);
    783       1.1   bouyer 			u_int clk = mode->htotal * (mode->vtotal * 2 + 1) *
    784       1.1   bouyer 			    framerate;
    785       1.1   bouyer 			if ((clk / 2) == mode->dot_clock * 1000)
    786       1.1   bouyer 				vtotal += 1;
    787       1.1   bouyer 		}
    788       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC4_REG,
    789       1.1   bouyer 		    (vtotal << 16) | (vbp - 1));
    790       1.1   bouyer 
    791       1.1   bouyer 		/* Sync */
    792       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_BASIC5_REG,
    793       1.1   bouyer 		    ((hspw - 1) << 16) | (vspw - 1));
    794       1.1   bouyer 		/* Polarity */
    795       1.1   bouyer 		val = SUNXI_TCON_IO_POL_IO2_INV;
    796       1.1   bouyer 		if (phsync_p)
    797       1.1   bouyer 			val |= SUNXI_TCON_IO_POL_PHSYNC;
    798       1.1   bouyer 		if (pvsync_p)
    799       1.1   bouyer 			val |= SUNXI_TCON_IO_POL_PVSYNC;
    800       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_IO_POL_REG, val);
    801       1.1   bouyer 
    802       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON_GINT1_REG,
    803       1.1   bouyer 		    __SHIFTIN(start_delay + 2, SUNXI_TCON_GINT1_TCON1_LINENO));
    804       1.1   bouyer 
    805       1.1   bouyer 		/* Setup LCDx CH1 PLL */
    806       1.1   bouyer 		error = clk_set_rate(sc->sc_clk_ch1, mode->dot_clock * 1000);
    807       1.1   bouyer 		if (error) {
    808       1.1   bouyer 			device_printf(sc->sc_dev,
    809       1.1   bouyer 			    ": failed to set ch1 clock to %d: %d\n",
    810       1.1   bouyer 			    mode->dot_clock, error);
    811       1.1   bouyer 		}
    812       1.1   bouyer 		error = clk_enable(sc->sc_clk_ch1);
    813       1.1   bouyer 		if (error) {
    814       1.1   bouyer 			device_printf(sc->sc_dev,
    815       1.1   bouyer 			    ": failed to enable ch1 clock: %d\n",
    816       1.1   bouyer 			    error);
    817       1.1   bouyer 		}
    818       1.1   bouyer 	} else {
    819       1.1   bouyer 		/* disable */
    820       1.1   bouyer 		val = TCON_READ(sc, SUNXI_TCON1_CTL_REG);
    821       1.1   bouyer 		val &= ~SUNXI_TCONx_CTL_EN;
    822       1.1   bouyer 		TCON_WRITE(sc, SUNXI_TCON1_CTL_REG, val);
    823       1.1   bouyer 		error = clk_disable(sc->sc_clk_ch1);
    824       1.1   bouyer 		if (error) {
    825       1.1   bouyer 			device_printf(sc->sc_dev,
    826       1.1   bouyer 			    ": failed to disable ch1 clock: %d\n",
    827       1.1   bouyer 			    error);
    828       1.1   bouyer 		}
    829       1.1   bouyer 	}
    830       1.1   bouyer }
    831       1.1   bouyer 
    832       1.6   bouyer /* check if this tcon is the console chosen by firmare */
    833       1.6   bouyer bool
    834       1.6   bouyer sunxi_tcon_is_console(device_t dev, const char *pipeline)
    835       1.6   bouyer {
    836       1.6   bouyer 	struct sunxi_tcon_softc *sc = device_private(dev);
    837       1.6   bouyer 	char p[64];
    838       1.6   bouyer 	char *e, *n = p;
    839       1.6   bouyer 	bool is_console = false;
    840       1.6   bouyer 
    841       1.6   bouyer 	KASSERT(device_is_a(dev, "sunxitcon"));
    842       1.6   bouyer 	strncpy(p, pipeline, sizeof(p) - 1);
    843       1.6   bouyer 	p[sizeof(p) - 1] = '\0';
    844       1.6   bouyer 
    845       1.6   bouyer 	/*
    846       1.6   bouyer 	 * pipeline is like "de_be0-lcd0-hdmi"
    847       1.6   bouyer 	 * of "de_be0-lcd1".
    848       1.6   bouyer 	 * In the first case check output type
    849       1.6   bouyer 	 * In the second check tcon unit number
    850       1.6   bouyer 	 */
    851       1.6   bouyer 	 n = p;
    852       1.6   bouyer 	 e = strsep(&n, "-");
    853       1.6   bouyer 	 if (e == NULL || memcmp(e, "de_be", 5) != 0)
    854       1.6   bouyer 		goto bad;
    855       1.6   bouyer 	 e = strsep(&n, "-");
    856       1.6   bouyer 	 if (e == NULL)
    857       1.6   bouyer 		goto bad;
    858       1.6   bouyer 	 if (n == NULL) {
    859       1.6   bouyer 		/* second case */
    860       1.6   bouyer 		if (strcmp(e, "lcd0") == 0) {
    861       1.6   bouyer 			if (sc->sc_unit == 0)
    862       1.6   bouyer 				is_console = true;
    863       1.6   bouyer 		 } else if (strcmp(e, "lcd1") == 0) {
    864       1.6   bouyer 			if (sc->sc_unit == 1)
    865       1.6   bouyer 				is_console = true;
    866       1.6   bouyer 		} else
    867       1.6   bouyer 			goto bad;
    868       1.6   bouyer 		return is_console;
    869       1.6   bouyer 	}
    870       1.6   bouyer 	/* first case */
    871       1.6   bouyer 	if (strcmp(n, "hdmi") == 0) {
    872       1.6   bouyer 		if (sc->sc_output_type == OUTPUT_HDMI)
    873       1.6   bouyer 			is_console = true;
    874       1.6   bouyer 		return is_console;
    875       1.6   bouyer 	}
    876       1.6   bouyer bad:
    877       1.6   bouyer 	aprint_error("warning: can't parse pipeline %s\n", pipeline);
    878       1.6   bouyer 	return is_console;
    879       1.6   bouyer }
    880       1.6   bouyer 
    881       1.1   bouyer #if defined(DDB) || defined(SUNXI_TCON_DEBUG)
    882       1.1   bouyer void
    883       1.1   bouyer sunxi_tcon_dump_regs(int u)
    884       1.1   bouyer {
    885       1.1   bouyer 	static const struct {
    886       1.1   bouyer 		const char *name;
    887       1.1   bouyer 		uint16_t reg;
    888       1.1   bouyer 	} regs[] = {
    889       1.1   bouyer 		{ "TCON0_BASIC0_REG", SUNXI_TCON0_BASIC0_REG },
    890       1.1   bouyer 		{ "TCON0_BASIC1_REG", SUNXI_TCON0_BASIC1_REG },
    891       1.1   bouyer 		{ "TCON0_BASIC2_REG", SUNXI_TCON0_BASIC2_REG },
    892       1.1   bouyer 		{ "TCON0_BASIC3_REG", SUNXI_TCON0_BASIC3_REG },
    893       1.1   bouyer 		{ "TCON0_CTL_REG", SUNXI_TCON0_CTL_REG },
    894       1.1   bouyer 		{ "TCON0_DCLK_REG", SUNXI_TCON0_DCLK_REG },
    895       1.1   bouyer 		{ "TCON0_IO_POL_REG", SUNXI_TCON0_IO_POL_REG },
    896       1.1   bouyer 		{ "TCON0_IO_TRI_REG", SUNXI_TCON0_IO_TRI_REG },
    897       1.1   bouyer 		{ "TCON0_LVDS_IF_REG", SUNXI_TCON0_LVDS_IF_REG },
    898       1.1   bouyer 		{ "TCON1_BASIC0_REG", SUNXI_TCON1_BASIC0_REG },
    899       1.1   bouyer 		{ "TCON1_BASIC1_REG", SUNXI_TCON1_BASIC1_REG },
    900       1.1   bouyer 		{ "TCON1_BASIC2_REG", SUNXI_TCON1_BASIC2_REG },
    901       1.1   bouyer 		{ "TCON1_BASIC3_REG", SUNXI_TCON1_BASIC3_REG },
    902       1.1   bouyer 		{ "TCON1_BASIC4_REG", SUNXI_TCON1_BASIC4_REG },
    903       1.1   bouyer 		{ "TCON1_BASIC5_REG", SUNXI_TCON1_BASIC5_REG },
    904       1.1   bouyer 		{ "TCON1_CTL_REG", SUNXI_TCON1_CTL_REG },
    905       1.1   bouyer 		{ "TCON1_IO_POL_REG", SUNXI_TCON1_IO_POL_REG },
    906       1.1   bouyer 		{ "TCON1_IO_TRI_REG", SUNXI_TCON1_IO_TRI_REG },
    907       1.1   bouyer 		{ "TCON_GCTL_REG", SUNXI_TCON_GCTL_REG },
    908       1.1   bouyer 		{ "TCON_GINT0_REG", SUNXI_TCON_GINT0_REG },
    909       1.1   bouyer 		{ "TCON_GINT1_REG", SUNXI_TCON_GINT1_REG },
    910       1.1   bouyer 		{ "TCON_MUX_CTL_REG", SUNXI_TCON_MUX_CTL_REG },
    911       1.1   bouyer 	};
    912       1.1   bouyer 	struct sunxi_tcon_softc *sc;
    913       1.1   bouyer 	device_t dev;
    914       1.1   bouyer 
    915       1.1   bouyer 	dev = device_find_by_driver_unit("sunxitcon", u);
    916       1.1   bouyer 	if (dev == NULL)
    917       1.1   bouyer 		return;
    918       1.1   bouyer 	sc = device_private(dev);
    919       1.1   bouyer 
    920       1.1   bouyer 	for (int i = 0; i < __arraycount(regs); i++) {
    921       1.1   bouyer 		printf("%s: 0x%08x\n", regs[i].name,
    922       1.1   bouyer 		    TCON_READ(sc, regs[i].reg));
    923       1.1   bouyer 	}
    924       1.1   bouyer }
    925       1.1   bouyer #endif
    926