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