1 From 1e6a19ca5f275155a4dd1961d351306b589b11a0 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Mon, 26 Apr 2021 14:42:26 +0200
4 Subject: [PATCH] drm/vc4: Register HDMI codec
6 The hdmi-codec brings a lot of advanced features, including the HDMI
7 channel mapping. Let's use it in our driver instead of our own codec.
9 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
11 drivers/gpu/drm/vc4/Kconfig | 1 +
12 drivers/gpu/drm/vc4/vc4_hdmi.c | 311 +++++++++------------------------
13 drivers/gpu/drm/vc4/vc4_hdmi.h | 3 +-
14 3 files changed, 84 insertions(+), 231 deletions(-)
16 --- a/drivers/gpu/drm/vc4/Kconfig
17 +++ b/drivers/gpu/drm/vc4/Kconfig
18 @@ -12,6 +12,7 @@ config DRM_VC4
21 select SND_SOC_GENERIC_DMAENGINE_PCM
22 + select SND_SOC_HDMI_CODEC
25 Choose this option if you have a system that has a Broadcom
26 --- a/drivers/gpu/drm/vc4/vc4_hdmi.c
27 +++ b/drivers/gpu/drm/vc4/vc4_hdmi.c
29 #include <linux/reset.h>
30 #include <sound/asoundef.h>
31 #include <sound/dmaengine_pcm.h>
32 +#include <sound/hdmi-codec.h>
33 #include <sound/pcm_drm_eld.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
38 #define HSM_MIN_CLOCK_FREQ 120000000
39 #define CEC_CLOCK_FREQ 40000
40 +#define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
42 +static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
44 + return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
47 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
49 @@ -473,16 +480,10 @@ static void vc4_hdmi_set_spd_infoframe(s
50 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
52 struct vc4_hdmi *vc4_hdmi = encoder_to_vc4_hdmi(encoder);
53 + struct hdmi_audio_infoframe *audio = &vc4_hdmi->audio.infoframe;
54 union hdmi_infoframe frame;
57 - ret = hdmi_audio_infoframe_init(&frame.audio);
59 - frame.audio.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
60 - frame.audio.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
61 - frame.audio.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
62 - frame.audio.channels = vc4_hdmi->audio.channels;
64 + memcpy(&frame.audio, audio, sizeof(*audio));
65 vc4_hdmi_write_infoframe(encoder, &frame);
68 @@ -1215,18 +1216,10 @@ static inline struct vc4_hdmi *dai_to_hd
69 return snd_soc_card_get_drvdata(card);
72 -static int vc4_hdmi_audio_startup(struct snd_pcm_substream *substream,
73 - struct snd_soc_dai *dai)
74 +static int vc4_hdmi_audio_startup(struct device *dev, void *data)
76 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
77 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
78 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
79 - struct drm_connector *connector = &vc4_hdmi->connector;
82 - if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
85 - vc4_hdmi->audio.substream = substream;
88 * If the HDMI encoder hasn't probed, or the encoder is
89 @@ -1236,15 +1229,18 @@ static int vc4_hdmi_audio_startup(struct
90 VC4_HDMI_RAM_PACKET_ENABLE))
93 - ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
96 + vc4_hdmi->audio.streaming = true;
100 + HDMI_WRITE(HDMI_MAI_CTL,
101 + VC4_HD_MAI_CTL_RESET |
102 + VC4_HD_MAI_CTL_FLUSH |
103 + VC4_HD_MAI_CTL_DLATE |
104 + VC4_HD_MAI_CTL_ERRORE |
105 + VC4_HD_MAI_CTL_ERRORF);
107 + if (vc4_hdmi->variant->phy_rng_enable)
108 + vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
110 -static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
115 @@ -1264,17 +1260,20 @@ static void vc4_hdmi_audio_reset(struct
116 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
119 -static void vc4_hdmi_audio_shutdown(struct snd_pcm_substream *substream,
120 - struct snd_soc_dai *dai)
121 +static void vc4_hdmi_audio_shutdown(struct device *dev, void *data)
123 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
124 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
126 - if (substream != vc4_hdmi->audio.substream)
128 + HDMI_WRITE(HDMI_MAI_CTL,
129 + VC4_HD_MAI_CTL_DLATE |
130 + VC4_HD_MAI_CTL_ERRORE |
131 + VC4_HD_MAI_CTL_ERRORF);
133 - vc4_hdmi_audio_reset(vc4_hdmi);
134 + if (vc4_hdmi->variant->phy_rng_disable)
135 + vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
137 - vc4_hdmi->audio.substream = NULL;
138 + vc4_hdmi->audio.streaming = false;
139 + vc4_hdmi_audio_reset(vc4_hdmi);
142 static int sample_rate_to_mai_fmt(int samplerate)
143 @@ -1316,42 +1315,35 @@ static int sample_rate_to_mai_fmt(int sa
146 /* HDMI audio codec callbacks */
147 -static int vc4_hdmi_audio_prepare(struct snd_pcm_substream *substream,
148 - struct snd_soc_dai *dai)
149 +static int vc4_hdmi_audio_prepare(struct device *dev, void *data,
150 + struct hdmi_codec_daifmt *daifmt,
151 + struct hdmi_codec_params *params)
153 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
154 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
155 struct drm_encoder *encoder = &vc4_hdmi->encoder.base.base;
156 - struct device *dev = &vc4_hdmi->pdev->dev;
157 u32 audio_packet_config, channel_mask;
159 u32 mai_audio_format;
162 - if (substream != vc4_hdmi->audio.substream)
164 + dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
165 + params->sample_rate, params->sample_width,
168 - dev_dbg(dev, "%s: %u Hz, %d bit, %d channels AES0=%02x\n",
170 - substream->runtime->rate,
171 - snd_pcm_format_width(substream->runtime->format),
172 - substream->runtime->channels,
173 - vc4_hdmi->audio.iec_status[0]);
175 - vc4_hdmi->audio.channels = substream->runtime->channels;
176 - vc4_hdmi->audio.samplerate = substream->runtime->rate;
177 + vc4_hdmi->audio.channels = params->channels;
178 + vc4_hdmi->audio.samplerate = params->sample_rate;
180 HDMI_WRITE(HDMI_MAI_CTL,
181 - VC4_HD_MAI_CTL_RESET |
182 - VC4_HD_MAI_CTL_FLUSH |
183 - VC4_HD_MAI_CTL_DLATE |
184 - VC4_HD_MAI_CTL_ERRORE |
185 - VC4_HD_MAI_CTL_ERRORF);
186 + VC4_SET_FIELD(params->channels, VC4_HD_MAI_CTL_CHNUM) |
187 + VC4_HD_MAI_CTL_WHOLSMP |
188 + VC4_HD_MAI_CTL_CHALIGN |
189 + VC4_HD_MAI_CTL_ENABLE);
191 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
193 mai_sample_rate = sample_rate_to_mai_fmt(vc4_hdmi->audio.samplerate);
194 - if (vc4_hdmi->audio.iec_status[0] & IEC958_AES0_NONAUDIO &&
195 - vc4_hdmi->audio.channels == 8)
196 + if (params->iec.status[0] & IEC958_AES0_NONAUDIO &&
197 + params->channels == 8)
198 mai_audio_format = VC4_HDMI_MAI_FORMAT_HBR;
200 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
201 @@ -1388,148 +1380,12 @@ static int vc4_hdmi_audio_prepare(struct
202 HDMI_WRITE(HDMI_AUDIO_PACKET_CONFIG, audio_packet_config);
203 vc4_hdmi_set_n_cts(vc4_hdmi);
205 + memcpy(&vc4_hdmi->audio.infoframe, ¶ms->cea, sizeof(params->cea));
206 vc4_hdmi_set_audio_infoframe(encoder);
211 -static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
212 - struct snd_soc_dai *dai)
214 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
217 - case SNDRV_PCM_TRIGGER_START:
218 - vc4_hdmi->audio.streaming = true;
220 - if (vc4_hdmi->variant->phy_rng_enable)
221 - vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
223 - HDMI_WRITE(HDMI_MAI_CTL,
224 - VC4_SET_FIELD(vc4_hdmi->audio.channels,
225 - VC4_HD_MAI_CTL_CHNUM) |
226 - VC4_HD_MAI_CTL_WHOLSMP |
227 - VC4_HD_MAI_CTL_CHALIGN |
228 - VC4_HD_MAI_CTL_ENABLE);
230 - case SNDRV_PCM_TRIGGER_STOP:
231 - HDMI_WRITE(HDMI_MAI_CTL,
232 - VC4_HD_MAI_CTL_DLATE |
233 - VC4_HD_MAI_CTL_ERRORE |
234 - VC4_HD_MAI_CTL_ERRORF);
236 - if (vc4_hdmi->variant->phy_rng_disable)
237 - vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
239 - vc4_hdmi->audio.streaming = false;
249 -static inline struct vc4_hdmi *
250 -snd_component_to_hdmi(struct snd_soc_component *component)
252 - struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
254 - return snd_soc_card_get_drvdata(card);
257 -static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
258 - struct snd_ctl_elem_info *uinfo)
260 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
261 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
262 - struct drm_connector *connector = &vc4_hdmi->connector;
264 - uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
265 - uinfo->count = sizeof(connector->eld);
270 -static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
271 - struct snd_ctl_elem_value *ucontrol)
273 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
274 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
275 - struct drm_connector *connector = &vc4_hdmi->connector;
277 - memcpy(ucontrol->value.bytes.data, connector->eld,
278 - sizeof(connector->eld));
283 -static int vc4_spdif_info(struct snd_kcontrol *kcontrol,
284 - struct snd_ctl_elem_info *uinfo)
286 - uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
291 -static int vc4_spdif_playback_get(struct snd_kcontrol *kcontrol,
292 - struct snd_ctl_elem_value *ucontrol)
294 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
295 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
297 - memcpy(ucontrol->value.iec958.status, vc4_hdmi->audio.iec_status,
298 - sizeof(vc4_hdmi->audio.iec_status));
303 -static int vc4_spdif_playback_put(struct snd_kcontrol *kcontrol,
304 - struct snd_ctl_elem_value *ucontrol)
306 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
307 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
309 - memcpy(vc4_hdmi->audio.iec_status, ucontrol->value.iec958.status,
310 - sizeof(vc4_hdmi->audio.iec_status));
315 -static int vc4_spdif_mask_get(struct snd_kcontrol *kcontrol,
316 - struct snd_ctl_elem_value *ucontrol)
318 - memset(ucontrol->value.iec958.status, 0xff,
319 - sizeof_field(struct vc4_hdmi_audio, iec_status));
324 -static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
326 - .access = SNDRV_CTL_ELEM_ACCESS_READ |
327 - SNDRV_CTL_ELEM_ACCESS_VOLATILE,
328 - .iface = SNDRV_CTL_ELEM_IFACE_PCM,
330 - .info = vc4_hdmi_audio_eld_ctl_info,
331 - .get = vc4_hdmi_audio_eld_ctl_get,
334 - .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
335 - .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
336 - .info = vc4_spdif_info,
337 - .get = vc4_spdif_playback_get,
338 - .put = vc4_spdif_playback_put,
341 - .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
342 - .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
343 - .info = vc4_spdif_info,
344 - .get = vc4_spdif_mask_get,
348 static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
349 SND_SOC_DAPM_OUTPUT("TX"),
351 @@ -1540,8 +1396,6 @@ static const struct snd_soc_dapm_route v
353 static const struct snd_soc_component_driver vc4_hdmi_audio_component_drv = {
354 .name = "vc4-hdmi-codec-dai-component",
355 - .controls = vc4_hdmi_audio_controls,
356 - .num_controls = ARRAY_SIZE(vc4_hdmi_audio_controls),
357 .dapm_widgets = vc4_hdmi_audio_widgets,
358 .num_dapm_widgets = ARRAY_SIZE(vc4_hdmi_audio_widgets),
359 .dapm_routes = vc4_hdmi_audio_routes,
360 @@ -1552,28 +1406,6 @@ static const struct snd_soc_component_dr
361 .non_legacy_dai_naming = 1,
364 -static const struct snd_soc_dai_ops vc4_hdmi_audio_dai_ops = {
365 - .startup = vc4_hdmi_audio_startup,
366 - .shutdown = vc4_hdmi_audio_shutdown,
367 - .prepare = vc4_hdmi_audio_prepare,
368 - .set_fmt = vc4_hdmi_audio_set_fmt,
369 - .trigger = vc4_hdmi_audio_trigger,
372 -static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
373 - .name = "vc4-hdmi-hifi",
375 - .stream_name = "Playback",
378 - .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
379 - SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
380 - SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
381 - SNDRV_PCM_RATE_192000,
382 - .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
386 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
387 .name = "vc4-hdmi-cpu-dai-component",
389 @@ -1600,7 +1432,6 @@ static struct snd_soc_dai_driver vc4_hdm
390 SNDRV_PCM_RATE_192000,
391 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
393 - .ops = &vc4_hdmi_audio_dai_ops,
396 static const struct snd_dmaengine_pcm_config pcm_conf = {
397 @@ -1608,6 +1439,31 @@ static const struct snd_dmaengine_pcm_co
398 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
402 +static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
403 + uint8_t *buf, size_t len)
405 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
406 + struct drm_connector *connector = &vc4_hdmi->connector;
408 + memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
413 +static const struct hdmi_codec_ops vc4_hdmi_codec_ops = {
414 + .get_eld = vc4_hdmi_audio_get_eld,
415 + .prepare = vc4_hdmi_audio_prepare,
416 + .audio_shutdown = vc4_hdmi_audio_shutdown,
417 + .audio_startup = vc4_hdmi_audio_startup,
420 +struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
421 + .ops = &vc4_hdmi_codec_ops,
422 + .max_i2s_channels = 8,
426 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
428 const struct vc4_hdmi_register *mai_data =
429 @@ -1615,6 +1471,7 @@ static int vc4_hdmi_audio_init(struct vc
430 struct snd_soc_dai_link *dai_link = &vc4_hdmi->audio.link;
431 struct snd_soc_card *card = &vc4_hdmi->audio.card;
432 struct device *dev = &vc4_hdmi->pdev->dev;
433 + struct platform_device *codec_pdev;
437 @@ -1650,11 +1507,6 @@ static int vc4_hdmi_audio_init(struct vc
438 vc4_hdmi->audio.dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
439 vc4_hdmi->audio.dma_data.maxburst = 2;
441 - vc4_hdmi->audio.iec_status[0] = IEC958_AES0_CON_NOT_COPYRIGHT;
442 - vc4_hdmi->audio.iec_status[1] =
443 - IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
444 - vc4_hdmi->audio.iec_status[3] = IEC958_AES3_CON_FS_48000;
446 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
448 dev_err(dev, "Could not register PCM component: %d\n", ret);
449 @@ -1668,12 +1520,13 @@ static int vc4_hdmi_audio_init(struct vc
453 - /* register component and codec dai */
454 - ret = devm_snd_soc_register_component(dev, &vc4_hdmi_audio_component_drv,
455 - &vc4_hdmi_audio_codec_dai_drv, 1);
457 - dev_err(dev, "Could not register component: %d\n", ret);
459 + codec_pdev = platform_device_register_data(dev, HDMI_CODEC_DRV_NAME,
460 + PLATFORM_DEVID_AUTO,
461 + &vc4_hdmi_codec_pdata,
462 + sizeof(vc4_hdmi_codec_pdata));
463 + if (IS_ERR(codec_pdev)) {
464 + dev_err(dev, "Couldn't register the HDMI codec: %ld\n", PTR_ERR(codec_pdev));
465 + return PTR_ERR(codec_pdev);
468 dai_link->cpus = &vc4_hdmi->audio.cpu;
469 @@ -1686,9 +1539,9 @@ static int vc4_hdmi_audio_init(struct vc
471 dai_link->name = "MAI";
472 dai_link->stream_name = "MAI PCM";
473 - dai_link->codecs->dai_name = vc4_hdmi_audio_codec_dai_drv.name;
474 + dai_link->codecs->dai_name = "i2s-hifi";
475 dai_link->cpus->dai_name = dev_name(dev);
476 - dai_link->codecs->name = dev_name(dev);
477 + dai_link->codecs->name = dev_name(&codec_pdev->dev);
478 dai_link->platforms->name = dev_name(dev);
480 card->dai_link = dai_link;
481 --- a/drivers/gpu/drm/vc4/vc4_hdmi.h
482 +++ b/drivers/gpu/drm/vc4/vc4_hdmi.h
483 @@ -114,8 +114,7 @@ struct vc4_hdmi_audio {
486 struct snd_dmaengine_dai_dma_data dma_data;
487 - struct snd_pcm_substream *substream;
489 + struct hdmi_audio_infoframe infoframe;
492 unsigned char iec_status[4];