cx88-alsa.c 24 KB

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