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