1 From 067cda9dd7d018b033877df4996383b3529fdbad Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 30 Apr 2021 14:22:06 +0200
4 Subject: [PATCH] ASoC: hdmi-codec: Add a prepare hook
6 The IEC958 status bit is usually set by the userspace after hw_params
7 has been called, so in order to use whatever is set by the userspace, we
8 need to implement the prepare hook. Let's add it to the hdmi_codec_ops,
9 and mandate that either prepare or hw_params is implemented.
11 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
13 include/sound/hdmi-codec.h | 12 +++-
14 sound/soc/codecs/hdmi-codec.c | 112 ++++++++++++++++++++++++++--------
15 2 files changed, 99 insertions(+), 25 deletions(-)
17 --- a/include/sound/hdmi-codec.h
18 +++ b/include/sound/hdmi-codec.h
19 @@ -65,13 +65,23 @@ struct hdmi_codec_ops {
22 * Configures HDMI-encoder for audio stream.
24 + * Having either prepare or hw_params is mandatory.
26 int (*hw_params)(struct device *dev, void *data,
27 struct hdmi_codec_daifmt *fmt,
28 struct hdmi_codec_params *hparms);
31 + * Configures HDMI-encoder for audio stream. Can be called
32 + * multiple times for each setup.
34 + * Having either prepare or hw_params is mandatory.
36 + int (*prepare)(struct device *dev, void *data,
37 + struct hdmi_codec_daifmt *fmt,
38 + struct hdmi_codec_params *hparms);
41 * Shuts down the audio stream.
44 --- a/sound/soc/codecs/hdmi-codec.c
45 +++ b/sound/soc/codecs/hdmi-codec.c
46 @@ -480,6 +480,42 @@ static void hdmi_codec_shutdown(struct s
47 mutex_unlock(&hcp->lock);
50 +static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
51 + unsigned int sample_width,
52 + unsigned int sample_rate,
53 + unsigned int channels,
54 + struct hdmi_codec_params *hp)
56 + struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
59 + /* Select a channel allocation that matches with ELD and pcm channels */
60 + idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
62 + dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
64 + hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
68 + memset(hp, 0, sizeof(*hp));
70 + hdmi_audio_infoframe_init(&hp->cea);
71 + hp->cea.channels = channels;
72 + hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
73 + hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
74 + hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
75 + hp->cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
77 + hp->sample_width = sample_width;
78 + hp->sample_rate = sample_rate;
79 + hp->channels = channels;
81 + hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
86 static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
87 struct snd_pcm_hw_params *params,
88 struct snd_soc_dai *dai)
89 @@ -494,13 +530,24 @@ static int hdmi_codec_hw_params(struct s
90 .dig_subframe = { 0 },
96 + if (!hcp->hcd.ops->hw_params)
99 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
100 params_width(params), params_rate(params),
101 params_channels(params));
103 - memcpy(hp.iec.status, hcp->iec_status, sizeof(hp->iec_status));
104 + ret = hdmi_codec_fill_codec_params(dai,
105 + params_width(params),
106 + params_rate(params),
107 + params_channels(params),
112 + memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
113 ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
114 sizeof(hp.iec.status));
116 @@ -509,32 +556,47 @@ static int hdmi_codec_hw_params(struct s
120 - hdmi_audio_infoframe_init(&hp.cea);
121 - hp.cea.channels = params_channels(params);
122 - hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
123 - hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
124 - hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
126 - /* Select a channel allocation that matches with ELD and pcm channels */
127 - idx = hdmi_codec_get_ch_alloc_table_idx(hcp, hp.cea.channels);
129 - dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
131 - hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
134 - hp.cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
135 - hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
137 - hp.sample_width = params_width(params);
138 - hp.sample_rate = params_rate(params);
139 - hp.channels = params_channels(params);
141 cf->bit_fmt = params_format(params);
142 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
146 +static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
147 + struct snd_soc_dai *dai)
149 + struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
150 + struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
151 + struct snd_pcm_runtime *runtime = substream->runtime;
152 + unsigned int channels = runtime->channels;
153 + unsigned int width = snd_pcm_format_width(runtime->format);
154 + unsigned int rate = runtime->rate;
155 + struct hdmi_codec_params hp;
158 + if (!hcp->hcd.ops->prepare)
161 + dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
162 + width, rate, channels);
164 + ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
168 + memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
169 + ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
170 + sizeof(hp.iec.status));
172 + dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
177 + cf->bit_fmt = runtime->format;
178 + return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
182 static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
185 @@ -626,6 +688,7 @@ static const struct snd_soc_dai_ops hdmi
186 .startup = hdmi_codec_startup,
187 .shutdown = hdmi_codec_shutdown,
188 .hw_params = hdmi_codec_hw_params,
189 + .prepare = hdmi_codec_prepare,
190 .set_fmt = hdmi_codec_i2s_set_fmt,
191 .mute_stream = hdmi_codec_mute,
193 @@ -889,7 +952,8 @@ static int hdmi_codec_probe(struct platf
196 dai_count = hcd->i2s + hcd->spdif;
197 - if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
198 + if (dai_count < 1 || !hcd->ops ||
199 + (!hcd->ops->hw_params && !hcd->ops->prepare) ||
200 !hcd->ops->audio_shutdown) {
201 dev_err(dev, "%s: Invalid parameters\n", __func__);