1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Apple Touch Bar DRM Driver
4 *
5 * Copyright (c) 2023 Kerem Karabay <kekrby@gmail.com>
6 */
7
8 #include <linux/align.h>
9 #include <linux/array_size.h>
10 #include <linux/bitops.h>
11 #include <linux/bug.h>
12 #include <linux/container_of.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/overflow.h>
16 #include <linux/slab.h>
17 #include <linux/types.h>
18 #include <linux/unaligned.h>
19 #include <linux/usb.h>
20
21 #include <drm/drm_atomic.h>
22 #include <drm/drm_atomic_helper.h>
23 #include <drm/drm_crtc.h>
24 #include <drm/drm_damage_helper.h>
25 #include <drm/drm_drv.h>
26 #include <drm/drm_encoder.h>
27 #include <drm/drm_format_helper.h>
28 #include <drm/drm_fourcc.h>
29 #include <drm/drm_framebuffer.h>
30 #include <drm/drm_gem_atomic_helper.h>
31 #include <drm/drm_gem_framebuffer_helper.h>
32 #include <drm/drm_gem_shmem_helper.h>
33 #include <drm/drm_plane.h>
34 #include <drm/drm_print.h>
35 #include <drm/drm_probe_helper.h>
36
37 #define APPLETBDRM_PIXEL_FORMAT cpu_to_le32(0x52474241) /* RGBA, the actual format is BGR888 */
38 #define APPLETBDRM_BITS_PER_PIXEL 24
39
40 #define APPLETBDRM_MSG_CLEAR_DISPLAY cpu_to_le32(0x434c5244) /* CLRD */
41 #define APPLETBDRM_MSG_GET_INFORMATION cpu_to_le32(0x47494e46) /* GINF */
42 #define APPLETBDRM_MSG_UPDATE_COMPLETE cpu_to_le32(0x5544434c) /* UDCL */
43 #define APPLETBDRM_MSG_SIGNAL_READINESS cpu_to_le32(0x52454459) /* REDY */
44
45 #define APPLETBDRM_BULK_MSG_TIMEOUT 1000
46
47 #define drm_to_adev(_drm) container_of(_drm, struct appletbdrm_device, drm)
48 #define adev_to_udev(adev) interface_to_usbdev(to_usb_interface((adev)->drm.dev))
49
50 struct appletbdrm_msg_request_header {
51 __le16 unk_00;
52 __le16 unk_02;
53 __le32 unk_04;
54 __le32 unk_08;
55 __le32 size;
56 } __packed;
57
58 struct appletbdrm_msg_response_header {
59 u8 unk_00[16];
60 __le32 msg;
61 } __packed;
62
63 struct appletbdrm_msg_simple_request {
64 struct appletbdrm_msg_request_header header;
65 __le32 msg;
66 u8 unk_14[8];
67 __le32 size;
68 } __packed;
69
70 struct appletbdrm_msg_information {
71 struct appletbdrm_msg_response_header header;
72 u8 unk_14[12];
73 __le32 width;
74 __le32 height;
75 u8 bits_per_pixel;
76 __le32 bytes_per_row;
77 __le32 orientation;
78 __le32 bitmap_info;
79 __le32 pixel_format;
80 __le32 width_inches; /* floating point */
81 __le32 height_inches; /* floating point */
82 } __packed;
83
84 struct appletbdrm_frame {
85 __le16 begin_x;
86 __le16 begin_y;
87 __le16 width;
88 __le16 height;
89 __le32 buf_size;
90 u8 buf[];
91 } __packed;
92
93 struct appletbdrm_fb_request_footer {
94 u8 unk_00[12];
95 __le32 unk_0c;
96 u8 unk_10[12];
97 __le32 unk_1c;
98 __le64 timestamp;
99 u8 unk_28[12];
100 __le32 unk_34;
101 u8 unk_38[20];
102 __le32 unk_4c;
103 } __packed;
104
105 struct appletbdrm_fb_request {
106 struct appletbdrm_msg_request_header header;
107 __le16 unk_10;
108 u8 msg_id;
109 u8 unk_13[29];
110 /*
111 * Contents of `data`:
112 * - struct appletbdrm_frame frames[];
113 * - struct appletbdrm_fb_request_footer footer;
114 * - padding to make the total size a multiple of 16
115 */
116 u8 data[];
117 } __packed;
118
119 struct appletbdrm_fb_request_response {
120 struct appletbdrm_msg_response_header header;
121 u8 unk_14[12];
122 __le64 timestamp;
123 } __packed;
124
125 struct appletbdrm_device {
126 unsigned int in_ep;
127 unsigned int out_ep;
128
129 unsigned int width;
130 unsigned int height;
131
132 struct drm_device drm;
133 struct drm_display_mode mode;
134 struct drm_connector connector;
135 struct drm_plane primary_plane;
136 struct drm_crtc crtc;
137 struct drm_encoder encoder;
138 };
139
140 struct appletbdrm_plane_state {
141 struct drm_shadow_plane_state base;
142 struct appletbdrm_fb_request *request;
143 struct appletbdrm_fb_request_response *response;
144 size_t request_size;
145 size_t frames_size;
146 };
147
to_appletbdrm_plane_state(struct drm_plane_state * state)148 static inline struct appletbdrm_plane_state *to_appletbdrm_plane_state(struct drm_plane_state *state)
149 {
150 return container_of(state, struct appletbdrm_plane_state, base.base);
151 }
152
appletbdrm_send_request(struct appletbdrm_device * adev,struct appletbdrm_msg_request_header * request,size_t size)153 static int appletbdrm_send_request(struct appletbdrm_device *adev,
154 struct appletbdrm_msg_request_header *request, size_t size)
155 {
156 struct usb_device *udev = adev_to_udev(adev);
157 struct drm_device *drm = &adev->drm;
158 int ret, actual_size;
159
160 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, adev->out_ep),
161 request, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT);
162 if (ret) {
163 drm_err(drm, "Failed to send message (%d)\n", ret);
164 return ret;
165 }
166
167 if (actual_size != size) {
168 drm_err(drm, "Actual size (%d) doesn't match expected size (%zu)\n",
169 actual_size, size);
170 return -EIO;
171 }
172
173 return 0;
174 }
175
appletbdrm_read_response(struct appletbdrm_device * adev,struct appletbdrm_msg_response_header * response,size_t size,__le32 expected_response)176 static int appletbdrm_read_response(struct appletbdrm_device *adev,
177 struct appletbdrm_msg_response_header *response,
178 size_t size, __le32 expected_response)
179 {
180 struct usb_device *udev = adev_to_udev(adev);
181 struct drm_device *drm = &adev->drm;
182 int ret, actual_size;
183 bool readiness_signal_received = false;
184
185 retry:
186 ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, adev->in_ep),
187 response, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT);
188 if (ret) {
189 drm_err(drm, "Failed to read response (%d)\n", ret);
190 return ret;
191 }
192
193 /*
194 * The device responds to the first request sent in a particular
195 * timeframe after the USB device configuration is set with a readiness
196 * signal, in which case the response should be read again
197 */
198 if (response->msg == APPLETBDRM_MSG_SIGNAL_READINESS) {
199 if (!readiness_signal_received) {
200 readiness_signal_received = true;
201 goto retry;
202 }
203
204 drm_err(drm, "Encountered unexpected readiness signal\n");
205 return -EINTR;
206 }
207
208 if (actual_size != size) {
209 drm_err(drm, "Actual size (%d) doesn't match expected size (%zu)\n",
210 actual_size, size);
211 return -EBADMSG;
212 }
213
214 if (response->msg != expected_response) {
215 drm_err(drm, "Unexpected response from device (expected %p4cl found %p4cl)\n",
216 &expected_response, &response->msg);
217 return -EIO;
218 }
219
220 return 0;
221 }
222
appletbdrm_send_msg(struct appletbdrm_device * adev,__le32 msg)223 static int appletbdrm_send_msg(struct appletbdrm_device *adev, __le32 msg)
224 {
225 struct appletbdrm_msg_simple_request *request;
226 int ret;
227
228 request = kzalloc_obj(*request);
229 if (!request)
230 return -ENOMEM;
231
232 request->header.unk_00 = cpu_to_le16(2);
233 request->header.unk_02 = cpu_to_le16(0x1512);
234 request->header.size = cpu_to_le32(sizeof(*request) - sizeof(request->header));
235 request->msg = msg;
236 request->size = request->header.size;
237
238 ret = appletbdrm_send_request(adev, &request->header, sizeof(*request));
239
240 kfree(request);
241
242 return ret;
243 }
244
appletbdrm_clear_display(struct appletbdrm_device * adev)245 static int appletbdrm_clear_display(struct appletbdrm_device *adev)
246 {
247 return appletbdrm_send_msg(adev, APPLETBDRM_MSG_CLEAR_DISPLAY);
248 }
249
appletbdrm_signal_readiness(struct appletbdrm_device * adev)250 static int appletbdrm_signal_readiness(struct appletbdrm_device *adev)
251 {
252 return appletbdrm_send_msg(adev, APPLETBDRM_MSG_SIGNAL_READINESS);
253 }
254
appletbdrm_get_information(struct appletbdrm_device * adev)255 static int appletbdrm_get_information(struct appletbdrm_device *adev)
256 {
257 struct appletbdrm_msg_information *info;
258 struct drm_device *drm = &adev->drm;
259 u8 bits_per_pixel;
260 __le32 pixel_format;
261 int ret;
262
263 info = kzalloc_obj(*info);
264 if (!info)
265 return -ENOMEM;
266
267 ret = appletbdrm_send_msg(adev, APPLETBDRM_MSG_GET_INFORMATION);
268 if (ret)
269 return ret;
270
271 ret = appletbdrm_read_response(adev, &info->header, sizeof(*info),
272 APPLETBDRM_MSG_GET_INFORMATION);
273 if (ret)
274 goto free_info;
275
276 bits_per_pixel = info->bits_per_pixel;
277 pixel_format = get_unaligned(&info->pixel_format);
278
279 adev->width = get_unaligned_le32(&info->width);
280 adev->height = get_unaligned_le32(&info->height);
281
282 if (bits_per_pixel != APPLETBDRM_BITS_PER_PIXEL) {
283 drm_err(drm, "Encountered unexpected bits per pixel value (%d)\n", bits_per_pixel);
284 ret = -EINVAL;
285 goto free_info;
286 }
287
288 if (pixel_format != APPLETBDRM_PIXEL_FORMAT) {
289 drm_err(drm, "Encountered unknown pixel format (%p4cl)\n", &pixel_format);
290 ret = -EINVAL;
291 goto free_info;
292 }
293
294 free_info:
295 kfree(info);
296
297 return ret;
298 }
299
rect_size(struct drm_rect * rect)300 static u32 rect_size(struct drm_rect *rect)
301 {
302 return drm_rect_width(rect) * drm_rect_height(rect) *
303 (BITS_TO_BYTES(APPLETBDRM_BITS_PER_PIXEL));
304 }
305
appletbdrm_connector_helper_get_modes(struct drm_connector * connector)306 static int appletbdrm_connector_helper_get_modes(struct drm_connector *connector)
307 {
308 struct appletbdrm_device *adev = drm_to_adev(connector->dev);
309
310 return drm_connector_helper_get_modes_fixed(connector, &adev->mode);
311 }
312
313 static const u32 appletbdrm_primary_plane_formats[] = {
314 DRM_FORMAT_BGR888,
315 DRM_FORMAT_XRGB8888, /* emulated */
316 };
317
appletbdrm_primary_plane_helper_begin_fb_access(struct drm_plane * plane,struct drm_plane_state * new_plane_state)318 static int appletbdrm_primary_plane_helper_begin_fb_access(struct drm_plane *plane,
319 struct drm_plane_state *new_plane_state)
320 {
321 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(new_plane_state);
322 size_t frames_size = 0;
323 struct drm_atomic_helper_damage_iter iter;
324 struct drm_rect damage;
325 size_t request_size;
326
327 drm_atomic_helper_damage_iter_init(&iter, plane->state, new_plane_state);
328 drm_atomic_for_each_plane_damage(&iter, &damage) {
329 frames_size += struct_size((struct appletbdrm_frame *)0, buf, rect_size(&damage));
330 }
331
332 if (!frames_size)
333 return 0;
334
335 request_size = ALIGN(sizeof(struct appletbdrm_fb_request) +
336 frames_size +
337 sizeof(struct appletbdrm_fb_request_footer), 16);
338
339 appletbdrm_state->request = kvzalloc(request_size, GFP_KERNEL);
340
341 if (!appletbdrm_state->request)
342 return -ENOMEM;
343
344 appletbdrm_state->response = kzalloc_obj(*appletbdrm_state->response);
345
346 if (!appletbdrm_state->response)
347 return -ENOMEM;
348
349 appletbdrm_state->request_size = request_size;
350 appletbdrm_state->frames_size = frames_size;
351
352 return drm_gem_begin_shadow_fb_access(plane, new_plane_state);
353 }
354
appletbdrm_primary_plane_helper_atomic_check(struct drm_plane * plane,struct drm_atomic_commit * state)355 static int appletbdrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
356 struct drm_atomic_commit *state)
357 {
358 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
359 struct drm_crtc *new_crtc = new_plane_state->crtc;
360 struct drm_crtc_state *new_crtc_state = NULL;
361 int ret;
362
363 if (new_crtc)
364 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
365
366 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
367 DRM_PLANE_NO_SCALING,
368 DRM_PLANE_NO_SCALING,
369 false, false);
370 if (ret)
371 return ret;
372 else if (!new_plane_state->visible)
373 return 0;
374
375 return 0;
376 }
377
appletbdrm_flush_damage(struct appletbdrm_device * adev,struct drm_plane_state * old_state,struct drm_plane_state * state)378 static int appletbdrm_flush_damage(struct appletbdrm_device *adev,
379 struct drm_plane_state *old_state,
380 struct drm_plane_state *state)
381 {
382 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(state);
383 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
384 struct appletbdrm_fb_request_response *response = appletbdrm_state->response;
385 struct appletbdrm_fb_request_footer *footer;
386 struct drm_atomic_helper_damage_iter iter;
387 struct drm_framebuffer *fb = state->fb;
388 struct appletbdrm_fb_request *request = appletbdrm_state->request;
389 struct drm_device *drm = &adev->drm;
390 struct appletbdrm_frame *frame;
391 u64 timestamp = ktime_get_ns();
392 struct drm_rect damage;
393 size_t frames_size = appletbdrm_state->frames_size;
394 size_t request_size = appletbdrm_state->request_size;
395 int ret;
396
397 if (!frames_size)
398 return 0;
399
400 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
401 if (ret) {
402 drm_err(drm, "Failed to start CPU framebuffer access (%d)\n", ret);
403 goto end_fb_cpu_access;
404 }
405
406 request->header.unk_00 = cpu_to_le16(2);
407 request->header.unk_02 = cpu_to_le16(0x12);
408 request->header.unk_04 = cpu_to_le32(9);
409 request->header.size = cpu_to_le32(request_size - sizeof(request->header));
410 request->unk_10 = cpu_to_le16(1);
411 request->msg_id = timestamp;
412
413 frame = (struct appletbdrm_frame *)request->data;
414
415 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
416 drm_atomic_for_each_plane_damage(&iter, &damage) {
417 struct drm_rect dst_clip = state->dst;
418 struct iosys_map dst = IOSYS_MAP_INIT_VADDR(frame->buf);
419 u32 buf_size = rect_size(&damage);
420
421 if (!drm_rect_intersect(&dst_clip, &damage))
422 continue;
423
424 /*
425 * The coordinates need to be translated to the coordinate
426 * system the device expects, see the comment in
427 * appletbdrm_setup_mode_config
428 */
429 frame->begin_x = cpu_to_le16(damage.y1);
430 frame->begin_y = cpu_to_le16(adev->height - damage.x2);
431 frame->width = cpu_to_le16(drm_rect_height(&damage));
432 frame->height = cpu_to_le16(drm_rect_width(&damage));
433 frame->buf_size = cpu_to_le32(buf_size);
434
435 switch (fb->format->format) {
436 case DRM_FORMAT_XRGB8888:
437 drm_fb_xrgb8888_to_bgr888(&dst, NULL, &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state);
438 break;
439 default:
440 drm_fb_memcpy(&dst, NULL, &shadow_plane_state->data[0], fb, &damage);
441 break;
442 }
443
444 frame = (void *)frame + struct_size(frame, buf, buf_size);
445 }
446
447 footer = (struct appletbdrm_fb_request_footer *)&request->data[frames_size];
448
449 footer->unk_0c = cpu_to_le32(0xfffe);
450 footer->unk_1c = cpu_to_le32(0x80001);
451 footer->unk_34 = cpu_to_le32(0x80002);
452 footer->unk_4c = cpu_to_le32(0xffff);
453 footer->timestamp = cpu_to_le64(timestamp);
454
455 ret = appletbdrm_send_request(adev, &request->header, request_size);
456 if (ret)
457 goto end_fb_cpu_access;
458
459 ret = appletbdrm_read_response(adev, &response->header, sizeof(*response),
460 APPLETBDRM_MSG_UPDATE_COMPLETE);
461 if (ret)
462 goto end_fb_cpu_access;
463
464 if (response->timestamp != footer->timestamp) {
465 drm_err(drm, "Response timestamp (%llu) doesn't match request timestamp (%llu)\n",
466 le64_to_cpu(response->timestamp), timestamp);
467 goto end_fb_cpu_access;
468 }
469
470 end_fb_cpu_access:
471 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
472
473 return ret;
474 }
475
appletbdrm_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_commit * old_state)476 static void appletbdrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
477 struct drm_atomic_commit *old_state)
478 {
479 struct appletbdrm_device *adev = drm_to_adev(plane->dev);
480 struct drm_device *drm = plane->dev;
481 struct drm_plane_state *plane_state = plane->state;
482 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane);
483 int idx;
484
485 if (!drm_dev_enter(drm, &idx))
486 return;
487
488 appletbdrm_flush_damage(adev, old_plane_state, plane_state);
489
490 drm_dev_exit(idx);
491 }
492
appletbdrm_primary_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_commit * state)493 static void appletbdrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
494 struct drm_atomic_commit *state)
495 {
496 struct drm_device *dev = plane->dev;
497 struct appletbdrm_device *adev = drm_to_adev(dev);
498 int idx;
499
500 if (!drm_dev_enter(dev, &idx))
501 return;
502
503 appletbdrm_clear_display(adev);
504
505 drm_dev_exit(idx);
506 }
507
appletbdrm_primary_plane_reset(struct drm_plane * plane)508 static void appletbdrm_primary_plane_reset(struct drm_plane *plane)
509 {
510 struct appletbdrm_plane_state *appletbdrm_state;
511
512 WARN_ON(plane->state);
513
514 appletbdrm_state = kzalloc_obj(*appletbdrm_state);
515 if (!appletbdrm_state)
516 return;
517
518 __drm_gem_reset_shadow_plane(plane, &appletbdrm_state->base);
519 }
520
appletbdrm_primary_plane_duplicate_state(struct drm_plane * plane)521 static struct drm_plane_state *appletbdrm_primary_plane_duplicate_state(struct drm_plane *plane)
522 {
523 struct drm_shadow_plane_state *new_shadow_plane_state;
524 struct appletbdrm_plane_state *appletbdrm_state;
525
526 if (WARN_ON(!plane->state))
527 return NULL;
528
529 appletbdrm_state = kzalloc_obj(*appletbdrm_state);
530 if (!appletbdrm_state)
531 return NULL;
532
533 /* Request and response are not duplicated and are allocated in .atomic_check */
534 appletbdrm_state->request = NULL;
535 appletbdrm_state->response = NULL;
536
537 appletbdrm_state->request_size = 0;
538 appletbdrm_state->frames_size = 0;
539
540 new_shadow_plane_state = &appletbdrm_state->base;
541
542 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
543
544 return &new_shadow_plane_state->base;
545 }
546
appletbdrm_primary_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)547 static void appletbdrm_primary_plane_destroy_state(struct drm_plane *plane,
548 struct drm_plane_state *state)
549 {
550 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(state);
551
552 kvfree(appletbdrm_state->request);
553 kfree(appletbdrm_state->response);
554
555 __drm_gem_destroy_shadow_plane_state(&appletbdrm_state->base);
556
557 kfree(appletbdrm_state);
558 }
559
560 static const struct drm_plane_helper_funcs appletbdrm_primary_plane_helper_funcs = {
561 .begin_fb_access = appletbdrm_primary_plane_helper_begin_fb_access,
562 .end_fb_access = drm_gem_end_shadow_fb_access,
563 .atomic_check = appletbdrm_primary_plane_helper_atomic_check,
564 .atomic_update = appletbdrm_primary_plane_helper_atomic_update,
565 .atomic_disable = appletbdrm_primary_plane_helper_atomic_disable,
566 };
567
568 static const struct drm_plane_funcs appletbdrm_primary_plane_funcs = {
569 .update_plane = drm_atomic_helper_update_plane,
570 .disable_plane = drm_atomic_helper_disable_plane,
571 .reset = appletbdrm_primary_plane_reset,
572 .atomic_duplicate_state = appletbdrm_primary_plane_duplicate_state,
573 .atomic_destroy_state = appletbdrm_primary_plane_destroy_state,
574 .destroy = drm_plane_cleanup,
575 };
576
appletbdrm_crtc_helper_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)577 static enum drm_mode_status appletbdrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
578 const struct drm_display_mode *mode)
579 {
580 struct appletbdrm_device *adev = drm_to_adev(crtc->dev);
581
582 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &adev->mode);
583 }
584
585 static const struct drm_mode_config_funcs appletbdrm_mode_config_funcs = {
586 .fb_create = drm_gem_fb_create_with_dirty,
587 .atomic_check = drm_atomic_helper_check,
588 .atomic_commit = drm_atomic_helper_commit,
589 };
590
591 static const struct drm_connector_funcs appletbdrm_connector_funcs = {
592 .reset = drm_atomic_helper_connector_reset,
593 .destroy = drm_connector_cleanup,
594 .fill_modes = drm_helper_probe_single_connector_modes,
595 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
596 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
597 };
598
599 static const struct drm_connector_helper_funcs appletbdrm_connector_helper_funcs = {
600 .get_modes = appletbdrm_connector_helper_get_modes,
601 };
602
603 static const struct drm_crtc_helper_funcs appletbdrm_crtc_helper_funcs = {
604 .mode_valid = appletbdrm_crtc_helper_mode_valid,
605 };
606
607 static const struct drm_crtc_funcs appletbdrm_crtc_funcs = {
608 .reset = drm_atomic_helper_crtc_reset,
609 .destroy = drm_crtc_cleanup,
610 .set_config = drm_atomic_helper_set_config,
611 .page_flip = drm_atomic_helper_page_flip,
612 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
613 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
614 };
615
616 static const struct drm_encoder_funcs appletbdrm_encoder_funcs = {
617 .destroy = drm_encoder_cleanup,
618 };
619
620 DEFINE_DRM_GEM_FOPS(appletbdrm_drm_fops);
621
622 static const struct drm_driver appletbdrm_drm_driver = {
623 DRM_GEM_SHMEM_DRIVER_OPS,
624 .name = "appletbdrm",
625 .desc = "Apple Touch Bar DRM Driver",
626 .major = 1,
627 .minor = 0,
628 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
629 .fops = &appletbdrm_drm_fops,
630 };
631
appletbdrm_setup_mode_config(struct appletbdrm_device * adev)632 static int appletbdrm_setup_mode_config(struct appletbdrm_device *adev)
633 {
634 struct drm_connector *connector = &adev->connector;
635 struct drm_plane *primary_plane;
636 struct drm_crtc *crtc;
637 struct drm_encoder *encoder;
638 struct drm_device *drm = &adev->drm;
639 int ret;
640
641 ret = drmm_mode_config_init(drm);
642 if (ret) {
643 drm_err(drm, "Failed to initialize mode configuration\n");
644 return ret;
645 }
646
647 primary_plane = &adev->primary_plane;
648 ret = drm_universal_plane_init(drm, primary_plane, 0,
649 &appletbdrm_primary_plane_funcs,
650 appletbdrm_primary_plane_formats,
651 ARRAY_SIZE(appletbdrm_primary_plane_formats),
652 NULL,
653 DRM_PLANE_TYPE_PRIMARY, NULL);
654 if (ret) {
655 drm_err(drm, "Failed to initialize universal plane object\n");
656 return ret;
657 }
658
659 drm_plane_helper_add(primary_plane, &appletbdrm_primary_plane_helper_funcs);
660 drm_plane_enable_fb_damage_clips(primary_plane);
661
662 crtc = &adev->crtc;
663 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
664 &appletbdrm_crtc_funcs, NULL);
665 if (ret) {
666 drm_err(drm, "Failed to initialize CRTC object\n");
667 return ret;
668 }
669
670 drm_crtc_helper_add(crtc, &appletbdrm_crtc_helper_funcs);
671
672 encoder = &adev->encoder;
673 ret = drm_encoder_init(drm, encoder, &appletbdrm_encoder_funcs,
674 DRM_MODE_ENCODER_DAC, NULL);
675 if (ret) {
676 drm_err(drm, "Failed to initialize encoder\n");
677 return ret;
678 }
679
680 encoder->possible_crtcs = drm_crtc_mask(crtc);
681
682 /*
683 * The coordinate system used by the device is different from the
684 * coordinate system of the framebuffer in that the x and y axes are
685 * swapped, and that the y axis is inverted; so what the device reports
686 * as the height is actually the width of the framebuffer and vice
687 * versa.
688 */
689 drm->mode_config.max_width = max(adev->height, DRM_SHADOW_PLANE_MAX_WIDTH);
690 drm->mode_config.max_height = max(adev->width, DRM_SHADOW_PLANE_MAX_HEIGHT);
691 drm->mode_config.preferred_depth = APPLETBDRM_BITS_PER_PIXEL;
692 drm->mode_config.funcs = &appletbdrm_mode_config_funcs;
693
694 adev->mode = (struct drm_display_mode) {
695 DRM_MODE_INIT(60, adev->height, adev->width,
696 DRM_MODE_RES_MM(adev->height, 218),
697 DRM_MODE_RES_MM(adev->width, 218))
698 };
699
700 ret = drm_connector_init(drm, connector,
701 &appletbdrm_connector_funcs, DRM_MODE_CONNECTOR_USB);
702 if (ret) {
703 drm_err(drm, "Failed to initialize connector\n");
704 return ret;
705 }
706
707 drm_connector_helper_add(connector, &appletbdrm_connector_helper_funcs);
708
709 ret = drm_connector_set_panel_orientation(connector,
710 DRM_MODE_PANEL_ORIENTATION_RIGHT_UP);
711 if (ret) {
712 drm_err(drm, "Failed to set panel orientation\n");
713 return ret;
714 }
715
716 connector->display_info.non_desktop = true;
717 ret = drm_object_property_set_value(&connector->base,
718 drm->mode_config.non_desktop_property, true);
719 if (ret) {
720 drm_err(drm, "Failed to set non-desktop property\n");
721 return ret;
722 }
723
724 ret = drm_connector_attach_encoder(connector, encoder);
725
726 if (ret) {
727 drm_err(drm, "Failed to initialize simple display pipe\n");
728 return ret;
729 }
730
731 drm_mode_config_reset(drm);
732
733 return 0;
734 }
735
appletbdrm_probe(struct usb_interface * intf,const struct usb_device_id * id)736 static int appletbdrm_probe(struct usb_interface *intf,
737 const struct usb_device_id *id)
738 {
739 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
740 struct device *dev = &intf->dev;
741 struct appletbdrm_device *adev;
742 struct drm_device *drm = NULL;
743 struct device *dma_dev;
744 int ret;
745
746 ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL);
747 if (ret) {
748 drm_err(drm, "appletbdrm: Failed to find bulk endpoints\n");
749 return ret;
750 }
751
752 adev = devm_drm_dev_alloc(dev, &appletbdrm_drm_driver, struct appletbdrm_device, drm);
753 if (IS_ERR(adev))
754 return PTR_ERR(adev);
755
756 adev->in_ep = bulk_in->bEndpointAddress;
757 adev->out_ep = bulk_out->bEndpointAddress;
758
759 drm = &adev->drm;
760
761 usb_set_intfdata(intf, adev);
762
763 dma_dev = usb_intf_get_dma_device(intf);
764 if (dma_dev) {
765 drm_dev_set_dma_dev(drm, dma_dev);
766 put_device(dma_dev);
767 } else {
768 drm_warn(drm, "buffer sharing not supported"); /* not an error */
769 }
770
771 ret = appletbdrm_get_information(adev);
772 if (ret) {
773 drm_err(drm, "Failed to get display information\n");
774 return ret;
775 }
776
777 ret = appletbdrm_signal_readiness(adev);
778 if (ret) {
779 drm_err(drm, "Failed to signal readiness\n");
780 return ret;
781 }
782
783 ret = appletbdrm_setup_mode_config(adev);
784 if (ret) {
785 drm_err(drm, "Failed to setup mode config\n");
786 return ret;
787 }
788
789 ret = drm_dev_register(drm, 0);
790 if (ret) {
791 drm_err(drm, "Failed to register DRM device\n");
792 return ret;
793 }
794
795 ret = appletbdrm_clear_display(adev);
796 if (ret) {
797 drm_err(drm, "Failed to clear display\n");
798 return ret;
799 }
800
801 return 0;
802 }
803
appletbdrm_disconnect(struct usb_interface * intf)804 static void appletbdrm_disconnect(struct usb_interface *intf)
805 {
806 struct appletbdrm_device *adev = usb_get_intfdata(intf);
807 struct drm_device *drm = &adev->drm;
808
809 drm_dev_unplug(drm);
810 drm_atomic_helper_shutdown(drm);
811 }
812
appletbdrm_shutdown(struct usb_interface * intf)813 static void appletbdrm_shutdown(struct usb_interface *intf)
814 {
815 struct appletbdrm_device *adev = usb_get_intfdata(intf);
816
817 /*
818 * The framebuffer needs to be cleared on shutdown since its content
819 * persists across boots
820 */
821 drm_atomic_helper_shutdown(&adev->drm);
822 }
823
824 static const struct usb_device_id appletbdrm_usb_id_table[] = {
825 { USB_DEVICE_INTERFACE_CLASS(0x05ac, 0x8302, USB_CLASS_AUDIO_VIDEO) },
826 {}
827 };
828 MODULE_DEVICE_TABLE(usb, appletbdrm_usb_id_table);
829
830 static struct usb_driver appletbdrm_usb_driver = {
831 .name = "appletbdrm",
832 .probe = appletbdrm_probe,
833 .disconnect = appletbdrm_disconnect,
834 .shutdown = appletbdrm_shutdown,
835 .id_table = appletbdrm_usb_id_table,
836 };
837 module_usb_driver(appletbdrm_usb_driver);
838
839 MODULE_AUTHOR("Kerem Karabay <kekrby@gmail.com>");
840 MODULE_DESCRIPTION("Apple Touch Bar DRM Driver");
841 MODULE_LICENSE("GPL");
842