atmel-pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 = 32 * 1024,
  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. int periods; /* period index of period_ptr */
  73. /* PDC register save */
  74. u32 pdc_xpr_save;
  75. u32 pdc_xcr_save;
  76. u32 pdc_xnpr_save;
  77. u32 pdc_xncr_save;
  78. };
  79. /*--------------------------------------------------------------------------*\
  80. * Helper functions
  81. \*--------------------------------------------------------------------------*/
  82. static int atmel_pcm_preallocate_dma_buffer(struct snd_pcm *pcm,
  83. int stream)
  84. {
  85. struct snd_pcm_substream *substream = pcm->streams[stream].substream;
  86. struct snd_dma_buffer *buf = &substream->dma_buffer;
  87. size_t size = atmel_pcm_hardware.buffer_bytes_max;
  88. buf->dev.type = SNDRV_DMA_TYPE_DEV;
  89. buf->dev.dev = pcm->card->dev;
  90. buf->private_data = NULL;
  91. buf->area = dma_alloc_coherent(pcm->card->dev, size,
  92. &buf->addr, GFP_KERNEL);
  93. pr_debug("atmel-pcm:"
  94. "preallocate_dma_buffer: area=%p, addr=%p, size=%d\n",
  95. (void *) buf->area,
  96. (void *) buf->addr,
  97. size);
  98. if (!buf->area)
  99. return -ENOMEM;
  100. buf->bytes = size;
  101. return 0;
  102. }
  103. /*--------------------------------------------------------------------------*\
  104. * ISR
  105. \*--------------------------------------------------------------------------*/
  106. static void atmel_pcm_dma_irq(u32 ssc_sr,
  107. struct snd_pcm_substream *substream)
  108. {
  109. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  110. struct atmel_pcm_dma_params *params = prtd->params;
  111. static int count;
  112. count++;
  113. if (ssc_sr & params->mask->ssc_endbuf) {
  114. pr_warning("atmel-pcm: buffer %s on %s"
  115. " (SSC_SR=%#x, count=%d)\n",
  116. substream->stream == SNDRV_PCM_STREAM_PLAYBACK
  117. ? "underrun" : "overrun",
  118. params->name, ssc_sr, count);
  119. /* re-start the PDC */
  120. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  121. params->mask->pdc_disable);
  122. prtd->period_ptr += prtd->period_size;
  123. if (prtd->period_ptr >= prtd->dma_buffer_end)
  124. prtd->period_ptr = prtd->dma_buffer;
  125. ssc_writex(params->ssc->regs, params->pdc->xpr,
  126. prtd->period_ptr);
  127. ssc_writex(params->ssc->regs, params->pdc->xcr,
  128. prtd->period_size / params->pdc_xfer_size);
  129. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  130. params->mask->pdc_enable);
  131. }
  132. if (ssc_sr & params->mask->ssc_endx) {
  133. /* Load the PDC next pointer and counter registers */
  134. prtd->period_ptr += prtd->period_size;
  135. if (prtd->period_ptr >= prtd->dma_buffer_end)
  136. prtd->period_ptr = prtd->dma_buffer;
  137. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  138. prtd->period_ptr);
  139. ssc_writex(params->ssc->regs, params->pdc->xncr,
  140. prtd->period_size / params->pdc_xfer_size);
  141. }
  142. snd_pcm_period_elapsed(substream);
  143. }
  144. /*--------------------------------------------------------------------------*\
  145. * PCM operations
  146. \*--------------------------------------------------------------------------*/
  147. static int atmel_pcm_hw_params(struct snd_pcm_substream *substream,
  148. struct snd_pcm_hw_params *params)
  149. {
  150. struct snd_pcm_runtime *runtime = substream->runtime;
  151. struct atmel_runtime_data *prtd = runtime->private_data;
  152. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  153. /* this may get called several times by oss emulation
  154. * with different params */
  155. snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
  156. runtime->dma_bytes = params_buffer_bytes(params);
  157. prtd->params = rtd->dai->cpu_dai->dma_data;
  158. prtd->params->dma_intr_handler = atmel_pcm_dma_irq;
  159. prtd->dma_buffer = runtime->dma_addr;
  160. prtd->dma_buffer_end = runtime->dma_addr + runtime->dma_bytes;
  161. prtd->period_size = params_period_bytes(params);
  162. pr_debug("atmel-pcm: "
  163. "hw_params: DMA for %s initialized "
  164. "(dma_bytes=%u, period_size=%u)\n",
  165. prtd->params->name,
  166. runtime->dma_bytes,
  167. prtd->period_size);
  168. return 0;
  169. }
  170. static int atmel_pcm_hw_free(struct snd_pcm_substream *substream)
  171. {
  172. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  173. struct atmel_pcm_dma_params *params = prtd->params;
  174. if (params != NULL) {
  175. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  176. params->mask->pdc_disable);
  177. prtd->params->dma_intr_handler = NULL;
  178. }
  179. return 0;
  180. }
  181. static int atmel_pcm_prepare(struct snd_pcm_substream *substream)
  182. {
  183. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  184. struct atmel_pcm_dma_params *params = prtd->params;
  185. ssc_writex(params->ssc->regs, SSC_IDR,
  186. params->mask->ssc_endx | params->mask->ssc_endbuf);
  187. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  188. params->mask->pdc_disable);
  189. return 0;
  190. }
  191. static int atmel_pcm_trigger(struct snd_pcm_substream *substream,
  192. int cmd)
  193. {
  194. struct snd_pcm_runtime *rtd = substream->runtime;
  195. struct atmel_runtime_data *prtd = rtd->private_data;
  196. struct atmel_pcm_dma_params *params = prtd->params;
  197. int ret = 0;
  198. pr_debug("atmel-pcm:buffer_size = %ld,"
  199. "dma_area = %p, dma_bytes = %u\n",
  200. rtd->buffer_size, rtd->dma_area, rtd->dma_bytes);
  201. switch (cmd) {
  202. case SNDRV_PCM_TRIGGER_START:
  203. prtd->period_ptr = prtd->dma_buffer;
  204. ssc_writex(params->ssc->regs, params->pdc->xpr,
  205. prtd->period_ptr);
  206. ssc_writex(params->ssc->regs, params->pdc->xcr,
  207. prtd->period_size / params->pdc_xfer_size);
  208. prtd->period_ptr += prtd->period_size;
  209. ssc_writex(params->ssc->regs, params->pdc->xnpr,
  210. prtd->period_ptr);
  211. ssc_writex(params->ssc->regs, params->pdc->xncr,
  212. prtd->period_size / params->pdc_xfer_size);
  213. pr_debug("atmel-pcm: trigger: "
  214. "period_ptr=%lx, xpr=%u, "
  215. "xcr=%u, xnpr=%u, xncr=%u\n",
  216. (unsigned long)prtd->period_ptr,
  217. ssc_readx(params->ssc->regs, params->pdc->xpr),
  218. ssc_readx(params->ssc->regs, params->pdc->xcr),
  219. ssc_readx(params->ssc->regs, params->pdc->xnpr),
  220. ssc_readx(params->ssc->regs, params->pdc->xncr));
  221. ssc_writex(params->ssc->regs, SSC_IER,
  222. params->mask->ssc_endx | params->mask->ssc_endbuf);
  223. ssc_writex(params->ssc->regs, SSC_PDC_PTCR,
  224. params->mask->pdc_enable);
  225. pr_debug("sr=%u imr=%u\n",
  226. ssc_readx(params->ssc->regs, SSC_SR),
  227. ssc_readx(params->ssc->regs, SSC_IER));
  228. break; /* SNDRV_PCM_TRIGGER_START */
  229. case SNDRV_PCM_TRIGGER_STOP:
  230. case SNDRV_PCM_TRIGGER_SUSPEND:
  231. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  232. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  233. params->mask->pdc_disable);
  234. break;
  235. case SNDRV_PCM_TRIGGER_RESUME:
  236. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  237. ssc_writex(params->ssc->regs, ATMEL_PDC_PTCR,
  238. params->mask->pdc_enable);
  239. break;
  240. default:
  241. ret = -EINVAL;
  242. }
  243. return ret;
  244. }
  245. static snd_pcm_uframes_t atmel_pcm_pointer(
  246. struct snd_pcm_substream *substream)
  247. {
  248. struct snd_pcm_runtime *runtime = substream->runtime;
  249. struct atmel_runtime_data *prtd = runtime->private_data;
  250. struct atmel_pcm_dma_params *params = prtd->params;
  251. dma_addr_t ptr;
  252. snd_pcm_uframes_t x;
  253. ptr = (dma_addr_t) ssc_readx(params->ssc->regs, params->pdc->xpr);
  254. x = bytes_to_frames(runtime, ptr - prtd->dma_buffer);
  255. if (x == runtime->buffer_size)
  256. x = 0;
  257. return x;
  258. }
  259. static int atmel_pcm_open(struct snd_pcm_substream *substream)
  260. {
  261. struct snd_pcm_runtime *runtime = substream->runtime;
  262. struct atmel_runtime_data *prtd;
  263. int ret = 0;
  264. snd_soc_set_runtime_hwparams(substream, &atmel_pcm_hardware);
  265. /* ensure that buffer size is a multiple of period size */
  266. ret = snd_pcm_hw_constraint_integer(runtime,
  267. SNDRV_PCM_HW_PARAM_PERIODS);
  268. if (ret < 0)
  269. goto out;
  270. prtd = kzalloc(sizeof(struct atmel_runtime_data), GFP_KERNEL);
  271. if (prtd == NULL) {
  272. ret = -ENOMEM;
  273. goto out;
  274. }
  275. runtime->private_data = prtd;
  276. out:
  277. return ret;
  278. }
  279. static int atmel_pcm_close(struct snd_pcm_substream *substream)
  280. {
  281. struct atmel_runtime_data *prtd = substream->runtime->private_data;
  282. kfree(prtd);
  283. return 0;
  284. }
  285. static int atmel_pcm_mmap(struct snd_pcm_substream *substream,
  286. struct vm_area_struct *vma)
  287. {
  288. return remap_pfn_range(vma, vma->vm_start,
  289. substream->dma_buffer.addr >> PAGE_SHIFT,
  290. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  291. }
  292. static struct snd_pcm_ops atmel_pcm_ops = {
  293. .open = atmel_pcm_open,
  294. .close = atmel_pcm_close,
  295. .ioctl = snd_pcm_lib_ioctl,
  296. .hw_params = atmel_pcm_hw_params,
  297. .hw_free = atmel_pcm_hw_free,
  298. .prepare = atmel_pcm_prepare,
  299. .trigger = atmel_pcm_trigger,
  300. .pointer = atmel_pcm_pointer,
  301. .mmap = atmel_pcm_mmap,
  302. };
  303. /*--------------------------------------------------------------------------*\
  304. * ASoC platform driver
  305. \*--------------------------------------------------------------------------*/
  306. static u64 atmel_pcm_dmamask = 0xffffffff;
  307. static int atmel_pcm_new(struct snd_card *card,
  308. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  309. {
  310. int ret = 0;
  311. if (!card->dev->dma_mask)
  312. card->dev->dma_mask = &atmel_pcm_dmamask;
  313. if (!card->dev->coherent_dma_mask)
  314. card->dev->coherent_dma_mask = 0xffffffff;
  315. if (dai->playback.channels_min) {
  316. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  317. SNDRV_PCM_STREAM_PLAYBACK);
  318. if (ret)
  319. goto out;
  320. }
  321. if (dai->capture.channels_min) {
  322. pr_debug("at32-pcm:"
  323. "Allocating PCM capture DMA buffer\n");
  324. ret = atmel_pcm_preallocate_dma_buffer(pcm,
  325. SNDRV_PCM_STREAM_CAPTURE);
  326. if (ret)
  327. goto out;
  328. }
  329. out:
  330. return ret;
  331. }
  332. static void atmel_pcm_free_dma_buffers(struct snd_pcm *pcm)
  333. {
  334. struct snd_pcm_substream *substream;
  335. struct snd_dma_buffer *buf;
  336. int stream;
  337. for (stream = 0; stream < 2; stream++) {
  338. substream = pcm->streams[stream].substream;
  339. if (!substream)
  340. continue;
  341. buf = &substream->dma_buffer;
  342. if (!buf->area)
  343. continue;
  344. dma_free_coherent(pcm->card->dev, buf->bytes,
  345. buf->area, buf->addr);
  346. buf->area = NULL;
  347. }
  348. }
  349. #ifdef CONFIG_PM
  350. static int atmel_pcm_suspend(struct snd_soc_dai *dai)
  351. {
  352. struct snd_pcm_runtime *runtime = dai->runtime;
  353. struct atmel_runtime_data *prtd;
  354. struct atmel_pcm_dma_params *params;
  355. if (!runtime)
  356. return 0;
  357. prtd = runtime->private_data;
  358. params = prtd->params;
  359. /* disable the PDC and save the PDC registers */
  360. ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_disable);
  361. prtd->pdc_xpr_save = ssc_readx(params->ssc->regs, params->pdc->xpr);
  362. prtd->pdc_xcr_save = ssc_readx(params->ssc->regs, params->pdc->xcr);
  363. prtd->pdc_xnpr_save = ssc_readx(params->ssc->regs, params->pdc->xnpr);
  364. prtd->pdc_xncr_save = ssc_readx(params->ssc->regs, params->pdc->xncr);
  365. return 0;
  366. }
  367. static int atmel_pcm_resume(struct snd_soc_dai *dai)
  368. {
  369. struct snd_pcm_runtime *runtime = dai->runtime;
  370. struct atmel_runtime_data *prtd;
  371. struct atmel_pcm_dma_params *params;
  372. if (!runtime)
  373. return 0;
  374. prtd = runtime->private_data;
  375. params = prtd->params;
  376. /* restore the PDC registers and enable the PDC */
  377. ssc_writex(params->ssc->regs, params->pdc->xpr, prtd->pdc_xpr_save);
  378. ssc_writex(params->ssc->regs, params->pdc->xcr, prtd->pdc_xcr_save);
  379. ssc_writex(params->ssc->regs, params->pdc->xnpr, prtd->pdc_xnpr_save);
  380. ssc_writex(params->ssc->regs, params->pdc->xncr, prtd->pdc_xncr_save);
  381. ssc_writel(params->ssc->regs, PDC_PTCR, params->mask->pdc_enable);
  382. return 0;
  383. }
  384. #else
  385. #define atmel_pcm_suspend NULL
  386. #define atmel_pcm_resume NULL
  387. #endif
  388. struct snd_soc_platform atmel_soc_platform = {
  389. .name = "atmel-audio",
  390. .pcm_ops = &atmel_pcm_ops,
  391. .pcm_new = atmel_pcm_new,
  392. .pcm_free = atmel_pcm_free_dma_buffers,
  393. .suspend = atmel_pcm_suspend,
  394. .resume = atmel_pcm_resume,
  395. };
  396. EXPORT_SYMBOL_GPL(atmel_soc_platform);
  397. static int __init atmel_pcm_modinit(void)
  398. {
  399. return snd_soc_register_platform(&atmel_soc_platform);
  400. }
  401. module_init(atmel_pcm_modinit);
  402. static void __exit atmel_pcm_modexit(void)
  403. {
  404. snd_soc_unregister_platform(&atmel_soc_platform);
  405. }
  406. module_exit(atmel_pcm_modexit);
  407. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  408. MODULE_DESCRIPTION("Atmel PCM module");
  409. MODULE_LICENSE("GPL");