blob: 65bde6f0ea1c02904716d1f18a6c73ee819b4c4a [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Jyri Sarha09184112016-03-31 16:36:00 +03002/*
3 * ALSA SoC codec for HDMI encoder drivers
Alexander A. Klimov5856d8b2020-07-19 17:38:22 +02004 * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com/
Jyri Sarha09184112016-03-31 16:36:00 +03005 * Author: Jyri Sarha <jsarha@ti.com>
Jyri Sarha09184112016-03-31 16:36:00 +03006 */
7#include <linux/module.h>
8#include <linux/string.h>
9#include <sound/core.h>
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +080010#include <sound/jack.h>
Jyri Sarha09184112016-03-31 16:36:00 +030011#include <sound/pcm.h>
12#include <sound/pcm_params.h>
13#include <sound/soc.h>
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010014#include <sound/tlv.h>
Jyri Sarha09184112016-03-31 16:36:00 +030015#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
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010021#define HDMI_CODEC_CHMAP_IDX_UNKNOWN -1
22
23struct hdmi_codec_channel_map_table {
24 unsigned char map; /* ALSA API channel map position */
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010025};
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 */
Colin Ian King3013c752017-08-12 23:50:28 +010062static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010063 { .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 */
Colin Ian King3013c752017-08-12 23:50:28 +010069static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010070 { .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
Jyri Sarha09184112016-03-31 16:36:00 +0300271struct hdmi_codec_priv {
272 struct hdmi_codec_pdata hcd;
Jyri Sarha09184112016-03-31 16:36:00 +0300273 uint8_t eld[MAX_ELD_BYTES];
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100274 struct snd_pcm_chmap *chmap_info;
275 unsigned int chmap_idx;
Jerome Brunet14428422019-12-06 11:35:42 +0100276 struct mutex lock;
277 bool busy;
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800278 struct snd_soc_jack *jack;
279 unsigned int jack_status;
Jyri Sarha09184112016-03-31 16:36:00 +0300280};
281
282static const struct snd_soc_dapm_widget hdmi_widgets[] = {
283 SND_SOC_DAPM_OUTPUT("TX"),
Shengjiu Wang144f8362020-11-26 14:36:48 +0800284 SND_SOC_DAPM_OUTPUT("RX"),
Jyri Sarha09184112016-03-31 16:36:00 +0300285};
286
Jyri Sarha09184112016-03-31 16:36:00 +0300287enum {
288 DAI_ID_I2S = 0,
289 DAI_ID_SPDIF,
290};
291
Philipp Zabel81151cf2016-04-20 10:59:58 +0200292static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
293 struct snd_ctl_elem_info *uinfo)
294{
Philipp Zabel81151cf2016-04-20 10:59:58 +0200295 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800296 uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
Philipp Zabel81151cf2016-04-20 10:59:58 +0200297
298 return 0;
299}
300
301static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
302 struct snd_ctl_elem_value *ucontrol)
303{
304 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
305 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
306
Philipp Zabel81151cf2016-04-20 10:59:58 +0200307 memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
Philipp Zabel81151cf2016-04-20 10:59:58 +0200308
309 return 0;
310}
311
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100312static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
313{
314 int i;
Colin Ian King1d6463a2017-07-11 15:08:55 +0100315 static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100316 [0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
317 [4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
318 };
319 unsigned long spk_mask = 0;
320
321 for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
322 if (spk_alloc & (1 << i))
323 spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
324 }
325
326 return spk_mask;
327}
328
Colin Ian King3013c752017-08-12 23:50:28 +0100329static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100330{
331 u8 spk_alloc;
332 unsigned long spk_mask;
333
334 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
335 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
336
337 /* Detect if only stereo supported, else return 8 channels mappings */
338 if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
339 hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
340 else
341 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
342}
343
344static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
345 unsigned char channels)
346{
347 int i;
348 u8 spk_alloc;
349 unsigned long spk_mask;
350 const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
351
352 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
353 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
354
355 for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
356 /* If spk_alloc == 0, HDMI is unplugged return stereo config*/
357 if (!spk_alloc && cap->ca_id == 0)
358 return i;
359 if (cap->n_ch != channels)
360 continue;
361 if (!(cap->mask == (spk_mask & cap->mask)))
362 continue;
363 return i;
364 }
365
366 return -EINVAL;
367}
368static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
369 struct snd_ctl_elem_value *ucontrol)
370{
371 unsigned const char *map;
372 unsigned int i;
373 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
374 struct hdmi_codec_priv *hcp = info->private_data;
375
376 map = info->chmap[hcp->chmap_idx].map;
377
378 for (i = 0; i < info->max_channels; i++) {
379 if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
380 ucontrol->value.integer.value[i] = 0;
381 else
382 ucontrol->value.integer.value[i] = map[i];
383 }
384
385 return 0;
386}
387
Jyri Sarha09184112016-03-31 16:36:00 +0300388static int hdmi_codec_startup(struct snd_pcm_substream *substream,
389 struct snd_soc_dai *dai)
390{
391 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Shengjiu Wang144f8362020-11-26 14:36:48 +0800392 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Jyri Sarha09184112016-03-31 16:36:00 +0300393 int ret = 0;
394
Jerome Brunet14428422019-12-06 11:35:42 +0100395 mutex_lock(&hcp->lock);
396 if (hcp->busy) {
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200397 dev_err(dai->dev, "Only one simultaneous stream supported!\n");
Jerome Brunet14428422019-12-06 11:35:42 +0100398 mutex_unlock(&hcp->lock);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200399 return -EINVAL;
400 }
Jyri Sarha09184112016-03-31 16:36:00 +0300401
402 if (hcp->hcd.ops->audio_startup) {
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000403 ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200404 if (ret)
405 goto err;
Jyri Sarha09184112016-03-31 16:36:00 +0300406 }
407
Shengjiu Wang144f8362020-11-26 14:36:48 +0800408 if (tx && hcp->hcd.ops->get_eld) {
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000409 ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
410 hcp->eld, sizeof(hcp->eld));
Jerome Brunet14428422019-12-06 11:35:42 +0100411 if (ret)
412 goto err;
Jyri Sarha09184112016-03-31 16:36:00 +0300413
Jerome Brunet14428422019-12-06 11:35:42 +0100414 ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
415 if (ret)
416 goto err;
417
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100418 /* Select chmap supported */
419 hdmi_codec_eld_chmap(hcp);
Jyri Sarha09184112016-03-31 16:36:00 +0300420 }
Jerome Brunet14428422019-12-06 11:35:42 +0100421
422 hcp->busy = true;
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200423
424err:
Jerome Brunet14428422019-12-06 11:35:42 +0100425 mutex_unlock(&hcp->lock);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200426 return ret;
Jyri Sarha09184112016-03-31 16:36:00 +0300427}
428
429static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
430 struct snd_soc_dai *dai)
431{
432 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
433
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100434 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000435 hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
Jyri Sarha09184112016-03-31 16:36:00 +0300436
Jerome Brunet14428422019-12-06 11:35:42 +0100437 mutex_lock(&hcp->lock);
438 hcp->busy = false;
439 mutex_unlock(&hcp->lock);
Jyri Sarha09184112016-03-31 16:36:00 +0300440}
441
442static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
443 struct snd_pcm_hw_params *params,
444 struct snd_soc_dai *dai)
445{
446 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Jerome Brunet0cf46102019-05-06 11:58:15 +0200447 struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
Jyri Sarha09184112016-03-31 16:36:00 +0300448 struct hdmi_codec_params hp = {
449 .iec = {
450 .status = { 0 },
451 .subcode = { 0 },
452 .pad = 0,
453 .dig_subframe = { 0 },
454 }
455 };
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100456 int ret, idx;
Jyri Sarha09184112016-03-31 16:36:00 +0300457
458 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
459 params_width(params), params_rate(params),
460 params_channels(params));
461
Jyri Sarha09184112016-03-31 16:36:00 +0300462 ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
463 sizeof(hp.iec.status));
464 if (ret < 0) {
465 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
466 ret);
467 return ret;
468 }
469
Jyri Sarha09184112016-03-31 16:36:00 +0300470 hdmi_audio_infoframe_init(&hp.cea);
471 hp.cea.channels = params_channels(params);
472 hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
473 hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
474 hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
475
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100476 /* Select a channel allocation that matches with ELD and pcm channels */
477 idx = hdmi_codec_get_ch_alloc_table_idx(hcp, hp.cea.channels);
478 if (idx < 0) {
479 dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
480 idx);
481 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
482 return idx;
483 }
484 hp.cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
485 hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
486
Jyri Sarha09184112016-03-31 16:36:00 +0300487 hp.sample_width = params_width(params);
488 hp.sample_rate = params_rate(params);
489 hp.channels = params_channels(params);
490
Sia Jee Heng28785f52021-02-04 09:42:55 +0800491 cf->bit_fmt = params_format(params);
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000492 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200493 cf, &hp);
Jyri Sarha09184112016-03-31 16:36:00 +0300494}
495
Jerome Brunet0cf46102019-05-06 11:58:15 +0200496static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
497 unsigned int fmt)
Jyri Sarha09184112016-03-31 16:36:00 +0300498{
Jerome Brunet0cf46102019-05-06 11:58:15 +0200499 struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
Jyri Sarha09184112016-03-31 16:36:00 +0300500
Jerome Brunet0cf46102019-05-06 11:58:15 +0200501 /* Reset daifmt */
502 memset(cf, 0, sizeof(*cf));
Jyri Sarha09184112016-03-31 16:36:00 +0300503
Russell King2e95f982019-02-28 15:30:34 +0000504 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
505 case SND_SOC_DAIFMT_CBM_CFM:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200506 cf->bit_clk_master = 1;
507 cf->frame_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000508 break;
509 case SND_SOC_DAIFMT_CBS_CFM:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200510 cf->frame_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000511 break;
512 case SND_SOC_DAIFMT_CBM_CFS:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200513 cf->bit_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000514 break;
515 case SND_SOC_DAIFMT_CBS_CFS:
516 break;
517 default:
518 return -EINVAL;
519 }
Jyri Sarha09184112016-03-31 16:36:00 +0300520
Russell King2e95f982019-02-28 15:30:34 +0000521 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
522 case SND_SOC_DAIFMT_NB_NF:
523 break;
524 case SND_SOC_DAIFMT_NB_IF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200525 cf->frame_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000526 break;
527 case SND_SOC_DAIFMT_IB_NF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200528 cf->bit_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000529 break;
530 case SND_SOC_DAIFMT_IB_IF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200531 cf->frame_clk_inv = 1;
532 cf->bit_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000533 break;
534 }
535
536 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
537 case SND_SOC_DAIFMT_I2S:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200538 cf->fmt = HDMI_I2S;
Russell King2e95f982019-02-28 15:30:34 +0000539 break;
540 case SND_SOC_DAIFMT_DSP_A:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200541 cf->fmt = HDMI_DSP_A;
Russell King2e95f982019-02-28 15:30:34 +0000542 break;
543 case SND_SOC_DAIFMT_DSP_B:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200544 cf->fmt = HDMI_DSP_B;
Russell King2e95f982019-02-28 15:30:34 +0000545 break;
546 case SND_SOC_DAIFMT_RIGHT_J:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200547 cf->fmt = HDMI_RIGHT_J;
Russell King2e95f982019-02-28 15:30:34 +0000548 break;
549 case SND_SOC_DAIFMT_LEFT_J:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200550 cf->fmt = HDMI_LEFT_J;
Russell King2e95f982019-02-28 15:30:34 +0000551 break;
552 case SND_SOC_DAIFMT_AC97:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200553 cf->fmt = HDMI_AC97;
Russell King2e95f982019-02-28 15:30:34 +0000554 break;
555 default:
556 dev_err(dai->dev, "Invalid DAI interface format\n");
557 return -EINVAL;
Jyri Sarha09184112016-03-31 16:36:00 +0300558 }
559
Russell King2e95f982019-02-28 15:30:34 +0000560 return 0;
Jyri Sarha09184112016-03-31 16:36:00 +0300561}
562
Kuninori Morimotod7897102020-07-09 10:55:45 +0900563static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
Jyri Sarha09184112016-03-31 16:36:00 +0300564{
565 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
566
Kuninori Morimotod7897102020-07-09 10:55:45 +0900567 /*
568 * ignore if direction was CAPTURE
569 * and it had .no_capture_mute flag
570 * see
571 * snd_soc_dai_digital_mute()
572 */
573 if (hcp->hcd.ops->mute_stream &&
574 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
575 !hcp->hcd.ops->no_capture_mute))
576 return hcp->hcd.ops->mute_stream(dai->dev->parent,
577 hcp->hcd.data,
578 mute, direction);
Jyri Sarha09184112016-03-31 16:36:00 +0300579
Kuninori Morimotoe07e49c2020-07-09 10:55:36 +0900580 return -ENOTSUPP;
Jyri Sarha09184112016-03-31 16:36:00 +0300581}
582
Jerome Brunet0cf46102019-05-06 11:58:15 +0200583static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
Jyri Sarha09184112016-03-31 16:36:00 +0300584 .startup = hdmi_codec_startup,
585 .shutdown = hdmi_codec_shutdown,
586 .hw_params = hdmi_codec_hw_params,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200587 .set_fmt = hdmi_codec_i2s_set_fmt,
Kuninori Morimotod7897102020-07-09 10:55:45 +0900588 .mute_stream = hdmi_codec_mute,
Jyri Sarha09184112016-03-31 16:36:00 +0300589};
590
Jerome Brunet0cf46102019-05-06 11:58:15 +0200591static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
592 .startup = hdmi_codec_startup,
593 .shutdown = hdmi_codec_shutdown,
594 .hw_params = hdmi_codec_hw_params,
Kuninori Morimotod7897102020-07-09 10:55:45 +0900595 .mute_stream = hdmi_codec_mute,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200596};
Jyri Sarha09184112016-03-31 16:36:00 +0300597
598#define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
599 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
600 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
601 SNDRV_PCM_RATE_192000)
602
603#define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
604 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
605 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
606 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
607
608/*
609 * This list is only for formats allowed on the I2S bus. So there is
610 * some formats listed that are not supported by HDMI interface. For
611 * instance allowing the 32-bit formats enables 24-precision with CPU
612 * DAIs that do not support 24-bit formats. If the extra formats cause
613 * problems, we should add the video side driver an option to disable
614 * them.
615 */
616#define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
617 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
618 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
619 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
Sia Jee Heng28785f52021-02-04 09:42:55 +0800620 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |\
621 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
Jyri Sarha09184112016-03-31 16:36:00 +0300622
Maxime Ripard366b45b2021-05-25 15:23:45 +0200623struct snd_kcontrol_new hdmi_codec_controls[] = {
624 {
625 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
626 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
627 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
628 .name = "ELD",
629 .info = hdmi_eld_ctl_info,
630 .get = hdmi_eld_ctl_get,
631 },
632};
633
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100634static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
635 struct snd_soc_dai *dai)
636{
637 struct snd_soc_dai_driver *drv = dai->driver;
638 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Maxime Ripard366b45b2021-05-25 15:23:45 +0200639 unsigned int i;
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100640 int ret;
641
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100642 ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
643 NULL, drv->playback.channels_max, 0,
644 &hcp->chmap_info);
645 if (ret < 0)
646 return ret;
647
648 /* override handlers */
649 hcp->chmap_info->private_data = hcp;
650 hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
651
652 /* default chmap supported is stereo */
653 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
654 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
655
Maxime Ripard366b45b2021-05-25 15:23:45 +0200656 for (i = 0; i < ARRAY_SIZE(hdmi_codec_controls); i++) {
657 struct snd_kcontrol *kctl;
Kuninori Morimotoecbb1b82017-07-06 00:39:29 +0000658
Maxime Ripard366b45b2021-05-25 15:23:45 +0200659 /* add ELD ctl with the device number corresponding to the PCM stream */
660 kctl = snd_ctl_new1(&hdmi_codec_controls[i], dai->component);
661 if (!kctl)
662 return -ENOMEM;
663
664 kctl->id.device = rtd->pcm->device;
665 ret = snd_ctl_add(rtd->card->snd_card, kctl);
666 if (ret < 0)
667 return ret;
668 }
669
670 return 0;
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100671}
672
Russell Kingd30e23d2018-07-14 16:01:06 +0100673static int hdmi_dai_probe(struct snd_soc_dai *dai)
674{
675 struct snd_soc_dapm_context *dapm;
Jerome Brunet0cf46102019-05-06 11:58:15 +0200676 struct hdmi_codec_daifmt *daifmt;
Shengjiu Wang144f8362020-11-26 14:36:48 +0800677 struct snd_soc_dapm_route route[] = {
678 {
679 .sink = "TX",
680 .source = dai->driver->playback.stream_name,
681 },
682 {
683 .sink = dai->driver->capture.stream_name,
684 .source = "RX",
685 },
Russell Kingd30e23d2018-07-14 16:01:06 +0100686 };
Jerome Brunet0cf46102019-05-06 11:58:15 +0200687 int ret;
Russell Kingd30e23d2018-07-14 16:01:06 +0100688
689 dapm = snd_soc_component_get_dapm(dai->component);
Shengjiu Wang144f8362020-11-26 14:36:48 +0800690 ret = snd_soc_dapm_add_routes(dapm, route, 2);
Jerome Brunet0cf46102019-05-06 11:58:15 +0200691 if (ret)
692 return ret;
Russell Kingd30e23d2018-07-14 16:01:06 +0100693
Jerome Brunet0cf46102019-05-06 11:58:15 +0200694 daifmt = kzalloc(sizeof(*daifmt), GFP_KERNEL);
695 if (!daifmt)
696 return -ENOMEM;
697
698 dai->playback_dma_data = daifmt;
699 return 0;
700}
701
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800702static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
703 unsigned int jack_status)
704{
705 if (hcp->jack && jack_status != hcp->jack_status) {
706 snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
707 hcp->jack_status = jack_status;
708 }
709}
710
711static void plugged_cb(struct device *dev, bool plugged)
712{
713 struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
714
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800715 if (plugged) {
716 if (hcp->hcd.ops->get_eld) {
717 hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
718 hcp->eld, sizeof(hcp->eld));
719 }
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800720 hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800721 } else {
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800722 hdmi_codec_jack_report(hcp, 0);
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800723 memset(hcp->eld, 0, sizeof(hcp->eld));
724 }
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800725}
726
Cheng-Yi Chiang55c5cc632020-09-22 14:23:16 +0800727static int hdmi_codec_set_jack(struct snd_soc_component *component,
728 struct snd_soc_jack *jack,
729 void *data)
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800730{
731 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
Stephan Gerhold2a0435d2021-01-07 17:51:31 +0100732 int ret = -ENOTSUPP;
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800733
734 if (hcp->hcd.ops->hook_plugged_cb) {
735 hcp->jack = jack;
736 ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
737 hcp->hcd.data,
738 plugged_cb,
739 component->dev);
740 if (ret)
741 hcp->jack = NULL;
742 }
743 return ret;
744}
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800745
Jerome Brunet0cf46102019-05-06 11:58:15 +0200746static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
747{
Pierre-Louis Bossart3c011ef2021-03-12 12:22:33 -0600748 struct hdmi_codec_daifmt *cf;
Jerome Brunet0cf46102019-05-06 11:58:15 +0200749 int ret;
750
751 ret = hdmi_dai_probe(dai);
752 if (ret)
753 return ret;
754
755 cf = dai->playback_dma_data;
756 cf->fmt = HDMI_SPDIF;
757
758 return 0;
759}
760
761static int hdmi_codec_dai_remove(struct snd_soc_dai *dai)
762{
763 kfree(dai->playback_dma_data);
764 return 0;
Russell Kingd30e23d2018-07-14 16:01:06 +0100765}
766
Bhumika Goyal6e37f932017-08-16 22:45:09 +0530767static const struct snd_soc_dai_driver hdmi_i2s_dai = {
Kuninori Morimoto24069b52017-05-18 01:40:02 +0000768 .name = "i2s-hifi",
Jyri Sarha09184112016-03-31 16:36:00 +0300769 .id = DAI_ID_I2S,
Russell Kingd30e23d2018-07-14 16:01:06 +0100770 .probe = hdmi_dai_probe,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200771 .remove = hdmi_codec_dai_remove,
Jyri Sarha09184112016-03-31 16:36:00 +0300772 .playback = {
Jeffy Chen943fa022017-08-12 08:40:38 +0800773 .stream_name = "I2S Playback",
Jyri Sarha09184112016-03-31 16:36:00 +0300774 .channels_min = 2,
775 .channels_max = 8,
776 .rates = HDMI_RATES,
777 .formats = I2S_FORMATS,
778 .sig_bits = 24,
779 },
Shengjiu Wang144f8362020-11-26 14:36:48 +0800780 .capture = {
781 .stream_name = "Capture",
782 .channels_min = 2,
783 .channels_max = 8,
784 .rates = HDMI_RATES,
785 .formats = I2S_FORMATS,
786 .sig_bits = 24,
787 },
Jerome Brunet0cf46102019-05-06 11:58:15 +0200788 .ops = &hdmi_codec_i2s_dai_ops,
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100789 .pcm_new = hdmi_codec_pcm_new,
Jyri Sarha09184112016-03-31 16:36:00 +0300790};
791
792static const struct snd_soc_dai_driver hdmi_spdif_dai = {
Kuninori Morimoto24069b52017-05-18 01:40:02 +0000793 .name = "spdif-hifi",
Jyri Sarha09184112016-03-31 16:36:00 +0300794 .id = DAI_ID_SPDIF,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200795 .probe = hdmi_dai_spdif_probe,
796 .remove = hdmi_codec_dai_remove,
Jyri Sarha09184112016-03-31 16:36:00 +0300797 .playback = {
Jeffy Chen943fa022017-08-12 08:40:38 +0800798 .stream_name = "SPDIF Playback",
Jyri Sarha09184112016-03-31 16:36:00 +0300799 .channels_min = 2,
800 .channels_max = 2,
801 .rates = HDMI_RATES,
802 .formats = SPDIF_FORMATS,
803 },
Shengjiu Wang144f8362020-11-26 14:36:48 +0800804 .capture = {
805 .stream_name = "Capture",
806 .channels_min = 2,
807 .channels_max = 2,
808 .rates = HDMI_RATES,
809 .formats = SPDIF_FORMATS,
810 },
Jerome Brunet0cf46102019-05-06 11:58:15 +0200811 .ops = &hdmi_codec_spdif_dai_ops,
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100812 .pcm_new = hdmi_codec_pcm_new,
Jyri Sarha09184112016-03-31 16:36:00 +0300813};
814
Kuninori Morimoto96203fb2017-05-18 01:40:20 +0000815static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
816 struct device_node *endpoint)
817{
818 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
819 int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
820
821 if (hcp->hcd.ops->get_dai_id)
822 ret = hcp->hcd.ops->get_dai_id(component, endpoint);
823
824 return ret;
825}
826
Tzung-Bi Shih4aadf4b2020-02-17 11:16:52 +0800827static void hdmi_remove(struct snd_soc_component *component)
828{
829 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
830
831 if (hcp->hcd.ops->hook_plugged_cb)
832 hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
833 hcp->hcd.data, NULL, NULL);
834}
835
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000836static const struct snd_soc_component_driver hdmi_driver = {
Tzung-Bi Shih4aadf4b2020-02-17 11:16:52 +0800837 .remove = hdmi_remove,
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000838 .dapm_widgets = hdmi_widgets,
839 .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000840 .of_xlate_dai_id = hdmi_of_xlate_dai_id,
841 .idle_bias_on = 1,
842 .use_pmdown_time = 1,
843 .endianness = 1,
844 .non_legacy_dai_naming = 1,
Cheng-Yi Chiang55c5cc632020-09-22 14:23:16 +0800845 .set_jack = hdmi_codec_set_jack,
Jyri Sarha09184112016-03-31 16:36:00 +0300846};
847
848static int hdmi_codec_probe(struct platform_device *pdev)
849{
850 struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
Jerome Brunet1de005d2019-05-06 11:58:14 +0200851 struct snd_soc_dai_driver *daidrv;
Jyri Sarha09184112016-03-31 16:36:00 +0300852 struct device *dev = &pdev->dev;
853 struct hdmi_codec_priv *hcp;
854 int dai_count, i = 0;
855 int ret;
856
Jyri Sarha09184112016-03-31 16:36:00 +0300857 if (!hcd) {
Colin Ian King7b6531c2019-04-17 17:26:28 +0100858 dev_err(dev, "%s: No platform data\n", __func__);
Jyri Sarha09184112016-03-31 16:36:00 +0300859 return -EINVAL;
860 }
861
862 dai_count = hcd->i2s + hcd->spdif;
863 if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
864 !hcd->ops->audio_shutdown) {
865 dev_err(dev, "%s: Invalid parameters\n", __func__);
866 return -EINVAL;
867 }
868
869 hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
870 if (!hcp)
871 return -ENOMEM;
872
873 hcp->hcd = *hcd;
Jerome Brunet14428422019-12-06 11:35:42 +0100874 mutex_init(&hcp->lock);
875
Jerome Brunet1de005d2019-05-06 11:58:14 +0200876 daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
877 if (!daidrv)
Jyri Sarha09184112016-03-31 16:36:00 +0300878 return -ENOMEM;
879
880 if (hcd->i2s) {
Jerome Brunet1de005d2019-05-06 11:58:14 +0200881 daidrv[i] = hdmi_i2s_dai;
882 daidrv[i].playback.channels_max = hcd->max_i2s_channels;
Jyri Sarha09184112016-03-31 16:36:00 +0300883 i++;
884 }
885
Jerome Brunet0cf46102019-05-06 11:58:15 +0200886 if (hcd->spdif)
Jerome Brunet1de005d2019-05-06 11:58:14 +0200887 daidrv[i] = hdmi_spdif_dai;
Jyri Sarha09184112016-03-31 16:36:00 +0300888
Russell King0ce23d62019-01-17 17:32:05 +0000889 dev_set_drvdata(dev, hcp);
890
Jerome Brunet1de005d2019-05-06 11:58:14 +0200891 ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
892 dai_count);
Jyri Sarha09184112016-03-31 16:36:00 +0300893 if (ret) {
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000894 dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
Jyri Sarha09184112016-03-31 16:36:00 +0300895 __func__, ret);
896 return ret;
897 }
Jyri Sarha09184112016-03-31 16:36:00 +0300898 return 0;
899}
900
Jyri Sarha09184112016-03-31 16:36:00 +0300901static struct platform_driver hdmi_codec_driver = {
902 .driver = {
903 .name = HDMI_CODEC_DRV_NAME,
904 },
905 .probe = hdmi_codec_probe,
Jyri Sarha09184112016-03-31 16:36:00 +0300906};
907
908module_platform_driver(hdmi_codec_driver);
909
910MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
911MODULE_DESCRIPTION("HDMI Audio Codec Driver");
912MODULE_LICENSE("GPL");
913MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);