atmel-pcm-pdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * atmel-pcm.c -- ALSA PCM interface for the Atmel atmel SoC.
  3. *
  4. * Copyright (C) 2005 SAN People
  5. * Copyright (C) 2008 Atmel
  6. *
  7. * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  8. *
  9. * Based on at91-pcm. by:
  10. * Frank Mandarino <fmandarino@endrelia.com>
  11. * Copyright 2006 Endrelia Technologies Inc.
  12. *
  13. * Based on pxa2xx-pcm.c by:
  14. *
  15. * Author: Nicolas Pitre
  16. * Created: Nov 30, 2004
  17. * Copyright: (C) 2004 MontaVista Software, Inc.
  18. *
  19. * This program is free software; you can redistribute it and/or modify
  20. * it under the terms of the GNU General Public License as published by
  21. * the Free Software Foundation; either version 2 of the License, or
  22. * (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. */
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/slab.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/atmel_pdc.h>
  39. #include <linux/atmel-ssc.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc.h>
  44. #include "atmel-pcm.h"
  45. static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  46. int stream)
  47. {
  48. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  49. struct snd_dma_buffer *buf = &substream->dma_buffer;
  50. size_t size = ATMEL_SSC_DMABUF_SIZE;
  51. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  52. buf->dev.dev = pcm->card->dev;
  53. buf->private_data = NULL;
  54. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  55. &buf->addr, GFP_KERNEL);
  56. pr_debug("atmel-pcm: alloc dma buffer: area=%p, addr=%p, size=%zu\n",
  57. (void *)buf->area, (void *)(long)buf->addr, size);
  58. if (!buf->area)
  59. return -ENOMEM;
  60. buf->bytes = size;
  61. return 0;
  62. }
  63. static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
  64. struct vm_area_struct *vma)
  65. {
  66. return remap_pfn_range(vma, vma->vm_start,
  67. substream->dma_buffer.addr >> PAGE_SHIFT,
  68. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  69. }
  70. static int atmel_pcm_new(struct snd_soc_pcm_runtime *rtd)
  71. {
  72. struct snd_card *card = rtd->card->snd_card;
  73. struct snd_pcm *pcm = rtd->pcm;
  74. int ret;
  75. ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  76. if (ret)
  77. return ret;
  78. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream) {
  79. pr_debug("atmel-pcm: allocating PCM playback DMA buffer\n");
  80. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  81. SNDRV_PCM_STREAM_PLAYBACK);
  82. if (ret)
  83. goto out;
  84. }
  85. if (pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  86. pr_debug("atmel-pcm: allocating PCM capture DMA buffer\n");
  87. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  88. SNDRV_PCM_STREAM_CAPTURE);
  89. if (ret)
  90. goto out;
  91. }
  92. out:
  93. return ret;
  94. }
  95. static void atmel_pcm_free(struct snd_pcm *pcm)
  96. {
  97. struct snd_pcm_substream *substream;
  98. struct snd_dma_buffer *buf;
  99. int stream;
  100. for (stream = 0; stream < 2; stream++) {
  101. substream = pcm->streams[stream].substream;
  102. if (!substream)
  103. continue;
  104. buf = &substream->dma_buffer;
  105. if (!buf->area)
  106. continue;
  107. dma_free_coherent(pcm->card->dev, buf->bytes,
  108. buf->area, buf->addr);
  109. buf->area = NULL;
  110. }
  111. }
  112. /*--------------------------------------------------------------------------*\
  113. * Hardware definition
  114. \*--------------------------------------------------------------------------*/
  115. /* TODO: These values were taken from the AT91 platform driver, check
  116. * them against real values for AT32
  117. */
  118. static const struct snd_pcm_hardware atmel_pcm_hardware = {
  119. .info = SNDRV_PCM_INFO_MMAP |
  120. SNDRV_PCM_INFO_MMAP_VALID |
  121. SNDRV_PCM_INFO_INTERLEAVED |
  122. SNDRV_PCM_INFO_PAUSE,
  123. .period_bytes_min = 32,
  124. .period_bytes_max = 8192,
  125. .periods_min = 2,
  126. .periods_max = 1024,
  127. .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE,
  128. };
  129. /*--------------------------------------------------------------------------*\
  130. * Data types
  131. \*--------------------------------------------------------------------------*/
  132. struct atmel_runtime_data {
  133. struct atmel_pcm_dma_params *params;
  134. dma_addr_t dma_buffer; /* physical address of dma buffer */
  135. dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
  136. size_t period_size;
  137. dma_addr_t period_ptr; /* physical address of next period */
  138. };
  139. /*--------------------------------------------------------------------------*\
  140. * ISR
  141. \*--------------------------------------------------------------------------*/
  142. static void atmel_pcm_dma_irq(u32 ssc_sr,
  143. struct snd_pcm_substream *substream)
  144. {
  145. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  146. struct atmel_pcm_dma_params *params = prtd->params;
  147. static int count;
  148. count++;
  149. if (ssc_sr & params->mask->ssc_endbuf) {
  150. pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
  151. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  152. ? "underrun" : "overrun",
  153. params->name, ssc_sr, count);
  154. /* re-start the PDC */
  155. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  156. params->mask->pdc_disable);
  157. prtd->period_ptr += prtd->period_size;
  158. if (prtd->period_ptr >= prtd->dma_buffer_end)
  159. prtd->period_ptr = prtd->dma_buffer;
  160. ssc_writex(params->ssc->regs, params->pdc->xpr,
  161. prtd->period_ptr);
  162. ssc_writex(params->ssc->regs, params->pdc->xcr,
  163. prtd->period_size / params->pdc_xfer_size);
  164. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  165. params->mask->pdc_enable);
  166. }
  167. if (ssc_sr & params->mask->ssc_endx) {
  168. /* Load the PDC next pointer and counter registers */
  169. prtd->period_ptr += prtd->period_size;
  170. if (prtd->period_ptr >= prtd->dma_buffer_end)
  171. prtd->period_ptr = prtd->dma_buffer;
  172. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  173. prtd->period_ptr);
  174. ssc_writex(params->ssc->regs, params->pdc->xncr,
  175. prtd->period_size / params->pdc_xfer_size);
  176. }
  177. snd_pcm_period_elapsed(substream);
  178. }
  179. /*--------------------------------------------------------------------------*\
  180. * PCM operations
  181. \*--------------------------------------------------------------------------*/
  182. static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  183. struct snd_pcm_hw_params *params)
  184. {
  185. struct snd_pcm_runtime *runtime = substream->runtime;
  186. struct atmel_runtime_data *prtd = runtime->private_data;
  187. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  188. /* this may get called several times by oss emulation
  189. * with different params */
  190. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  191. runtime->dma_bytes = params_buffer_bytes(params);
  192. prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  193. prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
  194. prtd->dma_buffer = runtime->dma_addr;
  195. prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
  196. prtd->period_size = params_period_bytes(params);
  197. pr_debug("atmel-pcm: "
  198. "hw_params: DMA for %s initialized "
  199. "(dma_bytes=%zu, period_size=%zu)\n",
  200. prtd->params->name,
  201. runtime->dma_bytes,
  202. prtd->period_size);
  203. return 0;
  204. }
  205. static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
  206. {
  207. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  208. struct atmel_pcm_dma_params *params = prtd->params;
  209. if (params != NULL) {
  210. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  211. params->mask->pdc_disable);
  212. prtd->params->dma_intr_handler = NULL;
  213. }
  214. return 0;
  215. }
  216. static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
  217. {
  218. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  219. struct atmel_pcm_dma_params *params = prtd->params;
  220. ssc_writex(params->ssc->regs, SSC_IDR,
  221. params->mask->ssc_endx | params->mask->ssc_endbuf);
  222. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  223. params->mask->pdc_disable);
  224. return 0;
  225. }
  226. static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
  227. int cmd)
  228. {
  229. struct snd_pcm_runtime *rtd = substream->runtime;
  230. struct atmel_runtime_data *prtd = rtd->private_data;
  231. struct atmel_pcm_dma_params *params = prtd->params;
  232. int ret = 0;
  233. pr_debug("atmel-pcm:buffer_size = %ld,"
  234. "dma_area = %p, dma_bytes = %zu\n",
  235. rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. prtd->period_ptr = prtd->dma_buffer;
  239. ssc_writex(params->ssc->regs, params->pdc->xpr,
  240. prtd->period_ptr);
  241. ssc_writex(params->ssc->regs, params->pdc->xcr,
  242. prtd->period_size / params->pdc_xfer_size);
  243. prtd->period_ptr += prtd->period_size;
  244. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  245. prtd->period_ptr);
  246. ssc_writex(params->ssc->regs, params->pdc->xncr,
  247. prtd->period_size / params->pdc_xfer_size);
  248. pr_debug("atmel-pcm: trigger: "
  249. "period_ptr=%lx, xpr=%u, "
  250. "xcr=%u, xnpr=%u, xncr=%u\n",
  251. (unsigned long)prtd->period_ptr,
  252. ssc_readx(params->ssc->regs, params->pdc->xpr),
  253. ssc_readx(params->ssc->regs, params->pdc->xcr),
  254. ssc_readx(params->ssc->regs, params->pdc->xnpr),
  255. ssc_readx(params->ssc->regs, params->pdc->xncr));
  256. ssc_writex(params->ssc->regs, SSC_IER,
  257. params->mask->ssc_endx | params->mask->ssc_endbuf);
  258. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  259. params->mask->pdc_enable);
  260. pr_debug("sr=%u imr=%u\n",
  261. ssc_readx(params->ssc->regs, SSC_SR),
  262. ssc_readx(params->ssc->regs, SSC_IER));
  263. break; /* SNDRV_PCM_TRIGGER_START */
  264. case SNDRV_PCM_TRIGGER_STOP:
  265. case SNDRV_PCM_TRIGGER_SUSPEND:
  266. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  267. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  268. params->mask->pdc_disable);
  269. break;
  270. case SNDRV_PCM_TRIGGER_RESUME:
  271. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  272. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  273. params->mask->pdc_enable);
  274. break;
  275. default:
  276. ret = -EINVAL;
  277. }
  278. return ret;
  279. }
  280. static snd_pcm_uframes_t atmel_pcm_pointer(
  281. struct snd_pcm_substream *substream)
  282. {
  283. struct snd_pcm_runtime *runtime = substream->runtime;
  284. struct atmel_runtime_data *prtd = runtime->private_data;
  285. struct atmel_pcm_dma_params *params = prtd->params;
  286. dma_addr_t ptr;
  287. snd_pcm_uframes_t x;
  288. ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
  289. x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
  290. if (x == runtime->buffer_size)
  291. x = 0;
  292. return x;
  293. }
  294. static int atmel_pcm_open(struct snd_pcm_substream *substream)
  295. {
  296. struct snd_pcm_runtime *runtime = substream->runtime;
  297. struct atmel_runtime_data *prtd;
  298. int ret = 0;
  299. snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
  300. /* ensure that buffer size is a multiple of period size */
  301. ret = snd_pcm_hw_constraint_integer(runtime,
  302. SNDRV_PCM_HW_PARAM_PERIODS);
  303. if (ret < 0)
  304. goto out;
  305. prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
  306. if (prtd == NULL) {
  307. ret = -ENOMEM;
  308. goto out;
  309. }
  310. runtime->private_data = prtd;
  311. out:
  312. return ret;
  313. }
  314. static int atmel_pcm_close(struct snd_pcm_substream *substream)
  315. {
  316. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  317. kfree(prtd);
  318. return 0;
  319. }
  320. static struct snd_pcm_ops atmel_pcm_ops = {
  321. .open = atmel_pcm_open,
  322. .close = atmel_pcm_close,
  323. .ioctl = snd_pcm_lib_ioctl,
  324. .hw_params = atmel_pcm_hw_params,
  325. .hw_free = atmel_pcm_hw_free,
  326. .prepare = atmel_pcm_prepare,
  327. .trigger = atmel_pcm_trigger,
  328. .pointer = atmel_pcm_pointer,
  329. .mmap = atmel_pcm_mmap,
  330. };
  331. static struct snd_soc_platform_driver atmel_soc_platform = {
  332. .ops = &atmel_pcm_ops,
  333. .pcm_new = atmel_pcm_new,
  334. .pcm_free = atmel_pcm_free,
  335. };
  336. int atmel_pcm_pdc_platform_register(struct device *dev)
  337. {
  338. return snd_soc_register_platform(dev, &atmel_soc_platform);
  339. }
  340. EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
  341. void atmel_pcm_pdc_platform_unregister(struct device *dev)
  342. {
  343. snd_soc_unregister_platform(dev);
  344. }
  345. EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister);
  346. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  347. MODULE_DESCRIPTION("Atmel PCM module");
  348. MODULE_LICENSE("GPL");