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