]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/media/pci/cx25821/cx25821-alsa.c
module: remove never implemented MODULE_SUPPORTED_DEVICE
[mirror_ubuntu-jammy-kernel.git] / drivers / media / pci / cx25821 / cx25821-alsa.c
CommitLineData
50acfb2b 1// SPDX-License-Identifier: GPL-2.0-only
1a9fc855
MCC
2/*
3 * Driver for the Conexant CX25821 PCIe bridge
4 *
5 * Copyright (C) 2009 Conexant Systems Inc.
6 * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com>
7 * Based on SAA713x ALSA driver and CX88 driver
1a9fc855
MCC
8 */
9
36d89f7d
JP
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
02b20b0b
MCC
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/device.h>
15#include <linux/interrupt.h>
16#include <linux/vmalloc.h>
17#include <linux/dma-mapping.h>
18#include <linux/pci.h>
5a0e3ad6 19#include <linux/slab.h>
02b20b0b 20
8889a8a5 21#include <linux/delay.h>
02b20b0b
MCC
22#include <sound/core.h>
23#include <sound/pcm.h>
24#include <sound/pcm_params.h>
25#include <sound/control.h>
26#include <sound/initval.h>
27#include <sound/tlv.h>
28
02b20b0b
MCC
29#include "cx25821.h"
30#include "cx25821-reg.h"
31
32#define AUDIO_SRAM_CHANNEL SRAM_CH08
33
36d89f7d
JP
34#define dprintk(level, fmt, arg...) \
35do { \
36 if (debug >= level) \
37 pr_info("%s/1: " fmt, chip->dev->name, ##arg); \
38} while (0)
39#define dprintk_core(level, fmt, arg...) \
40do { \
41 if (debug >= level) \
42 printk(KERN_DEBUG "%s/1: " fmt, chip->dev->name, ##arg); \
43} while (0)
02b20b0b 44
02b20b0b
MCC
45/****************************************************************************
46 Data type declarations - Can be moded to a header file later
47 ****************************************************************************/
48
02b20b0b
MCC
49static int devno;
50
fecfedeb
LP
51struct cx25821_audio_buffer {
52 unsigned int bpl;
5ede94c7 53 struct cx25821_riscmem risc;
8d8e6d60
HV
54 void *vaddr;
55 struct scatterlist *sglist;
99c2ec04
MCC
56 int sglen;
57 unsigned long nr_pages;
fecfedeb
LP
58};
59
02b20b0b 60struct cx25821_audio_dev {
1a9fc855
MCC
61 struct cx25821_dev *dev;
62 struct cx25821_dmaqueue q;
02b20b0b
MCC
63
64 /* pci i/o */
1a9fc855 65 struct pci_dev *pci;
02b20b0b
MCC
66
67 /* audio controls */
1a9fc855 68 int irq;
02b20b0b 69
1a9fc855 70 struct snd_card *card;
02b20b0b
MCC
71
72 unsigned long iobase;
1a9fc855
MCC
73 spinlock_t reg_lock;
74 atomic_t count;
02b20b0b 75
1a9fc855
MCC
76 unsigned int dma_size;
77 unsigned int period_size;
78 unsigned int num_periods;
02b20b0b 79
fecfedeb 80 struct cx25821_audio_buffer *buf;
02b20b0b 81
1a9fc855 82 struct snd_pcm_substream *substream;
02b20b0b 83};
02b20b0b
MCC
84
85
02b20b0b
MCC
86/****************************************************************************
87 Module global static vars
88 ****************************************************************************/
89
90static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
91static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
d275d935 92static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
02b20b0b
MCC
93
94module_param_array(enable, bool, NULL, 0444);
95MODULE_PARM_DESC(enable, "Enable cx25821 soundcard. default enabled.");
96
97module_param_array(index, int, NULL, 0444);
98MODULE_PARM_DESC(index, "Index value for cx25821 capture interface(s).");
99
02b20b0b
MCC
100/****************************************************************************
101 Module macros
102 ****************************************************************************/
103
104MODULE_DESCRIPTION("ALSA driver module for cx25821 based capture cards");
105MODULE_AUTHOR("Hiep Huynh");
106MODULE_LICENSE("GPL");
02b20b0b
MCC
107
108static unsigned int debug;
1a9fc855
MCC
109module_param(debug, int, 0644);
110MODULE_PARM_DESC(debug, "enable debug messages");
02b20b0b
MCC
111
112/****************************************************************************
16790554 113 Module specific functions
02b20b0b
MCC
114 ****************************************************************************/
115/* Constants taken from cx88-reg.h */
116#define AUD_INT_DN_RISCI1 (1 << 0)
117#define AUD_INT_UP_RISCI1 (1 << 1)
118#define AUD_INT_RDS_DN_RISCI1 (1 << 2)
1a9fc855 119#define AUD_INT_DN_RISCI2 (1 << 4) /* yes, 3 is skipped */
02b20b0b
MCC
120#define AUD_INT_UP_RISCI2 (1 << 5)
121#define AUD_INT_RDS_DN_RISCI2 (1 << 6)
122#define AUD_INT_DN_SYNC (1 << 12)
123#define AUD_INT_UP_SYNC (1 << 13)
124#define AUD_INT_RDS_DN_SYNC (1 << 14)
125#define AUD_INT_OPC_ERR (1 << 16)
126#define AUD_INT_BER_IRQ (1 << 20)
127#define AUD_INT_MCHG_IRQ (1 << 21)
128#define GP_COUNT_CONTROL_RESET 0x3
129
130#define PCI_MSK_AUD_EXT (1 << 4)
131#define PCI_MSK_AUD_INT (1 << 3)
8d8e6d60 132
99c2ec04
MCC
133static int cx25821_alsa_dma_init(struct cx25821_audio_dev *chip,
134 unsigned long nr_pages)
8d8e6d60
HV
135{
136 struct cx25821_audio_buffer *buf = chip->buf;
137 struct page *pg;
138 int i;
139
140 buf->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
141 if (NULL == buf->vaddr) {
99c2ec04 142 dprintk(1, "vmalloc_32(%lu pages) failed\n", nr_pages);
8d8e6d60
HV
143 return -ENOMEM;
144 }
145
99c2ec04 146 dprintk(1, "vmalloc is at addr 0x%p, size=%lu\n",
04f849c0 147 buf->vaddr,
8d8e6d60
HV
148 nr_pages << PAGE_SHIFT);
149
150 memset(buf->vaddr, 0, nr_pages << PAGE_SHIFT);
151 buf->nr_pages = nr_pages;
152
fad953ce 153 buf->sglist = vzalloc(array_size(sizeof(*buf->sglist), buf->nr_pages));
8d8e6d60
HV
154 if (NULL == buf->sglist)
155 goto vzalloc_err;
156
157 sg_init_table(buf->sglist, buf->nr_pages);
158 for (i = 0; i < buf->nr_pages; i++) {
159 pg = vmalloc_to_page(buf->vaddr + i * PAGE_SIZE);
160 if (NULL == pg)
161 goto vmalloc_to_page_err;
162 sg_set_page(&buf->sglist[i], pg, PAGE_SIZE, 0);
163 }
164 return 0;
165
166vmalloc_to_page_err:
167 vfree(buf->sglist);
168 buf->sglist = NULL;
169vzalloc_err:
170 vfree(buf->vaddr);
171 buf->vaddr = NULL;
172 return -ENOMEM;
173}
174
175static int cx25821_alsa_dma_map(struct cx25821_audio_dev *dev)
176{
177 struct cx25821_audio_buffer *buf = dev->buf;
178
179 buf->sglen = dma_map_sg(&dev->pci->dev, buf->sglist,
f95fc014 180 buf->nr_pages, DMA_FROM_DEVICE);
8d8e6d60
HV
181
182 if (0 == buf->sglen) {
183 pr_warn("%s: cx25821_alsa_map_sg failed\n", __func__);
184 return -ENOMEM;
185 }
186 return 0;
187}
188
189static int cx25821_alsa_dma_unmap(struct cx25821_audio_dev *dev)
190{
191 struct cx25821_audio_buffer *buf = dev->buf;
192
193 if (!buf->sglen)
194 return 0;
195
f95fc014 196 dma_unmap_sg(&dev->pci->dev, buf->sglist, buf->nr_pages, DMA_FROM_DEVICE);
8d8e6d60
HV
197 buf->sglen = 0;
198 return 0;
199}
200
201static int cx25821_alsa_dma_free(struct cx25821_audio_buffer *buf)
202{
203 vfree(buf->sglist);
204 buf->sglist = NULL;
205 vfree(buf->vaddr);
206 buf->vaddr = NULL;
207 return 0;
208}
209
02b20b0b
MCC
210/*
211 * BOARD Specific: Sets audio DMA
212 */
213
8889a8a5 214static int _cx25821_start_audio_dma(struct cx25821_audio_dev *chip)
02b20b0b 215{
fecfedeb 216 struct cx25821_audio_buffer *buf = chip->buf;
1a9fc855 217 struct cx25821_dev *dev = chip->dev;
bfef0d35 218 const struct sram_channel *audio_ch =
1a9fc855 219 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL];
02b20b0b
MCC
220 u32 tmp = 0;
221
8889a8a5 222 /* enable output on the GPIO 0 for the MCLK ADC (Audio) */
1a9fc855 223 cx25821_set_gpiopin_direction(chip->dev, 0, 0);
02b20b0b
MCC
224
225 /* Make sure RISC/FIFO are off before changing FIFO/RISC settings */
1a9fc855
MCC
226 cx_clear(AUD_INT_DMA_CTL,
227 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
02b20b0b
MCC
228
229 /* setup fifo + format - out channel */
1a9fc855
MCC
230 cx25821_sram_channel_setup_audio(chip->dev, audio_ch, buf->bpl,
231 buf->risc.dma);
02b20b0b
MCC
232
233 /* sets bpl size */
234 cx_write(AUD_A_LNGTH, buf->bpl);
235
236 /* reset counter */
8889a8a5
OP
237 /* GP_COUNT_CONTROL_RESET = 0x3 */
238 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
02b20b0b
MCC
239 atomic_set(&chip->count, 0);
240
8889a8a5 241 /* Set the input mode to 16-bit */
1a9fc855 242 tmp = cx_read(AUD_A_CFG);
e5f10621 243 cx_write(AUD_A_CFG, tmp | FLD_AUD_DST_PK_MODE | FLD_AUD_DST_ENABLE |
1a9fc855 244 FLD_AUD_CLK_ENABLE);
02b20b0b 245
36d89f7d
JP
246 /*
247 pr_info("DEBUG: Start audio DMA, %d B/line, cmds_start(0x%x)= %d lines/FIFO, %d periods, %d byte buffer\n",
248 buf->bpl, audio_ch->cmds_start,
249 cx_read(audio_ch->cmds_start + 12)>>1,
250 chip->num_periods, buf->bpl * chip->num_periods);
8889a8a5 251 */
02b20b0b
MCC
252
253 /* Enables corresponding bits at AUD_INT_STAT */
e5f10621
LF
254 cx_write(AUD_A_INT_MSK, FLD_AUD_DST_RISCI1 | FLD_AUD_DST_OF |
255 FLD_AUD_DST_SYNC | FLD_AUD_DST_OPC_ERR);
02b20b0b
MCC
256
257 /* Clean any pending interrupt bits already set */
258 cx_write(AUD_A_INT_STAT, ~0);
259
260 /* enable audio irqs */
261 cx_set(PCI_INT_MSK, chip->dev->pci_irqmask | PCI_MSK_AUD_INT);
262
8889a8a5 263 /* Turn on audio downstream fifo and risc enable 0x101 */
1a9fc855 264 tmp = cx_read(AUD_INT_DMA_CTL);
e5f10621
LF
265 cx_set(AUD_INT_DMA_CTL, tmp |
266 (FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN));
02b20b0b
MCC
267
268 mdelay(100);
269 return 0;
270}
271
272/*
273 * BOARD Specific: Resets audio DMA
274 */
8889a8a5 275static int _cx25821_stop_audio_dma(struct cx25821_audio_dev *chip)
02b20b0b
MCC
276{
277 struct cx25821_dev *dev = chip->dev;
278
279 /* stop dma */
1a9fc855
MCC
280 cx_clear(AUD_INT_DMA_CTL,
281 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
02b20b0b
MCC
282
283 /* disable irqs */
284 cx_clear(PCI_INT_MSK, PCI_MSK_AUD_INT);
e5f10621
LF
285 cx_clear(AUD_A_INT_MSK, AUD_INT_OPC_ERR | AUD_INT_DN_SYNC |
286 AUD_INT_DN_RISCI2 | AUD_INT_DN_RISCI1);
02b20b0b
MCC
287
288 return 0;
289}
290
291#define MAX_IRQ_LOOP 50
292
293/*
294 * BOARD Specific: IRQ dma bits
295 */
296static char *cx25821_aud_irqs[32] = {
1a9fc855 297 "dn_risci1", "up_risci1", "rds_dn_risc1", /* 0-2 */
767f1d43 298 NULL, /* reserved */
1a9fc855 299 "dn_risci2", "up_risci2", "rds_dn_risc2", /* 4-6 */
767f1d43
LF
300 NULL, /* reserved */
301 "dnf_of", "upf_uf", "rds_dnf_uf", /* 8-10 */
302 NULL, /* reserved */
303 "dn_sync", "up_sync", "rds_dn_sync", /* 12-14 */
304 NULL, /* reserved */
305 "opc_err", "par_err", "rip_err", /* 16-18 */
306 "pci_abort", "ber_irq", "mchg_irq" /* 19-21 */
02b20b0b
MCC
307};
308
309/*
310 * BOARD Specific: Threats IRQ audio specific calls
311 */
8889a8a5
OP
312static void cx25821_aud_irq(struct cx25821_audio_dev *chip, u32 status,
313 u32 mask)
02b20b0b
MCC
314{
315 struct cx25821_dev *dev = chip->dev;
316
8889a8a5 317 if (0 == (status & mask))
02b20b0b 318 return;
02b20b0b
MCC
319
320 cx_write(AUD_A_INT_STAT, status);
1a9fc855 321 if (debug > 1 || (status & mask & ~0xff))
e5f10621
LF
322 cx25821_print_irqbits(dev->name, "irq aud", cx25821_aud_irqs,
323 ARRAY_SIZE(cx25821_aud_irqs), status, mask);
02b20b0b
MCC
324
325 /* risc op code error */
326 if (status & AUD_INT_OPC_ERR) {
36d89f7d 327 pr_warn("WARNING %s/1: Audio risc op code error\n", dev->name);
1a9fc855
MCC
328
329 cx_clear(AUD_INT_DMA_CTL,
330 FLD_AUD_DST_A_RISC_EN | FLD_AUD_DST_A_FIFO_EN);
331 cx25821_sram_channel_dump_audio(dev,
e5f10621 332 &cx25821_sram_channels[AUDIO_SRAM_CHANNEL]);
02b20b0b
MCC
333 }
334 if (status & AUD_INT_DN_SYNC) {
36d89f7d 335 pr_warn("WARNING %s: Downstream sync error!\n", dev->name);
02b20b0b
MCC
336 cx_write(AUD_A_GPCNT_CTL, GP_COUNT_CONTROL_RESET);
337 return;
338 }
339
02b20b0b
MCC
340 /* risc1 downstream */
341 if (status & AUD_INT_DN_RISCI1) {
342 atomic_set(&chip->count, cx_read(AUD_A_GPCNT));
343 snd_pcm_period_elapsed(chip->substream);
344 }
345}
346
02b20b0b
MCC
347/*
348 * BOARD Specific: Handles IRQ calls
349 */
350static irqreturn_t cx25821_irq(int irq, void *dev_id)
351{
8889a8a5 352 struct cx25821_audio_dev *chip = dev_id;
02b20b0b
MCC
353 struct cx25821_dev *dev = chip->dev;
354 u32 status, pci_status;
355 u32 audint_status, audint_mask;
356 int loop, handled = 0;
02b20b0b 357
02b20b0b 358 audint_status = cx_read(AUD_A_INT_STAT);
1a9fc855 359 audint_mask = cx_read(AUD_A_INT_MSK);
02b20b0b
MCC
360 status = cx_read(PCI_INT_STAT);
361
1a9fc855 362 for (loop = 0; loop < 1; loop++) {
02b20b0b 363 status = cx_read(PCI_INT_STAT);
1a9fc855 364 if (0 == status) {
02b20b0b
MCC
365 status = cx_read(PCI_INT_STAT);
366 audint_status = cx_read(AUD_A_INT_STAT);
367 audint_mask = cx_read(AUD_A_INT_MSK);
368
1a9fc855 369 if (status) {
02b20b0b
MCC
370 handled = 1;
371 cx_write(PCI_INT_STAT, status);
372
1a9fc855
MCC
373 cx25821_aud_irq(chip, audint_status,
374 audint_mask);
02b20b0b 375 break;
0ef80072 376 } else {
02b20b0b 377 goto out;
0ef80072 378 }
02b20b0b
MCC
379 }
380
381 handled = 1;
382 cx_write(PCI_INT_STAT, status);
383
384 cx25821_aud_irq(chip, audint_status, audint_mask);
385 }
386
387 pci_status = cx_read(PCI_INT_STAT);
388
389 if (handled)
390 cx_write(PCI_INT_STAT, pci_status);
391
8889a8a5 392out:
02b20b0b
MCC
393 return IRQ_RETVAL(handled);
394}
395
8889a8a5 396static int dsp_buffer_free(struct cx25821_audio_dev *chip)
02b20b0b 397{
5ede94c7
HV
398 struct cx25821_riscmem *risc = &chip->buf->risc;
399
02b20b0b
MCC
400 BUG_ON(!chip->dma_size);
401
1a9fc855 402 dprintk(2, "Freeing buffer\n");
8d8e6d60
HV
403 cx25821_alsa_dma_unmap(chip);
404 cx25821_alsa_dma_free(chip->buf);
5ede94c7 405 pci_free_consistent(chip->pci, risc->size, risc->cpu, risc->dma);
02b20b0b
MCC
406 kfree(chip->buf);
407
8d8e6d60 408 chip->buf = NULL;
02b20b0b
MCC
409 chip->dma_size = 0;
410
411 return 0;
412}
413
414/****************************************************************************
415 ALSA PCM Interface
416 ****************************************************************************/
417
418/*
419 * Digital hardware definition
420 */
421#define DEFAULT_FIFO_SIZE 384
47f14314 422static const struct snd_pcm_hardware snd_cx25821_digital_hw = {
e5f10621
LF
423 .info = SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
424 SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_MMAP_VALID,
02b20b0b
MCC
425 .formats = SNDRV_PCM_FMTBIT_S16_LE,
426
1a9fc855
MCC
427 .rates = SNDRV_PCM_RATE_48000,
428 .rate_min = 48000,
429 .rate_max = 48000,
02b20b0b
MCC
430 .channels_min = 2,
431 .channels_max = 2,
432 /* Analog audio output will be full of clicks and pops if there
433 are not exactly four lines in the SRAM FIFO buffer. */
1a9fc855
MCC
434 .period_bytes_min = DEFAULT_FIFO_SIZE / 3,
435 .period_bytes_max = DEFAULT_FIFO_SIZE / 3,
02b20b0b
MCC
436 .periods_min = 1,
437 .periods_max = AUDIO_LINE_SIZE,
8889a8a5
OP
438 /* 128 * 128 = 16384 = 1024 * 16 */
439 .buffer_bytes_max = (AUDIO_LINE_SIZE * AUDIO_LINE_SIZE),
02b20b0b
MCC
440};
441
02b20b0b
MCC
442/*
443 * audio pcm capture open callback
444 */
445static int snd_cx25821_pcm_open(struct snd_pcm_substream *substream)
446{
8889a8a5 447 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
02b20b0b
MCC
448 struct snd_pcm_runtime *runtime = substream->runtime;
449 int err;
450 unsigned int bpl = 0;
451
452 if (!chip) {
36d89f7d 453 pr_err("DEBUG: cx25821 can't find device struct. Can't proceed with open\n");
02b20b0b
MCC
454 return -ENODEV;
455 }
456
e5f10621
LF
457 err = snd_pcm_hw_constraint_pow2(runtime, 0,
458 SNDRV_PCM_HW_PARAM_PERIODS);
02b20b0b
MCC
459 if (err < 0)
460 goto _error;
461
462 chip->substream = substream;
463
464 runtime->hw = snd_cx25821_digital_hw;
465
1a9fc855
MCC
466 if (cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size !=
467 DEFAULT_FIFO_SIZE) {
8889a8a5
OP
468 /* since there are 3 audio Clusters */
469 bpl = cx25821_sram_channels[AUDIO_SRAM_CHANNEL].fifo_size / 3;
1a9fc855 470 bpl &= ~7; /* must be multiple of 8 */
02b20b0b 471
8889a8a5 472 if (bpl > AUDIO_LINE_SIZE)
02b20b0b 473 bpl = AUDIO_LINE_SIZE;
8889a8a5 474
02b20b0b
MCC
475 runtime->hw.period_bytes_min = bpl;
476 runtime->hw.period_bytes_max = bpl;
477 }
478
479 return 0;
8889a8a5 480_error:
1a9fc855 481 dprintk(1, "Error opening PCM!\n");
02b20b0b
MCC
482 return err;
483}
484
485/*
486 * audio close callback
487 */
488static int snd_cx25821_close(struct snd_pcm_substream *substream)
489{
490 return 0;
491}
492
493/*
494 * hw_params callback
495 */
1a9fc855
MCC
496static int snd_cx25821_hw_params(struct snd_pcm_substream *substream,
497 struct snd_pcm_hw_params *hw_params)
02b20b0b 498{
8889a8a5 499 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
fecfedeb 500 struct cx25821_audio_buffer *buf;
02b20b0b
MCC
501 int ret;
502
503 if (substream->runtime->dma_area) {
504 dsp_buffer_free(chip);
505 substream->runtime->dma_area = NULL;
506 }
507
02b20b0b
MCC
508 chip->period_size = params_period_bytes(hw_params);
509 chip->num_periods = params_periods(hw_params);
510 chip->dma_size = chip->period_size * params_periods(hw_params);
511
512 BUG_ON(!chip->dma_size);
1a9fc855 513 BUG_ON(chip->num_periods & (chip->num_periods - 1));
02b20b0b 514
fecfedeb 515 buf = kzalloc(sizeof(*buf), GFP_KERNEL);
02b20b0b
MCC
516 if (NULL == buf)
517 return -ENOMEM;
518
8889a8a5 519 if (chip->period_size > AUDIO_LINE_SIZE)
02b20b0b 520 chip->period_size = AUDIO_LINE_SIZE;
02b20b0b 521
1a9fc855 522 buf->bpl = chip->period_size;
8d8e6d60 523 chip->buf = buf;
02b20b0b 524
8d8e6d60 525 ret = cx25821_alsa_dma_init(chip,
e5f10621 526 (PAGE_ALIGN(chip->dma_size) >> PAGE_SHIFT));
02b20b0b
MCC
527 if (ret < 0)
528 goto error;
529
8d8e6d60 530 ret = cx25821_alsa_dma_map(chip);
02b20b0b
MCC
531 if (ret < 0)
532 goto error;
533
8d8e6d60 534 ret = cx25821_risc_databuffer_audio(chip->pci, &buf->risc, buf->sglist,
e5f10621 535 chip->period_size, chip->num_periods, 1);
1a9fc855 536 if (ret < 0) {
36d89f7d 537 pr_info("DEBUG: ERROR after cx25821_risc_databuffer_audio()\n");
02b20b0b
MCC
538 goto error;
539 }
540
02b20b0b 541 /* Loop back to start of program */
1a9fc855 542 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
02b20b0b 543 buf->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
1a9fc855 544 buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */
02b20b0b 545
8d8e6d60 546 substream->runtime->dma_area = chip->buf->vaddr;
02b20b0b
MCC
547 substream->runtime->dma_bytes = chip->dma_size;
548 substream->runtime->dma_addr = 0;
549
550 return 0;
551
8889a8a5 552error:
8d8e6d60 553 chip->buf = NULL;
02b20b0b
MCC
554 kfree(buf);
555 return ret;
556}
557
558/*
559 * hw free callback
560 */
1a9fc855 561static int snd_cx25821_hw_free(struct snd_pcm_substream *substream)
02b20b0b 562{
8889a8a5 563 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
02b20b0b
MCC
564
565 if (substream->runtime->dma_area) {
566 dsp_buffer_free(chip);
567 substream->runtime->dma_area = NULL;
568 }
569
570 return 0;
571}
572
573/*
574 * prepare callback
575 */
576static int snd_cx25821_prepare(struct snd_pcm_substream *substream)
577{
578 return 0;
579}
580
581/*
582 * trigger callback
583 */
1a9fc855
MCC
584static int snd_cx25821_card_trigger(struct snd_pcm_substream *substream,
585 int cmd)
02b20b0b 586{
8889a8a5 587 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
02b20b0b
MCC
588 int err = 0;
589
590 /* Local interrupts are already disabled by ALSA */
591 spin_lock(&chip->reg_lock);
592
1a9fc855
MCC
593 switch (cmd) {
594 case SNDRV_PCM_TRIGGER_START:
595 err = _cx25821_start_audio_dma(chip);
596 break;
597 case SNDRV_PCM_TRIGGER_STOP:
598 err = _cx25821_stop_audio_dma(chip);
599 break;
600 default:
601 err = -EINVAL;
602 break;
02b20b0b
MCC
603 }
604
605 spin_unlock(&chip->reg_lock);
606
607 return err;
608}
609
610/*
611 * pointer callback
612 */
1a9fc855
MCC
613static snd_pcm_uframes_t snd_cx25821_pointer(struct snd_pcm_substream
614 *substream)
02b20b0b 615{
8889a8a5 616 struct cx25821_audio_dev *chip = snd_pcm_substream_chip(substream);
02b20b0b
MCC
617 struct snd_pcm_runtime *runtime = substream->runtime;
618 u16 count;
619
620 count = atomic_read(&chip->count);
621
1a9fc855 622 return runtime->period_size * (count & (runtime->periods - 1));
02b20b0b
MCC
623}
624
625/*
626 * page callback (needed for mmap)
627 */
628static struct page *snd_cx25821_page(struct snd_pcm_substream *substream,
1a9fc855 629 unsigned long offset)
02b20b0b
MCC
630{
631 void *pageptr = substream->runtime->dma_area + offset;
632
633 return vmalloc_to_page(pageptr);
634}
635
636/*
637 * operators
638 */
5c8d8c01 639static const struct snd_pcm_ops snd_cx25821_pcm_ops = {
02b20b0b
MCC
640 .open = snd_cx25821_pcm_open,
641 .close = snd_cx25821_close,
02b20b0b
MCC
642 .hw_params = snd_cx25821_hw_params,
643 .hw_free = snd_cx25821_hw_free,
644 .prepare = snd_cx25821_prepare,
645 .trigger = snd_cx25821_card_trigger,
646 .pointer = snd_cx25821_pointer,
647 .page = snd_cx25821_page,
648};
649
02b20b0b 650/*
8889a8a5
OP
651 * ALSA create a PCM device: Called when initializing the board.
652 * Sets up the name and hooks up the callbacks
02b20b0b 653 */
8889a8a5
OP
654static int snd_cx25821_pcm(struct cx25821_audio_dev *chip, int device,
655 char *name)
02b20b0b
MCC
656{
657 struct snd_pcm *pcm;
658 int err;
659
660 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
1a9fc855 661 if (err < 0) {
36d89f7d 662 pr_info("ERROR: FAILED snd_pcm_new() in %s\n", __func__);
02b20b0b
MCC
663 return err;
664 }
665 pcm->private_data = chip;
666 pcm->info_flags = 0;
cc1e6315 667 strscpy(pcm->name, name, sizeof(pcm->name));
02b20b0b
MCC
668 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cx25821_pcm_ops);
669
670 return 0;
671}
672
02b20b0b
MCC
673/****************************************************************************
674 Basic Flow for Sound Devices
675 ****************************************************************************/
676
677/*
678 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
679 * Only boards with eeprom and byte 1 at eeprom=1 have it
680 */
681
114e877a 682static const struct pci_device_id __maybe_unused cx25821_audio_pci_tbl[] = {
1a9fc855
MCC
683 {0x14f1, 0x0920, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
684 {0,}
02b20b0b 685};
1a9fc855 686
02b20b0b
MCC
687MODULE_DEVICE_TABLE(pci, cx25821_audio_pci_tbl);
688
02b20b0b
MCC
689/*
690 * Alsa Constructor - Component probe
691 */
692static int cx25821_audio_initdev(struct cx25821_dev *dev)
693{
1a9fc855 694 struct snd_card *card;
8889a8a5 695 struct cx25821_audio_dev *chip;
1a9fc855 696 int err;
02b20b0b 697
1a9fc855 698 if (devno >= SNDRV_CARDS) {
36d89f7d 699 pr_info("DEBUG ERROR: devno >= SNDRV_CARDS %s\n", __func__);
8889a8a5 700 return -ENODEV;
02b20b0b
MCC
701 }
702
703 if (!enable[devno]) {
704 ++devno;
36d89f7d 705 pr_info("DEBUG ERROR: !enable[devno] %s\n", __func__);
8889a8a5 706 return -ENOENT;
02b20b0b
MCC
707 }
708
e7356888
TI
709 err = snd_card_new(&dev->pci->dev, index[devno], id[devno],
710 THIS_MODULE,
711 sizeof(struct cx25821_audio_dev), &card);
53e712d0 712 if (err < 0) {
36d89f7d
JP
713 pr_info("DEBUG ERROR: cannot create snd_card_new in %s\n",
714 __func__);
53e712d0 715 return err;
02b20b0b
MCC
716 }
717
cc1e6315 718 strscpy(card->driver, "cx25821", sizeof(card->driver));
02b20b0b
MCC
719
720 /* Card "creation" */
ba952d84 721 chip = card->private_data;
02b20b0b
MCC
722 spin_lock_init(&chip->reg_lock);
723
724 chip->dev = dev;
725 chip->card = card;
726 chip->pci = dev->pci;
727 chip->iobase = pci_resource_start(dev->pci, 0);
728
02b20b0b
MCC
729 chip->irq = dev->pci->irq;
730
731 err = request_irq(dev->pci->irq, cx25821_irq,
18e9351e 732 IRQF_SHARED, chip->dev->name, chip);
02b20b0b
MCC
733
734 if (err < 0) {
e5f10621
LF
735 pr_err("ERROR %s: can't get IRQ %d for ALSA\n", chip->dev->name,
736 dev->pci->irq);
02b20b0b
MCC
737 goto error;
738 }
739
629dcf23 740 err = snd_cx25821_pcm(chip, 0, "cx25821 Digital");
741 if (err < 0) {
36d89f7d
JP
742 pr_info("DEBUG ERROR: cannot create snd_cx25821_pcm %s\n",
743 __func__);
02b20b0b
MCC
744 goto error;
745 }
746
cc1e6315 747 strscpy(card->shortname, "cx25821", sizeof(card->shortname));
1a9fc855
MCC
748 sprintf(card->longname, "%s at 0x%lx irq %d", chip->dev->name,
749 chip->iobase, chip->irq);
cc1e6315 750 strscpy(card->mixername, "CX25821", sizeof(card->mixername));
02b20b0b 751
e5f10621
LF
752 pr_info("%s/%i: ALSA support for cx25821 boards\n", card->driver,
753 devno);
02b20b0b
MCC
754
755 err = snd_card_register(card);
1a9fc855 756 if (err < 0) {
36d89f7d
JP
757 pr_info("DEBUG ERROR: cannot register sound card %s\n",
758 __func__);
02b20b0b
MCC
759 goto error;
760 }
761
a8f35ce3 762 dev->card = card;
02b20b0b
MCC
763 devno++;
764 return 0;
765
8889a8a5 766error:
02b20b0b
MCC
767 snd_card_free(card);
768 return err;
769}
770
02b20b0b
MCC
771/****************************************************************************
772 LINUX MODULE INIT
773 ****************************************************************************/
a8f35ce3
HV
774
775static int cx25821_alsa_exit_callback(struct device *dev, void *data)
776{
777 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
778 struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
779
780 snd_card_free(cxdev->card);
781 return 0;
782}
783
02b20b0b
MCC
784static void cx25821_audio_fini(void)
785{
a8f35ce3
HV
786 struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
787 int ret;
788
789 ret = driver_for_each_device(drv, NULL, NULL, cx25821_alsa_exit_callback);
f327cabf
MCC
790 if (ret)
791 pr_err("%s failed to find a cx25821 driver.\n", __func__);
a8f35ce3
HV
792}
793
794static int cx25821_alsa_init_callback(struct device *dev, void *data)
795{
796 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
797 struct cx25821_dev *cxdev = get_cx25821(v4l2_dev);
798
799 cx25821_audio_initdev(cxdev);
800 return 0;
02b20b0b
MCC
801}
802
803/*
804 * Module initializer
805 *
806 * Loops through present saa7134 cards, and assigns an ALSA device
807 * to each one
808 *
809 */
810static int cx25821_alsa_init(void)
811{
a8f35ce3 812 struct device_driver *drv = driver_find("cx25821", &pci_bus_type);
02b20b0b 813
a8f35ce3 814 return driver_for_each_device(drv, NULL, NULL, cx25821_alsa_init_callback);
02b20b0b
MCC
815
816}
817
818late_initcall(cx25821_alsa_init);
819module_exit(cx25821_audio_fini);