blob: bf454437b771f8ae1acd80d6590b9910247ff0f1 [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
3 * Ricardo Cerqueira <v4l@cerqueira.org>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08004 *
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08005 *
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08006 * Caveats:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08007 * - Volume doesn't work (it's always at max)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08008 *
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, version 2
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080022 */
23
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080024#include <sound/driver.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080025#include <linux/init.h>
26#include <linux/slab.h>
27#include <linux/time.h>
28#include <linux/wait.h>
29#include <linux/moduleparam.h>
30#include <linux/module.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080031#include <sound/core.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080032#include <sound/control.h>
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080033#include <sound/pcm.h>
34#include <sound/rawmidi.h>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080035#include <sound/initval.h>
36
37#include "saa7134.h"
38#include "saa7134-reg.h"
39
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080040static unsigned int alsa_debug = 0;
41module_param(alsa_debug, int, 0644);
42MODULE_PARM_DESC(alsa_debug,"enable debug messages [alsa]");
43
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080044/*
45 * Configuration macros
46 */
47
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080048/* defaults */
49#define MAX_BUFFER_SIZE (256*1024)
50#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
51#define USE_RATE SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000
52#define USE_RATE_MIN 32000
53#define USE_RATE_MAX 48000
54#define USE_CHANNELS_MIN 1
55#define USE_CHANNELS_MAX 2
56#ifndef USE_PERIODS_MIN
57#define USE_PERIODS_MIN 2
58#endif
59#ifndef USE_PERIODS_MAX
60#define USE_PERIODS_MAX 1024
61#endif
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080062
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080063#define MIXER_ADDR_TVTUNER 0
64#define MIXER_ADDR_LINE1 1
65#define MIXER_ADDR_LINE2 2
66#define MIXER_ADDR_LAST 2
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080067
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080068static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
69static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
70static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
71
72#define dprintk(fmt, arg...) if (alsa_debug) \
73 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
74
75/*
76 * Main chip structure
77 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080078typedef struct snd_card_saa7134 {
79 snd_card_t *card;
80 spinlock_t mixer_lock;
81 int mixer_volume[MIXER_ADDR_LAST+1][2];
82 int capture_source[MIXER_ADDR_LAST+1][2];
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080083 struct pci_dev *pci;
84 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080085
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080086 unsigned long iobase;
87 int irq;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080088
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080089 spinlock_t lock;
90} snd_card_saa7134_t;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080091
Nickolay V. Shmyrev07eef6c2005-11-08 21:38:14 -080092
93
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080094/*
95 * PCM structure
96 */
97
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080098typedef struct snd_card_saa7134_pcm {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080099 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800100
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800101 spinlock_t lock;
102 unsigned int pcm_size; /* buffer size */
103 unsigned int pcm_count; /* bytes per period */
104 unsigned int pcm_bps; /* bytes per second */
105 snd_pcm_substream_t *substream;
106} snd_card_saa7134_pcm_t;
107
108static snd_card_t *snd_saa7134_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
109
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800110/*
111 * saa7134 DMA audio stop
112 *
113 * Called when the capture device is released or the buffer overflows
114 *
115 * - Copied verbatim from saa7134-oss's dsp_dma_stop. Can be dropped
116 * if we just share dsp_dma_stop and use it here
117 *
118 */
119
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800120static void saa7134_dma_stop(struct saa7134_dev *dev)
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800121
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800122{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800123 dev->oss.dma_blk = -1;
124 dev->oss.dma_running = 0;
125 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800126}
127
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800128/*
129 * saa7134 DMA audio start
130 *
131 * Called when preparing the capture device for use
132 *
133 * - Copied verbatim from saa7134-oss's dsp_dma_start. Can be dropped
134 * if we just share dsp_dma_start and use it here
135 *
136 */
137
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800138static void saa7134_dma_start(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800139{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800140 dev->oss.dma_blk = 0;
141 dev->oss.dma_running = 1;
142 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800143}
144
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800145/*
146 * saa7134 audio DMA IRQ handler
147 *
148 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
149 * Handles shifting between the 2 buffers, manages the read counters,
150 * and notifies ALSA when periods elapse
151 *
152 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
153 *
154 */
155
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800156void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800157{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800158 int next_blk, reg = 0;
159
160 spin_lock(&dev->slock);
161 if (UNSET == dev->oss.dma_blk) {
162 dprintk("irq: recording stopped\n");
163 goto done;
164 }
165 if (0 != (status & 0x0f000000))
166 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
167 if (0 == (status & 0x10000000)) {
168 /* odd */
169 if (0 == (dev->oss.dma_blk & 0x01))
170 reg = SAA7134_RS_BA1(6);
171 } else {
172 /* even */
173 if (1 == (dev->oss.dma_blk & 0x01))
174 reg = SAA7134_RS_BA2(6);
175 }
176 if (0 == reg) {
177 dprintk("irq: field oops [%s]\n",
178 (status & 0x10000000) ? "even" : "odd");
179 goto done;
180 }
181
182 if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
183 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->oss.read_count,
184 dev->oss.bufsize, dev->oss.blocks);
185 saa7134_dma_stop(dev);
186 goto done;
187 }
188
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800189 /* next block addr */
190 next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
191 saa_writel(reg,next_blk * dev->oss.blksize);
192 if (alsa_debug > 2)
193 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
194 (status & 0x10000000) ? "even" : "odd ", next_blk,
195 next_blk * dev->oss.blksize, dev->oss.blocks, dev->oss.blksize, dev->oss.read_count);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800196
197
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800198 /* update status & wake waiting readers */
199 dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
200 dev->oss.read_count += dev->oss.blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800201
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800202 dev->oss.recording_on = reg;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800203
204 if (dev->oss.read_count >= snd_pcm_lib_period_bytes(dev->oss.substream)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800205 spin_unlock(&dev->slock);
206 snd_pcm_period_elapsed(dev->oss.substream);
207 spin_lock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800208 }
209 done:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800210 spin_unlock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800211
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800212}
213
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800214/*
215 * ALSA capture trigger
216 *
217 * - One of the ALSA capture callbacks.
218 *
219 * Called whenever a capture is started or stopped. Must be defined,
220 * but there's nothing we want to do here
221 *
222 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800223
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800224static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
225 int cmd)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800226{
227 return 0;
228}
229
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800230/*
231 * DMA buffer config
232 *
233 * Sets the values that will later be used as the size of the buffer,
234 * size of the fragments, and total number of fragments.
235 * Must be called during the preparation stage, before memory is
236 * allocated
237 *
238 * - Copied verbatim from saa7134-oss. Can be dropped
239 * if we just share dsp_buffer_conf from OSS.
240 */
241
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800242static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800243{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800244 if (blksize < 0x100)
245 blksize = 0x100;
246 if (blksize > 0x10000)
247 blksize = 0x10000;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800248
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800249 if (blocks < 2)
250 blocks = 2;
251 if ((blksize * blocks) > 1024*1024)
252 blocks = 1024*1024 / blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800253
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800254 dev->oss.blocks = blocks;
255 dev->oss.blksize = blksize;
256 dev->oss.bufsize = blksize * blocks;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800257
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800258 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
259 blocks,blksize,blksize * blocks / 1024);
260 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800261}
262
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800263/*
264 * DMA buffer initialization
265 *
266 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
267 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
268 * usage of V4L's
269 *
270 * - Copied verbatim from saa7134-oss. Can be dropped
271 * if we just share dsp_buffer_init from OSS.
272 */
273
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800274static int dsp_buffer_init(struct saa7134_dev *dev)
275{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800276 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800277
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800278 if (!dev->oss.bufsize)
279 BUG();
280 videobuf_dma_init(&dev->oss.dma);
281 err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800282 (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800283 if (0 != err)
284 return err;
285 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800286}
287
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800288/*
289 * ALSA PCM preparation
290 *
291 * - One of the ALSA capture callbacks.
292 *
293 * Called right after the capture device is opened, this function configures
294 * the buffer using the previously defined functions, allocates the memory,
295 * sets up the hardware registers, and then starts the DMA. When this function
296 * returns, the audio should be flowing.
297 *
298 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800299
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800300static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800301{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800302 snd_pcm_runtime_t *runtime = substream->runtime;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800303 int err, bswap, sign;
304 u32 fmt, control;
305 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800306 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800307 struct saa7134_dev *dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800308 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
309 unsigned int bps;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800310 unsigned long size;
311 unsigned count;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800312
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800313 size = snd_pcm_lib_buffer_bytes(substream);
314 count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800315
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800316 saapcm->saadev->oss.substream = substream;
317 bps = runtime->rate * runtime->channels;
318 bps *= snd_pcm_format_width(runtime->format);
319 bps /= 8;
320 if (bps <= 0)
321 return -EINVAL;
322 saapcm->pcm_bps = bps;
323 saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
324 saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800325
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800326
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800327 dev=saa7134->saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800328
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800329 dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800330
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800331 err = dsp_buffer_init(dev);
332 if (0 != err)
333 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800334
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800335 /* prepare buffer */
336 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
337 return err;
338 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
339 goto fail1;
340 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800341 dev->oss.dma.sglist,
342 dev->oss.dma.sglen,
343 0)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800344 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800345
346
347
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800348 switch (runtime->format) {
349 case SNDRV_PCM_FORMAT_U8:
350 case SNDRV_PCM_FORMAT_S8:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800351 fmt = 0x00;
352 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800353 case SNDRV_PCM_FORMAT_U16_LE:
354 case SNDRV_PCM_FORMAT_U16_BE:
355 case SNDRV_PCM_FORMAT_S16_LE:
356 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800357 fmt = 0x01;
358 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800359 default:
360 err = -EINVAL;
361 return 1;
362 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800363
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800364 switch (runtime->format) {
365 case SNDRV_PCM_FORMAT_S8:
366 case SNDRV_PCM_FORMAT_S16_LE:
367 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800368 sign = 1;
369 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800370 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800371 sign = 0;
372 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800374
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800375 switch (runtime->format) {
376 case SNDRV_PCM_FORMAT_U16_BE:
377 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800378 bswap = 1; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800379 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800380 bswap = 0; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800381 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800382
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800383 switch (dev->pci->device) {
384 case PCI_DEVICE_ID_PHILIPS_SAA7134:
385 if (1 == runtime->channels)
386 fmt |= (1 << 3);
387 if (2 == runtime->channels)
388 fmt |= (3 << 3);
389 if (sign)
390 fmt |= 0x04;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800391
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800392 fmt |= (MIXER_ADDR_TVTUNER == dev->oss.input) ? 0xc0 : 0x80;
393 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
394 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >> 8);
395 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
396 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800397
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800398 break;
399 case PCI_DEVICE_ID_PHILIPS_SAA7133:
400 case PCI_DEVICE_ID_PHILIPS_SAA7135:
401 if (1 == runtime->channels)
402 fmt |= (1 << 4);
403 if (2 == runtime->channels)
404 fmt |= (2 << 4);
405 if (!sign)
406 fmt |= 0x04;
407 saa_writel(SAA7133_NUM_SAMPLES, dev->oss.blksize -1);
408 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
409 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
410 break;
411 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800412
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800413 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
414 runtime->format, runtime->channels, fmt,
415 bswap ? 'b' : '-');
416 /* dma: setup channel 6 (= AUDIO) */
417 control = SAA7134_RS_CONTROL_BURST_16 |
418 SAA7134_RS_CONTROL_ME |
419 (dev->oss.pt.dma >> 12);
420 if (bswap)
421 control |= SAA7134_RS_CONTROL_BSWAP;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800422
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800423 /* I should be able to use runtime->dma_addr in the control
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800424 byte, but it doesn't work. So I allocate the DMA using the
425 V4L functions, and force ALSA to use that as the DMA area */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800426
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800427 runtime->dma_area = dev->oss.dma.vmalloc;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800428
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800429 saa_writel(SAA7134_RS_BA1(6),0);
430 saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
431 saa_writel(SAA7134_RS_PITCH(6),0);
432 saa_writel(SAA7134_RS_CONTROL(6),control);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800433
434 dev->oss.rate = runtime->rate;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800435 /* start dma */
436 spin_lock_irqsave(&dev->slock,flags);
437 saa7134_dma_start(dev);
438 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800439
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800440 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800441 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800442 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800443 fail1:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800444 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
445 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800446
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800447
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800448}
449
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800450/*
451 * ALSA pointer fetching
452 *
453 * - One of the ALSA capture callbacks.
454 *
455 * Called whenever a period elapses, it must return the current hardware
456 * position of the buffer.
457 * Also resets the read counter used to prevent overruns
458 *
459 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800460
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800461static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800462{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800463 snd_pcm_runtime_t *runtime = substream->runtime;
464 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
465 struct saa7134_dev *dev=saapcm->saadev;
466
467
468
469 if (dev->oss.read_count) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800470 dev->oss.read_count -= snd_pcm_lib_period_bytes(substream);
471 dev->oss.read_offset += snd_pcm_lib_period_bytes(substream);
472 if (dev->oss.read_offset == dev->oss.bufsize)
473 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800474 }
475
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800476 return bytes_to_frames(runtime, dev->oss.read_offset);
477}
478
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800479/*
480 * ALSA hardware capabilities definition
481 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800482
483static snd_pcm_hardware_t snd_card_saa7134_capture =
484{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800485 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800486 SNDRV_PCM_INFO_BLOCK_TRANSFER |
487 SNDRV_PCM_INFO_MMAP_VALID),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800488 .formats = USE_FORMATS,
489 .rates = USE_RATE,
490 .rate_min = USE_RATE_MIN,
491 .rate_max = USE_RATE_MAX,
492 .channels_min = USE_CHANNELS_MIN,
493 .channels_max = USE_CHANNELS_MAX,
494 .buffer_bytes_max = MAX_BUFFER_SIZE,
495 .period_bytes_min = 64,
496 .period_bytes_max = MAX_BUFFER_SIZE,
497 .periods_min = USE_PERIODS_MIN,
498 .periods_max = USE_PERIODS_MAX,
499 .fifo_size = 0x08070503,
500};
501
502static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
503{
504 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
505
506 kfree(saapcm);
507}
508
509
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800510/*
511 * ALSA hardware params
512 *
513 * - One of the ALSA capture callbacks.
514 *
515 * Called on initialization, right before the PCM preparation
516 * Usually used in ALSA to allocate the DMA, but since we don't use the
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800517 * ALSA DMA it does nothing
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800518 *
519 */
520
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800521static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
522 snd_pcm_hw_params_t * hw_params)
523{
524
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800525 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800526
527
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800528}
529
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800530/*
531 * ALSA hardware release
532 *
533 * - One of the ALSA capture callbacks.
534 *
535 * Called after closing the device, but before snd_card_saa7134_capture_close
536 * Usually used in ALSA to free the DMA, but since we don't use the
537 * ALSA DMA I'm almost sure this isn't necessary.
538 *
539 */
540
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800541static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800542{
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800543 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800544}
545
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800546/*
547 * DMA buffer release
548 *
549 * Called after closing the device, during snd_card_saa7134_capture_close
550 *
551 */
552
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800553static int dsp_buffer_free(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800554{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800555 if (!dev->oss.blksize)
556 BUG();
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800557
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800558 videobuf_dma_free(&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800559
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800560 dev->oss.blocks = 0;
561 dev->oss.blksize = 0;
562 dev->oss.bufsize = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800563
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800564 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800565}
566
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800567/*
568 * ALSA capture finish
569 *
570 * - One of the ALSA capture callbacks.
571 *
572 * Called after closing the device. It stops the DMA audio and releases
573 * the buffers
574 *
575 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800576
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800577static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800578{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800579 snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800580 struct saa7134_dev *dev = chip->saadev;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800581 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800582
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800583 /* stop dma */
584 spin_lock_irqsave(&dev->slock,flags);
585 saa7134_dma_stop(dev);
586 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800587
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800588 /* unlock buffer */
589 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
590 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800591
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800592 dsp_buffer_free(dev);
593 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800594}
595
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800596/*
597 * ALSA capture start
598 *
599 * - One of the ALSA capture callbacks.
600 *
601 * Called when opening the device. It creates and populates the PCM
602 * structure
603 *
604 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800605
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800606static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
607{
608 snd_pcm_runtime_t *runtime = substream->runtime;
609 snd_card_saa7134_pcm_t *saapcm;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800610 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
611 struct saa7134_dev *dev = saa7134->saadev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800612 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800613
614 down(&dev->oss.lock);
615
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800616 dev->oss.afmt = SNDRV_PCM_FORMAT_U8;
617 dev->oss.channels = 2;
618 dev->oss.read_count = 0;
619 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800620
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800621 up(&dev->oss.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800622
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800623 saapcm = kcalloc(1, sizeof(*saapcm), GFP_KERNEL);
624 if (saapcm == NULL)
625 return -ENOMEM;
626 saapcm->saadev=saa7134->saadev;
627
628 spin_lock_init(&saapcm->lock);
629
630 saapcm->substream = substream;
631 runtime->private_data = saapcm;
632 runtime->private_free = snd_card_saa7134_runtime_free;
633 runtime->hw = snd_card_saa7134_capture;
634
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800635 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
636 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800637
638 return 0;
639}
640
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800641/*
642 * ALSA capture callbacks definition
643 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800644
645static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
646 .open = snd_card_saa7134_capture_open,
647 .close = snd_card_saa7134_capture_close,
648 .ioctl = snd_pcm_lib_ioctl,
649 .hw_params = snd_card_saa7134_hw_params,
650 .hw_free = snd_card_saa7134_hw_free,
651 .prepare = snd_card_saa7134_capture_prepare,
652 .trigger = snd_card_saa7134_capture_trigger,
653 .pointer = snd_card_saa7134_capture_pointer,
654};
655
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800656/*
657 * ALSA PCM setup
658 *
659 * Called when initializing the board. Sets up the name and hooks up
660 * the callbacks
661 *
662 */
663
664static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800665{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800666 snd_pcm_t *pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800667 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800668
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800669 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800670 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800671 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
672 pcm->private_data = saa7134;
673 pcm->info_flags = 0;
674 strcpy(pcm->name, "SAA7134 PCM");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800675 return 0;
676}
677
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800678#define SAA713x_VOLUME(xname, xindex, addr) \
679{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
680 .info = snd_saa7134_volume_info, \
681 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
682 .private_value = addr }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800683
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800684static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
685{
686 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
687 uinfo->count = 2;
688 uinfo->value.integer.min = 0;
689 uinfo->value.integer.max = 20;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800690 return 0;
691}
692
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800693static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800694{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800695 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
696 unsigned long flags;
697 int addr = kcontrol->private_value;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800698
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800699 spin_lock_irqsave(&chip->mixer_lock, flags);
700 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
701 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
702 spin_unlock_irqrestore(&chip->mixer_lock, flags);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800703 return 0;
704}
705
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800706static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
707{
708 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
709 unsigned long flags;
710 int change, addr = kcontrol->private_value;
711 int left, right;
712
713 left = ucontrol->value.integer.value[0];
714 if (left < 0)
715 left = 0;
716 if (left > 20)
717 left = 20;
718 right = ucontrol->value.integer.value[1];
719 if (right < 0)
720 right = 0;
721 if (right > 20)
722 right = 20;
723 spin_lock_irqsave(&chip->mixer_lock, flags);
724 change = chip->mixer_volume[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800725 chip->mixer_volume[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800726 chip->mixer_volume[addr][0] = left;
727 chip->mixer_volume[addr][1] = right;
728 spin_unlock_irqrestore(&chip->mixer_lock, flags);
729 return change;
730}
731
732#define SAA713x_CAPSRC(xname, xindex, addr) \
733{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
734 .info = snd_saa7134_capsrc_info, \
735 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
736 .private_value = addr }
737
738static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
739{
740 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800741 uinfo->count = 2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800742 uinfo->value.integer.min = 0;
743 uinfo->value.integer.max = 1;
744 return 0;
745}
746
747static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
748{
749 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
750 unsigned long flags;
751 int addr = kcontrol->private_value;
752
753 spin_lock_irqsave(&chip->mixer_lock, flags);
754 ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
755 ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
756 spin_unlock_irqrestore(&chip->mixer_lock, flags);
757 return 0;
758}
759
760static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
761{
762 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
763 unsigned long flags;
764 int change, addr = kcontrol->private_value;
765 int left, right;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800766 u32 anabar, xbarin;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800767 int analog_io, rate;
768 struct saa7134_dev *dev;
769
770 dev = chip->saadev;
771
772 left = ucontrol->value.integer.value[0] & 1;
773 right = ucontrol->value.integer.value[1] & 1;
774 spin_lock_irqsave(&chip->mixer_lock, flags);
775
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800776 change = chip->capture_source[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800777 chip->capture_source[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800778 chip->capture_source[addr][0] = left;
779 chip->capture_source[addr][1] = right;
780 dev->oss.input=addr;
781 spin_unlock_irqrestore(&chip->mixer_lock, flags);
782
783
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800784 if (change) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800785 switch (dev->pci->device) {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800786
787 case PCI_DEVICE_ID_PHILIPS_SAA7134:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800788 switch (addr) {
789 case MIXER_ADDR_TVTUNER:
790 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
791 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
792 break;
793 case MIXER_ADDR_LINE1:
794 case MIXER_ADDR_LINE2:
795 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
796 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
797 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
798 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
799 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
800 break;
801 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800802
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800803 break;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800804 case PCI_DEVICE_ID_PHILIPS_SAA7133:
805 case PCI_DEVICE_ID_PHILIPS_SAA7135:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800806 xbarin = 0x03; // adc
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800807 anabar = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800808 switch (addr) {
809 case MIXER_ADDR_TVTUNER:
810 xbarin = 0; // Demodulator
811 anabar = 2; // DACs
812 break;
813 case MIXER_ADDR_LINE1:
814 anabar = 0; // aux1, aux1
815 break;
816 case MIXER_ADDR_LINE2:
817 anabar = 9; // aux2, aux2
818 break;
819 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800820
821 /* output xbar always main channel */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800822 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800823
824 if (left || right) { // We've got data, turn the input on
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800825 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
826 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
827 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800828 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800829 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
830 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
831 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800832 }
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800833 break;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800834 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800835 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800836
837 return change;
838}
839
840static snd_kcontrol_new_t snd_saa7134_controls[] = {
841SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
842SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
843SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
844SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
845SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
846SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800847};
848
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800849/*
850 * ALSA mixer setup
851 *
852 * Called when initializing the board. Sets up the name and hooks up
853 * the callbacks
854 *
855 */
856
857static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800858{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800859 snd_card_t *card = chip->card;
860 unsigned int idx;
861 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800862
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800863 snd_assert(chip != NULL, return -EINVAL);
864 strcpy(card->mixername, "SAA7134 Mixer");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800865
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800866 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
867 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
868 return err;
869 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800870 return 0;
871}
872
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800873static int snd_saa7134_free(snd_card_saa7134_t *chip)
874{
875 return 0;
876}
877
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800878static int snd_saa7134_dev_free(snd_device_t *device)
879{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800880 snd_card_saa7134_t *chip = device->device_data;
881 return snd_saa7134_free(chip);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800882}
883
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800884/*
885 * ALSA initialization
886 *
887 * Called by saa7134-core, it creates the basic structures and registers
888 * the ALSA devices
889 *
890 */
891
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800892int alsa_card_saa7134_create(struct saa7134_dev *saadev, unsigned int devicenum)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800893{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800894 static int dev;
895 snd_card_t *card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800896 snd_card_saa7134_t *chip;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800897 int err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800898 static snd_device_ops_t ops = {
899 .dev_free = snd_saa7134_dev_free,
900 };
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800901
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800902 if (dev >= SNDRV_CARDS)
903 return -ENODEV;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800904 if (!enable[dev])
905 return -ENODEV;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800906
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800907 if (devicenum) {
908 card = snd_card_new(devicenum, id[dev], THIS_MODULE, 0);
909 } else {
910 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
911 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800912 if (card == NULL)
913 return -ENOMEM;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800914
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800915 strcpy(card->driver, "SAA7134");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800916
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800917 /* Card "creation" */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800918
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800919 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800920 if (chip == NULL) {
921 return -ENOMEM;
922 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800923
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800924 spin_lock_init(&chip->lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800925
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800926 chip->saadev = saadev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800927
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800928 chip->card = card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800929
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800930 chip->pci = saadev->pci;
931 chip->irq = saadev->pci->irq;
932 chip->iobase = pci_resource_start(saadev->pci, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800933
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800934 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
935 snd_saa7134_free(chip);
936 return err;
937 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800938
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800939 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
940 goto __nodev;
941
942 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
943 goto __nodev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800944
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800945 spin_lock_init(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800946
947 snd_card_set_dev(card, &chip->pci->dev);
948
949 /* End of "creation" */
950
951 strcpy(card->shortname, "SAA7134");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800952 sprintf(card->longname, "%s at 0x%lx irq %d",
953 chip->saadev->name, chip->iobase, chip->irq);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800954
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800955 if ((err = snd_card_register(card)) == 0) {
956 snd_saa7134_cards[dev] = card;
957 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800958 }
959
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800960__nodev:
961 snd_card_free(card);
962 kfree(card);
963 return err;
964}
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800965
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800966void alsa_card_saa7134_exit(void)
967{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800968 int idx;
969 for (idx = 0; idx < SNDRV_CARDS; idx++) {
970 snd_card_free(snd_saa7134_cards[idx]);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800971 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800972}