pxa2xx-pcm-lib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License version 2 as
  4. * published by the Free Software Foundation.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/dma/pxa-dma.h>
  11. #include <sound/core.h>
  12. #include <sound/pcm.h>
  13. #include <sound/pcm_params.h>
  14. #include <sound/pxa2xx-lib.h>
  15. #include <sound/dmaengine_pcm.h>
  16. static const struct snd_pcm_hardware pxa2xx_pcm_hardware = {
  17. .info = SNDRV_PCM_INFO_MMAP |
  18. SNDRV_PCM_INFO_MMAP_VALID |
  19. SNDRV_PCM_INFO_INTERLEAVED |
  20. SNDRV_PCM_INFO_PAUSE |
  21. SNDRV_PCM_INFO_RESUME,
  22. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  23. SNDRV_PCM_FMTBIT_S24_LE |
  24. SNDRV_PCM_FMTBIT_S32_LE,
  25. .period_bytes_min = 32,
  26. .period_bytes_max = 8192 - 32,
  27. .periods_min = 1,
  28. .periods_max = 256,
  29. .buffer_bytes_max = 128 * 1024,
  30. .fifo_size = 32,
  31. };
  32. int pxa2xx_pcm_hw_params(struct snd_pcm_substream *substream,
  33. struct snd_pcm_hw_params *params)
  34. {
  35. struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
  36. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  37. struct snd_dmaengine_dai_dma_data *dma_params;
  38. struct dma_slave_config config;
  39. int ret;
  40. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  41. if (!dma_params)
  42. return 0;
  43. ret = snd_hwparams_to_dma_slave_config(substream, params, &config);
  44. if (ret)
  45. return ret;
  46. snd_dmaengine_pcm_set_config_from_dai_data(substream,
  47. snd_soc_dai_get_dma_data(rtd->cpu_dai, substream),
  48. &config);
  49. ret = dmaengine_slave_config(chan, &config);
  50. if (ret)
  51. return ret;
  52. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(pxa2xx_pcm_hw_params);
  56. int pxa2xx_pcm_hw_free(struct snd_pcm_substream *substream)
  57. {
  58. snd_pcm_set_runtime_buffer(substream, NULL);
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(pxa2xx_pcm_hw_free);
  62. int pxa2xx_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  63. {
  64. return snd_dmaengine_pcm_trigger(substream, cmd);
  65. }
  66. EXPORT_SYMBOL(pxa2xx_pcm_trigger);
  67. snd_pcm_uframes_t
  68. pxa2xx_pcm_pointer(struct snd_pcm_substream *substream)
  69. {
  70. return snd_dmaengine_pcm_pointer(substream);
  71. }
  72. EXPORT_SYMBOL(pxa2xx_pcm_pointer);
  73. int pxa2xx_pcm_prepare(struct snd_pcm_substream *substream)
  74. {
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(pxa2xx_pcm_prepare);
  78. int pxa2xx_pcm_open(struct snd_pcm_substream *substream)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct snd_pcm_runtime *runtime = substream->runtime;
  82. struct snd_dmaengine_dai_dma_data *dma_params;
  83. int ret;
  84. runtime->hw = pxa2xx_pcm_hardware;
  85. dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  86. if (!dma_params)
  87. return 0;
  88. /*
  89. * For mysterious reasons (and despite what the manual says)
  90. * playback samples are lost if the DMA count is not a multiple
  91. * of the DMA burst size. Let's add a rule to enforce that.
  92. */
  93. ret = snd_pcm_hw_constraint_step(runtime, 0,
  94. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  95. if (ret)
  96. return ret;
  97. ret = snd_pcm_hw_constraint_step(runtime, 0,
  98. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  99. if (ret)
  100. return ret;
  101. ret = snd_pcm_hw_constraint_integer(runtime,
  102. SNDRV_PCM_HW_PARAM_PERIODS);
  103. if (ret < 0)
  104. return ret;
  105. return snd_dmaengine_pcm_open(
  106. substream, dma_request_slave_channel(rtd->cpu_dai->dev,
  107. dma_params->chan_name));
  108. }
  109. EXPORT_SYMBOL(pxa2xx_pcm_open);
  110. int pxa2xx_pcm_close(struct snd_pcm_substream *substream)
  111. {
  112. return snd_dmaengine_pcm_close_release_chan(substream);
  113. }
  114. EXPORT_SYMBOL(pxa2xx_pcm_close);
  115. int pxa2xx_pcm_mmap(struct snd_pcm_substream *substream,
  116. struct vm_area_struct *vma)
  117. {
  118. struct snd_pcm_runtime *runtime = substream->runtime;
  119. return dma_mmap_wc(substream->pcm->card->dev, vma, runtime->dma_area,
  120. runtime->dma_addr, runtime->dma_bytes);
  121. }
  122. EXPORT_SYMBOL(pxa2xx_pcm_mmap);
  123. int pxa2xx_pcm_preallocate_dma_buffer(struct snd_pcm *pcm, int stream)
  124. {
  125. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  126. struct snd_dma_buffer *buf = &substream->dma_buffer;
  127. size_t size = pxa2xx_pcm_hardware.buffer_bytes_max;
  128. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  129. buf->dev.dev = pcm->card->dev;
  130. buf->private_data = NULL;
  131. buf->area = dma_alloc_wc(pcm->card->dev, size, &buf->addr, GFP_KERNEL);
  132. if (!buf->area)
  133. return -ENOMEM;
  134. buf->bytes = size;
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(pxa2xx_pcm_preallocate_dma_buffer);
  138. void pxa2xx_pcm_free_dma_buffers(struct snd_pcm *pcm)
  139. {
  140. struct snd_pcm_substream *substream;
  141. struct snd_dma_buffer *buf;
  142. int stream;
  143. for (stream = 0; stream < 2; stream++) {
  144. substream = pcm->streams[stream].substream;
  145. if (!substream)
  146. continue;
  147. buf = &substream->dma_buffer;
  148. if (!buf->area)
  149. continue;
  150. dma_free_wc(pcm->card->dev, buf->bytes, buf->area, buf->addr);
  151. buf->area = NULL;
  152. }
  153. }
  154. EXPORT_SYMBOL(pxa2xx_pcm_free_dma_buffers);
  155. int pxa2xx_soc_pcm_new(struct snd_soc_pcm_runtime *rtd)
  156. {
  157. struct snd_card *card = rtd->card->snd_card;
  158. struct snd_pcm *pcm = rtd->pcm;
  159. int ret;
  160. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  161. if (ret)
  162. return ret;
  163. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  164. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
  165. SNDRV_PCM_STREAM_PLAYBACK);
  166. if (ret)
  167. goto out;
  168. }
  169. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  170. ret = pxa2xx_pcm_preallocate_dma_buffer(pcm,
  171. SNDRV_PCM_STREAM_CAPTURE);
  172. if (ret)
  173. goto out;
  174. }
  175. out:
  176. return ret;
  177. }
  178. EXPORT_SYMBOL(pxa2xx_soc_pcm_new);
  179. const struct snd_pcm_ops pxa2xx_pcm_ops = {
  180. .open = pxa2xx_pcm_open,
  181. .close = pxa2xx_pcm_close,
  182. .ioctl = snd_pcm_lib_ioctl,
  183. .hw_params = pxa2xx_pcm_hw_params,
  184. .hw_free = pxa2xx_pcm_hw_free,
  185. .prepare = pxa2xx_pcm_prepare,
  186. .trigger = pxa2xx_pcm_trigger,
  187. .pointer = pxa2xx_pcm_pointer,
  188. .mmap = pxa2xx_pcm_mmap,
  189. };
  190. EXPORT_SYMBOL(pxa2xx_pcm_ops);
  191. MODULE_AUTHOR("Nicolas Pitre");
  192. MODULE_DESCRIPTION("Intel PXA2xx sound library");
  193. MODULE_LICENSE("GPL");