blob: 1d2c310ce838e5fb415ebd4d3b533ac11b8c4b9b [file] [log] [blame]
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001/*
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08002 * SAA713x ALSA support for V4L
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08003 *
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 2
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080017 */
18
Mauro Carvalho Chehab9a12ccf2015-04-30 06:44:56 -030019#include "saa7134.h"
20#include "saa7134-reg.h"
21
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080022#include <linux/init.h>
23#include <linux/slab.h>
24#include <linux/time.h>
25#include <linux/wait.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080026#include <linux/module.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080027#include <sound/core.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080028#include <sound/control.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080029#include <sound/pcm.h>
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -080030#include <sound/pcm_params.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080031#include <sound/initval.h>
Nickolay V. Shmyrev871242b2005-11-13 16:07:51 -080032#include <linux/interrupt.h>
Mauro Carvalho Chehabba0d3422014-05-13 23:02:14 -030033#include <linux/vmalloc.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080034
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080035/*
36 * Configuration macros
37 */
38
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080039/* defaults */
Oldřich Jedlička736dada2009-08-22 15:13:51 -030040#define MIXER_ADDR_UNSELECTED -1
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080041#define MIXER_ADDR_TVTUNER 0
42#define MIXER_ADDR_LINE1 1
43#define MIXER_ADDR_LINE2 2
44#define MIXER_ADDR_LAST 2
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080045
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080046
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080047static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
48static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
Ricardo Cerqueiraede22412006-02-27 00:08:11 -030049static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080050
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080051module_param_array(index, int, NULL, 0444);
Ricardo Cerqueiraede22412006-02-27 00:08:11 -030052module_param_array(enable, int, NULL, 0444);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080053MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
Ricardo Cerqueiraede22412006-02-27 00:08:11 -030054MODULE_PARM_DESC(enable, "Enable (or not) the SAA7134 capture interface(s).");
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080055
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080056/*
57 * Main chip structure
58 */
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080059
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080060typedef struct snd_card_saa7134 {
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010061 struct snd_card *card;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080062 spinlock_t mixer_lock;
63 int mixer_volume[MIXER_ADDR_LAST+1][2];
Oldřich Jedlička736dada2009-08-22 15:13:51 -030064 int capture_source_addr;
65 int capture_source[2];
66 struct snd_kcontrol *capture_ctl[MIXER_ADDR_LAST+1];
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080067 struct pci_dev *pci;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080068 struct saa7134_dev *dev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080069
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080070 unsigned long iobase;
Rafał Bilskiaac0ca62007-06-20 05:36:26 -030071 s16 irq;
72 u16 mute_was_on;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080073
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080074 spinlock_t lock;
75} snd_card_saa7134_t;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080076
Nickolay V. Shmyrev07eef6c2005-11-08 21:38:14 -080077
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080078/*
79 * PCM structure
80 */
81
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080082typedef struct snd_card_saa7134_pcm {
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080083 struct saa7134_dev *dev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080084
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080085 spinlock_t lock;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -080086
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010087 struct snd_pcm_substream *substream;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080088} snd_card_saa7134_pcm_t;
89
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +010090static struct snd_card *snd_saa7134_cards[SNDRV_CARDS];
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080091
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080092
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080093/*
94 * saa7134 DMA audio stop
95 *
96 * Called when the capture device is released or the buffer overflows
97 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -080098 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080099 *
100 */
101
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800102static void saa7134_dma_stop(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800103{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800104 dev->dmasound.dma_blk = -1;
105 dev->dmasound.dma_running = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800106 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800107}
108
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800109/*
110 * saa7134 DMA audio start
111 *
112 * Called when preparing the capture device for use
113 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800114 * - Copied verbatim from saa7134-oss's dsp_dma_start.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800115 *
116 */
117
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800118static void saa7134_dma_start(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800119{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800120 dev->dmasound.dma_blk = 0;
121 dev->dmasound.dma_running = 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800122 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800123}
124
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800125/*
126 * saa7134 audio DMA IRQ handler
127 *
128 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
129 * Handles shifting between the 2 buffers, manages the read counters,
130 * and notifies ALSA when periods elapse
131 *
132 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
133 *
134 */
135
Adrian Bunk943a4902005-12-01 00:51:35 -0800136static void saa7134_irq_alsa_done(struct saa7134_dev *dev,
137 unsigned long status)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800138{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800139 int next_blk, reg = 0;
140
141 spin_lock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800142 if (UNSET == dev->dmasound.dma_blk) {
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300143 pr_debug("irq: recording stopped\n");
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800144 goto done;
145 }
146 if (0 != (status & 0x0f000000))
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300147 pr_debug("irq: lost %ld\n", (status >> 24) & 0x0f);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800148 if (0 == (status & 0x10000000)) {
149 /* odd */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800150 if (0 == (dev->dmasound.dma_blk & 0x01))
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800151 reg = SAA7134_RS_BA1(6);
152 } else {
153 /* even */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800154 if (1 == (dev->dmasound.dma_blk & 0x01))
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800155 reg = SAA7134_RS_BA2(6);
156 }
157 if (0 == reg) {
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300158 pr_debug("irq: field oops [%s]\n",
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800159 (status & 0x10000000) ? "even" : "odd");
160 goto done;
161 }
162
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800163 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300164 pr_debug("irq: overrun [full=%d/%d] - Blocks in %d\n",
165 dev->dmasound.read_count,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800166 dev->dmasound.bufsize, dev->dmasound.blocks);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800167 spin_unlock(&dev->slock);
Takashi Iwai1fb85102014-11-07 17:08:28 +0100168 snd_pcm_stop_xrun(dev->dmasound.substream);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800169 return;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800170 }
171
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800172 /* next block addr */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800173 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
174 saa_writel(reg,next_blk * dev->dmasound.blksize);
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300175 pr_debug("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
176 (status & 0x10000000) ? "even" : "odd ", next_blk,
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300177 next_blk * dev->dmasound.blksize, dev->dmasound.blocks,
178 dev->dmasound.blksize, dev->dmasound.read_count);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800179
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800180 /* update status & wake waiting readers */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800181 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
182 dev->dmasound.read_count += dev->dmasound.blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800183
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800184 dev->dmasound.recording_on = reg;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800185
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800186 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800187 spin_unlock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800188 snd_pcm_period_elapsed(dev->dmasound.substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800189 spin_lock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800190 }
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800191
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800192 done:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800193 spin_unlock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800194
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800195}
196
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800197/*
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800198 * IRQ request handler
199 *
200 * Runs along with saa7134's IRQ handler, discards anything that isn't
201 * DMA sound
202 *
203 */
204
David Howells7d12e782006-10-05 14:55:46 +0100205static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id)
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800206{
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800207 struct saa7134_dmasound *dmasound = dev_id;
208 struct saa7134_dev *dev = dmasound->priv_data;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800209
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800210 unsigned long report, status;
211 int loop, handled = 0;
212
213 for (loop = 0; loop < 10; loop++) {
214 report = saa_readl(SAA7134_IRQ_REPORT);
215 status = saa_readl(SAA7134_IRQ_STATUS);
216
217 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
218 handled = 1;
Heikki Lindholm843b1372007-11-30 13:27:26 -0300219 saa_writel(SAA7134_IRQ_REPORT,
220 SAA7134_IRQ_REPORT_DONE_RA3);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800221 saa7134_irq_alsa_done(dev, status);
222 } else {
223 goto out;
224 }
225 }
226
227 if (loop == 10) {
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300228 pr_debug("error! looping IRQ!");
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800229 }
230
231out:
232 return IRQ_RETVAL(handled);
233}
234
235/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800236 * ALSA capture trigger
237 *
238 * - One of the ALSA capture callbacks.
239 *
240 * Called whenever a capture is started or stopped. Must be defined,
241 * but there's nothing we want to do here
242 *
243 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800244
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100245static int snd_card_saa7134_capture_trigger(struct snd_pcm_substream * substream,
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800246 int cmd)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800247{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100248 struct snd_pcm_runtime *runtime = substream->runtime;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800249 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
250 struct saa7134_dev *dev=pcm->dev;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800251 int err = 0;
252
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800253 spin_lock(&dev->slock);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800254 if (cmd == SNDRV_PCM_TRIGGER_START) {
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800255 /* start dma */
256 saa7134_dma_start(dev);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800257 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800258 /* stop dma */
259 saa7134_dma_stop(dev);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800260 } else {
261 err = -EINVAL;
262 }
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800263 spin_unlock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800264
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800265 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800266}
267
Hans Verkuil15e64f02014-04-17 03:57:14 -0300268static int saa7134_alsa_dma_init(struct saa7134_dev *dev, int nr_pages)
269{
270 struct saa7134_dmasound *dma = &dev->dmasound;
271 struct page *pg;
272 int i;
273
274 dma->vaddr = vmalloc_32(nr_pages << PAGE_SHIFT);
275 if (NULL == dma->vaddr) {
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300276 pr_debug("vmalloc_32(%d pages) failed\n", nr_pages);
Hans Verkuil15e64f02014-04-17 03:57:14 -0300277 return -ENOMEM;
278 }
279
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300280 pr_debug("vmalloc is at addr 0x%08lx, size=%d\n",
Hans Verkuil15e64f02014-04-17 03:57:14 -0300281 (unsigned long)dma->vaddr,
282 nr_pages << PAGE_SHIFT);
283
284 memset(dma->vaddr, 0, nr_pages << PAGE_SHIFT);
285 dma->nr_pages = nr_pages;
286
287 dma->sglist = vzalloc(dma->nr_pages * sizeof(*dma->sglist));
288 if (NULL == dma->sglist)
289 goto vzalloc_err;
290
291 sg_init_table(dma->sglist, dma->nr_pages);
292 for (i = 0; i < dma->nr_pages; i++) {
293 pg = vmalloc_to_page(dma->vaddr + i * PAGE_SIZE);
294 if (NULL == pg)
295 goto vmalloc_to_page_err;
296 sg_set_page(&dma->sglist[i], pg, PAGE_SIZE, 0);
297 }
298 return 0;
299
300vmalloc_to_page_err:
301 vfree(dma->sglist);
302 dma->sglist = NULL;
303vzalloc_err:
304 vfree(dma->vaddr);
305 dma->vaddr = NULL;
306 return -ENOMEM;
307}
308
309static int saa7134_alsa_dma_map(struct saa7134_dev *dev)
310{
311 struct saa7134_dmasound *dma = &dev->dmasound;
312
313 dma->sglen = dma_map_sg(&dev->pci->dev, dma->sglist,
314 dma->nr_pages, PCI_DMA_FROMDEVICE);
315
316 if (0 == dma->sglen) {
317 pr_warn("%s: saa7134_alsa_map_sg failed\n", __func__);
318 return -ENOMEM;
319 }
320 return 0;
321}
322
323static int saa7134_alsa_dma_unmap(struct saa7134_dev *dev)
324{
325 struct saa7134_dmasound *dma = &dev->dmasound;
326
327 if (!dma->sglen)
328 return 0;
329
330 dma_unmap_sg(&dev->pci->dev, dma->sglist, dma->sglen, PCI_DMA_FROMDEVICE);
331 dma->sglen = 0;
332 return 0;
333}
334
335static int saa7134_alsa_dma_free(struct saa7134_dmasound *dma)
336{
337 vfree(dma->sglist);
338 dma->sglist = NULL;
339 vfree(dma->vaddr);
340 dma->vaddr = NULL;
341 return 0;
342}
343
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800344/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800345 * DMA buffer initialization
346 *
347 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
348 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
349 * usage of V4L's
350 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800351 * - Copied verbatim from saa7134-oss.
352 *
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800353 */
354
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800355static int dsp_buffer_init(struct saa7134_dev *dev)
356{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800357 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800358
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800359 BUG_ON(!dev->dmasound.bufsize);
360
Hans Verkuil15e64f02014-04-17 03:57:14 -0300361 err = saa7134_alsa_dma_init(dev,
362 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800363 if (0 != err)
364 return err;
365 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800366}
367
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800368/*
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800369 * DMA buffer release
370 *
371 * Called after closing the device, during snd_card_saa7134_capture_close
372 *
373 */
374
375static int dsp_buffer_free(struct saa7134_dev *dev)
376{
Eric Sesterhennae246012006-03-13 13:17:11 -0300377 BUG_ON(!dev->dmasound.blksize);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800378
Hans Verkuil15e64f02014-04-17 03:57:14 -0300379 saa7134_alsa_dma_free(&dev->dmasound);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800380
381 dev->dmasound.blocks = 0;
382 dev->dmasound.blksize = 0;
383 dev->dmasound.bufsize = 0;
384
Michael Krufkyb930e1d2007-08-27 18:16:54 -0300385 return 0;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800386}
387
Oldřich Jedlička736dada2009-08-22 15:13:51 -0300388/*
389 * Setting the capture source and updating the ALSA controls
390 */
391static int snd_saa7134_capsrc_set(struct snd_kcontrol *kcontrol,
392 int left, int right, bool force_notify)
393{
394 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
395 int change = 0, addr = kcontrol->private_value;
396 int active, old_addr;
397 u32 anabar, xbarin;
398 int analog_io, rate;
399 struct saa7134_dev *dev;
400
401 dev = chip->dev;
402
403 spin_lock_irq(&chip->mixer_lock);
404
405 active = left != 0 || right != 0;
406 old_addr = chip->capture_source_addr;
407
408 /* The active capture source cannot be deactivated */
409 if (active) {
410 change = old_addr != addr ||
411 chip->capture_source[0] != left ||
412 chip->capture_source[1] != right;
413
414 chip->capture_source[0] = left;
415 chip->capture_source[1] = right;
416 chip->capture_source_addr = addr;
417 dev->dmasound.input = addr;
418 }
419 spin_unlock_irq(&chip->mixer_lock);
420
421 if (change) {
422 switch (dev->pci->device) {
423
424 case PCI_DEVICE_ID_PHILIPS_SAA7134:
425 switch (addr) {
426 case MIXER_ADDR_TVTUNER:
427 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
428 0xc0, 0xc0);
429 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
430 0x03, 0x00);
431 break;
432 case MIXER_ADDR_LINE1:
433 case MIXER_ADDR_LINE2:
434 analog_io = (MIXER_ADDR_LINE1 == addr) ?
435 0x00 : 0x08;
436 rate = (32000 == dev->dmasound.rate) ?
437 0x01 : 0x03;
438 saa_andorb(SAA7134_ANALOG_IO_SELECT,
439 0x08, analog_io);
440 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL,
441 0xc0, 0x80);
442 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,
443 0x03, rate);
444 break;
445 }
446
447 break;
448 case PCI_DEVICE_ID_PHILIPS_SAA7133:
449 case PCI_DEVICE_ID_PHILIPS_SAA7135:
450 xbarin = 0x03; /* adc */
451 anabar = 0;
452 switch (addr) {
453 case MIXER_ADDR_TVTUNER:
454 xbarin = 0; /* Demodulator */
455 anabar = 2; /* DACs */
456 break;
457 case MIXER_ADDR_LINE1:
458 anabar = 0; /* aux1, aux1 */
459 break;
460 case MIXER_ADDR_LINE2:
461 anabar = 9; /* aux2, aux2 */
462 break;
463 }
464
465 /* output xbar always main channel */
466 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1,
467 0xbbbb10);
468
469 if (left || right) {
470 /* We've got data, turn the input on */
471 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
472 xbarin);
473 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
474 } else {
475 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1,
476 0);
477 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
478 }
479 break;
480 }
481 }
482
483 if (change) {
484 if (force_notify)
485 snd_ctl_notify(chip->card,
486 SNDRV_CTL_EVENT_MASK_VALUE,
487 &chip->capture_ctl[addr]->id);
488
489 if (old_addr != MIXER_ADDR_UNSELECTED && old_addr != addr)
490 snd_ctl_notify(chip->card,
491 SNDRV_CTL_EVENT_MASK_VALUE,
492 &chip->capture_ctl[old_addr]->id);
493 }
494
495 return change;
496}
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800497
498/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800499 * ALSA PCM preparation
500 *
501 * - One of the ALSA capture callbacks.
502 *
503 * Called right after the capture device is opened, this function configures
504 * the buffer using the previously defined functions, allocates the memory,
505 * sets up the hardware registers, and then starts the DMA. When this function
506 * returns, the audio should be flowing.
507 *
508 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800509
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100510static int snd_card_saa7134_capture_prepare(struct snd_pcm_substream * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800511{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100512 struct snd_pcm_runtime *runtime = substream->runtime;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800513 int bswap, sign;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800514 u32 fmt, control;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800515 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800516 struct saa7134_dev *dev;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800517 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800518
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800519 pcm->dev->dmasound.substream = substream;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800520
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800521 dev = saa7134->dev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800522
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800523 if (snd_pcm_format_width(runtime->format) == 8)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800524 fmt = 0x00;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800525 else
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800526 fmt = 0x01;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800527
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800528 if (snd_pcm_format_signed(runtime->format))
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800529 sign = 1;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800530 else
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800531 sign = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800532
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800533 if (snd_pcm_format_big_endian(runtime->format))
534 bswap = 1;
535 else
536 bswap = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800537
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800538 switch (dev->pci->device) {
539 case PCI_DEVICE_ID_PHILIPS_SAA7134:
540 if (1 == runtime->channels)
541 fmt |= (1 << 3);
542 if (2 == runtime->channels)
543 fmt |= (3 << 3);
544 if (sign)
545 fmt |= 0x04;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800546
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800547 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
548 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
549 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
550 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800551 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800552
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800553 break;
554 case PCI_DEVICE_ID_PHILIPS_SAA7133:
555 case PCI_DEVICE_ID_PHILIPS_SAA7135:
556 if (1 == runtime->channels)
557 fmt |= (1 << 4);
558 if (2 == runtime->channels)
559 fmt |= (2 << 4);
560 if (!sign)
561 fmt |= 0x04;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800562 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800563 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800564 break;
565 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800566
Mauro Carvalho Chehab7592eef2015-04-30 06:59:53 -0300567 pr_debug("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800568 runtime->format, runtime->channels, fmt,
569 bswap ? 'b' : '-');
570 /* dma: setup channel 6 (= AUDIO) */
571 control = SAA7134_RS_CONTROL_BURST_16 |
572 SAA7134_RS_CONTROL_ME |
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800573 (dev->dmasound.pt.dma >> 12);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800574 if (bswap)
575 control |= SAA7134_RS_CONTROL_BSWAP;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800576
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800577 saa_writel(SAA7134_RS_BA1(6),0);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800578 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800579 saa_writel(SAA7134_RS_PITCH(6),0);
580 saa_writel(SAA7134_RS_CONTROL(6),control);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800581
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800582 dev->dmasound.rate = runtime->rate;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800583
Oldřich Jedlička736dada2009-08-22 15:13:51 -0300584 /* Setup and update the card/ALSA controls */
585 snd_saa7134_capsrc_set(saa7134->capture_ctl[dev->dmasound.input], 1, 1,
586 true);
587
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800588 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800589
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800590}
591
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800592/*
593 * ALSA pointer fetching
594 *
595 * - One of the ALSA capture callbacks.
596 *
597 * Called whenever a period elapses, it must return the current hardware
598 * position of the buffer.
599 * Also resets the read counter used to prevent overruns
600 *
601 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800602
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100603static snd_pcm_uframes_t
604snd_card_saa7134_capture_pointer(struct snd_pcm_substream * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800605{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100606 struct snd_pcm_runtime *runtime = substream->runtime;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800607 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
608 struct saa7134_dev *dev=pcm->dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800609
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800610 if (dev->dmasound.read_count) {
611 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
612 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
613 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
614 dev->dmasound.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800615 }
616
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800617 return bytes_to_frames(runtime, dev->dmasound.read_offset);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800618}
619
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800620/*
621 * ALSA hardware capabilities definition
Oldřich Jedlička7b543602009-08-18 16:24:54 -0300622 *
623 * Report only 32kHz for ALSA:
624 *
625 * - SAA7133/35 uses DDEP (DemDec Easy Programming mode), which works in 32kHz
626 * only
627 * - SAA7134 for TV mode uses DemDec mode (32kHz)
628 * - Radio works in 32kHz only
629 * - When recording 48kHz from Line1/Line2, switching of capture source to TV
630 * means
631 * switching to 32kHz without any frequency translation
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800632 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800633
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100634static struct snd_pcm_hardware snd_card_saa7134_capture =
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800635{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800636 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800637 SNDRV_PCM_INFO_BLOCK_TRANSFER |
638 SNDRV_PCM_INFO_MMAP_VALID),
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800639 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
640 SNDRV_PCM_FMTBIT_S16_BE | \
641 SNDRV_PCM_FMTBIT_S8 | \
642 SNDRV_PCM_FMTBIT_U8 | \
643 SNDRV_PCM_FMTBIT_U16_LE | \
644 SNDRV_PCM_FMTBIT_U16_BE,
Oldřich Jedlička7b543602009-08-18 16:24:54 -0300645 .rates = SNDRV_PCM_RATE_32000,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800646 .rate_min = 32000,
Oldřich Jedlička7b543602009-08-18 16:24:54 -0300647 .rate_max = 32000,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800648 .channels_min = 1,
649 .channels_max = 2,
650 .buffer_bytes_max = (256*1024),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800651 .period_bytes_min = 64,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800652 .period_bytes_max = (256*1024),
Heikki Lindholm174eb8e2007-11-23 18:59:18 -0300653 .periods_min = 4,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800654 .periods_max = 1024,
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800655};
656
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100657static void snd_card_saa7134_runtime_free(struct snd_pcm_runtime *runtime)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800658{
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800659 snd_card_saa7134_pcm_t *pcm = runtime->private_data;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800660
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800661 kfree(pcm);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800662}
663
664
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800665/*
666 * ALSA hardware params
667 *
668 * - One of the ALSA capture callbacks.
669 *
670 * Called on initialization, right before the PCM preparation
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800671 *
672 */
673
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100674static int snd_card_saa7134_hw_params(struct snd_pcm_substream * substream,
675 struct snd_pcm_hw_params * hw_params)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800676{
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800677 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
678 struct saa7134_dev *dev;
679 unsigned int period_size, periods;
680 int err;
681
682 period_size = params_period_bytes(hw_params);
683 periods = params_periods(hw_params);
684
Takashi Iwai5e246b82008-08-08 17:12:47 +0200685 if (period_size < 0x100 || period_size > 0x10000)
686 return -EINVAL;
687 if (periods < 4)
688 return -EINVAL;
689 if (period_size * periods > 1024 * 1024)
690 return -EINVAL;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800691
692 dev = saa7134->dev;
693
694 if (dev->dmasound.blocks == periods &&
695 dev->dmasound.blksize == period_size)
696 return 0;
697
698 /* release the old buffer */
699 if (substream->runtime->dma_area) {
700 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
Hans Verkuil15e64f02014-04-17 03:57:14 -0300701 saa7134_alsa_dma_unmap(dev);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800702 dsp_buffer_free(dev);
703 substream->runtime->dma_area = NULL;
704 }
705 dev->dmasound.blocks = periods;
706 dev->dmasound.blksize = period_size;
707 dev->dmasound.bufsize = period_size * periods;
708
709 err = dsp_buffer_init(dev);
710 if (0 != err) {
711 dev->dmasound.blocks = 0;
712 dev->dmasound.blksize = 0;
713 dev->dmasound.bufsize = 0;
714 return err;
715 }
716
Hans Verkuil15e64f02014-04-17 03:57:14 -0300717 err = saa7134_alsa_dma_map(dev);
Hans Verkuile72936d2014-04-17 03:48:50 -0300718 if (err) {
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800719 dsp_buffer_free(dev);
720 return err;
721 }
Hans Verkuile72936d2014-04-17 03:48:50 -0300722 err = saa7134_pgtable_alloc(dev->pci, &dev->dmasound.pt);
723 if (err) {
Hans Verkuil15e64f02014-04-17 03:57:14 -0300724 saa7134_alsa_dma_unmap(dev);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800725 dsp_buffer_free(dev);
726 return err;
727 }
Hans Verkuile72936d2014-04-17 03:48:50 -0300728 err = saa7134_pgtable_build(dev->pci, &dev->dmasound.pt,
729 dev->dmasound.sglist, dev->dmasound.sglen, 0);
730 if (err) {
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800731 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
Hans Verkuil15e64f02014-04-17 03:57:14 -0300732 saa7134_alsa_dma_unmap(dev);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800733 dsp_buffer_free(dev);
734 return err;
735 }
736
737 /* I should be able to use runtime->dma_addr in the control
738 byte, but it doesn't work. So I allocate the DMA using the
739 V4L functions, and force ALSA to use that as the DMA area */
740
Hans Verkuil15e64f02014-04-17 03:57:14 -0300741 substream->runtime->dma_area = dev->dmasound.vaddr;
Alexander E. Patrakov32d111a2007-10-31 11:40:09 -0300742 substream->runtime->dma_bytes = dev->dmasound.bufsize;
743 substream->runtime->dma_addr = 0;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800744
Alexander E. Patrakov32d111a2007-10-31 11:40:09 -0300745 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800746
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800747}
748
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800749/*
750 * ALSA hardware release
751 *
752 * - One of the ALSA capture callbacks.
753 *
754 * Called after closing the device, but before snd_card_saa7134_capture_close
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800755 * It stops the DMA audio and releases the buffers.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800756 *
757 */
758
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100759static int snd_card_saa7134_hw_free(struct snd_pcm_substream * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800760{
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800761 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
762 struct saa7134_dev *dev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800763
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800764 dev = saa7134->dev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800765
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800766 if (substream->runtime->dma_area) {
767 saa7134_pgtable_free(dev->pci, &dev->dmasound.pt);
Hans Verkuil15e64f02014-04-17 03:57:14 -0300768 saa7134_alsa_dma_unmap(dev);
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800769 dsp_buffer_free(dev);
770 substream->runtime->dma_area = NULL;
771 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800772
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800773 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800774}
775
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800776/*
777 * ALSA capture finish
778 *
779 * - One of the ALSA capture callbacks.
780 *
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800781 * Called after closing the device.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800782 *
783 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800784
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100785static int snd_card_saa7134_capture_close(struct snd_pcm_substream * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800786{
Ricardo Cerqueiradafcaaf2006-09-06 06:25:38 -0300787 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
788 struct saa7134_dev *dev = saa7134->dev;
789
Rafał Bilskiaac0ca62007-06-20 05:36:26 -0300790 if (saa7134->mute_was_on) {
791 dev->ctl_mute = 1;
792 saa7134_tvaudio_setmute(dev);
793 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800794 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800795}
796
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800797/*
798 * ALSA capture start
799 *
800 * - One of the ALSA capture callbacks.
801 *
802 * Called when opening the device. It creates and populates the PCM
803 * structure
804 *
805 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800806
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100807static int snd_card_saa7134_capture_open(struct snd_pcm_substream * substream)
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800808{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100809 struct snd_pcm_runtime *runtime = substream->runtime;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800810 snd_card_saa7134_pcm_t *pcm;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800811 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300812 struct saa7134_dev *dev;
Hartmut Hackmann5cfbacd2006-11-21 19:59:27 -0300813 int amux, err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800814
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300815 if (!saa7134) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -0300816 pr_err("BUG: saa7134 can't find device struct."
Mauro Carvalho Chehab83ee87a2008-06-14 09:41:18 -0300817 " Can't proceed with open\n");
818 return -ENODEV;
819 }
820 dev = saa7134->dev;
Ingo Molnar3593cab2006-02-07 06:49:14 -0200821 mutex_lock(&dev->dmasound.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800822
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800823 dev->dmasound.read_count = 0;
824 dev->dmasound.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800825
Hartmut Hackmann5cfbacd2006-11-21 19:59:27 -0300826 amux = dev->input->amux;
827 if ((amux < 1) || (amux > 3))
828 amux = 1;
829 dev->dmasound.input = amux - 1;
830
Ingo Molnar3593cab2006-02-07 06:49:14 -0200831 mutex_unlock(&dev->dmasound.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800832
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800833 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
834 if (pcm == NULL)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800835 return -ENOMEM;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800836
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800837 pcm->dev=saa7134->dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800838
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -0800839 spin_lock_init(&pcm->lock);
840
841 pcm->substream = substream;
842 runtime->private_data = pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800843 runtime->private_free = snd_card_saa7134_runtime_free;
844 runtime->hw = snd_card_saa7134_capture;
845
Rafał Bilskiaac0ca62007-06-20 05:36:26 -0300846 if (dev->ctl_mute != 0) {
847 saa7134->mute_was_on = 1;
848 dev->ctl_mute = 0;
849 saa7134_tvaudio_setmute(dev);
850 }
Ricardo Cerqueiradafcaaf2006-09-06 06:25:38 -0300851
Heikki Lindholm174eb8e2007-11-23 18:59:18 -0300852 err = snd_pcm_hw_constraint_integer(runtime,
853 SNDRV_PCM_HW_PARAM_PERIODS);
854 if (err < 0)
855 return err;
856
857 err = snd_pcm_hw_constraint_step(runtime, 0,
858 SNDRV_PCM_HW_PARAM_PERIODS, 2);
859 if (err < 0)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800860 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800861
862 return 0;
863}
864
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800865/*
Alexander E. Patrakov32d111a2007-10-31 11:40:09 -0300866 * page callback (needed for mmap)
867 */
868
869static struct page *snd_card_saa7134_page(struct snd_pcm_substream *substream,
870 unsigned long offset)
871{
872 void *pageptr = substream->runtime->dma_area + offset;
873 return vmalloc_to_page(pageptr);
874}
875
876/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800877 * ALSA capture callbacks definition
878 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800879
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100880static struct snd_pcm_ops snd_card_saa7134_capture_ops = {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800881 .open = snd_card_saa7134_capture_open,
882 .close = snd_card_saa7134_capture_close,
883 .ioctl = snd_pcm_lib_ioctl,
884 .hw_params = snd_card_saa7134_hw_params,
885 .hw_free = snd_card_saa7134_hw_free,
886 .prepare = snd_card_saa7134_capture_prepare,
887 .trigger = snd_card_saa7134_capture_trigger,
888 .pointer = snd_card_saa7134_capture_pointer,
Alexander E. Patrakov32d111a2007-10-31 11:40:09 -0300889 .page = snd_card_saa7134_page,
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800890};
891
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800892/*
893 * ALSA PCM setup
894 *
895 * Called when initializing the board. Sets up the name and hooks up
896 * the callbacks
897 *
898 */
899
900static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800901{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100902 struct snd_pcm *pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800903 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800904
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800905 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800906 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800907 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
908 pcm->private_data = saa7134;
909 pcm->info_flags = 0;
910 strcpy(pcm->name, "SAA7134 PCM");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800911 return 0;
912}
913
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800914#define SAA713x_VOLUME(xname, xindex, addr) \
915{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
916 .info = snd_saa7134_volume_info, \
917 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
918 .private_value = addr }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800919
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100920static int snd_saa7134_volume_info(struct snd_kcontrol * kcontrol,
921 struct snd_ctl_elem_info * uinfo)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800922{
923 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
924 uinfo->count = 2;
925 uinfo->value.integer.min = 0;
926 uinfo->value.integer.max = 20;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800927 return 0;
928}
929
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100930static int snd_saa7134_volume_get(struct snd_kcontrol * kcontrol,
931 struct snd_ctl_elem_value * ucontrol)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800932{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800933 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800934 int addr = kcontrol->private_value;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800935
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800936 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
937 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800938 return 0;
939}
940
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +0100941static int snd_saa7134_volume_put(struct snd_kcontrol * kcontrol,
942 struct snd_ctl_elem_value * ucontrol)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800943{
944 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
Hartmut Hackmann5cfbacd2006-11-21 19:59:27 -0300945 struct saa7134_dev *dev = chip->dev;
946
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800947 int change, addr = kcontrol->private_value;
948 int left, right;
949
950 left = ucontrol->value.integer.value[0];
951 if (left < 0)
952 left = 0;
953 if (left > 20)
954 left = 20;
955 right = ucontrol->value.integer.value[1];
956 if (right < 0)
957 right = 0;
958 if (right > 20)
959 right = 20;
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -0800960 spin_lock_irq(&chip->mixer_lock);
Hartmut Hackmann5cfbacd2006-11-21 19:59:27 -0300961 change = 0;
962 if (chip->mixer_volume[addr][0] != left) {
963 change = 1;
964 right = left;
965 }
966 if (chip->mixer_volume[addr][1] != right) {
967 change = 1;
968 left = right;
969 }
970 if (change) {
971 switch (dev->pci->device) {
972 case PCI_DEVICE_ID_PHILIPS_SAA7134:
973 switch (addr) {
974 case MIXER_ADDR_TVTUNER:
975 left = 20;
976 break;
977 case MIXER_ADDR_LINE1:
978 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x10,
979 (left > 10) ? 0x00 : 0x10);
980 break;
981 case MIXER_ADDR_LINE2:
982 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x20,
983 (left > 10) ? 0x00 : 0x20);
984 break;
985 }
986 break;
987 case PCI_DEVICE_ID_PHILIPS_SAA7133:
988 case PCI_DEVICE_ID_PHILIPS_SAA7135:
989 switch (addr) {
990 case MIXER_ADDR_TVTUNER:
991 left = 20;
992 break;
993 case MIXER_ADDR_LINE1:
994 saa_andorb(0x0594, 0x10,
995 (left > 10) ? 0x00 : 0x10);
996 break;
997 case MIXER_ADDR_LINE2:
998 saa_andorb(0x0594, 0x20,
999 (left > 10) ? 0x00 : 0x20);
1000 break;
1001 }
1002 break;
1003 }
1004 chip->mixer_volume[addr][0] = left;
1005 chip->mixer_volume[addr][1] = right;
1006 }
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001007 spin_unlock_irq(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001008 return change;
1009}
1010
1011#define SAA713x_CAPSRC(xname, xindex, addr) \
1012{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1013 .info = snd_saa7134_capsrc_info, \
1014 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
1015 .private_value = addr }
1016
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001017static int snd_saa7134_capsrc_info(struct snd_kcontrol * kcontrol,
1018 struct snd_ctl_elem_info * uinfo)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001019{
1020 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001021 uinfo->count = 2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001022 uinfo->value.integer.min = 0;
1023 uinfo->value.integer.max = 1;
1024 return 0;
1025}
1026
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001027static int snd_saa7134_capsrc_get(struct snd_kcontrol * kcontrol,
1028 struct snd_ctl_elem_value * ucontrol)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001029{
1030 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001031 int addr = kcontrol->private_value;
1032
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001033 spin_lock_irq(&chip->mixer_lock);
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001034 if (chip->capture_source_addr == addr) {
1035 ucontrol->value.integer.value[0] = chip->capture_source[0];
1036 ucontrol->value.integer.value[1] = chip->capture_source[1];
1037 } else {
1038 ucontrol->value.integer.value[0] = 0;
1039 ucontrol->value.integer.value[1] = 0;
1040 }
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001041 spin_unlock_irq(&chip->mixer_lock);
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001042
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001043 return 0;
1044}
1045
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001046static int snd_saa7134_capsrc_put(struct snd_kcontrol * kcontrol,
1047 struct snd_ctl_elem_value * ucontrol)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001048{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001049 int left, right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001050 left = ucontrol->value.integer.value[0] & 1;
1051 right = ucontrol->value.integer.value[1] & 1;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001052
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001053 return snd_saa7134_capsrc_set(kcontrol, left, right, false);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001054}
1055
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001056static struct snd_kcontrol_new snd_saa7134_volume_controls[] = {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001057SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001058SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001059SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001060};
1061
1062static struct snd_kcontrol_new snd_saa7134_capture_controls[] = {
1063SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
1064SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001065SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001066};
1067
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001068/*
1069 * ALSA mixer setup
1070 *
1071 * Called when initializing the board. Sets up the name and hooks up
1072 * the callbacks
1073 *
1074 */
1075
1076static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001077{
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001078 struct snd_card *card = chip->card;
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001079 struct snd_kcontrol *kcontrol;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001080 unsigned int idx;
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001081 int err, addr;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001082
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001083 strcpy(card->mixername, "SAA7134 Mixer");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001084
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001085 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_volume_controls); idx++) {
1086 kcontrol = snd_ctl_new1(&snd_saa7134_volume_controls[idx],
1087 chip);
1088 err = snd_ctl_add(card, kcontrol);
1089 if (err < 0)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001090 return err;
1091 }
Oldřich Jedlička736dada2009-08-22 15:13:51 -03001092
1093 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_capture_controls); idx++) {
1094 kcontrol = snd_ctl_new1(&snd_saa7134_capture_controls[idx],
1095 chip);
1096 addr = snd_saa7134_capture_controls[idx].private_value;
1097 chip->capture_ctl[addr] = kcontrol;
1098 err = snd_ctl_add(card, kcontrol);
1099 if (err < 0)
1100 return err;
1101 }
1102
1103 chip->capture_source_addr = MIXER_ADDR_UNSELECTED;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001104 return 0;
1105}
1106
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001107static void snd_saa7134_free(struct snd_card * card)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001108{
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001109 snd_card_saa7134_t *chip = card->private_data;
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001110
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001111 if (chip->dev->dmasound.priv_data == NULL)
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001112 return;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001113
Jeff Garzikf000fd82008-04-22 13:50:34 +02001114 if (chip->irq >= 0)
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001115 free_irq(chip->irq, &chip->dev->dmasound);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001116
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001117 chip->dev->dmasound.priv_data = NULL;
1118
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001119}
1120
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001121/*
1122 * ALSA initialization
1123 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001124 * Called by the init routine, once for each saa7134 device present,
1125 * it creates the basic structures and registers the ALSA devices
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001126 *
1127 */
1128
Adrian Bunk943a4902005-12-01 00:51:35 -08001129static int alsa_card_saa7134_create(struct saa7134_dev *dev, int devnum)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001130{
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001131
Takashi Iwaif7cbb7f2006-01-13 18:48:06 +01001132 struct snd_card *card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001133 snd_card_saa7134_t *chip;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001134 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001135
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001136
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001137 if (devnum >= SNDRV_CARDS)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001138 return -ENODEV;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001139 if (!enable[devnum])
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001140 return -ENODEV;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001141
Takashi Iwaie7356882014-01-29 14:48:43 +01001142 err = snd_card_new(&dev->pci->dev, index[devnum], id[devnum],
1143 THIS_MODULE, sizeof(snd_card_saa7134_t), &card);
Takashi Iwai758021b2009-01-12 15:17:09 +01001144 if (err < 0)
1145 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001146
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001147 strcpy(card->driver, "SAA7134");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001148
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001149 /* Card "creation" */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001150
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001151 card->private_free = snd_saa7134_free;
Joe Perchesabf84382010-07-12 17:50:03 -03001152 chip = card->private_data;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001153
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001154 spin_lock_init(&chip->lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001155 spin_lock_init(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001156
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001157 chip->dev = dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001158
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001159 chip->card = card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001160
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001161 chip->pci = dev->pci;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001162 chip->iobase = pci_resource_start(dev->pci, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001163
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001164
1165 err = request_irq(dev->pci->irq, saa7134_alsa_irq,
Michael Opdenacker3e018fe2013-10-13 02:49:29 -03001166 IRQF_SHARED, dev->name,
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001167 (void*) &dev->dmasound);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001168
1169 if (err < 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001170 pr_err("%s: can't get IRQ %d for ALSA\n",
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001171 dev->name, dev->pci->irq);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001172 goto __nodev;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001173 }
1174
Ricardo Cerqueirad5ee43a2005-11-13 16:08:10 -08001175 chip->irq = dev->pci->irq;
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001176
Ingo Molnar3593cab2006-02-07 06:49:14 -02001177 mutex_init(&dev->dmasound.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001178
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001179 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
1180 goto __nodev;
1181
1182 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
1183 goto __nodev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -08001184
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001185 /* End of "creation" */
1186
1187 strcpy(card->shortname, "SAA7134");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001188 sprintf(card->longname, "%s at 0x%lx irq %d",
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001189 chip->dev->name, chip->iobase, chip->irq);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001190
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -03001191 pr_info("%s/alsa: %s registered as card %d\n",
1192 dev->name, card->longname, index[devnum]);
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -08001193
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001194 if ((err = snd_card_register(card)) == 0) {
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001195 snd_saa7134_cards[devnum] = card;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001196 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001197 }
1198
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001199__nodev:
1200 snd_card_free(card);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001201 return err;
1202}
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001203
Ricardo Cerqueiraf5b974c2005-12-01 00:51:20 -08001204
1205static int alsa_device_init(struct saa7134_dev *dev)
1206{
1207 dev->dmasound.priv_data = dev;
1208 alsa_card_saa7134_create(dev,dev->nr);
1209 return 1;
1210}
1211
1212static int alsa_device_exit(struct saa7134_dev *dev)
1213{
1214
1215 snd_card_free(snd_saa7134_cards[dev->nr]);
1216 snd_saa7134_cards[dev->nr] = NULL;
1217 return 1;
1218}
1219
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001220/*
1221 * Module initializer
1222 *
1223 * Loops through present saa7134 cards, and assigns an ALSA device
1224 * to each one
1225 *
1226 */
1227
1228static int saa7134_alsa_init(void)
1229{
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001230 struct saa7134_dev *dev = NULL;
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001231 struct list_head *list;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001232
Adrian Bunk166fb6b2007-11-21 19:55:52 -03001233 saa7134_dmasound_init = alsa_device_init;
1234 saa7134_dmasound_exit = alsa_device_exit;
Ricardo Cerqueira7bb95292005-12-20 18:26:26 -02001235
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001236 pr_info("saa7134 ALSA driver for DMA sound loaded\n");
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001237
1238 list_for_each(list,&saa7134_devlist) {
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001239 dev = list_entry(list, struct saa7134_dev, devlist);
Mauro Carvalho Chehab40c41c82009-01-31 08:09:33 -03001240 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001241 pr_info("%s/alsa: %s doesn't support digital audio\n",
Mauro Carvalho Chehab40c41c82009-01-31 08:09:33 -03001242 dev->name, saa7134_boards[dev->board].name);
1243 else
1244 alsa_device_init(dev);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001245 }
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001246
Ricardo Cerqueira4aabf632005-11-13 16:08:00 -08001247 if (dev == NULL)
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001248 pr_info("saa7134 ALSA: no saa7134 cards found\n");
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001249
1250 return 0;
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -08001251
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001252}
1253
1254/*
1255 * Module destructor
1256 */
1257
Adrian Bunk943a4902005-12-01 00:51:35 -08001258static void saa7134_alsa_exit(void)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001259{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001260 int idx;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001261
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001262 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001263 snd_card_free(snd_saa7134_cards[idx]);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001264 }
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001265
Adrian Bunk78457012006-07-15 15:19:56 -03001266 saa7134_dmasound_init = NULL;
1267 saa7134_dmasound_exit = NULL;
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -03001268 pr_info("saa7134 ALSA driver for DMA sound unloaded\n");
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001269
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001270 return;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001271}
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001272
Linus Torvaldsc7ac6b42005-12-21 14:52:32 -08001273/* We initialize this late, to make sure the sound system is up and running */
1274late_initcall(saa7134_alsa_init);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001275module_exit(saa7134_alsa_exit);
1276MODULE_LICENSE("GPL");
1277MODULE_AUTHOR("Ricardo Cerqueira");