xref: /linux/drivers/gpu/drm/udl/udl_modeset.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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 
1144f29ad9SThomas Zimmermann #include <linux/bitfield.h>
1244f29ad9SThomas Zimmermann 
1372d73dd3SThomas Zimmermann #include <drm/drm_atomic.h>
149fda81e0SThomas Zimmermann #include <drm/drm_atomic_helper.h>
1548c5c68fSGeert Uytterhoeven #include <drm/drm_crtc_helper.h>
16230b8b04SThomas Zimmermann #include <drm/drm_damage_helper.h>
17ca2bd373SThomas Zimmermann #include <drm/drm_drv.h>
180862cfd3SThomas Zimmermann #include <drm/drm_edid.h>
19a8109f5bSThomas Zimmermann #include <drm/drm_fourcc.h>
205ceeb328SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
219fda81e0SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
22a8109f5bSThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
23a9dcf380SSam Ravnborg #include <drm/drm_modeset_helper_vtables.h>
240862cfd3SThomas Zimmermann #include <drm/drm_probe_helper.h>
25a9dcf380SSam Ravnborg #include <drm/drm_vblank.h>
26a9dcf380SSam Ravnborg 
275320918bSDave Airlie #include "udl_drv.h"
2890e0fd1eSThomas Zimmermann #include "udl_edid.h"
29ff76e82cSThomas Zimmermann #include "udl_proto.h"
309fda81e0SThomas Zimmermann 
315320918bSDave Airlie /*
321b8db07fSThomas Zimmermann  * All DisplayLink bulk operations start with 0xaf (UDL_MSG_BULK), followed by
331b8db07fSThomas Zimmermann  * a specific command code. All operations are written to a command buffer, which
341b8db07fSThomas Zimmermann  * the driver sends to the device.
355320918bSDave Airlie  */
udl_set_register(char * buf,u8 reg,u8 val)365320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val)
375320918bSDave Airlie {
381b8db07fSThomas Zimmermann 	*buf++ = UDL_MSG_BULK;
391b8db07fSThomas Zimmermann 	*buf++ = UDL_CMD_WRITEREG;
405320918bSDave Airlie 	*buf++ = reg;
415320918bSDave Airlie 	*buf++ = val;
421b8db07fSThomas Zimmermann 
435320918bSDave Airlie 	return buf;
445320918bSDave Airlie }
455320918bSDave Airlie 
udl_vidreg_lock(char * buf)465320918bSDave Airlie static char *udl_vidreg_lock(char *buf)
475320918bSDave Airlie {
48cb7b995dSThomas Zimmermann 	return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_LOCK);
495320918bSDave Airlie }
505320918bSDave Airlie 
udl_vidreg_unlock(char * buf)515320918bSDave Airlie static char *udl_vidreg_unlock(char *buf)
525320918bSDave Airlie {
53cb7b995dSThomas Zimmermann 	return udl_set_register(buf, UDL_REG_VIDREG, UDL_VIDREG_UNLOCK);
545320918bSDave Airlie }
555320918bSDave Airlie 
udl_set_blank_mode(char * buf,u8 mode)56997d33c3SThomas Zimmermann static char *udl_set_blank_mode(char *buf, u8 mode)
575320918bSDave Airlie {
58ff76e82cSThomas Zimmermann 	return udl_set_register(buf, UDL_REG_BLANKMODE, mode);
595320918bSDave Airlie }
605320918bSDave Airlie 
udl_set_color_depth(char * buf,u8 selection)615320918bSDave Airlie static char *udl_set_color_depth(char *buf, u8 selection)
625320918bSDave Airlie {
63ed24ed48SThomas Zimmermann 	return udl_set_register(buf, UDL_REG_COLORDEPTH, selection);
645320918bSDave Airlie }
655320918bSDave Airlie 
udl_set_base16bpp(char * buf,u32 base)6644f29ad9SThomas Zimmermann static char *udl_set_base16bpp(char *buf, u32 base)
675320918bSDave Airlie {
6844f29ad9SThomas Zimmermann 	/* the base pointer is 24 bits wide, 0x20 is hi byte. */
6944f29ad9SThomas Zimmermann 	u8 reg20 = FIELD_GET(UDL_BASE_ADDR2_MASK, base);
7044f29ad9SThomas Zimmermann 	u8 reg21 = FIELD_GET(UDL_BASE_ADDR1_MASK, base);
7144f29ad9SThomas Zimmermann 	u8 reg22 = FIELD_GET(UDL_BASE_ADDR0_MASK, base);
7244f29ad9SThomas Zimmermann 
7344f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR2, reg20);
7444f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR1, reg21);
7544f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE16BPP_ADDR0, reg22);
7644f29ad9SThomas Zimmermann 
7744f29ad9SThomas Zimmermann 	return buf;
785320918bSDave Airlie }
795320918bSDave Airlie 
805320918bSDave Airlie /*
815320918bSDave Airlie  * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
825320918bSDave Airlie  * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
835320918bSDave Airlie  */
udl_set_base8bpp(char * buf,u32 base)8444f29ad9SThomas Zimmermann static char *udl_set_base8bpp(char *buf, u32 base)
855320918bSDave Airlie {
8644f29ad9SThomas Zimmermann 	/* the base pointer is 24 bits wide, 0x26 is hi byte. */
8744f29ad9SThomas Zimmermann 	u8 reg26 = FIELD_GET(UDL_BASE_ADDR2_MASK, base);
8844f29ad9SThomas Zimmermann 	u8 reg27 = FIELD_GET(UDL_BASE_ADDR1_MASK, base);
8944f29ad9SThomas Zimmermann 	u8 reg28 = FIELD_GET(UDL_BASE_ADDR0_MASK, base);
9044f29ad9SThomas Zimmermann 
9144f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR2, reg26);
9244f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR1, reg27);
9344f29ad9SThomas Zimmermann 	buf = udl_set_register(buf, UDL_REG_BASE8BPP_ADDR0, reg28);
9444f29ad9SThomas Zimmermann 
9544f29ad9SThomas Zimmermann 	return buf;
965320918bSDave Airlie }
975320918bSDave Airlie 
udl_set_register_16(char * wrptr,u8 reg,u16 value)985320918bSDave Airlie static char *udl_set_register_16(char *wrptr, u8 reg, u16 value)
995320918bSDave Airlie {
1005320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value >> 8);
1015320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value);
1025320918bSDave Airlie }
1035320918bSDave Airlie 
1045320918bSDave Airlie /*
1055320918bSDave Airlie  * This is kind of weird because the controller takes some
1065320918bSDave Airlie  * register values in a different byte order than other registers.
1075320918bSDave Airlie  */
udl_set_register_16be(char * wrptr,u8 reg,u16 value)1085320918bSDave Airlie static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value)
1095320918bSDave Airlie {
1105320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value);
1115320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value >> 8);
1125320918bSDave Airlie }
1135320918bSDave Airlie 
1145320918bSDave Airlie /*
1155320918bSDave Airlie  * LFSR is linear feedback shift register. The reason we have this is
1165320918bSDave Airlie  * because the display controller needs to minimize the clock depth of
1175320918bSDave Airlie  * various counters used in the display path. So this code reverses the
1185320918bSDave Airlie  * provided value into the lfsr16 value by counting backwards to get
1195320918bSDave Airlie  * the value that needs to be set in the hardware comparator to get the
1205320918bSDave Airlie  * same actual count. This makes sense once you read above a couple of
1215320918bSDave Airlie  * times and think about it from a hardware perspective.
1225320918bSDave Airlie  */
udl_lfsr16(u16 actual_count)1235320918bSDave Airlie static u16 udl_lfsr16(u16 actual_count)
1245320918bSDave Airlie {
1255320918bSDave Airlie 	u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
1265320918bSDave Airlie 
1275320918bSDave Airlie 	while (actual_count--) {
1285320918bSDave Airlie 		lv =	 ((lv << 1) |
1295320918bSDave Airlie 			(((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
1305320918bSDave Airlie 			& 0xFFFF;
1315320918bSDave Airlie 	}
1325320918bSDave Airlie 
1335320918bSDave Airlie 	return (u16) lv;
1345320918bSDave Airlie }
1355320918bSDave Airlie 
1365320918bSDave Airlie /*
1375320918bSDave Airlie  * This does LFSR conversion on the value that is to be written.
1385320918bSDave Airlie  * See LFSR explanation above for more detail.
1395320918bSDave Airlie  */
udl_set_register_lfsr16(char * wrptr,u8 reg,u16 value)1405320918bSDave Airlie static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
1415320918bSDave Airlie {
1425320918bSDave Airlie 	return udl_set_register_16(wrptr, reg, udl_lfsr16(value));
1435320918bSDave Airlie }
1445320918bSDave Airlie 
1455320918bSDave Airlie /*
1469869e40dSThomas Zimmermann  * Takes a DRM display mode and converts it into the DisplayLink
1479869e40dSThomas Zimmermann  * equivalent register commands.
1485320918bSDave Airlie  */
udl_set_display_mode(char * buf,struct drm_display_mode * mode)1499869e40dSThomas Zimmermann static char *udl_set_display_mode(char *buf, struct drm_display_mode *mode)
1505320918bSDave Airlie {
1519869e40dSThomas Zimmermann 	u16 reg01 = mode->crtc_htotal - mode->crtc_hsync_start;
1529869e40dSThomas Zimmermann 	u16 reg03 = reg01 + mode->crtc_hdisplay;
1539869e40dSThomas Zimmermann 	u16 reg05 = mode->crtc_vtotal - mode->crtc_vsync_start;
1549869e40dSThomas Zimmermann 	u16 reg07 = reg05 + mode->crtc_vdisplay;
1559869e40dSThomas Zimmermann 	u16 reg09 = mode->crtc_htotal - 1;
1569869e40dSThomas Zimmermann 	u16 reg0b = 1; /* libdlo hardcodes hsync start to 1 */
1579869e40dSThomas Zimmermann 	u16 reg0d = mode->crtc_hsync_end - mode->crtc_hsync_start + 1;
1589869e40dSThomas Zimmermann 	u16 reg0f = mode->hdisplay;
1599869e40dSThomas Zimmermann 	u16 reg11 = mode->crtc_vtotal;
1609869e40dSThomas Zimmermann 	u16 reg13 = 0; /* libdlo hardcodes vsync start to 0 */
1619869e40dSThomas Zimmermann 	u16 reg15 = mode->crtc_vsync_end - mode->crtc_vsync_start;
1629869e40dSThomas Zimmermann 	u16 reg17 = mode->crtc_vdisplay;
1639869e40dSThomas Zimmermann 	u16 reg1b = mode->clock / 5;
1645320918bSDave Airlie 
1659869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYSTART, reg01);
1669869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYEND, reg03);
1679869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYSTART, reg05);
1689869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYEND, reg07);
1699869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XENDCOUNT, reg09);
1709869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCSTART, reg0b);
1719869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCEND, reg0d);
1729869e40dSThomas Zimmermann 	buf = udl_set_register_16(buf, UDL_REG_HPIXELS, reg0f);
1739869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YENDCOUNT, reg11);
1749869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCSTART, reg13);
1759869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCEND, reg15);
1769869e40dSThomas Zimmermann 	buf = udl_set_register_16(buf, UDL_REG_VPIXELS, reg17);
1779869e40dSThomas Zimmermann 	buf = udl_set_register_16be(buf, UDL_REG_PIXELCLOCK5KHZ, reg1b);
1785320918bSDave Airlie 
1799869e40dSThomas Zimmermann 	return buf;
1805320918bSDave Airlie }
1815320918bSDave Airlie 
udl_dummy_render(char * wrptr)1825bd42f69SDave Airlie static char *udl_dummy_render(char *wrptr)
1835bd42f69SDave Airlie {
1841b8db07fSThomas Zimmermann 	*wrptr++ = UDL_MSG_BULK;
1851b8db07fSThomas Zimmermann 	*wrptr++ = UDL_CMD_WRITECOPY16;
1865bd42f69SDave Airlie 	*wrptr++ = 0x00; /* from addr */
1875bd42f69SDave Airlie 	*wrptr++ = 0x00;
1885bd42f69SDave Airlie 	*wrptr++ = 0x00;
1895bd42f69SDave Airlie 	*wrptr++ = 0x01; /* one pixel */
1905bd42f69SDave Airlie 	*wrptr++ = 0x00; /* to address */
1915bd42f69SDave Airlie 	*wrptr++ = 0x00;
1925bd42f69SDave Airlie 	*wrptr++ = 0x00;
1935bd42f69SDave Airlie 	return wrptr;
1945bd42f69SDave Airlie }
1955bd42f69SDave Airlie 
udl_log_cpp(unsigned int cpp)196a8109f5bSThomas Zimmermann static long udl_log_cpp(unsigned int cpp)
197a8109f5bSThomas Zimmermann {
198a8109f5bSThomas Zimmermann 	if (WARN_ON(!is_power_of_2(cpp)))
199a8109f5bSThomas Zimmermann 		return -EINVAL;
200a8109f5bSThomas Zimmermann 	return __ffs(cpp);
201a8109f5bSThomas Zimmermann }
202a8109f5bSThomas Zimmermann 
udl_handle_damage(struct drm_framebuffer * fb,const struct iosys_map * map,const struct drm_rect * clip)2037938f421SLucas De Marchi static int udl_handle_damage(struct drm_framebuffer *fb,
2047938f421SLucas De Marchi 			     const struct iosys_map *map,
205b13fa27aSTakashi Iwai 			     const struct drm_rect *clip)
206a8109f5bSThomas Zimmermann {
207a8109f5bSThomas Zimmermann 	struct drm_device *dev = fb->dev;
2085ceeb328SThomas Zimmermann 	void *vaddr = map->vaddr; /* TODO: Use mapping abstraction properly */
209ce724470SThomas Zimmermann 	int i, ret;
210a8109f5bSThomas Zimmermann 	char *cmd;
211a8109f5bSThomas Zimmermann 	struct urb *urb;
212a8109f5bSThomas Zimmermann 	int log_bpp;
213a8109f5bSThomas Zimmermann 
214a8109f5bSThomas Zimmermann 	ret = udl_log_cpp(fb->format->cpp[0]);
215a8109f5bSThomas Zimmermann 	if (ret < 0)
216a8109f5bSThomas Zimmermann 		return ret;
217a8109f5bSThomas Zimmermann 	log_bpp = ret;
218a8109f5bSThomas Zimmermann 
219a8109f5bSThomas Zimmermann 	urb = udl_get_urb(dev);
220fcc21447SThomas Zimmermann 	if (!urb)
221fcc21447SThomas Zimmermann 		return -ENOMEM;
222a8109f5bSThomas Zimmermann 	cmd = urb->transfer_buffer;
223a8109f5bSThomas Zimmermann 
224b13fa27aSTakashi Iwai 	for (i = clip->y1; i < clip->y2; i++) {
225a8109f5bSThomas Zimmermann 		const int line_offset = fb->pitches[0] * i;
226b13fa27aSTakashi Iwai 		const int byte_offset = line_offset + (clip->x1 << log_bpp);
227b13fa27aSTakashi Iwai 		const int dev_byte_offset = (fb->width * i + clip->x1) << log_bpp;
228b13fa27aSTakashi Iwai 		const int byte_width = drm_rect_width(clip) << log_bpp;
229a8109f5bSThomas Zimmermann 		ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr,
230a8109f5bSThomas Zimmermann 				       &cmd, byte_offset, dev_byte_offset,
231a8109f5bSThomas Zimmermann 				       byte_width);
232a8109f5bSThomas Zimmermann 		if (ret)
233fcc21447SThomas Zimmermann 			return ret;
234a8109f5bSThomas Zimmermann 	}
235a8109f5bSThomas Zimmermann 
236a8109f5bSThomas Zimmermann 	if (cmd > (char *)urb->transfer_buffer) {
237a8109f5bSThomas Zimmermann 		/* Send partial buffer remaining before exiting */
238a8109f5bSThomas Zimmermann 		int len;
239a8109f5bSThomas Zimmermann 		if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length)
2401b8db07fSThomas Zimmermann 			*cmd++ = UDL_MSG_BULK;
241a8109f5bSThomas Zimmermann 		len = cmd - (char *)urb->transfer_buffer;
242a8109f5bSThomas Zimmermann 		ret = udl_submit_urb(dev, urb, len);
243a8109f5bSThomas Zimmermann 	} else {
244a8109f5bSThomas Zimmermann 		udl_urb_completion(urb);
245a8109f5bSThomas Zimmermann 	}
246a8109f5bSThomas Zimmermann 
247fcc21447SThomas Zimmermann 	return 0;
248a8109f5bSThomas Zimmermann }
249a8109f5bSThomas Zimmermann 
2509fda81e0SThomas Zimmermann /*
25172d73dd3SThomas Zimmermann  * Primary plane
2529fda81e0SThomas Zimmermann  */
2539fda81e0SThomas Zimmermann 
25472d73dd3SThomas Zimmermann static const uint32_t udl_primary_plane_formats[] = {
2559fda81e0SThomas Zimmermann 	DRM_FORMAT_RGB565,
2569fda81e0SThomas Zimmermann 	DRM_FORMAT_XRGB8888,
2579fda81e0SThomas Zimmermann };
2589fda81e0SThomas Zimmermann 
25972d73dd3SThomas Zimmermann static const uint64_t udl_primary_plane_fmtmods[] = {
26072d73dd3SThomas Zimmermann 	DRM_FORMAT_MOD_LINEAR,
26172d73dd3SThomas Zimmermann 	DRM_FORMAT_MOD_INVALID
26272d73dd3SThomas Zimmermann };
26372d73dd3SThomas Zimmermann 
udl_primary_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)264a0fce84cSThomas Zimmermann static int udl_primary_plane_helper_atomic_check(struct drm_plane *plane,
265a0fce84cSThomas Zimmermann 						 struct drm_atomic_state *state)
266a0fce84cSThomas Zimmermann {
267a0fce84cSThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
268a0fce84cSThomas Zimmermann 	struct drm_crtc *new_crtc = new_plane_state->crtc;
269a0fce84cSThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
270a0fce84cSThomas Zimmermann 
271a0fce84cSThomas Zimmermann 	if (new_crtc)
272a0fce84cSThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
273a0fce84cSThomas Zimmermann 
274a0fce84cSThomas Zimmermann 	return drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
275a0fce84cSThomas Zimmermann 						   DRM_PLANE_NO_SCALING,
276a0fce84cSThomas Zimmermann 						   DRM_PLANE_NO_SCALING,
277a0fce84cSThomas Zimmermann 						   false, false);
278a0fce84cSThomas Zimmermann }
279a0fce84cSThomas Zimmermann 
udl_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)28072d73dd3SThomas Zimmermann static void udl_primary_plane_helper_atomic_update(struct drm_plane *plane,
28172d73dd3SThomas Zimmermann 						   struct drm_atomic_state *state)
2825320918bSDave Airlie {
283ca2bd373SThomas Zimmermann 	struct drm_device *dev = plane->dev;
28472d73dd3SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
2855ceeb328SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
28672d73dd3SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
28772d73dd3SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
288fcc21447SThomas Zimmermann 	struct drm_atomic_helper_damage_iter iter;
289fcc21447SThomas Zimmermann 	struct drm_rect damage;
290fcc21447SThomas Zimmermann 	int ret, idx;
29172d73dd3SThomas Zimmermann 
29272d73dd3SThomas Zimmermann 	if (!fb)
29372d73dd3SThomas Zimmermann 		return; /* no framebuffer; plane is disabled */
29472d73dd3SThomas Zimmermann 
295fcc21447SThomas Zimmermann 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
296fcc21447SThomas Zimmermann 	if (ret)
297fcc21447SThomas Zimmermann 		return;
298fcc21447SThomas Zimmermann 
299fcc21447SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
300fcc21447SThomas Zimmermann 		goto out_drm_gem_fb_end_cpu_access;
301fcc21447SThomas Zimmermann 
302fcc21447SThomas Zimmermann 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
303fcc21447SThomas Zimmermann 	drm_atomic_for_each_plane_damage(&iter, &damage) {
304fcc21447SThomas Zimmermann 		udl_handle_damage(fb, &shadow_plane_state->data[0], &damage);
305fcc21447SThomas Zimmermann 	}
306ca2bd373SThomas Zimmermann 
307ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
308fcc21447SThomas Zimmermann 
309fcc21447SThomas Zimmermann out_drm_gem_fb_end_cpu_access:
310fcc21447SThomas Zimmermann 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
31172d73dd3SThomas Zimmermann }
31272d73dd3SThomas Zimmermann 
31372d73dd3SThomas Zimmermann static const struct drm_plane_helper_funcs udl_primary_plane_helper_funcs = {
31472d73dd3SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
315a0fce84cSThomas Zimmermann 	.atomic_check = udl_primary_plane_helper_atomic_check,
31672d73dd3SThomas Zimmermann 	.atomic_update = udl_primary_plane_helper_atomic_update,
31772d73dd3SThomas Zimmermann };
31872d73dd3SThomas Zimmermann 
31972d73dd3SThomas Zimmermann static const struct drm_plane_funcs udl_primary_plane_funcs = {
32072d73dd3SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
32172d73dd3SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
32272d73dd3SThomas Zimmermann 	.destroy = drm_plane_cleanup,
32372d73dd3SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
32472d73dd3SThomas Zimmermann };
32572d73dd3SThomas Zimmermann 
32672d73dd3SThomas Zimmermann /*
32772d73dd3SThomas Zimmermann  * CRTC
32872d73dd3SThomas Zimmermann  */
32972d73dd3SThomas Zimmermann 
udl_crtc_helper_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)33072d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
33172d73dd3SThomas Zimmermann {
33272d73dd3SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
33372d73dd3SThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
33472d73dd3SThomas Zimmermann 	struct drm_display_mode *mode = &crtc_state->mode;
335890e4de8SThomas Zimmermann 	struct urb *urb;
3365320918bSDave Airlie 	char *buf;
337ca2bd373SThomas Zimmermann 	int idx;
338ca2bd373SThomas Zimmermann 
339ca2bd373SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
340ca2bd373SThomas Zimmermann 		return;
3415320918bSDave Airlie 
342890e4de8SThomas Zimmermann 	urb = udl_get_urb(dev);
343890e4de8SThomas Zimmermann 	if (!urb)
344ca2bd373SThomas Zimmermann 		goto out;
3455320918bSDave Airlie 
346890e4de8SThomas Zimmermann 	buf = (char *)urb->transfer_buffer;
347890e4de8SThomas Zimmermann 	buf = udl_vidreg_lock(buf);
348ff76e82cSThomas Zimmermann 	buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP);
3495320918bSDave Airlie 	/* set base for 16bpp segment to 0 */
350890e4de8SThomas Zimmermann 	buf = udl_set_base16bpp(buf, 0);
3515320918bSDave Airlie 	/* set base for 8bpp segment to end of fb */
352890e4de8SThomas Zimmermann 	buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay);
3539869e40dSThomas Zimmermann 	buf = udl_set_display_mode(buf, mode);
354ff76e82cSThomas Zimmermann 	buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON);
355890e4de8SThomas Zimmermann 	buf = udl_vidreg_unlock(buf);
356890e4de8SThomas Zimmermann 	buf = udl_dummy_render(buf);
3575bd42f69SDave Airlie 
358890e4de8SThomas Zimmermann 	udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
359ca2bd373SThomas Zimmermann 
360ca2bd373SThomas Zimmermann out:
361ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
3629fda81e0SThomas Zimmermann }
3639fda81e0SThomas Zimmermann 
udl_crtc_helper_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)36472d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
3659fda81e0SThomas Zimmermann {
366997d33c3SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
367997d33c3SThomas Zimmermann 	struct urb *urb;
368997d33c3SThomas Zimmermann 	char *buf;
369ca2bd373SThomas Zimmermann 	int idx;
370ca2bd373SThomas Zimmermann 
371ca2bd373SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
372ca2bd373SThomas Zimmermann 		return;
373997d33c3SThomas Zimmermann 
374997d33c3SThomas Zimmermann 	urb = udl_get_urb(dev);
375997d33c3SThomas Zimmermann 	if (!urb)
376ca2bd373SThomas Zimmermann 		goto out;
377997d33c3SThomas Zimmermann 
378997d33c3SThomas Zimmermann 	buf = (char *)urb->transfer_buffer;
379997d33c3SThomas Zimmermann 	buf = udl_vidreg_lock(buf);
380ff76e82cSThomas Zimmermann 	buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN);
381997d33c3SThomas Zimmermann 	buf = udl_vidreg_unlock(buf);
382997d33c3SThomas Zimmermann 	buf = udl_dummy_render(buf);
383997d33c3SThomas Zimmermann 
384997d33c3SThomas Zimmermann 	udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
385ca2bd373SThomas Zimmermann 
386ca2bd373SThomas Zimmermann out:
387ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
3889fda81e0SThomas Zimmermann }
3899fda81e0SThomas Zimmermann 
39072d73dd3SThomas Zimmermann static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = {
39148c5c68fSGeert Uytterhoeven 	.atomic_check = drm_crtc_helper_atomic_check,
39272d73dd3SThomas Zimmermann 	.atomic_enable = udl_crtc_helper_atomic_enable,
39372d73dd3SThomas Zimmermann 	.atomic_disable = udl_crtc_helper_atomic_disable,
39472d73dd3SThomas Zimmermann };
39540377ef2SStéphane Marchesin 
39672d73dd3SThomas Zimmermann static const struct drm_crtc_funcs udl_crtc_funcs = {
39772d73dd3SThomas Zimmermann 	.reset = drm_atomic_helper_crtc_reset,
39872d73dd3SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
39972d73dd3SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
40072d73dd3SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
40172d73dd3SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
40272d73dd3SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
40372d73dd3SThomas Zimmermann };
4049fda81e0SThomas Zimmermann 
40572d73dd3SThomas Zimmermann /*
40672d73dd3SThomas Zimmermann  * Encoder
40772d73dd3SThomas Zimmermann  */
40840377ef2SStéphane Marchesin 
40972d73dd3SThomas Zimmermann static const struct drm_encoder_funcs udl_encoder_funcs = {
41072d73dd3SThomas Zimmermann 	.destroy = drm_encoder_cleanup,
4115320918bSDave Airlie };
4125320918bSDave Airlie 
4139fda81e0SThomas Zimmermann /*
4140862cfd3SThomas Zimmermann  * Connector
4150862cfd3SThomas Zimmermann  */
4160862cfd3SThomas Zimmermann 
udl_connector_helper_get_modes(struct drm_connector * connector)4170862cfd3SThomas Zimmermann static int udl_connector_helper_get_modes(struct drm_connector *connector)
4180862cfd3SThomas Zimmermann {
41990e0fd1eSThomas Zimmermann 	const struct drm_edid *drm_edid;
42090e0fd1eSThomas Zimmermann 	int count;
4210862cfd3SThomas Zimmermann 
42290e0fd1eSThomas Zimmermann 	drm_edid = udl_edid_read(connector);
42390e0fd1eSThomas Zimmermann 	drm_edid_connector_update(connector, drm_edid);
42490e0fd1eSThomas Zimmermann 	count = drm_edid_connector_add_modes(connector);
42590e0fd1eSThomas Zimmermann 	drm_edid_free(drm_edid);
4260862cfd3SThomas Zimmermann 
42790e0fd1eSThomas Zimmermann 	return count;
42890e0fd1eSThomas Zimmermann }
42990e0fd1eSThomas Zimmermann 
udl_connector_helper_detect_ctx(struct drm_connector * connector,struct drm_modeset_acquire_ctx * ctx,bool force)43090e0fd1eSThomas Zimmermann static int udl_connector_helper_detect_ctx(struct drm_connector *connector,
43190e0fd1eSThomas Zimmermann 					   struct drm_modeset_acquire_ctx *ctx,
43290e0fd1eSThomas Zimmermann 					   bool force)
43390e0fd1eSThomas Zimmermann {
43490e0fd1eSThomas Zimmermann 	struct udl_device *udl = to_udl(connector->dev);
43590e0fd1eSThomas Zimmermann 
43690e0fd1eSThomas Zimmermann 	if (udl_probe_edid(udl))
43790e0fd1eSThomas Zimmermann 		return connector_status_connected;
43890e0fd1eSThomas Zimmermann 
43990e0fd1eSThomas Zimmermann 	return connector_status_disconnected;
4400862cfd3SThomas Zimmermann }
4410862cfd3SThomas Zimmermann 
4420862cfd3SThomas Zimmermann static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
4430862cfd3SThomas Zimmermann 	.get_modes = udl_connector_helper_get_modes,
44490e0fd1eSThomas Zimmermann 	.detect_ctx = udl_connector_helper_detect_ctx,
4450862cfd3SThomas Zimmermann };
4460862cfd3SThomas Zimmermann 
4470862cfd3SThomas Zimmermann static const struct drm_connector_funcs udl_connector_funcs = {
4480862cfd3SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
4490862cfd3SThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
450*2262e917SThomas Zimmermann 	.destroy = drm_connector_cleanup,
4510862cfd3SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
4520862cfd3SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4530862cfd3SThomas Zimmermann };
4540862cfd3SThomas Zimmermann 
4550862cfd3SThomas Zimmermann /*
4569fda81e0SThomas Zimmermann  * Modesetting
4579fda81e0SThomas Zimmermann  */
4585320918bSDave Airlie 
udl_mode_config_mode_valid(struct drm_device * dev,const struct drm_display_mode * mode)459c020f660SThomas Zimmermann static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev,
460c020f660SThomas Zimmermann 						       const struct drm_display_mode *mode)
461c020f660SThomas Zimmermann {
462c020f660SThomas Zimmermann 	struct udl_device *udl = to_udl(dev);
463c020f660SThomas Zimmermann 
464c020f660SThomas Zimmermann 	if (udl->sku_pixel_limit) {
465c020f660SThomas Zimmermann 		if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
466c020f660SThomas Zimmermann 			return MODE_MEM;
467c020f660SThomas Zimmermann 	}
468c020f660SThomas Zimmermann 
469c020f660SThomas Zimmermann 	return MODE_OK;
470c020f660SThomas Zimmermann }
471c020f660SThomas Zimmermann 
47272d73dd3SThomas Zimmermann static const struct drm_mode_config_funcs udl_mode_config_funcs = {
473230b8b04SThomas Zimmermann 	.fb_create = drm_gem_fb_create_with_dirty,
474c020f660SThomas Zimmermann 	.mode_valid = udl_mode_config_mode_valid,
4759fda81e0SThomas Zimmermann 	.atomic_check  = drm_atomic_helper_check,
4769fda81e0SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
4775320918bSDave Airlie };
4785320918bSDave Airlie 
udl_modeset_init(struct drm_device * dev)4795320918bSDave Airlie int udl_modeset_init(struct drm_device *dev)
4805320918bSDave Airlie {
4816ae355a2SDaniel Vetter 	struct udl_device *udl = to_udl(dev);
48272d73dd3SThomas Zimmermann 	struct drm_plane *primary_plane;
48372d73dd3SThomas Zimmermann 	struct drm_crtc *crtc;
48472d73dd3SThomas Zimmermann 	struct drm_encoder *encoder;
485e829cf0bSThomas Zimmermann 	struct drm_connector *connector;
486e829cf0bSThomas Zimmermann 	int ret;
487e829cf0bSThomas Zimmermann 
488fe5b7c86SDaniel Vetter 	ret = drmm_mode_config_init(dev);
489fe5b7c86SDaniel Vetter 	if (ret)
490fe5b7c86SDaniel Vetter 		return ret;
4915320918bSDave Airlie 
4925320918bSDave Airlie 	dev->mode_config.min_width = 640;
4935320918bSDave Airlie 	dev->mode_config.min_height = 480;
4945320918bSDave Airlie 	dev->mode_config.max_width = 2048;
4955320918bSDave Airlie 	dev->mode_config.max_height = 2048;
496d8177841SThomas Zimmermann 	dev->mode_config.preferred_depth = 16;
49772d73dd3SThomas Zimmermann 	dev->mode_config.funcs = &udl_mode_config_funcs;
4985320918bSDave Airlie 
49972d73dd3SThomas Zimmermann 	primary_plane = &udl->primary_plane;
50072d73dd3SThomas Zimmermann 	ret = drm_universal_plane_init(dev, primary_plane, 0,
50172d73dd3SThomas Zimmermann 				       &udl_primary_plane_funcs,
50272d73dd3SThomas Zimmermann 				       udl_primary_plane_formats,
50372d73dd3SThomas Zimmermann 				       ARRAY_SIZE(udl_primary_plane_formats),
50472d73dd3SThomas Zimmermann 				       udl_primary_plane_fmtmods,
50572d73dd3SThomas Zimmermann 				       DRM_PLANE_TYPE_PRIMARY, NULL);
50672d73dd3SThomas Zimmermann 	if (ret)
50772d73dd3SThomas Zimmermann 		return ret;
50872d73dd3SThomas Zimmermann 	drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs);
50972d73dd3SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(primary_plane);
51072d73dd3SThomas Zimmermann 
51172d73dd3SThomas Zimmermann 	crtc = &udl->crtc;
51272d73dd3SThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
51372d73dd3SThomas Zimmermann 					&udl_crtc_funcs, NULL);
51472d73dd3SThomas Zimmermann 	if (ret)
51572d73dd3SThomas Zimmermann 		return ret;
51672d73dd3SThomas Zimmermann 	drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs);
51772d73dd3SThomas Zimmermann 
51872d73dd3SThomas Zimmermann 	encoder = &udl->encoder;
51972d73dd3SThomas Zimmermann 	ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL);
52072d73dd3SThomas Zimmermann 	if (ret)
52172d73dd3SThomas Zimmermann 		return ret;
52272d73dd3SThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
5235320918bSDave Airlie 
524*2262e917SThomas Zimmermann 	connector = &udl->connector;
525*2262e917SThomas Zimmermann 	ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA);
526*2262e917SThomas Zimmermann 	if (ret)
527*2262e917SThomas Zimmermann 		return ret;
528*2262e917SThomas Zimmermann 	drm_connector_helper_add(connector, &udl_connector_helper_funcs);
529*2262e917SThomas Zimmermann 
530*2262e917SThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_CONNECT |
531*2262e917SThomas Zimmermann 			    DRM_CONNECTOR_POLL_DISCONNECT;
532*2262e917SThomas Zimmermann 
53372d73dd3SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
5349fda81e0SThomas Zimmermann 	if (ret)
535fe5b7c86SDaniel Vetter 		return ret;
5369fda81e0SThomas Zimmermann 
5379fda81e0SThomas Zimmermann 	drm_mode_config_reset(dev);
5385320918bSDave Airlie 
5395320918bSDave Airlie 	return 0;
5405320918bSDave Airlie }
541