Home | History | Annotate | Line # | Download | only in ti
ti_lcdc.c revision 1.9
      1  1.9  riastrad /* $NetBSD: ti_lcdc.c,v 1.9 2021/12/19 12:44:25 riastradh Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2019 Jared D. McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.9  riastrad __KERNEL_RCSID(0, "$NetBSD: ti_lcdc.c,v 1.9 2021/12/19 12:44:25 riastradh Exp $");
     31  1.1  jmcneill 
     32  1.1  jmcneill #include <sys/param.h>
     33  1.1  jmcneill #include <sys/bus.h>
     34  1.1  jmcneill #include <sys/device.h>
     35  1.1  jmcneill #include <sys/intr.h>
     36  1.1  jmcneill #include <sys/systm.h>
     37  1.1  jmcneill #include <sys/kernel.h>
     38  1.1  jmcneill #include <sys/conf.h>
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <uvm/uvm_extern.h>
     41  1.1  jmcneill #include <uvm/uvm_object.h>
     42  1.1  jmcneill #include <uvm/uvm_device.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <drm/drm_crtc.h>
     45  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     46  1.9  riastrad #include <drm/drm_drv.h>
     47  1.9  riastrad #include <drm/drm_fb_helper.h>
     48  1.9  riastrad #include <drm/drm_fourcc.h>
     49  1.1  jmcneill #include <drm/drm_plane_helper.h>
     50  1.1  jmcneill 
     51  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     52  1.1  jmcneill #include <dev/fdt/fdt_port.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #include <arm/ti/ti_prcm.h>
     55  1.1  jmcneill #include <arm/ti/ti_lcdc.h>
     56  1.1  jmcneill #include <arm/ti/ti_lcdcreg.h>
     57  1.1  jmcneill 
     58  1.5   thorpej static const struct device_compatible_entry compat_data[] = {
     59  1.5   thorpej 	{ .compat = "ti,am33xx-tilcdc" },
     60  1.5   thorpej 	DEVICE_COMPAT_EOL
     61  1.1  jmcneill };
     62  1.1  jmcneill 
     63  1.1  jmcneill enum {
     64  1.1  jmcneill 	TILCDC_PORT_OUTPUT = 0,
     65  1.1  jmcneill };
     66  1.1  jmcneill 
     67  1.1  jmcneill static int	tilcdc_match(device_t, cfdata_t, void *);
     68  1.1  jmcneill static void	tilcdc_attach(device_t, device_t, void *);
     69  1.1  jmcneill 
     70  1.1  jmcneill static int	tilcdc_load(struct drm_device *, unsigned long);
     71  1.9  riastrad static void	tilcdc_unload(struct drm_device *);
     72  1.1  jmcneill 
     73  1.1  jmcneill static struct drm_driver tilcdc_driver = {
     74  1.9  riastrad 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
     75  1.1  jmcneill 	.dev_priv_size = 0,
     76  1.1  jmcneill 	.load = tilcdc_load,
     77  1.1  jmcneill 	.unload = tilcdc_unload,
     78  1.1  jmcneill 
     79  1.1  jmcneill 	.gem_free_object = drm_gem_cma_free_object,
     80  1.1  jmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
     81  1.1  jmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     82  1.1  jmcneill 
     83  1.1  jmcneill 	.dumb_create = drm_gem_cma_dumb_create,
     84  1.1  jmcneill 
     85  1.1  jmcneill 	.name = DRIVER_NAME,
     86  1.1  jmcneill 	.desc = DRIVER_DESC,
     87  1.1  jmcneill 	.date = DRIVER_DATE,
     88  1.1  jmcneill 	.major = DRIVER_MAJOR,
     89  1.1  jmcneill 	.minor = DRIVER_MINOR,
     90  1.1  jmcneill 	.patchlevel = DRIVER_PATCHLEVEL,
     91  1.1  jmcneill };
     92  1.1  jmcneill 
     93  1.1  jmcneill CFATTACH_DECL_NEW(ti_lcdc, sizeof(struct tilcdc_softc),
     94  1.1  jmcneill 	tilcdc_match, tilcdc_attach, NULL, NULL);
     95  1.1  jmcneill 
     96  1.1  jmcneill static int
     97  1.1  jmcneill tilcdc_mode_do_set_base(struct drm_crtc *crtc, struct drm_framebuffer *fb,
     98  1.1  jmcneill     int x, int y, int atomic)
     99  1.1  jmcneill {
    100  1.1  jmcneill 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    101  1.1  jmcneill 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    102  1.1  jmcneill 	struct tilcdc_framebuffer *sfb = atomic?
    103  1.1  jmcneill 	    to_tilcdc_framebuffer(fb) :
    104  1.1  jmcneill 	    to_tilcdc_framebuffer(crtc->primary->fb);
    105  1.1  jmcneill 
    106  1.1  jmcneill 	const uint32_t paddr = (uint32_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
    107  1.1  jmcneill 	const u_int psize = sfb->obj->dmamap->dm_segs[0].ds_len;
    108  1.1  jmcneill 
    109  1.1  jmcneill 	/* Framebuffer start address */
    110  1.1  jmcneill 	WR4(sc, LCD_LCDDMA_FB0_BASE, paddr);
    111  1.1  jmcneill 	WR4(sc, LCD_LCDDMA_FB0_CEILING, paddr + psize - 1);
    112  1.1  jmcneill 
    113  1.1  jmcneill 	return 0;
    114  1.1  jmcneill }
    115  1.1  jmcneill 
    116  1.1  jmcneill static void
    117  1.1  jmcneill tilcdc_destroy(struct drm_crtc *crtc)
    118  1.1  jmcneill {
    119  1.1  jmcneill 	drm_crtc_cleanup(crtc);
    120  1.1  jmcneill }
    121  1.1  jmcneill 
    122  1.1  jmcneill static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
    123  1.1  jmcneill 	.set_config = drm_crtc_helper_set_config,
    124  1.1  jmcneill 	.destroy = tilcdc_destroy,
    125  1.1  jmcneill };
    126  1.1  jmcneill 
    127  1.1  jmcneill static void
    128  1.1  jmcneill tilcdc_dpms(struct drm_crtc *crtc, int mode)
    129  1.1  jmcneill {
    130  1.1  jmcneill }
    131  1.1  jmcneill 
    132  1.1  jmcneill static bool
    133  1.1  jmcneill tilcdc_mode_fixup(struct drm_crtc *crtc,
    134  1.1  jmcneill     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    135  1.1  jmcneill {
    136  1.2  jmcneill #if 0
    137  1.1  jmcneill 	adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
    138  1.1  jmcneill 	adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
    139  1.1  jmcneill 
    140  1.1  jmcneill 	adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PHSYNC);
    141  1.1  jmcneill 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
    142  1.1  jmcneill 		adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
    143  1.1  jmcneill 	else
    144  1.1  jmcneill 		adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
    145  1.2  jmcneill #endif
    146  1.1  jmcneill 
    147  1.1  jmcneill 	return true;
    148  1.1  jmcneill }
    149  1.1  jmcneill 
    150  1.1  jmcneill static int
    151  1.1  jmcneill tilcdc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
    152  1.1  jmcneill     struct drm_display_mode *adjusted_mode, int x, int y,
    153  1.1  jmcneill     struct drm_framebuffer *old_fb)
    154  1.1  jmcneill {
    155  1.1  jmcneill 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    156  1.1  jmcneill 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    157  1.3  jmcneill 	int clk_div, div, diff, best_diff;
    158  1.1  jmcneill 	uint32_t val;
    159  1.1  jmcneill 
    160  1.1  jmcneill 	const u_int hspw = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
    161  1.1  jmcneill 	const u_int hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
    162  1.1  jmcneill 	const u_int hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
    163  1.1  jmcneill 	const u_int vspw = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
    164  1.1  jmcneill 	const u_int vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
    165  1.1  jmcneill 	const u_int vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
    166  1.1  jmcneill 
    167  1.1  jmcneill 	const u_int rate = clk_get_rate(sc->sc_clk);
    168  1.3  jmcneill 
    169  1.3  jmcneill 	clk_div = 255;
    170  1.3  jmcneill 	best_diff = -1;
    171  1.3  jmcneill 	for (div = 2; div < 255; div++) {
    172  1.3  jmcneill 		const int pixel_clock = (rate / div) / 1000;
    173  1.3  jmcneill 		diff = abs(adjusted_mode->crtc_clock - pixel_clock);
    174  1.3  jmcneill 		if (best_diff == -1 || diff < best_diff) {
    175  1.3  jmcneill 			best_diff = diff;
    176  1.3  jmcneill 			clk_div = div;
    177  1.3  jmcneill 		}
    178  1.1  jmcneill 	}
    179  1.1  jmcneill 	if (clk_div == 255) {
    180  1.1  jmcneill 		device_printf(sc->sc_dev, "couldn't configure pixel clock (%u)\n",
    181  1.1  jmcneill 		    adjusted_mode->crtc_clock);
    182  1.1  jmcneill 		return ERANGE;
    183  1.1  jmcneill 	}
    184  1.1  jmcneill 
    185  1.1  jmcneill 	val = CTRL_RASTER_MODE |
    186  1.1  jmcneill 	      (clk_div << CTRL_DIV_SHIFT);
    187  1.1  jmcneill 	WR4(sc, LCD_CTRL, val);
    188  1.1  jmcneill 
    189  1.1  jmcneill 	val = RASTER_TIMING_0_HFP(hfp) |
    190  1.1  jmcneill 	      RASTER_TIMING_0_HBP(hbp) |
    191  1.1  jmcneill 	      RASTER_TIMING_0_HSW(hspw) |
    192  1.1  jmcneill 	      RASTER_TIMING_0_PPL(adjusted_mode->hdisplay);
    193  1.1  jmcneill 	WR4(sc, LCD_RASTER_TIMING_0, val);
    194  1.1  jmcneill 
    195  1.1  jmcneill 	val = RASTER_TIMING_1_VFP(vfp) |
    196  1.1  jmcneill 	      RASTER_TIMING_1_VBP(vbp) |
    197  1.1  jmcneill 	      RASTER_TIMING_1_VSW(vspw) |
    198  1.1  jmcneill 	      RASTER_TIMING_1_LPP(adjusted_mode->vdisplay);
    199  1.1  jmcneill 	WR4(sc, LCD_RASTER_TIMING_1, val);
    200  1.1  jmcneill 
    201  1.1  jmcneill 	val = RASTER_TIMING_2_HFP(hfp) |
    202  1.1  jmcneill 	      RASTER_TIMING_2_HBP(hbp) |
    203  1.1  jmcneill 	      RASTER_TIMING_2_HSW(hspw) |
    204  1.1  jmcneill 	      RASTER_TIMING_2_LPP(adjusted_mode->vdisplay);
    205  1.1  jmcneill 	/* XXX TDA HDMI TX */
    206  1.1  jmcneill 	val |= RASTER_TIMING_2_IPC;
    207  1.1  jmcneill 	val |= RASTER_TIMING_2_PHSVS;
    208  1.1  jmcneill 	val |= RASTER_TIMING_2_PHSVS_RISE;
    209  1.1  jmcneill 	val |= RASTER_TIMING_2_ACB(255);
    210  1.1  jmcneill 	val |= RASTER_TIMING_2_ACBI(0);
    211  1.1  jmcneill 	WR4(sc, LCD_RASTER_TIMING_2, val);
    212  1.1  jmcneill 
    213  1.1  jmcneill 	val = (4 << LCDDMA_CTRL_BURST_SIZE_SHIFT) |
    214  1.1  jmcneill 	      (0 << LCDDMA_CTRL_TH_FIFO_RDY_SHIFT) |
    215  1.1  jmcneill 	      LCDDMA_CTRL_FB0_ONLY;
    216  1.1  jmcneill 	WR4(sc, LCD_LCDDMA_CTRL, val);
    217  1.1  jmcneill 
    218  1.1  jmcneill 	/* XXX TDA HDMI TX */
    219  1.1  jmcneill 	val = RASTER_CTRL_LCDTFT |
    220  1.1  jmcneill 	      RASTER_CTRL_TFT24 |
    221  1.1  jmcneill 	      RASTER_CTRL_TFT24_UNPACKED |
    222  1.1  jmcneill 	      RASTER_CTRL_REQDLY(0x80) |
    223  1.1  jmcneill 	      RASTER_CTRL_PALMODE_DATA_ONLY;
    224  1.1  jmcneill 	WR4(sc, LCD_RASTER_CTRL, val);
    225  1.1  jmcneill 
    226  1.1  jmcneill 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    227  1.1  jmcneill 
    228  1.1  jmcneill 	return 0;
    229  1.1  jmcneill }
    230  1.1  jmcneill 
    231  1.1  jmcneill static int
    232  1.1  jmcneill tilcdc_mode_set_base(struct drm_crtc *crtc, int x, int y,
    233  1.1  jmcneill     struct drm_framebuffer *old_fb)
    234  1.1  jmcneill {
    235  1.1  jmcneill 	tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
    236  1.1  jmcneill 
    237  1.1  jmcneill 	return 0;
    238  1.1  jmcneill }
    239  1.1  jmcneill 
    240  1.1  jmcneill static int
    241  1.1  jmcneill tilcdc_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
    242  1.1  jmcneill     int x, int y, enum mode_set_atomic state)
    243  1.1  jmcneill {
    244  1.1  jmcneill 	tilcdc_mode_do_set_base(crtc, fb, x, y, 1);
    245  1.1  jmcneill 
    246  1.1  jmcneill 	return 0;
    247  1.1  jmcneill }
    248  1.1  jmcneill 
    249  1.1  jmcneill static void
    250  1.1  jmcneill tilcdc_disable(struct drm_crtc *crtc)
    251  1.1  jmcneill {
    252  1.1  jmcneill }
    253  1.1  jmcneill 
    254  1.1  jmcneill static void
    255  1.1  jmcneill tilcdc_prepare(struct drm_crtc *crtc)
    256  1.1  jmcneill {
    257  1.1  jmcneill }
    258  1.1  jmcneill 
    259  1.1  jmcneill static void
    260  1.1  jmcneill tilcdc_commit(struct drm_crtc *crtc)
    261  1.1  jmcneill {
    262  1.1  jmcneill 	struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
    263  1.1  jmcneill 	struct tilcdc_softc * const sc = mixer_crtc->sc;
    264  1.1  jmcneill 	uint32_t val;
    265  1.1  jmcneill 
    266  1.1  jmcneill 	WR4(sc, LCD_CLKC_ENABLE, CLKC_ENABLE_DMA | CLKC_ENABLE_CORE);
    267  1.1  jmcneill 	WR4(sc, LCD_CLKC_RESET, CLKC_RESET_MAIN);
    268  1.1  jmcneill 	delay(100);
    269  1.1  jmcneill 	WR4(sc, LCD_CLKC_RESET, 0);
    270  1.1  jmcneill 
    271  1.1  jmcneill 	val = RD4(sc, LCD_RASTER_CTRL);
    272  1.1  jmcneill 	WR4(sc, LCD_RASTER_CTRL, val | RASTER_CTRL_LCDEN);
    273  1.1  jmcneill }
    274  1.1  jmcneill 
    275  1.1  jmcneill static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
    276  1.1  jmcneill 	.dpms = tilcdc_dpms,
    277  1.1  jmcneill 	.mode_fixup = tilcdc_mode_fixup,
    278  1.1  jmcneill 	.mode_set = tilcdc_mode_set,
    279  1.1  jmcneill 	.mode_set_base = tilcdc_mode_set_base,
    280  1.1  jmcneill 	.mode_set_base_atomic = tilcdc_mode_set_base_atomic,
    281  1.1  jmcneill 	.disable = tilcdc_disable,
    282  1.1  jmcneill 	.prepare = tilcdc_prepare,
    283  1.1  jmcneill 	.commit = tilcdc_commit,
    284  1.1  jmcneill };
    285  1.1  jmcneill 
    286  1.1  jmcneill static void
    287  1.1  jmcneill tilcdc_encoder_destroy(struct drm_encoder *encoder)
    288  1.1  jmcneill {
    289  1.1  jmcneill }
    290  1.1  jmcneill 
    291  1.1  jmcneill static const struct drm_encoder_funcs tilcdc_encoder_funcs = {
    292  1.1  jmcneill 	.destroy = tilcdc_encoder_destroy,
    293  1.1  jmcneill };
    294  1.1  jmcneill 
    295  1.1  jmcneill static void
    296  1.1  jmcneill tilcdc_encoder_dpms(struct drm_encoder *encoder, int mode)
    297  1.1  jmcneill {
    298  1.1  jmcneill }
    299  1.1  jmcneill 
    300  1.1  jmcneill static bool
    301  1.1  jmcneill tilcdc_encoder_mode_fixup(struct drm_encoder *encoder,
    302  1.1  jmcneill     const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    303  1.1  jmcneill {
    304  1.1  jmcneill 	return true;
    305  1.1  jmcneill }
    306  1.1  jmcneill 
    307  1.1  jmcneill static void
    308  1.1  jmcneill tilcdc_encoder_mode_set(struct drm_encoder *encoder,
    309  1.1  jmcneill     struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
    310  1.1  jmcneill {
    311  1.1  jmcneill }
    312  1.1  jmcneill 
    313  1.1  jmcneill static void
    314  1.1  jmcneill tilcdc_encoder_prepare(struct drm_encoder *encoder)
    315  1.1  jmcneill {
    316  1.1  jmcneill }
    317  1.1  jmcneill 
    318  1.1  jmcneill static void
    319  1.1  jmcneill tilcdc_encoder_commit(struct drm_encoder *encoder)
    320  1.1  jmcneill {
    321  1.1  jmcneill }
    322  1.1  jmcneill 
    323  1.1  jmcneill static const struct drm_encoder_helper_funcs tilcdc_encoder_helper_funcs = {
    324  1.1  jmcneill 	.dpms = tilcdc_encoder_dpms,
    325  1.1  jmcneill 	.mode_fixup = tilcdc_encoder_mode_fixup,
    326  1.1  jmcneill 	.prepare = tilcdc_encoder_prepare,
    327  1.1  jmcneill 	.commit = tilcdc_encoder_commit,
    328  1.1  jmcneill 	.mode_set = tilcdc_encoder_mode_set,
    329  1.1  jmcneill };
    330  1.1  jmcneill 
    331  1.1  jmcneill static int
    332  1.1  jmcneill tilcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
    333  1.1  jmcneill {
    334  1.1  jmcneill 	struct tilcdc_softc * const sc = device_private(dev);
    335  1.1  jmcneill 	struct drm_device *ddev = sc->sc_ddev;
    336  1.1  jmcneill 
    337  1.1  jmcneill 	if (!activate)
    338  1.1  jmcneill 		return EINVAL;
    339  1.1  jmcneill 
    340  1.1  jmcneill 	sc->sc_crtc.sc = sc;
    341  1.1  jmcneill 
    342  1.1  jmcneill 	WR4(sc, LCD_SYSCONFIG, SYSCONFIG_STANDBY_SMART | SYSCONFIG_IDLE_SMART);
    343  1.1  jmcneill 
    344  1.1  jmcneill 	drm_crtc_init(ddev, &sc->sc_crtc.base, &tilcdc_crtc_funcs);
    345  1.1  jmcneill 	drm_crtc_helper_add(&sc->sc_crtc.base, &tilcdc_crtc_helper_funcs);
    346  1.1  jmcneill 
    347  1.1  jmcneill 	sc->sc_encoder.sc = sc;
    348  1.1  jmcneill 	sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(&sc->sc_crtc.base);
    349  1.1  jmcneill 
    350  1.1  jmcneill 	drm_encoder_init(ddev, &sc->sc_encoder.base, &tilcdc_encoder_funcs,
    351  1.9  riastrad 	    DRM_MODE_ENCODER_TMDS, NULL);
    352  1.9  riastrad 	drm_encoder_helper_add(&sc->sc_encoder.base,
    353  1.9  riastrad 	    &tilcdc_encoder_helper_funcs);
    354  1.1  jmcneill 
    355  1.1  jmcneill 	return fdt_endpoint_activate(ep, activate);
    356  1.1  jmcneill }
    357  1.1  jmcneill 
    358  1.1  jmcneill static void *
    359  1.1  jmcneill tilcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
    360  1.1  jmcneill {
    361  1.1  jmcneill 	struct tilcdc_softc * const sc = device_private(dev);
    362  1.1  jmcneill 
    363  1.1  jmcneill 	return &sc->sc_encoder.base;
    364  1.1  jmcneill }
    365  1.1  jmcneill 
    366  1.1  jmcneill static int
    367  1.1  jmcneill tilcdc_match(device_t parent, cfdata_t cf, void *aux)
    368  1.1  jmcneill {
    369  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    370  1.1  jmcneill 
    371  1.5   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    372  1.1  jmcneill }
    373  1.1  jmcneill 
    374  1.1  jmcneill static void
    375  1.1  jmcneill tilcdc_attach(device_t parent, device_t self, void *aux)
    376  1.1  jmcneill {
    377  1.1  jmcneill 	struct tilcdc_softc * const sc = device_private(self);
    378  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    379  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    380  1.1  jmcneill 	struct drm_driver * const driver = &tilcdc_driver;
    381  1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    382  1.1  jmcneill 	bool is_disabled;
    383  1.1  jmcneill 	bus_addr_t addr;
    384  1.1  jmcneill 	bus_size_t size;
    385  1.1  jmcneill 	int error;
    386  1.1  jmcneill 
    387  1.1  jmcneill 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
    388  1.1  jmcneill 		aprint_normal(": TI LCDC (disabled)\n");
    389  1.1  jmcneill 		return;
    390  1.1  jmcneill 	}
    391  1.1  jmcneill 
    392  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    393  1.1  jmcneill 		aprint_error(": couldn't get registers\n");
    394  1.1  jmcneill 		return;
    395  1.1  jmcneill 	}
    396  1.1  jmcneill 
    397  1.1  jmcneill 	sc->sc_dev = self;
    398  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    399  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    400  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    401  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    402  1.1  jmcneill 		aprint_error(": couldn't map registers\n");
    403  1.1  jmcneill 		return;
    404  1.1  jmcneill 	}
    405  1.1  jmcneill 	sc->sc_clk = ti_prcm_get_hwmod(phandle, 0);
    406  1.1  jmcneill 	if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
    407  1.1  jmcneill 		aprint_error(": couldn't enable module\n");
    408  1.1  jmcneill 		return;
    409  1.1  jmcneill 	}
    410  1.1  jmcneill 
    411  1.1  jmcneill 	aprint_naive("\n");
    412  1.1  jmcneill 	aprint_normal(": TI LCDC\n");
    413  1.1  jmcneill 
    414  1.1  jmcneill 	sc->sc_ports.dp_ep_activate = tilcdc_ep_activate;
    415  1.1  jmcneill 	sc->sc_ports.dp_ep_get_data = tilcdc_ep_get_data;
    416  1.1  jmcneill 	fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
    417  1.1  jmcneill 
    418  1.1  jmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    419  1.8  riastrad 	if (IS_ERR(sc->sc_ddev)) {
    420  1.1  jmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    421  1.1  jmcneill 		return;
    422  1.1  jmcneill 	}
    423  1.1  jmcneill 	sc->sc_ddev->dev_private = sc;
    424  1.1  jmcneill 	sc->sc_ddev->bst = sc->sc_bst;
    425  1.1  jmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    426  1.1  jmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    427  1.1  jmcneill 	sc->sc_ddev->dmat_subregion_p = false;
    428  1.1  jmcneill 
    429  1.1  jmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
    430  1.1  jmcneill 	if (error) {
    431  1.9  riastrad 		drm_dev_put(sc->sc_ddev);
    432  1.1  jmcneill 		aprint_error_dev(self, "couldn't register DRM device: %d\n",
    433  1.1  jmcneill 		    error);
    434  1.1  jmcneill 		return;
    435  1.1  jmcneill 	}
    436  1.1  jmcneill 
    437  1.1  jmcneill 	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
    438  1.1  jmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    439  1.1  jmcneill 	    driver->date, sc->sc_ddev->primary->index);
    440  1.1  jmcneill }
    441  1.1  jmcneill 
    442  1.1  jmcneill static int
    443  1.1  jmcneill tilcdc_fb_create_handle(struct drm_framebuffer *fb,
    444  1.1  jmcneill     struct drm_file *file, unsigned int *handle)
    445  1.1  jmcneill {
    446  1.1  jmcneill 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    447  1.1  jmcneill 
    448  1.1  jmcneill 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    449  1.1  jmcneill }
    450  1.1  jmcneill 
    451  1.1  jmcneill static void
    452  1.1  jmcneill tilcdc_fb_destroy(struct drm_framebuffer *fb)
    453  1.1  jmcneill {
    454  1.1  jmcneill 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
    455  1.1  jmcneill 
    456  1.1  jmcneill 	drm_framebuffer_cleanup(fb);
    457  1.9  riastrad 	drm_gem_object_put_unlocked(&sfb->obj->base);
    458  1.1  jmcneill 	kmem_free(sfb, sizeof(*sfb));
    459  1.1  jmcneill }
    460  1.1  jmcneill 
    461  1.1  jmcneill static const struct drm_framebuffer_funcs tilcdc_framebuffer_funcs = {
    462  1.1  jmcneill 	.create_handle = tilcdc_fb_create_handle,
    463  1.1  jmcneill 	.destroy = tilcdc_fb_destroy,
    464  1.1  jmcneill };
    465  1.1  jmcneill 
    466  1.1  jmcneill static struct drm_framebuffer *
    467  1.1  jmcneill tilcdc_fb_create(struct drm_device *ddev, struct drm_file *file,
    468  1.9  riastrad     const struct drm_mode_fb_cmd2 *cmd)
    469  1.1  jmcneill {
    470  1.1  jmcneill 	struct tilcdc_framebuffer *fb;
    471  1.1  jmcneill 	struct drm_gem_object *gem_obj;
    472  1.1  jmcneill 	int error;
    473  1.1  jmcneill 
    474  1.1  jmcneill 	if (cmd->flags)
    475  1.1  jmcneill 		return NULL;
    476  1.1  jmcneill 
    477  1.9  riastrad 	gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
    478  1.1  jmcneill 	if (gem_obj == NULL)
    479  1.1  jmcneill 		return NULL;
    480  1.1  jmcneill 
    481  1.1  jmcneill 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    482  1.9  riastrad 	drm_helper_mode_fill_fb_struct(ddev, &fb->base, cmd);
    483  1.1  jmcneill 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    484  1.1  jmcneill 
    485  1.9  riastrad 	error = drm_framebuffer_init(ddev, &fb->base,
    486  1.9  riastrad 	    &tilcdc_framebuffer_funcs);
    487  1.1  jmcneill 	if (error != 0)
    488  1.1  jmcneill 		goto dealloc;
    489  1.1  jmcneill 
    490  1.1  jmcneill 	return &fb->base;
    491  1.1  jmcneill 
    492  1.1  jmcneill dealloc:
    493  1.1  jmcneill 	drm_framebuffer_cleanup(&fb->base);
    494  1.1  jmcneill 	kmem_free(fb, sizeof(*fb));
    495  1.9  riastrad 	drm_gem_object_put_unlocked(gem_obj);
    496  1.1  jmcneill 
    497  1.1  jmcneill 	return NULL;
    498  1.1  jmcneill }
    499  1.1  jmcneill 
    500  1.1  jmcneill static struct drm_mode_config_funcs tilcdc_mode_config_funcs = {
    501  1.1  jmcneill 	.fb_create = tilcdc_fb_create,
    502  1.1  jmcneill };
    503  1.1  jmcneill 
    504  1.1  jmcneill static int
    505  1.1  jmcneill tilcdc_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    506  1.1  jmcneill {
    507  1.1  jmcneill 	struct tilcdc_softc * const sc = tilcdc_private(helper->dev);
    508  1.1  jmcneill 	struct drm_device *ddev = helper->dev;
    509  1.1  jmcneill 	struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(helper->fb);
    510  1.1  jmcneill 	struct drm_framebuffer *fb = helper->fb;
    511  1.1  jmcneill 	struct tilcdcfb_attach_args tfa;
    512  1.1  jmcneill 	const char *br_wiring;
    513  1.1  jmcneill 	uint32_t pixel_format;
    514  1.1  jmcneill 	int error;
    515  1.1  jmcneill 
    516  1.1  jmcneill 	const u_int width = sizes->surface_width;
    517  1.1  jmcneill 	const u_int height = sizes->surface_height;
    518  1.1  jmcneill 	const u_int pitch = width * (32 / 8);
    519  1.1  jmcneill 
    520  1.1  jmcneill 	br_wiring = fdtbus_get_string(sc->sc_phandle, "blue-and-red-wiring");
    521  1.1  jmcneill 	if (br_wiring && strcmp(br_wiring, "straight") == 0) {
    522  1.1  jmcneill 		pixel_format = DRM_FORMAT_XBGR8888;
    523  1.1  jmcneill 	} else {
    524  1.1  jmcneill 		pixel_format = DRM_FORMAT_XRGB8888;
    525  1.1  jmcneill 	}
    526  1.1  jmcneill 
    527  1.1  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    528  1.1  jmcneill 
    529  1.1  jmcneill 	sfb->obj = drm_gem_cma_create(ddev, size);
    530  1.1  jmcneill 	if (sfb->obj == NULL) {
    531  1.1  jmcneill 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    532  1.1  jmcneill 		return -ENOMEM;
    533  1.1  jmcneill 	}
    534  1.1  jmcneill 
    535  1.1  jmcneill 	fb->pitches[0] = pitch;
    536  1.1  jmcneill 	fb->offsets[0] = 0;
    537  1.1  jmcneill 	fb->width = width;
    538  1.1  jmcneill 	fb->height = height;
    539  1.9  riastrad 	fb->modifier = 0;
    540  1.9  riastrad 	fb->flags = 0;
    541  1.9  riastrad 	fb->format = drm_format_info(pixel_format);
    542  1.9  riastrad 	fb->dev = ddev;
    543  1.1  jmcneill 
    544  1.1  jmcneill 	error = drm_framebuffer_init(ddev, fb, &tilcdc_framebuffer_funcs);
    545  1.1  jmcneill 	if (error != 0) {
    546  1.1  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    547  1.1  jmcneill 		return error;
    548  1.1  jmcneill 	}
    549  1.1  jmcneill 
    550  1.1  jmcneill 	memset(&tfa, 0, sizeof(tfa));
    551  1.1  jmcneill 	tfa.tfa_drm_dev = ddev;
    552  1.1  jmcneill 	tfa.tfa_fb_helper = helper;
    553  1.1  jmcneill 	tfa.tfa_fb_sizes = *sizes;
    554  1.1  jmcneill 	tfa.tfa_fb_bst = sc->sc_bst;
    555  1.1  jmcneill 	tfa.tfa_fb_dmat = sc->sc_dmat;
    556  1.1  jmcneill 	tfa.tfa_fb_linebytes = helper->fb->pitches[0];
    557  1.1  jmcneill 
    558  1.6   thorpej 	helper->fbdev = config_found(ddev->dev, &tfa, NULL,
    559  1.7   thorpej 	    CFARGS(.iattr = "tilcdcfbbus"));
    560  1.1  jmcneill 	if (helper->fbdev == NULL) {
    561  1.1  jmcneill 		DRM_ERROR("unable to attach framebuffer\n");
    562  1.1  jmcneill 		return -ENXIO;
    563  1.1  jmcneill 	}
    564  1.1  jmcneill 
    565  1.1  jmcneill 	return 0;
    566  1.1  jmcneill }
    567  1.1  jmcneill 
    568  1.1  jmcneill static struct drm_fb_helper_funcs tilcdc_fb_helper_funcs = {
    569  1.1  jmcneill 	.fb_probe = tilcdc_fb_probe,
    570  1.1  jmcneill };
    571  1.1  jmcneill 
    572  1.1  jmcneill static int
    573  1.1  jmcneill tilcdc_load(struct drm_device *ddev, unsigned long flags)
    574  1.1  jmcneill {
    575  1.1  jmcneill 	struct tilcdc_softc * const sc = tilcdc_private(ddev);
    576  1.1  jmcneill 	struct tilcdc_fbdev *fbdev;
    577  1.1  jmcneill 	struct fdt_endpoint *ep;
    578  1.1  jmcneill 	int error;
    579  1.1  jmcneill 
    580  1.1  jmcneill 	drm_mode_config_init(ddev);
    581  1.1  jmcneill 	ddev->mode_config.min_width = 0;
    582  1.1  jmcneill 	ddev->mode_config.min_height = 0;
    583  1.1  jmcneill 	ddev->mode_config.max_width = 2048;
    584  1.1  jmcneill 	ddev->mode_config.max_height = 2048;
    585  1.1  jmcneill 	ddev->mode_config.funcs = &tilcdc_mode_config_funcs;
    586  1.1  jmcneill 
    587  1.1  jmcneill 	ep = fdt_endpoint_get_from_index(&sc->sc_ports, TILCDC_PORT_OUTPUT, 0);
    588  1.1  jmcneill 	if (ep == NULL) {
    589  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't find endpoint\n");
    590  1.4       mrg 		error = ENXIO;
    591  1.4       mrg 		goto drmerr;
    592  1.1  jmcneill 	}
    593  1.1  jmcneill 	error = fdt_endpoint_activate_direct(ep, true);
    594  1.1  jmcneill 	if (error != 0) {
    595  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't activate endpoint: %d\n", error);
    596  1.4       mrg 		error = ENXIO;
    597  1.4       mrg 		goto drmerr;
    598  1.1  jmcneill 	}
    599  1.1  jmcneill 
    600  1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    601  1.1  jmcneill 
    602  1.1  jmcneill 	drm_fb_helper_prepare(ddev, &fbdev->helper, &tilcdc_fb_helper_funcs);
    603  1.1  jmcneill 
    604  1.9  riastrad 	error = drm_fb_helper_init(ddev, &fbdev->helper, 1);
    605  1.1  jmcneill 	if (error)
    606  1.4       mrg 		goto allocerr;
    607  1.1  jmcneill 
    608  1.9  riastrad 	fbdev->helper.fb = kmem_zalloc(sizeof(struct tilcdc_framebuffer),
    609  1.9  riastrad 	    KM_SLEEP);
    610  1.1  jmcneill 
    611  1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    612  1.1  jmcneill 
    613  1.1  jmcneill 	drm_helper_disable_unused_functions(ddev);
    614  1.1  jmcneill 
    615  1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    616  1.1  jmcneill 
    617  1.1  jmcneill 	return 0;
    618  1.1  jmcneill 
    619  1.4       mrg allocerr:
    620  1.4       mrg 	kmem_free(fbdev, sizeof(*fbdev));
    621  1.1  jmcneill drmerr:
    622  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    623  1.1  jmcneill 
    624  1.1  jmcneill 	return error;
    625  1.1  jmcneill }
    626  1.1  jmcneill 
    627  1.9  riastrad static void
    628  1.1  jmcneill tilcdc_unload(struct drm_device *ddev)
    629  1.1  jmcneill {
    630  1.9  riastrad 
    631  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    632  1.1  jmcneill }
    633