1*6f907a9bSriastradh /* $NetBSD: rk_fb.c,v 1.7 2022/09/25 07:50:15 riastradh Exp $ */
249517fcaSjmcneill
349517fcaSjmcneill /*-
449517fcaSjmcneill * Copyright (c) 2015-2019 Jared McNeill <jmcneill@invisible.ca>
549517fcaSjmcneill * All rights reserved.
649517fcaSjmcneill *
749517fcaSjmcneill * Redistribution and use in source and binary forms, with or without
849517fcaSjmcneill * modification, are permitted provided that the following conditions
949517fcaSjmcneill * are met:
1049517fcaSjmcneill * 1. Redistributions of source code must retain the above copyright
1149517fcaSjmcneill * notice, this list of conditions and the following disclaimer.
1249517fcaSjmcneill * 2. Redistributions in binary form must reproduce the above copyright
1349517fcaSjmcneill * notice, this list of conditions and the following disclaimer in the
1449517fcaSjmcneill * documentation and/or other materials provided with the distribution.
1549517fcaSjmcneill *
1649517fcaSjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1749517fcaSjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1849517fcaSjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1949517fcaSjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2049517fcaSjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2149517fcaSjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2249517fcaSjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2349517fcaSjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2449517fcaSjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2549517fcaSjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2649517fcaSjmcneill * SUCH DAMAGE.
2749517fcaSjmcneill */
2849517fcaSjmcneill
2949517fcaSjmcneill #include "opt_wsdisplay_compat.h"
3049517fcaSjmcneill
3149517fcaSjmcneill #include <sys/cdefs.h>
32*6f907a9bSriastradh __KERNEL_RCSID(0, "$NetBSD: rk_fb.c,v 1.7 2022/09/25 07:50:15 riastradh Exp $");
3349517fcaSjmcneill
3449517fcaSjmcneill #include <sys/param.h>
3549517fcaSjmcneill #include <sys/bus.h>
3649517fcaSjmcneill #include <sys/device.h>
3749517fcaSjmcneill
3849517fcaSjmcneill #include <dev/fdt/fdtvar.h>
3949517fcaSjmcneill
40dd47db3eSriastradh #include <arm/rockchip/rk_drm.h>
41dd47db3eSriastradh
423973e774Sriastradh #include <drm/drm_drv.h>
4349517fcaSjmcneill #include <drm/drmfb.h>
4449517fcaSjmcneill
4549517fcaSjmcneill static int rk_fb_match(device_t, cfdata_t, void *);
4649517fcaSjmcneill static void rk_fb_attach(device_t, device_t, void *);
4749517fcaSjmcneill
485549b16bSriastradh static void rk_fb_init(struct rk_drm_task *);
495549b16bSriastradh
5049517fcaSjmcneill static bool rk_fb_shutdown(device_t, int);
5149517fcaSjmcneill
5249517fcaSjmcneill struct rk_fb_softc {
5349517fcaSjmcneill struct drmfb_softc sc_drmfb;
5449517fcaSjmcneill device_t sc_dev;
5549517fcaSjmcneill struct rk_drm_framebuffer *sc_fb;
5649517fcaSjmcneill struct rk_drmfb_attach_args sc_sfa;
575549b16bSriastradh struct rk_drm_task sc_attach_task;
5849517fcaSjmcneill };
5949517fcaSjmcneill
6049517fcaSjmcneill static paddr_t rk_fb_mmapfb(struct drmfb_softc *, off_t, int);
6149517fcaSjmcneill static int rk_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
6249517fcaSjmcneill lwp_t *);
6349517fcaSjmcneill
6449517fcaSjmcneill static const struct drmfb_params rkfb_drmfb_params = {
6549517fcaSjmcneill .dp_mmapfb = rk_fb_mmapfb,
6649517fcaSjmcneill .dp_ioctl = rk_fb_ioctl,
6749517fcaSjmcneill };
6849517fcaSjmcneill
6949517fcaSjmcneill CFATTACH_DECL_NEW(rk_fb, sizeof(struct rk_fb_softc),
7049517fcaSjmcneill rk_fb_match, rk_fb_attach, NULL, NULL);
7149517fcaSjmcneill
7249517fcaSjmcneill static int
rk_fb_match(device_t parent,cfdata_t cf,void * aux)7349517fcaSjmcneill rk_fb_match(device_t parent, cfdata_t cf, void *aux)
7449517fcaSjmcneill {
7549517fcaSjmcneill return 1;
7649517fcaSjmcneill }
7749517fcaSjmcneill
7849517fcaSjmcneill static void
rk_fb_attach(device_t parent,device_t self,void * aux)7949517fcaSjmcneill rk_fb_attach(device_t parent, device_t self, void *aux)
8049517fcaSjmcneill {
8149517fcaSjmcneill struct rk_fb_softc * const sc = device_private(self);
8249517fcaSjmcneill struct rk_drmfb_attach_args * const sfa = aux;
8349517fcaSjmcneill
8449517fcaSjmcneill sc->sc_dev = self;
8549517fcaSjmcneill sc->sc_sfa = *sfa;
8649517fcaSjmcneill sc->sc_fb = to_rk_drm_framebuffer(sfa->sfa_fb_helper->fb);
8749517fcaSjmcneill
8849517fcaSjmcneill aprint_naive("\n");
8949517fcaSjmcneill aprint_normal("\n");
9049517fcaSjmcneill
915549b16bSriastradh rk_task_init(&sc->sc_attach_task, &rk_fb_init);
925549b16bSriastradh rk_task_schedule(parent, &sc->sc_attach_task);
935549b16bSriastradh }
945549b16bSriastradh
955549b16bSriastradh static void
rk_fb_turnoffandbackonagain(device_t self)960748e604Sriastradh rk_fb_turnoffandbackonagain(device_t self)
970748e604Sriastradh {
980748e604Sriastradh struct rk_fb_softc *sc = device_private(self);
990748e604Sriastradh struct rk_drmfb_attach_args * const sfa = &sc->sc_sfa;
1000748e604Sriastradh
1010748e604Sriastradh /*
1020748e604Sriastradh * This is a grody kludge to turn the display off and back on
1030748e604Sriastradh * again at boot; otherwise the initial modeset doesn't take.
1040748e604Sriastradh * This is surely a bug somewhere in rk_vop.c or nearby, but I
1050748e604Sriastradh * haven't been able to find it, and this gives us almost the
1060748e604Sriastradh * same effect.
1070748e604Sriastradh */
1080748e604Sriastradh mutex_lock(&sfa->sfa_fb_helper->lock);
1090748e604Sriastradh drm_client_modeset_dpms(&sfa->sfa_fb_helper->client, DRM_MODE_DPMS_OFF);
1100748e604Sriastradh drm_client_modeset_dpms(&sfa->sfa_fb_helper->client, DRM_MODE_DPMS_ON);
1110748e604Sriastradh mutex_unlock(&sfa->sfa_fb_helper->lock);
1120748e604Sriastradh }
1130748e604Sriastradh
1140748e604Sriastradh static void
rk_fb_init(struct rk_drm_task * task)1155549b16bSriastradh rk_fb_init(struct rk_drm_task *task)
1165549b16bSriastradh {
1175549b16bSriastradh struct rk_fb_softc * const sc =
1185549b16bSriastradh container_of(task, struct rk_fb_softc, sc_attach_task);
1195549b16bSriastradh device_t self = sc->sc_dev;
1205549b16bSriastradh struct rk_drmfb_attach_args * const sfa = &sc->sc_sfa;
1215549b16bSriastradh int error;
1225549b16bSriastradh
12349517fcaSjmcneill const struct drmfb_attach_args da = {
12449517fcaSjmcneill .da_dev = self,
12549517fcaSjmcneill .da_fb_helper = sfa->sfa_fb_helper,
12649517fcaSjmcneill .da_fb_sizes = &sfa->sfa_fb_sizes,
12749517fcaSjmcneill .da_fb_vaddr = sc->sc_fb->obj->vaddr,
12849517fcaSjmcneill .da_fb_linebytes = sfa->sfa_fb_linebytes,
12949517fcaSjmcneill .da_params = &rkfb_drmfb_params,
13049517fcaSjmcneill };
13149517fcaSjmcneill
13249517fcaSjmcneill error = drmfb_attach(&sc->sc_drmfb, &da);
13349517fcaSjmcneill if (error) {
13449517fcaSjmcneill aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
13549517fcaSjmcneill return;
13649517fcaSjmcneill }
13749517fcaSjmcneill
13849517fcaSjmcneill pmf_device_register1(self, NULL, NULL, rk_fb_shutdown);
1390748e604Sriastradh
1400748e604Sriastradh config_interrupts(self, rk_fb_turnoffandbackonagain);
14149517fcaSjmcneill }
14249517fcaSjmcneill
14349517fcaSjmcneill static bool
rk_fb_shutdown(device_t self,int flags)14449517fcaSjmcneill rk_fb_shutdown(device_t self, int flags)
14549517fcaSjmcneill {
14649517fcaSjmcneill struct rk_fb_softc * const sc = device_private(self);
14749517fcaSjmcneill
14849517fcaSjmcneill return drmfb_shutdown(&sc->sc_drmfb, flags);
14949517fcaSjmcneill }
15049517fcaSjmcneill
15149517fcaSjmcneill static paddr_t
rk_fb_mmapfb(struct drmfb_softc * sc,off_t off,int prot)15249517fcaSjmcneill rk_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
15349517fcaSjmcneill {
15449517fcaSjmcneill struct rk_fb_softc * const tfb_sc = (struct rk_fb_softc *)sc;
15549517fcaSjmcneill struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
15649517fcaSjmcneill
15749517fcaSjmcneill KASSERT(off >= 0);
15849517fcaSjmcneill KASSERT(off < obj->dmasize);
15949517fcaSjmcneill
16049517fcaSjmcneill return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
16149517fcaSjmcneill BUS_DMA_PREFETCHABLE);
16249517fcaSjmcneill }
16349517fcaSjmcneill
16449517fcaSjmcneill static int
rk_fb_ioctl(struct drmfb_softc * sc,u_long cmd,void * data,int flag,lwp_t * l)16549517fcaSjmcneill rk_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
16649517fcaSjmcneill lwp_t *l)
16749517fcaSjmcneill {
16849517fcaSjmcneill struct wsdisplayio_bus_id *busid;
16949517fcaSjmcneill struct wsdisplayio_fbinfo *fbi;
17049517fcaSjmcneill struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
17149517fcaSjmcneill int error;
17249517fcaSjmcneill
17349517fcaSjmcneill switch (cmd) {
17449517fcaSjmcneill case WSDISPLAYIO_GET_BUSID:
17549517fcaSjmcneill busid = data;
17649517fcaSjmcneill busid->bus_type = WSDISPLAYIO_BUS_SOC;
17749517fcaSjmcneill return 0;
17849517fcaSjmcneill case WSDISPLAYIO_GTYPE:
17949517fcaSjmcneill *(u_int *)data = WSDISPLAY_TYPE_GENFB;
18049517fcaSjmcneill return 0;
18149517fcaSjmcneill case WSDISPLAYIO_GET_FBINFO:
18249517fcaSjmcneill fbi = data;
18349517fcaSjmcneill error = wsdisplayio_get_fbinfo(ri, fbi);
18449517fcaSjmcneill fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
18549517fcaSjmcneill return error;
18649517fcaSjmcneill default:
18749517fcaSjmcneill return EPASSTHROUGH;
18849517fcaSjmcneill }
18949517fcaSjmcneill }
190