bcm27xx: add kernel 5.10 support
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.10 / 950-0635-ASoC-hdmi-codec-Add-a-prepare-hook.patch
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
5
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.
10
11 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
12 ---
13 include/sound/hdmi-codec.h | 12 +++-
14 sound/soc/codecs/hdmi-codec.c | 112 ++++++++++++++++++++++++++--------
15 2 files changed, 99 insertions(+), 25 deletions(-)
16
17 --- a/include/sound/hdmi-codec.h
18 +++ b/include/sound/hdmi-codec.h
19 @@ -65,13 +65,23 @@ struct hdmi_codec_ops {
20
21 /*
22 * Configures HDMI-encoder for audio stream.
23 - * Mandatory
24 + * Having either prepare or hw_params is mandatory.
25 */
26 int (*hw_params)(struct device *dev, void *data,
27 struct hdmi_codec_daifmt *fmt,
28 struct hdmi_codec_params *hparms);
29
30 /*
31 + * Configures HDMI-encoder for audio stream. Can be called
32 + * multiple times for each setup.
33 + *
34 + * Having either prepare or hw_params is mandatory.
35 + */
36 + int (*prepare)(struct device *dev, void *data,
37 + struct hdmi_codec_daifmt *fmt,
38 + struct hdmi_codec_params *hparms);
39 +
40 + /*
41 * Shuts down the audio stream.
42 * Mandatory
43 */
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);
48 }
49
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)
55 +{
56 + struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
57 + int idx;
58 +
59 + /* Select a channel allocation that matches with ELD and pcm channels */
60 + idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
61 + if (idx < 0) {
62 + dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
63 + idx);
64 + hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
65 + return idx;
66 + }
67 +
68 + memset(hp, 0, sizeof(*hp));
69 +
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;
76 +
77 + hp->sample_width = sample_width;
78 + hp->sample_rate = sample_rate;
79 + hp->channels = channels;
80 +
81 + hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
82 +
83 + return 0;
84 +}
85 +
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 },
91 }
92 };
93 - int ret, idx;
94 + int ret;
95 +
96 + if (!hcp->hcd.ops->hw_params)
97 + return 0;
98
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));
102
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),
108 + &hp);
109 + if (ret < 0)
110 + return ret;
111 +
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));
115 if (ret < 0) {
116 @@ -509,32 +556,47 @@ static int hdmi_codec_hw_params(struct s
117 return ret;
118 }
119
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;
125 -
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);
128 - if (idx < 0) {
129 - dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
130 - idx);
131 - hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
132 - return idx;
133 - }
134 - hp.cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
135 - hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
136 -
137 - hp.sample_width = params_width(params);
138 - hp.sample_rate = params_rate(params);
139 - hp.channels = params_channels(params);
140 -
141 cf->bit_fmt = params_format(params);
142 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
143 cf, &hp);
144 }
145
146 +static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
147 + struct snd_soc_dai *dai)
148 +{
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;
156 + int ret;
157 +
158 + if (!hcp->hcd.ops->prepare)
159 + return 0;
160 +
161 + dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
162 + width, rate, channels);
163 +
164 + ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
165 + if (ret < 0)
166 + return ret;
167 +
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));
171 + if (ret < 0) {
172 + dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
173 + ret);
174 + return ret;
175 + }
176 +
177 + cf->bit_fmt = runtime->format;
178 + return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
179 + cf, &hp);
180 +}
181 +
182 static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
183 unsigned int fmt)
184 {
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,
192 };
193 @@ -889,7 +952,8 @@ static int hdmi_codec_probe(struct platf
194 }
195
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__);
202 return -EINVAL;