linux/sound/soc/intel/baytrail/sst-baytrail-pcm.c
<<
>>
Prefs
   1/*
   2 * Intel Baytrail SST PCM Support
   3 * Copyright (c) 2014, Intel Corporation.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 */
  14
  15#include <linux/module.h>
  16#include <linux/dma-mapping.h>
  17#include <linux/slab.h>
  18#include <sound/core.h>
  19#include <sound/pcm.h>
  20#include <sound/pcm_params.h>
  21#include <sound/soc.h>
  22#include "sst-baytrail-ipc.h"
  23#include "../common/sst-dsp-priv.h"
  24#include "../common/sst-dsp.h"
  25
  26#define DRV_NAME "byt-dai"
  27#define BYT_PCM_COUNT           2
  28
  29static const struct snd_pcm_hardware sst_byt_pcm_hardware = {
  30        .info                   = SNDRV_PCM_INFO_MMAP |
  31                                  SNDRV_PCM_INFO_MMAP_VALID |
  32                                  SNDRV_PCM_INFO_INTERLEAVED |
  33                                  SNDRV_PCM_INFO_PAUSE |
  34                                  SNDRV_PCM_INFO_RESUME,
  35        .formats                = SNDRV_PCM_FMTBIT_S16_LE |
  36                                  SNDRV_PCM_FMTBIT_S24_LE,
  37        .period_bytes_min       = 384,
  38        .period_bytes_max       = 48000,
  39        .periods_min            = 2,
  40        .periods_max            = 250,
  41        .buffer_bytes_max       = 96000,
  42};
  43
  44/* private data for each PCM DSP stream */
  45struct sst_byt_pcm_data {
  46        struct sst_byt_stream *stream;
  47        struct snd_pcm_substream *substream;
  48        struct mutex mutex;
  49
  50        /* latest DSP DMA hw pointer */
  51        u32 hw_ptr;
  52
  53        struct work_struct work;
  54};
  55
  56/* private data for the driver */
  57struct sst_byt_priv_data {
  58        /* runtime DSP */
  59        struct sst_byt *byt;
  60
  61        /* DAI data */
  62        struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
  63
  64        /* flag indicating is stream context restore needed after suspend */
  65        bool restore_stream;
  66};
  67
  68/* this may get called several times by oss emulation */
  69static int sst_byt_pcm_hw_params(struct snd_pcm_substream *substream,
  70                                 struct snd_pcm_hw_params *params)
  71{
  72        struct snd_soc_pcm_runtime *rtd = substream->private_data;
  73        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
  74        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
  75        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  76        struct sst_byt *byt = pdata->byt;
  77        u32 rate, bits;
  78        u8 channels;
  79        int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  80
  81        dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
  82
  83        ret = sst_byt_stream_type(byt, pcm_data->stream,
  84                                  1, 1, !playback);
  85        if (ret < 0) {
  86                dev_err(rtd->dev, "failed to set stream format %d\n", ret);
  87                return ret;
  88        }
  89
  90        rate = params_rate(params);
  91        ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
  92        if (ret < 0) {
  93                dev_err(rtd->dev, "could not set rate %d\n", rate);
  94                return ret;
  95        }
  96
  97        bits = snd_pcm_format_width(params_format(params));
  98        ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
  99        if (ret < 0) {
 100                dev_err(rtd->dev, "could not set formats %d\n",
 101                        params_rate(params));
 102                return ret;
 103        }
 104
 105        channels = (u8)(params_channels(params) & 0xF);
 106        ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
 107        if (ret < 0) {
 108                dev_err(rtd->dev, "could not set channels %d\n",
 109                        params_rate(params));
 110                return ret;
 111        }
 112
 113        snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
 114
 115        ret = sst_byt_stream_buffer(byt, pcm_data->stream,
 116                                    substream->dma_buffer.addr,
 117                                    params_buffer_bytes(params));
 118        if (ret < 0) {
 119                dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
 120                return ret;
 121        }
 122
 123        ret = sst_byt_stream_commit(byt, pcm_data->stream);
 124        if (ret < 0) {
 125                dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
 126                return ret;
 127        }
 128
 129        return 0;
 130}
 131
 132static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
 133{
 134        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 135
 136        dev_dbg(rtd->dev, "PCM: hw_free\n");
 137        snd_pcm_lib_free_pages(substream);
 138
 139        return 0;
 140}
 141
 142static int sst_byt_pcm_restore_stream_context(struct snd_pcm_substream *substream)
 143{
 144        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 145        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 146        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 147        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
 148        struct sst_byt *byt = pdata->byt;
 149        int ret;
 150
 151        /* commit stream using existing stream params */
 152        ret = sst_byt_stream_commit(byt, pcm_data->stream);
 153        if (ret < 0) {
 154                dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
 155                return ret;
 156        }
 157
 158        sst_byt_stream_start(byt, pcm_data->stream, pcm_data->hw_ptr);
 159
 160        dev_dbg(rtd->dev, "stream context restored at offset %d\n",
 161                pcm_data->hw_ptr);
 162
 163        return 0;
 164}
 165
 166static void sst_byt_pcm_work(struct work_struct *work)
 167{
 168        struct sst_byt_pcm_data *pcm_data =
 169                container_of(work, struct sst_byt_pcm_data, work);
 170
 171        if (snd_pcm_running(pcm_data->substream))
 172                sst_byt_pcm_restore_stream_context(pcm_data->substream);
 173}
 174
 175static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 176{
 177        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 178        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 179        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 180        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
 181        struct sst_byt *byt = pdata->byt;
 182
 183        dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
 184
 185        switch (cmd) {
 186        case SNDRV_PCM_TRIGGER_START:
 187                pcm_data->hw_ptr = 0;
 188                sst_byt_stream_start(byt, pcm_data->stream, 0);
 189                break;
 190        case SNDRV_PCM_TRIGGER_RESUME:
 191                if (pdata->restore_stream == true)
 192                        schedule_work(&pcm_data->work);
 193                else
 194                        sst_byt_stream_resume(byt, pcm_data->stream);
 195                break;
 196        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 197                sst_byt_stream_resume(byt, pcm_data->stream);
 198                break;
 199        case SNDRV_PCM_TRIGGER_STOP:
 200                sst_byt_stream_stop(byt, pcm_data->stream);
 201                break;
 202        case SNDRV_PCM_TRIGGER_SUSPEND:
 203                pdata->restore_stream = false;
 204        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 205                sst_byt_stream_pause(byt, pcm_data->stream);
 206                break;
 207        default:
 208                break;
 209        }
 210
 211        return 0;
 212}
 213
 214static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
 215{
 216        struct sst_byt_pcm_data *pcm_data = data;
 217        struct snd_pcm_substream *substream = pcm_data->substream;
 218        struct snd_pcm_runtime *runtime = substream->runtime;
 219        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 220        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 221        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 222        struct sst_byt *byt = pdata->byt;
 223        u32 pos, hw_pos;
 224
 225        hw_pos = sst_byt_get_dsp_position(byt, pcm_data->stream,
 226                                          snd_pcm_lib_buffer_bytes(substream));
 227        pcm_data->hw_ptr = hw_pos;
 228        pos = frames_to_bytes(runtime,
 229                              (runtime->control->appl_ptr %
 230                               runtime->buffer_size));
 231
 232        dev_dbg(rtd->dev, "PCM: App/DMA pointer %u/%u bytes\n", pos, hw_pos);
 233
 234        snd_pcm_period_elapsed(substream);
 235        return pos;
 236}
 237
 238static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
 239{
 240        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 241        struct snd_pcm_runtime *runtime = substream->runtime;
 242        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 243        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 244        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
 245
 246        dev_dbg(rtd->dev, "PCM: DMA pointer %u bytes\n", pcm_data->hw_ptr);
 247
 248        return bytes_to_frames(runtime, pcm_data->hw_ptr);
 249}
 250
 251static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
 252{
 253        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 254        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 255        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 256        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
 257        struct sst_byt *byt = pdata->byt;
 258
 259        dev_dbg(rtd->dev, "PCM: open\n");
 260
 261        mutex_lock(&pcm_data->mutex);
 262
 263        pcm_data->substream = substream;
 264
 265        snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
 266
 267        pcm_data->stream = sst_byt_stream_new(byt, substream->stream + 1,
 268                                              byt_notify_pointer, pcm_data);
 269        if (pcm_data->stream == NULL) {
 270                dev_err(rtd->dev, "failed to create stream\n");
 271                mutex_unlock(&pcm_data->mutex);
 272                return -EINVAL;
 273        }
 274
 275        mutex_unlock(&pcm_data->mutex);
 276        return 0;
 277}
 278
 279static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
 280{
 281        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 282        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 283        struct sst_byt_priv_data *pdata = snd_soc_component_get_drvdata(component);
 284        struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
 285        struct sst_byt *byt = pdata->byt;
 286        int ret;
 287
 288        dev_dbg(rtd->dev, "PCM: close\n");
 289
 290        cancel_work_sync(&pcm_data->work);
 291        mutex_lock(&pcm_data->mutex);
 292        ret = sst_byt_stream_free(byt, pcm_data->stream);
 293        if (ret < 0) {
 294                dev_dbg(rtd->dev, "Free stream fail\n");
 295                goto out;
 296        }
 297        pcm_data->stream = NULL;
 298
 299out:
 300        mutex_unlock(&pcm_data->mutex);
 301        return ret;
 302}
 303
 304static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
 305                            struct vm_area_struct *vma)
 306{
 307        struct snd_soc_pcm_runtime *rtd = substream->private_data;
 308
 309        dev_dbg(rtd->dev, "PCM: mmap\n");
 310        return snd_pcm_lib_default_mmap(substream, vma);
 311}
 312
 313static const struct snd_pcm_ops sst_byt_pcm_ops = {
 314        .open           = sst_byt_pcm_open,
 315        .close          = sst_byt_pcm_close,
 316        .ioctl          = snd_pcm_lib_ioctl,
 317        .hw_params      = sst_byt_pcm_hw_params,
 318        .hw_free        = sst_byt_pcm_hw_free,
 319        .trigger        = sst_byt_pcm_trigger,
 320        .pointer        = sst_byt_pcm_pointer,
 321        .mmap           = sst_byt_pcm_mmap,
 322};
 323
 324static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
 325{
 326        struct snd_pcm *pcm = rtd->pcm;
 327        size_t size;
 328        struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
 329        struct sst_pdata *pdata = dev_get_platdata(component->dev);
 330
 331        if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
 332            pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
 333                size = sst_byt_pcm_hardware.buffer_bytes_max;
 334                snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
 335                                                      pdata->dma_dev,
 336                                                      size, size);
 337        }
 338
 339        return 0;
 340}
 341
 342static struct snd_soc_dai_driver byt_dais[] = {
 343        {
 344                .name  = "Baytrail PCM",
 345                .playback = {
 346                        .stream_name = "System Playback",
 347                        .channels_min = 2,
 348                        .channels_max = 2,
 349                        .rates = SNDRV_PCM_RATE_48000,
 350                        .formats = SNDRV_PCM_FMTBIT_S24_3LE |
 351                                   SNDRV_PCM_FMTBIT_S16_LE,
 352                },
 353                .capture = {
 354                        .stream_name = "Analog Capture",
 355                        .channels_min = 2,
 356                        .channels_max = 2,
 357                        .rates = SNDRV_PCM_RATE_48000,
 358                        .formats = SNDRV_PCM_FMTBIT_S16_LE,
 359                },
 360        },
 361};
 362
 363static int sst_byt_pcm_probe(struct snd_soc_component *component)
 364{
 365        struct sst_pdata *plat_data = dev_get_platdata(component->dev);
 366        struct sst_byt_priv_data *priv_data;
 367        int i;
 368
 369        if (!plat_data)
 370                return -ENODEV;
 371
 372        priv_data = devm_kzalloc(component->dev, sizeof(*priv_data),
 373                                 GFP_KERNEL);
 374        if (!priv_data)
 375                return -ENOMEM;
 376        priv_data->byt = plat_data->dsp;
 377        snd_soc_component_set_drvdata(component, priv_data);
 378
 379        for (i = 0; i < BYT_PCM_COUNT; i++) {
 380                mutex_init(&priv_data->pcm[i].mutex);
 381                INIT_WORK(&priv_data->pcm[i].work, sst_byt_pcm_work);
 382        }
 383
 384        return 0;
 385}
 386
 387static const struct snd_soc_component_driver byt_dai_component = {
 388        .name           = DRV_NAME,
 389        .probe          = sst_byt_pcm_probe,
 390        .ops            = &sst_byt_pcm_ops,
 391        .pcm_new        = sst_byt_pcm_new,
 392};
 393
 394#ifdef CONFIG_PM
 395static int sst_byt_pcm_dev_suspend_late(struct device *dev)
 396{
 397        struct sst_pdata *sst_pdata = dev_get_platdata(dev);
 398        struct sst_byt_priv_data *priv_data = dev_get_drvdata(dev);
 399        int ret;
 400
 401        dev_dbg(dev, "suspending late\n");
 402
 403        ret = sst_byt_dsp_suspend_late(dev, sst_pdata);
 404        if (ret < 0) {
 405                dev_err(dev, "failed to suspend %d\n", ret);
 406                return ret;
 407        }
 408
 409        priv_data->restore_stream = true;
 410
 411        return ret;
 412}
 413
 414static int sst_byt_pcm_dev_resume_early(struct device *dev)
 415{
 416        struct sst_pdata *sst_pdata = dev_get_platdata(dev);
 417        int ret;
 418
 419        dev_dbg(dev, "resume early\n");
 420
 421        /* load fw and boot DSP */
 422        ret = sst_byt_dsp_boot(dev, sst_pdata);
 423        if (ret)
 424                return ret;
 425
 426        /* wait for FW to finish booting */
 427        return sst_byt_dsp_wait_for_ready(dev, sst_pdata);
 428}
 429
 430static const struct dev_pm_ops sst_byt_pm_ops = {
 431        .suspend_late = sst_byt_pcm_dev_suspend_late,
 432        .resume_early = sst_byt_pcm_dev_resume_early,
 433};
 434
 435#define SST_BYT_PM_OPS  (&sst_byt_pm_ops)
 436#else
 437#define SST_BYT_PM_OPS  NULL
 438#endif
 439
 440static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
 441{
 442        struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
 443        int ret;
 444
 445        ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
 446        if (ret < 0)
 447                return -ENODEV;
 448
 449        ret = devm_snd_soc_register_component(&pdev->dev, &byt_dai_component,
 450                                         byt_dais, ARRAY_SIZE(byt_dais));
 451        if (ret < 0)
 452                goto err_plat;
 453
 454        return 0;
 455
 456err_plat:
 457        sst_byt_dsp_free(&pdev->dev, sst_pdata);
 458        return ret;
 459}
 460
 461static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
 462{
 463        struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
 464
 465        sst_byt_dsp_free(&pdev->dev, sst_pdata);
 466
 467        return 0;
 468}
 469
 470static struct platform_driver sst_byt_pcm_driver = {
 471        .driver = {
 472                .name = "baytrail-pcm-audio",
 473                .pm = SST_BYT_PM_OPS,
 474        },
 475
 476        .probe = sst_byt_pcm_dev_probe,
 477        .remove = sst_byt_pcm_dev_remove,
 478};
 479module_platform_driver(sst_byt_pcm_driver);
 480
 481MODULE_AUTHOR("Jarkko Nikula");
 482MODULE_DESCRIPTION("Baytrail PCM");
 483MODULE_LICENSE("GPL v2");
 484MODULE_ALIAS("platform:baytrail-pcm-audio");
 485