Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * omap-hdmi-audio.c -- OMAP4+ DSS HDMI audio support library
0004  *
0005  * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com
0006  *
0007  * Author: Jyri Sarha <jsarha@ti.com>
0008  */
0009 
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/err.h>
0013 #include <linux/string.h>
0014 #include <linux/platform_device.h>
0015 #include <sound/soc.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/dmaengine_pcm.h>
0018 #include <uapi/sound/asound.h>
0019 #include <sound/asoundef.h>
0020 #include <sound/omap-hdmi-audio.h>
0021 
0022 #include "sdma-pcm.h"
0023 
0024 #define DRV_NAME "omap-hdmi-audio"
0025 
0026 struct hdmi_audio_data {
0027     struct snd_soc_card *card;
0028 
0029     const struct omap_hdmi_audio_ops *ops;
0030     struct device *dssdev;
0031     struct snd_dmaengine_dai_dma_data dma_data;
0032     struct omap_dss_audio dss_audio;
0033     struct snd_aes_iec958 iec;
0034     struct snd_cea_861_aud_if cea;
0035 
0036     struct mutex current_stream_lock;
0037     struct snd_pcm_substream *current_stream;
0038 };
0039 
0040 static
0041 struct hdmi_audio_data *card_drvdata_substream(struct snd_pcm_substream *ss)
0042 {
0043     struct snd_soc_pcm_runtime *rtd = ss->private_data;
0044 
0045     return snd_soc_card_get_drvdata(rtd->card);
0046 }
0047 
0048 static void hdmi_dai_abort(struct device *dev)
0049 {
0050     struct hdmi_audio_data *ad = dev_get_drvdata(dev);
0051 
0052     mutex_lock(&ad->current_stream_lock);
0053     if (ad->current_stream && ad->current_stream->runtime &&
0054         snd_pcm_running(ad->current_stream)) {
0055         dev_err(dev, "HDMI display disabled, aborting playback\n");
0056         snd_pcm_stream_lock_irq(ad->current_stream);
0057         snd_pcm_stop(ad->current_stream, SNDRV_PCM_STATE_DISCONNECTED);
0058         snd_pcm_stream_unlock_irq(ad->current_stream);
0059     }
0060     mutex_unlock(&ad->current_stream_lock);
0061 }
0062 
0063 static int hdmi_dai_startup(struct snd_pcm_substream *substream,
0064                 struct snd_soc_dai *dai)
0065 {
0066     struct hdmi_audio_data *ad = card_drvdata_substream(substream);
0067     int ret;
0068     /*
0069      * Make sure that the period bytes are multiple of the DMA packet size.
0070      * Largest packet size we use is 32 32-bit words = 128 bytes
0071      */
0072     ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
0073                      SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
0074     if (ret < 0) {
0075         dev_err(dai->dev, "Could not apply period constraint: %d\n",
0076             ret);
0077         return ret;
0078     }
0079     ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
0080                      SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 128);
0081     if (ret < 0) {
0082         dev_err(dai->dev, "Could not apply buffer constraint: %d\n",
0083             ret);
0084         return ret;
0085     }
0086 
0087     snd_soc_dai_set_dma_data(dai, substream, &ad->dma_data);
0088 
0089     mutex_lock(&ad->current_stream_lock);
0090     ad->current_stream = substream;
0091     mutex_unlock(&ad->current_stream_lock);
0092 
0093     ret = ad->ops->audio_startup(ad->dssdev, hdmi_dai_abort);
0094 
0095     if (ret) {
0096         mutex_lock(&ad->current_stream_lock);
0097         ad->current_stream = NULL;
0098         mutex_unlock(&ad->current_stream_lock);
0099     }
0100 
0101     return ret;
0102 }
0103 
0104 static int hdmi_dai_hw_params(struct snd_pcm_substream *substream,
0105                   struct snd_pcm_hw_params *params,
0106                   struct snd_soc_dai *dai)
0107 {
0108     struct hdmi_audio_data *ad = card_drvdata_substream(substream);
0109     struct snd_aes_iec958 *iec = &ad->iec;
0110     struct snd_cea_861_aud_if *cea = &ad->cea;
0111 
0112     WARN_ON(ad->current_stream != substream);
0113 
0114     switch (params_format(params)) {
0115     case SNDRV_PCM_FORMAT_S16_LE:
0116         ad->dma_data.maxburst = 16;
0117         break;
0118     case SNDRV_PCM_FORMAT_S24_LE:
0119         ad->dma_data.maxburst = 32;
0120         break;
0121     default:
0122         dev_err(dai->dev, "format not supported!\n");
0123         return -EINVAL;
0124     }
0125 
0126     ad->dss_audio.iec = iec;
0127     ad->dss_audio.cea = cea;
0128     /*
0129      * fill the IEC-60958 channel status word
0130      */
0131     /* initialize the word bytes */
0132     memset(iec->status, 0, sizeof(iec->status));
0133 
0134     /* specify IEC-60958-3 (commercial use) */
0135     iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
0136 
0137     /* specify that the audio is LPCM*/
0138     iec->status[0] &= ~IEC958_AES0_NONAUDIO;
0139 
0140     iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
0141 
0142     iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
0143 
0144     iec->status[1] = IEC958_AES1_CON_GENERAL;
0145 
0146     iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
0147 
0148     iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
0149 
0150     switch (params_rate(params)) {
0151     case 32000:
0152         iec->status[3] |= IEC958_AES3_CON_FS_32000;
0153         break;
0154     case 44100:
0155         iec->status[3] |= IEC958_AES3_CON_FS_44100;
0156         break;
0157     case 48000:
0158         iec->status[3] |= IEC958_AES3_CON_FS_48000;
0159         break;
0160     case 88200:
0161         iec->status[3] |= IEC958_AES3_CON_FS_88200;
0162         break;
0163     case 96000:
0164         iec->status[3] |= IEC958_AES3_CON_FS_96000;
0165         break;
0166     case 176400:
0167         iec->status[3] |= IEC958_AES3_CON_FS_176400;
0168         break;
0169     case 192000:
0170         iec->status[3] |= IEC958_AES3_CON_FS_192000;
0171         break;
0172     default:
0173         dev_err(dai->dev, "rate not supported!\n");
0174         return -EINVAL;
0175     }
0176 
0177     /* specify the clock accuracy */
0178     iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
0179 
0180     /*
0181      * specify the word length. The same word length value can mean
0182      * two different lengths. Hence, we need to specify the maximum
0183      * word length as well.
0184      */
0185     switch (params_format(params)) {
0186     case SNDRV_PCM_FORMAT_S16_LE:
0187         iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
0188         iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
0189         break;
0190     case SNDRV_PCM_FORMAT_S24_LE:
0191         iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
0192         iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
0193         break;
0194     default:
0195         dev_err(dai->dev, "format not supported!\n");
0196         return -EINVAL;
0197     }
0198 
0199     /*
0200      * Fill the CEA-861 audio infoframe (see spec for details)
0201      */
0202 
0203     cea->db1_ct_cc = (params_channels(params) - 1)
0204         & CEA861_AUDIO_INFOFRAME_DB1CC;
0205     cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
0206 
0207     cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
0208     cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
0209 
0210     cea->db3 = 0; /* not used, all zeros */
0211 
0212     if (params_channels(params) == 2)
0213         cea->db4_ca = 0x0;
0214     else if (params_channels(params) == 6)
0215         cea->db4_ca = 0xb;
0216     else
0217         cea->db4_ca = 0x13;
0218 
0219     if (cea->db4_ca == 0x00)
0220         cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PERMITTED;
0221     else
0222         cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
0223 
0224     /* the expression is trivial but makes clear what we are doing */
0225     cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
0226 
0227     return ad->ops->audio_config(ad->dssdev, &ad->dss_audio);
0228 }
0229 
0230 static int hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
0231                 struct snd_soc_dai *dai)
0232 {
0233     struct hdmi_audio_data *ad = card_drvdata_substream(substream);
0234     int err = 0;
0235 
0236     WARN_ON(ad->current_stream != substream);
0237 
0238     switch (cmd) {
0239     case SNDRV_PCM_TRIGGER_START:
0240     case SNDRV_PCM_TRIGGER_RESUME:
0241     case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0242         err = ad->ops->audio_start(ad->dssdev);
0243         break;
0244     case SNDRV_PCM_TRIGGER_STOP:
0245     case SNDRV_PCM_TRIGGER_SUSPEND:
0246     case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0247         ad->ops->audio_stop(ad->dssdev);
0248         break;
0249     default:
0250         err = -EINVAL;
0251     }
0252     return err;
0253 }
0254 
0255 static void hdmi_dai_shutdown(struct snd_pcm_substream *substream,
0256                   struct snd_soc_dai *dai)
0257 {
0258     struct hdmi_audio_data *ad = card_drvdata_substream(substream);
0259 
0260     WARN_ON(ad->current_stream != substream);
0261 
0262     ad->ops->audio_shutdown(ad->dssdev);
0263 
0264     mutex_lock(&ad->current_stream_lock);
0265     ad->current_stream = NULL;
0266     mutex_unlock(&ad->current_stream_lock);
0267 }
0268 
0269 static const struct snd_soc_dai_ops hdmi_dai_ops = {
0270     .startup    = hdmi_dai_startup,
0271     .hw_params  = hdmi_dai_hw_params,
0272     .trigger    = hdmi_dai_trigger,
0273     .shutdown   = hdmi_dai_shutdown,
0274 };
0275 
0276 static const struct snd_soc_component_driver omap_hdmi_component = {
0277     .name = "omapdss_hdmi",
0278     .legacy_dai_naming = 1,
0279 };
0280 
0281 static struct snd_soc_dai_driver omap5_hdmi_dai = {
0282     .name = "omap5-hdmi-dai",
0283     .playback = {
0284         .channels_min = 2,
0285         .channels_max = 8,
0286         .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
0287               SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
0288               SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
0289               SNDRV_PCM_RATE_192000),
0290         .formats = SNDRV_PCM_FMTBIT_S16_LE,
0291     },
0292     .ops = &hdmi_dai_ops,
0293 };
0294 
0295 static struct snd_soc_dai_driver omap4_hdmi_dai = {
0296     .name = "omap4-hdmi-dai",
0297     .playback = {
0298         .channels_min = 2,
0299         .channels_max = 8,
0300         .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
0301               SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
0302               SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
0303               SNDRV_PCM_RATE_192000),
0304         .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
0305     },
0306     .ops = &hdmi_dai_ops,
0307 };
0308 
0309 static int omap_hdmi_audio_probe(struct platform_device *pdev)
0310 {
0311     struct omap_hdmi_audio_pdata *ha = pdev->dev.platform_data;
0312     struct device *dev = &pdev->dev;
0313     struct hdmi_audio_data *ad;
0314     struct snd_soc_dai_driver *dai_drv;
0315     struct snd_soc_card *card;
0316     struct snd_soc_dai_link_component *compnent;
0317     int ret;
0318 
0319     if (!ha) {
0320         dev_err(dev, "No platform data\n");
0321         return -EINVAL;
0322     }
0323 
0324     ad = devm_kzalloc(dev, sizeof(*ad), GFP_KERNEL);
0325     if (!ad)
0326         return -ENOMEM;
0327     ad->dssdev = ha->dev;
0328     ad->ops = ha->ops;
0329     ad->dma_data.addr = ha->audio_dma_addr;
0330     ad->dma_data.filter_data = "audio_tx";
0331     ad->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
0332     mutex_init(&ad->current_stream_lock);
0333 
0334     switch (ha->version) {
0335     case 4:
0336         dai_drv = &omap4_hdmi_dai;
0337         break;
0338     case 5:
0339         dai_drv = &omap5_hdmi_dai;
0340         break;
0341     default:
0342         return -EINVAL;
0343     }
0344     ret = devm_snd_soc_register_component(ad->dssdev, &omap_hdmi_component,
0345                      dai_drv, 1);
0346     if (ret)
0347         return ret;
0348 
0349     ret = sdma_pcm_platform_register(ad->dssdev, "audio_tx", NULL);
0350     if (ret)
0351         return ret;
0352 
0353     card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
0354     if (!card)
0355         return -ENOMEM;
0356 
0357     card->name = devm_kasprintf(dev, GFP_KERNEL,
0358                     "HDMI %s", dev_name(ad->dssdev));
0359     if (!card->name)
0360         return -ENOMEM;
0361 
0362     card->owner = THIS_MODULE;
0363     card->dai_link =
0364         devm_kzalloc(dev, sizeof(*(card->dai_link)), GFP_KERNEL);
0365     if (!card->dai_link)
0366         return -ENOMEM;
0367 
0368     compnent = devm_kzalloc(dev, 3 * sizeof(*compnent), GFP_KERNEL);
0369     if (!compnent)
0370         return -ENOMEM;
0371     card->dai_link->cpus        = &compnent[0];
0372     card->dai_link->num_cpus    = 1;
0373     card->dai_link->codecs      = &compnent[1];
0374     card->dai_link->num_codecs  = 1;
0375     card->dai_link->platforms   = &compnent[2];
0376     card->dai_link->num_platforms   = 1;
0377 
0378     card->dai_link->name = card->name;
0379     card->dai_link->stream_name = card->name;
0380     card->dai_link->cpus->dai_name = dev_name(ad->dssdev);
0381     card->dai_link->platforms->name = dev_name(ad->dssdev);
0382     card->dai_link->codecs->name = "snd-soc-dummy";
0383     card->dai_link->codecs->dai_name = "snd-soc-dummy-dai";
0384     card->num_links = 1;
0385     card->dev = dev;
0386 
0387     ret = snd_soc_register_card(card);
0388     if (ret) {
0389         dev_err(dev, "snd_soc_register_card failed (%d)\n", ret);
0390         return ret;
0391     }
0392 
0393     ad->card = card;
0394     snd_soc_card_set_drvdata(card, ad);
0395 
0396     dev_set_drvdata(dev, ad);
0397 
0398     return 0;
0399 }
0400 
0401 static int omap_hdmi_audio_remove(struct platform_device *pdev)
0402 {
0403     struct hdmi_audio_data *ad = platform_get_drvdata(pdev);
0404 
0405     snd_soc_unregister_card(ad->card);
0406     return 0;
0407 }
0408 
0409 static struct platform_driver hdmi_audio_driver = {
0410     .driver = {
0411         .name = DRV_NAME,
0412     },
0413     .probe = omap_hdmi_audio_probe,
0414     .remove = omap_hdmi_audio_remove,
0415 };
0416 
0417 module_platform_driver(hdmi_audio_driver);
0418 
0419 MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
0420 MODULE_DESCRIPTION("OMAP HDMI Audio Driver");
0421 MODULE_LICENSE("GPL");
0422 MODULE_ALIAS("platform:" DRV_NAME);