cx88-alsa.c 23 KB

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