1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Broadcom
4 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
5 * Copyright (C) 2013 Red Hat
6 * Author: Rob Clark <robdclark@gmail.com>
10 * DOC: VC4 Falcon HDMI module
12 * The HDMI core has a state machine and a PHY. On BCM2835, most of
13 * the unit operates off of the HSM clock from CPRMAN. It also
14 * internally uses the PLLH_PIX clock for the PHY.
16 * HDMI infoframes are kept within a small packet ram, where each
17 * packet can be individually enabled for including in a frame.
19 * HDMI audio is implemented entirely within the HDMI IP block. A
20 * register in the HDMI encoder takes SPDIF frames from the DMA engine
21 * and transfers them over an internal MAI (multi-channel audio
22 * interconnect) bus to the encoder side for insertion into the video
25 * The driver's HDMI encoder does not yet support power management.
26 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
27 * continuously running, and only the HDMI logic and packet ram are
28 * powered off/on at disable/enable time.
30 * The driver does not yet support CEC control, though the HDMI
31 * encoder block has CEC support.
34 #include <drm/display/drm_hdmi_helper.h>
35 #include <drm/display/drm_hdmi_state_helper.h>
36 #include <drm/display/drm_scdc_helper.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_edid.h>
40 #include <drm/drm_probe_helper.h>
41 #include <drm/drm_simple_kms_helper.h>
42 #include <linux/clk.h>
43 #include <linux/component.h>
44 #include <linux/gpio/consumer.h>
45 #include <linux/i2c.h>
47 #include <linux/of_address.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/rational.h>
50 #include <linux/reset.h>
51 #include <sound/dmaengine_pcm.h>
52 #include <sound/hdmi-codec.h>
53 #include <sound/pcm_drm_eld.h>
54 #include <sound/pcm_params.h>
55 #include <sound/soc.h>
56 #include "media/cec.h"
59 #include "vc4_hdmi_regs.h"
62 #define VC5_HDMI_HORZA_HFP_SHIFT 16
63 #define VC5_HDMI_HORZA_HFP_MASK VC4_MASK(28, 16)
64 #define VC5_HDMI_HORZA_VPOS BIT(15)
65 #define VC5_HDMI_HORZA_HPOS BIT(14)
66 #define VC5_HDMI_HORZA_HAP_SHIFT 0
67 #define VC5_HDMI_HORZA_HAP_MASK VC4_MASK(13, 0)
69 #define VC5_HDMI_HORZB_HBP_SHIFT 16
70 #define VC5_HDMI_HORZB_HBP_MASK VC4_MASK(26, 16)
71 #define VC5_HDMI_HORZB_HSP_SHIFT 0
72 #define VC5_HDMI_HORZB_HSP_MASK VC4_MASK(10, 0)
74 #define VC5_HDMI_VERTA_VSP_SHIFT 24
75 #define VC5_HDMI_VERTA_VSP_MASK VC4_MASK(28, 24)
76 #define VC5_HDMI_VERTA_VFP_SHIFT 16
77 #define VC5_HDMI_VERTA_VFP_MASK VC4_MASK(22, 16)
78 #define VC5_HDMI_VERTA_VAL_SHIFT 0
79 #define VC5_HDMI_VERTA_VAL_MASK VC4_MASK(12, 0)
81 #define VC5_HDMI_VERTB_VSPO_SHIFT 16
82 #define VC5_HDMI_VERTB_VSPO_MASK VC4_MASK(29, 16)
84 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
85 #define VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
86 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_SHIFT 0
87 #define VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK VC4_MASK(3, 0)
89 #define VC5_HDMI_SCRAMBLER_CTL_ENABLE BIT(0)
91 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_SHIFT 8
92 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK VC4_MASK(10, 8)
94 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_SHIFT 0
95 #define VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK VC4_MASK(3, 0)
97 #define VC5_HDMI_GCP_CONFIG_GCP_ENABLE BIT(31)
99 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_SHIFT 8
100 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK VC4_MASK(15, 8)
102 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK VC4_MASK(7, 0)
103 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_SET_AVMUTE BIT(0)
104 #define VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE BIT(4)
106 # define VC4_HD_M_SW_RST BIT(2)
107 # define VC4_HD_M_ENABLE BIT(0)
109 #define HSM_MIN_CLOCK_FREQ 120000000
110 #define CEC_CLOCK_FREQ 40000
112 #define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
114 static bool vc4_hdmi_supports_scrambling(struct vc4_hdmi
*vc4_hdmi
)
116 struct drm_display_info
*display
= &vc4_hdmi
->connector
.display_info
;
118 lockdep_assert_held(&vc4_hdmi
->mutex
);
120 if (!display
->is_hdmi
)
123 if (!display
->hdmi
.scdc
.supported
||
124 !display
->hdmi
.scdc
.scrambling
.supported
)
130 static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode
*mode
,
132 enum hdmi_colorspace fmt
)
134 unsigned long long clock
= drm_hdmi_compute_mode_clock(mode
, bpc
, fmt
);
136 return clock
> HDMI_14_MAX_TMDS_CLK
;
139 static int vc4_hdmi_debugfs_regs(struct seq_file
*m
, void *unused
)
141 struct drm_debugfs_entry
*entry
= m
->private;
142 struct vc4_hdmi
*vc4_hdmi
= entry
->file
.data
;
143 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
144 struct drm_printer p
= drm_seq_file_printer(m
);
147 if (!drm_dev_enter(drm
, &idx
))
150 WARN_ON(pm_runtime_resume_and_get(&vc4_hdmi
->pdev
->dev
));
152 drm_print_regset32(&p
, &vc4_hdmi
->hdmi_regset
);
153 drm_print_regset32(&p
, &vc4_hdmi
->hd_regset
);
154 drm_print_regset32(&p
, &vc4_hdmi
->cec_regset
);
155 drm_print_regset32(&p
, &vc4_hdmi
->csc_regset
);
156 drm_print_regset32(&p
, &vc4_hdmi
->dvp_regset
);
157 drm_print_regset32(&p
, &vc4_hdmi
->phy_regset
);
158 drm_print_regset32(&p
, &vc4_hdmi
->ram_regset
);
159 drm_print_regset32(&p
, &vc4_hdmi
->rm_regset
);
161 pm_runtime_put(&vc4_hdmi
->pdev
->dev
);
168 static void vc4_hdmi_reset(struct vc4_hdmi
*vc4_hdmi
)
170 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
175 * We can be called by our bind callback, when the
176 * connector->dev pointer might not be initialised yet.
178 if (drm
&& !drm_dev_enter(drm
, &idx
))
181 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
183 HDMI_WRITE(HDMI_M_CTL
, VC4_HD_M_SW_RST
);
185 HDMI_WRITE(HDMI_M_CTL
, 0);
187 HDMI_WRITE(HDMI_M_CTL
, VC4_HD_M_ENABLE
);
189 HDMI_WRITE(HDMI_SW_RESET_CONTROL
,
190 VC4_HDMI_SW_RESET_HDMI
|
191 VC4_HDMI_SW_RESET_FORMAT_DETECT
);
193 HDMI_WRITE(HDMI_SW_RESET_CONTROL
, 0);
195 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
201 static void vc5_hdmi_reset(struct vc4_hdmi
*vc4_hdmi
)
203 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
208 * We can be called by our bind callback, when the
209 * connector->dev pointer might not be initialised yet.
211 if (drm
&& !drm_dev_enter(drm
, &idx
))
214 reset_control_reset(vc4_hdmi
->reset
);
216 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
218 HDMI_WRITE(HDMI_DVP_CTL
, 0);
220 HDMI_WRITE(HDMI_CLOCK_STOP
,
221 HDMI_READ(HDMI_CLOCK_STOP
) | VC4_DVP_HT_CLOCK_STOP_PIXEL
);
223 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
229 #ifdef CONFIG_DRM_VC4_HDMI_CEC
230 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi
*vc4_hdmi
)
232 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
233 unsigned long cec_rate
;
240 * This function is called by our runtime_resume implementation
241 * and thus at bind time, when we haven't registered our
242 * connector yet and thus don't have a pointer to the DRM
245 if (drm
&& !drm_dev_enter(drm
, &idx
))
248 cec_rate
= clk_get_rate(vc4_hdmi
->cec_clock
);
250 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
252 value
= HDMI_READ(HDMI_CEC_CNTRL_1
);
253 value
&= ~VC4_HDMI_CEC_DIV_CLK_CNT_MASK
;
256 * Set the clock divider: the hsm_clock rate and this divider
257 * setting will give a 40 kHz CEC clock.
259 clk_cnt
= cec_rate
/ CEC_CLOCK_FREQ
;
260 value
|= clk_cnt
<< VC4_HDMI_CEC_DIV_CLK_CNT_SHIFT
;
261 HDMI_WRITE(HDMI_CEC_CNTRL_1
, value
);
263 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
269 static void vc4_hdmi_cec_update_clk_div(struct vc4_hdmi
*vc4_hdmi
) {}
272 static int reset_pipe(struct drm_crtc
*crtc
,
273 struct drm_modeset_acquire_ctx
*ctx
)
275 struct drm_atomic_state
*state
;
276 struct drm_crtc_state
*crtc_state
;
279 state
= drm_atomic_state_alloc(crtc
->dev
);
283 state
->acquire_ctx
= ctx
;
285 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
286 if (IS_ERR(crtc_state
)) {
287 ret
= PTR_ERR(crtc_state
);
291 crtc_state
->connectors_changed
= true;
293 ret
= drm_atomic_commit(state
);
295 drm_atomic_state_put(state
);
300 static int vc4_hdmi_reset_link(struct drm_connector
*connector
,
301 struct drm_modeset_acquire_ctx
*ctx
)
303 struct drm_device
*drm
;
304 struct vc4_hdmi
*vc4_hdmi
;
305 struct drm_connector_state
*conn_state
;
306 struct drm_crtc_state
*crtc_state
;
307 struct drm_crtc
*crtc
;
308 bool scrambling_needed
;
315 drm
= connector
->dev
;
316 ret
= drm_modeset_lock(&drm
->mode_config
.connection_mutex
, ctx
);
320 conn_state
= connector
->state
;
321 crtc
= conn_state
->crtc
;
325 ret
= drm_modeset_lock(&crtc
->mutex
, ctx
);
329 crtc_state
= crtc
->state
;
330 if (!crtc_state
->active
)
333 vc4_hdmi
= connector_to_vc4_hdmi(connector
);
334 mutex_lock(&vc4_hdmi
->mutex
);
336 if (!vc4_hdmi_supports_scrambling(vc4_hdmi
)) {
337 mutex_unlock(&vc4_hdmi
->mutex
);
341 scrambling_needed
= vc4_hdmi_mode_needs_scrambling(&vc4_hdmi
->saved_adjusted_mode
,
342 vc4_hdmi
->output_bpc
,
343 vc4_hdmi
->output_format
);
344 if (!scrambling_needed
) {
345 mutex_unlock(&vc4_hdmi
->mutex
);
349 if (conn_state
->commit
&&
350 !try_wait_for_completion(&conn_state
->commit
->hw_done
)) {
351 mutex_unlock(&vc4_hdmi
->mutex
);
355 ret
= drm_scdc_readb(connector
->ddc
, SCDC_TMDS_CONFIG
, &config
);
357 drm_err(drm
, "Failed to read TMDS config: %d\n", ret
);
358 mutex_unlock(&vc4_hdmi
->mutex
);
362 if (!!(config
& SCDC_SCRAMBLING_ENABLE
) == scrambling_needed
) {
363 mutex_unlock(&vc4_hdmi
->mutex
);
367 mutex_unlock(&vc4_hdmi
->mutex
);
370 * HDMI 2.0 says that one should not send scrambled data
371 * prior to configuring the sink scrambling, and that
372 * TMDS clock/data transmission should be suspended when
373 * changing the TMDS clock rate in the sink. So let's
374 * just do a full modeset here, even though some sinks
375 * would be perfectly happy if were to just reconfigure
376 * the SCDC settings on the fly.
378 return reset_pipe(crtc
, ctx
);
381 static void vc4_hdmi_handle_hotplug(struct vc4_hdmi
*vc4_hdmi
,
382 struct drm_modeset_acquire_ctx
*ctx
,
383 enum drm_connector_status status
)
385 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
386 const struct drm_edid
*drm_edid
;
390 * NOTE: This function should really be called with vc4_hdmi->mutex
391 * held, but doing so results in reentrancy issues since
392 * cec_s_phys_addr() might call .adap_enable, which leads to that
393 * funtion being called with our mutex held.
395 * A similar situation occurs with vc4_hdmi_reset_link() that
396 * will call into our KMS hooks if the scrambling was enabled.
398 * Concurrency isn't an issue at the moment since we don't share
399 * any state with any of the other frameworks so we can ignore
403 if (status
== connector_status_disconnected
) {
404 cec_phys_addr_invalidate(vc4_hdmi
->cec_adap
);
408 drm_edid
= drm_edid_read_ddc(connector
, vc4_hdmi
->ddc
);
410 drm_edid_connector_update(connector
, drm_edid
);
411 cec_s_phys_addr(vc4_hdmi
->cec_adap
,
412 connector
->display_info
.source_physical_address
, false);
417 drm_edid_free(drm_edid
);
420 ret
= vc4_hdmi_reset_link(connector
, ctx
);
421 if (ret
== -EDEADLK
) {
422 drm_modeset_backoff(ctx
);
430 static int vc4_hdmi_connector_detect_ctx(struct drm_connector
*connector
,
431 struct drm_modeset_acquire_ctx
*ctx
,
434 struct vc4_hdmi
*vc4_hdmi
= connector_to_vc4_hdmi(connector
);
435 enum drm_connector_status status
= connector_status_disconnected
;
439 * NOTE: This function should really take vc4_hdmi->mutex, but
440 * doing so results in reentrancy issues since
441 * vc4_hdmi_handle_hotplug() can call into other functions that
442 * would take the mutex while it's held here.
444 * Concurrency isn't an issue at the moment since we don't share
445 * any state with any of the other frameworks so we can ignore
449 ret
= pm_runtime_resume_and_get(&vc4_hdmi
->pdev
->dev
);
451 drm_err_once(connector
->dev
, "Failed to retain HDMI power domain: %d\n",
453 return connector_status_unknown
;
456 if (vc4_hdmi
->hpd_gpio
) {
457 if (gpiod_get_value_cansleep(vc4_hdmi
->hpd_gpio
))
458 status
= connector_status_connected
;
460 if (vc4_hdmi
->variant
->hp_detect
&&
461 vc4_hdmi
->variant
->hp_detect(vc4_hdmi
))
462 status
= connector_status_connected
;
465 vc4_hdmi_handle_hotplug(vc4_hdmi
, ctx
, status
);
466 pm_runtime_put(&vc4_hdmi
->pdev
->dev
);
471 static int vc4_hdmi_connector_get_modes(struct drm_connector
*connector
)
473 struct vc4_hdmi
*vc4_hdmi
= connector_to_vc4_hdmi(connector
);
474 struct vc4_dev
*vc4
= to_vc4_dev(connector
->dev
);
475 const struct drm_edid
*drm_edid
;
479 * NOTE: This function should really take vc4_hdmi->mutex, but doing so
480 * results in reentrancy issues since cec_s_phys_addr() might call
481 * .adap_enable, which leads to that funtion being called with our mutex
484 * Concurrency isn't an issue at the moment since we don't share
485 * any state with any of the other frameworks so we can ignore
489 drm_edid
= drm_edid_read_ddc(connector
, vc4_hdmi
->ddc
);
490 drm_edid_connector_update(connector
, drm_edid
);
491 cec_s_phys_addr(vc4_hdmi
->cec_adap
,
492 connector
->display_info
.source_physical_address
, false);
496 ret
= drm_edid_connector_add_modes(connector
);
497 drm_edid_free(drm_edid
);
499 if (!vc4
->hvs
->vc5_hdmi_enable_hdmi_20
) {
500 struct drm_device
*drm
= connector
->dev
;
501 const struct drm_display_mode
*mode
;
503 list_for_each_entry(mode
, &connector
->probed_modes
, head
) {
504 if (vc4_hdmi_mode_needs_scrambling(mode
, 8, HDMI_COLORSPACE_RGB
)) {
505 drm_warn_once(drm
, "The core clock cannot reach frequencies high enough to support 4k @ 60Hz.");
506 drm_warn_once(drm
, "Please change your config.txt file to add hdmi_enable_4kp60.");
514 static int vc4_hdmi_connector_atomic_check(struct drm_connector
*connector
,
515 struct drm_atomic_state
*state
)
517 struct drm_connector_state
*old_state
=
518 drm_atomic_get_old_connector_state(state
, connector
);
519 struct drm_connector_state
*new_state
=
520 drm_atomic_get_new_connector_state(state
, connector
);
521 struct drm_crtc
*crtc
= new_state
->crtc
;
526 if (old_state
->tv
.margins
.left
!= new_state
->tv
.margins
.left
||
527 old_state
->tv
.margins
.right
!= new_state
->tv
.margins
.right
||
528 old_state
->tv
.margins
.top
!= new_state
->tv
.margins
.top
||
529 old_state
->tv
.margins
.bottom
!= new_state
->tv
.margins
.bottom
) {
530 struct drm_crtc_state
*crtc_state
;
533 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
534 if (IS_ERR(crtc_state
))
535 return PTR_ERR(crtc_state
);
538 * Strictly speaking, we should be calling
539 * drm_atomic_helper_check_planes() after our call to
540 * drm_atomic_add_affected_planes(). However, the
541 * connector atomic_check is called as part of
542 * drm_atomic_helper_check_modeset() that already
543 * happens before a call to
544 * drm_atomic_helper_check_planes() in
545 * drm_atomic_helper_check().
547 ret
= drm_atomic_add_affected_planes(state
, crtc
);
552 if (old_state
->colorspace
!= new_state
->colorspace
) {
553 struct drm_crtc_state
*crtc_state
;
555 crtc_state
= drm_atomic_get_crtc_state(state
, crtc
);
556 if (IS_ERR(crtc_state
))
557 return PTR_ERR(crtc_state
);
559 crtc_state
->mode_changed
= true;
562 return drm_atomic_helper_connector_hdmi_check(connector
, state
);
565 static void vc4_hdmi_connector_reset(struct drm_connector
*connector
)
567 drm_atomic_helper_connector_reset(connector
);
568 __drm_atomic_helper_connector_hdmi_reset(connector
, connector
->state
);
569 drm_atomic_helper_connector_tv_margins_reset(connector
);
572 static const struct drm_connector_funcs vc4_hdmi_connector_funcs
= {
573 .fill_modes
= drm_helper_probe_single_connector_modes
,
574 .reset
= vc4_hdmi_connector_reset
,
575 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
576 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
579 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs
= {
580 .detect_ctx
= vc4_hdmi_connector_detect_ctx
,
581 .get_modes
= vc4_hdmi_connector_get_modes
,
582 .atomic_check
= vc4_hdmi_connector_atomic_check
,
585 static const struct drm_connector_hdmi_funcs vc4_hdmi_hdmi_connector_funcs
;
587 static int vc4_hdmi_connector_init(struct drm_device
*dev
,
588 struct vc4_hdmi
*vc4_hdmi
)
590 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
591 struct drm_encoder
*encoder
= &vc4_hdmi
->encoder
.base
;
592 unsigned int max_bpc
= 8;
595 if (vc4_hdmi
->variant
->supports_hdr
)
598 ret
= drmm_connector_hdmi_init(dev
, connector
,
599 "Broadcom", "Videocore",
600 &vc4_hdmi_connector_funcs
,
601 &vc4_hdmi_hdmi_connector_funcs
,
602 DRM_MODE_CONNECTOR_HDMIA
,
604 BIT(HDMI_COLORSPACE_RGB
) |
605 BIT(HDMI_COLORSPACE_YUV422
) |
606 BIT(HDMI_COLORSPACE_YUV444
),
611 drm_connector_helper_add(connector
, &vc4_hdmi_connector_helper_funcs
);
614 * Some of the properties below require access to state, like bpc.
615 * Allocate some default initial connector state with our reset helper.
617 if (connector
->funcs
->reset
)
618 connector
->funcs
->reset(connector
);
620 /* Create and attach TV margin props to this connector. */
621 ret
= drm_mode_create_tv_margin_properties(dev
);
625 ret
= drm_mode_create_hdmi_colorspace_property(connector
, 0);
629 drm_connector_attach_colorspace_property(connector
);
630 drm_connector_attach_tv_margin_properties(connector
);
632 connector
->polled
= (DRM_CONNECTOR_POLL_CONNECT
|
633 DRM_CONNECTOR_POLL_DISCONNECT
);
635 connector
->interlace_allowed
= 1;
636 connector
->doublescan_allowed
= 0;
637 connector
->stereo_allowed
= 1;
639 ret
= drm_connector_attach_broadcast_rgb_property(connector
);
643 drm_connector_attach_encoder(connector
, encoder
);
648 static int vc4_hdmi_stop_packet(struct vc4_hdmi
*vc4_hdmi
,
649 enum hdmi_infoframe_type type
,
652 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
653 u32 packet_id
= type
- 0x80;
658 if (!drm_dev_enter(drm
, &idx
))
661 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
662 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG
,
663 HDMI_READ(HDMI_RAM_PACKET_CONFIG
) & ~BIT(packet_id
));
664 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
667 ret
= wait_for(!(HDMI_READ(HDMI_RAM_PACKET_STATUS
) &
668 BIT(packet_id
)), 100);
675 static int vc4_hdmi_write_infoframe(struct drm_connector
*connector
,
676 enum hdmi_infoframe_type type
,
677 const u8
*infoframe
, size_t len
)
679 struct vc4_hdmi
*vc4_hdmi
= connector_to_vc4_hdmi(connector
);
680 struct drm_device
*drm
= connector
->dev
;
681 u32 packet_id
= type
- 0x80;
682 const struct vc4_hdmi_register
*ram_packet_start
=
683 &vc4_hdmi
->variant
->registers
[HDMI_RAM_PACKET_START
];
684 u32 packet_reg
= ram_packet_start
->offset
+ VC4_HDMI_PACKET_STRIDE
* packet_id
;
685 u32 packet_reg_next
= ram_packet_start
->offset
+
686 VC4_HDMI_PACKET_STRIDE
* (packet_id
+ 1);
687 void __iomem
*base
= __vc4_hdmi_get_field_base(vc4_hdmi
,
688 ram_packet_start
->reg
);
689 uint8_t buffer
[VC4_HDMI_PACKET_STRIDE
] = {};
695 if (!drm_dev_enter(drm
, &idx
))
698 if (len
> sizeof(buffer
)) {
703 memcpy(buffer
, infoframe
, len
);
705 WARN_ONCE(!(HDMI_READ(HDMI_RAM_PACKET_CONFIG
) &
706 VC4_HDMI_RAM_PACKET_ENABLE
),
707 "Packet RAM has to be on to store the packet.");
709 ret
= vc4_hdmi_stop_packet(vc4_hdmi
, type
, true);
711 drm_err(drm
, "Failed to wait for infoframe to go idle: %d\n", ret
);
715 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
717 for (i
= 0; i
< len
; i
+= 7) {
718 writel(buffer
[i
+ 0] << 0 |
724 writel(buffer
[i
+ 3] << 0 |
726 buffer
[i
+ 5] << 16 |
733 * clear remainder of packet ram as it's included in the
734 * infoframe and triggers a checksum error on hdmi analyser
736 for (; packet_reg
< packet_reg_next
; packet_reg
+= 4)
737 writel(0, base
+ packet_reg
);
739 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG
,
740 HDMI_READ(HDMI_RAM_PACKET_CONFIG
) | BIT(packet_id
));
742 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
744 ret
= wait_for((HDMI_READ(HDMI_RAM_PACKET_STATUS
) &
745 BIT(packet_id
)), 100);
747 drm_err(drm
, "Failed to wait for infoframe to start: %d\n", ret
);
754 #define SCRAMBLING_POLLING_DELAY_MS 1000
756 static void vc4_hdmi_enable_scrambling(struct drm_encoder
*encoder
)
758 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
759 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
760 struct drm_device
*drm
= connector
->dev
;
761 const struct drm_display_mode
*mode
= &vc4_hdmi
->saved_adjusted_mode
;
765 lockdep_assert_held(&vc4_hdmi
->mutex
);
767 if (!vc4_hdmi_supports_scrambling(vc4_hdmi
))
770 if (!vc4_hdmi_mode_needs_scrambling(mode
,
771 vc4_hdmi
->output_bpc
,
772 vc4_hdmi
->output_format
))
775 if (!drm_dev_enter(drm
, &idx
))
778 drm_scdc_set_high_tmds_clock_ratio(connector
, true);
779 drm_scdc_set_scrambling(connector
, true);
781 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
782 HDMI_WRITE(HDMI_SCRAMBLER_CTL
, HDMI_READ(HDMI_SCRAMBLER_CTL
) |
783 VC5_HDMI_SCRAMBLER_CTL_ENABLE
);
784 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
788 vc4_hdmi
->scdc_enabled
= true;
790 queue_delayed_work(system_wq
, &vc4_hdmi
->scrambling_work
,
791 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS
));
794 static void vc4_hdmi_disable_scrambling(struct drm_encoder
*encoder
)
796 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
797 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
798 struct drm_device
*drm
= connector
->dev
;
802 lockdep_assert_held(&vc4_hdmi
->mutex
);
804 if (!vc4_hdmi
->scdc_enabled
)
807 vc4_hdmi
->scdc_enabled
= false;
809 if (delayed_work_pending(&vc4_hdmi
->scrambling_work
))
810 cancel_delayed_work_sync(&vc4_hdmi
->scrambling_work
);
812 if (!drm_dev_enter(drm
, &idx
))
815 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
816 HDMI_WRITE(HDMI_SCRAMBLER_CTL
, HDMI_READ(HDMI_SCRAMBLER_CTL
) &
817 ~VC5_HDMI_SCRAMBLER_CTL_ENABLE
);
818 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
820 drm_scdc_set_scrambling(connector
, false);
821 drm_scdc_set_high_tmds_clock_ratio(connector
, false);
826 static void vc4_hdmi_scrambling_wq(struct work_struct
*work
)
828 struct vc4_hdmi
*vc4_hdmi
= container_of(to_delayed_work(work
),
831 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
833 if (drm_scdc_get_scrambling_status(connector
))
836 drm_scdc_set_high_tmds_clock_ratio(connector
, true);
837 drm_scdc_set_scrambling(connector
, true);
839 queue_delayed_work(system_wq
, &vc4_hdmi
->scrambling_work
,
840 msecs_to_jiffies(SCRAMBLING_POLLING_DELAY_MS
));
843 static void vc4_hdmi_encoder_post_crtc_disable(struct drm_encoder
*encoder
,
844 struct drm_atomic_state
*state
)
846 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
847 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
851 mutex_lock(&vc4_hdmi
->mutex
);
853 vc4_hdmi
->packet_ram_enabled
= false;
855 if (!drm_dev_enter(drm
, &idx
))
858 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
860 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG
, 0);
862 HDMI_WRITE(HDMI_VID_CTL
, HDMI_READ(HDMI_VID_CTL
) | VC4_HD_VID_CTL_CLRRGB
);
864 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
868 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
869 HDMI_WRITE(HDMI_VID_CTL
,
870 HDMI_READ(HDMI_VID_CTL
) & ~VC4_HD_VID_CTL_ENABLE
);
871 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
873 vc4_hdmi_disable_scrambling(encoder
);
878 mutex_unlock(&vc4_hdmi
->mutex
);
881 static void vc4_hdmi_encoder_post_crtc_powerdown(struct drm_encoder
*encoder
,
882 struct drm_atomic_state
*state
)
884 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
885 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
890 mutex_lock(&vc4_hdmi
->mutex
);
892 if (!drm_dev_enter(drm
, &idx
))
895 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
896 HDMI_WRITE(HDMI_VID_CTL
,
897 HDMI_READ(HDMI_VID_CTL
) | VC4_HD_VID_CTL_BLANKPIX
);
898 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
900 if (vc4_hdmi
->variant
->phy_disable
)
901 vc4_hdmi
->variant
->phy_disable(vc4_hdmi
);
903 clk_disable_unprepare(vc4_hdmi
->pixel_bvb_clock
);
904 clk_disable_unprepare(vc4_hdmi
->pixel_clock
);
906 ret
= pm_runtime_put(&vc4_hdmi
->pdev
->dev
);
908 drm_err(drm
, "Failed to release power domain: %d\n", ret
);
913 mutex_unlock(&vc4_hdmi
->mutex
);
916 static void vc4_hdmi_csc_setup(struct vc4_hdmi
*vc4_hdmi
,
917 struct drm_connector_state
*state
,
918 const struct drm_display_mode
*mode
)
920 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
925 if (!drm_dev_enter(drm
, &idx
))
928 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
930 csc_ctl
= VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR
,
931 VC4_HD_CSC_CTL_ORDER
);
933 if (state
->hdmi
.is_limited_range
) {
934 /* CEA VICs other than #1 requre limited range RGB
935 * output unless overridden by an AVI infoframe.
936 * Apply a colorspace conversion to squash 0-255 down
937 * to 16-235. The matrix here is:
944 csc_ctl
|= VC4_HD_CSC_CTL_ENABLE
;
945 csc_ctl
|= VC4_HD_CSC_CTL_RGB2YCC
;
946 csc_ctl
|= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM
,
947 VC4_HD_CSC_CTL_MODE
);
949 HDMI_WRITE(HDMI_CSC_12_11
, (0x000 << 16) | 0x000);
950 HDMI_WRITE(HDMI_CSC_14_13
, (0x100 << 16) | 0x6e0);
951 HDMI_WRITE(HDMI_CSC_22_21
, (0x6e0 << 16) | 0x000);
952 HDMI_WRITE(HDMI_CSC_24_23
, (0x100 << 16) | 0x000);
953 HDMI_WRITE(HDMI_CSC_32_31
, (0x000 << 16) | 0x6e0);
954 HDMI_WRITE(HDMI_CSC_34_33
, (0x100 << 16) | 0x000);
957 /* The RGB order applies even when CSC is disabled. */
958 HDMI_WRITE(HDMI_CSC_CTL
, csc_ctl
);
960 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
966 * Matrices for (internal) RGB to RGB output.
968 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
970 static const u16 vc5_hdmi_csc_full_rgb_to_rgb
[2][3][4] = {
979 { 0x2000, 0x0000, 0x0000, 0x0000 },
980 { 0x0000, 0x2000, 0x0000, 0x0000 },
981 { 0x0000, 0x0000, 0x2000, 0x0000 },
987 * CEA VICs other than #1 require limited range RGB
988 * output unless overridden by an AVI infoframe. Apply a
989 * colorspace conversion to squash 0-255 down to 16-235.
990 * The matrix here is:
996 { 0x1b80, 0x0000, 0x0000, 0x0400 },
997 { 0x0000, 0x1b80, 0x0000, 0x0400 },
998 { 0x0000, 0x0000, 0x1b80, 0x0400 },
1003 * Conversion between Full Range RGB and YUV using the BT.601 Colorspace
1005 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1007 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt601
[2][3][4] = {
1012 * [ 0.299000 0.587000 0.114000 0 ]
1013 * [ -0.168736 -0.331264 0.500000 128 ]
1014 * [ 0.500000 -0.418688 -0.081312 128 ]
1016 { 0x0991, 0x12c9, 0x03a6, 0x0000 },
1017 { 0xfa9b, 0xf567, 0x1000, 0x2000 },
1018 { 0x1000, 0xf29b, 0xfd67, 0x2000 },
1023 * [ 0.255785 0.502160 0.097523 16 ]
1024 * [ -0.147644 -0.289856 0.437500 128 ]
1025 * [ 0.437500 -0.366352 -0.071148 128 ]
1027 { 0x082f, 0x1012, 0x031f, 0x0400 },
1028 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1029 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1034 * Conversion between Full Range RGB and YUV using the BT.709 Colorspace
1036 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1038 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt709
[2][3][4] = {
1043 * [ 0.212600 0.715200 0.072200 0 ]
1044 * [ -0.114572 -0.385428 0.500000 128 ]
1045 * [ 0.500000 -0.454153 -0.045847 128 ]
1047 { 0x06ce, 0x16e3, 0x024f, 0x0000 },
1048 { 0xfc56, 0xf3ac, 0x1000, 0x2000 },
1049 { 0x1000, 0xf179, 0xfe89, 0x2000 },
1055 * [ 0.181906 0.611804 0.061758 16 ]
1056 * [ -0.100268 -0.337232 0.437500 128 ]
1057 * [ 0.437500 -0.397386 -0.040114 128 ]
1059 { 0x05d2, 0x1394, 0x01fa, 0x0400 },
1060 { 0xfccc, 0xf536, 0x0e00, 0x2000 },
1061 { 0x0e00, 0xf34a, 0xfeb8, 0x2000 },
1066 * Conversion between Full Range RGB and YUV using the BT.2020 Colorspace
1068 * Matrices are signed 2p13 fixed point, with signed 9p6 offsets
1070 static const u16 vc5_hdmi_csc_full_rgb_to_yuv_bt2020
[2][3][4] = {
1075 * [ 0.262700 0.678000 0.059300 0 ]
1076 * [ -0.139630 -0.360370 0.500000 128 ]
1077 * [ 0.500000 -0.459786 -0.040214 128 ]
1079 { 0x0868, 0x15b2, 0x01e6, 0x0000 },
1080 { 0xfb89, 0xf479, 0x1000, 0x2000 },
1081 { 0x1000, 0xf14a, 0xfeb8, 0x2000 },
1086 * [ 0.224732 0.580008 0.050729 16 ]
1087 * [ -0.122176 -0.315324 0.437500 128 ]
1088 * [ 0.437500 -0.402312 -0.035188 128 ]
1090 { 0x082f, 0x1012, 0x031f, 0x0400 },
1091 { 0xfb48, 0xf6ba, 0x0e00, 0x2000 },
1092 { 0x0e00, 0xf448, 0xfdba, 0x2000 },
1096 static void vc5_hdmi_set_csc_coeffs(struct vc4_hdmi
*vc4_hdmi
,
1097 const u16 coeffs
[3][4])
1099 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
1101 HDMI_WRITE(HDMI_CSC_12_11
, (coeffs
[0][1] << 16) | coeffs
[0][0]);
1102 HDMI_WRITE(HDMI_CSC_14_13
, (coeffs
[0][3] << 16) | coeffs
[0][2]);
1103 HDMI_WRITE(HDMI_CSC_22_21
, (coeffs
[1][1] << 16) | coeffs
[1][0]);
1104 HDMI_WRITE(HDMI_CSC_24_23
, (coeffs
[1][3] << 16) | coeffs
[1][2]);
1105 HDMI_WRITE(HDMI_CSC_32_31
, (coeffs
[2][1] << 16) | coeffs
[2][0]);
1106 HDMI_WRITE(HDMI_CSC_34_33
, (coeffs
[2][3] << 16) | coeffs
[2][2]);
1109 static void vc5_hdmi_set_csc_coeffs_swap(struct vc4_hdmi
*vc4_hdmi
,
1110 const u16 coeffs
[3][4])
1112 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
1114 /* YUV444 needs the CSC matrices using the channels in a different order */
1115 HDMI_WRITE(HDMI_CSC_12_11
, (coeffs
[1][1] << 16) | coeffs
[1][0]);
1116 HDMI_WRITE(HDMI_CSC_14_13
, (coeffs
[1][3] << 16) | coeffs
[1][2]);
1117 HDMI_WRITE(HDMI_CSC_22_21
, (coeffs
[2][1] << 16) | coeffs
[2][0]);
1118 HDMI_WRITE(HDMI_CSC_24_23
, (coeffs
[2][3] << 16) | coeffs
[2][2]);
1119 HDMI_WRITE(HDMI_CSC_32_31
, (coeffs
[0][1] << 16) | coeffs
[0][0]);
1120 HDMI_WRITE(HDMI_CSC_34_33
, (coeffs
[0][3] << 16) | coeffs
[0][2]);
1124 (*vc5_hdmi_find_yuv_csc_coeffs(struct vc4_hdmi
*vc4_hdmi
, u32 colorspace
, bool limited
))[4]
1126 switch (colorspace
) {
1127 case DRM_MODE_COLORIMETRY_SMPTE_170M_YCC
:
1128 case DRM_MODE_COLORIMETRY_XVYCC_601
:
1129 case DRM_MODE_COLORIMETRY_SYCC_601
:
1130 case DRM_MODE_COLORIMETRY_OPYCC_601
:
1131 case DRM_MODE_COLORIMETRY_BT601_YCC
:
1132 return vc5_hdmi_csc_full_rgb_to_yuv_bt601
[limited
];
1135 case DRM_MODE_COLORIMETRY_NO_DATA
:
1136 case DRM_MODE_COLORIMETRY_BT709_YCC
:
1137 case DRM_MODE_COLORIMETRY_XVYCC_709
:
1138 case DRM_MODE_COLORIMETRY_RGB_WIDE_FIXED
:
1139 case DRM_MODE_COLORIMETRY_RGB_WIDE_FLOAT
:
1140 return vc5_hdmi_csc_full_rgb_to_yuv_bt709
[limited
];
1142 case DRM_MODE_COLORIMETRY_BT2020_CYCC
:
1143 case DRM_MODE_COLORIMETRY_BT2020_YCC
:
1144 case DRM_MODE_COLORIMETRY_BT2020_RGB
:
1145 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_D65
:
1146 case DRM_MODE_COLORIMETRY_DCI_P3_RGB_THEATER
:
1147 return vc5_hdmi_csc_full_rgb_to_yuv_bt2020
[limited
];
1151 static void vc5_hdmi_csc_setup(struct vc4_hdmi
*vc4_hdmi
,
1152 struct drm_connector_state
*state
,
1153 const struct drm_display_mode
*mode
)
1155 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1156 unsigned int lim_range
= state
->hdmi
.is_limited_range
? 1 : 0;
1157 unsigned long flags
;
1158 const u16 (*csc
)[4];
1160 u32 if_xbar
= 0x543210;
1161 u32 csc_chan_ctl
= 0;
1162 u32 csc_ctl
= VC5_MT_CP_CSC_CTL_ENABLE
| VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM
,
1163 VC5_MT_CP_CSC_CTL_MODE
);
1166 if (!drm_dev_enter(drm
, &idx
))
1169 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1171 switch (state
->hdmi
.output_format
) {
1172 case HDMI_COLORSPACE_YUV444
:
1173 csc
= vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi
, state
->colorspace
, !!lim_range
);
1175 vc5_hdmi_set_csc_coeffs_swap(vc4_hdmi
, csc
);
1178 case HDMI_COLORSPACE_YUV422
:
1179 csc
= vc5_hdmi_find_yuv_csc_coeffs(vc4_hdmi
, state
->colorspace
, !!lim_range
);
1181 csc_ctl
|= VC4_SET_FIELD(VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422_STANDARD
,
1182 VC5_MT_CP_CSC_CTL_FILTER_MODE_444_TO_422
) |
1183 VC5_MT_CP_CSC_CTL_USE_444_TO_422
|
1184 VC5_MT_CP_CSC_CTL_USE_RNG_SUPPRESSION
;
1186 csc_chan_ctl
|= VC4_SET_FIELD(VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP_LEGACY_STYLE
,
1187 VC5_MT_CP_CHANNEL_CTL_OUTPUT_REMAP
);
1189 if_cfg
|= VC4_SET_FIELD(VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422_FORMAT_422_LEGACY
,
1190 VC5_DVP_HT_VEC_INTERFACE_CFG_SEL_422
);
1192 vc5_hdmi_set_csc_coeffs(vc4_hdmi
, csc
);
1195 case HDMI_COLORSPACE_RGB
:
1198 vc5_hdmi_set_csc_coeffs(vc4_hdmi
, vc5_hdmi_csc_full_rgb_to_rgb
[lim_range
]);
1205 HDMI_WRITE(HDMI_VEC_INTERFACE_CFG
, if_cfg
);
1206 HDMI_WRITE(HDMI_VEC_INTERFACE_XBAR
, if_xbar
);
1207 HDMI_WRITE(HDMI_CSC_CHANNEL_CTL
, csc_chan_ctl
);
1208 HDMI_WRITE(HDMI_CSC_CTL
, csc_ctl
);
1210 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1215 static void vc4_hdmi_set_timings(struct vc4_hdmi
*vc4_hdmi
,
1216 struct drm_connector_state
*state
,
1217 const struct drm_display_mode
*mode
)
1219 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1220 bool hsync_pos
= mode
->flags
& DRM_MODE_FLAG_PHSYNC
;
1221 bool vsync_pos
= mode
->flags
& DRM_MODE_FLAG_PVSYNC
;
1222 bool interlaced
= mode
->flags
& DRM_MODE_FLAG_INTERLACE
;
1223 u32 pixel_rep
= (mode
->flags
& DRM_MODE_FLAG_DBLCLK
) ? 2 : 1;
1224 u32 verta
= (VC4_SET_FIELD(mode
->crtc_vsync_end
- mode
->crtc_vsync_start
,
1225 VC4_HDMI_VERTA_VSP
) |
1226 VC4_SET_FIELD(mode
->crtc_vsync_start
- mode
->crtc_vdisplay
,
1227 VC4_HDMI_VERTA_VFP
) |
1228 VC4_SET_FIELD(mode
->crtc_vdisplay
, VC4_HDMI_VERTA_VAL
));
1229 u32 vertb
= (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO
) |
1230 VC4_SET_FIELD(mode
->crtc_vtotal
- mode
->crtc_vsync_end
+
1232 VC4_HDMI_VERTB_VBP
));
1233 u32 vertb_even
= (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO
) |
1234 VC4_SET_FIELD(mode
->crtc_vtotal
-
1235 mode
->crtc_vsync_end
,
1236 VC4_HDMI_VERTB_VBP
));
1237 unsigned long flags
;
1241 if (!drm_dev_enter(drm
, &idx
))
1244 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1246 HDMI_WRITE(HDMI_HORZA
,
1247 (vsync_pos
? VC4_HDMI_HORZA_VPOS
: 0) |
1248 (hsync_pos
? VC4_HDMI_HORZA_HPOS
: 0) |
1249 VC4_SET_FIELD(mode
->hdisplay
* pixel_rep
,
1250 VC4_HDMI_HORZA_HAP
));
1252 HDMI_WRITE(HDMI_HORZB
,
1253 VC4_SET_FIELD((mode
->htotal
-
1254 mode
->hsync_end
) * pixel_rep
,
1255 VC4_HDMI_HORZB_HBP
) |
1256 VC4_SET_FIELD((mode
->hsync_end
-
1257 mode
->hsync_start
) * pixel_rep
,
1258 VC4_HDMI_HORZB_HSP
) |
1259 VC4_SET_FIELD((mode
->hsync_start
-
1260 mode
->hdisplay
) * pixel_rep
,
1261 VC4_HDMI_HORZB_HFP
));
1263 HDMI_WRITE(HDMI_VERTA0
, verta
);
1264 HDMI_WRITE(HDMI_VERTA1
, verta
);
1266 HDMI_WRITE(HDMI_VERTB0
, vertb_even
);
1267 HDMI_WRITE(HDMI_VERTB1
, vertb
);
1269 reg
= HDMI_READ(HDMI_MISC_CONTROL
);
1270 reg
&= ~VC4_HDMI_MISC_CONTROL_PIXEL_REP_MASK
;
1271 reg
|= VC4_SET_FIELD(pixel_rep
- 1, VC4_HDMI_MISC_CONTROL_PIXEL_REP
);
1272 HDMI_WRITE(HDMI_MISC_CONTROL
, reg
);
1274 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1279 static void vc5_hdmi_set_timings(struct vc4_hdmi
*vc4_hdmi
,
1280 struct drm_connector_state
*state
,
1281 const struct drm_display_mode
*mode
)
1283 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1284 bool hsync_pos
= mode
->flags
& DRM_MODE_FLAG_PHSYNC
;
1285 bool vsync_pos
= mode
->flags
& DRM_MODE_FLAG_PVSYNC
;
1286 bool interlaced
= mode
->flags
& DRM_MODE_FLAG_INTERLACE
;
1287 u32 pixel_rep
= (mode
->flags
& DRM_MODE_FLAG_DBLCLK
) ? 2 : 1;
1288 u32 verta
= (VC4_SET_FIELD(mode
->crtc_vsync_end
- mode
->crtc_vsync_start
,
1289 VC5_HDMI_VERTA_VSP
) |
1290 VC4_SET_FIELD(mode
->crtc_vsync_start
- mode
->crtc_vdisplay
,
1291 VC5_HDMI_VERTA_VFP
) |
1292 VC4_SET_FIELD(mode
->crtc_vdisplay
, VC5_HDMI_VERTA_VAL
));
1293 u32 vertb
= (VC4_SET_FIELD(mode
->htotal
>> (2 - pixel_rep
),
1294 VC5_HDMI_VERTB_VSPO
) |
1295 VC4_SET_FIELD(mode
->crtc_vtotal
- mode
->crtc_vsync_end
+
1297 VC4_HDMI_VERTB_VBP
));
1298 u32 vertb_even
= (VC4_SET_FIELD(0, VC5_HDMI_VERTB_VSPO
) |
1299 VC4_SET_FIELD(mode
->crtc_vtotal
-
1300 mode
->crtc_vsync_end
,
1301 VC4_HDMI_VERTB_VBP
));
1302 unsigned long flags
;
1307 if (!drm_dev_enter(drm
, &idx
))
1310 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1312 HDMI_WRITE(HDMI_HORZA
,
1313 (vsync_pos
? VC5_HDMI_HORZA_VPOS
: 0) |
1314 (hsync_pos
? VC5_HDMI_HORZA_HPOS
: 0) |
1315 VC4_SET_FIELD(mode
->hdisplay
* pixel_rep
,
1316 VC5_HDMI_HORZA_HAP
) |
1317 VC4_SET_FIELD((mode
->hsync_start
-
1318 mode
->hdisplay
) * pixel_rep
,
1319 VC5_HDMI_HORZA_HFP
));
1321 HDMI_WRITE(HDMI_HORZB
,
1322 VC4_SET_FIELD((mode
->htotal
-
1323 mode
->hsync_end
) * pixel_rep
,
1324 VC5_HDMI_HORZB_HBP
) |
1325 VC4_SET_FIELD((mode
->hsync_end
-
1326 mode
->hsync_start
) * pixel_rep
,
1327 VC5_HDMI_HORZB_HSP
));
1329 HDMI_WRITE(HDMI_VERTA0
, verta
);
1330 HDMI_WRITE(HDMI_VERTA1
, verta
);
1332 HDMI_WRITE(HDMI_VERTB0
, vertb_even
);
1333 HDMI_WRITE(HDMI_VERTB1
, vertb
);
1335 switch (state
->hdmi
.output_bpc
) {
1349 * YCC422 is always 36-bit and not considered deep colour so
1350 * doesn't signal in GCP.
1352 if (state
->hdmi
.output_format
== HDMI_COLORSPACE_YUV422
) {
1356 reg
= HDMI_READ(HDMI_DEEP_COLOR_CONFIG_1
);
1357 reg
&= ~(VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE_MASK
|
1358 VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH_MASK
);
1359 reg
|= VC4_SET_FIELD(2, VC5_HDMI_DEEP_COLOR_CONFIG_1_INIT_PACK_PHASE
) |
1360 VC4_SET_FIELD(gcp
, VC5_HDMI_DEEP_COLOR_CONFIG_1_COLOR_DEPTH
);
1361 HDMI_WRITE(HDMI_DEEP_COLOR_CONFIG_1
, reg
);
1363 reg
= HDMI_READ(HDMI_GCP_WORD_1
);
1364 reg
&= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1_MASK
;
1365 reg
|= VC4_SET_FIELD(gcp
, VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_1
);
1366 reg
&= ~VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_MASK
;
1367 reg
|= VC5_HDMI_GCP_WORD_1_GCP_SUBPACKET_BYTE_0_CLEAR_AVMUTE
;
1368 HDMI_WRITE(HDMI_GCP_WORD_1
, reg
);
1370 reg
= HDMI_READ(HDMI_GCP_CONFIG
);
1371 reg
|= VC5_HDMI_GCP_CONFIG_GCP_ENABLE
;
1372 HDMI_WRITE(HDMI_GCP_CONFIG
, reg
);
1374 reg
= HDMI_READ(HDMI_MISC_CONTROL
);
1375 reg
&= ~VC5_HDMI_MISC_CONTROL_PIXEL_REP_MASK
;
1376 reg
|= VC4_SET_FIELD(pixel_rep
- 1, VC5_HDMI_MISC_CONTROL_PIXEL_REP
);
1377 HDMI_WRITE(HDMI_MISC_CONTROL
, reg
);
1379 HDMI_WRITE(HDMI_CLOCK_STOP
, 0);
1381 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1386 static void vc4_hdmi_recenter_fifo(struct vc4_hdmi
*vc4_hdmi
)
1388 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1389 unsigned long flags
;
1394 if (!drm_dev_enter(drm
, &idx
))
1397 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1399 drift
= HDMI_READ(HDMI_FIFO_CTL
);
1400 drift
&= VC4_HDMI_FIFO_VALID_WRITE_MASK
;
1402 HDMI_WRITE(HDMI_FIFO_CTL
,
1403 drift
& ~VC4_HDMI_FIFO_CTL_RECENTER
);
1404 HDMI_WRITE(HDMI_FIFO_CTL
,
1405 drift
| VC4_HDMI_FIFO_CTL_RECENTER
);
1407 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1409 usleep_range(1000, 1100);
1411 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1413 HDMI_WRITE(HDMI_FIFO_CTL
,
1414 drift
& ~VC4_HDMI_FIFO_CTL_RECENTER
);
1415 HDMI_WRITE(HDMI_FIFO_CTL
,
1416 drift
| VC4_HDMI_FIFO_CTL_RECENTER
);
1418 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1420 ret
= wait_for(HDMI_READ(HDMI_FIFO_CTL
) &
1421 VC4_HDMI_FIFO_CTL_RECENTER_DONE
, 1);
1422 WARN_ONCE(ret
, "Timeout waiting for "
1423 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
1428 static void vc4_hdmi_encoder_pre_crtc_configure(struct drm_encoder
*encoder
,
1429 struct drm_atomic_state
*state
)
1431 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1432 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1433 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
1434 struct drm_connector_state
*conn_state
=
1435 drm_atomic_get_new_connector_state(state
, connector
);
1436 const struct drm_display_mode
*mode
= &vc4_hdmi
->saved_adjusted_mode
;
1437 unsigned long long tmds_char_rate
= conn_state
->hdmi
.tmds_char_rate
;
1438 unsigned long bvb_rate
, hsm_rate
;
1439 unsigned long flags
;
1443 mutex_lock(&vc4_hdmi
->mutex
);
1445 if (!drm_dev_enter(drm
, &idx
))
1448 ret
= pm_runtime_resume_and_get(&vc4_hdmi
->pdev
->dev
);
1450 drm_err(drm
, "Failed to retain power domain: %d\n", ret
);
1455 * As stated in RPi's vc4 firmware "HDMI state machine (HSM) clock must
1456 * be faster than pixel clock, infinitesimally faster, tested in
1457 * simulation. Otherwise, exact value is unimportant for HDMI
1458 * operation." This conflicts with bcm2835's vc4 documentation, which
1459 * states HSM's clock has to be at least 108% of the pixel clock.
1461 * Real life tests reveal that vc4's firmware statement holds up, and
1462 * users are able to use pixel clocks closer to HSM's, namely for
1463 * 1920x1200@60Hz. So it was decided to have leave a 1% margin between
1464 * both clocks. Which, for RPi0-3 implies a maximum pixel clock of
1467 * Additionally, the AXI clock needs to be at least 25% of
1468 * pixel clock, but HSM ends up being the limiting factor.
1470 hsm_rate
= max_t(unsigned long,
1472 div_u64(tmds_char_rate
, 100) * 101);
1473 ret
= clk_set_min_rate(vc4_hdmi
->hsm_clock
, hsm_rate
);
1475 drm_err(drm
, "Failed to set HSM clock rate: %d\n", ret
);
1476 goto err_put_runtime_pm
;
1479 ret
= clk_set_rate(vc4_hdmi
->pixel_clock
, tmds_char_rate
);
1481 drm_err(drm
, "Failed to set pixel clock rate: %d\n", ret
);
1482 goto err_put_runtime_pm
;
1485 ret
= clk_prepare_enable(vc4_hdmi
->pixel_clock
);
1487 drm_err(drm
, "Failed to turn on pixel clock: %d\n", ret
);
1488 goto err_put_runtime_pm
;
1492 vc4_hdmi_cec_update_clk_div(vc4_hdmi
);
1494 if (tmds_char_rate
> 297000000)
1495 bvb_rate
= 300000000;
1496 else if (tmds_char_rate
> 148500000)
1497 bvb_rate
= 150000000;
1499 bvb_rate
= 75000000;
1501 ret
= clk_set_min_rate(vc4_hdmi
->pixel_bvb_clock
, bvb_rate
);
1503 drm_err(drm
, "Failed to set pixel bvb clock rate: %d\n", ret
);
1504 goto err_disable_pixel_clock
;
1507 ret
= clk_prepare_enable(vc4_hdmi
->pixel_bvb_clock
);
1509 drm_err(drm
, "Failed to turn on pixel bvb clock: %d\n", ret
);
1510 goto err_disable_pixel_clock
;
1513 if (vc4_hdmi
->variant
->phy_init
)
1514 vc4_hdmi
->variant
->phy_init(vc4_hdmi
, conn_state
);
1516 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1518 HDMI_WRITE(HDMI_SCHEDULER_CONTROL
,
1519 HDMI_READ(HDMI_SCHEDULER_CONTROL
) |
1520 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT
|
1521 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS
);
1523 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1525 if (vc4_hdmi
->variant
->set_timings
)
1526 vc4_hdmi
->variant
->set_timings(vc4_hdmi
, conn_state
, mode
);
1530 mutex_unlock(&vc4_hdmi
->mutex
);
1534 err_disable_pixel_clock
:
1535 clk_disable_unprepare(vc4_hdmi
->pixel_clock
);
1537 pm_runtime_put(&vc4_hdmi
->pdev
->dev
);
1541 mutex_unlock(&vc4_hdmi
->mutex
);
1545 static void vc4_hdmi_encoder_pre_crtc_enable(struct drm_encoder
*encoder
,
1546 struct drm_atomic_state
*state
)
1548 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1549 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1550 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
1551 const struct drm_display_mode
*mode
= &vc4_hdmi
->saved_adjusted_mode
;
1552 struct drm_connector_state
*conn_state
=
1553 drm_atomic_get_new_connector_state(state
, connector
);
1554 unsigned long flags
;
1557 mutex_lock(&vc4_hdmi
->mutex
);
1559 if (!drm_dev_enter(drm
, &idx
))
1562 if (vc4_hdmi
->variant
->csc_setup
)
1563 vc4_hdmi
->variant
->csc_setup(vc4_hdmi
, conn_state
, mode
);
1565 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1566 HDMI_WRITE(HDMI_FIFO_CTL
, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N
);
1567 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1572 mutex_unlock(&vc4_hdmi
->mutex
);
1575 static void vc4_hdmi_encoder_post_crtc_enable(struct drm_encoder
*encoder
,
1576 struct drm_atomic_state
*state
)
1578 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1579 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
1580 struct drm_device
*drm
= connector
->dev
;
1581 const struct drm_display_mode
*mode
= &vc4_hdmi
->saved_adjusted_mode
;
1582 struct drm_display_info
*display
= &vc4_hdmi
->connector
.display_info
;
1583 bool hsync_pos
= mode
->flags
& DRM_MODE_FLAG_PHSYNC
;
1584 bool vsync_pos
= mode
->flags
& DRM_MODE_FLAG_PVSYNC
;
1585 unsigned long flags
;
1589 mutex_lock(&vc4_hdmi
->mutex
);
1591 if (!drm_dev_enter(drm
, &idx
))
1594 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1596 HDMI_WRITE(HDMI_VID_CTL
,
1597 VC4_HD_VID_CTL_ENABLE
|
1598 VC4_HD_VID_CTL_CLRRGB
|
1599 VC4_HD_VID_CTL_UNDERFLOW_ENABLE
|
1600 VC4_HD_VID_CTL_FRAME_COUNTER_RESET
|
1601 VC4_HD_VID_CTL_BLANK_INSERT_EN
|
1602 (vsync_pos
? 0 : VC4_HD_VID_CTL_VSYNC_LOW
) |
1603 (hsync_pos
? 0 : VC4_HD_VID_CTL_HSYNC_LOW
));
1605 HDMI_WRITE(HDMI_VID_CTL
,
1606 HDMI_READ(HDMI_VID_CTL
) & ~VC4_HD_VID_CTL_BLANKPIX
);
1608 if (display
->is_hdmi
) {
1609 HDMI_WRITE(HDMI_SCHEDULER_CONTROL
,
1610 HDMI_READ(HDMI_SCHEDULER_CONTROL
) |
1611 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI
);
1613 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1615 ret
= wait_for(HDMI_READ(HDMI_SCHEDULER_CONTROL
) &
1616 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
, 1000);
1617 WARN_ONCE(ret
, "Timeout waiting for "
1618 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1620 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG
,
1621 HDMI_READ(HDMI_RAM_PACKET_CONFIG
) &
1622 ~(VC4_HDMI_RAM_PACKET_ENABLE
));
1623 HDMI_WRITE(HDMI_SCHEDULER_CONTROL
,
1624 HDMI_READ(HDMI_SCHEDULER_CONTROL
) &
1625 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI
);
1627 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1629 ret
= wait_for(!(HDMI_READ(HDMI_SCHEDULER_CONTROL
) &
1630 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
), 1000);
1631 WARN_ONCE(ret
, "Timeout waiting for "
1632 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
1635 if (display
->is_hdmi
) {
1636 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1638 WARN_ON(!(HDMI_READ(HDMI_SCHEDULER_CONTROL
) &
1639 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
));
1641 HDMI_WRITE(HDMI_RAM_PACKET_CONFIG
,
1642 VC4_HDMI_RAM_PACKET_ENABLE
);
1644 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1645 vc4_hdmi
->packet_ram_enabled
= true;
1647 drm_atomic_helper_connector_hdmi_update_infoframes(connector
, state
);
1650 vc4_hdmi_recenter_fifo(vc4_hdmi
);
1651 vc4_hdmi_enable_scrambling(encoder
);
1656 mutex_unlock(&vc4_hdmi
->mutex
);
1659 static void vc4_hdmi_encoder_atomic_mode_set(struct drm_encoder
*encoder
,
1660 struct drm_crtc_state
*crtc_state
,
1661 struct drm_connector_state
*conn_state
)
1663 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1665 mutex_lock(&vc4_hdmi
->mutex
);
1666 drm_mode_copy(&vc4_hdmi
->saved_adjusted_mode
,
1667 &crtc_state
->adjusted_mode
);
1668 vc4_hdmi
->output_bpc
= conn_state
->hdmi
.output_bpc
;
1669 vc4_hdmi
->output_format
= conn_state
->hdmi
.output_format
;
1670 mutex_unlock(&vc4_hdmi
->mutex
);
1673 static enum drm_mode_status
1674 vc4_hdmi_connector_clock_valid(const struct drm_connector
*connector
,
1675 const struct drm_display_mode
*mode
,
1676 unsigned long long clock
)
1678 const struct vc4_hdmi
*vc4_hdmi
= connector_to_vc4_hdmi(connector
);
1679 struct vc4_dev
*vc4
= to_vc4_dev(connector
->dev
);
1681 if (clock
> vc4_hdmi
->variant
->max_pixel_clock
)
1682 return MODE_CLOCK_HIGH
;
1684 if (!vc4
->hvs
->vc5_hdmi_enable_hdmi_20
&& clock
> HDMI_14_MAX_TMDS_CLK
)
1685 return MODE_CLOCK_HIGH
;
1687 /* 4096x2160@60 is not reliable without overclocking core */
1688 if (!vc4
->hvs
->vc5_hdmi_enable_4096by2160
&&
1689 mode
->hdisplay
> 3840 && mode
->vdisplay
>= 2160 &&
1690 drm_mode_vrefresh(mode
) >= 50)
1691 return MODE_CLOCK_HIGH
;
1696 static const struct drm_connector_hdmi_funcs vc4_hdmi_hdmi_connector_funcs
= {
1697 .tmds_char_rate_valid
= vc4_hdmi_connector_clock_valid
,
1698 .write_infoframe
= vc4_hdmi_write_infoframe
,
1701 #define WIFI_2_4GHz_CH1_MIN_FREQ 2400000000ULL
1702 #define WIFI_2_4GHz_CH1_MAX_FREQ 2422000000ULL
1704 static int vc4_hdmi_encoder_atomic_check(struct drm_encoder
*encoder
,
1705 struct drm_crtc_state
*crtc_state
,
1706 struct drm_connector_state
*conn_state
)
1708 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1709 struct drm_display_mode
*mode
= &crtc_state
->adjusted_mode
;
1710 unsigned long long tmds_char_rate
= mode
->clock
* 1000;
1711 unsigned long long tmds_bit_rate
;
1713 if (vc4_hdmi
->variant
->unsupported_odd_h_timings
) {
1714 if (mode
->flags
& DRM_MODE_FLAG_DBLCLK
) {
1715 /* Only try to fixup DBLCLK modes to get 480i and 576i
1717 * A generic solution for all modes with odd horizontal
1718 * timing values seems impossible based on trying to
1719 * solve it for 1366x768 monitors.
1721 if ((mode
->hsync_start
- mode
->hdisplay
) & 1)
1722 mode
->hsync_start
--;
1723 if ((mode
->hsync_end
- mode
->hsync_start
) & 1)
1727 /* Now check whether we still have odd values remaining */
1728 if ((mode
->hdisplay
% 2) || (mode
->hsync_start
% 2) ||
1729 (mode
->hsync_end
% 2) || (mode
->htotal
% 2))
1734 * The 1440p@60 pixel rate is in the same range than the first
1735 * WiFi channel (between 2.4GHz and 2.422GHz with 22MHz
1736 * bandwidth). Slightly lower the frequency to bring it out of
1739 tmds_bit_rate
= tmds_char_rate
* 10;
1740 if (vc4_hdmi
->disable_wifi_frequencies
&&
1741 (tmds_bit_rate
>= WIFI_2_4GHz_CH1_MIN_FREQ
&&
1742 tmds_bit_rate
<= WIFI_2_4GHz_CH1_MAX_FREQ
)) {
1743 mode
->clock
= 238560;
1744 tmds_char_rate
= mode
->clock
* 1000;
1750 static enum drm_mode_status
1751 vc4_hdmi_encoder_mode_valid(struct drm_encoder
*encoder
,
1752 const struct drm_display_mode
*mode
)
1754 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1755 unsigned long long rate
;
1757 if (vc4_hdmi
->variant
->unsupported_odd_h_timings
&&
1758 !(mode
->flags
& DRM_MODE_FLAG_DBLCLK
) &&
1759 ((mode
->hdisplay
% 2) || (mode
->hsync_start
% 2) ||
1760 (mode
->hsync_end
% 2) || (mode
->htotal
% 2)))
1761 return MODE_H_ILLEGAL
;
1763 rate
= drm_hdmi_compute_mode_clock(mode
, 8, HDMI_COLORSPACE_RGB
);
1764 return vc4_hdmi_connector_clock_valid(&vc4_hdmi
->connector
, mode
, rate
);
1767 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs
= {
1768 .atomic_check
= vc4_hdmi_encoder_atomic_check
,
1769 .atomic_mode_set
= vc4_hdmi_encoder_atomic_mode_set
,
1770 .mode_valid
= vc4_hdmi_encoder_mode_valid
,
1773 static int vc4_hdmi_late_register(struct drm_encoder
*encoder
)
1775 struct drm_device
*drm
= encoder
->dev
;
1776 struct vc4_hdmi
*vc4_hdmi
= encoder_to_vc4_hdmi(encoder
);
1777 const struct vc4_hdmi_variant
*variant
= vc4_hdmi
->variant
;
1779 drm_debugfs_add_file(drm
, variant
->debugfs_name
,
1780 vc4_hdmi_debugfs_regs
, vc4_hdmi
);
1785 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs
= {
1786 .late_register
= vc4_hdmi_late_register
,
1789 static u32
vc4_hdmi_channel_map(struct vc4_hdmi
*vc4_hdmi
, u32 channel_mask
)
1792 u32 channel_map
= 0;
1794 for (i
= 0; i
< 8; i
++) {
1795 if (channel_mask
& BIT(i
))
1796 channel_map
|= i
<< (3 * i
);
1801 static u32
vc5_hdmi_channel_map(struct vc4_hdmi
*vc4_hdmi
, u32 channel_mask
)
1804 u32 channel_map
= 0;
1806 for (i
= 0; i
< 8; i
++) {
1807 if (channel_mask
& BIT(i
))
1808 channel_map
|= i
<< (4 * i
);
1813 static bool vc5_hdmi_hp_detect(struct vc4_hdmi
*vc4_hdmi
)
1815 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1816 unsigned long flags
;
1820 if (!drm_dev_enter(drm
, &idx
))
1823 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1824 hotplug
= HDMI_READ(HDMI_HOTPLUG
);
1825 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1829 return !!(hotplug
& VC4_HDMI_HOTPLUG_CONNECTED
);
1832 /* HDMI audio codec callbacks */
1833 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi
*vc4_hdmi
,
1834 unsigned int samplerate
)
1836 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1838 unsigned long flags
;
1842 if (!drm_dev_enter(drm
, &idx
))
1845 hsm_clock
= clk_get_rate(vc4_hdmi
->audio_clock
);
1846 rational_best_approximation(hsm_clock
, samplerate
,
1847 VC4_HD_MAI_SMP_N_MASK
>>
1848 VC4_HD_MAI_SMP_N_SHIFT
,
1849 (VC4_HD_MAI_SMP_M_MASK
>>
1850 VC4_HD_MAI_SMP_M_SHIFT
) + 1,
1853 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1854 HDMI_WRITE(HDMI_MAI_SMP
,
1855 VC4_SET_FIELD(n
, VC4_HD_MAI_SMP_N
) |
1856 VC4_SET_FIELD(m
- 1, VC4_HD_MAI_SMP_M
));
1857 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1862 static void vc4_hdmi_set_n_cts(struct vc4_hdmi
*vc4_hdmi
, unsigned int samplerate
)
1864 const struct drm_display_mode
*mode
= &vc4_hdmi
->saved_adjusted_mode
;
1868 lockdep_assert_held(&vc4_hdmi
->mutex
);
1869 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
1871 n
= 128 * samplerate
/ 1000;
1872 tmp
= (u64
)(mode
->clock
* 1000) * n
;
1873 do_div(tmp
, 128 * samplerate
);
1876 HDMI_WRITE(HDMI_CRP_CFG
,
1877 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN
|
1878 VC4_SET_FIELD(n
, VC4_HDMI_CRP_CFG_N
));
1881 * We could get slightly more accurate clocks in some cases by
1882 * providing a CTS_1 value. The two CTS values are alternated
1883 * between based on the period fields
1885 HDMI_WRITE(HDMI_CTS_0
, cts
);
1886 HDMI_WRITE(HDMI_CTS_1
, cts
);
1889 static inline struct vc4_hdmi
*dai_to_hdmi(struct snd_soc_dai
*dai
)
1891 struct snd_soc_card
*card
= snd_soc_dai_get_drvdata(dai
);
1893 return snd_soc_card_get_drvdata(card
);
1896 static bool vc4_hdmi_audio_can_stream(struct vc4_hdmi
*vc4_hdmi
)
1898 struct drm_display_info
*display
= &vc4_hdmi
->connector
.display_info
;
1900 lockdep_assert_held(&vc4_hdmi
->mutex
);
1903 * If the encoder is currently in DVI mode, treat the codec DAI
1906 if (!display
->is_hdmi
)
1912 static int vc4_hdmi_audio_startup(struct device
*dev
, void *data
)
1914 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
1915 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1916 unsigned long flags
;
1920 mutex_lock(&vc4_hdmi
->mutex
);
1922 if (!drm_dev_enter(drm
, &idx
)) {
1927 if (!vc4_hdmi_audio_can_stream(vc4_hdmi
)) {
1932 vc4_hdmi
->audio
.streaming
= true;
1934 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1935 HDMI_WRITE(HDMI_MAI_CTL
,
1936 VC4_HD_MAI_CTL_RESET
|
1937 VC4_HD_MAI_CTL_FLUSH
|
1938 VC4_HD_MAI_CTL_DLATE
|
1939 VC4_HD_MAI_CTL_ERRORE
|
1940 VC4_HD_MAI_CTL_ERRORF
);
1941 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1943 if (vc4_hdmi
->variant
->phy_rng_enable
)
1944 vc4_hdmi
->variant
->phy_rng_enable(vc4_hdmi
);
1949 mutex_unlock(&vc4_hdmi
->mutex
);
1954 static void vc4_hdmi_audio_reset(struct vc4_hdmi
*vc4_hdmi
)
1956 struct device
*dev
= &vc4_hdmi
->pdev
->dev
;
1957 unsigned long flags
;
1960 lockdep_assert_held(&vc4_hdmi
->mutex
);
1962 vc4_hdmi
->audio
.streaming
= false;
1963 ret
= vc4_hdmi_stop_packet(vc4_hdmi
, HDMI_INFOFRAME_TYPE_AUDIO
, false);
1965 dev_err(dev
, "Failed to stop audio infoframe: %d\n", ret
);
1967 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1969 HDMI_WRITE(HDMI_MAI_CTL
, VC4_HD_MAI_CTL_RESET
);
1970 HDMI_WRITE(HDMI_MAI_CTL
, VC4_HD_MAI_CTL_ERRORF
);
1971 HDMI_WRITE(HDMI_MAI_CTL
, VC4_HD_MAI_CTL_FLUSH
);
1973 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1976 static void vc4_hdmi_audio_shutdown(struct device
*dev
, void *data
)
1978 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
1979 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
1980 unsigned long flags
;
1983 mutex_lock(&vc4_hdmi
->mutex
);
1985 if (!drm_dev_enter(drm
, &idx
))
1988 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
1990 HDMI_WRITE(HDMI_MAI_CTL
,
1991 VC4_HD_MAI_CTL_DLATE
|
1992 VC4_HD_MAI_CTL_ERRORE
|
1993 VC4_HD_MAI_CTL_ERRORF
);
1995 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
1997 if (vc4_hdmi
->variant
->phy_rng_disable
)
1998 vc4_hdmi
->variant
->phy_rng_disable(vc4_hdmi
);
2000 vc4_hdmi
->audio
.streaming
= false;
2001 vc4_hdmi_audio_reset(vc4_hdmi
);
2006 mutex_unlock(&vc4_hdmi
->mutex
);
2009 static int sample_rate_to_mai_fmt(int samplerate
)
2011 switch (samplerate
) {
2013 return VC4_HDMI_MAI_SAMPLE_RATE_8000
;
2015 return VC4_HDMI_MAI_SAMPLE_RATE_11025
;
2017 return VC4_HDMI_MAI_SAMPLE_RATE_12000
;
2019 return VC4_HDMI_MAI_SAMPLE_RATE_16000
;
2021 return VC4_HDMI_MAI_SAMPLE_RATE_22050
;
2023 return VC4_HDMI_MAI_SAMPLE_RATE_24000
;
2025 return VC4_HDMI_MAI_SAMPLE_RATE_32000
;
2027 return VC4_HDMI_MAI_SAMPLE_RATE_44100
;
2029 return VC4_HDMI_MAI_SAMPLE_RATE_48000
;
2031 return VC4_HDMI_MAI_SAMPLE_RATE_64000
;
2033 return VC4_HDMI_MAI_SAMPLE_RATE_88200
;
2035 return VC4_HDMI_MAI_SAMPLE_RATE_96000
;
2037 return VC4_HDMI_MAI_SAMPLE_RATE_128000
;
2039 return VC4_HDMI_MAI_SAMPLE_RATE_176400
;
2041 return VC4_HDMI_MAI_SAMPLE_RATE_192000
;
2043 return VC4_HDMI_MAI_SAMPLE_RATE_NOT_INDICATED
;
2047 /* HDMI audio codec callbacks */
2048 static int vc4_hdmi_audio_prepare(struct device
*dev
, void *data
,
2049 struct hdmi_codec_daifmt
*daifmt
,
2050 struct hdmi_codec_params
*params
)
2052 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
2053 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
2054 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
2055 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
2056 unsigned int sample_rate
= params
->sample_rate
;
2057 unsigned int channels
= params
->channels
;
2058 unsigned long flags
;
2059 u32 audio_packet_config
, channel_mask
;
2061 u32 mai_audio_format
;
2062 u32 mai_sample_rate
;
2066 dev_dbg(dev
, "%s: %u Hz, %d bit, %d channels\n", __func__
,
2067 sample_rate
, params
->sample_width
, channels
);
2069 mutex_lock(&vc4_hdmi
->mutex
);
2071 if (!drm_dev_enter(drm
, &idx
)) {
2076 if (!vc4_hdmi_audio_can_stream(vc4_hdmi
)) {
2081 vc4_hdmi_audio_set_mai_clock(vc4_hdmi
, sample_rate
);
2083 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
2084 HDMI_WRITE(HDMI_MAI_CTL
,
2085 VC4_SET_FIELD(channels
, VC4_HD_MAI_CTL_CHNUM
) |
2086 VC4_HD_MAI_CTL_WHOLSMP
|
2087 VC4_HD_MAI_CTL_CHALIGN
|
2088 VC4_HD_MAI_CTL_ENABLE
);
2090 mai_sample_rate
= sample_rate_to_mai_fmt(sample_rate
);
2091 if (params
->iec
.status
[0] & IEC958_AES0_NONAUDIO
&&
2092 params
->channels
== 8)
2093 mai_audio_format
= VC4_HDMI_MAI_FORMAT_HBR
;
2095 mai_audio_format
= VC4_HDMI_MAI_FORMAT_PCM
;
2096 HDMI_WRITE(HDMI_MAI_FMT
,
2097 VC4_SET_FIELD(mai_sample_rate
,
2098 VC4_HDMI_MAI_FORMAT_SAMPLE_RATE
) |
2099 VC4_SET_FIELD(mai_audio_format
,
2100 VC4_HDMI_MAI_FORMAT_AUDIO_FORMAT
));
2102 /* The B frame identifier should match the value used by alsa-lib (8) */
2103 audio_packet_config
=
2104 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT
|
2105 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS
|
2106 VC4_SET_FIELD(0x8, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER
);
2108 channel_mask
= GENMASK(channels
- 1, 0);
2109 audio_packet_config
|= VC4_SET_FIELD(channel_mask
,
2110 VC4_HDMI_AUDIO_PACKET_CEA_MASK
);
2112 /* Set the MAI threshold */
2113 if (vc4
->gen
>= VC4_GEN_5
)
2114 HDMI_WRITE(HDMI_MAI_THR
,
2115 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH
) |
2116 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW
) |
2117 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQHIGH
) |
2118 VC4_SET_FIELD(0x1c, VC4_HD_MAI_THR_DREQLOW
));
2120 HDMI_WRITE(HDMI_MAI_THR
,
2121 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICHIGH
) |
2122 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_PANICLOW
) |
2123 VC4_SET_FIELD(0x6, VC4_HD_MAI_THR_DREQHIGH
) |
2124 VC4_SET_FIELD(0x8, VC4_HD_MAI_THR_DREQLOW
));
2126 HDMI_WRITE(HDMI_MAI_CONFIG
,
2127 VC4_HDMI_MAI_CONFIG_BIT_REVERSE
|
2128 VC4_HDMI_MAI_CONFIG_FORMAT_REVERSE
|
2129 VC4_SET_FIELD(channel_mask
, VC4_HDMI_MAI_CHANNEL_MASK
));
2131 channel_map
= vc4_hdmi
->variant
->channel_map(vc4_hdmi
, channel_mask
);
2132 HDMI_WRITE(HDMI_MAI_CHANNEL_MAP
, channel_map
);
2133 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG
, audio_packet_config
);
2135 vc4_hdmi_set_n_cts(vc4_hdmi
, sample_rate
);
2137 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
2139 ret
= drm_atomic_helper_connector_hdmi_update_audio_infoframe(connector
,
2147 mutex_unlock(&vc4_hdmi
->mutex
);
2152 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp
= {
2153 .name
= "vc4-hdmi-cpu-dai-component",
2154 .legacy_dai_naming
= 1,
2157 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai
*dai
)
2159 struct vc4_hdmi
*vc4_hdmi
= dai_to_hdmi(dai
);
2161 snd_soc_dai_init_dma_data(dai
, &vc4_hdmi
->audio
.dma_data
, NULL
);
2166 static const struct snd_soc_dai_ops vc4_snd_dai_ops
= {
2167 .probe
= vc4_hdmi_audio_cpu_dai_probe
,
2170 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv
= {
2171 .name
= "vc4-hdmi-cpu-dai",
2172 .ops
= &vc4_snd_dai_ops
,
2174 .stream_name
= "Playback",
2177 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
2178 SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_88200
|
2179 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_176400
|
2180 SNDRV_PCM_RATE_192000
,
2181 .formats
= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
,
2185 static const struct snd_dmaengine_pcm_config pcm_conf
= {
2186 .chan_names
[SNDRV_PCM_STREAM_PLAYBACK
] = "audio-rx",
2187 .prepare_slave_config
= snd_dmaengine_pcm_prepare_slave_config
,
2190 static int vc4_hdmi_audio_get_eld(struct device
*dev
, void *data
,
2191 uint8_t *buf
, size_t len
)
2193 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
2194 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
2196 mutex_lock(&vc4_hdmi
->mutex
);
2197 memcpy(buf
, connector
->eld
, min(sizeof(connector
->eld
), len
));
2198 mutex_unlock(&vc4_hdmi
->mutex
);
2203 static const struct hdmi_codec_ops vc4_hdmi_codec_ops
= {
2204 .get_eld
= vc4_hdmi_audio_get_eld
,
2205 .prepare
= vc4_hdmi_audio_prepare
,
2206 .audio_shutdown
= vc4_hdmi_audio_shutdown
,
2207 .audio_startup
= vc4_hdmi_audio_startup
,
2210 static struct hdmi_codec_pdata vc4_hdmi_codec_pdata
= {
2211 .ops
= &vc4_hdmi_codec_ops
,
2212 .max_i2s_channels
= 8,
2216 static void vc4_hdmi_audio_codec_release(void *ptr
)
2218 struct vc4_hdmi
*vc4_hdmi
= ptr
;
2220 platform_device_unregister(vc4_hdmi
->audio
.codec_pdev
);
2221 vc4_hdmi
->audio
.codec_pdev
= NULL
;
2224 static int vc4_hdmi_audio_init(struct vc4_hdmi
*vc4_hdmi
)
2226 const struct vc4_hdmi_register
*mai_data
=
2227 &vc4_hdmi
->variant
->registers
[HDMI_MAI_DATA
];
2228 struct snd_soc_dai_link
*dai_link
= &vc4_hdmi
->audio
.link
;
2229 struct snd_soc_card
*card
= &vc4_hdmi
->audio
.card
;
2230 struct device
*dev
= &vc4_hdmi
->pdev
->dev
;
2231 struct platform_device
*codec_pdev
;
2237 * ASoC makes it a bit hard to retrieve a pointer to the
2238 * vc4_hdmi structure. Registering the card will overwrite our
2239 * device drvdata with a pointer to the snd_soc_card structure,
2240 * which can then be used to retrieve whatever drvdata we want
2243 * However, that doesn't fly in the case where we wouldn't
2244 * register an ASoC card (because of an old DT that is missing
2245 * the dmas properties for example), then the card isn't
2246 * registered and the device drvdata wouldn't be set.
2248 * We can deal with both cases by making sure a snd_soc_card
2249 * pointer and a vc4_hdmi structure are pointing to the same
2250 * memory address, so we can treat them indistinctly without any
2253 BUILD_BUG_ON(offsetof(struct vc4_hdmi_audio
, card
) != 0);
2254 BUILD_BUG_ON(offsetof(struct vc4_hdmi
, audio
) != 0);
2256 if (!of_find_property(dev
->of_node
, "dmas", &len
) || !len
) {
2258 "'dmas' DT property is missing or empty, no HDMI audio\n");
2262 if (mai_data
->reg
!= VC4_HD
) {
2263 WARN_ONCE(true, "MAI isn't in the HD block\n");
2268 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
2269 * the bus address specified in the DT, because the physical address
2270 * (the one returned by platform_get_resource()) is not appropriate
2271 * for DMA transfers.
2272 * This VC/MMU should probably be exposed to avoid this kind of hacks.
2274 index
= of_property_match_string(dev
->of_node
, "reg-names", "hd");
2275 /* Before BCM2711, we don't have a named register range */
2279 addr
= of_get_address(dev
->of_node
, index
, NULL
, NULL
);
2283 vc4_hdmi
->audio
.dma_data
.addr
= be32_to_cpup(addr
) + mai_data
->offset
;
2284 vc4_hdmi
->audio
.dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
2285 vc4_hdmi
->audio
.dma_data
.maxburst
= 2;
2288 * NOTE: Strictly speaking, we should probably use a DRM-managed
2289 * registration there to avoid removing all the audio components
2290 * by the time the driver doesn't have any user anymore.
2292 * However, the ASoC core uses a number of devm_kzalloc calls
2293 * when registering, even when using non-device-managed
2294 * functions (such as in snd_soc_register_component()).
2296 * If we call snd_soc_unregister_component() in a DRM-managed
2297 * action, the device-managed actions have already been executed
2298 * and thus we would access memory that has been freed.
2300 * Using device-managed hooks here probably leaves us open to a
2301 * bunch of issues if userspace still has a handle on the ALSA
2302 * device when the device is removed. However, this is mitigated
2303 * by the use of drm_dev_enter()/drm_dev_exit() in the audio
2304 * path to prevent the access to the device resources if it
2305 * isn't there anymore.
2307 * Then, the vc4_hdmi structure is DRM-managed and thus only
2308 * freed whenever the last user has closed the DRM device file.
2309 * It should thus outlive ALSA in most situations.
2311 ret
= devm_snd_dmaengine_pcm_register(dev
, &pcm_conf
, 0);
2313 dev_err(dev
, "Could not register PCM component: %d\n", ret
);
2317 ret
= devm_snd_soc_register_component(dev
, &vc4_hdmi_audio_cpu_dai_comp
,
2318 &vc4_hdmi_audio_cpu_dai_drv
, 1);
2320 dev_err(dev
, "Could not register CPU DAI: %d\n", ret
);
2324 codec_pdev
= platform_device_register_data(dev
, HDMI_CODEC_DRV_NAME
,
2325 PLATFORM_DEVID_AUTO
,
2326 &vc4_hdmi_codec_pdata
,
2327 sizeof(vc4_hdmi_codec_pdata
));
2328 if (IS_ERR(codec_pdev
)) {
2329 dev_err(dev
, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev
));
2330 return PTR_ERR(codec_pdev
);
2332 vc4_hdmi
->audio
.codec_pdev
= codec_pdev
;
2334 ret
= devm_add_action_or_reset(dev
, vc4_hdmi_audio_codec_release
, vc4_hdmi
);
2338 dai_link
->cpus
= &vc4_hdmi
->audio
.cpu
;
2339 dai_link
->codecs
= &vc4_hdmi
->audio
.codec
;
2340 dai_link
->platforms
= &vc4_hdmi
->audio
.platform
;
2342 dai_link
->num_cpus
= 1;
2343 dai_link
->num_codecs
= 1;
2344 dai_link
->num_platforms
= 1;
2346 dai_link
->name
= "MAI";
2347 dai_link
->stream_name
= "MAI PCM";
2348 dai_link
->codecs
->dai_name
= "i2s-hifi";
2349 dai_link
->cpus
->dai_name
= dev_name(dev
);
2350 dai_link
->codecs
->name
= dev_name(&codec_pdev
->dev
);
2351 dai_link
->platforms
->name
= dev_name(dev
);
2353 card
->dai_link
= dai_link
;
2354 card
->num_links
= 1;
2355 card
->name
= vc4_hdmi
->variant
->card_name
;
2356 card
->driver_name
= "vc4-hdmi";
2358 card
->owner
= THIS_MODULE
;
2361 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
2362 * stores a pointer to the snd card object in dev->driver_data. This
2363 * means we cannot use it for something else. The hdmi back-pointer is
2364 * now stored in card->drvdata and should be retrieved with
2365 * snd_soc_card_get_drvdata() if needed.
2367 snd_soc_card_set_drvdata(card
, vc4_hdmi
);
2368 ret
= devm_snd_soc_register_card(dev
, card
);
2370 dev_err_probe(dev
, ret
, "Could not register sound card\n");
2376 static irqreturn_t
vc4_hdmi_hpd_irq_thread(int irq
, void *priv
)
2378 struct vc4_hdmi
*vc4_hdmi
= priv
;
2379 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
2380 struct drm_device
*dev
= connector
->dev
;
2382 if (dev
&& dev
->registered
)
2383 drm_connector_helper_hpd_irq_event(connector
);
2388 static int vc4_hdmi_hotplug_init(struct vc4_hdmi
*vc4_hdmi
)
2390 struct drm_connector
*connector
= &vc4_hdmi
->connector
;
2391 struct platform_device
*pdev
= vc4_hdmi
->pdev
;
2394 if (vc4_hdmi
->variant
->external_irq_controller
) {
2395 unsigned int hpd_con
= platform_get_irq_byname(pdev
, "hpd-connected");
2396 unsigned int hpd_rm
= platform_get_irq_byname(pdev
, "hpd-removed");
2398 ret
= devm_request_threaded_irq(&pdev
->dev
, hpd_con
,
2400 vc4_hdmi_hpd_irq_thread
, IRQF_ONESHOT
,
2401 "vc4 hdmi hpd connected", vc4_hdmi
);
2405 ret
= devm_request_threaded_irq(&pdev
->dev
, hpd_rm
,
2407 vc4_hdmi_hpd_irq_thread
, IRQF_ONESHOT
,
2408 "vc4 hdmi hpd disconnected", vc4_hdmi
);
2412 connector
->polled
= DRM_CONNECTOR_POLL_HPD
;
2418 #ifdef CONFIG_DRM_VC4_HDMI_CEC
2419 static irqreturn_t
vc4_cec_irq_handler_rx_thread(int irq
, void *priv
)
2421 struct vc4_hdmi
*vc4_hdmi
= priv
;
2423 if (vc4_hdmi
->cec_rx_msg
.len
)
2424 cec_received_msg(vc4_hdmi
->cec_adap
,
2425 &vc4_hdmi
->cec_rx_msg
);
2430 static irqreturn_t
vc4_cec_irq_handler_tx_thread(int irq
, void *priv
)
2432 struct vc4_hdmi
*vc4_hdmi
= priv
;
2434 if (vc4_hdmi
->cec_tx_ok
) {
2435 cec_transmit_done(vc4_hdmi
->cec_adap
, CEC_TX_STATUS_OK
,
2439 * This CEC implementation makes 1 retry, so if we
2440 * get a NACK, then that means it made 2 attempts.
2442 cec_transmit_done(vc4_hdmi
->cec_adap
, CEC_TX_STATUS_NACK
,
2448 static irqreturn_t
vc4_cec_irq_handler_thread(int irq
, void *priv
)
2450 struct vc4_hdmi
*vc4_hdmi
= priv
;
2453 if (vc4_hdmi
->cec_irq_was_rx
)
2454 ret
= vc4_cec_irq_handler_rx_thread(irq
, priv
);
2456 ret
= vc4_cec_irq_handler_tx_thread(irq
, priv
);
2461 static void vc4_cec_read_msg(struct vc4_hdmi
*vc4_hdmi
, u32 cntrl1
)
2463 struct drm_device
*dev
= vc4_hdmi
->connector
.dev
;
2464 struct cec_msg
*msg
= &vc4_hdmi
->cec_rx_msg
;
2467 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
2469 msg
->len
= 1 + ((cntrl1
& VC4_HDMI_CEC_REC_WRD_CNT_MASK
) >>
2470 VC4_HDMI_CEC_REC_WRD_CNT_SHIFT
);
2472 if (msg
->len
> 16) {
2473 drm_err(dev
, "Attempting to read too much data (%d)\n", msg
->len
);
2477 for (i
= 0; i
< msg
->len
; i
+= 4) {
2478 u32 val
= HDMI_READ(HDMI_CEC_RX_DATA_1
+ (i
>> 2));
2480 msg
->msg
[i
] = val
& 0xff;
2481 msg
->msg
[i
+ 1] = (val
>> 8) & 0xff;
2482 msg
->msg
[i
+ 2] = (val
>> 16) & 0xff;
2483 msg
->msg
[i
+ 3] = (val
>> 24) & 0xff;
2487 static irqreturn_t
vc4_cec_irq_handler_tx_bare_locked(struct vc4_hdmi
*vc4_hdmi
)
2492 * We don't need to protect the register access using
2493 * drm_dev_enter() there because the interrupt handler lifetime
2494 * is tied to the device itself, and not to the DRM device.
2496 * So when the device will be gone, one of the first thing we
2497 * will be doing will be to unregister the interrupt handler,
2498 * and then unregister the DRM device. drm_dev_enter() would
2499 * thus always succeed if we are here.
2502 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
2504 cntrl1
= HDMI_READ(HDMI_CEC_CNTRL_1
);
2505 vc4_hdmi
->cec_tx_ok
= cntrl1
& VC4_HDMI_CEC_TX_STATUS_GOOD
;
2506 cntrl1
&= ~VC4_HDMI_CEC_START_XMIT_BEGIN
;
2507 HDMI_WRITE(HDMI_CEC_CNTRL_1
, cntrl1
);
2509 return IRQ_WAKE_THREAD
;
2512 static irqreturn_t
vc4_cec_irq_handler_tx_bare(int irq
, void *priv
)
2514 struct vc4_hdmi
*vc4_hdmi
= priv
;
2517 spin_lock(&vc4_hdmi
->hw_lock
);
2518 ret
= vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi
);
2519 spin_unlock(&vc4_hdmi
->hw_lock
);
2524 static irqreturn_t
vc4_cec_irq_handler_rx_bare_locked(struct vc4_hdmi
*vc4_hdmi
)
2528 lockdep_assert_held(&vc4_hdmi
->hw_lock
);
2531 * We don't need to protect the register access using
2532 * drm_dev_enter() there because the interrupt handler lifetime
2533 * is tied to the device itself, and not to the DRM device.
2535 * So when the device will be gone, one of the first thing we
2536 * will be doing will be to unregister the interrupt handler,
2537 * and then unregister the DRM device. drm_dev_enter() would
2538 * thus always succeed if we are here.
2541 vc4_hdmi
->cec_rx_msg
.len
= 0;
2542 cntrl1
= HDMI_READ(HDMI_CEC_CNTRL_1
);
2543 vc4_cec_read_msg(vc4_hdmi
, cntrl1
);
2544 cntrl1
|= VC4_HDMI_CEC_CLEAR_RECEIVE_OFF
;
2545 HDMI_WRITE(HDMI_CEC_CNTRL_1
, cntrl1
);
2546 cntrl1
&= ~VC4_HDMI_CEC_CLEAR_RECEIVE_OFF
;
2548 HDMI_WRITE(HDMI_CEC_CNTRL_1
, cntrl1
);
2550 return IRQ_WAKE_THREAD
;
2553 static irqreturn_t
vc4_cec_irq_handler_rx_bare(int irq
, void *priv
)
2555 struct vc4_hdmi
*vc4_hdmi
= priv
;
2558 spin_lock(&vc4_hdmi
->hw_lock
);
2559 ret
= vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi
);
2560 spin_unlock(&vc4_hdmi
->hw_lock
);
2565 static irqreturn_t
vc4_cec_irq_handler(int irq
, void *priv
)
2567 struct vc4_hdmi
*vc4_hdmi
= priv
;
2568 u32 stat
= HDMI_READ(HDMI_CEC_CPU_STATUS
);
2573 * We don't need to protect the register access using
2574 * drm_dev_enter() there because the interrupt handler lifetime
2575 * is tied to the device itself, and not to the DRM device.
2577 * So when the device will be gone, one of the first thing we
2578 * will be doing will be to unregister the interrupt handler,
2579 * and then unregister the DRM device. drm_dev_enter() would
2580 * thus always succeed if we are here.
2583 if (!(stat
& VC4_HDMI_CPU_CEC
))
2586 spin_lock(&vc4_hdmi
->hw_lock
);
2587 cntrl5
= HDMI_READ(HDMI_CEC_CNTRL_5
);
2588 vc4_hdmi
->cec_irq_was_rx
= cntrl5
& VC4_HDMI_CEC_RX_CEC_INT
;
2589 if (vc4_hdmi
->cec_irq_was_rx
)
2590 ret
= vc4_cec_irq_handler_rx_bare_locked(vc4_hdmi
);
2592 ret
= vc4_cec_irq_handler_tx_bare_locked(vc4_hdmi
);
2594 HDMI_WRITE(HDMI_CEC_CPU_CLEAR
, VC4_HDMI_CPU_CEC
);
2595 spin_unlock(&vc4_hdmi
->hw_lock
);
2600 static int vc4_hdmi_cec_enable(struct cec_adapter
*adap
)
2602 struct vc4_hdmi
*vc4_hdmi
= cec_get_drvdata(adap
);
2603 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
2604 /* clock period in microseconds */
2605 const u32 usecs
= 1000000 / CEC_CLOCK_FREQ
;
2606 unsigned long flags
;
2611 if (!drm_dev_enter(drm
, &idx
))
2613 * We can't return an error code, because the CEC
2614 * framework will emit WARN_ON messages at unbind
2619 ret
= pm_runtime_resume_and_get(&vc4_hdmi
->pdev
->dev
);
2625 mutex_lock(&vc4_hdmi
->mutex
);
2627 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
2629 val
= HDMI_READ(HDMI_CEC_CNTRL_5
);
2630 val
&= ~(VC4_HDMI_CEC_TX_SW_RESET
| VC4_HDMI_CEC_RX_SW_RESET
|
2631 VC4_HDMI_CEC_CNT_TO_4700_US_MASK
|
2632 VC4_HDMI_CEC_CNT_TO_4500_US_MASK
);
2633 val
|= ((4700 / usecs
) << VC4_HDMI_CEC_CNT_TO_4700_US_SHIFT
) |
2634 ((4500 / usecs
) << VC4_HDMI_CEC_CNT_TO_4500_US_SHIFT
);
2636 HDMI_WRITE(HDMI_CEC_CNTRL_5
, val
|
2637 VC4_HDMI_CEC_TX_SW_RESET
| VC4_HDMI_CEC_RX_SW_RESET
);
2638 HDMI_WRITE(HDMI_CEC_CNTRL_5
, val
);
2639 HDMI_WRITE(HDMI_CEC_CNTRL_2
,
2640 ((1500 / usecs
) << VC4_HDMI_CEC_CNT_TO_1500_US_SHIFT
) |
2641 ((1300 / usecs
) << VC4_HDMI_CEC_CNT_TO_1300_US_SHIFT
) |
2642 ((800 / usecs
) << VC4_HDMI_CEC_CNT_TO_800_US_SHIFT
) |
2643 ((600 / usecs
) << VC4_HDMI_CEC_CNT_TO_600_US_SHIFT
) |
2644 ((400 / usecs
) << VC4_HDMI_CEC_CNT_TO_400_US_SHIFT
));
2645 HDMI_WRITE(HDMI_CEC_CNTRL_3
,
2646 ((2750 / usecs
) << VC4_HDMI_CEC_CNT_TO_2750_US_SHIFT
) |
2647 ((2400 / usecs
) << VC4_HDMI_CEC_CNT_TO_2400_US_SHIFT
) |
2648 ((2050 / usecs
) << VC4_HDMI_CEC_CNT_TO_2050_US_SHIFT
) |
2649 ((1700 / usecs
) << VC4_HDMI_CEC_CNT_TO_1700_US_SHIFT
));
2650 HDMI_WRITE(HDMI_CEC_CNTRL_4
,
2651 ((4300 / usecs
) << VC4_HDMI_CEC_CNT_TO_4300_US_SHIFT
) |
2652 ((3900 / usecs
) << VC4_HDMI_CEC_CNT_TO_3900_US_SHIFT
) |
2653 ((3600 / usecs
) << VC4_HDMI_CEC_CNT_TO_3600_US_SHIFT
) |
2654 ((3500 / usecs
) << VC4_HDMI_CEC_CNT_TO_3500_US_SHIFT
));
2656 if (!vc4_hdmi
->variant
->external_irq_controller
)
2657 HDMI_WRITE(HDMI_CEC_CPU_MASK_CLEAR
, VC4_HDMI_CPU_CEC
);
2659 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
2661 mutex_unlock(&vc4_hdmi
->mutex
);
2667 static int vc4_hdmi_cec_disable(struct cec_adapter
*adap
)
2669 struct vc4_hdmi
*vc4_hdmi
= cec_get_drvdata(adap
);
2670 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
2671 unsigned long flags
;
2674 if (!drm_dev_enter(drm
, &idx
))
2676 * We can't return an error code, because the CEC
2677 * framework will emit WARN_ON messages at unbind
2682 mutex_lock(&vc4_hdmi
->mutex
);
2684 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
2686 if (!vc4_hdmi
->variant
->external_irq_controller
)
2687 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET
, VC4_HDMI_CPU_CEC
);
2689 HDMI_WRITE(HDMI_CEC_CNTRL_5
, HDMI_READ(HDMI_CEC_CNTRL_5
) |
2690 VC4_HDMI_CEC_TX_SW_RESET
| VC4_HDMI_CEC_RX_SW_RESET
);
2692 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
2694 mutex_unlock(&vc4_hdmi
->mutex
);
2696 pm_runtime_put(&vc4_hdmi
->pdev
->dev
);
2703 static int vc4_hdmi_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
2706 return vc4_hdmi_cec_enable(adap
);
2708 return vc4_hdmi_cec_disable(adap
);
2711 static int vc4_hdmi_cec_adap_log_addr(struct cec_adapter
*adap
, u8 log_addr
)
2713 struct vc4_hdmi
*vc4_hdmi
= cec_get_drvdata(adap
);
2714 struct drm_device
*drm
= vc4_hdmi
->connector
.dev
;
2715 unsigned long flags
;
2718 if (!drm_dev_enter(drm
, &idx
))
2720 * We can't return an error code, because the CEC
2721 * framework will emit WARN_ON messages at unbind
2726 mutex_lock(&vc4_hdmi
->mutex
);
2727 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
2728 HDMI_WRITE(HDMI_CEC_CNTRL_1
,
2729 (HDMI_READ(HDMI_CEC_CNTRL_1
) & ~VC4_HDMI_CEC_ADDR_MASK
) |
2730 (log_addr
& 0xf) << VC4_HDMI_CEC_ADDR_SHIFT
);
2731 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
2732 mutex_unlock(&vc4_hdmi
->mutex
);
2739 static int vc4_hdmi_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
2740 u32 signal_free_time
, struct cec_msg
*msg
)
2742 struct vc4_hdmi
*vc4_hdmi
= cec_get_drvdata(adap
);
2743 struct drm_device
*dev
= vc4_hdmi
->connector
.dev
;
2744 unsigned long flags
;
2749 if (!drm_dev_enter(dev
, &idx
))
2752 if (msg
->len
> 16) {
2753 drm_err(dev
, "Attempting to transmit too much data (%d)\n", msg
->len
);
2758 mutex_lock(&vc4_hdmi
->mutex
);
2760 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
2762 for (i
= 0; i
< msg
->len
; i
+= 4)
2763 HDMI_WRITE(HDMI_CEC_TX_DATA_1
+ (i
>> 2),
2765 (msg
->msg
[i
+ 1] << 8) |
2766 (msg
->msg
[i
+ 2] << 16) |
2767 (msg
->msg
[i
+ 3] << 24));
2769 val
= HDMI_READ(HDMI_CEC_CNTRL_1
);
2770 val
&= ~VC4_HDMI_CEC_START_XMIT_BEGIN
;
2771 HDMI_WRITE(HDMI_CEC_CNTRL_1
, val
);
2772 val
&= ~VC4_HDMI_CEC_MESSAGE_LENGTH_MASK
;
2773 val
|= (msg
->len
- 1) << VC4_HDMI_CEC_MESSAGE_LENGTH_SHIFT
;
2774 val
|= VC4_HDMI_CEC_START_XMIT_BEGIN
;
2776 HDMI_WRITE(HDMI_CEC_CNTRL_1
, val
);
2778 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
2779 mutex_unlock(&vc4_hdmi
->mutex
);
2785 static const struct cec_adap_ops vc4_hdmi_cec_adap_ops
= {
2786 .adap_enable
= vc4_hdmi_cec_adap_enable
,
2787 .adap_log_addr
= vc4_hdmi_cec_adap_log_addr
,
2788 .adap_transmit
= vc4_hdmi_cec_adap_transmit
,
2791 static void vc4_hdmi_cec_release(void *ptr
)
2793 struct vc4_hdmi
*vc4_hdmi
= ptr
;
2795 cec_unregister_adapter(vc4_hdmi
->cec_adap
);
2796 vc4_hdmi
->cec_adap
= NULL
;
2799 static int vc4_hdmi_cec_init(struct vc4_hdmi
*vc4_hdmi
)
2801 struct cec_connector_info conn_info
;
2802 struct platform_device
*pdev
= vc4_hdmi
->pdev
;
2803 struct device
*dev
= &pdev
->dev
;
2806 if (!of_property_present(dev
->of_node
, "interrupts")) {
2807 dev_warn(dev
, "'interrupts' DT property is missing, no CEC\n");
2811 vc4_hdmi
->cec_adap
= cec_allocate_adapter(&vc4_hdmi_cec_adap_ops
,
2813 vc4_hdmi
->variant
->card_name
,
2815 CEC_CAP_CONNECTOR_INFO
, 1);
2816 ret
= PTR_ERR_OR_ZERO(vc4_hdmi
->cec_adap
);
2820 cec_fill_conn_info_from_drm(&conn_info
, &vc4_hdmi
->connector
);
2821 cec_s_conn_info(vc4_hdmi
->cec_adap
, &conn_info
);
2823 if (vc4_hdmi
->variant
->external_irq_controller
) {
2824 ret
= devm_request_threaded_irq(dev
, platform_get_irq_byname(pdev
, "cec-rx"),
2825 vc4_cec_irq_handler_rx_bare
,
2826 vc4_cec_irq_handler_rx_thread
, 0,
2827 "vc4 hdmi cec rx", vc4_hdmi
);
2829 goto err_delete_cec_adap
;
2831 ret
= devm_request_threaded_irq(dev
, platform_get_irq_byname(pdev
, "cec-tx"),
2832 vc4_cec_irq_handler_tx_bare
,
2833 vc4_cec_irq_handler_tx_thread
, 0,
2834 "vc4 hdmi cec tx", vc4_hdmi
);
2836 goto err_delete_cec_adap
;
2838 ret
= devm_request_threaded_irq(dev
, platform_get_irq(pdev
, 0),
2839 vc4_cec_irq_handler
,
2840 vc4_cec_irq_handler_thread
, 0,
2841 "vc4 hdmi cec", vc4_hdmi
);
2843 goto err_delete_cec_adap
;
2846 ret
= cec_register_adapter(vc4_hdmi
->cec_adap
, &pdev
->dev
);
2848 goto err_delete_cec_adap
;
2851 * NOTE: Strictly speaking, we should probably use a DRM-managed
2852 * registration there to avoid removing the CEC adapter by the
2853 * time the DRM driver doesn't have any user anymore.
2855 * However, the CEC framework already cleans up the CEC adapter
2856 * only when the last user has closed its file descriptor, so we
2857 * don't need to handle it in DRM.
2859 * By the time the device-managed hook is executed, we will give
2860 * up our reference to the CEC adapter and therefore don't
2861 * really care when it's actually freed.
2863 * There's still a problematic sequence: if we unregister our
2864 * CEC adapter, but the userspace keeps a handle on the CEC
2865 * adapter but not the DRM device for some reason. In such a
2866 * case, our vc4_hdmi structure will be freed, but the
2867 * cec_adapter structure will have a dangling pointer to what
2868 * used to be our HDMI controller. If we get a CEC call at that
2869 * moment, we could end up with a use-after-free. Fortunately,
2870 * the CEC framework already handles this too, by calling
2871 * cec_is_registered() in cec_ioctl() and cec_poll().
2873 ret
= devm_add_action_or_reset(dev
, vc4_hdmi_cec_release
, vc4_hdmi
);
2879 err_delete_cec_adap
:
2880 cec_delete_adapter(vc4_hdmi
->cec_adap
);
2885 static int vc4_hdmi_cec_init(struct vc4_hdmi
*vc4_hdmi
)
2891 static void vc4_hdmi_free_regset(struct drm_device
*drm
, void *ptr
)
2893 struct debugfs_reg32
*regs
= ptr
;
2898 static int vc4_hdmi_build_regset(struct drm_device
*drm
,
2899 struct vc4_hdmi
*vc4_hdmi
,
2900 struct debugfs_regset32
*regset
,
2901 enum vc4_hdmi_regs reg
)
2903 const struct vc4_hdmi_variant
*variant
= vc4_hdmi
->variant
;
2904 struct debugfs_reg32
*regs
, *new_regs
;
2905 unsigned int count
= 0;
2909 regs
= kcalloc(variant
->num_registers
, sizeof(*regs
),
2914 for (i
= 0; i
< variant
->num_registers
; i
++) {
2915 const struct vc4_hdmi_register
*field
= &variant
->registers
[i
];
2917 if (field
->reg
!= reg
)
2920 regs
[count
].name
= field
->name
;
2921 regs
[count
].offset
= field
->offset
;
2925 new_regs
= krealloc(regs
, count
* sizeof(*regs
), GFP_KERNEL
);
2929 regset
->base
= __vc4_hdmi_get_field_base(vc4_hdmi
, reg
);
2930 regset
->regs
= new_regs
;
2931 regset
->nregs
= count
;
2933 ret
= drmm_add_action_or_reset(drm
, vc4_hdmi_free_regset
, new_regs
);
2940 static int vc4_hdmi_init_resources(struct drm_device
*drm
,
2941 struct vc4_hdmi
*vc4_hdmi
)
2943 struct platform_device
*pdev
= vc4_hdmi
->pdev
;
2944 struct device
*dev
= &pdev
->dev
;
2947 vc4_hdmi
->hdmicore_regs
= vc4_ioremap_regs(pdev
, 0);
2948 if (IS_ERR(vc4_hdmi
->hdmicore_regs
))
2949 return PTR_ERR(vc4_hdmi
->hdmicore_regs
);
2951 vc4_hdmi
->hd_regs
= vc4_ioremap_regs(pdev
, 1);
2952 if (IS_ERR(vc4_hdmi
->hd_regs
))
2953 return PTR_ERR(vc4_hdmi
->hd_regs
);
2955 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->hd_regset
, VC4_HD
);
2959 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->hdmi_regset
, VC4_HDMI
);
2963 vc4_hdmi
->pixel_clock
= devm_clk_get(dev
, "pixel");
2964 if (IS_ERR(vc4_hdmi
->pixel_clock
)) {
2965 ret
= PTR_ERR(vc4_hdmi
->pixel_clock
);
2966 if (ret
!= -EPROBE_DEFER
)
2967 drm_err(drm
, "Failed to get pixel clock\n");
2971 vc4_hdmi
->hsm_clock
= devm_clk_get(dev
, "hdmi");
2972 if (IS_ERR(vc4_hdmi
->hsm_clock
)) {
2973 drm_err(drm
, "Failed to get HDMI state machine clock\n");
2974 return PTR_ERR(vc4_hdmi
->hsm_clock
);
2976 vc4_hdmi
->audio_clock
= vc4_hdmi
->hsm_clock
;
2977 vc4_hdmi
->cec_clock
= vc4_hdmi
->hsm_clock
;
2982 static int vc5_hdmi_init_resources(struct drm_device
*drm
,
2983 struct vc4_hdmi
*vc4_hdmi
)
2985 struct platform_device
*pdev
= vc4_hdmi
->pdev
;
2986 struct device
*dev
= &pdev
->dev
;
2987 struct resource
*res
;
2990 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "hdmi");
2994 vc4_hdmi
->hdmicore_regs
= devm_ioremap(dev
, res
->start
,
2995 resource_size(res
));
2996 if (!vc4_hdmi
->hdmicore_regs
)
2999 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "hd");
3003 vc4_hdmi
->hd_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3004 if (!vc4_hdmi
->hd_regs
)
3007 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "cec");
3011 vc4_hdmi
->cec_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3012 if (!vc4_hdmi
->cec_regs
)
3015 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "csc");
3019 vc4_hdmi
->csc_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3020 if (!vc4_hdmi
->csc_regs
)
3023 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dvp");
3027 vc4_hdmi
->dvp_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3028 if (!vc4_hdmi
->dvp_regs
)
3031 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "phy");
3035 vc4_hdmi
->phy_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3036 if (!vc4_hdmi
->phy_regs
)
3039 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "packet");
3043 vc4_hdmi
->ram_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3044 if (!vc4_hdmi
->ram_regs
)
3047 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "rm");
3051 vc4_hdmi
->rm_regs
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3052 if (!vc4_hdmi
->rm_regs
)
3055 vc4_hdmi
->hsm_clock
= devm_clk_get(dev
, "hdmi");
3056 if (IS_ERR(vc4_hdmi
->hsm_clock
)) {
3057 drm_err(drm
, "Failed to get HDMI state machine clock\n");
3058 return PTR_ERR(vc4_hdmi
->hsm_clock
);
3061 vc4_hdmi
->pixel_bvb_clock
= devm_clk_get(dev
, "bvb");
3062 if (IS_ERR(vc4_hdmi
->pixel_bvb_clock
)) {
3063 drm_err(drm
, "Failed to get pixel bvb clock\n");
3064 return PTR_ERR(vc4_hdmi
->pixel_bvb_clock
);
3067 vc4_hdmi
->audio_clock
= devm_clk_get(dev
, "audio");
3068 if (IS_ERR(vc4_hdmi
->audio_clock
)) {
3069 drm_err(drm
, "Failed to get audio clock\n");
3070 return PTR_ERR(vc4_hdmi
->audio_clock
);
3073 vc4_hdmi
->cec_clock
= devm_clk_get(dev
, "cec");
3074 if (IS_ERR(vc4_hdmi
->cec_clock
)) {
3075 drm_err(drm
, "Failed to get CEC clock\n");
3076 return PTR_ERR(vc4_hdmi
->cec_clock
);
3079 vc4_hdmi
->reset
= devm_reset_control_get(dev
, NULL
);
3080 if (IS_ERR(vc4_hdmi
->reset
)) {
3081 drm_err(drm
, "Failed to get HDMI reset line\n");
3082 return PTR_ERR(vc4_hdmi
->reset
);
3085 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->hdmi_regset
, VC4_HDMI
);
3089 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->hd_regset
, VC4_HD
);
3093 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->cec_regset
, VC5_CEC
);
3097 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->csc_regset
, VC5_CSC
);
3101 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->dvp_regset
, VC5_DVP
);
3105 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->phy_regset
, VC5_PHY
);
3109 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->ram_regset
, VC5_RAM
);
3113 ret
= vc4_hdmi_build_regset(drm
, vc4_hdmi
, &vc4_hdmi
->rm_regset
, VC5_RM
);
3120 static int vc4_hdmi_runtime_suspend(struct device
*dev
)
3122 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
3124 clk_disable_unprepare(vc4_hdmi
->hsm_clock
);
3129 static int vc4_hdmi_runtime_resume(struct device
*dev
)
3131 struct vc4_hdmi
*vc4_hdmi
= dev_get_drvdata(dev
);
3132 unsigned long __maybe_unused flags
;
3133 u32 __maybe_unused value
;
3137 ret
= clk_prepare_enable(vc4_hdmi
->hsm_clock
);
3142 * Whenever the RaspberryPi boots without an HDMI monitor
3143 * plugged in, the firmware won't have initialized the HSM clock
3144 * rate and it will be reported as 0.
3146 * If we try to access a register of the controller in such a
3147 * case, it will lead to a silent CPU stall. Let's make sure we
3148 * prevent such a case.
3150 rate
= clk_get_rate(vc4_hdmi
->hsm_clock
);
3153 goto err_disable_clk
;
3156 if (vc4_hdmi
->variant
->reset
)
3157 vc4_hdmi
->variant
->reset(vc4_hdmi
);
3159 #ifdef CONFIG_DRM_VC4_HDMI_CEC
3160 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
3161 value
= HDMI_READ(HDMI_CEC_CNTRL_1
);
3162 /* Set the logical address to Unregistered */
3163 value
|= VC4_HDMI_CEC_ADDR_MASK
;
3164 HDMI_WRITE(HDMI_CEC_CNTRL_1
, value
);
3165 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
3167 vc4_hdmi_cec_update_clk_div(vc4_hdmi
);
3169 if (!vc4_hdmi
->variant
->external_irq_controller
) {
3170 spin_lock_irqsave(&vc4_hdmi
->hw_lock
, flags
);
3171 HDMI_WRITE(HDMI_CEC_CPU_MASK_SET
, 0xffffffff);
3172 spin_unlock_irqrestore(&vc4_hdmi
->hw_lock
, flags
);
3179 clk_disable_unprepare(vc4_hdmi
->hsm_clock
);
3183 static void vc4_hdmi_put_ddc_device(void *ptr
)
3185 struct vc4_hdmi
*vc4_hdmi
= ptr
;
3187 put_device(&vc4_hdmi
->ddc
->dev
);
3190 static int vc4_hdmi_bind(struct device
*dev
, struct device
*master
, void *data
)
3192 const struct vc4_hdmi_variant
*variant
= of_device_get_match_data(dev
);
3193 struct platform_device
*pdev
= to_platform_device(dev
);
3194 struct drm_device
*drm
= dev_get_drvdata(master
);
3195 struct vc4_hdmi
*vc4_hdmi
;
3196 struct drm_encoder
*encoder
;
3197 struct device_node
*ddc_node
;
3200 vc4_hdmi
= drmm_kzalloc(drm
, sizeof(*vc4_hdmi
), GFP_KERNEL
);
3204 ret
= drmm_mutex_init(drm
, &vc4_hdmi
->mutex
);
3208 spin_lock_init(&vc4_hdmi
->hw_lock
);
3209 INIT_DELAYED_WORK(&vc4_hdmi
->scrambling_work
, vc4_hdmi_scrambling_wq
);
3211 dev_set_drvdata(dev
, vc4_hdmi
);
3212 encoder
= &vc4_hdmi
->encoder
.base
;
3213 vc4_hdmi
->encoder
.type
= variant
->encoder_type
;
3214 vc4_hdmi
->encoder
.pre_crtc_configure
= vc4_hdmi_encoder_pre_crtc_configure
;
3215 vc4_hdmi
->encoder
.pre_crtc_enable
= vc4_hdmi_encoder_pre_crtc_enable
;
3216 vc4_hdmi
->encoder
.post_crtc_enable
= vc4_hdmi_encoder_post_crtc_enable
;
3217 vc4_hdmi
->encoder
.post_crtc_disable
= vc4_hdmi_encoder_post_crtc_disable
;
3218 vc4_hdmi
->encoder
.post_crtc_powerdown
= vc4_hdmi_encoder_post_crtc_powerdown
;
3219 vc4_hdmi
->pdev
= pdev
;
3220 vc4_hdmi
->variant
= variant
;
3223 * Since we don't know the state of the controller and its
3224 * display (if any), let's assume it's always enabled.
3225 * vc4_hdmi_disable_scrambling() will thus run at boot, make
3226 * sure it's disabled, and avoid any inconsistency.
3228 if (variant
->max_pixel_clock
> HDMI_14_MAX_TMDS_CLK
)
3229 vc4_hdmi
->scdc_enabled
= true;
3231 ret
= variant
->init_resources(drm
, vc4_hdmi
);
3235 ddc_node
= of_parse_phandle(dev
->of_node
, "ddc", 0);
3237 drm_err(drm
, "Failed to find ddc node in device tree\n");
3241 vc4_hdmi
->ddc
= of_find_i2c_adapter_by_node(ddc_node
);
3242 of_node_put(ddc_node
);
3243 if (!vc4_hdmi
->ddc
) {
3244 drm_err(drm
, "Failed to get ddc i2c adapter by node\n");
3245 return -EPROBE_DEFER
;
3248 ret
= devm_add_action_or_reset(dev
, vc4_hdmi_put_ddc_device
, vc4_hdmi
);
3252 /* Only use the GPIO HPD pin if present in the DT, otherwise
3253 * we'll use the HDMI core's register.
3255 vc4_hdmi
->hpd_gpio
= devm_gpiod_get_optional(dev
, "hpd", GPIOD_IN
);
3256 if (IS_ERR(vc4_hdmi
->hpd_gpio
)) {
3257 return PTR_ERR(vc4_hdmi
->hpd_gpio
);
3260 vc4_hdmi
->disable_wifi_frequencies
=
3261 of_property_read_bool(dev
->of_node
, "wifi-2.4ghz-coexistence");
3263 ret
= devm_pm_runtime_enable(dev
);
3268 * We need to have the device powered up at this point to call
3269 * our reset hook and for the CEC init.
3271 ret
= pm_runtime_resume_and_get(dev
);
3275 if ((of_device_is_compatible(dev
->of_node
, "brcm,bcm2711-hdmi0") ||
3276 of_device_is_compatible(dev
->of_node
, "brcm,bcm2711-hdmi1")) &&
3277 HDMI_READ(HDMI_VID_CTL
) & VC4_HD_VID_CTL_ENABLE
) {
3278 clk_prepare_enable(vc4_hdmi
->pixel_clock
);
3279 clk_prepare_enable(vc4_hdmi
->hsm_clock
);
3280 clk_prepare_enable(vc4_hdmi
->pixel_bvb_clock
);
3283 ret
= drmm_encoder_init(drm
, encoder
,
3284 &vc4_hdmi_encoder_funcs
,
3285 DRM_MODE_ENCODER_TMDS
,
3288 goto err_put_runtime_pm
;
3290 drm_encoder_helper_add(encoder
, &vc4_hdmi_encoder_helper_funcs
);
3292 ret
= vc4_hdmi_connector_init(drm
, vc4_hdmi
);
3294 goto err_put_runtime_pm
;
3296 ret
= vc4_hdmi_hotplug_init(vc4_hdmi
);
3298 goto err_put_runtime_pm
;
3300 ret
= vc4_hdmi_cec_init(vc4_hdmi
);
3302 goto err_put_runtime_pm
;
3304 ret
= vc4_hdmi_audio_init(vc4_hdmi
);
3306 goto err_put_runtime_pm
;
3308 pm_runtime_put_sync(dev
);
3313 pm_runtime_put_sync(dev
);
3318 static const struct component_ops vc4_hdmi_ops
= {
3319 .bind
= vc4_hdmi_bind
,
3322 static int vc4_hdmi_dev_probe(struct platform_device
*pdev
)
3324 return component_add(&pdev
->dev
, &vc4_hdmi_ops
);
3327 static void vc4_hdmi_dev_remove(struct platform_device
*pdev
)
3329 component_del(&pdev
->dev
, &vc4_hdmi_ops
);
3332 static const struct vc4_hdmi_variant bcm2835_variant
= {
3333 .encoder_type
= VC4_ENCODER_TYPE_HDMI0
,
3334 .debugfs_name
= "hdmi_regs",
3335 .card_name
= "vc4-hdmi",
3336 .max_pixel_clock
= 162000000,
3337 .registers
= vc4_hdmi_fields
,
3338 .num_registers
= ARRAY_SIZE(vc4_hdmi_fields
),
3340 .init_resources
= vc4_hdmi_init_resources
,
3341 .csc_setup
= vc4_hdmi_csc_setup
,
3342 .reset
= vc4_hdmi_reset
,
3343 .set_timings
= vc4_hdmi_set_timings
,
3344 .phy_init
= vc4_hdmi_phy_init
,
3345 .phy_disable
= vc4_hdmi_phy_disable
,
3346 .phy_rng_enable
= vc4_hdmi_phy_rng_enable
,
3347 .phy_rng_disable
= vc4_hdmi_phy_rng_disable
,
3348 .channel_map
= vc4_hdmi_channel_map
,
3349 .supports_hdr
= false,
3352 static const struct vc4_hdmi_variant bcm2711_hdmi0_variant
= {
3353 .encoder_type
= VC4_ENCODER_TYPE_HDMI0
,
3354 .debugfs_name
= "hdmi0_regs",
3355 .card_name
= "vc4-hdmi-0",
3356 .max_pixel_clock
= 600000000,
3357 .registers
= vc5_hdmi_hdmi0_fields
,
3358 .num_registers
= ARRAY_SIZE(vc5_hdmi_hdmi0_fields
),
3359 .phy_lane_mapping
= {
3365 .unsupported_odd_h_timings
= true,
3366 .external_irq_controller
= true,
3368 .init_resources
= vc5_hdmi_init_resources
,
3369 .csc_setup
= vc5_hdmi_csc_setup
,
3370 .reset
= vc5_hdmi_reset
,
3371 .set_timings
= vc5_hdmi_set_timings
,
3372 .phy_init
= vc5_hdmi_phy_init
,
3373 .phy_disable
= vc5_hdmi_phy_disable
,
3374 .phy_rng_enable
= vc5_hdmi_phy_rng_enable
,
3375 .phy_rng_disable
= vc5_hdmi_phy_rng_disable
,
3376 .channel_map
= vc5_hdmi_channel_map
,
3377 .supports_hdr
= true,
3378 .hp_detect
= vc5_hdmi_hp_detect
,
3381 static const struct vc4_hdmi_variant bcm2711_hdmi1_variant
= {
3382 .encoder_type
= VC4_ENCODER_TYPE_HDMI1
,
3383 .debugfs_name
= "hdmi1_regs",
3384 .card_name
= "vc4-hdmi-1",
3385 .max_pixel_clock
= HDMI_14_MAX_TMDS_CLK
,
3386 .registers
= vc5_hdmi_hdmi1_fields
,
3387 .num_registers
= ARRAY_SIZE(vc5_hdmi_hdmi1_fields
),
3388 .phy_lane_mapping
= {
3394 .unsupported_odd_h_timings
= true,
3395 .external_irq_controller
= true,
3397 .init_resources
= vc5_hdmi_init_resources
,
3398 .csc_setup
= vc5_hdmi_csc_setup
,
3399 .reset
= vc5_hdmi_reset
,
3400 .set_timings
= vc5_hdmi_set_timings
,
3401 .phy_init
= vc5_hdmi_phy_init
,
3402 .phy_disable
= vc5_hdmi_phy_disable
,
3403 .phy_rng_enable
= vc5_hdmi_phy_rng_enable
,
3404 .phy_rng_disable
= vc5_hdmi_phy_rng_disable
,
3405 .channel_map
= vc5_hdmi_channel_map
,
3406 .supports_hdr
= true,
3407 .hp_detect
= vc5_hdmi_hp_detect
,
3410 static const struct of_device_id vc4_hdmi_dt_match
[] = {
3411 { .compatible
= "brcm,bcm2835-hdmi", .data
= &bcm2835_variant
},
3412 { .compatible
= "brcm,bcm2711-hdmi0", .data
= &bcm2711_hdmi0_variant
},
3413 { .compatible
= "brcm,bcm2711-hdmi1", .data
= &bcm2711_hdmi1_variant
},
3417 static const struct dev_pm_ops vc4_hdmi_pm_ops
= {
3418 SET_RUNTIME_PM_OPS(vc4_hdmi_runtime_suspend
,
3419 vc4_hdmi_runtime_resume
,
3423 struct platform_driver vc4_hdmi_driver
= {
3424 .probe
= vc4_hdmi_dev_probe
,
3425 .remove
= vc4_hdmi_dev_remove
,
3428 .of_match_table
= vc4_hdmi_dt_match
,
3429 .pm
= &vc4_hdmi_pm_ops
,