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