1 /* $NetBSD: rk_fb.c,v 1.7 2022/09/25 07:50:15 riastradh 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.7 2022/09/25 07:50:15 riastradh 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 <arm/rockchip/rk_drm.h>
41
42 #include <drm/drm_drv.h>
43 #include <drm/drmfb.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 void rk_fb_init(struct rk_drm_task *);
49
50 static bool rk_fb_shutdown(device_t, int);
51
52 struct rk_fb_softc {
53 struct drmfb_softc sc_drmfb;
54 device_t sc_dev;
55 struct rk_drm_framebuffer *sc_fb;
56 struct rk_drmfb_attach_args sc_sfa;
57 struct rk_drm_task sc_attach_task;
58 };
59
60 static paddr_t rk_fb_mmapfb(struct drmfb_softc *, off_t, int);
61 static int rk_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
62 lwp_t *);
63
64 static const struct drmfb_params rkfb_drmfb_params = {
65 .dp_mmapfb = rk_fb_mmapfb,
66 .dp_ioctl = rk_fb_ioctl,
67 };
68
69 CFATTACH_DECL_NEW(rk_fb, sizeof(struct rk_fb_softc),
70 rk_fb_match, rk_fb_attach, NULL, NULL);
71
72 static int
rk_fb_match(device_t parent,cfdata_t cf,void * aux)73 rk_fb_match(device_t parent, cfdata_t cf, void *aux)
74 {
75 return 1;
76 }
77
78 static void
rk_fb_attach(device_t parent,device_t self,void * aux)79 rk_fb_attach(device_t parent, device_t self, void *aux)
80 {
81 struct rk_fb_softc * const sc = device_private(self);
82 struct rk_drmfb_attach_args * const sfa = aux;
83
84 sc->sc_dev = self;
85 sc->sc_sfa = *sfa;
86 sc->sc_fb = to_rk_drm_framebuffer(sfa->sfa_fb_helper->fb);
87
88 aprint_naive("\n");
89 aprint_normal("\n");
90
91 rk_task_init(&sc->sc_attach_task, &rk_fb_init);
92 rk_task_schedule(parent, &sc->sc_attach_task);
93 }
94
95 static void
rk_fb_turnoffandbackonagain(device_t self)96 rk_fb_turnoffandbackonagain(device_t self)
97 {
98 struct rk_fb_softc *sc = device_private(self);
99 struct rk_drmfb_attach_args * const sfa = &sc->sc_sfa;
100
101 /*
102 * This is a grody kludge to turn the display off and back on
103 * again at boot; otherwise the initial modeset doesn't take.
104 * This is surely a bug somewhere in rk_vop.c or nearby, but I
105 * haven't been able to find it, and this gives us almost the
106 * same effect.
107 */
108 mutex_lock(&sfa->sfa_fb_helper->lock);
109 drm_client_modeset_dpms(&sfa->sfa_fb_helper->client, DRM_MODE_DPMS_OFF);
110 drm_client_modeset_dpms(&sfa->sfa_fb_helper->client, DRM_MODE_DPMS_ON);
111 mutex_unlock(&sfa->sfa_fb_helper->lock);
112 }
113
114 static void
rk_fb_init(struct rk_drm_task * task)115 rk_fb_init(struct rk_drm_task *task)
116 {
117 struct rk_fb_softc * const sc =
118 container_of(task, struct rk_fb_softc, sc_attach_task);
119 device_t self = sc->sc_dev;
120 struct rk_drmfb_attach_args * const sfa = &sc->sc_sfa;
121 int error;
122
123 const struct drmfb_attach_args da = {
124 .da_dev = self,
125 .da_fb_helper = sfa->sfa_fb_helper,
126 .da_fb_sizes = &sfa->sfa_fb_sizes,
127 .da_fb_vaddr = sc->sc_fb->obj->vaddr,
128 .da_fb_linebytes = sfa->sfa_fb_linebytes,
129 .da_params = &rkfb_drmfb_params,
130 };
131
132 error = drmfb_attach(&sc->sc_drmfb, &da);
133 if (error) {
134 aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
135 return;
136 }
137
138 pmf_device_register1(self, NULL, NULL, rk_fb_shutdown);
139
140 config_interrupts(self, rk_fb_turnoffandbackonagain);
141 }
142
143 static bool
rk_fb_shutdown(device_t self,int flags)144 rk_fb_shutdown(device_t self, int flags)
145 {
146 struct rk_fb_softc * const sc = device_private(self);
147
148 return drmfb_shutdown(&sc->sc_drmfb, flags);
149 }
150
151 static paddr_t
rk_fb_mmapfb(struct drmfb_softc * sc,off_t off,int prot)152 rk_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
153 {
154 struct rk_fb_softc * const tfb_sc = (struct rk_fb_softc *)sc;
155 struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
156
157 KASSERT(off >= 0);
158 KASSERT(off < obj->dmasize);
159
160 return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
161 BUS_DMA_PREFETCHABLE);
162 }
163
164 static int
rk_fb_ioctl(struct drmfb_softc * sc,u_long cmd,void * data,int flag,lwp_t * l)165 rk_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
166 lwp_t *l)
167 {
168 struct wsdisplayio_bus_id *busid;
169 struct wsdisplayio_fbinfo *fbi;
170 struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
171 int error;
172
173 switch (cmd) {
174 case WSDISPLAYIO_GET_BUSID:
175 busid = data;
176 busid->bus_type = WSDISPLAYIO_BUS_SOC;
177 return 0;
178 case WSDISPLAYIO_GTYPE:
179 *(u_int *)data = WSDISPLAY_TYPE_GENFB;
180 return 0;
181 case WSDISPLAYIO_GET_FBINFO:
182 fbi = data;
183 error = wsdisplayio_get_fbinfo(ri, fbi);
184 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
185 return error;
186 default:
187 return EPASSTHROUGH;
188 }
189 }
190