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