atmel-pcm-pdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. /*--------------------------------------------------------------------------*\
  46. * Hardware definition
  47. \*--------------------------------------------------------------------------*/
  48. /* TODO: These values were taken from the AT91 platform driver, check
  49. * them against real values for AT32
  50. */
  51. static const struct snd_pcm_hardware atmel_pcm_hardware = {
  52. .info = SNDRV_PCM_INFO_MMAP |
  53. SNDRV_PCM_INFO_MMAP_VALID |
  54. SNDRV_PCM_INFO_INTERLEAVED |
  55. SNDRV_PCM_INFO_PAUSE,
  56. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  57. .period_bytes_min = 32,
  58. .period_bytes_max = 8192,
  59. .periods_min = 2,
  60. .periods_max = 1024,
  61. .buffer_bytes_max = ATMEL_SSC_DMABUF_SIZE,
  62. };
  63. /*--------------------------------------------------------------------------*\
  64. * Data types
  65. \*--------------------------------------------------------------------------*/
  66. struct atmel_runtime_data {
  67. struct atmel_pcm_dma_params *params;
  68. dma_addr_t dma_buffer; /* physical address of dma buffer */
  69. dma_addr_t dma_buffer_end; /* first address beyond DMA buffer */
  70. size_t period_size;
  71. dma_addr_t period_ptr; /* physical address of next period */
  72. /* PDC register save */
  73. u32 pdc_xpr_save;
  74. u32 pdc_xcr_save;
  75. u32 pdc_xnpr_save;
  76. u32 pdc_xncr_save;
  77. };
  78. /*--------------------------------------------------------------------------*\
  79. * ISR
  80. \*--------------------------------------------------------------------------*/
  81. static void atmel_pcm_dma_irq(u32 ssc_sr,
  82. struct snd_pcm_substream *substream)
  83. {
  84. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  85. struct atmel_pcm_dma_params *params = prtd->params;
  86. static int count;
  87. count++;
  88. if (ssc_sr & params->mask->ssc_endbuf) {
  89. pr_warn("atmel-pcm: buffer %s on %s (SSC_SR=%#x, count=%d)\n",
  90. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  91. ? "underrun" : "overrun",
  92. params->name, ssc_sr, count);
  93. /* re-start the PDC */
  94. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  95. params->mask->pdc_disable);
  96. prtd->period_ptr += prtd->period_size;
  97. if (prtd->period_ptr >= prtd->dma_buffer_end)
  98. prtd->period_ptr = prtd->dma_buffer;
  99. ssc_writex(params->ssc->regs, params->pdc->xpr,
  100. prtd->period_ptr);
  101. ssc_writex(params->ssc->regs, params->pdc->xcr,
  102. prtd->period_size / params->pdc_xfer_size);
  103. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  104. params->mask->pdc_enable);
  105. }
  106. if (ssc_sr & params->mask->ssc_endx) {
  107. /* Load the PDC next pointer and counter registers */
  108. prtd->period_ptr += prtd->period_size;
  109. if (prtd->period_ptr >= prtd->dma_buffer_end)
  110. prtd->period_ptr = prtd->dma_buffer;
  111. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  112. prtd->period_ptr);
  113. ssc_writex(params->ssc->regs, params->pdc->xncr,
  114. prtd->period_size / params->pdc_xfer_size);
  115. }
  116. snd_pcm_period_elapsed(substream);
  117. }
  118. /*--------------------------------------------------------------------------*\
  119. * PCM operations
  120. \*--------------------------------------------------------------------------*/
  121. static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  122. struct snd_pcm_hw_params *params)
  123. {
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct atmel_runtime_data *prtd = runtime->private_data;
  126. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  127. /* this may get called several times by oss emulation
  128. * with different params */
  129. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  130. runtime->dma_bytes = params_buffer_bytes(params);
  131. prtd->params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
  132. prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
  133. prtd->dma_buffer = runtime->dma_addr;
  134. prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
  135. prtd->period_size = params_period_bytes(params);
  136. pr_debug("atmel-pcm: "
  137. "hw_params: DMA for %s initialized "
  138. "(dma_bytes=%u, period_size=%u)\n",
  139. prtd->params->name,
  140. runtime->dma_bytes,
  141. prtd->period_size);
  142. return 0;
  143. }
  144. static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
  145. {
  146. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  147. struct atmel_pcm_dma_params *params = prtd->params;
  148. if (params != NULL) {
  149. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  150. params->mask->pdc_disable);
  151. prtd->params->dma_intr_handler = NULL;
  152. }
  153. return 0;
  154. }
  155. static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
  156. {
  157. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  158. struct atmel_pcm_dma_params *params = prtd->params;
  159. ssc_writex(params->ssc->regs, SSC_IDR,
  160. params->mask->ssc_endx | params->mask->ssc_endbuf);
  161. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  162. params->mask->pdc_disable);
  163. return 0;
  164. }
  165. static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
  166. int cmd)
  167. {
  168. struct snd_pcm_runtime *rtd = substream->runtime;
  169. struct atmel_runtime_data *prtd = rtd->private_data;
  170. struct atmel_pcm_dma_params *params = prtd->params;
  171. int ret = 0;
  172. pr_debug("atmel-pcm:buffer_size = %ld,"
  173. "dma_area = %p, dma_bytes = %u\n",
  174. rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
  175. switch (cmd) {
  176. case SNDRV_PCM_TRIGGER_START:
  177. prtd->period_ptr = prtd->dma_buffer;
  178. ssc_writex(params->ssc->regs, params->pdc->xpr,
  179. prtd->period_ptr);
  180. ssc_writex(params->ssc->regs, params->pdc->xcr,
  181. prtd->period_size / params->pdc_xfer_size);
  182. prtd->period_ptr += prtd->period_size;
  183. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  184. prtd->period_ptr);
  185. ssc_writex(params->ssc->regs, params->pdc->xncr,
  186. prtd->period_size / params->pdc_xfer_size);
  187. pr_debug("atmel-pcm: trigger: "
  188. "period_ptr=%lx, xpr=%u, "
  189. "xcr=%u, xnpr=%u, xncr=%u\n",
  190. (unsigned long)prtd->period_ptr,
  191. ssc_readx(params->ssc->regs, params->pdc->xpr),
  192. ssc_readx(params->ssc->regs, params->pdc->xcr),
  193. ssc_readx(params->ssc->regs, params->pdc->xnpr),
  194. ssc_readx(params->ssc->regs, params->pdc->xncr));
  195. ssc_writex(params->ssc->regs, SSC_IER,
  196. params->mask->ssc_endx | params->mask->ssc_endbuf);
  197. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  198. params->mask->pdc_enable);
  199. pr_debug("sr=%u imr=%u\n",
  200. ssc_readx(params->ssc->regs, SSC_SR),
  201. ssc_readx(params->ssc->regs, SSC_IER));
  202. break; /* SNDRV_PCM_TRIGGER_START */
  203. case SNDRV_PCM_TRIGGER_STOP:
  204. case SNDRV_PCM_TRIGGER_SUSPEND:
  205. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  206. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  207. params->mask->pdc_disable);
  208. break;
  209. case SNDRV_PCM_TRIGGER_RESUME:
  210. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  211. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  212. params->mask->pdc_enable);
  213. break;
  214. default:
  215. ret = -EINVAL;
  216. }
  217. return ret;
  218. }
  219. static snd_pcm_uframes_t atmel_pcm_pointer(
  220. struct snd_pcm_substream *substream)
  221. {
  222. struct snd_pcm_runtime *runtime = substream->runtime;
  223. struct atmel_runtime_data *prtd = runtime->private_data;
  224. struct atmel_pcm_dma_params *params = prtd->params;
  225. dma_addr_t ptr;
  226. snd_pcm_uframes_t x;
  227. ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
  228. x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
  229. if (x == runtime->buffer_size)
  230. x = 0;
  231. return x;
  232. }
  233. static int atmel_pcm_open(struct snd_pcm_substream *substream)
  234. {
  235. struct snd_pcm_runtime *runtime = substream->runtime;
  236. struct atmel_runtime_data *prtd;
  237. int ret = 0;
  238. snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
  239. /* ensure that buffer size is a multiple of period size */
  240. ret = snd_pcm_hw_constraint_integer(runtime,
  241. SNDRV_PCM_HW_PARAM_PERIODS);
  242. if (ret < 0)
  243. goto out;
  244. prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
  245. if (prtd == NULL) {
  246. ret = -ENOMEM;
  247. goto out;
  248. }
  249. runtime->private_data = prtd;
  250. out:
  251. return ret;
  252. }
  253. static int atmel_pcm_close(struct snd_pcm_substream *substream)
  254. {
  255. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  256. kfree(prtd);
  257. return 0;
  258. }
  259. static struct snd_pcm_ops atmel_pcm_ops = {
  260. .open = atmel_pcm_open,
  261. .close = atmel_pcm_close,
  262. .ioctl = snd_pcm_lib_ioctl,
  263. .hw_params = atmel_pcm_hw_params,
  264. .hw_free = atmel_pcm_hw_free,
  265. .prepare = atmel_pcm_prepare,
  266. .trigger = atmel_pcm_trigger,
  267. .pointer = atmel_pcm_pointer,
  268. .mmap = atmel_pcm_mmap,
  269. };
  270. /*--------------------------------------------------------------------------*\
  271. * ASoC platform driver
  272. \*--------------------------------------------------------------------------*/
  273. #ifdef CONFIG_PM
  274. static int atmel_pcm_suspend(struct snd_soc_dai *dai)
  275. {
  276. struct snd_pcm_runtime *runtime = dai->runtime;
  277. struct atmel_runtime_data *prtd;
  278. struct atmel_pcm_dma_params *params;
  279. if (!runtime)
  280. return 0;
  281. prtd = runtime->private_data;
  282. params = prtd->params;
  283. /* disable the PDC and save the PDC registers */
  284. ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
  285. prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
  286. prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
  287. prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
  288. prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
  289. return 0;
  290. }
  291. static int atmel_pcm_resume(struct snd_soc_dai *dai)
  292. {
  293. struct snd_pcm_runtime *runtime = dai->runtime;
  294. struct atmel_runtime_data *prtd;
  295. struct atmel_pcm_dma_params *params;
  296. if (!runtime)
  297. return 0;
  298. prtd = runtime->private_data;
  299. params = prtd->params;
  300. /* restore the PDC registers and enable the PDC */
  301. ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
  302. ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
  303. ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
  304. ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
  305. ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
  306. return 0;
  307. }
  308. #else
  309. #define atmel_pcm_suspend NULL
  310. #define atmel_pcm_resume NULL
  311. #endif
  312. static struct snd_soc_platform_driver atmel_soc_platform = {
  313. .ops = &atmel_pcm_ops,
  314. .pcm_new = atmel_pcm_new,
  315. .pcm_free = atmel_pcm_free,
  316. .suspend = atmel_pcm_suspend,
  317. .resume = atmel_pcm_resume,
  318. };
  319. int atmel_pcm_pdc_platform_register(struct device *dev)
  320. {
  321. return snd_soc_register_platform(dev, &atmel_soc_platform);
  322. }
  323. EXPORT_SYMBOL(atmel_pcm_pdc_platform_register);
  324. void atmel_pcm_pdc_platform_unregister(struct device *dev)
  325. {
  326. snd_soc_unregister_platform(dev);
  327. }
  328. EXPORT_SYMBOL(atmel_pcm_pdc_platform_unregister);
  329. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  330. MODULE_DESCRIPTION("Atmel PCM module");
  331. MODULE_LICENSE("GPL");