2 * Copyright (C) 2015 Broadcom
3 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
21 * DOC: VC4 Falcon HDMI module
23 * The HDMI core has a state machine and a PHY. On BCM2835, most of
24 * the unit operates off of the HSM clock from CPRMAN. It also
25 * internally uses the PLLH_PIX clock for the PHY.
27 * HDMI infoframes are kept within a small packet ram, where each
28 * packet can be individually enabled for including in a frame.
30 * HDMI audio is implemented entirely within the HDMI IP block. A
31 * register in the HDMI encoder takes SPDIF frames from the DMA engine
32 * and transfers them over an internal MAI (multi-channel audio
33 * interconnect) bus to the encoder side for insertion into the video
36 * The driver's HDMI encoder does not yet support power management.
37 * The HDMI encoder's power domain and the HSM/pixel clocks are kept
38 * continuously running, and only the HDMI logic and packet ram are
39 * powered off/on at disable/enable time.
41 * The driver does not yet support CEC control, though the HDMI
42 * encoder block has CEC support.
45 #include <drm/drm_atomic_helper.h>
46 #include <drm/drm_crtc_helper.h>
47 #include <drm/drm_edid.h>
48 #include <linux/clk.h>
49 #include <linux/component.h>
50 #include <linux/i2c.h>
51 #include <linux/of_address.h>
52 #include <linux/of_gpio.h>
53 #include <linux/of_platform.h>
54 #include <linux/pm_runtime.h>
55 #include <linux/rational.h>
56 #include <sound/dmaengine_pcm.h>
57 #include <sound/pcm_drm_eld.h>
58 #include <sound/pcm_params.h>
59 #include <sound/soc.h>
63 /* HDMI audio information */
64 struct vc4_hdmi_audio
{
65 struct snd_soc_card card
;
66 struct snd_soc_dai_link link
;
69 struct snd_dmaengine_dai_dma_data dma_data
;
70 struct snd_pcm_substream
*substream
;
73 /* General HDMI hardware state. */
75 struct platform_device
*pdev
;
77 struct drm_encoder
*encoder
;
78 struct drm_connector
*connector
;
80 struct vc4_hdmi_audio audio
;
82 struct i2c_adapter
*ddc
;
83 void __iomem
*hdmicore_regs
;
84 void __iomem
*hd_regs
;
88 struct clk
*pixel_clock
;
89 struct clk
*hsm_clock
;
92 #define HDMI_READ(offset) readl(vc4->hdmi->hdmicore_regs + offset)
93 #define HDMI_WRITE(offset, val) writel(val, vc4->hdmi->hdmicore_regs + offset)
94 #define HD_READ(offset) readl(vc4->hdmi->hd_regs + offset)
95 #define HD_WRITE(offset, val) writel(val, vc4->hdmi->hd_regs + offset)
97 /* VC4 HDMI encoder KMS struct */
98 struct vc4_hdmi_encoder
{
99 struct vc4_encoder base
;
101 bool limited_rgb_range
;
102 bool rgb_range_selectable
;
105 static inline struct vc4_hdmi_encoder
*
106 to_vc4_hdmi_encoder(struct drm_encoder
*encoder
)
108 return container_of(encoder
, struct vc4_hdmi_encoder
, base
.base
);
111 /* VC4 HDMI connector KMS struct */
112 struct vc4_hdmi_connector
{
113 struct drm_connector base
;
115 /* Since the connector is attached to just the one encoder,
116 * this is the reference to it so we can do the best_encoder()
119 struct drm_encoder
*encoder
;
122 static inline struct vc4_hdmi_connector
*
123 to_vc4_hdmi_connector(struct drm_connector
*connector
)
125 return container_of(connector
, struct vc4_hdmi_connector
, base
);
128 #define HDMI_REG(reg) { reg, #reg }
129 static const struct {
133 HDMI_REG(VC4_HDMI_CORE_REV
),
134 HDMI_REG(VC4_HDMI_SW_RESET_CONTROL
),
135 HDMI_REG(VC4_HDMI_HOTPLUG_INT
),
136 HDMI_REG(VC4_HDMI_HOTPLUG
),
137 HDMI_REG(VC4_HDMI_MAI_CHANNEL_MAP
),
138 HDMI_REG(VC4_HDMI_MAI_CONFIG
),
139 HDMI_REG(VC4_HDMI_MAI_FORMAT
),
140 HDMI_REG(VC4_HDMI_AUDIO_PACKET_CONFIG
),
141 HDMI_REG(VC4_HDMI_RAM_PACKET_CONFIG
),
142 HDMI_REG(VC4_HDMI_HORZA
),
143 HDMI_REG(VC4_HDMI_HORZB
),
144 HDMI_REG(VC4_HDMI_FIFO_CTL
),
145 HDMI_REG(VC4_HDMI_SCHEDULER_CONTROL
),
146 HDMI_REG(VC4_HDMI_VERTA0
),
147 HDMI_REG(VC4_HDMI_VERTA1
),
148 HDMI_REG(VC4_HDMI_VERTB0
),
149 HDMI_REG(VC4_HDMI_VERTB1
),
150 HDMI_REG(VC4_HDMI_TX_PHY_RESET_CTL
),
151 HDMI_REG(VC4_HDMI_TX_PHY_CTL0
),
154 static const struct {
158 HDMI_REG(VC4_HD_M_CTL
),
159 HDMI_REG(VC4_HD_MAI_CTL
),
160 HDMI_REG(VC4_HD_MAI_THR
),
161 HDMI_REG(VC4_HD_MAI_FMT
),
162 HDMI_REG(VC4_HD_MAI_SMP
),
163 HDMI_REG(VC4_HD_VID_CTL
),
164 HDMI_REG(VC4_HD_CSC_CTL
),
165 HDMI_REG(VC4_HD_FRAME_COUNT
),
168 #ifdef CONFIG_DEBUG_FS
169 int vc4_hdmi_debugfs_regs(struct seq_file
*m
, void *unused
)
171 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
172 struct drm_device
*dev
= node
->minor
->dev
;
173 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
176 for (i
= 0; i
< ARRAY_SIZE(hdmi_regs
); i
++) {
177 seq_printf(m
, "%s (0x%04x): 0x%08x\n",
178 hdmi_regs
[i
].name
, hdmi_regs
[i
].reg
,
179 HDMI_READ(hdmi_regs
[i
].reg
));
182 for (i
= 0; i
< ARRAY_SIZE(hd_regs
); i
++) {
183 seq_printf(m
, "%s (0x%04x): 0x%08x\n",
184 hd_regs
[i
].name
, hd_regs
[i
].reg
,
185 HD_READ(hd_regs
[i
].reg
));
190 #endif /* CONFIG_DEBUG_FS */
192 static void vc4_hdmi_dump_regs(struct drm_device
*dev
)
194 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
197 for (i
= 0; i
< ARRAY_SIZE(hdmi_regs
); i
++) {
198 DRM_INFO("0x%04x (%s): 0x%08x\n",
199 hdmi_regs
[i
].reg
, hdmi_regs
[i
].name
,
200 HDMI_READ(hdmi_regs
[i
].reg
));
202 for (i
= 0; i
< ARRAY_SIZE(hd_regs
); i
++) {
203 DRM_INFO("0x%04x (%s): 0x%08x\n",
204 hd_regs
[i
].reg
, hd_regs
[i
].name
,
205 HD_READ(hd_regs
[i
].reg
));
209 static enum drm_connector_status
210 vc4_hdmi_connector_detect(struct drm_connector
*connector
, bool force
)
212 struct drm_device
*dev
= connector
->dev
;
213 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
215 if (vc4
->hdmi
->hpd_gpio
) {
216 if (gpio_get_value_cansleep(vc4
->hdmi
->hpd_gpio
) ^
217 vc4
->hdmi
->hpd_active_low
)
218 return connector_status_connected
;
220 return connector_status_disconnected
;
223 if (drm_probe_ddc(vc4
->hdmi
->ddc
))
224 return connector_status_connected
;
226 if (HDMI_READ(VC4_HDMI_HOTPLUG
) & VC4_HDMI_HOTPLUG_CONNECTED
)
227 return connector_status_connected
;
229 return connector_status_disconnected
;
232 static void vc4_hdmi_connector_destroy(struct drm_connector
*connector
)
234 drm_connector_unregister(connector
);
235 drm_connector_cleanup(connector
);
238 static int vc4_hdmi_connector_get_modes(struct drm_connector
*connector
)
240 struct vc4_hdmi_connector
*vc4_connector
=
241 to_vc4_hdmi_connector(connector
);
242 struct drm_encoder
*encoder
= vc4_connector
->encoder
;
243 struct vc4_hdmi_encoder
*vc4_encoder
= to_vc4_hdmi_encoder(encoder
);
244 struct drm_device
*dev
= connector
->dev
;
245 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
249 edid
= drm_get_edid(connector
, vc4
->hdmi
->ddc
);
253 vc4_encoder
->hdmi_monitor
= drm_detect_hdmi_monitor(edid
);
255 if (edid
&& edid
->input
& DRM_EDID_INPUT_DIGITAL
) {
256 vc4_encoder
->rgb_range_selectable
=
257 drm_rgb_quant_range_selectable(edid
);
260 drm_mode_connector_update_edid_property(connector
, edid
);
261 ret
= drm_add_edid_modes(connector
, edid
);
262 drm_edid_to_eld(connector
, edid
);
267 static const struct drm_connector_funcs vc4_hdmi_connector_funcs
= {
268 .dpms
= drm_atomic_helper_connector_dpms
,
269 .detect
= vc4_hdmi_connector_detect
,
270 .fill_modes
= drm_helper_probe_single_connector_modes
,
271 .destroy
= vc4_hdmi_connector_destroy
,
272 .reset
= drm_atomic_helper_connector_reset
,
273 .atomic_duplicate_state
= drm_atomic_helper_connector_duplicate_state
,
274 .atomic_destroy_state
= drm_atomic_helper_connector_destroy_state
,
277 static const struct drm_connector_helper_funcs vc4_hdmi_connector_helper_funcs
= {
278 .get_modes
= vc4_hdmi_connector_get_modes
,
281 static struct drm_connector
*vc4_hdmi_connector_init(struct drm_device
*dev
,
282 struct drm_encoder
*encoder
)
284 struct drm_connector
*connector
= NULL
;
285 struct vc4_hdmi_connector
*hdmi_connector
;
288 hdmi_connector
= devm_kzalloc(dev
->dev
, sizeof(*hdmi_connector
),
290 if (!hdmi_connector
) {
294 connector
= &hdmi_connector
->base
;
296 hdmi_connector
->encoder
= encoder
;
298 drm_connector_init(dev
, connector
, &vc4_hdmi_connector_funcs
,
299 DRM_MODE_CONNECTOR_HDMIA
);
300 drm_connector_helper_add(connector
, &vc4_hdmi_connector_helper_funcs
);
302 connector
->polled
= (DRM_CONNECTOR_POLL_CONNECT
|
303 DRM_CONNECTOR_POLL_DISCONNECT
);
305 connector
->interlace_allowed
= 1;
306 connector
->doublescan_allowed
= 0;
308 drm_mode_connector_attach_encoder(connector
, encoder
);
314 vc4_hdmi_connector_destroy(connector
);
319 static void vc4_hdmi_encoder_destroy(struct drm_encoder
*encoder
)
321 drm_encoder_cleanup(encoder
);
324 static const struct drm_encoder_funcs vc4_hdmi_encoder_funcs
= {
325 .destroy
= vc4_hdmi_encoder_destroy
,
328 static int vc4_hdmi_stop_packet(struct drm_encoder
*encoder
,
329 enum hdmi_infoframe_type type
)
331 struct drm_device
*dev
= encoder
->dev
;
332 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
333 u32 packet_id
= type
- 0x80;
335 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG
,
336 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG
) & ~BIT(packet_id
));
338 return wait_for(!(HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS
) &
339 BIT(packet_id
)), 100);
342 static void vc4_hdmi_write_infoframe(struct drm_encoder
*encoder
,
343 union hdmi_infoframe
*frame
)
345 struct drm_device
*dev
= encoder
->dev
;
346 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
347 u32 packet_id
= frame
->any
.type
- 0x80;
348 u32 packet_reg
= VC4_HDMI_RAM_PACKET(packet_id
);
349 uint8_t buffer
[VC4_HDMI_PACKET_STRIDE
];
353 WARN_ONCE(!(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG
) &
354 VC4_HDMI_RAM_PACKET_ENABLE
),
355 "Packet RAM has to be on to store the packet.");
357 len
= hdmi_infoframe_pack(frame
, buffer
, sizeof(buffer
));
361 ret
= vc4_hdmi_stop_packet(encoder
, frame
->any
.type
);
363 DRM_ERROR("Failed to wait for infoframe to go idle: %d\n", ret
);
367 for (i
= 0; i
< len
; i
+= 7) {
368 HDMI_WRITE(packet_reg
,
371 buffer
[i
+ 2] << 16);
374 HDMI_WRITE(packet_reg
,
377 buffer
[i
+ 5] << 16 |
378 buffer
[i
+ 6] << 24);
382 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG
,
383 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG
) | BIT(packet_id
));
384 ret
= wait_for((HDMI_READ(VC4_HDMI_RAM_PACKET_STATUS
) &
385 BIT(packet_id
)), 100);
387 DRM_ERROR("Failed to wait for infoframe to start: %d\n", ret
);
390 static void vc4_hdmi_set_avi_infoframe(struct drm_encoder
*encoder
)
392 struct vc4_hdmi_encoder
*vc4_encoder
= to_vc4_hdmi_encoder(encoder
);
393 struct drm_crtc
*crtc
= encoder
->crtc
;
394 const struct drm_display_mode
*mode
= &crtc
->state
->adjusted_mode
;
395 union hdmi_infoframe frame
;
398 ret
= drm_hdmi_avi_infoframe_from_display_mode(&frame
.avi
, mode
);
400 DRM_ERROR("couldn't fill AVI infoframe\n");
404 drm_hdmi_avi_infoframe_quant_range(&frame
.avi
, mode
,
405 vc4_encoder
->limited_rgb_range
?
406 HDMI_QUANTIZATION_RANGE_LIMITED
:
407 HDMI_QUANTIZATION_RANGE_FULL
,
408 vc4_encoder
->rgb_range_selectable
);
410 vc4_hdmi_write_infoframe(encoder
, &frame
);
413 static void vc4_hdmi_set_spd_infoframe(struct drm_encoder
*encoder
)
415 union hdmi_infoframe frame
;
418 ret
= hdmi_spd_infoframe_init(&frame
.spd
, "Broadcom", "Videocore");
420 DRM_ERROR("couldn't fill SPD infoframe\n");
424 frame
.spd
.sdi
= HDMI_SPD_SDI_PC
;
426 vc4_hdmi_write_infoframe(encoder
, &frame
);
429 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder
*encoder
)
431 struct drm_device
*drm
= encoder
->dev
;
432 struct vc4_dev
*vc4
= drm
->dev_private
;
433 struct vc4_hdmi
*hdmi
= vc4
->hdmi
;
434 union hdmi_infoframe frame
;
437 ret
= hdmi_audio_infoframe_init(&frame
.audio
);
439 frame
.audio
.coding_type
= HDMI_AUDIO_CODING_TYPE_STREAM
;
440 frame
.audio
.sample_frequency
= HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM
;
441 frame
.audio
.sample_size
= HDMI_AUDIO_SAMPLE_SIZE_STREAM
;
442 frame
.audio
.channels
= hdmi
->audio
.channels
;
444 vc4_hdmi_write_infoframe(encoder
, &frame
);
447 static void vc4_hdmi_set_infoframes(struct drm_encoder
*encoder
)
449 vc4_hdmi_set_avi_infoframe(encoder
);
450 vc4_hdmi_set_spd_infoframe(encoder
);
453 static void vc4_hdmi_encoder_disable(struct drm_encoder
*encoder
)
455 struct drm_device
*dev
= encoder
->dev
;
456 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
457 struct vc4_hdmi
*hdmi
= vc4
->hdmi
;
460 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG
, 0);
462 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL
, 0xf << 16);
463 HD_WRITE(VC4_HD_VID_CTL
,
464 HD_READ(VC4_HD_VID_CTL
) & ~VC4_HD_VID_CTL_ENABLE
);
466 HD_WRITE(VC4_HD_M_CTL
, VC4_HD_M_SW_RST
);
468 HD_WRITE(VC4_HD_M_CTL
, 0);
470 clk_disable_unprepare(hdmi
->hsm_clock
);
471 clk_disable_unprepare(hdmi
->pixel_clock
);
473 ret
= pm_runtime_put(&hdmi
->pdev
->dev
);
475 DRM_ERROR("Failed to release power domain: %d\n", ret
);
478 static void vc4_hdmi_encoder_enable(struct drm_encoder
*encoder
)
480 struct drm_display_mode
*mode
= &encoder
->crtc
->state
->adjusted_mode
;
481 struct vc4_hdmi_encoder
*vc4_encoder
= to_vc4_hdmi_encoder(encoder
);
482 struct drm_device
*dev
= encoder
->dev
;
483 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
484 struct vc4_hdmi
*hdmi
= vc4
->hdmi
;
485 bool debug_dump_regs
= false;
486 bool hsync_pos
= mode
->flags
& DRM_MODE_FLAG_PHSYNC
;
487 bool vsync_pos
= mode
->flags
& DRM_MODE_FLAG_PVSYNC
;
488 bool interlaced
= mode
->flags
& DRM_MODE_FLAG_INTERLACE
;
489 u32 pixel_rep
= (mode
->flags
& DRM_MODE_FLAG_DBLCLK
) ? 2 : 1;
490 u32 verta
= (VC4_SET_FIELD(mode
->crtc_vsync_end
- mode
->crtc_vsync_start
,
491 VC4_HDMI_VERTA_VSP
) |
492 VC4_SET_FIELD(mode
->crtc_vsync_start
- mode
->crtc_vdisplay
,
493 VC4_HDMI_VERTA_VFP
) |
494 VC4_SET_FIELD(mode
->crtc_vdisplay
, VC4_HDMI_VERTA_VAL
));
495 u32 vertb
= (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO
) |
496 VC4_SET_FIELD(mode
->crtc_vtotal
- mode
->crtc_vsync_end
,
497 VC4_HDMI_VERTB_VBP
));
498 u32 vertb_even
= (VC4_SET_FIELD(0, VC4_HDMI_VERTB_VSPO
) |
499 VC4_SET_FIELD(mode
->crtc_vtotal
-
500 mode
->crtc_vsync_end
-
502 VC4_HDMI_VERTB_VBP
));
506 ret
= pm_runtime_get_sync(&hdmi
->pdev
->dev
);
508 DRM_ERROR("Failed to retain power domain: %d\n", ret
);
512 /* This is the rate that is set by the firmware. The number
513 * needs to be a bit higher than the pixel clock rate
514 * (generally 148.5Mhz).
516 ret
= clk_set_rate(hdmi
->hsm_clock
, 163682864);
518 DRM_ERROR("Failed to set HSM clock rate: %d\n", ret
);
522 ret
= clk_set_rate(hdmi
->pixel_clock
,
524 ((mode
->flags
& DRM_MODE_FLAG_DBLCLK
) ? 2 : 1));
526 DRM_ERROR("Failed to set pixel clock rate: %d\n", ret
);
530 ret
= clk_prepare_enable(hdmi
->pixel_clock
);
532 DRM_ERROR("Failed to turn on pixel clock: %d\n", ret
);
536 ret
= clk_prepare_enable(hdmi
->hsm_clock
);
538 DRM_ERROR("Failed to turn on HDMI state machine clock: %d\n",
540 clk_disable_unprepare(hdmi
->pixel_clock
);
544 HD_WRITE(VC4_HD_M_CTL
, VC4_HD_M_SW_RST
);
546 HD_WRITE(VC4_HD_M_CTL
, 0);
548 HD_WRITE(VC4_HD_M_CTL
, VC4_HD_M_ENABLE
);
550 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL
,
551 VC4_HDMI_SW_RESET_HDMI
|
552 VC4_HDMI_SW_RESET_FORMAT_DETECT
);
554 HDMI_WRITE(VC4_HDMI_SW_RESET_CONTROL
, 0);
556 /* PHY should be in reset, like
557 * vc4_hdmi_encoder_disable() does.
559 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL
, 0xf << 16);
561 HDMI_WRITE(VC4_HDMI_TX_PHY_RESET_CTL
, 0);
563 if (debug_dump_regs
) {
564 DRM_INFO("HDMI regs before:\n");
565 vc4_hdmi_dump_regs(dev
);
568 HD_WRITE(VC4_HD_VID_CTL
, 0);
570 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL
,
571 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) |
572 VC4_HDMI_SCHEDULER_CONTROL_MANUAL_FORMAT
|
573 VC4_HDMI_SCHEDULER_CONTROL_IGNORE_VSYNC_PREDICTS
);
575 HDMI_WRITE(VC4_HDMI_HORZA
,
576 (vsync_pos
? VC4_HDMI_HORZA_VPOS
: 0) |
577 (hsync_pos
? VC4_HDMI_HORZA_HPOS
: 0) |
578 VC4_SET_FIELD(mode
->hdisplay
* pixel_rep
,
579 VC4_HDMI_HORZA_HAP
));
581 HDMI_WRITE(VC4_HDMI_HORZB
,
582 VC4_SET_FIELD((mode
->htotal
-
583 mode
->hsync_end
) * pixel_rep
,
584 VC4_HDMI_HORZB_HBP
) |
585 VC4_SET_FIELD((mode
->hsync_end
-
586 mode
->hsync_start
) * pixel_rep
,
587 VC4_HDMI_HORZB_HSP
) |
588 VC4_SET_FIELD((mode
->hsync_start
-
589 mode
->hdisplay
) * pixel_rep
,
590 VC4_HDMI_HORZB_HFP
));
592 HDMI_WRITE(VC4_HDMI_VERTA0
, verta
);
593 HDMI_WRITE(VC4_HDMI_VERTA1
, verta
);
595 HDMI_WRITE(VC4_HDMI_VERTB0
, vertb_even
);
596 HDMI_WRITE(VC4_HDMI_VERTB1
, vertb
);
598 HD_WRITE(VC4_HD_VID_CTL
,
599 (vsync_pos
? 0 : VC4_HD_VID_CTL_VSYNC_LOW
) |
600 (hsync_pos
? 0 : VC4_HD_VID_CTL_HSYNC_LOW
));
602 csc_ctl
= VC4_SET_FIELD(VC4_HD_CSC_CTL_ORDER_BGR
,
603 VC4_HD_CSC_CTL_ORDER
);
605 if (vc4_encoder
->hdmi_monitor
&&
606 drm_default_rgb_quant_range(mode
) ==
607 HDMI_QUANTIZATION_RANGE_LIMITED
) {
608 /* CEA VICs other than #1 requre limited range RGB
609 * output unless overridden by an AVI infoframe.
610 * Apply a colorspace conversion to squash 0-255 down
611 * to 16-235. The matrix here is:
618 csc_ctl
|= VC4_HD_CSC_CTL_ENABLE
;
619 csc_ctl
|= VC4_HD_CSC_CTL_RGB2YCC
;
620 csc_ctl
|= VC4_SET_FIELD(VC4_HD_CSC_CTL_MODE_CUSTOM
,
621 VC4_HD_CSC_CTL_MODE
);
623 HD_WRITE(VC4_HD_CSC_12_11
, (0x000 << 16) | 0x000);
624 HD_WRITE(VC4_HD_CSC_14_13
, (0x100 << 16) | 0x6e0);
625 HD_WRITE(VC4_HD_CSC_22_21
, (0x6e0 << 16) | 0x000);
626 HD_WRITE(VC4_HD_CSC_24_23
, (0x100 << 16) | 0x000);
627 HD_WRITE(VC4_HD_CSC_32_31
, (0x000 << 16) | 0x6e0);
628 HD_WRITE(VC4_HD_CSC_34_33
, (0x100 << 16) | 0x000);
629 vc4_encoder
->limited_rgb_range
= true;
631 vc4_encoder
->limited_rgb_range
= false;
634 /* The RGB order applies even when CSC is disabled. */
635 HD_WRITE(VC4_HD_CSC_CTL
, csc_ctl
);
637 HDMI_WRITE(VC4_HDMI_FIFO_CTL
, VC4_HDMI_FIFO_CTL_MASTER_SLAVE_N
);
639 if (debug_dump_regs
) {
640 DRM_INFO("HDMI regs after:\n");
641 vc4_hdmi_dump_regs(dev
);
644 HD_WRITE(VC4_HD_VID_CTL
,
645 HD_READ(VC4_HD_VID_CTL
) |
646 VC4_HD_VID_CTL_ENABLE
|
647 VC4_HD_VID_CTL_UNDERFLOW_ENABLE
|
648 VC4_HD_VID_CTL_FRAME_COUNTER_RESET
);
650 if (vc4_encoder
->hdmi_monitor
) {
651 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL
,
652 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) |
653 VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI
);
655 ret
= wait_for(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) &
656 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
, 1000);
657 WARN_ONCE(ret
, "Timeout waiting for "
658 "VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
660 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG
,
661 HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG
) &
662 ~(VC4_HDMI_RAM_PACKET_ENABLE
));
663 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL
,
664 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) &
665 ~VC4_HDMI_SCHEDULER_CONTROL_MODE_HDMI
);
667 ret
= wait_for(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) &
668 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
), 1000);
669 WARN_ONCE(ret
, "Timeout waiting for "
670 "!VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE\n");
673 if (vc4_encoder
->hdmi_monitor
) {
676 WARN_ON(!(HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) &
677 VC4_HDMI_SCHEDULER_CONTROL_HDMI_ACTIVE
));
678 HDMI_WRITE(VC4_HDMI_SCHEDULER_CONTROL
,
679 HDMI_READ(VC4_HDMI_SCHEDULER_CONTROL
) |
680 VC4_HDMI_SCHEDULER_CONTROL_VERT_ALWAYS_KEEPOUT
);
682 HDMI_WRITE(VC4_HDMI_RAM_PACKET_CONFIG
,
683 VC4_HDMI_RAM_PACKET_ENABLE
);
685 vc4_hdmi_set_infoframes(encoder
);
687 drift
= HDMI_READ(VC4_HDMI_FIFO_CTL
);
688 drift
&= VC4_HDMI_FIFO_VALID_WRITE_MASK
;
690 HDMI_WRITE(VC4_HDMI_FIFO_CTL
,
691 drift
& ~VC4_HDMI_FIFO_CTL_RECENTER
);
692 HDMI_WRITE(VC4_HDMI_FIFO_CTL
,
693 drift
| VC4_HDMI_FIFO_CTL_RECENTER
);
695 HDMI_WRITE(VC4_HDMI_FIFO_CTL
,
696 drift
& ~VC4_HDMI_FIFO_CTL_RECENTER
);
697 HDMI_WRITE(VC4_HDMI_FIFO_CTL
,
698 drift
| VC4_HDMI_FIFO_CTL_RECENTER
);
700 ret
= wait_for(HDMI_READ(VC4_HDMI_FIFO_CTL
) &
701 VC4_HDMI_FIFO_CTL_RECENTER_DONE
, 1);
702 WARN_ONCE(ret
, "Timeout waiting for "
703 "VC4_HDMI_FIFO_CTL_RECENTER_DONE");
707 static const struct drm_encoder_helper_funcs vc4_hdmi_encoder_helper_funcs
= {
708 .disable
= vc4_hdmi_encoder_disable
,
709 .enable
= vc4_hdmi_encoder_enable
,
712 /* HDMI audio codec callbacks */
713 static void vc4_hdmi_audio_set_mai_clock(struct vc4_hdmi
*hdmi
)
715 struct drm_device
*drm
= hdmi
->encoder
->dev
;
716 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
717 u32 hsm_clock
= clk_get_rate(hdmi
->hsm_clock
);
720 rational_best_approximation(hsm_clock
, hdmi
->audio
.samplerate
,
721 VC4_HD_MAI_SMP_N_MASK
>>
722 VC4_HD_MAI_SMP_N_SHIFT
,
723 (VC4_HD_MAI_SMP_M_MASK
>>
724 VC4_HD_MAI_SMP_M_SHIFT
) + 1,
727 HD_WRITE(VC4_HD_MAI_SMP
,
728 VC4_SET_FIELD(n
, VC4_HD_MAI_SMP_N
) |
729 VC4_SET_FIELD(m
- 1, VC4_HD_MAI_SMP_M
));
732 static void vc4_hdmi_set_n_cts(struct vc4_hdmi
*hdmi
)
734 struct drm_encoder
*encoder
= hdmi
->encoder
;
735 struct drm_crtc
*crtc
= encoder
->crtc
;
736 struct drm_device
*drm
= encoder
->dev
;
737 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
738 const struct drm_display_mode
*mode
= &crtc
->state
->adjusted_mode
;
739 u32 samplerate
= hdmi
->audio
.samplerate
;
743 n
= 128 * samplerate
/ 1000;
744 tmp
= (u64
)(mode
->clock
* 1000) * n
;
745 do_div(tmp
, 128 * samplerate
);
748 HDMI_WRITE(VC4_HDMI_CRP_CFG
,
749 VC4_HDMI_CRP_CFG_EXTERNAL_CTS_EN
|
750 VC4_SET_FIELD(n
, VC4_HDMI_CRP_CFG_N
));
753 * We could get slightly more accurate clocks in some cases by
754 * providing a CTS_1 value. The two CTS values are alternated
755 * between based on the period fields
757 HDMI_WRITE(VC4_HDMI_CTS_0
, cts
);
758 HDMI_WRITE(VC4_HDMI_CTS_1
, cts
);
761 static inline struct vc4_hdmi
*dai_to_hdmi(struct snd_soc_dai
*dai
)
763 struct snd_soc_card
*card
= snd_soc_dai_get_drvdata(dai
);
765 return snd_soc_card_get_drvdata(card
);
768 static int vc4_hdmi_audio_startup(struct snd_pcm_substream
*substream
,
769 struct snd_soc_dai
*dai
)
771 struct vc4_hdmi
*hdmi
= dai_to_hdmi(dai
);
772 struct drm_encoder
*encoder
= hdmi
->encoder
;
773 struct vc4_dev
*vc4
= to_vc4_dev(encoder
->dev
);
776 if (hdmi
->audio
.substream
&& hdmi
->audio
.substream
!= substream
)
779 hdmi
->audio
.substream
= substream
;
782 * If the HDMI encoder hasn't probed, or the encoder is
783 * currently in DVI mode, treat the codec dai as missing.
785 if (!encoder
->crtc
|| !(HDMI_READ(VC4_HDMI_RAM_PACKET_CONFIG
) &
786 VC4_HDMI_RAM_PACKET_ENABLE
))
789 ret
= snd_pcm_hw_constraint_eld(substream
->runtime
,
790 hdmi
->connector
->eld
);
797 static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
802 static void vc4_hdmi_audio_reset(struct vc4_hdmi
*hdmi
)
804 struct drm_encoder
*encoder
= hdmi
->encoder
;
805 struct drm_device
*drm
= encoder
->dev
;
806 struct device
*dev
= &hdmi
->pdev
->dev
;
807 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
810 ret
= vc4_hdmi_stop_packet(encoder
, HDMI_INFOFRAME_TYPE_AUDIO
);
812 dev_err(dev
, "Failed to stop audio infoframe: %d\n", ret
);
814 HD_WRITE(VC4_HD_MAI_CTL
, VC4_HD_MAI_CTL_RESET
);
815 HD_WRITE(VC4_HD_MAI_CTL
, VC4_HD_MAI_CTL_ERRORF
);
816 HD_WRITE(VC4_HD_MAI_CTL
, VC4_HD_MAI_CTL_FLUSH
);
819 static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream
*substream
,
820 struct snd_soc_dai
*dai
)
822 struct vc4_hdmi
*hdmi
= dai_to_hdmi(dai
);
824 if (substream
!= hdmi
->audio
.substream
)
827 vc4_hdmi_audio_reset(hdmi
);
829 hdmi
->audio
.substream
= NULL
;
832 /* HDMI audio codec callbacks */
833 static int vc4_hdmi_audio_hw_params(struct snd_pcm_substream
*substream
,
834 struct snd_pcm_hw_params
*params
,
835 struct snd_soc_dai
*dai
)
837 struct vc4_hdmi
*hdmi
= dai_to_hdmi(dai
);
838 struct drm_encoder
*encoder
= hdmi
->encoder
;
839 struct drm_device
*drm
= encoder
->dev
;
840 struct device
*dev
= &hdmi
->pdev
->dev
;
841 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
842 u32 audio_packet_config
, channel_mask
;
845 if (substream
!= hdmi
->audio
.substream
)
848 dev_dbg(dev
, "%s: %u Hz, %d bit, %d channels\n", __func__
,
849 params_rate(params
), params_width(params
),
850 params_channels(params
));
852 hdmi
->audio
.channels
= params_channels(params
);
853 hdmi
->audio
.samplerate
= params_rate(params
);
855 HD_WRITE(VC4_HD_MAI_CTL
,
856 VC4_HD_MAI_CTL_RESET
|
857 VC4_HD_MAI_CTL_FLUSH
|
858 VC4_HD_MAI_CTL_DLATE
|
859 VC4_HD_MAI_CTL_ERRORE
|
860 VC4_HD_MAI_CTL_ERRORF
);
862 vc4_hdmi_audio_set_mai_clock(hdmi
);
864 audio_packet_config
=
865 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_SAMPLE_FLAT
|
866 VC4_HDMI_AUDIO_PACKET_ZERO_DATA_ON_INACTIVE_CHANNELS
|
867 VC4_SET_FIELD(0xf, VC4_HDMI_AUDIO_PACKET_B_FRAME_IDENTIFIER
);
869 channel_mask
= GENMASK(hdmi
->audio
.channels
- 1, 0);
870 audio_packet_config
|= VC4_SET_FIELD(channel_mask
,
871 VC4_HDMI_AUDIO_PACKET_CEA_MASK
);
873 /* Set the MAI threshold. This logic mimics the firmware's. */
874 if (hdmi
->audio
.samplerate
> 96000) {
875 HD_WRITE(VC4_HD_MAI_THR
,
876 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQHIGH
) |
877 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW
));
878 } else if (hdmi
->audio
.samplerate
> 48000) {
879 HD_WRITE(VC4_HD_MAI_THR
,
880 VC4_SET_FIELD(0x14, VC4_HD_MAI_THR_DREQHIGH
) |
881 VC4_SET_FIELD(0x12, VC4_HD_MAI_THR_DREQLOW
));
883 HD_WRITE(VC4_HD_MAI_THR
,
884 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICHIGH
) |
885 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_PANICLOW
) |
886 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQHIGH
) |
887 VC4_SET_FIELD(0x10, VC4_HD_MAI_THR_DREQLOW
));
890 HDMI_WRITE(VC4_HDMI_MAI_CONFIG
,
891 VC4_HDMI_MAI_CONFIG_BIT_REVERSE
|
892 VC4_SET_FIELD(channel_mask
, VC4_HDMI_MAI_CHANNEL_MASK
));
895 for (i
= 0; i
< 8; i
++) {
896 if (channel_mask
& BIT(i
))
897 channel_map
|= i
<< (3 * i
);
900 HDMI_WRITE(VC4_HDMI_MAI_CHANNEL_MAP
, channel_map
);
901 HDMI_WRITE(VC4_HDMI_AUDIO_PACKET_CONFIG
, audio_packet_config
);
902 vc4_hdmi_set_n_cts(hdmi
);
907 static int vc4_hdmi_audio_trigger(struct snd_pcm_substream
*substream
, int cmd
,
908 struct snd_soc_dai
*dai
)
910 struct vc4_hdmi
*hdmi
= dai_to_hdmi(dai
);
911 struct drm_encoder
*encoder
= hdmi
->encoder
;
912 struct drm_device
*drm
= encoder
->dev
;
913 struct vc4_dev
*vc4
= to_vc4_dev(drm
);
916 case SNDRV_PCM_TRIGGER_START
:
917 vc4_hdmi_set_audio_infoframe(encoder
);
918 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0
,
919 HDMI_READ(VC4_HDMI_TX_PHY_CTL0
) &
920 ~VC4_HDMI_TX_PHY_RNG_PWRDN
);
921 HD_WRITE(VC4_HD_MAI_CTL
,
922 VC4_SET_FIELD(hdmi
->audio
.channels
,
923 VC4_HD_MAI_CTL_CHNUM
) |
924 VC4_HD_MAI_CTL_ENABLE
);
926 case SNDRV_PCM_TRIGGER_STOP
:
927 HD_WRITE(VC4_HD_MAI_CTL
,
928 VC4_HD_MAI_CTL_DLATE
|
929 VC4_HD_MAI_CTL_ERRORE
|
930 VC4_HD_MAI_CTL_ERRORF
);
931 HDMI_WRITE(VC4_HDMI_TX_PHY_CTL0
,
932 HDMI_READ(VC4_HDMI_TX_PHY_CTL0
) |
933 VC4_HDMI_TX_PHY_RNG_PWRDN
);
942 static inline struct vc4_hdmi
*
943 snd_component_to_hdmi(struct snd_soc_component
*component
)
945 struct snd_soc_card
*card
= snd_soc_component_get_drvdata(component
);
947 return snd_soc_card_get_drvdata(card
);
950 static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol
*kcontrol
,
951 struct snd_ctl_elem_info
*uinfo
)
953 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
954 struct vc4_hdmi
*hdmi
= snd_component_to_hdmi(component
);
956 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BYTES
;
957 uinfo
->count
= sizeof(hdmi
->connector
->eld
);
962 static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol
*kcontrol
,
963 struct snd_ctl_elem_value
*ucontrol
)
965 struct snd_soc_component
*component
= snd_kcontrol_chip(kcontrol
);
966 struct vc4_hdmi
*hdmi
= snd_component_to_hdmi(component
);
968 memcpy(ucontrol
->value
.bytes
.data
, hdmi
->connector
->eld
,
969 sizeof(hdmi
->connector
->eld
));
974 static const struct snd_kcontrol_new vc4_hdmi_audio_controls
[] = {
976 .access
= SNDRV_CTL_ELEM_ACCESS_READ
|
977 SNDRV_CTL_ELEM_ACCESS_VOLATILE
,
978 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
980 .info
= vc4_hdmi_audio_eld_ctl_info
,
981 .get
= vc4_hdmi_audio_eld_ctl_get
,
985 static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets
[] = {
986 SND_SOC_DAPM_OUTPUT("TX"),
989 static const struct snd_soc_dapm_route vc4_hdmi_audio_routes
[] = {
990 { "TX", NULL
, "Playback" },
993 static const struct snd_soc_codec_driver vc4_hdmi_audio_codec_drv
= {
994 .component_driver
= {
995 .controls
= vc4_hdmi_audio_controls
,
996 .num_controls
= ARRAY_SIZE(vc4_hdmi_audio_controls
),
997 .dapm_widgets
= vc4_hdmi_audio_widgets
,
998 .num_dapm_widgets
= ARRAY_SIZE(vc4_hdmi_audio_widgets
),
999 .dapm_routes
= vc4_hdmi_audio_routes
,
1000 .num_dapm_routes
= ARRAY_SIZE(vc4_hdmi_audio_routes
),
1004 static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops
= {
1005 .startup
= vc4_hdmi_audio_startup
,
1006 .shutdown
= vc4_hdmi_audio_shutdown
,
1007 .hw_params
= vc4_hdmi_audio_hw_params
,
1008 .set_fmt
= vc4_hdmi_audio_set_fmt
,
1009 .trigger
= vc4_hdmi_audio_trigger
,
1012 static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv
= {
1013 .name
= "vc4-hdmi-hifi",
1015 .stream_name
= "Playback",
1018 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
1019 SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_88200
|
1020 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_176400
|
1021 SNDRV_PCM_RATE_192000
,
1022 .formats
= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
,
1026 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp
= {
1027 .name
= "vc4-hdmi-cpu-dai-component",
1030 static int vc4_hdmi_audio_cpu_dai_probe(struct snd_soc_dai
*dai
)
1032 struct vc4_hdmi
*hdmi
= dai_to_hdmi(dai
);
1034 snd_soc_dai_init_dma_data(dai
, &hdmi
->audio
.dma_data
, NULL
);
1039 static struct snd_soc_dai_driver vc4_hdmi_audio_cpu_dai_drv
= {
1040 .name
= "vc4-hdmi-cpu-dai",
1041 .probe
= vc4_hdmi_audio_cpu_dai_probe
,
1043 .stream_name
= "Playback",
1046 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
|
1047 SNDRV_PCM_RATE_48000
| SNDRV_PCM_RATE_88200
|
1048 SNDRV_PCM_RATE_96000
| SNDRV_PCM_RATE_176400
|
1049 SNDRV_PCM_RATE_192000
,
1050 .formats
= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE
,
1052 .ops
= &vc4_hdmi_audio_dai_ops
,
1055 static const struct snd_dmaengine_pcm_config pcm_conf
= {
1056 .chan_names
[SNDRV_PCM_STREAM_PLAYBACK
] = "audio-rx",
1057 .prepare_slave_config
= snd_dmaengine_pcm_prepare_slave_config
,
1060 static int vc4_hdmi_audio_init(struct vc4_hdmi
*hdmi
)
1062 struct snd_soc_dai_link
*dai_link
= &hdmi
->audio
.link
;
1063 struct snd_soc_card
*card
= &hdmi
->audio
.card
;
1064 struct device
*dev
= &hdmi
->pdev
->dev
;
1068 if (!of_find_property(dev
->of_node
, "dmas", NULL
)) {
1070 "'dmas' DT property is missing, no HDMI audio\n");
1075 * Get the physical address of VC4_HD_MAI_DATA. We need to retrieve
1076 * the bus address specified in the DT, because the physical address
1077 * (the one returned by platform_get_resource()) is not appropriate
1078 * for DMA transfers.
1079 * This VC/MMU should probably be exposed to avoid this kind of hacks.
1081 addr
= of_get_address(dev
->of_node
, 1, NULL
, NULL
);
1082 hdmi
->audio
.dma_data
.addr
= be32_to_cpup(addr
) + VC4_HD_MAI_DATA
;
1083 hdmi
->audio
.dma_data
.addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
1084 hdmi
->audio
.dma_data
.maxburst
= 2;
1086 ret
= devm_snd_dmaengine_pcm_register(dev
, &pcm_conf
, 0);
1088 dev_err(dev
, "Could not register PCM component: %d\n", ret
);
1092 ret
= devm_snd_soc_register_component(dev
, &vc4_hdmi_audio_cpu_dai_comp
,
1093 &vc4_hdmi_audio_cpu_dai_drv
, 1);
1095 dev_err(dev
, "Could not register CPU DAI: %d\n", ret
);
1099 /* register codec and codec dai */
1100 ret
= snd_soc_register_codec(dev
, &vc4_hdmi_audio_codec_drv
,
1101 &vc4_hdmi_audio_codec_dai_drv
, 1);
1103 dev_err(dev
, "Could not register codec: %d\n", ret
);
1107 dai_link
->name
= "MAI";
1108 dai_link
->stream_name
= "MAI PCM";
1109 dai_link
->codec_dai_name
= vc4_hdmi_audio_codec_dai_drv
.name
;
1110 dai_link
->cpu_dai_name
= dev_name(dev
);
1111 dai_link
->codec_name
= dev_name(dev
);
1112 dai_link
->platform_name
= dev_name(dev
);
1114 card
->dai_link
= dai_link
;
1115 card
->num_links
= 1;
1116 card
->name
= "vc4-hdmi";
1120 * Be careful, snd_soc_register_card() calls dev_set_drvdata() and
1121 * stores a pointer to the snd card object in dev->driver_data. This
1122 * means we cannot use it for something else. The hdmi back-pointer is
1123 * now stored in card->drvdata and should be retrieved with
1124 * snd_soc_card_get_drvdata() if needed.
1126 snd_soc_card_set_drvdata(card
, hdmi
);
1127 ret
= devm_snd_soc_register_card(dev
, card
);
1129 dev_err(dev
, "Could not register sound card: %d\n", ret
);
1130 goto unregister_codec
;
1136 snd_soc_unregister_codec(dev
);
1141 static void vc4_hdmi_audio_cleanup(struct vc4_hdmi
*hdmi
)
1143 struct device
*dev
= &hdmi
->pdev
->dev
;
1146 * If drvdata is not set this means the audio card was not
1147 * registered, just skip codec unregistration in this case.
1149 if (dev_get_drvdata(dev
))
1150 snd_soc_unregister_codec(dev
);
1153 static int vc4_hdmi_bind(struct device
*dev
, struct device
*master
, void *data
)
1155 struct platform_device
*pdev
= to_platform_device(dev
);
1156 struct drm_device
*drm
= dev_get_drvdata(master
);
1157 struct vc4_dev
*vc4
= drm
->dev_private
;
1158 struct vc4_hdmi
*hdmi
;
1159 struct vc4_hdmi_encoder
*vc4_hdmi_encoder
;
1160 struct device_node
*ddc_node
;
1164 hdmi
= devm_kzalloc(dev
, sizeof(*hdmi
), GFP_KERNEL
);
1168 vc4_hdmi_encoder
= devm_kzalloc(dev
, sizeof(*vc4_hdmi_encoder
),
1170 if (!vc4_hdmi_encoder
)
1172 vc4_hdmi_encoder
->base
.type
= VC4_ENCODER_TYPE_HDMI
;
1173 hdmi
->encoder
= &vc4_hdmi_encoder
->base
.base
;
1176 hdmi
->hdmicore_regs
= vc4_ioremap_regs(pdev
, 0);
1177 if (IS_ERR(hdmi
->hdmicore_regs
))
1178 return PTR_ERR(hdmi
->hdmicore_regs
);
1180 hdmi
->hd_regs
= vc4_ioremap_regs(pdev
, 1);
1181 if (IS_ERR(hdmi
->hd_regs
))
1182 return PTR_ERR(hdmi
->hd_regs
);
1184 hdmi
->pixel_clock
= devm_clk_get(dev
, "pixel");
1185 if (IS_ERR(hdmi
->pixel_clock
)) {
1186 DRM_ERROR("Failed to get pixel clock\n");
1187 return PTR_ERR(hdmi
->pixel_clock
);
1189 hdmi
->hsm_clock
= devm_clk_get(dev
, "hdmi");
1190 if (IS_ERR(hdmi
->hsm_clock
)) {
1191 DRM_ERROR("Failed to get HDMI state machine clock\n");
1192 return PTR_ERR(hdmi
->hsm_clock
);
1195 ddc_node
= of_parse_phandle(dev
->of_node
, "ddc", 0);
1197 DRM_ERROR("Failed to find ddc node in device tree\n");
1201 hdmi
->ddc
= of_find_i2c_adapter_by_node(ddc_node
);
1202 of_node_put(ddc_node
);
1204 DRM_DEBUG("Failed to get ddc i2c adapter by node\n");
1205 return -EPROBE_DEFER
;
1208 /* Only use the GPIO HPD pin if present in the DT, otherwise
1209 * we'll use the HDMI core's register.
1211 if (of_find_property(dev
->of_node
, "hpd-gpios", &value
)) {
1212 enum of_gpio_flags hpd_gpio_flags
;
1214 hdmi
->hpd_gpio
= of_get_named_gpio_flags(dev
->of_node
,
1217 if (hdmi
->hpd_gpio
< 0) {
1218 ret
= hdmi
->hpd_gpio
;
1222 hdmi
->hpd_active_low
= hpd_gpio_flags
& OF_GPIO_ACTIVE_LOW
;
1227 pm_runtime_enable(dev
);
1229 drm_encoder_init(drm
, hdmi
->encoder
, &vc4_hdmi_encoder_funcs
,
1230 DRM_MODE_ENCODER_TMDS
, NULL
);
1231 drm_encoder_helper_add(hdmi
->encoder
, &vc4_hdmi_encoder_helper_funcs
);
1233 hdmi
->connector
= vc4_hdmi_connector_init(drm
, hdmi
->encoder
);
1234 if (IS_ERR(hdmi
->connector
)) {
1235 ret
= PTR_ERR(hdmi
->connector
);
1236 goto err_destroy_encoder
;
1239 ret
= vc4_hdmi_audio_init(hdmi
);
1241 goto err_destroy_encoder
;
1245 err_destroy_encoder
:
1246 vc4_hdmi_encoder_destroy(hdmi
->encoder
);
1247 pm_runtime_disable(dev
);
1249 put_device(&hdmi
->ddc
->dev
);
1254 static void vc4_hdmi_unbind(struct device
*dev
, struct device
*master
,
1257 struct drm_device
*drm
= dev_get_drvdata(master
);
1258 struct vc4_dev
*vc4
= drm
->dev_private
;
1259 struct vc4_hdmi
*hdmi
= vc4
->hdmi
;
1261 vc4_hdmi_audio_cleanup(hdmi
);
1263 vc4_hdmi_connector_destroy(hdmi
->connector
);
1264 vc4_hdmi_encoder_destroy(hdmi
->encoder
);
1266 pm_runtime_disable(dev
);
1268 put_device(&hdmi
->ddc
->dev
);
1273 static const struct component_ops vc4_hdmi_ops
= {
1274 .bind
= vc4_hdmi_bind
,
1275 .unbind
= vc4_hdmi_unbind
,
1278 static int vc4_hdmi_dev_probe(struct platform_device
*pdev
)
1280 return component_add(&pdev
->dev
, &vc4_hdmi_ops
);
1283 static int vc4_hdmi_dev_remove(struct platform_device
*pdev
)
1285 component_del(&pdev
->dev
, &vc4_hdmi_ops
);
1289 static const struct of_device_id vc4_hdmi_dt_match
[] = {
1290 { .compatible
= "brcm,bcm2835-hdmi" },
1294 struct platform_driver vc4_hdmi_driver
= {
1295 .probe
= vc4_hdmi_dev_probe
,
1296 .remove
= vc4_hdmi_dev_remove
,
1299 .of_match_table
= vc4_hdmi_dt_match
,