tw686x-audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
  3. *
  4. * Based on the audio support from the tw6869 driver:
  5. * Copyright 2015 www.starterkit.ru <info@starterkit.ru>
  6. *
  7. * Based on:
  8. * Driver for Intersil|Techwell TW6869 based DVR cards
  9. * (c) 2011-12 liran <jli11@intersil.com> [Intersil|Techwell China]
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of version 2 of the GNU General Public License
  13. * as published by the Free Software Foundation.
  14. */
  15. #include <linux/types.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/kmod.h>
  20. #include <linux/mutex.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <sound/core.h>
  24. #include <sound/initval.h>
  25. #include <sound/pcm.h>
  26. #include <sound/control.h>
  27. #include "tw686x.h"
  28. #include "tw686x-regs.h"
  29. #define AUDIO_CHANNEL_OFFSET 8
  30. void tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests,
  31. unsigned int pb_status)
  32. {
  33. unsigned long flags;
  34. unsigned int ch, pb;
  35. for_each_set_bit(ch, &requests, max_channels(dev)) {
  36. struct tw686x_audio_channel *ac = &dev->audio_channels[ch];
  37. struct tw686x_audio_buf *done = NULL;
  38. struct tw686x_audio_buf *next = NULL;
  39. struct tw686x_dma_desc *desc;
  40. pb = !!(pb_status & BIT(AUDIO_CHANNEL_OFFSET + ch));
  41. spin_lock_irqsave(&ac->lock, flags);
  42. /* Sanity check */
  43. if (!ac->ss || !ac->curr_bufs[0] || !ac->curr_bufs[1]) {
  44. spin_unlock_irqrestore(&ac->lock, flags);
  45. continue;
  46. }
  47. if (!list_empty(&ac->buf_list)) {
  48. next = list_first_entry(&ac->buf_list,
  49. struct tw686x_audio_buf, list);
  50. list_move_tail(&next->list, &ac->buf_list);
  51. done = ac->curr_bufs[!pb];
  52. ac->curr_bufs[pb] = next;
  53. }
  54. spin_unlock_irqrestore(&ac->lock, flags);
  55. if (!done || !next)
  56. continue;
  57. /*
  58. * Checking for a non-nil dma_desc[pb]->virt buffer is
  59. * the same as checking for memcpy DMA mode.
  60. */
  61. desc = &ac->dma_descs[pb];
  62. if (desc->virt) {
  63. memcpy(done->virt, desc->virt,
  64. dev->period_size);
  65. } else {
  66. u32 reg = pb ? ADMA_B_ADDR[ch] : ADMA_P_ADDR[ch];
  67. reg_write(dev, reg, next->dma);
  68. }
  69. ac->ptr = done->dma - ac->buf[0].dma;
  70. snd_pcm_period_elapsed(ac->ss);
  71. }
  72. }
  73. static int tw686x_pcm_hw_params(struct snd_pcm_substream *ss,
  74. struct snd_pcm_hw_params *hw_params)
  75. {
  76. return snd_pcm_lib_malloc_pages(ss, params_buffer_bytes(hw_params));
  77. }
  78. static int tw686x_pcm_hw_free(struct snd_pcm_substream *ss)
  79. {
  80. return snd_pcm_lib_free_pages(ss);
  81. }
  82. /*
  83. * Audio parameters are global and shared among all
  84. * capture channels. The driver prevents changes to
  85. * the parameters if any audio channel is capturing.
  86. */
  87. static const struct snd_pcm_hardware tw686x_capture_hw = {
  88. .info = (SNDRV_PCM_INFO_MMAP |
  89. SNDRV_PCM_INFO_INTERLEAVED |
  90. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  91. SNDRV_PCM_INFO_MMAP_VALID),
  92. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  93. .rates = SNDRV_PCM_RATE_8000_48000,
  94. .rate_min = 8000,
  95. .rate_max = 48000,
  96. .channels_min = 1,
  97. .channels_max = 1,
  98. .buffer_bytes_max = TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
  99. .period_bytes_min = AUDIO_DMA_SIZE_MIN,
  100. .period_bytes_max = AUDIO_DMA_SIZE_MAX,
  101. .periods_min = TW686X_AUDIO_PERIODS_MIN,
  102. .periods_max = TW686X_AUDIO_PERIODS_MAX,
  103. };
  104. static int tw686x_pcm_open(struct snd_pcm_substream *ss)
  105. {
  106. struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
  107. struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
  108. struct snd_pcm_runtime *rt = ss->runtime;
  109. int err;
  110. ac->ss = ss;
  111. rt->hw = tw686x_capture_hw;
  112. err = snd_pcm_hw_constraint_integer(rt, SNDRV_PCM_HW_PARAM_PERIODS);
  113. if (err < 0)
  114. return err;
  115. return 0;
  116. }
  117. static int tw686x_pcm_close(struct snd_pcm_substream *ss)
  118. {
  119. struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
  120. struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
  121. ac->ss = NULL;
  122. return 0;
  123. }
  124. static int tw686x_pcm_prepare(struct snd_pcm_substream *ss)
  125. {
  126. struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
  127. struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
  128. struct snd_pcm_runtime *rt = ss->runtime;
  129. unsigned int period_size = snd_pcm_lib_period_bytes(ss);
  130. struct tw686x_audio_buf *p_buf, *b_buf;
  131. unsigned long flags;
  132. int i;
  133. spin_lock_irqsave(&dev->lock, flags);
  134. /*
  135. * Given the audio parameters are global (i.e. shared across
  136. * DMA channels), we need to check new params are allowed.
  137. */
  138. if (((dev->audio_rate != rt->rate) ||
  139. (dev->period_size != period_size)) && dev->audio_enabled)
  140. goto err_audio_busy;
  141. tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
  142. spin_unlock_irqrestore(&dev->lock, flags);
  143. if (dev->audio_rate != rt->rate) {
  144. u32 reg;
  145. dev->audio_rate = rt->rate;
  146. reg = ((125000000 / rt->rate) << 16) +
  147. ((125000000 % rt->rate) << 16) / rt->rate;
  148. reg_write(dev, AUDIO_CONTROL2, reg);
  149. }
  150. if (dev->period_size != period_size) {
  151. u32 reg;
  152. dev->period_size = period_size;
  153. reg = reg_read(dev, AUDIO_CONTROL1);
  154. reg &= ~(AUDIO_DMA_SIZE_MASK << AUDIO_DMA_SIZE_SHIFT);
  155. reg |= period_size << AUDIO_DMA_SIZE_SHIFT;
  156. reg_write(dev, AUDIO_CONTROL1, reg);
  157. }
  158. if (rt->periods < TW686X_AUDIO_PERIODS_MIN ||
  159. rt->periods > TW686X_AUDIO_PERIODS_MAX)
  160. return -EINVAL;
  161. spin_lock_irqsave(&ac->lock, flags);
  162. INIT_LIST_HEAD(&ac->buf_list);
  163. for (i = 0; i < rt->periods; i++) {
  164. ac->buf[i].dma = rt->dma_addr + period_size * i;
  165. ac->buf[i].virt = rt->dma_area + period_size * i;
  166. INIT_LIST_HEAD(&ac->buf[i].list);
  167. list_add_tail(&ac->buf[i].list, &ac->buf_list);
  168. }
  169. p_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
  170. list_move_tail(&p_buf->list, &ac->buf_list);
  171. b_buf = list_first_entry(&ac->buf_list, struct tw686x_audio_buf, list);
  172. list_move_tail(&b_buf->list, &ac->buf_list);
  173. ac->curr_bufs[0] = p_buf;
  174. ac->curr_bufs[1] = b_buf;
  175. ac->ptr = 0;
  176. if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY) {
  177. reg_write(dev, ADMA_P_ADDR[ac->ch], p_buf->dma);
  178. reg_write(dev, ADMA_B_ADDR[ac->ch], b_buf->dma);
  179. }
  180. spin_unlock_irqrestore(&ac->lock, flags);
  181. return 0;
  182. err_audio_busy:
  183. spin_unlock_irqrestore(&dev->lock, flags);
  184. return -EBUSY;
  185. }
  186. static int tw686x_pcm_trigger(struct snd_pcm_substream *ss, int cmd)
  187. {
  188. struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
  189. struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
  190. unsigned long flags;
  191. int err = 0;
  192. switch (cmd) {
  193. case SNDRV_PCM_TRIGGER_START:
  194. if (ac->curr_bufs[0] && ac->curr_bufs[1]) {
  195. spin_lock_irqsave(&dev->lock, flags);
  196. dev->audio_enabled = 1;
  197. tw686x_enable_channel(dev,
  198. AUDIO_CHANNEL_OFFSET + ac->ch);
  199. spin_unlock_irqrestore(&dev->lock, flags);
  200. mod_timer(&dev->dma_delay_timer,
  201. jiffies + msecs_to_jiffies(100));
  202. } else {
  203. err = -EIO;
  204. }
  205. break;
  206. case SNDRV_PCM_TRIGGER_STOP:
  207. spin_lock_irqsave(&dev->lock, flags);
  208. dev->audio_enabled = 0;
  209. tw686x_disable_channel(dev, AUDIO_CHANNEL_OFFSET + ac->ch);
  210. spin_unlock_irqrestore(&dev->lock, flags);
  211. spin_lock_irqsave(&ac->lock, flags);
  212. ac->curr_bufs[0] = NULL;
  213. ac->curr_bufs[1] = NULL;
  214. spin_unlock_irqrestore(&ac->lock, flags);
  215. break;
  216. default:
  217. err = -EINVAL;
  218. }
  219. return err;
  220. }
  221. static snd_pcm_uframes_t tw686x_pcm_pointer(struct snd_pcm_substream *ss)
  222. {
  223. struct tw686x_dev *dev = snd_pcm_substream_chip(ss);
  224. struct tw686x_audio_channel *ac = &dev->audio_channels[ss->number];
  225. return bytes_to_frames(ss->runtime, ac->ptr);
  226. }
  227. static const struct snd_pcm_ops tw686x_pcm_ops = {
  228. .open = tw686x_pcm_open,
  229. .close = tw686x_pcm_close,
  230. .ioctl = snd_pcm_lib_ioctl,
  231. .hw_params = tw686x_pcm_hw_params,
  232. .hw_free = tw686x_pcm_hw_free,
  233. .prepare = tw686x_pcm_prepare,
  234. .trigger = tw686x_pcm_trigger,
  235. .pointer = tw686x_pcm_pointer,
  236. };
  237. static int tw686x_snd_pcm_init(struct tw686x_dev *dev)
  238. {
  239. struct snd_card *card = dev->snd_card;
  240. struct snd_pcm *pcm;
  241. struct snd_pcm_substream *ss;
  242. unsigned int i;
  243. int err;
  244. err = snd_pcm_new(card, card->driver, 0, 0, max_channels(dev), &pcm);
  245. if (err < 0)
  246. return err;
  247. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &tw686x_pcm_ops);
  248. snd_pcm_chip(pcm) = dev;
  249. pcm->info_flags = 0;
  250. strlcpy(pcm->name, "tw686x PCM", sizeof(pcm->name));
  251. for (i = 0, ss = pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  252. ss; ss = ss->next, i++)
  253. snprintf(ss->name, sizeof(ss->name), "vch%u audio", i);
  254. return snd_pcm_lib_preallocate_pages_for_all(pcm,
  255. SNDRV_DMA_TYPE_DEV,
  256. snd_dma_pci_data(dev->pci_dev),
  257. TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX,
  258. TW686X_AUDIO_PAGE_MAX * AUDIO_DMA_SIZE_MAX);
  259. }
  260. static void tw686x_audio_dma_free(struct tw686x_dev *dev,
  261. struct tw686x_audio_channel *ac)
  262. {
  263. int pb;
  264. for (pb = 0; pb < 2; pb++) {
  265. if (!ac->dma_descs[pb].virt)
  266. continue;
  267. pci_free_consistent(dev->pci_dev, ac->dma_descs[pb].size,
  268. ac->dma_descs[pb].virt,
  269. ac->dma_descs[pb].phys);
  270. ac->dma_descs[pb].virt = NULL;
  271. }
  272. }
  273. static int tw686x_audio_dma_alloc(struct tw686x_dev *dev,
  274. struct tw686x_audio_channel *ac)
  275. {
  276. int pb;
  277. /*
  278. * In the memcpy DMA mode we allocate a consistent buffer
  279. * and use it for the DMA capture. Otherwise, DMA
  280. * acts on the ALSA buffers as received in pcm_prepare.
  281. */
  282. if (dev->dma_mode != TW686X_DMA_MODE_MEMCPY)
  283. return 0;
  284. for (pb = 0; pb < 2; pb++) {
  285. u32 reg = pb ? ADMA_B_ADDR[ac->ch] : ADMA_P_ADDR[ac->ch];
  286. void *virt;
  287. virt = pci_alloc_consistent(dev->pci_dev, AUDIO_DMA_SIZE_MAX,
  288. &ac->dma_descs[pb].phys);
  289. if (!virt) {
  290. dev_err(&dev->pci_dev->dev,
  291. "dma%d: unable to allocate audio DMA %s-buffer\n",
  292. ac->ch, pb ? "B" : "P");
  293. return -ENOMEM;
  294. }
  295. ac->dma_descs[pb].virt = virt;
  296. ac->dma_descs[pb].size = AUDIO_DMA_SIZE_MAX;
  297. reg_write(dev, reg, ac->dma_descs[pb].phys);
  298. }
  299. return 0;
  300. }
  301. void tw686x_audio_free(struct tw686x_dev *dev)
  302. {
  303. unsigned long flags;
  304. u32 dma_ch_mask;
  305. u32 dma_cmd;
  306. spin_lock_irqsave(&dev->lock, flags);
  307. dma_cmd = reg_read(dev, DMA_CMD);
  308. dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
  309. reg_write(dev, DMA_CMD, dma_cmd & ~0xff00);
  310. reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask & ~0xff00);
  311. spin_unlock_irqrestore(&dev->lock, flags);
  312. if (!dev->snd_card)
  313. return;
  314. snd_card_free(dev->snd_card);
  315. dev->snd_card = NULL;
  316. }
  317. int tw686x_audio_init(struct tw686x_dev *dev)
  318. {
  319. struct pci_dev *pci_dev = dev->pci_dev;
  320. struct snd_card *card;
  321. int err, ch;
  322. /* Enable external audio */
  323. reg_write(dev, AUDIO_CONTROL1, BIT(0));
  324. err = snd_card_new(&pci_dev->dev, SNDRV_DEFAULT_IDX1,
  325. SNDRV_DEFAULT_STR1,
  326. THIS_MODULE, 0, &card);
  327. if (err < 0)
  328. return err;
  329. dev->snd_card = card;
  330. strlcpy(card->driver, "tw686x", sizeof(card->driver));
  331. strlcpy(card->shortname, "tw686x", sizeof(card->shortname));
  332. strlcpy(card->longname, pci_name(pci_dev), sizeof(card->longname));
  333. snd_card_set_dev(card, &pci_dev->dev);
  334. for (ch = 0; ch < max_channels(dev); ch++) {
  335. struct tw686x_audio_channel *ac;
  336. ac = &dev->audio_channels[ch];
  337. spin_lock_init(&ac->lock);
  338. ac->dev = dev;
  339. ac->ch = ch;
  340. err = tw686x_audio_dma_alloc(dev, ac);
  341. if (err < 0)
  342. goto err_cleanup;
  343. }
  344. err = tw686x_snd_pcm_init(dev);
  345. if (err < 0)
  346. goto err_cleanup;
  347. err = snd_card_register(card);
  348. if (!err)
  349. return 0;
  350. err_cleanup:
  351. for (ch = 0; ch < max_channels(dev); ch++) {
  352. if (!dev->audio_channels[ch].dev)
  353. continue;
  354. tw686x_audio_dma_free(dev, &dev->audio_channels[ch]);
  355. }
  356. snd_card_free(card);
  357. dev->snd_card = NULL;
  358. return err;
  359. }