Home | History | Annotate | Line # | Download | only in ti
ti_lcdc.c revision 1.15
      1 /* $NetBSD: ti_lcdc.c,v 1.15 2025/10/13 14:12:13 thorpej 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: ti_lcdc.c,v 1.15 2025/10/13 14:12:13 thorpej 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 <uvm/uvm_extern.h>
     41 #include <uvm/uvm_object.h>
     42 #include <uvm/uvm_device.h>
     43 
     44 #include <drm/drm_crtc.h>
     45 #include <drm/drm_crtc_helper.h>
     46 #include <drm/drm_drv.h>
     47 #include <drm/drm_fb_helper.h>
     48 #include <drm/drm_file.h>
     49 #include <drm/drm_fourcc.h>
     50 #include <drm/drm_plane_helper.h>
     51 
     52 #include <dev/fdt/fdtvar.h>
     53 #include <dev/fdt/fdt_port.h>
     54 
     55 #include <arm/ti/ti_prcm.h>
     56 #include <arm/ti/ti_lcdc.h>
     57 #include <arm/ti/ti_lcdcreg.h>
     58 
     59 static const struct device_compatible_entry compat_data[] = {
     60 	{ .compat = "ti,am33xx-tilcdc" },
     61 	DEVICE_COMPAT_EOL
     62 };
     63 
     64 enum {
     65 	TILCDC_PORT_OUTPUT = 0,
     66 };
     67 
     68 static int	tilcdc_match(device_t, cfdata_t, void *);
     69 static void	tilcdc_attach(device_t, device_t, void *);
     70 
     71 static int	tilcdc_load(struct drm_device *, unsigned long);
     72 static void	tilcdc_unload(struct drm_device *);
     73 
     74 static void	tilcdc_drm_task_work(struct work *, void *);
     75 
     76 static struct drm_driver tilcdc_driver = {
     77 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
     78 	.dev_priv_size = 0,
     79 	.load = tilcdc_load,
     80 	.unload = tilcdc_unload,
     81 
     82 	.gem_free_object = drm_gem_cma_free_object,
     83 	.mmap_object = drm_gem_or_legacy_mmap_object,
     84 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     85 
     86 	.dumb_create = drm_gem_cma_dumb_create,
     87 
     88 	.name = DRIVER_NAME,
     89 	.desc = DRIVER_DESC,
     90 	.date = DRIVER_DATE,
     91 	.major = DRIVER_MAJOR,
     92 	.minor = DRIVER_MINOR,
     93 	.patchlevel = DRIVER_PATCHLEVEL,
     94 };
     95 
     96 CFATTACH_DECL_NEW(ti_lcdc, sizeof(struct tilcdc_softc),
     97 	tilcdc_match, tilcdc_attach, NULL, NULL);
     98 
     99 static int
    100 tilcdc_mode_do_set_base(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    101     int x, int y, int atomic)
    102 {
    103 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    104 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    105 	struct tilcdc_framebuffer *sfb = atomic?
    106 	    to_tilcdc_framebuffer(fb) :
    107 	    to_tilcdc_framebuffer(crtc->primary->fb);
    108 
    109 	const uint32_t paddr = (uint32_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
    110 	const u_int psize = sfb->obj->dmamap->dm_segs[0].ds_len;
    111 
    112 	/* Framebuffer start address */
    113 	WR4(sc, LCD_LCDDMA_FB0_BASE, paddr);
    114 	WR4(sc, LCD_LCDDMA_FB0_CEILING, paddr + psize - 1);
    115 
    116 	return 0;
    117 }
    118 
    119 static void
    120 tilcdc_destroy(struct drm_crtc *crtc)
    121 {
    122 	drm_crtc_cleanup(crtc);
    123 }
    124 
    125 static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
    126 	.set_config = drm_crtc_helper_set_config,
    127 	.destroy = tilcdc_destroy,
    128 };
    129 
    130 static void
    131 tilcdc_dpms(struct drm_crtc *crtc, int mode)
    132 {
    133 }
    134 
    135 static bool
    136 tilcdc_mode_fixup(struct drm_crtc *crtc,
    137     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    138 {
    139 #if 0
    140 	adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
    141 	adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
    142 
    143 	adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PHSYNC);
    144 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
    145 		adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
    146 	else
    147 		adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
    148 #endif
    149 
    150 	return true;
    151 }
    152 
    153 static int
    154 tilcdc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
    155     struct drm_display_mode *adjusted_mode, int x, int y,
    156     struct drm_framebuffer *old_fb)
    157 {
    158 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    159 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    160 	int clk_div, div, diff, best_diff;
    161 	uint32_t val;
    162 
    163 	const u_int hspw = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
    164 	const u_int hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
    165 	const u_int hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
    166 	const u_int vspw = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
    167 	const u_int vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
    168 	const u_int vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
    169 
    170 	const u_int rate = clk_get_rate(sc->sc_clk);
    171 
    172 	clk_div = 255;
    173 	best_diff = -1;
    174 	for (div = 2; div < 255; div++) {
    175 		const int pixel_clock = (rate / div) / 1000;
    176 		diff = abs(adjusted_mode->crtc_clock - pixel_clock);
    177 		if (best_diff == -1 || diff < best_diff) {
    178 			best_diff = diff;
    179 			clk_div = div;
    180 		}
    181 	}
    182 	if (clk_div == 255) {
    183 		device_printf(sc->sc_dev, "couldn't configure pixel clock (%u)\n",
    184 		    adjusted_mode->crtc_clock);
    185 		return ERANGE;
    186 	}
    187 
    188 	val = CTRL_RASTER_MODE |
    189 	      (clk_div << CTRL_DIV_SHIFT);
    190 	WR4(sc, LCD_CTRL, val);
    191 
    192 	val = RASTER_TIMING_0_HFP(hfp) |
    193 	      RASTER_TIMING_0_HBP(hbp) |
    194 	      RASTER_TIMING_0_HSW(hspw) |
    195 	      RASTER_TIMING_0_PPL(adjusted_mode->hdisplay);
    196 	WR4(sc, LCD_RASTER_TIMING_0, val);
    197 
    198 	val = RASTER_TIMING_1_VFP(vfp) |
    199 	      RASTER_TIMING_1_VBP(vbp) |
    200 	      RASTER_TIMING_1_VSW(vspw) |
    201 	      RASTER_TIMING_1_LPP(adjusted_mode->vdisplay);
    202 	WR4(sc, LCD_RASTER_TIMING_1, val);
    203 
    204 	val = RASTER_TIMING_2_HFP(hfp) |
    205 	      RASTER_TIMING_2_HBP(hbp) |
    206 	      RASTER_TIMING_2_HSW(hspw) |
    207 	      RASTER_TIMING_2_LPP(adjusted_mode->vdisplay);
    208 	/* XXX TDA HDMI TX */
    209 	val |= RASTER_TIMING_2_IPC;
    210 	val |= RASTER_TIMING_2_PHSVS;
    211 	val |= RASTER_TIMING_2_PHSVS_RISE;
    212 	val |= RASTER_TIMING_2_ACB(255);
    213 	val |= RASTER_TIMING_2_ACBI(0);
    214 	WR4(sc, LCD_RASTER_TIMING_2, val);
    215 
    216 	val = (4 << LCDDMA_CTRL_BURST_SIZE_SHIFT) |
    217 	      (0 << LCDDMA_CTRL_TH_FIFO_RDY_SHIFT) |
    218 	      LCDDMA_CTRL_FB0_ONLY;
    219 	WR4(sc, LCD_LCDDMA_CTRL, val);
    220 
    221 	/* XXX TDA HDMI TX */
    222 	val = RASTER_CTRL_LCDTFT |
    223 	      RASTER_CTRL_TFT24 |
    224 	      RASTER_CTRL_TFT24_UNPACKED |
    225 	      RASTER_CTRL_REQDLY(0x80) |
    226 	      RASTER_CTRL_PALMODE_DATA_ONLY;
    227 	WR4(sc, LCD_RASTER_CTRL, val);
    228 
    229 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    230 
    231 	return 0;
    232 }
    233 
    234 static int
    235 tilcdc_mode_set_base(struct drm_crtc *crtc, int x, int y,
    236     struct drm_framebuffer *old_fb)
    237 {
    238 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    239 
    240 	return 0;
    241 }
    242 
    243 static int
    244 tilcdc_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    245     int x, int y, enum mode_set_atomic state)
    246 {
    247 	tilcdc_mode_do_set_base(crtc, fb, x, y, 1);
    248 
    249 	return 0;
    250 }
    251 
    252 static void
    253 tilcdc_disable(struct drm_crtc *crtc)
    254 {
    255 }
    256 
    257 static void
    258 tilcdc_prepare(struct drm_crtc *crtc)
    259 {
    260 }
    261 
    262 static void
    263 tilcdc_commit(struct drm_crtc *crtc)
    264 {
    265 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    266 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    267 	uint32_t val;
    268 
    269 	WR4(sc, LCD_CLKC_ENABLE, CLKC_ENABLE_DMA | CLKC_ENABLE_CORE);
    270 	WR4(sc, LCD_CLKC_RESET, CLKC_RESET_MAIN);
    271 	delay(100);
    272 	WR4(sc, LCD_CLKC_RESET, 0);
    273 
    274 	val = RD4(sc, LCD_RASTER_CTRL);
    275 	WR4(sc, LCD_RASTER_CTRL, val | RASTER_CTRL_LCDEN);
    276 }
    277 
    278 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
    279 	.dpms = tilcdc_dpms,
    280 	.mode_fixup = tilcdc_mode_fixup,
    281 	.mode_set = tilcdc_mode_set,
    282 	.mode_set_base = tilcdc_mode_set_base,
    283 	.mode_set_base_atomic = tilcdc_mode_set_base_atomic,
    284 	.disable = tilcdc_disable,
    285 	.prepare = tilcdc_prepare,
    286 	.commit = tilcdc_commit,
    287 };
    288 
    289 static void
    290 tilcdc_encoder_destroy(struct drm_encoder *encoder)
    291 {
    292 }
    293 
    294 static const struct drm_encoder_funcs tilcdc_encoder_funcs = {
    295 	.destroy = tilcdc_encoder_destroy,
    296 };
    297 
    298 static void
    299 tilcdc_encoder_dpms(struct drm_encoder *encoder, int mode)
    300 {
    301 }
    302 
    303 static bool
    304 tilcdc_encoder_mode_fixup(struct drm_encoder *encoder,
    305     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    306 {
    307 	return true;
    308 }
    309 
    310 static void
    311 tilcdc_encoder_mode_set(struct drm_encoder *encoder,
    312     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    313 {
    314 }
    315 
    316 static void
    317 tilcdc_encoder_prepare(struct drm_encoder *encoder)
    318 {
    319 }
    320 
    321 static void
    322 tilcdc_encoder_commit(struct drm_encoder *encoder)
    323 {
    324 }
    325 
    326 static const struct drm_encoder_helper_funcs tilcdc_encoder_helper_funcs = {
    327 	.dpms = tilcdc_encoder_dpms,
    328 	.mode_fixup = tilcdc_encoder_mode_fixup,
    329 	.prepare = tilcdc_encoder_prepare,
    330 	.commit = tilcdc_encoder_commit,
    331 	.mode_set = tilcdc_encoder_mode_set,
    332 };
    333 
    334 static int
    335 tilcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    336 {
    337 	struct tilcdc_softc * const sc = device_private(dev);
    338 	struct drm_device *ddev = sc->sc_ddev;
    339 
    340 	if (!activate)
    341 		return EINVAL;
    342 
    343 	sc->sc_crtc.sc = sc;
    344 
    345 	WR4(sc, LCD_SYSCONFIG, SYSCONFIG_STANDBY_SMART | SYSCONFIG_IDLE_SMART);
    346 
    347 	drm_crtc_init(ddev, &sc->sc_crtc.base, &tilcdc_crtc_funcs);
    348 	drm_crtc_helper_add(&sc->sc_crtc.base, &tilcdc_crtc_helper_funcs);
    349 
    350 	sc->sc_encoder.sc = sc;
    351 	sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(&sc->sc_crtc.base);
    352 
    353 	drm_encoder_init(ddev, &sc->sc_encoder.base, &tilcdc_encoder_funcs,
    354 	    DRM_MODE_ENCODER_TMDS, NULL);
    355 	drm_encoder_helper_add(&sc->sc_encoder.base,
    356 	    &tilcdc_encoder_helper_funcs);
    357 
    358 	return fdt_endpoint_activate(ep, activate);
    359 }
    360 
    361 static void *
    362 tilcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    363 {
    364 	struct tilcdc_softc * const sc = device_private(dev);
    365 
    366 	return &sc->sc_encoder.base;
    367 }
    368 
    369 static int
    370 tilcdc_match(device_t parent, cfdata_t cf, void *aux)
    371 {
    372 	struct fdt_attach_args * const faa = aux;
    373 
    374 	return of_compatible_match(faa->faa_phandle, compat_data);
    375 }
    376 
    377 static void
    378 tilcdc_attach(device_t parent, device_t self, void *aux)
    379 {
    380 	struct tilcdc_softc * const sc = device_private(self);
    381 	struct fdt_attach_args * const faa = aux;
    382 	const int phandle = faa->faa_phandle;
    383 	struct drm_driver * const driver = &tilcdc_driver;
    384 	bus_addr_t addr;
    385 	bus_size_t size;
    386 	int error;
    387 
    388 	if (device_getprop_bool(self, "disabled")) {
    389 		aprint_normal(": TI LCDC (disabled)\n");
    390 		return;
    391 	}
    392 
    393 #ifdef WSDISPLAY_MULTICONS
    394 	device_setprop_bool(self, "is_console", true);
    395 #endif
    396 
    397 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    398 		aprint_error(": couldn't get registers\n");
    399 		return;
    400 	}
    401 
    402 	sc->sc_dev = self;
    403 	sc->sc_phandle = faa->faa_phandle;
    404 	sc->sc_dmat = faa->faa_dmat;
    405 	sc->sc_bst = faa->faa_bst;
    406 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    407 		aprint_error(": couldn't map registers\n");
    408 		return;
    409 	}
    410 	sc->sc_clk = ti_prcm_get_hwmod(phandle, 0);
    411 	if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
    412 		aprint_error(": couldn't enable module\n");
    413 		return;
    414 	}
    415 
    416 	aprint_naive("\n");
    417 	aprint_normal(": TI LCDC\n");
    418 
    419 	sc->sc_ports.dp_ep_activate = tilcdc_ep_activate;
    420 	sc->sc_ports.dp_ep_get_data = tilcdc_ep_get_data;
    421 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    422 
    423 	sc->sc_task_thread = NULL;
    424 	SIMPLEQ_INIT(&sc->sc_tasks);
    425 	if (workqueue_create(&sc->sc_task_wq, "tilcdcdrm",
    426 	    &tilcdc_drm_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE)) {
    427 		aprint_error_dev(self, "unable to create workqueue\n");
    428 		sc->sc_task_wq = NULL;
    429 		return;
    430 	}
    431 
    432 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    433 	if (IS_ERR(sc->sc_ddev)) {
    434 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    435 		return;
    436 	}
    437 	sc->sc_ddev->dev_private = sc;
    438 	sc->sc_ddev->bst = sc->sc_bst;
    439 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    440 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    441 	sc->sc_ddev->dmat_subregion_p = false;
    442 
    443 	/*
    444 	 * Cause any tasks issued synchronously during attach to be
    445 	 * processed at the end of this function.
    446 	 */
    447 	sc->sc_task_thread = curlwp;
    448 
    449 	error = -drm_dev_register(sc->sc_ddev, 0);
    450 	if (error) {
    451 		drm_dev_put(sc->sc_ddev);
    452 		sc->sc_ddev = NULL;
    453 		aprint_error_dev(self, "couldn't register DRM device: %d\n",
    454 		    error);
    455 		goto out;
    456 	}
    457 	sc->sc_dev_registered = true;
    458 
    459 	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
    460 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    461 	    driver->date, sc->sc_ddev->primary->index);
    462 
    463 	/*
    464 	 * Process asynchronous tasks queued synchronously during
    465 	 * attach.  This will be for display detection to attach a
    466 	 * framebuffer, so we have the opportunity for a console device
    467 	 * to attach before autoconf has completed, in time for init(8)
    468 	 * to find that console without panicking.
    469 	 */
    470 	while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) {
    471 		struct tilcdc_drm_task *const task =
    472 		    SIMPLEQ_FIRST(&sc->sc_tasks);
    473 
    474 		SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, tdt_u.queue);
    475 		(*task->tdt_fn)(task);
    476 	}
    477 
    478 out:	/* Cause any subsequent tasks to be processed by the workqueue.  */
    479 	atomic_store_relaxed(&sc->sc_task_thread, NULL);
    480 }
    481 
    482 static int
    483 tilcdc_fb_create_handle(struct drm_framebuffer *fb,
    484     struct drm_file *file, unsigned int *handle)
    485 {
    486 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    487 
    488 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    489 }
    490 
    491 static void
    492 tilcdc_fb_destroy(struct drm_framebuffer *fb)
    493 {
    494 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    495 
    496 	drm_framebuffer_cleanup(fb);
    497 	drm_gem_object_put_unlocked(&sfb->obj->base);
    498 	kmem_free(sfb, sizeof(*sfb));
    499 }
    500 
    501 static const struct drm_framebuffer_funcs tilcdc_framebuffer_funcs = {
    502 	.create_handle = tilcdc_fb_create_handle,
    503 	.destroy = tilcdc_fb_destroy,
    504 };
    505 
    506 static struct drm_framebuffer *
    507 tilcdc_fb_create(struct drm_device *ddev, struct drm_file *file,
    508     const struct drm_mode_fb_cmd2 *cmd)
    509 {
    510 	struct tilcdc_framebuffer *fb;
    511 	struct drm_gem_object *gem_obj;
    512 	int error;
    513 
    514 	if (cmd->flags)
    515 		return NULL;
    516 
    517 	gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
    518 	if (gem_obj == NULL)
    519 		return NULL;
    520 
    521 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    522 	drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
    523 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    524 
    525 	error = drm_framebuffer_init(ddev, &fb->base,
    526 	    &tilcdc_framebuffer_funcs);
    527 	if (error != 0)
    528 		goto dealloc;
    529 
    530 	return &fb->base;
    531 
    532 dealloc:
    533 	drm_framebuffer_cleanup(&fb->base);
    534 	kmem_free(fb, sizeof(*fb));
    535 	drm_gem_object_put_unlocked(gem_obj);
    536 
    537 	return NULL;
    538 }
    539 
    540 static struct drm_mode_config_funcs tilcdc_mode_config_funcs = {
    541 	.fb_create = tilcdc_fb_create,
    542 };
    543 
    544 static int
    545 tilcdc_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    546 {
    547 	struct tilcdc_softc * const sc = tilcdc_private(helper->dev);
    548 	struct drm_device *ddev = helper->dev;
    549 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(helper->fb);
    550 	struct drm_framebuffer *fb = helper->fb;
    551 	struct tilcdcfb_attach_args tfa;
    552 	const char *br_wiring;
    553 	uint32_t pixel_format;
    554 	int error;
    555 
    556 	const u_int width = sizes->surface_width;
    557 	const u_int height = sizes->surface_height;
    558 	const u_int pitch = width * (32 / 8);
    559 
    560 	br_wiring = fdtbus_get_string(sc->sc_phandle, "blue-and-red-wiring");
    561 	if (br_wiring && strcmp(br_wiring, "straight") == 0) {
    562 		pixel_format = DRM_FORMAT_XBGR8888;
    563 	} else {
    564 		pixel_format = DRM_FORMAT_XRGB8888;
    565 	}
    566 
    567 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    568 
    569 	sfb->obj = drm_gem_cma_create(ddev, size);
    570 	if (sfb->obj == NULL) {
    571 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    572 		return -ENOMEM;
    573 	}
    574 
    575 	fb->pitches[0] = pitch;
    576 	fb->offsets[0] = 0;
    577 	fb->width = width;
    578 	fb->height = height;
    579 	fb->modifier = 0;
    580 	fb->flags = 0;
    581 	fb->format = drm_format_info(pixel_format);
    582 	fb->dev = ddev;
    583 
    584 	error = drm_framebuffer_init(ddev, fb, &tilcdc_framebuffer_funcs);
    585 	if (error != 0) {
    586 		DRM_ERROR("failed to initialize framebuffer\n");
    587 		return error;
    588 	}
    589 
    590 	memset(&tfa, 0, sizeof(tfa));
    591 	tfa.tfa_drm_dev = ddev;
    592 	tfa.tfa_fb_helper = helper;
    593 	tfa.tfa_fb_sizes = *sizes;
    594 	tfa.tfa_fb_bst = sc->sc_bst;
    595 	tfa.tfa_fb_dmat = sc->sc_dmat;
    596 	tfa.tfa_fb_linebytes = helper->fb->pitches[0];
    597 
    598 	helper->fbdev = config_found(ddev->dev, &tfa, NULL,
    599 	    CFARGS(.iattr = "tilcdcfbbus"));
    600 	if (helper->fbdev == NULL) {
    601 		DRM_ERROR("unable to attach framebuffer\n");
    602 		return -ENXIO;
    603 	}
    604 
    605 	return 0;
    606 }
    607 
    608 static struct drm_fb_helper_funcs tilcdc_fb_helper_funcs = {
    609 	.fb_probe = tilcdc_fb_probe,
    610 };
    611 
    612 static int
    613 tilcdc_load(struct drm_device *ddev, unsigned long flags)
    614 {
    615 	struct tilcdc_softc * const sc = tilcdc_private(ddev);
    616 	struct tilcdc_fbdev *fbdev;
    617 	struct fdt_endpoint *ep;
    618 	int error;
    619 
    620 	drm_mode_config_init(ddev);
    621 	ddev->mode_config.min_width = 0;
    622 	ddev->mode_config.min_height = 0;
    623 	ddev->mode_config.max_width = 2048;
    624 	ddev->mode_config.max_height = 2048;
    625 	ddev->mode_config.funcs = &tilcdc_mode_config_funcs;
    626 
    627 	ep = fdt_endpoint_get_from_index(&sc->sc_ports, TILCDC_PORT_OUTPUT, 0);
    628 	if (ep == NULL) {
    629 		aprint_error_dev(sc->sc_dev, "couldn't find endpoint\n");
    630 		error = ENXIO;
    631 		goto drmerr;
    632 	}
    633 	error = fdt_endpoint_activate_direct(ep, true);
    634 	if (error != 0) {
    635 		aprint_error_dev(sc->sc_dev, "couldn't activate endpoint: %d\n", error);
    636 		error = ENXIO;
    637 		goto drmerr;
    638 	}
    639 
    640 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    641 
    642 	drm_fb_helper_prepare(ddev, &fbdev->helper, &tilcdc_fb_helper_funcs);
    643 
    644 	error = drm_fb_helper_init(ddev, &fbdev->helper, 1);
    645 	if (error)
    646 		goto allocerr;
    647 
    648 	fbdev->helper.fb = kmem_zalloc(sizeof(struct tilcdc_framebuffer),
    649 	    KM_SLEEP);
    650 
    651 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    652 
    653 	drm_helper_disable_unused_functions(ddev);
    654 
    655 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    656 
    657 	return 0;
    658 
    659 allocerr:
    660 	kmem_free(fbdev, sizeof(*fbdev));
    661 drmerr:
    662 	drm_mode_config_cleanup(ddev);
    663 
    664 	return error;
    665 }
    666 
    667 static void
    668 tilcdc_unload(struct drm_device *ddev)
    669 {
    670 
    671 	drm_mode_config_cleanup(ddev);
    672 }
    673 
    674 static void
    675 tilcdc_drm_task_work(struct work *work, void *cookie)
    676 {
    677 	struct tilcdc_drm_task *task = container_of(work,
    678 	    struct tilcdc_drm_task, tdt_u.work);
    679 
    680 	(*task->tdt_fn)(task);
    681 }
    682 
    683 void
    684 tilcdc_task_init(struct tilcdc_drm_task *task,
    685     void (*fn)(struct tilcdc_drm_task *))
    686 {
    687 
    688 	task->tdt_fn = fn;
    689 }
    690 
    691 void
    692 tilcdc_task_schedule(device_t self, struct tilcdc_drm_task *task)
    693 {
    694 	struct tilcdc_softc *sc = device_private(self);
    695 
    696 	if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp)
    697 		SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, tdt_u.queue);
    698 	else
    699 		workqueue_enqueue(sc->sc_task_wq, &task->tdt_u.work, NULL);
    700 }
    701