Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 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 | |
| 31 | struct dmaengine_pcm_runtime_data { |
| 32 | struct dma_chan *dma_chan; |
Lars-Peter Clausen | 3528f27 | 2012-06-11 20:11:42 +0200 | [diff] [blame] | 33 | dma_cookie_t cookie; |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 34 | |
| 35 | unsigned int pos; |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | static inline struct dmaengine_pcm_runtime_data *substream_to_prtd( |
| 39 | const struct snd_pcm_substream *substream) |
| 40 | { |
| 41 | return substream->runtime->private_data; |
| 42 | } |
| 43 | |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 44 | struct dma_chan *snd_dmaengine_pcm_get_chan(struct snd_pcm_substream *substream) |
| 45 | { |
| 46 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 47 | |
| 48 | return prtd->dma_chan; |
| 49 | } |
| 50 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_get_chan); |
| 51 | |
| 52 | /** |
| 53 | * snd_hwparams_to_dma_slave_config - Convert hw_params to dma_slave_config |
| 54 | * @substream: PCM substream |
| 55 | * @params: hw_params |
| 56 | * @slave_config: DMA slave config |
| 57 | * |
| 58 | * This function can be used to initialize a dma_slave_config from a substream |
| 59 | * and hw_params in a dmaengine based PCM driver implementation. |
| 60 | */ |
| 61 | int snd_hwparams_to_dma_slave_config(const struct snd_pcm_substream *substream, |
| 62 | const struct snd_pcm_hw_params *params, |
| 63 | struct dma_slave_config *slave_config) |
| 64 | { |
| 65 | enum dma_slave_buswidth buswidth; |
| 66 | |
| 67 | switch (params_format(params)) { |
| 68 | case SNDRV_PCM_FORMAT_S8: |
| 69 | buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 70 | break; |
| 71 | case SNDRV_PCM_FORMAT_S16_LE: |
| 72 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 73 | break; |
| 74 | case SNDRV_PCM_FORMAT_S18_3LE: |
| 75 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 76 | case SNDRV_PCM_FORMAT_S24_LE: |
| 77 | case SNDRV_PCM_FORMAT_S32_LE: |
| 78 | buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 79 | break; |
| 80 | default: |
| 81 | return -EINVAL; |
| 82 | } |
| 83 | |
| 84 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 85 | slave_config->direction = DMA_MEM_TO_DEV; |
| 86 | slave_config->dst_addr_width = buswidth; |
| 87 | } else { |
| 88 | slave_config->direction = DMA_DEV_TO_MEM; |
| 89 | slave_config->src_addr_width = buswidth; |
| 90 | } |
| 91 | |
Lars-Peter Clausen | 5fa70f7 | 2013-04-03 11:02:56 +0200 | [diff] [blame] | 92 | slave_config->device_fc = false; |
| 93 | |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 94 | return 0; |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(snd_hwparams_to_dma_slave_config); |
| 97 | |
Lars-Peter Clausen | 85c9f9c | 2013-04-03 11:06:02 +0200 | [diff] [blame] | 98 | /** |
| 99 | * snd_dmaengine_pcm_set_config_from_dai_data() - Initializes a dma slave config |
| 100 | * using DAI DMA data. |
| 101 | * @substream: PCM substream |
| 102 | * @dma_data: DAI DMA data |
| 103 | * @slave_config: DMA slave configuration |
| 104 | * |
| 105 | * Initializes the {dst,src}_addr, {dst,src}_maxburst, {dst,src}_addr_width and |
| 106 | * slave_id fields of the DMA slave config from the same fields of the DAI DMA |
| 107 | * data struct. The src and dst fields will be initialized depending on the |
| 108 | * direction of the substream. If the substream is a playback stream the dst |
| 109 | * fields will be initialized, if it is a capture stream the src fields will be |
| 110 | * initialized. The {dst,src}_addr_width field will only be initialized if the |
| 111 | * addr_width field of the DAI DMA data struct is not equal to |
| 112 | * DMA_SLAVE_BUSWIDTH_UNDEFINED. |
| 113 | */ |
| 114 | void snd_dmaengine_pcm_set_config_from_dai_data( |
| 115 | const struct snd_pcm_substream *substream, |
| 116 | const struct snd_dmaengine_dai_dma_data *dma_data, |
| 117 | struct dma_slave_config *slave_config) |
| 118 | { |
| 119 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 120 | slave_config->dst_addr = dma_data->addr; |
| 121 | slave_config->dst_maxburst = dma_data->maxburst; |
| 122 | if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED) |
| 123 | slave_config->dst_addr_width = dma_data->addr_width; |
| 124 | } else { |
| 125 | slave_config->src_addr = dma_data->addr; |
| 126 | slave_config->src_maxburst = dma_data->maxburst; |
| 127 | if (dma_data->addr_width != DMA_SLAVE_BUSWIDTH_UNDEFINED) |
| 128 | slave_config->src_addr_width = dma_data->addr_width; |
| 129 | } |
| 130 | |
| 131 | slave_config->slave_id = dma_data->slave_id; |
| 132 | } |
| 133 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_set_config_from_dai_data); |
| 134 | |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 135 | static void dmaengine_pcm_dma_complete(void *arg) |
| 136 | { |
| 137 | struct snd_pcm_substream *substream = arg; |
| 138 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 139 | |
| 140 | prtd->pos += snd_pcm_lib_period_bytes(substream); |
| 141 | if (prtd->pos >= snd_pcm_lib_buffer_bytes(substream)) |
| 142 | prtd->pos = 0; |
| 143 | |
| 144 | snd_pcm_period_elapsed(substream); |
| 145 | } |
| 146 | |
| 147 | static int dmaengine_pcm_prepare_and_submit(struct snd_pcm_substream *substream) |
| 148 | { |
| 149 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 150 | struct dma_chan *chan = prtd->dma_chan; |
| 151 | struct dma_async_tx_descriptor *desc; |
| 152 | enum dma_transfer_direction direction; |
Peter Ujfalusi | e7736cd | 2012-09-24 10:58:04 +0300 | [diff] [blame] | 153 | unsigned long flags = DMA_CTRL_ACK; |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 154 | |
| 155 | direction = snd_pcm_substream_to_dma_direction(substream); |
| 156 | |
Peter Ujfalusi | e7736cd | 2012-09-24 10:58:04 +0300 | [diff] [blame] | 157 | if (!substream->runtime->no_period_wakeup) |
| 158 | flags |= DMA_PREP_INTERRUPT; |
| 159 | |
Mika Westerberg | 7a08cf70 | 2012-03-05 14:02:14 +0100 | [diff] [blame] | 160 | prtd->pos = 0; |
Vinod Koul | 41ba6b7 | 2012-03-26 17:28:08 +0530 | [diff] [blame] | 161 | desc = dmaengine_prep_dma_cyclic(chan, |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 162 | substream->runtime->dma_addr, |
| 163 | snd_pcm_lib_buffer_bytes(substream), |
Peter Ujfalusi | e7736cd | 2012-09-24 10:58:04 +0300 | [diff] [blame] | 164 | snd_pcm_lib_period_bytes(substream), direction, flags); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 165 | |
| 166 | if (!desc) |
| 167 | return -ENOMEM; |
| 168 | |
| 169 | desc->callback = dmaengine_pcm_dma_complete; |
| 170 | desc->callback_param = substream; |
Lars-Peter Clausen | 3528f27 | 2012-06-11 20:11:42 +0200 | [diff] [blame] | 171 | prtd->cookie = dmaengine_submit(desc); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * snd_dmaengine_pcm_trigger - dmaengine based PCM trigger implementation |
| 178 | * @substream: PCM substream |
| 179 | * @cmd: Trigger command |
| 180 | * |
| 181 | * Returns 0 on success, a negative error code otherwise. |
| 182 | * |
| 183 | * This function can be used as the PCM trigger callback for dmaengine based PCM |
| 184 | * driver implementations. |
| 185 | */ |
| 186 | int snd_dmaengine_pcm_trigger(struct snd_pcm_substream *substream, int cmd) |
| 187 | { |
| 188 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 189 | int ret; |
| 190 | |
| 191 | switch (cmd) { |
| 192 | case SNDRV_PCM_TRIGGER_START: |
| 193 | ret = dmaengine_pcm_prepare_and_submit(substream); |
| 194 | if (ret) |
| 195 | return ret; |
| 196 | dma_async_issue_pending(prtd->dma_chan); |
| 197 | break; |
| 198 | case SNDRV_PCM_TRIGGER_RESUME: |
| 199 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 200 | dmaengine_resume(prtd->dma_chan); |
| 201 | break; |
| 202 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 203 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 204 | dmaengine_pause(prtd->dma_chan); |
| 205 | break; |
| 206 | case SNDRV_PCM_TRIGGER_STOP: |
| 207 | dmaengine_terminate_all(prtd->dma_chan); |
| 208 | break; |
| 209 | default: |
| 210 | return -EINVAL; |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_trigger); |
| 216 | |
| 217 | /** |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 218 | * snd_dmaengine_pcm_pointer_no_residue - dmaengine based PCM pointer implementation |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 219 | * @substream: PCM substream |
| 220 | * |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 221 | * This function is deprecated and should not be used by new drivers, as its |
| 222 | * results may be unreliable. |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 223 | */ |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 224 | snd_pcm_uframes_t snd_dmaengine_pcm_pointer_no_residue(struct snd_pcm_substream *substream) |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 225 | { |
| 226 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 227 | return bytes_to_frames(substream->runtime, prtd->pos); |
| 228 | } |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 229 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer_no_residue); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 230 | |
Lars-Peter Clausen | 3528f27 | 2012-06-11 20:11:42 +0200 | [diff] [blame] | 231 | /** |
| 232 | * snd_dmaengine_pcm_pointer - dmaengine based PCM pointer implementation |
| 233 | * @substream: PCM substream |
| 234 | * |
| 235 | * This function can be used as the PCM pointer callback for dmaengine based PCM |
| 236 | * driver implementations. |
| 237 | */ |
| 238 | snd_pcm_uframes_t snd_dmaengine_pcm_pointer(struct snd_pcm_substream *substream) |
| 239 | { |
| 240 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 241 | struct dma_tx_state state; |
| 242 | enum dma_status status; |
| 243 | unsigned int buf_size; |
| 244 | unsigned int pos = 0; |
| 245 | |
| 246 | status = dmaengine_tx_status(prtd->dma_chan, prtd->cookie, &state); |
| 247 | if (status == DMA_IN_PROGRESS || status == DMA_PAUSED) { |
| 248 | buf_size = snd_pcm_lib_buffer_bytes(substream); |
| 249 | if (state.residue > 0 && state.residue <= buf_size) |
| 250 | pos = buf_size - state.residue; |
| 251 | } |
| 252 | |
| 253 | return bytes_to_frames(substream->runtime, pos); |
| 254 | } |
| 255 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_pointer); |
| 256 | |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 257 | /** |
| 258 | * snd_dmaengine_pcm_request_channel - Request channel for the dmaengine PCM |
| 259 | * @filter_fn: Filter function used to request the DMA channel |
| 260 | * @filter_data: Data passed to the DMA filter function |
| 261 | * |
| 262 | * Returns NULL or the requested DMA channel. |
| 263 | * |
| 264 | * This function request a DMA channel for usage with dmaengine PCM. |
| 265 | */ |
| 266 | struct dma_chan *snd_dmaengine_pcm_request_channel(dma_filter_fn filter_fn, |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 267 | void *filter_data) |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 268 | { |
| 269 | dma_cap_mask_t mask; |
| 270 | |
| 271 | dma_cap_zero(mask); |
| 272 | dma_cap_set(DMA_SLAVE, mask); |
| 273 | dma_cap_set(DMA_CYCLIC, mask); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 274 | |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 275 | return dma_request_channel(mask, filter_fn, filter_data); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 276 | } |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_request_channel); |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 278 | |
| 279 | /** |
| 280 | * snd_dmaengine_pcm_open - Open a dmaengine based PCM substream |
| 281 | * @substream: PCM substream |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 282 | * @chan: DMA channel to use for data transfers |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 283 | * |
| 284 | * Returns 0 on success, a negative error code otherwise. |
| 285 | * |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 286 | * The function should usually be called from the pcm open callback. Note that |
| 287 | * this function will use private_data field of the substream's runtime. So it |
| 288 | * is not availabe to your pcm driver implementation. |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 289 | */ |
| 290 | int snd_dmaengine_pcm_open(struct snd_pcm_substream *substream, |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 291 | struct dma_chan *chan) |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 292 | { |
| 293 | struct dmaengine_pcm_runtime_data *prtd; |
| 294 | int ret; |
| 295 | |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 296 | if (!chan) |
| 297 | return -ENXIO; |
| 298 | |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 299 | ret = snd_pcm_hw_constraint_integer(substream->runtime, |
| 300 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 301 | if (ret < 0) |
| 302 | return ret; |
| 303 | |
| 304 | prtd = kzalloc(sizeof(*prtd), GFP_KERNEL); |
| 305 | if (!prtd) |
| 306 | return -ENOMEM; |
| 307 | |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 308 | prtd->dma_chan = chan; |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 309 | |
| 310 | substream->runtime->private_data = prtd; |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open); |
| 315 | |
| 316 | /** |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 317 | * snd_dmaengine_pcm_open_request_chan - Open a dmaengine based PCM substream and request channel |
| 318 | * @substream: PCM substream |
| 319 | * @filter_fn: Filter function used to request the DMA channel |
| 320 | * @filter_data: Data passed to the DMA filter function |
| 321 | * |
| 322 | * Returns 0 on success, a negative error code otherwise. |
| 323 | * |
| 324 | * This function will request a DMA channel using the passed filter function and |
| 325 | * data. The function should usually be called from the pcm open callback. Note |
| 326 | * that this function will use private_data field of the substream's runtime. So |
| 327 | * it is not availabe to your pcm driver implementation. |
| 328 | */ |
| 329 | int snd_dmaengine_pcm_open_request_chan(struct snd_pcm_substream *substream, |
| 330 | dma_filter_fn filter_fn, void *filter_data) |
| 331 | { |
| 332 | return snd_dmaengine_pcm_open(substream, |
Lars-Peter Clausen | c999836 | 2013-04-15 19:19:51 +0200 | [diff] [blame] | 333 | snd_dmaengine_pcm_request_channel(filter_fn, filter_data)); |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 334 | } |
| 335 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_open_request_chan); |
| 336 | |
| 337 | /** |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 338 | * snd_dmaengine_pcm_close - Close a dmaengine based PCM substream |
| 339 | * @substream: PCM substream |
| 340 | */ |
| 341 | int snd_dmaengine_pcm_close(struct snd_pcm_substream *substream) |
| 342 | { |
| 343 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 344 | |
Lars-Peter Clausen | e7f73a1 | 2012-02-22 10:49:08 +0100 | [diff] [blame] | 345 | kfree(prtd); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close); |
Lothar Waßmann | cba69b4 | 2012-11-22 13:31:10 +0100 | [diff] [blame] | 350 | |
Lars-Peter Clausen | 7c1c1d4 | 2013-04-15 19:19:48 +0200 | [diff] [blame] | 351 | /** |
| 352 | * snd_dmaengine_pcm_release_chan_close - Close a dmaengine based PCM substream and release channel |
| 353 | * @substream: PCM substream |
| 354 | * |
| 355 | * Releases the DMA channel associated with the PCM substream. |
| 356 | */ |
| 357 | int snd_dmaengine_pcm_close_release_chan(struct snd_pcm_substream *substream) |
| 358 | { |
| 359 | struct dmaengine_pcm_runtime_data *prtd = substream_to_prtd(substream); |
| 360 | |
| 361 | dma_release_channel(prtd->dma_chan); |
| 362 | |
| 363 | return snd_dmaengine_pcm_close(substream); |
| 364 | } |
| 365 | EXPORT_SYMBOL_GPL(snd_dmaengine_pcm_close_release_chan); |
| 366 | |
Lothar Waßmann | cba69b4 | 2012-11-22 13:31:10 +0100 | [diff] [blame] | 367 | MODULE_LICENSE("GPL"); |