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