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(sizeof(*request), GFP_KERNEL);
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(sizeof(*info), GFP_KERNEL);
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_atomic_check(struct drm_plane * plane,struct drm_atomic_state * state)318 static int appletbdrm_primary_plane_helper_atomic_check(struct drm_plane *plane,
319 struct drm_atomic_state *state)
320 {
321 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
322 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
323 struct drm_crtc *new_crtc = new_plane_state->crtc;
324 struct drm_crtc_state *new_crtc_state = NULL;
325 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(new_plane_state);
326 struct drm_atomic_helper_damage_iter iter;
327 struct drm_rect damage;
328 size_t frames_size = 0;
329 size_t request_size;
330 int ret;
331
332 if (new_crtc)
333 new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc);
334
335 ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
336 DRM_PLANE_NO_SCALING,
337 DRM_PLANE_NO_SCALING,
338 false, false);
339 if (ret)
340 return ret;
341 else if (!new_plane_state->visible)
342 return 0;
343
344 drm_atomic_helper_damage_iter_init(&iter, old_plane_state, new_plane_state);
345 drm_atomic_for_each_plane_damage(&iter, &damage) {
346 frames_size += struct_size((struct appletbdrm_frame *)0, buf, rect_size(&damage));
347 }
348
349 if (!frames_size)
350 return 0;
351
352 request_size = ALIGN(sizeof(struct appletbdrm_fb_request) +
353 frames_size +
354 sizeof(struct appletbdrm_fb_request_footer), 16);
355
356 appletbdrm_state->request = kzalloc(request_size, GFP_KERNEL);
357
358 if (!appletbdrm_state->request)
359 return -ENOMEM;
360
361 appletbdrm_state->response = kzalloc(sizeof(*appletbdrm_state->response), GFP_KERNEL);
362
363 if (!appletbdrm_state->response)
364 return -ENOMEM;
365
366 appletbdrm_state->request_size = request_size;
367 appletbdrm_state->frames_size = frames_size;
368
369 return 0;
370 }
371
appletbdrm_flush_damage(struct appletbdrm_device * adev,struct drm_plane_state * old_state,struct drm_plane_state * state)372 static int appletbdrm_flush_damage(struct appletbdrm_device *adev,
373 struct drm_plane_state *old_state,
374 struct drm_plane_state *state)
375 {
376 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(state);
377 struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state);
378 struct appletbdrm_fb_request_response *response = appletbdrm_state->response;
379 struct appletbdrm_fb_request_footer *footer;
380 struct drm_atomic_helper_damage_iter iter;
381 struct drm_framebuffer *fb = state->fb;
382 struct appletbdrm_fb_request *request = appletbdrm_state->request;
383 struct drm_device *drm = &adev->drm;
384 struct appletbdrm_frame *frame;
385 u64 timestamp = ktime_get_ns();
386 struct drm_rect damage;
387 size_t frames_size = appletbdrm_state->frames_size;
388 size_t request_size = appletbdrm_state->request_size;
389 int ret;
390
391 if (!frames_size)
392 return 0;
393
394 ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE);
395 if (ret) {
396 drm_err(drm, "Failed to start CPU framebuffer access (%d)\n", ret);
397 goto end_fb_cpu_access;
398 }
399
400 request->header.unk_00 = cpu_to_le16(2);
401 request->header.unk_02 = cpu_to_le16(0x12);
402 request->header.unk_04 = cpu_to_le32(9);
403 request->header.size = cpu_to_le32(request_size - sizeof(request->header));
404 request->unk_10 = cpu_to_le16(1);
405 request->msg_id = timestamp;
406
407 frame = (struct appletbdrm_frame *)request->data;
408
409 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
410 drm_atomic_for_each_plane_damage(&iter, &damage) {
411 struct drm_rect dst_clip = state->dst;
412 struct iosys_map dst = IOSYS_MAP_INIT_VADDR(frame->buf);
413 u32 buf_size = rect_size(&damage);
414
415 if (!drm_rect_intersect(&dst_clip, &damage))
416 continue;
417
418 /*
419 * The coordinates need to be translated to the coordinate
420 * system the device expects, see the comment in
421 * appletbdrm_setup_mode_config
422 */
423 frame->begin_x = cpu_to_le16(damage.y1);
424 frame->begin_y = cpu_to_le16(adev->height - damage.x2);
425 frame->width = cpu_to_le16(drm_rect_height(&damage));
426 frame->height = cpu_to_le16(drm_rect_width(&damage));
427 frame->buf_size = cpu_to_le32(buf_size);
428
429 switch (fb->format->format) {
430 case DRM_FORMAT_XRGB8888:
431 drm_fb_xrgb8888_to_bgr888(&dst, NULL, &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state);
432 break;
433 default:
434 drm_fb_memcpy(&dst, NULL, &shadow_plane_state->data[0], fb, &damage);
435 break;
436 }
437
438 frame = (void *)frame + struct_size(frame, buf, buf_size);
439 }
440
441 footer = (struct appletbdrm_fb_request_footer *)&request->data[frames_size];
442
443 footer->unk_0c = cpu_to_le32(0xfffe);
444 footer->unk_1c = cpu_to_le32(0x80001);
445 footer->unk_34 = cpu_to_le32(0x80002);
446 footer->unk_4c = cpu_to_le32(0xffff);
447 footer->timestamp = cpu_to_le64(timestamp);
448
449 ret = appletbdrm_send_request(adev, &request->header, request_size);
450 if (ret)
451 goto end_fb_cpu_access;
452
453 ret = appletbdrm_read_response(adev, &response->header, sizeof(*response),
454 APPLETBDRM_MSG_UPDATE_COMPLETE);
455 if (ret)
456 goto end_fb_cpu_access;
457
458 if (response->timestamp != footer->timestamp) {
459 drm_err(drm, "Response timestamp (%llu) doesn't match request timestamp (%llu)\n",
460 le64_to_cpu(response->timestamp), timestamp);
461 goto end_fb_cpu_access;
462 }
463
464 end_fb_cpu_access:
465 drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE);
466
467 return ret;
468 }
469
appletbdrm_primary_plane_helper_atomic_update(struct drm_plane * plane,struct drm_atomic_state * old_state)470 static void appletbdrm_primary_plane_helper_atomic_update(struct drm_plane *plane,
471 struct drm_atomic_state *old_state)
472 {
473 struct appletbdrm_device *adev = drm_to_adev(plane->dev);
474 struct drm_device *drm = plane->dev;
475 struct drm_plane_state *plane_state = plane->state;
476 struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane);
477 int idx;
478
479 if (!drm_dev_enter(drm, &idx))
480 return;
481
482 appletbdrm_flush_damage(adev, old_plane_state, plane_state);
483
484 drm_dev_exit(idx);
485 }
486
appletbdrm_primary_plane_helper_atomic_disable(struct drm_plane * plane,struct drm_atomic_state * state)487 static void appletbdrm_primary_plane_helper_atomic_disable(struct drm_plane *plane,
488 struct drm_atomic_state *state)
489 {
490 struct drm_device *dev = plane->dev;
491 struct appletbdrm_device *adev = drm_to_adev(dev);
492 int idx;
493
494 if (!drm_dev_enter(dev, &idx))
495 return;
496
497 appletbdrm_clear_display(adev);
498
499 drm_dev_exit(idx);
500 }
501
appletbdrm_primary_plane_reset(struct drm_plane * plane)502 static void appletbdrm_primary_plane_reset(struct drm_plane *plane)
503 {
504 struct appletbdrm_plane_state *appletbdrm_state;
505
506 WARN_ON(plane->state);
507
508 appletbdrm_state = kzalloc(sizeof(*appletbdrm_state), GFP_KERNEL);
509 if (!appletbdrm_state)
510 return;
511
512 __drm_gem_reset_shadow_plane(plane, &appletbdrm_state->base);
513 }
514
appletbdrm_primary_plane_duplicate_state(struct drm_plane * plane)515 static struct drm_plane_state *appletbdrm_primary_plane_duplicate_state(struct drm_plane *plane)
516 {
517 struct drm_shadow_plane_state *new_shadow_plane_state;
518 struct appletbdrm_plane_state *appletbdrm_state;
519
520 if (WARN_ON(!plane->state))
521 return NULL;
522
523 appletbdrm_state = kzalloc(sizeof(*appletbdrm_state), GFP_KERNEL);
524 if (!appletbdrm_state)
525 return NULL;
526
527 /* Request and response are not duplicated and are allocated in .atomic_check */
528 appletbdrm_state->request = NULL;
529 appletbdrm_state->response = NULL;
530
531 appletbdrm_state->request_size = 0;
532 appletbdrm_state->frames_size = 0;
533
534 new_shadow_plane_state = &appletbdrm_state->base;
535
536 __drm_gem_duplicate_shadow_plane_state(plane, new_shadow_plane_state);
537
538 return &new_shadow_plane_state->base;
539 }
540
appletbdrm_primary_plane_destroy_state(struct drm_plane * plane,struct drm_plane_state * state)541 static void appletbdrm_primary_plane_destroy_state(struct drm_plane *plane,
542 struct drm_plane_state *state)
543 {
544 struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(state);
545
546 kfree(appletbdrm_state->request);
547 kfree(appletbdrm_state->response);
548
549 __drm_gem_destroy_shadow_plane_state(&appletbdrm_state->base);
550
551 kfree(appletbdrm_state);
552 }
553
554 static const struct drm_plane_helper_funcs appletbdrm_primary_plane_helper_funcs = {
555 DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
556 .atomic_check = appletbdrm_primary_plane_helper_atomic_check,
557 .atomic_update = appletbdrm_primary_plane_helper_atomic_update,
558 .atomic_disable = appletbdrm_primary_plane_helper_atomic_disable,
559 };
560
561 static const struct drm_plane_funcs appletbdrm_primary_plane_funcs = {
562 .update_plane = drm_atomic_helper_update_plane,
563 .disable_plane = drm_atomic_helper_disable_plane,
564 .reset = appletbdrm_primary_plane_reset,
565 .atomic_duplicate_state = appletbdrm_primary_plane_duplicate_state,
566 .atomic_destroy_state = appletbdrm_primary_plane_destroy_state,
567 .destroy = drm_plane_cleanup,
568 };
569
appletbdrm_crtc_helper_mode_valid(struct drm_crtc * crtc,const struct drm_display_mode * mode)570 static enum drm_mode_status appletbdrm_crtc_helper_mode_valid(struct drm_crtc *crtc,
571 const struct drm_display_mode *mode)
572 {
573 struct appletbdrm_device *adev = drm_to_adev(crtc->dev);
574
575 return drm_crtc_helper_mode_valid_fixed(crtc, mode, &adev->mode);
576 }
577
578 static const struct drm_mode_config_funcs appletbdrm_mode_config_funcs = {
579 .fb_create = drm_gem_fb_create_with_dirty,
580 .atomic_check = drm_atomic_helper_check,
581 .atomic_commit = drm_atomic_helper_commit,
582 };
583
584 static const struct drm_connector_funcs appletbdrm_connector_funcs = {
585 .reset = drm_atomic_helper_connector_reset,
586 .destroy = drm_connector_cleanup,
587 .fill_modes = drm_helper_probe_single_connector_modes,
588 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
589 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
590 };
591
592 static const struct drm_connector_helper_funcs appletbdrm_connector_helper_funcs = {
593 .get_modes = appletbdrm_connector_helper_get_modes,
594 };
595
596 static const struct drm_crtc_helper_funcs appletbdrm_crtc_helper_funcs = {
597 .mode_valid = appletbdrm_crtc_helper_mode_valid,
598 };
599
600 static const struct drm_crtc_funcs appletbdrm_crtc_funcs = {
601 .reset = drm_atomic_helper_crtc_reset,
602 .destroy = drm_crtc_cleanup,
603 .set_config = drm_atomic_helper_set_config,
604 .page_flip = drm_atomic_helper_page_flip,
605 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
606 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
607 };
608
609 static const struct drm_encoder_funcs appletbdrm_encoder_funcs = {
610 .destroy = drm_encoder_cleanup,
611 };
612
613 DEFINE_DRM_GEM_FOPS(appletbdrm_drm_fops);
614
615 static const struct drm_driver appletbdrm_drm_driver = {
616 DRM_GEM_SHMEM_DRIVER_OPS,
617 .name = "appletbdrm",
618 .desc = "Apple Touch Bar DRM Driver",
619 .major = 1,
620 .minor = 0,
621 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
622 .fops = &appletbdrm_drm_fops,
623 };
624
appletbdrm_setup_mode_config(struct appletbdrm_device * adev)625 static int appletbdrm_setup_mode_config(struct appletbdrm_device *adev)
626 {
627 struct drm_connector *connector = &adev->connector;
628 struct drm_plane *primary_plane;
629 struct drm_crtc *crtc;
630 struct drm_encoder *encoder;
631 struct drm_device *drm = &adev->drm;
632 int ret;
633
634 ret = drmm_mode_config_init(drm);
635 if (ret) {
636 drm_err(drm, "Failed to initialize mode configuration\n");
637 return ret;
638 }
639
640 primary_plane = &adev->primary_plane;
641 ret = drm_universal_plane_init(drm, primary_plane, 0,
642 &appletbdrm_primary_plane_funcs,
643 appletbdrm_primary_plane_formats,
644 ARRAY_SIZE(appletbdrm_primary_plane_formats),
645 NULL,
646 DRM_PLANE_TYPE_PRIMARY, NULL);
647 if (ret) {
648 drm_err(drm, "Failed to initialize universal plane object\n");
649 return ret;
650 }
651
652 drm_plane_helper_add(primary_plane, &appletbdrm_primary_plane_helper_funcs);
653 drm_plane_enable_fb_damage_clips(primary_plane);
654
655 crtc = &adev->crtc;
656 ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
657 &appletbdrm_crtc_funcs, NULL);
658 if (ret) {
659 drm_err(drm, "Failed to initialize CRTC object\n");
660 return ret;
661 }
662
663 drm_crtc_helper_add(crtc, &appletbdrm_crtc_helper_funcs);
664
665 encoder = &adev->encoder;
666 ret = drm_encoder_init(drm, encoder, &appletbdrm_encoder_funcs,
667 DRM_MODE_ENCODER_DAC, NULL);
668 if (ret) {
669 drm_err(drm, "Failed to initialize encoder\n");
670 return ret;
671 }
672
673 encoder->possible_crtcs = drm_crtc_mask(crtc);
674
675 /*
676 * The coordinate system used by the device is different from the
677 * coordinate system of the framebuffer in that the x and y axes are
678 * swapped, and that the y axis is inverted; so what the device reports
679 * as the height is actually the width of the framebuffer and vice
680 * versa.
681 */
682 drm->mode_config.max_width = max(adev->height, DRM_SHADOW_PLANE_MAX_WIDTH);
683 drm->mode_config.max_height = max(adev->width, DRM_SHADOW_PLANE_MAX_HEIGHT);
684 drm->mode_config.preferred_depth = APPLETBDRM_BITS_PER_PIXEL;
685 drm->mode_config.funcs = &appletbdrm_mode_config_funcs;
686
687 adev->mode = (struct drm_display_mode) {
688 DRM_MODE_INIT(60, adev->height, adev->width,
689 DRM_MODE_RES_MM(adev->height, 218),
690 DRM_MODE_RES_MM(adev->width, 218))
691 };
692
693 ret = drm_connector_init(drm, connector,
694 &appletbdrm_connector_funcs, DRM_MODE_CONNECTOR_USB);
695 if (ret) {
696 drm_err(drm, "Failed to initialize connector\n");
697 return ret;
698 }
699
700 drm_connector_helper_add(connector, &appletbdrm_connector_helper_funcs);
701
702 ret = drm_connector_set_panel_orientation(connector,
703 DRM_MODE_PANEL_ORIENTATION_RIGHT_UP);
704 if (ret) {
705 drm_err(drm, "Failed to set panel orientation\n");
706 return ret;
707 }
708
709 connector->display_info.non_desktop = true;
710 ret = drm_object_property_set_value(&connector->base,
711 drm->mode_config.non_desktop_property, true);
712 if (ret) {
713 drm_err(drm, "Failed to set non-desktop property\n");
714 return ret;
715 }
716
717 ret = drm_connector_attach_encoder(connector, encoder);
718
719 if (ret) {
720 drm_err(drm, "Failed to initialize simple display pipe\n");
721 return ret;
722 }
723
724 drm_mode_config_reset(drm);
725
726 return 0;
727 }
728
appletbdrm_probe(struct usb_interface * intf,const struct usb_device_id * id)729 static int appletbdrm_probe(struct usb_interface *intf,
730 const struct usb_device_id *id)
731 {
732 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
733 struct device *dev = &intf->dev;
734 struct appletbdrm_device *adev;
735 struct drm_device *drm = NULL;
736 struct device *dma_dev;
737 int ret;
738
739 ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL);
740 if (ret) {
741 drm_err(drm, "appletbdrm: Failed to find bulk endpoints\n");
742 return ret;
743 }
744
745 adev = devm_drm_dev_alloc(dev, &appletbdrm_drm_driver, struct appletbdrm_device, drm);
746 if (IS_ERR(adev))
747 return PTR_ERR(adev);
748
749 adev->in_ep = bulk_in->bEndpointAddress;
750 adev->out_ep = bulk_out->bEndpointAddress;
751
752 drm = &adev->drm;
753
754 usb_set_intfdata(intf, adev);
755
756 dma_dev = usb_intf_get_dma_device(intf);
757 if (dma_dev) {
758 drm_dev_set_dma_dev(drm, dma_dev);
759 put_device(dma_dev);
760 } else {
761 drm_warn(drm, "buffer sharing not supported"); /* not an error */
762 }
763
764 ret = appletbdrm_get_information(adev);
765 if (ret) {
766 drm_err(drm, "Failed to get display information\n");
767 return ret;
768 }
769
770 ret = appletbdrm_signal_readiness(adev);
771 if (ret) {
772 drm_err(drm, "Failed to signal readiness\n");
773 return ret;
774 }
775
776 ret = appletbdrm_setup_mode_config(adev);
777 if (ret) {
778 drm_err(drm, "Failed to setup mode config\n");
779 return ret;
780 }
781
782 ret = drm_dev_register(drm, 0);
783 if (ret) {
784 drm_err(drm, "Failed to register DRM device\n");
785 return ret;
786 }
787
788 ret = appletbdrm_clear_display(adev);
789 if (ret) {
790 drm_err(drm, "Failed to clear display\n");
791 return ret;
792 }
793
794 return 0;
795 }
796
appletbdrm_disconnect(struct usb_interface * intf)797 static void appletbdrm_disconnect(struct usb_interface *intf)
798 {
799 struct appletbdrm_device *adev = usb_get_intfdata(intf);
800 struct drm_device *drm = &adev->drm;
801
802 drm_dev_unplug(drm);
803 drm_atomic_helper_shutdown(drm);
804 }
805
appletbdrm_shutdown(struct usb_interface * intf)806 static void appletbdrm_shutdown(struct usb_interface *intf)
807 {
808 struct appletbdrm_device *adev = usb_get_intfdata(intf);
809
810 /*
811 * The framebuffer needs to be cleared on shutdown since its content
812 * persists across boots
813 */
814 drm_atomic_helper_shutdown(&adev->drm);
815 }
816
817 static const struct usb_device_id appletbdrm_usb_id_table[] = {
818 { USB_DEVICE_INTERFACE_CLASS(0x05ac, 0x8302, USB_CLASS_AUDIO_VIDEO) },
819 {}
820 };
821 MODULE_DEVICE_TABLE(usb, appletbdrm_usb_id_table);
822
823 static struct usb_driver appletbdrm_usb_driver = {
824 .name = "appletbdrm",
825 .probe = appletbdrm_probe,
826 .disconnect = appletbdrm_disconnect,
827 .shutdown = appletbdrm_shutdown,
828 .id_table = appletbdrm_usb_id_table,
829 };
830 module_usb_driver(appletbdrm_usb_driver);
831
832 MODULE_AUTHOR("Kerem Karabay <kekrby@gmail.com>");
833 MODULE_DESCRIPTION("Apple Touch Bar DRM Driver");
834 MODULE_LICENSE("GPL");
835