cx88-alsa.c 25 KB

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