linux/sound/soc/omap/omap-hdmi-audio.c
<<
>>
Prefs
   1/*
   2 * omap-hdmi-audio.c -- OMAP4+ DSS HDMI audio support library
   3 *
   4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com
   5 *
   6 * Author: Jyri Sarha <jsarha@ti.com>
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License
  10 * version 2 as published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful, but
  13 * WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15 * General Public License for more details.
  16 *
  17 */
  18
  19#include <linux/kernel.h>
  20#include <linux/module.h>
  21#include <linux/err.h>
  22#include <linux/string.h>
  23#include <linux/platform_device.h>
  24#include <sound/soc.h>
  25#include <sound/pcm_params.h>
  26#include <sound/dmaengine_pcm.h>
  27#include <uapi/sound/asound.h>
  28#include <sound/asoundef.h>
  29#include <sound/omap-pcm.h>
  30#include <sound/omap-hdmi-audio.h>
  31
  32#define DRV_NAME "omap-hdmi-audio"
  33
  34struct hdmi_audio_data {
  35        struct snd_soc_card *card;
  36
  37        const struct omap_hdmi_audio_ops *ops;
  38        struct device *dssdev;
  39        struct snd_dmaengine_dai_dma_data dma_data;
  40        struct omap_dss_audio dss_audio;
  41        struct snd_aes_iec958 iec;
  42        struct snd_cea_861_aud_if cea;
  43
  44        struct mutex current_stream_lock;
  45        struct snd_pcm_substream *current_stream;
  46};
  47
  48static
  49struct hdmi_audio_data *card_drvdata_substream(struct snd_pcm_substream *ss)
  50{
  51        struct snd_soc_pcm_runtime *rtd = ss->private_data;
  52
  53        return snd_soc_card_get_drvdata(rtd->card);
  54}
  55
  56static void hdmi_dai_abort(struct device *dev)
  57{
  58        struct hdmi_audio_data *ad = dev_get_drvdata(dev);
  59
  60        mutex_lock(&ad->current_stream_lock);
  61        if (ad->current_stream && ad->current_stream->runtime &&
  62            snd_pcm_running(ad->current_stream)) {
  63                dev_err(dev, "HDMI display disabled, aborting playback\n");
  64                snd_pcm_stream_lock_irq(ad->current_stream);
  65                snd_pcm_stop(ad->current_stream, SNDRV_PCM_STATE_DISCONNECTED);
  66                snd_pcm_stream_unlock_irq(ad->current_stream);
  67        }
  68        mutex_unlock(&ad->current_stream_lock);
  69}
  70
  71static int hdmi_dai_startup(struct snd_pcm_substream *substream,
  72                            struct snd_soc_dai *dai)
  73{
  74        struct hdmi_audio_data *ad = card_drvdata_substream(substream);
  75        int ret;
  76        /*
  77         * Make sure that the period bytes are multiple of the DMA packet size.
  78         * Largest packet size we use is 32 32-bit words = 128 bytes
  79         */
  80        ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  81                                         SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
  82        if (ret < 0) {
  83                dev_err(dai->dev, "Could not apply period constraint: %d\n",
  84                        ret);
  85                return ret;
  86        }
  87        ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
  88                                         SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 128);
  89        if (ret < 0) {
  90                dev_err(dai->dev, "Could not apply buffer constraint: %d\n",
  91                        ret);
  92                return ret;
  93        }
  94
  95        snd_soc_dai_set_dma_data(dai, substream, &ad->dma_data);
  96
  97        mutex_lock(&ad->current_stream_lock);
  98        ad->current_stream = substream;
  99        mutex_unlock(&ad->current_stream_lock);
 100
 101        ret = ad->ops->audio_startup(ad->dssdev, hdmi_dai_abort);
 102
 103        if (ret) {
 104                mutex_lock(&ad->current_stream_lock);
 105                ad->current_stream = NULL;
 106                mutex_unlock(&ad->current_stream_lock);
 107        }
 108
 109        return ret;
 110}
 111
 112static int hdmi_dai_hw_params(struct snd_pcm_substream *substream,
 113                              struct snd_pcm_hw_params *params,
 114                              struct snd_soc_dai *dai)
 115{
 116        struct hdmi_audio_data *ad = card_drvdata_substream(substream);
 117        struct snd_aes_iec958 *iec = &ad->iec;
 118        struct snd_cea_861_aud_if *cea = &ad->cea;
 119
 120        WARN_ON(ad->current_stream != substream);
 121
 122        switch (params_format(params)) {
 123        case SNDRV_PCM_FORMAT_S16_LE:
 124                ad->dma_data.maxburst = 16;
 125                break;
 126        case SNDRV_PCM_FORMAT_S24_LE:
 127                ad->dma_data.maxburst = 32;
 128                break;
 129        default:
 130                dev_err(dai->dev, "format not supported!\n");
 131                return -EINVAL;
 132        }
 133
 134        ad->dss_audio.iec = iec;
 135        ad->dss_audio.cea = cea;
 136        /*
 137         * fill the IEC-60958 channel status word
 138         */
 139        /* initialize the word bytes */
 140        memset(iec->status, 0, sizeof(iec->status));
 141
 142        /* specify IEC-60958-3 (commercial use) */
 143        iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
 144
 145        /* specify that the audio is LPCM*/
 146        iec->status[0] &= ~IEC958_AES0_NONAUDIO;
 147
 148        iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
 149
 150        iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
 151
 152        iec->status[1] = IEC958_AES1_CON_GENERAL;
 153
 154        iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
 155
 156        iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
 157
 158        switch (params_rate(params)) {
 159        case 32000:
 160                iec->status[3] |= IEC958_AES3_CON_FS_32000;
 161                break;
 162        case 44100:
 163                iec->status[3] |= IEC958_AES3_CON_FS_44100;
 164                break;
 165        case 48000:
 166                iec->status[3] |= IEC958_AES3_CON_FS_48000;
 167                break;
 168        case 88200:
 169                iec->status[3] |= IEC958_AES3_CON_FS_88200;
 170                break;
 171        case 96000:
 172                iec->status[3] |= IEC958_AES3_CON_FS_96000;
 173                break;
 174        case 176400:
 175                iec->status[3] |= IEC958_AES3_CON_FS_176400;
 176                break;
 177        case 192000:
 178                iec->status[3] |= IEC958_AES3_CON_FS_192000;
 179                break;
 180        default:
 181                dev_err(dai->dev, "rate not supported!\n");
 182                return -EINVAL;
 183        }
 184
 185        /* specify the clock accuracy */
 186        iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
 187
 188        /*
 189         * specify the word length. The same word length value can mean
 190         * two different lengths. Hence, we need to specify the maximum
 191         * word length as well.
 192         */
 193        switch (params_format(params)) {
 194        case SNDRV_PCM_FORMAT_S16_LE:
 195                iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
 196                iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
 197                break;
 198        case SNDRV_PCM_FORMAT_S24_LE:
 199                iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
 200                iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
 201                break;
 202        default:
 203                dev_err(dai->dev, "format not supported!\n");
 204                return -EINVAL;
 205        }
 206
 207        /*
 208         * Fill the CEA-861 audio infoframe (see spec for details)
 209         */
 210
 211        cea->db1_ct_cc = (params_channels(params) - 1)
 212                & CEA861_AUDIO_INFOFRAME_DB1CC;
 213        cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
 214
 215        cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
 216        cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
 217
 218        cea->db3 = 0; /* not used, all zeros */
 219
 220        if (params_channels(params) == 2)
 221                cea->db4_ca = 0x0;
 222        else if (params_channels(params) == 6)
 223                cea->db4_ca = 0xb;
 224        else
 225                cea->db4_ca = 0x13;
 226
 227        if (cea->db4_ca == 0x00)
 228                cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PERMITTED;
 229        else
 230                cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
 231
 232        /* the expression is trivial but makes clear what we are doing */
 233        cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
 234
 235        return ad->ops->audio_config(ad->dssdev, &ad->dss_audio);
 236}
 237
 238static int hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
 239                            struct snd_soc_dai *dai)
 240{
 241        struct hdmi_audio_data *ad = card_drvdata_substream(substream);
 242        int err = 0;
 243
 244        WARN_ON(ad->current_stream != substream);
 245
 246        switch (cmd) {
 247        case SNDRV_PCM_TRIGGER_START:
 248        case SNDRV_PCM_TRIGGER_RESUME:
 249        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 250                err = ad->ops->audio_start(ad->dssdev);
 251                break;
 252        case SNDRV_PCM_TRIGGER_STOP:
 253        case SNDRV_PCM_TRIGGER_SUSPEND:
 254        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 255                ad->ops->audio_stop(ad->dssdev);
 256                break;
 257        default:
 258                err = -EINVAL;
 259        }
 260        return err;
 261}
 262
 263static void hdmi_dai_shutdown(struct snd_pcm_substream *substream,
 264                              struct snd_soc_dai *dai)
 265{
 266        struct hdmi_audio_data *ad = card_drvdata_substream(substream);
 267
 268        WARN_ON(ad->current_stream != substream);
 269
 270        ad->ops->audio_shutdown(ad->dssdev);
 271
 272        mutex_lock(&ad->current_stream_lock);
 273        ad->current_stream = NULL;
 274        mutex_unlock(&ad->current_stream_lock);
 275}
 276
 277static const struct snd_soc_dai_ops hdmi_dai_ops = {
 278        .startup        = hdmi_dai_startup,
 279        .hw_params      = hdmi_dai_hw_params,
 280        .trigger        = hdmi_dai_trigger,
 281        .shutdown       = hdmi_dai_shutdown,
 282};
 283
 284static const struct snd_soc_component_driver omap_hdmi_component = {
 285        .name = "omapdss_hdmi",
 286};
 287
 288static struct snd_soc_dai_driver omap5_hdmi_dai = {
 289        .name = "omap5-hdmi-dai",
 290        .playback = {
 291                .channels_min = 2,
 292                .channels_max = 8,
 293                .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
 294                          SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
 295                          SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
 296                          SNDRV_PCM_RATE_192000),
 297                .formats = SNDRV_PCM_FMTBIT_S16_LE,
 298        },
 299        .ops = &hdmi_dai_ops,
 300};
 301
 302static struct snd_soc_dai_driver omap4_hdmi_dai = {
 303        .name = "omap4-hdmi-dai",
 304        .playback = {
 305                .channels_min = 2,
 306                .channels_max = 8,
 307                .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
 308                          SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
 309                          SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
 310                          SNDRV_PCM_RATE_192000),
 311                .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
 312        },
 313        .ops = &hdmi_dai_ops,
 314};
 315
 316static int omap_hdmi_audio_probe(struct platform_device *pdev)
 317{
 318        struct omap_hdmi_audio_pdata *ha = pdev->dev.platform_data;
 319        struct device *dev = &pdev->dev;
 320        struct hdmi_audio_data *ad;
 321        struct snd_soc_dai_driver *dai_drv;
 322        struct snd_soc_card *card;
 323        int ret;
 324
 325        if (!ha) {
 326                dev_err(dev, "No platform data\n");
 327                return -EINVAL;
 328        }
 329
 330        ad = devm_kzalloc(dev, sizeof(*ad), GFP_KERNEL);
 331        if (!ad)
 332                return -ENOMEM;
 333        ad->dssdev = ha->dev;
 334        ad->ops = ha->ops;
 335        ad->dma_data.addr = ha->audio_dma_addr;
 336        ad->dma_data.filter_data = "audio_tx";
 337        ad->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
 338        mutex_init(&ad->current_stream_lock);
 339
 340        switch (ha->version) {
 341        case 4:
 342                dai_drv = &omap4_hdmi_dai;
 343                break;
 344        case 5:
 345                dai_drv = &omap5_hdmi_dai;
 346                break;
 347        default:
 348                return -EINVAL;
 349        }
 350        ret = snd_soc_register_component(ad->dssdev, &omap_hdmi_component,
 351                                         dai_drv, 1);
 352        if (ret)
 353                return ret;
 354
 355        ret = omap_pcm_platform_register(ad->dssdev);
 356        if (ret)
 357                return ret;
 358
 359        card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
 360        if (!card)
 361                return -ENOMEM;
 362
 363        card->name = devm_kasprintf(dev, GFP_KERNEL,
 364                                    "HDMI %s", dev_name(ad->dssdev));
 365        if (!card->name)
 366                return -ENOMEM;
 367
 368        card->owner = THIS_MODULE;
 369        card->dai_link =
 370                devm_kzalloc(dev, sizeof(*(card->dai_link)), GFP_KERNEL);
 371        if (!card->dai_link)
 372                return -ENOMEM;
 373        card->dai_link->name = card->name;
 374        card->dai_link->stream_name = card->name;
 375        card->dai_link->cpu_dai_name = dev_name(ad->dssdev);
 376        card->dai_link->platform_name = dev_name(ad->dssdev);
 377        card->dai_link->codec_name = "snd-soc-dummy";
 378        card->dai_link->codec_dai_name = "snd-soc-dummy-dai";
 379        card->num_links = 1;
 380        card->dev = dev;
 381
 382        ret = snd_soc_register_card(card);
 383        if (ret) {
 384                dev_err(dev, "snd_soc_register_card failed (%d)\n", ret);
 385                snd_soc_unregister_component(ad->dssdev);
 386                return ret;
 387        }
 388
 389        ad->card = card;
 390        snd_soc_card_set_drvdata(card, ad);
 391
 392        dev_set_drvdata(dev, ad);
 393
 394        return 0;
 395}
 396
 397static int omap_hdmi_audio_remove(struct platform_device *pdev)
 398{
 399        struct hdmi_audio_data *ad = platform_get_drvdata(pdev);
 400
 401        snd_soc_unregister_card(ad->card);
 402        snd_soc_unregister_component(ad->dssdev);
 403        return 0;
 404}
 405
 406static struct platform_driver hdmi_audio_driver = {
 407        .driver = {
 408                .name = DRV_NAME,
 409        },
 410        .probe = omap_hdmi_audio_probe,
 411        .remove = omap_hdmi_audio_remove,
 412};
 413
 414module_platform_driver(hdmi_audio_driver);
 415
 416MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>");
 417MODULE_DESCRIPTION("OMAP HDMI Audio Driver");
 418MODULE_LICENSE("GPL");
 419MODULE_ALIAS("platform:" DRV_NAME);
 420