cx23885-alsa.c 14 KB

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