s6000-pcm.c 13 KB

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