1 /* $NetBSD: ti_fb.c,v 1.4 2022/09/25 07:50:32 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: ti_fb.c,v 1.4 2022/09/25 07:50:32 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 #include <dev/fdt/fdt_port.h>
40
41 #include <drm/drm_drv.h>
42 #include <drm/drmfb.h>
43
44 #include <arm/ti/ti_lcdc.h>
45
46 static int ti_fb_match(device_t, cfdata_t, void *);
47 static void ti_fb_attach(device_t, device_t, void *);
48
49 static void tilcdc_fb_init(struct tilcdc_drm_task *);
50
51 static bool ti_fb_shutdown(device_t, int);
52
53 struct ti_fb_softc {
54 struct drmfb_softc sc_drmfb;
55 device_t sc_dev;
56 struct tilcdc_framebuffer *sc_fb;
57 struct tilcdcfb_attach_args sc_tfa;
58 struct tilcdc_drm_task sc_attach_task;
59 };
60
61 static paddr_t ti_fb_mmapfb(struct drmfb_softc *, off_t, int);
62 static int ti_fb_ioctl(struct drmfb_softc *, u_long, void *, int,
63 lwp_t *);
64
65 static const struct drmfb_params tifb_drmfb_params = {
66 .dp_mmapfb = ti_fb_mmapfb,
67 .dp_ioctl = ti_fb_ioctl,
68 };
69
70 CFATTACH_DECL_NEW(ti_fb, sizeof(struct ti_fb_softc),
71 ti_fb_match, ti_fb_attach, NULL, NULL);
72
73 static int
ti_fb_match(device_t parent,cfdata_t cf,void * aux)74 ti_fb_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 return 1;
77 }
78
79 static void
ti_fb_attach(device_t parent,device_t self,void * aux)80 ti_fb_attach(device_t parent, device_t self, void *aux)
81 {
82 struct ti_fb_softc * const sc = device_private(self);
83 struct tilcdcfb_attach_args * const tfa = aux;
84
85 sc->sc_dev = self;
86 sc->sc_tfa = *tfa;
87 sc->sc_fb = to_tilcdc_framebuffer(tfa->tfa_fb_helper->fb);
88
89 aprint_naive("\n");
90 aprint_normal("\n");
91
92 tilcdc_task_init(&sc->sc_attach_task, &tilcdc_fb_init);
93 tilcdc_task_schedule(parent, &sc->sc_attach_task);
94 }
95
96 static void
tilcdc_fb_init(struct tilcdc_drm_task * task)97 tilcdc_fb_init(struct tilcdc_drm_task *task)
98 {
99 struct ti_fb_softc *sc = container_of(task, struct ti_fb_softc,
100 sc_attach_task);
101 device_t self = sc->sc_dev;
102 struct tilcdcfb_attach_args * const tfa = &sc->sc_tfa;
103 const struct drmfb_attach_args da = {
104 .da_dev = self,
105 .da_fb_helper = tfa->tfa_fb_helper,
106 .da_fb_sizes = &tfa->tfa_fb_sizes,
107 .da_fb_vaddr = sc->sc_fb->obj->vaddr,
108 .da_fb_linebytes = tfa->tfa_fb_linebytes,
109 .da_params = &tifb_drmfb_params,
110 };
111 int error;
112
113 error = drmfb_attach(&sc->sc_drmfb, &da);
114 if (error) {
115 aprint_error_dev(self, "failed to attach drmfb: %d\n", error);
116 return;
117 }
118
119 pmf_device_register1(self, NULL, NULL, ti_fb_shutdown);
120 }
121
122 static bool
ti_fb_shutdown(device_t self,int flags)123 ti_fb_shutdown(device_t self, int flags)
124 {
125 struct ti_fb_softc * const sc = device_private(self);
126
127 return drmfb_shutdown(&sc->sc_drmfb, flags);
128 }
129
130 static paddr_t
ti_fb_mmapfb(struct drmfb_softc * sc,off_t off,int prot)131 ti_fb_mmapfb(struct drmfb_softc *sc, off_t off, int prot)
132 {
133 struct ti_fb_softc * const tfb_sc = (struct ti_fb_softc *)sc;
134 struct drm_gem_cma_object *obj = tfb_sc->sc_fb->obj;
135
136 KASSERT(off >= 0);
137 KASSERT(off < obj->dmasize);
138
139 return bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1, off, prot,
140 BUS_DMA_PREFETCHABLE);
141 }
142
143 static int
ti_fb_ioctl(struct drmfb_softc * sc,u_long cmd,void * data,int flag,lwp_t * l)144 ti_fb_ioctl(struct drmfb_softc *sc, u_long cmd, void *data, int flag,
145 lwp_t *l)
146 {
147 struct wsdisplayio_bus_id *busid;
148 struct wsdisplayio_fbinfo *fbi;
149 struct rasops_info *ri = &sc->sc_genfb.vd.active->scr_ri;
150 int error;
151
152 switch (cmd) {
153 case WSDISPLAYIO_GET_BUSID:
154 busid = data;
155 busid->bus_type = WSDISPLAYIO_BUS_SOC;
156 return 0;
157 case WSDISPLAYIO_GTYPE:
158 *(u_int *)data = WSDISPLAY_TYPE_GENFB;
159 return 0;
160 case WSDISPLAYIO_GET_FBINFO:
161 fbi = data;
162 error = wsdisplayio_get_fbinfo(ri, fbi);
163 fbi->fbi_flags |= WSFB_VRAM_IS_RAM;
164 return error;
165 default:
166 return EPASSTHROUGH;
167 }
168 }
169