Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#define _PAS2_PCM_C_
/*
 * sound/pas2_pcm.c
 *
 * The low level driver for the Pro Audio Spectrum ADC/DAC.
 */

#include <linux/config.h>
#include "sound_config.h"

#if defined(CONFIG_PAS) && defined(CONFIG_AUDIO)

#ifndef DEB
#define DEB(WHAT)
#endif

#define PAS_PCM_INTRBITS (0x08)
/*
 * Sample buffer timer interrupt enable
 */

#define PCM_NON	0
#define PCM_DAC	1
#define PCM_ADC	2

static unsigned long pcm_speed = 0;	/* sampling rate */
static unsigned char pcm_channels = 1;	/* channels (1 or 2) */
static unsigned char pcm_bits = 8;	/* bits/sample (8 or 16) */
static unsigned char pcm_filter = 0;	/* filter FLAG */
static unsigned char pcm_mode = PCM_NON;
static unsigned long pcm_count = 0;
static unsigned short pcm_bitsok = 8;	/* mask of OK bits */
static int      pcm_busy = 0;
int             pas_audiodev = 0;
static int      open_mode = 0;

int
pcm_set_speed (int arg)
{
  int             foo, tmp;
  unsigned long   flags;

  if (arg == 0)
    return pcm_speed;

  if (arg > 44100)
    arg = 44100;
  if (arg < 5000)
    arg = 5000;

  if (pcm_channels & 2)
    {
      foo = (596590 + (arg / 2)) / arg;
      arg = 596590 / foo;
    }
  else
    {
      foo = (1193180 + (arg / 2)) / arg;
      arg = 1193180 / foo;
    }

  pcm_speed = arg;

  tmp = pas_read (0x0B8A);

  /*
     * Set anti-aliasing filters according to sample rate. You really *NEED*
     * to enable this feature for all normal recording unless you want to
     * experiment with aliasing effects.
     * These filters apply to the selected "recording" source.
     * I (pfw) don't know the encoding of these 5 bits. The values shown
     * come from the SDK found on ftp.uwp.edu:/pub/msdos/proaudio/.
     *
     * I cleared bit 5 of these values, since that bit controls the master
     * mute flag. (Olav Wölfelschneider)
     *
   */
#if !defined NO_AUTO_FILTER_SET
  tmp &= 0xe0;
  if (pcm_speed >= 2 * 17897)
    tmp |= 0x01;
  else if (pcm_speed >= 2 * 15909)
    tmp |= 0x02;
  else if (pcm_speed >= 2 * 11931)
    tmp |= 0x09;
  else if (pcm_speed >= 2 * 8948)
    tmp |= 0x11;
  else if (pcm_speed >= 2 * 5965)
    tmp |= 0x19;
  else if (pcm_speed >= 2 * 2982)
    tmp |= 0x04;
  pcm_filter = tmp;
#endif

  save_flags (flags);
  cli ();

  pas_write (tmp & ~(0x40 | 0x80), 0x0B8A);
  pas_write (0x00 | 0x30 | 0x04, 0x138B);
  pas_write (foo & 0xff, 0x1388);
  pas_write ((foo >> 8) & 0xff, 0x1388);
  pas_write (tmp, 0x0B8A);

  restore_flags (flags);

  return pcm_speed;
}

int
pcm_set_channels (int arg)
{

  if ((arg != 1) && (arg != 2))
    return pcm_channels;

  if (arg != pcm_channels)
    {
      pas_write (pas_read (0xF8A) ^ 0x20, 0xF8A);

      pcm_channels = arg;
      pcm_set_speed (pcm_speed);	/*
					   * The speed must be reinitialized
					 */
    }

  return pcm_channels;
}

int
pcm_set_bits (int arg)
{
  if (arg == 0)
    return pcm_bits;

  if ((arg & pcm_bitsok) != arg)
    return pcm_bits;

  if (arg != pcm_bits)
    {
      pas_write (pas_read (0x8389) ^ 0x04, 0x8389);

      pcm_bits = arg;
    }

  return pcm_bits;
}

static int
pas_audio_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
{
  DEB (printk ("pas2_pcm.c: static int pas_audio_ioctl(unsigned int cmd = %X, unsigned int arg = %X)\n", cmd, arg));

  switch (cmd)
    {
    case SOUND_PCM_WRITE_RATE:
      if (local)
	return pcm_set_speed ((int) arg);
      return ioctl_out (arg, pcm_set_speed (ioctl_in (arg)));
      break;

    case SOUND_PCM_READ_RATE:
      if (local)
	return pcm_speed;
      return ioctl_out (arg, pcm_speed);
      break;

    case SNDCTL_DSP_STEREO:
      if (local)
	return pcm_set_channels ((int) arg + 1) - 1;
      return ioctl_out (arg, pcm_set_channels (ioctl_in (arg) + 1) - 1);
      break;

    case SOUND_PCM_WRITE_CHANNELS:
      if (local)
	return pcm_set_channels ((int) arg);
      return ioctl_out (arg, pcm_set_channels (ioctl_in (arg)));
      break;

    case SOUND_PCM_READ_CHANNELS:
      if (local)
	return pcm_channels;
      return ioctl_out (arg, pcm_channels);
      break;

    case SNDCTL_DSP_SETFMT:
      if (local)
	return pcm_set_bits ((int) arg);
      return ioctl_out (arg, pcm_set_bits (ioctl_in (arg)));
      break;

    case SOUND_PCM_READ_BITS:
      if (local)
	return pcm_bits;
      return ioctl_out (arg, pcm_bits);

    case SOUND_PCM_WRITE_FILTER:	/*
					 * NOT YET IMPLEMENTED
					 */
      if (ioctl_in (arg) > 1)
	return -EINVAL;
      pcm_filter = ioctl_in (arg);
      break;

    case SOUND_PCM_READ_FILTER:
      return ioctl_out (arg, pcm_filter);
      break;

    default:
      return -EINVAL;
    }

  return -EINVAL;
}

static void
pas_audio_reset (int dev)
{
  DEB (printk ("pas2_pcm.c: static void pas_audio_reset(void)\n"));

  pas_write (pas_read (0xF8A) & ~0x40, 0xF8A);
}

static int
pas_audio_open (int dev, int mode)
{
  int             err;
  unsigned long   flags;

  DEB (printk ("pas2_pcm.c: static int pas_audio_open(int mode = %X)\n", mode));

  save_flags (flags);
  cli ();
  if (pcm_busy)
    {
      restore_flags (flags);
      return -EBUSY;
    }

  pcm_busy = 1;
  restore_flags (flags);

  if ((err = pas_set_intr (PAS_PCM_INTRBITS)) < 0)
    return err;


  pcm_count = 0;
  open_mode = mode;

  return 0;
}

static void
pas_audio_close (int dev)
{
  unsigned long   flags;

  DEB (printk ("pas2_pcm.c: static void pas_audio_close(void)\n"));

  save_flags (flags);
  cli ();

  pas_audio_reset (dev);
  pas_remove_intr (PAS_PCM_INTRBITS);
  pcm_mode = PCM_NON;

  pcm_busy = 0;
  restore_flags (flags);
}

static void
pas_audio_output_block (int dev, unsigned long buf, int count,
			int intrflag, int restart_dma)
{
  unsigned long   flags, cnt;

  DEB (printk ("pas2_pcm.c: static void pas_audio_output_block(char *buf = %P, int count = %X)\n", buf, count));

  cnt = count;
  if (audio_devs[dev]->dmachan1 > 3)
    cnt >>= 1;

  if (audio_devs[dev]->flags & DMA_AUTOMODE &&
      intrflag &&
      cnt == pcm_count)
    return;			/*
				 * Auto mode on. No need to react
				 */

  save_flags (flags);
  cli ();

  pas_write (pas_read (0xF8A) & ~0x40,
	     0xF8A);

  if (restart_dma)
    DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);

  if (audio_devs[dev]->dmachan1 > 3)
    count >>= 1;

  if (count != pcm_count)
    {
      pas_write (pas_read (0x0B8A) & ~0x80, 0x0B8A);
      pas_write (0x40 | 0x30 | 0x04, 0x138B);
      pas_write (count & 0xff, 0x1389);
      pas_write ((count >> 8) & 0xff, 0x1389);
      pas_write (pas_read (0x0B8A) | 0x80, 0x0B8A);

      pcm_count = count;
    }
  pas_write (pas_read (0x0B8A) | 0x80 | 0x40, 0x0B8A);
#ifdef NO_TRIGGER
  pas_write (pas_read (0xF8A) | 0x40 | 0x10, 0xF8A);
#endif

  pcm_mode = PCM_DAC;

  restore_flags (flags);
}

static void
pas_audio_start_input (int dev, unsigned long buf, int count,
		       int intrflag, int restart_dma)
{
  unsigned long   flags;
  int             cnt;

  DEB (printk ("pas2_pcm.c: static void pas_audio_start_input(char *buf = %P, int count = %X)\n", buf, count));

  cnt = count;
  if (audio_devs[dev]->dmachan1 > 3)
    cnt >>= 1;

  if (audio_devs[pas_audiodev]->flags & DMA_AUTOMODE &&
      intrflag &&
      cnt == pcm_count)
    return;			/*
				 * Auto mode on. No need to react
				 */

  save_flags (flags);
  cli ();

  if (restart_dma)
    DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);

  if (audio_devs[dev]->dmachan1 > 3)
    count >>= 1;

  if (count != pcm_count)
    {
      pas_write (pas_read (0x0B8A) & ~0x80, 0x0B8A);
      pas_write (0x40 | 0x30 | 0x04, 0x138B);
      pas_write (count & 0xff, 0x1389);
      pas_write ((count >> 8) & 0xff, 0x1389);
      pas_write (pas_read (0x0B8A) | 0x80, 0x0B8A);

      pcm_count = count;
    }
  pas_write (pas_read (0x0B8A) | 0x80 | 0x40, 0x0B8A);
#ifdef NO_TRIGGER
  pas_write ((pas_read (0xF8A) | 0x40) & ~0x10, 0xF8A);
#endif

  pcm_mode = PCM_ADC;

  restore_flags (flags);
}

#ifndef NO_TRIGGER
static void
pas_audio_trigger (int dev, int state)
{
  unsigned long   flags;

  save_flags (flags);
  cli ();
  state &= open_mode;

  if (state & PCM_ENABLE_OUTPUT)
    pas_write (pas_read (0xF8A) | 0x40 | 0x10, 0xF8A);
  else if (state & PCM_ENABLE_INPUT)
    pas_write ((pas_read (0xF8A) | 0x40) & ~0x10, 0xF8A);
  else
    pas_write (pas_read (0xF8A) & ~0x40, 0xF8A);

  restore_flags (flags);
}
#endif

static int
pas_audio_prepare_for_input (int dev, int bsize, int bcount)
{
  return 0;
}

static int
pas_audio_prepare_for_output (int dev, int bsize, int bcount)
{
  return 0;
}

static struct audio_driver pas_audio_driver =
{
  pas_audio_open,
  pas_audio_close,
  pas_audio_output_block,
  pas_audio_start_input,
  pas_audio_ioctl,
  pas_audio_prepare_for_input,
  pas_audio_prepare_for_output,
  pas_audio_reset,
  pas_audio_reset,
  NULL,
  NULL,
  NULL,
  NULL,
  pas_audio_trigger
};

static struct audio_operations pas_audio_operations =
{
  "Pro Audio Spectrum",
  DMA_AUTOMODE,
  AFMT_U8 | AFMT_S16_LE,
  NULL,
  &pas_audio_driver
};

void
pas_pcm_init (struct address_info *hw_config)
{
  DEB (printk ("pas2_pcm.c: long pas_pcm_init()\n"));

  pcm_bitsok = 8;
  if (pas_read (0xEF8B) & 0x08)
    pcm_bitsok |= 16;

  pcm_set_speed (DSP_DEFAULT_SPEED);

  if (num_audiodevs < MAX_AUDIO_DEV)
    {
      audio_devs[pas_audiodev = num_audiodevs++] = &pas_audio_operations;
      audio_devs[pas_audiodev]->dmachan1 = hw_config->dma;
      audio_devs[pas_audiodev]->buffsize = DSP_BUFFSIZE;
    }
  else
    printk ("PAS2: Too many PCM devices available\n");
}

void
pas_pcm_interrupt (unsigned char status, int cause)
{
  if (cause == 1)		/*
				 * PCM buffer done
				 */
    {
      /*
       * Halt the PCM first. Otherwise we don't have time to start a new
       * block before the PCM chip proceeds to the next sample
       */

      if (!(audio_devs[pas_audiodev]->flags & DMA_AUTOMODE))
	{
	  pas_write (pas_read (0xF8A) & ~0x40,
		     0xF8A);
	}

      switch (pcm_mode)
	{

	case PCM_DAC:
	  DMAbuf_outputintr (pas_audiodev, 1);
	  break;

	case PCM_ADC:
	  DMAbuf_inputintr (pas_audiodev);
	  break;

	default:
	  printk ("PAS: Unexpected PCM interrupt\n");
	}
    }
}

#endif