xref: /linux/sound/soc/codecs/hdmi-codec.c (revision c17ae8c26eac16ad244daef44044d714f68a2ddc)
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 	if (!memcmp(hcp->iec_status, ucontrol->value.iec958.status,
430 		    sizeof(hcp->iec_status)))
431 		return 0;
432 
433 	memcpy(hcp->iec_status, ucontrol->value.iec958.status,
434 	       sizeof(hcp->iec_status));
435 
436 	return 1;
437 }
438 
439 static int hdmi_codec_iec958_mask_get(struct snd_kcontrol *kcontrol,
440 				      struct snd_ctl_elem_value *ucontrol)
441 {
442 	memset(ucontrol->value.iec958.status, 0xff,
443 	       sizeof_field(struct hdmi_codec_priv, iec_status));
444 
445 	return 0;
446 }
447 
448 static int hdmi_codec_startup(struct snd_pcm_substream *substream,
449 			      struct snd_soc_dai *dai)
450 {
451 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
452 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
453 	bool has_capture = !hcp->hcd.no_i2s_capture;
454 	bool has_playback = !hcp->hcd.no_i2s_playback;
455 	int ret = 0;
456 
457 	if (!((has_playback && tx) || (has_capture && !tx)))
458 		return 0;
459 
460 	guard(mutex)(&hcp->lock);
461 	if (hcp->busy) {
462 		dev_err(dai->dev, "Only one simultaneous stream supported!\n");
463 		return -EINVAL;
464 	}
465 
466 	if (hcp->hcd.ops->audio_startup) {
467 		ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
468 		if (ret)
469 			return ret;
470 	}
471 
472 	if (tx && hcp->hcd.ops->get_eld) {
473 		ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
474 					    hcp->eld, sizeof(hcp->eld));
475 		if (ret)
476 			return ret;
477 
478 		snd_parse_eld(dai->dev, &hcp->eld_parsed,
479 			      hcp->eld, sizeof(hcp->eld));
480 
481 		ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
482 		if (ret)
483 			return ret;
484 
485 		/* Select chmap supported */
486 		hdmi_codec_eld_chmap(hcp);
487 	}
488 
489 	hcp->busy = true;
490 
491 	return ret;
492 }
493 
494 static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
495 				struct snd_soc_dai *dai)
496 {
497 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
498 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
499 	bool has_capture = !hcp->hcd.no_i2s_capture;
500 	bool has_playback = !hcp->hcd.no_i2s_playback;
501 
502 	if (!((has_playback && tx) || (has_capture && !tx)))
503 		return;
504 
505 	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
506 	hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
507 
508 	guard(mutex)(&hcp->lock);
509 	hcp->busy = false;
510 }
511 
512 static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
513 					unsigned int sample_width,
514 					unsigned int sample_rate,
515 					unsigned int channels,
516 					struct hdmi_codec_params *hp)
517 {
518 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
519 	int idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
520 	u8 ca_id = 0;
521 	bool pcm_audio = !(hcp->iec_status[0] & IEC958_AES0_NONAUDIO);
522 
523 	if (pcm_audio) {
524 		/* Select a channel allocation that matches with ELD and pcm channels */
525 		idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
526 
527 		if (idx < 0) {
528 			dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
529 				idx);
530 			hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
531 			return idx;
532 		}
533 
534 		ca_id = hdmi_codec_channel_alloc[idx].ca_id;
535 	}
536 
537 	memset(hp, 0, sizeof(*hp));
538 
539 	hdmi_audio_infoframe_init(&hp->cea);
540 
541 	if (pcm_audio)
542 		hp->cea.channels = channels;
543 	else
544 		hp->cea.channels = 0;
545 
546 	hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
547 	hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
548 	hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
549 	hp->cea.channel_allocation = ca_id;
550 
551 	hp->sample_width = sample_width;
552 	hp->sample_rate = sample_rate;
553 	hp->channels = channels;
554 
555 	if (pcm_audio)
556 		hcp->chmap_idx = ca_id;
557 	else
558 		hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
559 
560 	return 0;
561 }
562 
563 static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
564 				struct snd_pcm_hw_params *params,
565 				struct snd_soc_dai *dai)
566 {
567 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
568 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
569 	struct hdmi_codec_params hp = {
570 		.iec = {
571 			.status = { 0 },
572 			.subcode = { 0 },
573 			.pad = 0,
574 			.dig_subframe = { 0 },
575 		}
576 	};
577 	int ret;
578 
579 	if (!hcp->hcd.ops->hw_params)
580 		return 0;
581 
582 	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
583 		params_width(params), params_rate(params),
584 		params_channels(params));
585 
586 	ret = hdmi_codec_fill_codec_params(dai,
587 					   params_width(params),
588 					   params_rate(params),
589 					   params_channels(params),
590 					   &hp);
591 	if (ret < 0)
592 		return ret;
593 
594 	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
595 	ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
596 						     sizeof(hp.iec.status));
597 	if (ret < 0) {
598 		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
599 			ret);
600 		return ret;
601 	}
602 
603 	cf->bit_fmt = params_format(params);
604 	return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
605 				       cf, &hp);
606 }
607 
608 static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
609 			      struct snd_soc_dai *dai)
610 {
611 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
612 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
613 	struct snd_pcm_runtime *runtime = substream->runtime;
614 	unsigned int channels = runtime->channels;
615 	unsigned int width = snd_pcm_format_width(runtime->format);
616 	unsigned int rate = runtime->rate;
617 	struct hdmi_codec_params hp;
618 	int ret;
619 
620 	if (!hcp->hcd.ops->prepare)
621 		return 0;
622 
623 	dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
624 		width, rate, channels);
625 
626 	ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
627 	if (ret < 0)
628 		return ret;
629 
630 	memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
631 	ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
632 					   sizeof(hp.iec.status));
633 	if (ret < 0) {
634 		dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
635 			ret);
636 		return ret;
637 	}
638 
639 	cf->bit_fmt = runtime->format;
640 	return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
641 				     cf, &hp);
642 }
643 
644 static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
645 				  unsigned int fmt)
646 {
647 	struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
648 
649 	/* Reset daifmt */
650 	memset(cf, 0, sizeof(*cf));
651 
652 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
653 	case SND_SOC_DAIFMT_CBP_CFP:
654 		cf->bit_clk_provider = 1;
655 		cf->frame_clk_provider = 1;
656 		break;
657 	case SND_SOC_DAIFMT_CBC_CFP:
658 		cf->frame_clk_provider = 1;
659 		break;
660 	case SND_SOC_DAIFMT_CBP_CFC:
661 		cf->bit_clk_provider = 1;
662 		break;
663 	case SND_SOC_DAIFMT_CBC_CFC:
664 		break;
665 	default:
666 		return -EINVAL;
667 	}
668 
669 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
670 	case SND_SOC_DAIFMT_NB_NF:
671 		break;
672 	case SND_SOC_DAIFMT_NB_IF:
673 		cf->frame_clk_inv = 1;
674 		break;
675 	case SND_SOC_DAIFMT_IB_NF:
676 		cf->bit_clk_inv = 1;
677 		break;
678 	case SND_SOC_DAIFMT_IB_IF:
679 		cf->frame_clk_inv = 1;
680 		cf->bit_clk_inv = 1;
681 		break;
682 	}
683 
684 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
685 	case SND_SOC_DAIFMT_I2S:
686 		cf->fmt = HDMI_I2S;
687 		break;
688 	case SND_SOC_DAIFMT_DSP_A:
689 		cf->fmt = HDMI_DSP_A;
690 		break;
691 	case SND_SOC_DAIFMT_DSP_B:
692 		cf->fmt = HDMI_DSP_B;
693 		break;
694 	case SND_SOC_DAIFMT_RIGHT_J:
695 		cf->fmt = HDMI_RIGHT_J;
696 		break;
697 	case SND_SOC_DAIFMT_LEFT_J:
698 		cf->fmt = HDMI_LEFT_J;
699 		break;
700 	case SND_SOC_DAIFMT_AC97:
701 		cf->fmt = HDMI_AC97;
702 		break;
703 	default:
704 		dev_err(dai->dev, "Invalid DAI interface format\n");
705 		return -EINVAL;
706 	}
707 
708 	return 0;
709 }
710 
711 static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
712 {
713 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
714 
715 	/*
716 	 * ignore if direction was CAPTURE
717 	 * and it had .no_capture_mute flag
718 	 * see
719 	 *	snd_soc_dai_digital_mute()
720 	 */
721 	if (hcp->hcd.ops->mute_stream &&
722 	    (direction == SNDRV_PCM_STREAM_PLAYBACK ||
723 	     !hcp->hcd.no_capture_mute))
724 		return hcp->hcd.ops->mute_stream(dai->dev->parent,
725 						 hcp->hcd.data,
726 						 mute, direction);
727 
728 	return -ENOTSUPP;
729 }
730 
731 /*
732  * This driver can select all SND_SOC_DAIFMT_CBx_CFx,
733  * but need to be selected from Sound Card, not be auto selected.
734  * Because it might be used from other driver.
735  * For example,
736  *	${LINUX}/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
737  */
738 static const u64 hdmi_codec_formats =
739 	SND_SOC_POSSIBLE_DAIFMT_NB_NF	|
740 	SND_SOC_POSSIBLE_DAIFMT_NB_IF	|
741 	SND_SOC_POSSIBLE_DAIFMT_IB_NF	|
742 	SND_SOC_POSSIBLE_DAIFMT_IB_IF	|
743 	SND_SOC_POSSIBLE_DAIFMT_I2S	|
744 	SND_SOC_POSSIBLE_DAIFMT_DSP_A	|
745 	SND_SOC_POSSIBLE_DAIFMT_DSP_B	|
746 	SND_SOC_POSSIBLE_DAIFMT_RIGHT_J	|
747 	SND_SOC_POSSIBLE_DAIFMT_LEFT_J	|
748 	SND_SOC_POSSIBLE_DAIFMT_AC97;
749 
750 #define HDMI_RATES	(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
751 			 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
752 			 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
753 			 SNDRV_PCM_RATE_192000)
754 
755 #define SPDIF_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
756 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
757 
758 /*
759  * This list is only for formats allowed on the I2S bus. So there is
760  * some formats listed that are not supported by HDMI interface. For
761  * instance allowing the 32-bit formats enables 24-precision with CPU
762  * DAIs that do not support 24-bit formats. If the extra formats cause
763  * problems, we should add the video side driver an option to disable
764  * them.
765  */
766 #define I2S_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
767 			 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
768 			 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
769 
770 static struct snd_kcontrol_new hdmi_codec_controls[] = {
771 	{
772 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
773 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
774 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
775 		.info = hdmi_codec_iec958_info,
776 		.get = hdmi_codec_iec958_mask_get,
777 	},
778 	{
779 		.iface = SNDRV_CTL_ELEM_IFACE_PCM,
780 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
781 		.info = hdmi_codec_iec958_info,
782 		.get = hdmi_codec_iec958_default_get,
783 		.put = hdmi_codec_iec958_default_put,
784 	},
785 	{
786 		.access	= (SNDRV_CTL_ELEM_ACCESS_READ |
787 			   SNDRV_CTL_ELEM_ACCESS_VOLATILE),
788 		.iface	= SNDRV_CTL_ELEM_IFACE_PCM,
789 		.name	= "ELD",
790 		.info	= hdmi_eld_ctl_info,
791 		.get	= hdmi_eld_ctl_get,
792 	},
793 };
794 
795 static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
796 			      struct snd_soc_dai *dai)
797 {
798 	struct snd_soc_dai_driver *drv = dai->driver;
799 	struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
800 	unsigned int i;
801 	int ret;
802 
803 	ret =  snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
804 				      NULL, drv->playback.channels_max, 0,
805 				      &hcp->chmap_info);
806 	if (ret < 0)
807 		return ret;
808 
809 	/* override handlers */
810 	hcp->chmap_info->private_data = hcp;
811 	hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
812 
813 	/* default chmap supported is stereo */
814 	hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
815 	hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
816 
817 	for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
818 		struct snd_kcontrol *kctl;
819 
820 		/* add ELD ctl with the device number corresponding to the PCM stream */
821 		kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
822 		if (!kctl)
823 			return -ENOMEM;
824 
825 		kctl->id.device = rtd->pcm->device;
826 		ret = snd_ctl_add(rtd->card->snd_card, kctl);
827 		if (ret < 0)
828 			return ret;
829 	}
830 
831 	return 0;
832 }
833 
834 #ifdef CONFIG_SND_PROC_FS
835 static void print_eld_info(struct snd_info_entry *entry,
836 			   struct snd_info_buffer *buffer)
837 {
838 	struct hdmi_codec_priv *hcp = entry->private_data;
839 
840 	snd_print_eld_info(&hcp->eld_parsed, buffer);
841 }
842 
843 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
844 			     struct snd_soc_dai *dai)
845 {
846 	struct snd_soc_component *component = dai->component;
847 	struct snd_soc_card *card = component->card;
848 	struct snd_soc_dai *d;
849 	struct snd_soc_pcm_runtime *rtd;
850 	struct snd_info_entry *entry;
851 	char name[32];
852 	int err, i, id = 0;
853 
854 	/*
855 	 * To avoid duplicate proc entry, find its rtd and use rtd->id
856 	 * instead of dai->id
857 	 */
858 	for_each_card_rtds(card, rtd) {
859 		for_each_rtd_dais(rtd, i, d)
860 			if (d == dai) {
861 				id = rtd->id;
862 				goto found;
863 			}
864 	}
865 found:
866 	snprintf(name, sizeof(name), "eld#%d", id);
867 	err = snd_card_proc_new(card->snd_card, name, &entry);
868 	if (err < 0)
869 		return err;
870 
871 	snd_info_set_text_ops(entry, hcp, print_eld_info);
872 	hcp->proc_entry = entry;
873 
874 	return 0;
875 }
876 
877 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
878 {
879 	snd_info_free_entry(hcp->proc_entry);
880 	hcp->proc_entry = NULL;
881 }
882 #else
883 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
884 			     struct snd_soc_dai *dai)
885 {
886 	return 0;
887 }
888 
889 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
890 {
891 }
892 #endif
893 
894 static int hdmi_dai_probe(struct snd_soc_dai *dai)
895 {
896 	struct hdmi_codec_priv *hcp =
897 		snd_soc_component_get_drvdata(dai->component);
898 	struct snd_soc_dapm_context *dapm;
899 	struct hdmi_codec_daifmt *daifmt;
900 	struct snd_soc_dapm_route route[] = {
901 		{
902 			.sink = "TX",
903 			.source = dai->driver->playback.stream_name,
904 		},
905 		{
906 			.sink = dai->driver->capture.stream_name,
907 			.source = "RX",
908 		},
909 	};
910 	int ret, i;
911 
912 	dapm = snd_soc_component_to_dapm(dai->component);
913 
914 	/* One of the directions might be omitted for unidirectional DAIs */
915 	for (i = 0; i < ARRAY_SIZE(route); i++) {
916 		if (!route[i].source || !route[i].sink)
917 			continue;
918 
919 		ret = snd_soc_dapm_add_routes(dapm, &route[i], 1);
920 		if (ret)
921 			return ret;
922 	}
923 
924 	daifmt = devm_kzalloc(dai->dev, sizeof(*daifmt), GFP_KERNEL);
925 	if (!daifmt)
926 		return -ENOMEM;
927 
928 	snd_soc_dai_dma_data_set_playback(dai, daifmt);
929 
930 	return hdmi_dai_proc_new(hcp, dai);
931 }
932 
933 static int hdmi_dai_remove(struct snd_soc_dai *dai)
934 {
935 	struct hdmi_codec_priv *hcp =
936 		snd_soc_component_get_drvdata(dai->component);
937 
938 	hdmi_dai_proc_free(hcp);
939 	return 0;
940 }
941 
942 static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
943 				   unsigned int jack_status)
944 {
945 	if (jack_status != hcp->jack_status) {
946 		if (hcp->jack)
947 			snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_AVOUT);
948 		hcp->jack_status = jack_status;
949 	}
950 }
951 
952 static void plugged_cb(struct device *dev, bool plugged)
953 {
954 	struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
955 	int ret;
956 
957 	if (plugged) {
958 		if (hcp->hcd.ops->get_eld) {
959 			hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
960 					    hcp->eld, sizeof(hcp->eld));
961 			ret = snd_parse_eld(dev, &hcp->eld_parsed,
962 					    hcp->eld, sizeof(hcp->eld));
963 			if (ret < 0)
964 				dev_dbg(dev, "Failed to parse ELD: %d\n", ret);
965 			else
966 				snd_show_eld(dev, &hcp->eld_parsed);
967 		}
968 		hdmi_codec_jack_report(hcp, SND_JACK_AVOUT);
969 	} else {
970 		hdmi_codec_jack_report(hcp, 0);
971 		memset(hcp->eld, 0, sizeof(hcp->eld));
972 	}
973 }
974 
975 static int hdmi_codec_set_jack(struct snd_soc_component *component,
976 			       struct snd_soc_jack *jack,
977 			       void *data)
978 {
979 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
980 
981 	if (hcp->hcd.ops->hook_plugged_cb) {
982 		hcp->jack = jack;
983 
984 		/*
985 		 * Report the initial jack status which may have been provided
986 		 * by the parent hdmi driver while the hpd hook was registered.
987 		 */
988 		snd_soc_jack_report(jack, hcp->jack_status, SND_JACK_AVOUT);
989 
990 		return 0;
991 	}
992 
993 	return -ENOTSUPP;
994 }
995 
996 static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
997 {
998 	struct hdmi_codec_daifmt *cf;
999 	int ret;
1000 
1001 	ret = hdmi_dai_probe(dai);
1002 	if (ret)
1003 		return ret;
1004 
1005 	cf = snd_soc_dai_dma_data_get_playback(dai);
1006 	cf->fmt = HDMI_SPDIF;
1007 
1008 	return 0;
1009 }
1010 
1011 static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
1012 	.probe				= hdmi_dai_probe,
1013 	.remove				= hdmi_dai_remove,
1014 	.startup			= hdmi_codec_startup,
1015 	.shutdown			= hdmi_codec_shutdown,
1016 	.hw_params			= hdmi_codec_hw_params,
1017 	.prepare			= hdmi_codec_prepare,
1018 	.set_fmt			= hdmi_codec_i2s_set_fmt,
1019 	.mute_stream			= hdmi_codec_mute,
1020 	.pcm_new			= hdmi_codec_pcm_new,
1021 	.auto_selectable_formats	= &hdmi_codec_formats,
1022 	.num_auto_selectable_formats	= 1,
1023 };
1024 
1025 static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
1026 	.probe		= hdmi_dai_spdif_probe,
1027 	.startup	= hdmi_codec_startup,
1028 	.shutdown	= hdmi_codec_shutdown,
1029 	.hw_params	= hdmi_codec_hw_params,
1030 	.prepare	= hdmi_codec_prepare,
1031 	.mute_stream	= hdmi_codec_mute,
1032 	.pcm_new	= hdmi_codec_pcm_new,
1033 };
1034 
1035 static const struct snd_soc_dai_driver hdmi_i2s_dai = {
1036 	.name = "i2s-hifi",
1037 	.id = DAI_ID_I2S,
1038 	.playback = {
1039 		.stream_name = "I2S Playback",
1040 		.channels_min = 2,
1041 		.channels_max = 8,
1042 		.rates = HDMI_RATES,
1043 		.formats = I2S_FORMATS,
1044 		.sig_bits = 24,
1045 	},
1046 	.capture = {
1047 		.stream_name = "Capture",
1048 		.channels_min = 2,
1049 		.channels_max = 8,
1050 		.rates = HDMI_RATES,
1051 		.formats = I2S_FORMATS,
1052 		.sig_bits = 24,
1053 	},
1054 	.ops = &hdmi_codec_i2s_dai_ops,
1055 };
1056 
1057 static const struct snd_soc_dai_driver hdmi_spdif_dai = {
1058 	.name = "spdif-hifi",
1059 	.id = DAI_ID_SPDIF,
1060 	.playback = {
1061 		.stream_name = "SPDIF Playback",
1062 		.channels_min = 2,
1063 		.channels_max = 2,
1064 		.rates = HDMI_RATES,
1065 		.formats = SPDIF_FORMATS,
1066 	},
1067 	.capture = {
1068 		.stream_name = "Capture",
1069 		.channels_min = 2,
1070 		.channels_max = 2,
1071 		.rates = HDMI_RATES,
1072 		.formats = SPDIF_FORMATS,
1073 	},
1074 	.ops = &hdmi_codec_spdif_dai_ops,
1075 };
1076 
1077 static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
1078 				 struct device_node *endpoint)
1079 {
1080 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1081 	int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
1082 
1083 	if (hcp->hcd.ops->get_dai_id)
1084 		ret = hcp->hcd.ops->get_dai_id(component, endpoint, hcp->hcd.data);
1085 
1086 	return ret;
1087 }
1088 
1089 static int hdmi_probe(struct snd_soc_component *component)
1090 {
1091 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1092 	int ret = 0;
1093 
1094 	if (hcp->hcd.ops->hook_plugged_cb) {
1095 		ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1096 						    hcp->hcd.data,
1097 						    plugged_cb,
1098 						    component->dev);
1099 	}
1100 
1101 	return ret;
1102 }
1103 
1104 static void hdmi_remove(struct snd_soc_component *component)
1105 {
1106 	struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1107 
1108 	if (hcp->hcd.ops->hook_plugged_cb)
1109 		hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1110 					      hcp->hcd.data, NULL, NULL);
1111 }
1112 
1113 static const struct snd_soc_component_driver hdmi_driver = {
1114 	.probe			= hdmi_probe,
1115 	.remove			= hdmi_remove,
1116 	.dapm_widgets		= hdmi_widgets,
1117 	.num_dapm_widgets	= ARRAY_SIZE(hdmi_widgets),
1118 	.of_xlate_dai_id	= hdmi_of_xlate_dai_id,
1119 	.idle_bias_on		= 1,
1120 	.use_pmdown_time	= 1,
1121 	.endianness		= 1,
1122 	.set_jack		= hdmi_codec_set_jack,
1123 };
1124 
1125 static int hdmi_codec_probe(struct platform_device *pdev)
1126 {
1127 	struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
1128 	struct snd_soc_dai_driver *daidrv;
1129 	struct device *dev = &pdev->dev;
1130 	struct hdmi_codec_priv *hcp;
1131 	int dai_count, i = 0;
1132 	int ret;
1133 
1134 	if (!hcd) {
1135 		dev_err(dev, "%s: No platform data\n", __func__);
1136 		return -EINVAL;
1137 	}
1138 
1139 	dai_count = hcd->i2s + hcd->spdif;
1140 	if (dai_count < 1 || !hcd->ops ||
1141 	    (!hcd->ops->hw_params && !hcd->ops->prepare) ||
1142 	    !hcd->ops->audio_shutdown) {
1143 		dev_err(dev, "%s: Invalid parameters\n", __func__);
1144 		return -EINVAL;
1145 	}
1146 
1147 	hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
1148 	if (!hcp)
1149 		return -ENOMEM;
1150 
1151 	hcp->hcd = *hcd;
1152 	mutex_init(&hcp->lock);
1153 
1154 	ret = snd_pcm_create_iec958_consumer_default(hcp->iec_status,
1155 						     sizeof(hcp->iec_status));
1156 	if (ret < 0)
1157 		return ret;
1158 
1159 	daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
1160 	if (!daidrv)
1161 		return -ENOMEM;
1162 
1163 	if (hcd->i2s) {
1164 		daidrv[i] = hdmi_i2s_dai;
1165 		daidrv[i].playback.channels_max = hcd->max_i2s_channels;
1166 		if (hcd->i2s_formats) {
1167 			daidrv[i].playback.formats = hcd->i2s_formats;
1168 			daidrv[i].capture.formats = hcd->i2s_formats;
1169 		}
1170 		if (hcd->no_i2s_playback)
1171 			memset(&daidrv[i].playback, 0,
1172 			       sizeof(daidrv[i].playback));
1173 		if (hcd->no_i2s_capture)
1174 			memset(&daidrv[i].capture, 0,
1175 			       sizeof(daidrv[i].capture));
1176 		i++;
1177 	}
1178 
1179 	if (hcd->spdif) {
1180 		daidrv[i] = hdmi_spdif_dai;
1181 		if (hcd->no_spdif_playback)
1182 			memset(&daidrv[i].playback, 0,
1183 			       sizeof(daidrv[i].playback));
1184 		if (hcd->no_spdif_capture)
1185 			memset(&daidrv[i].capture, 0,
1186 			       sizeof(daidrv[i].capture));
1187 	}
1188 
1189 	dev_set_drvdata(dev, hcp);
1190 
1191 	ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
1192 					      dai_count);
1193 	if (ret) {
1194 		dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
1195 			__func__, ret);
1196 		return ret;
1197 	}
1198 	return 0;
1199 }
1200 
1201 static struct platform_driver hdmi_codec_driver = {
1202 	.driver = {
1203 		.name = HDMI_CODEC_DRV_NAME,
1204 	},
1205 	.probe = hdmi_codec_probe,
1206 };
1207 
1208 module_platform_driver(hdmi_codec_driver);
1209 
1210 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1211 MODULE_DESCRIPTION("HDMI Audio Codec Driver");
1212 MODULE_LICENSE("GPL");
1213 MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);
1214