cx88-alsa.c 24 KB

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