cx23885-alsa.c 13 KB

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