s6000-pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * ALSA PCM interface for the Stetch s6000 family
  3. *
  4. * Author: Daniel Gloeckner, <dg@emlix.com>
  5. * Copyright: (C) 2009 emlix GmbH <info@emlix.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/interrupt.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include <asm/dma.h>
  22. #include <variant/dmac.h>
  23. #include "s6000-pcm.h"
  24. #define S6_PCM_PREALLOCATE_SIZE (96 * 1024)
  25. #define S6_PCM_PREALLOCATE_MAX (2048 * 1024)
  26. static struct snd_pcm_hardware s6000_pcm_hardware = {
  27. .info = (SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
  28. SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID |
  29. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_JOINT_DUPLEX),
  30. .formats = (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE),
  31. .rates = (SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_5512 | \
  32. SNDRV_PCM_RATE_8000_192000),
  33. .rate_min = 0,
  34. .rate_max = 1562500,
  35. .channels_min = 2,
  36. .channels_max = 8,
  37. .buffer_bytes_max = 0x7ffffff0,
  38. .period_bytes_min = 16,
  39. .period_bytes_max = 0xfffff0,
  40. .periods_min = 2,
  41. .periods_max = 1024, /* no limit */
  42. .fifo_size = 0,
  43. };
  44. struct s6000_runtime_data {
  45. spinlock_t lock;
  46. int period; /* current DMA period */
  47. };
  48. static void s6000_pcm_enqueue_dma(struct snd_pcm_substream *substream)
  49. {
  50. struct snd_pcm_runtime *runtime = substream->runtime;
  51. struct s6000_runtime_data *prtd = runtime->private_data;
  52. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  53. struct s6000_pcm_dma_params *par;
  54. int channel;
  55. unsigned int period_size;
  56. unsigned int dma_offset;
  57. dma_addr_t dma_pos;
  58. dma_addr_t src, dst;
  59. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  60. period_size = snd_pcm_lib_period_bytes(substream);
  61. dma_offset = prtd->period * period_size;
  62. dma_pos = runtime->dma_addr + dma_offset;
  63. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  64. src = dma_pos;
  65. dst = par->sif_out;
  66. channel = par->dma_out;
  67. } else {
  68. src = par->sif_in;
  69. dst = dma_pos;
  70. channel = par->dma_in;
  71. }
  72. if (!s6dmac_channel_enabled(DMA_MASK_DMAC(channel),
  73. DMA_INDEX_CHNL(channel)))
  74. return;
  75. if (s6dmac_fifo_full(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel))) {
  76. printk(KERN_ERR "s6000-pcm: fifo full\n");
  77. return;
  78. }
  79. BUG_ON(period_size & 15);
  80. s6dmac_put_fifo(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel),
  81. src, dst, period_size);
  82. prtd->period++;
  83. if (unlikely(prtd->period >= runtime->periods))
  84. prtd->period = 0;
  85. }
  86. static irqreturn_t s6000_pcm_irq(int irq, void *data)
  87. {
  88. struct snd_pcm *pcm = data;
  89. struct snd_soc_pcm_runtime *runtime = pcm->private_data;
  90. struct s6000_runtime_data *prtd;
  91. unsigned int has_xrun;
  92. int i, ret = IRQ_NONE;
  93. for (i = 0; i < 2; ++i) {
  94. struct snd_pcm_substream *substream = pcm->streams[i].substream;
  95. struct s6000_pcm_dma_params *params =
  96. snd_soc_dai_get_dma_data(runtime->cpu_dai, substream);
  97. u32 channel;
  98. unsigned int pending;
  99. if (substream == SNDRV_PCM_STREAM_PLAYBACK)
  100. channel = params->dma_out;
  101. else
  102. channel = params->dma_in;
  103. has_xrun = params->check_xrun(runtime->cpu_dai);
  104. if (!channel)
  105. continue;
  106. if (unlikely(has_xrun & (1 << i)) &&
  107. substream->runtime &&
  108. snd_pcm_running(substream)) {
  109. dev_dbg(pcm->dev, "xrun\n");
  110. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  111. ret = IRQ_HANDLED;
  112. }
  113. pending = s6dmac_int_sources(DMA_MASK_DMAC(channel),
  114. DMA_INDEX_CHNL(channel));
  115. if (pending & 1) {
  116. ret = IRQ_HANDLED;
  117. if (likely(substream->runtime &&
  118. snd_pcm_running(substream))) {
  119. snd_pcm_period_elapsed(substream);
  120. dev_dbg(pcm->dev, "period elapsed %x %x\n",
  121. s6dmac_cur_src(DMA_MASK_DMAC(channel),
  122. DMA_INDEX_CHNL(channel)),
  123. s6dmac_cur_dst(DMA_MASK_DMAC(channel),
  124. DMA_INDEX_CHNL(channel)));
  125. prtd = substream->runtime->private_data;
  126. spin_lock(&prtd->lock);
  127. s6000_pcm_enqueue_dma(substream);
  128. spin_unlock(&prtd->lock);
  129. }
  130. }
  131. if (unlikely(pending & ~7)) {
  132. if (pending & (1 << 3))
  133. printk(KERN_WARNING
  134. "s6000-pcm: DMA %x Underflow\n",
  135. channel);
  136. if (pending & (1 << 4))
  137. printk(KERN_WARNING
  138. "s6000-pcm: DMA %x Overflow\n",
  139. channel);
  140. if (pending & 0x1e0)
  141. printk(KERN_WARNING
  142. "s6000-pcm: DMA %x Master Error "
  143. "(mask %x)\n",
  144. channel, pending >> 5);
  145. }
  146. }
  147. return ret;
  148. }
  149. static int s6000_pcm_start(struct snd_pcm_substream *substream)
  150. {
  151. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  152. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  153. struct s6000_pcm_dma_params *par;
  154. unsigned long flags;
  155. int srcinc;
  156. u32 dma;
  157. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  158. spin_lock_irqsave(&prtd->lock, flags);
  159. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  160. srcinc = 1;
  161. dma = par->dma_out;
  162. } else {
  163. srcinc = 0;
  164. dma = par->dma_in;
  165. }
  166. s6dmac_enable_chan(DMA_MASK_DMAC(dma), DMA_INDEX_CHNL(dma),
  167. 1 /* priority 1 (0 is max) */,
  168. 0 /* peripheral requests w/o xfer length mode */,
  169. srcinc /* source address increment */,
  170. srcinc^1 /* destination address increment */,
  171. 0 /* chunksize 0 (skip impossible on this dma) */,
  172. 0 /* source skip after chunk (impossible) */,
  173. 0 /* destination skip after chunk (impossible) */,
  174. 4 /* 16 byte burst size */,
  175. -1 /* don't conserve bandwidth */,
  176. 0 /* low watermark irq descriptor threshold */,
  177. 0 /* disable hardware timestamps */,
  178. 1 /* enable channel */);
  179. s6000_pcm_enqueue_dma(substream);
  180. s6000_pcm_enqueue_dma(substream);
  181. spin_unlock_irqrestore(&prtd->lock, flags);
  182. return 0;
  183. }
  184. static int s6000_pcm_stop(struct snd_pcm_substream *substream)
  185. {
  186. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  187. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  188. struct s6000_pcm_dma_params *par;
  189. unsigned long flags;
  190. u32 channel;
  191. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  192. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  193. channel = par->dma_out;
  194. else
  195. channel = par->dma_in;
  196. s6dmac_set_terminal_count(DMA_MASK_DMAC(channel),
  197. DMA_INDEX_CHNL(channel), 0);
  198. spin_lock_irqsave(&prtd->lock, flags);
  199. s6dmac_disable_chan(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel));
  200. spin_unlock_irqrestore(&prtd->lock, flags);
  201. return 0;
  202. }
  203. static int s6000_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  204. {
  205. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  206. struct s6000_pcm_dma_params *par;
  207. int ret;
  208. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  209. ret = par->trigger(substream, cmd, 0);
  210. if (ret < 0)
  211. return ret;
  212. switch (cmd) {
  213. case SNDRV_PCM_TRIGGER_START:
  214. case SNDRV_PCM_TRIGGER_RESUME:
  215. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  216. ret = s6000_pcm_start(substream);
  217. break;
  218. case SNDRV_PCM_TRIGGER_STOP:
  219. case SNDRV_PCM_TRIGGER_SUSPEND:
  220. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  221. ret = s6000_pcm_stop(substream);
  222. break;
  223. default:
  224. ret = -EINVAL;
  225. }
  226. if (ret < 0)
  227. return ret;
  228. return par->trigger(substream, cmd, 1);
  229. }
  230. static int s6000_pcm_prepare(struct snd_pcm_substream *substream)
  231. {
  232. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  233. prtd->period = 0;
  234. return 0;
  235. }
  236. static snd_pcm_uframes_t s6000_pcm_pointer(struct snd_pcm_substream *substream)
  237. {
  238. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  239. struct s6000_pcm_dma_params *par;
  240. struct snd_pcm_runtime *runtime = substream->runtime;
  241. struct s6000_runtime_data *prtd = runtime->private_data;
  242. unsigned long flags;
  243. unsigned int offset;
  244. dma_addr_t count;
  245. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  246. spin_lock_irqsave(&prtd->lock, flags);
  247. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  248. count = s6dmac_cur_src(DMA_MASK_DMAC(par->dma_out),
  249. DMA_INDEX_CHNL(par->dma_out));
  250. else
  251. count = s6dmac_cur_dst(DMA_MASK_DMAC(par->dma_in),
  252. DMA_INDEX_CHNL(par->dma_in));
  253. count -= runtime->dma_addr;
  254. spin_unlock_irqrestore(&prtd->lock, flags);
  255. offset = bytes_to_frames(runtime, count);
  256. if (unlikely(offset >= runtime->buffer_size))
  257. offset = 0;
  258. return offset;
  259. }
  260. static int s6000_pcm_open(struct snd_pcm_substream *substream)
  261. {
  262. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  263. struct s6000_pcm_dma_params *par;
  264. struct snd_pcm_runtime *runtime = substream->runtime;
  265. struct s6000_runtime_data *prtd;
  266. int ret;
  267. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  268. snd_soc_set_runtime_hwparams(substream, &s6000_pcm_hardware);
  269. ret = snd_pcm_hw_constraint_step(runtime, 0,
  270. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 16);
  271. if (ret < 0)
  272. return ret;
  273. ret = snd_pcm_hw_constraint_step(runtime, 0,
  274. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 16);
  275. if (ret < 0)
  276. return ret;
  277. ret = snd_pcm_hw_constraint_integer(runtime,
  278. SNDRV_PCM_HW_PARAM_PERIODS);
  279. if (ret < 0)
  280. return ret;
  281. if (par->same_rate) {
  282. int rate;
  283. spin_lock(&par->lock); /* needed? */
  284. rate = par->rate;
  285. spin_unlock(&par->lock);
  286. if (rate != -1) {
  287. ret = snd_pcm_hw_constraint_minmax(runtime,
  288. SNDRV_PCM_HW_PARAM_RATE,
  289. rate, rate);
  290. if (ret < 0)
  291. return ret;
  292. }
  293. }
  294. prtd = kzalloc(sizeof(struct s6000_runtime_data), GFP_KERNEL);
  295. if (prtd == NULL)
  296. return -ENOMEM;
  297. spin_lock_init(&prtd->lock);
  298. runtime->private_data = prtd;
  299. return 0;
  300. }
  301. static int s6000_pcm_close(struct snd_pcm_substream *substream)
  302. {
  303. struct snd_pcm_runtime *runtime = substream->runtime;
  304. struct s6000_runtime_data *prtd = runtime->private_data;
  305. kfree(prtd);
  306. return 0;
  307. }
  308. static int s6000_pcm_hw_params(struct snd_pcm_substream *substream,
  309. struct snd_pcm_hw_params *hw_params)
  310. {
  311. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  312. struct s6000_pcm_dma_params *par;
  313. int ret;
  314. ret = snd_pcm_lib_malloc_pages(substream,
  315. params_buffer_bytes(hw_params));
  316. if (ret < 0) {
  317. printk(KERN_WARNING "s6000-pcm: allocation of memory failed\n");
  318. return ret;
  319. }
  320. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  321. if (par->same_rate) {
  322. spin_lock(&par->lock);
  323. if (par->rate == -1 ||
  324. !(par->in_use & ~(1 << substream->stream))) {
  325. par->rate = params_rate(hw_params);
  326. par->in_use |= 1 << substream->stream;
  327. } else if (params_rate(hw_params) != par->rate) {
  328. snd_pcm_lib_free_pages(substream);
  329. par->in_use &= ~(1 << substream->stream);
  330. ret = -EBUSY;
  331. }
  332. spin_unlock(&par->lock);
  333. }
  334. return ret;
  335. }
  336. static int s6000_pcm_hw_free(struct snd_pcm_substream *substream)
  337. {
  338. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  339. struct s6000_pcm_dma_params *par =
  340. snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  341. spin_lock(&par->lock);
  342. par->in_use &= ~(1 << substream->stream);
  343. if (!par->in_use)
  344. par->rate = -1;
  345. spin_unlock(&par->lock);
  346. return snd_pcm_lib_free_pages(substream);
  347. }
  348. static struct snd_pcm_ops s6000_pcm_ops = {
  349. .open = s6000_pcm_open,
  350. .close = s6000_pcm_close,
  351. .ioctl = snd_pcm_lib_ioctl,
  352. .hw_params = s6000_pcm_hw_params,
  353. .hw_free = s6000_pcm_hw_free,
  354. .trigger = s6000_pcm_trigger,
  355. .prepare = s6000_pcm_prepare,
  356. .pointer = s6000_pcm_pointer,
  357. };
  358. static void s6000_pcm_free(struct snd_pcm *pcm)
  359. {
  360. struct snd_soc_pcm_runtime *runtime = pcm->private_data;
  361. struct s6000_pcm_dma_params *params =
  362. snd_soc_dai_get_dma_data(runtime->cpu_dai, pcm->streams[0].substream);
  363. free_irq(params->irq, pcm);
  364. snd_pcm_lib_preallocate_free_for_all(pcm);
  365. }
  366. static u64 s6000_pcm_dmamask = DMA_BIT_MASK(32);
  367. static int s6000_pcm_new(struct snd_card *card,
  368. struct snd_soc_dai *dai, struct snd_pcm *pcm)
  369. {
  370. struct snd_soc_pcm_runtime *runtime = pcm->private_data;
  371. struct s6000_pcm_dma_params *params;
  372. int res;
  373. params = snd_soc_dai_get_dma_data(runtime->cpu_dai,
  374. pcm->streams[0].substream);
  375. if (!card->dev->dma_mask)
  376. card->dev->dma_mask = &s6000_pcm_dmamask;
  377. if (!card->dev->coherent_dma_mask)
  378. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  379. if (params->dma_in) {
  380. s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_in),
  381. DMA_INDEX_CHNL(params->dma_in));
  382. s6dmac_int_sources(DMA_MASK_DMAC(params->dma_in),
  383. DMA_INDEX_CHNL(params->dma_in));
  384. }
  385. if (params->dma_out) {
  386. s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_out),
  387. DMA_INDEX_CHNL(params->dma_out));
  388. s6dmac_int_sources(DMA_MASK_DMAC(params->dma_out),
  389. DMA_INDEX_CHNL(params->dma_out));
  390. }
  391. res = request_irq(params->irq, s6000_pcm_irq, IRQF_SHARED,
  392. "s6000-audio", pcm);
  393. if (res) {
  394. printk(KERN_ERR "s6000-pcm couldn't get IRQ\n");
  395. return res;
  396. }
  397. res = snd_pcm_lib_preallocate_pages_for_all(pcm,
  398. SNDRV_DMA_TYPE_DEV,
  399. card->dev,
  400. S6_PCM_PREALLOCATE_SIZE,
  401. S6_PCM_PREALLOCATE_MAX);
  402. if (res)
  403. printk(KERN_WARNING "s6000-pcm: preallocation failed\n");
  404. spin_lock_init(&params->lock);
  405. params->in_use = 0;
  406. params->rate = -1;
  407. return 0;
  408. }
  409. static struct snd_soc_platform_driver s6000_soc_platform = {
  410. .ops = &s6000_pcm_ops,
  411. .pcm_new = s6000_pcm_new,
  412. .pcm_free = s6000_pcm_free,
  413. };
  414. static int __devinit s6000_soc_platform_probe(struct platform_device *pdev)
  415. {
  416. return snd_soc_register_platform(&pdev->dev, &s6000_soc_platform);
  417. }
  418. static int __devexit s6000_soc_platform_remove(struct platform_device *pdev)
  419. {
  420. snd_soc_unregister_platform(&pdev->dev);
  421. return 0;
  422. }
  423. static struct platform_driver s6000_pcm_driver = {
  424. .driver = {
  425. .name = "s6000-pcm-audio",
  426. .owner = THIS_MODULE,
  427. },
  428. .probe = s6000_soc_platform_probe,
  429. .remove = __devexit_p(s6000_soc_platform_remove),
  430. };
  431. static int __init snd_s6000_pcm_init(void)
  432. {
  433. return platform_driver_register(&s6000_pcm_driver);
  434. }
  435. module_init(snd_s6000_pcm_init);
  436. static void __exit snd_s6000_pcm_exit(void)
  437. {
  438. platform_driver_unregister(&s6000_pcm_driver);
  439. }
  440. module_exit(snd_s6000_pcm_exit);
  441. MODULE_AUTHOR("Daniel Gloeckner");
  442. MODULE_DESCRIPTION("Stretch s6000 family PCM DMA module");
  443. MODULE_LICENSE("GPL");