cx88-alsa.c 22 KB

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