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