Ola Lilja | 01a0c11 | 2012-05-24 15:26:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2012 |
| 3 | * |
| 4 | * Author: Ola Lilja <ola.o.lilja@stericsson.com>, |
| 5 | * Roger Nilsson <roger.xr.nilsson@stericsson.com> |
| 6 | * for ST-Ericsson. |
| 7 | * |
| 8 | * License terms: |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as published |
| 12 | * by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <asm/page.h> |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/dmaengine.h> |
| 20 | #include <linux/slab.h> |
Linus Walleij | 865fab6 | 2012-10-18 14:20:16 +0200 | [diff] [blame^] | 21 | #include <linux/platform_data/dma-ste-dma40.h> |
Ola Lilja | 01a0c11 | 2012-05-24 15:26:32 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sound/pcm.h> |
| 24 | #include <sound/pcm_params.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/dmaengine_pcm.h> |
| 27 | |
| 28 | #include "ux500_msp_i2s.h" |
| 29 | #include "ux500_pcm.h" |
| 30 | |
| 31 | static struct snd_pcm_hardware ux500_pcm_hw_playback = { |
| 32 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 33 | SNDRV_PCM_INFO_MMAP | |
| 34 | SNDRV_PCM_INFO_RESUME | |
| 35 | SNDRV_PCM_INFO_PAUSE, |
| 36 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 37 | SNDRV_PCM_FMTBIT_U16_LE | |
| 38 | SNDRV_PCM_FMTBIT_S16_BE | |
| 39 | SNDRV_PCM_FMTBIT_U16_BE, |
| 40 | .rates = SNDRV_PCM_RATE_KNOT, |
| 41 | .rate_min = UX500_PLATFORM_MIN_RATE_PLAYBACK, |
| 42 | .rate_max = UX500_PLATFORM_MAX_RATE_PLAYBACK, |
| 43 | .channels_min = UX500_PLATFORM_MIN_CHANNELS, |
| 44 | .channels_max = UX500_PLATFORM_MAX_CHANNELS, |
| 45 | .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX, |
| 46 | .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN, |
| 47 | .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX, |
| 48 | .periods_min = UX500_PLATFORM_PERIODS_MIN, |
| 49 | .periods_max = UX500_PLATFORM_PERIODS_MAX, |
| 50 | }; |
| 51 | |
| 52 | static struct snd_pcm_hardware ux500_pcm_hw_capture = { |
| 53 | .info = SNDRV_PCM_INFO_INTERLEAVED | |
| 54 | SNDRV_PCM_INFO_MMAP | |
| 55 | SNDRV_PCM_INFO_RESUME | |
| 56 | SNDRV_PCM_INFO_PAUSE, |
| 57 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 58 | SNDRV_PCM_FMTBIT_U16_LE | |
| 59 | SNDRV_PCM_FMTBIT_S16_BE | |
| 60 | SNDRV_PCM_FMTBIT_U16_BE, |
| 61 | .rates = SNDRV_PCM_RATE_KNOT, |
| 62 | .rate_min = UX500_PLATFORM_MIN_RATE_CAPTURE, |
| 63 | .rate_max = UX500_PLATFORM_MAX_RATE_CAPTURE, |
| 64 | .channels_min = UX500_PLATFORM_MIN_CHANNELS, |
| 65 | .channels_max = UX500_PLATFORM_MAX_CHANNELS, |
| 66 | .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX, |
| 67 | .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN, |
| 68 | .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX, |
| 69 | .periods_min = UX500_PLATFORM_PERIODS_MIN, |
| 70 | .periods_max = UX500_PLATFORM_PERIODS_MAX, |
| 71 | }; |
| 72 | |
| 73 | static void ux500_pcm_dma_hw_free(struct device *dev, |
| 74 | struct snd_pcm_substream *substream) |
| 75 | { |
| 76 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 77 | struct snd_dma_buffer *buf = runtime->dma_buffer_p; |
| 78 | |
| 79 | if (runtime->dma_area == NULL) |
| 80 | return; |
| 81 | |
| 82 | if (buf != &substream->dma_buffer) { |
| 83 | dma_free_coherent(buf->dev.dev, buf->bytes, buf->area, |
| 84 | buf->addr); |
| 85 | kfree(runtime->dma_buffer_p); |
| 86 | } |
| 87 | |
| 88 | snd_pcm_set_runtime_buffer(substream, NULL); |
| 89 | } |
| 90 | |
| 91 | static int ux500_pcm_open(struct snd_pcm_substream *substream) |
| 92 | { |
| 93 | int stream_id = substream->pstr->stream; |
| 94 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 95 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 96 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 97 | struct device *dev = dai->dev; |
| 98 | int ret; |
| 99 | struct ux500_msp_dma_params *dma_params; |
| 100 | u16 per_data_width, mem_data_width; |
| 101 | struct stedma40_chan_cfg *dma_cfg; |
| 102 | |
| 103 | dev_dbg(dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id, |
| 104 | snd_pcm_stream_str(substream)); |
| 105 | |
| 106 | dev_dbg(dev, "%s: Set runtime hwparams.\n", __func__); |
| 107 | if (stream_id == SNDRV_PCM_STREAM_PLAYBACK) |
| 108 | snd_soc_set_runtime_hwparams(substream, |
| 109 | &ux500_pcm_hw_playback); |
| 110 | else |
| 111 | snd_soc_set_runtime_hwparams(substream, |
| 112 | &ux500_pcm_hw_capture); |
| 113 | |
| 114 | /* ensure that buffer size is a multiple of period size */ |
| 115 | ret = snd_pcm_hw_constraint_integer(runtime, |
| 116 | SNDRV_PCM_HW_PARAM_PERIODS); |
| 117 | if (ret < 0) { |
| 118 | dev_err(dev, "%s: Error: snd_pcm_hw_constraints failed (%d)\n", |
| 119 | __func__, ret); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | dev_dbg(dev, "%s: Set hw-struct for %s.\n", __func__, |
| 124 | snd_pcm_stream_str(substream)); |
| 125 | runtime->hw = (stream_id == SNDRV_PCM_STREAM_PLAYBACK) ? |
| 126 | ux500_pcm_hw_playback : ux500_pcm_hw_capture; |
| 127 | |
| 128 | mem_data_width = STEDMA40_HALFWORD_WIDTH; |
| 129 | |
| 130 | dma_params = snd_soc_dai_get_dma_data(dai, substream); |
| 131 | switch (dma_params->data_size) { |
| 132 | case 32: |
| 133 | per_data_width = STEDMA40_WORD_WIDTH; |
| 134 | break; |
| 135 | case 16: |
| 136 | per_data_width = STEDMA40_HALFWORD_WIDTH; |
| 137 | break; |
| 138 | case 8: |
| 139 | per_data_width = STEDMA40_BYTE_WIDTH; |
| 140 | break; |
| 141 | default: |
| 142 | per_data_width = STEDMA40_WORD_WIDTH; |
| 143 | dev_warn(rtd->platform->dev, |
| 144 | "%s: Unknown data-size (%d)! Assuming 32 bits.\n", |
| 145 | __func__, dma_params->data_size); |
| 146 | } |
| 147 | |
| 148 | dma_cfg = dma_params->dma_cfg; |
| 149 | |
| 150 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { |
| 151 | dma_cfg->src_info.data_width = mem_data_width; |
| 152 | dma_cfg->dst_info.data_width = per_data_width; |
| 153 | } else { |
| 154 | dma_cfg->src_info.data_width = per_data_width; |
| 155 | dma_cfg->dst_info.data_width = mem_data_width; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | ret = snd_dmaengine_pcm_open(substream, stedma40_filter, dma_cfg); |
| 160 | if (ret) { |
| 161 | dev_dbg(dai->dev, |
| 162 | "%s: ERROR: snd_dmaengine_pcm_open failed (%d)!\n", |
| 163 | __func__, ret); |
| 164 | return ret; |
| 165 | } |
| 166 | |
| 167 | snd_dmaengine_pcm_set_data(substream, dma_cfg); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int ux500_pcm_close(struct snd_pcm_substream *substream) |
| 173 | { |
| 174 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 175 | struct snd_soc_dai *dai = rtd->cpu_dai; |
| 176 | |
| 177 | dev_dbg(dai->dev, "%s: Enter\n", __func__); |
| 178 | |
| 179 | snd_dmaengine_pcm_close(substream); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int ux500_pcm_hw_params(struct snd_pcm_substream *substream, |
| 185 | struct snd_pcm_hw_params *hw_params) |
| 186 | { |
| 187 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 188 | struct snd_dma_buffer *buf = runtime->dma_buffer_p; |
| 189 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 190 | int ret = 0; |
| 191 | int size; |
| 192 | |
| 193 | dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__); |
| 194 | |
| 195 | size = params_buffer_bytes(hw_params); |
| 196 | |
| 197 | if (buf) { |
| 198 | if (buf->bytes >= size) |
| 199 | goto out; |
| 200 | ux500_pcm_dma_hw_free(NULL, substream); |
| 201 | } |
| 202 | |
| 203 | if (substream->dma_buffer.area != NULL && |
| 204 | substream->dma_buffer.bytes >= size) { |
| 205 | buf = &substream->dma_buffer; |
| 206 | } else { |
| 207 | buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL); |
| 208 | if (!buf) |
| 209 | goto nomem; |
| 210 | |
| 211 | buf->dev.type = SNDRV_DMA_TYPE_DEV; |
| 212 | buf->dev.dev = NULL; |
| 213 | buf->area = dma_alloc_coherent(NULL, size, &buf->addr, |
| 214 | GFP_KERNEL); |
| 215 | buf->bytes = size; |
| 216 | buf->private_data = NULL; |
| 217 | |
| 218 | if (!buf->area) |
| 219 | goto free; |
| 220 | } |
| 221 | snd_pcm_set_runtime_buffer(substream, buf); |
| 222 | ret = 1; |
| 223 | out: |
| 224 | runtime->dma_bytes = size; |
| 225 | return ret; |
| 226 | |
| 227 | free: |
| 228 | kfree(buf); |
| 229 | nomem: |
| 230 | return -ENOMEM; |
| 231 | } |
| 232 | |
| 233 | static int ux500_pcm_hw_free(struct snd_pcm_substream *substream) |
| 234 | { |
| 235 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 236 | |
| 237 | dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__); |
| 238 | |
| 239 | ux500_pcm_dma_hw_free(NULL, substream); |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | static int ux500_pcm_mmap(struct snd_pcm_substream *substream, |
| 245 | struct vm_area_struct *vma) |
| 246 | { |
| 247 | struct snd_pcm_runtime *runtime = substream->runtime; |
| 248 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 249 | |
| 250 | dev_dbg(rtd->platform->dev, "%s: Enter.\n", __func__); |
| 251 | |
| 252 | return dma_mmap_coherent(NULL, vma, runtime->dma_area, |
| 253 | runtime->dma_addr, runtime->dma_bytes); |
| 254 | } |
| 255 | |
| 256 | static struct snd_pcm_ops ux500_pcm_ops = { |
| 257 | .open = ux500_pcm_open, |
| 258 | .close = ux500_pcm_close, |
| 259 | .ioctl = snd_pcm_lib_ioctl, |
| 260 | .hw_params = ux500_pcm_hw_params, |
| 261 | .hw_free = ux500_pcm_hw_free, |
| 262 | .trigger = snd_dmaengine_pcm_trigger, |
Lars-Peter Clausen | 9883ab2 | 2012-06-11 20:11:41 +0200 | [diff] [blame] | 263 | .pointer = snd_dmaengine_pcm_pointer_no_residue, |
Ola Lilja | 01a0c11 | 2012-05-24 15:26:32 +0200 | [diff] [blame] | 264 | .mmap = ux500_pcm_mmap |
| 265 | }; |
| 266 | |
| 267 | int ux500_pcm_new(struct snd_soc_pcm_runtime *rtd) |
| 268 | { |
| 269 | struct snd_pcm *pcm = rtd->pcm; |
| 270 | |
| 271 | dev_dbg(rtd->platform->dev, "%s: Enter (id = '%s').\n", __func__, |
| 272 | pcm->id); |
| 273 | |
| 274 | pcm->info_flags = 0; |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static struct snd_soc_platform_driver ux500_pcm_soc_drv = { |
| 280 | .ops = &ux500_pcm_ops, |
| 281 | .pcm_new = ux500_pcm_new, |
| 282 | }; |
| 283 | |
| 284 | static int __devexit ux500_pcm_drv_probe(struct platform_device *pdev) |
| 285 | { |
| 286 | int ret; |
| 287 | |
| 288 | ret = snd_soc_register_platform(&pdev->dev, &ux500_pcm_soc_drv); |
| 289 | if (ret < 0) { |
| 290 | dev_err(&pdev->dev, |
| 291 | "%s: ERROR: Failed to register platform '%s' (%d)!\n", |
| 292 | __func__, pdev->name, ret); |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int __devinit ux500_pcm_drv_remove(struct platform_device *pdev) |
| 300 | { |
| 301 | snd_soc_unregister_platform(&pdev->dev); |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static struct platform_driver ux500_pcm_driver = { |
| 307 | .driver = { |
| 308 | .name = "ux500-pcm", |
| 309 | .owner = THIS_MODULE, |
| 310 | }, |
| 311 | |
| 312 | .probe = ux500_pcm_drv_probe, |
| 313 | .remove = __devexit_p(ux500_pcm_drv_remove), |
| 314 | }; |
| 315 | module_platform_driver(ux500_pcm_driver); |
| 316 | |
Ola Lilja | 85f24391 | 2012-06-13 10:09:51 +0200 | [diff] [blame] | 317 | MODULE_LICENSE("GPL v2"); |