Home | History | Annotate | Line # | Download | only in ti
ti_lcdc.c revision 1.6
      1 /* $NetBSD: ti_lcdc.c,v 1.6 2021/04/24 23:36:29 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.6 2021/04/24 23:36:29 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/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 struct device_compatible_entry compat_data[] = {
     58 	{ .compat = "ti,am33xx-tilcdc" },
     59 	DEVICE_COMPAT_EOL
     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 	int clk_div, div, diff, best_diff;
    163 	uint32_t val;
    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 
    174 	clk_div = 255;
    175 	best_diff = -1;
    176 	for (div = 2; div < 255; div++) {
    177 		const int pixel_clock = (rate / div) / 1000;
    178 		diff = abs(adjusted_mode->crtc_clock - pixel_clock);
    179 		if (best_diff == -1 || diff < best_diff) {
    180 			best_diff = diff;
    181 			clk_div = div;
    182 		}
    183 	}
    184 	if (clk_div == 255) {
    185 		device_printf(sc->sc_dev, "couldn't configure pixel clock (%u)\n",
    186 		    adjusted_mode->crtc_clock);
    187 		return ERANGE;
    188 	}
    189 
    190 	val = CTRL_RASTER_MODE |
    191 	      (clk_div << CTRL_DIV_SHIFT);
    192 	WR4(sc, LCD_CTRL, val);
    193 
    194 	val = RASTER_TIMING_0_HFP(hfp) |
    195 	      RASTER_TIMING_0_HBP(hbp) |
    196 	      RASTER_TIMING_0_HSW(hspw) |
    197 	      RASTER_TIMING_0_PPL(adjusted_mode->hdisplay);
    198 	WR4(sc, LCD_RASTER_TIMING_0, val);
    199 
    200 	val = RASTER_TIMING_1_VFP(vfp) |
    201 	      RASTER_TIMING_1_VBP(vbp) |
    202 	      RASTER_TIMING_1_VSW(vspw) |
    203 	      RASTER_TIMING_1_LPP(adjusted_mode->vdisplay);
    204 	WR4(sc, LCD_RASTER_TIMING_1, val);
    205 
    206 	val = RASTER_TIMING_2_HFP(hfp) |
    207 	      RASTER_TIMING_2_HBP(hbp) |
    208 	      RASTER_TIMING_2_HSW(hspw) |
    209 	      RASTER_TIMING_2_LPP(adjusted_mode->vdisplay);
    210 	/* XXX TDA HDMI TX */
    211 	val |= RASTER_TIMING_2_IPC;
    212 	val |= RASTER_TIMING_2_PHSVS;
    213 	val |= RASTER_TIMING_2_PHSVS_RISE;
    214 	val |= RASTER_TIMING_2_ACB(255);
    215 	val |= RASTER_TIMING_2_ACBI(0);
    216 	WR4(sc, LCD_RASTER_TIMING_2, val);
    217 
    218 	val = (4 << LCDDMA_CTRL_BURST_SIZE_SHIFT) |
    219 	      (0 << LCDDMA_CTRL_TH_FIFO_RDY_SHIFT) |
    220 	      LCDDMA_CTRL_FB0_ONLY;
    221 	WR4(sc, LCD_LCDDMA_CTRL, val);
    222 
    223 	/* XXX TDA HDMI TX */
    224 	val = RASTER_CTRL_LCDTFT |
    225 	      RASTER_CTRL_TFT24 |
    226 	      RASTER_CTRL_TFT24_UNPACKED |
    227 	      RASTER_CTRL_REQDLY(0x80) |
    228 	      RASTER_CTRL_PALMODE_DATA_ONLY;
    229 	WR4(sc, LCD_RASTER_CTRL, val);
    230 
    231 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    232 
    233 	return 0;
    234 }
    235 
    236 static int
    237 tilcdc_mode_set_base(struct drm_crtc *crtc, int x, int y,
    238     struct drm_framebuffer *old_fb)
    239 {
    240 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    241 
    242 	return 0;
    243 }
    244 
    245 static int
    246 tilcdc_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    247     int x, int y, enum mode_set_atomic state)
    248 {
    249 	tilcdc_mode_do_set_base(crtc, fb, x, y, 1);
    250 
    251 	return 0;
    252 }
    253 
    254 static void
    255 tilcdc_disable(struct drm_crtc *crtc)
    256 {
    257 }
    258 
    259 static void
    260 tilcdc_prepare(struct drm_crtc *crtc)
    261 {
    262 }
    263 
    264 static void
    265 tilcdc_commit(struct drm_crtc *crtc)
    266 {
    267 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    268 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    269 	uint32_t val;
    270 
    271 	WR4(sc, LCD_CLKC_ENABLE, CLKC_ENABLE_DMA | CLKC_ENABLE_CORE);
    272 	WR4(sc, LCD_CLKC_RESET, CLKC_RESET_MAIN);
    273 	delay(100);
    274 	WR4(sc, LCD_CLKC_RESET, 0);
    275 
    276 	val = RD4(sc, LCD_RASTER_CTRL);
    277 	WR4(sc, LCD_RASTER_CTRL, val | RASTER_CTRL_LCDEN);
    278 }
    279 
    280 static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
    281 	.dpms = tilcdc_dpms,
    282 	.mode_fixup = tilcdc_mode_fixup,
    283 	.mode_set = tilcdc_mode_set,
    284 	.mode_set_base = tilcdc_mode_set_base,
    285 	.mode_set_base_atomic = tilcdc_mode_set_base_atomic,
    286 	.disable = tilcdc_disable,
    287 	.prepare = tilcdc_prepare,
    288 	.commit = tilcdc_commit,
    289 };
    290 
    291 static void
    292 tilcdc_encoder_destroy(struct drm_encoder *encoder)
    293 {
    294 }
    295 
    296 static const struct drm_encoder_funcs tilcdc_encoder_funcs = {
    297 	.destroy = tilcdc_encoder_destroy,
    298 };
    299 
    300 static void
    301 tilcdc_encoder_dpms(struct drm_encoder *encoder, int mode)
    302 {
    303 }
    304 
    305 static bool
    306 tilcdc_encoder_mode_fixup(struct drm_encoder *encoder,
    307     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    308 {
    309 	return true;
    310 }
    311 
    312 static void
    313 tilcdc_encoder_mode_set(struct drm_encoder *encoder,
    314     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    315 {
    316 }
    317 
    318 static void
    319 tilcdc_encoder_prepare(struct drm_encoder *encoder)
    320 {
    321 }
    322 
    323 static void
    324 tilcdc_encoder_commit(struct drm_encoder *encoder)
    325 {
    326 }
    327 
    328 static const struct drm_encoder_helper_funcs tilcdc_encoder_helper_funcs = {
    329 	.dpms = tilcdc_encoder_dpms,
    330 	.mode_fixup = tilcdc_encoder_mode_fixup,
    331 	.prepare = tilcdc_encoder_prepare,
    332 	.commit = tilcdc_encoder_commit,
    333 	.mode_set = tilcdc_encoder_mode_set,
    334 };
    335 
    336 static int
    337 tilcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    338 {
    339 	struct tilcdc_softc * const sc = device_private(dev);
    340 	struct drm_device *ddev = sc->sc_ddev;
    341 
    342 	if (!activate)
    343 		return EINVAL;
    344 
    345 	sc->sc_crtc.sc = sc;
    346 
    347 	WR4(sc, LCD_SYSCONFIG, SYSCONFIG_STANDBY_SMART | SYSCONFIG_IDLE_SMART);
    348 
    349 	drm_crtc_init(ddev, &sc->sc_crtc.base, &tilcdc_crtc_funcs);
    350 	drm_crtc_helper_add(&sc->sc_crtc.base, &tilcdc_crtc_helper_funcs);
    351 
    352 	sc->sc_encoder.sc = sc;
    353 	sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(&sc->sc_crtc.base);
    354 
    355 	drm_encoder_init(ddev, &sc->sc_encoder.base, &tilcdc_encoder_funcs,
    356 	    DRM_MODE_ENCODER_TMDS);
    357 	drm_encoder_helper_add(&sc->sc_encoder.base, &tilcdc_encoder_helper_funcs);
    358 
    359 	return fdt_endpoint_activate(ep, activate);
    360 }
    361 
    362 static void *
    363 tilcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    364 {
    365 	struct tilcdc_softc * const sc = device_private(dev);
    366 
    367 	return &sc->sc_encoder.base;
    368 }
    369 
    370 static int
    371 tilcdc_match(device_t parent, cfdata_t cf, void *aux)
    372 {
    373 	struct fdt_attach_args * const faa = aux;
    374 
    375 	return of_compatible_match(faa->faa_phandle, compat_data);
    376 }
    377 
    378 static void
    379 tilcdc_attach(device_t parent, device_t self, void *aux)
    380 {
    381 	struct tilcdc_softc * const sc = device_private(self);
    382 	struct fdt_attach_args * const faa = aux;
    383 	const int phandle = faa->faa_phandle;
    384 	struct drm_driver * const driver = &tilcdc_driver;
    385 	prop_dictionary_t dict = device_properties(self);
    386 	bool is_disabled;
    387 	bus_addr_t addr;
    388 	bus_size_t size;
    389 	int error;
    390 
    391 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
    392 		aprint_normal(": TI LCDC (disabled)\n");
    393 		return;
    394 	}
    395 
    396 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    397 		aprint_error(": couldn't get registers\n");
    398 		return;
    399 	}
    400 
    401 	sc->sc_dev = self;
    402 	sc->sc_phandle = faa->faa_phandle;
    403 	sc->sc_dmat = faa->faa_dmat;
    404 	sc->sc_bst = faa->faa_bst;
    405 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    406 		aprint_error(": couldn't map registers\n");
    407 		return;
    408 	}
    409 	sc->sc_clk = ti_prcm_get_hwmod(phandle, 0);
    410 	if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
    411 		aprint_error(": couldn't enable module\n");
    412 		return;
    413 	}
    414 
    415 	aprint_naive("\n");
    416 	aprint_normal(": TI LCDC\n");
    417 
    418 	sc->sc_ports.dp_ep_activate = tilcdc_ep_activate;
    419 	sc->sc_ports.dp_ep_get_data = tilcdc_ep_get_data;
    420 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    421 
    422 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    423 	if (sc->sc_ddev == NULL) {
    424 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    425 		return;
    426 	}
    427 	sc->sc_ddev->dev_private = sc;
    428 	sc->sc_ddev->bst = sc->sc_bst;
    429 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    430 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    431 	sc->sc_ddev->dmat_subregion_p = false;
    432 
    433 	error = -drm_dev_register(sc->sc_ddev, 0);
    434 	if (error) {
    435 		drm_dev_unref(sc->sc_ddev);
    436 		aprint_error_dev(self, "couldn't register DRM device: %d\n",
    437 		    error);
    438 		return;
    439 	}
    440 
    441 	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
    442 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    443 	    driver->date, sc->sc_ddev->primary->index);
    444 }
    445 
    446 static int
    447 tilcdc_set_busid(struct drm_device *ddev, struct drm_master *master)
    448 {
    449 	struct tilcdc_softc * const sc = tilcdc_private(ddev);
    450 	char id[32];
    451 
    452 	snprintf(id, sizeof(id), "platform:tilcdc:%u", device_unit(sc->sc_dev));
    453 
    454 	master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
    455 	if (master->unique == NULL)
    456 		return -ENOMEM;
    457 	strcpy(master->unique, id);
    458 	master->unique_len = strlen(master->unique);
    459 
    460 	return 0;
    461 }
    462 
    463 static int
    464 tilcdc_fb_create_handle(struct drm_framebuffer *fb,
    465     struct drm_file *file, unsigned int *handle)
    466 {
    467 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    468 
    469 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    470 }
    471 
    472 static void
    473 tilcdc_fb_destroy(struct drm_framebuffer *fb)
    474 {
    475 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    476 
    477 	drm_framebuffer_cleanup(fb);
    478 	drm_gem_object_unreference_unlocked(&sfb->obj->base);
    479 	kmem_free(sfb, sizeof(*sfb));
    480 }
    481 
    482 static const struct drm_framebuffer_funcs tilcdc_framebuffer_funcs = {
    483 	.create_handle = tilcdc_fb_create_handle,
    484 	.destroy = tilcdc_fb_destroy,
    485 };
    486 
    487 static struct drm_framebuffer *
    488 tilcdc_fb_create(struct drm_device *ddev, struct drm_file *file,
    489     struct drm_mode_fb_cmd2 *cmd)
    490 {
    491 	struct tilcdc_framebuffer *fb;
    492 	struct drm_gem_object *gem_obj;
    493 	int error;
    494 
    495 	if (cmd->flags)
    496 		return NULL;
    497 
    498 	gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
    499 	if (gem_obj == NULL)
    500 		return NULL;
    501 
    502 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    503 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    504 	fb->base.pitches[0] = cmd->pitches[0];
    505 	fb->base.pitches[1] = cmd->pitches[1];
    506 	fb->base.pitches[2] = cmd->pitches[2];
    507 	fb->base.offsets[0] = cmd->offsets[0];
    508 	fb->base.offsets[1] = cmd->offsets[2];
    509 	fb->base.offsets[2] = cmd->offsets[1];
    510 	fb->base.width = cmd->width;
    511 	fb->base.height = cmd->height;
    512 	fb->base.pixel_format = cmd->pixel_format;
    513 	fb->base.bits_per_pixel = drm_format_plane_cpp(fb->base.pixel_format, 0) * 8;
    514 
    515 	switch (fb->base.pixel_format) {
    516 	case DRM_FORMAT_XRGB8888:
    517 	case DRM_FORMAT_XBGR8888:
    518 		fb->base.depth = 32;
    519 		break;
    520 	default:
    521 		break;
    522 	}
    523 
    524 	error = drm_framebuffer_init(ddev, &fb->base, &tilcdc_framebuffer_funcs);
    525 	if (error != 0)
    526 		goto dealloc;
    527 
    528 	return &fb->base;
    529 
    530 dealloc:
    531 	drm_framebuffer_cleanup(&fb->base);
    532 	kmem_free(fb, sizeof(*fb));
    533 	drm_gem_object_unreference_unlocked(gem_obj);
    534 
    535 	return NULL;
    536 }
    537 
    538 static struct drm_mode_config_funcs tilcdc_mode_config_funcs = {
    539 	.fb_create = tilcdc_fb_create,
    540 };
    541 
    542 static int
    543 tilcdc_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    544 {
    545 	struct tilcdc_softc * const sc = tilcdc_private(helper->dev);
    546 	struct drm_device *ddev = helper->dev;
    547 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(helper->fb);
    548 	struct drm_framebuffer *fb = helper->fb;
    549 	struct tilcdcfb_attach_args tfa;
    550 	const char *br_wiring;
    551 	uint32_t pixel_format;
    552 	int error;
    553 
    554 	const u_int width = sizes->surface_width;
    555 	const u_int height = sizes->surface_height;
    556 	const u_int pitch = width * (32 / 8);
    557 
    558 	br_wiring = fdtbus_get_string(sc->sc_phandle, "blue-and-red-wiring");
    559 	if (br_wiring && strcmp(br_wiring, "straight") == 0) {
    560 		pixel_format = DRM_FORMAT_XBGR8888;
    561 	} else {
    562 		pixel_format = DRM_FORMAT_XRGB8888;
    563 	}
    564 
    565 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    566 
    567 	sfb->obj = drm_gem_cma_create(ddev, size);
    568 	if (sfb->obj == NULL) {
    569 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    570 		return -ENOMEM;
    571 	}
    572 
    573 	fb->pitches[0] = pitch;
    574 	fb->offsets[0] = 0;
    575 	fb->width = width;
    576 	fb->height = height;
    577 	fb->pixel_format = pixel_format;
    578 	drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
    579 
    580 	error = drm_framebuffer_init(ddev, fb, &tilcdc_framebuffer_funcs);
    581 	if (error != 0) {
    582 		DRM_ERROR("failed to initialize framebuffer\n");
    583 		return error;
    584 	}
    585 
    586 	memset(&tfa, 0, sizeof(tfa));
    587 	tfa.tfa_drm_dev = ddev;
    588 	tfa.tfa_fb_helper = helper;
    589 	tfa.tfa_fb_sizes = *sizes;
    590 	tfa.tfa_fb_bst = sc->sc_bst;
    591 	tfa.tfa_fb_dmat = sc->sc_dmat;
    592 	tfa.tfa_fb_linebytes = helper->fb->pitches[0];
    593 
    594 	helper->fbdev = config_found(ddev->dev, &tfa, NULL,
    595 	    CFARG_IATTR, "tilcdcfbbus",
    596 	    CFARG_EOL);
    597 	if (helper->fbdev == NULL) {
    598 		DRM_ERROR("unable to attach framebuffer\n");
    599 		return -ENXIO;
    600 	}
    601 
    602 	return 0;
    603 }
    604 
    605 static struct drm_fb_helper_funcs tilcdc_fb_helper_funcs = {
    606 	.fb_probe = tilcdc_fb_probe,
    607 };
    608 
    609 static int
    610 tilcdc_load(struct drm_device *ddev, unsigned long flags)
    611 {
    612 	struct tilcdc_softc * const sc = tilcdc_private(ddev);
    613 	struct tilcdc_fbdev *fbdev;
    614 	struct fdt_endpoint *ep;
    615 	int error;
    616 
    617 	drm_mode_config_init(ddev);
    618 	ddev->mode_config.min_width = 0;
    619 	ddev->mode_config.min_height = 0;
    620 	ddev->mode_config.max_width = 2048;
    621 	ddev->mode_config.max_height = 2048;
    622 	ddev->mode_config.funcs = &tilcdc_mode_config_funcs;
    623 
    624 	ep = fdt_endpoint_get_from_index(&sc->sc_ports, TILCDC_PORT_OUTPUT, 0);
    625 	if (ep == NULL) {
    626 		aprint_error_dev(sc->sc_dev, "couldn't find endpoint\n");
    627 		error = ENXIO;
    628 		goto drmerr;
    629 	}
    630 	error = fdt_endpoint_activate_direct(ep, true);
    631 	if (error != 0) {
    632 		aprint_error_dev(sc->sc_dev, "couldn't activate endpoint: %d\n", error);
    633 		error = ENXIO;
    634 		goto drmerr;
    635 	}
    636 
    637 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    638 
    639 	drm_fb_helper_prepare(ddev, &fbdev->helper, &tilcdc_fb_helper_funcs);
    640 
    641 	error = drm_fb_helper_init(ddev, &fbdev->helper, 1, 1);
    642 	if (error)
    643 		goto allocerr;
    644 
    645 	fbdev->helper.fb = kmem_zalloc(sizeof(struct tilcdc_framebuffer), KM_SLEEP);
    646 
    647 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    648 
    649 	drm_helper_disable_unused_functions(ddev);
    650 
    651 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    652 
    653 	return 0;
    654 
    655 allocerr:
    656 	kmem_free(fbdev, sizeof(*fbdev));
    657 drmerr:
    658 	drm_mode_config_cleanup(ddev);
    659 
    660 	return error;
    661 }
    662 
    663 static int
    664 tilcdc_unload(struct drm_device *ddev)
    665 {
    666 	drm_mode_config_cleanup(ddev);
    667 
    668 	return 0;
    669 }
    670