cx88-alsa.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. int left = value->value.integer.value[0];
  481. int right = value->value.integer.value[1];
  482. int v, b;
  483. /* Pass volume & balance onto any WM8775 */
  484. if (left >= right) {
  485. v = left << 10;
  486. b = left ? (0x8000 * right) / left : 0x8000;
  487. } else {
  488. v = right << 10;
  489. b = right ? 0xffff - (0x8000 * left) / right : 0x8000;
  490. }
  491. wm8775_s_ctrl(core, V4L2_CID_AUDIO_VOLUME, v);
  492. wm8775_s_ctrl(core, V4L2_CID_AUDIO_BALANCE, b);
  493. }
  494. /* OK - TODO: test it */
  495. static int snd_cx88_volume_put(struct snd_kcontrol *kcontrol,
  496. struct snd_ctl_elem_value *value)
  497. {
  498. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  499. struct cx88_core *core=chip->core;
  500. int left, right, v, b;
  501. int changed = 0;
  502. u32 old;
  503. if (core->board.audio_chip == V4L2_IDENT_WM8775)
  504. snd_cx88_wm8775_volume_put(kcontrol, value);
  505. left = value->value.integer.value[0] & 0x3f;
  506. right = value->value.integer.value[1] & 0x3f;
  507. b = right - left;
  508. if (b < 0) {
  509. v = 0x3f - left;
  510. b = (-b) | 0x40;
  511. } else {
  512. v = 0x3f - right;
  513. }
  514. /* Do we really know this will always be called with IRQs on? */
  515. spin_lock_irq(&chip->reg_lock);
  516. old = cx_read(AUD_VOL_CTL);
  517. if (v != (old & 0x3f)) {
  518. cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, (old & ~0x3f) | v);
  519. changed = 1;
  520. }
  521. if ((cx_read(AUD_BAL_CTL) & 0x7f) != b) {
  522. cx_write(AUD_BAL_CTL, b);
  523. changed = 1;
  524. }
  525. spin_unlock_irq(&chip->reg_lock);
  526. return changed;
  527. }
  528. static const DECLARE_TLV_DB_SCALE(snd_cx88_db_scale, -6300, 100, 0);
  529. static const struct snd_kcontrol_new snd_cx88_volume = {
  530. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  531. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
  532. SNDRV_CTL_ELEM_ACCESS_TLV_READ,
  533. .name = "Analog-TV Volume",
  534. .info = snd_cx88_volume_info,
  535. .get = snd_cx88_volume_get,
  536. .put = snd_cx88_volume_put,
  537. .tlv.p = snd_cx88_db_scale,
  538. };
  539. static int snd_cx88_switch_get(struct snd_kcontrol *kcontrol,
  540. struct snd_ctl_elem_value *value)
  541. {
  542. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  543. struct cx88_core *core = chip->core;
  544. u32 bit = kcontrol->private_value;
  545. value->value.integer.value[0] = !(cx_read(AUD_VOL_CTL) & bit);
  546. return 0;
  547. }
  548. static int snd_cx88_switch_put(struct snd_kcontrol *kcontrol,
  549. struct snd_ctl_elem_value *value)
  550. {
  551. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  552. struct cx88_core *core = chip->core;
  553. u32 bit = kcontrol->private_value;
  554. int ret = 0;
  555. u32 vol;
  556. spin_lock_irq(&chip->reg_lock);
  557. vol = cx_read(AUD_VOL_CTL);
  558. if (value->value.integer.value[0] != !(vol & bit)) {
  559. vol ^= bit;
  560. cx_swrite(SHADOW_AUD_VOL_CTL, AUD_VOL_CTL, vol);
  561. /* Pass mute onto any WM8775 */
  562. if ((core->board.audio_chip == V4L2_IDENT_WM8775) &&
  563. ((1<<6) == bit))
  564. wm8775_s_ctrl(core, V4L2_CID_AUDIO_MUTE, 0 != (vol & bit));
  565. ret = 1;
  566. }
  567. spin_unlock_irq(&chip->reg_lock);
  568. return ret;
  569. }
  570. static const struct snd_kcontrol_new snd_cx88_dac_switch = {
  571. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  572. .name = "Audio-Out Switch",
  573. .info = snd_ctl_boolean_mono_info,
  574. .get = snd_cx88_switch_get,
  575. .put = snd_cx88_switch_put,
  576. .private_value = (1<<8),
  577. };
  578. static const struct snd_kcontrol_new snd_cx88_source_switch = {
  579. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  580. .name = "Analog-TV Switch",
  581. .info = snd_ctl_boolean_mono_info,
  582. .get = snd_cx88_switch_get,
  583. .put = snd_cx88_switch_put,
  584. .private_value = (1<<6),
  585. };
  586. static int snd_cx88_alc_get(struct snd_kcontrol *kcontrol,
  587. struct snd_ctl_elem_value *value)
  588. {
  589. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  590. struct cx88_core *core = chip->core;
  591. s32 val;
  592. val = wm8775_g_ctrl(core, V4L2_CID_AUDIO_LOUDNESS);
  593. value->value.integer.value[0] = val ? 1 : 0;
  594. return 0;
  595. }
  596. static int snd_cx88_alc_put(struct snd_kcontrol *kcontrol,
  597. struct snd_ctl_elem_value *value)
  598. {
  599. snd_cx88_card_t *chip = snd_kcontrol_chip(kcontrol);
  600. struct cx88_core *core = chip->core;
  601. struct v4l2_control client_ctl;
  602. memset(&client_ctl, 0, sizeof(client_ctl));
  603. client_ctl.value = 0 != value->value.integer.value[0];
  604. client_ctl.id = V4L2_CID_AUDIO_LOUDNESS;
  605. call_hw(core, WM8775_GID, core, s_ctrl, &client_ctl);
  606. return 0;
  607. }
  608. static struct snd_kcontrol_new snd_cx88_alc_switch = {
  609. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  610. .name = "Line-In ALC Switch",
  611. .info = snd_ctl_boolean_mono_info,
  612. .get = snd_cx88_alc_get,
  613. .put = snd_cx88_alc_put,
  614. };
  615. /****************************************************************************
  616. Basic Flow for Sound Devices
  617. ****************************************************************************/
  618. /*
  619. * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
  620. * Only boards with eeprom and byte 1 at eeprom=1 have it
  621. */
  622. static const struct pci_device_id const cx88_audio_pci_tbl[] __devinitdata = {
  623. {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  624. {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
  625. {0, }
  626. };
  627. MODULE_DEVICE_TABLE(pci, cx88_audio_pci_tbl);
  628. /*
  629. * Chip-specific destructor
  630. */
  631. static int snd_cx88_free(snd_cx88_card_t *chip)
  632. {
  633. if (chip->irq >= 0)
  634. free_irq(chip->irq, chip);
  635. cx88_core_put(chip->core,chip->pci);
  636. pci_disable_device(chip->pci);
  637. return 0;
  638. }
  639. /*
  640. * Component Destructor
  641. */
  642. static void snd_cx88_dev_free(struct snd_card * card)
  643. {
  644. snd_cx88_card_t *chip = card->private_data;
  645. snd_cx88_free(chip);
  646. }
  647. /*
  648. * Alsa Constructor - Component probe
  649. */
  650. static int devno;
  651. static int __devinit snd_cx88_create(struct snd_card *card,
  652. struct pci_dev *pci,
  653. snd_cx88_card_t **rchip,
  654. struct cx88_core **core_ptr)
  655. {
  656. snd_cx88_card_t *chip;
  657. struct cx88_core *core;
  658. int err;
  659. unsigned char pci_lat;
  660. *rchip = NULL;
  661. err = pci_enable_device(pci);
  662. if (err < 0)
  663. return err;
  664. pci_set_master(pci);
  665. chip = card->private_data;
  666. core = cx88_core_get(pci);
  667. if (NULL == core) {
  668. err = -EINVAL;
  669. return err;
  670. }
  671. if (!pci_dma_supported(pci,DMA_BIT_MASK(32))) {
  672. dprintk(0, "%s/1: Oops: no 32bit PCI DMA ???\n",core->name);
  673. err = -EIO;
  674. cx88_core_put(core, pci);
  675. return err;
  676. }
  677. /* pci init */
  678. chip->card = card;
  679. chip->pci = pci;
  680. chip->irq = -1;
  681. spin_lock_init(&chip->reg_lock);
  682. chip->core = core;
  683. /* get irq */
  684. err = request_irq(chip->pci->irq, cx8801_irq,
  685. IRQF_SHARED | IRQF_DISABLED, chip->core->name, chip);
  686. if (err < 0) {
  687. dprintk(0, "%s: can't get IRQ %d\n",
  688. chip->core->name, chip->pci->irq);
  689. return err;
  690. }
  691. /* print pci info */
  692. pci_read_config_byte(pci, PCI_LATENCY_TIMER, &pci_lat);
  693. dprintk(1,"ALSA %s/%i: found at %s, rev: %d, irq: %d, "
  694. "latency: %d, mmio: 0x%llx\n", core->name, devno,
  695. pci_name(pci), pci->revision, pci->irq,
  696. pci_lat, (unsigned long long)pci_resource_start(pci,0));
  697. chip->irq = pci->irq;
  698. synchronize_irq(chip->irq);
  699. snd_card_set_dev(card, &pci->dev);
  700. *rchip = chip;
  701. *core_ptr = core;
  702. return 0;
  703. }
  704. static int __devinit cx88_audio_initdev(struct pci_dev *pci,
  705. const struct pci_device_id *pci_id)
  706. {
  707. struct snd_card *card;
  708. snd_cx88_card_t *chip;
  709. struct cx88_core *core = NULL;
  710. int err;
  711. if (devno >= SNDRV_CARDS)
  712. return (-ENODEV);
  713. if (!enable[devno]) {
  714. ++devno;
  715. return (-ENOENT);
  716. }
  717. err = snd_card_create(index[devno], id[devno], THIS_MODULE,
  718. sizeof(snd_cx88_card_t), &card);
  719. if (err < 0)
  720. return err;
  721. card->private_free = snd_cx88_dev_free;
  722. err = snd_cx88_create(card, pci, &chip, &core);
  723. if (err < 0)
  724. goto error;
  725. err = snd_cx88_pcm(chip, 0, "CX88 Digital");
  726. if (err < 0)
  727. goto error;
  728. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_volume, chip));
  729. if (err < 0)
  730. goto error;
  731. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_dac_switch, chip));
  732. if (err < 0)
  733. goto error;
  734. err = snd_ctl_add(card, snd_ctl_new1(&snd_cx88_source_switch, chip));
  735. if (err < 0)
  736. goto error;
  737. /* If there's a wm8775 then add a Line-In ALC switch */
  738. if (core->board.audio_chip == V4L2_IDENT_WM8775)
  739. snd_ctl_add(card, snd_ctl_new1(&snd_cx88_alc_switch, chip));
  740. strcpy (card->driver, "CX88x");
  741. sprintf(card->shortname, "Conexant CX%x", pci->device);
  742. sprintf(card->longname, "%s at %#llx",
  743. card->shortname,(unsigned long long)pci_resource_start(pci, 0));
  744. strcpy (card->mixername, "CX88");
  745. dprintk (0, "%s/%i: ALSA support for cx2388x boards\n",
  746. card->driver,devno);
  747. err = snd_card_register(card);
  748. if (err < 0)
  749. goto error;
  750. pci_set_drvdata(pci,card);
  751. devno++;
  752. return 0;
  753. error:
  754. snd_card_free(card);
  755. return err;
  756. }
  757. /*
  758. * ALSA destructor
  759. */
  760. static void __devexit cx88_audio_finidev(struct pci_dev *pci)
  761. {
  762. struct cx88_audio_dev *card = pci_get_drvdata(pci);
  763. snd_card_free((void *)card);
  764. pci_set_drvdata(pci, NULL);
  765. devno--;
  766. }
  767. /*
  768. * PCI driver definition
  769. */
  770. static struct pci_driver cx88_audio_pci_driver = {
  771. .name = "cx88_audio",
  772. .id_table = cx88_audio_pci_tbl,
  773. .probe = cx88_audio_initdev,
  774. .remove = __devexit_p(cx88_audio_finidev),
  775. };
  776. /****************************************************************************
  777. LINUX MODULE INIT
  778. ****************************************************************************/
  779. /*
  780. * module init
  781. */
  782. static int __init cx88_audio_init(void)
  783. {
  784. printk(KERN_INFO "cx2388x alsa driver version %s loaded\n",
  785. CX88_VERSION);
  786. return pci_register_driver(&cx88_audio_pci_driver);
  787. }
  788. /*
  789. * module remove
  790. */
  791. static void __exit cx88_audio_fini(void)
  792. {
  793. pci_unregister_driver(&cx88_audio_pci_driver);
  794. }
  795. module_init(cx88_audio_init);
  796. module_exit(cx88_audio_fini);