xref: /openbmc/linux/drivers/gpu/drm/udl/udl_modeset.c (revision 9869e40df1a72eede80b4816d6e522e57a6fd97b)
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 
1172d73dd3SThomas Zimmermann #include <drm/drm_atomic.h>
129fda81e0SThomas Zimmermann #include <drm/drm_atomic_helper.h>
13760285e7SDavid Howells #include <drm/drm_crtc_helper.h>
14230b8b04SThomas Zimmermann #include <drm/drm_damage_helper.h>
15ca2bd373SThomas Zimmermann #include <drm/drm_drv.h>
160862cfd3SThomas Zimmermann #include <drm/drm_edid.h>
17a8109f5bSThomas Zimmermann #include <drm/drm_fourcc.h>
185ceeb328SThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
199fda81e0SThomas Zimmermann #include <drm/drm_gem_framebuffer_helper.h>
20a8109f5bSThomas Zimmermann #include <drm/drm_gem_shmem_helper.h>
21a9dcf380SSam Ravnborg #include <drm/drm_modeset_helper_vtables.h>
2272d73dd3SThomas Zimmermann #include <drm/drm_plane_helper.h>
230862cfd3SThomas Zimmermann #include <drm/drm_probe_helper.h>
24a9dcf380SSam Ravnborg #include <drm/drm_vblank.h>
25a9dcf380SSam Ravnborg 
265320918bSDave Airlie #include "udl_drv.h"
27ff76e82cSThomas Zimmermann #include "udl_proto.h"
289fda81e0SThomas Zimmermann 
295320918bSDave Airlie /*
305320918bSDave Airlie  * All DisplayLink bulk operations start with 0xAF, followed by specific code
315320918bSDave Airlie  * All operations are written to buffers which then later get sent to device
325320918bSDave Airlie  */
335320918bSDave Airlie static char *udl_set_register(char *buf, u8 reg, u8 val)
345320918bSDave Airlie {
355320918bSDave Airlie 	*buf++ = 0xAF;
365320918bSDave Airlie 	*buf++ = 0x20;
375320918bSDave Airlie 	*buf++ = reg;
385320918bSDave Airlie 	*buf++ = val;
395320918bSDave Airlie 	return buf;
405320918bSDave Airlie }
415320918bSDave Airlie 
425320918bSDave Airlie static char *udl_vidreg_lock(char *buf)
435320918bSDave Airlie {
445320918bSDave Airlie 	return udl_set_register(buf, 0xFF, 0x00);
455320918bSDave Airlie }
465320918bSDave Airlie 
475320918bSDave Airlie static char *udl_vidreg_unlock(char *buf)
485320918bSDave Airlie {
495320918bSDave Airlie 	return udl_set_register(buf, 0xFF, 0xFF);
505320918bSDave Airlie }
515320918bSDave Airlie 
52997d33c3SThomas Zimmermann static char *udl_set_blank_mode(char *buf, u8 mode)
535320918bSDave Airlie {
54ff76e82cSThomas Zimmermann 	return udl_set_register(buf, UDL_REG_BLANKMODE, mode);
555320918bSDave Airlie }
565320918bSDave Airlie 
575320918bSDave Airlie static char *udl_set_color_depth(char *buf, u8 selection)
585320918bSDave Airlie {
595320918bSDave Airlie 	return udl_set_register(buf, 0x00, selection);
605320918bSDave Airlie }
615320918bSDave Airlie 
625320918bSDave Airlie static char *udl_set_base16bpp(char *wrptr, u32 base)
635320918bSDave Airlie {
645320918bSDave Airlie 	/* the base pointer is 16 bits wide, 0x20 is hi byte. */
655320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x20, base >> 16);
665320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x21, base >> 8);
675320918bSDave Airlie 	return udl_set_register(wrptr, 0x22, base);
685320918bSDave Airlie }
695320918bSDave Airlie 
705320918bSDave Airlie /*
715320918bSDave Airlie  * DisplayLink HW has separate 16bpp and 8bpp framebuffers.
725320918bSDave Airlie  * In 24bpp modes, the low 323 RGB bits go in the 8bpp framebuffer
735320918bSDave Airlie  */
745320918bSDave Airlie static char *udl_set_base8bpp(char *wrptr, u32 base)
755320918bSDave Airlie {
765320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x26, base >> 16);
775320918bSDave Airlie 	wrptr = udl_set_register(wrptr, 0x27, base >> 8);
785320918bSDave Airlie 	return udl_set_register(wrptr, 0x28, base);
795320918bSDave Airlie }
805320918bSDave Airlie 
815320918bSDave Airlie static char *udl_set_register_16(char *wrptr, u8 reg, u16 value)
825320918bSDave Airlie {
835320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value >> 8);
845320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value);
855320918bSDave Airlie }
865320918bSDave Airlie 
875320918bSDave Airlie /*
885320918bSDave Airlie  * This is kind of weird because the controller takes some
895320918bSDave Airlie  * register values in a different byte order than other registers.
905320918bSDave Airlie  */
915320918bSDave Airlie static char *udl_set_register_16be(char *wrptr, u8 reg, u16 value)
925320918bSDave Airlie {
935320918bSDave Airlie 	wrptr = udl_set_register(wrptr, reg, value);
945320918bSDave Airlie 	return udl_set_register(wrptr, reg+1, value >> 8);
955320918bSDave Airlie }
965320918bSDave Airlie 
975320918bSDave Airlie /*
985320918bSDave Airlie  * LFSR is linear feedback shift register. The reason we have this is
995320918bSDave Airlie  * because the display controller needs to minimize the clock depth of
1005320918bSDave Airlie  * various counters used in the display path. So this code reverses the
1015320918bSDave Airlie  * provided value into the lfsr16 value by counting backwards to get
1025320918bSDave Airlie  * the value that needs to be set in the hardware comparator to get the
1035320918bSDave Airlie  * same actual count. This makes sense once you read above a couple of
1045320918bSDave Airlie  * times and think about it from a hardware perspective.
1055320918bSDave Airlie  */
1065320918bSDave Airlie static u16 udl_lfsr16(u16 actual_count)
1075320918bSDave Airlie {
1085320918bSDave Airlie 	u32 lv = 0xFFFF; /* This is the lfsr value that the hw starts with */
1095320918bSDave Airlie 
1105320918bSDave Airlie 	while (actual_count--) {
1115320918bSDave Airlie 		lv =	 ((lv << 1) |
1125320918bSDave Airlie 			(((lv >> 15) ^ (lv >> 4) ^ (lv >> 2) ^ (lv >> 1)) & 1))
1135320918bSDave Airlie 			& 0xFFFF;
1145320918bSDave Airlie 	}
1155320918bSDave Airlie 
1165320918bSDave Airlie 	return (u16) lv;
1175320918bSDave Airlie }
1185320918bSDave Airlie 
1195320918bSDave Airlie /*
1205320918bSDave Airlie  * This does LFSR conversion on the value that is to be written.
1215320918bSDave Airlie  * See LFSR explanation above for more detail.
1225320918bSDave Airlie  */
1235320918bSDave Airlie static char *udl_set_register_lfsr16(char *wrptr, u8 reg, u16 value)
1245320918bSDave Airlie {
1255320918bSDave Airlie 	return udl_set_register_16(wrptr, reg, udl_lfsr16(value));
1265320918bSDave Airlie }
1275320918bSDave Airlie 
1285320918bSDave Airlie /*
129*9869e40dSThomas Zimmermann  * Takes a DRM display mode and converts it into the DisplayLink
130*9869e40dSThomas Zimmermann  * equivalent register commands.
1315320918bSDave Airlie  */
132*9869e40dSThomas Zimmermann static char *udl_set_display_mode(char *buf, struct drm_display_mode *mode)
1335320918bSDave Airlie {
134*9869e40dSThomas Zimmermann 	u16 reg01 = mode->crtc_htotal - mode->crtc_hsync_start;
135*9869e40dSThomas Zimmermann 	u16 reg03 = reg01 + mode->crtc_hdisplay;
136*9869e40dSThomas Zimmermann 	u16 reg05 = mode->crtc_vtotal - mode->crtc_vsync_start;
137*9869e40dSThomas Zimmermann 	u16 reg07 = reg05 + mode->crtc_vdisplay;
138*9869e40dSThomas Zimmermann 	u16 reg09 = mode->crtc_htotal - 1;
139*9869e40dSThomas Zimmermann 	u16 reg0b = 1; /* libdlo hardcodes hsync start to 1 */
140*9869e40dSThomas Zimmermann 	u16 reg0d = mode->crtc_hsync_end - mode->crtc_hsync_start + 1;
141*9869e40dSThomas Zimmermann 	u16 reg0f = mode->hdisplay;
142*9869e40dSThomas Zimmermann 	u16 reg11 = mode->crtc_vtotal;
143*9869e40dSThomas Zimmermann 	u16 reg13 = 0; /* libdlo hardcodes vsync start to 0 */
144*9869e40dSThomas Zimmermann 	u16 reg15 = mode->crtc_vsync_end - mode->crtc_vsync_start;
145*9869e40dSThomas Zimmermann 	u16 reg17 = mode->crtc_vdisplay;
146*9869e40dSThomas Zimmermann 	u16 reg1b = mode->clock / 5;
1475320918bSDave Airlie 
148*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYSTART, reg01);
149*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XDISPLAYEND, reg03);
150*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYSTART, reg05);
151*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YDISPLAYEND, reg07);
152*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_XENDCOUNT, reg09);
153*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCSTART, reg0b);
154*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_HSYNCEND, reg0d);
155*9869e40dSThomas Zimmermann 	buf = udl_set_register_16(buf, UDL_REG_HPIXELS, reg0f);
156*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_YENDCOUNT, reg11);
157*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCSTART, reg13);
158*9869e40dSThomas Zimmermann 	buf = udl_set_register_lfsr16(buf, UDL_REG_VSYNCEND, reg15);
159*9869e40dSThomas Zimmermann 	buf = udl_set_register_16(buf, UDL_REG_VPIXELS, reg17);
160*9869e40dSThomas Zimmermann 	buf = udl_set_register_16be(buf, UDL_REG_PIXELCLOCK5KHZ, reg1b);
1615320918bSDave Airlie 
162*9869e40dSThomas Zimmermann 	return buf;
1635320918bSDave Airlie }
1645320918bSDave Airlie 
1655bd42f69SDave Airlie static char *udl_dummy_render(char *wrptr)
1665bd42f69SDave Airlie {
1675bd42f69SDave Airlie 	*wrptr++ = 0xAF;
1685bd42f69SDave Airlie 	*wrptr++ = 0x6A; /* copy */
1695bd42f69SDave Airlie 	*wrptr++ = 0x00; /* from addr */
1705bd42f69SDave Airlie 	*wrptr++ = 0x00;
1715bd42f69SDave Airlie 	*wrptr++ = 0x00;
1725bd42f69SDave Airlie 	*wrptr++ = 0x01; /* one pixel */
1735bd42f69SDave Airlie 	*wrptr++ = 0x00; /* to address */
1745bd42f69SDave Airlie 	*wrptr++ = 0x00;
1755bd42f69SDave Airlie 	*wrptr++ = 0x00;
1765bd42f69SDave Airlie 	return wrptr;
1775bd42f69SDave Airlie }
1785bd42f69SDave Airlie 
179a8109f5bSThomas Zimmermann static long udl_log_cpp(unsigned int cpp)
180a8109f5bSThomas Zimmermann {
181a8109f5bSThomas Zimmermann 	if (WARN_ON(!is_power_of_2(cpp)))
182a8109f5bSThomas Zimmermann 		return -EINVAL;
183a8109f5bSThomas Zimmermann 	return __ffs(cpp);
184a8109f5bSThomas Zimmermann }
185a8109f5bSThomas Zimmermann 
1867938f421SLucas De Marchi static int udl_handle_damage(struct drm_framebuffer *fb,
1877938f421SLucas De Marchi 			     const struct iosys_map *map,
188b13fa27aSTakashi Iwai 			     const struct drm_rect *clip)
189a8109f5bSThomas Zimmermann {
190a8109f5bSThomas Zimmermann 	struct drm_device *dev = fb->dev;
1915ceeb328SThomas Zimmermann 	void *vaddr = map->vaddr; /* TODO: Use mapping abstraction properly */
192ce724470SThomas Zimmermann 	int i, ret;
193a8109f5bSThomas Zimmermann 	char *cmd;
194a8109f5bSThomas Zimmermann 	struct urb *urb;
195a8109f5bSThomas Zimmermann 	int log_bpp;
196a8109f5bSThomas Zimmermann 
197a8109f5bSThomas Zimmermann 	ret = udl_log_cpp(fb->format->cpp[0]);
198a8109f5bSThomas Zimmermann 	if (ret < 0)
199a8109f5bSThomas Zimmermann 		return ret;
200a8109f5bSThomas Zimmermann 	log_bpp = ret;
201a8109f5bSThomas Zimmermann 
202a8109f5bSThomas Zimmermann 	urb = udl_get_urb(dev);
203fcc21447SThomas Zimmermann 	if (!urb)
204fcc21447SThomas Zimmermann 		return -ENOMEM;
205a8109f5bSThomas Zimmermann 	cmd = urb->transfer_buffer;
206a8109f5bSThomas Zimmermann 
207b13fa27aSTakashi Iwai 	for (i = clip->y1; i < clip->y2; i++) {
208a8109f5bSThomas Zimmermann 		const int line_offset = fb->pitches[0] * i;
209b13fa27aSTakashi Iwai 		const int byte_offset = line_offset + (clip->x1 << log_bpp);
210b13fa27aSTakashi Iwai 		const int dev_byte_offset = (fb->width * i + clip->x1) << log_bpp;
211b13fa27aSTakashi Iwai 		const int byte_width = drm_rect_width(clip) << log_bpp;
212a8109f5bSThomas Zimmermann 		ret = udl_render_hline(dev, log_bpp, &urb, (char *)vaddr,
213a8109f5bSThomas Zimmermann 				       &cmd, byte_offset, dev_byte_offset,
214a8109f5bSThomas Zimmermann 				       byte_width);
215a8109f5bSThomas Zimmermann 		if (ret)
216fcc21447SThomas Zimmermann 			return ret;
217a8109f5bSThomas Zimmermann 	}
218a8109f5bSThomas Zimmermann 
219a8109f5bSThomas Zimmermann 	if (cmd > (char *)urb->transfer_buffer) {
220a8109f5bSThomas Zimmermann 		/* Send partial buffer remaining before exiting */
221a8109f5bSThomas Zimmermann 		int len;
222a8109f5bSThomas Zimmermann 		if (cmd < (char *)urb->transfer_buffer + urb->transfer_buffer_length)
223a8109f5bSThomas Zimmermann 			*cmd++ = 0xAF;
224a8109f5bSThomas Zimmermann 		len = cmd - (char *)urb->transfer_buffer;
225a8109f5bSThomas Zimmermann 		ret = udl_submit_urb(dev, urb, len);
226a8109f5bSThomas Zimmermann 	} else {
227a8109f5bSThomas Zimmermann 		udl_urb_completion(urb);
228a8109f5bSThomas Zimmermann 	}
229a8109f5bSThomas Zimmermann 
230fcc21447SThomas Zimmermann 	return 0;
231a8109f5bSThomas Zimmermann }
232a8109f5bSThomas Zimmermann 
2339fda81e0SThomas Zimmermann /*
23472d73dd3SThomas Zimmermann  * Primary plane
2359fda81e0SThomas Zimmermann  */
2369fda81e0SThomas Zimmermann 
23772d73dd3SThomas Zimmermann static const uint32_t udl_primary_plane_formats[] = {
2389fda81e0SThomas Zimmermann 	DRM_FORMAT_RGB565,
2399fda81e0SThomas Zimmermann 	DRM_FORMAT_XRGB8888,
2409fda81e0SThomas Zimmermann };
2419fda81e0SThomas Zimmermann 
24272d73dd3SThomas Zimmermann static const uint64_t udl_primary_plane_fmtmods[] = {
24372d73dd3SThomas Zimmermann 	DRM_FORMAT_MOD_LINEAR,
24472d73dd3SThomas Zimmermann 	DRM_FORMAT_MOD_INVALID
24572d73dd3SThomas Zimmermann };
24672d73dd3SThomas Zimmermann 
24772d73dd3SThomas Zimmermann static void udl_primary_plane_helper_atomic_update(struct drm_plane *plane,
24872d73dd3SThomas Zimmermann 						   struct drm_atomic_state *state)
2495320918bSDave Airlie {
250ca2bd373SThomas Zimmermann 	struct drm_device *dev = plane->dev;
25172d73dd3SThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
2525ceeb328SThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
25372d73dd3SThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
25472d73dd3SThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
255fcc21447SThomas Zimmermann 	struct drm_atomic_helper_damage_iter iter;
256fcc21447SThomas Zimmermann 	struct drm_rect damage;
257fcc21447SThomas Zimmermann 	int ret, idx;
25872d73dd3SThomas Zimmermann 
25972d73dd3SThomas Zimmermann 	if (!fb)
26072d73dd3SThomas Zimmermann 		return; /* no framebuffer; plane is disabled */
26172d73dd3SThomas Zimmermann 
262fcc21447SThomas Zimmermann 	ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
263fcc21447SThomas Zimmermann 	if (ret)
264fcc21447SThomas Zimmermann 		return;
265fcc21447SThomas Zimmermann 
266fcc21447SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
267fcc21447SThomas Zimmermann 		goto out_drm_gem_fb_end_cpu_access;
268fcc21447SThomas Zimmermann 
269fcc21447SThomas Zimmermann 	drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state);
270fcc21447SThomas Zimmermann 	drm_atomic_for_each_plane_damage(&iter, &damage) {
271fcc21447SThomas Zimmermann 		udl_handle_damage(fb, &shadow_plane_state->data[0], &damage);
272fcc21447SThomas Zimmermann 	}
273ca2bd373SThomas Zimmermann 
274ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
275fcc21447SThomas Zimmermann 
276fcc21447SThomas Zimmermann out_drm_gem_fb_end_cpu_access:
277fcc21447SThomas Zimmermann 	drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
27872d73dd3SThomas Zimmermann }
27972d73dd3SThomas Zimmermann 
28072d73dd3SThomas Zimmermann static const struct drm_plane_helper_funcs udl_primary_plane_helper_funcs = {
28172d73dd3SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
28272d73dd3SThomas Zimmermann 	.atomic_check = drm_plane_helper_atomic_check,
28372d73dd3SThomas Zimmermann 	.atomic_update = udl_primary_plane_helper_atomic_update,
28472d73dd3SThomas Zimmermann };
28572d73dd3SThomas Zimmermann 
28672d73dd3SThomas Zimmermann static const struct drm_plane_funcs udl_primary_plane_funcs = {
28772d73dd3SThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
28872d73dd3SThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
28972d73dd3SThomas Zimmermann 	.destroy = drm_plane_cleanup,
29072d73dd3SThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
29172d73dd3SThomas Zimmermann };
29272d73dd3SThomas Zimmermann 
29372d73dd3SThomas Zimmermann /*
29472d73dd3SThomas Zimmermann  * CRTC
29572d73dd3SThomas Zimmermann  */
29672d73dd3SThomas Zimmermann 
29772d73dd3SThomas Zimmermann static int udl_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
29872d73dd3SThomas Zimmermann {
29972d73dd3SThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
30072d73dd3SThomas Zimmermann 
30172d73dd3SThomas Zimmermann 	return drm_atomic_helper_check_crtc_state(new_crtc_state, false);
30272d73dd3SThomas Zimmermann }
30372d73dd3SThomas Zimmermann 
30472d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state)
30572d73dd3SThomas Zimmermann {
30672d73dd3SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
30772d73dd3SThomas Zimmermann 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
30872d73dd3SThomas Zimmermann 	struct drm_display_mode *mode = &crtc_state->mode;
309890e4de8SThomas Zimmermann 	struct urb *urb;
3105320918bSDave Airlie 	char *buf;
311ca2bd373SThomas Zimmermann 	int idx;
312ca2bd373SThomas Zimmermann 
313ca2bd373SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
314ca2bd373SThomas Zimmermann 		return;
3155320918bSDave Airlie 
316890e4de8SThomas Zimmermann 	urb = udl_get_urb(dev);
317890e4de8SThomas Zimmermann 	if (!urb)
318ca2bd373SThomas Zimmermann 		goto out;
3195320918bSDave Airlie 
320890e4de8SThomas Zimmermann 	buf = (char *)urb->transfer_buffer;
321890e4de8SThomas Zimmermann 	buf = udl_vidreg_lock(buf);
322ff76e82cSThomas Zimmermann 	buf = udl_set_color_depth(buf, UDL_COLORDEPTH_16BPP);
3235320918bSDave Airlie 	/* set base for 16bpp segment to 0 */
324890e4de8SThomas Zimmermann 	buf = udl_set_base16bpp(buf, 0);
3255320918bSDave Airlie 	/* set base for 8bpp segment to end of fb */
326890e4de8SThomas Zimmermann 	buf = udl_set_base8bpp(buf, 2 * mode->vdisplay * mode->hdisplay);
327*9869e40dSThomas Zimmermann 	buf = udl_set_display_mode(buf, mode);
328ff76e82cSThomas Zimmermann 	buf = udl_set_blank_mode(buf, UDL_BLANKMODE_ON);
329890e4de8SThomas Zimmermann 	buf = udl_vidreg_unlock(buf);
330890e4de8SThomas Zimmermann 	buf = udl_dummy_render(buf);
3315bd42f69SDave Airlie 
332890e4de8SThomas Zimmermann 	udl_submit_urb(dev, urb, buf - (char *)urb->transfer_buffer);
333ca2bd373SThomas Zimmermann 
334ca2bd373SThomas Zimmermann out:
335ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
3369fda81e0SThomas Zimmermann }
3379fda81e0SThomas Zimmermann 
33872d73dd3SThomas Zimmermann static void udl_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state)
3399fda81e0SThomas Zimmermann {
340997d33c3SThomas Zimmermann 	struct drm_device *dev = crtc->dev;
341997d33c3SThomas Zimmermann 	struct urb *urb;
342997d33c3SThomas Zimmermann 	char *buf;
343ca2bd373SThomas Zimmermann 	int idx;
344ca2bd373SThomas Zimmermann 
345ca2bd373SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
346ca2bd373SThomas Zimmermann 		return;
347997d33c3SThomas Zimmermann 
348997d33c3SThomas Zimmermann 	urb = udl_get_urb(dev);
349997d33c3SThomas Zimmermann 	if (!urb)
350ca2bd373SThomas Zimmermann 		goto out;
351997d33c3SThomas Zimmermann 
352997d33c3SThomas Zimmermann 	buf = (char *)urb->transfer_buffer;
353997d33c3SThomas Zimmermann 	buf = udl_vidreg_lock(buf);
354ff76e82cSThomas Zimmermann 	buf = udl_set_blank_mode(buf, UDL_BLANKMODE_POWERDOWN);
355997d33c3SThomas Zimmermann 	buf = udl_vidreg_unlock(buf);
356997d33c3SThomas Zimmermann 	buf = udl_dummy_render(buf);
357997d33c3SThomas Zimmermann 
358997d33c3SThomas 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 
36472d73dd3SThomas Zimmermann static const struct drm_crtc_helper_funcs udl_crtc_helper_funcs = {
36572d73dd3SThomas Zimmermann 	.atomic_check = udl_crtc_helper_atomic_check,
36672d73dd3SThomas Zimmermann 	.atomic_enable = udl_crtc_helper_atomic_enable,
36772d73dd3SThomas Zimmermann 	.atomic_disable = udl_crtc_helper_atomic_disable,
36872d73dd3SThomas Zimmermann };
36940377ef2SStéphane Marchesin 
37072d73dd3SThomas Zimmermann static const struct drm_crtc_funcs udl_crtc_funcs = {
37172d73dd3SThomas Zimmermann 	.reset = drm_atomic_helper_crtc_reset,
37272d73dd3SThomas Zimmermann 	.destroy = drm_crtc_cleanup,
37372d73dd3SThomas Zimmermann 	.set_config = drm_atomic_helper_set_config,
37472d73dd3SThomas Zimmermann 	.page_flip = drm_atomic_helper_page_flip,
37572d73dd3SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
37672d73dd3SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
37772d73dd3SThomas Zimmermann };
3789fda81e0SThomas Zimmermann 
37972d73dd3SThomas Zimmermann /*
38072d73dd3SThomas Zimmermann  * Encoder
38172d73dd3SThomas Zimmermann  */
38240377ef2SStéphane Marchesin 
38372d73dd3SThomas Zimmermann static const struct drm_encoder_funcs udl_encoder_funcs = {
38472d73dd3SThomas Zimmermann 	.destroy = drm_encoder_cleanup,
3855320918bSDave Airlie };
3865320918bSDave Airlie 
3879fda81e0SThomas Zimmermann /*
3880862cfd3SThomas Zimmermann  * Connector
3890862cfd3SThomas Zimmermann  */
3900862cfd3SThomas Zimmermann 
3910862cfd3SThomas Zimmermann static int udl_connector_helper_get_modes(struct drm_connector *connector)
3920862cfd3SThomas Zimmermann {
3930862cfd3SThomas Zimmermann 	struct udl_connector *udl_connector = to_udl_connector(connector);
3940862cfd3SThomas Zimmermann 
3950862cfd3SThomas Zimmermann 	drm_connector_update_edid_property(connector, udl_connector->edid);
3960862cfd3SThomas Zimmermann 	if (udl_connector->edid)
3970862cfd3SThomas Zimmermann 		return drm_add_edid_modes(connector, udl_connector->edid);
3980862cfd3SThomas Zimmermann 
3990862cfd3SThomas Zimmermann 	return 0;
4000862cfd3SThomas Zimmermann }
4010862cfd3SThomas Zimmermann 
4020862cfd3SThomas Zimmermann static const struct drm_connector_helper_funcs udl_connector_helper_funcs = {
4030862cfd3SThomas Zimmermann 	.get_modes = udl_connector_helper_get_modes,
4040862cfd3SThomas Zimmermann };
4050862cfd3SThomas Zimmermann 
4060862cfd3SThomas Zimmermann static int udl_get_edid_block(void *data, u8 *buf, unsigned int block, size_t len)
4070862cfd3SThomas Zimmermann {
4080862cfd3SThomas Zimmermann 	struct udl_device *udl = data;
4090862cfd3SThomas Zimmermann 	struct drm_device *dev = &udl->drm;
4100862cfd3SThomas Zimmermann 	struct usb_device *udev = udl_to_usb_device(udl);
4110862cfd3SThomas Zimmermann 	u8 *read_buff;
4120862cfd3SThomas Zimmermann 	int ret;
4130862cfd3SThomas Zimmermann 	size_t i;
4140862cfd3SThomas Zimmermann 
4150862cfd3SThomas Zimmermann 	read_buff = kmalloc(2, GFP_KERNEL);
4160862cfd3SThomas Zimmermann 	if (!read_buff)
4170862cfd3SThomas Zimmermann 		return -ENOMEM;
4180862cfd3SThomas Zimmermann 
4190862cfd3SThomas Zimmermann 	for (i = 0; i < len; i++) {
4200862cfd3SThomas Zimmermann 		int bval = (i + block * EDID_LENGTH) << 8;
4210862cfd3SThomas Zimmermann 
4220862cfd3SThomas Zimmermann 		ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
4230862cfd3SThomas Zimmermann 				      0x02, (0x80 | (0x02 << 5)), bval,
4240862cfd3SThomas Zimmermann 				      0xA1, read_buff, 2, USB_CTRL_GET_TIMEOUT);
4250862cfd3SThomas Zimmermann 		if (ret < 0) {
4260862cfd3SThomas Zimmermann 			drm_err(dev, "Read EDID byte %zu failed err %x\n", i, ret);
4270862cfd3SThomas Zimmermann 			goto err_kfree;
4280862cfd3SThomas Zimmermann 		} else if (ret < 1) {
4290862cfd3SThomas Zimmermann 			ret = -EIO;
4300862cfd3SThomas Zimmermann 			drm_err(dev, "Read EDID byte %zu failed\n", i);
4310862cfd3SThomas Zimmermann 			goto err_kfree;
4320862cfd3SThomas Zimmermann 		}
4330862cfd3SThomas Zimmermann 
4340862cfd3SThomas Zimmermann 		buf[i] = read_buff[1];
4350862cfd3SThomas Zimmermann 	}
4360862cfd3SThomas Zimmermann 
4370862cfd3SThomas Zimmermann 	kfree(read_buff);
4380862cfd3SThomas Zimmermann 
4390862cfd3SThomas Zimmermann 	return 0;
4400862cfd3SThomas Zimmermann 
4410862cfd3SThomas Zimmermann err_kfree:
4420862cfd3SThomas Zimmermann 	kfree(read_buff);
4430862cfd3SThomas Zimmermann 	return ret;
4440862cfd3SThomas Zimmermann }
4450862cfd3SThomas Zimmermann 
4460862cfd3SThomas Zimmermann static enum drm_connector_status udl_connector_detect(struct drm_connector *connector, bool force)
4470862cfd3SThomas Zimmermann {
448ca2bd373SThomas Zimmermann 	struct drm_device *dev = connector->dev;
449ca2bd373SThomas Zimmermann 	struct udl_device *udl = to_udl(dev);
4500862cfd3SThomas Zimmermann 	struct udl_connector *udl_connector = to_udl_connector(connector);
451ca2bd373SThomas Zimmermann 	enum drm_connector_status status = connector_status_disconnected;
452ca2bd373SThomas Zimmermann 	int idx;
4530862cfd3SThomas Zimmermann 
4540862cfd3SThomas Zimmermann 	/* cleanup previous EDID */
4550862cfd3SThomas Zimmermann 	kfree(udl_connector->edid);
456ca2bd373SThomas Zimmermann 	udl_connector->edid = NULL;
4570862cfd3SThomas Zimmermann 
458ca2bd373SThomas Zimmermann 	if (!drm_dev_enter(dev, &idx))
4590862cfd3SThomas Zimmermann 		return connector_status_disconnected;
4600862cfd3SThomas Zimmermann 
461ca2bd373SThomas Zimmermann 	udl_connector->edid = drm_do_get_edid(connector, udl_get_edid_block, udl);
462ca2bd373SThomas Zimmermann 	if (udl_connector->edid)
463ca2bd373SThomas Zimmermann 		status = connector_status_connected;
464ca2bd373SThomas Zimmermann 
465ca2bd373SThomas Zimmermann 	drm_dev_exit(idx);
466ca2bd373SThomas Zimmermann 
467ca2bd373SThomas Zimmermann 	return status;
4680862cfd3SThomas Zimmermann }
4690862cfd3SThomas Zimmermann 
4700862cfd3SThomas Zimmermann static void udl_connector_destroy(struct drm_connector *connector)
4710862cfd3SThomas Zimmermann {
4720862cfd3SThomas Zimmermann 	struct udl_connector *udl_connector = to_udl_connector(connector);
4730862cfd3SThomas Zimmermann 
4740862cfd3SThomas Zimmermann 	drm_connector_cleanup(connector);
4750862cfd3SThomas Zimmermann 	kfree(udl_connector->edid);
4760862cfd3SThomas Zimmermann 	kfree(udl_connector);
4770862cfd3SThomas Zimmermann }
4780862cfd3SThomas Zimmermann 
4790862cfd3SThomas Zimmermann static const struct drm_connector_funcs udl_connector_funcs = {
4800862cfd3SThomas Zimmermann 	.reset = drm_atomic_helper_connector_reset,
4810862cfd3SThomas Zimmermann 	.detect = udl_connector_detect,
4820862cfd3SThomas Zimmermann 	.fill_modes = drm_helper_probe_single_connector_modes,
4830862cfd3SThomas Zimmermann 	.destroy = udl_connector_destroy,
4840862cfd3SThomas Zimmermann 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
4850862cfd3SThomas Zimmermann 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
4860862cfd3SThomas Zimmermann };
4870862cfd3SThomas Zimmermann 
4880862cfd3SThomas Zimmermann struct drm_connector *udl_connector_init(struct drm_device *dev)
4890862cfd3SThomas Zimmermann {
4900862cfd3SThomas Zimmermann 	struct udl_connector *udl_connector;
4910862cfd3SThomas Zimmermann 	struct drm_connector *connector;
4920862cfd3SThomas Zimmermann 	int ret;
4930862cfd3SThomas Zimmermann 
4940862cfd3SThomas Zimmermann 	udl_connector = kzalloc(sizeof(*udl_connector), GFP_KERNEL);
4950862cfd3SThomas Zimmermann 	if (!udl_connector)
4960862cfd3SThomas Zimmermann 		return ERR_PTR(-ENOMEM);
4970862cfd3SThomas Zimmermann 
4980862cfd3SThomas Zimmermann 	connector = &udl_connector->connector;
4990862cfd3SThomas Zimmermann 	ret = drm_connector_init(dev, connector, &udl_connector_funcs, DRM_MODE_CONNECTOR_VGA);
5000862cfd3SThomas Zimmermann 	if (ret)
5010862cfd3SThomas Zimmermann 		goto err_kfree;
5020862cfd3SThomas Zimmermann 
5030862cfd3SThomas Zimmermann 	drm_connector_helper_add(connector, &udl_connector_helper_funcs);
5040862cfd3SThomas Zimmermann 
5050862cfd3SThomas Zimmermann 	connector->polled = DRM_CONNECTOR_POLL_HPD |
5060862cfd3SThomas Zimmermann 			    DRM_CONNECTOR_POLL_CONNECT |
5070862cfd3SThomas Zimmermann 			    DRM_CONNECTOR_POLL_DISCONNECT;
5080862cfd3SThomas Zimmermann 
5090862cfd3SThomas Zimmermann 	return connector;
5100862cfd3SThomas Zimmermann 
5110862cfd3SThomas Zimmermann err_kfree:
5120862cfd3SThomas Zimmermann 	kfree(udl_connector);
5130862cfd3SThomas Zimmermann 	return ERR_PTR(ret);
5140862cfd3SThomas Zimmermann }
5150862cfd3SThomas Zimmermann 
5160862cfd3SThomas Zimmermann /*
5179fda81e0SThomas Zimmermann  * Modesetting
5189fda81e0SThomas Zimmermann  */
5195320918bSDave Airlie 
520c020f660SThomas Zimmermann static enum drm_mode_status udl_mode_config_mode_valid(struct drm_device *dev,
521c020f660SThomas Zimmermann 						       const struct drm_display_mode *mode)
522c020f660SThomas Zimmermann {
523c020f660SThomas Zimmermann 	struct udl_device *udl = to_udl(dev);
524c020f660SThomas Zimmermann 
525c020f660SThomas Zimmermann 	if (udl->sku_pixel_limit) {
526c020f660SThomas Zimmermann 		if (mode->vdisplay * mode->hdisplay > udl->sku_pixel_limit)
527c020f660SThomas Zimmermann 			return MODE_MEM;
528c020f660SThomas Zimmermann 	}
529c020f660SThomas Zimmermann 
530c020f660SThomas Zimmermann 	return MODE_OK;
531c020f660SThomas Zimmermann }
532c020f660SThomas Zimmermann 
53372d73dd3SThomas Zimmermann static const struct drm_mode_config_funcs udl_mode_config_funcs = {
534230b8b04SThomas Zimmermann 	.fb_create = drm_gem_fb_create_with_dirty,
535c020f660SThomas Zimmermann 	.mode_valid = udl_mode_config_mode_valid,
5369fda81e0SThomas Zimmermann 	.atomic_check  = drm_atomic_helper_check,
5379fda81e0SThomas Zimmermann 	.atomic_commit = drm_atomic_helper_commit,
5385320918bSDave Airlie };
5395320918bSDave Airlie 
5405320918bSDave Airlie int udl_modeset_init(struct drm_device *dev)
5415320918bSDave Airlie {
5426ae355a2SDaniel Vetter 	struct udl_device *udl = to_udl(dev);
54372d73dd3SThomas Zimmermann 	struct drm_plane *primary_plane;
54472d73dd3SThomas Zimmermann 	struct drm_crtc *crtc;
54572d73dd3SThomas Zimmermann 	struct drm_encoder *encoder;
546e829cf0bSThomas Zimmermann 	struct drm_connector *connector;
547e829cf0bSThomas Zimmermann 	int ret;
548e829cf0bSThomas Zimmermann 
549fe5b7c86SDaniel Vetter 	ret = drmm_mode_config_init(dev);
550fe5b7c86SDaniel Vetter 	if (ret)
551fe5b7c86SDaniel Vetter 		return ret;
5525320918bSDave Airlie 
5535320918bSDave Airlie 	dev->mode_config.min_width = 640;
5545320918bSDave Airlie 	dev->mode_config.min_height = 480;
5555320918bSDave Airlie 	dev->mode_config.max_width = 2048;
5565320918bSDave Airlie 	dev->mode_config.max_height = 2048;
557d8177841SThomas Zimmermann 	dev->mode_config.preferred_depth = 16;
55872d73dd3SThomas Zimmermann 	dev->mode_config.funcs = &udl_mode_config_funcs;
5595320918bSDave Airlie 
56072d73dd3SThomas Zimmermann 	primary_plane = &udl->primary_plane;
56172d73dd3SThomas Zimmermann 	ret = drm_universal_plane_init(dev, primary_plane, 0,
56272d73dd3SThomas Zimmermann 				       &udl_primary_plane_funcs,
56372d73dd3SThomas Zimmermann 				       udl_primary_plane_formats,
56472d73dd3SThomas Zimmermann 				       ARRAY_SIZE(udl_primary_plane_formats),
56572d73dd3SThomas Zimmermann 				       udl_primary_plane_fmtmods,
56672d73dd3SThomas Zimmermann 				       DRM_PLANE_TYPE_PRIMARY, NULL);
56772d73dd3SThomas Zimmermann 	if (ret)
56872d73dd3SThomas Zimmermann 		return ret;
56972d73dd3SThomas Zimmermann 	drm_plane_helper_add(primary_plane, &udl_primary_plane_helper_funcs);
57072d73dd3SThomas Zimmermann 	drm_plane_enable_fb_damage_clips(primary_plane);
57172d73dd3SThomas Zimmermann 
57272d73dd3SThomas Zimmermann 	crtc = &udl->crtc;
57372d73dd3SThomas Zimmermann 	ret = drm_crtc_init_with_planes(dev, crtc, primary_plane, NULL,
57472d73dd3SThomas Zimmermann 					&udl_crtc_funcs, NULL);
57572d73dd3SThomas Zimmermann 	if (ret)
57672d73dd3SThomas Zimmermann 		return ret;
57772d73dd3SThomas Zimmermann 	drm_crtc_helper_add(crtc, &udl_crtc_helper_funcs);
57872d73dd3SThomas Zimmermann 
57972d73dd3SThomas Zimmermann 	encoder = &udl->encoder;
58072d73dd3SThomas Zimmermann 	ret = drm_encoder_init(dev, encoder, &udl_encoder_funcs, DRM_MODE_ENCODER_DAC, NULL);
58172d73dd3SThomas Zimmermann 	if (ret)
58272d73dd3SThomas Zimmermann 		return ret;
58372d73dd3SThomas Zimmermann 	encoder->possible_crtcs = drm_crtc_mask(crtc);
5845320918bSDave Airlie 
585e829cf0bSThomas Zimmermann 	connector = udl_connector_init(dev);
586fe5b7c86SDaniel Vetter 	if (IS_ERR(connector))
587fe5b7c86SDaniel Vetter 		return PTR_ERR(connector);
58872d73dd3SThomas Zimmermann 	ret = drm_connector_attach_encoder(connector, encoder);
5899fda81e0SThomas Zimmermann 	if (ret)
590fe5b7c86SDaniel Vetter 		return ret;
5919fda81e0SThomas Zimmermann 
5929fda81e0SThomas Zimmermann 	drm_mode_config_reset(dev);
5935320918bSDave Airlie 
5945320918bSDave Airlie 	return 0;
5955320918bSDave Airlie }
596