s6000-pcm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. if (WARN_ON(period_size & 15))
  80. return;
  81. s6dmac_put_fifo(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel),
  82. src, dst, period_size);
  83. prtd->period++;
  84. if (unlikely(prtd->period >= runtime->periods))
  85. prtd->period = 0;
  86. }
  87. static irqreturn_t s6000_pcm_irq(int irq, void *data)
  88. {
  89. struct snd_pcm *pcm = data;
  90. struct snd_soc_pcm_runtime *runtime = pcm->private_data;
  91. struct s6000_runtime_data *prtd;
  92. unsigned int has_xrun;
  93. int i, ret = IRQ_NONE;
  94. for (i = 0; i < 2; ++i) {
  95. struct snd_pcm_substream *substream = pcm->streams[i].substream;
  96. struct s6000_pcm_dma_params *params =
  97. snd_soc_dai_get_dma_data(runtime->cpu_dai, substream);
  98. u32 channel;
  99. unsigned int pending;
  100. if (substream == SNDRV_PCM_STREAM_PLAYBACK)
  101. channel = params->dma_out;
  102. else
  103. channel = params->dma_in;
  104. has_xrun = params->check_xrun(runtime->cpu_dai);
  105. if (!channel)
  106. continue;
  107. if (unlikely(has_xrun & (1 << i)) &&
  108. substream->runtime &&
  109. snd_pcm_running(substream)) {
  110. dev_dbg(pcm->dev, "xrun\n");
  111. snd_pcm_stream_lock(substream);
  112. snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
  113. snd_pcm_stream_unlock(substream);
  114. ret = IRQ_HANDLED;
  115. }
  116. pending = s6dmac_int_sources(DMA_MASK_DMAC(channel),
  117. DMA_INDEX_CHNL(channel));
  118. if (pending & 1) {
  119. ret = IRQ_HANDLED;
  120. if (likely(substream->runtime &&
  121. snd_pcm_running(substream))) {
  122. snd_pcm_period_elapsed(substream);
  123. dev_dbg(pcm->dev, "period elapsed %x %x\n",
  124. s6dmac_cur_src(DMA_MASK_DMAC(channel),
  125. DMA_INDEX_CHNL(channel)),
  126. s6dmac_cur_dst(DMA_MASK_DMAC(channel),
  127. DMA_INDEX_CHNL(channel)));
  128. prtd = substream->runtime->private_data;
  129. spin_lock(&prtd->lock);
  130. s6000_pcm_enqueue_dma(substream);
  131. spin_unlock(&prtd->lock);
  132. }
  133. }
  134. if (unlikely(pending & ~7)) {
  135. if (pending & (1 << 3))
  136. printk(KERN_WARNING
  137. "s6000-pcm: DMA %x Underflow\n",
  138. channel);
  139. if (pending & (1 << 4))
  140. printk(KERN_WARNING
  141. "s6000-pcm: DMA %x Overflow\n",
  142. channel);
  143. if (pending & 0x1e0)
  144. printk(KERN_WARNING
  145. "s6000-pcm: DMA %x Master Error "
  146. "(mask %x)\n",
  147. channel, pending >> 5);
  148. }
  149. }
  150. return ret;
  151. }
  152. static int s6000_pcm_start(struct snd_pcm_substream *substream)
  153. {
  154. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  155. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  156. struct s6000_pcm_dma_params *par;
  157. unsigned long flags;
  158. int srcinc;
  159. u32 dma;
  160. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  161. spin_lock_irqsave(&prtd->lock, flags);
  162. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  163. srcinc = 1;
  164. dma = par->dma_out;
  165. } else {
  166. srcinc = 0;
  167. dma = par->dma_in;
  168. }
  169. s6dmac_enable_chan(DMA_MASK_DMAC(dma), DMA_INDEX_CHNL(dma),
  170. 1 /* priority 1 (0 is max) */,
  171. 0 /* peripheral requests w/o xfer length mode */,
  172. srcinc /* source address increment */,
  173. srcinc^1 /* destination address increment */,
  174. 0 /* chunksize 0 (skip impossible on this dma) */,
  175. 0 /* source skip after chunk (impossible) */,
  176. 0 /* destination skip after chunk (impossible) */,
  177. 4 /* 16 byte burst size */,
  178. -1 /* don't conserve bandwidth */,
  179. 0 /* low watermark irq descriptor threshold */,
  180. 0 /* disable hardware timestamps */,
  181. 1 /* enable channel */);
  182. s6000_pcm_enqueue_dma(substream);
  183. s6000_pcm_enqueue_dma(substream);
  184. spin_unlock_irqrestore(&prtd->lock, flags);
  185. return 0;
  186. }
  187. static int s6000_pcm_stop(struct snd_pcm_substream *substream)
  188. {
  189. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  190. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  191. struct s6000_pcm_dma_params *par;
  192. unsigned long flags;
  193. u32 channel;
  194. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  195. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  196. channel = par->dma_out;
  197. else
  198. channel = par->dma_in;
  199. s6dmac_set_terminal_count(DMA_MASK_DMAC(channel),
  200. DMA_INDEX_CHNL(channel), 0);
  201. spin_lock_irqsave(&prtd->lock, flags);
  202. s6dmac_disable_chan(DMA_MASK_DMAC(channel), DMA_INDEX_CHNL(channel));
  203. spin_unlock_irqrestore(&prtd->lock, flags);
  204. return 0;
  205. }
  206. static int s6000_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  207. {
  208. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  209. struct s6000_pcm_dma_params *par;
  210. int ret;
  211. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  212. ret = par->trigger(substream, cmd, 0);
  213. if (ret < 0)
  214. return ret;
  215. switch (cmd) {
  216. case SNDRV_PCM_TRIGGER_START:
  217. case SNDRV_PCM_TRIGGER_RESUME:
  218. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  219. ret = s6000_pcm_start(substream);
  220. break;
  221. case SNDRV_PCM_TRIGGER_STOP:
  222. case SNDRV_PCM_TRIGGER_SUSPEND:
  223. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  224. ret = s6000_pcm_stop(substream);
  225. break;
  226. default:
  227. ret = -EINVAL;
  228. }
  229. if (ret < 0)
  230. return ret;
  231. return par->trigger(substream, cmd, 1);
  232. }
  233. static int s6000_pcm_prepare(struct snd_pcm_substream *substream)
  234. {
  235. struct s6000_runtime_data *prtd = substream->runtime->private_data;
  236. prtd->period = 0;
  237. return 0;
  238. }
  239. static snd_pcm_uframes_t s6000_pcm_pointer(struct snd_pcm_substream *substream)
  240. {
  241. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  242. struct s6000_pcm_dma_params *par;
  243. struct snd_pcm_runtime *runtime = substream->runtime;
  244. struct s6000_runtime_data *prtd = runtime->private_data;
  245. unsigned long flags;
  246. unsigned int offset;
  247. dma_addr_t count;
  248. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  249. spin_lock_irqsave(&prtd->lock, flags);
  250. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  251. count = s6dmac_cur_src(DMA_MASK_DMAC(par->dma_out),
  252. DMA_INDEX_CHNL(par->dma_out));
  253. else
  254. count = s6dmac_cur_dst(DMA_MASK_DMAC(par->dma_in),
  255. DMA_INDEX_CHNL(par->dma_in));
  256. count -= runtime->dma_addr;
  257. spin_unlock_irqrestore(&prtd->lock, flags);
  258. offset = bytes_to_frames(runtime, count);
  259. if (unlikely(offset >= runtime->buffer_size))
  260. offset = 0;
  261. return offset;
  262. }
  263. static int s6000_pcm_open(struct snd_pcm_substream *substream)
  264. {
  265. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  266. struct s6000_pcm_dma_params *par;
  267. struct snd_pcm_runtime *runtime = substream->runtime;
  268. struct s6000_runtime_data *prtd;
  269. int ret;
  270. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  271. snd_soc_set_runtime_hwparams(substream, &s6000_pcm_hardware);
  272. ret = snd_pcm_hw_constraint_step(runtime, 0,
  273. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 16);
  274. if (ret < 0)
  275. return ret;
  276. ret = snd_pcm_hw_constraint_step(runtime, 0,
  277. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 16);
  278. if (ret < 0)
  279. return ret;
  280. ret = snd_pcm_hw_constraint_integer(runtime,
  281. SNDRV_PCM_HW_PARAM_PERIODS);
  282. if (ret < 0)
  283. return ret;
  284. if (par->same_rate) {
  285. int rate;
  286. spin_lock(&par->lock); /* needed? */
  287. rate = par->rate;
  288. spin_unlock(&par->lock);
  289. if (rate != -1) {
  290. ret = snd_pcm_hw_constraint_minmax(runtime,
  291. SNDRV_PCM_HW_PARAM_RATE,
  292. rate, rate);
  293. if (ret < 0)
  294. return ret;
  295. }
  296. }
  297. prtd = kzalloc(sizeof(struct s6000_runtime_data), GFP_KERNEL);
  298. if (prtd == NULL)
  299. return -ENOMEM;
  300. spin_lock_init(&prtd->lock);
  301. runtime->private_data = prtd;
  302. return 0;
  303. }
  304. static int s6000_pcm_close(struct snd_pcm_substream *substream)
  305. {
  306. struct snd_pcm_runtime *runtime = substream->runtime;
  307. struct s6000_runtime_data *prtd = runtime->private_data;
  308. kfree(prtd);
  309. return 0;
  310. }
  311. static int s6000_pcm_hw_params(struct snd_pcm_substream *substream,
  312. struct snd_pcm_hw_params *hw_params)
  313. {
  314. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  315. struct s6000_pcm_dma_params *par;
  316. int ret;
  317. ret = snd_pcm_lib_malloc_pages(substream,
  318. params_buffer_bytes(hw_params));
  319. if (ret < 0) {
  320. printk(KERN_WARNING "s6000-pcm: allocation of memory failed\n");
  321. return ret;
  322. }
  323. par = snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  324. if (par->same_rate) {
  325. spin_lock(&par->lock);
  326. if (par->rate == -1 ||
  327. !(par->in_use & ~(1 << substream->stream))) {
  328. par->rate = params_rate(hw_params);
  329. par->in_use |= 1 << substream->stream;
  330. } else if (params_rate(hw_params) != par->rate) {
  331. snd_pcm_lib_free_pages(substream);
  332. par->in_use &= ~(1 << substream->stream);
  333. ret = -EBUSY;
  334. }
  335. spin_unlock(&par->lock);
  336. }
  337. return ret;
  338. }
  339. static int s6000_pcm_hw_free(struct snd_pcm_substream *substream)
  340. {
  341. struct snd_soc_pcm_runtime *soc_runtime = substream->private_data;
  342. struct s6000_pcm_dma_params *par =
  343. snd_soc_dai_get_dma_data(soc_runtime->cpu_dai, substream);
  344. spin_lock(&par->lock);
  345. par->in_use &= ~(1 << substream->stream);
  346. if (!par->in_use)
  347. par->rate = -1;
  348. spin_unlock(&par->lock);
  349. return snd_pcm_lib_free_pages(substream);
  350. }
  351. static struct snd_pcm_ops s6000_pcm_ops = {
  352. .open = s6000_pcm_open,
  353. .close = s6000_pcm_close,
  354. .ioctl = snd_pcm_lib_ioctl,
  355. .hw_params = s6000_pcm_hw_params,
  356. .hw_free = s6000_pcm_hw_free,
  357. .trigger = s6000_pcm_trigger,
  358. .prepare = s6000_pcm_prepare,
  359. .pointer = s6000_pcm_pointer,
  360. };
  361. static void s6000_pcm_free(struct snd_pcm *pcm)
  362. {
  363. struct snd_soc_pcm_runtime *runtime = pcm->private_data;
  364. struct s6000_pcm_dma_params *params =
  365. snd_soc_dai_get_dma_data(runtime->cpu_dai,
  366. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
  367. free_irq(params->irq, pcm);
  368. snd_pcm_lib_preallocate_free_for_all(pcm);
  369. }
  370. static int s6000_pcm_new(struct snd_soc_pcm_runtime *runtime)
  371. {
  372. struct snd_card *card = runtime->card->snd_card;
  373. struct snd_pcm *pcm = runtime->pcm;
  374. struct s6000_pcm_dma_params *params;
  375. int res;
  376. params = snd_soc_dai_get_dma_data(runtime->cpu_dai,
  377. pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream);
  378. res = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
  379. if (res)
  380. return res;
  381. if (params->dma_in) {
  382. s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_in),
  383. DMA_INDEX_CHNL(params->dma_in));
  384. s6dmac_int_sources(DMA_MASK_DMAC(params->dma_in),
  385. DMA_INDEX_CHNL(params->dma_in));
  386. }
  387. if (params->dma_out) {
  388. s6dmac_disable_chan(DMA_MASK_DMAC(params->dma_out),
  389. DMA_INDEX_CHNL(params->dma_out));
  390. s6dmac_int_sources(DMA_MASK_DMAC(params->dma_out),
  391. DMA_INDEX_CHNL(params->dma_out));
  392. }
  393. res = request_irq(params->irq, s6000_pcm_irq, IRQF_SHARED,
  394. "s6000-audio", pcm);
  395. if (res) {
  396. printk(KERN_ERR "s6000-pcm couldn't get IRQ\n");
  397. return res;
  398. }
  399. res = snd_pcm_lib_preallocate_pages_for_all(pcm,
  400. SNDRV_DMA_TYPE_DEV,
  401. card->dev,
  402. S6_PCM_PREALLOCATE_SIZE,
  403. S6_PCM_PREALLOCATE_MAX);
  404. if (res)
  405. printk(KERN_WARNING "s6000-pcm: preallocation failed\n");
  406. spin_lock_init(&params->lock);
  407. params->in_use = 0;
  408. params->rate = -1;
  409. return 0;
  410. }
  411. static struct snd_soc_platform_driver s6000_soc_platform = {
  412. .ops = &s6000_pcm_ops,
  413. .pcm_new = s6000_pcm_new,
  414. .pcm_free = s6000_pcm_free,
  415. };
  416. static int s6000_soc_platform_probe(struct platform_device *pdev)
  417. {
  418. return snd_soc_register_platform(&pdev->dev, &s6000_soc_platform);
  419. }
  420. static int s6000_soc_platform_remove(struct platform_device *pdev)
  421. {
  422. snd_soc_unregister_platform(&pdev->dev);
  423. return 0;
  424. }
  425. static struct platform_driver s6000_pcm_driver = {
  426. .driver = {
  427. .name = "s6000-pcm-audio",
  428. .owner = THIS_MODULE,
  429. },
  430. .probe = s6000_soc_platform_probe,
  431. .remove = s6000_soc_platform_remove,
  432. };
  433. module_platform_driver(s6000_pcm_driver);
  434. MODULE_AUTHOR("Daniel Gloeckner");
  435. MODULE_DESCRIPTION("Stretch s6000 family PCM DMA module");
  436. MODULE_LICENSE("GPL");