Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
omap-pcm.c
Go to the documentation of this file.
1 /*
2  * omap-pcm.c -- ALSA PCM interface for the OMAP SoC
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Jarkko Nikula <[email protected]>
7  * Peter Ujfalusi <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #include <linux/dma-mapping.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/omap-dma.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/dmaengine_pcm.h>
33 #include <sound/soc.h>
34 
35 #include <plat/cpu.h>
36 #include "omap-pcm.h"
37 
38 static const struct snd_pcm_hardware omap_pcm_hardware = {
39  .info = SNDRV_PCM_INFO_MMAP |
45  .formats = SNDRV_PCM_FMTBIT_S16_LE |
47  .period_bytes_min = 32,
48  .period_bytes_max = 64 * 1024,
49  .periods_min = 2,
50  .periods_max = 255,
51  .buffer_bytes_max = 128 * 1024,
52 };
53 
54 static int omap_pcm_get_dma_buswidth(int num_bits)
55 {
56  int buswidth;
57 
58  switch (num_bits) {
59  case 16:
60  buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
61  break;
62  case 32:
63  buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
64  break;
65  default:
66  buswidth = -EINVAL;
67  break;
68  }
69  return buswidth;
70 }
71 
72 
73 /* this may get called several times by oss emulation */
74 static int omap_pcm_hw_params(struct snd_pcm_substream *substream,
75  struct snd_pcm_hw_params *params)
76 {
77  struct snd_pcm_runtime *runtime = substream->runtime;
78  struct snd_soc_pcm_runtime *rtd = substream->private_data;
79  struct omap_pcm_dma_data *dma_data;
80  struct dma_slave_config config;
81  struct dma_chan *chan;
82  int err = 0;
83 
84  dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
85 
86  /* return if this is a bufferless transfer e.g.
87  * codec <--> BT codec or GSM modem -- lg FIXME */
88  if (!dma_data)
89  return 0;
90 
91  snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
92  runtime->dma_bytes = params_buffer_bytes(params);
93 
94  chan = snd_dmaengine_pcm_get_chan(substream);
95  if (!chan)
96  return -EINVAL;
97 
98  /* fills in addr_width and direction */
99  err = snd_hwparams_to_dma_slave_config(substream, params, &config);
100  if (err)
101  return err;
102 
103  /* Override the *_dma addr_width if requested by the DAI driver */
104  if (dma_data->data_type) {
105  int buswidth = omap_pcm_get_dma_buswidth(dma_data->data_type);
106 
107  if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
108  config.dst_addr_width = buswidth;
109  else
110  config.src_addr_width = buswidth;
111  }
112 
113  config.src_addr = dma_data->port_addr;
114  config.dst_addr = dma_data->port_addr;
115  config.src_maxburst = dma_data->packet_size;
116  config.dst_maxburst = dma_data->packet_size;
117 
118  return dmaengine_slave_config(chan, &config);
119 }
120 
121 static int omap_pcm_hw_free(struct snd_pcm_substream *substream)
122 {
123  snd_pcm_set_runtime_buffer(substream, NULL);
124  return 0;
125 }
126 
127 static int omap_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
128 {
129  struct snd_soc_pcm_runtime *rtd = substream->private_data;
130  struct omap_pcm_dma_data *dma_data;
131  int ret = 0;
132 
133  dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
134 
135  switch (cmd) {
139  /* Configure McBSP internal buffer usage */
140  if (dma_data->set_threshold)
141  dma_data->set_threshold(substream);
142  break;
143 
147  break;
148  default:
149  ret = -EINVAL;
150  }
151 
152  if (ret == 0)
153  ret = snd_dmaengine_pcm_trigger(substream, cmd);
154 
155  return ret;
156 }
157 
158 static snd_pcm_uframes_t omap_pcm_pointer(struct snd_pcm_substream *substream)
159 {
161 
162  if (cpu_is_omap1510())
163  offset = snd_dmaengine_pcm_pointer_no_residue(substream);
164  else
165  offset = snd_dmaengine_pcm_pointer(substream);
166 
167  return offset;
168 }
169 
170 static int omap_pcm_open(struct snd_pcm_substream *substream)
171 {
172  struct snd_pcm_runtime *runtime = substream->runtime;
173  struct snd_soc_pcm_runtime *rtd = substream->private_data;
174  struct omap_pcm_dma_data *dma_data;
175  int ret;
176 
177  snd_soc_set_runtime_hwparams(substream, &omap_pcm_hardware);
178 
179  /* Ensure that buffer size is a multiple of period size */
180  ret = snd_pcm_hw_constraint_integer(runtime,
182  if (ret < 0)
183  return ret;
184 
185  dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
187  &dma_data->dma_req);
188  return ret;
189 }
190 
191 static int omap_pcm_close(struct snd_pcm_substream *substream)
192 {
193  snd_dmaengine_pcm_close(substream);
194  return 0;
195 }
196 
197 static int omap_pcm_mmap(struct snd_pcm_substream *substream,
198  struct vm_area_struct *vma)
199 {
200  struct snd_pcm_runtime *runtime = substream->runtime;
201 
202  return dma_mmap_writecombine(substream->pcm->card->dev, vma,
203  runtime->dma_area,
204  runtime->dma_addr,
205  runtime->dma_bytes);
206 }
207 
208 static struct snd_pcm_ops omap_pcm_ops = {
209  .open = omap_pcm_open,
210  .close = omap_pcm_close,
211  .ioctl = snd_pcm_lib_ioctl,
212  .hw_params = omap_pcm_hw_params,
213  .hw_free = omap_pcm_hw_free,
214  .trigger = omap_pcm_trigger,
215  .pointer = omap_pcm_pointer,
216  .mmap = omap_pcm_mmap,
217 };
218 
219 static u64 omap_pcm_dmamask = DMA_BIT_MASK(64);
220 
221 static int omap_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
222  int stream)
223 {
224  struct snd_pcm_substream *substream = pcm->streams[stream].substream;
225  struct snd_dma_buffer *buf = &substream->dma_buffer;
226  size_t size = omap_pcm_hardware.buffer_bytes_max;
227 
228  buf->dev.type = SNDRV_DMA_TYPE_DEV;
229  buf->dev.dev = pcm->card->dev;
230  buf->private_data = NULL;
231  buf->area = dma_alloc_writecombine(pcm->card->dev, size,
232  &buf->addr, GFP_KERNEL);
233  if (!buf->area)
234  return -ENOMEM;
235 
236  buf->bytes = size;
237  return 0;
238 }
239 
240 static void omap_pcm_free_dma_buffers(struct snd_pcm *pcm)
241 {
242  struct snd_pcm_substream *substream;
243  struct snd_dma_buffer *buf;
244  int stream;
245 
246  for (stream = 0; stream < 2; stream++) {
247  substream = pcm->streams[stream].substream;
248  if (!substream)
249  continue;
250 
251  buf = &substream->dma_buffer;
252  if (!buf->area)
253  continue;
254 
255  dma_free_writecombine(pcm->card->dev, buf->bytes,
256  buf->area, buf->addr);
257  buf->area = NULL;
258  }
259 }
260 
261 static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
262 {
263  struct snd_card *card = rtd->card->snd_card;
264  struct snd_pcm *pcm = rtd->pcm;
265  int ret = 0;
266 
267  if (!card->dev->dma_mask)
268  card->dev->dma_mask = &omap_pcm_dmamask;
269  if (!card->dev->coherent_dma_mask)
270  card->dev->coherent_dma_mask = DMA_BIT_MASK(64);
271 
272  if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
273  ret = omap_pcm_preallocate_dma_buffer(pcm,
275  if (ret)
276  goto out;
277  }
278 
279  if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
280  ret = omap_pcm_preallocate_dma_buffer(pcm,
282  if (ret)
283  goto out;
284  }
285 
286 out:
287  /* free preallocated buffers in case of error */
288  if (ret)
289  omap_pcm_free_dma_buffers(pcm);
290 
291  return ret;
292 }
293 
294 static struct snd_soc_platform_driver omap_soc_platform = {
295  .ops = &omap_pcm_ops,
296  .pcm_new = omap_pcm_new,
297  .pcm_free = omap_pcm_free_dma_buffers,
298 };
299 
300 static __devinit int omap_pcm_probe(struct platform_device *pdev)
301 {
302  return snd_soc_register_platform(&pdev->dev,
303  &omap_soc_platform);
304 }
305 
306 static int __devexit omap_pcm_remove(struct platform_device *pdev)
307 {
309  return 0;
310 }
311 
312 static struct platform_driver omap_pcm_driver = {
313  .driver = {
314  .name = "omap-pcm-audio",
315  .owner = THIS_MODULE,
316  },
317 
318  .probe = omap_pcm_probe,
319  .remove = __devexit_p(omap_pcm_remove),
320 };
321 
322 module_platform_driver(omap_pcm_driver);
323 
324 MODULE_AUTHOR("Jarkko Nikula <[email protected]>");
325 MODULE_DESCRIPTION("OMAP PCM DMA module");
326 MODULE_LICENSE("GPL");
327 MODULE_ALIAS("platform:omap-pcm-audio");