blob: 83e74ddccf5985edef60f063070941a12b506e18 [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 */
25 unsigned long spk_mask; /* speaker position bit mask */
26};
27
28/*
29 * CEA speaker placement for HDMI 1.4:
30 *
31 * FL FLC FC FRC FR FRW
32 *
33 * LFE
34 *
35 * RL RLC RC RRC RR
36 *
37 * Speaker placement has to be extended to support HDMI 2.0
38 */
39enum hdmi_codec_cea_spk_placement {
40 FL = BIT(0), /* Front Left */
41 FC = BIT(1), /* Front Center */
42 FR = BIT(2), /* Front Right */
43 FLC = BIT(3), /* Front Left Center */
44 FRC = BIT(4), /* Front Right Center */
45 RL = BIT(5), /* Rear Left */
46 RC = BIT(6), /* Rear Center */
47 RR = BIT(7), /* Rear Right */
48 RLC = BIT(8), /* Rear Left Center */
49 RRC = BIT(9), /* Rear Right Center */
50 LFE = BIT(10), /* Low Frequency Effect */
51};
52
53/*
54 * cea Speaker allocation structure
55 */
56struct hdmi_codec_cea_spk_alloc {
57 const int ca_id;
58 unsigned int n_ch;
59 unsigned long mask;
60};
61
62/* Channel maps stereo HDMI */
Colin Ian King3013c752017-08-12 23:50:28 +010063static const struct snd_pcm_chmap_elem hdmi_codec_stereo_chmaps[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010064 { .channels = 2,
65 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
66 { }
67};
68
69/* Channel maps for multi-channel playbacks, up to 8 n_ch */
Colin Ian King3013c752017-08-12 23:50:28 +010070static const struct snd_pcm_chmap_elem hdmi_codec_8ch_chmaps[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +010071 { .channels = 2, /* CA_ID 0x00 */
72 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
73 { .channels = 4, /* CA_ID 0x01 */
74 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
75 SNDRV_CHMAP_NA } },
76 { .channels = 4, /* CA_ID 0x02 */
77 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
78 SNDRV_CHMAP_FC } },
79 { .channels = 4, /* CA_ID 0x03 */
80 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
81 SNDRV_CHMAP_FC } },
82 { .channels = 6, /* CA_ID 0x04 */
83 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
84 SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
85 { .channels = 6, /* CA_ID 0x05 */
86 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
87 SNDRV_CHMAP_NA, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
88 { .channels = 6, /* CA_ID 0x06 */
89 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
90 SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
91 { .channels = 6, /* CA_ID 0x07 */
92 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
93 SNDRV_CHMAP_FC, SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
94 { .channels = 6, /* CA_ID 0x08 */
95 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
96 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
97 { .channels = 6, /* CA_ID 0x09 */
98 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
99 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
100 { .channels = 6, /* CA_ID 0x0A */
101 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
102 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
103 { .channels = 6, /* CA_ID 0x0B */
104 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
105 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
106 { .channels = 8, /* CA_ID 0x0C */
107 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
108 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
109 SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
110 { .channels = 8, /* CA_ID 0x0D */
111 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
112 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
113 SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
114 { .channels = 8, /* CA_ID 0x0E */
115 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
116 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
117 SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
118 { .channels = 8, /* CA_ID 0x0F */
119 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
120 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
121 SNDRV_CHMAP_RC, SNDRV_CHMAP_NA } },
122 { .channels = 8, /* CA_ID 0x10 */
123 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
124 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
125 SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
126 { .channels = 8, /* CA_ID 0x11 */
127 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
128 SNDRV_CHMAP_NA, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
129 SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
130 { .channels = 8, /* CA_ID 0x12 */
131 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
132 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
133 SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
134 { .channels = 8, /* CA_ID 0x13 */
135 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
136 SNDRV_CHMAP_FC, SNDRV_CHMAP_RL, SNDRV_CHMAP_RR,
137 SNDRV_CHMAP_RLC, SNDRV_CHMAP_RRC } },
138 { .channels = 8, /* CA_ID 0x14 */
139 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
140 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
141 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
142 { .channels = 8, /* CA_ID 0x15 */
143 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
144 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
145 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
146 { .channels = 8, /* CA_ID 0x16 */
147 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
148 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
149 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
150 { .channels = 8, /* CA_ID 0x17 */
151 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
152 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
153 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
154 { .channels = 8, /* CA_ID 0x18 */
155 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
156 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
157 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
158 { .channels = 8, /* CA_ID 0x19 */
159 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
160 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
161 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
162 { .channels = 8, /* CA_ID 0x1A */
163 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
164 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
165 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
166 { .channels = 8, /* CA_ID 0x1B */
167 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
168 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
169 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
170 { .channels = 8, /* CA_ID 0x1C */
171 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
172 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
173 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
174 { .channels = 8, /* CA_ID 0x1D */
175 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
176 SNDRV_CHMAP_NA, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
177 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
178 { .channels = 8, /* CA_ID 0x1E */
179 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_NA,
180 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
181 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
182 { .channels = 8, /* CA_ID 0x1F */
183 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, SNDRV_CHMAP_LFE,
184 SNDRV_CHMAP_FC, SNDRV_CHMAP_NA, SNDRV_CHMAP_NA,
185 SNDRV_CHMAP_FLC, SNDRV_CHMAP_FRC } },
186 { }
187};
188
189/*
190 * hdmi_codec_channel_alloc: speaker configuration available for CEA
191 *
192 * This is an ordered list that must match with hdmi_codec_8ch_chmaps struct
193 * The preceding ones have better chances to be selected by
194 * hdmi_codec_get_ch_alloc_table_idx().
195 */
196static const struct hdmi_codec_cea_spk_alloc hdmi_codec_channel_alloc[] = {
197 { .ca_id = 0x00, .n_ch = 2,
198 .mask = FL | FR},
199 /* 2.1 */
200 { .ca_id = 0x01, .n_ch = 4,
201 .mask = FL | FR | LFE},
202 /* Dolby Surround */
203 { .ca_id = 0x02, .n_ch = 4,
204 .mask = FL | FR | FC },
205 /* surround51 */
206 { .ca_id = 0x0b, .n_ch = 6,
207 .mask = FL | FR | LFE | FC | RL | RR},
208 /* surround40 */
209 { .ca_id = 0x08, .n_ch = 6,
210 .mask = FL | FR | RL | RR },
211 /* surround41 */
212 { .ca_id = 0x09, .n_ch = 6,
213 .mask = FL | FR | LFE | RL | RR },
214 /* surround50 */
215 { .ca_id = 0x0a, .n_ch = 6,
216 .mask = FL | FR | FC | RL | RR },
217 /* 6.1 */
218 { .ca_id = 0x0f, .n_ch = 8,
219 .mask = FL | FR | LFE | FC | RL | RR | RC },
220 /* surround71 */
221 { .ca_id = 0x13, .n_ch = 8,
222 .mask = FL | FR | LFE | FC | RL | RR | RLC | RRC },
223 /* others */
224 { .ca_id = 0x03, .n_ch = 8,
225 .mask = FL | FR | LFE | FC },
226 { .ca_id = 0x04, .n_ch = 8,
227 .mask = FL | FR | RC},
228 { .ca_id = 0x05, .n_ch = 8,
229 .mask = FL | FR | LFE | RC },
230 { .ca_id = 0x06, .n_ch = 8,
231 .mask = FL | FR | FC | RC },
232 { .ca_id = 0x07, .n_ch = 8,
233 .mask = FL | FR | LFE | FC | RC },
234 { .ca_id = 0x0c, .n_ch = 8,
235 .mask = FL | FR | RC | RL | RR },
236 { .ca_id = 0x0d, .n_ch = 8,
237 .mask = FL | FR | LFE | RL | RR | RC },
238 { .ca_id = 0x0e, .n_ch = 8,
239 .mask = FL | FR | FC | RL | RR | RC },
240 { .ca_id = 0x10, .n_ch = 8,
241 .mask = FL | FR | RL | RR | RLC | RRC },
242 { .ca_id = 0x11, .n_ch = 8,
243 .mask = FL | FR | LFE | RL | RR | RLC | RRC },
244 { .ca_id = 0x12, .n_ch = 8,
245 .mask = FL | FR | FC | RL | RR | RLC | RRC },
246 { .ca_id = 0x14, .n_ch = 8,
247 .mask = FL | FR | FLC | FRC },
248 { .ca_id = 0x15, .n_ch = 8,
249 .mask = FL | FR | LFE | FLC | FRC },
250 { .ca_id = 0x16, .n_ch = 8,
251 .mask = FL | FR | FC | FLC | FRC },
252 { .ca_id = 0x17, .n_ch = 8,
253 .mask = FL | FR | LFE | FC | FLC | FRC },
254 { .ca_id = 0x18, .n_ch = 8,
255 .mask = FL | FR | RC | FLC | FRC },
256 { .ca_id = 0x19, .n_ch = 8,
257 .mask = FL | FR | LFE | RC | FLC | FRC },
258 { .ca_id = 0x1a, .n_ch = 8,
259 .mask = FL | FR | RC | FC | FLC | FRC },
260 { .ca_id = 0x1b, .n_ch = 8,
261 .mask = FL | FR | LFE | RC | FC | FLC | FRC },
262 { .ca_id = 0x1c, .n_ch = 8,
263 .mask = FL | FR | RL | RR | FLC | FRC },
264 { .ca_id = 0x1d, .n_ch = 8,
265 .mask = FL | FR | LFE | RL | RR | FLC | FRC },
266 { .ca_id = 0x1e, .n_ch = 8,
267 .mask = FL | FR | FC | RL | RR | FLC | FRC },
268 { .ca_id = 0x1f, .n_ch = 8,
269 .mask = FL | FR | LFE | FC | RL | RR | FLC | FRC },
270};
271
Jyri Sarha09184112016-03-31 16:36:00 +0300272struct hdmi_codec_priv {
273 struct hdmi_codec_pdata hcd;
Jyri Sarha09184112016-03-31 16:36:00 +0300274 uint8_t eld[MAX_ELD_BYTES];
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100275 struct snd_pcm_chmap *chmap_info;
276 unsigned int chmap_idx;
Jerome Brunet14428422019-12-06 11:35:42 +0100277 struct mutex lock;
278 bool busy;
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800279 struct snd_soc_jack *jack;
280 unsigned int jack_status;
Jyri Sarha09184112016-03-31 16:36:00 +0300281};
282
283static const struct snd_soc_dapm_widget hdmi_widgets[] = {
284 SND_SOC_DAPM_OUTPUT("TX"),
Shengjiu Wang144f8362020-11-26 14:36:48 +0800285 SND_SOC_DAPM_OUTPUT("RX"),
Jyri Sarha09184112016-03-31 16:36:00 +0300286};
287
Jyri Sarha09184112016-03-31 16:36:00 +0300288enum {
289 DAI_ID_I2S = 0,
290 DAI_ID_SPDIF,
291};
292
Philipp Zabel81151cf2016-04-20 10:59:58 +0200293static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
294 struct snd_ctl_elem_info *uinfo)
295{
Philipp Zabel81151cf2016-04-20 10:59:58 +0200296 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
Pankaj Bharadiyac5936422019-12-09 10:31:43 -0800297 uinfo->count = sizeof_field(struct hdmi_codec_priv, eld);
Philipp Zabel81151cf2016-04-20 10:59:58 +0200298
299 return 0;
300}
301
302static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
303 struct snd_ctl_elem_value *ucontrol)
304{
305 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
306 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
307
Philipp Zabel81151cf2016-04-20 10:59:58 +0200308 memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
Philipp Zabel81151cf2016-04-20 10:59:58 +0200309
310 return 0;
311}
312
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100313static unsigned long hdmi_codec_spk_mask_from_alloc(int spk_alloc)
314{
315 int i;
Colin Ian King1d6463a2017-07-11 15:08:55 +0100316 static const unsigned long hdmi_codec_eld_spk_alloc_bits[] = {
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100317 [0] = FL | FR, [1] = LFE, [2] = FC, [3] = RL | RR,
318 [4] = RC, [5] = FLC | FRC, [6] = RLC | RRC,
319 };
320 unsigned long spk_mask = 0;
321
322 for (i = 0; i < ARRAY_SIZE(hdmi_codec_eld_spk_alloc_bits); i++) {
323 if (spk_alloc & (1 << i))
324 spk_mask |= hdmi_codec_eld_spk_alloc_bits[i];
325 }
326
327 return spk_mask;
328}
329
Colin Ian King3013c752017-08-12 23:50:28 +0100330static void hdmi_codec_eld_chmap(struct hdmi_codec_priv *hcp)
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100331{
332 u8 spk_alloc;
333 unsigned long spk_mask;
334
335 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
336 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
337
338 /* Detect if only stereo supported, else return 8 channels mappings */
339 if ((spk_mask & ~(FL | FR)) && hcp->chmap_info->max_channels > 2)
340 hcp->chmap_info->chmap = hdmi_codec_8ch_chmaps;
341 else
342 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
343}
344
345static int hdmi_codec_get_ch_alloc_table_idx(struct hdmi_codec_priv *hcp,
346 unsigned char channels)
347{
348 int i;
349 u8 spk_alloc;
350 unsigned long spk_mask;
351 const struct hdmi_codec_cea_spk_alloc *cap = hdmi_codec_channel_alloc;
352
353 spk_alloc = drm_eld_get_spk_alloc(hcp->eld);
354 spk_mask = hdmi_codec_spk_mask_from_alloc(spk_alloc);
355
356 for (i = 0; i < ARRAY_SIZE(hdmi_codec_channel_alloc); i++, cap++) {
357 /* If spk_alloc == 0, HDMI is unplugged return stereo config*/
358 if (!spk_alloc && cap->ca_id == 0)
359 return i;
360 if (cap->n_ch != channels)
361 continue;
362 if (!(cap->mask == (spk_mask & cap->mask)))
363 continue;
364 return i;
365 }
366
367 return -EINVAL;
368}
369static int hdmi_codec_chmap_ctl_get(struct snd_kcontrol *kcontrol,
370 struct snd_ctl_elem_value *ucontrol)
371{
372 unsigned const char *map;
373 unsigned int i;
374 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
375 struct hdmi_codec_priv *hcp = info->private_data;
376
377 map = info->chmap[hcp->chmap_idx].map;
378
379 for (i = 0; i < info->max_channels; i++) {
380 if (hcp->chmap_idx == HDMI_CODEC_CHMAP_IDX_UNKNOWN)
381 ucontrol->value.integer.value[i] = 0;
382 else
383 ucontrol->value.integer.value[i] = map[i];
384 }
385
386 return 0;
387}
388
Jyri Sarha09184112016-03-31 16:36:00 +0300389static int hdmi_codec_startup(struct snd_pcm_substream *substream,
390 struct snd_soc_dai *dai)
391{
392 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Shengjiu Wang144f8362020-11-26 14:36:48 +0800393 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
Jyri Sarha09184112016-03-31 16:36:00 +0300394 int ret = 0;
395
Jerome Brunet14428422019-12-06 11:35:42 +0100396 mutex_lock(&hcp->lock);
397 if (hcp->busy) {
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200398 dev_err(dai->dev, "Only one simultaneous stream supported!\n");
Jerome Brunet14428422019-12-06 11:35:42 +0100399 mutex_unlock(&hcp->lock);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200400 return -EINVAL;
401 }
Jyri Sarha09184112016-03-31 16:36:00 +0300402
403 if (hcp->hcd.ops->audio_startup) {
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000404 ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200405 if (ret)
406 goto err;
Jyri Sarha09184112016-03-31 16:36:00 +0300407 }
408
Shengjiu Wang144f8362020-11-26 14:36:48 +0800409 if (tx && hcp->hcd.ops->get_eld) {
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000410 ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
411 hcp->eld, sizeof(hcp->eld));
Jerome Brunet14428422019-12-06 11:35:42 +0100412 if (ret)
413 goto err;
Jyri Sarha09184112016-03-31 16:36:00 +0300414
Jerome Brunet14428422019-12-06 11:35:42 +0100415 ret = snd_pcm_hw_constraint_eld(substream->runtime, hcp->eld);
416 if (ret)
417 goto err;
418
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100419 /* Select chmap supported */
420 hdmi_codec_eld_chmap(hcp);
Jyri Sarha09184112016-03-31 16:36:00 +0300421 }
Jerome Brunet14428422019-12-06 11:35:42 +0100422
423 hcp->busy = true;
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200424
425err:
Jerome Brunet14428422019-12-06 11:35:42 +0100426 mutex_unlock(&hcp->lock);
Jerome Brunet3fcf94e2019-05-06 11:58:13 +0200427 return ret;
Jyri Sarha09184112016-03-31 16:36:00 +0300428}
429
430static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
431 struct snd_soc_dai *dai)
432{
433 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
434
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100435 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000436 hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
Jyri Sarha09184112016-03-31 16:36:00 +0300437
Jerome Brunet14428422019-12-06 11:35:42 +0100438 mutex_lock(&hcp->lock);
439 hcp->busy = false;
440 mutex_unlock(&hcp->lock);
Jyri Sarha09184112016-03-31 16:36:00 +0300441}
442
443static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
444 struct snd_pcm_hw_params *params,
445 struct snd_soc_dai *dai)
446{
447 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Jerome Brunet0cf46102019-05-06 11:58:15 +0200448 struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
Jyri Sarha09184112016-03-31 16:36:00 +0300449 struct hdmi_codec_params hp = {
450 .iec = {
451 .status = { 0 },
452 .subcode = { 0 },
453 .pad = 0,
454 .dig_subframe = { 0 },
455 }
456 };
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100457 int ret, idx;
Jyri Sarha09184112016-03-31 16:36:00 +0300458
459 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
460 params_width(params), params_rate(params),
461 params_channels(params));
462
Jyri Sarha09184112016-03-31 16:36:00 +0300463 ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
464 sizeof(hp.iec.status));
465 if (ret < 0) {
466 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
467 ret);
468 return ret;
469 }
470
Jyri Sarha09184112016-03-31 16:36:00 +0300471 hdmi_audio_infoframe_init(&hp.cea);
472 hp.cea.channels = params_channels(params);
473 hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
474 hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
475 hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
476
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100477 /* Select a channel allocation that matches with ELD and pcm channels */
478 idx = hdmi_codec_get_ch_alloc_table_idx(hcp, hp.cea.channels);
479 if (idx < 0) {
480 dev_err(dai->dev, "Not able to map channels to speakers (%d)\n",
481 idx);
482 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
483 return idx;
484 }
485 hp.cea.channel_allocation = hdmi_codec_channel_alloc[idx].ca_id;
486 hcp->chmap_idx = hdmi_codec_channel_alloc[idx].ca_id;
487
Jyri Sarha09184112016-03-31 16:36:00 +0300488 hp.sample_width = params_width(params);
489 hp.sample_rate = params_rate(params);
490 hp.channels = params_channels(params);
491
Sia Jee Heng28785f52021-02-04 09:42:55 +0800492 cf->bit_fmt = params_format(params);
Kuninori Morimotoefc91942016-06-24 02:47:55 +0000493 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200494 cf, &hp);
Jyri Sarha09184112016-03-31 16:36:00 +0300495}
496
Jerome Brunet0cf46102019-05-06 11:58:15 +0200497static int hdmi_codec_i2s_set_fmt(struct snd_soc_dai *dai,
498 unsigned int fmt)
Jyri Sarha09184112016-03-31 16:36:00 +0300499{
Jerome Brunet0cf46102019-05-06 11:58:15 +0200500 struct hdmi_codec_daifmt *cf = dai->playback_dma_data;
Jyri Sarha09184112016-03-31 16:36:00 +0300501
Jerome Brunet0cf46102019-05-06 11:58:15 +0200502 /* Reset daifmt */
503 memset(cf, 0, sizeof(*cf));
Jyri Sarha09184112016-03-31 16:36:00 +0300504
Russell King2e95f982019-02-28 15:30:34 +0000505 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
506 case SND_SOC_DAIFMT_CBM_CFM:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200507 cf->bit_clk_master = 1;
508 cf->frame_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000509 break;
510 case SND_SOC_DAIFMT_CBS_CFM:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200511 cf->frame_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000512 break;
513 case SND_SOC_DAIFMT_CBM_CFS:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200514 cf->bit_clk_master = 1;
Russell King2e95f982019-02-28 15:30:34 +0000515 break;
516 case SND_SOC_DAIFMT_CBS_CFS:
517 break;
518 default:
519 return -EINVAL;
520 }
Jyri Sarha09184112016-03-31 16:36:00 +0300521
Russell King2e95f982019-02-28 15:30:34 +0000522 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
523 case SND_SOC_DAIFMT_NB_NF:
524 break;
525 case SND_SOC_DAIFMT_NB_IF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200526 cf->frame_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000527 break;
528 case SND_SOC_DAIFMT_IB_NF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200529 cf->bit_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000530 break;
531 case SND_SOC_DAIFMT_IB_IF:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200532 cf->frame_clk_inv = 1;
533 cf->bit_clk_inv = 1;
Russell King2e95f982019-02-28 15:30:34 +0000534 break;
535 }
536
537 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
538 case SND_SOC_DAIFMT_I2S:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200539 cf->fmt = HDMI_I2S;
Russell King2e95f982019-02-28 15:30:34 +0000540 break;
541 case SND_SOC_DAIFMT_DSP_A:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200542 cf->fmt = HDMI_DSP_A;
Russell King2e95f982019-02-28 15:30:34 +0000543 break;
544 case SND_SOC_DAIFMT_DSP_B:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200545 cf->fmt = HDMI_DSP_B;
Russell King2e95f982019-02-28 15:30:34 +0000546 break;
547 case SND_SOC_DAIFMT_RIGHT_J:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200548 cf->fmt = HDMI_RIGHT_J;
Russell King2e95f982019-02-28 15:30:34 +0000549 break;
550 case SND_SOC_DAIFMT_LEFT_J:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200551 cf->fmt = HDMI_LEFT_J;
Russell King2e95f982019-02-28 15:30:34 +0000552 break;
553 case SND_SOC_DAIFMT_AC97:
Jerome Brunet0cf46102019-05-06 11:58:15 +0200554 cf->fmt = HDMI_AC97;
Russell King2e95f982019-02-28 15:30:34 +0000555 break;
556 default:
557 dev_err(dai->dev, "Invalid DAI interface format\n");
558 return -EINVAL;
Jyri Sarha09184112016-03-31 16:36:00 +0300559 }
560
Russell King2e95f982019-02-28 15:30:34 +0000561 return 0;
Jyri Sarha09184112016-03-31 16:36:00 +0300562}
563
Kuninori Morimotod7897102020-07-09 10:55:45 +0900564static int hdmi_codec_mute(struct snd_soc_dai *dai, int mute, int direction)
Jyri Sarha09184112016-03-31 16:36:00 +0300565{
566 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
567
Kuninori Morimotod7897102020-07-09 10:55:45 +0900568 /*
569 * ignore if direction was CAPTURE
570 * and it had .no_capture_mute flag
571 * see
572 * snd_soc_dai_digital_mute()
573 */
574 if (hcp->hcd.ops->mute_stream &&
575 (direction == SNDRV_PCM_STREAM_PLAYBACK ||
576 !hcp->hcd.ops->no_capture_mute))
577 return hcp->hcd.ops->mute_stream(dai->dev->parent,
578 hcp->hcd.data,
579 mute, direction);
Jyri Sarha09184112016-03-31 16:36:00 +0300580
Kuninori Morimotoe07e49c2020-07-09 10:55:36 +0900581 return -ENOTSUPP;
Jyri Sarha09184112016-03-31 16:36:00 +0300582}
583
Jerome Brunet0cf46102019-05-06 11:58:15 +0200584static const struct snd_soc_dai_ops hdmi_codec_i2s_dai_ops = {
Jyri Sarha09184112016-03-31 16:36:00 +0300585 .startup = hdmi_codec_startup,
586 .shutdown = hdmi_codec_shutdown,
587 .hw_params = hdmi_codec_hw_params,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200588 .set_fmt = hdmi_codec_i2s_set_fmt,
Kuninori Morimotod7897102020-07-09 10:55:45 +0900589 .mute_stream = hdmi_codec_mute,
Jyri Sarha09184112016-03-31 16:36:00 +0300590};
591
Jerome Brunet0cf46102019-05-06 11:58:15 +0200592static const struct snd_soc_dai_ops hdmi_codec_spdif_dai_ops = {
593 .startup = hdmi_codec_startup,
594 .shutdown = hdmi_codec_shutdown,
595 .hw_params = hdmi_codec_hw_params,
Kuninori Morimotod7897102020-07-09 10:55:45 +0900596 .mute_stream = hdmi_codec_mute,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200597};
Jyri Sarha09184112016-03-31 16:36:00 +0300598
599#define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
600 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
601 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
602 SNDRV_PCM_RATE_192000)
603
604#define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
605 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
606 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
607 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
608
609/*
610 * This list is only for formats allowed on the I2S bus. So there is
611 * some formats listed that are not supported by HDMI interface. For
612 * instance allowing the 32-bit formats enables 24-precision with CPU
613 * DAIs that do not support 24-bit formats. If the extra formats cause
614 * problems, we should add the video side driver an option to disable
615 * them.
616 */
617#define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
618 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
619 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
620 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
Sia Jee Heng28785f52021-02-04 09:42:55 +0800621 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE |\
622 SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
Jyri Sarha09184112016-03-31 16:36:00 +0300623
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100624static int hdmi_codec_pcm_new(struct snd_soc_pcm_runtime *rtd,
625 struct snd_soc_dai *dai)
626{
627 struct snd_soc_dai_driver *drv = dai->driver;
628 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
Kuninori Morimotoecbb1b82017-07-06 00:39:29 +0000629 struct snd_kcontrol *kctl;
630 struct snd_kcontrol_new hdmi_eld_ctl = {
631 .access = SNDRV_CTL_ELEM_ACCESS_READ |
632 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
633 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
634 .name = "ELD",
635 .info = hdmi_eld_ctl_info,
636 .get = hdmi_eld_ctl_get,
637 .device = rtd->pcm->device,
638 };
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100639 int ret;
640
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100641 ret = snd_pcm_add_chmap_ctls(rtd->pcm, SNDRV_PCM_STREAM_PLAYBACK,
642 NULL, drv->playback.channels_max, 0,
643 &hcp->chmap_info);
644 if (ret < 0)
645 return ret;
646
647 /* override handlers */
648 hcp->chmap_info->private_data = hcp;
649 hcp->chmap_info->kctl->get = hdmi_codec_chmap_ctl_get;
650
651 /* default chmap supported is stereo */
652 hcp->chmap_info->chmap = hdmi_codec_stereo_chmaps;
653 hcp->chmap_idx = HDMI_CODEC_CHMAP_IDX_UNKNOWN;
654
Kuninori Morimotoecbb1b82017-07-06 00:39:29 +0000655 /* add ELD ctl with the device number corresponding to the PCM stream */
656 kctl = snd_ctl_new1(&hdmi_eld_ctl, dai->component);
657 if (!kctl)
658 return -ENOMEM;
659
660 return snd_ctl_add(rtd->card->snd_card, kctl);
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100661}
662
Russell Kingd30e23d2018-07-14 16:01:06 +0100663static int hdmi_dai_probe(struct snd_soc_dai *dai)
664{
665 struct snd_soc_dapm_context *dapm;
Jerome Brunet0cf46102019-05-06 11:58:15 +0200666 struct hdmi_codec_daifmt *daifmt;
Shengjiu Wang144f8362020-11-26 14:36:48 +0800667 struct snd_soc_dapm_route route[] = {
668 {
669 .sink = "TX",
670 .source = dai->driver->playback.stream_name,
671 },
672 {
673 .sink = dai->driver->capture.stream_name,
674 .source = "RX",
675 },
Russell Kingd30e23d2018-07-14 16:01:06 +0100676 };
Jerome Brunet0cf46102019-05-06 11:58:15 +0200677 int ret;
Russell Kingd30e23d2018-07-14 16:01:06 +0100678
679 dapm = snd_soc_component_get_dapm(dai->component);
Shengjiu Wang144f8362020-11-26 14:36:48 +0800680 ret = snd_soc_dapm_add_routes(dapm, route, 2);
Jerome Brunet0cf46102019-05-06 11:58:15 +0200681 if (ret)
682 return ret;
Russell Kingd30e23d2018-07-14 16:01:06 +0100683
Jerome Brunet0cf46102019-05-06 11:58:15 +0200684 daifmt = kzalloc(sizeof(*daifmt), GFP_KERNEL);
685 if (!daifmt)
686 return -ENOMEM;
687
688 dai->playback_dma_data = daifmt;
689 return 0;
690}
691
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800692static void hdmi_codec_jack_report(struct hdmi_codec_priv *hcp,
693 unsigned int jack_status)
694{
695 if (hcp->jack && jack_status != hcp->jack_status) {
696 snd_soc_jack_report(hcp->jack, jack_status, SND_JACK_LINEOUT);
697 hcp->jack_status = jack_status;
698 }
699}
700
701static void plugged_cb(struct device *dev, bool plugged)
702{
703 struct hdmi_codec_priv *hcp = dev_get_drvdata(dev);
704
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800705 if (plugged) {
706 if (hcp->hcd.ops->get_eld) {
707 hcp->hcd.ops->get_eld(dev->parent, hcp->hcd.data,
708 hcp->eld, sizeof(hcp->eld));
709 }
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800710 hdmi_codec_jack_report(hcp, SND_JACK_LINEOUT);
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800711 } else {
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800712 hdmi_codec_jack_report(hcp, 0);
Cheng-Yi Chiang25ce4f22020-11-18 12:38:52 +0800713 memset(hcp->eld, 0, sizeof(hcp->eld));
714 }
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800715}
716
Cheng-Yi Chiang55c5cc632020-09-22 14:23:16 +0800717static int hdmi_codec_set_jack(struct snd_soc_component *component,
718 struct snd_soc_jack *jack,
719 void *data)
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800720{
721 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
Stephan Gerhold2a0435d2021-01-07 17:51:31 +0100722 int ret = -ENOTSUPP;
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800723
724 if (hcp->hcd.ops->hook_plugged_cb) {
725 hcp->jack = jack;
726 ret = hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
727 hcp->hcd.data,
728 plugged_cb,
729 component->dev);
730 if (ret)
731 hcp->jack = NULL;
732 }
733 return ret;
734}
Cheng-Yi Chiang6fa59632019-07-17 16:33:23 +0800735
Jerome Brunet0cf46102019-05-06 11:58:15 +0200736static int hdmi_dai_spdif_probe(struct snd_soc_dai *dai)
737{
Pierre-Louis Bossart3c011ef2021-03-12 12:22:33 -0600738 struct hdmi_codec_daifmt *cf;
Jerome Brunet0cf46102019-05-06 11:58:15 +0200739 int ret;
740
741 ret = hdmi_dai_probe(dai);
742 if (ret)
743 return ret;
744
745 cf = dai->playback_dma_data;
746 cf->fmt = HDMI_SPDIF;
747
748 return 0;
749}
750
751static int hdmi_codec_dai_remove(struct snd_soc_dai *dai)
752{
753 kfree(dai->playback_dma_data);
754 return 0;
Russell Kingd30e23d2018-07-14 16:01:06 +0100755}
756
Bhumika Goyal6e37f932017-08-16 22:45:09 +0530757static const struct snd_soc_dai_driver hdmi_i2s_dai = {
Kuninori Morimoto24069b52017-05-18 01:40:02 +0000758 .name = "i2s-hifi",
Jyri Sarha09184112016-03-31 16:36:00 +0300759 .id = DAI_ID_I2S,
Russell Kingd30e23d2018-07-14 16:01:06 +0100760 .probe = hdmi_dai_probe,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200761 .remove = hdmi_codec_dai_remove,
Jyri Sarha09184112016-03-31 16:36:00 +0300762 .playback = {
Jeffy Chen943fa022017-08-12 08:40:38 +0800763 .stream_name = "I2S Playback",
Jyri Sarha09184112016-03-31 16:36:00 +0300764 .channels_min = 2,
765 .channels_max = 8,
766 .rates = HDMI_RATES,
767 .formats = I2S_FORMATS,
768 .sig_bits = 24,
769 },
Shengjiu Wang144f8362020-11-26 14:36:48 +0800770 .capture = {
771 .stream_name = "Capture",
772 .channels_min = 2,
773 .channels_max = 8,
774 .rates = HDMI_RATES,
775 .formats = I2S_FORMATS,
776 .sig_bits = 24,
777 },
Jerome Brunet0cf46102019-05-06 11:58:15 +0200778 .ops = &hdmi_codec_i2s_dai_ops,
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100779 .pcm_new = hdmi_codec_pcm_new,
Jyri Sarha09184112016-03-31 16:36:00 +0300780};
781
782static const struct snd_soc_dai_driver hdmi_spdif_dai = {
Kuninori Morimoto24069b52017-05-18 01:40:02 +0000783 .name = "spdif-hifi",
Jyri Sarha09184112016-03-31 16:36:00 +0300784 .id = DAI_ID_SPDIF,
Jerome Brunet0cf46102019-05-06 11:58:15 +0200785 .probe = hdmi_dai_spdif_probe,
786 .remove = hdmi_codec_dai_remove,
Jyri Sarha09184112016-03-31 16:36:00 +0300787 .playback = {
Jeffy Chen943fa022017-08-12 08:40:38 +0800788 .stream_name = "SPDIF Playback",
Jyri Sarha09184112016-03-31 16:36:00 +0300789 .channels_min = 2,
790 .channels_max = 2,
791 .rates = HDMI_RATES,
792 .formats = SPDIF_FORMATS,
793 },
Shengjiu Wang144f8362020-11-26 14:36:48 +0800794 .capture = {
795 .stream_name = "Capture",
796 .channels_min = 2,
797 .channels_max = 2,
798 .rates = HDMI_RATES,
799 .formats = SPDIF_FORMATS,
800 },
Jerome Brunet0cf46102019-05-06 11:58:15 +0200801 .ops = &hdmi_codec_spdif_dai_ops,
Arnaud Pouliquencd6111b2017-01-03 16:52:52 +0100802 .pcm_new = hdmi_codec_pcm_new,
Jyri Sarha09184112016-03-31 16:36:00 +0300803};
804
Kuninori Morimoto96203fb2017-05-18 01:40:20 +0000805static int hdmi_of_xlate_dai_id(struct snd_soc_component *component,
806 struct device_node *endpoint)
807{
808 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
809 int ret = -ENOTSUPP; /* see snd_soc_get_dai_id() */
810
811 if (hcp->hcd.ops->get_dai_id)
812 ret = hcp->hcd.ops->get_dai_id(component, endpoint);
813
814 return ret;
815}
816
Tzung-Bi Shih4aadf4b2020-02-17 11:16:52 +0800817static void hdmi_remove(struct snd_soc_component *component)
818{
819 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
820
821 if (hcp->hcd.ops->hook_plugged_cb)
822 hcp->hcd.ops->hook_plugged_cb(component->dev->parent,
823 hcp->hcd.data, NULL, NULL);
824}
825
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000826static const struct snd_soc_component_driver hdmi_driver = {
Tzung-Bi Shih4aadf4b2020-02-17 11:16:52 +0800827 .remove = hdmi_remove,
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000828 .dapm_widgets = hdmi_widgets,
829 .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000830 .of_xlate_dai_id = hdmi_of_xlate_dai_id,
831 .idle_bias_on = 1,
832 .use_pmdown_time = 1,
833 .endianness = 1,
834 .non_legacy_dai_naming = 1,
Cheng-Yi Chiang55c5cc632020-09-22 14:23:16 +0800835 .set_jack = hdmi_codec_set_jack,
Jyri Sarha09184112016-03-31 16:36:00 +0300836};
837
838static int hdmi_codec_probe(struct platform_device *pdev)
839{
840 struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
Jerome Brunet1de005d2019-05-06 11:58:14 +0200841 struct snd_soc_dai_driver *daidrv;
Jyri Sarha09184112016-03-31 16:36:00 +0300842 struct device *dev = &pdev->dev;
843 struct hdmi_codec_priv *hcp;
844 int dai_count, i = 0;
845 int ret;
846
Jyri Sarha09184112016-03-31 16:36:00 +0300847 if (!hcd) {
Colin Ian King7b6531c2019-04-17 17:26:28 +0100848 dev_err(dev, "%s: No platform data\n", __func__);
Jyri Sarha09184112016-03-31 16:36:00 +0300849 return -EINVAL;
850 }
851
852 dai_count = hcd->i2s + hcd->spdif;
853 if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
854 !hcd->ops->audio_shutdown) {
855 dev_err(dev, "%s: Invalid parameters\n", __func__);
856 return -EINVAL;
857 }
858
859 hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
860 if (!hcp)
861 return -ENOMEM;
862
863 hcp->hcd = *hcd;
Jerome Brunet14428422019-12-06 11:35:42 +0100864 mutex_init(&hcp->lock);
865
Jerome Brunet1de005d2019-05-06 11:58:14 +0200866 daidrv = devm_kcalloc(dev, dai_count, sizeof(*daidrv), GFP_KERNEL);
867 if (!daidrv)
Jyri Sarha09184112016-03-31 16:36:00 +0300868 return -ENOMEM;
869
870 if (hcd->i2s) {
Jerome Brunet1de005d2019-05-06 11:58:14 +0200871 daidrv[i] = hdmi_i2s_dai;
872 daidrv[i].playback.channels_max = hcd->max_i2s_channels;
Jyri Sarha09184112016-03-31 16:36:00 +0300873 i++;
874 }
875
Jerome Brunet0cf46102019-05-06 11:58:15 +0200876 if (hcd->spdif)
Jerome Brunet1de005d2019-05-06 11:58:14 +0200877 daidrv[i] = hdmi_spdif_dai;
Jyri Sarha09184112016-03-31 16:36:00 +0300878
Russell King0ce23d62019-01-17 17:32:05 +0000879 dev_set_drvdata(dev, hcp);
880
Jerome Brunet1de005d2019-05-06 11:58:14 +0200881 ret = devm_snd_soc_register_component(dev, &hdmi_driver, daidrv,
882 dai_count);
Jyri Sarha09184112016-03-31 16:36:00 +0300883 if (ret) {
Kuninori Morimotod20aa602018-01-29 04:34:21 +0000884 dev_err(dev, "%s: snd_soc_register_component() failed (%d)\n",
Jyri Sarha09184112016-03-31 16:36:00 +0300885 __func__, ret);
886 return ret;
887 }
Jyri Sarha09184112016-03-31 16:36:00 +0300888 return 0;
889}
890
Jyri Sarha09184112016-03-31 16:36:00 +0300891static struct platform_driver hdmi_codec_driver = {
892 .driver = {
893 .name = HDMI_CODEC_DRV_NAME,
894 },
895 .probe = hdmi_codec_probe,
Jyri Sarha09184112016-03-31 16:36:00 +0300896};
897
898module_platform_driver(hdmi_codec_driver);
899
900MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
901MODULE_DESCRIPTION("HDMI Audio Codec Driver");
902MODULE_LICENSE("GPL");
903MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);