1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 Hans de Goede <hdegoede@redhat.com>
4  */
5 
6 #include <linux/dma-buf.h>
7 #include <linux/module.h>
8 #include <linux/usb.h>
9 
10 #include <drm/drm_atomic_helper.h>
11 #include <drm/drm_atomic_state_helper.h>
12 #include <drm/drm_connector.h>
13 #include <drm/drm_damage_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_fb_helper.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_format_helper.h>
18 #include <drm/drm_fourcc.h>
19 #include <drm/drm_gem_shmem_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_ioctl.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_modeset_helper_vtables.h>
24 #include <drm/drm_probe_helper.h>
25 #include <drm/drm_simple_kms_helper.h>
26 
27 static bool eco_mode;
28 module_param(eco_mode, bool, 0644);
29 MODULE_PARM_DESC(eco_mode, "Turn on Eco mode (less bright, more silent)");
30 
31 #define DRIVER_NAME		"gm12u320"
32 #define DRIVER_DESC		"Grain Media GM12U320 USB projector display"
33 #define DRIVER_DATE		"2019"
34 #define DRIVER_MAJOR		1
35 #define DRIVER_MINOR		0
36 
37 /*
38  * The DLP has an actual width of 854 pixels, but that is not a multiple
39  * of 8, breaking things left and right, so we export a width of 848.
40  */
41 #define GM12U320_USER_WIDTH		848
42 #define GM12U320_REAL_WIDTH		854
43 #define GM12U320_HEIGHT			480
44 
45 #define GM12U320_BLOCK_COUNT		20
46 
47 #define GM12U320_ERR(fmt, ...) \
48 	DRM_DEV_ERROR(&gm12u320->udev->dev, fmt, ##__VA_ARGS__)
49 
50 #define MISC_RCV_EPT			1
51 #define DATA_RCV_EPT			2
52 #define DATA_SND_EPT			3
53 #define MISC_SND_EPT			4
54 
55 #define DATA_BLOCK_HEADER_SIZE		84
56 #define DATA_BLOCK_CONTENT_SIZE		64512
57 #define DATA_BLOCK_FOOTER_SIZE		20
58 #define DATA_BLOCK_SIZE			(DATA_BLOCK_HEADER_SIZE + \
59 					 DATA_BLOCK_CONTENT_SIZE + \
60 					 DATA_BLOCK_FOOTER_SIZE)
61 #define DATA_LAST_BLOCK_CONTENT_SIZE	4032
62 #define DATA_LAST_BLOCK_SIZE		(DATA_BLOCK_HEADER_SIZE + \
63 					 DATA_LAST_BLOCK_CONTENT_SIZE + \
64 					 DATA_BLOCK_FOOTER_SIZE)
65 
66 #define CMD_SIZE			31
67 #define READ_STATUS_SIZE		13
68 #define MISC_VALUE_SIZE			4
69 
70 #define CMD_TIMEOUT			200
71 #define DATA_TIMEOUT			1000
72 #define IDLE_TIMEOUT			2000
73 #define FIRST_FRAME_TIMEOUT		2000
74 
75 #define MISC_REQ_GET_SET_ECO_A		0xff
76 #define MISC_REQ_GET_SET_ECO_B		0x35
77 /* Windows driver does once every second, with arg d = 1, other args 0 */
78 #define MISC_REQ_UNKNOWN1_A		0xff
79 #define MISC_REQ_UNKNOWN1_B		0x38
80 /* Windows driver does this on init, with arg a, b = 0, c = 0xa0, d = 4 */
81 #define MISC_REQ_UNKNOWN2_A		0xa5
82 #define MISC_REQ_UNKNOWN2_B		0x00
83 
84 struct gm12u320_device {
85 	struct drm_device	         dev;
86 	struct device                   *dmadev;
87 	struct drm_simple_display_pipe   pipe;
88 	struct drm_connector	         conn;
89 	struct usb_device               *udev;
90 	unsigned char                   *cmd_buf;
91 	unsigned char                   *data_buf[GM12U320_BLOCK_COUNT];
92 	struct {
93 		struct delayed_work       work;
94 		struct mutex             lock;
95 		struct drm_framebuffer  *fb;
96 		struct drm_rect          rect;
97 		int frame;
98 		int draw_status_timeout;
99 	} fb_update;
100 };
101 
102 #define to_gm12u320(__dev) container_of(__dev, struct gm12u320_device, dev)
103 
104 static const char cmd_data[CMD_SIZE] = {
105 	0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00,
106 	0x68, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x10, 0xff,
107 	0x00, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x80, 0x00,
108 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
109 };
110 
111 static const char cmd_draw[CMD_SIZE] = {
112 	0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00,
113 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xfe,
114 	0x00, 0x00, 0x00, 0xc0, 0xd1, 0x05, 0x00, 0x40,
115 	0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
116 };
117 
118 static const char cmd_misc[CMD_SIZE] = {
119 	0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00,
120 	0x04, 0x00, 0x00, 0x00, 0x80, 0x01, 0x10, 0xfd,
121 	0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00,
122 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
123 };
124 
125 static const char data_block_header[DATA_BLOCK_HEADER_SIZE] = {
126 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
129 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
130 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
134 	0xfb, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
135 	0x00, 0x04, 0x15, 0x00, 0x00, 0xfc, 0x00, 0x00,
136 	0x01, 0x00, 0x00, 0xdb
137 };
138 
139 static const char data_last_block_header[DATA_BLOCK_HEADER_SIZE] = {
140 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
142 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
146 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
147 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 	0xfb, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149 	0x2a, 0x00, 0x20, 0x00, 0xc0, 0x0f, 0x00, 0x00,
150 	0x01, 0x00, 0x00, 0xd7
151 };
152 
153 static const char data_block_footer[DATA_BLOCK_FOOTER_SIZE] = {
154 	0xfb, 0x14, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
155 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156 	0x80, 0x00, 0x00, 0x4f
157 };
158 
gm12u320_usb_alloc(struct gm12u320_device *gm12u320)159 static int gm12u320_usb_alloc(struct gm12u320_device *gm12u320)
160 {
161 	int i, block_size;
162 	const char *hdr;
163 
164 	gm12u320->cmd_buf = drmm_kmalloc(&gm12u320->dev, CMD_SIZE, GFP_KERNEL);
165 	if (!gm12u320->cmd_buf)
166 		return -ENOMEM;
167 
168 	for (i = 0; i < GM12U320_BLOCK_COUNT; i++) {
169 		if (i == GM12U320_BLOCK_COUNT - 1) {
170 			block_size = DATA_LAST_BLOCK_SIZE;
171 			hdr = data_last_block_header;
172 		} else {
173 			block_size = DATA_BLOCK_SIZE;
174 			hdr = data_block_header;
175 		}
176 
177 		gm12u320->data_buf[i] = drmm_kzalloc(&gm12u320->dev,
178 						     block_size, GFP_KERNEL);
179 		if (!gm12u320->data_buf[i])
180 			return -ENOMEM;
181 
182 		memcpy(gm12u320->data_buf[i], hdr, DATA_BLOCK_HEADER_SIZE);
183 		memcpy(gm12u320->data_buf[i] +
184 				(block_size - DATA_BLOCK_FOOTER_SIZE),
185 		       data_block_footer, DATA_BLOCK_FOOTER_SIZE);
186 	}
187 
188 	return 0;
189 }
190 
gm12u320_misc_request(struct gm12u320_device *gm12u320, u8 req_a, u8 req_b, u8 arg_a, u8 arg_b, u8 arg_c, u8 arg_d)191 static int gm12u320_misc_request(struct gm12u320_device *gm12u320,
192 				 u8 req_a, u8 req_b,
193 				 u8 arg_a, u8 arg_b, u8 arg_c, u8 arg_d)
194 {
195 	int ret, len;
196 
197 	memcpy(gm12u320->cmd_buf, &cmd_misc, CMD_SIZE);
198 	gm12u320->cmd_buf[20] = req_a;
199 	gm12u320->cmd_buf[21] = req_b;
200 	gm12u320->cmd_buf[22] = arg_a;
201 	gm12u320->cmd_buf[23] = arg_b;
202 	gm12u320->cmd_buf[24] = arg_c;
203 	gm12u320->cmd_buf[25] = arg_d;
204 
205 	/* Send request */
206 	ret = usb_bulk_msg(gm12u320->udev,
207 			   usb_sndbulkpipe(gm12u320->udev, MISC_SND_EPT),
208 			   gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
209 	if (ret || len != CMD_SIZE) {
210 		GM12U320_ERR("Misc. req. error %d\n", ret);
211 		return -EIO;
212 	}
213 
214 	/* Read value */
215 	ret = usb_bulk_msg(gm12u320->udev,
216 			   usb_rcvbulkpipe(gm12u320->udev, MISC_RCV_EPT),
217 			   gm12u320->cmd_buf, MISC_VALUE_SIZE, &len,
218 			   DATA_TIMEOUT);
219 	if (ret || len != MISC_VALUE_SIZE) {
220 		GM12U320_ERR("Misc. value error %d\n", ret);
221 		return -EIO;
222 	}
223 	/* cmd_buf[0] now contains the read value, which we don't use */
224 
225 	/* Read status */
226 	ret = usb_bulk_msg(gm12u320->udev,
227 			   usb_rcvbulkpipe(gm12u320->udev, MISC_RCV_EPT),
228 			   gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
229 			   CMD_TIMEOUT);
230 	if (ret || len != READ_STATUS_SIZE) {
231 		GM12U320_ERR("Misc. status error %d\n", ret);
232 		return -EIO;
233 	}
234 
235 	return 0;
236 }
237 
gm12u320_32bpp_to_24bpp_packed(u8 *dst, u8 *src, int len)238 static void gm12u320_32bpp_to_24bpp_packed(u8 *dst, u8 *src, int len)
239 {
240 	while (len--) {
241 		*dst++ = *src++;
242 		*dst++ = *src++;
243 		*dst++ = *src++;
244 		src++;
245 	}
246 }
247 
gm12u320_copy_fb_to_blocks(struct gm12u320_device *gm12u320)248 static void gm12u320_copy_fb_to_blocks(struct gm12u320_device *gm12u320)
249 {
250 	int block, dst_offset, len, remain, ret, x1, x2, y1, y2;
251 	struct drm_framebuffer *fb;
252 	void *vaddr;
253 	u8 *src;
254 
255 	mutex_lock(&gm12u320->fb_update.lock);
256 
257 	if (!gm12u320->fb_update.fb)
258 		goto unlock;
259 
260 	fb = gm12u320->fb_update.fb;
261 	x1 = gm12u320->fb_update.rect.x1;
262 	x2 = gm12u320->fb_update.rect.x2;
263 	y1 = gm12u320->fb_update.rect.y1;
264 	y2 = gm12u320->fb_update.rect.y2;
265 
266 	vaddr = drm_gem_shmem_vmap(fb->obj[0]);
267 	if (IS_ERR(vaddr)) {
268 		GM12U320_ERR("failed to vmap fb: %ld\n", PTR_ERR(vaddr));
269 		goto put_fb;
270 	}
271 
272 	if (fb->obj[0]->import_attach) {
273 		ret = dma_buf_begin_cpu_access(
274 			fb->obj[0]->import_attach->dmabuf, DMA_FROM_DEVICE);
275 		if (ret) {
276 			GM12U320_ERR("dma_buf_begin_cpu_access err: %d\n", ret);
277 			goto vunmap;
278 		}
279 	}
280 
281 	src = vaddr + y1 * fb->pitches[0] + x1 * 4;
282 
283 	x1 += (GM12U320_REAL_WIDTH - GM12U320_USER_WIDTH) / 2;
284 	x2 += (GM12U320_REAL_WIDTH - GM12U320_USER_WIDTH) / 2;
285 
286 	for (; y1 < y2; y1++) {
287 		remain = 0;
288 		len = (x2 - x1) * 3;
289 		dst_offset = (y1 * GM12U320_REAL_WIDTH + x1) * 3;
290 		block = dst_offset / DATA_BLOCK_CONTENT_SIZE;
291 		dst_offset %= DATA_BLOCK_CONTENT_SIZE;
292 
293 		if ((dst_offset + len) > DATA_BLOCK_CONTENT_SIZE) {
294 			remain = dst_offset + len - DATA_BLOCK_CONTENT_SIZE;
295 			len = DATA_BLOCK_CONTENT_SIZE - dst_offset;
296 		}
297 
298 		dst_offset += DATA_BLOCK_HEADER_SIZE;
299 		len /= 3;
300 
301 		gm12u320_32bpp_to_24bpp_packed(
302 			gm12u320->data_buf[block] + dst_offset,
303 			src, len);
304 
305 		if (remain) {
306 			block++;
307 			dst_offset = DATA_BLOCK_HEADER_SIZE;
308 			gm12u320_32bpp_to_24bpp_packed(
309 				gm12u320->data_buf[block] + dst_offset,
310 				src + len * 4, remain / 3);
311 		}
312 		src += fb->pitches[0];
313 	}
314 
315 	if (fb->obj[0]->import_attach) {
316 		ret = dma_buf_end_cpu_access(fb->obj[0]->import_attach->dmabuf,
317 					     DMA_FROM_DEVICE);
318 		if (ret)
319 			GM12U320_ERR("dma_buf_end_cpu_access err: %d\n", ret);
320 	}
321 vunmap:
322 	drm_gem_shmem_vunmap(fb->obj[0], vaddr);
323 put_fb:
324 	drm_framebuffer_put(fb);
325 	gm12u320->fb_update.fb = NULL;
326 unlock:
327 	mutex_unlock(&gm12u320->fb_update.lock);
328 }
329 
gm12u320_fb_update_work(struct work_struct *work)330 static void gm12u320_fb_update_work(struct work_struct *work)
331 {
332 	struct gm12u320_device *gm12u320 =
333 		container_of(to_delayed_work(work), struct gm12u320_device,
334 			     fb_update.work);
335 	int block, block_size, len;
336 	int ret = 0;
337 
338 	gm12u320_copy_fb_to_blocks(gm12u320);
339 
340 	for (block = 0; block < GM12U320_BLOCK_COUNT; block++) {
341 		if (block == GM12U320_BLOCK_COUNT - 1)
342 			block_size = DATA_LAST_BLOCK_SIZE;
343 		else
344 			block_size = DATA_BLOCK_SIZE;
345 
346 		/* Send data command to device */
347 		memcpy(gm12u320->cmd_buf, cmd_data, CMD_SIZE);
348 		gm12u320->cmd_buf[8] = block_size & 0xff;
349 		gm12u320->cmd_buf[9] = block_size >> 8;
350 		gm12u320->cmd_buf[20] = 0xfc - block * 4;
351 		gm12u320->cmd_buf[21] =
352 			block | (gm12u320->fb_update.frame << 7);
353 
354 		ret = usb_bulk_msg(gm12u320->udev,
355 			usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
356 			gm12u320->cmd_buf, CMD_SIZE, &len,
357 			CMD_TIMEOUT);
358 		if (ret || len != CMD_SIZE)
359 			goto err;
360 
361 		/* Send data block to device */
362 		ret = usb_bulk_msg(gm12u320->udev,
363 			usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
364 			gm12u320->data_buf[block], block_size,
365 			&len, DATA_TIMEOUT);
366 		if (ret || len != block_size)
367 			goto err;
368 
369 		/* Read status */
370 		ret = usb_bulk_msg(gm12u320->udev,
371 			usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
372 			gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
373 			CMD_TIMEOUT);
374 		if (ret || len != READ_STATUS_SIZE)
375 			goto err;
376 	}
377 
378 	/* Send draw command to device */
379 	memcpy(gm12u320->cmd_buf, cmd_draw, CMD_SIZE);
380 	ret = usb_bulk_msg(gm12u320->udev,
381 		usb_sndbulkpipe(gm12u320->udev, DATA_SND_EPT),
382 		gm12u320->cmd_buf, CMD_SIZE, &len, CMD_TIMEOUT);
383 	if (ret || len != CMD_SIZE)
384 		goto err;
385 
386 	/* Read status */
387 	ret = usb_bulk_msg(gm12u320->udev,
388 		usb_rcvbulkpipe(gm12u320->udev, DATA_RCV_EPT),
389 		gm12u320->cmd_buf, READ_STATUS_SIZE, &len,
390 		gm12u320->fb_update.draw_status_timeout);
391 	if (ret || len != READ_STATUS_SIZE)
392 		goto err;
393 
394 	gm12u320->fb_update.draw_status_timeout = CMD_TIMEOUT;
395 	gm12u320->fb_update.frame = !gm12u320->fb_update.frame;
396 
397 	/*
398 	 * We must draw a frame every 2s otherwise the projector
399 	 * switches back to showing its logo.
400 	 */
401 	queue_delayed_work(system_long_wq, &gm12u320->fb_update.work,
402 			   msecs_to_jiffies(IDLE_TIMEOUT));
403 
404 	return;
405 err:
406 	/* Do not log errors caused by module unload or device unplug */
407 	if (ret != -ENODEV && ret != -ECONNRESET && ret != -ESHUTDOWN)
408 		GM12U320_ERR("Frame update error: %d\n", ret);
409 }
410 
gm12u320_fb_mark_dirty(struct drm_framebuffer *fb, struct drm_rect *dirty)411 static void gm12u320_fb_mark_dirty(struct drm_framebuffer *fb,
412 				   struct drm_rect *dirty)
413 {
414 	struct gm12u320_device *gm12u320 = to_gm12u320(fb->dev);
415 	struct drm_framebuffer *old_fb = NULL;
416 	bool wakeup = false;
417 
418 	mutex_lock(&gm12u320->fb_update.lock);
419 
420 	if (gm12u320->fb_update.fb != fb) {
421 		old_fb = gm12u320->fb_update.fb;
422 		drm_framebuffer_get(fb);
423 		gm12u320->fb_update.fb = fb;
424 		gm12u320->fb_update.rect = *dirty;
425 		wakeup = true;
426 	} else {
427 		struct drm_rect *rect = &gm12u320->fb_update.rect;
428 
429 		rect->x1 = min(rect->x1, dirty->x1);
430 		rect->y1 = min(rect->y1, dirty->y1);
431 		rect->x2 = max(rect->x2, dirty->x2);
432 		rect->y2 = max(rect->y2, dirty->y2);
433 	}
434 
435 	mutex_unlock(&gm12u320->fb_update.lock);
436 
437 	if (wakeup)
438 		mod_delayed_work(system_long_wq, &gm12u320->fb_update.work, 0);
439 
440 	if (old_fb)
441 		drm_framebuffer_put(old_fb);
442 }
443 
gm12u320_stop_fb_update(struct gm12u320_device *gm12u320)444 static void gm12u320_stop_fb_update(struct gm12u320_device *gm12u320)
445 {
446 	struct drm_framebuffer *old_fb;
447 
448 	cancel_delayed_work_sync(&gm12u320->fb_update.work);
449 
450 	mutex_lock(&gm12u320->fb_update.lock);
451 	old_fb = gm12u320->fb_update.fb;
452 	gm12u320->fb_update.fb = NULL;
453 	mutex_unlock(&gm12u320->fb_update.lock);
454 
455 	drm_framebuffer_put(old_fb);
456 }
457 
gm12u320_set_ecomode(struct gm12u320_device *gm12u320)458 static int gm12u320_set_ecomode(struct gm12u320_device *gm12u320)
459 {
460 	return gm12u320_misc_request(gm12u320, MISC_REQ_GET_SET_ECO_A,
461 				     MISC_REQ_GET_SET_ECO_B, 0x01 /* set */,
462 				     eco_mode ? 0x01 : 0x00, 0x00, 0x01);
463 }
464 
465 /* ------------------------------------------------------------------ */
466 /* gm12u320 connector						      */
467 
468 /*
469  * We use fake EDID info so that userspace know that it is dealing with
470  * an Acer projector, rather then listing this as an "unknown" monitor.
471  * Note this assumes this driver is only ever used with the Acer C120, if we
472  * add support for other devices the vendor and model should be parameterized.
473  */
474 static struct edid gm12u320_edid = {
475 	.header		= { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 },
476 	.mfg_id		= { 0x04, 0x72 },	/* "ACR" */
477 	.prod_code	= { 0x20, 0xc1 },	/* C120h */
478 	.serial		= 0xaa55aa55,
479 	.mfg_week	= 1,
480 	.mfg_year	= 16,
481 	.version	= 1,			/* EDID 1.3 */
482 	.revision	= 3,			/* EDID 1.3 */
483 	.input		= 0x08,			/* Analog input */
484 	.features	= 0x0a,			/* Pref timing in DTD 1 */
485 	.standard_timings = { { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
486 			      { 1, 1 }, { 1, 1 }, { 1, 1 }, { 1, 1 } },
487 	.detailed_timings = { {
488 		.pixel_clock = 3383,
489 		/* hactive = 848, hblank = 256 */
490 		.data.pixel_data.hactive_lo = 0x50,
491 		.data.pixel_data.hblank_lo = 0x00,
492 		.data.pixel_data.hactive_hblank_hi = 0x31,
493 		/* vactive = 480, vblank = 28 */
494 		.data.pixel_data.vactive_lo = 0xe0,
495 		.data.pixel_data.vblank_lo = 0x1c,
496 		.data.pixel_data.vactive_vblank_hi = 0x10,
497 		/* hsync offset 40 pw 128, vsync offset 1 pw 4 */
498 		.data.pixel_data.hsync_offset_lo = 0x28,
499 		.data.pixel_data.hsync_pulse_width_lo = 0x80,
500 		.data.pixel_data.vsync_offset_pulse_width_lo = 0x14,
501 		.data.pixel_data.hsync_vsync_offset_pulse_width_hi = 0x00,
502 		/* Digital separate syncs, hsync+, vsync+ */
503 		.data.pixel_data.misc = 0x1e,
504 	}, {
505 		.pixel_clock = 0,
506 		.data.other_data.type = 0xfd, /* Monitor ranges */
507 		.data.other_data.data.range.min_vfreq = 59,
508 		.data.other_data.data.range.max_vfreq = 61,
509 		.data.other_data.data.range.min_hfreq_khz = 29,
510 		.data.other_data.data.range.max_hfreq_khz = 32,
511 		.data.other_data.data.range.pixel_clock_mhz = 4, /* 40 MHz */
512 		.data.other_data.data.range.flags = 0,
513 		.data.other_data.data.range.formula.cvt = {
514 			0xa0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 },
515 	}, {
516 		.pixel_clock = 0,
517 		.data.other_data.type = 0xfc, /* Model string */
518 		.data.other_data.data.str.str = {
519 			'P', 'r', 'o', 'j', 'e', 'c', 't', 'o', 'r', '\n',
520 			' ', ' ',  ' ' },
521 	}, {
522 		.pixel_clock = 0,
523 		.data.other_data.type = 0xfe, /* Unspecified text / padding */
524 		.data.other_data.data.str.str = {
525 			'\n', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
526 			' ', ' ',  ' ' },
527 	} },
528 	.checksum = 0x13,
529 };
530 
gm12u320_conn_get_modes(struct drm_connector *connector)531 static int gm12u320_conn_get_modes(struct drm_connector *connector)
532 {
533 	drm_connector_update_edid_property(connector, &gm12u320_edid);
534 	return drm_add_edid_modes(connector, &gm12u320_edid);
535 }
536 
537 static const struct drm_connector_helper_funcs gm12u320_conn_helper_funcs = {
538 	.get_modes = gm12u320_conn_get_modes,
539 };
540 
541 static const struct drm_connector_funcs gm12u320_conn_funcs = {
542 	.fill_modes = drm_helper_probe_single_connector_modes,
543 	.destroy = drm_connector_cleanup,
544 	.reset = drm_atomic_helper_connector_reset,
545 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
546 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
547 };
548 
gm12u320_conn_init(struct gm12u320_device *gm12u320)549 static int gm12u320_conn_init(struct gm12u320_device *gm12u320)
550 {
551 	drm_connector_helper_add(&gm12u320->conn, &gm12u320_conn_helper_funcs);
552 	return drm_connector_init(&gm12u320->dev, &gm12u320->conn,
553 				  &gm12u320_conn_funcs, DRM_MODE_CONNECTOR_VGA);
554 }
555 
556 /* ------------------------------------------------------------------ */
557 /* gm12u320 (simple) display pipe				      */
558 
gm12u320_pipe_enable(struct drm_simple_display_pipe *pipe, struct drm_crtc_state *crtc_state, struct drm_plane_state *plane_state)559 static void gm12u320_pipe_enable(struct drm_simple_display_pipe *pipe,
560 				 struct drm_crtc_state *crtc_state,
561 				 struct drm_plane_state *plane_state)
562 {
563 	struct drm_rect rect = { 0, 0, GM12U320_USER_WIDTH, GM12U320_HEIGHT };
564 	struct gm12u320_device *gm12u320 = to_gm12u320(pipe->crtc.dev);
565 
566 	gm12u320->fb_update.draw_status_timeout = FIRST_FRAME_TIMEOUT;
567 	gm12u320_fb_mark_dirty(plane_state->fb, &rect);
568 }
569 
gm12u320_pipe_disable(struct drm_simple_display_pipe *pipe)570 static void gm12u320_pipe_disable(struct drm_simple_display_pipe *pipe)
571 {
572 	struct gm12u320_device *gm12u320 = to_gm12u320(pipe->crtc.dev);
573 
574 	gm12u320_stop_fb_update(gm12u320);
575 }
576 
gm12u320_pipe_update(struct drm_simple_display_pipe *pipe, struct drm_plane_state *old_state)577 static void gm12u320_pipe_update(struct drm_simple_display_pipe *pipe,
578 				 struct drm_plane_state *old_state)
579 {
580 	struct drm_plane_state *state = pipe->plane.state;
581 	struct drm_rect rect;
582 
583 	if (drm_atomic_helper_damage_merged(old_state, state, &rect))
584 		gm12u320_fb_mark_dirty(pipe->plane.state->fb, &rect);
585 }
586 
587 static const struct drm_simple_display_pipe_funcs gm12u320_pipe_funcs = {
588 	.enable	    = gm12u320_pipe_enable,
589 	.disable    = gm12u320_pipe_disable,
590 	.update	    = gm12u320_pipe_update,
591 };
592 
593 static const uint32_t gm12u320_pipe_formats[] = {
594 	DRM_FORMAT_XRGB8888,
595 };
596 
597 static const uint64_t gm12u320_pipe_modifiers[] = {
598 	DRM_FORMAT_MOD_LINEAR,
599 	DRM_FORMAT_MOD_INVALID
600 };
601 
602 /*
603  * FIXME: Dma-buf sharing requires DMA support by the importing device.
604  *        This function is a workaround to make USB devices work as well.
605  *        See todo.rst for how to fix the issue in the dma-buf framework.
606  */
gm12u320_gem_prime_import(struct drm_device *dev, struct dma_buf *dma_buf)607 static struct drm_gem_object *gm12u320_gem_prime_import(struct drm_device *dev,
608 							struct dma_buf *dma_buf)
609 {
610 	struct gm12u320_device *gm12u320 = to_gm12u320(dev);
611 
612 	if (!gm12u320->dmadev)
613 		return ERR_PTR(-ENODEV);
614 
615 	return drm_gem_prime_import_dev(dev, dma_buf, gm12u320->dmadev);
616 }
617 
618 DEFINE_DRM_GEM_FOPS(gm12u320_fops);
619 
620 static struct drm_driver gm12u320_drm_driver = {
621 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
622 
623 	.name		 = DRIVER_NAME,
624 	.desc		 = DRIVER_DESC,
625 	.date		 = DRIVER_DATE,
626 	.major		 = DRIVER_MAJOR,
627 	.minor		 = DRIVER_MINOR,
628 
629 	.fops		 = &gm12u320_fops,
630 	DRM_GEM_SHMEM_DRIVER_OPS,
631 	.gem_prime_import = gm12u320_gem_prime_import,
632 };
633 
634 static const struct drm_mode_config_funcs gm12u320_mode_config_funcs = {
635 	.fb_create = drm_gem_fb_create_with_dirty,
636 	.atomic_check = drm_atomic_helper_check,
637 	.atomic_commit = drm_atomic_helper_commit,
638 };
639 
gm12u320_usb_probe(struct usb_interface *interface, const struct usb_device_id *id)640 static int gm12u320_usb_probe(struct usb_interface *interface,
641 			      const struct usb_device_id *id)
642 {
643 	struct gm12u320_device *gm12u320;
644 	struct drm_device *dev;
645 	int ret;
646 
647 	/*
648 	 * The gm12u320 presents itself to the system as 2 usb mass-storage
649 	 * interfaces, we only care about / need the first one.
650 	 */
651 	if (interface->cur_altsetting->desc.bInterfaceNumber != 0)
652 		return -ENODEV;
653 
654 	gm12u320 = devm_drm_dev_alloc(&interface->dev, &gm12u320_drm_driver,
655 				      struct gm12u320_device, dev);
656 	if (IS_ERR(gm12u320))
657 		return PTR_ERR(gm12u320);
658 	dev = &gm12u320->dev;
659 
660 	gm12u320->dmadev = usb_intf_get_dma_device(to_usb_interface(dev->dev));
661 	if (!gm12u320->dmadev)
662 		drm_warn(dev, "buffer sharing not supported"); /* not an error */
663 
664 	gm12u320->udev = interface_to_usbdev(interface);
665 	INIT_DELAYED_WORK(&gm12u320->fb_update.work, gm12u320_fb_update_work);
666 	mutex_init(&gm12u320->fb_update.lock);
667 
668 	ret = drmm_mode_config_init(dev);
669 	if (ret)
670 		goto err_put_device;
671 
672 	dev->mode_config.min_width = GM12U320_USER_WIDTH;
673 	dev->mode_config.max_width = GM12U320_USER_WIDTH;
674 	dev->mode_config.min_height = GM12U320_HEIGHT;
675 	dev->mode_config.max_height = GM12U320_HEIGHT;
676 	dev->mode_config.funcs = &gm12u320_mode_config_funcs;
677 
678 	ret = gm12u320_usb_alloc(gm12u320);
679 	if (ret)
680 		goto err_put_device;
681 
682 	ret = gm12u320_set_ecomode(gm12u320);
683 	if (ret)
684 		goto err_put_device;
685 
686 	ret = gm12u320_conn_init(gm12u320);
687 	if (ret)
688 		goto err_put_device;
689 
690 	ret = drm_simple_display_pipe_init(&gm12u320->dev,
691 					   &gm12u320->pipe,
692 					   &gm12u320_pipe_funcs,
693 					   gm12u320_pipe_formats,
694 					   ARRAY_SIZE(gm12u320_pipe_formats),
695 					   gm12u320_pipe_modifiers,
696 					   &gm12u320->conn);
697 	if (ret)
698 		goto err_put_device;
699 
700 	drm_mode_config_reset(dev);
701 
702 	usb_set_intfdata(interface, dev);
703 	ret = drm_dev_register(dev, 0);
704 	if (ret)
705 		goto err_put_device;
706 
707 	drm_fbdev_generic_setup(dev, 0);
708 
709 	return 0;
710 
711 err_put_device:
712 	put_device(gm12u320->dmadev);
713 	return ret;
714 }
715 
gm12u320_usb_disconnect(struct usb_interface *interface)716 static void gm12u320_usb_disconnect(struct usb_interface *interface)
717 {
718 	struct drm_device *dev = usb_get_intfdata(interface);
719 	struct gm12u320_device *gm12u320 = to_gm12u320(dev);
720 
721 	put_device(gm12u320->dmadev);
722 	gm12u320->dmadev = NULL;
723 	drm_dev_unplug(dev);
724 	drm_atomic_helper_shutdown(dev);
725 }
726 
gm12u320_suspend(struct usb_interface *interface, pm_message_t message)727 static __maybe_unused int gm12u320_suspend(struct usb_interface *interface,
728 					   pm_message_t message)
729 {
730 	struct drm_device *dev = usb_get_intfdata(interface);
731 
732 	return drm_mode_config_helper_suspend(dev);
733 }
734 
gm12u320_resume(struct usb_interface *interface)735 static __maybe_unused int gm12u320_resume(struct usb_interface *interface)
736 {
737 	struct drm_device *dev = usb_get_intfdata(interface);
738 	struct gm12u320_device *gm12u320 = to_gm12u320(dev);
739 
740 	gm12u320_set_ecomode(gm12u320);
741 
742 	return drm_mode_config_helper_resume(dev);
743 }
744 
745 static const struct usb_device_id id_table[] = {
746 	{ USB_DEVICE(0x1de1, 0xc102) },
747 	{},
748 };
749 MODULE_DEVICE_TABLE(usb, id_table);
750 
751 static struct usb_driver gm12u320_usb_driver = {
752 	.name = "gm12u320",
753 	.probe = gm12u320_usb_probe,
754 	.disconnect = gm12u320_usb_disconnect,
755 	.id_table = id_table,
756 #ifdef CONFIG_PM
757 	.suspend = gm12u320_suspend,
758 	.resume = gm12u320_resume,
759 	.reset_resume = gm12u320_resume,
760 #endif
761 };
762 
763 module_usb_driver(gm12u320_usb_driver);
764 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
765 MODULE_LICENSE("GPL");
766