ti_lcdc.c revision 1.8 1 1.8 riastrad /* $NetBSD: ti_lcdc.c,v 1.8 2021/12/19 11:01:21 riastradh Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 Jared D. 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 <sys/cdefs.h>
30 1.8 riastrad __KERNEL_RCSID(0, "$NetBSD: ti_lcdc.c,v 1.8 2021/12/19 11:01:21 riastradh Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/bus.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/intr.h>
36 1.1 jmcneill #include <sys/systm.h>
37 1.1 jmcneill #include <sys/kernel.h>
38 1.1 jmcneill #include <sys/conf.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <uvm/uvm_extern.h>
41 1.1 jmcneill #include <uvm/uvm_object.h>
42 1.1 jmcneill #include <uvm/uvm_device.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <drm/drmP.h>
45 1.1 jmcneill #include <drm/drm_crtc.h>
46 1.1 jmcneill #include <drm/drm_crtc_helper.h>
47 1.1 jmcneill #include <drm/drm_plane_helper.h>
48 1.1 jmcneill #include <drm/drm_fb_helper.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <dev/fdt/fdtvar.h>
51 1.1 jmcneill #include <dev/fdt/fdt_port.h>
52 1.1 jmcneill
53 1.1 jmcneill #include <arm/ti/ti_prcm.h>
54 1.1 jmcneill #include <arm/ti/ti_lcdc.h>
55 1.1 jmcneill #include <arm/ti/ti_lcdcreg.h>
56 1.1 jmcneill
57 1.5 thorpej static const struct device_compatible_entry compat_data[] = {
58 1.5 thorpej { .compat = "ti,am33xx-tilcdc" },
59 1.5 thorpej DEVICE_COMPAT_EOL
60 1.1 jmcneill };
61 1.1 jmcneill
62 1.1 jmcneill enum {
63 1.1 jmcneill TILCDC_PORT_OUTPUT = 0,
64 1.1 jmcneill };
65 1.1 jmcneill
66 1.1 jmcneill static int tilcdc_match(device_t, cfdata_t, void *);
67 1.1 jmcneill static void tilcdc_attach(device_t, device_t, void *);
68 1.1 jmcneill
69 1.1 jmcneill static int tilcdc_set_busid(struct drm_device *, struct drm_master *);
70 1.1 jmcneill
71 1.1 jmcneill static int tilcdc_load(struct drm_device *, unsigned long);
72 1.1 jmcneill static int tilcdc_unload(struct drm_device *);
73 1.1 jmcneill
74 1.1 jmcneill static struct drm_driver tilcdc_driver = {
75 1.1 jmcneill .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME,
76 1.1 jmcneill .dev_priv_size = 0,
77 1.1 jmcneill .load = tilcdc_load,
78 1.1 jmcneill .unload = tilcdc_unload,
79 1.1 jmcneill
80 1.1 jmcneill .gem_free_object = drm_gem_cma_free_object,
81 1.1 jmcneill .mmap_object = drm_gem_or_legacy_mmap_object,
82 1.1 jmcneill .gem_uvm_ops = &drm_gem_cma_uvm_ops,
83 1.1 jmcneill
84 1.1 jmcneill .dumb_create = drm_gem_cma_dumb_create,
85 1.1 jmcneill .dumb_map_offset = drm_gem_cma_dumb_map_offset,
86 1.1 jmcneill .dumb_destroy = drm_gem_dumb_destroy,
87 1.1 jmcneill
88 1.1 jmcneill .name = DRIVER_NAME,
89 1.1 jmcneill .desc = DRIVER_DESC,
90 1.1 jmcneill .date = DRIVER_DATE,
91 1.1 jmcneill .major = DRIVER_MAJOR,
92 1.1 jmcneill .minor = DRIVER_MINOR,
93 1.1 jmcneill .patchlevel = DRIVER_PATCHLEVEL,
94 1.1 jmcneill
95 1.1 jmcneill .set_busid = tilcdc_set_busid,
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill CFATTACH_DECL_NEW(ti_lcdc, sizeof(struct tilcdc_softc),
99 1.1 jmcneill tilcdc_match, tilcdc_attach, NULL, NULL);
100 1.1 jmcneill
101 1.1 jmcneill static int
102 1.1 jmcneill tilcdc_mode_do_set_base(struct drm_crtc *crtc, struct drm_framebuffer *fb,
103 1.1 jmcneill int x, int y, int atomic)
104 1.1 jmcneill {
105 1.1 jmcneill struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
106 1.1 jmcneill struct tilcdc_softc * const sc = mixer_crtc->sc;
107 1.1 jmcneill struct tilcdc_framebuffer *sfb = atomic?
108 1.1 jmcneill to_tilcdc_framebuffer(fb) :
109 1.1 jmcneill to_tilcdc_framebuffer(crtc->primary->fb);
110 1.1 jmcneill
111 1.1 jmcneill const uint32_t paddr = (uint32_t)sfb->obj->dmamap->dm_segs[0].ds_addr;
112 1.1 jmcneill const u_int psize = sfb->obj->dmamap->dm_segs[0].ds_len;
113 1.1 jmcneill
114 1.1 jmcneill /* Framebuffer start address */
115 1.1 jmcneill WR4(sc, LCD_LCDDMA_FB0_BASE, paddr);
116 1.1 jmcneill WR4(sc, LCD_LCDDMA_FB0_CEILING, paddr + psize - 1);
117 1.1 jmcneill
118 1.1 jmcneill return 0;
119 1.1 jmcneill }
120 1.1 jmcneill
121 1.1 jmcneill static void
122 1.1 jmcneill tilcdc_destroy(struct drm_crtc *crtc)
123 1.1 jmcneill {
124 1.1 jmcneill drm_crtc_cleanup(crtc);
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
128 1.1 jmcneill .set_config = drm_crtc_helper_set_config,
129 1.1 jmcneill .destroy = tilcdc_destroy,
130 1.1 jmcneill };
131 1.1 jmcneill
132 1.1 jmcneill static void
133 1.1 jmcneill tilcdc_dpms(struct drm_crtc *crtc, int mode)
134 1.1 jmcneill {
135 1.1 jmcneill }
136 1.1 jmcneill
137 1.1 jmcneill static bool
138 1.1 jmcneill tilcdc_mode_fixup(struct drm_crtc *crtc,
139 1.1 jmcneill const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
140 1.1 jmcneill {
141 1.2 jmcneill #if 0
142 1.1 jmcneill adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
143 1.1 jmcneill adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
144 1.1 jmcneill
145 1.1 jmcneill adjusted_mode->flags &= ~(DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PHSYNC);
146 1.1 jmcneill if (mode->flags & DRM_MODE_FLAG_NHSYNC)
147 1.1 jmcneill adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
148 1.1 jmcneill else
149 1.1 jmcneill adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
150 1.2 jmcneill #endif
151 1.1 jmcneill
152 1.1 jmcneill return true;
153 1.1 jmcneill }
154 1.1 jmcneill
155 1.1 jmcneill static int
156 1.1 jmcneill tilcdc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
157 1.1 jmcneill struct drm_display_mode *adjusted_mode, int x, int y,
158 1.1 jmcneill struct drm_framebuffer *old_fb)
159 1.1 jmcneill {
160 1.1 jmcneill struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
161 1.1 jmcneill struct tilcdc_softc * const sc = mixer_crtc->sc;
162 1.3 jmcneill int clk_div, div, diff, best_diff;
163 1.1 jmcneill uint32_t val;
164 1.1 jmcneill
165 1.1 jmcneill const u_int hspw = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
166 1.1 jmcneill const u_int hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
167 1.1 jmcneill const u_int hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
168 1.1 jmcneill const u_int vspw = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
169 1.1 jmcneill const u_int vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
170 1.1 jmcneill const u_int vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
171 1.1 jmcneill
172 1.1 jmcneill const u_int rate = clk_get_rate(sc->sc_clk);
173 1.3 jmcneill
174 1.3 jmcneill clk_div = 255;
175 1.3 jmcneill best_diff = -1;
176 1.3 jmcneill for (div = 2; div < 255; div++) {
177 1.3 jmcneill const int pixel_clock = (rate / div) / 1000;
178 1.3 jmcneill diff = abs(adjusted_mode->crtc_clock - pixel_clock);
179 1.3 jmcneill if (best_diff == -1 || diff < best_diff) {
180 1.3 jmcneill best_diff = diff;
181 1.3 jmcneill clk_div = div;
182 1.3 jmcneill }
183 1.1 jmcneill }
184 1.1 jmcneill if (clk_div == 255) {
185 1.1 jmcneill device_printf(sc->sc_dev, "couldn't configure pixel clock (%u)\n",
186 1.1 jmcneill adjusted_mode->crtc_clock);
187 1.1 jmcneill return ERANGE;
188 1.1 jmcneill }
189 1.1 jmcneill
190 1.1 jmcneill val = CTRL_RASTER_MODE |
191 1.1 jmcneill (clk_div << CTRL_DIV_SHIFT);
192 1.1 jmcneill WR4(sc, LCD_CTRL, val);
193 1.1 jmcneill
194 1.1 jmcneill val = RASTER_TIMING_0_HFP(hfp) |
195 1.1 jmcneill RASTER_TIMING_0_HBP(hbp) |
196 1.1 jmcneill RASTER_TIMING_0_HSW(hspw) |
197 1.1 jmcneill RASTER_TIMING_0_PPL(adjusted_mode->hdisplay);
198 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_0, val);
199 1.1 jmcneill
200 1.1 jmcneill val = RASTER_TIMING_1_VFP(vfp) |
201 1.1 jmcneill RASTER_TIMING_1_VBP(vbp) |
202 1.1 jmcneill RASTER_TIMING_1_VSW(vspw) |
203 1.1 jmcneill RASTER_TIMING_1_LPP(adjusted_mode->vdisplay);
204 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_1, val);
205 1.1 jmcneill
206 1.1 jmcneill val = RASTER_TIMING_2_HFP(hfp) |
207 1.1 jmcneill RASTER_TIMING_2_HBP(hbp) |
208 1.1 jmcneill RASTER_TIMING_2_HSW(hspw) |
209 1.1 jmcneill RASTER_TIMING_2_LPP(adjusted_mode->vdisplay);
210 1.1 jmcneill /* XXX TDA HDMI TX */
211 1.1 jmcneill val |= RASTER_TIMING_2_IPC;
212 1.1 jmcneill val |= RASTER_TIMING_2_PHSVS;
213 1.1 jmcneill val |= RASTER_TIMING_2_PHSVS_RISE;
214 1.1 jmcneill val |= RASTER_TIMING_2_ACB(255);
215 1.1 jmcneill val |= RASTER_TIMING_2_ACBI(0);
216 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_2, val);
217 1.1 jmcneill
218 1.1 jmcneill val = (4 << LCDDMA_CTRL_BURST_SIZE_SHIFT) |
219 1.1 jmcneill (0 << LCDDMA_CTRL_TH_FIFO_RDY_SHIFT) |
220 1.1 jmcneill LCDDMA_CTRL_FB0_ONLY;
221 1.1 jmcneill WR4(sc, LCD_LCDDMA_CTRL, val);
222 1.1 jmcneill
223 1.1 jmcneill /* XXX TDA HDMI TX */
224 1.1 jmcneill val = RASTER_CTRL_LCDTFT |
225 1.1 jmcneill RASTER_CTRL_TFT24 |
226 1.1 jmcneill RASTER_CTRL_TFT24_UNPACKED |
227 1.1 jmcneill RASTER_CTRL_REQDLY(0x80) |
228 1.1 jmcneill RASTER_CTRL_PALMODE_DATA_ONLY;
229 1.1 jmcneill WR4(sc, LCD_RASTER_CTRL, val);
230 1.1 jmcneill
231 1.1 jmcneill tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
232 1.1 jmcneill
233 1.1 jmcneill return 0;
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static int
237 1.1 jmcneill tilcdc_mode_set_base(struct drm_crtc *crtc, int x, int y,
238 1.1 jmcneill struct drm_framebuffer *old_fb)
239 1.1 jmcneill {
240 1.1 jmcneill tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
241 1.1 jmcneill
242 1.1 jmcneill return 0;
243 1.1 jmcneill }
244 1.1 jmcneill
245 1.1 jmcneill static int
246 1.1 jmcneill tilcdc_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
247 1.1 jmcneill int x, int y, enum mode_set_atomic state)
248 1.1 jmcneill {
249 1.1 jmcneill tilcdc_mode_do_set_base(crtc, fb, x, y, 1);
250 1.1 jmcneill
251 1.1 jmcneill return 0;
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill static void
255 1.1 jmcneill tilcdc_disable(struct drm_crtc *crtc)
256 1.1 jmcneill {
257 1.1 jmcneill }
258 1.1 jmcneill
259 1.1 jmcneill static void
260 1.1 jmcneill tilcdc_prepare(struct drm_crtc *crtc)
261 1.1 jmcneill {
262 1.1 jmcneill }
263 1.1 jmcneill
264 1.1 jmcneill static void
265 1.1 jmcneill tilcdc_commit(struct drm_crtc *crtc)
266 1.1 jmcneill {
267 1.1 jmcneill struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
268 1.1 jmcneill struct tilcdc_softc * const sc = mixer_crtc->sc;
269 1.1 jmcneill uint32_t val;
270 1.1 jmcneill
271 1.1 jmcneill WR4(sc, LCD_CLKC_ENABLE, CLKC_ENABLE_DMA | CLKC_ENABLE_CORE);
272 1.1 jmcneill WR4(sc, LCD_CLKC_RESET, CLKC_RESET_MAIN);
273 1.1 jmcneill delay(100);
274 1.1 jmcneill WR4(sc, LCD_CLKC_RESET, 0);
275 1.1 jmcneill
276 1.1 jmcneill val = RD4(sc, LCD_RASTER_CTRL);
277 1.1 jmcneill WR4(sc, LCD_RASTER_CTRL, val | RASTER_CTRL_LCDEN);
278 1.1 jmcneill }
279 1.1 jmcneill
280 1.1 jmcneill static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
281 1.1 jmcneill .dpms = tilcdc_dpms,
282 1.1 jmcneill .mode_fixup = tilcdc_mode_fixup,
283 1.1 jmcneill .mode_set = tilcdc_mode_set,
284 1.1 jmcneill .mode_set_base = tilcdc_mode_set_base,
285 1.1 jmcneill .mode_set_base_atomic = tilcdc_mode_set_base_atomic,
286 1.1 jmcneill .disable = tilcdc_disable,
287 1.1 jmcneill .prepare = tilcdc_prepare,
288 1.1 jmcneill .commit = tilcdc_commit,
289 1.1 jmcneill };
290 1.1 jmcneill
291 1.1 jmcneill static void
292 1.1 jmcneill tilcdc_encoder_destroy(struct drm_encoder *encoder)
293 1.1 jmcneill {
294 1.1 jmcneill }
295 1.1 jmcneill
296 1.1 jmcneill static const struct drm_encoder_funcs tilcdc_encoder_funcs = {
297 1.1 jmcneill .destroy = tilcdc_encoder_destroy,
298 1.1 jmcneill };
299 1.1 jmcneill
300 1.1 jmcneill static void
301 1.1 jmcneill tilcdc_encoder_dpms(struct drm_encoder *encoder, int mode)
302 1.1 jmcneill {
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.1 jmcneill static bool
306 1.1 jmcneill tilcdc_encoder_mode_fixup(struct drm_encoder *encoder,
307 1.1 jmcneill const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
308 1.1 jmcneill {
309 1.1 jmcneill return true;
310 1.1 jmcneill }
311 1.1 jmcneill
312 1.1 jmcneill static void
313 1.1 jmcneill tilcdc_encoder_mode_set(struct drm_encoder *encoder,
314 1.1 jmcneill struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
315 1.1 jmcneill {
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill static void
319 1.1 jmcneill tilcdc_encoder_prepare(struct drm_encoder *encoder)
320 1.1 jmcneill {
321 1.1 jmcneill }
322 1.1 jmcneill
323 1.1 jmcneill static void
324 1.1 jmcneill tilcdc_encoder_commit(struct drm_encoder *encoder)
325 1.1 jmcneill {
326 1.1 jmcneill }
327 1.1 jmcneill
328 1.1 jmcneill static const struct drm_encoder_helper_funcs tilcdc_encoder_helper_funcs = {
329 1.1 jmcneill .dpms = tilcdc_encoder_dpms,
330 1.1 jmcneill .mode_fixup = tilcdc_encoder_mode_fixup,
331 1.1 jmcneill .prepare = tilcdc_encoder_prepare,
332 1.1 jmcneill .commit = tilcdc_encoder_commit,
333 1.1 jmcneill .mode_set = tilcdc_encoder_mode_set,
334 1.1 jmcneill };
335 1.1 jmcneill
336 1.1 jmcneill static int
337 1.1 jmcneill tilcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
338 1.1 jmcneill {
339 1.1 jmcneill struct tilcdc_softc * const sc = device_private(dev);
340 1.1 jmcneill struct drm_device *ddev = sc->sc_ddev;
341 1.1 jmcneill
342 1.1 jmcneill if (!activate)
343 1.1 jmcneill return EINVAL;
344 1.1 jmcneill
345 1.1 jmcneill sc->sc_crtc.sc = sc;
346 1.1 jmcneill
347 1.1 jmcneill WR4(sc, LCD_SYSCONFIG, SYSCONFIG_STANDBY_SMART | SYSCONFIG_IDLE_SMART);
348 1.1 jmcneill
349 1.1 jmcneill drm_crtc_init(ddev, &sc->sc_crtc.base, &tilcdc_crtc_funcs);
350 1.1 jmcneill drm_crtc_helper_add(&sc->sc_crtc.base, &tilcdc_crtc_helper_funcs);
351 1.1 jmcneill
352 1.1 jmcneill sc->sc_encoder.sc = sc;
353 1.1 jmcneill sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(&sc->sc_crtc.base);
354 1.1 jmcneill
355 1.1 jmcneill drm_encoder_init(ddev, &sc->sc_encoder.base, &tilcdc_encoder_funcs,
356 1.1 jmcneill DRM_MODE_ENCODER_TMDS);
357 1.1 jmcneill drm_encoder_helper_add(&sc->sc_encoder.base, &tilcdc_encoder_helper_funcs);
358 1.1 jmcneill
359 1.1 jmcneill return fdt_endpoint_activate(ep, activate);
360 1.1 jmcneill }
361 1.1 jmcneill
362 1.1 jmcneill static void *
363 1.1 jmcneill tilcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
364 1.1 jmcneill {
365 1.1 jmcneill struct tilcdc_softc * const sc = device_private(dev);
366 1.1 jmcneill
367 1.1 jmcneill return &sc->sc_encoder.base;
368 1.1 jmcneill }
369 1.1 jmcneill
370 1.1 jmcneill static int
371 1.1 jmcneill tilcdc_match(device_t parent, cfdata_t cf, void *aux)
372 1.1 jmcneill {
373 1.1 jmcneill struct fdt_attach_args * const faa = aux;
374 1.1 jmcneill
375 1.5 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
376 1.1 jmcneill }
377 1.1 jmcneill
378 1.1 jmcneill static void
379 1.1 jmcneill tilcdc_attach(device_t parent, device_t self, void *aux)
380 1.1 jmcneill {
381 1.1 jmcneill struct tilcdc_softc * const sc = device_private(self);
382 1.1 jmcneill struct fdt_attach_args * const faa = aux;
383 1.1 jmcneill const int phandle = faa->faa_phandle;
384 1.1 jmcneill struct drm_driver * const driver = &tilcdc_driver;
385 1.1 jmcneill prop_dictionary_t dict = device_properties(self);
386 1.1 jmcneill bool is_disabled;
387 1.1 jmcneill bus_addr_t addr;
388 1.1 jmcneill bus_size_t size;
389 1.1 jmcneill int error;
390 1.1 jmcneill
391 1.1 jmcneill if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
392 1.1 jmcneill aprint_normal(": TI LCDC (disabled)\n");
393 1.1 jmcneill return;
394 1.1 jmcneill }
395 1.1 jmcneill
396 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
397 1.1 jmcneill aprint_error(": couldn't get registers\n");
398 1.1 jmcneill return;
399 1.1 jmcneill }
400 1.1 jmcneill
401 1.1 jmcneill sc->sc_dev = self;
402 1.1 jmcneill sc->sc_phandle = faa->faa_phandle;
403 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
404 1.1 jmcneill sc->sc_bst = faa->faa_bst;
405 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
406 1.1 jmcneill aprint_error(": couldn't map registers\n");
407 1.1 jmcneill return;
408 1.1 jmcneill }
409 1.1 jmcneill sc->sc_clk = ti_prcm_get_hwmod(phandle, 0);
410 1.1 jmcneill if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
411 1.1 jmcneill aprint_error(": couldn't enable module\n");
412 1.1 jmcneill return;
413 1.1 jmcneill }
414 1.1 jmcneill
415 1.1 jmcneill aprint_naive("\n");
416 1.1 jmcneill aprint_normal(": TI LCDC\n");
417 1.1 jmcneill
418 1.1 jmcneill sc->sc_ports.dp_ep_activate = tilcdc_ep_activate;
419 1.1 jmcneill sc->sc_ports.dp_ep_get_data = tilcdc_ep_get_data;
420 1.1 jmcneill fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
421 1.1 jmcneill
422 1.1 jmcneill sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
423 1.8 riastrad if (IS_ERR(sc->sc_ddev)) {
424 1.1 jmcneill aprint_error_dev(self, "couldn't allocate DRM device\n");
425 1.1 jmcneill return;
426 1.1 jmcneill }
427 1.1 jmcneill sc->sc_ddev->dev_private = sc;
428 1.1 jmcneill sc->sc_ddev->bst = sc->sc_bst;
429 1.1 jmcneill sc->sc_ddev->bus_dmat = sc->sc_dmat;
430 1.1 jmcneill sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
431 1.1 jmcneill sc->sc_ddev->dmat_subregion_p = false;
432 1.1 jmcneill
433 1.1 jmcneill error = -drm_dev_register(sc->sc_ddev, 0);
434 1.1 jmcneill if (error) {
435 1.1 jmcneill drm_dev_unref(sc->sc_ddev);
436 1.1 jmcneill aprint_error_dev(self, "couldn't register DRM device: %d\n",
437 1.1 jmcneill error);
438 1.1 jmcneill return;
439 1.1 jmcneill }
440 1.1 jmcneill
441 1.1 jmcneill aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
442 1.1 jmcneill driver->name, driver->major, driver->minor, driver->patchlevel,
443 1.1 jmcneill driver->date, sc->sc_ddev->primary->index);
444 1.1 jmcneill }
445 1.1 jmcneill
446 1.1 jmcneill static int
447 1.1 jmcneill tilcdc_set_busid(struct drm_device *ddev, struct drm_master *master)
448 1.1 jmcneill {
449 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(ddev);
450 1.1 jmcneill char id[32];
451 1.1 jmcneill
452 1.1 jmcneill snprintf(id, sizeof(id), "platform:tilcdc:%u", device_unit(sc->sc_dev));
453 1.1 jmcneill
454 1.1 jmcneill master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
455 1.1 jmcneill if (master->unique == NULL)
456 1.1 jmcneill return -ENOMEM;
457 1.1 jmcneill strcpy(master->unique, id);
458 1.1 jmcneill master->unique_len = strlen(master->unique);
459 1.1 jmcneill
460 1.1 jmcneill return 0;
461 1.1 jmcneill }
462 1.1 jmcneill
463 1.1 jmcneill static int
464 1.1 jmcneill tilcdc_fb_create_handle(struct drm_framebuffer *fb,
465 1.1 jmcneill struct drm_file *file, unsigned int *handle)
466 1.1 jmcneill {
467 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
468 1.1 jmcneill
469 1.1 jmcneill return drm_gem_handle_create(file, &sfb->obj->base, handle);
470 1.1 jmcneill }
471 1.1 jmcneill
472 1.1 jmcneill static void
473 1.1 jmcneill tilcdc_fb_destroy(struct drm_framebuffer *fb)
474 1.1 jmcneill {
475 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
476 1.1 jmcneill
477 1.1 jmcneill drm_framebuffer_cleanup(fb);
478 1.1 jmcneill drm_gem_object_unreference_unlocked(&sfb->obj->base);
479 1.1 jmcneill kmem_free(sfb, sizeof(*sfb));
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill static const struct drm_framebuffer_funcs tilcdc_framebuffer_funcs = {
483 1.1 jmcneill .create_handle = tilcdc_fb_create_handle,
484 1.1 jmcneill .destroy = tilcdc_fb_destroy,
485 1.1 jmcneill };
486 1.1 jmcneill
487 1.1 jmcneill static struct drm_framebuffer *
488 1.1 jmcneill tilcdc_fb_create(struct drm_device *ddev, struct drm_file *file,
489 1.1 jmcneill struct drm_mode_fb_cmd2 *cmd)
490 1.1 jmcneill {
491 1.1 jmcneill struct tilcdc_framebuffer *fb;
492 1.1 jmcneill struct drm_gem_object *gem_obj;
493 1.1 jmcneill int error;
494 1.1 jmcneill
495 1.1 jmcneill if (cmd->flags)
496 1.1 jmcneill return NULL;
497 1.1 jmcneill
498 1.1 jmcneill gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
499 1.1 jmcneill if (gem_obj == NULL)
500 1.1 jmcneill return NULL;
501 1.1 jmcneill
502 1.1 jmcneill fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
503 1.1 jmcneill fb->obj = to_drm_gem_cma_obj(gem_obj);
504 1.1 jmcneill fb->base.pitches[0] = cmd->pitches[0];
505 1.1 jmcneill fb->base.pitches[1] = cmd->pitches[1];
506 1.1 jmcneill fb->base.pitches[2] = cmd->pitches[2];
507 1.1 jmcneill fb->base.offsets[0] = cmd->offsets[0];
508 1.1 jmcneill fb->base.offsets[1] = cmd->offsets[2];
509 1.1 jmcneill fb->base.offsets[2] = cmd->offsets[1];
510 1.1 jmcneill fb->base.width = cmd->width;
511 1.1 jmcneill fb->base.height = cmd->height;
512 1.1 jmcneill fb->base.pixel_format = cmd->pixel_format;
513 1.1 jmcneill fb->base.bits_per_pixel = drm_format_plane_cpp(fb->base.pixel_format, 0) * 8;
514 1.1 jmcneill
515 1.1 jmcneill switch (fb->base.pixel_format) {
516 1.1 jmcneill case DRM_FORMAT_XRGB8888:
517 1.1 jmcneill case DRM_FORMAT_XBGR8888:
518 1.1 jmcneill fb->base.depth = 32;
519 1.1 jmcneill break;
520 1.1 jmcneill default:
521 1.1 jmcneill break;
522 1.1 jmcneill }
523 1.1 jmcneill
524 1.1 jmcneill error = drm_framebuffer_init(ddev, &fb->base, &tilcdc_framebuffer_funcs);
525 1.1 jmcneill if (error != 0)
526 1.1 jmcneill goto dealloc;
527 1.1 jmcneill
528 1.1 jmcneill return &fb->base;
529 1.1 jmcneill
530 1.1 jmcneill dealloc:
531 1.1 jmcneill drm_framebuffer_cleanup(&fb->base);
532 1.1 jmcneill kmem_free(fb, sizeof(*fb));
533 1.1 jmcneill drm_gem_object_unreference_unlocked(gem_obj);
534 1.1 jmcneill
535 1.1 jmcneill return NULL;
536 1.1 jmcneill }
537 1.1 jmcneill
538 1.1 jmcneill static struct drm_mode_config_funcs tilcdc_mode_config_funcs = {
539 1.1 jmcneill .fb_create = tilcdc_fb_create,
540 1.1 jmcneill };
541 1.1 jmcneill
542 1.1 jmcneill static int
543 1.1 jmcneill tilcdc_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
544 1.1 jmcneill {
545 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(helper->dev);
546 1.1 jmcneill struct drm_device *ddev = helper->dev;
547 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(helper->fb);
548 1.1 jmcneill struct drm_framebuffer *fb = helper->fb;
549 1.1 jmcneill struct tilcdcfb_attach_args tfa;
550 1.1 jmcneill const char *br_wiring;
551 1.1 jmcneill uint32_t pixel_format;
552 1.1 jmcneill int error;
553 1.1 jmcneill
554 1.1 jmcneill const u_int width = sizes->surface_width;
555 1.1 jmcneill const u_int height = sizes->surface_height;
556 1.1 jmcneill const u_int pitch = width * (32 / 8);
557 1.1 jmcneill
558 1.1 jmcneill br_wiring = fdtbus_get_string(sc->sc_phandle, "blue-and-red-wiring");
559 1.1 jmcneill if (br_wiring && strcmp(br_wiring, "straight") == 0) {
560 1.1 jmcneill pixel_format = DRM_FORMAT_XBGR8888;
561 1.1 jmcneill } else {
562 1.1 jmcneill pixel_format = DRM_FORMAT_XRGB8888;
563 1.1 jmcneill }
564 1.1 jmcneill
565 1.1 jmcneill const size_t size = roundup(height * pitch, PAGE_SIZE);
566 1.1 jmcneill
567 1.1 jmcneill sfb->obj = drm_gem_cma_create(ddev, size);
568 1.1 jmcneill if (sfb->obj == NULL) {
569 1.1 jmcneill DRM_ERROR("failed to allocate memory for framebuffer\n");
570 1.1 jmcneill return -ENOMEM;
571 1.1 jmcneill }
572 1.1 jmcneill
573 1.1 jmcneill fb->pitches[0] = pitch;
574 1.1 jmcneill fb->offsets[0] = 0;
575 1.1 jmcneill fb->width = width;
576 1.1 jmcneill fb->height = height;
577 1.1 jmcneill fb->pixel_format = pixel_format;
578 1.1 jmcneill drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
579 1.1 jmcneill
580 1.1 jmcneill error = drm_framebuffer_init(ddev, fb, &tilcdc_framebuffer_funcs);
581 1.1 jmcneill if (error != 0) {
582 1.1 jmcneill DRM_ERROR("failed to initialize framebuffer\n");
583 1.1 jmcneill return error;
584 1.1 jmcneill }
585 1.1 jmcneill
586 1.1 jmcneill memset(&tfa, 0, sizeof(tfa));
587 1.1 jmcneill tfa.tfa_drm_dev = ddev;
588 1.1 jmcneill tfa.tfa_fb_helper = helper;
589 1.1 jmcneill tfa.tfa_fb_sizes = *sizes;
590 1.1 jmcneill tfa.tfa_fb_bst = sc->sc_bst;
591 1.1 jmcneill tfa.tfa_fb_dmat = sc->sc_dmat;
592 1.1 jmcneill tfa.tfa_fb_linebytes = helper->fb->pitches[0];
593 1.1 jmcneill
594 1.6 thorpej helper->fbdev = config_found(ddev->dev, &tfa, NULL,
595 1.7 thorpej CFARGS(.iattr = "tilcdcfbbus"));
596 1.1 jmcneill if (helper->fbdev == NULL) {
597 1.1 jmcneill DRM_ERROR("unable to attach framebuffer\n");
598 1.1 jmcneill return -ENXIO;
599 1.1 jmcneill }
600 1.1 jmcneill
601 1.1 jmcneill return 0;
602 1.1 jmcneill }
603 1.1 jmcneill
604 1.1 jmcneill static struct drm_fb_helper_funcs tilcdc_fb_helper_funcs = {
605 1.1 jmcneill .fb_probe = tilcdc_fb_probe,
606 1.1 jmcneill };
607 1.1 jmcneill
608 1.1 jmcneill static int
609 1.1 jmcneill tilcdc_load(struct drm_device *ddev, unsigned long flags)
610 1.1 jmcneill {
611 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(ddev);
612 1.1 jmcneill struct tilcdc_fbdev *fbdev;
613 1.1 jmcneill struct fdt_endpoint *ep;
614 1.1 jmcneill int error;
615 1.1 jmcneill
616 1.1 jmcneill drm_mode_config_init(ddev);
617 1.1 jmcneill ddev->mode_config.min_width = 0;
618 1.1 jmcneill ddev->mode_config.min_height = 0;
619 1.1 jmcneill ddev->mode_config.max_width = 2048;
620 1.1 jmcneill ddev->mode_config.max_height = 2048;
621 1.1 jmcneill ddev->mode_config.funcs = &tilcdc_mode_config_funcs;
622 1.1 jmcneill
623 1.1 jmcneill ep = fdt_endpoint_get_from_index(&sc->sc_ports, TILCDC_PORT_OUTPUT, 0);
624 1.1 jmcneill if (ep == NULL) {
625 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't find endpoint\n");
626 1.4 mrg error = ENXIO;
627 1.4 mrg goto drmerr;
628 1.1 jmcneill }
629 1.1 jmcneill error = fdt_endpoint_activate_direct(ep, true);
630 1.1 jmcneill if (error != 0) {
631 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't activate endpoint: %d\n", error);
632 1.4 mrg error = ENXIO;
633 1.4 mrg goto drmerr;
634 1.1 jmcneill }
635 1.1 jmcneill
636 1.1 jmcneill fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
637 1.1 jmcneill
638 1.1 jmcneill drm_fb_helper_prepare(ddev, &fbdev->helper, &tilcdc_fb_helper_funcs);
639 1.1 jmcneill
640 1.1 jmcneill error = drm_fb_helper_init(ddev, &fbdev->helper, 1, 1);
641 1.1 jmcneill if (error)
642 1.4 mrg goto allocerr;
643 1.1 jmcneill
644 1.1 jmcneill fbdev->helper.fb = kmem_zalloc(sizeof(struct tilcdc_framebuffer), KM_SLEEP);
645 1.1 jmcneill
646 1.1 jmcneill drm_fb_helper_single_add_all_connectors(&fbdev->helper);
647 1.1 jmcneill
648 1.1 jmcneill drm_helper_disable_unused_functions(ddev);
649 1.1 jmcneill
650 1.1 jmcneill drm_fb_helper_initial_config(&fbdev->helper, 32);
651 1.1 jmcneill
652 1.1 jmcneill return 0;
653 1.1 jmcneill
654 1.4 mrg allocerr:
655 1.4 mrg kmem_free(fbdev, sizeof(*fbdev));
656 1.1 jmcneill drmerr:
657 1.1 jmcneill drm_mode_config_cleanup(ddev);
658 1.1 jmcneill
659 1.1 jmcneill return error;
660 1.1 jmcneill }
661 1.1 jmcneill
662 1.1 jmcneill static int
663 1.1 jmcneill tilcdc_unload(struct drm_device *ddev)
664 1.1 jmcneill {
665 1.1 jmcneill drm_mode_config_cleanup(ddev);
666 1.1 jmcneill
667 1.1 jmcneill return 0;
668 1.1 jmcneill }
669