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