2 * linux/drivers/sound/vidc.c
4 * Copyright (C) 1997-2000 by Russell King <rmk@arm.linux.org.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * VIDC20 audio driver.
12 * The VIDC20 sound hardware consists of the VIDC20 itself, a DAC and a DMA
13 * engine. The DMA transfers fixed-format (16-bit little-endian linear)
14 * samples to the VIDC20, which then transfers this data serially to the
15 * DACs. The samplerate is controlled by the VIDC.
17 * We currently support a mixer device, but it is currently non-functional.
20 #include <linux/config.h>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
25 #include <asm/hardware.h>
28 #include <asm/hardware/iomd.h>
30 #include <asm/system.h>
32 #include "sound_config.h"
36 #define _SIOC_TYPE(x) _IOC_TYPE(x)
39 #define _SIOC_NR(x) _IOC_NR(x)
42 #define VIDC_SOUND_CLOCK (250000)
45 * When using SERIAL SOUND mode (external DAC), the number of physical
46 * channels is fixed at 2.
50 static int vidc_audio_rate;
51 static char vidc_audio_format;
52 static char vidc_audio_channels;
54 static unsigned char vidc_level_l[SOUND_MIXER_NRDEVICES] = {
67 static unsigned char vidc_level_r[SOUND_MIXER_NRDEVICES] = {
80 static unsigned int vidc_audio_volume_l; /* left PCM vol, 0 - 65536 */
81 static unsigned int vidc_audio_volume_r; /* right PCM vol, 0 - 65536 */
83 static void (*old_mksound)(unsigned int hz, unsigned int ticks);
84 extern void (*kd_mksound)(unsigned int hz, unsigned int ticks);
85 extern void vidc_update_filler(int bits, int channels);
86 extern int softoss_dev;
89 vidc_mksound(unsigned int hz, unsigned int ticks)
91 // printk("BEEP - %d %d!\n", hz, ticks);
95 vidc_mixer_set(int mdev, unsigned int level)
97 unsigned int lev_l = level & 0x007f;
98 unsigned int lev_r = (level & 0x7f00) >> 8;
99 unsigned int mlev_l, mlev_r;
106 #define SCALE(lev,master) ((lev) * (master) * 65536 / 10000)
108 mlev_l = vidc_level_l[SOUND_MIXER_VOLUME];
109 mlev_r = vidc_level_r[SOUND_MIXER_VOLUME];
112 case SOUND_MIXER_VOLUME:
113 case SOUND_MIXER_PCM:
114 vidc_level_l[mdev] = lev_l;
115 vidc_level_r[mdev] = lev_r;
117 vidc_audio_volume_l = SCALE(lev_l, mlev_l);
118 vidc_audio_volume_r = SCALE(lev_r, mlev_r);
119 /*printk("VIDC: PCM vol %05X %05X\n", vidc_audio_volume_l, vidc_audio_volume_r);*/
125 static int vidc_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
130 if (_SIOC_TYPE(cmd) != 'M')
133 mdev = _SIOC_NR(cmd);
135 if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
136 if (get_user(val, (unsigned int *)arg))
139 if (mdev < SOUND_MIXER_NRDEVICES)
140 vidc_mixer_set(mdev, val);
149 case SOUND_MIXER_RECSRC:
153 case SOUND_MIXER_DEVMASK:
154 val = SOUND_MASK_VOLUME | SOUND_MASK_PCM | SOUND_MASK_SYNTH;
157 case SOUND_MIXER_STEREODEVS:
158 val = SOUND_MASK_VOLUME | SOUND_MASK_PCM | SOUND_MASK_SYNTH;
161 case SOUND_MIXER_RECMASK:
165 case SOUND_MIXER_CAPS:
170 if (mdev < SOUND_MIXER_NRDEVICES)
171 val = vidc_level_l[mdev] | vidc_level_r[mdev] << 8;
176 return put_user(val, (unsigned int *)arg) ? -EFAULT : 0;
179 static unsigned int vidc_audio_set_format(int dev, unsigned int fmt)
187 vidc_audio_format = fmt;
188 vidc_update_filler(vidc_audio_format, vidc_audio_channels);
192 return vidc_audio_format;
195 static int vidc_audio_set_speed(int dev, int rate)
198 unsigned int hwctrl, hwrate;
199 unsigned int newsize, new2size;
202 * If we have selected 44.1kHz, use the DAC clock.
204 if (0 && rate == 44100) {
210 hwrate = (((VIDC_SOUND_CLOCK * 2) / rate) + 1) >> 1;
216 rate = VIDC_SOUND_CLOCK / hwrate;
219 vidc_writel(0xb0000000 | (hwrate - 2));
220 vidc_writel(0xb1000000 | hwctrl);
222 newsize = (10000 / hwrate) & ~3;
227 for (new2size = 128; new2size < newsize; new2size <<= 1);
228 if (new2size - newsize > newsize - (new2size >> 1))
230 if (new2size > 4096) {
231 printk(KERN_ERR "VIDC: error: dma buffer (%d) %d > 4K\n",
235 dma_bufsize = new2size;
236 vidc_audio_rate = rate;
238 return vidc_audio_rate;
241 static short vidc_audio_set_channels(int dev, short channels)
248 vidc_audio_channels = channels;
249 vidc_update_filler(vidc_audio_format, vidc_audio_channels);
253 return vidc_audio_channels;
259 static int vidc_audio_open(int dev, int mode)
261 /* This audio device does not have recording capability */
262 if (mode == OPEN_READ)
275 static void vidc_audio_close(int dev)
281 * Output a block via DMA to sound device.
283 * We just set the DMA start and count; the DMA interrupt routine
284 * will take care of formatting the samples (via the appropriate
285 * vidc_filler routine), and flag via vidc_audio_dma_interrupt when
286 * more data is required.
289 vidc_audio_output_block(int dev, unsigned long buf, int total_count, int one)
291 struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
294 local_irq_save(flags);
295 dma_start = buf - (unsigned long)dmap->raw_buf_phys + (unsigned long)dmap->raw_buf;
296 dma_count = total_count;
297 local_irq_restore(flags);
301 vidc_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
305 static int vidc_audio_prepare_for_input(int dev, int bsize, int bcount)
310 static void vidc_audio_dma_interrupt(void)
312 DMAbuf_outputintr(vidc_adev, 1);
316 * Prepare for outputting samples.
318 * Each buffer that will be passed will be `bsize' bytes long,
319 * with a total of `bcount' buffers.
321 static int vidc_audio_prepare_for_output(int dev, int bsize, int bcount)
323 struct audio_operations *adev = audio_devs[dev];
325 dma_interrupt = NULL;
326 adev->dmap_out->flags |= DMA_NODMA;
332 * Stop our current operation.
334 static void vidc_audio_reset(int dev)
336 dma_interrupt = NULL;
339 static int vidc_audio_local_qlen(int dev)
341 return /*dma_count !=*/ 0;
344 static void vidc_audio_trigger(int dev, int enable_bits)
346 struct audio_operations *adev = audio_devs[dev];
348 if (enable_bits & PCM_ENABLE_OUTPUT) {
349 if (!(adev->flags & DMA_ACTIVE)) {
352 local_irq_save(flags);
354 /* prevent recusion */
355 adev->flags |= DMA_ACTIVE;
357 dma_interrupt = vidc_audio_dma_interrupt;
358 vidc_sound_dma_irq(0, NULL, NULL);
359 iomd_writeb(DMA_CR_E | 0x10, IOMD_SD0CR);
361 local_irq_restore(flags);
366 static struct audio_driver vidc_audio_driver =
369 open: vidc_audio_open,
370 close: vidc_audio_close,
371 output_block: vidc_audio_output_block,
372 start_input: vidc_audio_start_input,
373 prepare_for_input: vidc_audio_prepare_for_input,
374 prepare_for_output: vidc_audio_prepare_for_output,
375 halt_io: vidc_audio_reset,
376 local_qlen: vidc_audio_local_qlen,
377 trigger: vidc_audio_trigger,
378 set_speed: vidc_audio_set_speed,
379 set_bits: vidc_audio_set_format,
380 set_channels: vidc_audio_set_channels
383 static struct mixer_operations vidc_mixer_operations = {
387 ioctl: vidc_mixer_ioctl
390 void vidc_update_filler(int format, int channels)
392 #define TYPE(fmt,ch) (((fmt)<<2) | ((ch)&3))
394 switch (TYPE(format, channels)) {
396 case TYPE(AFMT_U8, 1):
397 vidc_filler = vidc_fill_1x8_u;
400 case TYPE(AFMT_U8, 2):
401 vidc_filler = vidc_fill_2x8_u;
404 case TYPE(AFMT_S8, 1):
405 vidc_filler = vidc_fill_1x8_s;
408 case TYPE(AFMT_S8, 2):
409 vidc_filler = vidc_fill_2x8_s;
412 case TYPE(AFMT_S16_LE, 1):
413 vidc_filler = vidc_fill_1x16_s;
416 case TYPE(AFMT_S16_LE, 2):
417 vidc_filler = vidc_fill_2x16_s;
422 static void __init attach_vidc(struct address_info *hw_config)
427 sprintf(name, "VIDC %d-bit sound", hw_config->card_subtype);
428 conf_printf(name, hw_config);
429 memset(dma_buf, 0, sizeof(dma_buf));
431 adev = sound_install_audiodrv(AUDIO_DRIVER_VERSION, name,
432 &vidc_audio_driver, sizeof(vidc_audio_driver),
433 DMA_AUTOMODE, AFMT_U8 | AFMT_S8 | AFMT_S16_LE,
434 NULL, hw_config->dma, hw_config->dma2);
440 * 1024 bytes => 64 buffers
442 audio_devs[adev]->min_fragment = 10;
443 audio_devs[adev]->mixer_dev = num_mixers;
445 audio_devs[adev]->mixer_dev =
446 sound_install_mixer(MIXER_DRIVER_VERSION,
447 name, &vidc_mixer_operations,
448 sizeof(vidc_mixer_operations), NULL);
450 if (audio_devs[adev]->mixer_dev < 0)
453 for (i = 0; i < 2; i++) {
454 dma_buf[i] = get_free_page(GFP_KERNEL);
456 printk(KERN_ERR "%s: can't allocate required buffers\n",
460 dma_pbuf[i] = virt_to_phys((void *)dma_buf[i]);
463 if (sound_alloc_dma(hw_config->dma, hw_config->name)) {
464 printk(KERN_ERR "%s: DMA %d is in use\n", name, hw_config->dma);
468 if (request_irq(hw_config->irq, vidc_sound_dma_irq, 0,
469 hw_config->name, &dma_start)) {
470 printk(KERN_ERR "%s: IRQ %d is in use\n", name, hw_config->irq);
473 old_mksound = kd_mksound;
474 kd_mksound = vidc_mksound;
476 vidc_mixer_set(SOUND_MIXER_VOLUME, (85 | 85 << 8));
478 #if defined(CONFIG_SOUND_SOFTOSS) || defined(CONFIG_SOUND_SOFTOSS_MODULE)
484 sound_free_dma(hw_config->dma);
487 for (i = 0; i < 2; i++)
488 free_page(dma_buf[i]);
489 sound_unload_mixerdev(audio_devs[adev]->mixer_dev);
491 sound_unload_audiodev(adev);
496 static int __init probe_vidc(struct address_info *hw_config)
498 hw_config->irq = IRQ_DMAS0;
499 hw_config->dma = DMA_VIRTUAL_SOUND;
500 hw_config->dma2 = -1;
501 hw_config->card_subtype = 16;
502 hw_config->name = "VIDC20";
506 static void __exit unload_vidc(struct address_info *hw_config)
508 int i, adev = vidc_adev;
513 kd_mksound = old_mksound;
515 free_irq(hw_config->irq, &dma_start);
516 sound_free_dma(hw_config->dma);
519 sound_unload_mixerdev(audio_devs[adev]->mixer_dev);
520 sound_unload_audiodev(adev);
521 for (i = 0; i < 2; i++)
522 free_page(dma_buf[i]);
526 static struct address_info cfg;
528 static int __init init_vidc(void)
530 if (probe_vidc(&cfg) == 0)
538 static void __exit cleanup_vidc(void)
543 module_init(init_vidc);
544 module_exit(cleanup_vidc);
546 MODULE_AUTHOR("Russell King");
547 MODULE_DESCRIPTION("VIDC20 audio driver");
548 MODULE_LICENSE("GPL");