blob: 7972939cb46cb444d1e2d43b9e58fcec68e81482 [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 *
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08004 *
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08005 * Caveats:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08006 * - Volume doesn't work (it's always at max)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08007 *
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, version 2
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080021 */
22
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080023#include <sound/driver.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080024#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/time.h>
27#include <linux/wait.h>
28#include <linux/moduleparam.h>
29#include <linux/module.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080030#include <sound/core.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080031#include <sound/control.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080032#include <sound/pcm.h>
33#include <sound/rawmidi.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080034#include <sound/initval.h>
35
36#include "saa7134.h"
37#include "saa7134-reg.h"
38
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080039static unsigned int debug = 0;
40module_param(debug, int, 0644);
41MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
42
43unsigned int dsp_nr = 0;
44module_param(dsp_nr, int, 0444);
45MODULE_PARM_DESC(dsp_nr, "alsa device number");
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080046
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080047/*
48 * Configuration macros
49 */
50
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080051/* defaults */
52#define MAX_BUFFER_SIZE (256*1024)
53#define USE_FORMATS SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE
54#define USE_RATE SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000
55#define USE_RATE_MIN 32000
56#define USE_RATE_MAX 48000
57#define USE_CHANNELS_MIN 1
58#define USE_CHANNELS_MAX 2
59#ifndef USE_PERIODS_MIN
60#define USE_PERIODS_MIN 2
61#endif
62#ifndef USE_PERIODS_MAX
63#define USE_PERIODS_MAX 1024
64#endif
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080065
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080066#define MIXER_ADDR_TVTUNER 0
67#define MIXER_ADDR_LINE1 1
68#define MIXER_ADDR_LINE2 2
69#define MIXER_ADDR_LAST 2
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080070
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080071static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
72static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
73static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
74
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080075#define dprintk(fmt, arg...) if (debug) \
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080076 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
77
78/*
79 * Main chip structure
80 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080081typedef struct snd_card_saa7134 {
82 snd_card_t *card;
83 spinlock_t mixer_lock;
84 int mixer_volume[MIXER_ADDR_LAST+1][2];
85 int capture_source[MIXER_ADDR_LAST+1][2];
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080086 struct pci_dev *pci;
87 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080088
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080089 unsigned long iobase;
90 int irq;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080091
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080092 spinlock_t lock;
93} snd_card_saa7134_t;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080094
Nickolay V. Shmyrev07eef6c2005-11-08 21:38:14 -080095
96
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080097/*
98 * PCM structure
99 */
100
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800101typedef struct snd_card_saa7134_pcm {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800102 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800103
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800104 spinlock_t lock;
105 unsigned int pcm_size; /* buffer size */
106 unsigned int pcm_count; /* bytes per period */
107 unsigned int pcm_bps; /* bytes per second */
108 snd_pcm_substream_t *substream;
109} snd_card_saa7134_pcm_t;
110
111static snd_card_t *snd_saa7134_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
112
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800113
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800114/*
115 * saa7134 DMA audio stop
116 *
117 * Called when the capture device is released or the buffer overflows
118 *
119 * - Copied verbatim from saa7134-oss's dsp_dma_stop. Can be dropped
120 * if we just share dsp_dma_stop and use it here
121 *
122 */
123
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800124static void saa7134_dma_stop(struct saa7134_dev *dev)
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800125
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800126{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800127 dev->oss.dma_blk = -1;
128 dev->oss.dma_running = 0;
129 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800130}
131
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800132/*
133 * saa7134 DMA audio start
134 *
135 * Called when preparing the capture device for use
136 *
137 * - Copied verbatim from saa7134-oss's dsp_dma_start. Can be dropped
138 * if we just share dsp_dma_start and use it here
139 *
140 */
141
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800142static void saa7134_dma_start(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800143{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800144 dev->oss.dma_blk = 0;
145 dev->oss.dma_running = 1;
146 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800147}
148
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800149/*
150 * saa7134 audio DMA IRQ handler
151 *
152 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
153 * Handles shifting between the 2 buffers, manages the read counters,
154 * and notifies ALSA when periods elapse
155 *
156 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
157 *
158 */
159
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800160void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800161{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800162 int next_blk, reg = 0;
163
164 spin_lock(&dev->slock);
165 if (UNSET == dev->oss.dma_blk) {
166 dprintk("irq: recording stopped\n");
167 goto done;
168 }
169 if (0 != (status & 0x0f000000))
170 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
171 if (0 == (status & 0x10000000)) {
172 /* odd */
173 if (0 == (dev->oss.dma_blk & 0x01))
174 reg = SAA7134_RS_BA1(6);
175 } else {
176 /* even */
177 if (1 == (dev->oss.dma_blk & 0x01))
178 reg = SAA7134_RS_BA2(6);
179 }
180 if (0 == reg) {
181 dprintk("irq: field oops [%s]\n",
182 (status & 0x10000000) ? "even" : "odd");
183 goto done;
184 }
185
186 if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
187 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->oss.read_count,
188 dev->oss.bufsize, dev->oss.blocks);
189 saa7134_dma_stop(dev);
190 goto done;
191 }
192
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800193 /* next block addr */
194 next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
195 saa_writel(reg,next_blk * dev->oss.blksize);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800196 if (debug > 2)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800197 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
198 (status & 0x10000000) ? "even" : "odd ", next_blk,
199 next_blk * dev->oss.blksize, dev->oss.blocks, dev->oss.blksize, dev->oss.read_count);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800200
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800201 /* update status & wake waiting readers */
202 dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
203 dev->oss.read_count += dev->oss.blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800204
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800205 dev->oss.recording_on = reg;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800206
207 if (dev->oss.read_count >= snd_pcm_lib_period_bytes(dev->oss.substream)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800208 spin_unlock(&dev->slock);
209 snd_pcm_period_elapsed(dev->oss.substream);
210 spin_lock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800211 }
212 done:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800213 spin_unlock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800214
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800215}
216
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800217/*
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800218 * IRQ request handler
219 *
220 * Runs along with saa7134's IRQ handler, discards anything that isn't
221 * DMA sound
222 *
223 */
224
225static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id, struct pt_regs *regs)
226{
227 struct saa7134_dev *dev = (struct saa7134_dev*) dev_id;
228 unsigned long report, status;
229 int loop, handled = 0;
230
231 for (loop = 0; loop < 10; loop++) {
232 report = saa_readl(SAA7134_IRQ_REPORT);
233 status = saa_readl(SAA7134_IRQ_STATUS);
234
235 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
236 handled = 1;
237 saa_writel(SAA7134_IRQ_REPORT,report);
238 saa7134_irq_alsa_done(dev, status);
239 } else {
240 goto out;
241 }
242 }
243
244 if (loop == 10) {
245 dprintk("error! looping IRQ!");
246 }
247
248out:
249 return IRQ_RETVAL(handled);
250}
251
252/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800253 * ALSA capture trigger
254 *
255 * - One of the ALSA capture callbacks.
256 *
257 * Called whenever a capture is started or stopped. Must be defined,
258 * but there's nothing we want to do here
259 *
260 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800261
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800262static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
263 int cmd)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800264{
265 return 0;
266}
267
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800268/*
269 * DMA buffer config
270 *
271 * Sets the values that will later be used as the size of the buffer,
272 * size of the fragments, and total number of fragments.
273 * Must be called during the preparation stage, before memory is
274 * allocated
275 *
276 * - Copied verbatim from saa7134-oss. Can be dropped
277 * if we just share dsp_buffer_conf from OSS.
278 */
279
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800280static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800281{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800282 if (blksize < 0x100)
283 blksize = 0x100;
284 if (blksize > 0x10000)
285 blksize = 0x10000;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800286
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800287 if (blocks < 2)
288 blocks = 2;
289 if ((blksize * blocks) > 1024*1024)
290 blocks = 1024*1024 / blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800291
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800292 dev->oss.blocks = blocks;
293 dev->oss.blksize = blksize;
294 dev->oss.bufsize = blksize * blocks;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800295
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800296 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
297 blocks,blksize,blksize * blocks / 1024);
298 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800299}
300
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800301/*
302 * DMA buffer initialization
303 *
304 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
305 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
306 * usage of V4L's
307 *
308 * - Copied verbatim from saa7134-oss. Can be dropped
309 * if we just share dsp_buffer_init from OSS.
310 */
311
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800312static int dsp_buffer_init(struct saa7134_dev *dev)
313{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800314 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800315
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800316 if (!dev->oss.bufsize)
317 BUG();
318 videobuf_dma_init(&dev->oss.dma);
319 err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800320 (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800321 if (0 != err)
322 return err;
323 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800324}
325
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800326/*
327 * ALSA PCM preparation
328 *
329 * - One of the ALSA capture callbacks.
330 *
331 * Called right after the capture device is opened, this function configures
332 * the buffer using the previously defined functions, allocates the memory,
333 * sets up the hardware registers, and then starts the DMA. When this function
334 * returns, the audio should be flowing.
335 *
336 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800337
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800338static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800339{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800340 snd_pcm_runtime_t *runtime = substream->runtime;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800341 int err, bswap, sign;
342 u32 fmt, control;
343 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800344 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800345 struct saa7134_dev *dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800346 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
347 unsigned int bps;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800348 unsigned long size;
349 unsigned count;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800350
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800351 size = snd_pcm_lib_buffer_bytes(substream);
352 count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800353
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800354 saapcm->saadev->oss.substream = substream;
355 bps = runtime->rate * runtime->channels;
356 bps *= snd_pcm_format_width(runtime->format);
357 bps /= 8;
358 if (bps <= 0)
359 return -EINVAL;
360 saapcm->pcm_bps = bps;
361 saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
362 saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800363
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800364
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800365 dev=saa7134->saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800366
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800367 dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800368
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800369 err = dsp_buffer_init(dev);
370 if (0 != err)
371 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 /* prepare buffer */
374 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
375 return err;
376 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
377 goto fail1;
378 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800379 dev->oss.dma.sglist,
380 dev->oss.dma.sglen,
381 0)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800382 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800383
384
385
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800386 switch (runtime->format) {
387 case SNDRV_PCM_FORMAT_U8:
388 case SNDRV_PCM_FORMAT_S8:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800389 fmt = 0x00;
390 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800391 case SNDRV_PCM_FORMAT_U16_LE:
392 case SNDRV_PCM_FORMAT_U16_BE:
393 case SNDRV_PCM_FORMAT_S16_LE:
394 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800395 fmt = 0x01;
396 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800397 default:
398 err = -EINVAL;
399 return 1;
400 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800401
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800402 switch (runtime->format) {
403 case SNDRV_PCM_FORMAT_S8:
404 case SNDRV_PCM_FORMAT_S16_LE:
405 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800406 sign = 1;
407 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800408 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800409 sign = 0;
410 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800411 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800412
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800413 switch (runtime->format) {
414 case SNDRV_PCM_FORMAT_U16_BE:
415 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800416 bswap = 1; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800417 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800418 bswap = 0; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800419 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800420
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800421 switch (dev->pci->device) {
422 case PCI_DEVICE_ID_PHILIPS_SAA7134:
423 if (1 == runtime->channels)
424 fmt |= (1 << 3);
425 if (2 == runtime->channels)
426 fmt |= (3 << 3);
427 if (sign)
428 fmt |= 0x04;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800429
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800430 fmt |= (MIXER_ADDR_TVTUNER == dev->oss.input) ? 0xc0 : 0x80;
431 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
432 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >> 8);
433 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
434 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800435
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800436 break;
437 case PCI_DEVICE_ID_PHILIPS_SAA7133:
438 case PCI_DEVICE_ID_PHILIPS_SAA7135:
439 if (1 == runtime->channels)
440 fmt |= (1 << 4);
441 if (2 == runtime->channels)
442 fmt |= (2 << 4);
443 if (!sign)
444 fmt |= 0x04;
445 saa_writel(SAA7133_NUM_SAMPLES, dev->oss.blksize -1);
446 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
447 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
448 break;
449 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800450
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800451 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
452 runtime->format, runtime->channels, fmt,
453 bswap ? 'b' : '-');
454 /* dma: setup channel 6 (= AUDIO) */
455 control = SAA7134_RS_CONTROL_BURST_16 |
456 SAA7134_RS_CONTROL_ME |
457 (dev->oss.pt.dma >> 12);
458 if (bswap)
459 control |= SAA7134_RS_CONTROL_BSWAP;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800460
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800461 /* I should be able to use runtime->dma_addr in the control
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800462 byte, but it doesn't work. So I allocate the DMA using the
463 V4L functions, and force ALSA to use that as the DMA area */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800464
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800465 runtime->dma_area = dev->oss.dma.vmalloc;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800466
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800467 saa_writel(SAA7134_RS_BA1(6),0);
468 saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
469 saa_writel(SAA7134_RS_PITCH(6),0);
470 saa_writel(SAA7134_RS_CONTROL(6),control);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800471
472 dev->oss.rate = runtime->rate;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800473 /* start dma */
474 spin_lock_irqsave(&dev->slock,flags);
475 saa7134_dma_start(dev);
476 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800477
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800478 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800479 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800480 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800481 fail1:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800482 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
483 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800484
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800485
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800486}
487
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800488/*
489 * ALSA pointer fetching
490 *
491 * - One of the ALSA capture callbacks.
492 *
493 * Called whenever a period elapses, it must return the current hardware
494 * position of the buffer.
495 * Also resets the read counter used to prevent overruns
496 *
497 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800498
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800499static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800500{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800501 snd_pcm_runtime_t *runtime = substream->runtime;
502 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
503 struct saa7134_dev *dev=saapcm->saadev;
504
505
506
507 if (dev->oss.read_count) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800508 dev->oss.read_count -= snd_pcm_lib_period_bytes(substream);
509 dev->oss.read_offset += snd_pcm_lib_period_bytes(substream);
510 if (dev->oss.read_offset == dev->oss.bufsize)
511 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800512 }
513
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800514 return bytes_to_frames(runtime, dev->oss.read_offset);
515}
516
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800517/*
518 * ALSA hardware capabilities definition
519 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800520
521static snd_pcm_hardware_t snd_card_saa7134_capture =
522{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800523 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800524 SNDRV_PCM_INFO_BLOCK_TRANSFER |
525 SNDRV_PCM_INFO_MMAP_VALID),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800526 .formats = USE_FORMATS,
527 .rates = USE_RATE,
528 .rate_min = USE_RATE_MIN,
529 .rate_max = USE_RATE_MAX,
530 .channels_min = USE_CHANNELS_MIN,
531 .channels_max = USE_CHANNELS_MAX,
532 .buffer_bytes_max = MAX_BUFFER_SIZE,
533 .period_bytes_min = 64,
534 .period_bytes_max = MAX_BUFFER_SIZE,
535 .periods_min = USE_PERIODS_MIN,
536 .periods_max = USE_PERIODS_MAX,
537 .fifo_size = 0x08070503,
538};
539
540static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
541{
542 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
543
544 kfree(saapcm);
545}
546
547
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800548/*
549 * ALSA hardware params
550 *
551 * - One of the ALSA capture callbacks.
552 *
553 * Called on initialization, right before the PCM preparation
554 * Usually used in ALSA to allocate the DMA, but since we don't use the
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800555 * ALSA DMA it does nothing
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800556 *
557 */
558
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800559static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
560 snd_pcm_hw_params_t * hw_params)
561{
562
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800563 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800564
565
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800566}
567
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800568/*
569 * ALSA hardware release
570 *
571 * - One of the ALSA capture callbacks.
572 *
573 * Called after closing the device, but before snd_card_saa7134_capture_close
574 * Usually used in ALSA to free the DMA, but since we don't use the
575 * ALSA DMA I'm almost sure this isn't necessary.
576 *
577 */
578
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800579static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800580{
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800581 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800582}
583
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800584/*
585 * DMA buffer release
586 *
587 * Called after closing the device, during snd_card_saa7134_capture_close
588 *
589 */
590
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800591static int dsp_buffer_free(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800592{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800593 if (!dev->oss.blksize)
594 BUG();
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800595
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800596 videobuf_dma_free(&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800597
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800598 dev->oss.blocks = 0;
599 dev->oss.blksize = 0;
600 dev->oss.bufsize = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800601
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800602 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800603}
604
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800605/*
606 * ALSA capture finish
607 *
608 * - One of the ALSA capture callbacks.
609 *
610 * Called after closing the device. It stops the DMA audio and releases
611 * the buffers
612 *
613 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800614
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800615static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800616{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800617 snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800618 struct saa7134_dev *dev = chip->saadev;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800619 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800620
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800621 /* stop dma */
622 spin_lock_irqsave(&dev->slock,flags);
623 saa7134_dma_stop(dev);
624 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800625
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800626 /* unlock buffer */
627 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
628 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800629
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800630 dsp_buffer_free(dev);
631 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800632}
633
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800634/*
635 * ALSA capture start
636 *
637 * - One of the ALSA capture callbacks.
638 *
639 * Called when opening the device. It creates and populates the PCM
640 * structure
641 *
642 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800643
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800644static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
645{
646 snd_pcm_runtime_t *runtime = substream->runtime;
647 snd_card_saa7134_pcm_t *saapcm;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800648 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
649 struct saa7134_dev *dev = saa7134->saadev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800650 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800651
652 down(&dev->oss.lock);
653
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800654 dev->oss.afmt = SNDRV_PCM_FORMAT_U8;
655 dev->oss.channels = 2;
656 dev->oss.read_count = 0;
657 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800658
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800659 up(&dev->oss.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800660
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800661 saapcm = kcalloc(1, sizeof(*saapcm), GFP_KERNEL);
662 if (saapcm == NULL)
663 return -ENOMEM;
664 saapcm->saadev=saa7134->saadev;
665
666 spin_lock_init(&saapcm->lock);
667
668 saapcm->substream = substream;
669 runtime->private_data = saapcm;
670 runtime->private_free = snd_card_saa7134_runtime_free;
671 runtime->hw = snd_card_saa7134_capture;
672
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800673 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
674 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800675
676 return 0;
677}
678
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800679/*
680 * ALSA capture callbacks definition
681 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800682
683static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
684 .open = snd_card_saa7134_capture_open,
685 .close = snd_card_saa7134_capture_close,
686 .ioctl = snd_pcm_lib_ioctl,
687 .hw_params = snd_card_saa7134_hw_params,
688 .hw_free = snd_card_saa7134_hw_free,
689 .prepare = snd_card_saa7134_capture_prepare,
690 .trigger = snd_card_saa7134_capture_trigger,
691 .pointer = snd_card_saa7134_capture_pointer,
692};
693
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800694/*
695 * ALSA PCM setup
696 *
697 * Called when initializing the board. Sets up the name and hooks up
698 * the callbacks
699 *
700 */
701
702static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800703{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800704 snd_pcm_t *pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800705 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800706
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800707 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800708 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800709 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
710 pcm->private_data = saa7134;
711 pcm->info_flags = 0;
712 strcpy(pcm->name, "SAA7134 PCM");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800713 return 0;
714}
715
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800716#define SAA713x_VOLUME(xname, xindex, addr) \
717{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
718 .info = snd_saa7134_volume_info, \
719 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
720 .private_value = addr }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800721
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800722static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
723{
724 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
725 uinfo->count = 2;
726 uinfo->value.integer.min = 0;
727 uinfo->value.integer.max = 20;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800728 return 0;
729}
730
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800731static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800732{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800733 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
734 unsigned long flags;
735 int addr = kcontrol->private_value;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800736
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800737 spin_lock_irqsave(&chip->mixer_lock, flags);
738 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
739 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
740 spin_unlock_irqrestore(&chip->mixer_lock, flags);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800741 return 0;
742}
743
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800744static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
745{
746 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
747 unsigned long flags;
748 int change, addr = kcontrol->private_value;
749 int left, right;
750
751 left = ucontrol->value.integer.value[0];
752 if (left < 0)
753 left = 0;
754 if (left > 20)
755 left = 20;
756 right = ucontrol->value.integer.value[1];
757 if (right < 0)
758 right = 0;
759 if (right > 20)
760 right = 20;
761 spin_lock_irqsave(&chip->mixer_lock, flags);
762 change = chip->mixer_volume[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800763 chip->mixer_volume[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800764 chip->mixer_volume[addr][0] = left;
765 chip->mixer_volume[addr][1] = right;
766 spin_unlock_irqrestore(&chip->mixer_lock, flags);
767 return change;
768}
769
770#define SAA713x_CAPSRC(xname, xindex, addr) \
771{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
772 .info = snd_saa7134_capsrc_info, \
773 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
774 .private_value = addr }
775
776static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
777{
778 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800779 uinfo->count = 2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800780 uinfo->value.integer.min = 0;
781 uinfo->value.integer.max = 1;
782 return 0;
783}
784
785static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
786{
787 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
788 unsigned long flags;
789 int addr = kcontrol->private_value;
790
791 spin_lock_irqsave(&chip->mixer_lock, flags);
792 ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
793 ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
794 spin_unlock_irqrestore(&chip->mixer_lock, flags);
795 return 0;
796}
797
798static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
799{
800 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
801 unsigned long flags;
802 int change, addr = kcontrol->private_value;
803 int left, right;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800804 u32 anabar, xbarin;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800805 int analog_io, rate;
806 struct saa7134_dev *dev;
807
808 dev = chip->saadev;
809
810 left = ucontrol->value.integer.value[0] & 1;
811 right = ucontrol->value.integer.value[1] & 1;
812 spin_lock_irqsave(&chip->mixer_lock, flags);
813
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800814 change = chip->capture_source[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800815 chip->capture_source[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800816 chip->capture_source[addr][0] = left;
817 chip->capture_source[addr][1] = right;
818 dev->oss.input=addr;
819 spin_unlock_irqrestore(&chip->mixer_lock, flags);
820
821
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800822 if (change) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800823 switch (dev->pci->device) {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800824
825 case PCI_DEVICE_ID_PHILIPS_SAA7134:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800826 switch (addr) {
827 case MIXER_ADDR_TVTUNER:
828 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
829 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
830 break;
831 case MIXER_ADDR_LINE1:
832 case MIXER_ADDR_LINE2:
833 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
834 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
835 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
836 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
837 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
838 break;
839 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800840
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800841 break;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800842 case PCI_DEVICE_ID_PHILIPS_SAA7133:
843 case PCI_DEVICE_ID_PHILIPS_SAA7135:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800844 xbarin = 0x03; // adc
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800845 anabar = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800846 switch (addr) {
847 case MIXER_ADDR_TVTUNER:
848 xbarin = 0; // Demodulator
849 anabar = 2; // DACs
850 break;
851 case MIXER_ADDR_LINE1:
852 anabar = 0; // aux1, aux1
853 break;
854 case MIXER_ADDR_LINE2:
855 anabar = 9; // aux2, aux2
856 break;
857 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800858
859 /* output xbar always main channel */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800860 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800861
862 if (left || right) { // We've got data, turn the input on
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800863 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
864 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800865 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800866 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
867 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800868 }
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800869 break;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800870 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800871 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800872
873 return change;
874}
875
876static snd_kcontrol_new_t snd_saa7134_controls[] = {
877SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
878SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
879SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
880SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
881SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
882SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800883};
884
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800885/*
886 * ALSA mixer setup
887 *
888 * Called when initializing the board. Sets up the name and hooks up
889 * the callbacks
890 *
891 */
892
893static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800894{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800895 snd_card_t *card = chip->card;
896 unsigned int idx;
897 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800898
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800899 snd_assert(chip != NULL, return -EINVAL);
900 strcpy(card->mixername, "SAA7134 Mixer");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800901
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800902 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
903 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
904 return err;
905 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800906 return 0;
907}
908
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800909static int snd_saa7134_free(snd_card_saa7134_t *chip)
910{
911 return 0;
912}
913
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800914static int snd_saa7134_dev_free(snd_device_t *device)
915{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800916 snd_card_saa7134_t *chip = device->device_data;
917 return snd_saa7134_free(chip);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800918}
919
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800920/*
921 * ALSA initialization
922 *
923 * Called by saa7134-core, it creates the basic structures and registers
924 * the ALSA devices
925 *
926 */
927
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800928int alsa_card_saa7134_create (struct saa7134_dev *saadev, unsigned int devicenum)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800929{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800930 static int dev;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800931
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800932 snd_card_t *card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800933 snd_card_saa7134_t *chip;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800934 int err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800935 static snd_device_ops_t ops = {
936 .dev_free = snd_saa7134_dev_free,
937 };
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800938
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800939
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800940 if (dev >= SNDRV_CARDS)
941 return -ENODEV;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800942 if (!enable[dev])
943 return -ENODEV;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800944
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800945 if (devicenum) {
946 card = snd_card_new(devicenum, id[dev], THIS_MODULE, 0);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800947 dsp_nr++;
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800948 } else {
949 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
950 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800951 if (card == NULL)
952 return -ENOMEM;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800953
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800954 strcpy(card->driver, "SAA7134");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800955
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800956 /* Card "creation" */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800957
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800958 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800959 if (chip == NULL) {
960 return -ENOMEM;
961 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800962
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800963 spin_lock_init(&chip->lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800964
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800965 chip->saadev = saadev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800966
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800967 chip->card = card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800968
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800969 chip->pci = saadev->pci;
970 chip->irq = saadev->pci->irq;
971 chip->iobase = pci_resource_start(saadev->pci, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800972
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800973 err = request_irq(chip->pci->irq, saa7134_alsa_irq,
974 SA_SHIRQ | SA_INTERRUPT, saadev->name, saadev);
975
976 if (err < 0) {
977 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
978 saadev->name, saadev->pci->irq);
979 return err;
980 }
981
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800982 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
983 snd_saa7134_free(chip);
984 return err;
985 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800986
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800987 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
988 goto __nodev;
989
990 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
991 goto __nodev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800992
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800993 spin_lock_init(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800994
995 snd_card_set_dev(card, &chip->pci->dev);
996
997 /* End of "creation" */
998
999 strcpy(card->shortname, "SAA7134");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001000 sprintf(card->longname, "%s at 0x%lx irq %d",
1001 chip->saadev->name, chip->iobase, chip->irq);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001002
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001003 if ((err = snd_card_register(card)) == 0) {
1004 snd_saa7134_cards[dev] = card;
1005 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001006 }
1007
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001008__nodev:
1009 snd_card_free(card);
1010 kfree(card);
1011 return err;
1012}
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001013
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001014/*
1015 * Module initializer
1016 *
1017 * Loops through present saa7134 cards, and assigns an ALSA device
1018 * to each one
1019 *
1020 */
1021
1022static int saa7134_alsa_init(void)
1023{
1024 struct saa7134_dev *saadev = NULL;
1025 struct list_head *list;
1026
1027 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1028
1029 list_for_each(list,&saa7134_devlist) {
1030 saadev = list_entry(list, struct saa7134_dev, devlist);
1031 alsa_card_saa7134_create(saadev,dsp_nr);
1032 }
1033
1034 if (saadev == NULL)
1035 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1036
1037 return 0;
1038
1039}
1040
1041/*
1042 * Module destructor
1043 */
1044
1045void saa7134_alsa_exit(void)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001046{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001047 int idx;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001048 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001049 snd_card_free(snd_saa7134_cards[idx]);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001050 }
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001051 printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
1052 return;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001053}
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001054
1055module_init(saa7134_alsa_init);
1056module_exit(saa7134_alsa_exit);
1057MODULE_LICENSE("GPL");
1058MODULE_AUTHOR("Ricardo Cerqueira");