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