cx23885-alsa.c 15 KB

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