linux/sound/soc/soc-dmaengine-pcm.c
<<
>>
Prefs
   1/*
   2 *  Copyright (C) 2012, Analog Devices Inc.
   3 *      Author: Lars-Peter Clausen <lars@metafoo.de>
   4 *
   5 *  Based on:
   6 *      imx-pcm-dma-mx2.c, Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
   7 *      mxs-pcm.c, Copyright (C) 2011 Freescale Semiconductor, Inc.
   8 *      ep93xx-pcm.c, Copyright (C) 2006 Lennert Buytenhek <buytenh@wantstofly.org>
   9 *                    Copyright (C) 2006 Applied Data Systems
  10 *
  11 *  This program is free software; you can redistribute it and/or modify it
  12 *  under  the terms of the GNU General  Public License as published by the
  13 *  Free Software Foundation;  either version 2 of the License, or (at your
  14 *  option) any later version.
  15 *
  16 *  You should have received a copy of the GNU General Public License along
  17 *  with this program; if not, write to the Free Software Foundation, Inc.,
  18 *  675 Mass Ave, Cambridge, MA 02139, USA.
  19 *
  20 */
  21#include <linux/module.h>
  22#include <linux/init.h>
  23#include <linux/dmaengine.h>
  24#include <linux/slab.h>
  25#include <sound/pcm.h>
  26#include <sound/pcm_params.h>
  27#include <sound/soc.h>
  28
  29#include <sound/dmaengine_pcm.h>
  30
  31struct dmaengine_pcm_runtime_data {
  32        struct dma_chan *dma_chan;
  33        dma_cookie_t cookie;
  34
  35        unsigned int pos;
  36
  37        void *data;
  38};
  39
  40static inline struct dmaengine_pcm_runtime_data *substream_to_prtd(
  41        const struct snd_pcm_substream *substream)
  42{
  43        return substream->runtime->private_data;
  44}
  45
  46/**
  47 * snd_dmaengine_pcm_set_data - Set dmaengine substream private data
  48 * @substream: PCM substream
  49 * @data: Data to set
  50 */
  51void snd_dmaengine_pcm_set_data(struct snd_pcm_substream *substream, void *data)
  52{
  53        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  54
  55        prtd->data = data;
  56}
  57EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_data);
  58
  59/**
  60 * snd_dmaengine_pcm_get_data - Get dmaeinge substream private data
  61 * @substream: PCM substream
  62 *
  63 * Returns the data previously set with snd_dmaengine_pcm_set_data
  64 */
  65void *snd_dmaengine_pcm_get_data(struct snd_pcm_substream *substream)
  66{
  67        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  68
  69        return prtd->data;
  70}
  71EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_data);
  72
  73struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream)
  74{
  75        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
  76
  77        return prtd->dma_chan;
  78}
  79EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan);
  80
  81/**
  82 * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config
  83 * @substream: PCM substream
  84 * @params: hw_params
  85 * @slave_config: DMA slave config
  86 *
  87 * This function can be used to initialize a dma_slave_config from a substream
  88 * and hw_params in a dmaengine based PCM driver implementation.
  89 */
  90int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream,
  91        const struct snd_pcm_hw_params *params,
  92        struct dma_slave_config *slave_config)
  93{
  94        enum dma_slave_buswidth buswidth;
  95
  96        switch (params_format(params)) {
  97        case SNDRV_PCM_FORMAT_S8:
  98                buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
  99                break;
 100        case SNDRV_PCM_FORMAT_S16_LE:
 101                buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
 102                break;
 103        case SNDRV_PCM_FORMAT_S18_3LE:
 104        case SNDRV_PCM_FORMAT_S20_3LE:
 105        case SNDRV_PCM_FORMAT_S24_LE:
 106        case SNDRV_PCM_FORMAT_S32_LE:
 107                buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
 108                break;
 109        default:
 110                return -EINVAL;
 111        }
 112
 113        if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
 114                slave_config->direction = DMA_MEM_TO_DEV;
 115                slave_config->dst_addr_width = buswidth;
 116        } else {
 117                slave_config->direction = DMA_DEV_TO_MEM;
 118                slave_config->src_addr_width = buswidth;
 119        }
 120
 121        return 0;
 122}
 123EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config);
 124
 125static void dmaengine_pcm_dma_complete(void *arg)
 126{
 127        struct snd_pcm_substream *substream = arg;
 128        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 129
 130        prtd->pos += snd_pcm_lib_period_bytes(substream);
 131        if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream))
 132                prtd->pos = 0;
 133
 134        snd_pcm_period_elapsed(substream);
 135}
 136
 137static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream)
 138{
 139        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 140        struct dma_chan *chan = prtd->dma_chan;
 141        struct dma_async_tx_descriptor *desc;
 142        enum dma_transfer_direction direction;
 143        unsigned long flags = DMA_CTRL_ACK;
 144
 145        direction = snd_pcm_substream_to_dma_direction(substream);
 146
 147        if (!substream->runtime->no_period_wakeup)
 148                flags |= DMA_PREP_INTERRUPT;
 149
 150        prtd->pos = 0;
 151        desc = dmaengine_prep_dma_cyclic(chan,
 152                substream->runtime->dma_addr,
 153                snd_pcm_lib_buffer_bytes(substream),
 154                snd_pcm_lib_period_bytes(substream), direction, flags);
 155
 156        if (!desc)
 157                return -ENOMEM;
 158
 159        desc->callback = dmaengine_pcm_dma_complete;
 160        desc->callback_param = substream;
 161        prtd->cookie = dmaengine_submit(desc);
 162
 163        return 0;
 164}
 165
 166/**
 167 * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation
 168 * @substream: PCM substream
 169 * @cmd: Trigger command
 170 *
 171 * Returns 0 on success, a negative error code otherwise.
 172 *
 173 * This function can be used as the PCM trigger callback for dmaengine based PCM
 174 * driver implementations.
 175 */
 176int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
 177{
 178        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 179        int ret;
 180
 181        switch (cmd) {
 182        case SNDRV_PCM_TRIGGER_START:
 183                ret = dmaengine_pcm_prepare_and_submit(substream);
 184                if (ret)
 185                        return ret;
 186                dma_async_issue_pending(prtd->dma_chan);
 187                break;
 188        case SNDRV_PCM_TRIGGER_RESUME:
 189        case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
 190                dmaengine_resume(prtd->dma_chan);
 191                break;
 192        case SNDRV_PCM_TRIGGER_SUSPEND:
 193        case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
 194                dmaengine_pause(prtd->dma_chan);
 195                break;
 196        case SNDRV_PCM_TRIGGER_STOP:
 197                dmaengine_terminate_all(prtd->dma_chan);
 198                break;
 199        default:
 200                return -EINVAL;
 201        }
 202
 203        return 0;
 204}
 205EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger);
 206
 207/**
 208 * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation
 209 * @substream: PCM substream
 210 *
 211 * This function is deprecated and should not be used by new drivers, as its
 212 * results may be unreliable.
 213 */
 214snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream)
 215{
 216        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 217        return bytes_to_frames(substream->runtime, prtd->pos);
 218}
 219EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue);
 220
 221/**
 222 * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation
 223 * @substream: PCM substream
 224 *
 225 * This function can be used as the PCM pointer callback for dmaengine based PCM
 226 * driver implementations.
 227 */
 228snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream)
 229{
 230        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 231        struct dma_tx_state state;
 232        enum dma_status status;
 233        unsigned int buf_size;
 234        unsigned int pos = 0;
 235
 236        status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state);
 237        if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) {
 238                buf_size = snd_pcm_lib_buffer_bytes(substream);
 239                if (state.residue > 0 && state.residue <= buf_size)
 240                        pos = buf_size - state.residue;
 241        }
 242
 243        return bytes_to_frames(substream->runtime, pos);
 244}
 245EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer);
 246
 247static int dmaengine_pcm_request_channel(struct dmaengine_pcm_runtime_data *prtd,
 248        dma_filter_fn filter_fn, void *filter_data)
 249{
 250        dma_cap_mask_t mask;
 251
 252        dma_cap_zero(mask);
 253        dma_cap_set(DMA_SLAVE, mask);
 254        dma_cap_set(DMA_CYCLIC, mask);
 255        prtd->dma_chan = dma_request_channel(mask, filter_fn, filter_data);
 256
 257        if (!prtd->dma_chan)
 258                return -ENXIO;
 259
 260        return 0;
 261}
 262
 263/**
 264 * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream
 265 * @substream: PCM substream
 266 * @filter_fn: Filter function used to request the DMA channel
 267 * @filter_data: Data passed to the DMA filter function
 268 *
 269 * Returns 0 on success, a negative error code otherwise.
 270 *
 271 * This function will request a DMA channel using the passed filter function and
 272 * data. The function should usually be called from the pcm open callback.
 273 *
 274 * Note that this function will use private_data field of the substream's
 275 * runtime. So it is not availabe to your pcm driver implementation. If you need
 276 * to keep additional data attached to a substream use
 277 * snd_dmaengine_pcm_{set,get}_data.
 278 */
 279int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream,
 280        dma_filter_fn filter_fn, void *filter_data)
 281{
 282        struct dmaengine_pcm_runtime_data *prtd;
 283        int ret;
 284
 285        ret = snd_pcm_hw_constraint_integer(substream->runtime,
 286                                            SNDRV_PCM_HW_PARAM_PERIODS);
 287        if (ret < 0)
 288                return ret;
 289
 290        prtd = kzalloc(sizeof(*prtd), GFP_KERNEL);
 291        if (!prtd)
 292                return -ENOMEM;
 293
 294        ret = dmaengine_pcm_request_channel(prtd, filter_fn, filter_data);
 295        if (ret < 0) {
 296                kfree(prtd);
 297                return ret;
 298        }
 299
 300        substream->runtime->private_data = prtd;
 301
 302        return 0;
 303}
 304EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open);
 305
 306/**
 307 * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream
 308 * @substream: PCM substream
 309 */
 310int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream)
 311{
 312        struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream);
 313
 314        dma_release_channel(prtd->dma_chan);
 315        kfree(prtd);
 316
 317        return 0;
 318}
 319EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close);
 320
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.