cx88-alsa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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_sg_dma_unmap(&chip->pci->dev, 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. if (!chip) {
  273. printk(KERN_ERR "BUG: cx88 can't find device struct."
  274. " Can't proceed with open\n");
  275. return -ENODEV;
  276. }
  277. err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
  278. if (err < 0)
  279. goto _error;
  280. chip->substream = substream;
  281. runtime->hw = snd_cx88_digital_hw;
  282. if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
  283. unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
  284. bpl &= ~7; /* must be multiple of 8 */
  285. runtime->hw.period_bytes_min = bpl;
  286. runtime->hw.period_bytes_max = bpl;
  287. }
  288. return 0;
  289. _error:
  290. dprintk(1,"Error opening PCM!\n");
  291. return err;
  292. }
  293. /*
  294. * audio close callback
  295. */
  296. static int snd_cx88_close(struct snd_pcm_substream *substream)
  297. {
  298. return 0;
  299. }
  300. /*
  301. * hw_params callback
  302. */
  303. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  304. struct snd_pcm_hw_params * hw_params)
  305. {
  306. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  307. struct videobuf_dmabuf *dma;
  308. struct cx88_buffer *buf;
  309. int ret;
  310. if (substream->runtime->dma_area) {
  311. dsp_buffer_free(chip);
  312. substream->runtime->dma_area = NULL;
  313. }
  314. chip->period_size = params_period_bytes(hw_params);
  315. chip->num_periods = params_periods(hw_params);
  316. chip->dma_size = chip->period_size * params_periods(hw_params);
  317. BUG_ON(!chip->dma_size);
  318. BUG_ON(chip->num_periods & (chip->num_periods-1));
  319. buf = videobuf_sg_alloc(sizeof(*buf));
  320. if (NULL == buf)
  321. return -ENOMEM;
  322. buf->vb.memory = V4L2_MEMORY_MMAP;
  323. buf->vb.field = V4L2_FIELD_NONE;
  324. buf->vb.width = chip->period_size;
  325. buf->bpl = chip->period_size;
  326. buf->vb.height = chip->num_periods;
  327. buf->vb.size = chip->dma_size;
  328. dma = videobuf_to_dma(&buf->vb);
  329. videobuf_dma_init(dma);
  330. ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
  331. (PAGE_ALIGN(buf->vb.size) >> PAGE_SHIFT));
  332. if (ret < 0)
  333. goto error;
  334. ret = videobuf_sg_dma_map(&chip->pci->dev, dma);
  335. if (ret < 0)
  336. goto error;
  337. ret = cx88_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
  338. buf->vb.width, buf->vb.height, 1);
  339. if (ret < 0)
  340. goto error;
  341. /* Loop back to start of program */
  342. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  343. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  344. buf->vb.state = VIDEOBUF_PREPARED;
  345. chip->buf = buf;
  346. chip->dma_risc = dma;
  347. substream->runtime->dma_area = chip->dma_risc->vmalloc;
  348. substream->runtime->dma_bytes = chip->dma_size;
  349. substream->runtime->dma_addr = 0;
  350. return 0;
  351. error:
  352. kfree(buf);
  353. return ret;
  354. }
  355. /*
  356. * hw free callback
  357. */
  358. static int snd_cx88_hw_free(struct snd_pcm_substream * substream)
  359. {
  360. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  361. if (substream->runtime->dma_area) {
  362. dsp_buffer_free(chip);
  363. substream->runtime->dma_area = NULL;
  364. }
  365. return 0;
  366. }
  367. /*
  368. * prepare callback
  369. */
  370. static int snd_cx88_prepare(struct snd_pcm_substream *substream)
  371. {
  372. return 0;
  373. }
  374. /*
  375. * trigger callback
  376. */
  377. static int snd_cx88_card_trigger(struct snd_pcm_substream *substream, int cmd)
  378. {
  379. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  380. int err;
  381. /* Local interrupts are already disabled by ALSA */
  382. spin_lock(&chip->reg_lock);
  383. switch (cmd) {
  384. case SNDRV_PCM_TRIGGER_START:
  385. err=_cx88_start_audio_dma(chip);
  386. break;
  387. case SNDRV_PCM_TRIGGER_STOP:
  388. err=_cx88_stop_audio_dma(chip);
  389. break;
  390. default:
  391. err=-EINVAL;
  392. break;
  393. }
  394. spin_unlock(&chip->reg_lock);
  395. return err;
  396. }
  397. /*
  398. * pointer callback
  399. */
  400. static snd_pcm_uframes_t snd_cx88_pointer(struct snd_pcm_substream *substream)
  401. {
  402. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  403. struct snd_pcm_runtime *runtime = substream->runtime;
  404. u16 count;
  405. count = atomic_read(&chip->count);
  406. // dprintk(2, "%s - count %d (+%u), period %d, frame %lu\n", __func__,
  407. // count, new, count & (runtime->periods-1),
  408. // runtime->period_size * (count & (runtime->periods-1)));
  409. return runtime->period_size * (count & (runtime->periods-1));
  410. }
  411. /*
  412. * page callback (needed for mmap)
  413. */
  414. static struct page *snd_cx88_page(struct snd_pcm_substream *substream,
  415. unsigned long offset)
  416. {
  417. void *pageptr = substream->runtime->dma_area + offset;
  418. return vmalloc_to_page(pageptr);
  419. }
  420. /*
  421. * operators
  422. */
  423. static struct snd_pcm_ops snd_cx88_pcm_ops = {
  424. .open = snd_cx88_pcm_open,
  425. .close = snd_cx88_close,
  426. .ioctl = snd_pcm_lib_ioctl,
  427. .hw_params = snd_cx88_hw_params,
  428. .hw_free = snd_cx88_hw_free,
  429. .prepare = snd_cx88_prepare,
  430. .trigger = snd_cx88_card_trigger,
  431. .pointer = snd_cx88_pointer,
  432. .page = snd_cx88_page,
  433. };
  434. /*
  435. * create a PCM device
  436. */
  437. static int __devinit snd_cx88_pcm(snd_cx88_card_t *chip, int device, char *name)
  438. {
  439. int err;
  440. struct snd_pcm *pcm;
  441. err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
  442. if (err < 0)
  443. return err;
  444. pcm->private_data = chip;
  445. strcpy(pcm->name, name);
  446. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx88_pcm_ops);
  447. return 0;
  448. }
  449. /****************************************************************************
  450. CONTROL INTERFACE
  451. ****************************************************************************/
  452. static int snd_cx88_volume_info(struct snd_kcontrol *kcontrol,
  453. struct snd_ctl_elem_info *info)
  454. {
  455. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  456. info->count = 2;
  457. info->value.integer.min = 0;
  458. info->value.integer.max = 0x3f;
  459. return 0;
  460. }
  461. static int snd_cx88_volume_get(struct snd_kcontrol *kcontrol,
  462. struct snd_ctl_elem_value *value)
  463. {
  464. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  465. struct cx88_core *core=chip->core;
  466. int vol = 0x3f - (cx_read(AUD_VOL_CTL) & 0x3f),
  467. bal = cx_read(AUD_BAL_CTL);
  468. value->value.integer.value[(bal & 0x40) ? 0 : 1] = vol;
  469. vol -= (bal & 0x3f);
  470. value->value.integer.value[(bal & 0x40) ? 1 : 0] = vol < 0 ? 0 : vol;
  471. return 0;
  472. }
  473. /* OK - TODO: test it */
  474. static int snd_cx88_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. int v, b;
  480. int changed = 0;
  481. u32 old;
  482. b = value->value.integer.value[1] - value->value.integer.value[0];
  483. if (b < 0) {
  484. v = 0x3f - value->value.integer.value[0];
  485. b = (-b) | 0x40;
  486. } else {
  487. v = 0x3f - value->value.integer.value[1];
  488. }
  489. /* Do we really know this will always be called with IRQs on? */
  490. spin_lock_irq(&chip->reg_lock);
  491. old = cx_read(AUD_VOL_CTL);
  492. if (v != (old & 0x3f)) {
  493. cx_write(AUD_VOL_CTL, (old & ~0x3f) | v);
  494. changed = 1;
  495. }
  496. if (cx_read(AUD_BAL_CTL) != b) {
  497. cx_write(AUD_BAL_CTL, b);
  498. changed = 1;
  499. }
  500. spin_unlock_irq(&chip->reg_lock);
  501. return changed;
  502. }
  503. static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
  504. static struct snd_kcontrol_new snd_cx88_volume = {
  505. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  506. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  507. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  508. .name = "Playback Volume",
  509. .info = snd_cx88_volume_info,
  510. .get = snd_cx88_volume_get,
  511. .put = snd_cx88_volume_put,
  512. .tlv.p = snd_cx88_db_scale,
  513. };
  514. static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
  515. struct snd_ctl_elem_value *value)
  516. {
  517. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  518. struct cx88_core *core = chip->core;
  519. u32 bit = kcontrol->private_value;
  520. value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
  521. return 0;
  522. }
  523. static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
  524. struct snd_ctl_elem_value *value)
  525. {
  526. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  527. struct cx88_core *core = chip->core;
  528. u32 bit = kcontrol->private_value;
  529. int ret = 0;
  530. u32 vol;
  531. spin_lock_irq(&chip->reg_lock);
  532. vol = cx_read(AUD_VOL_CTL);
  533. if (value->value.integer.value[0] != !(vol & bit)) {
  534. vol ^= bit;
  535. cx_write(AUD_VOL_CTL, vol);
  536. ret = 1;
  537. }
  538. spin_unlock_irq(&chip->reg_lock);
  539. return ret;
  540. }
  541. static struct snd_kcontrol_new snd_cx88_dac_switch = {
  542. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  543. .name = "Playback Switch",
  544. .info = snd_ctl_boolean_mono_info,
  545. .get = snd_cx88_switch_get,
  546. .put = snd_cx88_switch_put,
  547. .private_value = (1<<8),
  548. };
  549. static struct snd_kcontrol_new snd_cx88_source_switch = {
  550. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  551. .name = "Capture Switch",
  552. .info = snd_ctl_boolean_mono_info,
  553. .get = snd_cx88_switch_get,
  554. .put = snd_cx88_switch_put,
  555. .private_value = (1<<6),
  556. };
  557. /****************************************************************************
  558. Basic Flow for Sound Devices
  559. ****************************************************************************/
  560. /*
  561. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  562. * Only boards with eeprom and byte 1 at eeprom=1 have it
  563. */
  564. static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
  565. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  566. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  567. {0, }
  568. };
  569. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  570. /*
  571. * Chip-specific destructor
  572. */
  573. static int snd_cx88_free(snd_cx88_card_t *chip)
  574. {
  575. if (chip->irq >= 0)
  576. free_irq(chip->irq, chip);
  577. cx88_core_put(chip->core,chip->pci);
  578. pci_disable_device(chip->pci);
  579. return 0;
  580. }
  581. /*
  582. * Component Destructor
  583. */
  584. static void snd_cx88_dev_free(struct snd_card * card)
  585. {
  586. snd_cx88_card_t *chip = card->private_data;
  587. snd_cx88_free(chip);
  588. }
  589. /*
  590. * Alsa Constructor - Component probe
  591. */
  592. static int devno;
  593. static int __devinit snd_cx88_create(struct snd_card *card,
  594. struct pci_dev *pci,
  595. snd_cx88_card_t **rchip)
  596. {
  597. snd_cx88_card_t *chip;
  598. struct cx88_core *core;
  599. int err;
  600. unsigned char pci_lat;
  601. *rchip = NULL;
  602. err = pci_enable_device(pci);
  603. if (err < 0)
  604. return err;
  605. pci_set_master(pci);
  606. chip = (snd_cx88_card_t *) card->private_data;
  607. core = cx88_core_get(pci);
  608. if (NULL == core) {
  609. err = -EINVAL;
  610. return err;
  611. }
  612. if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
  613. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  614. err = -EIO;
  615. cx88_core_put(core,pci);
  616. return err;
  617. }
  618. /* pci init */
  619. chip->card = card;
  620. chip->pci = pci;
  621. chip->irq = -1;
  622. spin_lock_init(&chip->reg_lock);
  623. chip->core = core;
  624. /* get irq */
  625. err = request_irq(chip->pci->irq, cx8801_irq,
  626. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  627. if (err < 0) {
  628. dprintk(0, "%s: can't get IRQ %d\n",
  629. chip->core->name, chip->pci->irq);
  630. return err;
  631. }
  632. /* print pci info */
  633. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  634. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  635. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  636. pci_name(pci), pci->revision, pci->irq,
  637. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  638. chip->irq = pci->irq;
  639. synchronize_irq(chip->irq);
  640. snd_card_set_dev(card, &pci->dev);
  641. *rchip = chip;
  642. return 0;
  643. }
  644. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  645. const struct pci_device_id *pci_id)
  646. {
  647. struct snd_card *card;
  648. snd_cx88_card_t *chip;
  649. int err;
  650. if (devno >= SNDRV_CARDS)
  651. return (-ENODEV);
  652. if (!enable[devno]) {
  653. ++devno;
  654. return (-ENOENT);
  655. }
  656. err = snd_card_create(index[devno], id[devno], THIS_MODULE,
  657. sizeof(snd_cx88_card_t), &card);
  658. if (err < 0)
  659. return err;
  660. card->private_free = snd_cx88_dev_free;
  661. err = snd_cx88_create(card, pci, &chip);
  662. if (err < 0)
  663. goto error;
  664. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  665. if (err < 0)
  666. goto error;
  667. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
  668. if (err < 0)
  669. goto error;
  670. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
  671. if (err < 0)
  672. goto error;
  673. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
  674. if (err < 0)
  675. goto error;
  676. strcpy (card->driver, "CX88x");
  677. sprintf(card->shortname, "Conexant CX%x", pci->device);
  678. sprintf(card->longname, "%s at %#llx",
  679. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  680. strcpy (card->mixername, "CX88");
  681. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  682. card->driver,devno);
  683. err = snd_card_register(card);
  684. if (err < 0)
  685. goto error;
  686. pci_set_drvdata(pci,card);
  687. devno++;
  688. return 0;
  689. error:
  690. snd_card_free(card);
  691. return err;
  692. }
  693. /*
  694. * ALSA destructor
  695. */
  696. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  697. {
  698. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  699. snd_card_free((void *)card);
  700. pci_set_drvdata(pci, NULL);
  701. devno--;
  702. }
  703. /*
  704. * PCI driver definition
  705. */
  706. static struct pci_driver cx88_audio_pci_driver = {
  707. .name = "cx88_audio",
  708. .id_table = cx88_audio_pci_tbl,
  709. .probe = cx88_audio_initdev,
  710. .remove = __devexit_p(cx88_audio_finidev),
  711. };
  712. /****************************************************************************
  713. LINUX MODULE INIT
  714. ****************************************************************************/
  715. /*
  716. * module init
  717. */
  718. static int __init cx88_audio_init(void)
  719. {
  720. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  721. (CX88_VERSION_CODE >> 16) & 0xff,
  722. (CX88_VERSION_CODE >> 8) & 0xff,
  723. CX88_VERSION_CODE & 0xff);
  724. #ifdef SNAPSHOT
  725. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  726. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  727. #endif
  728. return pci_register_driver(&cx88_audio_pci_driver);
  729. }
  730. /*
  731. * module remove
  732. */
  733. static void __exit cx88_audio_fini(void)
  734. {
  735. pci_unregister_driver(&cx88_audio_pci_driver);
  736. }
  737. module_init(cx88_audio_init);
  738. module_exit(cx88_audio_fini);
  739. /* ----------------------------------------------------------- */
  740. /*
  741. * Local variables:
  742. * c-basic-offset: 8
  743. * End:
  744. */