forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathdcp.c
More file actions
1378 lines (1142 loc) · 36.2 KB
/
dcp.c
File metadata and controls
1378 lines (1142 loc) · 36.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only OR MIT
/* Copyright 2021 Alyssa Rosenzweig */
#include <linux/align.h>
#include <linux/bitmap.h>
#include <linux/clk.h>
#include <linux/completion.h>
#include <linux/component.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/gpio/consumer.h>
#include <linux/iommu.h>
#include <linux/jiffies.h>
#include <linux/kconfig.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/slab.h>
#include <linux/soc/apple/rtkit.h>
#include <linux/string.h>
#include <linux/usb/typec_altmode.h>
#include <linux/usb/typec_dp.h>
#include <linux/usb/typec_mux.h>
#include <linux/workqueue.h>
#include <drm/drm_fb_dma_helper.h>
#include <drm/drm_fourcc.h>
#include <drm/drm_framebuffer.h>
#include <drm/drm_module.h>
#include <drm/drm_probe_helper.h>
#include <drm/drm_vblank.h>
#include "afk.h"
#include "av.h"
#include "dcp.h"
#include "dcp-internal.h"
#include "iomfb.h"
#include "parser.h"
#include "trace.h"
#define APPLE_DCP_COPROC_CPU_CONTROL 0x44
#define APPLE_DCP_COPROC_CPU_CONTROL_RUN BIT(4)
#define DCP_BOOT_TIMEOUT msecs_to_jiffies(1000)
static bool show_notch;
module_param(show_notch, bool, 0644);
MODULE_PARM_DESC(show_notch, "Use the full display height and shows the notch");
bool hdmi_audio;
module_param(hdmi_audio, bool, 0644);
MODULE_PARM_DESC(hdmi_audio, "Enable unstable HDMI audio support");
static bool unstable_edid = true;
module_param(unstable_edid, bool, 0644);
MODULE_PARM_DESC(unstable_edid, "Enable unstable EDID retrival support");
bool force_vrr;
module_param(force_vrr, bool, 0644);
MODULE_PARM_DESC(force_vrr, "Always enable Adaptive Sync/ProMotion on supported displays");
/* copied and simplified from drm_vblank.c */
static void send_vblank_event(struct drm_device *dev,
struct drm_pending_vblank_event *e,
u64 seq, ktime_t now)
{
struct timespec64 tv;
if (e->event.base.type != DRM_EVENT_FLIP_COMPLETE)
return;
tv = ktime_to_timespec64(now);
e->event.vbl.sequence = seq;
/*
* e->event is a user space structure, with hardcoded unsigned
* 32-bit seconds/microseconds. This is safe as we always use
* monotonic timestamps since linux-4.15
*/
e->event.vbl.tv_sec = tv.tv_sec;
e->event.vbl.tv_usec = tv.tv_nsec / 1000;
/*
* Use the same timestamp for any associated fence signal to avoid
* mismatch in timestamps for vsync & fence events triggered by the
* same HW event. Frameworks like SurfaceFlinger in Android expects the
* retire-fence timestamp to match exactly with HW vsync as it uses it
* for its software vsync modeling.
*/
drm_send_event_timestamp_locked(dev, &e->base, now);
}
/**
* dcp_crtc_send_page_flip_event - helper to send vblank event after pageflip
*
* Compensate for unknown slack between page flip and arrival of the
* swap_complete callback. Minimal observed duration on DCP with HDMI output
* was around 2.3 ms. If the fb swap was submitted closer to the expected
* swap_complete it gets a penalty of one frame duration. This is on the border
* of unreasonable considering that Apple advertises support for 240 Hz (frame
* duration of 4.167 ms).
* It is unreasonable considering kwin's kms commit scheduling. Kwin commits
* 1.5 ms + the mode's vblank time before the expected next page flip
* completion. This results in presenting at half the display's rate for HDMI
* outputs.
* This might be a difference between dcp and dcpext.
*/
static void dcp_crtc_send_page_flip_event(struct apple_crtc *crtc,
struct drm_pending_vblank_event *e,
ktime_t now, ktime_t start)
{
struct drm_device *dev = crtc->base.dev;
u64 seq;
unsigned int pipe = drm_crtc_index(&crtc->base);
ktime_t flip;
seq = 0;
if (start != KTIME_MIN) {
s64 delta = ktime_us_delta(now, start);
if (delta <= 500)
flip = now;
else if (delta >= 2500)
flip = ktime_sub_us(now, 1000);
else
flip = ktime_sub_us(now, (delta - 500) / 2);
} else {
flip = now;
}
e->pipe = pipe;
send_vblank_event(dev, e, seq, flip);
}
/* HACK: moved here to avoid circular dependency between apple_drv and dcp */
void dcp_drm_crtc_vblank(struct apple_crtc *crtc)
{
unsigned long flags;
spin_lock_irqsave(&crtc->base.dev->event_lock, flags);
if (crtc->event) {
drm_crtc_send_vblank_event(&crtc->base, crtc->event);
crtc->event = NULL;
}
spin_unlock_irqrestore(&crtc->base.dev->event_lock, flags);
}
void dcp_drm_crtc_page_flip(struct apple_dcp *dcp, ktime_t now)
{
unsigned long flags;
struct apple_crtc *crtc = dcp->crtc;
spin_lock_irqsave(&crtc->base.dev->event_lock, flags);
if (crtc->event) {
if (crtc->event->event.base.type == DRM_EVENT_FLIP_COMPLETE)
dcp_crtc_send_page_flip_event(crtc, crtc->event, now, dcp->swap_start);
else
drm_crtc_send_vblank_event(&crtc->base, crtc->event);
crtc->event = NULL;
dcp->swap_start = KTIME_MIN;
}
spin_unlock_irqrestore(&crtc->base.dev->event_lock, flags);
}
void dcp_set_dimensions(struct apple_dcp *dcp)
{
int i;
int width_mm = dcp->width_mm;
int height_mm = dcp->height_mm;
if (width_mm == 0 || height_mm == 0) {
width_mm = dcp->panel.width_mm;
height_mm = dcp->panel.height_mm;
}
/* Set the connector info */
if (dcp->connector) {
struct drm_connector *connector = &dcp->connector->base;
mutex_lock(&connector->dev->mode_config.mutex);
connector->display_info.width_mm = width_mm;
connector->display_info.height_mm = height_mm;
mutex_unlock(&connector->dev->mode_config.mutex);
}
/*
* Fix up any probed modes. Modes are created when parsing
* TimingElements, dimensions are calculated when parsing
* DisplayAttributes, and TimingElements may be sent first
*/
for (i = 0; i < dcp->nr_modes; ++i) {
dcp->modes[i].mode.width_mm = width_mm;
dcp->modes[i].mode.height_mm = height_mm;
}
}
bool dcp_has_panel(struct apple_dcp *dcp)
{
return dcp->panel.width_mm > 0;
}
int dcp_set_crc(struct drm_crtc *crtc, bool enabled)
{
struct apple_crtc *ac = to_apple_crtc(crtc);
struct apple_dcp *dcp = platform_get_drvdata(ac->dcp);
dcp->crc_enabled = enabled;
return 0;
}
/*
* Helper to send a DRM vblank event. We do not know how call swap_submit_dcp
* without surfaces. To avoid timeouts in drm_atomic_helper_wait_for_vblanks
* send a vblank event via a workqueue.
*/
static void dcp_delayed_vblank(struct work_struct *work)
{
struct apple_dcp *dcp;
dcp = container_of(work, struct apple_dcp, vblank_wq);
mdelay(5);
dcp_drm_crtc_vblank(dcp->crtc);
}
static void dcp_recv_msg(void *cookie, u8 endpoint, u64 message)
{
struct apple_dcp *dcp = cookie;
trace_dcp_recv_msg(dcp, endpoint, message);
switch (endpoint) {
case IOMFB_ENDPOINT:
return iomfb_recv_msg(dcp, message);
case AV_ENDPOINT:
afk_receive_message(dcp->avep, message);
return;
case SYSTEM_ENDPOINT:
afk_receive_message(dcp->systemep, message);
return;
case DISP0_ENDPOINT:
afk_receive_message(dcp->ibootep, message);
return;
case DPAVSERV_ENDPOINT:
afk_receive_message(dcp->dcpavservep, message);
return;
case DPTX_ENDPOINT:
afk_receive_message(dcp->dptxep, message);
return;
default:
WARN(endpoint, "unknown DCP endpoint %hhu\n", endpoint);
}
}
static void dcp_rtk_crashed(void *cookie, const void *crashlog, size_t crashlog_size)
{
struct apple_dcp *dcp = cookie;
dcp->crashed = true;
dev_err(dcp->dev, "DCP has crashed\n");
if (dcp->connector) {
dcp->connector->connected = 0;
drm_edid_free(dcp->connector->drm_edid);
dcp->connector->drm_edid = NULL;
schedule_work(&dcp->connector->hotplug_wq);
}
complete(&dcp->start_done);
}
static int dcp_rtk_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr)
{
struct apple_dcp *dcp = cookie;
if (bfr->iova) {
struct iommu_domain *domain =
iommu_get_domain_for_dev(dcp->dev);
phys_addr_t phy_addr;
if (!domain)
return -ENOMEM;
// TODO: get map from device-tree
phy_addr = iommu_iova_to_phys(domain, bfr->iova);
if (!phy_addr)
return -ENOMEM;
// TODO: verify phy_addr, cache attribute
bfr->buffer = memremap(phy_addr, bfr->size, MEMREMAP_WB);
if (!bfr->buffer)
return -ENOMEM;
bfr->is_mapped = true;
dev_info(dcp->dev,
"shmem_setup: iova: %lx -> pa: %lx -> iomem: %lx\n",
(uintptr_t)bfr->iova, (uintptr_t)phy_addr,
(uintptr_t)bfr->buffer);
} else {
bfr->buffer = dma_alloc_coherent(dcp->dev, bfr->size,
&bfr->iova, GFP_KERNEL);
if (!bfr->buffer)
return -ENOMEM;
dev_info(dcp->dev, "shmem_setup: iova: %lx, buffer: %lx\n",
(uintptr_t)bfr->iova, (uintptr_t)bfr->buffer);
}
return 0;
}
static void dcp_rtk_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr)
{
struct apple_dcp *dcp = cookie;
if (bfr->is_mapped)
memunmap(bfr->buffer);
else
dma_free_coherent(dcp->dev, bfr->size, bfr->buffer, bfr->iova);
}
static struct apple_rtkit_ops rtkit_ops = {
.crashed = dcp_rtk_crashed,
.recv_message = dcp_recv_msg,
.shmem_setup = dcp_rtk_shmem_setup,
.shmem_destroy = dcp_rtk_shmem_destroy,
};
void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message)
{
trace_dcp_send_msg(dcp, endpoint, message);
apple_rtkit_send_message(dcp->rtk, endpoint, message, NULL,
true);
}
int dcp_crtc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state)
{
struct platform_device *pdev = to_apple_crtc(crtc)->dcp;
struct apple_dcp *dcp = platform_get_drvdata(pdev);
struct drm_plane_state *new_state;
struct drm_plane *plane;
struct drm_crtc_state *crtc_state;
int plane_idx, plane_count = 0;
bool needs_modeset;
if (dcp->crashed)
return -EINVAL;
crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
needs_modeset = drm_atomic_crtc_needs_modeset(crtc_state) || !dcp->valid_mode;
if (!needs_modeset && !dcp->connector->connected) {
dev_err(dcp->dev, "crtc_atomic_check: disconnected but no modeset\n");
return -EINVAL;
}
for_each_new_plane_in_state(state, plane, new_state, plane_idx) {
/* skip planes not for this crtc */
if (new_state->crtc != crtc)
continue;
plane_count += 1;
}
if (plane_count > DCP_MAX_PLANES) {
dev_err(dcp->dev, "crtc_atomic_check: Blend supports only 2 layers!\n");
return -EINVAL;
}
// if (dcp->vrr_enabled != crtc_state->vrr_enabled) {
// crtc_state->mode_changed = true;
// }
return 0;
}
int dcp_get_connector_type(struct platform_device *pdev)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
return (dcp->connector_type);
}
#define DPTX_CONNECT_TIMEOUT msecs_to_jiffies(2000)
static int dcp_dptx_connect(struct apple_dcp *dcp, u32 port)
{
int ret = 0;
if (!dcp->phy) {
dev_warn(dcp->dev, "dcp_dptx_connect: missing phy\n");
return -ENODEV;
}
dev_info(dcp->dev, "%s(port=%d)\n", __func__, port);
mutex_lock(&dcp->hpd_mutex);
if (!dcp->dptxport[port].enabled) {
dev_warn(dcp->dev, "dcp_dptx_connect: dptx service for port %d not enabled\n", port);
ret = -ENODEV;
goto out_unlock;
}
if (dcp->dptxport[port].connected)
goto out_unlock;
reinit_completion(&dcp->dptxport[port].linkcfg_completion);
dcp->dptxport[port].atcphy = dcp->phy;
dptxport_connect(dcp->dptxport[port].service, 0, dcp->dptx_phy, dcp->dptx_die);
dptxport_request_display(dcp->dptxport[port].service);
dcp->dptxport[port].connected = true;
mutex_unlock(&dcp->hpd_mutex);
ret = wait_for_completion_timeout(&dcp->dptxport[port].linkcfg_completion,
DPTX_CONNECT_TIMEOUT);
if (ret < 0)
dev_warn(dcp->dev, "dcp_dptx_connect: port %d link complete failed:%d\n",
port, ret);
else
dev_dbg(dcp->dev, "dcp_dptx_connect: waited %d ms for link\n",
jiffies_to_msecs(DPTX_CONNECT_TIMEOUT - ret));
usleep_range(5, 10);
if (dcp->connector_type == DRM_MODE_CONNECTOR_DisplayPort)
dptxport_set_hpd(dcp->dptxport[port].service, true);
if (dcp->avep)
av_service_connect(dcp);
return 0;
out_unlock:
mutex_unlock(&dcp->hpd_mutex);
return ret;
}
static void disconnected_hpd_event(struct apple_connector *con)
{
if (con && con->connected) {
con->connected = 0;
drm_kms_helper_connector_hotplug_event(&con->base);
}
}
static int dcp_dptx_disconnect(struct apple_dcp *dcp, u32 port)
{
dev_info(dcp->dev, "%s(port=%d)\n", __func__, port);
mutex_lock(&dcp->hpd_mutex);
if (dcp->dptxport[port].enabled && dcp->dptxport[port].connected) {
dptxport_release_display(dcp->dptxport[port].service);
dcp->dptxport[port].connected = false;
}
mutex_unlock(&dcp->hpd_mutex);
return 0;
}
int dcp_dptx_connect_oob(struct platform_device *pdev, u32 port)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
return dcp_dptx_connect(dcp, port);
}
int dcp_dptx_disconnect_oob(struct platform_device *pdev, u32 port)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
disconnected_hpd_event(dcp->connector);
if (dcp->avep)
av_service_disconnect(dcp);
if (dcp->dptxport[port].enabled)
dptxport_set_hpd(dcp->dptxport[port].service, false);
return dcp_dptx_disconnect(dcp, port);
}
static irqreturn_t dcp_dp2hdmi_hpd(int irq, void *data)
{
struct apple_dcp *dcp = data;
bool connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
/* do nothing on disconnect and trust that dcp detects it itself.
* Parallel disconnect HPDs result drm disabling the CRTC even when it
* should not.
* The interrupt should be changed to rising but for now the disconnect
* IRQs might be helpful for debugging.
*/
dev_info(dcp->dev, "DP2HDMI HPD irq, connected:%d\n", connected);
if (connected) {
msleep(500);
connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
dev_info(dcp->dev, "DP2HDMI HPD irq, 500ms debounce: connected:%d\n", connected);
}
if (connected)
dcp_dptx_connect(dcp, 0);
return IRQ_HANDLED;
}
void dcp_link(struct platform_device *pdev, struct apple_crtc *crtc,
struct apple_connector *connector)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
dcp->crtc = crtc;
dcp->connector = connector;
}
bool dcp_fw_compat_is_12_x(struct platform_device *pdev)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
return dcp->fw_compat == DCP_FIRMWARE_V_12_3;
}
int dcp_start(struct platform_device *pdev)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
int ret;
init_completion(&dcp->start_done);
/* start RTKit endpoints */
ret = systemep_init(dcp);
if (ret)
dev_warn(dcp->dev, "Failed to start system endpoint: %d\n", ret);
if (unstable_edid && !dcp_has_panel(dcp)) {
ret = dpavservep_init(dcp);
if (ret)
dev_warn(dcp->dev, "Failed to start DPAVSERV endpoint: %d",
ret);
}
if (dcp->phy && dcp->fw_compat >= DCP_FIRMWARE_V_13_5) {
ret = ibootep_init(dcp);
if (ret)
dev_warn(dcp->dev, "Failed to start IBOOT endpoint: %d\n",
ret);
ret = dptxep_init(dcp);
if (ret) {
dev_warn(dcp->dev, "Failed to start DPTX endpoint: %d\n",
ret);
#ifdef DCP_DPTX_DISCONNECT_ON_INIT
/*
* This disconnect / connect cycle on init is only necessary
* when using dcp0 on j473, j474s and presumedly j475c.
* Since dcp0 is not used at the moment let's avoid this
* since it is possibly the cause for startup issues.
*/
} else if (dcp->dptxport[0].enabled) {
bool connected;
/* force disconnect on start - necessary if the display
* is already up from m1n1
*/
dptxport_set_hpd(dcp->dptxport[0].service, false);
dptxport_release_display(dcp->dptxport[0].service);
usleep_range(10 * USEC_PER_MSEC, 25 * USEC_PER_MSEC);
connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
dev_info(dcp->dev, "%s: DP2HDMI HPD connected:%d\n", __func__, connected);
// necessary on j473/j474 but not on j314c
if (connected)
dcp_dptx_connect(dcp, 0);
#endif
}
} else if (dcp->phy) {
dev_warn(dcp->dev, "OS firmware incompatible with dptxport EP\n");
}
ret = iomfb_start_rtkit(dcp);
if (ret)
dev_err(dcp->dev, "Failed to start IOMFB endpoint: %d\n", ret);
#if IS_ENABLED(CONFIG_DRM_APPLE_AUDIO)
if (hdmi_audio) {
ret = avep_init(dcp);
if (ret)
dev_warn(dcp->dev, "Failed to start AV endpoint: %d", ret);
ret = 0;
}
#endif
return ret;
}
static void _dcp_poweroff(struct apple_dcp *dcp)
{
switch (dcp->fw_compat) {
case DCP_FIRMWARE_V_12_3:
iomfb_poweroff_v12_3(dcp);
break;
case DCP_FIRMWARE_V_13_5:
iomfb_poweroff_v13_3(dcp);
break;
default:
WARN_ONCE(true, "Unexpected firmware version: %u\n", dcp->fw_compat);
break;
}
}
static int dcp_enable_dp2hdmi_hpd(struct apple_dcp *dcp)
{
// check HPD state before enabling the edge triggered IRQ
if (dcp->hdmi_hpd) {
bool connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
dev_info(dcp->dev, "%s: DP2HDMI HPD connected:%d\n", __func__, connected);
if (connected)
dcp_dptx_connect(dcp, 0);
else
_dcp_poweroff(dcp);
}
if (dcp->hdmi_hpd_irq)
enable_irq(dcp->hdmi_hpd_irq);
return 0;
}
int dcp_wait_ready(struct platform_device *pdev, u64 timeout)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
int ret;
if (dcp->crashed)
return -ENODEV;
if (dcp->active)
return dcp_enable_dp2hdmi_hpd(dcp);
if (timeout <= 0)
return -ETIMEDOUT;
ret = wait_for_completion_timeout(&dcp->start_done, timeout);
if (ret < 0)
return ret;
if (dcp->crashed)
return -ENODEV;
if (dcp->active)
dcp_enable_dp2hdmi_hpd(dcp);
return dcp->active ? 0 : -ETIMEDOUT;
}
static void __maybe_unused dcp_sleep(struct apple_dcp *dcp)
{
switch (dcp->fw_compat) {
case DCP_FIRMWARE_V_12_3:
iomfb_sleep_v12_3(dcp);
break;
case DCP_FIRMWARE_V_13_5:
iomfb_sleep_v13_3(dcp);
break;
default:
WARN_ONCE(true, "Unexpected firmware version: %u\n", dcp->fw_compat);
break;
}
}
void dcp_poweron(struct platform_device *pdev)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
if (dcp->hdmi_hpd) {
bool connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
dev_info(dcp->dev, "%s: DP2HDMI HPD connected:%d\n", __func__, connected);
if (connected)
dcp_dptx_connect(dcp, 0);
}
switch (dcp->fw_compat) {
case DCP_FIRMWARE_V_12_3:
iomfb_poweron_v12_3(dcp);
break;
case DCP_FIRMWARE_V_13_5:
iomfb_poweron_v13_3(dcp);
break;
default:
WARN_ONCE(true, "Unexpected firmware version: %u\n", dcp->fw_compat);
break;
}
if (dcp->avep)
av_service_connect(dcp);
}
void dcp_poweroff(struct platform_device *pdev)
{
struct apple_dcp *dcp = platform_get_drvdata(pdev);
if (dcp->avep)
av_service_disconnect(dcp);
_dcp_poweroff(dcp);
if (dcp->hdmi_hpd) {
bool connected = gpiod_get_value_cansleep(dcp->hdmi_hpd);
if (!connected) {
disconnected_hpd_event(dcp->connector);
dcp_dptx_disconnect(dcp, 0);
}
}
}
static void dcp_work_register_backlight(struct work_struct *work)
{
int ret;
struct apple_dcp *dcp;
dcp = container_of(work, struct apple_dcp, bl_register_wq);
mutex_lock(&dcp->bl_register_mutex);
if (dcp->brightness.bl_dev)
goto out_unlock;
/* try to register backlight device, */
ret = dcp_backlight_register(dcp);
if (ret) {
dev_err(dcp->dev, "Unable to register backlight device\n");
dcp->brightness.maximum = 0;
}
out_unlock:
mutex_unlock(&dcp->bl_register_mutex);
}
static void dcp_work_update_backlight(struct work_struct *work)
{
struct apple_dcp *dcp;
dcp = container_of(work, struct apple_dcp, bl_update_wq);
dcp_backlight_update(dcp);
}
static int dcp_create_piodma_iommu_dev(struct apple_dcp *dcp)
{
int ret;
struct device_node *node __free(device_node) = of_get_child_by_name(dcp->dev->of_node, "piodma");
if (!node)
return dev_err_probe(dcp->dev, -ENODEV,
"Failed to get piodma child DT node\n");
dcp->piodma = of_platform_device_create(node, NULL, dcp->dev);
if (!dcp->piodma)
return dev_err_probe(dcp->dev, -ENODEV, "Failed to create piodma pdev for %pOF\n", node);
ret = dma_set_mask_and_coherent(&dcp->piodma->dev, DMA_BIT_MASK(42));
if (ret)
goto err_destroy_pdev;
ret = of_dma_configure(&dcp->piodma->dev, node, true);
if (ret) {
ret = dev_err_probe(dcp->dev, ret,
"Failed to configure IOMMU child DMA\n");
goto err_destroy_pdev;
}
dcp->iommu_dom = iommu_get_domain_for_dev(&dcp->piodma->dev);
if (IS_ERR(dcp->iommu_dom)) {
ret = dev_err_probe(dcp->dev, PTR_ERR(dcp->iommu_dom),
"Failed to get default iommu domain for "
"piodma device\n");
dcp->iommu_dom = NULL;
goto err_destroy_pdev;
}
return 0;
err_destroy_pdev:
of_platform_device_destroy(&dcp->piodma->dev, NULL);
return ret;
}
static int dcp_get_bw_scratch_reg(struct apple_dcp *dcp, u32 expected)
{
struct of_phandle_args ph_args;
u32 addr_idx, disp_idx, offset;
int ret;
ret = of_parse_phandle_with_args(dcp->dev->of_node, "apple,bw-scratch",
"#apple,bw-scratch-cells", 0, &ph_args);
if (ret < 0) {
dev_err(dcp->dev, "Failed to read 'apple,bw-scratch': %d\n", ret);
return ret;
}
if (ph_args.args_count != 3) {
dev_err(dcp->dev, "Unexpected 'apple,bw-scratch' arg count %d\n",
ph_args.args_count);
ret = -EINVAL;
goto err_of_node_put;
}
addr_idx = ph_args.args[0];
disp_idx = ph_args.args[1];
offset = ph_args.args[2];
if (disp_idx != expected || disp_idx >= MAX_DISP_REGISTERS) {
dev_err(dcp->dev, "Unexpected disp_reg value in 'apple,bw-scratch': %d\n",
disp_idx);
ret = -EINVAL;
goto err_of_node_put;
}
ret = of_address_to_resource(ph_args.np, addr_idx, &dcp->disp_bw_scratch_res);
if (ret < 0) {
dev_err(dcp->dev, "Failed to get 'apple,bw-scratch' resource %d from %pOF\n",
addr_idx, ph_args.np);
goto err_of_node_put;
}
if (offset > resource_size(&dcp->disp_bw_scratch_res) - 4) {
ret = -EINVAL;
goto err_of_node_put;
}
dcp->disp_registers[disp_idx] = &dcp->disp_bw_scratch_res;
dcp->disp_bw_scratch_index = disp_idx;
dcp->disp_bw_scratch_offset = offset;
ret = 0;
err_of_node_put:
of_node_put(ph_args.np);
return ret;
}
static int dcp_get_bw_doorbell_reg(struct apple_dcp *dcp, u32 expected)
{
struct of_phandle_args ph_args;
u32 addr_idx, disp_idx;
int ret;
ret = of_parse_phandle_with_args(dcp->dev->of_node, "apple,bw-doorbell",
"#apple,bw-doorbell-cells", 0, &ph_args);
if (ret < 0) {
dev_err(dcp->dev, "Failed to read 'apple,bw-doorbell': %d\n", ret);
return ret;
}
if (ph_args.args_count != 2) {
dev_err(dcp->dev, "Unexpected 'apple,bw-doorbell' arg count %d\n",
ph_args.args_count);
ret = -EINVAL;
goto err_of_node_put;
}
addr_idx = ph_args.args[0];
disp_idx = ph_args.args[1];
if (disp_idx != expected || disp_idx >= MAX_DISP_REGISTERS) {
dev_err(dcp->dev, "Unexpected disp_reg value in 'apple,bw-doorbell': %d\n",
disp_idx);
ret = -EINVAL;
goto err_of_node_put;
}
ret = of_address_to_resource(ph_args.np, addr_idx, &dcp->disp_bw_doorbell_res);
if (ret < 0) {
dev_err(dcp->dev, "Failed to get 'apple,bw-doorbell' resource %d from %pOF\n",
addr_idx, ph_args.np);
goto err_of_node_put;
}
dcp->disp_bw_doorbell_index = disp_idx;
dcp->disp_registers[disp_idx] = &dcp->disp_bw_doorbell_res;
ret = 0;
err_of_node_put:
of_node_put(ph_args.np);
return ret;
}
static int dcp_get_disp_regs(struct apple_dcp *dcp)
{
struct platform_device *pdev = to_platform_device(dcp->dev);
int count = pdev->num_resources - 1;
int i, ret;
if (count <= 0 || count > MAX_DISP_REGISTERS)
return -EINVAL;
for (i = 0; i < count; ++i) {
dcp->disp_registers[i] =
platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
}
/* load pmgr bandwidth scratch resource and offset */
ret = dcp_get_bw_scratch_reg(dcp, count);
if (ret < 0)
return ret;
count += 1;
/* load pmgr bandwidth doorbell resource if present (only on t8103) */
if (of_property_present(dcp->dev->of_node, "apple,bw-doorbell")) {
ret = dcp_get_bw_doorbell_reg(dcp, count);
if (ret < 0)
return ret;
count += 1;
}
dcp->nr_disp_registers = count;
return 0;
}
#define DCP_FW_VERSION_MIN_LEN 3
#define DCP_FW_VERSION_MAX_LEN 5
#define DCP_FW_VERSION_STR_LEN (DCP_FW_VERSION_MAX_LEN * 4)
static int dcp_read_fw_version(struct device *dev, const char *name,
char *version_str)
{
u32 ver[DCP_FW_VERSION_MAX_LEN];
int len_str;
int len;
len = of_property_read_variable_u32_array(dev->of_node, name, ver,
DCP_FW_VERSION_MIN_LEN,
DCP_FW_VERSION_MAX_LEN);
switch (len) {
case 3:
len_str = scnprintf(version_str, DCP_FW_VERSION_STR_LEN,
"%d.%d.%d", ver[0], ver[1], ver[2]);
break;
case 4:
len_str = scnprintf(version_str, DCP_FW_VERSION_STR_LEN,
"%d.%d.%d.%d", ver[0], ver[1], ver[2],
ver[3]);
break;
case 5:
len_str = scnprintf(version_str, DCP_FW_VERSION_STR_LEN,
"%d.%d.%d.%d.%d", ver[0], ver[1], ver[2],
ver[3], ver[4]);
break;
default:
len_str = strscpy(version_str, "UNKNOWN",
DCP_FW_VERSION_STR_LEN);
if (len >= 0)
len = -EOVERFLOW;
break;
}
if (len_str >= DCP_FW_VERSION_STR_LEN)
dev_warn(dev, "'%s' truncated: '%s'\n", name, version_str);
return len;
}
static enum dcp_firmware_version dcp_check_firmware_version(struct device *dev)
{
char compat_str[DCP_FW_VERSION_STR_LEN];
char fw_str[DCP_FW_VERSION_STR_LEN];
int ret;
/* firmware version is just informative */
dcp_read_fw_version(dev, "apple,firmware-version", fw_str);
ret = dcp_read_fw_version(dev, "apple,firmware-compat", compat_str);
if (ret < 0) {
dev_err(dev, "Could not read 'apple,firmware-compat': %d\n", ret);
return DCP_FIRMWARE_UNKNOWN;
}
if (strncmp(compat_str, "12.3.0", sizeof(compat_str)) == 0)
return DCP_FIRMWARE_V_12_3;
/*
* m1n1 reports firmware version 13.5 as compatible with 13.3. This is
* only true for the iomfb endpoint. The interface for the dptx-port
* endpoint changed between 13.3 and 13.5. The driver will only support
* firmware 13.5. Check the actual firmware version for compat version
* 13.3 until m1n1 reports 13.5 as "firmware-compat".
*/
else if ((strncmp(compat_str, "13.3.0", sizeof(compat_str)) == 0) &&
(strncmp(fw_str, "13.5.0", sizeof(compat_str)) == 0))
return DCP_FIRMWARE_V_13_5;
else if (strncmp(compat_str, "13.5.0", sizeof(compat_str)) == 0)
return DCP_FIRMWARE_V_13_5;
dev_err(dev, "DCP firmware-compat %s (FW: %s) is not supported\n",
compat_str, fw_str);
return DCP_FIRMWARE_UNKNOWN;
}
static int dcp_comp_bind(struct device *dev, struct device *main, void *data)
{
struct device_node *panel_np;
struct apple_dcp *dcp = dev_get_drvdata(dev);
u32 cpu_ctrl;
int ret;
ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(42));
if (ret)
return ret;