xref: /netbsd-src/sys/arch/arm/rockchip/rk_fb.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* $NetBSD: rk_fb.c,v 1.2 2021/05/21 09:33:27 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2015-2019 Jared McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include "opt_wsdisplay_compat.h"
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: rk_fb.c,v 1.2 2021/05/21 09:33:27 jmcneill Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 
38 #include <dev/fdt/fdtvar.h>
39 
40 #include <drm/drmP.h>
41 #include <drm/drmfb.h>
42 
43 #include <arm/rockchip/rk_drm.h>
44 
45 static int	rk_fb_match(device_t, cfdata_t, void *);
46 static void	rk_fb_attach(device_t, device_t, void *);
47 
48 static bool	rk_fb_shutdown(device_t, int);
49 
50 struct rk_fb_softc {
51 	struct drmfb_softc	sc_drmfb;
52 	device_t		sc_dev;
53 	struct rk_drm_framebuffer *sc_fb;
54 	struct rk_drmfb_attach_args sc_sfa;
55 };
56 
57 static paddr_t	rk_fb_mmapfb(struct drmfb_softc *, off_t, int);
58 static int	rk_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
59 			       lwp_t *);
60 
61 static const struct drmfb_params rkfb_drmfb_params = {
62 	.dp_mmapfb = rk_fb_mmapfb,
63 	.dp_ioctl = rk_fb_ioctl,
64 };
65 
66 CFATTACH_DECL_NEW(rk_fb, sizeof(struct rk_fb_softc),
67 	rk_fb_match, rk_fb_attach, NULL, NULL);
68 
69 static int
70 rk_fb_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 	return 1;
73 }
74 
75 static void
76 rk_fb_attach(device_t parent, device_t self, void *aux)
77 {
78 	struct rk_fb_softc * const sc = device_private(self);
79 	struct rk_drmfb_attach_args * const sfa = aux;
80 	int error;
81 
82 	sc->sc_dev = self;
83 	sc->sc_sfa = *sfa;
84 	sc->sc_fb = to_rk_drm_framebuffer(sfa->sfa_fb_helper->fb);
85 
86 	aprint_naive("\n");
87 	aprint_normal("\n");
88 
89 #ifdef WSDISPLAY_MULTICONS
90 	prop_dictionary_t dict = device_properties(self);
91 	const bool is_console = true;
92 	prop_dictionary_set_bool(dict, "is_console", is_console);
93 #endif
94 
95 	const struct drmfb_attach_args da = {
96 		.da_dev = self,
97 		.da_fb_helper = sfa->sfa_fb_helper,
98 		.da_fb_sizes = &sfa->sfa_fb_sizes,
99 		.da_fb_vaddr = sc->sc_fb->obj->vaddr,
100 		.da_fb_linebytes = sfa->sfa_fb_linebytes,
101 		.da_params = &rkfb_drmfb_params,
102 	};
103 
104 	error = drmfb_attach(&sc->sc_drmfb, &da);
105 	if (error) {
106 		aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
107 		return;
108 	}
109 
110 	pmf_device_register1(self, NULL, NULL, rk_fb_shutdown);
111 }
112 
113 static bool
114 rk_fb_shutdown(device_t self, int flags)
115 {
116 	struct rk_fb_softc * const sc = device_private(self);
117 
118 	return drmfb_shutdown(&sc->sc_drmfb, flags);
119 }
120 
121 static paddr_t
122 rk_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
123 {
124 	struct rk_fb_softc * const tfb_sc = (struct rk_fb_softc *)sc;
125 	struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
126 
127 	KASSERT(off >= 0);
128 	KASSERT(off < obj->dmasize);
129 
130 	return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
131 	    BUS_DMA_PREFETCHABLE);
132 }
133 
134 static int
135 rk_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
136     lwp_t *l)
137 {
138 	struct wsdisplayio_bus_id *busid;
139 	struct wsdisplayio_fbinfo *fbi;
140 	struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
141 	int error;
142 
143 	switch (cmd) {
144 	case WSDISPLAYIO_GET_BUSID:
145 		busid = data;
146 		busid->bus_type = WSDISPLAYIO_BUS_SOC;
147 		return 0;
148 	case WSDISPLAYIO_GTYPE:
149 		*(u_int *)data = WSDISPLAY_TYPE_GENFB;
150 		return 0;
151 	case WSDISPLAYIO_GET_FBINFO:
152 		fbi = data;
153 		error = wsdisplayio_get_fbinfo(ri, fbi);
154 		fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
155 		return error;
156 	default:
157 		return EPASSTHROUGH;
158 	}
159 }
160