cx88-alsa.c 22 KB

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