ti_lcdc.c revision 1.2 1 1.2 jmcneill /* $NetBSD: ti_lcdc.c,v 1.2 2019/11/03 23:31:49 jmcneill 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.2 jmcneill __KERNEL_RCSID(0, "$NetBSD: ti_lcdc.c,v 1.2 2019/11/03 23:31:49 jmcneill 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.1 jmcneill static const char * const compatible[] = {
58 1.1 jmcneill "ti,am33xx-tilcdc",
59 1.1 jmcneill NULL
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.1 jmcneill uint32_t val;
163 1.1 jmcneill u_int clk_div;
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.1 jmcneill for (clk_div = 2; clk_div < 255; clk_div++) {
174 1.1 jmcneill if (rate / clk_div <= (int)adjusted_mode->crtc_clock * 1000)
175 1.1 jmcneill break;
176 1.1 jmcneill }
177 1.1 jmcneill if (clk_div == 255) {
178 1.1 jmcneill device_printf(sc->sc_dev, "couldn't configure pixel clock (%u)\n",
179 1.1 jmcneill adjusted_mode->crtc_clock);
180 1.1 jmcneill return ERANGE;
181 1.1 jmcneill }
182 1.1 jmcneill
183 1.1 jmcneill val = CTRL_RASTER_MODE |
184 1.1 jmcneill (clk_div << CTRL_DIV_SHIFT);
185 1.1 jmcneill WR4(sc, LCD_CTRL, val);
186 1.1 jmcneill
187 1.1 jmcneill val = RASTER_TIMING_0_HFP(hfp) |
188 1.1 jmcneill RASTER_TIMING_0_HBP(hbp) |
189 1.1 jmcneill RASTER_TIMING_0_HSW(hspw) |
190 1.1 jmcneill RASTER_TIMING_0_PPL(adjusted_mode->hdisplay);
191 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_0, val);
192 1.1 jmcneill
193 1.1 jmcneill val = RASTER_TIMING_1_VFP(vfp) |
194 1.1 jmcneill RASTER_TIMING_1_VBP(vbp) |
195 1.1 jmcneill RASTER_TIMING_1_VSW(vspw) |
196 1.1 jmcneill RASTER_TIMING_1_LPP(adjusted_mode->vdisplay);
197 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_1, val);
198 1.1 jmcneill
199 1.1 jmcneill val = RASTER_TIMING_2_HFP(hfp) |
200 1.1 jmcneill RASTER_TIMING_2_HBP(hbp) |
201 1.1 jmcneill RASTER_TIMING_2_HSW(hspw) |
202 1.1 jmcneill RASTER_TIMING_2_LPP(adjusted_mode->vdisplay);
203 1.1 jmcneill /* XXX TDA HDMI TX */
204 1.1 jmcneill val |= RASTER_TIMING_2_IPC;
205 1.1 jmcneill val |= RASTER_TIMING_2_PHSVS;
206 1.1 jmcneill val |= RASTER_TIMING_2_PHSVS_RISE;
207 1.1 jmcneill val |= RASTER_TIMING_2_ACB(255);
208 1.1 jmcneill val |= RASTER_TIMING_2_ACBI(0);
209 1.1 jmcneill WR4(sc, LCD_RASTER_TIMING_2, val);
210 1.1 jmcneill
211 1.1 jmcneill val = (4 << LCDDMA_CTRL_BURST_SIZE_SHIFT) |
212 1.1 jmcneill (0 << LCDDMA_CTRL_TH_FIFO_RDY_SHIFT) |
213 1.1 jmcneill LCDDMA_CTRL_FB0_ONLY;
214 1.1 jmcneill WR4(sc, LCD_LCDDMA_CTRL, val);
215 1.1 jmcneill
216 1.1 jmcneill /* XXX TDA HDMI TX */
217 1.1 jmcneill val = RASTER_CTRL_LCDTFT |
218 1.1 jmcneill RASTER_CTRL_TFT24 |
219 1.1 jmcneill RASTER_CTRL_TFT24_UNPACKED |
220 1.1 jmcneill RASTER_CTRL_REQDLY(0x80) |
221 1.1 jmcneill RASTER_CTRL_PALMODE_DATA_ONLY;
222 1.1 jmcneill WR4(sc, LCD_RASTER_CTRL, val);
223 1.1 jmcneill
224 1.1 jmcneill tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
225 1.1 jmcneill
226 1.1 jmcneill return 0;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill static int
230 1.1 jmcneill tilcdc_mode_set_base(struct drm_crtc *crtc, int x, int y,
231 1.1 jmcneill struct drm_framebuffer *old_fb)
232 1.1 jmcneill {
233 1.1 jmcneill tilcdc_mode_do_set_base(crtc, old_fb, x, y, 0);
234 1.1 jmcneill
235 1.1 jmcneill return 0;
236 1.1 jmcneill }
237 1.1 jmcneill
238 1.1 jmcneill static int
239 1.1 jmcneill tilcdc_mode_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
240 1.1 jmcneill int x, int y, enum mode_set_atomic state)
241 1.1 jmcneill {
242 1.1 jmcneill tilcdc_mode_do_set_base(crtc, fb, x, y, 1);
243 1.1 jmcneill
244 1.1 jmcneill return 0;
245 1.1 jmcneill }
246 1.1 jmcneill
247 1.1 jmcneill static void
248 1.1 jmcneill tilcdc_disable(struct drm_crtc *crtc)
249 1.1 jmcneill {
250 1.1 jmcneill }
251 1.1 jmcneill
252 1.1 jmcneill static void
253 1.1 jmcneill tilcdc_prepare(struct drm_crtc *crtc)
254 1.1 jmcneill {
255 1.1 jmcneill }
256 1.1 jmcneill
257 1.1 jmcneill static void
258 1.1 jmcneill tilcdc_commit(struct drm_crtc *crtc)
259 1.1 jmcneill {
260 1.1 jmcneill struct tilcdc_crtc *mixer_crtc = to_tilcdc_crtc(crtc);
261 1.1 jmcneill struct tilcdc_softc * const sc = mixer_crtc->sc;
262 1.1 jmcneill uint32_t val;
263 1.1 jmcneill
264 1.1 jmcneill WR4(sc, LCD_CLKC_ENABLE, CLKC_ENABLE_DMA | CLKC_ENABLE_CORE);
265 1.1 jmcneill WR4(sc, LCD_CLKC_RESET, CLKC_RESET_MAIN);
266 1.1 jmcneill delay(100);
267 1.1 jmcneill WR4(sc, LCD_CLKC_RESET, 0);
268 1.1 jmcneill
269 1.1 jmcneill val = RD4(sc, LCD_RASTER_CTRL);
270 1.1 jmcneill WR4(sc, LCD_RASTER_CTRL, val | RASTER_CTRL_LCDEN);
271 1.1 jmcneill }
272 1.1 jmcneill
273 1.1 jmcneill static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
274 1.1 jmcneill .dpms = tilcdc_dpms,
275 1.1 jmcneill .mode_fixup = tilcdc_mode_fixup,
276 1.1 jmcneill .mode_set = tilcdc_mode_set,
277 1.1 jmcneill .mode_set_base = tilcdc_mode_set_base,
278 1.1 jmcneill .mode_set_base_atomic = tilcdc_mode_set_base_atomic,
279 1.1 jmcneill .disable = tilcdc_disable,
280 1.1 jmcneill .prepare = tilcdc_prepare,
281 1.1 jmcneill .commit = tilcdc_commit,
282 1.1 jmcneill };
283 1.1 jmcneill
284 1.1 jmcneill static void
285 1.1 jmcneill tilcdc_encoder_destroy(struct drm_encoder *encoder)
286 1.1 jmcneill {
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill static const struct drm_encoder_funcs tilcdc_encoder_funcs = {
290 1.1 jmcneill .destroy = tilcdc_encoder_destroy,
291 1.1 jmcneill };
292 1.1 jmcneill
293 1.1 jmcneill static void
294 1.1 jmcneill tilcdc_encoder_dpms(struct drm_encoder *encoder, int mode)
295 1.1 jmcneill {
296 1.1 jmcneill }
297 1.1 jmcneill
298 1.1 jmcneill static bool
299 1.1 jmcneill tilcdc_encoder_mode_fixup(struct drm_encoder *encoder,
300 1.1 jmcneill const struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
301 1.1 jmcneill {
302 1.1 jmcneill return true;
303 1.1 jmcneill }
304 1.1 jmcneill
305 1.1 jmcneill static void
306 1.1 jmcneill tilcdc_encoder_mode_set(struct drm_encoder *encoder,
307 1.1 jmcneill struct drm_display_mode *mode, struct drm_display_mode *adjusted_mode)
308 1.1 jmcneill {
309 1.1 jmcneill }
310 1.1 jmcneill
311 1.1 jmcneill static void
312 1.1 jmcneill tilcdc_encoder_prepare(struct drm_encoder *encoder)
313 1.1 jmcneill {
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill static void
317 1.1 jmcneill tilcdc_encoder_commit(struct drm_encoder *encoder)
318 1.1 jmcneill {
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill static const struct drm_encoder_helper_funcs tilcdc_encoder_helper_funcs = {
322 1.1 jmcneill .dpms = tilcdc_encoder_dpms,
323 1.1 jmcneill .mode_fixup = tilcdc_encoder_mode_fixup,
324 1.1 jmcneill .prepare = tilcdc_encoder_prepare,
325 1.1 jmcneill .commit = tilcdc_encoder_commit,
326 1.1 jmcneill .mode_set = tilcdc_encoder_mode_set,
327 1.1 jmcneill };
328 1.1 jmcneill
329 1.1 jmcneill static int
330 1.1 jmcneill tilcdc_ep_activate(device_t dev, struct fdt_endpoint *ep, bool activate)
331 1.1 jmcneill {
332 1.1 jmcneill struct tilcdc_softc * const sc = device_private(dev);
333 1.1 jmcneill struct drm_device *ddev = sc->sc_ddev;
334 1.1 jmcneill
335 1.1 jmcneill if (!activate)
336 1.1 jmcneill return EINVAL;
337 1.1 jmcneill
338 1.1 jmcneill sc->sc_crtc.sc = sc;
339 1.1 jmcneill
340 1.1 jmcneill WR4(sc, LCD_SYSCONFIG, SYSCONFIG_STANDBY_SMART | SYSCONFIG_IDLE_SMART);
341 1.1 jmcneill
342 1.1 jmcneill drm_crtc_init(ddev, &sc->sc_crtc.base, &tilcdc_crtc_funcs);
343 1.1 jmcneill drm_crtc_helper_add(&sc->sc_crtc.base, &tilcdc_crtc_helper_funcs);
344 1.1 jmcneill
345 1.1 jmcneill sc->sc_encoder.sc = sc;
346 1.1 jmcneill sc->sc_encoder.base.possible_crtcs = 1 << drm_crtc_index(&sc->sc_crtc.base);
347 1.1 jmcneill
348 1.1 jmcneill drm_encoder_init(ddev, &sc->sc_encoder.base, &tilcdc_encoder_funcs,
349 1.1 jmcneill DRM_MODE_ENCODER_TMDS);
350 1.1 jmcneill drm_encoder_helper_add(&sc->sc_encoder.base, &tilcdc_encoder_helper_funcs);
351 1.1 jmcneill
352 1.1 jmcneill return fdt_endpoint_activate(ep, activate);
353 1.1 jmcneill }
354 1.1 jmcneill
355 1.1 jmcneill static void *
356 1.1 jmcneill tilcdc_ep_get_data(device_t dev, struct fdt_endpoint *ep)
357 1.1 jmcneill {
358 1.1 jmcneill struct tilcdc_softc * const sc = device_private(dev);
359 1.1 jmcneill
360 1.1 jmcneill return &sc->sc_encoder.base;
361 1.1 jmcneill }
362 1.1 jmcneill
363 1.1 jmcneill static int
364 1.1 jmcneill tilcdc_match(device_t parent, cfdata_t cf, void *aux)
365 1.1 jmcneill {
366 1.1 jmcneill struct fdt_attach_args * const faa = aux;
367 1.1 jmcneill
368 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
369 1.1 jmcneill }
370 1.1 jmcneill
371 1.1 jmcneill static void
372 1.1 jmcneill tilcdc_attach(device_t parent, device_t self, void *aux)
373 1.1 jmcneill {
374 1.1 jmcneill struct tilcdc_softc * const sc = device_private(self);
375 1.1 jmcneill struct fdt_attach_args * const faa = aux;
376 1.1 jmcneill const int phandle = faa->faa_phandle;
377 1.1 jmcneill struct drm_driver * const driver = &tilcdc_driver;
378 1.1 jmcneill prop_dictionary_t dict = device_properties(self);
379 1.1 jmcneill bool is_disabled;
380 1.1 jmcneill bus_addr_t addr;
381 1.1 jmcneill bus_size_t size;
382 1.1 jmcneill int error;
383 1.1 jmcneill
384 1.1 jmcneill if (prop_dictionary_get_bool(dict, "disabled", &is_disabled) && is_disabled) {
385 1.1 jmcneill aprint_normal(": TI LCDC (disabled)\n");
386 1.1 jmcneill return;
387 1.1 jmcneill }
388 1.1 jmcneill
389 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
390 1.1 jmcneill aprint_error(": couldn't get registers\n");
391 1.1 jmcneill return;
392 1.1 jmcneill }
393 1.1 jmcneill
394 1.1 jmcneill sc->sc_dev = self;
395 1.1 jmcneill sc->sc_phandle = faa->faa_phandle;
396 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
397 1.1 jmcneill sc->sc_bst = faa->faa_bst;
398 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
399 1.1 jmcneill aprint_error(": couldn't map registers\n");
400 1.1 jmcneill return;
401 1.1 jmcneill }
402 1.1 jmcneill sc->sc_clk = ti_prcm_get_hwmod(phandle, 0);
403 1.1 jmcneill if (sc->sc_clk == NULL || clk_enable(sc->sc_clk) != 0) {
404 1.1 jmcneill aprint_error(": couldn't enable module\n");
405 1.1 jmcneill return;
406 1.1 jmcneill }
407 1.1 jmcneill
408 1.1 jmcneill aprint_naive("\n");
409 1.1 jmcneill aprint_normal(": TI LCDC\n");
410 1.1 jmcneill
411 1.1 jmcneill sc->sc_ports.dp_ep_activate = tilcdc_ep_activate;
412 1.1 jmcneill sc->sc_ports.dp_ep_get_data = tilcdc_ep_get_data;
413 1.1 jmcneill fdt_ports_register(&sc->sc_ports, self, phandle, EP_DRM_ENCODER);
414 1.1 jmcneill
415 1.1 jmcneill sc->sc_ddev = drm_dev_alloc(driver, sc->sc_dev);
416 1.1 jmcneill if (sc->sc_ddev == NULL) {
417 1.1 jmcneill aprint_error_dev(self, "couldn't allocate DRM device\n");
418 1.1 jmcneill return;
419 1.1 jmcneill }
420 1.1 jmcneill sc->sc_ddev->dev_private = sc;
421 1.1 jmcneill sc->sc_ddev->bst = sc->sc_bst;
422 1.1 jmcneill sc->sc_ddev->bus_dmat = sc->sc_dmat;
423 1.1 jmcneill sc->sc_ddev->dmat = sc->sc_ddev->bus_dmat;
424 1.1 jmcneill sc->sc_ddev->dmat_subregion_p = false;
425 1.1 jmcneill
426 1.1 jmcneill error = -drm_dev_register(sc->sc_ddev, 0);
427 1.1 jmcneill if (error) {
428 1.1 jmcneill drm_dev_unref(sc->sc_ddev);
429 1.1 jmcneill aprint_error_dev(self, "couldn't register DRM device: %d\n",
430 1.1 jmcneill error);
431 1.1 jmcneill return;
432 1.1 jmcneill }
433 1.1 jmcneill
434 1.1 jmcneill aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
435 1.1 jmcneill driver->name, driver->major, driver->minor, driver->patchlevel,
436 1.1 jmcneill driver->date, sc->sc_ddev->primary->index);
437 1.1 jmcneill }
438 1.1 jmcneill
439 1.1 jmcneill static int
440 1.1 jmcneill tilcdc_set_busid(struct drm_device *ddev, struct drm_master *master)
441 1.1 jmcneill {
442 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(ddev);
443 1.1 jmcneill char id[32];
444 1.1 jmcneill
445 1.1 jmcneill snprintf(id, sizeof(id), "platform:tilcdc:%u", device_unit(sc->sc_dev));
446 1.1 jmcneill
447 1.1 jmcneill master->unique = kzalloc(strlen(id) + 1, GFP_KERNEL);
448 1.1 jmcneill if (master->unique == NULL)
449 1.1 jmcneill return -ENOMEM;
450 1.1 jmcneill strcpy(master->unique, id);
451 1.1 jmcneill master->unique_len = strlen(master->unique);
452 1.1 jmcneill
453 1.1 jmcneill return 0;
454 1.1 jmcneill }
455 1.1 jmcneill
456 1.1 jmcneill static int
457 1.1 jmcneill tilcdc_fb_create_handle(struct drm_framebuffer *fb,
458 1.1 jmcneill struct drm_file *file, unsigned int *handle)
459 1.1 jmcneill {
460 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
461 1.1 jmcneill
462 1.1 jmcneill return drm_gem_handle_create(file, &sfb->obj->base, handle);
463 1.1 jmcneill }
464 1.1 jmcneill
465 1.1 jmcneill static void
466 1.1 jmcneill tilcdc_fb_destroy(struct drm_framebuffer *fb)
467 1.1 jmcneill {
468 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(fb);
469 1.1 jmcneill
470 1.1 jmcneill drm_framebuffer_cleanup(fb);
471 1.1 jmcneill drm_gem_object_unreference_unlocked(&sfb->obj->base);
472 1.1 jmcneill kmem_free(sfb, sizeof(*sfb));
473 1.1 jmcneill }
474 1.1 jmcneill
475 1.1 jmcneill static const struct drm_framebuffer_funcs tilcdc_framebuffer_funcs = {
476 1.1 jmcneill .create_handle = tilcdc_fb_create_handle,
477 1.1 jmcneill .destroy = tilcdc_fb_destroy,
478 1.1 jmcneill };
479 1.1 jmcneill
480 1.1 jmcneill static struct drm_framebuffer *
481 1.1 jmcneill tilcdc_fb_create(struct drm_device *ddev, struct drm_file *file,
482 1.1 jmcneill struct drm_mode_fb_cmd2 *cmd)
483 1.1 jmcneill {
484 1.1 jmcneill struct tilcdc_framebuffer *fb;
485 1.1 jmcneill struct drm_gem_object *gem_obj;
486 1.1 jmcneill int error;
487 1.1 jmcneill
488 1.1 jmcneill if (cmd->flags)
489 1.1 jmcneill return NULL;
490 1.1 jmcneill
491 1.1 jmcneill gem_obj = drm_gem_object_lookup(ddev, file, cmd->handles[0]);
492 1.1 jmcneill if (gem_obj == NULL)
493 1.1 jmcneill return NULL;
494 1.1 jmcneill
495 1.1 jmcneill fb = kmem_zalloc(sizeof(*fb), KM_SLEEP);
496 1.1 jmcneill fb->obj = to_drm_gem_cma_obj(gem_obj);
497 1.1 jmcneill fb->base.pitches[0] = cmd->pitches[0];
498 1.1 jmcneill fb->base.pitches[1] = cmd->pitches[1];
499 1.1 jmcneill fb->base.pitches[2] = cmd->pitches[2];
500 1.1 jmcneill fb->base.offsets[0] = cmd->offsets[0];
501 1.1 jmcneill fb->base.offsets[1] = cmd->offsets[2];
502 1.1 jmcneill fb->base.offsets[2] = cmd->offsets[1];
503 1.1 jmcneill fb->base.width = cmd->width;
504 1.1 jmcneill fb->base.height = cmd->height;
505 1.1 jmcneill fb->base.pixel_format = cmd->pixel_format;
506 1.1 jmcneill fb->base.bits_per_pixel = drm_format_plane_cpp(fb->base.pixel_format, 0) * 8;
507 1.1 jmcneill
508 1.1 jmcneill switch (fb->base.pixel_format) {
509 1.1 jmcneill case DRM_FORMAT_XRGB8888:
510 1.1 jmcneill case DRM_FORMAT_XBGR8888:
511 1.1 jmcneill fb->base.depth = 32;
512 1.1 jmcneill break;
513 1.1 jmcneill default:
514 1.1 jmcneill break;
515 1.1 jmcneill }
516 1.1 jmcneill
517 1.1 jmcneill error = drm_framebuffer_init(ddev, &fb->base, &tilcdc_framebuffer_funcs);
518 1.1 jmcneill if (error != 0)
519 1.1 jmcneill goto dealloc;
520 1.1 jmcneill
521 1.1 jmcneill return &fb->base;
522 1.1 jmcneill
523 1.1 jmcneill dealloc:
524 1.1 jmcneill drm_framebuffer_cleanup(&fb->base);
525 1.1 jmcneill kmem_free(fb, sizeof(*fb));
526 1.1 jmcneill drm_gem_object_unreference_unlocked(gem_obj);
527 1.1 jmcneill
528 1.1 jmcneill return NULL;
529 1.1 jmcneill }
530 1.1 jmcneill
531 1.1 jmcneill static struct drm_mode_config_funcs tilcdc_mode_config_funcs = {
532 1.1 jmcneill .fb_create = tilcdc_fb_create,
533 1.1 jmcneill };
534 1.1 jmcneill
535 1.1 jmcneill static int
536 1.1 jmcneill tilcdc_fb_probe(struct drm_fb_helper *helper, struct drm_fb_helper_surface_size *sizes)
537 1.1 jmcneill {
538 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(helper->dev);
539 1.1 jmcneill struct drm_device *ddev = helper->dev;
540 1.1 jmcneill struct tilcdc_framebuffer *sfb = to_tilcdc_framebuffer(helper->fb);
541 1.1 jmcneill struct drm_framebuffer *fb = helper->fb;
542 1.1 jmcneill struct tilcdcfb_attach_args tfa;
543 1.1 jmcneill const char *br_wiring;
544 1.1 jmcneill uint32_t pixel_format;
545 1.1 jmcneill int error;
546 1.1 jmcneill
547 1.1 jmcneill const u_int width = sizes->surface_width;
548 1.1 jmcneill const u_int height = sizes->surface_height;
549 1.1 jmcneill const u_int pitch = width * (32 / 8);
550 1.1 jmcneill
551 1.1 jmcneill br_wiring = fdtbus_get_string(sc->sc_phandle, "blue-and-red-wiring");
552 1.1 jmcneill if (br_wiring && strcmp(br_wiring, "straight") == 0) {
553 1.1 jmcneill pixel_format = DRM_FORMAT_XBGR8888;
554 1.1 jmcneill } else {
555 1.1 jmcneill pixel_format = DRM_FORMAT_XRGB8888;
556 1.1 jmcneill }
557 1.1 jmcneill
558 1.1 jmcneill const size_t size = roundup(height * pitch, PAGE_SIZE);
559 1.1 jmcneill
560 1.1 jmcneill sfb->obj = drm_gem_cma_create(ddev, size);
561 1.1 jmcneill if (sfb->obj == NULL) {
562 1.1 jmcneill DRM_ERROR("failed to allocate memory for framebuffer\n");
563 1.1 jmcneill return -ENOMEM;
564 1.1 jmcneill }
565 1.1 jmcneill
566 1.1 jmcneill fb->pitches[0] = pitch;
567 1.1 jmcneill fb->offsets[0] = 0;
568 1.1 jmcneill fb->width = width;
569 1.1 jmcneill fb->height = height;
570 1.1 jmcneill fb->pixel_format = pixel_format;
571 1.1 jmcneill drm_fb_get_bpp_depth(fb->pixel_format, &fb->depth, &fb->bits_per_pixel);
572 1.1 jmcneill
573 1.1 jmcneill error = drm_framebuffer_init(ddev, fb, &tilcdc_framebuffer_funcs);
574 1.1 jmcneill if (error != 0) {
575 1.1 jmcneill DRM_ERROR("failed to initialize framebuffer\n");
576 1.1 jmcneill return error;
577 1.1 jmcneill }
578 1.1 jmcneill
579 1.1 jmcneill memset(&tfa, 0, sizeof(tfa));
580 1.1 jmcneill tfa.tfa_drm_dev = ddev;
581 1.1 jmcneill tfa.tfa_fb_helper = helper;
582 1.1 jmcneill tfa.tfa_fb_sizes = *sizes;
583 1.1 jmcneill tfa.tfa_fb_bst = sc->sc_bst;
584 1.1 jmcneill tfa.tfa_fb_dmat = sc->sc_dmat;
585 1.1 jmcneill tfa.tfa_fb_linebytes = helper->fb->pitches[0];
586 1.1 jmcneill
587 1.1 jmcneill helper->fbdev = config_found_ia(ddev->dev, "tilcdcfbbus", &tfa, NULL);
588 1.1 jmcneill if (helper->fbdev == NULL) {
589 1.1 jmcneill DRM_ERROR("unable to attach framebuffer\n");
590 1.1 jmcneill return -ENXIO;
591 1.1 jmcneill }
592 1.1 jmcneill
593 1.1 jmcneill return 0;
594 1.1 jmcneill }
595 1.1 jmcneill
596 1.1 jmcneill static struct drm_fb_helper_funcs tilcdc_fb_helper_funcs = {
597 1.1 jmcneill .fb_probe = tilcdc_fb_probe,
598 1.1 jmcneill };
599 1.1 jmcneill
600 1.1 jmcneill static int
601 1.1 jmcneill tilcdc_load(struct drm_device *ddev, unsigned long flags)
602 1.1 jmcneill {
603 1.1 jmcneill struct tilcdc_softc * const sc = tilcdc_private(ddev);
604 1.1 jmcneill struct tilcdc_fbdev *fbdev;
605 1.1 jmcneill struct fdt_endpoint *ep;
606 1.1 jmcneill int error;
607 1.1 jmcneill
608 1.1 jmcneill drm_mode_config_init(ddev);
609 1.1 jmcneill ddev->mode_config.min_width = 0;
610 1.1 jmcneill ddev->mode_config.min_height = 0;
611 1.1 jmcneill ddev->mode_config.max_width = 2048;
612 1.1 jmcneill ddev->mode_config.max_height = 2048;
613 1.1 jmcneill ddev->mode_config.funcs = &tilcdc_mode_config_funcs;
614 1.1 jmcneill
615 1.1 jmcneill ep = fdt_endpoint_get_from_index(&sc->sc_ports, TILCDC_PORT_OUTPUT, 0);
616 1.1 jmcneill if (ep == NULL) {
617 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't find endpoint\n");
618 1.1 jmcneill return ENXIO;
619 1.1 jmcneill }
620 1.1 jmcneill error = fdt_endpoint_activate_direct(ep, true);
621 1.1 jmcneill if (error != 0) {
622 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't activate endpoint: %d\n", error);
623 1.1 jmcneill return error;
624 1.1 jmcneill }
625 1.1 jmcneill
626 1.1 jmcneill fbdev = kmem_zalloc(sizeof(*fbdev), KM_SLEEP);
627 1.1 jmcneill
628 1.1 jmcneill drm_fb_helper_prepare(ddev, &fbdev->helper, &tilcdc_fb_helper_funcs);
629 1.1 jmcneill
630 1.1 jmcneill error = drm_fb_helper_init(ddev, &fbdev->helper, 1, 1);
631 1.1 jmcneill if (error)
632 1.1 jmcneill goto drmerr;
633 1.1 jmcneill
634 1.1 jmcneill fbdev->helper.fb = kmem_zalloc(sizeof(struct tilcdc_framebuffer), KM_SLEEP);
635 1.1 jmcneill
636 1.1 jmcneill drm_fb_helper_single_add_all_connectors(&fbdev->helper);
637 1.1 jmcneill
638 1.1 jmcneill drm_helper_disable_unused_functions(ddev);
639 1.1 jmcneill
640 1.1 jmcneill drm_fb_helper_initial_config(&fbdev->helper, 32);
641 1.1 jmcneill
642 1.1 jmcneill return 0;
643 1.1 jmcneill
644 1.1 jmcneill drmerr:
645 1.1 jmcneill drm_mode_config_cleanup(ddev);
646 1.1 jmcneill kmem_free(fbdev, sizeof(*fbdev));
647 1.1 jmcneill
648 1.1 jmcneill return error;
649 1.1 jmcneill }
650 1.1 jmcneill
651 1.1 jmcneill static int
652 1.1 jmcneill tilcdc_unload(struct drm_device *ddev)
653 1.1 jmcneill {
654 1.1 jmcneill drm_mode_config_cleanup(ddev);
655 1.1 jmcneill
656 1.1 jmcneill return 0;
657 1.1 jmcneill }
658