atmel-pcm.c 14 KB

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