cx88-alsa.c 25 KB

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