blob: d09e01f7fc3b937f23be62ef9a04493d0003268e [file] [log] [blame]
Nickolay V. Shmyrev5f7591c2005-11-08 21:37:04 -08001/*
2 *
3 * Support for audio capture
4 * PCI function #1 of the saa7134
5 *
6 * (c) 2005 Mauro Carvalho Chehab <mchehab@brturbo.com.br>
7 * (c) 2004 Gerd Knorr <kraxel@bytesex.org>
8 * (c) 2003 Clemens Ladisch <clemens@ladisch.de>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/device.h>
28#include <linux/interrupt.h>
29#include <asm/delay.h>
30#include <sound/driver.h>
31#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/control.h>
35#include <sound/initval.h>
36
37#include "saa7134.h"
38#include "saa7134-reg.h"
39
40#define dprintk(level,fmt, arg...) if (debug >= level) \
41 printk(KERN_DEBUG "%s/1: " fmt, chip->core->name , ## arg)
42
43
44/****************************************************************************
45 Data type declarations - Can be moded to a header file later
46 ****************************************************************************/
47
48#define ANALOG_CLOCK 1792000
49#define CLOCK_DIV_MIN 4
50#define CLOCK_DIV_MAX 15
51#define MAX_PCM_DEVICES 4
52#define MAX_PCM_SUBSTREAMS 16
53
54enum { DEVICE_DIGITAL, DEVICE_ANALOG };
55
56/* These can be replaced after done */
57#define MIXER_ADDR_LAST MAX_saa7134_INPUT
58
59struct saa7134_audio_dev {
60 struct saa7134_core *core;
61 struct saa7134_buffer *buf;
62 struct saa7134_dmaqueue q;
63
64 /* pci i/o */
65 struct pci_dev *pci;
66 unsigned char pci_rev,pci_lat;
67
68 /* audio controls */
69 int irq;
70 int dig_rate; /* Digital sampling rate */
71
72 snd_card_t *card;
73
74 spinlock_t reg_lock;
75
76 unsigned int dma_size;
77 unsigned int period_size;
78
79 int mixer_volume[MIXER_ADDR_LAST+1][2];
80 int capture_source[MIXER_ADDR_LAST+1][2];
81
82 long opened;
83 snd_pcm_substream_t *substream;
84};
85typedef struct saa7134_audio_dev snd_saa7134_card_t;
86
87/****************************************************************************
88 Module global static vars
89 ****************************************************************************/
90
91static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
92static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
93static int enable[SNDRV_CARDS] = {1, [1 ... (SNDRV_CARDS - 1)] = 1};
94
95module_param_array(enable, bool, NULL, 0444);
96MODULE_PARM_DESC(enable, "Enable saa7134x soundcard. default enabled.");
97
98/****************************************************************************
99 Module macros
100 ****************************************************************************/
101
102MODULE_DESCRIPTION("ALSA driver module for saa7134 based TV cards");
103MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@brturbo.com.br>");
104MODULE_LICENSE("GPL");
105MODULE_SUPPORTED_DEVICE("{{Philips, saa7131E},"
106 "{{Philips, saa7134},"
107 "{{Philips, saa7133}");
108static unsigned int debug = 0;
109module_param(debug,int,0644);
110MODULE_PARM_DESC(debug,"enable debug messages");
111
112/****************************************************************************
113 Module specific funtions
114 ****************************************************************************/
115
116/*
117 * BOARD Specific: Sets audio DMA
118 */
119
120int saa7134_start_audio_dma(snd_saa7134_card_t *chip)
121{
122 struct saa7134_core *core = chip->core;
123
124 return 0;
125}
126
127/*
128 * BOARD Specific: Resets audio DMA
129 */
130int saa7134_stop_audio_dma(snd_saa7134_card_t *chip)
131{
132 struct saa7134_core *core=chip->core;
133 return 0;
134}
135
136#define MAX_IRQ_LOOP 10
137
138static void saa713401_timeout(unsigned long data)
139{
140 snd_saa7134_card_t *chip = (snd_saa7134_card_t *)data;
141}
142
143/* FIXME: Wrong values*/
144static char *saa7134_aud_irqs[32] = {
145 "y_risci1", "u_risci1", "v_risci1", "vbi_risc1",
146 "y_risci2", "u_risci2", "v_risci2", "vbi_risc2",
147 "y_oflow", "u_oflow", "v_oflow", "vbi_oflow",
148 "y_sync", "u_sync", "v_sync", "vbi_sync",
149 "opc_err", "par_err", "rip_err", "pci_abort",
150};
151
152
153static void saa713401_aud_irq(snd_saa7134_card_t *chip)
154{
155 struct saa7134_core *core = chip->core;
156}
157
158static irqreturn_t saa7134_irq(int irq, void *dev_id, struct pt_regs *regs)
159{
160 snd_saa7134_card_t *chip = dev_id;
161 struct saa7134_core *core = chip->core;
162}
163
164/****************************************************************************
165 ALSA PCM Interface
166 ****************************************************************************/
167
168/*
169 * Digital hardware definition
170 */
171static snd_pcm_hardware_t snd_saa7134_digital_hw = {
172 .info = SNDRV_PCM_INFO_MMAP |
173 SNDRV_PCM_INFO_INTERLEAVED |
174 SNDRV_PCM_INFO_BLOCK_TRANSFER |
175 SNDRV_PCM_INFO_MMAP_VALID,
176 .formats = SNDRV_PCM_FMTBIT_S16_LE,
177 .rates = 0, /* set at runtime */
178 .channels_min = 2,
179 .channels_max = 2,
180 .buffer_bytes_max = 255 * 4092,
181 .period_bytes_min = 32,
182 .period_bytes_max = 4092,
183 .periods_min = 2,
184 .periods_max = 255,
185};
186
187/*
188 * Sets board to provide digital audio
189 */
190static int snd_saa7134_set_digital_hw(snd_saa7134_card_t *chip, snd_pcm_runtime_t *runtime)
191{
192 return 0;
193}
194
195/*
196 * audio open callback
197 */
198static int snd_saa7134_pcm_open(snd_pcm_substream_t *substream)
199{
200 snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
201 snd_pcm_runtime_t *runtime = substream->runtime;
202 int err;
203
204 if (test_and_set_bit(0, &chip->opened))
205 return -EBUSY;
206
207 err = snd_saa7134_set_digital_hw(chip, runtime);
208
209 if (err < 0)
210 goto _error;
211
212 err = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
213 if (err < 0)
214 goto _error;
215
216 chip->substream = substream;
217 return 0;
218
219_error:
220 clear_bit(0, &chip->opened);
221 smp_mb__after_clear_bit();
222 return err;
223}
224
225/*
226 * audio close callback
227 */
228static int snd_saa7134_close(snd_pcm_substream_t *substream)
229{
230 snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
231
232 chip->substream = NULL;
233 clear_bit(0, &chip->opened);
234 smp_mb__after_clear_bit();
235 return 0;
236}
237
238/*
239 * hw_params callback
240 */
241static int snd_saa7134_hw_params(snd_pcm_substream_t * substream,
242 snd_pcm_hw_params_t * hw_params)
243{
244 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
245}
246
247/*
248 * hw free callback
249 */
250static int snd_saa7134_hw_free(snd_pcm_substream_t * substream)
251{
252 return snd_pcm_lib_free_pages(substream);
253}
254
255/*
256 * prepare callback
257 */
258static int snd_saa7134_prepare(snd_pcm_substream_t *substream)
259{
260 snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
261 return 0;
262}
263
264
265/*
266 * trigger callback
267 */
268static int snd_saa7134_card_trigger(snd_pcm_substream_t *substream, int cmd)
269{
270 snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
271
272 switch (cmd) {
273 case SNDRV_PCM_TRIGGER_START:
274 return snd_saa7134_start(chip);
275 case SNDRV_PCM_TRIGGER_STOP:
276 return snd_saa7134_stop(chip);
277 default:
278 return -EINVAL;
279 }
280}
281
282/*
283 * pointer callback
284 */
285static snd_pcm_uframes_t snd_saa7134_pointer(snd_pcm_substream_t *substream)
286{
287// snd_saa7134_card_t *chip = snd_pcm_substream_chip(substream);
288// snd_pcm_runtime_t *runtime = substream->runtime;
289
290// return (snd_pcm_uframes_t)bytes_to_frames(runtime, chip->current_line * chip->line_bytes);
291}
292
293/*
294 * operators
295 */
296static snd_pcm_ops_t snd_saa7134_pcm_ops = {
297 .open = snd_saa7134_pcm_open,
298 .close = snd_saa7134_close,
299 .ioctl = snd_pcm_lib_ioctl,
300 .hw_params = snd_saa7134_hw_params,
301 .hw_free = snd_saa7134_hw_free,
302 .prepare = snd_saa7134_prepare,
303 .trigger = snd_saa7134_card_trigger,
304 .pointer = snd_saa7134_pointer,
305 .page = snd_pcm_sgbuf_ops_page,
306};
307
308/*
309 * create a PCM device
310 */
311static int __devinit snd_saa7134_pcm(snd_saa7134_card_t *chip, int device, char *name)
312{
313 int err;
314 snd_pcm_t *pcm;
315
316 err = snd_pcm_new(chip->card, name, device, 0, 1, &pcm);
317 if (err < 0)
318 return err;
319 pcm->private_data = chip;
320 strcpy(pcm->name, name);
321 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_saa7134_pcm_ops);
322 return snd_pcm_lib_preallocate_pages_for_all(pcm,
323 SNDRV_DMA_TYPE_DEV_SG,
324 snd_dma_pci_data(chip->pci),
325 128 * 1024,
326 (255 * 4092 + 1023) & ~1023);
327}
328
329/****************************************************************************
330 CONTROL INTERFACE
331 ****************************************************************************/
332static int snd_saa7134_capture_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *info)
333{
334 info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
335 info->count = 1;
336 info->value.integer.min = 0;
337 info->value.integer.max = 0x3f;
338
339 return 0;
340}
341
342/* OK - TODO: test it */
343static int snd_saa7134_capture_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
344{
345 snd_saa7134_card_t *chip = snd_kcontrol_chip(kcontrol);
346 struct saa7134_core *core=chip->core;
347
348 return 0;
349}
350
351/* OK - TODO: test it */
352static int snd_saa7134_capture_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *value)
353{
354 snd_saa7134_card_t *chip = snd_kcontrol_chip(kcontrol);
355 struct saa7134_core *core=chip->core;
356
357 return 0;
358}
359
360static snd_kcontrol_new_t snd_saa7134_capture_volume = {
361 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
362 .name = "Capture Volume",
363 .info = snd_saa7134_capture_volume_info,
364 .get = snd_saa7134_capture_volume_get,
365 .put = snd_saa7134_capture_volume_put,
366};
367
368/*
369 ***************************************
370 */
371
372/****************************************************************************
373 Basic Flow for Sound Devices
374 ****************************************************************************/
375
376/*
377 * PCI ID Table - 14f1:8801 and 14f1:8811 means function 1: Audio
378 * Only boards with eeprom and byte 1 at eeprom=1 have it
379 */
380
381struct pci_device_id saa7134_audio_pci_tbl[] = {
382 {0x14f1,0x8801,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
383 {0x14f1,0x8811,PCI_ANY_ID,PCI_ANY_ID,0,0,0},
384 {0, }
385};
386MODULE_DEVICE_TABLE(pci, saa7134_audio_pci_tbl);
387
388/*
389 * Chip-specific destructor
390 */
391
392static int snd_saa7134_free(snd_saa7134_card_t *chip)
393{
394 if (chip->irq >= 0)
395 free_irq(chip->irq, chip);
396
397 /* free memory */
398 saa7134_core_put(chip->core,chip->pci);
399
400 pci_release_regions(chip->pci);
401 pci_disable_device(chip->pci);
402
403 kfree(chip);
404 return 0;
405}
406
407/*
408 * Component Destructor
409 */
410static int snd_saa7134_dev_free(snd_device_t *device)
411{
412 snd_saa7134_card_t *chip = device->device_data;
413 return snd_saa7134_free(chip);
414}
415
416
417/*
418 * Alsa Constructor - Component probe
419 */
420
421static int devno=0;
422static int __devinit snd_saa7134_create(snd_card_t *card, struct pci_dev *pci,
423 snd_saa7134_card_t **rchip)
424{
425 snd_saa7134_card_t *chip;
426 struct saa7134_core *core;
427 return 0;
428}
429
430static int __devinit saa7134_audio_initdev(struct pci_dev *pci,
431 const struct pci_device_id *pci_id)
432{
433 snd_card_t *card;
434 snd_saa7134_card_t *chip;
435 int err;
436
437 if (devno >= SNDRV_CARDS)
438 return (-ENODEV);
439
440 if (!enable[devno]) {
441 ++devno;
442 return (-ENOENT);
443 }
444
445 card = snd_card_new(index[devno], id[devno], THIS_MODULE, 0);
446 if (!card)
447 return (-ENOMEM);
448
449 err = snd_saa7134_create(card, pci, &chip);
450 if (err < 0)
451 return (err);
452
453/*
454 err = snd_saa7134_pcm(chip, DEVICE_DIGITAL, "saa7134 Digital");
455 if (err < 0)
456 goto fail_free;
457*/
458 err = snd_ctl_add(card, snd_ctl_new1(&snd_saa7134_capture_volume, chip));
459 if (err < 0) {
460 snd_card_free(card);
461 return (err);
462 }
463
464 strcpy (card->driver, "saa7134_ALSA");
465 sprintf(card->shortname, "Saa7134 %x", pci->device);
466 sprintf(card->longname, "%s at %#lx",
467 card->shortname, pci_resource_start(pci, 0));
468 strcpy (card->mixername, "saa7134");
469
470 dprintk (0, "%s/%i: Alsa support for saa7134x boards\n",
471 card->driver,devno);
472
473 err = snd_card_register(card);
474 if (err < 0) {
475 snd_card_free(card);
476 return (err);
477 }
478
479 pci_set_drvdata(pci,card);
480
481 devno++;
482 return 0;
483}
484/*
485 * ALSA destructor
486 */
487static void __devexit saa7134_audio_finidev(struct pci_dev *pci)
488{
489 snd_card_free(pci_get_drvdata(pci));
490 pci_set_drvdata(pci, NULL);
491
492 devno--;
493}
494
495/*
496 * PCI driver definition
497 */
498
499static struct pci_driver saa7134_audio_pci_driver = {
500 .name = "saa7134_audio",
501 .id_table = saa7134_audio_pci_tbl,
502 .probe = saa7134_audio_initdev,
503 .remove = saa7134_audio_finidev,
504 SND_PCI_PM_CALLBACKS
505};
506
507/****************************************************************************
508 LINUX MODULE INIT
509 ****************************************************************************/
510
511/*
512 * module init
513 */
514static int saa7134_audio_init(void)
515{
516 printk(KERN_INFO "saa7134x alsa driver version %d.%d.%d loaded\n",
517 (saa7134_VERSION_CODE >> 16) & 0xff,
518 (saa7134_VERSION_CODE >> 8) & 0xff,
519 saa7134_VERSION_CODE & 0xff);
520#ifdef SNAPSHOT
521 printk(KERN_INFO "saa7134x: snapshot date %04d-%02d-%02d\n",
522 SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
523#endif
524 return pci_module_init(&saa7134_audio_pci_driver);
525}
526
527/*
528 * module remove
529 */
530static void saa7134_audio_fini(void)
531{
532 pci_unregister_driver(&saa7134_audio_pci_driver);
533}
534
535module_init(saa7134_audio_init);
536module_exit(saa7134_audio_fini);
537
538/* ----------------------------------------------------------- */
539/*
540 * Local variables:
541 * c-basic-offset: 8
542 * End:
543 */