9a294b3f31dff25d7acb66d504cf8c6178724a0e
[openwrt/staging/jow.git] / target / linux / bcm27xx / patches-5.10 / 950-0636-drm-vc4-Register-HDMI-codec.patch
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
5
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.
8
9 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
10 ---
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(-)
15
16 --- a/drivers/gpu/drm/vc4/Kconfig
17 +++ b/drivers/gpu/drm/vc4/Kconfig
18 @@ -12,6 +12,7 @@ config DRM_VC4
19 select SND_PCM
20 select SND_PCM_ELD
21 select SND_SOC_GENERIC_DMAENGINE_PCM
22 + select SND_SOC_HDMI_CODEC
23 select DRM_MIPI_DSI
24 help
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
28 @@ -47,6 +47,7 @@
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>
36 @@ -95,6 +96,12 @@
37 # define VC4_HD_M_ENABLE BIT(0)
38
39 #define CEC_CLOCK_FREQ 40000
40 +#define HDMI_14_MAX_TMDS_CLK (340 * 1000 * 1000)
41 +
42 +static bool vc4_hdmi_mode_needs_scrambling(const struct drm_display_mode *mode)
43 +{
44 + return (mode->clock * 1000) > HDMI_14_MAX_TMDS_CLK;
45 +}
46
47 static int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused)
48 {
49 @@ -472,16 +479,10 @@ static void vc4_hdmi_set_spd_infoframe(s
50 static void vc4_hdmi_set_audio_infoframe(struct drm_encoder *encoder)
51 {
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;
55 - int ret;
56 -
57 - ret = hdmi_audio_infoframe_init(&frame.audio);
58 -
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;
63
64 + memcpy(&frame.audio, audio, sizeof(*audio));
65 vc4_hdmi_write_infoframe(encoder, &frame);
66 }
67
68 @@ -1214,18 +1215,10 @@ static inline struct vc4_hdmi *dai_to_hd
69 return snd_soc_card_get_drvdata(card);
70 }
71
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)
75 {
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;
80 - int ret;
81 -
82 - if (vc4_hdmi->audio.substream && vc4_hdmi->audio.substream != substream)
83 - return -EINVAL;
84 -
85 - vc4_hdmi->audio.substream = substream;
86
87 /*
88 * If the HDMI encoder hasn't probed, or the encoder is
89 @@ -1235,15 +1228,18 @@ static int vc4_hdmi_audio_startup(struct
90 VC4_HDMI_RAM_PACKET_ENABLE))
91 return -ENODEV;
92
93 - ret = snd_pcm_hw_constraint_eld(substream->runtime, connector->eld);
94 - if (ret)
95 - return ret;
96 + vc4_hdmi->audio.streaming = true;
97
98 - return 0;
99 -}
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);
106 +
107 + if (vc4_hdmi->variant->phy_rng_enable)
108 + vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
109
110 -static int vc4_hdmi_audio_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
111 -{
112 return 0;
113 }
114
115 @@ -1263,17 +1259,20 @@ static void vc4_hdmi_audio_reset(struct
116 HDMI_WRITE(HDMI_MAI_CTL, VC4_HD_MAI_CTL_FLUSH);
117 }
118
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)
122 {
123 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
124 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
125
126 - if (substream != vc4_hdmi->audio.substream)
127 - return;
128 + HDMI_WRITE(HDMI_MAI_CTL,
129 + VC4_HD_MAI_CTL_DLATE |
130 + VC4_HD_MAI_CTL_ERRORE |
131 + VC4_HD_MAI_CTL_ERRORF);
132
133 - vc4_hdmi_audio_reset(vc4_hdmi);
134 + if (vc4_hdmi->variant->phy_rng_disable)
135 + vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
136
137 - vc4_hdmi->audio.substream = NULL;
138 + vc4_hdmi->audio.streaming = false;
139 + vc4_hdmi_audio_reset(vc4_hdmi);
140 }
141
142 static int sample_rate_to_mai_fmt(int samplerate)
143 @@ -1315,42 +1314,35 @@ static int sample_rate_to_mai_fmt(int sa
144 }
145
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)
152 {
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;
158 u32 channel_map;
159 u32 mai_audio_format;
160 u32 mai_sample_rate;
161
162 - if (substream != vc4_hdmi->audio.substream)
163 - return -EINVAL;
164 + dev_dbg(dev, "%s: %u Hz, %d bit, %d channels\n", __func__,
165 + params->sample_rate, params->sample_width,
166 + params->channels);
167
168 - dev_dbg(dev, "%s: %u Hz, %d bit, %d channels AES0=%02x\n",
169 - __func__,
170 - substream->runtime->rate,
171 - snd_pcm_format_width(substream->runtime->format),
172 - substream->runtime->channels,
173 - vc4_hdmi->audio.iec_status[0]);
174 -
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;
179
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);
190
191 vc4_hdmi_audio_set_mai_clock(vc4_hdmi);
192
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;
199 else
200 mai_audio_format = VC4_HDMI_MAI_FORMAT_PCM;
201 @@ -1387,148 +1379,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);
204
205 + memcpy(&vc4_hdmi->audio.infoframe, &params->cea, sizeof(params->cea));
206 vc4_hdmi_set_audio_infoframe(encoder);
207
208 return 0;
209 }
210
211 -static int vc4_hdmi_audio_trigger(struct snd_pcm_substream *substream, int cmd,
212 - struct snd_soc_dai *dai)
213 -{
214 - struct vc4_hdmi *vc4_hdmi = dai_to_hdmi(dai);
215 -
216 - switch (cmd) {
217 - case SNDRV_PCM_TRIGGER_START:
218 - vc4_hdmi->audio.streaming = true;
219 -
220 - if (vc4_hdmi->variant->phy_rng_enable)
221 - vc4_hdmi->variant->phy_rng_enable(vc4_hdmi);
222 -
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);
229 - break;
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);
235 -
236 - if (vc4_hdmi->variant->phy_rng_disable)
237 - vc4_hdmi->variant->phy_rng_disable(vc4_hdmi);
238 -
239 - vc4_hdmi->audio.streaming = false;
240 -
241 - break;
242 - default:
243 - break;
244 - }
245 -
246 - return 0;
247 -}
248 -
249 -static inline struct vc4_hdmi *
250 -snd_component_to_hdmi(struct snd_soc_component *component)
251 -{
252 - struct snd_soc_card *card = snd_soc_component_get_drvdata(component);
253 -
254 - return snd_soc_card_get_drvdata(card);
255 -}
256 -
257 -static int vc4_hdmi_audio_eld_ctl_info(struct snd_kcontrol *kcontrol,
258 - struct snd_ctl_elem_info *uinfo)
259 -{
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;
263 -
264 - uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
265 - uinfo->count = sizeof(connector->eld);
266 -
267 - return 0;
268 -}
269 -
270 -static int vc4_hdmi_audio_eld_ctl_get(struct snd_kcontrol *kcontrol,
271 - struct snd_ctl_elem_value *ucontrol)
272 -{
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;
276 -
277 - memcpy(ucontrol->value.bytes.data, connector->eld,
278 - sizeof(connector->eld));
279 -
280 - return 0;
281 -}
282 -
283 -static int vc4_spdif_info(struct snd_kcontrol *kcontrol,
284 - struct snd_ctl_elem_info *uinfo)
285 -{
286 - uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
287 - uinfo->count = 1;
288 - return 0;
289 -}
290 -
291 -static int vc4_spdif_playback_get(struct snd_kcontrol *kcontrol,
292 - struct snd_ctl_elem_value *ucontrol)
293 -{
294 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
295 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
296 -
297 - memcpy(ucontrol->value.iec958.status, vc4_hdmi->audio.iec_status,
298 - sizeof(vc4_hdmi->audio.iec_status));
299 -
300 - return 0;
301 -}
302 -
303 -static int vc4_spdif_playback_put(struct snd_kcontrol *kcontrol,
304 - struct snd_ctl_elem_value *ucontrol)
305 -{
306 - struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
307 - struct vc4_hdmi *vc4_hdmi = snd_component_to_hdmi(component);
308 -
309 - memcpy(vc4_hdmi->audio.iec_status, ucontrol->value.iec958.status,
310 - sizeof(vc4_hdmi->audio.iec_status));
311 -
312 - return 0;
313 -}
314 -
315 -static int vc4_spdif_mask_get(struct snd_kcontrol *kcontrol,
316 - struct snd_ctl_elem_value *ucontrol)
317 -{
318 - memset(ucontrol->value.iec958.status, 0xff,
319 - sizeof_field(struct vc4_hdmi_audio, iec_status));
320 -
321 - return 0;
322 -}
323 -
324 -static const struct snd_kcontrol_new vc4_hdmi_audio_controls[] = {
325 - {
326 - .access = SNDRV_CTL_ELEM_ACCESS_READ |
327 - SNDRV_CTL_ELEM_ACCESS_VOLATILE,
328 - .iface = SNDRV_CTL_ELEM_IFACE_PCM,
329 - .name = "ELD",
330 - .info = vc4_hdmi_audio_eld_ctl_info,
331 - .get = vc4_hdmi_audio_eld_ctl_get,
332 - },
333 - {
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,
339 - },
340 - {
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,
345 - },
346 -};
347 -
348 static const struct snd_soc_dapm_widget vc4_hdmi_audio_widgets[] = {
349 SND_SOC_DAPM_OUTPUT("TX"),
350 };
351 @@ -1539,8 +1395,6 @@ static const struct snd_soc_dapm_route v
352
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 @@ -1551,28 +1405,6 @@ static const struct snd_soc_component_dr
361 .non_legacy_dai_naming = 1,
362 };
363
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,
370 -};
371 -
372 -static struct snd_soc_dai_driver vc4_hdmi_audio_codec_dai_drv = {
373 - .name = "vc4-hdmi-hifi",
374 - .playback = {
375 - .stream_name = "Playback",
376 - .channels_min = 2,
377 - .channels_max = 8,
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,
383 - },
384 -};
385 -
386 static const struct snd_soc_component_driver vc4_hdmi_audio_cpu_dai_comp = {
387 .name = "vc4-hdmi-cpu-dai-component",
388 };
389 @@ -1599,7 +1431,6 @@ static struct snd_soc_dai_driver vc4_hdm
390 SNDRV_PCM_RATE_192000,
391 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
392 },
393 - .ops = &vc4_hdmi_audio_dai_ops,
394 };
395
396 static const struct snd_dmaengine_pcm_config pcm_conf = {
397 @@ -1607,6 +1438,31 @@ static const struct snd_dmaengine_pcm_co
398 .prepare_slave_config = snd_dmaengine_pcm_prepare_slave_config,
399 };
400
401 +
402 +static int vc4_hdmi_audio_get_eld(struct device *dev, void *data,
403 + uint8_t *buf, size_t len)
404 +{
405 + struct vc4_hdmi *vc4_hdmi = dev_get_drvdata(dev);
406 + struct drm_connector *connector = &vc4_hdmi->connector;
407 +
408 + memcpy(buf, connector->eld, min(sizeof(connector->eld), len));
409 +
410 + return 0;
411 +}
412 +
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,
418 +};
419 +
420 +struct hdmi_codec_pdata vc4_hdmi_codec_pdata = {
421 + .ops = &vc4_hdmi_codec_ops,
422 + .max_i2s_channels = 8,
423 + .i2s = 1,
424 +};
425 +
426 static int vc4_hdmi_audio_init(struct vc4_hdmi *vc4_hdmi)
427 {
428 const struct vc4_hdmi_register *mai_data =
429 @@ -1614,6 +1470,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;
434 const __be32 *addr;
435 int index;
436 int ret;
437 @@ -1649,11 +1506,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;
440
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;
445 -
446 ret = devm_snd_dmaengine_pcm_register(dev, &pcm_conf, 0);
447 if (ret) {
448 dev_err(dev, "Could not register PCM component: %d\n", ret);
449 @@ -1667,12 +1519,13 @@ static int vc4_hdmi_audio_init(struct vc
450 return ret;
451 }
452
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);
456 - if (ret) {
457 - dev_err(dev, "Could not register component: %d\n", ret);
458 - return 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);
466 }
467
468 dai_link->cpus = &vc4_hdmi->audio.cpu;
469 @@ -1685,9 +1538,9 @@ static int vc4_hdmi_audio_init(struct vc
470
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);
479
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 {
484 int samplerate;
485 int channels;
486 struct snd_dmaengine_dai_dma_data dma_data;
487 - struct snd_pcm_substream *substream;
488 -
489 + struct hdmi_audio_infoframe infoframe;
490 bool streaming;
491
492 unsigned char iec_status[4];