cx23885-alsa.c 15 KB

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