cx88-alsa.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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@suse.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 <asm/delay.h>
  34. #include <sound/driver.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 "cx88.h"
  41. #include "cx88-reg.h"
  42. #define dprintk(level,fmt, arg...) if (debug >= level) \
  43. printk(KERN_INFO "%s/1: " fmt, chip->core->name , ## arg)
  44. #define dprintk_core(level,fmt, arg...) if (debug >= level) \
  45. printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
  46. /****************************************************************************
  47. Data type declarations - Can be moded to a header file later
  48. ****************************************************************************/
  49. /* These can be replaced after done */
  50. #define MIXER_ADDR_LAST MAX_CX88_INPUT
  51. struct cx88_audio_dev {
  52. struct cx88_core *core;
  53. struct cx88_dmaqueue q;
  54. u64 starttime;
  55. /* pci i/o */
  56. struct pci_dev *pci;
  57. /* audio controls */
  58. int irq;
  59. struct snd_card *card;
  60. spinlock_t reg_lock;
  61. atomic_t count;
  62. unsigned int dma_size;
  63. unsigned int period_size;
  64. unsigned int num_periods;
  65. struct videobuf_dmabuf dma_risc;
  66. int mixer_volume[MIXER_ADDR_LAST+1][2];
  67. int capture_source[MIXER_ADDR_LAST+1][2];
  68. struct cx88_buffer *buf;
  69. struct snd_pcm_substream *substream;
  70. };
  71. typedef struct cx88_audio_dev snd_cx88_card_t;
  72. /****************************************************************************
  73. Module global static vars
  74. ****************************************************************************/
  75. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  76. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  77. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  78. module_param_array(enable, bool, NULL, 0444);
  79. MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
  80. module_param_array(index, int, NULL, 0444);
  81. MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
  82. /****************************************************************************
  83. Module macros
  84. ****************************************************************************/
  85. MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
  86. MODULE_AUTHOR("Ricardo Cerqueira");
  87. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  88. MODULE_LICENSE("GPL");
  89. MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
  90. "{{Conexant,23882},"
  91. "{{Conexant,23883}");
  92. static unsigned int debug;
  93. module_param(debug,int,0644);
  94. MODULE_PARM_DESC(debug,"enable debug messages");
  95. /****************************************************************************
  96. Module specific funtions
  97. ****************************************************************************/
  98. /*
  99. * BOARD Specific: Sets audio DMA
  100. */
  101. static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
  102. {
  103. struct cx88_buffer *buf = chip->buf;
  104. struct cx88_core *core=chip->core;
  105. struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
  106. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  107. cx_clear(MO_AUD_DMACNTRL, 0x11);
  108. /* setup fifo + format - out channel */
  109. cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
  110. /* sets bpl size */
  111. cx_write(MO_AUDD_LNGTH, buf->bpl);
  112. /* reset counter */
  113. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  114. atomic_set(&chip->count, 0);
  115. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
  116. "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
  117. chip->num_periods, buf->bpl * chip->num_periods);
  118. /* Enables corresponding bits at AUD_INT_STAT */
  119. cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  120. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  121. /* Clean any pending interrupt bits already set */
  122. cx_write(MO_AUD_INTSTAT, ~0);
  123. /* enable audio irqs */
  124. cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
  125. /* start dma */
  126. cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  127. cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
  128. if (debug)
  129. cx88_sram_channel_dump(chip->core, audio_ch);
  130. return 0;
  131. }
  132. /*
  133. * BOARD Specific: Resets audio DMA
  134. */
  135. static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
  136. {
  137. struct cx88_core *core=chip->core;
  138. dprintk(1, "Stopping audio DMA\n");
  139. /* stop dma */
  140. cx_clear(MO_AUD_DMACNTRL, 0x11);
  141. /* disable irqs */
  142. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  143. cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  144. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  145. if (debug)
  146. cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
  147. return 0;
  148. }
  149. #define MAX_IRQ_LOOP 50
  150. /*
  151. * BOARD Specific: IRQ dma bits
  152. */
  153. static char *cx88_aud_irqs[32] = {
  154. "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
  155. NULL, /* reserved */
  156. "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
  157. NULL, /* reserved */
  158. "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
  159. NULL, /* reserved */
  160. "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
  161. NULL, /* reserved */
  162. "opc_err", "par_err", "rip_err", /* 16-18 */
  163. "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
  164. };
  165. /*
  166. * BOARD Specific: Threats IRQ audio specific calls
  167. */
  168. static void cx8801_aud_irq(snd_cx88_card_t *chip)
  169. {
  170. struct cx88_core *core = chip->core;
  171. u32 status, mask;
  172. status = cx_read(MO_AUD_INTSTAT);
  173. mask = cx_read(MO_AUD_INTMSK);
  174. if (0 == (status & mask))
  175. return;
  176. cx_write(MO_AUD_INTSTAT, status);
  177. if (debug > 1 || (status & mask & ~0xff))
  178. cx88_print_irqbits(core->name, "irq aud",
  179. cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
  180. status, mask);
  181. /* risc op code error */
  182. if (status & AUD_INT_OPC_ERR) {
  183. printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
  184. cx_clear(MO_AUD_DMACNTRL, 0x11);
  185. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
  186. }
  187. if (status & AUD_INT_DN_SYNC) {
  188. dprintk(1, "Downstream sync error\n");
  189. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  190. return;
  191. }
  192. /* risc1 downstream */
  193. if (status & AUD_INT_DN_RISCI1) {
  194. atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
  195. snd_pcm_period_elapsed(chip->substream);
  196. }
  197. /* FIXME: Any other status should deserve a special handling? */
  198. }
  199. /*
  200. * BOARD Specific: Handles IRQ calls
  201. */
  202. static irqreturn_t cx8801_irq(int irq, void *dev_id)
  203. {
  204. snd_cx88_card_t *chip = dev_id;
  205. struct cx88_core *core = chip->core;
  206. u32 status;
  207. int loop, handled = 0;
  208. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  209. status = cx_read(MO_PCI_INTSTAT) &
  210. (core->pci_irqmask | PCI_INT_AUDINT);
  211. if (0 == status)
  212. goto out;
  213. dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
  214. loop, MAX_IRQ_LOOP, status);
  215. handled = 1;
  216. cx_write(MO_PCI_INTSTAT, status);
  217. if (status & core->pci_irqmask)
  218. cx88_core_irq(core, status);
  219. if (status & PCI_INT_AUDINT)
  220. cx8801_aud_irq(chip);
  221. }
  222. if (MAX_IRQ_LOOP == loop) {
  223. printk(KERN_ERR
  224. "%s/1: IRQ loop detected, disabling interrupts\n",
  225. core->name);
  226. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  227. }
  228. out:
  229. return IRQ_RETVAL(handled);
  230. }
  231. static int dsp_buffer_free(snd_cx88_card_t *chip)
  232. {
  233. BUG_ON(!chip->dma_size);
  234. dprintk(2,"Freeing buffer\n");
  235. videobuf_pci_dma_unmap(chip->pci, &chip->dma_risc);
  236. videobuf_dma_free(&chip->dma_risc);
  237. btcx_riscmem_free(chip->pci,&chip->buf->risc);
  238. kfree(chip->buf);
  239. chip->dma_size = 0;
  240. return 0;
  241. }
  242. /****************************************************************************
  243. ALSA PCM Interface
  244. ****************************************************************************/
  245. /*
  246. * Digital hardware definition
  247. */
  248. #define DEFAULT_FIFO_SIZE 4096
  249. static struct snd_pcm_hardware snd_cx88_digital_hw = {
  250. .info = SNDRV_PCM_INFO_MMAP |
  251. SNDRV_PCM_INFO_INTERLEAVED |
  252. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  253. SNDRV_PCM_INFO_MMAP_VALID,
  254. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  255. .rates = SNDRV_PCM_RATE_48000,
  256. .rate_min = 48000,
  257. .rate_max = 48000,
  258. .channels_min = 2,
  259. .channels_max = 2,
  260. /* Analog audio output will be full of clicks and pops if there
  261. are not exactly four lines in the SRAM FIFO buffer. */
  262. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  263. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  264. .periods_min = 1,
  265. .periods_max = 1024,
  266. .buffer_bytes_max = (1024*1024),
  267. };
  268. /*
  269. * audio pcm capture open callback
  270. */
  271. static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
  272. {
  273. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  274. struct snd_pcm_runtime *runtime = substream->runtime;
  275. int err;
  276. err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
  277. if (err < 0)
  278. goto _error;
  279. chip->substream = substream;
  280. runtime->hw = snd_cx88_digital_hw;
  281. if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
  282. unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
  283. bpl &= ~7; /* must be multiple of 8 */
  284. runtime->hw.period_bytes_min = bpl;
  285. runtime->hw.period_bytes_max = bpl;
  286. }
  287. return 0;
  288. _error:
  289. dprintk(1,"Error opening PCM!\n");
  290. return err;
  291. }
  292. /*
  293. * audio close callback
  294. */
  295. static int snd_cx88_close(struct snd_pcm_substream *substream)
  296. {
  297. return 0;
  298. }
  299. /*
  300. * hw_params callback
  301. */
  302. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  303. struct snd_pcm_hw_params * hw_params)
  304. {
  305. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  306. struct cx88_buffer *buf;
  307. int ret;
  308. if (substream->runtime->dma_area) {
  309. dsp_buffer_free(chip);
  310. substream->runtime->dma_area = NULL;
  311. }
  312. chip->period_size = params_period_bytes(hw_params);
  313. chip->num_periods = params_periods(hw_params);
  314. chip->dma_size = chip->period_size * params_periods(hw_params);
  315. BUG_ON(!chip->dma_size);
  316. BUG_ON(chip->num_periods & (chip->num_periods-1));
  317. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  318. if (NULL == buf)
  319. return -ENOMEM;
  320. buf->vb.memory = V4L2_MEMORY_MMAP;
  321. buf->vb.field = V4L2_FIELD_NONE;
  322. buf->vb.width = chip->period_size;
  323. buf->bpl = chip->period_size;
  324. buf->vb.height = chip->num_periods;
  325. buf->vb.size = chip->dma_size;
  326. videobuf_dma_init(&buf->vb.dma);
  327. ret = videobuf_dma_init_kernel(&buf->vb.dma, PCI_DMA_FROMDEVICE,
  328. (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
  329. if (ret < 0)
  330. goto error;
  331. ret = videobuf_pci_dma_map(chip->pci,&buf->vb.dma);
  332. if (ret < 0)
  333. goto error;
  334. ret = cx88_risc_databuffer(chip->pci, &buf->risc, buf->vb.dma.sglist,
  335. buf->vb.width, buf->vb.height, 1);
  336. if (ret < 0)
  337. goto error;
  338. /* Loop back to start of program */
  339. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  340. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  341. buf->vb.state = STATE_PREPARED;
  342. chip->buf = buf;
  343. chip->dma_risc = buf->vb.dma;
  344. substream->runtime->dma_area = chip->dma_risc.vmalloc;
  345. substream->runtime->dma_bytes = chip->dma_size;
  346. substream->runtime->dma_addr = 0;
  347. return 0;
  348. error:
  349. kfree(buf);
  350. return ret;
  351. }
  352. /*
  353. * hw free callback
  354. */
  355. static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
  356. {
  357. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  358. if (substream->runtime->dma_area) {
  359. dsp_buffer_free(chip);
  360. substream->runtime->dma_area = NULL;
  361. }
  362. return 0;
  363. }
  364. /*
  365. * prepare callback
  366. */
  367. static int snd_cx88_prepare(struct snd_pcm_substream *substream)
  368. {
  369. return 0;
  370. }
  371. /*
  372. * trigger callback
  373. */
  374. static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
  375. {
  376. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  377. int err;
  378. /* Local interrupts are already disabled by ALSA */
  379. spin_lock(&chip->reg_lock);
  380. switch (cmd) {
  381. case SNDRV_PCM_TRIGGER_START:
  382. err=_cx88_start_audio_dma(chip);
  383. break;
  384. case SNDRV_PCM_TRIGGER_STOP:
  385. err=_cx88_stop_audio_dma(chip);
  386. break;
  387. default:
  388. err=-EINVAL;
  389. break;
  390. }
  391. spin_unlock(&chip->reg_lock);
  392. return err;
  393. }
  394. /*
  395. * pointer callback
  396. */
  397. static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
  398. {
  399. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  400. struct snd_pcm_runtime *runtime = substream->runtime;
  401. u16 count;
  402. count = atomic_read(&chip->count);
  403. // dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __FUNCTION__,
  404. // count, new, count & (runtime->periods-1),
  405. // runtime->period_size * (count & (runtime->periods-1)));
  406. return runtime->period_size * (count & (runtime->periods-1));
  407. }
  408. /*
  409. * page callback (needed for mmap)
  410. */
  411. static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
  412. unsigned long offset)
  413. {
  414. void *pageptr = substream->runtime->dma_area + offset;
  415. return vmalloc_to_page(pageptr);
  416. }
  417. /*
  418. * operators
  419. */
  420. static struct snd_pcm_ops snd_cx88_pcm_ops = {
  421. .open = snd_cx88_pcm_open,
  422. .close = snd_cx88_close,
  423. .ioctl = snd_pcm_lib_ioctl,
  424. .hw_params = snd_cx88_hw_params,
  425. .hw_free = snd_cx88_hw_free,
  426. .prepare = snd_cx88_prepare,
  427. .trigger = snd_cx88_card_trigger,
  428. .pointer = snd_cx88_pointer,
  429. .page = snd_cx88_page,
  430. };
  431. /*
  432. * create a PCM device
  433. */
  434. static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
  435. {
  436. int err;
  437. struct snd_pcm *pcm;
  438. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  439. if (err < 0)
  440. return err;
  441. pcm->private_data = chip;
  442. strcpy(pcm->name, name);
  443. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
  444. return 0;
  445. }
  446. /****************************************************************************
  447. CONTROL INTERFACE
  448. ****************************************************************************/
  449. static int snd_cx88_capture_volume_info(struct snd_kcontrol *kcontrol,
  450. struct snd_ctl_elem_info *info)
  451. {
  452. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  453. info->count = 2;
  454. info->value.integer.min = 0;
  455. info->value.integer.max = 0x3f;
  456. return 0;
  457. }
  458. /* OK - TODO: test it */
  459. static int snd_cx88_capture_volume_get(struct snd_kcontrol *kcontrol,
  460. struct snd_ctl_elem_value *value)
  461. {
  462. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  463. struct cx88_core *core=chip->core;
  464. int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
  465. bal = cx_read(AUD_BAL_CTL);
  466. value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
  467. vol -= (bal & 0x3f);
  468. value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
  469. return 0;
  470. }
  471. /* OK - TODO: test it */
  472. static int snd_cx88_capture_volume_put(struct snd_kcontrol *kcontrol,
  473. struct snd_ctl_elem_value *value)
  474. {
  475. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  476. struct cx88_core *core=chip->core;
  477. int v, b;
  478. int changed = 0;
  479. u32 old;
  480. b = value->value.integer.value[1] - value->value.integer.value[0];
  481. if (b < 0) {
  482. v = 0x3f - value->value.integer.value[0];
  483. b = (-b) | 0x40;
  484. } else {
  485. v = 0x3f - value->value.integer.value[1];
  486. }
  487. /* Do we really know this will always be called with IRQs on? */
  488. spin_lock_irq(&chip->reg_lock);
  489. old = cx_read(AUD_VOL_CTL);
  490. if (v != (old & 0x3f)) {
  491. cx_write(AUD_VOL_CTL, (old & ~0x3f) | v);
  492. changed = 1;
  493. }
  494. if (cx_read(AUD_BAL_CTL) != b) {
  495. cx_write(AUD_BAL_CTL, b);
  496. changed = 1;
  497. }
  498. spin_unlock_irq(&chip->reg_lock);
  499. return changed;
  500. }
  501. static struct snd_kcontrol_new snd_cx88_capture_volume = {
  502. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  503. .name = "Capture Volume",
  504. .info = snd_cx88_capture_volume_info,
  505. .get = snd_cx88_capture_volume_get,
  506. .put = snd_cx88_capture_volume_put,
  507. };
  508. /****************************************************************************
  509. Basic Flow for Sound Devices
  510. ****************************************************************************/
  511. /*
  512. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  513. * Only boards with eeprom and byte 1 at eeprom=1 have it
  514. */
  515. static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
  516. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  517. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  518. {0, }
  519. };
  520. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  521. /*
  522. * Chip-specific destructor
  523. */
  524. static int snd_cx88_free(snd_cx88_card_t *chip)
  525. {
  526. if (chip->irq >= 0){
  527. synchronize_irq(chip->irq);
  528. free_irq(chip->irq, chip);
  529. }
  530. cx88_core_put(chip->core,chip->pci);
  531. pci_disable_device(chip->pci);
  532. return 0;
  533. }
  534. /*
  535. * Component Destructor
  536. */
  537. static void snd_cx88_dev_free(struct snd_card * card)
  538. {
  539. snd_cx88_card_t *chip = card->private_data;
  540. snd_cx88_free(chip);
  541. }
  542. /*
  543. * Alsa Constructor - Component probe
  544. */
  545. static int devno;
  546. static int __devinit snd_cx88_create(struct snd_card *card,
  547. struct pci_dev *pci,
  548. snd_cx88_card_t **rchip)
  549. {
  550. snd_cx88_card_t *chip;
  551. struct cx88_core *core;
  552. int err;
  553. unsigned char pci_lat;
  554. *rchip = NULL;
  555. err = pci_enable_device(pci);
  556. if (err < 0)
  557. return err;
  558. pci_set_master(pci);
  559. chip = (snd_cx88_card_t *) card->private_data;
  560. core = cx88_core_get(pci);
  561. if (NULL == core) {
  562. err = -EINVAL;
  563. kfree (chip);
  564. return err;
  565. }
  566. if (!pci_dma_supported(pci,DMA_32BIT_MASK)) {
  567. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  568. err = -EIO;
  569. cx88_core_put(core,pci);
  570. return err;
  571. }
  572. /* pci init */
  573. chip->card = card;
  574. chip->pci = pci;
  575. chip->irq = -1;
  576. spin_lock_init(&chip->reg_lock);
  577. chip->core = core;
  578. /* get irq */
  579. err = request_irq(chip->pci->irq, cx8801_irq,
  580. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  581. if (err < 0) {
  582. dprintk(0, "%s: can't get IRQ %d\n",
  583. chip->core->name, chip->pci->irq);
  584. return err;
  585. }
  586. /* print pci info */
  587. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  588. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  589. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  590. pci_name(pci), pci->revision, pci->irq,
  591. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  592. chip->irq = pci->irq;
  593. synchronize_irq(chip->irq);
  594. snd_card_set_dev(card, &pci->dev);
  595. *rchip = chip;
  596. return 0;
  597. }
  598. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  599. const struct pci_device_id *pci_id)
  600. {
  601. struct snd_card *card;
  602. snd_cx88_card_t *chip;
  603. int err;
  604. if (devno >= SNDRV_CARDS)
  605. return (-ENODEV);
  606. if (!enable[devno]) {
  607. ++devno;
  608. return (-ENOENT);
  609. }
  610. card = snd_card_new(index[devno], id[devno], THIS_MODULE, sizeof(snd_cx88_card_t));
  611. if (!card)
  612. return (-ENOMEM);
  613. card->private_free = snd_cx88_dev_free;
  614. err = snd_cx88_create(card, pci, &chip);
  615. if (err < 0)
  616. return (err);
  617. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  618. if (err < 0)
  619. goto error;
  620. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_capture_volume, chip));
  621. if (err < 0)
  622. goto error;
  623. strcpy (card->driver, "CX88x");
  624. sprintf(card->shortname, "Conexant CX%x", pci->device);
  625. sprintf(card->longname, "%s at %#llx",
  626. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  627. strcpy (card->mixername, "CX88");
  628. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  629. card->driver,devno);
  630. err = snd_card_register(card);
  631. if (err < 0)
  632. goto error;
  633. pci_set_drvdata(pci,card);
  634. devno++;
  635. return 0;
  636. error:
  637. snd_card_free(card);
  638. return err;
  639. }
  640. /*
  641. * ALSA destructor
  642. */
  643. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  644. {
  645. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  646. snd_card_free((void *)card);
  647. pci_set_drvdata(pci, NULL);
  648. devno--;
  649. }
  650. /*
  651. * PCI driver definition
  652. */
  653. static struct pci_driver cx88_audio_pci_driver = {
  654. .name = "cx88_audio",
  655. .id_table = cx88_audio_pci_tbl,
  656. .probe = cx88_audio_initdev,
  657. .remove = cx88_audio_finidev,
  658. };
  659. /****************************************************************************
  660. LINUX MODULE INIT
  661. ****************************************************************************/
  662. /*
  663. * module init
  664. */
  665. static int cx88_audio_init(void)
  666. {
  667. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  668. (CX88_VERSION_CODE >> 16) & 0xff,
  669. (CX88_VERSION_CODE >> 8) & 0xff,
  670. CX88_VERSION_CODE & 0xff);
  671. #ifdef SNAPSHOT
  672. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  673. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  674. #endif
  675. return pci_register_driver(&cx88_audio_pci_driver);
  676. }
  677. /*
  678. * module remove
  679. */
  680. static void cx88_audio_fini(void)
  681. {
  682. pci_unregister_driver(&cx88_audio_pci_driver);
  683. }
  684. module_init(cx88_audio_init);
  685. module_exit(cx88_audio_fini);
  686. /* ----------------------------------------------------------- */
  687. /*
  688. * Local variables:
  689. * c-basic-offset: 8
  690. * End:
  691. */