atmel-pcm.c 14 KB

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