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 where ca_id must exist in hdmi_codec_8ch_chmaps
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 { .ca_id = 0x03, .n_ch = 4,
196 .mask = FL | FR | LFE | FC },
197 { .ca_id = 0x02, .n_ch = 4,
198 .mask = FL | FR | FC },
199 { .ca_id = 0x01, .n_ch = 4,
200 .mask = FL | FR | LFE },
201 { .ca_id = 0x0b, .n_ch = 6,
202 .mask = FL | FR | LFE | FC | RL | RR },
203 { .ca_id = 0x0a, .n_ch = 6,
204 .mask = FL | FR | FC | RL | RR },
205 { .ca_id = 0x09, .n_ch = 6,
206 .mask = FL | FR | LFE | RL | RR },
207 { .ca_id = 0x08, .n_ch = 6,
208 .mask = FL | FR | RL | RR },
209 { .ca_id = 0x07, .n_ch = 6,
210 .mask = FL | FR | LFE | FC | RC },
211 { .ca_id = 0x06, .n_ch = 6,
212 .mask = FL | FR | FC | RC },
213 { .ca_id = 0x05, .n_ch = 6,
214 .mask = FL | FR | LFE | RC },
215 { .ca_id = 0x04, .n_ch = 6,
216 .mask = FL | FR | RC },
217 { .ca_id = 0x13, .n_ch = 8,
218 .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
219 { .ca_id = 0x1f, .n_ch = 8,
220 .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
221 { .ca_id = 0x12, .n_ch = 8,
222 .mask = FL | FR | FC | RL | RR | RLC | RRC },
223 { .ca_id = 0x1e, .n_ch = 8,
224 .mask = FL | FR | FC | RL | RR | FLC | FRC },
225 { .ca_id = 0x11, .n_ch = 8,
226 .mask = FL | FR | LFE | RL | RR | RLC | RRC },
227 { .ca_id = 0x1d, .n_ch = 8,
228 .mask = FL | FR | LFE | RL | RR | FLC | FRC },
229 { .ca_id = 0x10, .n_ch = 8,
230 .mask = FL | FR | RL | RR | RLC | RRC },
231 { .ca_id = 0x1c, .n_ch = 8,
232 .mask = FL | FR | RL | RR | FLC | FRC },
233 { .ca_id = 0x0f, .n_ch = 8,
234 .mask = FL | FR | LFE | FC | RL | RR | RC },
235 { .ca_id = 0x1b, .n_ch = 8,
236 .mask = FL | FR | LFE | RC | FC | FLC | FRC },
237 { .ca_id = 0x0e, .n_ch = 8,
238 .mask = FL | FR | FC | RL | RR | RC },
239 { .ca_id = 0x1a, .n_ch = 8,
240 .mask = FL | FR | RC | FC | FLC | FRC },
241 { .ca_id = 0x0d, .n_ch = 8,
242 .mask = FL | FR | LFE | RL | RR | RC },
243 { .ca_id = 0x19, .n_ch = 8,
244 .mask = FL | FR | LFE | RC | FLC | FRC },
245 { .ca_id = 0x0c, .n_ch = 8,
246 .mask = FL | FR | RC | RL | RR },
247 { .ca_id = 0x18, .n_ch = 8,
248 .mask = FL | FR | RC | FLC | FRC },
249 { .ca_id = 0x17, .n_ch = 8,
250 .mask = FL | FR | LFE | FC | FLC | FRC },
251 { .ca_id = 0x16, .n_ch = 8,
252 .mask = FL | FR | FC | FLC | FRC },
253 { .ca_id = 0x15, .n_ch = 8,
254 .mask = FL | FR | LFE | FLC | FRC },
255 { .ca_id = 0x14, .n_ch = 8,
256 .mask = FL | FR | FLC | FRC },
257 { .ca_id = 0x0b, .n_ch = 8,
258 .mask = FL | FR | LFE | FC | RL | RR },
259 { .ca_id = 0x0a, .n_ch = 8,
260 .mask = FL | FR | FC | RL | RR },
261 { .ca_id = 0x09, .n_ch = 8,
262 .mask = FL | FR | LFE | RL | RR },
263 { .ca_id = 0x08, .n_ch = 8,
264 .mask = FL | FR | RL | RR },
265 { .ca_id = 0x07, .n_ch = 8,
266 .mask = FL | FR | LFE | FC | RC },
267 { .ca_id = 0x06, .n_ch = 8,
268 .mask = FL | FR | FC | RC },
269 { .ca_id = 0x05, .n_ch = 8,
270 .mask = FL | FR | LFE | RC },
271 { .ca_id = 0x04, .n_ch = 8,
272 .mask = FL | FR | RC },
273 { .ca_id = 0x03, .n_ch = 8,
274 .mask = FL | FR | LFE | FC },
275 { .ca_id = 0x02, .n_ch = 8,
276 .mask = FL | FR | FC },
277 { .ca_id = 0x01, .n_ch = 8,
278 .mask = FL | FR | LFE },
279 };
280
281 struct hdmi_codec_priv {
282 struct hdmi_codec_pdata hcd;
283 uint8_t eld[MAX_ELD_BYTES];
284 struct snd_parsed_hdmi_eld eld_parsed;
285 struct snd_pcm_chmap *chmap_info;
286 unsigned int chmap_idx;
287 struct mutex lock;
288 bool busy;
289 struct snd_soc_jack *jack;
290 unsigned int jack_status;
291 u8 iec_status[AES_IEC958_STATUS_SIZE];
292 struct snd_info_entry *proc_entry;
293 };
294
295 static const struct snd_soc_dapm_widget hdmi_widgets[] = {
296 SND_SOC_DAPM_OUTPUT("TX"),
297 SND_SOC_DAPM_OUTPUT("RX"),
298 };
299
300 enum {
301 DAI_ID_I2S = 0,
302 DAI_ID_SPDIF,
303 };
304
hdmi_eld_ctl_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)305 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
306 struct snd_ctl_elem_info *uinfo)
307 {
308 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
309 uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
310
311 return 0;
312 }
313
hdmi_eld_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)314 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
315 struct snd_ctl_elem_value *ucontrol)
316 {
317 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
318 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
319
320 memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
321
322 return 0;
323 }
324
hdmi_codec_spk_mask_from_alloc(int spk_alloc)325 static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
326 {
327 int i;
328 static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
329 [0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
330 [4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
331 };
332 unsigned long spk_mask = 0;
333
334 for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
335 if (spk_alloc & (1 << i))
336 spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
337 }
338
339 return spk_mask;
340 }
341
hdmi_codec_eld_chmap(struct hdmi_codec_priv * hcp)342 static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
343 {
344 u8 spk_alloc;
345 unsigned long spk_mask;
346
347 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
348 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
349
350 /* Detect if only stereo supported, else return 8 channels mappings */
351 if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
352 hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
353 else
354 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
355 }
356
hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv * hcp,unsigned char channels)357 static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
358 unsigned char channels)
359 {
360 int i;
361 u8 spk_alloc;
362 unsigned long spk_mask;
363 const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
364
365 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
366 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
367
368 for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
369 /* If spk_alloc == 0, HDMI is unplugged return stereo config*/
370 if (!spk_alloc && cap->ca_id == 0)
371 return i;
372 if (cap->n_ch != channels)
373 continue;
374 if (!(cap->mask == (spk_mask & cap->mask)))
375 continue;
376 return i;
377 }
378
379 return -EINVAL;
380 }
hdmi_codec_chmap_ctl_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)381 static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
382 struct snd_ctl_elem_value *ucontrol)
383 {
384 unsigned const char *map;
385 unsigned int i;
386 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
387 struct hdmi_codec_priv *hcp = info->private_data;
388
389 if (hcp->chmap_idx != HDMI_CODEC_CHMAP_IDX_UNKNOWN)
390 map = info->chmap[hcp->chmap_idx].map;
391
392 for (i = 0; i < info->max_channels; i++) {
393 if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
394 ucontrol->value.integer.value[i] = 0;
395 else
396 ucontrol->value.integer.value[i] = map[i];
397 }
398
399 return 0;
400 }
401
hdmi_codec_iec958_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)402 static int hdmi_codec_iec958_info(struct snd_kcontrol *kcontrol,
403 struct snd_ctl_elem_info *uinfo)
404 {
405 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
406 uinfo->count = 1;
407 return 0;
408 }
409
hdmi_codec_iec958_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)410 static int hdmi_codec_iec958_default_get(struct snd_kcontrol *kcontrol,
411 struct snd_ctl_elem_value *ucontrol)
412 {
413 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
414 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
415
416 memcpy(ucontrol->value.iec958.status, hcp->iec_status,
417 sizeof(hcp->iec_status));
418
419 return 0;
420 }
421
hdmi_codec_iec958_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)422 static int hdmi_codec_iec958_default_put(struct snd_kcontrol *kcontrol,
423 struct snd_ctl_elem_value *ucontrol)
424 {
425 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
426 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
427
428 memcpy(hcp->iec_status, ucontrol->value.iec958.status,
429 sizeof(hcp->iec_status));
430
431 return 0;
432 }
433
hdmi_codec_iec958_mask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)434 static int hdmi_codec_iec958_mask_get(struct snd_kcontrol *kcontrol,
435 struct snd_ctl_elem_value *ucontrol)
436 {
437 memset(ucontrol->value.iec958.status, 0xff,
438 sizeof_field(struct hdmi_codec_priv, iec_status));
439
440 return 0;
441 }
442
hdmi_codec_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)443 static int hdmi_codec_startup(struct snd_pcm_substream *substream,
444 struct snd_soc_dai *dai)
445 {
446 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
447 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
448 bool has_capture = !hcp->hcd.no_i2s_capture;
449 bool has_playback = !hcp->hcd.no_i2s_playback;
450 int ret = 0;
451
452 if (!((has_playback && tx) || (has_capture && !tx)))
453 return 0;
454
455 mutex_lock(&hcp->lock);
456 if (hcp->busy) {
457 dev_err(dai->dev, "Only one simultaneous stream supported!\n");
458 mutex_unlock(&hcp->lock);
459 return -EINVAL;
460 }
461
462 if (hcp->hcd.ops->audio_startup) {
463 ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
464 if (ret)
465 goto err;
466 }
467
468 if (tx && hcp->hcd.ops->get_eld) {
469 ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
470 hcp->eld, sizeof(hcp->eld));
471 if (ret)
472 goto err;
473
474 snd_parse_eld(dai->dev, &hcp->eld_parsed,
475 hcp->eld, sizeof(hcp->eld));
476
477 ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
478 if (ret)
479 goto err;
480
481 /* Select chmap supported */
482 hdmi_codec_eld_chmap(hcp);
483 }
484
485 hcp->busy = true;
486
487 err:
488 mutex_unlock(&hcp->lock);
489 return ret;
490 }
491
hdmi_codec_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)492 static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
493 struct snd_soc_dai *dai)
494 {
495 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
496 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
497 bool has_capture = !hcp->hcd.no_i2s_capture;
498 bool has_playback = !hcp->hcd.no_i2s_playback;
499
500 if (!((has_playback && tx) || (has_capture && !tx)))
501 return;
502
503 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
504 hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
505
506 mutex_lock(&hcp->lock);
507 hcp->busy = false;
508 mutex_unlock(&hcp->lock);
509 }
510
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)511 static int hdmi_codec_fill_codec_params(struct snd_soc_dai *dai,
512 unsigned int sample_width,
513 unsigned int sample_rate,
514 unsigned int channels,
515 struct hdmi_codec_params *hp)
516 {
517 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
518 int idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
519 u8 ca_id = 0;
520 bool pcm_audio = !(hcp->iec_status[0] & IEC958_AES0_NONAUDIO);
521
522 if (pcm_audio) {
523 /* Select a channel allocation that matches with ELD and pcm channels */
524 idx = hdmi_codec_get_ch_alloc_table_idx(hcp, channels);
525
526 if (idx < 0) {
527 dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
528 idx);
529 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
530 return idx;
531 }
532
533 ca_id = hdmi_codec_channel_alloc[idx].ca_id;
534 }
535
536 memset(hp, 0, sizeof(*hp));
537
538 hdmi_audio_infoframe_init(&hp->cea);
539
540 if (pcm_audio)
541 hp->cea.channels = channels;
542 else
543 hp->cea.channels = 0;
544
545 hp->cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
546 hp->cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
547 hp->cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
548 hp->cea.channel_allocation = ca_id;
549
550 hp->sample_width = sample_width;
551 hp->sample_rate = sample_rate;
552 hp->channels = channels;
553
554 if (pcm_audio)
555 hcp->chmap_idx = ca_id;
556 else
557 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
558
559 return 0;
560 }
561
hdmi_codec_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)562 static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
563 struct snd_pcm_hw_params *params,
564 struct snd_soc_dai *dai)
565 {
566 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
567 struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
568 struct hdmi_codec_params hp = {
569 .iec = {
570 .status = { 0 },
571 .subcode = { 0 },
572 .pad = 0,
573 .dig_subframe = { 0 },
574 }
575 };
576 int ret;
577
578 if (!hcp->hcd.ops->hw_params)
579 return 0;
580
581 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
582 params_width(params), params_rate(params),
583 params_channels(params));
584
585 ret = hdmi_codec_fill_codec_params(dai,
586 params_width(params),
587 params_rate(params),
588 params_channels(params),
589 &hp);
590 if (ret < 0)
591 return ret;
592
593 memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
594 ret = snd_pcm_fill_iec958_consumer_hw_params(params, hp.iec.status,
595 sizeof(hp.iec.status));
596 if (ret < 0) {
597 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
598 ret);
599 return ret;
600 }
601
602 cf->bit_fmt = params_format(params);
603 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
604 cf, &hp);
605 }
606
hdmi_codec_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)607 static int hdmi_codec_prepare(struct snd_pcm_substream *substream,
608 struct snd_soc_dai *dai)
609 {
610 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
611 struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
612 struct snd_pcm_runtime *runtime = substream->runtime;
613 unsigned int channels = runtime->channels;
614 unsigned int width = snd_pcm_format_width(runtime->format);
615 unsigned int rate = runtime->rate;
616 struct hdmi_codec_params hp;
617 int ret;
618
619 if (!hcp->hcd.ops->prepare)
620 return 0;
621
622 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
623 width, rate, channels);
624
625 ret = hdmi_codec_fill_codec_params(dai, width, rate, channels, &hp);
626 if (ret < 0)
627 return ret;
628
629 memcpy(hp.iec.status, hcp->iec_status, sizeof(hp.iec.status));
630 ret = snd_pcm_fill_iec958_consumer(runtime, hp.iec.status,
631 sizeof(hp.iec.status));
632 if (ret < 0) {
633 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
634 ret);
635 return ret;
636 }
637
638 cf->bit_fmt = runtime->format;
639 return hcp->hcd.ops->prepare(dai->dev->parent, hcp->hcd.data,
640 cf, &hp);
641 }
642
hdmi_codec_i2s_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)643 static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
644 unsigned int fmt)
645 {
646 struct hdmi_codec_daifmt *cf = snd_soc_dai_dma_data_get_playback(dai);
647
648 /* Reset daifmt */
649 memset(cf, 0, sizeof(*cf));
650
651 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
652 case SND_SOC_DAIFMT_CBP_CFP:
653 cf->bit_clk_provider = 1;
654 cf->frame_clk_provider = 1;
655 break;
656 case SND_SOC_DAIFMT_CBC_CFP:
657 cf->frame_clk_provider = 1;
658 break;
659 case SND_SOC_DAIFMT_CBP_CFC:
660 cf->bit_clk_provider = 1;
661 break;
662 case SND_SOC_DAIFMT_CBC_CFC:
663 break;
664 default:
665 return -EINVAL;
666 }
667
668 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
669 case SND_SOC_DAIFMT_NB_NF:
670 break;
671 case SND_SOC_DAIFMT_NB_IF:
672 cf->frame_clk_inv = 1;
673 break;
674 case SND_SOC_DAIFMT_IB_NF:
675 cf->bit_clk_inv = 1;
676 break;
677 case SND_SOC_DAIFMT_IB_IF:
678 cf->frame_clk_inv = 1;
679 cf->bit_clk_inv = 1;
680 break;
681 }
682
683 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
684 case SND_SOC_DAIFMT_I2S:
685 cf->fmt = HDMI_I2S;
686 break;
687 case SND_SOC_DAIFMT_DSP_A:
688 cf->fmt = HDMI_DSP_A;
689 break;
690 case SND_SOC_DAIFMT_DSP_B:
691 cf->fmt = HDMI_DSP_B;
692 break;
693 case SND_SOC_DAIFMT_RIGHT_J:
694 cf->fmt = HDMI_RIGHT_J;
695 break;
696 case SND_SOC_DAIFMT_LEFT_J:
697 cf->fmt = HDMI_LEFT_J;
698 break;
699 case SND_SOC_DAIFMT_AC97:
700 cf->fmt = HDMI_AC97;
701 break;
702 default:
703 dev_err(dai->dev, "Invalid DAI interface format\n");
704 return -EINVAL;
705 }
706
707 return 0;
708 }
709
hdmi_codec_mute(struct snd_soc_dai * dai,int mute,int direction)710 static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
711 {
712 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
713
714 /*
715 * ignore if direction was CAPTURE
716 * and it had .no_capture_mute flag
717 * see
718 * snd_soc_dai_digital_mute()
719 */
720 if (hcp->hcd.ops->mute_stream &&
721 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
722 !hcp->hcd.no_capture_mute))
723 return hcp->hcd.ops->mute_stream(dai->dev->parent,
724 hcp->hcd.data,
725 mute, direction);
726
727 return -ENOTSUPP;
728 }
729
730 /*
731 * This driver can select all SND_SOC_DAIFMT_CBx_CFx,
732 * but need to be selected from Sound Card, not be auto selected.
733 * Because it might be used from other driver.
734 * For example,
735 * ${LINUX}/drivers/gpu/drm/bridge/synopsys/dw-hdmi-i2s-audio.c
736 */
737 static const u64 hdmi_codec_formats =
738 SND_SOC_POSSIBLE_DAIFMT_NB_NF |
739 SND_SOC_POSSIBLE_DAIFMT_NB_IF |
740 SND_SOC_POSSIBLE_DAIFMT_IB_NF |
741 SND_SOC_POSSIBLE_DAIFMT_IB_IF |
742 SND_SOC_POSSIBLE_DAIFMT_I2S |
743 SND_SOC_POSSIBLE_DAIFMT_DSP_A |
744 SND_SOC_POSSIBLE_DAIFMT_DSP_B |
745 SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
746 SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
747 SND_SOC_POSSIBLE_DAIFMT_AC97;
748
749 #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
750 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
751 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
752 SNDRV_PCM_RATE_192000)
753
754 #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
755 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
756
757 /*
758 * This list is only for formats allowed on the I2S bus. So there is
759 * some formats listed that are not supported by HDMI interface. For
760 * instance allowing the 32-bit formats enables 24-precision with CPU
761 * DAIs that do not support 24-bit formats. If the extra formats cause
762 * problems, we should add the video side driver an option to disable
763 * them.
764 */
765 #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
766 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
767 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
768
769 static struct snd_kcontrol_new hdmi_codec_controls[] = {
770 {
771 .access = SNDRV_CTL_ELEM_ACCESS_READ,
772 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
773 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
774 .info = hdmi_codec_iec958_info,
775 .get = hdmi_codec_iec958_mask_get,
776 },
777 {
778 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
779 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
780 .info = hdmi_codec_iec958_info,
781 .get = hdmi_codec_iec958_default_get,
782 .put = hdmi_codec_iec958_default_put,
783 },
784 {
785 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
786 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
787 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
788 .name = "ELD",
789 .info = hdmi_eld_ctl_info,
790 .get = hdmi_eld_ctl_get,
791 },
792 };
793
hdmi_codec_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)794 static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
795 struct snd_soc_dai *dai)
796 {
797 struct snd_soc_dai_driver *drv = dai->driver;
798 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
799 unsigned int i;
800 int ret;
801
802 ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
803 NULL, drv->playback.channels_max, 0,
804 &hcp->chmap_info);
805 if (ret < 0)
806 return ret;
807
808 /* override handlers */
809 hcp->chmap_info->private_data = hcp;
810 hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
811
812 /* default chmap supported is stereo */
813 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
814 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
815
816 for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
817 struct snd_kcontrol *kctl;
818
819 /* add ELD ctl with the device number corresponding to the PCM stream */
820 kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
821 if (!kctl)
822 return -ENOMEM;
823
824 kctl->id.device = rtd->pcm->device;
825 ret = snd_ctl_add(rtd->card->snd_card, kctl);
826 if (ret < 0)
827 return ret;
828 }
829
830 return 0;
831 }
832
833 #ifdef CONFIG_SND_PROC_FS
print_eld_info(struct snd_info_entry * entry,struct snd_info_buffer * buffer)834 static void print_eld_info(struct snd_info_entry *entry,
835 struct snd_info_buffer *buffer)
836 {
837 struct hdmi_codec_priv *hcp = entry->private_data;
838
839 snd_print_eld_info(&hcp->eld_parsed, buffer);
840 }
841
hdmi_dai_proc_new(struct hdmi_codec_priv * hcp,struct snd_soc_dai * dai)842 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
843 struct snd_soc_dai *dai)
844 {
845 struct snd_info_entry *entry;
846 char name[32];
847 int err;
848
849 snprintf(name, sizeof(name), "eld#%d", dai->id);
850 err = snd_card_proc_new(dai->component->card->snd_card, name, &entry);
851 if (err < 0)
852 return err;
853
854 snd_info_set_text_ops(entry, hcp, print_eld_info);
855 hcp->proc_entry = entry;
856
857 return 0;
858 }
859
hdmi_dai_proc_free(struct hdmi_codec_priv * hcp)860 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
861 {
862 snd_info_free_entry(hcp->proc_entry);
863 hcp->proc_entry = NULL;
864 }
865 #else
hdmi_dai_proc_new(struct hdmi_codec_priv * hcp,struct snd_soc_dai * dai)866 static int hdmi_dai_proc_new(struct hdmi_codec_priv *hcp,
867 struct snd_soc_dai *dai)
868 {
869 return 0;
870 }
871
hdmi_dai_proc_free(struct hdmi_codec_priv * hcp)872 static void hdmi_dai_proc_free(struct hdmi_codec_priv *hcp)
873 {
874 }
875 #endif
876
hdmi_dai_probe(struct snd_soc_dai * dai)877 static int hdmi_dai_probe(struct snd_soc_dai *dai)
878 {
879 struct hdmi_codec_priv *hcp =
880 snd_soc_component_get_drvdata(dai->component);
881 struct snd_soc_dapm_context *dapm;
882 struct hdmi_codec_daifmt *daifmt;
883 struct snd_soc_dapm_route route[] = {
884 {
885 .sink = "TX",
886 .source = dai->driver->playback.stream_name,
887 },
888 {
889 .sink = dai->driver->capture.stream_name,
890 .source = "RX",
891 },
892 };
893 int ret, i;
894
895 dapm = snd_soc_component_get_dapm(dai->component);
896
897 /* One of the directions might be omitted for unidirectional DAIs */
898 for (i = 0; i < ARRAY_SIZE(route); i++) {
899 if (!route[i].source || !route[i].sink)
900 continue;
901
902 ret = snd_soc_dapm_add_routes(dapm, &route[i], 1);
903 if (ret)
904 return ret;
905 }
906
907 daifmt = devm_kzalloc(dai->dev, sizeof(*daifmt), GFP_KERNEL);
908 if (!daifmt)
909 return -ENOMEM;
910
911 snd_soc_dai_dma_data_set_playback(dai, daifmt);
912
913 return hdmi_dai_proc_new(hcp, dai);
914 }
915
hdmi_dai_remove(struct snd_soc_dai * dai)916 static int hdmi_dai_remove(struct snd_soc_dai *dai)
917 {
918 struct hdmi_codec_priv *hcp =
919 snd_soc_component_get_drvdata(dai->component);
920
921 hdmi_dai_proc_free(hcp);
922 return 0;
923 }
924
hdmi_codec_jack_report(struct hdmi_codec_priv * hcp,unsigned int jack_status)925 static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
926 unsigned int jack_status)
927 {
928 if (jack_status != hcp->jack_status) {
929 if (hcp->jack)
930 snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
931 hcp->jack_status = jack_status;
932 }
933 }
934
plugged_cb(struct device * dev,bool plugged)935 static void plugged_cb(struct device *dev, bool plugged)
936 {
937 struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
938 int ret;
939
940 if (plugged) {
941 if (hcp->hcd.ops->get_eld) {
942 hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
943 hcp->eld, sizeof(hcp->eld));
944 ret = snd_parse_eld(dev, &hcp->eld_parsed,
945 hcp->eld, sizeof(hcp->eld));
946 if (ret < 0)
947 dev_dbg(dev, "Failed to parse ELD: %d\n", ret);
948 else
949 snd_show_eld(dev, &hcp->eld_parsed);
950 }
951 hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
952 } else {
953 hdmi_codec_jack_report(hcp, 0);
954 memset(hcp->eld, 0, sizeof(hcp->eld));
955 }
956 }
957
hdmi_codec_set_jack(struct snd_soc_component * component,struct snd_soc_jack * jack,void * data)958 static int hdmi_codec_set_jack(struct snd_soc_component *component,
959 struct snd_soc_jack *jack,
960 void *data)
961 {
962 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
963
964 if (hcp->hcd.ops->hook_plugged_cb) {
965 hcp->jack = jack;
966
967 /*
968 * Report the initial jack status which may have been provided
969 * by the parent hdmi driver while the hpd hook was registered.
970 */
971 snd_soc_jack_report(jack, hcp->jack_status, SND_JACK_LINEOUT);
972
973 return 0;
974 }
975
976 return -ENOTSUPP;
977 }
978
hdmi_dai_spdif_probe(struct snd_soc_dai * dai)979 static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
980 {
981 struct hdmi_codec_daifmt *cf;
982 int ret;
983
984 ret = hdmi_dai_probe(dai);
985 if (ret)
986 return ret;
987
988 cf = snd_soc_dai_dma_data_get_playback(dai);
989 cf->fmt = HDMI_SPDIF;
990
991 return 0;
992 }
993
994 static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
995 .probe = hdmi_dai_probe,
996 .remove = hdmi_dai_remove,
997 .startup = hdmi_codec_startup,
998 .shutdown = hdmi_codec_shutdown,
999 .hw_params = hdmi_codec_hw_params,
1000 .prepare = hdmi_codec_prepare,
1001 .set_fmt = hdmi_codec_i2s_set_fmt,
1002 .mute_stream = hdmi_codec_mute,
1003 .pcm_new = hdmi_codec_pcm_new,
1004 .auto_selectable_formats = &hdmi_codec_formats,
1005 .num_auto_selectable_formats = 1,
1006 };
1007
1008 static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
1009 .probe = hdmi_dai_spdif_probe,
1010 .startup = hdmi_codec_startup,
1011 .shutdown = hdmi_codec_shutdown,
1012 .hw_params = hdmi_codec_hw_params,
1013 .mute_stream = hdmi_codec_mute,
1014 .pcm_new = hdmi_codec_pcm_new,
1015 };
1016
1017 static const struct snd_soc_dai_driver hdmi_i2s_dai = {
1018 .name = "i2s-hifi",
1019 .id = DAI_ID_I2S,
1020 .playback = {
1021 .stream_name = "I2S Playback",
1022 .channels_min = 2,
1023 .channels_max = 8,
1024 .rates = HDMI_RATES,
1025 .formats = I2S_FORMATS,
1026 .sig_bits = 24,
1027 },
1028 .capture = {
1029 .stream_name = "Capture",
1030 .channels_min = 2,
1031 .channels_max = 8,
1032 .rates = HDMI_RATES,
1033 .formats = I2S_FORMATS,
1034 .sig_bits = 24,
1035 },
1036 .ops = &hdmi_codec_i2s_dai_ops,
1037 };
1038
1039 static const struct snd_soc_dai_driver hdmi_spdif_dai = {
1040 .name = "spdif-hifi",
1041 .id = DAI_ID_SPDIF,
1042 .playback = {
1043 .stream_name = "SPDIF Playback",
1044 .channels_min = 2,
1045 .channels_max = 2,
1046 .rates = HDMI_RATES,
1047 .formats = SPDIF_FORMATS,
1048 },
1049 .capture = {
1050 .stream_name = "Capture",
1051 .channels_min = 2,
1052 .channels_max = 2,
1053 .rates = HDMI_RATES,
1054 .formats = SPDIF_FORMATS,
1055 },
1056 .ops = &hdmi_codec_spdif_dai_ops,
1057 };
1058
hdmi_of_xlate_dai_id(struct snd_soc_component * component,struct device_node * endpoint)1059 static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
1060 struct device_node *endpoint)
1061 {
1062 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1063 int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
1064
1065 if (hcp->hcd.ops->get_dai_id)
1066 ret = hcp->hcd.ops->get_dai_id(component, endpoint, hcp->hcd.data);
1067
1068 return ret;
1069 }
1070
hdmi_probe(struct snd_soc_component * component)1071 static int hdmi_probe(struct snd_soc_component *component)
1072 {
1073 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1074 int ret = 0;
1075
1076 if (hcp->hcd.ops->hook_plugged_cb) {
1077 ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1078 hcp->hcd.data,
1079 plugged_cb,
1080 component->dev);
1081 }
1082
1083 return ret;
1084 }
1085
hdmi_remove(struct snd_soc_component * component)1086 static void hdmi_remove(struct snd_soc_component *component)
1087 {
1088 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
1089
1090 if (hcp->hcd.ops->hook_plugged_cb)
1091 hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
1092 hcp->hcd.data, NULL, NULL);
1093 }
1094
1095 static const struct snd_soc_component_driver hdmi_driver = {
1096 .probe = hdmi_probe,
1097 .remove = hdmi_remove,
1098 .dapm_widgets = hdmi_widgets,
1099 .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
1100 .of_xlate_dai_id = hdmi_of_xlate_dai_id,
1101 .idle_bias_on = 1,
1102 .use_pmdown_time = 1,
1103 .endianness = 1,
1104 .set_jack = hdmi_codec_set_jack,
1105 };
1106
hdmi_codec_probe(struct platform_device * pdev)1107 static int hdmi_codec_probe(struct platform_device *pdev)
1108 {
1109 struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
1110 struct snd_soc_dai_driver *daidrv;
1111 struct device *dev = &pdev->dev;
1112 struct hdmi_codec_priv *hcp;
1113 int dai_count, i = 0;
1114 int ret;
1115
1116 if (!hcd) {
1117 dev_err(dev, "%s: No platform data\n", __func__);
1118 return -EINVAL;
1119 }
1120
1121 dai_count = hcd->i2s + hcd->spdif;
1122 if (dai_count < 1 || !hcd->ops ||
1123 (!hcd->ops->hw_params && !hcd->ops->prepare) ||
1124 !hcd->ops->audio_shutdown) {
1125 dev_err(dev, "%s: Invalid parameters\n", __func__);
1126 return -EINVAL;
1127 }
1128
1129 hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
1130 if (!hcp)
1131 return -ENOMEM;
1132
1133 hcp->hcd = *hcd;
1134 mutex_init(&hcp->lock);
1135
1136 ret = snd_pcm_create_iec958_consumer_default(hcp->iec_status,
1137 sizeof(hcp->iec_status));
1138 if (ret < 0)
1139 return ret;
1140
1141 daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
1142 if (!daidrv)
1143 return -ENOMEM;
1144
1145 if (hcd->i2s) {
1146 daidrv[i] = hdmi_i2s_dai;
1147 daidrv[i].playback.channels_max = hcd->max_i2s_channels;
1148 if (hcd->no_i2s_playback)
1149 memset(&daidrv[i].playback, 0,
1150 sizeof(daidrv[i].playback));
1151 if (hcd->no_i2s_capture)
1152 memset(&daidrv[i].capture, 0,
1153 sizeof(daidrv[i].capture));
1154 i++;
1155 }
1156
1157 if (hcd->spdif) {
1158 daidrv[i] = hdmi_spdif_dai;
1159 if (hcd->no_spdif_playback)
1160 memset(&daidrv[i].playback, 0,
1161 sizeof(daidrv[i].playback));
1162 if (hcd->no_spdif_capture)
1163 memset(&daidrv[i].capture, 0,
1164 sizeof(daidrv[i].capture));
1165 }
1166
1167 dev_set_drvdata(dev, hcp);
1168
1169 ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
1170 dai_count);
1171 if (ret) {
1172 dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
1173 __func__, ret);
1174 return ret;
1175 }
1176 return 0;
1177 }
1178
1179 static struct platform_driver hdmi_codec_driver = {
1180 .driver = {
1181 .name = HDMI_CODEC_DRV_NAME,
1182 },
1183 .probe = hdmi_codec_probe,
1184 };
1185
1186 module_platform_driver(hdmi_codec_driver);
1187
1188 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
1189 MODULE_DESCRIPTION("HDMI Audio Codec Driver");
1190 MODULE_LICENSE("GPL");
1191 MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);
1192