Home | History | Annotate | Line # | Download | only in rockchip
rk_drm.c revision 1.8
      1  1.8  riastrad /* $NetBSD: rk_drm.c,v 1.8 2021/12/19 11:00:46 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.8  riastrad __KERNEL_RCSID(0, "$NetBSD: rk_drm.c,v 1.8 2021/12/19 11:00:46 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.8  riastrad #include <drm/drm_auth.h>
     45  1.8  riastrad #include <drm/drm_drv.h>
     46  1.1  jmcneill #include <drm/drm_crtc_helper.h>
     47  1.1  jmcneill #include <drm/drm_fb_helper.h>
     48  1.8  riastrad #include <drm/drm_fourcc.h>
     49  1.8  riastrad #include <drm/drm_vblank.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/rockchip/rk_drm.h>
     55  1.1  jmcneill 
     56  1.1  jmcneill #define	RK_DRM_MAX_WIDTH	3840
     57  1.1  jmcneill #define	RK_DRM_MAX_HEIGHT	2160
     58  1.1  jmcneill 
     59  1.1  jmcneill static TAILQ_HEAD(, rk_drm_ports) rk_drm_ports =
     60  1.1  jmcneill     TAILQ_HEAD_INITIALIZER(rk_drm_ports);
     61  1.1  jmcneill 
     62  1.4   thorpej static const struct device_compatible_entry compat_data[] = {
     63  1.4   thorpej 	{ .compat = "rockchip,display-subsystem" },
     64  1.4   thorpej 	DEVICE_COMPAT_EOL
     65  1.1  jmcneill };
     66  1.1  jmcneill 
     67  1.1  jmcneill static const char * fb_compatible[] = {
     68  1.1  jmcneill 	"simple-framebuffer",
     69  1.1  jmcneill 	NULL
     70  1.1  jmcneill };
     71  1.1  jmcneill 
     72  1.1  jmcneill static int	rk_drm_match(device_t, cfdata_t, void *);
     73  1.1  jmcneill static void	rk_drm_attach(device_t, device_t, void *);
     74  1.1  jmcneill 
     75  1.1  jmcneill static void	rk_drm_init(device_t);
     76  1.1  jmcneill static vmem_t	*rk_drm_alloc_cma_pool(struct drm_device *, size_t);
     77  1.1  jmcneill 
     78  1.1  jmcneill static int	rk_drm_set_busid(struct drm_device *, struct drm_master *);
     79  1.1  jmcneill 
     80  1.1  jmcneill static uint32_t	rk_drm_get_vblank_counter(struct drm_device *, unsigned int);
     81  1.1  jmcneill static int	rk_drm_enable_vblank(struct drm_device *, unsigned int);
     82  1.1  jmcneill static void	rk_drm_disable_vblank(struct drm_device *, unsigned int);
     83  1.1  jmcneill 
     84  1.1  jmcneill static int	rk_drm_load(struct drm_device *, unsigned long);
     85  1.8  riastrad static void	rk_drm_unload(struct drm_device *);
     86  1.1  jmcneill 
     87  1.1  jmcneill static struct drm_driver rk_drm_driver = {
     88  1.8  riastrad 	.driver_features = DRIVER_MODESET | DRIVER_GEM,
     89  1.1  jmcneill 	.dev_priv_size = 0,
     90  1.1  jmcneill 	.load = rk_drm_load,
     91  1.1  jmcneill 	.unload = rk_drm_unload,
     92  1.1  jmcneill 
     93  1.1  jmcneill 	.gem_free_object = drm_gem_cma_free_object,
     94  1.1  jmcneill 	.mmap_object = drm_gem_or_legacy_mmap_object,
     95  1.1  jmcneill 	.gem_uvm_ops = &drm_gem_cma_uvm_ops,
     96  1.1  jmcneill 
     97  1.1  jmcneill 	.dumb_create = drm_gem_cma_dumb_create,
     98  1.1  jmcneill 	.dumb_destroy = drm_gem_dumb_destroy,
     99  1.1  jmcneill 
    100  1.1  jmcneill 	.get_vblank_counter = rk_drm_get_vblank_counter,
    101  1.1  jmcneill 	.enable_vblank = rk_drm_enable_vblank,
    102  1.1  jmcneill 	.disable_vblank = rk_drm_disable_vblank,
    103  1.1  jmcneill 
    104  1.1  jmcneill 	.name = DRIVER_NAME,
    105  1.1  jmcneill 	.desc = DRIVER_DESC,
    106  1.1  jmcneill 	.date = DRIVER_DATE,
    107  1.1  jmcneill 	.major = DRIVER_MAJOR,
    108  1.1  jmcneill 	.minor = DRIVER_MINOR,
    109  1.1  jmcneill 	.patchlevel = DRIVER_PATCHLEVEL,
    110  1.1  jmcneill 
    111  1.1  jmcneill 	.set_busid = rk_drm_set_busid,
    112  1.1  jmcneill };
    113  1.1  jmcneill 
    114  1.1  jmcneill CFATTACH_DECL_NEW(rk_drm, sizeof(struct rk_drm_softc),
    115  1.1  jmcneill 	rk_drm_match, rk_drm_attach, NULL, NULL);
    116  1.1  jmcneill 
    117  1.1  jmcneill static int
    118  1.1  jmcneill rk_drm_match(device_t parent, cfdata_t cf, void *aux)
    119  1.1  jmcneill {
    120  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    121  1.1  jmcneill 
    122  1.4   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    123  1.1  jmcneill }
    124  1.1  jmcneill 
    125  1.1  jmcneill static void
    126  1.1  jmcneill rk_drm_attach(device_t parent, device_t self, void *aux)
    127  1.1  jmcneill {
    128  1.1  jmcneill 	struct rk_drm_softc * const sc = device_private(self);
    129  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    130  1.1  jmcneill 	struct drm_driver * const driver = &rk_drm_driver;
    131  1.1  jmcneill 	prop_dictionary_t dict = device_properties(self);
    132  1.1  jmcneill 	bool is_disabled;
    133  1.1  jmcneill 
    134  1.1  jmcneill 	sc->sc_dev = self;
    135  1.1  jmcneill 	sc->sc_dmat = faa->faa_dmat;
    136  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    137  1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    138  1.1  jmcneill 
    139  1.1  jmcneill 	aprint_naive("\n");
    140  1.1  jmcneill 
    141  1.1  jmcneill 	if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
    142  1.1  jmcneill 		aprint_normal(": (disabled)\n");
    143  1.1  jmcneill 		return;
    144  1.1  jmcneill 	}
    145  1.1  jmcneill 
    146  1.1  jmcneill 	aprint_normal("\n");
    147  1.1  jmcneill 
    148  1.1  jmcneill 	sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
    149  1.1  jmcneill 	if (sc->sc_ddev == NULL) {
    150  1.1  jmcneill 		aprint_error_dev(self, "couldn't allocate DRM device\n");
    151  1.1  jmcneill 		return;
    152  1.1  jmcneill 	}
    153  1.1  jmcneill 	sc->sc_ddev->dev_private = sc;
    154  1.1  jmcneill 	sc->sc_ddev->bst = sc->sc_bst;
    155  1.1  jmcneill 	sc->sc_ddev->bus_dmat = sc->sc_dmat;
    156  1.1  jmcneill 	sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
    157  1.1  jmcneill 	sc->sc_ddev->dmat_subregion_p = false;
    158  1.1  jmcneill 
    159  1.1  jmcneill 	fdt_remove_bycompat(fb_compatible);
    160  1.1  jmcneill 
    161  1.1  jmcneill 	config_defer(self, rk_drm_init);
    162  1.1  jmcneill }
    163  1.1  jmcneill 
    164  1.1  jmcneill static void
    165  1.1  jmcneill rk_drm_init(device_t dev)
    166  1.1  jmcneill {
    167  1.1  jmcneill 	struct rk_drm_softc * const sc = device_private(dev);
    168  1.1  jmcneill 	struct drm_driver * const driver = &rk_drm_driver;
    169  1.1  jmcneill 	int error;
    170  1.1  jmcneill 
    171  1.1  jmcneill 	error = -drm_dev_register(sc->sc_ddev, 0);
    172  1.1  jmcneill 	if (error) {
    173  1.1  jmcneill 		aprint_error_dev(dev, "couldn't register DRM device: %d\n",
    174  1.1  jmcneill 		    error);
    175  1.1  jmcneill 		return;
    176  1.1  jmcneill 	}
    177  1.1  jmcneill 
    178  1.1  jmcneill 	aprint_normal_dev(dev, "initialized %s %d.%d.%d %s on minor %d\n",
    179  1.1  jmcneill 	    driver->name, driver->major, driver->minor, driver->patchlevel,
    180  1.1  jmcneill 	    driver->date, sc->sc_ddev->primary->index);
    181  1.1  jmcneill }
    182  1.1  jmcneill 
    183  1.1  jmcneill static vmem_t *
    184  1.1  jmcneill rk_drm_alloc_cma_pool(struct drm_device *ddev, size_t cma_size)
    185  1.1  jmcneill {
    186  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    187  1.1  jmcneill 	bus_dma_segment_t segs[1];
    188  1.1  jmcneill 	int nsegs;
    189  1.1  jmcneill 	int error;
    190  1.1  jmcneill 
    191  1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, cma_size, PAGE_SIZE, 0,
    192  1.1  jmcneill 	    segs, 1, &nsegs, BUS_DMA_NOWAIT);
    193  1.1  jmcneill 	if (error) {
    194  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't allocate CMA pool\n");
    195  1.1  jmcneill 		return NULL;
    196  1.1  jmcneill 	}
    197  1.1  jmcneill 
    198  1.1  jmcneill 	return vmem_create("rkdrm", segs[0].ds_addr, segs[0].ds_len,
    199  1.1  jmcneill 	    PAGE_SIZE, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
    200  1.1  jmcneill }
    201  1.1  jmcneill 
    202  1.1  jmcneill static int
    203  1.1  jmcneill rk_drm_set_busid(struct drm_device *ddev, struct drm_master *master)
    204  1.1  jmcneill {
    205  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    206  1.1  jmcneill 	char id[32];
    207  1.1  jmcneill 
    208  1.1  jmcneill 	snprintf(id, sizeof(id), "platform:rk:%u", device_unit(sc->sc_dev));
    209  1.1  jmcneill 
    210  1.1  jmcneill 	master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
    211  1.1  jmcneill 	if (master->unique == NULL)
    212  1.1  jmcneill 		return -ENOMEM;
    213  1.1  jmcneill 	strcpy(master->unique, id);
    214  1.1  jmcneill 	master->unique_len = strlen(master->unique);
    215  1.1  jmcneill 
    216  1.1  jmcneill 	return 0;
    217  1.1  jmcneill }
    218  1.1  jmcneill 
    219  1.1  jmcneill static int
    220  1.1  jmcneill rk_drm_fb_create_handle(struct drm_framebuffer *fb,
    221  1.1  jmcneill     struct drm_file *file, unsigned int *handle)
    222  1.1  jmcneill {
    223  1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
    224  1.1  jmcneill 
    225  1.1  jmcneill 	return drm_gem_handle_create(file, &sfb->obj->base, handle);
    226  1.1  jmcneill }
    227  1.1  jmcneill 
    228  1.1  jmcneill static void
    229  1.1  jmcneill rk_drm_fb_destroy(struct drm_framebuffer *fb)
    230  1.1  jmcneill {
    231  1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(fb);
    232  1.1  jmcneill 
    233  1.1  jmcneill 	drm_framebuffer_cleanup(fb);
    234  1.8  riastrad 	drm_gem_object_put_unlocked(&sfb->obj->base);
    235  1.1  jmcneill 	kmem_free(sfb, sizeof(*sfb));
    236  1.1  jmcneill }
    237  1.1  jmcneill 
    238  1.1  jmcneill static const struct drm_framebuffer_funcs rk_drm_framebuffer_funcs = {
    239  1.1  jmcneill 	.create_handle = rk_drm_fb_create_handle,
    240  1.1  jmcneill 	.destroy = rk_drm_fb_destroy,
    241  1.1  jmcneill };
    242  1.1  jmcneill 
    243  1.1  jmcneill static struct drm_framebuffer *
    244  1.1  jmcneill rk_drm_fb_create(struct drm_device *ddev, struct drm_file *file,
    245  1.8  riastrad     const struct drm_mode_fb_cmd2 *cmd)
    246  1.1  jmcneill {
    247  1.1  jmcneill 	struct rk_drm_framebuffer *fb;
    248  1.1  jmcneill 	struct drm_gem_object *gem_obj;
    249  1.1  jmcneill 	int error;
    250  1.1  jmcneill 
    251  1.1  jmcneill 	if (cmd->flags)
    252  1.1  jmcneill 		return NULL;
    253  1.1  jmcneill 
    254  1.8  riastrad 	gem_obj = drm_gem_object_lookup(file, cmd->handles[0]);
    255  1.1  jmcneill 	if (gem_obj == NULL)
    256  1.1  jmcneill 		return NULL;
    257  1.1  jmcneill 
    258  1.1  jmcneill 	fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
    259  1.1  jmcneill 	fb->obj = to_drm_gem_cma_obj(gem_obj);
    260  1.1  jmcneill 	fb->base.pitches[0] = cmd->pitches[0];
    261  1.1  jmcneill 	fb->base.pitches[1] = cmd->pitches[1];
    262  1.1  jmcneill 	fb->base.pitches[2] = cmd->pitches[2];
    263  1.1  jmcneill 	fb->base.offsets[0] = cmd->offsets[0];
    264  1.1  jmcneill 	fb->base.offsets[1] = cmd->offsets[2];
    265  1.1  jmcneill 	fb->base.offsets[2] = cmd->offsets[1];
    266  1.1  jmcneill 	fb->base.width = cmd->width;
    267  1.1  jmcneill 	fb->base.height = cmd->height;
    268  1.8  riastrad 	fb->base.format = drm_format_info(cmd->pixel_format);
    269  1.1  jmcneill 
    270  1.1  jmcneill 	error = drm_framebuffer_init(ddev, &fb->base, &rk_drm_framebuffer_funcs);
    271  1.1  jmcneill 	if (error != 0)
    272  1.1  jmcneill 		goto dealloc;
    273  1.1  jmcneill 
    274  1.1  jmcneill 	return &fb->base;
    275  1.1  jmcneill 
    276  1.1  jmcneill dealloc:
    277  1.1  jmcneill 	drm_framebuffer_cleanup(&fb->base);
    278  1.1  jmcneill 	kmem_free(fb, sizeof(*fb));
    279  1.8  riastrad 	drm_gem_object_put_unlocked(gem_obj);
    280  1.1  jmcneill 
    281  1.1  jmcneill 	return NULL;
    282  1.1  jmcneill }
    283  1.1  jmcneill 
    284  1.1  jmcneill static struct drm_mode_config_funcs rk_drm_mode_config_funcs = {
    285  1.1  jmcneill 	.fb_create = rk_drm_fb_create,
    286  1.1  jmcneill };
    287  1.1  jmcneill 
    288  1.1  jmcneill static int
    289  1.1  jmcneill rk_drm_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
    290  1.1  jmcneill {
    291  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(helper->dev);
    292  1.1  jmcneill 	struct drm_device *ddev = helper->dev;
    293  1.1  jmcneill 	struct rk_drm_framebuffer *sfb = to_rk_drm_framebuffer(helper->fb);
    294  1.1  jmcneill 	struct drm_framebuffer *fb = helper->fb;
    295  1.1  jmcneill 	struct rk_drmfb_attach_args sfa;
    296  1.1  jmcneill 	size_t cma_size;
    297  1.1  jmcneill 	int error;
    298  1.1  jmcneill 
    299  1.1  jmcneill 	const u_int width = sizes->surface_width;
    300  1.1  jmcneill 	const u_int height = sizes->surface_height;
    301  1.1  jmcneill 	const u_int pitch = width * (32 / 8);
    302  1.1  jmcneill 
    303  1.1  jmcneill 	const size_t size = roundup(height * pitch, PAGE_SIZE);
    304  1.1  jmcneill 
    305  1.1  jmcneill 	/* Reserve enough memory for the FB console plus a 4K plane, rounded to 1MB */
    306  1.1  jmcneill 	cma_size = size;
    307  1.1  jmcneill 	cma_size += (RK_DRM_MAX_WIDTH * RK_DRM_MAX_HEIGHT * 4);
    308  1.1  jmcneill 	cma_size = roundup(cma_size, 1024 * 1024);
    309  1.1  jmcneill 	sc->sc_ddev->cma_pool = rk_drm_alloc_cma_pool(sc->sc_ddev, cma_size);
    310  1.1  jmcneill 	if (sc->sc_ddev->cma_pool != NULL)
    311  1.1  jmcneill 		aprint_normal_dev(sc->sc_dev, "reserved %u MB DRAM for CMA\n",
    312  1.1  jmcneill 		    (u_int)(cma_size / (1024 * 1024)));
    313  1.1  jmcneill 
    314  1.1  jmcneill 	sfb->obj = drm_gem_cma_create(ddev, size);
    315  1.1  jmcneill 	if (sfb->obj == NULL) {
    316  1.1  jmcneill 		DRM_ERROR("failed to allocate memory for framebuffer\n");
    317  1.1  jmcneill 		return -ENOMEM;
    318  1.1  jmcneill 	}
    319  1.1  jmcneill 
    320  1.1  jmcneill 	fb->pitches[0] = pitch;
    321  1.1  jmcneill 	fb->offsets[0] = 0;
    322  1.1  jmcneill 	fb->width = width;
    323  1.1  jmcneill 	fb->height = height;
    324  1.6       mrg #ifdef __ARM_BIG_ENDIAN
    325  1.8  riastrad 	fb->format = drm_format_info(DRM_FORMAT_BGRX8888);
    326  1.6       mrg #else
    327  1.8  riastrad 	fb->format = drm_format_info(DRM_FORMAT_XRGB8888);
    328  1.6       mrg #endif
    329  1.1  jmcneill 
    330  1.1  jmcneill 	error = drm_framebuffer_init(ddev, fb, &rk_drm_framebuffer_funcs);
    331  1.1  jmcneill 	if (error != 0) {
    332  1.1  jmcneill 		DRM_ERROR("failed to initialize framebuffer\n");
    333  1.1  jmcneill 		return error;
    334  1.1  jmcneill 	}
    335  1.1  jmcneill 
    336  1.1  jmcneill 	memset(&sfa, 0, sizeof(sfa));
    337  1.1  jmcneill 	sfa.sfa_drm_dev = ddev;
    338  1.1  jmcneill 	sfa.sfa_fb_helper = helper;
    339  1.1  jmcneill 	sfa.sfa_fb_sizes = *sizes;
    340  1.1  jmcneill 	sfa.sfa_fb_bst = sc->sc_bst;
    341  1.1  jmcneill 	sfa.sfa_fb_dmat = sc->sc_dmat;
    342  1.1  jmcneill 	sfa.sfa_fb_linebytes = helper->fb->pitches[0];
    343  1.1  jmcneill 
    344  1.5   thorpej 	helper->fbdev = config_found(ddev->dev, &sfa, NULL,
    345  1.7   thorpej 	    CFARGS(.iattr = "rkfbbus"));
    346  1.1  jmcneill 	if (helper->fbdev == NULL) {
    347  1.1  jmcneill 		DRM_ERROR("unable to attach framebuffer\n");
    348  1.1  jmcneill 		return -ENXIO;
    349  1.1  jmcneill 	}
    350  1.1  jmcneill 
    351  1.1  jmcneill 	return 0;
    352  1.1  jmcneill }
    353  1.1  jmcneill 
    354  1.1  jmcneill static struct drm_fb_helper_funcs rk_drm_fb_helper_funcs = {
    355  1.1  jmcneill 	.fb_probe = rk_drm_fb_probe,
    356  1.1  jmcneill };
    357  1.1  jmcneill 
    358  1.1  jmcneill static int
    359  1.1  jmcneill rk_drm_load(struct drm_device *ddev, unsigned long flags)
    360  1.1  jmcneill {
    361  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    362  1.1  jmcneill 	struct rk_drm_ports *sport;
    363  1.1  jmcneill 	struct rk_drm_fbdev *fbdev;
    364  1.1  jmcneill 	struct fdt_endpoint *ep;
    365  1.1  jmcneill 	const u_int *data;
    366  1.1  jmcneill 	int datalen, error, num_crtc, ep_index;
    367  1.1  jmcneill 
    368  1.1  jmcneill 	drm_mode_config_init(ddev);
    369  1.1  jmcneill 	ddev->mode_config.min_width = 0;
    370  1.1  jmcneill 	ddev->mode_config.min_height = 0;
    371  1.1  jmcneill 	ddev->mode_config.max_width = RK_DRM_MAX_WIDTH;
    372  1.1  jmcneill 	ddev->mode_config.max_height = RK_DRM_MAX_HEIGHT;
    373  1.1  jmcneill 	ddev->mode_config.funcs = &rk_drm_mode_config_funcs;
    374  1.1  jmcneill 
    375  1.1  jmcneill 	num_crtc = 0;
    376  1.1  jmcneill 	data = fdtbus_get_prop(sc->sc_phandle, "ports", &datalen);
    377  1.1  jmcneill 	while (datalen >= 4) {
    378  1.1  jmcneill 		const int crtc_phandle = fdtbus_get_phandle_from_native(be32dec(data));
    379  1.1  jmcneill 
    380  1.1  jmcneill 		TAILQ_FOREACH(sport, &rk_drm_ports, entries)
    381  1.1  jmcneill 			if (sport->phandle == crtc_phandle && sport->ddev == NULL) {
    382  1.1  jmcneill 				sport->ddev = ddev;
    383  1.1  jmcneill 				for (ep_index = 0; (ep = fdt_endpoint_get_from_index(sport->port, 0, ep_index)) != NULL; ep_index++) {
    384  1.1  jmcneill 					error = fdt_endpoint_activate_direct(ep, true);
    385  1.1  jmcneill 					if (error != 0)
    386  1.1  jmcneill 						aprint_debug_dev(sc->sc_dev,
    387  1.1  jmcneill 						    "failed to activate endpoint %d: %d\n",
    388  1.1  jmcneill 						    ep_index, error);
    389  1.1  jmcneill 				}
    390  1.1  jmcneill 				num_crtc++;
    391  1.1  jmcneill 			}
    392  1.1  jmcneill 
    393  1.1  jmcneill 		datalen -= 4;
    394  1.1  jmcneill 		data++;
    395  1.1  jmcneill 	}
    396  1.1  jmcneill 
    397  1.1  jmcneill 	if (num_crtc == 0) {
    398  1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "no display interface ports configured\n");
    399  1.3       mrg 		error = ENXIO;
    400  1.3       mrg 		goto drmerr;
    401  1.1  jmcneill 	}
    402  1.1  jmcneill 
    403  1.1  jmcneill 	fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
    404  1.1  jmcneill 
    405  1.1  jmcneill 	drm_fb_helper_prepare(ddev, &fbdev->helper, &rk_drm_fb_helper_funcs);
    406  1.1  jmcneill 
    407  1.8  riastrad 	error = drm_fb_helper_init(ddev, &fbdev->helper, num_crtc);
    408  1.1  jmcneill 	if (error)
    409  1.3       mrg 		goto allocerr;
    410  1.1  jmcneill 
    411  1.1  jmcneill 	fbdev->helper.fb = kmem_zalloc(sizeof(struct rk_drm_framebuffer), KM_SLEEP);
    412  1.1  jmcneill 
    413  1.1  jmcneill 	drm_fb_helper_single_add_all_connectors(&fbdev->helper);
    414  1.1  jmcneill 
    415  1.1  jmcneill 	drm_helper_disable_unused_functions(ddev);
    416  1.1  jmcneill 
    417  1.1  jmcneill 	drm_fb_helper_initial_config(&fbdev->helper, 32);
    418  1.1  jmcneill 
    419  1.1  jmcneill 	/* XXX */
    420  1.1  jmcneill 	ddev->irq_enabled = true;
    421  1.1  jmcneill 	drm_vblank_init(ddev, num_crtc);
    422  1.1  jmcneill 
    423  1.1  jmcneill 	return 0;
    424  1.1  jmcneill 
    425  1.3       mrg allocerr:
    426  1.3       mrg 	kmem_free(fbdev, sizeof(*fbdev));
    427  1.1  jmcneill drmerr:
    428  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    429  1.1  jmcneill 
    430  1.1  jmcneill 	return error;
    431  1.1  jmcneill }
    432  1.1  jmcneill 
    433  1.1  jmcneill static uint32_t
    434  1.1  jmcneill rk_drm_get_vblank_counter(struct drm_device *ddev, unsigned int crtc)
    435  1.1  jmcneill {
    436  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    437  1.1  jmcneill 
    438  1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    439  1.1  jmcneill 		return 0;
    440  1.1  jmcneill 
    441  1.1  jmcneill 	if (sc->sc_vbl[crtc].get_vblank_counter == NULL)
    442  1.1  jmcneill 		return 0;
    443  1.1  jmcneill 
    444  1.1  jmcneill 	return sc->sc_vbl[crtc].get_vblank_counter(sc->sc_vbl[crtc].priv);
    445  1.1  jmcneill }
    446  1.1  jmcneill 
    447  1.1  jmcneill static int
    448  1.1  jmcneill rk_drm_enable_vblank(struct drm_device *ddev, unsigned int crtc)
    449  1.1  jmcneill {
    450  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    451  1.1  jmcneill 
    452  1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    453  1.1  jmcneill 		return 0;
    454  1.1  jmcneill 
    455  1.1  jmcneill 	if (sc->sc_vbl[crtc].enable_vblank == NULL)
    456  1.1  jmcneill 		return 0;
    457  1.1  jmcneill 
    458  1.1  jmcneill 	sc->sc_vbl[crtc].enable_vblank(sc->sc_vbl[crtc].priv);
    459  1.1  jmcneill 
    460  1.1  jmcneill 	return 0;
    461  1.1  jmcneill }
    462  1.1  jmcneill 
    463  1.1  jmcneill static void
    464  1.1  jmcneill rk_drm_disable_vblank(struct drm_device *ddev, unsigned int crtc)
    465  1.1  jmcneill {
    466  1.1  jmcneill 	struct rk_drm_softc * const sc = rk_drm_private(ddev);
    467  1.1  jmcneill 
    468  1.1  jmcneill 	if (crtc >= __arraycount(sc->sc_vbl))
    469  1.1  jmcneill 		return;
    470  1.1  jmcneill 
    471  1.1  jmcneill 	if (sc->sc_vbl[crtc].disable_vblank == NULL)
    472  1.1  jmcneill 		return;
    473  1.1  jmcneill 
    474  1.1  jmcneill 	sc->sc_vbl[crtc].disable_vblank(sc->sc_vbl[crtc].priv);
    475  1.1  jmcneill }
    476  1.1  jmcneill 
    477  1.8  riastrad static void
    478  1.1  jmcneill rk_drm_unload(struct drm_device *ddev)
    479  1.1  jmcneill {
    480  1.1  jmcneill 	drm_mode_config_cleanup(ddev);
    481  1.1  jmcneill }
    482  1.1  jmcneill 
    483  1.1  jmcneill int
    484  1.1  jmcneill rk_drm_register_port(int phandle, struct fdt_device_ports *port)
    485  1.1  jmcneill {
    486  1.1  jmcneill 	struct rk_drm_ports *sport;
    487  1.1  jmcneill 
    488  1.1  jmcneill 	sport = kmem_zalloc(sizeof(*sport), KM_SLEEP);
    489  1.1  jmcneill 	sport->phandle = phandle;
    490  1.1  jmcneill 	sport->port = port;
    491  1.1  jmcneill 	sport->ddev = NULL;
    492  1.1  jmcneill 	TAILQ_INSERT_TAIL(&rk_drm_ports, sport, entries);
    493  1.1  jmcneill 
    494  1.1  jmcneill 	return 0;
    495  1.1  jmcneill }
    496  1.1  jmcneill 
    497  1.1  jmcneill struct drm_device *
    498  1.1  jmcneill rk_drm_port_device(struct fdt_device_ports *port)
    499  1.1  jmcneill {
    500  1.1  jmcneill 	struct rk_drm_ports *sport;
    501  1.1  jmcneill 
    502  1.1  jmcneill 	TAILQ_FOREACH(sport, &rk_drm_ports, entries)
    503  1.1  jmcneill 		if (sport->port == port)
    504  1.1  jmcneill 			return sport->ddev;
    505  1.1  jmcneill 
    506  1.1  jmcneill 	return NULL;
    507  1.1  jmcneill }
    508