blob: 289d04064b7013a4a61d69fc4ff7989c5dfbd5b8 [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>
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080033#include <sound/initval.h>
34
35#include "saa7134.h"
36#include "saa7134-reg.h"
37
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080038static unsigned int debug = 0;
39module_param(debug, int, 0644);
40MODULE_PARM_DESC(debug,"enable debug messages [alsa]");
41
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080042/*
43 * Configuration macros
44 */
45
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080046/* defaults */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080047#define MIXER_ADDR_TVTUNER 0
48#define MIXER_ADDR_LINE1 1
49#define MIXER_ADDR_LINE2 2
50#define MIXER_ADDR_LAST 2
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080051
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080052static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
53static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
54static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 0};
55
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080056module_param_array(index, int, NULL, 0444);
57MODULE_PARM_DESC(index, "Index value for SAA7134 capture interface(s).");
58
Ricardo Cerqueirac817e762005-11-13 16:07:47 -080059int position;
60
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080061#define dprintk(fmt, arg...) if (debug) \
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080062 printk(KERN_DEBUG "%s/alsa: " fmt, dev->name , ## arg)
63
64/*
65 * Main chip structure
66 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080067typedef struct snd_card_saa7134 {
68 snd_card_t *card;
69 spinlock_t mixer_lock;
70 int mixer_volume[MIXER_ADDR_LAST+1][2];
71 int capture_source[MIXER_ADDR_LAST+1][2];
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080072 struct pci_dev *pci;
73 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080074
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080075 unsigned long iobase;
76 int irq;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080077
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080078 spinlock_t lock;
79} snd_card_saa7134_t;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080080
Nickolay V. Shmyrev07eef6c2005-11-08 21:38:14 -080081
82
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -080083/*
84 * PCM structure
85 */
86
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080087typedef struct snd_card_saa7134_pcm {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080088 struct saa7134_dev *saadev;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -080089
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080090 spinlock_t lock;
91 unsigned int pcm_size; /* buffer size */
92 unsigned int pcm_count; /* bytes per period */
93 unsigned int pcm_bps; /* bytes per second */
94 snd_pcm_substream_t *substream;
95} snd_card_saa7134_pcm_t;
96
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -080097static snd_card_t *snd_saa7134_cards[SNDRV_CARDS];
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -080098
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -080099
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800100/*
101 * saa7134 DMA audio stop
102 *
103 * Called when the capture device is released or the buffer overflows
104 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800105 * - Copied verbatim from saa7134-oss's dsp_dma_stop.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800106 *
107 */
108
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800109static void saa7134_dma_stop(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800110{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800111 dev->dmasound.dma_blk = -1;
112 dev->dmasound.dma_running = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800113 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800114}
115
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800116/*
117 * saa7134 DMA audio start
118 *
119 * Called when preparing the capture device for use
120 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800121 * - Copied verbatim from saa7134-oss's dsp_dma_start.
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800122 *
123 */
124
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800125static void saa7134_dma_start(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800126{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800127 dev->dmasound.dma_blk = 0;
128 dev->dmasound.dma_running = 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800129 saa7134_set_dmabits(dev);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800130}
131
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800132/*
133 * saa7134 audio DMA IRQ handler
134 *
135 * Called whenever we get an SAA7134_IRQ_REPORT_DONE_RA3 interrupt
136 * Handles shifting between the 2 buffers, manages the read counters,
137 * and notifies ALSA when periods elapse
138 *
139 * - Mostly copied from saa7134-oss's saa7134_irq_oss_done.
140 *
141 */
142
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800143void saa7134_irq_alsa_done(struct saa7134_dev *dev, unsigned long status)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800144{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800145 int next_blk, reg = 0;
146
147 spin_lock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800148 if (UNSET == dev->dmasound.dma_blk) {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800149 dprintk("irq: recording stopped\n");
150 goto done;
151 }
152 if (0 != (status & 0x0f000000))
153 dprintk("irq: lost %ld\n", (status >> 24) & 0x0f);
154 if (0 == (status & 0x10000000)) {
155 /* odd */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800156 if (0 == (dev->dmasound.dma_blk & 0x01))
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800157 reg = SAA7134_RS_BA1(6);
158 } else {
159 /* even */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800160 if (1 == (dev->dmasound.dma_blk & 0x01))
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800161 reg = SAA7134_RS_BA2(6);
162 }
163 if (0 == reg) {
164 dprintk("irq: field oops [%s]\n",
165 (status & 0x10000000) ? "even" : "odd");
166 goto done;
167 }
168
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800169 if (dev->dmasound.read_count >= dev->dmasound.blksize * (dev->dmasound.blocks-2)) {
170 dprintk("irq: overrun [full=%d/%d] - Blocks in %d\n",dev->dmasound.read_count,
171 dev->dmasound.bufsize, dev->dmasound.blocks);
172 snd_pcm_stop(dev->dmasound.substream,SNDRV_PCM_STATE_XRUN);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800173 goto done;
174 }
175
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800176 /* next block addr */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800177 next_blk = (dev->dmasound.dma_blk + 2) % dev->dmasound.blocks;
178 saa_writel(reg,next_blk * dev->dmasound.blksize);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800179 if (debug > 2)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800180 dprintk("irq: ok, %s, next_blk=%d, addr=%x, blocks=%u, size=%u, read=%u\n",
181 (status & 0x10000000) ? "even" : "odd ", next_blk,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800182 next_blk * dev->dmasound.blksize, dev->dmasound.blocks, dev->dmasound.blksize, dev->dmasound.read_count);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800183
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800184 /* update status & wake waiting readers */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800185 dev->dmasound.dma_blk = (dev->dmasound.dma_blk + 1) % dev->dmasound.blocks;
186 dev->dmasound.read_count += dev->dmasound.blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800187
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800188 dev->dmasound.recording_on = reg;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800189
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800190 if (dev->dmasound.read_count >= snd_pcm_lib_period_bytes(dev->dmasound.substream)) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800191 spin_unlock(&dev->slock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800192 snd_pcm_period_elapsed(dev->dmasound.substream);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800193 spin_lock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800194 }
195 done:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800196 spin_unlock(&dev->slock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800197
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800198}
199
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800200/*
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800201 * IRQ request handler
202 *
203 * Runs along with saa7134's IRQ handler, discards anything that isn't
204 * DMA sound
205 *
206 */
207
208static irqreturn_t saa7134_alsa_irq(int irq, void *dev_id, struct pt_regs *regs)
209{
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800210 snd_card_saa7134_t *saa7134 = dev_id;
211 struct saa7134_dev *dev = saa7134->saadev;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800212 unsigned long report, status;
213 int loop, handled = 0;
214
215 for (loop = 0; loop < 10; loop++) {
216 report = saa_readl(SAA7134_IRQ_REPORT);
217 status = saa_readl(SAA7134_IRQ_STATUS);
218
219 if (report & SAA7134_IRQ_REPORT_DONE_RA3) {
220 handled = 1;
221 saa_writel(SAA7134_IRQ_REPORT,report);
222 saa7134_irq_alsa_done(dev, status);
223 } else {
224 goto out;
225 }
226 }
227
228 if (loop == 10) {
229 dprintk("error! looping IRQ!");
230 }
231
232out:
233 return IRQ_RETVAL(handled);
234}
235
236/*
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800237 * ALSA capture trigger
238 *
239 * - One of the ALSA capture callbacks.
240 *
241 * Called whenever a capture is started or stopped. Must be defined,
242 * but there's nothing we want to do here
243 *
244 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800245
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800246static int snd_card_saa7134_capture_trigger(snd_pcm_substream_t * substream,
247 int cmd)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800248{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800249 snd_pcm_runtime_t *runtime = substream->runtime;
250 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
251 struct saa7134_dev *dev=saapcm->saadev;
252 int err = 0;
253
254 spin_lock_irq(&dev->slock);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800255 if (cmd == SNDRV_PCM_TRIGGER_START) {
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800256 /* start dma */
257 saa7134_dma_start(dev);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800258 } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800259 /* stop dma */
260 saa7134_dma_stop(dev);
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800261 } else {
262 err = -EINVAL;
263 }
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800264 spin_unlock_irq(&dev->slock);
265
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800266 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800267}
268
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800269/*
270 * DMA buffer config
271 *
272 * Sets the values that will later be used as the size of the buffer,
273 * size of the fragments, and total number of fragments.
274 * Must be called during the preparation stage, before memory is
275 * allocated
276 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800277 * - Copied verbatim from saa7134-oss.
278 *
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800279 */
280
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800281static int dsp_buffer_conf(struct saa7134_dev *dev, int blksize, int blocks)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800282{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800283 if (blksize < 0x100)
284 blksize = 0x100;
285 if (blksize > 0x10000)
286 blksize = 0x10000;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800287
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800288 if (blocks < 2)
289 blocks = 2;
290 if ((blksize * blocks) > 1024*1024)
291 blocks = 1024*1024 / blksize;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800292
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800293 dev->dmasound.blocks = blocks;
294 dev->dmasound.blksize = blksize;
295 dev->dmasound.bufsize = blksize * blocks;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800296
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800297 dprintk("buffer config: %d blocks / %d bytes, %d kB total\n",
298 blocks,blksize,blksize * blocks / 1024);
299 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800300}
301
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800302/*
303 * DMA buffer initialization
304 *
305 * Uses V4L functions to initialize the DMA. Shouldn't be necessary in
306 * ALSA, but I was unable to use ALSA's own DMA, and had to force the
307 * usage of V4L's
308 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800309 * - Copied verbatim from saa7134-oss.
310 *
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800311 */
312
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800313static int dsp_buffer_init(struct saa7134_dev *dev)
314{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800315 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800316
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800317 if (!dev->dmasound.bufsize)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800318 BUG();
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800319 videobuf_dma_init(&dev->dmasound.dma);
320 err = videobuf_dma_init_kernel(&dev->dmasound.dma, PCI_DMA_FROMDEVICE,
321 (dev->dmasound.bufsize + PAGE_SIZE) >> PAGE_SHIFT);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800322 if (0 != err)
323 return err;
324 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800325}
326
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800327/*
328 * ALSA PCM preparation
329 *
330 * - One of the ALSA capture callbacks.
331 *
332 * Called right after the capture device is opened, this function configures
333 * the buffer using the previously defined functions, allocates the memory,
334 * sets up the hardware registers, and then starts the DMA. When this function
335 * returns, the audio should be flowing.
336 *
337 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800338
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800339static int snd_card_saa7134_capture_prepare(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800340{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800341 snd_pcm_runtime_t *runtime = substream->runtime;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800342 int err, bswap, sign;
343 u32 fmt, control;
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 Cerqueirab54134b2005-11-08 21:38:54 -0800354 saapcm->saadev->dmasound.substream = substream;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800355 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)
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800371 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800372
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800373 /* prepare buffer */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800374 if (0 != (err = videobuf_dma_pci_map(dev->pci,&dev->dmasound.dma)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800375 return err;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800376 if (0 != (err = saa7134_pgtable_alloc(dev->pci,&dev->dmasound.pt)))
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800377 goto fail1;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800378 if (0 != (err = saa7134_pgtable_build(dev->pci,&dev->dmasound.pt,
379 dev->dmasound.dma.sglist,
380 dev->dmasound.dma.sglen,
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800381 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
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800430 fmt |= (MIXER_ADDR_TVTUNER == dev->dmasound.input) ? 0xc0 : 0x80;
431 saa_writeb(SAA7134_NUM_SAMPLES0, ((dev->dmasound.blksize - 1) & 0x0000ff));
432 saa_writeb(SAA7134_NUM_SAMPLES1, ((dev->dmasound.blksize - 1) & 0x00ff00) >> 8);
433 saa_writeb(SAA7134_NUM_SAMPLES2, ((dev->dmasound.blksize - 1) & 0xff0000) >> 16);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800434 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;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800445 saa_writel(SAA7133_NUM_SAMPLES, dev->dmasound.blksize -1);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800446 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 |
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800457 (dev->dmasound.pt.dma >> 12);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800458 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 Cerqueirab54134b2005-11-08 21:38:54 -0800465 runtime->dma_area = dev->dmasound.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);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800468 saa_writel(SAA7134_RS_BA2(6),dev->dmasound.blksize);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800469 saa_writel(SAA7134_RS_PITCH(6),0);
470 saa_writel(SAA7134_RS_CONTROL(6),control);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800471
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800472 dev->dmasound.rate = runtime->rate;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800473
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800474 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800475 fail2:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800476 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800477 fail1:
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800478 videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800479 return err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800480
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800481
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800482}
483
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800484/*
485 * ALSA pointer fetching
486 *
487 * - One of the ALSA capture callbacks.
488 *
489 * Called whenever a period elapses, it must return the current hardware
490 * position of the buffer.
491 * Also resets the read counter used to prevent overruns
492 *
493 */
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800494
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800495static snd_pcm_uframes_t snd_card_saa7134_capture_pointer(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800496{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800497 snd_pcm_runtime_t *runtime = substream->runtime;
498 snd_card_saa7134_pcm_t *saapcm = runtime->private_data;
499 struct saa7134_dev *dev=saapcm->saadev;
500
501
502
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800503 if (dev->dmasound.read_count) {
504 dev->dmasound.read_count -= snd_pcm_lib_period_bytes(substream);
505 dev->dmasound.read_offset += snd_pcm_lib_period_bytes(substream);
506 if (dev->dmasound.read_offset == dev->dmasound.bufsize)
507 dev->dmasound.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800508 }
509
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800510 return bytes_to_frames(runtime, dev->dmasound.read_offset);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800511}
512
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800513/*
514 * ALSA hardware capabilities definition
515 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800516
517static snd_pcm_hardware_t snd_card_saa7134_capture =
518{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800519 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
Mauro Carvalho Chehabf2421ca2005-11-08 21:37:45 -0800520 SNDRV_PCM_INFO_BLOCK_TRANSFER |
521 SNDRV_PCM_INFO_MMAP_VALID),
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800522 .formats = SNDRV_PCM_FMTBIT_S16_LE | \
523 SNDRV_PCM_FMTBIT_S16_BE | \
524 SNDRV_PCM_FMTBIT_S8 | \
525 SNDRV_PCM_FMTBIT_U8 | \
526 SNDRV_PCM_FMTBIT_U16_LE | \
527 SNDRV_PCM_FMTBIT_U16_BE,
528 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_48000,
529 .rate_min = 32000,
530 .rate_max = 48000,
531 .channels_min = 1,
532 .channels_max = 2,
533 .buffer_bytes_max = (256*1024),
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800534 .period_bytes_min = 64,
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800535 .period_bytes_max = (256*1024),
536 .periods_min = 2,
537 .periods_max = 1024,
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800538};
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{
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800562 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800563
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800564}
565
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800566/*
567 * ALSA hardware release
568 *
569 * - One of the ALSA capture callbacks.
570 *
571 * Called after closing the device, but before snd_card_saa7134_capture_close
572 * Usually used in ALSA to free the DMA, but since we don't use the
573 * ALSA DMA I'm almost sure this isn't necessary.
574 *
575 */
576
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800577static int snd_card_saa7134_hw_free(snd_pcm_substream_t * substream)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800578{
Ricardo Cerqueirae8b23c02005-11-08 21:37:56 -0800579 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800580}
581
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800582/*
583 * DMA buffer release
584 *
585 * Called after closing the device, during snd_card_saa7134_capture_close
586 *
587 */
588
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800589static int dsp_buffer_free(struct saa7134_dev *dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800590{
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800591 if (!dev->dmasound.blksize)
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800592 BUG();
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800593
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800594 videobuf_dma_free(&dev->dmasound.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800595
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800596 dev->dmasound.blocks = 0;
597 dev->dmasound.blksize = 0;
598 dev->dmasound.bufsize = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800599
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800600 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800601}
602
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800603/*
604 * ALSA capture finish
605 *
606 * - One of the ALSA capture callbacks.
607 *
608 * Called after closing the device. It stops the DMA audio and releases
609 * the buffers
610 *
611 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800612
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800613static int snd_card_saa7134_capture_close(snd_pcm_substream_t * substream)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800614{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800615 snd_card_saa7134_t *chip = snd_pcm_substream_chip(substream);
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800616 struct saa7134_dev *dev = chip->saadev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800617
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800618 /* unlock buffer */
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800619 saa7134_pgtable_free(dev->pci,&dev->dmasound.pt);
620 videobuf_dma_pci_unmap(dev->pci,&dev->dmasound.dma);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800621
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800622 dsp_buffer_free(dev);
623 return 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800624}
625
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800626/*
627 * ALSA capture start
628 *
629 * - One of the ALSA capture callbacks.
630 *
631 * Called when opening the device. It creates and populates the PCM
632 * structure
633 *
634 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800635
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800636static int snd_card_saa7134_capture_open(snd_pcm_substream_t * substream)
637{
638 snd_pcm_runtime_t *runtime = substream->runtime;
639 snd_card_saa7134_pcm_t *saapcm;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800640 snd_card_saa7134_t *saa7134 = snd_pcm_substream_chip(substream);
641 struct saa7134_dev *dev = saa7134->saadev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800642 int err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800643
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800644 down(&dev->dmasound.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800645
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800646 dev->dmasound.afmt = SNDRV_PCM_FORMAT_U8;
647 dev->dmasound.channels = 2;
648 dev->dmasound.read_count = 0;
649 dev->dmasound.read_offset = 0;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800650
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800651 up(&dev->dmasound.lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800652
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800653 saapcm = kzalloc(sizeof(*saapcm), GFP_KERNEL);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800654 if (saapcm == NULL)
655 return -ENOMEM;
656 saapcm->saadev=saa7134->saadev;
657
658 spin_lock_init(&saapcm->lock);
659
660 saapcm->substream = substream;
661 runtime->private_data = saapcm;
662 runtime->private_free = snd_card_saa7134_runtime_free;
663 runtime->hw = snd_card_saa7134_capture;
664
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800665 if ((err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS)) < 0)
666 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800667
668 return 0;
669}
670
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800671/*
672 * ALSA capture callbacks definition
673 */
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800674
675static snd_pcm_ops_t snd_card_saa7134_capture_ops = {
676 .open = snd_card_saa7134_capture_open,
677 .close = snd_card_saa7134_capture_close,
678 .ioctl = snd_pcm_lib_ioctl,
679 .hw_params = snd_card_saa7134_hw_params,
680 .hw_free = snd_card_saa7134_hw_free,
681 .prepare = snd_card_saa7134_capture_prepare,
682 .trigger = snd_card_saa7134_capture_trigger,
683 .pointer = snd_card_saa7134_capture_pointer,
684};
685
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800686/*
687 * ALSA PCM setup
688 *
689 * Called when initializing the board. Sets up the name and hooks up
690 * the callbacks
691 *
692 */
693
694static int snd_card_saa7134_pcm(snd_card_saa7134_t *saa7134, int device)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800695{
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800696 snd_pcm_t *pcm;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800697 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800698
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800699 if ((err = snd_pcm_new(saa7134->card, "SAA7134 PCM", device, 0, 1, &pcm)) < 0)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800700 return err;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800701 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_card_saa7134_capture_ops);
702 pcm->private_data = saa7134;
703 pcm->info_flags = 0;
704 strcpy(pcm->name, "SAA7134 PCM");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800705 return 0;
706}
707
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800708#define SAA713x_VOLUME(xname, xindex, addr) \
709{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
710 .info = snd_saa7134_volume_info, \
711 .get = snd_saa7134_volume_get, .put = snd_saa7134_volume_put, \
712 .private_value = addr }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800713
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800714static int snd_saa7134_volume_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
715{
716 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
717 uinfo->count = 2;
718 uinfo->value.integer.min = 0;
719 uinfo->value.integer.max = 20;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800720 return 0;
721}
722
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800723static int snd_saa7134_volume_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800724{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800725 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800726 int addr = kcontrol->private_value;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800727
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800728 ucontrol->value.integer.value[0] = chip->mixer_volume[addr][0];
729 ucontrol->value.integer.value[1] = chip->mixer_volume[addr][1];
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800730 return 0;
731}
732
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800733static int snd_saa7134_volume_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
734{
735 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
736 unsigned long flags;
737 int change, addr = kcontrol->private_value;
738 int left, right;
739
740 left = ucontrol->value.integer.value[0];
741 if (left < 0)
742 left = 0;
743 if (left > 20)
744 left = 20;
745 right = ucontrol->value.integer.value[1];
746 if (right < 0)
747 right = 0;
748 if (right > 20)
749 right = 20;
750 spin_lock_irqsave(&chip->mixer_lock, flags);
751 change = chip->mixer_volume[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800752 chip->mixer_volume[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800753 chip->mixer_volume[addr][0] = left;
754 chip->mixer_volume[addr][1] = right;
755 spin_unlock_irqrestore(&chip->mixer_lock, flags);
756 return change;
757}
758
759#define SAA713x_CAPSRC(xname, xindex, addr) \
760{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
761 .info = snd_saa7134_capsrc_info, \
762 .get = snd_saa7134_capsrc_get, .put = snd_saa7134_capsrc_put, \
763 .private_value = addr }
764
765static int snd_saa7134_capsrc_info(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
766{
767 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800768 uinfo->count = 2;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800769 uinfo->value.integer.min = 0;
770 uinfo->value.integer.max = 1;
771 return 0;
772}
773
774static int snd_saa7134_capsrc_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
775{
776 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
777 unsigned long flags;
778 int addr = kcontrol->private_value;
779
780 spin_lock_irqsave(&chip->mixer_lock, flags);
781 ucontrol->value.integer.value[0] = chip->capture_source[addr][0];
782 ucontrol->value.integer.value[1] = chip->capture_source[addr][1];
783 spin_unlock_irqrestore(&chip->mixer_lock, flags);
784 return 0;
785}
786
787static int snd_saa7134_capsrc_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
788{
789 snd_card_saa7134_t *chip = snd_kcontrol_chip(kcontrol);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800790 int change, addr = kcontrol->private_value;
791 int left, right;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800792 u32 anabar, xbarin;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800793 int analog_io, rate;
794 struct saa7134_dev *dev;
795
796 dev = chip->saadev;
797
798 left = ucontrol->value.integer.value[0] & 1;
799 right = ucontrol->value.integer.value[1] & 1;
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800800 spin_lock_irq(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800801
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800802 change = chip->capture_source[addr][0] != left ||
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800803 chip->capture_source[addr][1] != right;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800804 chip->capture_source[addr][0] = left;
805 chip->capture_source[addr][1] = right;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800806 dev->dmasound.input=addr;
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800807 spin_unlock_irq(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800808
809
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800810 if (change) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800811 switch (dev->pci->device) {
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800812
813 case PCI_DEVICE_ID_PHILIPS_SAA7134:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800814 switch (addr) {
815 case MIXER_ADDR_TVTUNER:
816 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0xc0);
817 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
818 break;
819 case MIXER_ADDR_LINE1:
820 case MIXER_ADDR_LINE2:
821 analog_io = (MIXER_ADDR_LINE1 == addr) ? 0x00 : 0x08;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800822 rate = (32000 == dev->dmasound.rate) ? 0x01 : 0x03;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800823 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, analog_io);
824 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, 0x80);
825 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, rate);
826 break;
827 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800828
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800829 break;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800830 case PCI_DEVICE_ID_PHILIPS_SAA7133:
831 case PCI_DEVICE_ID_PHILIPS_SAA7135:
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800832 xbarin = 0x03; // adc
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800833 anabar = 0;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800834 switch (addr) {
835 case MIXER_ADDR_TVTUNER:
836 xbarin = 0; // Demodulator
837 anabar = 2; // DACs
838 break;
839 case MIXER_ADDR_LINE1:
840 anabar = 0; // aux1, aux1
841 break;
842 case MIXER_ADDR_LINE2:
843 anabar = 9; // aux2, aux2
844 break;
845 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800846
847 /* output xbar always main channel */
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800848 saa_dsp_writel(dev, SAA7133_DIGITAL_OUTPUT_SEL1, 0xbbbb10);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800849
850 if (left || right) { // We've got data, turn the input on
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800851 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, xbarin);
852 saa_writel(SAA7133_ANALOG_IO_SELECT, anabar);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800853 } else {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800854 saa_dsp_writel(dev, SAA7133_DIGITAL_INPUT_XBAR1, 0);
855 saa_writel(SAA7133_ANALOG_IO_SELECT, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800856 }
Ricardo Cerqueiraa8666232005-11-08 21:37:14 -0800857 break;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800858 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800859 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800860
861 return change;
862}
863
864static snd_kcontrol_new_t snd_saa7134_controls[] = {
865SAA713x_VOLUME("Video Volume", 0, MIXER_ADDR_TVTUNER),
866SAA713x_CAPSRC("Video Capture Switch", 0, MIXER_ADDR_TVTUNER),
867SAA713x_VOLUME("Line Volume", 1, MIXER_ADDR_LINE1),
868SAA713x_CAPSRC("Line Capture Switch", 1, MIXER_ADDR_LINE1),
869SAA713x_VOLUME("Line Volume", 2, MIXER_ADDR_LINE2),
870SAA713x_CAPSRC("Line Capture Switch", 2, MIXER_ADDR_LINE2),
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800871};
872
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800873/*
874 * ALSA mixer setup
875 *
876 * Called when initializing the board. Sets up the name and hooks up
877 * the callbacks
878 *
879 */
880
881static int snd_card_saa7134_new_mixer(snd_card_saa7134_t * chip)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800882{
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800883 snd_card_t *card = chip->card;
884 unsigned int idx;
885 int err;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800886
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800887 snd_assert(chip != NULL, return -EINVAL);
888 strcpy(card->mixername, "SAA7134 Mixer");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800889
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800890 for (idx = 0; idx < ARRAY_SIZE(snd_saa7134_controls); idx++) {
891 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_controls[idx], chip))) < 0)
892 return err;
893 }
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800894 return 0;
895}
896
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800897static void snd_saa7134_free(snd_card_t * card)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800898{
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800899 return;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800900}
901
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800902static int snd_saa7134_dev_free(snd_device_t *device)
903{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800904 snd_card_saa7134_t *chip = device->device_data;
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800905
906 if (chip->irq >= 0) {
907 synchronize_irq(chip->irq);
908 free_irq(chip->irq, (void *) chip);
909 }
910
911 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800912}
913
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800914/*
915 * ALSA initialization
916 *
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800917 * Called by the init routine, once for each saa7134 device present,
918 * it creates the basic structures and registers the ALSA devices
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800919 *
920 */
921
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800922int alsa_card_saa7134_create(struct saa7134_dev *saadev, int dev)
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800923{
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800924
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800925 snd_card_t *card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800926 snd_card_saa7134_t *chip;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800927 int err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800928 static snd_device_ops_t ops = {
929 .dev_free = snd_saa7134_dev_free,
930 };
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800931
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800932
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800933 if (dev >= SNDRV_CARDS)
934 return -ENODEV;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800935 if (!enable[dev])
936 return -ENODEV;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800937
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800938 card = snd_card_new(index[dev], id[dev], THIS_MODULE, sizeof(snd_card_saa7134_t));
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800939
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800940 if (card == NULL)
941 return -ENOMEM;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800942
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800943 strcpy(card->driver, "SAA7134");
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800944
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800945 /* Card "creation" */
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800946
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800947 card->private_free = snd_saa7134_free;
948 chip = (snd_card_saa7134_t *) card->private_data;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800949
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800950 spin_lock_init(&chip->lock);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800951 spin_lock_init(&chip->mixer_lock);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800952
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800953 chip->saadev = saadev;
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800954
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800955 chip->card = card;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800956
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800957 chip->pci = saadev->pci;
958 chip->irq = saadev->pci->irq;
959 chip->iobase = pci_resource_start(saadev->pci, 0);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800960
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800961 err = request_irq(saadev->pci->irq, saa7134_alsa_irq,
Ricardo Cerqueirac817e762005-11-13 16:07:47 -0800962 SA_SHIRQ | SA_INTERRUPT, saadev->name, (void *)chip);
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800963
964 if (err < 0) {
965 printk(KERN_ERR "%s: can't get IRQ %d for ALSA\n",
966 saadev->name, saadev->pci->irq);
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800967 goto __nodev;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800968 }
969
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800970 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -0800971 goto __nodev;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800972 }
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800973
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800974 if ((err = snd_card_saa7134_new_mixer(chip)) < 0)
975 goto __nodev;
976
977 if ((err = snd_card_saa7134_pcm(chip, 0)) < 0)
978 goto __nodev;
Ricardo Cerqueirab2c15ea2005-11-08 21:37:14 -0800979
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800980 snd_card_set_dev(card, &chip->pci->dev);
981
982 /* End of "creation" */
983
984 strcpy(card->shortname, "SAA7134");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800985 sprintf(card->longname, "%s at 0x%lx irq %d",
986 chip->saadev->name, chip->iobase, chip->irq);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800987
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800988 if ((err = snd_card_register(card)) == 0) {
989 snd_saa7134_cards[dev] = card;
990 return 0;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800991 }
992
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800993__nodev:
994 snd_card_free(card);
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -0800995 return err;
996}
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -0800997
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -0800998/*
999 * Module initializer
1000 *
1001 * Loops through present saa7134 cards, and assigns an ALSA device
1002 * to each one
1003 *
1004 */
1005
1006static int saa7134_alsa_init(void)
1007{
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001008 struct saa7134_dev *saadev = NULL;
1009 struct list_head *list;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001010
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001011 position = 0;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001012
Ricardo Cerqueirac817e762005-11-13 16:07:47 -08001013 printk(KERN_INFO "saa7134 ALSA driver for DMA sound loaded\n");
1014
1015 list_for_each(list,&saa7134_devlist) {
1016 saadev = list_entry(list, struct saa7134_dev, devlist);
1017 alsa_card_saa7134_create(saadev,position);
1018 position++;
1019 }
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001020
1021 if (saadev == NULL)
1022 printk(KERN_INFO "saa7134 ALSA: no saa7134 cards found\n");
1023
1024 return 0;
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001025}
1026
1027/*
1028 * Module destructor
1029 */
1030
1031void saa7134_alsa_exit(void)
Ricardo Cerqueirabd15eba2005-11-08 21:37:11 -08001032{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001033 int idx;
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001034
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001035 for (idx = 0; idx < SNDRV_CARDS; idx++) {
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -08001036 snd_card_free(snd_saa7134_cards[idx]);
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001037 }
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001038
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001039 printk(KERN_INFO "saa7134 ALSA driver for DMA sound unloaded\n");
Ricardo Cerqueirab54134b2005-11-08 21:38:54 -08001040
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001041 return;
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001042}
Ricardo Cerqueira79dd0c692005-11-08 21:38:53 -08001043
1044module_init(saa7134_alsa_init);
1045module_exit(saa7134_alsa_exit);
1046MODULE_LICENSE("GPL");
1047MODULE_AUTHOR("Ricardo Cerqueira");