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