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