cx23885-alsa.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. *
  3. * Support for CX23885 analog audio capture
  4. *
  5. * (c) 2008 Mijhail Moreyra <mijhail.moreyra@gmail.com>
  6. * Adapted from cx88-alsa.c
  7. * (c) 2009 Steven Toth <stoth@kernellabs.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/init.h>
  25. #include <linux/device.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/pci.h>
  30. #include <asm/delay.h>
  31. #include <sound/core.h>
  32. #include <sound/pcm.h>
  33. #include <sound/pcm_params.h>
  34. #include <sound/control.h>
  35. #include <sound/initval.h>
  36. #include <sound/tlv.h>
  37. #include "cx23885.h"
  38. #include "cx23885-reg.h"
  39. #define AUDIO_SRAM_CHANNEL SRAM_CH07
  40. #define dprintk(level, fmt, arg...) do { \
  41. if (audio_debug + 1 > level) \
  42. printk(KERN_INFO "%s: " fmt, chip->dev->name , ## arg); \
  43. } while(0)
  44. #define dprintk_core(level, fmt, arg...) if (audio_debug >= level) \
  45. printk(KERN_DEBUG "%s: " fmt, chip->dev->name , ## arg)
  46. /****************************************************************************
  47. Module global static vars
  48. ****************************************************************************/
  49. static unsigned int disable_analog_audio;
  50. module_param(disable_analog_audio, int, 0644);
  51. MODULE_PARM_DESC(disable_analog_audio, "disable analog audio ALSA driver");
  52. static unsigned int audio_debug;
  53. module_param(audio_debug, int, 0644);
  54. MODULE_PARM_DESC(audio_debug, "enable debug messages [analog audio]");
  55. /****************************************************************************
  56. Board specific funtions
  57. ****************************************************************************/
  58. /* Constants taken from cx88-reg.h */
  59. #define AUD_INT_DN_RISCI1 (1 << 0)
  60. #define AUD_INT_UP_RISCI1 (1 << 1)
  61. #define AUD_INT_RDS_DN_RISCI1 (1 << 2)
  62. #define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
  63. #define AUD_INT_UP_RISCI2 (1 << 5)
  64. #define AUD_INT_RDS_DN_RISCI2 (1 << 6)
  65. #define AUD_INT_DN_SYNC (1 << 12)
  66. #define AUD_INT_UP_SYNC (1 << 13)
  67. #define AUD_INT_RDS_DN_SYNC (1 << 14)
  68. #define AUD_INT_OPC_ERR (1 << 16)
  69. #define AUD_INT_BER_IRQ (1 << 20)
  70. #define AUD_INT_MCHG_IRQ (1 << 21)
  71. #define GP_COUNT_CONTROL_RESET 0x3
  72. /*
  73. * BOARD Specific: Sets audio DMA
  74. */
  75. static int cx23885_start_audio_dma(struct cx23885_audio_dev *chip)
  76. {
  77. struct cx23885_audio_buffer *buf = chip->buf;
  78. struct cx23885_dev *dev = chip->dev;
  79. struct sram_channel *audio_ch =
  80. &dev->sram_channels[AUDIO_SRAM_CHANNEL];
  81. dprintk(1, "%s()\n", __func__);
  82. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  83. cx_clear(AUD_INT_DMA_CTL, 0x11);
  84. /* setup fifo + format - out channel */
  85. cx23885_sram_channel_setup(chip->dev, audio_ch, buf->bpl,
  86. buf->risc.dma);
  87. /* sets bpl size */
  88. cx_write(AUD_INT_A_LNGTH, buf->bpl);
  89. /* This is required to get good audio (1 seems to be ok) */
  90. cx_write(AUD_INT_A_MODE, 1);
  91. /* reset counter */
  92. cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  93. atomic_set(&chip->count, 0);
  94. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
  95. "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start+12)>>1,
  96. chip->num_periods, buf->bpl * chip->num_periods);
  97. /* Enables corresponding bits at AUD_INT_STAT */
  98. cx_write(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  99. AUD_INT_DN_RISCI1);
  100. /* Clean any pending interrupt bits already set */
  101. cx_write(AUDIO_INT_INT_STAT, ~0);
  102. /* enable audio irqs */
  103. cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
  104. /* start dma */
  105. cx_set(DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  106. cx_set(AUD_INT_DMA_CTL, 0x11); /* audio downstream FIFO and
  107. RISC enable */
  108. if (audio_debug)
  109. cx23885_sram_channel_dump(chip->dev, audio_ch);
  110. return 0;
  111. }
  112. /*
  113. * BOARD Specific: Resets audio DMA
  114. */
  115. static int cx23885_stop_audio_dma(struct cx23885_audio_dev *chip)
  116. {
  117. struct cx23885_dev *dev = chip->dev;
  118. dprintk(1, "Stopping audio DMA\n");
  119. /* stop dma */
  120. cx_clear(AUD_INT_DMA_CTL, 0x11);
  121. /* disable irqs */
  122. cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
  123. cx_clear(AUDIO_INT_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  124. AUD_INT_DN_RISCI1);
  125. if (audio_debug)
  126. cx23885_sram_channel_dump(chip->dev,
  127. &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
  128. return 0;
  129. }
  130. /*
  131. * BOARD Specific: Handles audio IRQ
  132. */
  133. int cx23885_audio_irq(struct cx23885_dev *dev, u32 status, u32 mask)
  134. {
  135. struct cx23885_audio_dev *chip = dev->audio_dev;
  136. if (0 == (status & mask))
  137. return 0;
  138. cx_write(AUDIO_INT_INT_STAT, status);
  139. /* risc op code error */
  140. if (status & AUD_INT_OPC_ERR) {
  141. printk(KERN_WARNING "%s/1: Audio risc op code error\n",
  142. dev->name);
  143. cx_clear(AUD_INT_DMA_CTL, 0x11);
  144. cx23885_sram_channel_dump(dev,
  145. &dev->sram_channels[AUDIO_SRAM_CHANNEL]);
  146. }
  147. if (status & AUD_INT_DN_SYNC) {
  148. dprintk(1, "Downstream sync error\n");
  149. cx_write(AUD_INT_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
  150. return 1;
  151. }
  152. /* risc1 downstream */
  153. if (status & AUD_INT_DN_RISCI1) {
  154. atomic_set(&chip->count, cx_read(AUD_INT_A_GPCNT));
  155. snd_pcm_period_elapsed(chip->substream);
  156. }
  157. /* FIXME: Any other status should deserve a special handling? */
  158. return 1;
  159. }
  160. static int dsp_buffer_free(struct cx23885_audio_dev *chip)
  161. {
  162. BUG_ON(!chip->dma_size);
  163. dprintk(2, "Freeing buffer\n");
  164. videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
  165. videobuf_dma_free(chip->dma_risc);
  166. btcx_riscmem_free(chip->pci, &chip->buf->risc);
  167. kfree(chip->buf);
  168. chip->dma_risc = NULL;
  169. chip->dma_size = 0;
  170. return 0;
  171. }
  172. /****************************************************************************
  173. ALSA PCM Interface
  174. ****************************************************************************/
  175. /*
  176. * Digital hardware definition
  177. */
  178. #define DEFAULT_FIFO_SIZE 4096
  179. static struct snd_pcm_hardware snd_cx23885_digital_hw = {
  180. .info = SNDRV_PCM_INFO_MMAP |
  181. SNDRV_PCM_INFO_INTERLEAVED |
  182. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  183. SNDRV_PCM_INFO_MMAP_VALID,
  184. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  185. .rates = SNDRV_PCM_RATE_48000,
  186. .rate_min = 48000,
  187. .rate_max = 48000,
  188. .channels_min = 2,
  189. .channels_max = 2,
  190. /* Analog audio output will be full of clicks and pops if there
  191. are not exactly four lines in the SRAM FIFO buffer. */
  192. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  193. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  194. .periods_min = 1,
  195. .periods_max = 1024,
  196. .buffer_bytes_max = (1024*1024),
  197. };
  198. /*
  199. * audio pcm capture open callback
  200. */
  201. static int snd_cx23885_pcm_open(struct snd_pcm_substream *substream)
  202. {
  203. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  204. struct snd_pcm_runtime *runtime = substream->runtime;
  205. int err;
  206. if (!chip) {
  207. printk(KERN_ERR "BUG: cx23885 can't find device struct."
  208. " Can't proceed with open\n");
  209. return -ENODEV;
  210. }
  211. err = snd_pcm_hw_constraint_pow2(runtime, 0,
  212. SNDRV_PCM_HW_PARAM_PERIODS);
  213. if (err < 0)
  214. goto _error;
  215. chip->substream = substream;
  216. runtime->hw = snd_cx23885_digital_hw;
  217. if (chip->dev->sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
  218. DEFAULT_FIFO_SIZE) {
  219. unsigned int bpl = chip->dev->
  220. sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 4;
  221. bpl &= ~7; /* must be multiple of 8 */
  222. runtime->hw.period_bytes_min = bpl;
  223. runtime->hw.period_bytes_max = bpl;
  224. }
  225. return 0;
  226. _error:
  227. dprintk(1, "Error opening PCM!\n");
  228. return err;
  229. }
  230. /*
  231. * audio close callback
  232. */
  233. static int snd_cx23885_close(struct snd_pcm_substream *substream)
  234. {
  235. return 0;
  236. }
  237. /*
  238. * hw_params callback
  239. */
  240. static int snd_cx23885_hw_params(struct snd_pcm_substream *substream,
  241. struct snd_pcm_hw_params *hw_params)
  242. {
  243. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  244. struct videobuf_dmabuf *dma;
  245. struct cx23885_audio_buffer *buf;
  246. int ret;
  247. if (substream->runtime->dma_area) {
  248. dsp_buffer_free(chip);
  249. substream->runtime->dma_area = NULL;
  250. }
  251. chip->period_size = params_period_bytes(hw_params);
  252. chip->num_periods = params_periods(hw_params);
  253. chip->dma_size = chip->period_size * params_periods(hw_params);
  254. BUG_ON(!chip->dma_size);
  255. BUG_ON(chip->num_periods & (chip->num_periods-1));
  256. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  257. if (NULL == buf)
  258. return -ENOMEM;
  259. buf->bpl = chip->period_size;
  260. dma = &buf->dma;
  261. videobuf_dma_init(dma);
  262. ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
  263. (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
  264. if (ret < 0)
  265. goto error;
  266. ret = videobuf_dma_map(&chip->pci->dev, dma);
  267. if (ret < 0)
  268. goto error;
  269. ret = cx23885_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
  270. chip->period_size, chip->num_periods, 1);
  271. if (ret < 0)
  272. goto error;
  273. /* Loop back to start of program */
  274. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  275. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  276. buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
  277. chip->buf = buf;
  278. chip->dma_risc = dma;
  279. substream->runtime->dma_area = chip->dma_risc->vaddr;
  280. substream->runtime->dma_bytes = chip->dma_size;
  281. substream->runtime->dma_addr = 0;
  282. return 0;
  283. error:
  284. kfree(buf);
  285. return ret;
  286. }
  287. /*
  288. * hw free callback
  289. */
  290. static int snd_cx23885_hw_free(struct snd_pcm_substream *substream)
  291. {
  292. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  293. if (substream->runtime->dma_area) {
  294. dsp_buffer_free(chip);
  295. substream->runtime->dma_area = NULL;
  296. }
  297. return 0;
  298. }
  299. /*
  300. * prepare callback
  301. */
  302. static int snd_cx23885_prepare(struct snd_pcm_substream *substream)
  303. {
  304. return 0;
  305. }
  306. /*
  307. * trigger callback
  308. */
  309. static int snd_cx23885_card_trigger(struct snd_pcm_substream *substream,
  310. int cmd)
  311. {
  312. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  313. int err;
  314. /* Local interrupts are already disabled by ALSA */
  315. spin_lock(&chip->lock);
  316. switch (cmd) {
  317. case SNDRV_PCM_TRIGGER_START:
  318. err = cx23885_start_audio_dma(chip);
  319. break;
  320. case SNDRV_PCM_TRIGGER_STOP:
  321. err = cx23885_stop_audio_dma(chip);
  322. break;
  323. default:
  324. err = -EINVAL;
  325. break;
  326. }
  327. spin_unlock(&chip->lock);
  328. return err;
  329. }
  330. /*
  331. * pointer callback
  332. */
  333. static snd_pcm_uframes_t snd_cx23885_pointer(
  334. struct snd_pcm_substream *substream)
  335. {
  336. struct cx23885_audio_dev *chip = snd_pcm_substream_chip(substream);
  337. struct snd_pcm_runtime *runtime = substream->runtime;
  338. u16 count;
  339. count = atomic_read(&chip->count);
  340. return runtime->period_size * (count & (runtime->periods-1));
  341. }
  342. /*
  343. * page callback (needed for mmap)
  344. */
  345. static struct page *snd_cx23885_page(struct snd_pcm_substream *substream,
  346. unsigned long offset)
  347. {
  348. void *pageptr = substream->runtime->dma_area + offset;
  349. return vmalloc_to_page(pageptr);
  350. }
  351. /*
  352. * operators
  353. */
  354. static struct snd_pcm_ops snd_cx23885_pcm_ops = {
  355. .open = snd_cx23885_pcm_open,
  356. .close = snd_cx23885_close,
  357. .ioctl = snd_pcm_lib_ioctl,
  358. .hw_params = snd_cx23885_hw_params,
  359. .hw_free = snd_cx23885_hw_free,
  360. .prepare = snd_cx23885_prepare,
  361. .trigger = snd_cx23885_card_trigger,
  362. .pointer = snd_cx23885_pointer,
  363. .page = snd_cx23885_page,
  364. };
  365. /*
  366. * create a PCM device
  367. */
  368. static int snd_cx23885_pcm(struct cx23885_audio_dev *chip, int device,
  369. char *name)
  370. {
  371. int err;
  372. struct snd_pcm *pcm;
  373. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  374. if (err < 0)
  375. return err;
  376. pcm->private_data = chip;
  377. strcpy(pcm->name, name);
  378. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx23885_pcm_ops);
  379. return 0;
  380. }
  381. /****************************************************************************
  382. Basic Flow for Sound Devices
  383. ****************************************************************************/
  384. /*
  385. * Alsa Constructor - Component probe
  386. */
  387. struct cx23885_audio_dev *cx23885_audio_register(struct cx23885_dev *dev)
  388. {
  389. struct snd_card *card;
  390. struct cx23885_audio_dev *chip;
  391. int err;
  392. if (disable_analog_audio)
  393. return NULL;
  394. if (dev->sram_channels[AUDIO_SRAM_CHANNEL].cmds_start == 0) {
  395. printk(KERN_WARNING "%s(): Missing SRAM channel configuration "
  396. "for analog TV Audio\n", __func__);
  397. return NULL;
  398. }
  399. err = snd_card_create(SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  400. THIS_MODULE, sizeof(struct cx23885_audio_dev), &card);
  401. if (err < 0)
  402. goto error;
  403. chip = (struct cx23885_audio_dev *) card->private_data;
  404. chip->dev = dev;
  405. chip->pci = dev->pci;
  406. chip->card = card;
  407. spin_lock_init(&chip->lock);
  408. snd_card_set_dev(card, &dev->pci->dev);
  409. err = snd_cx23885_pcm(chip, 0, "CX23885 Digital");
  410. if (err < 0)
  411. goto error;
  412. strcpy(card->driver, "CX23885");
  413. sprintf(card->shortname, "Conexant CX23885");
  414. sprintf(card->longname, "%s at %s", card->shortname, dev->name);
  415. err = snd_card_register(card);
  416. if (err < 0)
  417. goto error;
  418. dprintk(0, "registered ALSA audio device\n");
  419. return chip;
  420. error:
  421. snd_card_free(card);
  422. printk(KERN_ERR "%s(): Failed to register analog "
  423. "audio adapter\n", __func__);
  424. return NULL;
  425. }
  426. /*
  427. * ALSA destructor
  428. */
  429. void cx23885_audio_unregister(struct cx23885_dev *dev)
  430. {
  431. struct cx23885_audio_dev *chip = dev->audio_dev;
  432. snd_card_free(chip->card);
  433. }