Home | History | Annotate | Line # | Download | only in sunxi
sunxi_lcdc.c revision 1.4
      1 /* $NetBSD: sunxi_lcdc.c,v 1.4 2019/02/04 12:10:13 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: sunxi_lcdc.c,v 1.4 2019/02/04 12:10:13 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/conf.h>
     39 
     40 #include <drm/drmP.h>
     41 #include <drm/drm_crtc_helper.h>
     42 
     43 #include <dev/fdt/fdtvar.h>
     44 #include <dev/fdt/fdt_port.h>
     45 
     46 #include <arm/sunxi/sunxi_drm.h>
     47 
     48 #define	TCON_GCTL_REG		0x000
     49 #define	 TCON_GCTL_TCON_EN			__BIT(31)
     50 #define	 TCON_GCTL_GAMMA_EN			__BIT(30)
     51 #define	 TCON_GCTL_IO_MAP_SEL			__BIT(0)
     52 #define	TCON_GINT0_REG		0x004
     53 #define	 TCON_GINT0_TCON0_VB_INT_EN		__BIT(31)
     54 #define	 TCON_GINT0_TCON1_VB_INT_EN		__BIT(30)
     55 #define	 TCON_GINT0_TCON0_VB_INT_FLAG		__BIT(15)
     56 #define	 TCON_GINT0_TCON1_VB_INT_FLAG		__BIT(14)
     57 #define	TCON_GINT1_REG		0x008
     58 #define	 TCON_GINT1_TCON1_LINE_INT_NUM		__BITS(11,0)
     59 
     60 #define	TCON0_CTL_REG		0x040
     61 #define	 TCON0_CTL_TCON0_EN			__BIT(31)
     62 #define	 TCON0_CTL_START_DELAY			__BITS(8,4)
     63 #define	 TCON0_CTL_TCON0_SRC_SEL		__BITS(2,0)
     64 #define	TCON0_DCLK_REG		0x044
     65 #define	 TCON0_DCLK_EN				__BITS(31,28)
     66 #define	 TCON0_DCLK_DIV				__BITS(6,0)
     67 #define	TCON0_BASIC0_REG	0x048
     68 #define	TCON0_BASIC1_REG	0x04c
     69 #define	TCON0_BASIC2_REG	0x050
     70 #define	TCON0_BASIC3_REG	0x054
     71 #define	TCON0_IO_POL_REG	0x088
     72 #define	 TCON0_IO_POL_IO_OUTPUT_SEL		__BIT(31)
     73 #define	 TCON0_IO_POL_DCLK_SEL			__BITS(30,28)
     74 #define	 TCON0_IO_POL_IO3_INV			__BIT(27)
     75 #define	 TCON0_IO_POL_IO2_INV			__BIT(26)
     76 #define	 TCON0_IO_POL_IO1_INV			__BIT(25)
     77 #define	 TCON0_IO_POL_IO0_INV			__BIT(24)
     78 #define	 TCON0_IO_POL_DATA_INV			__BITS(23,0)
     79 #define	TCON0_IO_TRI_REG	0x08c
     80 
     81 #define	TCON1_CTL_REG		0x090
     82 #define	 TCON1_CTL_TCON1_EN			__BIT(31)
     83 #define	 TCON1_CTL_START_DELAY			__BITS(8,4)
     84 #define	 TCON1_CTL_TCON1_SRC_SEL		__BIT(1)
     85 #define	TCON1_BASIC0_REG	0x094
     86 #define	TCON1_BASIC1_REG	0x098
     87 #define	TCON1_BASIC2_REG	0x09c
     88 #define	TCON1_BASIC3_REG	0x0a0
     89 #define	TCON1_BASIC4_REG	0x0a4
     90 #define	TCON1_BASIC5_REG	0x0a8
     91 #define	TCON1_IO_POL_REG	0x0f0
     92 #define	 TCON1_IO_POL_IO3_INV			__BIT(27)
     93 #define	 TCON1_IO_POL_IO2_INV			__BIT(26)
     94 #define	 TCON1_IO_POL_IO1_INV			__BIT(25)
     95 #define	 TCON1_IO_POL_IO0_INV			__BIT(24)
     96 #define	 TCON1_IO_POL_DATA_INV			__BITS(23,0)
     97 #define	TCON1_IO_TRI_REG	0x0f4
     98 
     99 enum {
    100 	TCON_PORT_INPUT = 0,
    101 	TCON_PORT_OUTPUT = 1,
    102 };
    103 
    104 enum tcon_type {
    105 	TYPE_TCON0,
    106 	TYPE_TCON1,
    107 };
    108 
    109 static const struct of_compat_data compat_data[] = {
    110 	{ "allwinner,sun8i-h3-tcon-tv",		TYPE_TCON1 },
    111 	{ "allwinner,sun50i-a64-tcon-lcd",	TYPE_TCON0 },
    112 	{ "allwinner,sun50i-a64-tcon-tv",	TYPE_TCON1 },
    113 	{ NULL }
    114 };
    115 
    116 struct sunxi_lcdc_softc;
    117 
    118 struct sunxi_lcdc_encoder {
    119 	struct drm_encoder	base;
    120 	struct sunxi_lcdc_softc *sc;
    121 	struct drm_display_mode	curmode;
    122 };
    123 
    124 struct sunxi_lcdc_softc {
    125 	device_t		sc_dev;
    126 	bus_space_tag_t		sc_bst;
    127 	bus_space_handle_t	sc_bsh;
    128 	int			sc_phandle;
    129 
    130 	enum tcon_type		sc_type;
    131 
    132 	struct clk		*sc_clk_ch[2];
    133 
    134 	struct sunxi_lcdc_encoder sc_encoder;
    135 	struct drm_connector	sc_connector;
    136 
    137 	struct fdt_device_ports	sc_ports;
    138 
    139 	uint32_t		sc_vbl_counter;
    140 };
    141 
    142 #define	to_sunxi_lcdc_encoder(x)	container_of(x, struct sunxi_lcdc_encoder, base)
    143 
    144 #define	TCON_READ(sc, reg)				\
    145 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    146 #define	TCON_WRITE(sc, reg, val)			\
    147 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    148 
    149 static void
    150 sunxi_lcdc_destroy(struct drm_encoder *encoder)
    151 {
    152 }
    153 
    154 static const struct drm_encoder_funcs sunxi_lcdc_funcs = {
    155 	.destroy = sunxi_lcdc_destroy,
    156 };
    157 
    158 static void
    159 sunxi_lcdc_tcon_dpms(struct drm_encoder *encoder, int mode)
    160 {
    161 }
    162 
    163 static bool
    164 sunxi_lcdc_tcon_mode_fixup(struct drm_encoder *encoder,
    165     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    166 {
    167 	return true;
    168 }
    169 
    170 static void
    171 sunxi_lcdc_tcon_mode_set(struct drm_encoder *encoder,
    172     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    173 {
    174 	struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
    175 
    176 	lcdc_encoder->curmode = *adjusted_mode;
    177 }
    178 
    179 static void
    180 sunxi_lcdc_tcon0_prepare(struct drm_encoder *encoder)
    181 {
    182 	struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
    183 	struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
    184 	uint32_t val;
    185 
    186 	val = TCON_READ(sc, TCON_GCTL_REG);
    187 	val |= TCON_GCTL_TCON_EN;
    188 	TCON_WRITE(sc, TCON_GCTL_REG, val);
    189 
    190 	TCON_WRITE(sc, TCON0_IO_TRI_REG, 0);
    191 }
    192 
    193 static void
    194 sunxi_lcdc_tcon1_prepare(struct drm_encoder *encoder)
    195 {
    196 	struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
    197 	struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
    198 	uint32_t val;
    199 
    200 	val = TCON_READ(sc, TCON_GCTL_REG);
    201 	val |= TCON_GCTL_TCON_EN;
    202 	TCON_WRITE(sc, TCON_GCTL_REG, val);
    203 
    204 	TCON_WRITE(sc, TCON1_IO_POL_REG, 0);
    205 	TCON_WRITE(sc, TCON1_IO_TRI_REG, 0xffffffff);
    206 }
    207 
    208 static void
    209 sunxi_lcdc_tcon0_commit(struct drm_encoder *encoder)
    210 {
    211 	struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
    212 	struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
    213 	struct drm_display_mode *mode = &lcdc_encoder->curmode;
    214 	uint32_t val;
    215 	int error;
    216 
    217 	const u_int interlace_p = (mode->flags & DRM_MODE_FLAG_INTERLACE) != 0;
    218 	const u_int hspw = mode->hsync_end - mode->hsync_start;
    219 	const u_int hbp = mode->htotal - mode->hsync_start;
    220 	const u_int vspw = mode->vsync_end - mode->vsync_start;
    221 	const u_int vbp = mode->vtotal - mode->vsync_start;
    222 	const u_int vblank_len =
    223 	    ((mode->vtotal << interlace_p) >> 1) - mode->vdisplay - 2;
    224 	const u_int start_delay =
    225 	    vblank_len >= 32 ? 30 : vblank_len - 2;
    226 
    227 	val = TCON0_CTL_TCON0_EN |
    228 	      __SHIFTIN(start_delay, TCON0_CTL_START_DELAY);
    229 	TCON_WRITE(sc, TCON0_CTL_REG, val);
    230 
    231 	TCON_WRITE(sc, TCON0_BASIC0_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    232 	TCON_WRITE(sc, TCON0_BASIC1_REG, ((mode->htotal - 1) << 16) | (hbp - 1));
    233 	TCON_WRITE(sc, TCON0_BASIC2_REG, ((mode->vtotal * 2) << 16) | (vbp - 1));
    234 	TCON_WRITE(sc, TCON0_BASIC3_REG, ((hspw - 1) << 16) | (vspw - 1));
    235 
    236 	val = TCON_READ(sc, TCON0_IO_POL_REG);
    237 	val &= ~(TCON0_IO_POL_IO3_INV|TCON0_IO_POL_IO2_INV|
    238 		 TCON0_IO_POL_IO1_INV|TCON0_IO_POL_IO0_INV|
    239 		 TCON0_IO_POL_DATA_INV);
    240 	if ((mode->flags & DRM_MODE_FLAG_PHSYNC) == 0)
    241 		val |= TCON0_IO_POL_IO1_INV;
    242 	if ((mode->flags & DRM_MODE_FLAG_PVSYNC) == 0)
    243 		val |= TCON0_IO_POL_IO0_INV;
    244 	TCON_WRITE(sc, TCON0_IO_POL_REG, val);
    245 
    246 	if (sc->sc_clk_ch[0] != NULL) {
    247 		error = clk_set_rate(sc->sc_clk_ch[1], mode->crtc_clock * 1000);
    248 		if (error != 0) {
    249 			device_printf(sc->sc_dev, "failed to set CH0 PLL rate to %u Hz: %d\n",
    250 			    mode->crtc_clock * 1000, error);
    251 			return;
    252 		}
    253 		error = clk_enable(sc->sc_clk_ch[1]);
    254 		if (error != 0) {
    255 			device_printf(sc->sc_dev, "failed to enable CH0 PLL: %d\n", error);
    256 			return;
    257 		}
    258 	} else {
    259 		device_printf(sc->sc_dev, "no CH0 PLL configured\n");
    260 	}
    261 }
    262 
    263 static void
    264 sunxi_lcdc_tcon1_commit(struct drm_encoder *encoder)
    265 {
    266 	struct sunxi_lcdc_encoder *lcdc_encoder = to_sunxi_lcdc_encoder(encoder);
    267 	struct sunxi_lcdc_softc * const sc = lcdc_encoder->sc;
    268 	struct drm_display_mode *mode = &lcdc_encoder->curmode;
    269 	uint32_t val;
    270 	int error;
    271 
    272 	const u_int interlace_p = (mode->flags & DRM_MODE_FLAG_INTERLACE) != 0;
    273 	const u_int hspw = mode->hsync_end - mode->hsync_start;
    274 	const u_int hbp = mode->htotal - mode->hsync_start;
    275 	const u_int vspw = mode->vsync_end - mode->vsync_start;
    276 	const u_int vbp = mode->vtotal - mode->vsync_start;
    277 	const u_int vblank_len =
    278 	    ((mode->vtotal << interlace_p) >> 1) - mode->vdisplay - 2;
    279 	const u_int start_delay =
    280 	    vblank_len >= 32 ? 30 : vblank_len - 2;
    281 
    282 	val = TCON1_CTL_TCON1_EN |
    283 	      __SHIFTIN(start_delay, TCON1_CTL_START_DELAY);
    284 	TCON_WRITE(sc, TCON1_CTL_REG, val);
    285 
    286 	TCON_WRITE(sc, TCON1_BASIC0_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    287 	TCON_WRITE(sc, TCON1_BASIC1_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    288 	TCON_WRITE(sc, TCON1_BASIC2_REG, ((mode->hdisplay - 1) << 16) | (mode->vdisplay - 1));
    289 	TCON_WRITE(sc, TCON1_BASIC3_REG, ((mode->htotal - 1) << 16) | (hbp - 1));
    290 	TCON_WRITE(sc, TCON1_BASIC4_REG, ((mode->vtotal * 2) << 16) | (vbp - 1));
    291 	TCON_WRITE(sc, TCON1_BASIC5_REG, ((hspw - 1) << 16) | (vspw - 1));
    292 
    293 	TCON_WRITE(sc, TCON_GINT1_REG,
    294 	    __SHIFTIN(start_delay + 2, TCON_GINT1_TCON1_LINE_INT_NUM));
    295 
    296 	if (sc->sc_clk_ch[1] != NULL) {
    297 		error = clk_set_rate(sc->sc_clk_ch[1], mode->crtc_clock * 1000);
    298 		if (error != 0) {
    299 			device_printf(sc->sc_dev, "failed to set CH1 PLL rate to %u Hz: %d\n",
    300 			    mode->crtc_clock * 1000, error);
    301 			return;
    302 		}
    303 		error = clk_enable(sc->sc_clk_ch[1]);
    304 		if (error != 0) {
    305 			device_printf(sc->sc_dev, "failed to enable CH1 PLL: %d\n", error);
    306 			return;
    307 		}
    308 	} else {
    309 		device_printf(sc->sc_dev, "no CH1 PLL configured\n");
    310 	}
    311 }
    312 
    313 static const struct drm_encoder_helper_funcs sunxi_lcdc_tcon0_helper_funcs = {
    314 	.dpms = sunxi_lcdc_tcon_dpms,
    315 	.mode_fixup = sunxi_lcdc_tcon_mode_fixup,
    316 	.prepare = sunxi_lcdc_tcon0_prepare,
    317 	.commit = sunxi_lcdc_tcon0_commit,
    318 	.mode_set = sunxi_lcdc_tcon_mode_set,
    319 };
    320 
    321 static const struct drm_encoder_helper_funcs sunxi_lcdc_tcon1_helper_funcs = {
    322 	.dpms = sunxi_lcdc_tcon_dpms,
    323 	.mode_fixup = sunxi_lcdc_tcon_mode_fixup,
    324 	.prepare = sunxi_lcdc_tcon1_prepare,
    325 	.commit = sunxi_lcdc_tcon1_commit,
    326 	.mode_set = sunxi_lcdc_tcon_mode_set,
    327 };
    328 
    329 static int
    330 sunxi_lcdc_encoder_mode(struct fdt_endpoint *out_ep)
    331 {
    332 	struct fdt_endpoint *remote_ep = fdt_endpoint_remote(out_ep);
    333 
    334 	if (remote_ep == NULL)
    335 		return DRM_MODE_ENCODER_NONE;
    336 
    337 	switch (fdt_endpoint_type(remote_ep)) {
    338 	case EP_DRM_BRIDGE:
    339 		return DRM_MODE_ENCODER_TMDS;
    340 	case EP_DRM_PANEL:
    341 		return DRM_MODE_ENCODER_LVDS;
    342 	default:
    343 		return DRM_MODE_ENCODER_NONE;
    344 	}
    345 }
    346 
    347 static uint32_t
    348 sunxi_lcdc_get_vblank_counter(void *priv)
    349 {
    350 	struct sunxi_lcdc_softc * const sc = priv;
    351 
    352 	return sc->sc_vbl_counter;
    353 }
    354 
    355 static void
    356 sunxi_lcdc_enable_vblank(void *priv)
    357 {
    358 	struct sunxi_lcdc_softc * const sc = priv;
    359         const int crtc_index = ffs32(sc->sc_encoder.base.possible_crtcs) - 1;
    360 
    361 	if (crtc_index == 0)
    362 		TCON_WRITE(sc, TCON_GINT0_REG, TCON_GINT0_TCON0_VB_INT_EN);
    363 	else
    364 		TCON_WRITE(sc, TCON_GINT0_REG, TCON_GINT0_TCON1_VB_INT_EN);
    365 }
    366 
    367 static void
    368 sunxi_lcdc_disable_vblank(void *priv)
    369 {
    370 	struct sunxi_lcdc_softc * const sc = priv;
    371 
    372 	TCON_WRITE(sc, TCON_GINT0_REG, 0);
    373 }
    374 
    375 static void
    376 sunxi_lcdc_setup_vblank(struct sunxi_lcdc_softc *sc)
    377 {
    378         const int crtc_index = ffs32(sc->sc_encoder.base.possible_crtcs) - 1;
    379 	struct drm_device *ddev = sc->sc_encoder.base.dev;
    380 	struct sunxi_drm_softc *drm_sc;
    381 
    382 	KASSERT(ddev != NULL);
    383 
    384 	drm_sc = device_private(ddev->dev);
    385 	drm_sc->sc_vbl[crtc_index].priv = sc;
    386 	drm_sc->sc_vbl[crtc_index].get_vblank_counter = sunxi_lcdc_get_vblank_counter;
    387 	drm_sc->sc_vbl[crtc_index].enable_vblank = sunxi_lcdc_enable_vblank;
    388 	drm_sc->sc_vbl[crtc_index].disable_vblank = sunxi_lcdc_disable_vblank;
    389 }
    390 
    391 static int
    392 sunxi_lcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    393 {
    394 	struct sunxi_lcdc_softc * const sc = device_private(dev);
    395 	struct fdt_endpoint *in_ep = fdt_endpoint_remote(ep);
    396 	struct fdt_endpoint *out_ep;
    397 	struct drm_crtc *crtc;
    398 
    399 	if (!activate)
    400 		return EINVAL;
    401 
    402 	if (fdt_endpoint_port_index(ep) != TCON_PORT_INPUT)
    403 		return EINVAL;
    404 
    405 	if (fdt_endpoint_type(in_ep) != EP_DRM_CRTC)
    406 		return EINVAL;
    407 
    408 	crtc = fdt_endpoint_get_data(in_ep);
    409 
    410 	sc->sc_encoder.sc = sc;
    411 	sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(crtc);
    412 
    413 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, TCON_PORT_OUTPUT, 0);
    414 	if (out_ep != NULL) {
    415 		drm_encoder_init(crtc->dev, &sc->sc_encoder.base, &sunxi_lcdc_funcs,
    416 		    sunxi_lcdc_encoder_mode(out_ep));
    417 		drm_encoder_helper_add(&sc->sc_encoder.base, &sunxi_lcdc_tcon0_helper_funcs);
    418 
    419 		sunxi_lcdc_setup_vblank(sc);
    420 
    421 		return fdt_endpoint_activate(out_ep, activate);
    422 	}
    423 
    424 	out_ep = fdt_endpoint_get_from_index(&sc->sc_ports, TCON_PORT_OUTPUT, 1);
    425 	if (out_ep != NULL) {
    426 		drm_encoder_init(crtc->dev, &sc->sc_encoder.base, &sunxi_lcdc_funcs,
    427 		    sunxi_lcdc_encoder_mode(out_ep));
    428 		drm_encoder_helper_add(&sc->sc_encoder.base, &sunxi_lcdc_tcon1_helper_funcs);
    429 
    430 		sunxi_lcdc_setup_vblank(sc);
    431 
    432 		return fdt_endpoint_activate(out_ep, activate);
    433 	}
    434 
    435 	return ENXIO;
    436 }
    437 
    438 static void *
    439 sunxi_lcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    440 {
    441 	struct sunxi_lcdc_softc * const sc = device_private(dev);
    442 
    443 	return &sc->sc_encoder;
    444 }
    445 
    446 static int
    447 sunxi_lcdc_intr(void *priv)
    448 {
    449 	struct sunxi_lcdc_softc * const sc = priv;
    450 	uint32_t val;
    451 	int rv = 0;
    452 
    453 	const int crtc_index = ffs32(sc->sc_encoder.base.possible_crtcs) - 1;
    454 	const uint32_t status_mask = crtc_index == 0 ?
    455 	    TCON_GINT0_TCON0_VB_INT_FLAG : TCON_GINT0_TCON1_VB_INT_FLAG;
    456 
    457 	val = TCON_READ(sc, TCON_GINT0_REG);
    458 	if ((val & status_mask) != 0) {
    459 		TCON_WRITE(sc, TCON_GINT0_REG, val & ~status_mask);
    460 		atomic_inc_32(&sc->sc_vbl_counter);
    461 		drm_handle_vblank(sc->sc_encoder.base.dev, crtc_index);
    462 		rv = 1;
    463 	}
    464 
    465 	return rv;
    466 }
    467 
    468 static int
    469 sunxi_lcdc_match(device_t parent, cfdata_t cf, void *aux)
    470 {
    471 	struct fdt_attach_args * const faa = aux;
    472 
    473 	return of_match_compat_data(faa->faa_phandle, compat_data);
    474 }
    475 
    476 static void
    477 sunxi_lcdc_attach(device_t parent, device_t self, void *aux)
    478 {
    479 	struct sunxi_lcdc_softc * const sc = device_private(self);
    480 	struct fdt_attach_args * const faa = aux;
    481 	const int phandle = faa->faa_phandle;
    482 	struct fdtbus_reset *rst;
    483 	char intrstr[128];
    484 	struct clk *clk;
    485 	bus_addr_t addr;
    486 	bus_size_t size;
    487 	void *ih;
    488 
    489 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    490 		aprint_error(": couldn't get registers\n");
    491 		return;
    492 	}
    493 
    494 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    495 		aprint_error(": couldn't decode interrupt\n");
    496 		return;
    497 	}
    498 
    499 	rst = fdtbus_reset_get(phandle, "lcd");
    500 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    501 		aprint_error(": couldn't de-assert reset\n");
    502 		return;
    503 	}
    504 
    505 	clk = fdtbus_clock_get(phandle, "ahb");
    506 	if (clk == NULL || clk_enable(clk) != 0) {
    507 		aprint_error(": couldn't enable bus clock\n");
    508 		return;
    509 	}
    510 
    511 	sc->sc_dev = self;
    512 	sc->sc_bst = faa->faa_bst;
    513 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    514 		aprint_error(": couldn't map registers\n");
    515 		return;
    516 	}
    517 	sc->sc_phandle = faa->faa_phandle;
    518 	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
    519 	sc->sc_clk_ch[0] = fdtbus_clock_get(phandle, "tcon-ch0");
    520 	sc->sc_clk_ch[1] = fdtbus_clock_get(phandle, "tcon-ch1");
    521 
    522 	aprint_naive("\n");
    523 	switch (sc->sc_type) {
    524 	case TYPE_TCON0:
    525 		aprint_normal(": TCON0\n");
    526 		break;
    527 	case TYPE_TCON1:
    528 		aprint_normal(": TCON1\n");
    529 		break;
    530 	}
    531 
    532 	sc->sc_ports.dp_ep_activate = sunxi_lcdc_ep_activate;
    533 	sc->sc_ports.dp_ep_get_data = sunxi_lcdc_ep_get_data;
    534 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    535 
    536 	ih = fdtbus_intr_establish(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
    537 	    sunxi_lcdc_intr, sc);
    538 	if (ih == NULL) {
    539 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    540 		    intrstr);
    541 		return;
    542 	}
    543 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    544 }
    545 
    546 CFATTACH_DECL_NEW(sunxi_lcdc, sizeof(struct sunxi_lcdc_softc),
    547 	sunxi_lcdc_match, sunxi_lcdc_attach, NULL, NULL);
    548