OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [sound/] [vidc.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *  linux/drivers/sound/vidc.c
3
 *
4
 *  Copyright (C) 1997-2000 by Russell King <rmk@arm.linux.org.uk>
5
 *
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.
9
 *
10
 *  VIDC20 audio driver.
11
 *
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.
16
 *
17
 * We currently support a mixer device, but it is currently non-functional.
18
 */
19
 
20
#include <linux/config.h>
21
#include <linux/init.h>
22
#include <linux/module.h>
23
#include <linux/kernel.h>
24
 
25
#include <asm/hardware.h>
26
#include <asm/dma.h>
27
#include <asm/io.h>
28
#include <asm/hardware/iomd.h>
29
#include <asm/irq.h>
30
#include <asm/system.h>
31
 
32
#include "sound_config.h"
33
#include "vidc.h"
34
 
35
#ifndef _SIOC_TYPE
36
#define _SIOC_TYPE(x)   _IOC_TYPE(x)
37
#endif
38
#ifndef _SIOC_NR
39
#define _SIOC_NR(x)     _IOC_NR(x)
40
#endif
41
 
42
#define VIDC_SOUND_CLOCK        (250000)
43
 
44
/*
45
 * When using SERIAL SOUND mode (external DAC), the number of physical
46
 * channels is fixed at 2.
47
 */
48
static int              vidc_busy;
49
static int              vidc_adev;
50
static int              vidc_audio_rate;
51
static char             vidc_audio_format;
52
static char             vidc_audio_channels;
53
 
54
static unsigned char    vidc_level_l[SOUND_MIXER_NRDEVICES] = {
55
        85,             /* master       */
56
        50,             /* bass         */
57
        50,             /* treble       */
58
        0,               /* synth        */
59
        75,             /* pcm          */
60
        0,               /* speaker      */
61
        100,            /* ext line     */
62
        0,               /* mic          */
63
        100,            /* CD           */
64
        0,
65
};
66
 
67
static unsigned char    vidc_level_r[SOUND_MIXER_NRDEVICES] = {
68
        85,             /* master       */
69
        50,             /* bass         */
70
        50,             /* treble       */
71
        0,               /* synth        */
72
        75,             /* pcm          */
73
        0,               /* speaker      */
74
        100,            /* ext line     */
75
        0,               /* mic          */
76
        100,            /* CD           */
77
        0,
78
};
79
 
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 */
82
 
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;
87
 
88
static void
89
vidc_mksound(unsigned int hz, unsigned int ticks)
90
{
91
//      printk("BEEP - %d %d!\n", hz, ticks);
92
}
93
 
94
static void
95
vidc_mixer_set(int mdev, unsigned int level)
96
{
97
        unsigned int lev_l = level & 0x007f;
98
        unsigned int lev_r = (level & 0x7f00) >> 8;
99
        unsigned int mlev_l, mlev_r;
100
 
101
        if (lev_l > 100)
102
                lev_l = 100;
103
        if (lev_r > 100)
104
                lev_r = 100;
105
 
106
#define SCALE(lev,master)       ((lev) * (master) * 65536 / 10000)
107
 
108
        mlev_l = vidc_level_l[SOUND_MIXER_VOLUME];
109
        mlev_r = vidc_level_r[SOUND_MIXER_VOLUME];
110
 
111
        switch (mdev) {
112
        case SOUND_MIXER_VOLUME:
113
        case SOUND_MIXER_PCM:
114
                vidc_level_l[mdev] = lev_l;
115
                vidc_level_r[mdev] = lev_r;
116
 
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);*/
120
                break;
121
        }
122
#undef SCALE
123
}
124
 
125
static int vidc_mixer_ioctl(int dev, unsigned int cmd, caddr_t arg)
126
{
127
        unsigned int val;
128
        unsigned int mdev;
129
 
130
        if (_SIOC_TYPE(cmd) != 'M')
131
                return -EINVAL;
132
 
133
        mdev = _SIOC_NR(cmd);
134
 
135
        if (_SIOC_DIR(cmd) & _SIOC_WRITE) {
136
                if (get_user(val, (unsigned int *)arg))
137
                        return -EFAULT;
138
 
139
                if (mdev < SOUND_MIXER_NRDEVICES)
140
                        vidc_mixer_set(mdev, val);
141
                else
142
                        return -EINVAL;
143
        }
144
 
145
        /*
146
         * Return parameters
147
         */
148
        switch (mdev) {
149
        case SOUND_MIXER_RECSRC:
150
                val = 0;
151
                break;
152
 
153
        case SOUND_MIXER_DEVMASK:
154
                val = SOUND_MASK_VOLUME | SOUND_MASK_PCM | SOUND_MASK_SYNTH;
155
                break;
156
 
157
        case SOUND_MIXER_STEREODEVS:
158
                val = SOUND_MASK_VOLUME | SOUND_MASK_PCM | SOUND_MASK_SYNTH;
159
                break;
160
 
161
        case SOUND_MIXER_RECMASK:
162
                val = 0;
163
                break;
164
 
165
        case SOUND_MIXER_CAPS:
166
                val = 0;
167
                break;
168
 
169
        default:
170
                if (mdev < SOUND_MIXER_NRDEVICES)
171
                        val = vidc_level_l[mdev] | vidc_level_r[mdev] << 8;
172
                else
173
                        return -EINVAL;
174
        }
175
 
176
        return put_user(val, (unsigned int *)arg) ? -EFAULT : 0;
177
}
178
 
179
static unsigned int vidc_audio_set_format(int dev, unsigned int fmt)
180
{
181
        switch (fmt) {
182
        default:
183
                fmt = AFMT_S16_LE;
184
        case AFMT_U8:
185
        case AFMT_S8:
186
        case AFMT_S16_LE:
187
                vidc_audio_format = fmt;
188
                vidc_update_filler(vidc_audio_format, vidc_audio_channels);
189
        case AFMT_QUERY:
190
                break;
191
        }
192
        return vidc_audio_format;
193
}
194
 
195
static int vidc_audio_set_speed(int dev, int rate)
196
{
197
        if (rate) {
198
                unsigned int hwctrl, hwrate;
199
                unsigned int newsize, new2size;
200
 
201
                /*
202
                 * If we have selected 44.1kHz, use the DAC clock.
203
                 */
204
                if (0 && rate == 44100) {
205
                        hwctrl = 0x00000002;
206
                        hwrate = 3;
207
                } else {
208
                        hwctrl = 0x00000003;
209
 
210
                        hwrate = (((VIDC_SOUND_CLOCK * 2) / rate) + 1) >> 1;
211
                        if (hwrate < 3)
212
                                hwrate = 3;
213
                        if (hwrate > 255)
214
                                hwrate = 255;
215
 
216
                        rate = VIDC_SOUND_CLOCK / hwrate;
217
                }
218
 
219
                vidc_writel(0xb0000000 | (hwrate - 2));
220
                vidc_writel(0xb1000000 | hwctrl);
221
 
222
                newsize = (10000 / hwrate) & ~3;
223
                if (newsize < 208)
224
                        newsize = 208;
225
                if (newsize > 4096)
226
                        newsize = 4096;
227
                for (new2size = 128; new2size < newsize; new2size <<= 1);
228
                        if (new2size - newsize > newsize - (new2size >> 1))
229
                                new2size >>= 1;
230
                if (new2size > 4096) {
231
                        printk(KERN_ERR "VIDC: error: dma buffer (%d) %d > 4K\n",
232
                                newsize, new2size);
233
                        new2size = 4096;
234
                }
235
                dma_bufsize = new2size;
236
                vidc_audio_rate = rate;
237
        }
238
        return vidc_audio_rate;
239
}
240
 
241
static short vidc_audio_set_channels(int dev, short channels)
242
{
243
        switch (channels) {
244
        default:
245
                channels = 2;
246
        case 1:
247
        case 2:
248
                vidc_audio_channels = channels;
249
                vidc_update_filler(vidc_audio_format, vidc_audio_channels);
250
        case 0:
251
                break;
252
        }
253
        return vidc_audio_channels;
254
}
255
 
256
/*
257
 * Open the device
258
 */
259
static int vidc_audio_open(int dev, int mode)
260
{
261
        /* This audio device does not have recording capability */
262
        if (mode == OPEN_READ)
263
                return -EPERM;
264
 
265
        if (vidc_busy)
266
                return -EBUSY;
267
 
268
        vidc_busy = 1;
269
        return 0;
270
}
271
 
272
/*
273
 * Close the device
274
 */
275
static void vidc_audio_close(int dev)
276
{
277
        vidc_busy = 0;
278
}
279
 
280
/*
281
 * Output a block via DMA to sound device.
282
 *
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.
287
 */
288
static void
289
vidc_audio_output_block(int dev, unsigned long buf, int total_count, int one)
290
{
291
        struct dma_buffparms *dmap = audio_devs[dev]->dmap_out;
292
        unsigned long flags;
293
 
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);
298
}
299
 
300
static void
301
vidc_audio_start_input(int dev, unsigned long buf, int count, int intrflag)
302
{
303
}
304
 
305
static int vidc_audio_prepare_for_input(int dev, int bsize, int bcount)
306
{
307
        return -EINVAL;
308
}
309
 
310
static void vidc_audio_dma_interrupt(void)
311
{
312
        DMAbuf_outputintr(vidc_adev, 1);
313
}
314
 
315
/*
316
 * Prepare for outputting samples.
317
 *
318
 * Each buffer that will be passed will be `bsize' bytes long,
319
 * with a total of `bcount' buffers.
320
 */
321
static int vidc_audio_prepare_for_output(int dev, int bsize, int bcount)
322
{
323
        struct audio_operations *adev = audio_devs[dev];
324
 
325
        dma_interrupt = NULL;
326
        adev->dmap_out->flags |= DMA_NODMA;
327
 
328
        return 0;
329
}
330
 
331
/*
332
 * Stop our current operation.
333
 */
334
static void vidc_audio_reset(int dev)
335
{
336
        dma_interrupt = NULL;
337
}
338
 
339
static int vidc_audio_local_qlen(int dev)
340
{
341
        return /*dma_count !=*/ 0;
342
}
343
 
344
static void vidc_audio_trigger(int dev, int enable_bits)
345
{
346
        struct audio_operations *adev = audio_devs[dev];
347
 
348
        if (enable_bits & PCM_ENABLE_OUTPUT) {
349
                if (!(adev->flags & DMA_ACTIVE)) {
350
                        unsigned long flags;
351
 
352
                        local_irq_save(flags);
353
 
354
                        /* prevent recusion */
355
                        adev->flags |= DMA_ACTIVE;
356
 
357
                        dma_interrupt = vidc_audio_dma_interrupt;
358
                        vidc_sound_dma_irq(0, NULL, NULL);
359
                        iomd_writeb(DMA_CR_E | 0x10, IOMD_SD0CR);
360
 
361
                        local_irq_restore(flags);
362
                }
363
        }
364
}
365
 
366
static struct audio_driver vidc_audio_driver =
367
{
368
        owner:                  THIS_MODULE,
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
381
};
382
 
383
static struct mixer_operations vidc_mixer_operations = {
384
        owner:          THIS_MODULE,
385
        id:             "VIDC",
386
        name:           "VIDCsound",
387
        ioctl:          vidc_mixer_ioctl
388
};
389
 
390
void vidc_update_filler(int format, int channels)
391
{
392
#define TYPE(fmt,ch) (((fmt)<<2) | ((ch)&3))
393
 
394
        switch (TYPE(format, channels)) {
395
        default:
396
        case TYPE(AFMT_U8, 1):
397
                vidc_filler = vidc_fill_1x8_u;
398
                break;
399
 
400
        case TYPE(AFMT_U8, 2):
401
                vidc_filler = vidc_fill_2x8_u;
402
                break;
403
 
404
        case TYPE(AFMT_S8, 1):
405
                vidc_filler = vidc_fill_1x8_s;
406
                break;
407
 
408
        case TYPE(AFMT_S8, 2):
409
                vidc_filler = vidc_fill_2x8_s;
410
                break;
411
 
412
        case TYPE(AFMT_S16_LE, 1):
413
                vidc_filler = vidc_fill_1x16_s;
414
                break;
415
 
416
        case TYPE(AFMT_S16_LE, 2):
417
                vidc_filler = vidc_fill_2x16_s;
418
                break;
419
        }
420
}
421
 
422
static void __init attach_vidc(struct address_info *hw_config)
423
{
424
        char name[32];
425
        int i, adev;
426
 
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));
430
 
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);
435
 
436
        if (adev < 0)
437
                goto audio_failed;
438
 
439
        /*
440
         * 1024 bytes => 64 buffers
441
         */
442
        audio_devs[adev]->min_fragment = 10;
443
        audio_devs[adev]->mixer_dev = num_mixers;
444
 
445
        audio_devs[adev]->mixer_dev =
446
                sound_install_mixer(MIXER_DRIVER_VERSION,
447
                                name, &vidc_mixer_operations,
448
                                sizeof(vidc_mixer_operations), NULL);
449
 
450
        if (audio_devs[adev]->mixer_dev < 0)
451
                goto mixer_failed;
452
 
453
        for (i = 0; i < 2; i++) {
454
                dma_buf[i] = get_free_page(GFP_KERNEL);
455
                if (!dma_buf[i]) {
456
                        printk(KERN_ERR "%s: can't allocate required buffers\n",
457
                                name);
458
                        goto mem_failed;
459
                }
460
                dma_pbuf[i] = virt_to_phys((void *)dma_buf[i]);
461
        }
462
 
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);
465
                goto dma_failed;
466
        }
467
 
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);
471
                goto irq_failed;
472
        }
473
        old_mksound = kd_mksound;
474
        kd_mksound = vidc_mksound;
475
        vidc_adev = adev;
476
        vidc_mixer_set(SOUND_MIXER_VOLUME, (85 | 85 << 8));
477
 
478
#if defined(CONFIG_SOUND_SOFTOSS) || defined(CONFIG_SOUND_SOFTOSS_MODULE)
479
        softoss_dev = adev;
480
#endif
481
        return;
482
 
483
irq_failed:
484
        sound_free_dma(hw_config->dma);
485
dma_failed:
486
mem_failed:
487
        for (i = 0; i < 2; i++)
488
                free_page(dma_buf[i]);
489
        sound_unload_mixerdev(audio_devs[adev]->mixer_dev);
490
mixer_failed:
491
        sound_unload_audiodev(adev);
492
audio_failed:
493
        return;
494
}
495
 
496
static int __init probe_vidc(struct address_info *hw_config)
497
{
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";
503
        return 1;
504
}
505
 
506
static void __exit unload_vidc(struct address_info *hw_config)
507
{
508
        int i, adev = vidc_adev;
509
 
510
        vidc_adev = -1;
511
 
512
        if (old_mksound)
513
                kd_mksound = old_mksound;
514
 
515
        free_irq(hw_config->irq, &dma_start);
516
        sound_free_dma(hw_config->dma);
517
 
518
        if (adev >= 0) {
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]);
523
        }
524
}
525
 
526
static struct address_info cfg;
527
 
528
static int __init init_vidc(void)
529
{
530
        if (probe_vidc(&cfg) == 0)
531
                return -ENODEV;
532
 
533
        attach_vidc(&cfg);
534
 
535
        return 0;
536
}
537
 
538
static void __exit cleanup_vidc(void)
539
{
540
        unload_vidc(&cfg);
541
}
542
 
543
module_init(init_vidc);
544
module_exit(cleanup_vidc);
545
 
546
MODULE_AUTHOR("Russell King");
547
MODULE_DESCRIPTION("VIDC20 audio driver");
548
MODULE_LICENSE("GPL");
549
EXPORT_NO_SYMBOLS;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.