blob: e9ffe8f144acbf5dfbfb1300c718351a6a9530e1 [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
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080092/*
93 * PCM structure
94 */
95
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080096typedef struct snd_card_saa7134_pcm {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080097 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080098
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080099 spinlock_t lock;
100 unsigned int pcm_size; /* buffer size */
101 unsigned int pcm_count; /* bytes per period */
102 unsigned int pcm_bps; /* bytes per second */
103 snd_pcm_substream_t *substream;
104} snd_card_saa7134_pcm_t;
105
106static snd_card_t *snd_saa7134_cards[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
107
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800108/*
109 * saa7134 DMA audio stop
110 *
111 * Called when the capture device is released or the buffer overflows
112 *
113 * - Copied verbatim from saa7134-oss's dsp_dma_stop. Can be dropped
114 * if we just share dsp_dma_stop and use it here
115 *
116 */
117
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800118static void saa7134_dma_stop(struct saa7134_dev *dev)
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800119
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800120{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800121 dev->oss.dma_blk = -1;
122 dev->oss.dma_running = 0;
123 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800124}
125
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800126/*
127 * saa7134 DMA audio start
128 *
129 * Called when preparing the capture device for use
130 *
131 * - Copied verbatim from saa7134-oss's dsp_dma_start. Can be dropped
132 * if we just share dsp_dma_start and use it here
133 *
134 */
135
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800136static void saa7134_dma_start(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800137{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800138 dev->oss.dma_blk = 0;
139 dev->oss.dma_running = 1;
140 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800141}
142
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800143/*
144 * saa7134 audio DMA IRQ handler
145 *
146 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
147 * Handles shifting between the 2 buffers, manages the read counters,
148 * and notifies ALSA when periods elapse
149 *
150 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
151 *
152 */
153
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800154void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800155{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800156 int next_blk, reg = 0;
157
158 spin_lock(&dev->slock);
159 if (UNSET == dev->oss.dma_blk) {
160 dprintk("irq: recording stopped\n");
161 goto done;
162 }
163 if (0 != (status & 0x0f000000))
164 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
165 if (0 == (status & 0x10000000)) {
166 /* odd */
167 if (0 == (dev->oss.dma_blk & 0x01))
168 reg = SAA7134_RS_BA1(6);
169 } else {
170 /* even */
171 if (1 == (dev->oss.dma_blk & 0x01))
172 reg = SAA7134_RS_BA2(6);
173 }
174 if (0 == reg) {
175 dprintk("irq: field oops [%s]\n",
176 (status & 0x10000000) ? "even" : "odd");
177 goto done;
178 }
179
180 if (dev->oss.read_count >= dev->oss.blksize * (dev->oss.blocks-2)) {
181 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->oss.read_count,
182 dev->oss.bufsize, dev->oss.blocks);
183 saa7134_dma_stop(dev);
184 goto done;
185 }
186
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800187 /* next block addr */
188 next_blk = (dev->oss.dma_blk + 2) % dev->oss.blocks;
189 saa_writel(reg,next_blk * dev->oss.blksize);
190 if (alsa_debug > 2)
191 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
192 (status & 0x10000000) ? "even" : "odd ", next_blk,
193 next_blk * dev->oss.blksize, dev->oss.blocks, dev->oss.blksize, dev->oss.read_count);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800194
195
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800196 /* update status & wake waiting readers */
197 dev->oss.dma_blk = (dev->oss.dma_blk + 1) % dev->oss.blocks;
198 dev->oss.read_count += dev->oss.blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800199
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800200 dev->oss.recording_on = reg;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800201
202 if (dev->oss.read_count >= snd_pcm_lib_period_bytes(dev->oss.substream)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800203 spin_unlock(&dev->slock);
204 snd_pcm_period_elapsed(dev->oss.substream);
205 spin_lock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800206 }
207 done:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800208 spin_unlock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800209
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800210}
211
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800212/*
213 * ALSA capture trigger
214 *
215 * - One of the ALSA capture callbacks.
216 *
217 * Called whenever a capture is started or stopped. Must be defined,
218 * but there's nothing we want to do here
219 *
220 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800221
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800222static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
223 int cmd)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800224{
225 return 0;
226}
227
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800228/*
229 * DMA buffer config
230 *
231 * Sets the values that will later be used as the size of the buffer,
232 * size of the fragments, and total number of fragments.
233 * Must be called during the preparation stage, before memory is
234 * allocated
235 *
236 * - Copied verbatim from saa7134-oss. Can be dropped
237 * if we just share dsp_buffer_conf from OSS.
238 */
239
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800240static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800241{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800242 if (blksize < 0x100)
243 blksize = 0x100;
244 if (blksize > 0x10000)
245 blksize = 0x10000;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800246
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800247 if (blocks < 2)
248 blocks = 2;
249 if ((blksize * blocks) > 1024*1024)
250 blocks = 1024*1024 / blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800251
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800252 dev->oss.blocks = blocks;
253 dev->oss.blksize = blksize;
254 dev->oss.bufsize = blksize * blocks;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800255
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800256 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
257 blocks,blksize,blksize * blocks / 1024);
258 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800259}
260
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800261/*
262 * DMA buffer initialization
263 *
264 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
265 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
266 * usage of V4L's
267 *
268 * - Copied verbatim from saa7134-oss. Can be dropped
269 * if we just share dsp_buffer_init from OSS.
270 */
271
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800272static int dsp_buffer_init(struct saa7134_dev *dev)
273{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800274 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800275
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800276 if (!dev->oss.bufsize)
277 BUG();
278 videobuf_dma_init(&dev->oss.dma);
279 err = videobuf_dma_init_kernel(&dev->oss.dma, PCI_DMA_FROMDEVICE,
280 (dev->oss.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
281 if (0 != err)
282 return err;
283 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800284}
285
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800286/*
287 * ALSA PCM preparation
288 *
289 * - One of the ALSA capture callbacks.
290 *
291 * Called right after the capture device is opened, this function configures
292 * the buffer using the previously defined functions, allocates the memory,
293 * sets up the hardware registers, and then starts the DMA. When this function
294 * returns, the audio should be flowing.
295 *
296 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800297
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800298static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800299{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800300 snd_pcm_runtime_t *runtime = substream->runtime;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800301 int err, bswap, sign;
302 u32 fmt, control;
303 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800304 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800305 struct saa7134_dev *dev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800306 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
307 unsigned int bps;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800308 unsigned long size;
309 unsigned count;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800310
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800311 size = snd_pcm_lib_buffer_bytes(substream);
312 count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800313
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800314 saapcm->saadev->oss.substream = substream;
315 bps = runtime->rate * runtime->channels;
316 bps *= snd_pcm_format_width(runtime->format);
317 bps /= 8;
318 if (bps <= 0)
319 return -EINVAL;
320 saapcm->pcm_bps = bps;
321 saapcm->pcm_size = snd_pcm_lib_buffer_bytes(substream);
322 saapcm->pcm_count = snd_pcm_lib_period_bytes(substream);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800323
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800324
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800325 dev=saa7134->saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800326
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800327 dsp_buffer_conf(dev,saapcm->pcm_count,(saapcm->pcm_size/saapcm->pcm_count));
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800328
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800329 err = dsp_buffer_init(dev);
330 if (0 != err)
331 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800332
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800333 /* prepare buffer */
334 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->oss.dma)))
335 return err;
336 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->oss.pt)))
337 goto fail1;
338 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->oss.pt,
339 dev->oss.dma.sglist,
340 dev->oss.dma.sglen,
341 0)))
342 goto fail2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800343
344
345
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800346 switch (runtime->format) {
347 case SNDRV_PCM_FORMAT_U8:
348 case SNDRV_PCM_FORMAT_S8:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800349 fmt = 0x00;
350 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800351 case SNDRV_PCM_FORMAT_U16_LE:
352 case SNDRV_PCM_FORMAT_U16_BE:
353 case SNDRV_PCM_FORMAT_S16_LE:
354 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800355 fmt = 0x01;
356 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800357 default:
358 err = -EINVAL;
359 return 1;
360 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800361
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800362 switch (runtime->format) {
363 case SNDRV_PCM_FORMAT_S8:
364 case SNDRV_PCM_FORMAT_S16_LE:
365 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800366 sign = 1;
367 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800368 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800369 sign = 0;
370 break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800371 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 switch (runtime->format) {
374 case SNDRV_PCM_FORMAT_U16_BE:
375 case SNDRV_PCM_FORMAT_S16_BE:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800376 bswap = 1; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800377 default:
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800378 bswap = 0; break;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800379 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800380
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800381 switch (dev->pci->device) {
382 case PCI_DEVICE_ID_PHILIPS_SAA7134:
383 if (1 == runtime->channels)
384 fmt |= (1 << 3);
385 if (2 == runtime->channels)
386 fmt |= (3 << 3);
387 if (sign)
388 fmt |= 0x04;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800389
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800390 fmt |= (MIXER_ADDR_TVTUNER == dev->oss.input) ? 0xc0 : 0x80;
391 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->oss.blksize - 1) & 0x0000ff));
392 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->oss.blksize - 1) & 0x00ff00) >> 8);
393 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->oss.blksize - 1) & 0xff0000) >> 16);
394 saa_writeb(SAA7134_AUDIO_FORMAT_CTRL, fmt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800395
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800396 break;
397 case PCI_DEVICE_ID_PHILIPS_SAA7133:
398 case PCI_DEVICE_ID_PHILIPS_SAA7135:
399 if (1 == runtime->channels)
400 fmt |= (1 << 4);
401 if (2 == runtime->channels)
402 fmt |= (2 << 4);
403 if (!sign)
404 fmt |= 0x04;
405 saa_writel(SAA7133_NUM_SAMPLES, dev->oss.blksize -1);
406 saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210 | (fmt << 24));
407 //saa_writel(SAA7133_AUDIO_CHANNEL, 0x543210);
408 break;
409 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800410
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800411 dprintk("rec_start: afmt=%d ch=%d => fmt=0x%x swap=%c\n",
412 runtime->format, runtime->channels, fmt,
413 bswap ? 'b' : '-');
414 /* dma: setup channel 6 (= AUDIO) */
415 control = SAA7134_RS_CONTROL_BURST_16 |
416 SAA7134_RS_CONTROL_ME |
417 (dev->oss.pt.dma >> 12);
418 if (bswap)
419 control |= SAA7134_RS_CONTROL_BSWAP;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800420
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800421 /* I should be able to use runtime->dma_addr in the control
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800422 byte, but it doesn't work. So I allocate the DMA using the
423 V4L functions, and force ALSA to use that as the DMA area */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800424
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800425 runtime->dma_area = dev->oss.dma.vmalloc;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800426
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800427 saa_writel(SAA7134_RS_BA1(6),0);
428 saa_writel(SAA7134_RS_BA2(6),dev->oss.blksize);
429 saa_writel(SAA7134_RS_PITCH(6),0);
430 saa_writel(SAA7134_RS_CONTROL(6),control);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800431
432 dev->oss.rate = runtime->rate;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800433 /* start dma */
434 spin_lock_irqsave(&dev->slock,flags);
435 saa7134_dma_start(dev);
436 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800437
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800438 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800439 fail2:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800440 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800441 fail1:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800442 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
443 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800444
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800445
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800446}
447
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800448/*
449 * ALSA pointer fetching
450 *
451 * - One of the ALSA capture callbacks.
452 *
453 * Called whenever a period elapses, it must return the current hardware
454 * position of the buffer.
455 * Also resets the read counter used to prevent overruns
456 *
457 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800458
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800459static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800460{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800461 snd_pcm_runtime_t *runtime = substream->runtime;
462 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
463 struct saa7134_dev *dev=saapcm->saadev;
464
465
466
467 if (dev->oss.read_count) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800468 dev->oss.read_count -= snd_pcm_lib_period_bytes(substream);
469 dev->oss.read_offset += snd_pcm_lib_period_bytes(substream);
470 if (dev->oss.read_offset == dev->oss.bufsize)
471 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800472 }
473
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800474 return bytes_to_frames(runtime, dev->oss.read_offset);
475}
476
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800477/*
478 * ALSA hardware capabilities definition
479 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800480
481static snd_pcm_hardware_t snd_card_saa7134_capture =
482{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800483 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
484 SNDRV_PCM_INFO_BLOCK_TRANSFER |
485 SNDRV_PCM_INFO_MMAP_VALID),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800486 .formats = USE_FORMATS,
487 .rates = USE_RATE,
488 .rate_min = USE_RATE_MIN,
489 .rate_max = USE_RATE_MAX,
490 .channels_min = USE_CHANNELS_MIN,
491 .channels_max = USE_CHANNELS_MAX,
492 .buffer_bytes_max = MAX_BUFFER_SIZE,
493 .period_bytes_min = 64,
494 .period_bytes_max = MAX_BUFFER_SIZE,
495 .periods_min = USE_PERIODS_MIN,
496 .periods_max = USE_PERIODS_MAX,
497 .fifo_size = 0x08070503,
498};
499
500static void snd_card_saa7134_runtime_free(snd_pcm_runtime_t *runtime)
501{
502 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
503
504 kfree(saapcm);
505}
506
507
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800508/*
509 * ALSA hardware params
510 *
511 * - One of the ALSA capture callbacks.
512 *
513 * Called on initialization, right before the PCM preparation
514 * Usually used in ALSA to allocate the DMA, but since we don't use the
515 * ALSA DMA I'm almost sure this isn't necessary.
516 *
517 */
518
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800519static int snd_card_saa7134_hw_params(snd_pcm_substream_t * substream,
520 snd_pcm_hw_params_t * hw_params)
521{
522
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800523 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800524
525
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800526}
527
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800528/*
529 * ALSA hardware release
530 *
531 * - One of the ALSA capture callbacks.
532 *
533 * Called after closing the device, but before snd_card_saa7134_capture_close
534 * Usually used in ALSA to free the DMA, but since we don't use the
535 * ALSA DMA I'm almost sure this isn't necessary.
536 *
537 */
538
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800539static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800540{
541 return snd_pcm_lib_free_pages(substream);
542}
543
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800544/*
545 * DMA buffer release
546 *
547 * Called after closing the device, during snd_card_saa7134_capture_close
548 *
549 */
550
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800551static int dsp_buffer_free(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800552{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800553 if (!dev->oss.blksize)
554 BUG();
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800555
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800556 videobuf_dma_free(&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800557
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800558 dev->oss.blocks = 0;
559 dev->oss.blksize = 0;
560 dev->oss.bufsize = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800561
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800562 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800563}
564
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800565/*
566 * ALSA capture finish
567 *
568 * - One of the ALSA capture callbacks.
569 *
570 * Called after closing the device. It stops the DMA audio and releases
571 * the buffers
572 *
573 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800574
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800575static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800576{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800577 snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800578 struct saa7134_dev *dev = chip->saadev;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800579 unsigned long flags;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800580
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800581 /* stop dma */
582 spin_lock_irqsave(&dev->slock,flags);
583 saa7134_dma_stop(dev);
584 spin_unlock_irqrestore(&dev->slock,flags);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800585
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800586 /* unlock buffer */
587 saa7134_pgtable_free(dev->pci,&dev->oss.pt);
588 videobuf_dma_pci_unmap(dev->pci,&dev->oss.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800589
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800590 dsp_buffer_free(dev);
591 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800592}
593
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800594/*
595 * ALSA capture start
596 *
597 * - One of the ALSA capture callbacks.
598 *
599 * Called when opening the device. It creates and populates the PCM
600 * structure
601 *
602 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800603
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800604static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
605{
606 snd_pcm_runtime_t *runtime = substream->runtime;
607 snd_card_saa7134_pcm_t *saapcm;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800608 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
609 struct saa7134_dev *dev = saa7134->saadev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800610 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800611
612 down(&dev->oss.lock);
613
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800614 dev->oss.afmt = SNDRV_PCM_FORMAT_U8;
615 dev->oss.channels = 2;
616 dev->oss.read_count = 0;
617 dev->oss.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800618
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800619 up(&dev->oss.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800620
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800621 saapcm = kcalloc(1, sizeof(*saapcm), GFP_KERNEL);
622 if (saapcm == NULL)
623 return -ENOMEM;
624 saapcm->saadev=saa7134->saadev;
625
626 spin_lock_init(&saapcm->lock);
627
628 saapcm->substream = substream;
629 runtime->private_data = saapcm;
630 runtime->private_free = snd_card_saa7134_runtime_free;
631 runtime->hw = snd_card_saa7134_capture;
632
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800633 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
634 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800635
636 return 0;
637}
638
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800639/*
640 * ALSA capture callbacks definition
641 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800642
643static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
644 .open = snd_card_saa7134_capture_open,
645 .close = snd_card_saa7134_capture_close,
646 .ioctl = snd_pcm_lib_ioctl,
647 .hw_params = snd_card_saa7134_hw_params,
648 .hw_free = snd_card_saa7134_hw_free,
649 .prepare = snd_card_saa7134_capture_prepare,
650 .trigger = snd_card_saa7134_capture_trigger,
651 .pointer = snd_card_saa7134_capture_pointer,
652};
653
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800654/*
655 * ALSA PCM setup
656 *
657 * Called when initializing the board. Sets up the name and hooks up
658 * the callbacks
659 *
660 */
661
662static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800663{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800664 snd_pcm_t *pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800665 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800666
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800667 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800668 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800669 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
670 pcm->private_data = saa7134;
671 pcm->info_flags = 0;
672 strcpy(pcm->name, "SAA7134 PCM");
673 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
674 snd_dma_pci_data(saa7134->pci),
675 128*1024, 256*1024);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800676 return 0;
677}
678
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800679#define SAA713x_VOLUME(xname, xindex, addr) \
680{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
681 .info = snd_saa7134_volume_info, \
682 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
683 .private_value = addr }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800684
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800685static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
686{
687 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
688 uinfo->count = 2;
689 uinfo->value.integer.min = 0;
690 uinfo->value.integer.max = 20;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800691 return 0;
692}
693
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800694static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800695{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800696 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
697 unsigned long flags;
698 int addr = kcontrol->private_value;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800699
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800700 spin_lock_irqsave(&chip->mixer_lock, flags);
701 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
702 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
703 spin_unlock_irqrestore(&chip->mixer_lock, flags);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800704 return 0;
705}
706
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800707static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
708{
709 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
710 unsigned long flags;
711 int change, addr = kcontrol->private_value;
712 int left, right;
713
714 left = ucontrol->value.integer.value[0];
715 if (left < 0)
716 left = 0;
717 if (left > 20)
718 left = 20;
719 right = ucontrol->value.integer.value[1];
720 if (right < 0)
721 right = 0;
722 if (right > 20)
723 right = 20;
724 spin_lock_irqsave(&chip->mixer_lock, flags);
725 change = chip->mixer_volume[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800726 chip->mixer_volume[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800727 chip->mixer_volume[addr][0] = left;
728 chip->mixer_volume[addr][1] = right;
729 spin_unlock_irqrestore(&chip->mixer_lock, flags);
730 return change;
731}
732
733#define SAA713x_CAPSRC(xname, xindex, addr) \
734{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
735 .info = snd_saa7134_capsrc_info, \
736 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
737 .private_value = addr }
738
739static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
740{
741 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800742 uinfo->count = 2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800743 uinfo->value.integer.min = 0;
744 uinfo->value.integer.max = 1;
745 return 0;
746}
747
748static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
749{
750 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
751 unsigned long flags;
752 int addr = kcontrol->private_value;
753
754 spin_lock_irqsave(&chip->mixer_lock, flags);
755 ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
756 ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
757 spin_unlock_irqrestore(&chip->mixer_lock, flags);
758 return 0;
759}
760
761static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
762{
763 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
764 unsigned long flags;
765 int change, addr = kcontrol->private_value;
766 int left, right;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800767 u32 anabar, xbarin;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800768 int analog_io, rate;
769 struct saa7134_dev *dev;
770
771 dev = chip->saadev;
772
773 left = ucontrol->value.integer.value[0] & 1;
774 right = ucontrol->value.integer.value[1] & 1;
775 spin_lock_irqsave(&chip->mixer_lock, flags);
776
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800777 change = chip->capture_source[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800778 chip->capture_source[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800779 chip->capture_source[addr][0] = left;
780 chip->capture_source[addr][1] = right;
781 dev->oss.input=addr;
782 spin_unlock_irqrestore(&chip->mixer_lock, flags);
783
784
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800785 if (change) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800786 switch (dev->pci->device) {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800787
788 case PCI_DEVICE_ID_PHILIPS_SAA7134:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800789 switch (addr) {
790 case MIXER_ADDR_TVTUNER:
791 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
792 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
793 break;
794 case MIXER_ADDR_LINE1:
795 case MIXER_ADDR_LINE2:
796 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
797 rate = (32000 == dev->oss.rate) ? 0x01 : 0x03;
798 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
799 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
800 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
801 break;
802 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800803
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800804 break;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800805 case PCI_DEVICE_ID_PHILIPS_SAA7133:
806 case PCI_DEVICE_ID_PHILIPS_SAA7135:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800807 xbarin = 0x03; // adc
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800808 anabar = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800809 switch (addr) {
810 case MIXER_ADDR_TVTUNER:
811 xbarin = 0; // Demodulator
812 anabar = 2; // DACs
813 break;
814 case MIXER_ADDR_LINE1:
815 anabar = 0; // aux1, aux1
816 break;
817 case MIXER_ADDR_LINE2:
818 anabar = 9; // aux2, aux2
819 break;
820 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800821
822 /* output xbar always main channel */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800823 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800824
825 if (left || right) { // We've got data, turn the input on
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800826 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
827 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
828 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800829 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800830 //saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL2, 0x101010);
831 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
832 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800833 }
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800834 break;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800835 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800836 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800837
838 return change;
839}
840
841static snd_kcontrol_new_t snd_saa7134_controls[] = {
842SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
843SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
844SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
845SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
846SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
847SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800848};
849
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800850/*
851 * ALSA mixer setup
852 *
853 * Called when initializing the board. Sets up the name and hooks up
854 * the callbacks
855 *
856 */
857
858static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800859{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800860 snd_card_t *card = chip->card;
861 unsigned int idx;
862 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800863
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800864 snd_assert(chip != NULL, return -EINVAL);
865 strcpy(card->mixername, "SAA7134 Mixer");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800866
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800867 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
868 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
869 return err;
870 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800871 return 0;
872}
873
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800874static int snd_saa7134_free(snd_card_saa7134_t *chip)
875{
876 return 0;
877}
878
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800879static int snd_saa7134_dev_free(snd_device_t *device)
880{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800881 snd_card_saa7134_t *chip = device->device_data;
882 return snd_saa7134_free(chip);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800883}
884
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800885/*
886 * ALSA initialization
887 *
888 * Called by saa7134-core, it creates the basic structures and registers
889 * the ALSA devices
890 *
891 */
892
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800893int alsa_card_saa7134_create(struct saa7134_dev *saadev, unsigned int devicenum)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800894{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800895 static int dev;
896 snd_card_t *card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800897 snd_card_saa7134_t *chip;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800898 int err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800899 static snd_device_ops_t ops = {
900 .dev_free = snd_saa7134_dev_free,
901 };
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800902
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800903 if (dev >= SNDRV_CARDS)
904 return -ENODEV;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800905 if (!enable[dev])
906 return -ENODEV;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800907
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800908 if (devicenum) {
909 card = snd_card_new(devicenum, id[dev], THIS_MODULE, 0);
910 } else {
911 card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
912 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800913 if (card == NULL)
914 return -ENOMEM;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800915
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800916 strcpy(card->driver, "SAA7134");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800917
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800918 /* Card "creation" */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800919
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800920 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800921 if (chip == NULL) {
922 return -ENOMEM;
923 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800924
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800925 spin_lock_init(&chip->lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800926
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800927 chip->saadev = saadev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800928
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800929 chip->card = card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800930
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800931 chip->pci = saadev->pci;
932 chip->irq = saadev->pci->irq;
933 chip->iobase = pci_resource_start(saadev->pci, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800934
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800935 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
936 snd_saa7134_free(chip);
937 return err;
938 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800939
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800940 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
941 goto __nodev;
942
943 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
944 goto __nodev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800945
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800946 spin_lock_init(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800947
948 snd_card_set_dev(card, &chip->pci->dev);
949
950 /* End of "creation" */
951
952 strcpy(card->shortname, "SAA7134");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800953 sprintf(card->longname, "%s at 0x%lx irq %d",
954 chip->saadev->name, chip->iobase, chip->irq);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800955
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800956 if ((err = snd_card_register(card)) == 0) {
957 snd_saa7134_cards[dev] = card;
958 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800959 }
960
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800961__nodev:
962 snd_card_free(card);
963 kfree(card);
964 return err;
965}
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800966
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800967void alsa_card_saa7134_exit(void)
968{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800969 int idx;
970 for (idx = 0; idx < SNDRV_CARDS; idx++) {
971 snd_card_free(snd_saa7134_cards[idx]);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800972 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800973}