/sound/soc/s6000/s6000-pcm.c

https://bitbucket.org/abioy/linux · C · 515 lines · 416 code · 89 blank · 10 comment · 49 complexity · 7c0e98d82f67b1cef1c765ccd9b1e414 MD5 · raw file

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