cx88-alsa.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. /*
  2. *
  3. * Support for audio capture
  4. * PCI function #1 of the cx2388x.
  5. *
  6. * (c) 2007 Trent Piepho <xyzzy@speakeasy.org>
  7. * (c) 2005,2006 Ricardo Cerqueira <v4l@cerqueira.org>
  8. * (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
  9. * Based on a dummy cx88 module by Gerd Knorr <kraxel@bytesex.org>
  10. * Based on dummy.c by Jaroslav Kysela <perex@perex.cz>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/pci.h>
  33. #include <linux/slab.h>
  34. #include <asm/delay.h>
  35. #include <sound/core.h>
  36. #include <sound/pcm.h>
  37. #include <sound/pcm_params.h>
  38. #include <sound/control.h>
  39. #include <sound/initval.h>
  40. #include <sound/tlv.h>
  41. #include "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_buffer {
  51. unsigned int bpl;
  52. struct btcx_riscmem risc;
  53. struct videobuf_dmabuf dma;
  54. };
  55. struct cx88_audio_dev {
  56. struct cx88_core *core;
  57. struct cx88_dmaqueue q;
  58. /* pci i/o */
  59. struct pci_dev *pci;
  60. /* audio controls */
  61. int irq;
  62. struct snd_card *card;
  63. spinlock_t reg_lock;
  64. atomic_t count;
  65. unsigned int dma_size;
  66. unsigned int period_size;
  67. unsigned int num_periods;
  68. struct videobuf_dmabuf *dma_risc;
  69. struct cx88_audio_buffer *buf;
  70. struct snd_pcm_substream *substream;
  71. };
  72. typedef struct cx88_audio_dev snd_cx88_card_t;
  73. /****************************************************************************
  74. Module global static vars
  75. ****************************************************************************/
  76. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  77. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  78. static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
  79. module_param_array(enable, bool, NULL, 0444);
  80. MODULE_PARM_DESC(enable, "Enable cx88x soundcard. default enabled.");
  81. module_param_array(index, int, NULL, 0444);
  82. MODULE_PARM_DESC(index, "Index value for cx88x capture interface(s).");
  83. /****************************************************************************
  84. Module macros
  85. ****************************************************************************/
  86. MODULE_DESCRIPTION("ALSA driver module for cx2388x based TV cards");
  87. MODULE_AUTHOR("Ricardo Cerqueira");
  88. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  89. MODULE_LICENSE("GPL");
  90. MODULE_SUPPORTED_DEVICE("{{Conexant,23881},"
  91. "{{Conexant,23882},"
  92. "{{Conexant,23883}");
  93. static unsigned int debug;
  94. module_param(debug,int,0644);
  95. MODULE_PARM_DESC(debug,"enable debug messages");
  96. /****************************************************************************
  97. Module specific funtions
  98. ****************************************************************************/
  99. /*
  100. * BOARD Specific: Sets audio DMA
  101. */
  102. static int _cx88_start_audio_dma(snd_cx88_card_t *chip)
  103. {
  104. struct cx88_audio_buffer *buf = chip->buf;
  105. struct cx88_core *core=chip->core;
  106. struct sram_channel *audio_ch = &cx88_sram_channels[SRAM_CH25];
  107. /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
  108. cx_clear(MO_AUD_DMACNTRL, 0x11);
  109. /* setup fifo + format - out channel */
  110. cx88_sram_channel_setup(chip->core, audio_ch, buf->bpl, buf->risc.dma);
  111. /* sets bpl size */
  112. cx_write(MO_AUDD_LNGTH, buf->bpl);
  113. /* reset counter */
  114. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  115. atomic_set(&chip->count, 0);
  116. dprintk(1, "Start audio DMA, %d B/line, %d lines/FIFO, %d periods, %d "
  117. "byte buffer\n", buf->bpl, cx_read(audio_ch->cmds_start + 8)>>1,
  118. chip->num_periods, buf->bpl * chip->num_periods);
  119. /* Enables corresponding bits at AUD_INT_STAT */
  120. cx_write(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  121. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  122. /* Clean any pending interrupt bits already set */
  123. cx_write(MO_AUD_INTSTAT, ~0);
  124. /* enable audio irqs */
  125. cx_set(MO_PCI_INTMSK, chip->core->pci_irqmask | PCI_INT_AUDINT);
  126. /* start dma */
  127. cx_set(MO_DEV_CNTRL2, (1<<5)); /* Enables Risc Processor */
  128. cx_set(MO_AUD_DMACNTRL, 0x11); /* audio downstream FIFO and RISC enable */
  129. if (debug)
  130. cx88_sram_channel_dump(chip->core, audio_ch);
  131. return 0;
  132. }
  133. /*
  134. * BOARD Specific: Resets audio DMA
  135. */
  136. static int _cx88_stop_audio_dma(snd_cx88_card_t *chip)
  137. {
  138. struct cx88_core *core=chip->core;
  139. dprintk(1, "Stopping audio DMA\n");
  140. /* stop dma */
  141. cx_clear(MO_AUD_DMACNTRL, 0x11);
  142. /* disable irqs */
  143. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  144. cx_clear(MO_AUD_INTMSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
  145. AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
  146. if (debug)
  147. cx88_sram_channel_dump(chip->core, &cx88_sram_channels[SRAM_CH25]);
  148. return 0;
  149. }
  150. #define MAX_IRQ_LOOP 50
  151. /*
  152. * BOARD Specific: IRQ dma bits
  153. */
  154. static char *cx88_aud_irqs[32] = {
  155. "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
  156. NULL, /* reserved */
  157. "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
  158. NULL, /* reserved */
  159. "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
  160. NULL, /* reserved */
  161. "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
  162. NULL, /* reserved */
  163. "opc_err", "par_err", "rip_err", /* 16-18 */
  164. "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
  165. };
  166. /*
  167. * BOARD Specific: Threats IRQ audio specific calls
  168. */
  169. static void cx8801_aud_irq(snd_cx88_card_t *chip)
  170. {
  171. struct cx88_core *core = chip->core;
  172. u32 status, mask;
  173. status = cx_read(MO_AUD_INTSTAT);
  174. mask = cx_read(MO_AUD_INTMSK);
  175. if (0 == (status & mask))
  176. return;
  177. cx_write(MO_AUD_INTSTAT, status);
  178. if (debug > 1 || (status & mask & ~0xff))
  179. cx88_print_irqbits(core->name, "irq aud",
  180. cx88_aud_irqs, ARRAY_SIZE(cx88_aud_irqs),
  181. status, mask);
  182. /* risc op code error */
  183. if (status & AUD_INT_OPC_ERR) {
  184. printk(KERN_WARNING "%s/1: Audio risc op code error\n",core->name);
  185. cx_clear(MO_AUD_DMACNTRL, 0x11);
  186. cx88_sram_channel_dump(core, &cx88_sram_channels[SRAM_CH25]);
  187. }
  188. if (status & AUD_INT_DN_SYNC) {
  189. dprintk(1, "Downstream sync error\n");
  190. cx_write(MO_AUDD_GPCNTRL, GP_COUNT_CONTROL_RESET);
  191. return;
  192. }
  193. /* risc1 downstream */
  194. if (status & AUD_INT_DN_RISCI1) {
  195. atomic_set(&chip->count, cx_read(MO_AUDD_GPCNT));
  196. snd_pcm_period_elapsed(chip->substream);
  197. }
  198. /* FIXME: Any other status should deserve a special handling? */
  199. }
  200. /*
  201. * BOARD Specific: Handles IRQ calls
  202. */
  203. static irqreturn_t cx8801_irq(int irq, void *dev_id)
  204. {
  205. snd_cx88_card_t *chip = dev_id;
  206. struct cx88_core *core = chip->core;
  207. u32 status;
  208. int loop, handled = 0;
  209. for (loop = 0; loop < MAX_IRQ_LOOP; loop++) {
  210. status = cx_read(MO_PCI_INTSTAT) &
  211. (core->pci_irqmask | PCI_INT_AUDINT);
  212. if (0 == status)
  213. goto out;
  214. dprintk(3, "cx8801_irq loop %d/%d, status %x\n",
  215. loop, MAX_IRQ_LOOP, status);
  216. handled = 1;
  217. cx_write(MO_PCI_INTSTAT, status);
  218. if (status & core->pci_irqmask)
  219. cx88_core_irq(core, status);
  220. if (status & PCI_INT_AUDINT)
  221. cx8801_aud_irq(chip);
  222. }
  223. if (MAX_IRQ_LOOP == loop) {
  224. printk(KERN_ERR
  225. "%s/1: IRQ loop detected, disabling interrupts\n",
  226. core->name);
  227. cx_clear(MO_PCI_INTMSK, PCI_INT_AUDINT);
  228. }
  229. out:
  230. return IRQ_RETVAL(handled);
  231. }
  232. static int dsp_buffer_free(snd_cx88_card_t *chip)
  233. {
  234. BUG_ON(!chip->dma_size);
  235. dprintk(2,"Freeing buffer\n");
  236. videobuf_dma_unmap(&chip->pci->dev, chip->dma_risc);
  237. videobuf_dma_free(chip->dma_risc);
  238. btcx_riscmem_free(chip->pci,&chip->buf->risc);
  239. kfree(chip->buf);
  240. chip->dma_risc = NULL;
  241. chip->dma_size = 0;
  242. return 0;
  243. }
  244. /****************************************************************************
  245. ALSA PCM Interface
  246. ****************************************************************************/
  247. /*
  248. * Digital hardware definition
  249. */
  250. #define DEFAULT_FIFO_SIZE 4096
  251. static struct snd_pcm_hardware snd_cx88_digital_hw = {
  252. .info = SNDRV_PCM_INFO_MMAP |
  253. SNDRV_PCM_INFO_INTERLEAVED |
  254. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  255. SNDRV_PCM_INFO_MMAP_VALID,
  256. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  257. .rates = SNDRV_PCM_RATE_48000,
  258. .rate_min = 48000,
  259. .rate_max = 48000,
  260. .channels_min = 2,
  261. .channels_max = 2,
  262. /* Analog audio output will be full of clicks and pops if there
  263. are not exactly four lines in the SRAM FIFO buffer. */
  264. .period_bytes_min = DEFAULT_FIFO_SIZE/4,
  265. .period_bytes_max = DEFAULT_FIFO_SIZE/4,
  266. .periods_min = 1,
  267. .periods_max = 1024,
  268. .buffer_bytes_max = (1024*1024),
  269. };
  270. /*
  271. * audio pcm capture open callback
  272. */
  273. static int snd_cx88_pcm_open(struct snd_pcm_substream *substream)
  274. {
  275. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  276. struct snd_pcm_runtime *runtime = substream->runtime;
  277. int err;
  278. if (!chip) {
  279. printk(KERN_ERR "BUG: cx88 can't find device struct."
  280. " Can't proceed with open\n");
  281. return -ENODEV;
  282. }
  283. err = snd_pcm_hw_constraint_pow2(runtime, 0, SNDRV_PCM_HW_PARAM_PERIODS);
  284. if (err < 0)
  285. goto _error;
  286. chip->substream = substream;
  287. runtime->hw = snd_cx88_digital_hw;
  288. if (cx88_sram_channels[SRAM_CH25].fifo_size != DEFAULT_FIFO_SIZE) {
  289. unsigned int bpl = cx88_sram_channels[SRAM_CH25].fifo_size / 4;
  290. bpl &= ~7; /* must be multiple of 8 */
  291. runtime->hw.period_bytes_min = bpl;
  292. runtime->hw.period_bytes_max = bpl;
  293. }
  294. return 0;
  295. _error:
  296. dprintk(1,"Error opening PCM!\n");
  297. return err;
  298. }
  299. /*
  300. * audio close callback
  301. */
  302. static int snd_cx88_close(struct snd_pcm_substream *substream)
  303. {
  304. return 0;
  305. }
  306. /*
  307. * hw_params callback
  308. */
  309. static int snd_cx88_hw_params(struct snd_pcm_substream * substream,
  310. struct snd_pcm_hw_params * hw_params)
  311. {
  312. snd_cx88_card_t *chip = snd_pcm_substream_chip(substream);
  313. struct videobuf_dmabuf *dma;
  314. struct cx88_audio_buffer *buf;
  315. int ret;
  316. if (substream->runtime->dma_area) {
  317. dsp_buffer_free(chip);
  318. substream->runtime->dma_area = NULL;
  319. }
  320. chip->period_size = params_period_bytes(hw_params);
  321. chip->num_periods = params_periods(hw_params);
  322. chip->dma_size = chip->period_size * params_periods(hw_params);
  323. BUG_ON(!chip->dma_size);
  324. BUG_ON(chip->num_periods & (chip->num_periods-1));
  325. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  326. if (NULL == buf)
  327. return -ENOMEM;
  328. buf->bpl = chip->period_size;
  329. dma = &buf->dma;
  330. videobuf_dma_init(dma);
  331. ret = videobuf_dma_init_kernel(dma, PCI_DMA_FROMDEVICE,
  332. (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
  333. if (ret < 0)
  334. goto error;
  335. ret = videobuf_dma_map(&chip->pci->dev, dma);
  336. if (ret < 0)
  337. goto error;
  338. ret = cx88_risc_databuffer(chip->pci, &buf->risc, dma->sglist,
  339. chip->period_size, chip->num_periods, 1);
  340. if (ret < 0)
  341. goto error;
  342. /* Loop back to start of program */
  343. buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP|RISC_IRQ1|RISC_CNT_INC);
  344. buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
  345. chip->buf = buf;
  346. chip->dma_risc = dma;
  347. substream->runtime->dma_area = chip->dma_risc->vaddr;
  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 left, right, v, b;
  480. int changed = 0;
  481. u32 old;
  482. left = value->value.integer.value[0] & 0x3f;
  483. right = value->value.integer.value[1] & 0x3f;
  484. b = right - left;
  485. if (b < 0) {
  486. v = 0x3f - left;
  487. b = (-b) | 0x40;
  488. } else {
  489. v = 0x3f - right;
  490. }
  491. /* Do we really know this will always be called with IRQs on? */
  492. spin_lock_irq(&chip->reg_lock);
  493. old = cx_read(AUD_VOL_CTL);
  494. if (v != (old & 0x3f)) {
  495. cx_write(AUD_VOL_CTL, (old & ~0x3f) | v);
  496. changed = 1;
  497. }
  498. if (cx_read(AUD_BAL_CTL) != b) {
  499. cx_write(AUD_BAL_CTL, b);
  500. changed = 1;
  501. }
  502. spin_unlock_irq(&chip->reg_lock);
  503. return changed;
  504. }
  505. static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
  506. static struct snd_kcontrol_new snd_cx88_volume = {
  507. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  508. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  509. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  510. .name = "Playback Volume",
  511. .info = snd_cx88_volume_info,
  512. .get = snd_cx88_volume_get,
  513. .put = snd_cx88_volume_put,
  514. .tlv.p = snd_cx88_db_scale,
  515. };
  516. static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
  517. struct snd_ctl_elem_value *value)
  518. {
  519. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  520. struct cx88_core *core = chip->core;
  521. u32 bit = kcontrol->private_value;
  522. value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
  523. return 0;
  524. }
  525. static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
  526. struct snd_ctl_elem_value *value)
  527. {
  528. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  529. struct cx88_core *core = chip->core;
  530. u32 bit = kcontrol->private_value;
  531. int ret = 0;
  532. u32 vol;
  533. spin_lock_irq(&chip->reg_lock);
  534. vol = cx_read(AUD_VOL_CTL);
  535. if (value->value.integer.value[0] != !(vol & bit)) {
  536. vol ^= bit;
  537. cx_write(AUD_VOL_CTL, vol);
  538. ret = 1;
  539. }
  540. spin_unlock_irq(&chip->reg_lock);
  541. return ret;
  542. }
  543. static struct snd_kcontrol_new snd_cx88_dac_switch = {
  544. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  545. .name = "Playback Switch",
  546. .info = snd_ctl_boolean_mono_info,
  547. .get = snd_cx88_switch_get,
  548. .put = snd_cx88_switch_put,
  549. .private_value = (1<<8),
  550. };
  551. static struct snd_kcontrol_new snd_cx88_source_switch = {
  552. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  553. .name = "Capture Switch",
  554. .info = snd_ctl_boolean_mono_info,
  555. .get = snd_cx88_switch_get,
  556. .put = snd_cx88_switch_put,
  557. .private_value = (1<<6),
  558. };
  559. /****************************************************************************
  560. Basic Flow for Sound Devices
  561. ****************************************************************************/
  562. /*
  563. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  564. * Only boards with eeprom and byte 1 at eeprom=1 have it
  565. */
  566. static struct pci_device_id cx88_audio_pci_tbl[] __devinitdata = {
  567. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  568. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  569. {0, }
  570. };
  571. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  572. /*
  573. * Chip-specific destructor
  574. */
  575. static int snd_cx88_free(snd_cx88_card_t *chip)
  576. {
  577. if (chip->irq >= 0)
  578. free_irq(chip->irq, chip);
  579. cx88_core_put(chip->core,chip->pci);
  580. pci_disable_device(chip->pci);
  581. return 0;
  582. }
  583. /*
  584. * Component Destructor
  585. */
  586. static void snd_cx88_dev_free(struct snd_card * card)
  587. {
  588. snd_cx88_card_t *chip = card->private_data;
  589. snd_cx88_free(chip);
  590. }
  591. /*
  592. * Alsa Constructor - Component probe
  593. */
  594. static int devno;
  595. static int __devinit snd_cx88_create(struct snd_card *card,
  596. struct pci_dev *pci,
  597. snd_cx88_card_t **rchip)
  598. {
  599. snd_cx88_card_t *chip;
  600. struct cx88_core *core;
  601. int err;
  602. unsigned char pci_lat;
  603. *rchip = NULL;
  604. err = pci_enable_device(pci);
  605. if (err < 0)
  606. return err;
  607. pci_set_master(pci);
  608. chip = (snd_cx88_card_t *) card->private_data;
  609. core = cx88_core_get(pci);
  610. if (NULL == core) {
  611. err = -EINVAL;
  612. return err;
  613. }
  614. if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
  615. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  616. err = -EIO;
  617. cx88_core_put(core,pci);
  618. return err;
  619. }
  620. /* pci init */
  621. chip->card = card;
  622. chip->pci = pci;
  623. chip->irq = -1;
  624. spin_lock_init(&chip->reg_lock);
  625. chip->core = core;
  626. /* get irq */
  627. err = request_irq(chip->pci->irq, cx8801_irq,
  628. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  629. if (err < 0) {
  630. dprintk(0, "%s: can't get IRQ %d\n",
  631. chip->core->name, chip->pci->irq);
  632. return err;
  633. }
  634. /* print pci info */
  635. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  636. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  637. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  638. pci_name(pci), pci->revision, pci->irq,
  639. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  640. chip->irq = pci->irq;
  641. synchronize_irq(chip->irq);
  642. snd_card_set_dev(card, &pci->dev);
  643. *rchip = chip;
  644. return 0;
  645. }
  646. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  647. const struct pci_device_id *pci_id)
  648. {
  649. struct snd_card *card;
  650. snd_cx88_card_t *chip;
  651. int err;
  652. if (devno >= SNDRV_CARDS)
  653. return (-ENODEV);
  654. if (!enable[devno]) {
  655. ++devno;
  656. return (-ENOENT);
  657. }
  658. err = snd_card_create(index[devno], id[devno], THIS_MODULE,
  659. sizeof(snd_cx88_card_t), &card);
  660. if (err < 0)
  661. return err;
  662. card->private_free = snd_cx88_dev_free;
  663. err = snd_cx88_create(card, pci, &chip);
  664. if (err < 0)
  665. goto error;
  666. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  667. if (err < 0)
  668. goto error;
  669. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
  670. if (err < 0)
  671. goto error;
  672. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
  673. if (err < 0)
  674. goto error;
  675. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
  676. if (err < 0)
  677. goto error;
  678. strcpy (card->driver, "CX88x");
  679. sprintf(card->shortname, "Conexant CX%x", pci->device);
  680. sprintf(card->longname, "%s at %#llx",
  681. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  682. strcpy (card->mixername, "CX88");
  683. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  684. card->driver,devno);
  685. err = snd_card_register(card);
  686. if (err < 0)
  687. goto error;
  688. pci_set_drvdata(pci,card);
  689. devno++;
  690. return 0;
  691. error:
  692. snd_card_free(card);
  693. return err;
  694. }
  695. /*
  696. * ALSA destructor
  697. */
  698. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  699. {
  700. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  701. snd_card_free((void *)card);
  702. pci_set_drvdata(pci, NULL);
  703. devno--;
  704. }
  705. /*
  706. * PCI driver definition
  707. */
  708. static struct pci_driver cx88_audio_pci_driver = {
  709. .name = "cx88_audio",
  710. .id_table = cx88_audio_pci_tbl,
  711. .probe = cx88_audio_initdev,
  712. .remove = __devexit_p(cx88_audio_finidev),
  713. };
  714. /****************************************************************************
  715. LINUX MODULE INIT
  716. ****************************************************************************/
  717. /*
  718. * module init
  719. */
  720. static int __init cx88_audio_init(void)
  721. {
  722. printk(KERN_INFO "cx2388x alsa driver version %d.%d.%d loaded\n",
  723. (CX88_VERSION_CODE >> 16) & 0xff,
  724. (CX88_VERSION_CODE >> 8) & 0xff,
  725. CX88_VERSION_CODE & 0xff);
  726. #ifdef SNAPSHOT
  727. printk(KERN_INFO "cx2388x: snapshot date %04d-%02d-%02d\n",
  728. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  729. #endif
  730. return pci_register_driver(&cx88_audio_pci_driver);
  731. }
  732. /*
  733. * module remove
  734. */
  735. static void __exit cx88_audio_fini(void)
  736. {
  737. pci_unregister_driver(&cx88_audio_pci_driver);
  738. }
  739. module_init(cx88_audio_init);
  740. module_exit(cx88_audio_fini);
  741. /* ----------------------------------------------------------- */
  742. /*
  743. * Local variables:
  744. * c-basic-offset: 8
  745. * End:
  746. */