xref: /openbmc/linux/drivers/gpu/drm/udl/udl_modeset.c (revision 6ae355a260d25d2bd465f89d1668300aaa9fec4c)
112eb90f1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
25320918bSDave Airlie /*
35320918bSDave Airlie  * Copyright (C) 2012 Red Hat
45320918bSDave Airlie  *
55320918bSDave Airlie  * based in parts on udlfb.c:
65320918bSDave Airlie  * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
75320918bSDave Airlie  * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
85320918bSDave Airlie  * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
95320918bSDave Airlie 
105320918bSDave Airlie  */
115320918bSDave Airlie 
12a8109f5bSThomas Zimmermann #include <linux/dma-buf.h>
13a8109f5bSThomas Zimmermann 
149fda81e0SThomas Zimmermann #include <drm/drm_atomic_helper.h>
15760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
16230b8b04SThomas Zimmermann #include <drm/drm_damage_helper.h>
17a8109f5bSThomas Zimmermann #include <drm/drm_fourcc.h>
189fda81e0SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
19a8109f5bSThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
20a9dcf380SSam Ravnborg #include <drm/drm_modeset_helper_vtables.h>
21a9dcf380SSam Ravnborg #include <drm/drm_vblank.h>
22a9dcf380SSam Ravnborg 
235320918bSDave Airlie #include "udl_drv.h"
245320918bSDave Airlie 
259fda81e0SThomas Zimmermann #define UDL_COLOR_DEPTH_16BPP	0
269fda81e0SThomas Zimmermann 
275320918bSDave Airlie /*
285320918bSDave Airlie  * All DisplayLink bulk operations start with 0xAF, followed by specific code
295320918bSDave Airlie  * All operations are written to buffers which then later get sent to device
305320918bSDave Airlie  */
315320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val)
325320918bSDave Airlie {
335320918bSDave Airlie 	*buf++ = 0xAF;
345320918bSDave Airlie 	*buf++ = 0x20;
355320918bSDave Airlie 	*buf++ = reg;
365320918bSDave Airlie 	*buf++ = val;
375320918bSDave Airlie 	return buf;
385320918bSDave Airlie }
395320918bSDave Airlie 
405320918bSDave Airlie static char *udl_vidreg_lock(char *buf)
415320918bSDave Airlie {
425320918bSDave Airlie 	return udl_set_register(buf, 0xFF, 0x00);
435320918bSDave Airlie }
445320918bSDave Airlie 
455320918bSDave Airlie static char *udl_vidreg_unlock(char *buf)
465320918bSDave Airlie {
475320918bSDave Airlie 	return udl_set_register(buf, 0xFF, 0xFF);
485320918bSDave Airlie }
495320918bSDave Airlie 
50997d33c3SThomas Zimmermann static char *udl_set_blank_mode(char *buf, u8 mode)
515320918bSDave Airlie {
52997d33c3SThomas Zimmermann 	return udl_set_register(buf, UDL_REG_BLANK_MODE, mode);
535320918bSDave Airlie }
545320918bSDave Airlie 
555320918bSDave Airlie static char *udl_set_color_depth(char *buf, u8 selection)
565320918bSDave Airlie {
575320918bSDave Airlie 	return udl_set_register(buf, 0x00, selection);
585320918bSDave Airlie }
595320918bSDave Airlie 
605320918bSDave Airlie static char *udl_set_base16bpp(char *wrptr, u32 base)
615320918bSDave Airlie {
625320918bSDave Airlie 	/* the base pointer is 16 bits wide, 0x20 is hi byte. */
635320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x20, base >> 16);
645320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x21, base >> 8);
655320918bSDave Airlie 	return udl_set_register(wrptr, 0x22, base);
665320918bSDave Airlie }
675320918bSDave Airlie 
685320918bSDave Airlie /*
695320918bSDave Airlie  * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
705320918bSDave Airlie  * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
715320918bSDave Airlie  */
725320918bSDave Airlie static char *udl_set_base8bpp(char *wrptr, u32 base)
735320918bSDave Airlie {
745320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x26, base >> 16);
755320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x27, base >> 8);
765320918bSDave Airlie 	return udl_set_register(wrptr, 0x28, base);
775320918bSDave Airlie }
785320918bSDave Airlie 
795320918bSDave Airlie static char *udl_set_register_16(char *wrptr, u8 reg, u16 value)
805320918bSDave Airlie {
815320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value >> 8);
825320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value);
835320918bSDave Airlie }
845320918bSDave Airlie 
855320918bSDave Airlie /*
865320918bSDave Airlie  * This is kind of weird because the controller takes some
875320918bSDave Airlie  * register values in a different byte order than other registers.
885320918bSDave Airlie  */
895320918bSDave Airlie static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value)
905320918bSDave Airlie {
915320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value);
925320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value >> 8);
935320918bSDave Airlie }
945320918bSDave Airlie 
955320918bSDave Airlie /*
965320918bSDave Airlie  * LFSR is linear feedback shift register. The reason we have this is
975320918bSDave Airlie  * because the display controller needs to minimize the clock depth of
985320918bSDave Airlie  * various counters used in the display path. So this code reverses the
995320918bSDave Airlie  * provided value into the lfsr16 value by counting backwards to get
1005320918bSDave Airlie  * the value that needs to be set in the hardware comparator to get the
1015320918bSDave Airlie  * same actual count. This makes sense once you read above a couple of
1025320918bSDave Airlie  * times and think about it from a hardware perspective.
1035320918bSDave Airlie  */
1045320918bSDave Airlie static u16 udl_lfsr16(u16 actual_count)
1055320918bSDave Airlie {
1065320918bSDave Airlie 	u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
1075320918bSDave Airlie 
1085320918bSDave Airlie 	while (actual_count--) {
1095320918bSDave Airlie 		lv =	 ((lv << 1) |
1105320918bSDave Airlie 			(((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
1115320918bSDave Airlie 			& 0xFFFF;
1125320918bSDave Airlie 	}
1135320918bSDave Airlie 
1145320918bSDave Airlie 	return (u16) lv;
1155320918bSDave Airlie }
1165320918bSDave Airlie 
1175320918bSDave Airlie /*
1185320918bSDave Airlie  * This does LFSR conversion on the value that is to be written.
1195320918bSDave Airlie  * See LFSR explanation above for more detail.
1205320918bSDave Airlie  */
1215320918bSDave Airlie static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
1225320918bSDave Airlie {
1235320918bSDave Airlie 	return udl_set_register_16(wrptr, reg, udl_lfsr16(value));
1245320918bSDave Airlie }
1255320918bSDave Airlie 
1265320918bSDave Airlie /*
1275320918bSDave Airlie  * This takes a standard fbdev screeninfo struct and all of its monitor mode
1285320918bSDave Airlie  * details and converts them into the DisplayLink equivalent register commands.
1295320918bSDave Airlie   ERR(vreg(dev,               0x00, (color_depth == 16) ? 0 : 1));
1305320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x01, xDisplayStart));
1315320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x03, xDisplayEnd));
1325320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x05, yDisplayStart));
1335320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x07, yDisplayEnd));
1345320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x09, xEndCount));
1355320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x0B, hSyncStart));
1365320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x0D, hSyncEnd));
1375320918bSDave Airlie   ERR(vreg_big_endian(dev,    0x0F, hPixels));
1385320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x11, yEndCount));
1395320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x13, vSyncStart));
1405320918bSDave Airlie   ERR(vreg_lfsr16(dev,        0x15, vSyncEnd));
1415320918bSDave Airlie   ERR(vreg_big_endian(dev,    0x17, vPixels));
1425320918bSDave Airlie   ERR(vreg_little_endian(dev, 0x1B, pixelClock5KHz));
1435320918bSDave Airlie 
1445320918bSDave Airlie   ERR(vreg(dev,               0x1F, 0));
1455320918bSDave Airlie 
1465320918bSDave Airlie   ERR(vbuf(dev, WRITE_VIDREG_UNLOCK, DSIZEOF(WRITE_VIDREG_UNLOCK)));
1475320918bSDave Airlie  */
1485320918bSDave Airlie static char *udl_set_vid_cmds(char *wrptr, struct drm_display_mode *mode)
1495320918bSDave Airlie {
1505320918bSDave Airlie 	u16 xds, yds;
1515320918bSDave Airlie 	u16 xde, yde;
1525320918bSDave Airlie 	u16 yec;
1535320918bSDave Airlie 
1545320918bSDave Airlie 	/* x display start */
1555320918bSDave Airlie 	xds = mode->crtc_htotal - mode->crtc_hsync_start;
1565320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x01, xds);
1575320918bSDave Airlie 	/* x display end */
1585320918bSDave Airlie 	xde = xds + mode->crtc_hdisplay;
1595320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x03, xde);
1605320918bSDave Airlie 
1615320918bSDave Airlie 	/* y display start */
1625320918bSDave Airlie 	yds = mode->crtc_vtotal - mode->crtc_vsync_start;
1635320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x05, yds);
1645320918bSDave Airlie 	/* y display end */
1655320918bSDave Airlie 	yde = yds + mode->crtc_vdisplay;
1665320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x07, yde);
1675320918bSDave Airlie 
1685320918bSDave Airlie 	/* x end count is active + blanking - 1 */
1695320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x09,
1705320918bSDave Airlie 					mode->crtc_htotal - 1);
1715320918bSDave Airlie 
1725320918bSDave Airlie 	/* libdlo hardcodes hsync start to 1 */
1735320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x0B, 1);
1745320918bSDave Airlie 
1755320918bSDave Airlie 	/* hsync end is width of sync pulse + 1 */
1765320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x0D,
1775320918bSDave Airlie 					mode->crtc_hsync_end - mode->crtc_hsync_start + 1);
1785320918bSDave Airlie 
1795320918bSDave Airlie 	/* hpixels is active pixels */
1805320918bSDave Airlie 	wrptr = udl_set_register_16(wrptr, 0x0F, mode->hdisplay);
1815320918bSDave Airlie 
1825320918bSDave Airlie 	/* yendcount is vertical active + vertical blanking */
1835320918bSDave Airlie 	yec = mode->crtc_vtotal;
1845320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x11, yec);
1855320918bSDave Airlie 
1865320918bSDave Airlie 	/* libdlo hardcodes vsync start to 0 */
1875320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x13, 0);
1885320918bSDave Airlie 
1895320918bSDave Airlie 	/* vsync end is width of vsync pulse */
1905320918bSDave Airlie 	wrptr = udl_set_register_lfsr16(wrptr, 0x15, mode->crtc_vsync_end - mode->crtc_vsync_start);
1915320918bSDave Airlie 
1925320918bSDave Airlie 	/* vpixels is active pixels */
1935320918bSDave Airlie 	wrptr = udl_set_register_16(wrptr, 0x17, mode->crtc_vdisplay);
1945320918bSDave Airlie 
1955320918bSDave Airlie 	wrptr = udl_set_register_16be(wrptr, 0x1B,
1965320918bSDave Airlie 				      mode->clock / 5);
1975320918bSDave Airlie 
1985320918bSDave Airlie 	return wrptr;
1995320918bSDave Airlie }
2005320918bSDave Airlie 
2015bd42f69SDave Airlie static char *udl_dummy_render(char *wrptr)
2025bd42f69SDave Airlie {
2035bd42f69SDave Airlie 	*wrptr++ = 0xAF;
2045bd42f69SDave Airlie 	*wrptr++ = 0x6A; /* copy */
2055bd42f69SDave Airlie 	*wrptr++ = 0x00; /* from addr */
2065bd42f69SDave Airlie 	*wrptr++ = 0x00;
2075bd42f69SDave Airlie 	*wrptr++ = 0x00;
2085bd42f69SDave Airlie 	*wrptr++ = 0x01; /* one pixel */
2095bd42f69SDave Airlie 	*wrptr++ = 0x00; /* to address */
2105bd42f69SDave Airlie 	*wrptr++ = 0x00;
2115bd42f69SDave Airlie 	*wrptr++ = 0x00;
2125bd42f69SDave Airlie 	return wrptr;
2135bd42f69SDave Airlie }
2145bd42f69SDave Airlie 
2155320918bSDave Airlie static int udl_crtc_write_mode_to_hw(struct drm_crtc *crtc)
2165320918bSDave Airlie {
2175320918bSDave Airlie 	struct drm_device *dev = crtc->dev;
218*6ae355a2SDaniel Vetter 	struct udl_device *udl = to_udl(dev);
2195320918bSDave Airlie 	struct urb *urb;
2205320918bSDave Airlie 	char *buf;
2215320918bSDave Airlie 	int retval;
2225320918bSDave Airlie 
223997d33c3SThomas Zimmermann 	if (udl->mode_buf_len == 0) {
224997d33c3SThomas Zimmermann 		DRM_ERROR("No mode set\n");
225997d33c3SThomas Zimmermann 		return -EINVAL;
226997d33c3SThomas Zimmermann 	}
227997d33c3SThomas Zimmermann 
2285320918bSDave Airlie 	urb = udl_get_urb(dev);
2295320918bSDave Airlie 	if (!urb)
2305320918bSDave Airlie 		return -ENOMEM;
2315320918bSDave Airlie 
2325320918bSDave Airlie 	buf = (char *)urb->transfer_buffer;
2335320918bSDave Airlie 
2345320918bSDave Airlie 	memcpy(buf, udl->mode_buf, udl->mode_buf_len);
2355320918bSDave Airlie 	retval = udl_submit_urb(dev, urb, udl->mode_buf_len);
23690991209SMikulas Patocka 	DRM_DEBUG("write mode info %d\n", udl->mode_buf_len);
2375320918bSDave Airlie 	return retval;
2385320918bSDave Airlie }
2395320918bSDave Airlie 
240a8109f5bSThomas Zimmermann static long udl_log_cpp(unsigned int cpp)
241a8109f5bSThomas Zimmermann {
242a8109f5bSThomas Zimmermann 	if (WARN_ON(!is_power_of_2(cpp)))
243a8109f5bSThomas Zimmermann 		return -EINVAL;
244a8109f5bSThomas Zimmermann 	return __ffs(cpp);
245a8109f5bSThomas Zimmermann }
246a8109f5bSThomas Zimmermann 
247a8109f5bSThomas Zimmermann static int udl_aligned_damage_clip(struct drm_rect *clip, int x, int y,
248a8109f5bSThomas Zimmermann 				   int width, int height)
249a8109f5bSThomas Zimmermann {
250a8109f5bSThomas Zimmermann 	int x1, x2;
251a8109f5bSThomas Zimmermann 
252a8109f5bSThomas Zimmermann 	if (WARN_ON_ONCE(x < 0) ||
253a8109f5bSThomas Zimmermann 	    WARN_ON_ONCE(y < 0) ||
254a8109f5bSThomas Zimmermann 	    WARN_ON_ONCE(width < 0) ||
255a8109f5bSThomas Zimmermann 	    WARN_ON_ONCE(height < 0))
256a8109f5bSThomas Zimmermann 		return -EINVAL;
257a8109f5bSThomas Zimmermann 
258a8109f5bSThomas Zimmermann 	x1 = ALIGN_DOWN(x, sizeof(unsigned long));
259a8109f5bSThomas Zimmermann 	x2 = ALIGN(width + (x - x1), sizeof(unsigned long)) + x1;
260a8109f5bSThomas Zimmermann 
261a8109f5bSThomas Zimmermann 	clip->x1 = x1;
262a8109f5bSThomas Zimmermann 	clip->y1 = y;
263a8109f5bSThomas Zimmermann 	clip->x2 = x2;
264a8109f5bSThomas Zimmermann 	clip->y2 = y + height;
265a8109f5bSThomas Zimmermann 
266a8109f5bSThomas Zimmermann 	return 0;
267a8109f5bSThomas Zimmermann }
268a8109f5bSThomas Zimmermann 
269a8109f5bSThomas Zimmermann int udl_handle_damage(struct drm_framebuffer *fb, int x, int y,
270a8109f5bSThomas Zimmermann 		      int width, int height)
271a8109f5bSThomas Zimmermann {
272a8109f5bSThomas Zimmermann 	struct drm_device *dev = fb->dev;
273a8109f5bSThomas Zimmermann 	struct dma_buf_attachment *import_attach = fb->obj[0]->import_attach;
274a8109f5bSThomas Zimmermann 	int i, ret, tmp_ret;
275a8109f5bSThomas Zimmermann 	char *cmd;
276a8109f5bSThomas Zimmermann 	struct urb *urb;
277a8109f5bSThomas Zimmermann 	struct drm_rect clip;
278a8109f5bSThomas Zimmermann 	int log_bpp;
279a8109f5bSThomas Zimmermann 	void *vaddr;
280a8109f5bSThomas Zimmermann 
281a8109f5bSThomas Zimmermann 	ret = udl_log_cpp(fb->format->cpp[0]);
282a8109f5bSThomas Zimmermann 	if (ret < 0)
283a8109f5bSThomas Zimmermann 		return ret;
284a8109f5bSThomas Zimmermann 	log_bpp = ret;
285a8109f5bSThomas Zimmermann 
286a8109f5bSThomas Zimmermann 	ret = udl_aligned_damage_clip(&clip, x, y, width, height);
287a8109f5bSThomas Zimmermann 	if (ret)
288a8109f5bSThomas Zimmermann 		return ret;
289a8109f5bSThomas Zimmermann 	else if ((clip.x2 > fb->width) || (clip.y2 > fb->height))
290a8109f5bSThomas Zimmermann 		return -EINVAL;
291a8109f5bSThomas Zimmermann 
292a8109f5bSThomas Zimmermann 	if (import_attach) {
293a8109f5bSThomas Zimmermann 		ret = dma_buf_begin_cpu_access(import_attach->dmabuf,
294a8109f5bSThomas Zimmermann 					       DMA_FROM_DEVICE);
295a8109f5bSThomas Zimmermann 		if (ret)
296a8109f5bSThomas Zimmermann 			return ret;
297a8109f5bSThomas Zimmermann 	}
298a8109f5bSThomas Zimmermann 
299a8109f5bSThomas Zimmermann 	vaddr = drm_gem_shmem_vmap(fb->obj[0]);
300a8109f5bSThomas Zimmermann 	if (IS_ERR(vaddr)) {
301a8109f5bSThomas Zimmermann 		DRM_ERROR("failed to vmap fb\n");
302a8109f5bSThomas Zimmermann 		goto out_dma_buf_end_cpu_access;
303a8109f5bSThomas Zimmermann 	}
304a8109f5bSThomas Zimmermann 
305a8109f5bSThomas Zimmermann 	urb = udl_get_urb(dev);
306a8109f5bSThomas Zimmermann 	if (!urb)
307a8109f5bSThomas Zimmermann 		goto out_drm_gem_shmem_vunmap;
308a8109f5bSThomas Zimmermann 	cmd = urb->transfer_buffer;
309a8109f5bSThomas Zimmermann 
310a8109f5bSThomas Zimmermann 	for (i = clip.y1; i < clip.y2; i++) {
311a8109f5bSThomas Zimmermann 		const int line_offset = fb->pitches[0] * i;
312a8109f5bSThomas Zimmermann 		const int byte_offset = line_offset + (clip.x1 << log_bpp);
313a8109f5bSThomas Zimmermann 		const int dev_byte_offset = (fb->width * i + clip.x1) << log_bpp;
314a8109f5bSThomas Zimmermann 		const int byte_width = (clip.x2 - clip.x1) << log_bpp;
315a8109f5bSThomas Zimmermann 		ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr,
316a8109f5bSThomas Zimmermann 				       &cmd, byte_offset, dev_byte_offset,
317a8109f5bSThomas Zimmermann 				       byte_width);
318a8109f5bSThomas Zimmermann 		if (ret)
319a8109f5bSThomas Zimmermann 			goto out_drm_gem_shmem_vunmap;
320a8109f5bSThomas Zimmermann 	}
321a8109f5bSThomas Zimmermann 
322a8109f5bSThomas Zimmermann 	if (cmd > (char *)urb->transfer_buffer) {
323a8109f5bSThomas Zimmermann 		/* Send partial buffer remaining before exiting */
324a8109f5bSThomas Zimmermann 		int len;
325a8109f5bSThomas Zimmermann 		if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length)
326a8109f5bSThomas Zimmermann 			*cmd++ = 0xAF;
327a8109f5bSThomas Zimmermann 		len = cmd - (char *)urb->transfer_buffer;
328a8109f5bSThomas Zimmermann 		ret = udl_submit_urb(dev, urb, len);
329a8109f5bSThomas Zimmermann 	} else {
330a8109f5bSThomas Zimmermann 		udl_urb_completion(urb);
331a8109f5bSThomas Zimmermann 	}
332a8109f5bSThomas Zimmermann 
333a8109f5bSThomas Zimmermann 	ret = 0;
334a8109f5bSThomas Zimmermann 
335a8109f5bSThomas Zimmermann out_drm_gem_shmem_vunmap:
336a8109f5bSThomas Zimmermann 	drm_gem_shmem_vunmap(fb->obj[0], vaddr);
337a8109f5bSThomas Zimmermann out_dma_buf_end_cpu_access:
338a8109f5bSThomas Zimmermann 	if (import_attach) {
339a8109f5bSThomas Zimmermann 		tmp_ret = dma_buf_end_cpu_access(import_attach->dmabuf,
340a8109f5bSThomas Zimmermann 						 DMA_FROM_DEVICE);
341a8109f5bSThomas Zimmermann 		if (tmp_ret && !ret)
342a8109f5bSThomas Zimmermann 			ret = tmp_ret; /* only update ret if not set yet */
343a8109f5bSThomas Zimmermann 	}
344a8109f5bSThomas Zimmermann 
345a8109f5bSThomas Zimmermann 	return ret;
346a8109f5bSThomas Zimmermann }
347a8109f5bSThomas Zimmermann 
3489fda81e0SThomas Zimmermann /*
3499fda81e0SThomas Zimmermann  * Simple display pipeline
3509fda81e0SThomas Zimmermann  */
3519fda81e0SThomas Zimmermann 
3529fda81e0SThomas Zimmermann static const uint32_t udl_simple_display_pipe_formats[] = {
3539fda81e0SThomas Zimmermann 	DRM_FORMAT_RGB565,
3549fda81e0SThomas Zimmermann 	DRM_FORMAT_XRGB8888,
3559fda81e0SThomas Zimmermann };
3569fda81e0SThomas Zimmermann 
3579fda81e0SThomas Zimmermann static enum drm_mode_status
3589fda81e0SThomas Zimmermann udl_simple_display_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
3599fda81e0SThomas Zimmermann 				   const struct drm_display_mode *mode)
3605320918bSDave Airlie {
3619fda81e0SThomas Zimmermann 	return MODE_OK;
3625320918bSDave Airlie }
3635320918bSDave Airlie 
3649fda81e0SThomas Zimmermann static void
3659fda81e0SThomas Zimmermann udl_simple_display_pipe_enable(struct drm_simple_display_pipe *pipe,
3669fda81e0SThomas Zimmermann 			       struct drm_crtc_state *crtc_state,
3679fda81e0SThomas Zimmermann 			       struct drm_plane_state *plane_state)
3685320918bSDave Airlie {
3699fda81e0SThomas Zimmermann 	struct drm_crtc *crtc = &pipe->crtc;
3705320918bSDave Airlie 	struct drm_device *dev = crtc->dev;
3719fda81e0SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
372*6ae355a2SDaniel Vetter 	struct udl_device *udl = to_udl(dev);
3739fda81e0SThomas Zimmermann 	struct drm_display_mode *mode = &crtc_state->mode;
3745320918bSDave Airlie 	char *buf;
3755320918bSDave Airlie 	char *wrptr;
3769fda81e0SThomas Zimmermann 	int color_depth = UDL_COLOR_DEPTH_16BPP;
3775320918bSDave Airlie 
3785320918bSDave Airlie 	buf = (char *)udl->mode_buf;
3795320918bSDave Airlie 
3805320918bSDave Airlie 	/* This first section has to do with setting the base address on the
3819fda81e0SThomas Zimmermann 	 * controller associated with the display. There are 2 base
3829fda81e0SThomas Zimmermann 	 * pointers, currently, we only use the 16 bpp segment.
3835320918bSDave Airlie 	 */
3845320918bSDave Airlie 	wrptr = udl_vidreg_lock(buf);
3855320918bSDave Airlie 	wrptr = udl_set_color_depth(wrptr, color_depth);
3865320918bSDave Airlie 	/* set base for 16bpp segment to 0 */
3875320918bSDave Airlie 	wrptr = udl_set_base16bpp(wrptr, 0);
3885320918bSDave Airlie 	/* set base for 8bpp segment to end of fb */
3895320918bSDave Airlie 	wrptr = udl_set_base8bpp(wrptr, 2 * mode->vdisplay * mode->hdisplay);
3905320918bSDave Airlie 
3919fda81e0SThomas Zimmermann 	wrptr = udl_set_vid_cmds(wrptr, mode);
392997d33c3SThomas Zimmermann 	wrptr = udl_set_blank_mode(wrptr, UDL_BLANK_MODE_ON);
3935320918bSDave Airlie 	wrptr = udl_vidreg_unlock(wrptr);
3945320918bSDave Airlie 
3955bd42f69SDave Airlie 	wrptr = udl_dummy_render(wrptr);
3965bd42f69SDave Airlie 
3975320918bSDave Airlie 	udl->mode_buf_len = wrptr - buf;
3985320918bSDave Airlie 
39983446035SThomas Zimmermann 	udl_handle_damage(fb, 0, 0, fb->width, fb->height);
4009fda81e0SThomas Zimmermann 
401997d33c3SThomas Zimmermann 	if (!crtc_state->mode_changed)
402997d33c3SThomas Zimmermann 		return;
403997d33c3SThomas Zimmermann 
404997d33c3SThomas Zimmermann 	/* enable display */
405997d33c3SThomas Zimmermann 	udl_crtc_write_mode_to_hw(crtc);
4069fda81e0SThomas Zimmermann }
4079fda81e0SThomas Zimmermann 
4089fda81e0SThomas Zimmermann static void
4099fda81e0SThomas Zimmermann udl_simple_display_pipe_disable(struct drm_simple_display_pipe *pipe)
4109fda81e0SThomas Zimmermann {
411997d33c3SThomas Zimmermann 	struct drm_crtc *crtc = &pipe->crtc;
412997d33c3SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
413997d33c3SThomas Zimmermann 	struct urb *urb;
414997d33c3SThomas Zimmermann 	char *buf;
415997d33c3SThomas Zimmermann 
416997d33c3SThomas Zimmermann 	urb = udl_get_urb(dev);
417997d33c3SThomas Zimmermann 	if (!urb)
418997d33c3SThomas Zimmermann 		return;
419997d33c3SThomas Zimmermann 
420997d33c3SThomas Zimmermann 	buf = (char *)urb->transfer_buffer;
421997d33c3SThomas Zimmermann 	buf = udl_vidreg_lock(buf);
422997d33c3SThomas Zimmermann 	buf = udl_set_blank_mode(buf, UDL_BLANK_MODE_POWERDOWN);
423997d33c3SThomas Zimmermann 	buf = udl_vidreg_unlock(buf);
424997d33c3SThomas Zimmermann 	buf = udl_dummy_render(buf);
425997d33c3SThomas Zimmermann 
426997d33c3SThomas Zimmermann 	udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
4279fda81e0SThomas Zimmermann }
4289fda81e0SThomas Zimmermann 
4299fda81e0SThomas Zimmermann static void
4309fda81e0SThomas Zimmermann udl_simple_display_pipe_update(struct drm_simple_display_pipe *pipe,
4319fda81e0SThomas Zimmermann 			       struct drm_plane_state *old_plane_state)
4325320918bSDave Airlie {
433230b8b04SThomas Zimmermann 	struct drm_plane_state *state = pipe->plane.state;
434230b8b04SThomas Zimmermann 	struct drm_framebuffer *fb = state->fb;
435230b8b04SThomas Zimmermann 	struct drm_rect rect;
43640377ef2SStéphane Marchesin 
4379fda81e0SThomas Zimmermann 	if (!fb)
4389fda81e0SThomas Zimmermann 		return;
4399fda81e0SThomas Zimmermann 
440230b8b04SThomas Zimmermann 	if (drm_atomic_helper_damage_merged(old_plane_state, state, &rect))
441230b8b04SThomas Zimmermann 		udl_handle_damage(fb, rect.x1, rect.y1, rect.x2 - rect.x1,
442230b8b04SThomas Zimmermann 				  rect.y2 - rect.y1);
44340377ef2SStéphane Marchesin }
44440377ef2SStéphane Marchesin 
4459fda81e0SThomas Zimmermann static const
4469fda81e0SThomas Zimmermann struct drm_simple_display_pipe_funcs udl_simple_display_pipe_funcs = {
4479fda81e0SThomas Zimmermann 	.mode_valid = udl_simple_display_pipe_mode_valid,
4489fda81e0SThomas Zimmermann 	.enable = udl_simple_display_pipe_enable,
4499fda81e0SThomas Zimmermann 	.disable = udl_simple_display_pipe_disable,
4509fda81e0SThomas Zimmermann 	.update = udl_simple_display_pipe_update,
4519fda81e0SThomas Zimmermann 	.prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
4525320918bSDave Airlie };
4535320918bSDave Airlie 
4549fda81e0SThomas Zimmermann /*
4559fda81e0SThomas Zimmermann  * Modesetting
4569fda81e0SThomas Zimmermann  */
4575320918bSDave Airlie 
4585320918bSDave Airlie static const struct drm_mode_config_funcs udl_mode_funcs = {
459230b8b04SThomas Zimmermann 	.fb_create = drm_gem_fb_create_with_dirty,
4609fda81e0SThomas Zimmermann 	.atomic_check  = drm_atomic_helper_check,
4619fda81e0SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
4625320918bSDave Airlie };
4635320918bSDave Airlie 
4645320918bSDave Airlie int udl_modeset_init(struct drm_device *dev)
4655320918bSDave Airlie {
4669fda81e0SThomas Zimmermann 	size_t format_count = ARRAY_SIZE(udl_simple_display_pipe_formats);
467*6ae355a2SDaniel Vetter 	struct udl_device *udl = to_udl(dev);
468e829cf0bSThomas Zimmermann 	struct drm_connector *connector;
469e829cf0bSThomas Zimmermann 	int ret;
470e829cf0bSThomas Zimmermann 
471fe5b7c86SDaniel Vetter 	ret = drmm_mode_config_init(dev);
472fe5b7c86SDaniel Vetter 	if (ret)
473fe5b7c86SDaniel Vetter 		return ret;
4745320918bSDave Airlie 
4755320918bSDave Airlie 	dev->mode_config.min_width = 640;
4765320918bSDave Airlie 	dev->mode_config.min_height = 480;
4775320918bSDave Airlie 
4785320918bSDave Airlie 	dev->mode_config.max_width = 2048;
4795320918bSDave Airlie 	dev->mode_config.max_height = 2048;
4805320918bSDave Airlie 
4815320918bSDave Airlie 	dev->mode_config.prefer_shadow = 0;
482d8177841SThomas Zimmermann 	dev->mode_config.preferred_depth = 16;
4835320918bSDave Airlie 
484e6ecefaaSLaurent Pinchart 	dev->mode_config.funcs = &udl_mode_funcs;
4855320918bSDave Airlie 
486e829cf0bSThomas Zimmermann 	connector = udl_connector_init(dev);
487fe5b7c86SDaniel Vetter 	if (IS_ERR(connector))
488fe5b7c86SDaniel Vetter 		return PTR_ERR(connector);
489e829cf0bSThomas Zimmermann 
4909fda81e0SThomas Zimmermann 	format_count = ARRAY_SIZE(udl_simple_display_pipe_formats);
4915320918bSDave Airlie 
4929fda81e0SThomas Zimmermann 	ret = drm_simple_display_pipe_init(dev, &udl->display_pipe,
4939fda81e0SThomas Zimmermann 					   &udl_simple_display_pipe_funcs,
4949fda81e0SThomas Zimmermann 					   udl_simple_display_pipe_formats,
4959fda81e0SThomas Zimmermann 					   format_count, NULL, connector);
4969fda81e0SThomas Zimmermann 	if (ret)
497fe5b7c86SDaniel Vetter 		return ret;
4989fda81e0SThomas Zimmermann 
4999fda81e0SThomas Zimmermann 	drm_mode_config_reset(dev);
5005320918bSDave Airlie 
5015320918bSDave Airlie 	return 0;
5025320918bSDave Airlie }
503