150acfb2bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b285192aSMauro Carvalho Chehab /*
3b285192aSMauro Carvalho Chehab * SAA713x ALSA support for V4L
4b285192aSMauro Carvalho Chehab */
5b285192aSMauro Carvalho Chehab
69a12ccfcSMauro Carvalho Chehab #include "saa7134.h"
79a12ccfcSMauro Carvalho Chehab #include "saa7134-reg.h"
89a12ccfcSMauro Carvalho Chehab
9b285192aSMauro Carvalho Chehab #include <linux/init.h>
10b285192aSMauro Carvalho Chehab #include <linux/slab.h>
11b285192aSMauro Carvalho Chehab #include <linux/time.h>
12b285192aSMauro Carvalho Chehab #include <linux/wait.h>
13b285192aSMauro Carvalho Chehab #include <linux/module.h>
14b285192aSMauro Carvalho Chehab #include <sound/core.h>
15b285192aSMauro Carvalho Chehab #include <sound/control.h>
16b285192aSMauro Carvalho Chehab #include <sound/pcm.h>
17b285192aSMauro Carvalho Chehab #include <sound/pcm_params.h>
18b285192aSMauro Carvalho Chehab #include <sound/initval.h>
19b285192aSMauro Carvalho Chehab #include <linux/interrupt.h>
20ba0d342eSMauro Carvalho Chehab #include <linux/vmalloc.h>
21b285192aSMauro Carvalho Chehab
22b285192aSMauro Carvalho Chehab /*
23b285192aSMauro Carvalho Chehab * Configuration macros
24b285192aSMauro Carvalho Chehab */
25b285192aSMauro Carvalho Chehab
26b285192aSMauro Carvalho Chehab /* defaults */
27b285192aSMauro Carvalho Chehab #define MIXER_ADDR_UNSELECTED -1
28b285192aSMauro Carvalho Chehab #define MIXER_ADDR_TVTUNER 0
29b285192aSMauro Carvalho Chehab #define MIXER_ADDR_LINE1 1
30b285192aSMauro Carvalho Chehab #define MIXER_ADDR_LINE2 2
31b285192aSMauro Carvalho Chehab #define MIXER_ADDR_LAST 2
32b285192aSMauro Carvalho Chehab
33b285192aSMauro Carvalho Chehab
34b285192aSMauro Carvalho Chehab static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
35b285192aSMauro Carvalho Chehab static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
36b285192aSMauro Carvalho Chehab static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
37b285192aSMauro Carvalho Chehab
38b285192aSMauro Carvalho Chehab module_param_array(index, int, NULL, 0444);
39b285192aSMauro Carvalho Chehab module_param_array(enable, int, NULL, 0444);
40b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
41b285192aSMauro Carvalho Chehab MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
42b285192aSMauro Carvalho Chehab
43b285192aSMauro Carvalho Chehab /*
44b285192aSMauro Carvalho Chehab * Main chip structure
45b285192aSMauro Carvalho Chehab */
46b285192aSMauro Carvalho Chehab
47b285192aSMauro Carvalho Chehab typedef struct snd_card_saa7134 {
48b285192aSMauro Carvalho Chehab struct snd_card *card;
49b285192aSMauro Carvalho Chehab spinlock_t mixer_lock;
50b285192aSMauro Carvalho Chehab int mixer_volume[MIXER_ADDR_LAST+1][2];
51b285192aSMauro Carvalho Chehab int capture_source_addr;
52b285192aSMauro Carvalho Chehab int capture_source[2];
53b285192aSMauro Carvalho Chehab struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
54b285192aSMauro Carvalho Chehab struct pci_dev *pci;
55b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
56b285192aSMauro Carvalho Chehab
57b285192aSMauro Carvalho Chehab unsigned long iobase;
58b285192aSMauro Carvalho Chehab s16 irq;
59b285192aSMauro Carvalho Chehab u16 mute_was_on;
60b285192aSMauro Carvalho Chehab
61b285192aSMauro Carvalho Chehab spinlock_t lock;
62b285192aSMauro Carvalho Chehab } snd_card_saa7134_t;
63b285192aSMauro Carvalho Chehab
64b285192aSMauro Carvalho Chehab
65b285192aSMauro Carvalho Chehab /*
66b285192aSMauro Carvalho Chehab * PCM structure
67b285192aSMauro Carvalho Chehab */
68b285192aSMauro Carvalho Chehab
69b285192aSMauro Carvalho Chehab typedef struct snd_card_saa7134_pcm {
70b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
71b285192aSMauro Carvalho Chehab
72b285192aSMauro Carvalho Chehab spinlock_t lock;
73b285192aSMauro Carvalho Chehab
74b285192aSMauro Carvalho Chehab struct snd_pcm_substream *substream;
75b285192aSMauro Carvalho Chehab } snd_card_saa7134_pcm_t;
76b285192aSMauro Carvalho Chehab
77b285192aSMauro Carvalho Chehab static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
78b285192aSMauro Carvalho Chehab
79b285192aSMauro Carvalho Chehab
80b285192aSMauro Carvalho Chehab /*
81b285192aSMauro Carvalho Chehab * saa7134 DMA audio stop
82b285192aSMauro Carvalho Chehab *
83b285192aSMauro Carvalho Chehab * Called when the capture device is released or the buffer overflows
84b285192aSMauro Carvalho Chehab *
85b285192aSMauro Carvalho Chehab * - Copied verbatim from saa7134-oss's dsp_dma_stop.
86b285192aSMauro Carvalho Chehab *
87b285192aSMauro Carvalho Chehab */
88b285192aSMauro Carvalho Chehab
saa7134_dma_stop(struct saa7134_dev * dev)89b285192aSMauro Carvalho Chehab static void saa7134_dma_stop(struct saa7134_dev *dev)
90b285192aSMauro Carvalho Chehab {
91b285192aSMauro Carvalho Chehab dev->dmasound.dma_blk = -1;
92b285192aSMauro Carvalho Chehab dev->dmasound.dma_running = 0;
93b285192aSMauro Carvalho Chehab saa7134_set_dmabits(dev);
94b285192aSMauro Carvalho Chehab }
95b285192aSMauro Carvalho Chehab
96b285192aSMauro Carvalho Chehab /*
97b285192aSMauro Carvalho Chehab * saa7134 DMA audio start
98b285192aSMauro Carvalho Chehab *
99b285192aSMauro Carvalho Chehab * Called when preparing the capture device for use
100b285192aSMauro Carvalho Chehab *
101b285192aSMauro Carvalho Chehab * - Copied verbatim from saa7134-oss's dsp_dma_start.
102b285192aSMauro Carvalho Chehab *
103b285192aSMauro Carvalho Chehab */
104b285192aSMauro Carvalho Chehab
saa7134_dma_start(struct saa7134_dev * dev)105b285192aSMauro Carvalho Chehab static void saa7134_dma_start(struct saa7134_dev *dev)
106b285192aSMauro Carvalho Chehab {
107b285192aSMauro Carvalho Chehab dev->dmasound.dma_blk = 0;
108b285192aSMauro Carvalho Chehab dev->dmasound.dma_running = 1;
109b285192aSMauro Carvalho Chehab saa7134_set_dmabits(dev);
110b285192aSMauro Carvalho Chehab }
111b285192aSMauro Carvalho Chehab
112b285192aSMauro Carvalho Chehab /*
113b285192aSMauro Carvalho Chehab * saa7134 audio DMA IRQ handler
114b285192aSMauro Carvalho Chehab *
115b285192aSMauro Carvalho Chehab * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
116b285192aSMauro Carvalho Chehab * Handles shifting between the 2 buffers, manages the read counters,
117b285192aSMauro Carvalho Chehab * and notifies ALSA when periods elapse
118b285192aSMauro Carvalho Chehab *
119b285192aSMauro Carvalho Chehab * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
120b285192aSMauro Carvalho Chehab *
121b285192aSMauro Carvalho Chehab */
122b285192aSMauro Carvalho Chehab
saa7134_irq_alsa_done(struct saa7134_dev * dev,unsigned long status)123b285192aSMauro Carvalho Chehab static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
124b285192aSMauro Carvalho Chehab unsigned long status)
125b285192aSMauro Carvalho Chehab {
126b285192aSMauro Carvalho Chehab int next_blk, reg = 0;
127b285192aSMauro Carvalho Chehab
128b285192aSMauro Carvalho Chehab spin_lock(&dev->slock);
129b285192aSMauro Carvalho Chehab if (UNSET == dev->dmasound.dma_blk) {
1307592eefcSMauro Carvalho Chehab pr_debug("irq: recording stopped\n");
131b285192aSMauro Carvalho Chehab goto done;
132b285192aSMauro Carvalho Chehab }
133b285192aSMauro Carvalho Chehab if (0 != (status & 0x0f000000))
1347592eefcSMauro Carvalho Chehab pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
135b285192aSMauro Carvalho Chehab if (0 == (status & 0x10000000)) {
136b285192aSMauro Carvalho Chehab /* odd */
137b285192aSMauro Carvalho Chehab if (0 == (dev->dmasound.dma_blk & 0x01))
138b285192aSMauro Carvalho Chehab reg = SAA7134_RS_BA1(6);
139b285192aSMauro Carvalho Chehab } else {
140b285192aSMauro Carvalho Chehab /* even */
141b285192aSMauro Carvalho Chehab if (1 == (dev->dmasound.dma_blk & 0x01))
142b285192aSMauro Carvalho Chehab reg = SAA7134_RS_BA2(6);
143b285192aSMauro Carvalho Chehab }
144b285192aSMauro Carvalho Chehab if (0 == reg) {
1457592eefcSMauro Carvalho Chehab pr_debug("irq: field oops [%s]\n",
146b285192aSMauro Carvalho Chehab (status & 0x10000000) ? "even" : "odd");
147b285192aSMauro Carvalho Chehab goto done;
148b285192aSMauro Carvalho Chehab }
149b285192aSMauro Carvalho Chehab
150b285192aSMauro Carvalho Chehab if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
1516139ebc6SMauro Carvalho Chehab pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",
1526139ebc6SMauro Carvalho Chehab dev->dmasound.read_count,
153b285192aSMauro Carvalho Chehab dev->dmasound.bufsize, dev->dmasound.blocks);
154b285192aSMauro Carvalho Chehab spin_unlock(&dev->slock);
1551fb8510cSTakashi Iwai snd_pcm_stop_xrun(dev->dmasound.substream);
156b285192aSMauro Carvalho Chehab return;
157b285192aSMauro Carvalho Chehab }
158b285192aSMauro Carvalho Chehab
159b285192aSMauro Carvalho Chehab /* next block addr */
160b285192aSMauro Carvalho Chehab next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
161b285192aSMauro Carvalho Chehab saa_writel(reg,next_blk * dev->dmasound.blksize);
1627592eefcSMauro Carvalho Chehab pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
163b285192aSMauro Carvalho Chehab (status & 0x10000000) ? "even" : "odd ", next_blk,
1646139ebc6SMauro Carvalho Chehab next_blk * dev->dmasound.blksize, dev->dmasound.blocks,
1656139ebc6SMauro Carvalho Chehab dev->dmasound.blksize, dev->dmasound.read_count);
166b285192aSMauro Carvalho Chehab
167b285192aSMauro Carvalho Chehab /* update status & wake waiting readers */
168b285192aSMauro Carvalho Chehab dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
169b285192aSMauro Carvalho Chehab dev->dmasound.read_count += dev->dmasound.blksize;
170b285192aSMauro Carvalho Chehab
171b285192aSMauro Carvalho Chehab dev->dmasound.recording_on = reg;
172b285192aSMauro Carvalho Chehab
173b285192aSMauro Carvalho Chehab if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
174b285192aSMauro Carvalho Chehab spin_unlock(&dev->slock);
175b285192aSMauro Carvalho Chehab snd_pcm_period_elapsed(dev->dmasound.substream);
176b285192aSMauro Carvalho Chehab spin_lock(&dev->slock);
177b285192aSMauro Carvalho Chehab }
178b285192aSMauro Carvalho Chehab
179b285192aSMauro Carvalho Chehab done:
180b285192aSMauro Carvalho Chehab spin_unlock(&dev->slock);
181b285192aSMauro Carvalho Chehab
182b285192aSMauro Carvalho Chehab }
183b285192aSMauro Carvalho Chehab
184b285192aSMauro Carvalho Chehab /*
185b285192aSMauro Carvalho Chehab * IRQ request handler
186b285192aSMauro Carvalho Chehab *
187b285192aSMauro Carvalho Chehab * Runs along with saa7134's IRQ handler, discards anything that isn't
188b285192aSMauro Carvalho Chehab * DMA sound
189b285192aSMauro Carvalho Chehab *
190b285192aSMauro Carvalho Chehab */
191b285192aSMauro Carvalho Chehab
saa7134_alsa_irq(int irq,void * dev_id)192b285192aSMauro Carvalho Chehab static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
193b285192aSMauro Carvalho Chehab {
194b285192aSMauro Carvalho Chehab struct saa7134_dmasound *dmasound = dev_id;
195b285192aSMauro Carvalho Chehab struct saa7134_dev *dev = dmasound->priv_data;
196b285192aSMauro Carvalho Chehab
197b285192aSMauro Carvalho Chehab unsigned long report, status;
198b285192aSMauro Carvalho Chehab int loop, handled = 0;
199b285192aSMauro Carvalho Chehab
200b285192aSMauro Carvalho Chehab for (loop = 0; loop < 10; loop++) {
201b285192aSMauro Carvalho Chehab report = saa_readl(SAA7134_IRQ_REPORT);
202b285192aSMauro Carvalho Chehab status = saa_readl(SAA7134_IRQ_STATUS);
203b285192aSMauro Carvalho Chehab
204b285192aSMauro Carvalho Chehab if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
205b285192aSMauro Carvalho Chehab handled = 1;
206b285192aSMauro Carvalho Chehab saa_writel(SAA7134_IRQ_REPORT,
207b285192aSMauro Carvalho Chehab SAA7134_IRQ_REPORT_DONE_RA3);
208b285192aSMauro Carvalho Chehab saa7134_irq_alsa_done(dev, status);
209b285192aSMauro Carvalho Chehab } else {
210b285192aSMauro Carvalho Chehab goto out;
211b285192aSMauro Carvalho Chehab }
212b285192aSMauro Carvalho Chehab }
213b285192aSMauro Carvalho Chehab
214b285192aSMauro Carvalho Chehab if (loop == 10) {
2157592eefcSMauro Carvalho Chehab pr_debug("error! looping IRQ!");
216b285192aSMauro Carvalho Chehab }
217b285192aSMauro Carvalho Chehab
218b285192aSMauro Carvalho Chehab out:
219b285192aSMauro Carvalho Chehab return IRQ_RETVAL(handled);
220b285192aSMauro Carvalho Chehab }
221b285192aSMauro Carvalho Chehab
222b285192aSMauro Carvalho Chehab /*
223b285192aSMauro Carvalho Chehab * ALSA capture trigger
224b285192aSMauro Carvalho Chehab *
225b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
226b285192aSMauro Carvalho Chehab *
227b285192aSMauro Carvalho Chehab * Called whenever a capture is started or stopped. Must be defined,
228b285192aSMauro Carvalho Chehab * but there's nothing we want to do here
229b285192aSMauro Carvalho Chehab *
230b285192aSMauro Carvalho Chehab */
231b285192aSMauro Carvalho Chehab
snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,int cmd)232b285192aSMauro Carvalho Chehab static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
233b285192aSMauro Carvalho Chehab int cmd)
234b285192aSMauro Carvalho Chehab {
235b285192aSMauro Carvalho Chehab struct snd_pcm_runtime *runtime = substream->runtime;
236b285192aSMauro Carvalho Chehab snd_card_saa7134_pcm_t *pcm = runtime->private_data;
237b285192aSMauro Carvalho Chehab struct saa7134_dev *dev=pcm->dev;
238b285192aSMauro Carvalho Chehab int err = 0;
239b285192aSMauro Carvalho Chehab
240b285192aSMauro Carvalho Chehab spin_lock(&dev->slock);
241b285192aSMauro Carvalho Chehab if (cmd == SNDRV_PCM_TRIGGER_START) {
242b285192aSMauro Carvalho Chehab /* start dma */
243b285192aSMauro Carvalho Chehab saa7134_dma_start(dev);
244b285192aSMauro Carvalho Chehab } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
245b285192aSMauro Carvalho Chehab /* stop dma */
246b285192aSMauro Carvalho Chehab saa7134_dma_stop(dev);
247b285192aSMauro Carvalho Chehab } else {
248b285192aSMauro Carvalho Chehab err = -EINVAL;
249b285192aSMauro Carvalho Chehab }
250b285192aSMauro Carvalho Chehab spin_unlock(&dev->slock);
251b285192aSMauro Carvalho Chehab
252b285192aSMauro Carvalho Chehab return err;
253b285192aSMauro Carvalho Chehab }
254b285192aSMauro Carvalho Chehab
saa7134_alsa_dma_init(struct saa7134_dev * dev,unsigned long nr_pages)255c91effc0SMauro Carvalho Chehab static int saa7134_alsa_dma_init(struct saa7134_dev *dev,
256c91effc0SMauro Carvalho Chehab unsigned long nr_pages)
25715e64f0dSHans Verkuil {
25815e64f0dSHans Verkuil struct saa7134_dmasound *dma = &dev->dmasound;
25915e64f0dSHans Verkuil struct page *pg;
26015e64f0dSHans Verkuil int i;
26115e64f0dSHans Verkuil
26215e64f0dSHans Verkuil dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
26315e64f0dSHans Verkuil if (NULL == dma->vaddr) {
264c91effc0SMauro Carvalho Chehab pr_debug("vmalloc_32(%lu pages) failed\n", nr_pages);
26515e64f0dSHans Verkuil return -ENOMEM;
26615e64f0dSHans Verkuil }
26715e64f0dSHans Verkuil
268c91effc0SMauro Carvalho Chehab pr_debug("vmalloc is at addr %p, size=%lu\n",
2696062ba61SMauro Carvalho Chehab dma->vaddr, nr_pages << PAGE_SHIFT);
27015e64f0dSHans Verkuil
27115e64f0dSHans Verkuil memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
27215e64f0dSHans Verkuil dma->nr_pages = nr_pages;
27315e64f0dSHans Verkuil
274fad953ceSKees Cook dma->sglist = vzalloc(array_size(sizeof(*dma->sglist), dma->nr_pages));
27515e64f0dSHans Verkuil if (NULL == dma->sglist)
27615e64f0dSHans Verkuil goto vzalloc_err;
27715e64f0dSHans Verkuil
27815e64f0dSHans Verkuil sg_init_table(dma->sglist, dma->nr_pages);
27915e64f0dSHans Verkuil for (i = 0; i < dma->nr_pages; i++) {
28015e64f0dSHans Verkuil pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
28115e64f0dSHans Verkuil if (NULL == pg)
28215e64f0dSHans Verkuil goto vmalloc_to_page_err;
28315e64f0dSHans Verkuil sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
28415e64f0dSHans Verkuil }
28515e64f0dSHans Verkuil return 0;
28615e64f0dSHans Verkuil
28715e64f0dSHans Verkuil vmalloc_to_page_err:
28815e64f0dSHans Verkuil vfree(dma->sglist);
28915e64f0dSHans Verkuil dma->sglist = NULL;
29015e64f0dSHans Verkuil vzalloc_err:
29115e64f0dSHans Verkuil vfree(dma->vaddr);
29215e64f0dSHans Verkuil dma->vaddr = NULL;
29315e64f0dSHans Verkuil return -ENOMEM;
29415e64f0dSHans Verkuil }
29515e64f0dSHans Verkuil
saa7134_alsa_dma_map(struct saa7134_dev * dev)29615e64f0dSHans Verkuil static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
29715e64f0dSHans Verkuil {
29815e64f0dSHans Verkuil struct saa7134_dmasound *dma = &dev->dmasound;
29915e64f0dSHans Verkuil
30015e64f0dSHans Verkuil dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
301f95fc014SMarek Szyprowski dma->nr_pages, DMA_FROM_DEVICE);
30215e64f0dSHans Verkuil
30315e64f0dSHans Verkuil if (0 == dma->sglen) {
30415e64f0dSHans Verkuil pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
30515e64f0dSHans Verkuil return -ENOMEM;
30615e64f0dSHans Verkuil }
30715e64f0dSHans Verkuil return 0;
30815e64f0dSHans Verkuil }
30915e64f0dSHans Verkuil
saa7134_alsa_dma_unmap(struct saa7134_dev * dev)31015e64f0dSHans Verkuil static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
31115e64f0dSHans Verkuil {
31215e64f0dSHans Verkuil struct saa7134_dmasound *dma = &dev->dmasound;
31315e64f0dSHans Verkuil
31415e64f0dSHans Verkuil if (!dma->sglen)
31515e64f0dSHans Verkuil return 0;
31615e64f0dSHans Verkuil
317f95fc014SMarek Szyprowski dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->nr_pages, DMA_FROM_DEVICE);
31815e64f0dSHans Verkuil dma->sglen = 0;
31915e64f0dSHans Verkuil return 0;
32015e64f0dSHans Verkuil }
32115e64f0dSHans Verkuil
saa7134_alsa_dma_free(struct saa7134_dmasound * dma)32215e64f0dSHans Verkuil static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
32315e64f0dSHans Verkuil {
32415e64f0dSHans Verkuil vfree(dma->sglist);
32515e64f0dSHans Verkuil dma->sglist = NULL;
32615e64f0dSHans Verkuil vfree(dma->vaddr);
32715e64f0dSHans Verkuil dma->vaddr = NULL;
32815e64f0dSHans Verkuil return 0;
32915e64f0dSHans Verkuil }
33015e64f0dSHans Verkuil
331b285192aSMauro Carvalho Chehab /*
332b285192aSMauro Carvalho Chehab * DMA buffer initialization
333b285192aSMauro Carvalho Chehab *
334b285192aSMauro Carvalho Chehab * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
335b285192aSMauro Carvalho Chehab * ALSA, but I was unable to use ALSA's own DMA, and had to force the
336b285192aSMauro Carvalho Chehab * usage of V4L's
337b285192aSMauro Carvalho Chehab *
338b285192aSMauro Carvalho Chehab * - Copied verbatim from saa7134-oss.
339b285192aSMauro Carvalho Chehab *
340b285192aSMauro Carvalho Chehab */
341b285192aSMauro Carvalho Chehab
dsp_buffer_init(struct saa7134_dev * dev)342b285192aSMauro Carvalho Chehab static int dsp_buffer_init(struct saa7134_dev *dev)
343b285192aSMauro Carvalho Chehab {
344b285192aSMauro Carvalho Chehab int err;
345b285192aSMauro Carvalho Chehab
346b285192aSMauro Carvalho Chehab BUG_ON(!dev->dmasound.bufsize);
347b285192aSMauro Carvalho Chehab
34815e64f0dSHans Verkuil err = saa7134_alsa_dma_init(dev,
349b285192aSMauro Carvalho Chehab (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
350b285192aSMauro Carvalho Chehab if (0 != err)
351b285192aSMauro Carvalho Chehab return err;
352b285192aSMauro Carvalho Chehab return 0;
353b285192aSMauro Carvalho Chehab }
354b285192aSMauro Carvalho Chehab
355b285192aSMauro Carvalho Chehab /*
356b285192aSMauro Carvalho Chehab * DMA buffer release
357b285192aSMauro Carvalho Chehab *
358b285192aSMauro Carvalho Chehab * Called after closing the device, during snd_card_saa7134_capture_close
359b285192aSMauro Carvalho Chehab *
360b285192aSMauro Carvalho Chehab */
361b285192aSMauro Carvalho Chehab
dsp_buffer_free(struct saa7134_dev * dev)362b285192aSMauro Carvalho Chehab static int dsp_buffer_free(struct saa7134_dev *dev)
363b285192aSMauro Carvalho Chehab {
364b285192aSMauro Carvalho Chehab BUG_ON(!dev->dmasound.blksize);
365b285192aSMauro Carvalho Chehab
36615e64f0dSHans Verkuil saa7134_alsa_dma_free(&dev->dmasound);
367b285192aSMauro Carvalho Chehab
368b285192aSMauro Carvalho Chehab dev->dmasound.blocks = 0;
369b285192aSMauro Carvalho Chehab dev->dmasound.blksize = 0;
370b285192aSMauro Carvalho Chehab dev->dmasound.bufsize = 0;
371b285192aSMauro Carvalho Chehab
372b285192aSMauro Carvalho Chehab return 0;
373b285192aSMauro Carvalho Chehab }
374b285192aSMauro Carvalho Chehab
375b285192aSMauro Carvalho Chehab /*
376b285192aSMauro Carvalho Chehab * Setting the capture source and updating the ALSA controls
377b285192aSMauro Carvalho Chehab */
snd_saa7134_capsrc_set(struct snd_kcontrol * kcontrol,int left,int right,bool force_notify)378b285192aSMauro Carvalho Chehab static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
379b285192aSMauro Carvalho Chehab int left, int right, bool force_notify)
380b285192aSMauro Carvalho Chehab {
381b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
382b285192aSMauro Carvalho Chehab int change = 0, addr = kcontrol->private_value;
383b285192aSMauro Carvalho Chehab int active, old_addr;
384b285192aSMauro Carvalho Chehab u32 anabar, xbarin;
385b285192aSMauro Carvalho Chehab int analog_io, rate;
386b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
387b285192aSMauro Carvalho Chehab
388b285192aSMauro Carvalho Chehab dev = chip->dev;
389b285192aSMauro Carvalho Chehab
390b285192aSMauro Carvalho Chehab spin_lock_irq(&chip->mixer_lock);
391b285192aSMauro Carvalho Chehab
392b285192aSMauro Carvalho Chehab active = left != 0 || right != 0;
393b285192aSMauro Carvalho Chehab old_addr = chip->capture_source_addr;
394b285192aSMauro Carvalho Chehab
395b285192aSMauro Carvalho Chehab /* The active capture source cannot be deactivated */
396b285192aSMauro Carvalho Chehab if (active) {
397b285192aSMauro Carvalho Chehab change = old_addr != addr ||
398b285192aSMauro Carvalho Chehab chip->capture_source[0] != left ||
399b285192aSMauro Carvalho Chehab chip->capture_source[1] != right;
400b285192aSMauro Carvalho Chehab
401b285192aSMauro Carvalho Chehab chip->capture_source[0] = left;
402b285192aSMauro Carvalho Chehab chip->capture_source[1] = right;
403b285192aSMauro Carvalho Chehab chip->capture_source_addr = addr;
404b285192aSMauro Carvalho Chehab dev->dmasound.input = addr;
405b285192aSMauro Carvalho Chehab }
406b285192aSMauro Carvalho Chehab spin_unlock_irq(&chip->mixer_lock);
407b285192aSMauro Carvalho Chehab
408b285192aSMauro Carvalho Chehab if (change) {
409b285192aSMauro Carvalho Chehab switch (dev->pci->device) {
410b285192aSMauro Carvalho Chehab
411b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7134:
412b285192aSMauro Carvalho Chehab switch (addr) {
413b285192aSMauro Carvalho Chehab case MIXER_ADDR_TVTUNER:
414b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
415b285192aSMauro Carvalho Chehab 0xc0, 0xc0);
416b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
417b285192aSMauro Carvalho Chehab 0x03, 0x00);
418b285192aSMauro Carvalho Chehab break;
419b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE1:
420b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE2:
421b285192aSMauro Carvalho Chehab analog_io = (MIXER_ADDR_LINE1 == addr) ?
422b285192aSMauro Carvalho Chehab 0x00 : 0x08;
423b285192aSMauro Carvalho Chehab rate = (32000 == dev->dmasound.rate) ?
424b285192aSMauro Carvalho Chehab 0x01 : 0x03;
425b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_ANALOG_IO_SELECT,
426b285192aSMauro Carvalho Chehab 0x08, analog_io);
427b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
428b285192aSMauro Carvalho Chehab 0xc0, 0x80);
429b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
430b285192aSMauro Carvalho Chehab 0x03, rate);
431b285192aSMauro Carvalho Chehab break;
432b285192aSMauro Carvalho Chehab }
433b285192aSMauro Carvalho Chehab
434b285192aSMauro Carvalho Chehab break;
435b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7133:
436b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7135:
437b285192aSMauro Carvalho Chehab xbarin = 0x03; /* adc */
438b285192aSMauro Carvalho Chehab anabar = 0;
439b285192aSMauro Carvalho Chehab switch (addr) {
440b285192aSMauro Carvalho Chehab case MIXER_ADDR_TVTUNER:
441b285192aSMauro Carvalho Chehab xbarin = 0; /* Demodulator */
442b285192aSMauro Carvalho Chehab anabar = 2; /* DACs */
443b285192aSMauro Carvalho Chehab break;
444b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE1:
445b285192aSMauro Carvalho Chehab anabar = 0; /* aux1, aux1 */
446b285192aSMauro Carvalho Chehab break;
447b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE2:
448b285192aSMauro Carvalho Chehab anabar = 9; /* aux2, aux2 */
449b285192aSMauro Carvalho Chehab break;
450b285192aSMauro Carvalho Chehab }
451b285192aSMauro Carvalho Chehab
452b285192aSMauro Carvalho Chehab /* output xbar always main channel */
453b285192aSMauro Carvalho Chehab saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
454b285192aSMauro Carvalho Chehab 0xbbbb10);
455b285192aSMauro Carvalho Chehab
456b285192aSMauro Carvalho Chehab if (left || right) {
457b285192aSMauro Carvalho Chehab /* We've got data, turn the input on */
458b285192aSMauro Carvalho Chehab saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
459b285192aSMauro Carvalho Chehab xbarin);
460b285192aSMauro Carvalho Chehab saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
461b285192aSMauro Carvalho Chehab } else {
462b285192aSMauro Carvalho Chehab saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
463b285192aSMauro Carvalho Chehab 0);
464b285192aSMauro Carvalho Chehab saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
465b285192aSMauro Carvalho Chehab }
466b285192aSMauro Carvalho Chehab break;
467b285192aSMauro Carvalho Chehab }
468b285192aSMauro Carvalho Chehab }
469b285192aSMauro Carvalho Chehab
470b285192aSMauro Carvalho Chehab if (change) {
471b285192aSMauro Carvalho Chehab if (force_notify)
472b285192aSMauro Carvalho Chehab snd_ctl_notify(chip->card,
473b285192aSMauro Carvalho Chehab SNDRV_CTL_EVENT_MASK_VALUE,
474b285192aSMauro Carvalho Chehab &chip->capture_ctl[addr]->id);
475b285192aSMauro Carvalho Chehab
476b285192aSMauro Carvalho Chehab if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
477b285192aSMauro Carvalho Chehab snd_ctl_notify(chip->card,
478b285192aSMauro Carvalho Chehab SNDRV_CTL_EVENT_MASK_VALUE,
479b285192aSMauro Carvalho Chehab &chip->capture_ctl[old_addr]->id);
480b285192aSMauro Carvalho Chehab }
481b285192aSMauro Carvalho Chehab
482b285192aSMauro Carvalho Chehab return change;
483b285192aSMauro Carvalho Chehab }
484b285192aSMauro Carvalho Chehab
485b285192aSMauro Carvalho Chehab /*
486b285192aSMauro Carvalho Chehab * ALSA PCM preparation
487b285192aSMauro Carvalho Chehab *
488b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
489b285192aSMauro Carvalho Chehab *
490b285192aSMauro Carvalho Chehab * Called right after the capture device is opened, this function configures
491b285192aSMauro Carvalho Chehab * the buffer using the previously defined functions, allocates the memory,
492b285192aSMauro Carvalho Chehab * sets up the hardware registers, and then starts the DMA. When this function
493b285192aSMauro Carvalho Chehab * returns, the audio should be flowing.
494b285192aSMauro Carvalho Chehab *
495b285192aSMauro Carvalho Chehab */
496b285192aSMauro Carvalho Chehab
snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)497b285192aSMauro Carvalho Chehab static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
498b285192aSMauro Carvalho Chehab {
499b285192aSMauro Carvalho Chehab struct snd_pcm_runtime *runtime = substream->runtime;
500b285192aSMauro Carvalho Chehab int bswap, sign;
501b285192aSMauro Carvalho Chehab u32 fmt, control;
502b285192aSMauro Carvalho Chehab snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
503b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
504b285192aSMauro Carvalho Chehab snd_card_saa7134_pcm_t *pcm = runtime->private_data;
505b285192aSMauro Carvalho Chehab
506b285192aSMauro Carvalho Chehab pcm->dev->dmasound.substream = substream;
507b285192aSMauro Carvalho Chehab
508b285192aSMauro Carvalho Chehab dev = saa7134->dev;
509b285192aSMauro Carvalho Chehab
510b285192aSMauro Carvalho Chehab if (snd_pcm_format_width(runtime->format) == 8)
511b285192aSMauro Carvalho Chehab fmt = 0x00;
512b285192aSMauro Carvalho Chehab else
513b285192aSMauro Carvalho Chehab fmt = 0x01;
514b285192aSMauro Carvalho Chehab
515b285192aSMauro Carvalho Chehab if (snd_pcm_format_signed(runtime->format))
516b285192aSMauro Carvalho Chehab sign = 1;
517b285192aSMauro Carvalho Chehab else
518b285192aSMauro Carvalho Chehab sign = 0;
519b285192aSMauro Carvalho Chehab
520b285192aSMauro Carvalho Chehab if (snd_pcm_format_big_endian(runtime->format))
521b285192aSMauro Carvalho Chehab bswap = 1;
522b285192aSMauro Carvalho Chehab else
523b285192aSMauro Carvalho Chehab bswap = 0;
524b285192aSMauro Carvalho Chehab
525b285192aSMauro Carvalho Chehab switch (dev->pci->device) {
526b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7134:
527b285192aSMauro Carvalho Chehab if (1 == runtime->channels)
528b285192aSMauro Carvalho Chehab fmt |= (1 << 3);
529b285192aSMauro Carvalho Chehab if (2 == runtime->channels)
530b285192aSMauro Carvalho Chehab fmt |= (3 << 3);
531b285192aSMauro Carvalho Chehab if (sign)
532b285192aSMauro Carvalho Chehab fmt |= 0x04;
533b285192aSMauro Carvalho Chehab
534b285192aSMauro Carvalho Chehab fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
535b285192aSMauro Carvalho Chehab saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
536b285192aSMauro Carvalho Chehab saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
537b285192aSMauro Carvalho Chehab saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
538b285192aSMauro Carvalho Chehab saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
539b285192aSMauro Carvalho Chehab
540b285192aSMauro Carvalho Chehab break;
541b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7133:
542b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7135:
543b285192aSMauro Carvalho Chehab if (1 == runtime->channels)
544b285192aSMauro Carvalho Chehab fmt |= (1 << 4);
545b285192aSMauro Carvalho Chehab if (2 == runtime->channels)
546b285192aSMauro Carvalho Chehab fmt |= (2 << 4);
547b285192aSMauro Carvalho Chehab if (!sign)
548b285192aSMauro Carvalho Chehab fmt |= 0x04;
549b285192aSMauro Carvalho Chehab saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
550b285192aSMauro Carvalho Chehab saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
551b285192aSMauro Carvalho Chehab break;
552b285192aSMauro Carvalho Chehab }
553b285192aSMauro Carvalho Chehab
5547592eefcSMauro Carvalho Chehab pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
555b285192aSMauro Carvalho Chehab runtime->format, runtime->channels, fmt,
556b285192aSMauro Carvalho Chehab bswap ? 'b' : '-');
557b285192aSMauro Carvalho Chehab /* dma: setup channel 6 (= AUDIO) */
558b285192aSMauro Carvalho Chehab control = SAA7134_RS_CONTROL_BURST_16 |
559b285192aSMauro Carvalho Chehab SAA7134_RS_CONTROL_ME |
560b285192aSMauro Carvalho Chehab (dev->dmasound.pt.dma >> 12);
561b285192aSMauro Carvalho Chehab if (bswap)
562b285192aSMauro Carvalho Chehab control |= SAA7134_RS_CONTROL_BSWAP;
563b285192aSMauro Carvalho Chehab
564b285192aSMauro Carvalho Chehab saa_writel(SAA7134_RS_BA1(6),0);
565b285192aSMauro Carvalho Chehab saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
566b285192aSMauro Carvalho Chehab saa_writel(SAA7134_RS_PITCH(6),0);
567b285192aSMauro Carvalho Chehab saa_writel(SAA7134_RS_CONTROL(6),control);
568b285192aSMauro Carvalho Chehab
569b285192aSMauro Carvalho Chehab dev->dmasound.rate = runtime->rate;
570b285192aSMauro Carvalho Chehab
571b285192aSMauro Carvalho Chehab /* Setup and update the card/ALSA controls */
572b285192aSMauro Carvalho Chehab snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
573b285192aSMauro Carvalho Chehab true);
574b285192aSMauro Carvalho Chehab
575b285192aSMauro Carvalho Chehab return 0;
576b285192aSMauro Carvalho Chehab
577b285192aSMauro Carvalho Chehab }
578b285192aSMauro Carvalho Chehab
579b285192aSMauro Carvalho Chehab /*
580b285192aSMauro Carvalho Chehab * ALSA pointer fetching
581b285192aSMauro Carvalho Chehab *
582b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
583b285192aSMauro Carvalho Chehab *
584b285192aSMauro Carvalho Chehab * Called whenever a period elapses, it must return the current hardware
585b285192aSMauro Carvalho Chehab * position of the buffer.
586b285192aSMauro Carvalho Chehab * Also resets the read counter used to prevent overruns
587b285192aSMauro Carvalho Chehab *
588b285192aSMauro Carvalho Chehab */
589b285192aSMauro Carvalho Chehab
590b285192aSMauro Carvalho Chehab static snd_pcm_uframes_t
snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)591b285192aSMauro Carvalho Chehab snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
592b285192aSMauro Carvalho Chehab {
593b285192aSMauro Carvalho Chehab struct snd_pcm_runtime *runtime = substream->runtime;
594b285192aSMauro Carvalho Chehab snd_card_saa7134_pcm_t *pcm = runtime->private_data;
595b285192aSMauro Carvalho Chehab struct saa7134_dev *dev=pcm->dev;
596b285192aSMauro Carvalho Chehab
597b285192aSMauro Carvalho Chehab if (dev->dmasound.read_count) {
598b285192aSMauro Carvalho Chehab dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
599b285192aSMauro Carvalho Chehab dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
600b285192aSMauro Carvalho Chehab if (dev->dmasound.read_offset == dev->dmasound.bufsize)
601b285192aSMauro Carvalho Chehab dev->dmasound.read_offset = 0;
602b285192aSMauro Carvalho Chehab }
603b285192aSMauro Carvalho Chehab
604b285192aSMauro Carvalho Chehab return bytes_to_frames(runtime, dev->dmasound.read_offset);
605b285192aSMauro Carvalho Chehab }
606b285192aSMauro Carvalho Chehab
607b285192aSMauro Carvalho Chehab /*
608b285192aSMauro Carvalho Chehab * ALSA hardware capabilities definition
609b285192aSMauro Carvalho Chehab *
610b285192aSMauro Carvalho Chehab * Report only 32kHz for ALSA:
611b285192aSMauro Carvalho Chehab *
612b285192aSMauro Carvalho Chehab * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
613b285192aSMauro Carvalho Chehab * only
614b285192aSMauro Carvalho Chehab * - SAA7134 for TV mode uses DemDec mode (32kHz)
615b285192aSMauro Carvalho Chehab * - Radio works in 32kHz only
616b285192aSMauro Carvalho Chehab * - When recording 48kHz from Line1/Line2, switching of capture source to TV
617b285192aSMauro Carvalho Chehab * means
618b285192aSMauro Carvalho Chehab * switching to 32kHz without any frequency translation
619b285192aSMauro Carvalho Chehab */
620b285192aSMauro Carvalho Chehab
62147f14314SBhumika Goyal static const struct snd_pcm_hardware snd_card_saa7134_capture =
622b285192aSMauro Carvalho Chehab {
623b285192aSMauro Carvalho Chehab .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
624b285192aSMauro Carvalho Chehab SNDRV_PCM_INFO_BLOCK_TRANSFER |
625b285192aSMauro Carvalho Chehab SNDRV_PCM_INFO_MMAP_VALID),
626b285192aSMauro Carvalho Chehab .formats = SNDRV_PCM_FMTBIT_S16_LE | \
627b285192aSMauro Carvalho Chehab SNDRV_PCM_FMTBIT_S16_BE | \
628b285192aSMauro Carvalho Chehab SNDRV_PCM_FMTBIT_S8 | \
629b285192aSMauro Carvalho Chehab SNDRV_PCM_FMTBIT_U8 | \
630b285192aSMauro Carvalho Chehab SNDRV_PCM_FMTBIT_U16_LE | \
631b285192aSMauro Carvalho Chehab SNDRV_PCM_FMTBIT_U16_BE,
632b285192aSMauro Carvalho Chehab .rates = SNDRV_PCM_RATE_32000,
633b285192aSMauro Carvalho Chehab .rate_min = 32000,
634b285192aSMauro Carvalho Chehab .rate_max = 32000,
635b285192aSMauro Carvalho Chehab .channels_min = 1,
636b285192aSMauro Carvalho Chehab .channels_max = 2,
637b285192aSMauro Carvalho Chehab .buffer_bytes_max = (256*1024),
638b285192aSMauro Carvalho Chehab .period_bytes_min = 64,
639b285192aSMauro Carvalho Chehab .period_bytes_max = (256*1024),
640b285192aSMauro Carvalho Chehab .periods_min = 4,
641b285192aSMauro Carvalho Chehab .periods_max = 1024,
642b285192aSMauro Carvalho Chehab };
643b285192aSMauro Carvalho Chehab
snd_card_saa7134_runtime_free(struct snd_pcm_runtime * runtime)644b285192aSMauro Carvalho Chehab static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
645b285192aSMauro Carvalho Chehab {
646b285192aSMauro Carvalho Chehab snd_card_saa7134_pcm_t *pcm = runtime->private_data;
647b285192aSMauro Carvalho Chehab
648b285192aSMauro Carvalho Chehab kfree(pcm);
649b285192aSMauro Carvalho Chehab }
650b285192aSMauro Carvalho Chehab
651b285192aSMauro Carvalho Chehab
652b285192aSMauro Carvalho Chehab /*
653b285192aSMauro Carvalho Chehab * ALSA hardware params
654b285192aSMauro Carvalho Chehab *
655b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
656b285192aSMauro Carvalho Chehab *
657b285192aSMauro Carvalho Chehab * Called on initialization, right before the PCM preparation
658b285192aSMauro Carvalho Chehab *
659b285192aSMauro Carvalho Chehab */
660b285192aSMauro Carvalho Chehab
snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hw_params)661b285192aSMauro Carvalho Chehab static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
662b285192aSMauro Carvalho Chehab struct snd_pcm_hw_params * hw_params)
663b285192aSMauro Carvalho Chehab {
664b285192aSMauro Carvalho Chehab snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
665b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
666b285192aSMauro Carvalho Chehab unsigned int period_size, periods;
667b285192aSMauro Carvalho Chehab int err;
668b285192aSMauro Carvalho Chehab
669b285192aSMauro Carvalho Chehab period_size = params_period_bytes(hw_params);
670b285192aSMauro Carvalho Chehab periods = params_periods(hw_params);
671b285192aSMauro Carvalho Chehab
672b285192aSMauro Carvalho Chehab if (period_size < 0x100 || period_size > 0x10000)
673b285192aSMauro Carvalho Chehab return -EINVAL;
674b285192aSMauro Carvalho Chehab if (periods < 4)
675b285192aSMauro Carvalho Chehab return -EINVAL;
676b285192aSMauro Carvalho Chehab if (period_size * periods > 1024 * 1024)
677b285192aSMauro Carvalho Chehab return -EINVAL;
678b285192aSMauro Carvalho Chehab
679b285192aSMauro Carvalho Chehab dev = saa7134->dev;
680b285192aSMauro Carvalho Chehab
681b285192aSMauro Carvalho Chehab if (dev->dmasound.blocks == periods &&
682b285192aSMauro Carvalho Chehab dev->dmasound.blksize == period_size)
683b285192aSMauro Carvalho Chehab return 0;
684b285192aSMauro Carvalho Chehab
685b285192aSMauro Carvalho Chehab /* release the old buffer */
686b285192aSMauro Carvalho Chehab if (substream->runtime->dma_area) {
687b285192aSMauro Carvalho Chehab saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
68815e64f0dSHans Verkuil saa7134_alsa_dma_unmap(dev);
689b285192aSMauro Carvalho Chehab dsp_buffer_free(dev);
690b285192aSMauro Carvalho Chehab substream->runtime->dma_area = NULL;
691b285192aSMauro Carvalho Chehab }
692b285192aSMauro Carvalho Chehab dev->dmasound.blocks = periods;
693b285192aSMauro Carvalho Chehab dev->dmasound.blksize = period_size;
694b285192aSMauro Carvalho Chehab dev->dmasound.bufsize = period_size * periods;
695b285192aSMauro Carvalho Chehab
696b285192aSMauro Carvalho Chehab err = dsp_buffer_init(dev);
697b285192aSMauro Carvalho Chehab if (0 != err) {
698b285192aSMauro Carvalho Chehab dev->dmasound.blocks = 0;
699b285192aSMauro Carvalho Chehab dev->dmasound.blksize = 0;
700b285192aSMauro Carvalho Chehab dev->dmasound.bufsize = 0;
701b285192aSMauro Carvalho Chehab return err;
702b285192aSMauro Carvalho Chehab }
703b285192aSMauro Carvalho Chehab
70415e64f0dSHans Verkuil err = saa7134_alsa_dma_map(dev);
705e72936d2SHans Verkuil if (err) {
706b285192aSMauro Carvalho Chehab dsp_buffer_free(dev);
707b285192aSMauro Carvalho Chehab return err;
708b285192aSMauro Carvalho Chehab }
709e72936d2SHans Verkuil err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
710e72936d2SHans Verkuil if (err) {
71115e64f0dSHans Verkuil saa7134_alsa_dma_unmap(dev);
712b285192aSMauro Carvalho Chehab dsp_buffer_free(dev);
713b285192aSMauro Carvalho Chehab return err;
714b285192aSMauro Carvalho Chehab }
715e72936d2SHans Verkuil err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
716e72936d2SHans Verkuil dev->dmasound.sglist, dev->dmasound.sglen, 0);
717e72936d2SHans Verkuil if (err) {
718b285192aSMauro Carvalho Chehab saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
71915e64f0dSHans Verkuil saa7134_alsa_dma_unmap(dev);
720b285192aSMauro Carvalho Chehab dsp_buffer_free(dev);
721b285192aSMauro Carvalho Chehab return err;
722b285192aSMauro Carvalho Chehab }
723b285192aSMauro Carvalho Chehab
724b285192aSMauro Carvalho Chehab /* I should be able to use runtime->dma_addr in the control
725b285192aSMauro Carvalho Chehab byte, but it doesn't work. So I allocate the DMA using the
726b285192aSMauro Carvalho Chehab V4L functions, and force ALSA to use that as the DMA area */
727b285192aSMauro Carvalho Chehab
72815e64f0dSHans Verkuil substream->runtime->dma_area = dev->dmasound.vaddr;
729b285192aSMauro Carvalho Chehab substream->runtime->dma_bytes = dev->dmasound.bufsize;
730b285192aSMauro Carvalho Chehab substream->runtime->dma_addr = 0;
731b285192aSMauro Carvalho Chehab
732b285192aSMauro Carvalho Chehab return 0;
733b285192aSMauro Carvalho Chehab
734b285192aSMauro Carvalho Chehab }
735b285192aSMauro Carvalho Chehab
736b285192aSMauro Carvalho Chehab /*
737b285192aSMauro Carvalho Chehab * ALSA hardware release
738b285192aSMauro Carvalho Chehab *
739b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
740b285192aSMauro Carvalho Chehab *
741b285192aSMauro Carvalho Chehab * Called after closing the device, but before snd_card_saa7134_capture_close
742b285192aSMauro Carvalho Chehab * It stops the DMA audio and releases the buffers.
743b285192aSMauro Carvalho Chehab *
744b285192aSMauro Carvalho Chehab */
745b285192aSMauro Carvalho Chehab
snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)746b285192aSMauro Carvalho Chehab static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
747b285192aSMauro Carvalho Chehab {
748b285192aSMauro Carvalho Chehab snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
749b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
750b285192aSMauro Carvalho Chehab
751b285192aSMauro Carvalho Chehab dev = saa7134->dev;
752b285192aSMauro Carvalho Chehab
753b285192aSMauro Carvalho Chehab if (substream->runtime->dma_area) {
754b285192aSMauro Carvalho Chehab saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
75515e64f0dSHans Verkuil saa7134_alsa_dma_unmap(dev);
756b285192aSMauro Carvalho Chehab dsp_buffer_free(dev);
757b285192aSMauro Carvalho Chehab substream->runtime->dma_area = NULL;
758b285192aSMauro Carvalho Chehab }
759b285192aSMauro Carvalho Chehab
760b285192aSMauro Carvalho Chehab return 0;
761b285192aSMauro Carvalho Chehab }
762b285192aSMauro Carvalho Chehab
763b285192aSMauro Carvalho Chehab /*
764b285192aSMauro Carvalho Chehab * ALSA capture finish
765b285192aSMauro Carvalho Chehab *
766b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
767b285192aSMauro Carvalho Chehab *
768b285192aSMauro Carvalho Chehab * Called after closing the device.
769b285192aSMauro Carvalho Chehab *
770b285192aSMauro Carvalho Chehab */
771b285192aSMauro Carvalho Chehab
snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)772b285192aSMauro Carvalho Chehab static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
773b285192aSMauro Carvalho Chehab {
774b285192aSMauro Carvalho Chehab snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
775b285192aSMauro Carvalho Chehab struct saa7134_dev *dev = saa7134->dev;
776b285192aSMauro Carvalho Chehab
777b285192aSMauro Carvalho Chehab if (saa7134->mute_was_on) {
778b285192aSMauro Carvalho Chehab dev->ctl_mute = 1;
779b285192aSMauro Carvalho Chehab saa7134_tvaudio_setmute(dev);
780b285192aSMauro Carvalho Chehab }
781b285192aSMauro Carvalho Chehab return 0;
782b285192aSMauro Carvalho Chehab }
783b285192aSMauro Carvalho Chehab
784b285192aSMauro Carvalho Chehab /*
785b285192aSMauro Carvalho Chehab * ALSA capture start
786b285192aSMauro Carvalho Chehab *
787b285192aSMauro Carvalho Chehab * - One of the ALSA capture callbacks.
788b285192aSMauro Carvalho Chehab *
789b285192aSMauro Carvalho Chehab * Called when opening the device. It creates and populates the PCM
790b285192aSMauro Carvalho Chehab * structure
791b285192aSMauro Carvalho Chehab *
792b285192aSMauro Carvalho Chehab */
793b285192aSMauro Carvalho Chehab
snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)794b285192aSMauro Carvalho Chehab static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
795b285192aSMauro Carvalho Chehab {
796b285192aSMauro Carvalho Chehab struct snd_pcm_runtime *runtime = substream->runtime;
797b285192aSMauro Carvalho Chehab snd_card_saa7134_pcm_t *pcm;
798b285192aSMauro Carvalho Chehab snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
799b285192aSMauro Carvalho Chehab struct saa7134_dev *dev;
800b285192aSMauro Carvalho Chehab int amux, err;
801b285192aSMauro Carvalho Chehab
802b285192aSMauro Carvalho Chehab if (!saa7134) {
803395eff95SMauro Carvalho Chehab pr_err("BUG: saa7134 can't find device struct. Can't proceed with open\n");
804b285192aSMauro Carvalho Chehab return -ENODEV;
805b285192aSMauro Carvalho Chehab }
806b285192aSMauro Carvalho Chehab dev = saa7134->dev;
807b285192aSMauro Carvalho Chehab mutex_lock(&dev->dmasound.lock);
808b285192aSMauro Carvalho Chehab
809b285192aSMauro Carvalho Chehab dev->dmasound.read_count = 0;
810b285192aSMauro Carvalho Chehab dev->dmasound.read_offset = 0;
811b285192aSMauro Carvalho Chehab
812b285192aSMauro Carvalho Chehab amux = dev->input->amux;
813b285192aSMauro Carvalho Chehab if ((amux < 1) || (amux > 3))
814b285192aSMauro Carvalho Chehab amux = 1;
815b285192aSMauro Carvalho Chehab dev->dmasound.input = amux - 1;
816b285192aSMauro Carvalho Chehab
817b285192aSMauro Carvalho Chehab mutex_unlock(&dev->dmasound.lock);
818b285192aSMauro Carvalho Chehab
819b285192aSMauro Carvalho Chehab pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
820b285192aSMauro Carvalho Chehab if (pcm == NULL)
821b285192aSMauro Carvalho Chehab return -ENOMEM;
822b285192aSMauro Carvalho Chehab
823b285192aSMauro Carvalho Chehab pcm->dev=saa7134->dev;
824b285192aSMauro Carvalho Chehab
825b285192aSMauro Carvalho Chehab spin_lock_init(&pcm->lock);
826b285192aSMauro Carvalho Chehab
827b285192aSMauro Carvalho Chehab pcm->substream = substream;
828b285192aSMauro Carvalho Chehab runtime->private_data = pcm;
829b285192aSMauro Carvalho Chehab runtime->private_free = snd_card_saa7134_runtime_free;
830b285192aSMauro Carvalho Chehab runtime->hw = snd_card_saa7134_capture;
831b285192aSMauro Carvalho Chehab
832b285192aSMauro Carvalho Chehab if (dev->ctl_mute != 0) {
833b285192aSMauro Carvalho Chehab saa7134->mute_was_on = 1;
834b285192aSMauro Carvalho Chehab dev->ctl_mute = 0;
835b285192aSMauro Carvalho Chehab saa7134_tvaudio_setmute(dev);
836b285192aSMauro Carvalho Chehab }
837b285192aSMauro Carvalho Chehab
838b285192aSMauro Carvalho Chehab err = snd_pcm_hw_constraint_integer(runtime,
839b285192aSMauro Carvalho Chehab SNDRV_PCM_HW_PARAM_PERIODS);
840b285192aSMauro Carvalho Chehab if (err < 0)
841b285192aSMauro Carvalho Chehab return err;
842b285192aSMauro Carvalho Chehab
843b285192aSMauro Carvalho Chehab err = snd_pcm_hw_constraint_step(runtime, 0,
844b285192aSMauro Carvalho Chehab SNDRV_PCM_HW_PARAM_PERIODS, 2);
845b285192aSMauro Carvalho Chehab if (err < 0)
846b285192aSMauro Carvalho Chehab return err;
847b285192aSMauro Carvalho Chehab
848b285192aSMauro Carvalho Chehab return 0;
849b285192aSMauro Carvalho Chehab }
850b285192aSMauro Carvalho Chehab
851b285192aSMauro Carvalho Chehab /*
852b285192aSMauro Carvalho Chehab * page callback (needed for mmap)
853b285192aSMauro Carvalho Chehab */
854b285192aSMauro Carvalho Chehab
snd_card_saa7134_page(struct snd_pcm_substream * substream,unsigned long offset)855b285192aSMauro Carvalho Chehab static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
856b285192aSMauro Carvalho Chehab unsigned long offset)
857b285192aSMauro Carvalho Chehab {
858b285192aSMauro Carvalho Chehab void *pageptr = substream->runtime->dma_area + offset;
859b285192aSMauro Carvalho Chehab return vmalloc_to_page(pageptr);
860b285192aSMauro Carvalho Chehab }
861b285192aSMauro Carvalho Chehab
862b285192aSMauro Carvalho Chehab /*
863b285192aSMauro Carvalho Chehab * ALSA capture callbacks definition
864b285192aSMauro Carvalho Chehab */
865b285192aSMauro Carvalho Chehab
8665c8d8c01SJulia Lawall static const struct snd_pcm_ops snd_card_saa7134_capture_ops = {
867b285192aSMauro Carvalho Chehab .open = snd_card_saa7134_capture_open,
868b285192aSMauro Carvalho Chehab .close = snd_card_saa7134_capture_close,
869b285192aSMauro Carvalho Chehab .hw_params = snd_card_saa7134_hw_params,
870b285192aSMauro Carvalho Chehab .hw_free = snd_card_saa7134_hw_free,
871b285192aSMauro Carvalho Chehab .prepare = snd_card_saa7134_capture_prepare,
872b285192aSMauro Carvalho Chehab .trigger = snd_card_saa7134_capture_trigger,
873b285192aSMauro Carvalho Chehab .pointer = snd_card_saa7134_capture_pointer,
874b285192aSMauro Carvalho Chehab .page = snd_card_saa7134_page,
875b285192aSMauro Carvalho Chehab };
876b285192aSMauro Carvalho Chehab
877b285192aSMauro Carvalho Chehab /*
878b285192aSMauro Carvalho Chehab * ALSA PCM setup
879b285192aSMauro Carvalho Chehab *
880b285192aSMauro Carvalho Chehab * Called when initializing the board. Sets up the name and hooks up
881b285192aSMauro Carvalho Chehab * the callbacks
882b285192aSMauro Carvalho Chehab *
883b285192aSMauro Carvalho Chehab */
884b285192aSMauro Carvalho Chehab
snd_card_saa7134_pcm(snd_card_saa7134_t * saa7134,int device)885b285192aSMauro Carvalho Chehab static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
886b285192aSMauro Carvalho Chehab {
887b285192aSMauro Carvalho Chehab struct snd_pcm *pcm;
888b285192aSMauro Carvalho Chehab int err;
889b285192aSMauro Carvalho Chehab
890b285192aSMauro Carvalho Chehab if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
891b285192aSMauro Carvalho Chehab return err;
892b285192aSMauro Carvalho Chehab snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
893b285192aSMauro Carvalho Chehab pcm->private_data = saa7134;
894b285192aSMauro Carvalho Chehab pcm->info_flags = 0;
895cc1e6315SMauro Carvalho Chehab strscpy(pcm->name, "SAA7134 PCM", sizeof(pcm->name));
896b285192aSMauro Carvalho Chehab return 0;
897b285192aSMauro Carvalho Chehab }
898b285192aSMauro Carvalho Chehab
899b285192aSMauro Carvalho Chehab #define SAA713x_VOLUME(xname, xindex, addr) \
900b285192aSMauro Carvalho Chehab { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
901b285192aSMauro Carvalho Chehab .info = snd_saa7134_volume_info, \
902b285192aSMauro Carvalho Chehab .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
903b285192aSMauro Carvalho Chehab .private_value = addr }
904b285192aSMauro Carvalho Chehab
snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)905b285192aSMauro Carvalho Chehab static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
906b285192aSMauro Carvalho Chehab struct snd_ctl_elem_info * uinfo)
907b285192aSMauro Carvalho Chehab {
908b285192aSMauro Carvalho Chehab uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
909b285192aSMauro Carvalho Chehab uinfo->count = 2;
910b285192aSMauro Carvalho Chehab uinfo->value.integer.min = 0;
911b285192aSMauro Carvalho Chehab uinfo->value.integer.max = 20;
912b285192aSMauro Carvalho Chehab return 0;
913b285192aSMauro Carvalho Chehab }
914b285192aSMauro Carvalho Chehab
snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)915b285192aSMauro Carvalho Chehab static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
916b285192aSMauro Carvalho Chehab struct snd_ctl_elem_value * ucontrol)
917b285192aSMauro Carvalho Chehab {
918b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
919b285192aSMauro Carvalho Chehab int addr = kcontrol->private_value;
920b285192aSMauro Carvalho Chehab
921b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
922b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
923b285192aSMauro Carvalho Chehab return 0;
924b285192aSMauro Carvalho Chehab }
925b285192aSMauro Carvalho Chehab
snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)926b285192aSMauro Carvalho Chehab static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
927b285192aSMauro Carvalho Chehab struct snd_ctl_elem_value * ucontrol)
928b285192aSMauro Carvalho Chehab {
929b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
930b285192aSMauro Carvalho Chehab struct saa7134_dev *dev = chip->dev;
931b285192aSMauro Carvalho Chehab
932b285192aSMauro Carvalho Chehab int change, addr = kcontrol->private_value;
933b285192aSMauro Carvalho Chehab int left, right;
934b285192aSMauro Carvalho Chehab
935b285192aSMauro Carvalho Chehab left = ucontrol->value.integer.value[0];
936b285192aSMauro Carvalho Chehab if (left < 0)
937b285192aSMauro Carvalho Chehab left = 0;
938b285192aSMauro Carvalho Chehab if (left > 20)
939b285192aSMauro Carvalho Chehab left = 20;
940b285192aSMauro Carvalho Chehab right = ucontrol->value.integer.value[1];
941b285192aSMauro Carvalho Chehab if (right < 0)
942b285192aSMauro Carvalho Chehab right = 0;
943b285192aSMauro Carvalho Chehab if (right > 20)
944b285192aSMauro Carvalho Chehab right = 20;
945b285192aSMauro Carvalho Chehab spin_lock_irq(&chip->mixer_lock);
946b285192aSMauro Carvalho Chehab change = 0;
947b285192aSMauro Carvalho Chehab if (chip->mixer_volume[addr][0] != left) {
948b285192aSMauro Carvalho Chehab change = 1;
949b285192aSMauro Carvalho Chehab right = left;
950b285192aSMauro Carvalho Chehab }
951b285192aSMauro Carvalho Chehab if (chip->mixer_volume[addr][1] != right) {
952b285192aSMauro Carvalho Chehab change = 1;
953b285192aSMauro Carvalho Chehab left = right;
954b285192aSMauro Carvalho Chehab }
955b285192aSMauro Carvalho Chehab if (change) {
956b285192aSMauro Carvalho Chehab switch (dev->pci->device) {
957b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7134:
958b285192aSMauro Carvalho Chehab switch (addr) {
959b285192aSMauro Carvalho Chehab case MIXER_ADDR_TVTUNER:
960b285192aSMauro Carvalho Chehab left = 20;
961b285192aSMauro Carvalho Chehab break;
962b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE1:
963b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
964b285192aSMauro Carvalho Chehab (left > 10) ? 0x00 : 0x10);
965b285192aSMauro Carvalho Chehab break;
966b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE2:
967b285192aSMauro Carvalho Chehab saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
968b285192aSMauro Carvalho Chehab (left > 10) ? 0x00 : 0x20);
969b285192aSMauro Carvalho Chehab break;
970b285192aSMauro Carvalho Chehab }
971b285192aSMauro Carvalho Chehab break;
972b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7133:
973b285192aSMauro Carvalho Chehab case PCI_DEVICE_ID_PHILIPS_SAA7135:
974b285192aSMauro Carvalho Chehab switch (addr) {
975b285192aSMauro Carvalho Chehab case MIXER_ADDR_TVTUNER:
976b285192aSMauro Carvalho Chehab left = 20;
977b285192aSMauro Carvalho Chehab break;
978b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE1:
979b285192aSMauro Carvalho Chehab saa_andorb(0x0594, 0x10,
980b285192aSMauro Carvalho Chehab (left > 10) ? 0x00 : 0x10);
981b285192aSMauro Carvalho Chehab break;
982b285192aSMauro Carvalho Chehab case MIXER_ADDR_LINE2:
983b285192aSMauro Carvalho Chehab saa_andorb(0x0594, 0x20,
984b285192aSMauro Carvalho Chehab (left > 10) ? 0x00 : 0x20);
985b285192aSMauro Carvalho Chehab break;
986b285192aSMauro Carvalho Chehab }
987b285192aSMauro Carvalho Chehab break;
988b285192aSMauro Carvalho Chehab }
989b285192aSMauro Carvalho Chehab chip->mixer_volume[addr][0] = left;
990b285192aSMauro Carvalho Chehab chip->mixer_volume[addr][1] = right;
991b285192aSMauro Carvalho Chehab }
992b285192aSMauro Carvalho Chehab spin_unlock_irq(&chip->mixer_lock);
993b285192aSMauro Carvalho Chehab return change;
994b285192aSMauro Carvalho Chehab }
995b285192aSMauro Carvalho Chehab
996b285192aSMauro Carvalho Chehab #define SAA713x_CAPSRC(xname, xindex, addr) \
997b285192aSMauro Carvalho Chehab { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
998b285192aSMauro Carvalho Chehab .info = snd_saa7134_capsrc_info, \
999b285192aSMauro Carvalho Chehab .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1000b285192aSMauro Carvalho Chehab .private_value = addr }
1001b285192aSMauro Carvalho Chehab
snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1002b285192aSMauro Carvalho Chehab static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1003b285192aSMauro Carvalho Chehab struct snd_ctl_elem_info * uinfo)
1004b285192aSMauro Carvalho Chehab {
1005b285192aSMauro Carvalho Chehab uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1006b285192aSMauro Carvalho Chehab uinfo->count = 2;
1007b285192aSMauro Carvalho Chehab uinfo->value.integer.min = 0;
1008b285192aSMauro Carvalho Chehab uinfo->value.integer.max = 1;
1009b285192aSMauro Carvalho Chehab return 0;
1010b285192aSMauro Carvalho Chehab }
1011b285192aSMauro Carvalho Chehab
snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1012b285192aSMauro Carvalho Chehab static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1013b285192aSMauro Carvalho Chehab struct snd_ctl_elem_value * ucontrol)
1014b285192aSMauro Carvalho Chehab {
1015b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
1016b285192aSMauro Carvalho Chehab int addr = kcontrol->private_value;
1017b285192aSMauro Carvalho Chehab
1018b285192aSMauro Carvalho Chehab spin_lock_irq(&chip->mixer_lock);
1019b285192aSMauro Carvalho Chehab if (chip->capture_source_addr == addr) {
1020b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[0] = chip->capture_source[0];
1021b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[1] = chip->capture_source[1];
1022b285192aSMauro Carvalho Chehab } else {
1023b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[0] = 0;
1024b285192aSMauro Carvalho Chehab ucontrol->value.integer.value[1] = 0;
1025b285192aSMauro Carvalho Chehab }
1026b285192aSMauro Carvalho Chehab spin_unlock_irq(&chip->mixer_lock);
1027b285192aSMauro Carvalho Chehab
1028b285192aSMauro Carvalho Chehab return 0;
1029b285192aSMauro Carvalho Chehab }
1030b285192aSMauro Carvalho Chehab
snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1031b285192aSMauro Carvalho Chehab static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1032b285192aSMauro Carvalho Chehab struct snd_ctl_elem_value * ucontrol)
1033b285192aSMauro Carvalho Chehab {
1034b285192aSMauro Carvalho Chehab int left, right;
1035b285192aSMauro Carvalho Chehab left = ucontrol->value.integer.value[0] & 1;
1036b285192aSMauro Carvalho Chehab right = ucontrol->value.integer.value[1] & 1;
1037b285192aSMauro Carvalho Chehab
1038b285192aSMauro Carvalho Chehab return snd_saa7134_capsrc_set(kcontrol, left, right, false);
1039b285192aSMauro Carvalho Chehab }
1040b285192aSMauro Carvalho Chehab
1041b285192aSMauro Carvalho Chehab static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
1042b285192aSMauro Carvalho Chehab SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
1043b285192aSMauro Carvalho Chehab SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
1044b285192aSMauro Carvalho Chehab SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
1045b285192aSMauro Carvalho Chehab };
1046b285192aSMauro Carvalho Chehab
1047b285192aSMauro Carvalho Chehab static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1048b285192aSMauro Carvalho Chehab SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1049b285192aSMauro Carvalho Chehab SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
1050b285192aSMauro Carvalho Chehab SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
1051b285192aSMauro Carvalho Chehab };
1052b285192aSMauro Carvalho Chehab
1053b285192aSMauro Carvalho Chehab /*
1054b285192aSMauro Carvalho Chehab * ALSA mixer setup
1055b285192aSMauro Carvalho Chehab *
1056b285192aSMauro Carvalho Chehab * Called when initializing the board. Sets up the name and hooks up
1057b285192aSMauro Carvalho Chehab * the callbacks
1058b285192aSMauro Carvalho Chehab *
1059b285192aSMauro Carvalho Chehab */
1060b285192aSMauro Carvalho Chehab
snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)1061b285192aSMauro Carvalho Chehab static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
1062b285192aSMauro Carvalho Chehab {
1063b285192aSMauro Carvalho Chehab struct snd_card *card = chip->card;
1064b285192aSMauro Carvalho Chehab struct snd_kcontrol *kcontrol;
1065b285192aSMauro Carvalho Chehab unsigned int idx;
1066b285192aSMauro Carvalho Chehab int err, addr;
1067b285192aSMauro Carvalho Chehab
1068cc1e6315SMauro Carvalho Chehab strscpy(card->mixername, "SAA7134 Mixer", sizeof(card->mixername));
1069b285192aSMauro Carvalho Chehab
1070b285192aSMauro Carvalho Chehab for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1071b285192aSMauro Carvalho Chehab kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1072b285192aSMauro Carvalho Chehab chip);
1073b285192aSMauro Carvalho Chehab err = snd_ctl_add(card, kcontrol);
1074b285192aSMauro Carvalho Chehab if (err < 0)
1075b285192aSMauro Carvalho Chehab return err;
1076b285192aSMauro Carvalho Chehab }
1077b285192aSMauro Carvalho Chehab
1078b285192aSMauro Carvalho Chehab for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1079b285192aSMauro Carvalho Chehab kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1080b285192aSMauro Carvalho Chehab chip);
1081b285192aSMauro Carvalho Chehab addr = snd_saa7134_capture_controls[idx].private_value;
1082b285192aSMauro Carvalho Chehab chip->capture_ctl[addr] = kcontrol;
1083b285192aSMauro Carvalho Chehab err = snd_ctl_add(card, kcontrol);
1084b285192aSMauro Carvalho Chehab if (err < 0)
1085b285192aSMauro Carvalho Chehab return err;
1086b285192aSMauro Carvalho Chehab }
1087b285192aSMauro Carvalho Chehab
1088b285192aSMauro Carvalho Chehab chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
1089b285192aSMauro Carvalho Chehab return 0;
1090b285192aSMauro Carvalho Chehab }
1091b285192aSMauro Carvalho Chehab
snd_saa7134_free(struct snd_card * card)1092b285192aSMauro Carvalho Chehab static void snd_saa7134_free(struct snd_card * card)
1093b285192aSMauro Carvalho Chehab {
1094b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip = card->private_data;
1095b285192aSMauro Carvalho Chehab
1096b285192aSMauro Carvalho Chehab if (chip->dev->dmasound.priv_data == NULL)
1097b285192aSMauro Carvalho Chehab return;
1098b285192aSMauro Carvalho Chehab
1099b285192aSMauro Carvalho Chehab chip->dev->dmasound.priv_data = NULL;
1100b285192aSMauro Carvalho Chehab
1101b285192aSMauro Carvalho Chehab }
1102b285192aSMauro Carvalho Chehab
1103b285192aSMauro Carvalho Chehab /*
1104b285192aSMauro Carvalho Chehab * ALSA initialization
1105b285192aSMauro Carvalho Chehab *
1106b285192aSMauro Carvalho Chehab * Called by the init routine, once for each saa7134 device present,
1107b285192aSMauro Carvalho Chehab * it creates the basic structures and registers the ALSA devices
1108b285192aSMauro Carvalho Chehab *
1109b285192aSMauro Carvalho Chehab */
1110b285192aSMauro Carvalho Chehab
alsa_card_saa7134_create(struct saa7134_dev * dev,int devnum)1111b285192aSMauro Carvalho Chehab static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
1112b285192aSMauro Carvalho Chehab {
1113b285192aSMauro Carvalho Chehab
1114b285192aSMauro Carvalho Chehab struct snd_card *card;
1115b285192aSMauro Carvalho Chehab snd_card_saa7134_t *chip;
1116b285192aSMauro Carvalho Chehab int err;
1117b285192aSMauro Carvalho Chehab
1118b285192aSMauro Carvalho Chehab
1119b285192aSMauro Carvalho Chehab if (devnum >= SNDRV_CARDS)
1120b285192aSMauro Carvalho Chehab return -ENODEV;
1121b285192aSMauro Carvalho Chehab if (!enable[devnum])
1122b285192aSMauro Carvalho Chehab return -ENODEV;
1123b285192aSMauro Carvalho Chehab
1124e7356888STakashi Iwai err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1125e7356888STakashi Iwai THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
1126b285192aSMauro Carvalho Chehab if (err < 0)
1127b285192aSMauro Carvalho Chehab return err;
1128b285192aSMauro Carvalho Chehab
1129cc1e6315SMauro Carvalho Chehab strscpy(card->driver, "SAA7134", sizeof(card->driver));
1130b285192aSMauro Carvalho Chehab
1131b285192aSMauro Carvalho Chehab /* Card "creation" */
1132b285192aSMauro Carvalho Chehab
1133b285192aSMauro Carvalho Chehab card->private_free = snd_saa7134_free;
1134b285192aSMauro Carvalho Chehab chip = card->private_data;
1135b285192aSMauro Carvalho Chehab
1136b285192aSMauro Carvalho Chehab spin_lock_init(&chip->lock);
1137b285192aSMauro Carvalho Chehab spin_lock_init(&chip->mixer_lock);
1138b285192aSMauro Carvalho Chehab
1139b285192aSMauro Carvalho Chehab chip->dev = dev;
1140b285192aSMauro Carvalho Chehab
1141b285192aSMauro Carvalho Chehab chip->card = card;
1142b285192aSMauro Carvalho Chehab
1143b285192aSMauro Carvalho Chehab chip->pci = dev->pci;
1144b285192aSMauro Carvalho Chehab chip->iobase = pci_resource_start(dev->pci, 0);
1145b285192aSMauro Carvalho Chehab
1146b285192aSMauro Carvalho Chehab
1147179559edSRicardo Ribalda err = devm_request_irq(&dev->pci->dev, dev->pci->irq, saa7134_alsa_irq,
1148179559edSRicardo Ribalda IRQF_SHARED, dev->name, &dev->dmasound);
1149b285192aSMauro Carvalho Chehab if (err < 0) {
115083582009SMauro Carvalho Chehab pr_err("%s: can't get IRQ %d for ALSA\n",
1151b285192aSMauro Carvalho Chehab dev->name, dev->pci->irq);
1152b285192aSMauro Carvalho Chehab goto __nodev;
1153b285192aSMauro Carvalho Chehab }
1154b285192aSMauro Carvalho Chehab
1155b285192aSMauro Carvalho Chehab chip->irq = dev->pci->irq;
1156b285192aSMauro Carvalho Chehab
1157b285192aSMauro Carvalho Chehab mutex_init(&dev->dmasound.lock);
1158b285192aSMauro Carvalho Chehab
1159b285192aSMauro Carvalho Chehab if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1160b285192aSMauro Carvalho Chehab goto __nodev;
1161b285192aSMauro Carvalho Chehab
1162b285192aSMauro Carvalho Chehab if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1163b285192aSMauro Carvalho Chehab goto __nodev;
1164b285192aSMauro Carvalho Chehab
1165b285192aSMauro Carvalho Chehab /* End of "creation" */
1166b285192aSMauro Carvalho Chehab
1167cc1e6315SMauro Carvalho Chehab strscpy(card->shortname, "SAA7134", sizeof(card->shortname));
1168b285192aSMauro Carvalho Chehab sprintf(card->longname, "%s at 0x%lx irq %d",
1169b285192aSMauro Carvalho Chehab chip->dev->name, chip->iobase, chip->irq);
1170b285192aSMauro Carvalho Chehab
11716139ebc6SMauro Carvalho Chehab pr_info("%s/alsa: %s registered as card %d\n",
11726139ebc6SMauro Carvalho Chehab dev->name, card->longname, index[devnum]);
1173b285192aSMauro Carvalho Chehab
1174b285192aSMauro Carvalho Chehab if ((err = snd_card_register(card)) == 0) {
1175b285192aSMauro Carvalho Chehab snd_saa7134_cards[devnum] = card;
1176b285192aSMauro Carvalho Chehab return 0;
1177b285192aSMauro Carvalho Chehab }
1178b285192aSMauro Carvalho Chehab
1179b285192aSMauro Carvalho Chehab __nodev:
1180b285192aSMauro Carvalho Chehab snd_card_free(card);
1181b285192aSMauro Carvalho Chehab return err;
1182b285192aSMauro Carvalho Chehab }
1183b285192aSMauro Carvalho Chehab
1184b285192aSMauro Carvalho Chehab
alsa_device_init(struct saa7134_dev * dev)1185b285192aSMauro Carvalho Chehab static int alsa_device_init(struct saa7134_dev *dev)
1186b285192aSMauro Carvalho Chehab {
1187b285192aSMauro Carvalho Chehab dev->dmasound.priv_data = dev;
1188b285192aSMauro Carvalho Chehab alsa_card_saa7134_create(dev,dev->nr);
1189b285192aSMauro Carvalho Chehab return 1;
1190b285192aSMauro Carvalho Chehab }
1191b285192aSMauro Carvalho Chehab
alsa_device_exit(struct saa7134_dev * dev)1192b285192aSMauro Carvalho Chehab static int alsa_device_exit(struct saa7134_dev *dev)
1193b285192aSMauro Carvalho Chehab {
1194ac75fe5dSMauro Carvalho Chehab if (!snd_saa7134_cards[dev->nr])
1195ac75fe5dSMauro Carvalho Chehab return 1;
1196b285192aSMauro Carvalho Chehab
1197b285192aSMauro Carvalho Chehab snd_card_free(snd_saa7134_cards[dev->nr]);
1198b285192aSMauro Carvalho Chehab snd_saa7134_cards[dev->nr] = NULL;
1199b285192aSMauro Carvalho Chehab return 1;
1200b285192aSMauro Carvalho Chehab }
1201b285192aSMauro Carvalho Chehab
1202b285192aSMauro Carvalho Chehab /*
1203b285192aSMauro Carvalho Chehab * Module initializer
1204b285192aSMauro Carvalho Chehab *
1205b285192aSMauro Carvalho Chehab * Loops through present saa7134 cards, and assigns an ALSA device
1206b285192aSMauro Carvalho Chehab * to each one
1207b285192aSMauro Carvalho Chehab *
1208b285192aSMauro Carvalho Chehab */
1209b285192aSMauro Carvalho Chehab
saa7134_alsa_init(void)1210b285192aSMauro Carvalho Chehab static int saa7134_alsa_init(void)
1211b285192aSMauro Carvalho Chehab {
12129f1f4b64SJakob Koschel struct saa7134_dev *dev;
1213b285192aSMauro Carvalho Chehab
1214b285192aSMauro Carvalho Chehab saa7134_dmasound_init = alsa_device_init;
1215b285192aSMauro Carvalho Chehab saa7134_dmasound_exit = alsa_device_exit;
1216b285192aSMauro Carvalho Chehab
121783582009SMauro Carvalho Chehab pr_info("saa7134 ALSA driver for DMA sound loaded\n");
1218b285192aSMauro Carvalho Chehab
12193f3475a5SYang Yingliang list_for_each_entry(dev, &saa7134_devlist, devlist) {
1220b285192aSMauro Carvalho Chehab if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
122183582009SMauro Carvalho Chehab pr_info("%s/alsa: %s doesn't support digital audio\n",
1222b285192aSMauro Carvalho Chehab dev->name, saa7134_boards[dev->board].name);
1223b285192aSMauro Carvalho Chehab else
1224b285192aSMauro Carvalho Chehab alsa_device_init(dev);
1225b285192aSMauro Carvalho Chehab }
1226b285192aSMauro Carvalho Chehab
12279f1f4b64SJakob Koschel if (list_empty(&saa7134_devlist))
122883582009SMauro Carvalho Chehab pr_info("saa7134 ALSA: no saa7134 cards found\n");
1229b285192aSMauro Carvalho Chehab
1230b285192aSMauro Carvalho Chehab return 0;
1231b285192aSMauro Carvalho Chehab
1232b285192aSMauro Carvalho Chehab }
1233b285192aSMauro Carvalho Chehab
1234b285192aSMauro Carvalho Chehab /*
1235b285192aSMauro Carvalho Chehab * Module destructor
1236b285192aSMauro Carvalho Chehab */
1237b285192aSMauro Carvalho Chehab
saa7134_alsa_exit(void)1238b285192aSMauro Carvalho Chehab static void saa7134_alsa_exit(void)
1239b285192aSMauro Carvalho Chehab {
1240b285192aSMauro Carvalho Chehab int idx;
1241b285192aSMauro Carvalho Chehab
1242b285192aSMauro Carvalho Chehab for (idx = 0; idx < SNDRV_CARDS; idx++) {
1243ac75fe5dSMauro Carvalho Chehab if (snd_saa7134_cards[idx])
1244b285192aSMauro Carvalho Chehab snd_card_free(snd_saa7134_cards[idx]);
1245b285192aSMauro Carvalho Chehab }
1246b285192aSMauro Carvalho Chehab
1247b285192aSMauro Carvalho Chehab saa7134_dmasound_init = NULL;
1248b285192aSMauro Carvalho Chehab saa7134_dmasound_exit = NULL;
124983582009SMauro Carvalho Chehab pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
1250b285192aSMauro Carvalho Chehab
1251b285192aSMauro Carvalho Chehab return;
1252b285192aSMauro Carvalho Chehab }
1253b285192aSMauro Carvalho Chehab
1254b285192aSMauro Carvalho Chehab /* We initialize this late, to make sure the sound system is up and running */
1255b285192aSMauro Carvalho Chehab late_initcall(saa7134_alsa_init);
1256b285192aSMauro Carvalho Chehab module_exit(saa7134_alsa_exit);
1257*c22010ceSJeff Johnson MODULE_DESCRIPTION("Philips SAA7134 DMA audio support");
1258b285192aSMauro Carvalho Chehab MODULE_LICENSE("GPL");
1259b285192aSMauro Carvalho Chehab MODULE_AUTHOR("Ricardo Cerqueira");
1260