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