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>
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.
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.
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/pcm_drm_eld.h>
22 #include <sound/hdmi-codec.h>
23 #include <sound/pcm_iec958.h>
25 #include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
29 struct list_head list;
32 #define pos_to_hdmi_device(pos) container_of((pos), struct hdmi_device, list)
33 LIST_HEAD(hdmi_device_list);
35 #define DAI_NAME_SIZE 16
36 struct hdmi_codec_priv {
37 struct hdmi_codec_pdata hcd;
38 struct snd_soc_dai_driver *daidrv;
39 struct hdmi_codec_daifmt daifmt[2];
40 struct mutex current_stream_lock;
41 struct snd_pcm_substream *current_stream;
42 struct snd_pcm_hw_constraint_list ratec;
43 uint8_t eld[MAX_ELD_BYTES];
46 static const struct snd_soc_dapm_widget hdmi_widgets[] = {
47 SND_SOC_DAPM_OUTPUT("TX"),
50 static const struct snd_soc_dapm_route hdmi_routes[] = {
51 { "TX", NULL, "Playback" },
59 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
60 struct snd_ctl_elem_info *uinfo)
62 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
63 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
65 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
66 uinfo->count = sizeof(hcp->eld);
71 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
72 struct snd_ctl_elem_value *ucontrol)
74 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
75 struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
77 memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
82 static const struct snd_kcontrol_new hdmi_controls[] = {
84 .access = SNDRV_CTL_ELEM_ACCESS_READ |
85 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
86 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
88 .info = hdmi_eld_ctl_info,
89 .get = hdmi_eld_ctl_get,
93 static int hdmi_codec_new_stream(struct snd_pcm_substream *substream,
94 struct snd_soc_dai *dai)
96 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
99 mutex_lock(&hcp->current_stream_lock);
100 if (!hcp->current_stream) {
101 hcp->current_stream = substream;
102 } else if (hcp->current_stream != substream) {
103 dev_err(dai->dev, "Only one simultaneous stream supported!\n");
106 mutex_unlock(&hcp->current_stream_lock);
111 static int hdmi_codec_startup(struct snd_pcm_substream *substream,
112 struct snd_soc_dai *dai)
114 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
117 dev_dbg(dai->dev, "%s()\n", __func__);
119 ret = hdmi_codec_new_stream(substream, dai);
123 if (hcp->hcd.ops->audio_startup) {
124 ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
126 mutex_lock(&hcp->current_stream_lock);
127 hcp->current_stream = NULL;
128 mutex_unlock(&hcp->current_stream_lock);
133 if (hcp->hcd.ops->get_eld) {
134 ret = hcp->hcd.ops->get_eld(dai->dev->parent, hcp->hcd.data,
135 hcp->eld, sizeof(hcp->eld));
138 ret = snd_pcm_hw_constraint_eld(substream->runtime,
141 mutex_lock(&hcp->current_stream_lock);
142 hcp->current_stream = NULL;
143 mutex_unlock(&hcp->current_stream_lock);
151 static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
152 struct snd_soc_dai *dai)
154 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
156 dev_dbg(dai->dev, "%s()\n", __func__);
158 WARN_ON(hcp->current_stream != substream);
160 hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
162 mutex_lock(&hcp->current_stream_lock);
163 hcp->current_stream = NULL;
164 mutex_unlock(&hcp->current_stream_lock);
167 static int hdmi_codec_hw_params(struct snd_pcm_substream *substream,
168 struct snd_pcm_hw_params *params,
169 struct snd_soc_dai *dai)
171 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
172 struct hdmi_codec_params hp = {
177 .dig_subframe = { 0 },
182 dev_dbg(dai->dev, "%s() width %d rate %d channels %d\n", __func__,
183 params_width(params), params_rate(params),
184 params_channels(params));
186 if (params_width(params) > 24)
189 ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
190 sizeof(hp.iec.status));
192 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
197 ret = hdmi_codec_new_stream(substream, dai);
201 hdmi_audio_infoframe_init(&hp.cea);
202 hp.cea.channels = params_channels(params);
203 hp.cea.coding_type = HDMI_AUDIO_CODING_TYPE_STREAM;
204 hp.cea.sample_size = HDMI_AUDIO_SAMPLE_SIZE_STREAM;
205 hp.cea.sample_frequency = HDMI_AUDIO_SAMPLE_FREQUENCY_STREAM;
207 hp.sample_width = params_width(params);
208 hp.sample_rate = params_rate(params);
209 hp.channels = params_channels(params);
211 return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
212 &hcp->daifmt[dai->id], &hp);
215 static int hdmi_codec_set_fmt(struct snd_soc_dai *dai,
218 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
219 struct hdmi_codec_daifmt cf = { 0 };
222 dev_dbg(dai->dev, "%s()\n", __func__);
224 if (dai->id == DAI_ID_SPDIF) {
227 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
228 case SND_SOC_DAIFMT_CBM_CFM:
229 cf.bit_clk_master = 1;
230 cf.frame_clk_master = 1;
232 case SND_SOC_DAIFMT_CBS_CFM:
233 cf.frame_clk_master = 1;
235 case SND_SOC_DAIFMT_CBM_CFS:
236 cf.bit_clk_master = 1;
238 case SND_SOC_DAIFMT_CBS_CFS:
244 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
245 case SND_SOC_DAIFMT_NB_NF:
247 case SND_SOC_DAIFMT_NB_IF:
248 cf.frame_clk_inv = 1;
250 case SND_SOC_DAIFMT_IB_NF:
253 case SND_SOC_DAIFMT_IB_IF:
254 cf.frame_clk_inv = 1;
259 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
260 case SND_SOC_DAIFMT_I2S:
263 case SND_SOC_DAIFMT_DSP_A:
266 case SND_SOC_DAIFMT_DSP_B:
269 case SND_SOC_DAIFMT_RIGHT_J:
270 cf.fmt = HDMI_RIGHT_J;
272 case SND_SOC_DAIFMT_LEFT_J:
273 cf.fmt = HDMI_LEFT_J;
275 case SND_SOC_DAIFMT_AC97:
279 dev_err(dai->dev, "Invalid DAI interface format\n");
284 hcp->daifmt[dai->id] = cf;
289 static int hdmi_codec_digital_mute(struct snd_soc_dai *dai, int mute)
291 struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
293 dev_dbg(dai->dev, "%s()\n", __func__);
295 if (hcp->hcd.ops->digital_mute)
296 return hcp->hcd.ops->digital_mute(dai->dev->parent,
297 hcp->hcd.data, mute);
302 static const struct snd_soc_dai_ops hdmi_dai_ops = {
303 .startup = hdmi_codec_startup,
304 .shutdown = hdmi_codec_shutdown,
305 .hw_params = hdmi_codec_hw_params,
306 .set_fmt = hdmi_codec_set_fmt,
307 .digital_mute = hdmi_codec_digital_mute,
311 #define HDMI_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |\
312 SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |\
313 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
314 SNDRV_PCM_RATE_192000)
316 #define SPDIF_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
317 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
318 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
319 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE)
322 * This list is only for formats allowed on the I2S bus. So there is
323 * some formats listed that are not supported by HDMI interface. For
324 * instance allowing the 32-bit formats enables 24-precision with CPU
325 * DAIs that do not support 24-bit formats. If the extra formats cause
326 * problems, we should add the video side driver an option to disable
329 #define I2S_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |\
330 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S20_3BE |\
331 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_3BE |\
332 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S24_BE |\
333 SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S32_BE)
335 static struct snd_soc_dai_driver hdmi_i2s_dai = {
338 .stream_name = "Playback",
342 .formats = I2S_FORMATS,
345 .ops = &hdmi_dai_ops,
348 static const struct snd_soc_dai_driver hdmi_spdif_dai = {
351 .stream_name = "Playback",
355 .formats = SPDIF_FORMATS,
357 .ops = &hdmi_dai_ops,
360 static char hdmi_dai_name[][DAI_NAME_SIZE] = {
367 static int hdmi_of_xlate_dai_name(struct snd_soc_component *component,
368 struct of_phandle_args *args,
369 const char **dai_name)
373 if (args->args_count)
378 if (id < ARRAY_SIZE(hdmi_dai_name)) {
379 *dai_name = hdmi_dai_name[id];
386 static struct snd_soc_codec_driver hdmi_codec = {
387 .component_driver = {
388 .controls = hdmi_controls,
389 .num_controls = ARRAY_SIZE(hdmi_controls),
390 .dapm_widgets = hdmi_widgets,
391 .num_dapm_widgets = ARRAY_SIZE(hdmi_widgets),
392 .dapm_routes = hdmi_routes,
393 .num_dapm_routes = ARRAY_SIZE(hdmi_routes),
394 .of_xlate_dai_name = hdmi_of_xlate_dai_name,
398 static int hdmi_codec_probe(struct platform_device *pdev)
400 struct hdmi_codec_pdata *hcd = pdev->dev.platform_data;
401 struct device *dev = &pdev->dev;
402 struct hdmi_codec_priv *hcp;
403 struct hdmi_device *hd;
404 struct list_head *pos;
405 int dai_count, i = 0;
408 dev_dbg(dev, "%s()\n", __func__);
411 dev_err(dev, "%s: No plalform data\n", __func__);
415 dai_count = hcd->i2s + hcd->spdif;
416 if (dai_count < 1 || !hcd->ops || !hcd->ops->hw_params ||
417 !hcd->ops->audio_shutdown) {
418 dev_err(dev, "%s: Invalid parameters\n", __func__);
422 hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
427 list_for_each(pos, &hdmi_device_list) {
428 struct hdmi_device *tmp = pos_to_hdmi_device(pos);
430 if (tmp->dev == dev->parent) {
437 hd = devm_kzalloc(dev, sizeof(*hd), GFP_KERNEL);
441 hd->dev = dev->parent;
443 list_add_tail(&hd->list, &hdmi_device_list);
446 if (hd->cnt >= ARRAY_SIZE(hdmi_dai_name)) {
447 dev_err(dev, "too many hdmi codec are deteced\n");
452 mutex_init(&hcp->current_stream_lock);
454 hcp->daidrv = devm_kzalloc(dev, dai_count * sizeof(*hcp->daidrv),
460 hcp->daidrv[i] = hdmi_i2s_dai;
461 hcp->daidrv[i].playback.channels_max =
462 hcd->max_i2s_channels;
463 hcp->daidrv[i].name = hdmi_dai_name[hd->cnt++];
468 hcp->daidrv[i] = hdmi_spdif_dai;
469 hcp->daidrv[i].name = hdmi_dai_name[hd->cnt++];
472 ret = snd_soc_register_codec(dev, &hdmi_codec, hcp->daidrv,
475 dev_err(dev, "%s: snd_soc_register_codec() failed (%d)\n",
480 dev_set_drvdata(dev, hcp);
484 static int hdmi_codec_remove(struct platform_device *pdev)
486 snd_soc_unregister_codec(&pdev->dev);
490 static struct platform_driver hdmi_codec_driver = {
492 .name = HDMI_CODEC_DRV_NAME,
494 .probe = hdmi_codec_probe,
495 .remove = hdmi_codec_remove,
498 module_platform_driver(hdmi_codec_driver);
500 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
501 MODULE_DESCRIPTION("HDMI Audio Codec Driver");
502 MODULE_LICENSE("GPL");
503 MODULE_ALIAS("platform:" HDMI_CODEC_DRV_NAME);