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