xref: /linux/sound/soc/codecs/hdmi-codec.c (revision 59e6295fac26b8e85c1ea859cdd89fa1e47519d7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ALSA SoC codec for HDMI encoder drivers
4  * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
5  * Author: Jyri Sarha <jsarha@ti.com>
6  */
7 #include <linux/cleanup.h>
8 #include <linux/module.h>
9 #include <linux/string.h>
10 #include <sound/core.h>
11 #include <sound/jack.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/tlv.h>
16 #include <sound/pcm_drm_eld.h>
17 #include <sound/hdmi-codec.h>
18 #include <sound/pcm_iec958.h>
19 
20 #include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
21 #include <drm/drm_eld.h>
22 
23 #define HDMI_CODEC_CHMAP_IDX_UNKNOWN  -1
24 
25 /*
26  * CEA speaker placement for HDMI 1.4:
27  *
28  *  FL  FLC   FC   FRC   FR   FRW
29  *
30  *                                  LFE
31  *
32  *  RL  RLC   RC   RRC   RR
33  *
34  *  Speaker placement has to be extended to support HDMI 2.0
35  */
36 enum hdmi_codec_cea_spk_placement {
37 	FL  = BIT(0),	/* Front Left           */
38 	FC  = BIT(1),	/* Front Center         */
39 	FR  = BIT(2),	/* Front Right          */
40 	FLC = BIT(3),	/* Front Left Center    */
41 	FRC = BIT(4),	/* Front Right Center   */
42 	RL  = BIT(5),	/* Rear Left            */
43 	RC  = BIT(6),	/* Rear Center          */
44 	RR  = BIT(7),	/* Rear Right           */
45 	RLC = BIT(8),	/* Rear Left Center     */
46 	RRC = BIT(9),	/* Rear Right Center    */
47 	LFE = BIT(10),	/* Low Frequency Effect */
48 };
49 
50 /*
51  * cea Speaker allocation structure
52  */
53 struct hdmi_codec_cea_spk_alloc {
54 	const int ca_id;
55 	unsigned int n_ch;
56 	unsigned long mask;
57 };
58 
59 /* Channel maps  stereo HDMI */
60 static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
61 	{ .channels = 2,
62 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
63 	{ }
64 };
65 
66 /* Channel maps for multi-channel playbacks, up to 8 n_ch */
67 static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
68 	{ .channels = 2, /* CA_ID 0x00 */
69 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
70 	{ .channels = 4, /* CA_ID 0x01 */
71 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
72 		   SNDRV_CHMAP_NA } },
73 	{ .channels = 4, /* CA_ID 0x02 */
74 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
75 		   SNDRV_CHMAP_FC } },
76 	{ .channels = 4, /* CA_ID 0x03 */
77 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
78 		   SNDRV_CHMAP_FC } },
79 	{ .channels = 6, /* CA_ID 0x04 */
80 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
81 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
82 	{ .channels = 6, /* CA_ID 0x05 */
83 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
84 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
85 	{ .channels = 6, /* CA_ID 0x06 */
86 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
87 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
88 	{ .channels = 6, /* CA_ID 0x07 */
89 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
90 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
91 	{ .channels = 6, /* CA_ID 0x08 */
92 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
93 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
94 	{ .channels = 6, /* CA_ID 0x09 */
95 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
96 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
97 	{ .channels = 6, /* CA_ID 0x0A */
98 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
99 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
100 	{ .channels = 6, /* CA_ID 0x0B */
101 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
102 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
103 	{ .channels = 8, /* CA_ID 0x0C */
104 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
105 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
106 		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
107 	{ .channels = 8, /* CA_ID 0x0D */
108 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
109 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
110 		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
111 	{ .channels = 8, /* CA_ID 0x0E */
112 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
113 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
114 		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
115 	{ .channels = 8, /* CA_ID 0x0F */
116 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
117 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
118 		   SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
119 	{ .channels = 8, /* CA_ID 0x10 */
120 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
121 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
122 		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
123 	{ .channels = 8, /* CA_ID 0x11 */
124 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
125 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
126 		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
127 	{ .channels = 8, /* CA_ID 0x12 */
128 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
129 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
130 		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
131 	{ .channels = 8, /* CA_ID 0x13 */
132 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
133 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
134 		   SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
135 	{ .channels = 8, /* CA_ID 0x14 */
136 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
137 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
138 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
139 	{ .channels = 8, /* CA_ID 0x15 */
140 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
141 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
142 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
143 	{ .channels = 8, /* CA_ID 0x16 */
144 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
145 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
146 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
147 	{ .channels = 8, /* CA_ID 0x17 */
148 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
149 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
150 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
151 	{ .channels = 8, /* CA_ID 0x18 */
152 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
153 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
154 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
155 	{ .channels = 8, /* CA_ID 0x19 */
156 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
157 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
158 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
159 	{ .channels = 8, /* CA_ID 0x1A */
160 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
161 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
162 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
163 	{ .channels = 8, /* CA_ID 0x1B */
164 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
165 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
166 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
167 	{ .channels = 8, /* CA_ID 0x1C */
168 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
169 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
170 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
171 	{ .channels = 8, /* CA_ID 0x1D */
172 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
173 		   SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
174 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
175 	{ .channels = 8, /* CA_ID 0x1E */
176 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
177 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
178 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
179 	{ .channels = 8, /* CA_ID 0x1F */
180 	  .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
181 		   SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
182 		   SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
183 	{ }
184 };
185 
186 /*
187  * hdmi_codec_channel_alloc: speaker configuration available for CEA
188  *
189  * This is an ordered list where ca_id must exist in hdmi_codec_8ch_chmaps
190  * The preceding ones have better chances to be selected by
191  * hdmi_codec_get_ch_alloc_table_idx().
192  */
193 static const struct hdmi_codec_cea_spk_alloc hdmi_codec_channel_alloc[] = {
194 	{ .ca_id = 0x00, .n_ch = 2,
195 	  .mask = FL | FR },
196 	{ .ca_id = 0x03, .n_ch = 4,
197 	  .mask = FL | FR | LFE | FC },
198 	{ .ca_id = 0x02, .n_ch = 4,
199 	  .mask = FL | FR | FC },
200 	{ .ca_id = 0x01, .n_ch = 4,
201 	  .mask = FL | FR | LFE },
202 	{ .ca_id = 0x0b, .n_ch = 6,
203 	  .mask = FL | FR | LFE | FC | RL | RR },
204 	{ .ca_id = 0x0a, .n_ch = 6,
205 	  .mask = FL | FR | FC | RL | RR },
206 	{ .ca_id = 0x09, .n_ch = 6,
207 	  .mask = FL | FR | LFE | RL | RR },
208 	{ .ca_id = 0x08, .n_ch = 6,
209 	  .mask = FL | FR | RL | RR },
210 	{ .ca_id = 0x07, .n_ch = 6,
211 	  .mask = FL | FR | LFE | FC | RC },
212 	{ .ca_id = 0x06, .n_ch = 6,
213 	  .mask = FL | FR | FC | RC },
214 	{ .ca_id = 0x05, .n_ch = 6,
215 	  .mask = FL | FR | LFE | RC },
216 	{ .ca_id = 0x04, .n_ch = 6,
217 	  .mask = FL | FR | RC },
218 	{ .ca_id = 0x13, .n_ch = 8,
219 	  .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
220 	{ .ca_id = 0x1f, .n_ch = 8,
221 	  .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
222 	{ .ca_id = 0x12, .n_ch = 8,
223 	  .mask = FL | FR | FC | RL | RR | RLC | RRC },
224 	{ .ca_id = 0x1e, .n_ch = 8,
225 	  .mask = FL | FR | FC | RL | RR | FLC | FRC },
226 	{ .ca_id = 0x11, .n_ch = 8,
227 	  .mask = FL | FR | LFE | RL | RR | RLC | RRC },
228 	{ .ca_id = 0x1d, .n_ch = 8,
229 	  .mask = FL | FR | LFE | RL | RR | FLC | FRC },
230 	{ .ca_id = 0x10, .n_ch = 8,
231 	  .mask = FL | FR | RL | RR | RLC | RRC },
232 	{ .ca_id = 0x1c, .n_ch = 8,
233 	  .mask = FL | FR | RL | RR | FLC | FRC },
234 	{ .ca_id = 0x0f, .n_ch = 8,
235 	  .mask = FL | FR | LFE | FC | RL | RR | RC },
236 	{ .ca_id = 0x1b, .n_ch = 8,
237 	  .mask = FL | FR | LFE | RC | FC | FLC | FRC },
238 	{ .ca_id = 0x0e, .n_ch = 8,
239 	  .mask = FL | FR | FC | RL | RR | RC },
240 	{ .ca_id = 0x1a, .n_ch = 8,
241 	  .mask = FL | FR | RC | FC | FLC | FRC },
242 	{ .ca_id = 0x0d, .n_ch = 8,
243 	  .mask = FL | FR | LFE | RL | RR | RC },
244 	{ .ca_id = 0x19, .n_ch = 8,
245 	  .mask = FL | FR | LFE | RC | FLC | FRC },
246 	{ .ca_id = 0x0c, .n_ch = 8,
247 	  .mask = FL | FR | RC | RL | RR },
248 	{ .ca_id = 0x18, .n_ch = 8,
249 	  .mask = FL | FR | RC | FLC | FRC },
250 	{ .ca_id = 0x17, .n_ch = 8,
251 	  .mask = FL | FR | LFE | FC | FLC | FRC },
252 	{ .ca_id = 0x16, .n_ch = 8,
253 	  .mask = FL | FR | FC | FLC | FRC },
254 	{ .ca_id = 0x15, .n_ch = 8,
255 	  .mask = FL | FR | LFE | FLC | FRC },
256 	{ .ca_id = 0x14, .n_ch = 8,
257 	  .mask = FL | FR | FLC | FRC },
258 	{ .ca_id = 0x0b, .n_ch = 8,
259 	  .mask = FL | FR | LFE | FC | RL | RR },
260 	{ .ca_id = 0x0a, .n_ch = 8,
261 	  .mask = FL | FR | FC | RL | RR },
262 	{ .ca_id = 0x09, .n_ch = 8,
263 	  .mask = FL | FR | LFE | RL | RR },
264 	{ .ca_id = 0x08, .n_ch = 8,
265 	  .mask = FL | FR | RL | RR },
266 	{ .ca_id = 0x07, .n_ch = 8,
267 	  .mask = FL | FR | LFE | FC | RC },
268 	{ .ca_id = 0x06, .n_ch = 8,
269 	  .mask = FL | FR | FC | RC },
270 	{ .ca_id = 0x05, .n_ch = 8,
271 	  .mask = FL | FR | LFE | RC },
272 	{ .ca_id = 0x04, .n_ch = 8,
273 	  .mask = FL | FR | RC },
274 	{ .ca_id = 0x03, .n_ch = 8,
275 	  .mask = FL | FR | LFE | FC },
276 	{ .ca_id = 0x02, .n_ch = 8,
277 	  .mask = FL | FR | FC },
278 	{ .ca_id = 0x01, .n_ch = 8,
279 	  .mask = FL | FR | LFE },
280 };
281 
282 struct hdmi_codec_priv {
283 	struct hdmi_codec_pdata hcd;
284 	uint8_t eld[MAX_ELD_BYTES];
285 	struct snd_parsed_hdmi_eld eld_parsed;
286 	struct snd_pcm_chmap *chmap_info;
287 	unsigned int chmap_idx;
288 	struct mutex lock;
289 	bool busy;
290 	struct snd_soc_jack *jack;
291 	unsigned int jack_status;
292 	u8 iec_status[AES_IEC958_STATUS_SIZE];
293 	struct snd_info_entry *proc_entry;
294 };
295 
296 static const struct snd_soc_dapm_widget hdmi_widgets[] = {
297 	SND_SOC_DAPM_OUTPUT("TX"),
298 	SND_SOC_DAPM_OUTPUT("RX"),
299 };
300 
301 enum {
302 	DAI_ID_I2S = 0,
303 	DAI_ID_SPDIF,
304 };
305 
306 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
307 			     struct snd_ctl_elem_info *uinfo)
308 {
309 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
310 	uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
311 
312 	return 0;
313 }
314 
315 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
316 			    struct snd_ctl_elem_value *ucontrol)
317 {
318 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
319 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
320 
321 	memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
322 
323 	return 0;
324 }
325 
326 static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
327 {
328 	int i;
329 	static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
330 		[0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
331 		[4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
332 	};
333 	unsigned long spk_mask = 0;
334 
335 	for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
336 		if (spk_alloc & (1 << i))
337 			spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
338 	}
339 
340 	return spk_mask;
341 }
342 
343 static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
344 {
345 	u8 spk_alloc;
346 	unsigned long spk_mask;
347 
348 	spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
349 	spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
350 
351 	/* Detect if only stereo supported, else return 8 channels mappings */
352 	if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
353 		hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
354 	else
355 		hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
356 }
357 
358 static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
359 					     unsigned char channels)
360 {
361 	int i;
362 	u8 spk_alloc;
363 	unsigned long spk_mask;
364 	const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
365 
366 	spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
367 	spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
368 
369 	for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
370 		/* If spk_alloc == 0, HDMI is unplugged return stereo config*/
371 		if (!spk_alloc && cap->ca_id == 0)
372 			return i;
373 		if (cap->n_ch != channels)
374 			continue;
375 		if (!(cap->mask == (spk_mask & cap->mask)))
376 			continue;
377 		return i;
378 	}
379 
380 	return -EINVAL;
381 }
382 static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
383 			      struct snd_ctl_elem_value *ucontrol)
384 {
385 	unsigned const char *map;
386 	unsigned int i;
387 	struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
388 	struct hdmi_codec_priv *hcp = info->private_data;
389 
390 	if (hcp->chmap_idx != HDMI_CODEC_CHMAP_IDX_UNKNOWN)
391 		map = info->chmap[hcp->chmap_idx].map;
392 
393 	for (i = 0; i < info->max_channels; i++) {
394 		if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
395 			ucontrol->value.integer.value[i] = 0;
396 		else
397 			ucontrol->value.integer.value[i] = map[i];
398 	}
399 
400 	return 0;
401 }
402 
403 static int hdmi_codec_iec958_info(struct snd_kcontrol *kcontrol,
404 				  struct snd_ctl_elem_info *uinfo)
405 {
406 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
407 	uinfo->count = 1;
408 	return 0;
409 }
410 
411 static int hdmi_codec_iec958_default_get(struct snd_kcontrol *kcontrol,
412 					 struct snd_ctl_elem_value *ucontrol)
413 {
414 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
415 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
416 
417 	memcpy(ucontrol->value.iec958.status, hcp->iec_status,
418 	       sizeof(hcp->iec_status));
419 
420 	return 0;
421 }
422 
423 static int hdmi_codec_iec958_default_put(struct snd_kcontrol *kcontrol,
424 					 struct snd_ctl_elem_value *ucontrol)
425 {
426 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
427 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
428 
429 	memcpy(hcp->iec_status, ucontrol->value.iec958.status,
430 	       sizeof(hcp->iec_status));
431 
432 	return 0;
433 }
434 
435 static int hdmi_codec_iec958_mask_get(struct snd_kcontrol *kcontrol,
436 				      struct snd_ctl_elem_value *ucontrol)
437 {
438 	memset(ucontrol->value.iec958.status, 0xff,
439 	       sizeof_field(struct hdmi_codec_priv, iec_status));
440 
441 	return 0;
442 }
443 
444 static int hdmi_codec_startup(struct snd_pcm_substream *substream,
445 			      struct snd_soc_dai *dai)
446 {
447 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
448 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
449 	bool has_capture = !hcp->hcd.no_i2s_capture;
450 	bool has_playback = !hcp->hcd.no_i2s_playback;
451 	int ret = 0;
452 
453 	if (!((has_playback && tx) || (has_capture && !tx)))
454 		return 0;
455 
456 	guard(mutex)(&hcp->lock);
457 	if (hcp->busy) {
458 		dev_err(dai->dev, "Only one simultaneous stream supported!\n");
459 		return -EINVAL;
460 	}
461 
462 	if (hcp->hcd.ops->audio_startup) {
463 		ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
464 		if (ret)
465 			return ret;
466 	}
467 
468 	if (tx && hcp->hcd.ops->get_eld) {
469 		ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
470 					    hcp->eld, sizeof(hcp->eld));
471 		if (ret)
472 			return ret;
473 
474 		snd_parse_eld(dai->dev, &hcp->eld_parsed,
475 			      hcp->eld, sizeof(hcp->eld));
476 
477 		ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
478 		if (ret)
479 			return ret;
480 
481 		/* Select chmap supported */
482 		hdmi_codec_eld_chmap(hcp);
483 	}
484 
485 	hcp->busy = true;
486 
487 	return ret;
488 }
489 
490 static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
491 				struct snd_soc_dai *dai)
492 {
493 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
494 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
495 	bool has_capture = !hcp->hcd.no_i2s_capture;
496 	bool has_playback = !hcp->hcd.no_i2s_playback;
497 
498 	if (!((has_playback && tx) || (has_capture && !tx)))
499 		return;
500 
501 	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
502 	hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
503 
504 	guard(mutex)(&hcp->lock);
505 	hcp->busy = false;
506 }
507 
508 static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
509 					unsigned int sample_width,
510 					unsigned int sample_rate,
511 					unsigned int channels,
512 					struct hdmi_codec_params *hp)
513 {
514 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
515 	int idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
516 	u8 ca_id = 0;
517 	bool pcm_audio = !(hcp->iec_status[0] & IEC958_AES0_NONAUDIO);
518 
519 	if (pcm_audio) {
520 		/* Select a channel allocation that matches with ELD and pcm channels */
521 		idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
522 
523 		if (idx < 0) {
524 			dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
525 				idx);
526 			hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
527 			return idx;
528 		}
529 
530 		ca_id = hdmi_codec_channel_alloc[idx].ca_id;
531 	}
532 
533 	memset(hp, 0, sizeof(*hp));
534 
535 	hdmi_audio_infoframe_init(&hp->cea);
536 
537 	if (pcm_audio)
538 		hp->cea.channels = channels;
539 	else
540 		hp->cea.channels = 0;
541 
542 	hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
543 	hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
544 	hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
545 	hp->cea.channel_allocation = ca_id;
546 
547 	hp->sample_width = sample_width;
548 	hp->sample_rate = sample_rate;
549 	hp->channels = channels;
550 
551 	if (pcm_audio)
552 		hcp->chmap_idx = ca_id;
553 	else
554 		hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
555 
556 	return 0;
557 }
558 
559 static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
560 				struct snd_pcm_hw_params *params,
561 				struct snd_soc_dai *dai)
562 {
563 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
564 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
565 	struct hdmi_codec_params hp = {
566 		.iec = {
567 			.status = { 0 },
568 			.subcode = { 0 },
569 			.pad = 0,
570 			.dig_subframe = { 0 },
571 		}
572 	};
573 	int ret;
574 
575 	if (!hcp->hcd.ops->hw_params)
576 		return 0;
577 
578 	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
579 		params_width(params), params_rate(params),
580 		params_channels(params));
581 
582 	ret = hdmi_codec_fill_codec_params(dai,
583 					   params_width(params),
584 					   params_rate(params),
585 					   params_channels(params),
586 					   &hp);
587 	if (ret < 0)
588 		return ret;
589 
590 	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
591 	ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
592 						     sizeof(hp.iec.status));
593 	if (ret < 0) {
594 		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
595 			ret);
596 		return ret;
597 	}
598 
599 	cf->bit_fmt = params_format(params);
600 	return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
601 				       cf, &hp);
602 }
603 
604 static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
605 			      struct snd_soc_dai *dai)
606 {
607 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
608 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
609 	struct snd_pcm_runtime *runtime = substream->runtime;
610 	unsigned int channels = runtime->channels;
611 	unsigned int width = snd_pcm_format_width(runtime->format);
612 	unsigned int rate = runtime->rate;
613 	struct hdmi_codec_params hp;
614 	int ret;
615 
616 	if (!hcp->hcd.ops->prepare)
617 		return 0;
618 
619 	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
620 		width, rate, channels);
621 
622 	ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
623 	if (ret < 0)
624 		return ret;
625 
626 	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
627 	ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
628 					   sizeof(hp.iec.status));
629 	if (ret < 0) {
630 		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
631 			ret);
632 		return ret;
633 	}
634 
635 	cf->bit_fmt = runtime->format;
636 	return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
637 				     cf, &hp);
638 }
639 
640 static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
641 				  unsigned int fmt)
642 {
643 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
644 
645 	/* Reset daifmt */
646 	memset(cf, 0, sizeof(*cf));
647 
648 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
649 	case SND_SOC_DAIFMT_CBP_CFP:
650 		cf->bit_clk_provider = 1;
651 		cf->frame_clk_provider = 1;
652 		break;
653 	case SND_SOC_DAIFMT_CBC_CFP:
654 		cf->frame_clk_provider = 1;
655 		break;
656 	case SND_SOC_DAIFMT_CBP_CFC:
657 		cf->bit_clk_provider = 1;
658 		break;
659 	case SND_SOC_DAIFMT_CBC_CFC:
660 		break;
661 	default:
662 		return -EINVAL;
663 	}
664 
665 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
666 	case SND_SOC_DAIFMT_NB_NF:
667 		break;
668 	case SND_SOC_DAIFMT_NB_IF:
669 		cf->frame_clk_inv = 1;
670 		break;
671 	case SND_SOC_DAIFMT_IB_NF:
672 		cf->bit_clk_inv = 1;
673 		break;
674 	case SND_SOC_DAIFMT_IB_IF:
675 		cf->frame_clk_inv = 1;
676 		cf->bit_clk_inv = 1;
677 		break;
678 	}
679 
680 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
681 	case SND_SOC_DAIFMT_I2S:
682 		cf->fmt = HDMI_I2S;
683 		break;
684 	case SND_SOC_DAIFMT_DSP_A:
685 		cf->fmt = HDMI_DSP_A;
686 		break;
687 	case SND_SOC_DAIFMT_DSP_B:
688 		cf->fmt = HDMI_DSP_B;
689 		break;
690 	case SND_SOC_DAIFMT_RIGHT_J:
691 		cf->fmt = HDMI_RIGHT_J;
692 		break;
693 	case SND_SOC_DAIFMT_LEFT_J:
694 		cf->fmt = HDMI_LEFT_J;
695 		break;
696 	case SND_SOC_DAIFMT_AC97:
697 		cf->fmt = HDMI_AC97;
698 		break;
699 	default:
700 		dev_err(dai->dev, "Invalid DAI interface format\n");
701 		return -EINVAL;
702 	}
703 
704 	return 0;
705 }
706 
707 static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
708 {
709 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
710 
711 	/*
712 	 * ignore if direction was CAPTURE
713 	 * and it had .no_capture_mute flag
714 	 * see
715 	 *	snd_soc_dai_digital_mute()
716 	 */
717 	if (hcp->hcd.ops->mute_stream &&
718 	    (direction == SNDRV_PCM_STREAM_PLAYBACK ||
719 	     !hcp->hcd.no_capture_mute))
720 		return hcp->hcd.ops->mute_stream(dai->dev->parent,
721 						 hcp->hcd.data,
722 						 mute, direction);
723 
724 	return -ENOTSUPP;
725 }
726 
727 /*
728  * This driver can select all SND_SOC_DAIFMT_CBx_CFx,
729  * but need to be selected from Sound Card, not be auto selected.
730  * Because it might be used from other driver.
731  * For example,
732  *	${LINUX}/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
733  */
734 static const u64 hdmi_codec_formats =
735 	SND_SOC_POSSIBLE_DAIFMT_NB_NF	|
736 	SND_SOC_POSSIBLE_DAIFMT_NB_IF	|
737 	SND_SOC_POSSIBLE_DAIFMT_IB_NF	|
738 	SND_SOC_POSSIBLE_DAIFMT_IB_IF	|
739 	SND_SOC_POSSIBLE_DAIFMT_I2S	|
740 	SND_SOC_POSSIBLE_DAIFMT_DSP_A	|
741 	SND_SOC_POSSIBLE_DAIFMT_DSP_B	|
742 	SND_SOC_POSSIBLE_DAIFMT_RIGHT_J	|
743 	SND_SOC_POSSIBLE_DAIFMT_LEFT_J	|
744 	SND_SOC_POSSIBLE_DAIFMT_AC97;
745 
746 #define HDMI_RATES	(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
747 			 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
748 			 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
749 			 SNDRV_PCM_RATE_192000)
750 
751 #define SPDIF_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
752 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
753 
754 /*
755  * This list is only for formats allowed on the I2S bus. So there is
756  * some formats listed that are not supported by HDMI interface. For
757  * instance allowing the 32-bit formats enables 24-precision with CPU
758  * DAIs that do not support 24-bit formats. If the extra formats cause
759  * problems, we should add the video side driver an option to disable
760  * them.
761  */
762 #define I2S_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
763 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
764 			 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
765 
766 static struct snd_kcontrol_new hdmi_codec_controls[] = {
767 	{
768 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
769 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
770 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
771 		.info = hdmi_codec_iec958_info,
772 		.get = hdmi_codec_iec958_mask_get,
773 	},
774 	{
775 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
776 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
777 		.info = hdmi_codec_iec958_info,
778 		.get = hdmi_codec_iec958_default_get,
779 		.put = hdmi_codec_iec958_default_put,
780 	},
781 	{
782 		.access	= (SNDRV_CTL_ELEM_ACCESS_READ |
783 			   SNDRV_CTL_ELEM_ACCESS_VOLATILE),
784 		.iface	= SNDRV_CTL_ELEM_IFACE_PCM,
785 		.name	= "ELD",
786 		.info	= hdmi_eld_ctl_info,
787 		.get	= hdmi_eld_ctl_get,
788 	},
789 };
790 
791 static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
792 			      struct snd_soc_dai *dai)
793 {
794 	struct snd_soc_dai_driver *drv = dai->driver;
795 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
796 	unsigned int i;
797 	int ret;
798 
799 	ret =  snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
800 				      NULL, drv->playback.channels_max, 0,
801 				      &hcp->chmap_info);
802 	if (ret < 0)
803 		return ret;
804 
805 	/* override handlers */
806 	hcp->chmap_info->private_data = hcp;
807 	hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
808 
809 	/* default chmap supported is stereo */
810 	hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
811 	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
812 
813 	for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
814 		struct snd_kcontrol *kctl;
815 
816 		/* add ELD ctl with the device number corresponding to the PCM stream */
817 		kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
818 		if (!kctl)
819 			return -ENOMEM;
820 
821 		kctl->id.device = rtd->pcm->device;
822 		ret = snd_ctl_add(rtd->card->snd_card, kctl);
823 		if (ret < 0)
824 			return ret;
825 	}
826 
827 	return 0;
828 }
829 
830 #ifdef CONFIG_SND_PROC_FS
831 static void print_eld_info(struct snd_info_entry *entry,
832 			   struct snd_info_buffer *buffer)
833 {
834 	struct hdmi_codec_priv *hcp = entry->private_data;
835 
836 	snd_print_eld_info(&hcp->eld_parsed, buffer);
837 }
838 
839 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
840 			     struct snd_soc_dai *dai)
841 {
842 	struct snd_soc_component *component = dai->component;
843 	struct snd_soc_card *card = component->card;
844 	struct snd_soc_dai *d;
845 	struct snd_soc_pcm_runtime *rtd;
846 	struct snd_info_entry *entry;
847 	char name[32];
848 	int err, i, id = 0;
849 
850 	/*
851 	 * To avoid duplicate proc entry, find its rtd and use rtd->id
852 	 * instead of dai->id
853 	 */
854 	for_each_card_rtds(card, rtd) {
855 		for_each_rtd_dais(rtd, i, d)
856 			if (d == dai) {
857 				id = rtd->id;
858 				goto found;
859 			}
860 	}
861 found:
862 	snprintf(name, sizeof(name), "eld#%d", id);
863 	err = snd_card_proc_new(card->snd_card, name, &entry);
864 	if (err < 0)
865 		return err;
866 
867 	snd_info_set_text_ops(entry, hcp, print_eld_info);
868 	hcp->proc_entry = entry;
869 
870 	return 0;
871 }
872 
873 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
874 {
875 	snd_info_free_entry(hcp->proc_entry);
876 	hcp->proc_entry = NULL;
877 }
878 #else
879 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
880 			     struct snd_soc_dai *dai)
881 {
882 	return 0;
883 }
884 
885 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
886 {
887 }
888 #endif
889 
890 static int hdmi_dai_probe(struct snd_soc_dai *dai)
891 {
892 	struct hdmi_codec_priv *hcp =
893 		snd_soc_component_get_drvdata(dai->component);
894 	struct snd_soc_dapm_context *dapm;
895 	struct hdmi_codec_daifmt *daifmt;
896 	struct snd_soc_dapm_route route[] = {
897 		{
898 			.sink = "TX",
899 			.source = dai->driver->playback.stream_name,
900 		},
901 		{
902 			.sink = dai->driver->capture.stream_name,
903 			.source = "RX",
904 		},
905 	};
906 	int ret, i;
907 
908 	dapm = snd_soc_component_to_dapm(dai->component);
909 
910 	/* One of the directions might be omitted for unidirectional DAIs */
911 	for (i = 0; i < ARRAY_SIZE(route); i++) {
912 		if (!route[i].source || !route[i].sink)
913 			continue;
914 
915 		ret = snd_soc_dapm_add_routes(dapm, &route[i], 1);
916 		if (ret)
917 			return ret;
918 	}
919 
920 	daifmt = devm_kzalloc(dai->dev, sizeof(*daifmt), GFP_KERNEL);
921 	if (!daifmt)
922 		return -ENOMEM;
923 
924 	snd_soc_dai_dma_data_set_playback(dai, daifmt);
925 
926 	return hdmi_dai_proc_new(hcp, dai);
927 }
928 
929 static int hdmi_dai_remove(struct snd_soc_dai *dai)
930 {
931 	struct hdmi_codec_priv *hcp =
932 		snd_soc_component_get_drvdata(dai->component);
933 
934 	hdmi_dai_proc_free(hcp);
935 	return 0;
936 }
937 
938 static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
939 				   unsigned int jack_status)
940 {
941 	if (jack_status != hcp->jack_status) {
942 		if (hcp->jack)
943 			snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_AVOUT);
944 		hcp->jack_status = jack_status;
945 	}
946 }
947 
948 static void plugged_cb(struct device *dev, bool plugged)
949 {
950 	struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
951 	int ret;
952 
953 	if (plugged) {
954 		if (hcp->hcd.ops->get_eld) {
955 			hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
956 					    hcp->eld, sizeof(hcp->eld));
957 			ret = snd_parse_eld(dev, &hcp->eld_parsed,
958 					    hcp->eld, sizeof(hcp->eld));
959 			if (ret < 0)
960 				dev_dbg(dev, "Failed to parse ELD: %d\n", ret);
961 			else
962 				snd_show_eld(dev, &hcp->eld_parsed);
963 		}
964 		hdmi_codec_jack_report(hcp, SND_JACK_AVOUT);
965 	} else {
966 		hdmi_codec_jack_report(hcp, 0);
967 		memset(hcp->eld, 0, sizeof(hcp->eld));
968 	}
969 }
970 
971 static int hdmi_codec_set_jack(struct snd_soc_component *component,
972 			       struct snd_soc_jack *jack,
973 			       void *data)
974 {
975 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
976 
977 	if (hcp->hcd.ops->hook_plugged_cb) {
978 		hcp->jack = jack;
979 
980 		/*
981 		 * Report the initial jack status which may have been provided
982 		 * by the parent hdmi driver while the hpd hook was registered.
983 		 */
984 		snd_soc_jack_report(jack, hcp->jack_status, SND_JACK_AVOUT);
985 
986 		return 0;
987 	}
988 
989 	return -ENOTSUPP;
990 }
991 
992 static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
993 {
994 	struct hdmi_codec_daifmt *cf;
995 	int ret;
996 
997 	ret = hdmi_dai_probe(dai);
998 	if (ret)
999 		return ret;
1000 
1001 	cf = snd_soc_dai_dma_data_get_playback(dai);
1002 	cf->fmt = HDMI_SPDIF;
1003 
1004 	return 0;
1005 }
1006 
1007 static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
1008 	.probe				= hdmi_dai_probe,
1009 	.remove				= hdmi_dai_remove,
1010 	.startup			= hdmi_codec_startup,
1011 	.shutdown			= hdmi_codec_shutdown,
1012 	.hw_params			= hdmi_codec_hw_params,
1013 	.prepare			= hdmi_codec_prepare,
1014 	.set_fmt			= hdmi_codec_i2s_set_fmt,
1015 	.mute_stream			= hdmi_codec_mute,
1016 	.pcm_new			= hdmi_codec_pcm_new,
1017 	.auto_selectable_formats	= &hdmi_codec_formats,
1018 	.num_auto_selectable_formats	= 1,
1019 };
1020 
1021 static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
1022 	.probe		= hdmi_dai_spdif_probe,
1023 	.startup	= hdmi_codec_startup,
1024 	.shutdown	= hdmi_codec_shutdown,
1025 	.hw_params	= hdmi_codec_hw_params,
1026 	.prepare	= hdmi_codec_prepare,
1027 	.mute_stream	= hdmi_codec_mute,
1028 	.pcm_new	= hdmi_codec_pcm_new,
1029 };
1030 
1031 static const struct snd_soc_dai_driver hdmi_i2s_dai = {
1032 	.name = "i2s-hifi",
1033 	.id = DAI_ID_I2S,
1034 	.playback = {
1035 		.stream_name = "I2S Playback",
1036 		.channels_min = 2,
1037 		.channels_max = 8,
1038 		.rates = HDMI_RATES,
1039 		.formats = I2S_FORMATS,
1040 		.sig_bits = 24,
1041 	},
1042 	.capture = {
1043 		.stream_name = "Capture",
1044 		.channels_min = 2,
1045 		.channels_max = 8,
1046 		.rates = HDMI_RATES,
1047 		.formats = I2S_FORMATS,
1048 		.sig_bits = 24,
1049 	},
1050 	.ops = &hdmi_codec_i2s_dai_ops,
1051 };
1052 
1053 static const struct snd_soc_dai_driver hdmi_spdif_dai = {
1054 	.name = "spdif-hifi",
1055 	.id = DAI_ID_SPDIF,
1056 	.playback = {
1057 		.stream_name = "SPDIF Playback",
1058 		.channels_min = 2,
1059 		.channels_max = 2,
1060 		.rates = HDMI_RATES,
1061 		.formats = SPDIF_FORMATS,
1062 	},
1063 	.capture = {
1064 		.stream_name = "Capture",
1065 		.channels_min = 2,
1066 		.channels_max = 2,
1067 		.rates = HDMI_RATES,
1068 		.formats = SPDIF_FORMATS,
1069 	},
1070 	.ops = &hdmi_codec_spdif_dai_ops,
1071 };
1072 
1073 static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
1074 				 struct device_node *endpoint)
1075 {
1076 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1077 	int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
1078 
1079 	if (hcp->hcd.ops->get_dai_id)
1080 		ret = hcp->hcd.ops->get_dai_id(component, endpoint, hcp->hcd.data);
1081 
1082 	return ret;
1083 }
1084 
1085 static int hdmi_probe(struct snd_soc_component *component)
1086 {
1087 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1088 	int ret = 0;
1089 
1090 	if (hcp->hcd.ops->hook_plugged_cb) {
1091 		ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1092 						    hcp->hcd.data,
1093 						    plugged_cb,
1094 						    component->dev);
1095 	}
1096 
1097 	return ret;
1098 }
1099 
1100 static void hdmi_remove(struct snd_soc_component *component)
1101 {
1102 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1103 
1104 	if (hcp->hcd.ops->hook_plugged_cb)
1105 		hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1106 					      hcp->hcd.data, NULL, NULL);
1107 }
1108 
1109 static const struct snd_soc_component_driver hdmi_driver = {
1110 	.probe			= hdmi_probe,
1111 	.remove			= hdmi_remove,
1112 	.dapm_widgets		= hdmi_widgets,
1113 	.num_dapm_widgets	= ARRAY_SIZE(hdmi_widgets),
1114 	.of_xlate_dai_id	= hdmi_of_xlate_dai_id,
1115 	.idle_bias_on		= 1,
1116 	.use_pmdown_time	= 1,
1117 	.endianness		= 1,
1118 	.set_jack		= hdmi_codec_set_jack,
1119 };
1120 
1121 static int hdmi_codec_probe(struct platform_device *pdev)
1122 {
1123 	struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
1124 	struct snd_soc_dai_driver *daidrv;
1125 	struct device *dev = &pdev->dev;
1126 	struct hdmi_codec_priv *hcp;
1127 	int dai_count, i = 0;
1128 	int ret;
1129 
1130 	if (!hcd) {
1131 		dev_err(dev, "%s: No platform data\n", __func__);
1132 		return -EINVAL;
1133 	}
1134 
1135 	dai_count = hcd->i2s + hcd->spdif;
1136 	if (dai_count < 1 || !hcd->ops ||
1137 	    (!hcd->ops->hw_params && !hcd->ops->prepare) ||
1138 	    !hcd->ops->audio_shutdown) {
1139 		dev_err(dev, "%s: Invalid parameters\n", __func__);
1140 		return -EINVAL;
1141 	}
1142 
1143 	hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
1144 	if (!hcp)
1145 		return -ENOMEM;
1146 
1147 	hcp->hcd = *hcd;
1148 	mutex_init(&hcp->lock);
1149 
1150 	ret = snd_pcm_create_iec958_consumer_default(hcp->iec_status,
1151 						     sizeof(hcp->iec_status));
1152 	if (ret < 0)
1153 		return ret;
1154 
1155 	daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
1156 	if (!daidrv)
1157 		return -ENOMEM;
1158 
1159 	if (hcd->i2s) {
1160 		daidrv[i] = hdmi_i2s_dai;
1161 		daidrv[i].playback.channels_max = hcd->max_i2s_channels;
1162 		if (hcd->i2s_formats) {
1163 			daidrv[i].playback.formats = hcd->i2s_formats;
1164 			daidrv[i].capture.formats = hcd->i2s_formats;
1165 		}
1166 		if (hcd->no_i2s_playback)
1167 			memset(&daidrv[i].playback, 0,
1168 			       sizeof(daidrv[i].playback));
1169 		if (hcd->no_i2s_capture)
1170 			memset(&daidrv[i].capture, 0,
1171 			       sizeof(daidrv[i].capture));
1172 		i++;
1173 	}
1174 
1175 	if (hcd->spdif) {
1176 		daidrv[i] = hdmi_spdif_dai;
1177 		if (hcd->no_spdif_playback)
1178 			memset(&daidrv[i].playback, 0,
1179 			       sizeof(daidrv[i].playback));
1180 		if (hcd->no_spdif_capture)
1181 			memset(&daidrv[i].capture, 0,
1182 			       sizeof(daidrv[i].capture));
1183 	}
1184 
1185 	dev_set_drvdata(dev, hcp);
1186 
1187 	ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
1188 					      dai_count);
1189 	if (ret) {
1190 		dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
1191 			__func__, ret);
1192 		return ret;
1193 	}
1194 	return 0;
1195 }
1196 
1197 static struct platform_driver hdmi_codec_driver = {
1198 	.driver = {
1199 		.name = HDMI_CODEC_DRV_NAME,
1200 	},
1201 	.probe = hdmi_codec_probe,
1202 };
1203 
1204 module_platform_driver(hdmi_codec_driver);
1205 
1206 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1207 MODULE_DESCRIPTION("HDMI Audio Codec Driver");
1208 MODULE_LICENSE("GPL");
1209 MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);
1210