/drivers/media/video/cx88/cx88-alsa.c

https://bitbucket.org/ndreys/linux-sunxi · C · 1003 lines · 662 code · 183 blank · 158 comment · 65 complexity · f5094a90ff24965442cc0c987717ee1f MD5 · raw file

  1. /*
  2. *
  3. * Support for audio capture
  4. * PCI function #1 of the cx2388x.
  5. *
  6. * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
  7. * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
  8. * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  9. * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
  10. * Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/pci.h>
  33. #include <linux/slab.h>
  34. #include <asm/delay.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include <sound/control.h>
  39. #include <sound/initval.h>
  40. #include <sound/tlv.h>
  41. #include <media/wm8775.h>
  42. #include "cx88.h"
  43. #include "cx88-reg.h"
  44. #define dprintk(level,fmt, arg...) if (debug >= level) \
  45. printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg)
  46. #define dprintk_core(level,fmt, arg...) if (debug >= level) \
  47. printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
  48. /****************************************************************************
  49. Data type declarations - Can be moded to a header file later
  50. ****************************************************************************/
  51. struct cx88_audio_buffer {
  52. unsigned int bpl;
  53. struct btcx_riscmem risc;
  54. struct videobuf_dmabuf dma;
  55. };
  56. struct cx88_audio_dev {
  57. struct cx88_core *core;
  58. struct cx88_dmaqueue q;
  59. /* pci i/o */
  60. struct pci_dev *pci;
  61. /* audio controls */
  62. int irq;
  63. struct snd_card *card;
  64. spinlock_t reg_lock;
  65. atomic_t count;
  66. unsigned int dma_size;
  67. unsigned int period_size;
  68. unsigned int num_periods;
  69. struct videobuf_dmabuf *dma_risc;
  70. struct cx88_audio_buffer *buf;
  71. struct snd_pcm_substream *substream;
  72. };
  73. typedef struct cx88_audio_dev snd_cx88_card_t;
  74. /****************************************************************************
  75. Module global static vars
  76. ****************************************************************************/
  77. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  78. static const char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  79. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  80. module_param_array(enable, bool, NULL, 0444);
  81. MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
  82. module_param_array(index, int, NULL, 0444);
  83. MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
  84. /****************************************************************************
  85. Module macros
  86. ****************************************************************************/
  87. MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
  88. MODULE_AUTHOR("Ricardo Cerqueira");
  89. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  90. MODULE_LICENSE("GPL");
  91. MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
  92. "{{Conexant,23882},"
  93. "{{Conexant,23883}");
  94. static unsigned int debug;
  95. module_param(debug,int,0644);
  96. MODULE_PARM_DESC(debug,"enable debug messages");
  97. /****************************************************************************
  98. Module specific funtions
  99. ****************************************************************************/
  100. /*
  101. * BOARD Specific: Sets audio DMA
  102. */
  103. static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
  104. {
  105. struct cx88_audio_buffer *buf = chip->buf;
  106. struct cx88_core *core=chip->core;
  107. const struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
  108. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  109. cx_clear(MO_AUD_DMACNTRL, 0x11);
  110. /* setup fifo + format - out channel */
  111. cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
  112. /* sets bpl size */
  113. cx_write(MO_AUDD_LNGTH, buf->bpl);
  114. /* reset counter */
  115. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  116. atomic_set(&chip->count, 0);
  117. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
  118. "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
  119. chip->num_periods, buf->bpl * chip->num_periods);
  120. /* Enables corresponding bits at AUD_INT_STAT */
  121. cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  122. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  123. /* Clean any pending interrupt bits already set */
  124. cx_write(MO_AUD_INTSTAT, ~0);
  125. /* enable audio irqs */
  126. cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
  127. /* start dma */
  128. cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  129. cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
  130. if (debug)
  131. cx88_sram_channel_dump(chip->core, audio_ch);
  132. return 0;
  133. }
  134. /*
  135. * BOARD Specific: Resets audio DMA
  136. */
  137. static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
  138. {
  139. struct cx88_core *core=chip->core;
  140. dprintk(1, "Stopping audio DMA\n");
  141. /* stop dma */
  142. cx_clear(MO_AUD_DMACNTRL, 0x11);
  143. /* disable irqs */
  144. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  145. cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  146. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  147. if (debug)
  148. cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
  149. return 0;
  150. }
  151. #define MAX_IRQ_LOOP 50
  152. /*
  153. * BOARD Specific: IRQ dma bits
  154. */
  155. static const char *cx88_aud_irqs[32] = {
  156. "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
  157. NULL, /* reserved */
  158. "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
  159. NULL, /* reserved */
  160. "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
  161. NULL, /* reserved */
  162. "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
  163. NULL, /* reserved */
  164. "opc_err", "par_err", "rip_err", /* 16-18 */
  165. "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
  166. };
  167. /*
  168. * BOARD Specific: Threats IRQ audio specific calls
  169. */
  170. static void cx8801_aud_irq(snd_cx88_card_t *chip)
  171. {
  172. struct cx88_core *core = chip->core;
  173. u32 status, mask;
  174. status = cx_read(MO_AUD_INTSTAT);
  175. mask = cx_read(MO_AUD_INTMSK);
  176. if (0 == (status & mask))
  177. return;
  178. cx_write(MO_AUD_INTSTAT, status);
  179. if (debug > 1 || (status & mask & ~0xff))
  180. cx88_print_irqbits(core->name, "irq aud",
  181. cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
  182. status, mask);
  183. /* risc op code error */
  184. if (status & AUD_INT_OPC_ERR) {
  185. printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
  186. cx_clear(MO_AUD_DMACNTRL, 0x11);
  187. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
  188. }
  189. if (status & AUD_INT_DN_SYNC) {
  190. dprintk(1, "Downstream sync error\n");
  191. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  192. return;
  193. }
  194. /* risc1 downstream */
  195. if (status & AUD_INT_DN_RISCI1) {
  196. atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
  197. snd_pcm_period_elapsed(chip->substream);
  198. }
  199. /* FIXME: Any other status should deserve a special handling? */
  200. }
  201. /*
  202. * BOARD Specific: Handles IRQ calls
  203. */
  204. static irqreturn_t cx8801_irq(int irq, void *dev_id)
  205. {
  206. snd_cx88_card_t *chip = dev_id;
  207. struct cx88_core *core = chip->core;
  208. u32 status;
  209. int loop, handled = 0;
  210. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  211. status = cx_read(MO_PCI_INTSTAT) &
  212. (core->pci_irqmask | PCI_INT_AUDINT);
  213. if (0 == status)
  214. goto out;
  215. dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
  216. loop, MAX_IRQ_LOOP, status);
  217. handled = 1;
  218. cx_write(MO_PCI_INTSTAT, status);
  219. if (status & core->pci_irqmask)
  220. cx88_core_irq(core, status);
  221. if (status & PCI_INT_AUDINT)
  222. cx8801_aud_irq(chip);
  223. }
  224. if (MAX_IRQ_LOOP == loop) {
  225. printk(KERN_ERR
  226. "%s/1: IRQ loop detected, disabling interrupts\n",
  227. core->name);
  228. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  229. }
  230. out:
  231. return IRQ_RETVAL(handled);
  232. }
  233. static int dsp_buffer_free(snd_cx88_card_t *chip)
  234. {
  235. BUG_ON(!chip->dma_size);
  236. dprintk(2,"Freeing buffer\n");
  237. videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
  238. videobuf_dma_free(chip->dma_risc);
  239. btcx_riscmem_free(chip->pci,&chip->buf->risc);
  240. kfree(chip->buf);
  241. chip->dma_risc = NULL;
  242. chip->dma_size = 0;
  243. return 0;
  244. }
  245. /****************************************************************************
  246. ALSA PCM Interface
  247. ****************************************************************************/
  248. /*
  249. * Digital hardware definition
  250. */
  251. #define DEFAULT_FIFO_SIZE 4096
  252. static const struct snd_pcm_hardware snd_cx88_digital_hw = {
  253. .info = SNDRV_PCM_INFO_MMAP |
  254. SNDRV_PCM_INFO_INTERLEAVED |
  255. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  256. SNDRV_PCM_INFO_MMAP_VALID,
  257. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  258. .rates = SNDRV_PCM_RATE_48000,
  259. .rate_min = 48000,
  260. .rate_max = 48000,
  261. .channels_min = 2,
  262. .channels_max = 2,
  263. /* Analog audio output will be full of clicks and pops if there
  264. are not exactly four lines in the SRAM FIFO buffer. */
  265. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  266. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  267. .periods_min = 1,
  268. .periods_max = 1024,
  269. .buffer_bytes_max = (1024*1024),
  270. };
  271. /*
  272. * audio pcm capture open callback
  273. */
  274. static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
  275. {
  276. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  277. struct snd_pcm_runtime *runtime = substream->runtime;
  278. int err;
  279. if (!chip) {
  280. printk(KERN_ERR "BUG: cx88 can't find device struct."
  281. " Can't proceed with open\n");
  282. return -ENODEV;
  283. }
  284. err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
  285. if (err < 0)
  286. goto _error;
  287. chip->substream = substream;
  288. runtime->hw = snd_cx88_digital_hw;
  289. if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
  290. unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
  291. bpl &= ~7; /* must be multiple of 8 */
  292. runtime->hw.period_bytes_min = bpl;
  293. runtime->hw.period_bytes_max = bpl;
  294. }
  295. return 0;
  296. _error:
  297. dprintk(1,"Error opening PCM!\n");
  298. return err;
  299. }
  300. /*
  301. * audio close callback
  302. */
  303. static int snd_cx88_close(struct snd_pcm_substream *substream)
  304. {
  305. return 0;
  306. }
  307. /*
  308. * hw_params callback
  309. */
  310. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  311. struct snd_pcm_hw_params * hw_params)
  312. {
  313. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  314. struct videobuf_dmabuf *dma;
  315. struct cx88_audio_buffer *buf;
  316. int ret;
  317. if (substream->runtime->dma_area) {
  318. dsp_buffer_free(chip);
  319. substream->runtime->dma_area = NULL;
  320. }
  321. chip->period_size = params_period_bytes(hw_params);
  322. chip->num_periods = params_periods(hw_params);
  323. chip->dma_size = chip->period_size * params_periods(hw_params);
  324. BUG_ON(!chip->dma_size);
  325. BUG_ON(chip->num_periods & (chip->num_periods-1));
  326. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  327. if (NULL == buf)
  328. return -ENOMEM;
  329. buf->bpl = chip->period_size;
  330. dma = &buf->dma;
  331. videobuf_dma_init(dma);
  332. ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
  333. (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
  334. if (ret < 0)
  335. goto error;
  336. ret = videobuf_dma_map(&chip->pci->dev, dma);
  337. if (ret < 0)
  338. goto error;
  339. ret = cx88_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
  340. chip->period_size, chip->num_periods, 1);
  341. if (ret < 0)
  342. goto error;
  343. /* Loop back to start of program */
  344. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  345. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  346. chip->buf = buf;
  347. chip->dma_risc = dma;
  348. substream->runtime->dma_area = chip->dma_risc->vaddr;
  349. substream->runtime->dma_bytes = chip->dma_size;
  350. substream->runtime->dma_addr = 0;
  351. return 0;
  352. error:
  353. kfree(buf);
  354. return ret;
  355. }
  356. /*
  357. * hw free callback
  358. */
  359. static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
  360. {
  361. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  362. if (substream->runtime->dma_area) {
  363. dsp_buffer_free(chip);
  364. substream->runtime->dma_area = NULL;
  365. }
  366. return 0;
  367. }
  368. /*
  369. * prepare callback
  370. */
  371. static int snd_cx88_prepare(struct snd_pcm_substream *substream)
  372. {
  373. return 0;
  374. }
  375. /*
  376. * trigger callback
  377. */
  378. static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
  379. {
  380. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  381. int err;
  382. /* Local interrupts are already disabled by ALSA */
  383. spin_lock(&chip->reg_lock);
  384. switch (cmd) {
  385. case SNDRV_PCM_TRIGGER_START:
  386. err=_cx88_start_audio_dma(chip);
  387. break;
  388. case SNDRV_PCM_TRIGGER_STOP:
  389. err=_cx88_stop_audio_dma(chip);
  390. break;
  391. default:
  392. err=-EINVAL;
  393. break;
  394. }
  395. spin_unlock(&chip->reg_lock);
  396. return err;
  397. }
  398. /*
  399. * pointer callback
  400. */
  401. static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
  402. {
  403. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  404. struct snd_pcm_runtime *runtime = substream->runtime;
  405. u16 count;
  406. count = atomic_read(&chip->count);
  407. // dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
  408. // count, new, count & (runtime->periods-1),
  409. // runtime->period_size * (count & (runtime->periods-1)));
  410. return runtime->period_size * (count & (runtime->periods-1));
  411. }
  412. /*
  413. * page callback (needed for mmap)
  414. */
  415. static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
  416. unsigned long offset)
  417. {
  418. void *pageptr = substream->runtime->dma_area + offset;
  419. return vmalloc_to_page(pageptr);
  420. }
  421. /*
  422. * operators
  423. */
  424. static struct snd_pcm_ops snd_cx88_pcm_ops = {
  425. .open = snd_cx88_pcm_open,
  426. .close = snd_cx88_close,
  427. .ioctl = snd_pcm_lib_ioctl,
  428. .hw_params = snd_cx88_hw_params,
  429. .hw_free = snd_cx88_hw_free,
  430. .prepare = snd_cx88_prepare,
  431. .trigger = snd_cx88_card_trigger,
  432. .pointer = snd_cx88_pointer,
  433. .page = snd_cx88_page,
  434. };
  435. /*
  436. * create a PCM device
  437. */
  438. static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, const char *name)
  439. {
  440. int err;
  441. struct snd_pcm *pcm;
  442. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  443. if (err < 0)
  444. return err;
  445. pcm->private_data = chip;
  446. strcpy(pcm->name, name);
  447. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
  448. return 0;
  449. }
  450. /****************************************************************************
  451. CONTROL INTERFACE
  452. ****************************************************************************/
  453. static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
  454. struct snd_ctl_elem_info *info)
  455. {
  456. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  457. info->count = 2;
  458. info->value.integer.min = 0;
  459. info->value.integer.max = 0x3f;
  460. return 0;
  461. }
  462. static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
  463. struct snd_ctl_elem_value *value)
  464. {
  465. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  466. struct cx88_core *core=chip->core;
  467. int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
  468. bal = cx_read(AUD_BAL_CTL);
  469. value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
  470. vol -= (bal & 0x3f);
  471. value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
  472. return 0;
  473. }
  474. static void snd_cx88_wm8775_volume_put(struct snd_kcontrol *kcontrol,
  475. struct snd_ctl_elem_value *value)
  476. {
  477. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  478. struct cx88_core *core = chip->core;
  479. struct v4l2_control client_ctl;
  480. int left = value->value.integer.value[0];
  481. int right = value->value.integer.value[1];
  482. int v, b;
  483. memset(&client_ctl, 0, sizeof(client_ctl));
  484. /* Pass volume & balance onto any WM8775 */
  485. if (left >= right) {
  486. v = left << 10;
  487. b = left ? (0x8000 * right) / left : 0x8000;
  488. } else {
  489. v = right << 10;
  490. b = right ? 0xffff - (0x8000 * left) / right : 0x8000;
  491. }
  492. client_ctl.value = v;
  493. client_ctl.id = V4L2_CID_AUDIO_VOLUME;
  494. call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
  495. client_ctl.value = b;
  496. client_ctl.id = V4L2_CID_AUDIO_BALANCE;
  497. call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
  498. }
  499. /* OK - TODO: test it */
  500. static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
  501. struct snd_ctl_elem_value *value)
  502. {
  503. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  504. struct cx88_core *core=chip->core;
  505. int left, right, v, b;
  506. int changed = 0;
  507. u32 old;
  508. if (core->board.audio_chip == V4L2_IDENT_WM8775)
  509. snd_cx88_wm8775_volume_put(kcontrol, value);
  510. left = value->value.integer.value[0] & 0x3f;
  511. right = value->value.integer.value[1] & 0x3f;
  512. b = right - left;
  513. if (b < 0) {
  514. v = 0x3f - left;
  515. b = (-b) | 0x40;
  516. } else {
  517. v = 0x3f - right;
  518. }
  519. /* Do we really know this will always be called with IRQs on? */
  520. spin_lock_irq(&chip->reg_lock);
  521. old = cx_read(AUD_VOL_CTL);
  522. if (v != (old & 0x3f)) {
  523. cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v);
  524. changed = 1;
  525. }
  526. if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) {
  527. cx_write(AUD_BAL_CTL, b);
  528. changed = 1;
  529. }
  530. spin_unlock_irq(&chip->reg_lock);
  531. return changed;
  532. }
  533. static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
  534. static const struct snd_kcontrol_new snd_cx88_volume = {
  535. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  536. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  537. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  538. .name = "Analog-TV Volume",
  539. .info = snd_cx88_volume_info,
  540. .get = snd_cx88_volume_get,
  541. .put = snd_cx88_volume_put,
  542. .tlv.p = snd_cx88_db_scale,
  543. };
  544. static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
  545. struct snd_ctl_elem_value *value)
  546. {
  547. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  548. struct cx88_core *core = chip->core;
  549. u32 bit = kcontrol->private_value;
  550. value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
  551. return 0;
  552. }
  553. static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
  554. struct snd_ctl_elem_value *value)
  555. {
  556. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  557. struct cx88_core *core = chip->core;
  558. u32 bit = kcontrol->private_value;
  559. int ret = 0;
  560. u32 vol;
  561. spin_lock_irq(&chip->reg_lock);
  562. vol = cx_read(AUD_VOL_CTL);
  563. if (value->value.integer.value[0] != !(vol & bit)) {
  564. vol ^= bit;
  565. cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol);
  566. /* Pass mute onto any WM8775 */
  567. if ((core->board.audio_chip == V4L2_IDENT_WM8775) &&
  568. ((1<<6) == bit)) {
  569. struct v4l2_control client_ctl;
  570. memset(&client_ctl, 0, sizeof(client_ctl));
  571. client_ctl.value = 0 != (vol & bit);
  572. client_ctl.id = V4L2_CID_AUDIO_MUTE;
  573. call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
  574. }
  575. ret = 1;
  576. }
  577. spin_unlock_irq(&chip->reg_lock);
  578. return ret;
  579. }
  580. static const struct snd_kcontrol_new snd_cx88_dac_switch = {
  581. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  582. .name = "Audio-Out Switch",
  583. .info = snd_ctl_boolean_mono_info,
  584. .get = snd_cx88_switch_get,
  585. .put = snd_cx88_switch_put,
  586. .private_value = (1<<8),
  587. };
  588. static const struct snd_kcontrol_new snd_cx88_source_switch = {
  589. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  590. .name = "Analog-TV Switch",
  591. .info = snd_ctl_boolean_mono_info,
  592. .get = snd_cx88_switch_get,
  593. .put = snd_cx88_switch_put,
  594. .private_value = (1<<6),
  595. };
  596. static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol,
  597. struct snd_ctl_elem_value *value)
  598. {
  599. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  600. struct cx88_core *core = chip->core;
  601. struct v4l2_control client_ctl;
  602. memset(&client_ctl, 0, sizeof(client_ctl));
  603. client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
  604. call_hw(core, WM8775_GID, core, g_ctrl, &client_ctl);
  605. value->value.integer.value[0] = client_ctl.value ? 1 : 0;
  606. return 0;
  607. }
  608. static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
  609. struct snd_ctl_elem_value *value)
  610. {
  611. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  612. struct cx88_core *core = chip->core;
  613. struct v4l2_control client_ctl;
  614. memset(&client_ctl, 0, sizeof(client_ctl));
  615. client_ctl.value = 0 != value->value.integer.value[0];
  616. client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
  617. call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
  618. return 0;
  619. }
  620. static struct snd_kcontrol_new snd_cx88_alc_switch = {
  621. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  622. .name = "Line-In ALC Switch",
  623. .info = snd_ctl_boolean_mono_info,
  624. .get = snd_cx88_alc_get,
  625. .put = snd_cx88_alc_put,
  626. };
  627. /****************************************************************************
  628. Basic Flow for Sound Devices
  629. ****************************************************************************/
  630. /*
  631. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  632. * Only boards with eeprom and byte 1 at eeprom=1 have it
  633. */
  634. static const struct pci_device_id const cx88_audio_pci_tbl[] __devinitdata = {
  635. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  636. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  637. {0, }
  638. };
  639. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  640. /*
  641. * Chip-specific destructor
  642. */
  643. static int snd_cx88_free(snd_cx88_card_t *chip)
  644. {
  645. if (chip->irq >= 0)
  646. free_irq(chip->irq, chip);
  647. cx88_core_put(chip->core,chip->pci);
  648. pci_disable_device(chip->pci);
  649. return 0;
  650. }
  651. /*
  652. * Component Destructor
  653. */
  654. static void snd_cx88_dev_free(struct snd_card * card)
  655. {
  656. snd_cx88_card_t *chip = card->private_data;
  657. snd_cx88_free(chip);
  658. }
  659. /*
  660. * Alsa Constructor - Component probe
  661. */
  662. static int devno;
  663. static int __devinit snd_cx88_create(struct snd_card *card,
  664. struct pci_dev *pci,
  665. snd_cx88_card_t **rchip,
  666. struct cx88_core **core_ptr)
  667. {
  668. snd_cx88_card_t *chip;
  669. struct cx88_core *core;
  670. int err;
  671. unsigned char pci_lat;
  672. *rchip = NULL;
  673. err = pci_enable_device(pci);
  674. if (err < 0)
  675. return err;
  676. pci_set_master(pci);
  677. chip = card->private_data;
  678. core = cx88_core_get(pci);
  679. if (NULL == core) {
  680. err = -EINVAL;
  681. return err;
  682. }
  683. if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
  684. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  685. err = -EIO;
  686. cx88_core_put(core, pci);
  687. return err;
  688. }
  689. /* pci init */
  690. chip->card = card;
  691. chip->pci = pci;
  692. chip->irq = -1;
  693. spin_lock_init(&chip->reg_lock);
  694. chip->core = core;
  695. /* get irq */
  696. err = request_irq(chip->pci->irq, cx8801_irq,
  697. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  698. if (err < 0) {
  699. dprintk(0, "%s: can't get IRQ %d\n",
  700. chip->core->name, chip->pci->irq);
  701. return err;
  702. }
  703. /* print pci info */
  704. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  705. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  706. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  707. pci_name(pci), pci->revision, pci->irq,
  708. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  709. chip->irq = pci->irq;
  710. synchronize_irq(chip->irq);
  711. snd_card_set_dev(card, &pci->dev);
  712. *rchip = chip;
  713. *core_ptr = core;
  714. return 0;
  715. }
  716. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  717. const struct pci_device_id *pci_id)
  718. {
  719. struct snd_card *card;
  720. snd_cx88_card_t *chip;
  721. struct cx88_core *core = NULL;
  722. int err;
  723. if (devno >= SNDRV_CARDS)
  724. return (-ENODEV);
  725. if (!enable[devno]) {
  726. ++devno;
  727. return (-ENOENT);
  728. }
  729. err = snd_card_create(index[devno], id[devno], THIS_MODULE,
  730. sizeof(snd_cx88_card_t), &card);
  731. if (err < 0)
  732. return err;
  733. card->private_free = snd_cx88_dev_free;
  734. err = snd_cx88_create(card, pci, &chip, &core);
  735. if (err < 0)
  736. goto error;
  737. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  738. if (err < 0)
  739. goto error;
  740. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
  741. if (err < 0)
  742. goto error;
  743. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
  744. if (err < 0)
  745. goto error;
  746. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
  747. if (err < 0)
  748. goto error;
  749. /* If there's a wm8775 then add a Line-In ALC switch */
  750. if (core->board.audio_chip == V4L2_IDENT_WM8775)
  751. snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip));
  752. strcpy (card->driver, "CX88x");
  753. sprintf(card->shortname, "Conexant CX%x", pci->device);
  754. sprintf(card->longname, "%s at %#llx",
  755. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  756. strcpy (card->mixername, "CX88");
  757. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  758. card->driver,devno);
  759. err = snd_card_register(card);
  760. if (err < 0)
  761. goto error;
  762. pci_set_drvdata(pci,card);
  763. devno++;
  764. return 0;
  765. error:
  766. snd_card_free(card);
  767. return err;
  768. }
  769. /*
  770. * ALSA destructor
  771. */
  772. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  773. {
  774. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  775. snd_card_free((void *)card);
  776. pci_set_drvdata(pci, NULL);
  777. devno--;
  778. }
  779. /*
  780. * PCI driver definition
  781. */
  782. static struct pci_driver cx88_audio_pci_driver = {
  783. .name = "cx88_audio",
  784. .id_table = cx88_audio_pci_tbl,
  785. .probe = cx88_audio_initdev,
  786. .remove = __devexit_p(cx88_audio_finidev),
  787. };
  788. /****************************************************************************
  789. LINUX MODULE INIT
  790. ****************************************************************************/
  791. /*
  792. * module init
  793. */
  794. static int __init cx88_audio_init(void)
  795. {
  796. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  797. (CX88_VERSION_CODE >> 16) & 0xff,
  798. (CX88_VERSION_CODE >> 8) & 0xff,
  799. CX88_VERSION_CODE & 0xff);
  800. #ifdef SNAPSHOT
  801. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  802. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  803. #endif
  804. return pci_register_driver(&cx88_audio_pci_driver);
  805. }
  806. /*
  807. * module remove
  808. */
  809. static void __exit cx88_audio_fini(void)
  810. {
  811. pci_unregister_driver(&cx88_audio_pci_driver);
  812. }
  813. module_init(cx88_audio_init);
  814. module_exit(cx88_audio_fini);
  815. /* ----------------------------------------------------------- */
  816. /*
  817. * Local variables:
  818. * c-basic-offset: 8
  819. * End:
  820. */