GNU Linux-libre 4.9.309-gnu1
[releases.git] / sound / soc / codecs / hdmi-codec.c
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/pcm_drm_eld.h>
22 #include <sound/hdmi-codec.h>
23 #include <sound/pcm_iec958.h>
24
25 #include <drm/drm_crtc.h> /* This is only to get MAX_ELD_BYTES */
26
27 struct hdmi_device {
28         struct device *dev;
29         struct list_head list;
30         int cnt;
31 };
32 #define pos_to_hdmi_device(pos) container_of((pos), struct hdmi_device, list)
33 LIST_HEAD(hdmi_device_list);
34
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];
44 };
45
46 static const struct snd_soc_dapm_widget hdmi_widgets[] = {
47         SND_SOC_DAPM_OUTPUT("TX"),
48 };
49
50 static const struct snd_soc_dapm_route hdmi_routes[] = {
51         { "TX", NULL, "Playback" },
52 };
53
54 enum {
55         DAI_ID_I2S = 0,
56         DAI_ID_SPDIF,
57 };
58
59 static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
60                              struct snd_ctl_elem_info *uinfo)
61 {
62         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
63         struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
64
65         uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
66         uinfo->count = sizeof(hcp->eld);
67
68         return 0;
69 }
70
71 static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
72                             struct snd_ctl_elem_value *ucontrol)
73 {
74         struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
75         struct hdmi_codec_priv *hcp = snd_soc_component_get_drvdata(component);
76
77         memcpy(ucontrol->value.bytes.data, hcp->eld, sizeof(hcp->eld));
78
79         return 0;
80 }
81
82 static const struct snd_kcontrol_new hdmi_controls[] = {
83         {
84                 .access = SNDRV_CTL_ELEM_ACCESS_READ |
85                           SNDRV_CTL_ELEM_ACCESS_VOLATILE,
86                 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
87                 .name = "ELD",
88                 .info = hdmi_eld_ctl_info,
89                 .get = hdmi_eld_ctl_get,
90         },
91 };
92
93 static int hdmi_codec_new_stream(struct snd_pcm_substream *substream,
94                                  struct snd_soc_dai *dai)
95 {
96         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
97         int ret = 0;
98
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");
104                 ret = -EINVAL;
105         }
106         mutex_unlock(&hcp->current_stream_lock);
107
108         return ret;
109 }
110
111 static int hdmi_codec_startup(struct snd_pcm_substream *substream,
112                               struct snd_soc_dai *dai)
113 {
114         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
115         int ret = 0;
116
117         dev_dbg(dai->dev, "%s()\n", __func__);
118
119         ret = hdmi_codec_new_stream(substream, dai);
120         if (ret)
121                 return ret;
122
123         if (hcp->hcd.ops->audio_startup) {
124                 ret = hcp->hcd.ops->audio_startup(dai->dev->parent, hcp->hcd.data);
125                 if (ret) {
126                         mutex_lock(&hcp->current_stream_lock);
127                         hcp->current_stream = NULL;
128                         mutex_unlock(&hcp->current_stream_lock);
129                         return ret;
130                 }
131         }
132
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));
136
137                 if (!ret) {
138                         ret = snd_pcm_hw_constraint_eld(substream->runtime,
139                                                         hcp->eld);
140                         if (ret) {
141                                 mutex_lock(&hcp->current_stream_lock);
142                                 hcp->current_stream = NULL;
143                                 mutex_unlock(&hcp->current_stream_lock);
144                                 return ret;
145                         }
146                 }
147         }
148         return 0;
149 }
150
151 static void hdmi_codec_shutdown(struct snd_pcm_substream *substream,
152                                 struct snd_soc_dai *dai)
153 {
154         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
155
156         dev_dbg(dai->dev, "%s()\n", __func__);
157
158         WARN_ON(hcp->current_stream != substream);
159
160         hcp->hcd.ops->audio_shutdown(dai->dev->parent, hcp->hcd.data);
161
162         mutex_lock(&hcp->current_stream_lock);
163         hcp->current_stream = NULL;
164         mutex_unlock(&hcp->current_stream_lock);
165 }
166
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)
170 {
171         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
172         struct hdmi_codec_params hp = {
173                 .iec = {
174                         .status = { 0 },
175                         .subcode = { 0 },
176                         .pad = 0,
177                         .dig_subframe = { 0 },
178                 }
179         };
180         int ret;
181
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));
185
186         if (params_width(params) > 24)
187                 params->msbits = 24;
188
189         ret = snd_pcm_create_iec958_consumer_hw_params(params, hp.iec.status,
190                                                        sizeof(hp.iec.status));
191         if (ret < 0) {
192                 dev_err(dai->dev, "Creating IEC958 channel status failed %d\n",
193                         ret);
194                 return ret;
195         }
196
197         ret = hdmi_codec_new_stream(substream, dai);
198         if (ret)
199                 return ret;
200
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;
206
207         hp.sample_width = params_width(params);
208         hp.sample_rate = params_rate(params);
209         hp.channels = params_channels(params);
210
211         return hcp->hcd.ops->hw_params(dai->dev->parent, hcp->hcd.data,
212                                        &hcp->daifmt[dai->id], &hp);
213 }
214
215 static int hdmi_codec_set_fmt(struct snd_soc_dai *dai,
216                               unsigned int fmt)
217 {
218         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
219         struct hdmi_codec_daifmt cf = { 0 };
220         int ret = 0;
221
222         dev_dbg(dai->dev, "%s()\n", __func__);
223
224         if (dai->id == DAI_ID_SPDIF) {
225                 cf.fmt = HDMI_SPDIF;
226         } else {
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;
231                         break;
232                 case SND_SOC_DAIFMT_CBS_CFM:
233                         cf.frame_clk_master = 1;
234                         break;
235                 case SND_SOC_DAIFMT_CBM_CFS:
236                         cf.bit_clk_master = 1;
237                         break;
238                 case SND_SOC_DAIFMT_CBS_CFS:
239                         break;
240                 default:
241                         return -EINVAL;
242                 }
243
244                 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
245                 case SND_SOC_DAIFMT_NB_NF:
246                         break;
247                 case SND_SOC_DAIFMT_NB_IF:
248                         cf.frame_clk_inv = 1;
249                         break;
250                 case SND_SOC_DAIFMT_IB_NF:
251                         cf.bit_clk_inv = 1;
252                         break;
253                 case SND_SOC_DAIFMT_IB_IF:
254                         cf.frame_clk_inv = 1;
255                         cf.bit_clk_inv = 1;
256                         break;
257                 }
258
259                 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
260                 case SND_SOC_DAIFMT_I2S:
261                         cf.fmt = HDMI_I2S;
262                         break;
263                 case SND_SOC_DAIFMT_DSP_A:
264                         cf.fmt = HDMI_DSP_A;
265                         break;
266                 case SND_SOC_DAIFMT_DSP_B:
267                         cf.fmt = HDMI_DSP_B;
268                         break;
269                 case SND_SOC_DAIFMT_RIGHT_J:
270                         cf.fmt = HDMI_RIGHT_J;
271                         break;
272                 case SND_SOC_DAIFMT_LEFT_J:
273                         cf.fmt = HDMI_LEFT_J;
274                         break;
275                 case SND_SOC_DAIFMT_AC97:
276                         cf.fmt = HDMI_AC97;
277                         break;
278                 default:
279                         dev_err(dai->dev, "Invalid DAI interface format\n");
280                         return -EINVAL;
281                 }
282         }
283
284         hcp->daifmt[dai->id] = cf;
285
286         return ret;
287 }
288
289 static int hdmi_codec_digital_mute(struct snd_soc_dai *dai, int mute)
290 {
291         struct hdmi_codec_priv *hcp = snd_soc_dai_get_drvdata(dai);
292
293         dev_dbg(dai->dev, "%s()\n", __func__);
294
295         if (hcp->hcd.ops->digital_mute)
296                 return hcp->hcd.ops->digital_mute(dai->dev->parent,
297                                                   hcp->hcd.data, mute);
298
299         return 0;
300 }
301
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,
308 };
309
310
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)
315
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)
320
321 /*
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
327  * them.
328  */
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)
334
335 static struct snd_soc_dai_driver hdmi_i2s_dai = {
336         .id = DAI_ID_I2S,
337         .playback = {
338                 .stream_name = "Playback",
339                 .channels_min = 2,
340                 .channels_max = 8,
341                 .rates = HDMI_RATES,
342                 .formats = I2S_FORMATS,
343                 .sig_bits = 24,
344         },
345         .ops = &hdmi_dai_ops,
346 };
347
348 static const struct snd_soc_dai_driver hdmi_spdif_dai = {
349         .id = DAI_ID_SPDIF,
350         .playback = {
351                 .stream_name = "Playback",
352                 .channels_min = 2,
353                 .channels_max = 2,
354                 .rates = HDMI_RATES,
355                 .formats = SPDIF_FORMATS,
356         },
357         .ops = &hdmi_dai_ops,
358 };
359
360 static char hdmi_dai_name[][DAI_NAME_SIZE] = {
361         "hdmi-hifi.0",
362         "hdmi-hifi.1",
363         "hdmi-hifi.2",
364         "hdmi-hifi.3",
365 };
366
367 static int hdmi_of_xlate_dai_name(struct snd_soc_component *component,
368                                   struct of_phandle_args *args,
369                                   const char **dai_name)
370 {
371         int id;
372
373         if (args->args_count)
374                 id = args->args[0];
375         else
376                 id = 0;
377
378         if (id < ARRAY_SIZE(hdmi_dai_name)) {
379                 *dai_name = hdmi_dai_name[id];
380                 return 0;
381         }
382
383         return -EAGAIN;
384 }
385
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,
395         },
396 };
397
398 static int hdmi_codec_probe(struct platform_device *pdev)
399 {
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;
406         int ret;
407
408         dev_dbg(dev, "%s()\n", __func__);
409
410         if (!hcd) {
411                 dev_err(dev, "%s: No plalform data\n", __func__);
412                 return -EINVAL;
413         }
414
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__);
419                 return -EINVAL;
420         }
421
422         hcp = devm_kzalloc(dev, sizeof(*hcp), GFP_KERNEL);
423         if (!hcp)
424                 return -ENOMEM;
425
426         hd = NULL;
427         list_for_each(pos, &hdmi_device_list) {
428                 struct hdmi_device *tmp = pos_to_hdmi_device(pos);
429
430                 if (tmp->dev == dev->parent) {
431                         hd = tmp;
432                         break;
433                 }
434         }
435
436         if (!hd) {
437                 hd = devm_kzalloc(dev, sizeof(*hd), GFP_KERNEL);
438                 if (!hd)
439                         return -ENOMEM;
440
441                 hd->dev = dev->parent;
442
443                 list_add_tail(&hd->list, &hdmi_device_list);
444         }
445
446         if (hd->cnt >= ARRAY_SIZE(hdmi_dai_name)) {
447                 dev_err(dev, "too many hdmi codec are deteced\n");
448                 return -EINVAL;
449         }
450
451         hcp->hcd = *hcd;
452         mutex_init(&hcp->current_stream_lock);
453
454         hcp->daidrv = devm_kzalloc(dev, dai_count * sizeof(*hcp->daidrv),
455                                    GFP_KERNEL);
456         if (!hcp->daidrv)
457                 return -ENOMEM;
458
459         if (hcd->i2s) {
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++];
464                 i++;
465         }
466
467         if (hcd->spdif) {
468                 hcp->daidrv[i] = hdmi_spdif_dai;
469                 hcp->daidrv[i].name = hdmi_dai_name[hd->cnt++];
470         }
471
472         ret = snd_soc_register_codec(dev, &hdmi_codec, hcp->daidrv,
473                                      dai_count);
474         if (ret) {
475                 dev_err(dev, "%s: snd_soc_register_codec() failed (%d)\n",
476                         __func__, ret);
477                 return ret;
478         }
479
480         dev_set_drvdata(dev, hcp);
481         return 0;
482 }
483
484 static int hdmi_codec_remove(struct platform_device *pdev)
485 {
486         snd_soc_unregister_codec(&pdev->dev);
487         return 0;
488 }
489
490 static struct platform_driver hdmi_codec_driver = {
491         .driver = {
492                 .name = HDMI_CODEC_DRV_NAME,
493         },
494         .probe = hdmi_codec_probe,
495         .remove = hdmi_codec_remove,
496 };
497
498 module_platform_driver(hdmi_codec_driver);
499
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);