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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [drivers/] [sound/] [ad1848.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1626 jcastillo
/*
2
 * sound/ad1848.c
3
 *
4
 * The low level driver for the AD1848/CS4248 codec chip which
5
 * is used for example in the MS Sound System.
6
 *
7
 * The CS4231 which is used in the GUS MAX and some other cards is
8
 * upwards compatible with AD1848 and this driver is able to drive it.
9
 *
10
 * CS4231A and AD1845 are upward compatible with CS4231. However
11
 * the new features of these chips are different.
12
 *
13
 * CS4232 is a PnP audio chip which contains a CS4231A (and SB, MPU).
14
 * CS4232A is an improved version of CS4232.
15
 */
16
 
17
/*
18
 * Copyright (C) by Hannu Savolainen 1993-1996
19
 *
20
 * USS/Lite for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
21
 * Version 2 (June 1991). See the "COPYING" file distributed with this software
22
 * for more info.
23
 */
24
#include <linux/config.h>
25
 
26
 
27
#define DEB(x)
28
#define DEB1(x)
29
#include "sound_config.h"
30
 
31
#if defined(CONFIG_AD1848)
32
 
33
#include "ad1848_mixer.h"
34
 
35
typedef struct
36
  {
37
    int             base;
38
    int             irq;
39
    int             dual_dma;   /* 1, when two DMA channels allocated */
40
    unsigned char   MCE_bit;
41
    unsigned char   saved_regs[16];
42
    int             debug_flag;
43
 
44
    int             speed;
45
    unsigned char   speed_bits;
46
    int             channels;
47
    int             audio_format;
48
    unsigned char   format_bits;
49
 
50
    int             xfer_count;
51
    int             audio_mode;
52
    int             intr_active;
53
    int             opened;
54
    char           *chip_name;
55
    int             model;
56
#define MD_1848         1
57
#define MD_4231         2
58
#define MD_4231A        3
59
#define MD_1845         4
60
#define MD_4232         5
61
#define MD_C930         6
62
#define MD_IWAVE        7
63
 
64
    /* Mixer parameters */
65
    int             recmask;
66
    int             supported_devices;
67
    int             supported_rec_devices;
68
    unsigned short  levels[32];
69
    int             dev_no;
70
    volatile unsigned long timer_ticks;
71
    int             timer_running;
72
    int             irq_ok;
73
    int            *osp;
74
  }
75
 
76
ad1848_info;
77
 
78
static int      nr_ad1848_devs = 0;
79
static volatile char irq2dev[17] =
80
{-1, -1, -1, -1, -1, -1, -1, -1,
81
 -1, -1, -1, -1, -1, -1, -1, -1, -1};
82
 
83
static int      timer_installed = -1;
84
 
85
static int      ad_format_mask[8 /*devc->model */ ] =
86
{
87
  0,
88
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,
89
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
90
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
91
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW,     /* AD1845 */
92
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
93
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM,
94
  AFMT_U8 | AFMT_S16_LE | AFMT_MU_LAW | AFMT_A_LAW | AFMT_U16_LE | AFMT_IMA_ADPCM
95
};
96
 
97
static ad1848_info dev_info[MAX_AUDIO_DEV];
98
 
99
#define io_Index_Addr(d)        ((d)->base)
100
#define io_Indexed_Data(d)      ((d)->base+1)
101
#define io_Status(d)            ((d)->base+2)
102
#define io_Polled_IO(d)         ((d)->base+3)
103
 
104
static int      ad1848_open (int dev, int mode);
105
static void     ad1848_close (int dev);
106
static int      ad1848_ioctl (int dev, unsigned int cmd, caddr_t arg, int local);
107
static void     ad1848_output_block (int dev, unsigned long buf, int count, int intrflag, int dma_restart);
108
static void     ad1848_start_input (int dev, unsigned long buf, int count, int intrflag, int dma_restart);
109
static int      ad1848_prepare_for_output (int dev, int bsize, int bcount);
110
static int      ad1848_prepare_for_IO (int dev, int bsize, int bcount);
111
static void     ad1848_reset (int dev);
112
static void     ad1848_halt (int dev);
113
static void     ad1848_halt_input (int dev);
114
static void     ad1848_halt_output (int dev);
115
static void     ad1848_trigger (int dev, int bits);
116
static int      ad1848_tmr_install (int dev);
117
static void     ad1848_tmr_reprogram (int dev);
118
 
119
static int
120
ad_read (ad1848_info * devc, int reg)
121
{
122
  unsigned long   flags;
123
  int             x;
124
  int             timeout = 900000;
125
 
126
  while (timeout > 0 && inb (devc->base) == 0x80)        /*Are we initializing */
127
    timeout--;
128
 
129
  save_flags (flags);
130
  cli ();
131
  outb ((unsigned char) (reg & 0xff) | devc->MCE_bit, io_Index_Addr (devc));
132
  x = inb (io_Indexed_Data (devc));
133
  /*  printk("(%02x<-%02x) ", reg|devc->MCE_bit, x); */
134
  restore_flags (flags);
135
 
136
  return x;
137
}
138
 
139
static void
140
ad_write (ad1848_info * devc, int reg, int data)
141
{
142
  unsigned long   flags;
143
  int             timeout = 900000;
144
 
145
  while (timeout > 0 &&
146
         inb (devc->base) == 0x80)      /*Are we initializing */
147
    timeout--;
148
 
149
  save_flags (flags);
150
  cli ();
151
  outb ((unsigned char) (reg & 0xff) | devc->MCE_bit, io_Index_Addr (devc));
152
  outb ((unsigned char) (data & 0xff), io_Indexed_Data (devc));
153
  /* printk("(%02x->%02x) ", reg|devc->MCE_bit, data); */
154
  restore_flags (flags);
155
}
156
 
157
static void
158
wait_for_calibration (ad1848_info * devc)
159
{
160
  int             timeout = 0;
161
 
162
  /*
163
     * Wait until the auto calibration process has finished.
164
     *
165
     * 1)       Wait until the chip becomes ready (reads don't return 0x80).
166
     * 2)       Wait until the ACI bit of I11 gets on and then off.
167
   */
168
 
169
  timeout = 100000;
170
  while (timeout > 0 && inb (devc->base) == 0x80)
171
    timeout--;
172
  if (inb (devc->base) & 0x80)
173
    printk ("ad1848: Auto calibration timed out(1).\n");
174
 
175
  timeout = 100;
176
  while (timeout > 0 && !(ad_read (devc, 11) & 0x20))
177
    timeout--;
178
  if (!(ad_read (devc, 11) & 0x20))
179
    return;
180
 
181
  timeout = 80000;
182
  while (timeout > 0 && ad_read (devc, 11) & 0x20)
183
    timeout--;
184
  if (ad_read (devc, 11) & 0x20)
185
    if (devc->model != MD_1845)
186
      printk ("ad1848: Auto calibration timed out(3).\n");
187
}
188
 
189
static void
190
ad_mute (ad1848_info * devc)
191
{
192
  int             i;
193
  unsigned char   prev;
194
 
195
  /*
196
     * Save old register settings and mute output channels
197
   */
198
  for (i = 6; i < 8; i++)
199
    {
200
      prev = devc->saved_regs[i] = ad_read (devc, i);
201
      ad_write (devc, i, prev | 0x80);
202
    }
203
 
204
/*
205
 * Let's have some delay
206
 */
207
  for (i = 0; i < 1000; i++)
208
    inb (devc->base);
209
}
210
 
211
static void
212
ad_unmute (ad1848_info * devc)
213
{
214
  int             i;
215
 
216
/*
217
 * Let's have some delay
218
 */
219
  for (i = 0; i < 1000; i++)
220
    inb (devc->base);
221
 
222
  /*
223
     * Restore back old volume registers (unmute)
224
   */
225
  for (i = 6; i < 8; i++)
226
    {
227
      ad_write (devc, i, devc->saved_regs[i] & ~0x80);
228
    }
229
}
230
 
231
static void
232
ad_enter_MCE (ad1848_info * devc)
233
{
234
  unsigned long   flags;
235
  int             timeout = 1000;
236
  unsigned short  prev;
237
 
238
  while (timeout > 0 && inb (devc->base) == 0x80)        /*Are we initializing */
239
    timeout--;
240
 
241
  save_flags (flags);
242
  cli ();
243
 
244
  devc->MCE_bit = 0x40;
245
  prev = inb (io_Index_Addr (devc));
246
  if (prev & 0x40)
247
    {
248
      restore_flags (flags);
249
      return;
250
    }
251
 
252
  outb (devc->MCE_bit, io_Index_Addr (devc));
253
  restore_flags (flags);
254
}
255
 
256
static void
257
ad_leave_MCE (ad1848_info * devc)
258
{
259
  unsigned long   flags;
260
  unsigned char   prev;
261
  int             timeout = 1000;
262
 
263
  while (timeout > 0 && inb (devc->base) == 0x80)        /*Are we initializing */
264
    timeout--;
265
 
266
  save_flags (flags);
267
  cli ();
268
 
269
  devc->MCE_bit = 0x00;
270
  prev = inb (io_Index_Addr (devc));
271
  outb (0x00, io_Index_Addr (devc));    /* Clear the MCE bit */
272
 
273
  if ((prev & 0x40) == 0)        /* Not in MCE mode */
274
    {
275
      restore_flags (flags);
276
      return;
277
    }
278
 
279
  outb (0x00, io_Index_Addr (devc));    /* Clear the MCE bit */
280
  wait_for_calibration (devc);
281
  restore_flags (flags);
282
}
283
 
284
 
285
static int
286
ad1848_set_recmask (ad1848_info * devc, int mask)
287
{
288
  unsigned char   recdev;
289
  int             i, n;
290
 
291
  mask &= devc->supported_rec_devices;
292
 
293
  n = 0;
294
  for (i = 0; i < 32; i++)       /* Count selected device bits */
295
    if (mask & (1 << i))
296
      n++;
297
 
298
  if (n == 0)
299
    mask = SOUND_MASK_MIC;
300
  else if (n != 1)              /* Too many devices selected */
301
    {
302
      mask &= ~devc->recmask;   /* Filter out active settings */
303
 
304
      n = 0;
305
      for (i = 0; i < 32; i++)   /* Count selected device bits */
306
        if (mask & (1 << i))
307
          n++;
308
 
309
      if (n != 1)
310
        mask = SOUND_MASK_MIC;
311
    }
312
 
313
  switch (mask)
314
    {
315
    case SOUND_MASK_MIC:
316
      recdev = 2;
317
      break;
318
 
319
    case SOUND_MASK_LINE:
320
    case SOUND_MASK_LINE3:
321
      recdev = 0;
322
      break;
323
 
324
    case SOUND_MASK_CD:
325
    case SOUND_MASK_LINE1:
326
      recdev = 1;
327
      break;
328
 
329
    case SOUND_MASK_IMIX:
330
      recdev = 3;
331
      break;
332
 
333
    default:
334
      mask = SOUND_MASK_MIC;
335
      recdev = 2;
336
    }
337
 
338
  recdev <<= 6;
339
  ad_write (devc, 0, (ad_read (devc, 0) & 0x3f) | recdev);
340
  ad_write (devc, 1, (ad_read (devc, 1) & 0x3f) | recdev);
341
 
342
  devc->recmask = mask;
343
  return mask;
344
}
345
 
346
static void
347
change_bits (unsigned char *regval, int dev, int chn, int newval)
348
{
349
  unsigned char   mask;
350
  int             shift;
351
 
352
  if (mix_devices[dev][chn].polarity == 1)      /* Reverse */
353
    newval = 100 - newval;
354
 
355
  mask = (1 << mix_devices[dev][chn].nbits) - 1;
356
  shift = mix_devices[dev][chn].bitpos;
357
  newval = (int) ((newval * mask) + 50) / 100;  /* Scale it */
358
 
359
  *regval &= ~(mask << shift);  /* Clear bits */
360
  *regval |= (newval & mask) << shift;  /* Set new value */
361
}
362
 
363
static int
364
ad1848_mixer_get (ad1848_info * devc, int dev)
365
{
366
  if (!((1 << dev) & devc->supported_devices))
367
    return -(EINVAL);
368
 
369
  return devc->levels[dev];
370
}
371
 
372
static int
373
ad1848_mixer_set (ad1848_info * devc, int dev, int value)
374
{
375
  int             left = value & 0x000000ff;
376
  int             right = (value & 0x0000ff00) >> 8;
377
  int             retvol;
378
 
379
  int             regoffs;
380
  unsigned char   val;
381
 
382
  if (left > 100)
383
    left = 100;
384
  if (right > 100)
385
    right = 100;
386
 
387
  if (mix_devices[dev][RIGHT_CHN].nbits == 0)    /* Mono control */
388
    right = left;
389
 
390
  retvol = left | (right << 8);
391
 
392
  /* Scale volumes */
393
  left = mix_cvt[left];
394
  right = mix_cvt[right];
395
 
396
  /* Scale it again */
397
  left = mix_cvt[left];
398
  right = mix_cvt[right];
399
 
400
  if (dev > 31)
401
    return -(EINVAL);
402
 
403
  if (!(devc->supported_devices & (1 << dev)))
404
    return -(EINVAL);
405
 
406
  if (mix_devices[dev][LEFT_CHN].nbits == 0)
407
    return -(EINVAL);
408
 
409
  devc->levels[dev] = retvol;
410
 
411
  /*
412
     * Set the left channel
413
   */
414
 
415
  regoffs = mix_devices[dev][LEFT_CHN].regno;
416
  val = ad_read (devc, regoffs);
417
  change_bits (&val, dev, LEFT_CHN, left);
418
  ad_write (devc, regoffs, val);
419
  devc->saved_regs[regoffs] = val;
420
 
421
  /*
422
     * Set the right channel
423
   */
424
 
425
  if (mix_devices[dev][RIGHT_CHN].nbits == 0)
426
    return retvol;              /* Was just a mono channel */
427
 
428
  regoffs = mix_devices[dev][RIGHT_CHN].regno;
429
  val = ad_read (devc, regoffs);
430
  change_bits (&val, dev, RIGHT_CHN, right);
431
  ad_write (devc, regoffs, val);
432
  devc->saved_regs[regoffs] = val;
433
 
434
  return retvol;
435
}
436
 
437
static void
438
ad1848_mixer_reset (ad1848_info * devc)
439
{
440
  int             i;
441
 
442
  switch (devc->model)
443
    {
444
    case MD_4231:
445
    case MD_4231A:
446
    case MD_C930:
447
    case MD_IWAVE:
448
    case MD_1845:
449
      devc->supported_devices = MODE2_MIXER_DEVICES;
450
      break;
451
 
452
    case MD_4232:
453
      devc->supported_devices = MODE3_MIXER_DEVICES;
454
      break;
455
 
456
    default:
457
      devc->supported_devices = MODE1_MIXER_DEVICES;
458
    }
459
 
460
  devc->supported_rec_devices = MODE1_REC_DEVICES;
461
 
462
  for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
463
    if (devc->supported_devices & (1 << i))
464
      ad1848_mixer_set (devc, i, default_mixer_levels[i]);
465
  ad1848_set_recmask (devc, SOUND_MASK_MIC);
466
}
467
 
468
static int
469
ad1848_mixer_ioctl (int dev, unsigned int cmd, caddr_t arg)
470
{
471
  ad1848_info    *devc = mixer_devs[dev]->devc;
472
 
473
  if (((cmd >> 8) & 0xff) == 'M')
474
    {
475
 
476
      if (_IOC_DIR (cmd) & _IOC_WRITE)
477
        switch (cmd & 0xff)
478
          {
479
          case SOUND_MIXER_RECSRC:
480
            return snd_ioctl_return ((int *) arg, ad1848_set_recmask (devc, get_user ((int *) arg)));
481
            break;
482
 
483
          default:
484
            return snd_ioctl_return ((int *) arg, ad1848_mixer_set (devc, cmd & 0xff, get_user ((int *) arg)));
485
          }
486
      else
487
        switch (cmd & 0xff)     /*
488
                                 * Return parameters
489
                                 */
490
          {
491
 
492
          case SOUND_MIXER_RECSRC:
493
            return snd_ioctl_return ((int *) arg, devc->recmask);
494
            break;
495
 
496
          case SOUND_MIXER_DEVMASK:
497
            return snd_ioctl_return ((int *) arg, devc->supported_devices);
498
            break;
499
 
500
          case SOUND_MIXER_STEREODEVS:
501
            return snd_ioctl_return ((int *) arg, devc->supported_devices & ~(SOUND_MASK_SPEAKER | SOUND_MASK_IMIX));
502
            break;
503
 
504
          case SOUND_MIXER_RECMASK:
505
            return snd_ioctl_return ((int *) arg, devc->supported_rec_devices);
506
            break;
507
 
508
          case SOUND_MIXER_CAPS:
509
            return snd_ioctl_return ((int *) arg, SOUND_CAP_EXCL_INPUT);
510
            break;
511
 
512
          default:
513
            return snd_ioctl_return ((int *) arg, ad1848_mixer_get (devc, cmd & 0xff));
514
          }
515
    }
516
  else
517
    return -(EINVAL);
518
}
519
 
520
static int
521
ad1848_set_speed (int dev, int arg)
522
{
523
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
524
 
525
  /*
526
     * The sampling speed is encoded in the least significant nibble of I8. The
527
     * LSB selects the clock source (0=24.576 MHz, 1=16.9344 MHz) and other
528
     * three bits select the divisor (indirectly):
529
     *
530
     * The available speeds are in the following table. Keep the speeds in
531
     * the increasing order.
532
   */
533
  typedef struct
534
    {
535
      int             speed;
536
      unsigned char   bits;
537
    }
538
  speed_struct;
539
 
540
  static speed_struct speed_table[] =
541
  {
542
    {5510, (0 << 1) | 1},
543
    {5510, (0 << 1) | 1},
544
    {6620, (7 << 1) | 1},
545
    {8000, (0 << 1) | 0},
546
    {9600, (7 << 1) | 0},
547
    {11025, (1 << 1) | 1},
548
    {16000, (1 << 1) | 0},
549
    {18900, (2 << 1) | 1},
550
    {22050, (3 << 1) | 1},
551
    {27420, (2 << 1) | 0},
552
    {32000, (3 << 1) | 0},
553
    {33075, (6 << 1) | 1},
554
    {37800, (4 << 1) | 1},
555
    {44100, (5 << 1) | 1},
556
    {48000, (6 << 1) | 0}
557
  };
558
 
559
  int             i, n, selected = -1;
560
 
561
  n = sizeof (speed_table) / sizeof (speed_struct);
562
 
563
  if (arg == 0)
564
    return devc->speed;
565
 
566
  if (devc->model == MD_1845)   /* AD1845 has different timer than others */
567
    {
568
      if (arg < 4000)
569
        arg = 4000;
570
      if (arg > 50000)
571
        arg = 50000;
572
 
573
      devc->speed = arg;
574
      devc->speed_bits = speed_table[3].bits;
575
      return devc->speed;
576
    }
577
 
578
  if (arg < speed_table[0].speed)
579
    selected = 0;
580
  if (arg > speed_table[n - 1].speed)
581
    selected = n - 1;
582
 
583
  for (i = 1 /*really */ ; selected == -1 && i < n; i++)
584
    if (speed_table[i].speed == arg)
585
      selected = i;
586
    else if (speed_table[i].speed > arg)
587
      {
588
        int             diff1, diff2;
589
 
590
        diff1 = arg - speed_table[i - 1].speed;
591
        diff2 = speed_table[i].speed - arg;
592
 
593
        if (diff1 < diff2)
594
          selected = i - 1;
595
        else
596
          selected = i;
597
      }
598
 
599
  if (selected == -1)
600
    {
601
      printk ("ad1848: Can't find speed???\n");
602
      selected = 3;
603
    }
604
 
605
  devc->speed = speed_table[selected].speed;
606
  devc->speed_bits = speed_table[selected].bits;
607
  return devc->speed;
608
}
609
 
610
static short
611
ad1848_set_channels (int dev, short arg)
612
{
613
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
614
 
615
  if (arg != 1 && arg != 2)
616
    return devc->channels;
617
 
618
  devc->channels = arg;
619
  return arg;
620
}
621
 
622
static unsigned int
623
ad1848_set_bits (int dev, unsigned int arg)
624
{
625
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
626
 
627
  static struct format_tbl
628
    {
629
      int             format;
630
      unsigned char   bits;
631
    }
632
  format2bits[] =
633
  {
634
    {
635
      0, 0
636
    }
637
    ,
638
    {
639
      AFMT_MU_LAW, 1
640
    }
641
    ,
642
    {
643
      AFMT_A_LAW, 3
644
    }
645
    ,
646
    {
647
      AFMT_IMA_ADPCM, 5
648
    }
649
    ,
650
    {
651
      AFMT_U8, 0
652
    }
653
    ,
654
    {
655
      AFMT_S16_LE, 2
656
    }
657
    ,
658
    {
659
      AFMT_S16_BE, 6
660
    }
661
    ,
662
    {
663
      AFMT_S8, 0
664
    }
665
    ,
666
    {
667
      AFMT_U16_LE, 0
668
    }
669
    ,
670
    {
671
      AFMT_U16_BE, 0
672
    }
673
  };
674
  int             i, n = sizeof (format2bits) / sizeof (struct format_tbl);
675
 
676
  if (arg == 0)
677
    return devc->audio_format;
678
 
679
  if (!(arg & ad_format_mask[devc->model]))
680
    arg = AFMT_U8;
681
 
682
  devc->audio_format = arg;
683
 
684
  for (i = 0; i < n; i++)
685
    if (format2bits[i].format == arg)
686
      {
687
        if ((devc->format_bits = format2bits[i].bits) == 0)
688
          return devc->audio_format = AFMT_U8;  /* Was not supported */
689
 
690
        return arg;
691
      }
692
 
693
  /* Still hanging here. Something must be terribly wrong */
694
  devc->format_bits = 0;
695
  return devc->audio_format = AFMT_U8;
696
}
697
 
698
static struct audio_driver ad1848_audio_driver =
699
{
700
  ad1848_open,
701
  ad1848_close,
702
  ad1848_output_block,
703
  ad1848_start_input,
704
  ad1848_ioctl,
705
  ad1848_prepare_for_IO,
706
  ad1848_prepare_for_output,
707
  ad1848_reset,
708
  ad1848_halt,
709
  NULL,
710
  NULL,
711
  ad1848_halt_input,
712
  ad1848_halt_output,
713
  ad1848_trigger,
714
  ad1848_set_speed,
715
  ad1848_set_bits,
716
  ad1848_set_channels
717
};
718
 
719
static struct mixer_operations ad1848_mixer_operations =
720
{
721
  "SOUNDPORT",
722
  "AD1848/CS4248/CS4231",
723
  ad1848_mixer_ioctl
724
};
725
 
726
static int
727
ad1848_open (int dev, int mode)
728
{
729
  ad1848_info    *devc = NULL;
730
  unsigned long   flags;
731
 
732
  if (dev < 0 || dev >= num_audiodevs)
733
    return -(ENXIO);
734
 
735
  devc = (ad1848_info *) audio_devs[dev]->devc;
736
 
737
 
738
  save_flags (flags);
739
  cli ();
740
  if (devc->opened)
741
    {
742
      restore_flags (flags);
743
      return -(EBUSY);
744
    }
745
 
746
  devc->dual_dma = 0;
747
 
748
  if (audio_devs[dev]->flags & DMA_DUPLEX)
749
    {
750
      devc->dual_dma = 1;
751
    }
752
 
753
  devc->intr_active = 0;
754
  devc->opened = 1;
755
  devc->audio_mode = 0;
756
  ad1848_trigger (dev, 0);
757
  restore_flags (flags);
758
/*
759
 * Mute output until the playback really starts. This decreases clicking (hope so).
760
 */
761
  ad_mute (devc);
762
 
763
  return 0;
764
}
765
 
766
static void
767
ad1848_close (int dev)
768
{
769
  unsigned long   flags;
770
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
771
 
772
  DEB (printk ("ad1848_close(void)\n"));
773
 
774
  save_flags (flags);
775
  cli ();
776
 
777
  devc->intr_active = 0;
778
  ad1848_reset (dev);
779
 
780
 
781
  devc->opened = 0;
782
  devc->audio_mode = 0;
783
 
784
  ad_unmute (devc);
785
  restore_flags (flags);
786
}
787
 
788
static int
789
ad1848_ioctl (int dev, unsigned int cmd, caddr_t arg, int local)
790
{
791
  return -(EINVAL);
792
}
793
 
794
static void
795
ad1848_output_block (int dev, unsigned long buf, int count, int intrflag, int dma_restart)
796
{
797
  unsigned long   flags, cnt;
798
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
799
 
800
  if (!dma_restart)
801
    return;
802
 
803
  cnt = count;
804
 
805
  if (devc->audio_format == AFMT_IMA_ADPCM)
806
    {
807
      cnt /= 4;
808
    }
809
  else
810
    {
811
      if (devc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))     /* 16 bit data */
812
        cnt >>= 1;
813
    }
814
  if (devc->channels > 1)
815
    cnt >>= 1;
816
  cnt--;
817
 
818
  if (devc->audio_mode & PCM_ENABLE_OUTPUT && audio_devs[dev]->flags & DMA_AUTOMODE &&
819
      intrflag &&
820
      cnt == devc->xfer_count)
821
    {
822
      devc->audio_mode |= PCM_ENABLE_OUTPUT;
823
      devc->intr_active = 1;
824
      return;                   /*
825
                                 * Auto DMA mode on. No need to react
826
                                 */
827
    }
828
  save_flags (flags);
829
  cli ();
830
 
831
  /* ad_write (devc, 9, ad_read (devc, 9) & ~ 0x01); / * Playback disable */
832
  ad1848_halt (dev);
833
  DMAbuf_start_dma (dev, buf, count, DMA_MODE_WRITE);
834
 
835
  ad_write (devc, 15, (unsigned char) (cnt & 0xff));
836
  ad_write (devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
837
 
838
 
839
  devc->xfer_count = cnt;
840
  devc->audio_mode |= PCM_ENABLE_OUTPUT;
841
  devc->intr_active = 1;
842
  restore_flags (flags);
843
}
844
 
845
static void
846
ad1848_start_input (int dev, unsigned long buf, int count, int intrflag, int dma_restart)
847
{
848
  unsigned long   flags, cnt;
849
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
850
 
851
  if (!dma_restart)
852
    return;
853
 
854
  cnt = count;
855
  if (devc->audio_format == AFMT_IMA_ADPCM)
856
    {
857
      cnt /= 4;
858
    }
859
  else
860
    {
861
      if (devc->audio_format & (AFMT_S16_LE | AFMT_S16_BE))     /* 16 bit data */
862
        cnt >>= 1;
863
    }
864
  if (devc->channels > 1)
865
    cnt >>= 1;
866
  cnt--;
867
 
868
  if (devc->audio_mode & PCM_ENABLE_INPUT && audio_devs[dev]->flags & DMA_AUTOMODE &&
869
      intrflag &&
870
      cnt == devc->xfer_count)
871
    {
872
      devc->audio_mode |= PCM_ENABLE_INPUT;
873
      devc->intr_active = 1;
874
      return;                   /*
875
                                 * Auto DMA mode on. No need to react
876
                                 */
877
    }
878
  save_flags (flags);
879
  cli ();
880
 
881
  if (dma_restart)
882
    {
883
      /* ad1848_halt (dev); */
884
      ad_write (devc, 9, ad_read (devc, 9) & ~0x02);    /* Capture disable */
885
      DMAbuf_start_dma (dev, buf, count, DMA_MODE_READ);
886
    }
887
 
888
  if (devc->model == MD_1848 || !devc->dual_dma)        /* Single DMA channel mode */
889
    {
890
      ad_write (devc, 15, (unsigned char) (cnt & 0xff));
891
      ad_write (devc, 14, (unsigned char) ((cnt >> 8) & 0xff));
892
    }
893
  else
894
    /* Dual DMA channel mode */
895
    {
896
      ad_write (devc, 31, (unsigned char) (cnt & 0xff));
897
      ad_write (devc, 30, (unsigned char) ((cnt >> 8) & 0xff));
898
    }
899
 
900
  ad_unmute (devc);
901
 
902
  devc->xfer_count = cnt;
903
  devc->audio_mode |= PCM_ENABLE_INPUT;
904
  devc->intr_active = 1;
905
  restore_flags (flags);
906
}
907
 
908
static int
909
ad1848_prepare_for_output (int dev, int bsize, int bcount)
910
{
911
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
912
 
913
  ad_mute (devc);
914
 
915
  return ad1848_prepare_for_IO (dev, bsize, bcount);
916
}
917
 
918
static int
919
ad1848_prepare_for_IO (int dev, int bsize, int bcount)
920
{
921
  int             timeout;
922
  unsigned char   fs, old_fs, tmp = 0;
923
  unsigned long   flags;
924
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
925
 
926
  if (devc->audio_mode)
927
    return 0;
928
 
929
  save_flags (flags);
930
  cli ();
931
  fs = devc->speed_bits | (devc->format_bits << 5);
932
 
933
  if (devc->channels > 1)
934
    fs |= 0x10;
935
 
936
  if (devc->model == MD_1845)   /* Use alternate speed select registers */
937
    {
938
      fs &= 0xf0;               /* Mask off the rate select bits */
939
 
940
      ad_write (devc, 22, (devc->speed >> 8) & 0xff);   /* Speed MSB */
941
      ad_write (devc, 23, devc->speed & 0xff);  /* Speed LSB */
942
    }
943
 
944
  old_fs = ad_read (devc, 8);
945
 
946
  if (devc->model != MD_4232)
947
    if (fs == old_fs)           /* No change */
948
      {
949
        restore_flags (flags);
950
        devc->xfer_count = 0;
951
        return 0;
952
      }
953
 
954
  ad_enter_MCE (devc);          /* Enables changes to the format select reg */
955
 
956
  if (devc->model == MD_4232)
957
    {
958
      tmp = ad_read (devc, 16);
959
      ad_write (devc, 16, tmp | 0x30);
960
    }
961
 
962
  ad_write (devc, 8, fs);
963
  /*
964
   * Write to I8 starts resynchronization. Wait until it completes.
965
   */
966
  timeout = 10000;
967
  while (timeout > 0 && inb (devc->base) == 0x80)
968
    timeout--;
969
 
970
  /*
971
     * If mode >= 2 (CS4231), set I28 also. It's the capture format register.
972
   */
973
  if (devc->model != MD_1848)
974
    {
975
      ad_write (devc, 28, fs);
976
 
977
      /*
978
         * Write to I28 starts resynchronization. Wait until it completes.
979
       */
980
      timeout = 10000;
981
      while (timeout > 0 && inb (devc->base) == 0x80)
982
        timeout--;
983
 
984
    }
985
 
986
  if (devc->model == MD_4232)
987
    ad_write (devc, 16, tmp & ~0x30);
988
 
989
  ad_leave_MCE (devc);          /*
990
                                 * Starts the calibration process.
991
                                 */
992
  restore_flags (flags);
993
  devc->xfer_count = 0;
994
 
995
#ifdef CONFIG_SEQUENCER
996
  if (dev == timer_installed && devc->timer_running)
997
    if ((fs & 0x01) != (old_fs & 0x01))
998
      {
999
        ad1848_tmr_reprogram (dev);
1000
      }
1001
#endif
1002
  return 0;
1003
}
1004
 
1005
static void
1006
ad1848_reset (int dev)
1007
{
1008
  ad1848_halt (dev);
1009
}
1010
 
1011
static void
1012
ad1848_halt (int dev)
1013
{
1014
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1015
 
1016
  unsigned char   bits = ad_read (devc, 9);
1017
 
1018
  if (bits & 0x01)
1019
    ad1848_halt_output (dev);
1020
 
1021
  if (bits & 0x02)
1022
    ad1848_halt_input (dev);
1023
}
1024
 
1025
static void
1026
ad1848_halt_input (int dev)
1027
{
1028
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1029
  unsigned long   flags;
1030
 
1031
  save_flags (flags);
1032
  cli ();
1033
 
1034
  ad_mute (devc);
1035
 
1036
  if (devc->model == MD_4232)   /* Use applied black magic */
1037
    {
1038
      int             tmout;
1039
 
1040
      disable_dma (audio_devs[dev]->dmachan1);
1041
 
1042
      for (tmout = 0; tmout < 100000; tmout++)
1043
        if (ad_read (devc, 11) & 0x10)
1044
          break;
1045
      ad_write (devc, 9, ad_read (devc, 9) & ~0x01);    /* Stop playback */
1046
 
1047
      enable_dma (audio_devs[dev]->dmachan1);
1048
      restore_flags (flags);
1049
      return;
1050
    }
1051
  ad_write (devc, 9, ad_read (devc, 9) & ~0x02);        /* Stop capture */
1052
 
1053
 
1054
  outb (0, io_Status (devc));    /* Clear interrupt status */
1055
  outb (0, io_Status (devc));    /* Clear interrupt status */
1056
 
1057
  devc->audio_mode &= ~PCM_ENABLE_INPUT;
1058
 
1059
  restore_flags (flags);
1060
}
1061
 
1062
static void
1063
ad1848_halt_output (int dev)
1064
{
1065
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1066
  unsigned long   flags;
1067
 
1068
  save_flags (flags);
1069
  cli ();
1070
 
1071
  ad_mute (devc);
1072
  if (devc->model == MD_4232)   /* Use applied black magic */
1073
    {
1074
      int             tmout;
1075
 
1076
      disable_dma (audio_devs[dev]->dmachan1);
1077
 
1078
      for (tmout = 0; tmout < 100000; tmout++)
1079
        if (ad_read (devc, 11) & 0x10)
1080
          break;
1081
      ad_write (devc, 9, ad_read (devc, 9) & ~0x01);    /* Stop playback */
1082
 
1083
      enable_dma (audio_devs[dev]->dmachan1);
1084
      restore_flags (flags);
1085
      return;
1086
    }
1087
  ad_write (devc, 9, ad_read (devc, 9) & ~0x01);        /* Stop playback */
1088
 
1089
 
1090
  outb (0, io_Status (devc));    /* Clear interrupt status */
1091
  outb (0, io_Status (devc));    /* Clear interrupt status */
1092
 
1093
  devc->audio_mode &= ~PCM_ENABLE_OUTPUT;
1094
 
1095
  restore_flags (flags);
1096
}
1097
 
1098
static void
1099
ad1848_trigger (int dev, int state)
1100
{
1101
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
1102
  unsigned long   flags;
1103
  unsigned char   tmp, old;
1104
 
1105
  save_flags (flags);
1106
  cli ();
1107
  state &= devc->audio_mode;
1108
 
1109
  tmp = (old = ad_read (devc, 9)) & ~0x03;
1110
  if (state & PCM_ENABLE_INPUT)
1111
    tmp |= 0x02;
1112
  if (state & PCM_ENABLE_OUTPUT)
1113
    tmp |= 0x01;
1114
 
1115
  if (!(state & PCM_ENABLE_OUTPUT) && old & 0x01);
1116
  ad_mute (devc);
1117
 
1118
  ad_write (devc, 9, tmp);
1119
 
1120
  if (state & PCM_ENABLE_OUTPUT && !(old & 0x01));
1121
  ad_unmute (devc);
1122
 
1123
  restore_flags (flags);
1124
}
1125
 
1126
int
1127
ad1848_detect (int io_base, int *ad_flags, int *osp)
1128
{
1129
  /*
1130
   * Initial values for the indirect registers of CS4248/AD1848.
1131
   */
1132
  static int      init_values[] =
1133
  {
1134
    0xa8, 0xa8, 0x08, 0x08, 0x08, 0x08, 0x00, 0x00,
1135
    0x00, 0x0c, 0x02, 0x00, 0x8a, 0x01, 0x00, 0x00,
1136
 
1137
  /* Positions 16 to 31 just for CS4231/2 and ad1845 */
1138
    0x80, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,
1139
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1140
  };
1141
 
1142
  unsigned char   tmp;
1143
  ad1848_info    *devc = &dev_info[nr_ad1848_devs];
1144
  unsigned char   tmp1 = 0xff, tmp2 = 0xff;
1145
  int             optiC930 = 0;  /* OPTi 82C930 flag */
1146
  int             interwave = 0;
1147
  int             ad1847_flag = 0;
1148
 
1149
  int             i;
1150
 
1151
  DDB (printk ("ad1848_detect(%x)\n", io_base));
1152
 
1153
  if (ad_flags)
1154
    {
1155
      if (*ad_flags == 0x12345678)
1156
        interwave = 1;
1157
      *ad_flags = 0;
1158
    }
1159
 
1160
  if (nr_ad1848_devs >= MAX_AUDIO_DEV)
1161
    {
1162
      DDB (printk ("ad1848 detect error - step 0\n"));
1163
      return 0;
1164
    }
1165
  if (check_region (io_base, 4))
1166
    {
1167
      printk ("\n\nad1848.c: Port %x not free.\n\n", io_base);
1168
      return 0;
1169
    }
1170
 
1171
  devc->base = io_base;
1172
  devc->irq_ok = 0;
1173
  devc->timer_running = 0;
1174
  devc->MCE_bit = 0x40;
1175
  devc->irq = 0;
1176
  devc->opened = 0;
1177
  devc->chip_name = "AD1848";
1178
  devc->model = MD_1848;        /* AD1848 or CS4248 */
1179
  devc->osp = osp;
1180
  devc->debug_flag = 0;
1181
 
1182
  /*
1183
     * Check that the I/O address is in use.
1184
     *
1185
     * The bit 0x80 of the base I/O port is known to be 0 after the
1186
     * chip has performed its power on initialization. Just assume
1187
     * this has happened before the OS is starting.
1188
     *
1189
     * If the I/O address is unused, it typically returns 0xff.
1190
   */
1191
 
1192
  DDB (printk ("ad1848_detect() - step A\n"));
1193
  if ((inb (devc->base) & 0x80) != 0x00)        /* Not a AD1848 */
1194
    {
1195
      DDB (printk ("ad1848 detect error - step A (%02x)\n",
1196
                   inb (devc->base)));
1197
      return 0;
1198
    }
1199
 
1200
  DDB (printk ("ad1848: regs: "));
1201
  for (i = 0; i < 32; i++)
1202
    DDB (printk ("%02x ", ad_read (devc, i)));
1203
  DDB (printk ("\n"));
1204
 
1205
  /*
1206
     * Test if it's possible to change contents of the indirect registers.
1207
     * Registers 0 and 1 are ADC volume registers. The bit 0x10 is read only
1208
     * so try to avoid using it.
1209
   */
1210
 
1211
  DDB (printk ("ad1848_detect() - step B\n"));
1212
  ad_write (devc, 0, 0xaa);
1213
  ad_write (devc, 1, 0x45);     /* 0x55 with bit 0x10 clear */
1214
 
1215
  if ((tmp1 = ad_read (devc, 0)) != 0xaa || (tmp2 = ad_read (devc, 1)) != 0x45)
1216
    if (tmp2 == 0x65)           /* AD1847 has couple of bits hardcoded to 1 */
1217
      ad1847_flag = 1;
1218
    else
1219
      {
1220
        DDB (printk ("ad1848 detect error - step B (%x/%x)\n", tmp1, tmp2));
1221
        return 0;
1222
      }
1223
 
1224
  DDB (printk ("ad1848_detect() - step C\n"));
1225
  ad_write (devc, 0, 0x45);
1226
  ad_write (devc, 1, 0xaa);
1227
 
1228
  if ((tmp1 = ad_read (devc, 0)) != 0x45 || (tmp2 = ad_read (devc, 1)) != 0xaa)
1229
    if (tmp2 == 0x8a)           /* AD1847 has few bits hardcoded to 1 */
1230
      ad1847_flag = 1;
1231
    else
1232
      {
1233
        DDB (printk ("ad1848 detect error - step C (%x/%x)\n", tmp1, tmp2));
1234
        return 0;
1235
      }
1236
 
1237
  /*
1238
     * The indirect register I12 has some read only bits. Lets
1239
     * try to change them.
1240
   */
1241
 
1242
  DDB (printk ("ad1848_detect() - step D\n"));
1243
  tmp = ad_read (devc, 12);
1244
  ad_write (devc, 12, (~tmp) & 0x0f);
1245
 
1246
  if ((tmp & 0x0f) != ((tmp1 = ad_read (devc, 12)) & 0x0f))
1247
    {
1248
      DDB (printk ("ad1848 detect error - step D (%x)\n", tmp1));
1249
      return 0;
1250
    }
1251
 
1252
  /*
1253
     * NOTE! Last 4 bits of the reg I12 tell the chip revision.
1254
     *   0x01=RevB and 0x0A=RevC.
1255
   */
1256
 
1257
  /*
1258
     * The original AD1848/CS4248 has just 15 indirect registers. This means
1259
     * that I0 and I16 should return the same value (etc.).
1260
     * Ensure that the Mode2 enable bit of I12 is 0. Otherwise this test fails
1261
     * with CS4231.
1262
   */
1263
 
1264
/*
1265
 * OPTi 82C930 has mode2 control bit in another place. This test will fail
1266
 * with it. Accept this situation as a possible indication of this chip.
1267
 */
1268
 
1269
  DDB (printk ("ad1848_detect() - step F\n"));
1270
  ad_write (devc, 12, 0);        /* Mode2=disabled */
1271
 
1272
  for (i = 0; i < 16; i++)
1273
    if ((tmp1 = ad_read (devc, i)) != (tmp2 = ad_read (devc, i + 16)))
1274
      {
1275
        DDB (printk ("ad1848 detect step F(%d/%x/%x) - OPTi chip?\n", i, tmp1, tmp2));
1276
        if (!ad1847_flag)
1277
          optiC930 = 1;
1278
        break;
1279
      }
1280
 
1281
  /*
1282
     * Try to switch the chip to mode2 (CS4231) by setting the MODE2 bit (0x40).
1283
     * The bit 0x80 is always 1 in CS4248 and CS4231.
1284
   */
1285
 
1286
  DDB (printk ("ad1848_detect() - step G\n"));
1287
  ad_write (devc, 12, 0x40);    /* Set mode2, clear 0x80 */
1288
 
1289
  tmp1 = ad_read (devc, 12);
1290
  if (tmp1 & 0x80)
1291
    {
1292
      if (ad_flags)
1293
        *ad_flags |= AD_F_CS4248;
1294
 
1295
      devc->chip_name = "CS4248";       /* Our best knowledge just now */
1296
    }
1297
 
1298
  if (optiC930 || (tmp1 & 0xc0) == (0x80 | 0x40))
1299
    {
1300
      /*
1301
         *      CS4231 detected - is it?
1302
         *
1303
         *      Verify that setting I0 doesn't change I16.
1304
       */
1305
      DDB (printk ("ad1848_detect() - step H\n"));
1306
      ad_write (devc, 16, 0);    /* Set I16 to known value */
1307
 
1308
      ad_write (devc, 0, 0x45);
1309
      if ((tmp1 = ad_read (devc, 16)) != 0x45)  /* No change -> CS4231? */
1310
        {
1311
 
1312
          ad_write (devc, 0, 0xaa);
1313
          if ((tmp1 = ad_read (devc, 16)) == 0xaa)      /* Rotten bits? */
1314
            {
1315
              DDB (printk ("ad1848 detect error - step H(%x)\n", tmp1));
1316
              return 0;
1317
            }
1318
 
1319
          /*
1320
             * Verify that some bits of I25 are read only.
1321
           */
1322
 
1323
          DDB (printk ("ad1848_detect() - step I\n"));
1324
          tmp1 = ad_read (devc, 25);    /* Original bits */
1325
          ad_write (devc, 25, ~tmp1);   /* Invert all bits */
1326
          if ((ad_read (devc, 25) & 0xe7) == (tmp1 & 0xe7))
1327
            {
1328
              int             id;
1329
 
1330
              /*
1331
               *      It's at least CS4231
1332
               */
1333
              devc->chip_name = "CS4231";
1334
 
1335
              devc->model = MD_4231;
1336
 
1337
              /*
1338
               * It could be an AD1845 or CS4231A as well.
1339
               * CS4231 and AD1845 report the same revision info in I25
1340
               * while the CS4231A reports different.
1341
               */
1342
 
1343
              DDB (printk ("ad1848_detect() - step I\n"));
1344
              id = ad_read (devc, 25) & 0xe7;
1345
 
1346
              switch (id)
1347
                {
1348
 
1349
                case 0xa0:
1350
                  devc->chip_name = "CS4231A";
1351
                  devc->model = MD_4231A;
1352
                  break;
1353
 
1354
                case 0xa2:
1355
                  devc->chip_name = "CS4232";
1356
                  devc->model = MD_4232;
1357
                  break;
1358
 
1359
                case 0xb2:
1360
                  devc->chip_name = "CS4232A";
1361
                  devc->model = MD_4232;
1362
                  break;
1363
 
1364
                case 0x80:
1365
                  {
1366
                    /*
1367
                     * It must be a CS4231 or AD1845. The register I23 of
1368
                     * CS4231 is undefined and it appears to be read only.
1369
                     * AD1845 uses I23 for setting sample rate. Assume
1370
                     * the chip is AD1845 if I23 is changeable.
1371
                     */
1372
 
1373
                    unsigned char   tmp = ad_read (devc, 23);
1374
 
1375
                    ad_write (devc, 23, ~tmp);
1376
                    if (ad_read (devc, 23) != tmp)      /* AD1845 ? */
1377
                      {
1378
                        devc->chip_name = "AD1845";
1379
                        devc->model = MD_1845;
1380
                      }
1381
                    else if (interwave)
1382
                      {
1383
                        devc->model = MD_IWAVE;
1384
                        devc->chip_name = "IWave";
1385
                      }
1386
 
1387
                    ad_write (devc, 23, tmp);   /* Restore */
1388
                  }
1389
                  break;
1390
 
1391
                default:        /* Assume CS4231 or OPTi 82C930 */
1392
                  if (optiC930)
1393
                    {
1394
                      devc->chip_name = "82C930";
1395
                      devc->model = MD_C930;
1396
                    }
1397
                  else
1398
                    {
1399
                      devc->model = MD_4231;
1400
                    }
1401
 
1402
                }
1403
            }
1404
          ad_write (devc, 25, tmp1);    /* Restore bits */
1405
 
1406
          DDB (printk ("ad1848_detect() - step K\n"));
1407
        }
1408
    }
1409
 
1410
  DDB (printk ("ad1848_detect() - step L\n"));
1411
  if (ad_flags)
1412
    {
1413
      if (devc->model != MD_1848)
1414
        *ad_flags |= AD_F_CS4231;
1415
    }
1416
 
1417
  DDB (printk ("ad1848_detect() - Detected OK\n"));
1418
 
1419
  if (devc->model == MD_1848 && ad1847_flag)
1420
    devc->chip_name = "AD1847";
1421
 
1422
  for (i = 0; i < 16; i++)
1423
    ad_write (devc, i, init_values[i]);
1424
 
1425
  ad_mute (devc);               /* Initialize some variables */
1426
  ad_unmute (devc);             /* Leave it unmuted now */
1427
 
1428
  if (devc->model > MD_1848)
1429
    {
1430
      ad_write (devc, 12, ad_read (devc, 12) | 0x40);   /* Mode2 = enabled */
1431
 
1432
      if (devc->model == MD_IWAVE)
1433
        ad_write (devc, 12, 0x6c);      /* Select codec mode 3 */
1434
 
1435
      for (i = 16; i < 32; i++)
1436
        ad_write (devc, i, init_values[i]);
1437
    }
1438
 
1439
  return 1;
1440
}
1441
 
1442
void
1443
ad1848_init (char *name, int io_base, int irq, int dma_playback, int dma_capture, int share_dma, int *osp)
1444
{
1445
  /*
1446
     * NOTE! If irq < 0, there is another driver which has allocated the IRQ
1447
     *   so that this driver doesn't need to allocate/deallocate it.
1448
     *   The actually used IRQ is ABS(irq).
1449
   */
1450
 
1451
 
1452
  int             my_dev;
1453
  char            dev_name[100];
1454
 
1455
  ad1848_info    *devc = &dev_info[nr_ad1848_devs];
1456
 
1457
  int             audio_flags = DMA_AUTOMODE;
1458
 
1459
 
1460
  request_region (devc->base, 4, devc->chip_name);
1461
 
1462
  devc->irq = (irq > 0) ? irq : 0;
1463
  devc->opened = 0;
1464
  devc->timer_ticks = 0;
1465
  devc->osp = osp;
1466
 
1467
  if (devc->model > MD_1848)
1468
    {
1469
      if (dma_capture == dma_playback || dma_capture == -1 || dma_playback == -1)
1470
        {
1471
          ad_write (devc, 9, ad_read (devc, 9) | 0x04);         /* Single DMA mode */
1472
          audio_flags &= ~DMA_DUPLEX;
1473
        }
1474
      else
1475
        {
1476
          ad_write (devc, 9, ad_read (devc, 9) & ~0x04);        /* Dual DMA mode */
1477
          audio_flags |= DMA_DUPLEX;
1478
        }
1479
 
1480
      if (devc->model == MD_1845)
1481
        ad_write (devc, 27, ad_read (devc, 27) | 0x08);         /* Alternate freq select enabled */
1482
 
1483
      if (devc->model == MD_IWAVE)
1484
        {                       /* Some magic Interwave specific initialization */
1485
          ad_write (devc, 12, 0x6c);    /* Select codec mode 3 */
1486
          ad_write (devc, 17, 0xc2);    /* Alternate feature enable */
1487
        }
1488
    }
1489
  else
1490
    {
1491
      audio_flags &= ~DMA_DUPLEX;
1492
      ad_write (devc, 9, ad_read (devc, 9) | 0x04);     /* Single DMA mode */
1493
    }
1494
 
1495
  outb (0, io_Status (devc));    /* Clear pending interrupts */
1496
 
1497
  if (name != NULL && name[0] != 0)
1498
    sprintf (dev_name,
1499
             "%s (%s)", name, devc->chip_name);
1500
  else
1501
    sprintf (dev_name,
1502
             "Generic audio codec (%s)", devc->chip_name);
1503
 
1504
  conf_printf2 (dev_name,
1505
                devc->base, devc->irq, dma_playback, dma_capture);
1506
 
1507
  if (devc->model == MD_1848)
1508
    audio_flags |= DMA_HARDSTOP;
1509
 
1510
  if ((my_dev = sound_install_audiodrv (AUDIO_DRIVER_VERSION,
1511
                                        dev_name,
1512
                                        &ad1848_audio_driver,
1513
                                        sizeof (struct audio_driver),
1514
                                        audio_flags,
1515
                                        ad_format_mask[devc->model],
1516
                                        devc,
1517
                                        dma_playback,
1518
                                        dma_capture)) < 0)
1519
    {
1520
      return;
1521
    }
1522
 
1523
  if (irq > 0)
1524
    {
1525
      irq2dev[irq] = devc->dev_no = my_dev;
1526
      if (snd_set_irq_handler (devc->irq, ad1848_interrupt,
1527
                               "SoundPort",
1528
                               devc->osp) < 0)
1529
        {
1530
          printk ("ad1848: IRQ in use\n");
1531
        }
1532
 
1533
      if (devc->model != MD_1848)
1534
        {
1535
          int             x;
1536
          unsigned char   tmp = ad_read (devc, 16);
1537
 
1538
          devc->timer_ticks = 0;
1539
 
1540
          ad_write (devc, 21, 0x00);    /* Timer MSB */
1541
          ad_write (devc, 20, 0x10);    /* Timer LSB */
1542
 
1543
          ad_write (devc, 16, tmp | 0x40);      /* Enable timer */
1544
          for (x = 0; x < 100000 && devc->timer_ticks == 0; x++);
1545
          ad_write (devc, 16, tmp & ~0x40);     /* Disable timer */
1546
 
1547
          if (devc->timer_ticks == 0)
1548
            printk ("ad1848: Interrupt test failed (IRQ%d)\n", devc->irq);
1549
          else
1550
            devc->irq_ok = 1;
1551
        }
1552
      else
1553
        devc->irq_ok = 1;       /* Couldn't test. assume it's OK */
1554
    }
1555
  else if (irq < 0)
1556
    irq2dev[-irq] = devc->dev_no = my_dev;
1557
  nr_ad1848_devs++;
1558
 
1559
#ifdef CONFIG_SEQUENCER
1560
  if (devc->model != MD_1848 && devc->model != MD_1845 && devc->irq_ok)
1561
    ad1848_tmr_install (my_dev);
1562
#endif
1563
 
1564
  if (!share_dma)
1565
    {
1566
      if (sound_alloc_dma (dma_playback, "Sound System"))
1567
        printk ("ad1848.c: Can't allocate DMA%d\n", dma_playback);
1568
 
1569
      if (dma_capture != dma_playback)
1570
        if (sound_alloc_dma (dma_capture, "Sound System (capture)"))
1571
          printk ("ad1848.c: Can't allocate DMA%d\n", dma_capture);
1572
    }
1573
 
1574
  /*
1575
     * Toggle the MCE bit. It completes the initialization phase.
1576
   */
1577
 
1578
  ad_enter_MCE (devc);          /* In case the bit was off */
1579
  ad_leave_MCE (devc);
1580
  ad1848_mixer_reset (devc);
1581
 
1582
  if (sound_install_mixer (MIXER_DRIVER_VERSION,
1583
                           dev_name,
1584
                           &ad1848_mixer_operations,
1585
                           sizeof (struct mixer_operations),
1586
                           devc) >= 0)
1587
    {
1588
      audio_devs[my_dev]->mixer_dev = num_mixers - 1;
1589
    }
1590
}
1591
 
1592
void
1593
ad1848_unload (int io_base, int irq, int dma_playback, int dma_capture, int share_dma)
1594
{
1595
  int             i, dev = 0;
1596
  ad1848_info    *devc = NULL;
1597
 
1598
  for (i = 0; devc == NULL && i < nr_ad1848_devs; i++)
1599
    if (dev_info[i].base == io_base)
1600
      {
1601
        devc = &dev_info[i];
1602
        dev = devc->dev_no;
1603
      }
1604
 
1605
  if (devc != NULL)
1606
    {
1607
      release_region (devc->base, 4);
1608
 
1609
      if (!share_dma)
1610
        {
1611
          if (irq > 0)
1612
            snd_release_irq (devc->irq);
1613
 
1614
          sound_free_dma (audio_devs[dev]->dmachan1);
1615
 
1616
          if (audio_devs[dev]->dmachan2 != audio_devs[dev]->dmachan1)
1617
            sound_free_dma (audio_devs[dev]->dmachan2);
1618
        }
1619
    }
1620
  else
1621
    printk ("ad1848: Can't find device to be unloaded. Base=%x\n",
1622
            io_base);
1623
}
1624
 
1625
void
1626
ad1848_interrupt (int irq, void *dev_id, struct pt_regs *dummy)
1627
{
1628
  unsigned char   status;
1629
  ad1848_info    *devc;
1630
  int             dev;
1631
  int             alt_stat = 0xff;
1632
 
1633
  if (irq < 0 || irq > 15)
1634
    {
1635
      dev = -1;
1636
    }
1637
  else
1638
    dev = irq2dev[irq];
1639
 
1640
  if (dev < 0 || dev >= num_audiodevs)
1641
    {
1642
      for (irq = 0; irq < 17; irq++)
1643
        if (irq2dev[irq] != -1)
1644
          break;
1645
 
1646
      if (irq > 15)
1647
        {
1648
          /* printk ("ad1848.c: Bogus interrupt %d\n", irq); */
1649
          return;
1650
        }
1651
 
1652
      dev = irq2dev[irq];
1653
      devc = (ad1848_info *) audio_devs[dev]->devc;
1654
    }
1655
  else
1656
    devc = (ad1848_info *) audio_devs[dev]->devc;
1657
 
1658
  status = inb (io_Status (devc));
1659
 
1660
  if (status == 0x80)
1661
    printk ("ad1848_interrupt: Why?\n");
1662
 
1663
  if (status & 0x01)
1664
    {
1665
 
1666
      if (devc->model != MD_1848)
1667
        alt_stat = ad_read (devc, 24);
1668
 
1669
      if (devc->opened && devc->audio_mode & PCM_ENABLE_INPUT && alt_stat & 0x20)
1670
        {
1671
          DMAbuf_inputintr (dev);
1672
        }
1673
 
1674
      if (devc->opened && devc->audio_mode & PCM_ENABLE_OUTPUT &&
1675
          alt_stat & 0x10)
1676
        {
1677
          DMAbuf_outputintr (dev, 1);
1678
        }
1679
 
1680
      if (devc->model != MD_1848 && alt_stat & 0x40)    /* Timer interrupt */
1681
        {
1682
          devc->timer_ticks++;
1683
#ifdef CONFIG_SEQUENCER
1684
          if (timer_installed == dev && devc->timer_running)
1685
            sound_timer_interrupt ();
1686
#endif
1687
        }
1688
    }
1689
 
1690
  if (devc->model != MD_1848)
1691
    ad_write (devc, 24, ad_read (devc, 24) & ~alt_stat);        /* Selective ack */
1692
  else
1693
    outb (0, io_Status (devc));  /* Clear interrupt status */
1694
}
1695
 
1696
/*
1697
 * Some extra code for the MS Sound System
1698
 */
1699
 
1700
void
1701
check_opl3 (int base, struct address_info *hw_config)
1702
{
1703
 
1704
#ifdef CONFIG_YM3812
1705
  if (check_region (base, 4))
1706
    {
1707
      printk ("\n\nopl3.c: I/O port %x already in use\n\n", base);
1708
      return;
1709
    }
1710
 
1711
  if (!opl3_detect (base, hw_config->osp))
1712
    return;
1713
 
1714
  opl3_init (base, hw_config->osp);
1715
  request_region (base, 4, "OPL3/OPL2");
1716
#endif
1717
}
1718
 
1719
#ifdef DESKPROXL
1720
/*
1721
 * Very experimental initialization sequence for the integrated sound system
1722
 * of Compaq Deskpro XL. Will be moved somewhere else in future.
1723
 */
1724
 
1725
static int
1726
init_deskpro (struct address_info *hw_config)
1727
{
1728
  unsigned char   tmp;
1729
 
1730
  if ((tmp = inb (0xc44)) == 0xff)
1731
    {
1732
      DDB (printk ("init_deskpro: Dead port 0xc44\n"));
1733
      return 0;
1734
    }
1735
 
1736
  outb (tmp | 0x04, 0xc44);     /* Select bank 1 */
1737
  if (inb (0xc44) != 0x04)
1738
    {
1739
      DDB (printk ("init_deskpro: Invalid bank1 signature in port 0xc44\n"));
1740
      return 0;
1741
    }
1742
 
1743
/*
1744
 * OK. It looks like a Deskpro so let's proceed.
1745
 */
1746
 
1747
/*
1748
 * I/O port 0xc44 Audio configuration register.
1749
 *
1750
 * bits 0xc0:   Audio revision bits
1751
 *              0x00 = Compaq Business Audio
1752
 *              0x40 = MS Sound System Compatible (reset default)
1753
 *              0x80 = Reserved
1754
 *              0xc0 = Reserved
1755
 * bit 0x20:    No Wait State Enable
1756
 *              0x00 = Disabled (reset default, DMA mode)
1757
 *              0x20 = Enabled (programmed I/O mode)
1758
 * bit 0x10:    MS Sound System Decode Enable
1759
 *              0x00 = Decoding disabled (reset default)
1760
 *              0x10 = Decoding enabled
1761
 * bit 0x08:    FM Synthesis Decode Enable
1762
 *              0x00 = Decoding Disabled (reset default)
1763
 *              0x08 = Decoding enabled
1764
 * bit 0x04     Bank select
1765
 *              0x00 = Bank 0
1766
 *              0x04 = Bank 1
1767
 * bits 0x03    MSS Base address
1768
 *              0x00 = 0x530 (reset default)
1769
 *              0x01 = 0x604
1770
 *              0x02 = 0xf40
1771
 *              0x03 = 0xe80
1772
 */
1773
 
1774
#ifdef DEBUGXL
1775
  /* Debug printing */
1776
  printk ("Port 0xc44 (before): ");
1777
  outb (tmp & ~0x04, 0xc44);
1778
  printk ("%02x ", inb (0xc44));
1779
  outb (tmp | 0x04, 0xc44);
1780
  printk ("%02x\n", inb (0xc44));
1781
#endif
1782
 
1783
  /* Set bank 1 of the register */
1784
  tmp = 0x58;                   /* MSS Mode, MSS&FM decode enabled */
1785
 
1786
  switch (hw_config->io_base)
1787
    {
1788
    case 0x530:
1789
      tmp |= 0x00;
1790
      break;
1791
    case 0x604:
1792
      tmp |= 0x01;
1793
      break;
1794
    case 0xf40:
1795
      tmp |= 0x02;
1796
      break;
1797
    case 0xe80:
1798
      tmp |= 0x03;
1799
      break;
1800
    default:
1801
      DDB (printk ("init_deskpro: Invalid MSS port %x\n",
1802
                   hw_config->io_base));
1803
      return 0;
1804
    }
1805
  outb (tmp & ~0x04, 0xc44);    /* Write to bank=0 */
1806
 
1807
#ifdef DEBUGXL
1808
  /* Debug printing */
1809
  printk ("Port 0xc44 (after): ");
1810
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1811
  printk ("%02x ", inb (0xc44));
1812
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1813
  printk ("%02x\n", inb (0xc44));
1814
#endif
1815
 
1816
/*
1817
 * I/O port 0xc45 FM Address Decode/MSS ID Register.
1818
 *
1819
 * bank=0, bits 0xfe:   FM synthesis Decode Compare bits 7:1 (default=0x88)
1820
 * bank=0, bit 0x01:    SBIC Power Control Bit
1821
 *                      0x00 = Powered up
1822
 *                      0x01 = Powered down
1823
 * bank=1, bits 0xfc:   MSS ID (default=0x40)
1824
 */
1825
 
1826
#ifdef DEBUGXL
1827
  /* Debug printing */
1828
  printk ("Port 0xc45 (before): ");
1829
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1830
  printk ("%02x ", inb (0xc45));
1831
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1832
  printk ("%02x\n", inb (0xc45));
1833
#endif
1834
 
1835
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1836
  outb (0x88, 0xc45);           /* FM base 7:0 = 0x88 */
1837
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1838
  outb (0x10, 0xc45);           /* MSS ID = 0x10 (MSS port returns 0x04) */
1839
 
1840
#ifdef DEBUGXL
1841
  /* Debug printing */
1842
  printk ("Port 0xc45 (after): ");
1843
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1844
  printk ("%02x ", inb (0xc45));
1845
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1846
  printk ("%02x\n", inb (0xc45));
1847
#endif
1848
 
1849
 
1850
/*
1851
 * I/O port 0xc46 FM Address Decode/Address ASIC Revision Register.
1852
 *
1853
 * bank=0, bits 0xff:   FM synthesis Decode Compare bits 15:8 (default=0x03)
1854
 * bank=1, bits 0xff:   Audio addressing ASIC id
1855
 */
1856
 
1857
#ifdef DEBUGXL
1858
  /* Debug printing */
1859
  printk ("Port 0xc46 (before): ");
1860
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1861
  printk ("%02x ", inb (0xc46));
1862
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1863
  printk ("%02x\n", inb (0xc46));
1864
#endif
1865
 
1866
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1867
  outb (0x03, 0xc46);           /* FM base 15:8 = 0x03 */
1868
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1869
  outb (0x11, 0xc46);           /* ASIC ID = 0x11 */
1870
 
1871
#ifdef DEBUGXL
1872
  /* Debug printing */
1873
  printk ("Port 0xc46 (after): ");
1874
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1875
  printk ("%02x ", inb (0xc46));
1876
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1877
  printk ("%02x\n", inb (0xc46));
1878
#endif
1879
 
1880
/*
1881
 * I/O port 0xc47 FM Address Decode Register.
1882
 *
1883
 * bank=0, bits 0xff:   Decode enable selection for various FM address bits
1884
 * bank=1, bits 0xff:   Reserved
1885
 */
1886
 
1887
#ifdef DEBUGXL
1888
  /* Debug printing */
1889
  printk ("Port 0xc47 (before): ");
1890
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1891
  printk ("%02x ", inb (0xc47));
1892
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1893
  printk ("%02x\n", inb (0xc47));
1894
#endif
1895
 
1896
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1897
  outb (0x7c, 0xc47);           /* FM decode enable bits = 0x7c */
1898
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1899
  outb (0x00, 0xc47);           /* Reserved bank1 = 0x00 */
1900
 
1901
#ifdef DEBUGXL
1902
  /* Debug printing */
1903
  printk ("Port 0xc47 (after): ");
1904
  outb (tmp & ~0x04, 0xc44);    /* Select bank=0 */
1905
  printk ("%02x ", inb (0xc47));
1906
  outb (tmp | 0x04, 0xc44);     /* Select bank=1 */
1907
  printk ("%02x\n", inb (0xc47));
1908
#endif
1909
 
1910
/*
1911
 * I/O port 0xc6f = Audio Disable Function Register
1912
 */
1913
 
1914
#ifdef DEBUGXL
1915
  printk ("Port 0xc6f (before) = %02x\n", inb (0xc6f));
1916
#endif
1917
 
1918
  outb (0x80, 0xc6f);
1919
 
1920
#ifdef DEBUGXL
1921
  printk ("Port 0xc6f (after) = %02x\n", inb (0xc6f));
1922
#endif
1923
 
1924
  return 1;
1925
}
1926
#endif
1927
 
1928
int
1929
probe_ms_sound (struct address_info *hw_config)
1930
{
1931
  unsigned char   tmp;
1932
 
1933
  DDB (printk ("Entered probe_ms_sound(%x, %d)\n", hw_config->io_base, hw_config->card_subtype));
1934
 
1935
  if (check_region (hw_config->io_base, 8))
1936
    {
1937
      printk ("MSS: I/O port conflict\n");
1938
      return 0;
1939
    }
1940
 
1941
  if (hw_config->card_subtype == 1)     /* Has no IRQ/DMA registers */
1942
    {
1943
      /* check_opl3(0x388, hw_config); */
1944
      return ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp);
1945
    }
1946
 
1947
#ifdef DESKPROXL
1948
  if (hw_config->card_subtype == 2)     /* Compaq Deskpro XL */
1949
    {
1950
      if (!init_deskpro (hw_config))
1951
        return 0;
1952
    }
1953
#endif
1954
 
1955
  /*
1956
     * Check if the IO port returns valid signature. The original MS Sound
1957
     * system returns 0x04 while some cards (AudioTrix Pro for example)
1958
     * return 0x00 or 0x0f.
1959
   */
1960
 
1961
  if ((tmp = inb (hw_config->io_base + 3)) == 0xff)     /* Bus float */
1962
    {
1963
      int             ret;
1964
 
1965
      DDB (printk ("I/O address is inactive (%x)\n", tmp));
1966
      if (!(ret = ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp)))
1967
        return 0;
1968
      return 1;
1969
    }
1970
  DDB (printk ("MSS signature = %x\n", tmp & 0x3f));
1971
  if ((tmp & 0x3f) != 0x04 &&
1972
      (tmp & 0x3f) != 0x0f &&
1973
      (tmp & 0x3f) != 0x00)
1974
    {
1975
      int             ret;
1976
 
1977
      DDB (printk ("No MSS signature detected on port 0x%x (0x%x)\n",
1978
                   hw_config->io_base, inb (hw_config->io_base + 3)));
1979
      DDB (printk ("Trying to detect codec anyway but IRQ/DMA may not work\n"));
1980
      if (!(ret = ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp)))
1981
        return 0;
1982
 
1983
      return 1;
1984
    }
1985
 
1986
  if (hw_config->irq > 11)
1987
    {
1988
      printk ("MSS: Bad IRQ %d\n", hw_config->irq);
1989
      return 0;
1990
    }
1991
 
1992
  if (hw_config->dma != 0 && hw_config->dma != 1 && hw_config->dma != 3)
1993
    {
1994
      printk ("MSS: Bad DMA %d\n", hw_config->dma);
1995
      return 0;
1996
    }
1997
 
1998
  /*
1999
     * Check that DMA0 is not in use with a 8 bit board.
2000
   */
2001
 
2002
  if (hw_config->dma == 0 && inb (hw_config->io_base + 3) & 0x80)
2003
    {
2004
      printk ("MSS: Can't use DMA0 with a 8 bit card/slot\n");
2005
      return 0;
2006
    }
2007
 
2008
  if (hw_config->irq > 7 && hw_config->irq != 9 && inb (hw_config->io_base + 3) & 0x80)
2009
    {
2010
      printk ("MSS: Can't use IRQ%d with a 8 bit card/slot\n", hw_config->irq);
2011
      return 0;
2012
    }
2013
 
2014
  return ad1848_detect (hw_config->io_base + 4, NULL, hw_config->osp);
2015
}
2016
 
2017
void
2018
attach_ms_sound (struct address_info *hw_config)
2019
{
2020
  static char     interrupt_bits[12] =
2021
  {
2022
    -1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
2023
  };
2024
  char            bits, dma2_bit = 0;
2025
  int             ad_flags = 0;
2026
 
2027
  static char     dma_bits[4] =
2028
  {
2029
    1, 2, 0, 3
2030
  };
2031
 
2032
  int             config_port = hw_config->io_base + 0;
2033
  int             version_port = hw_config->io_base + 3;
2034
  int             dma = hw_config->dma;
2035
  int             dma2 = hw_config->dma2;
2036
 
2037
  if (!ad1848_detect (hw_config->io_base + 4, &ad_flags, hw_config->osp))
2038
    return;
2039
 
2040
  if (hw_config->card_subtype == 1)     /* Has no IRQ/DMA registers */
2041
    {
2042
      ad1848_init ("MS Sound System", hw_config->io_base + 4,
2043
                   hw_config->irq,
2044
                   hw_config->dma,
2045
                   hw_config->dma2, 0, hw_config->osp);
2046
      request_region (hw_config->io_base, 4, "WSS config");
2047
      return;
2048
    }
2049
 
2050
  /*
2051
     * Set the IRQ and DMA addresses.
2052
   */
2053
 
2054
  bits = interrupt_bits[hw_config->irq];
2055
  if (bits == -1)
2056
    return;
2057
 
2058
  outb (bits | 0x40, config_port);
2059
  if ((inb (version_port) & 0x40) == 0)
2060
    printk ("[IRQ Conflict?]");
2061
 
2062
/*
2063
 * Handle the capture DMA channel
2064
 */
2065
 
2066
  if (ad_flags & AD_F_CS4231 && dma2 != -1 && dma2 != dma)
2067
    {
2068
      if (!((dma == 0 && dma2 == 1) ||
2069
            (dma == 1 && dma2 == 0) ||
2070
            (dma == 3 && dma2 == 0)))
2071
        {                       /* Unsupported combination. Try to swap channels */
2072
          int             tmp = dma;
2073
 
2074
          dma = dma2;
2075
          dma2 = tmp;
2076
        }
2077
 
2078
      if ((dma == 0 && dma2 == 1) ||
2079
          (dma == 1 && dma2 == 0) ||
2080
          (dma == 3 && dma2 == 0))
2081
        {
2082
          dma2_bit = 0x04;      /* Enable capture DMA */
2083
        }
2084
      else
2085
        {
2086
          printk ("MSS: Invalid capture DMA\n");
2087
          dma2 = dma;
2088
        }
2089
    }
2090
  else
2091
    dma2 = dma;
2092
 
2093
  outb (bits | dma_bits[dma] | dma2_bit, config_port);  /* Write IRQ+DMA setup */
2094
 
2095
  ad1848_init ("MSS audio codec", hw_config->io_base + 4,
2096
               hw_config->irq,
2097
               dma,
2098
               dma2, 0,
2099
               hw_config->osp);
2100
  request_region (hw_config->io_base, 4, "WSS config");
2101
}
2102
 
2103
void
2104
unload_ms_sound (struct address_info *hw_config)
2105
{
2106
  ad1848_unload (hw_config->io_base + 4,
2107
                 hw_config->irq,
2108
                 hw_config->dma,
2109
                 hw_config->dma, 0);
2110
  release_region (hw_config->io_base, 4);
2111
}
2112
 
2113
/*
2114
 * WSS compatible PnP codec support
2115
 */
2116
 
2117
int
2118
probe_pnp_ad1848 (struct address_info *hw_config)
2119
{
2120
  return ad1848_detect (hw_config->io_base, NULL, hw_config->osp);
2121
}
2122
 
2123
void
2124
attach_pnp_ad1848 (struct address_info *hw_config)
2125
{
2126
 
2127
  ad1848_init (hw_config->name, hw_config->io_base,
2128
               hw_config->irq,
2129
               hw_config->dma,
2130
               hw_config->dma2, 0, hw_config->osp);
2131
}
2132
 
2133
void
2134
unload_pnp_ad1848 (struct address_info *hw_config)
2135
{
2136
  ad1848_unload (hw_config->io_base,
2137
                 hw_config->irq,
2138
                 hw_config->dma,
2139
                 hw_config->dma2, 0);
2140
  release_region (hw_config->io_base, 4);
2141
}
2142
 
2143
#ifdef CONFIG_SEQUENCER
2144
/*
2145
 * Timer stuff (for /dev/music).
2146
 */
2147
 
2148
static unsigned int current_interval = 0;
2149
 
2150
static unsigned int
2151
ad1848_tmr_start (int dev, unsigned int usecs)
2152
{
2153
  unsigned long   flags;
2154
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2155
  unsigned long   xtal_nsecs;   /* nanoseconds per xtal oscillator tick */
2156
  unsigned long   divider;
2157
 
2158
  save_flags (flags);
2159
  cli ();
2160
 
2161
/*
2162
 * Length of the timer interval (in nanoseconds) depends on the
2163
 * selected crystal oscillator. Check this from bit 0x01 of I8.
2164
 *
2165
 * AD1845 has just one oscillator which has cycle time of 10.050 us
2166
 * (when a 24.576 MHz xtal oscillator is used).
2167
 *
2168
 * Convert requested interval to nanoseconds before computing
2169
 * the timer divider.
2170
 */
2171
 
2172
  if (devc->model == MD_1845)
2173
    xtal_nsecs = 10050;
2174
  else if (ad_read (devc, 8) & 0x01)
2175
    xtal_nsecs = 9920;
2176
  else
2177
    xtal_nsecs = 9969;
2178
 
2179
  divider = (usecs * 1000 + xtal_nsecs / 2) / xtal_nsecs;
2180
 
2181
  if (divider < 100)            /* Don't allow shorter intervals than about 1ms */
2182
    divider = 100;
2183
 
2184
  if (divider > 65535)          /* Overflow check */
2185
    divider = 65535;
2186
 
2187
  ad_write (devc, 21, (divider >> 8) & 0xff);   /* Set upper bits */
2188
  ad_write (devc, 20, divider & 0xff);  /* Set lower bits */
2189
  ad_write (devc, 16, ad_read (devc, 16) | 0x40);       /* Start the timer */
2190
  devc->timer_running = 1;
2191
  restore_flags (flags);
2192
 
2193
  return current_interval = (divider * xtal_nsecs + 500) / 1000;
2194
}
2195
 
2196
static void
2197
ad1848_tmr_reprogram (int dev)
2198
{
2199
/*
2200
 *    Audio driver has changed sampling rate so that a different xtal
2201
 *      oscillator was selected. We have to reprogram the timer rate.
2202
 */
2203
 
2204
  ad1848_tmr_start (dev, current_interval);
2205
  sound_timer_syncinterval (current_interval);
2206
}
2207
 
2208
static void
2209
ad1848_tmr_disable (int dev)
2210
{
2211
  unsigned long   flags;
2212
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2213
 
2214
  save_flags (flags);
2215
  cli ();
2216
  ad_write (devc, 16, ad_read (devc, 16) & ~0x40);
2217
  devc->timer_running = 0;
2218
  restore_flags (flags);
2219
}
2220
 
2221
static void
2222
ad1848_tmr_restart (int dev)
2223
{
2224
  unsigned long   flags;
2225
  ad1848_info    *devc = (ad1848_info *) audio_devs[dev]->devc;
2226
 
2227
  if (current_interval == 0)
2228
    return;
2229
 
2230
  save_flags (flags);
2231
  cli ();
2232
  ad_write (devc, 16, ad_read (devc, 16) | 0x40);
2233
  devc->timer_running = 1;
2234
  restore_flags (flags);
2235
}
2236
 
2237
static struct sound_lowlev_timer ad1848_tmr =
2238
{
2239
  0,
2240
  ad1848_tmr_start,
2241
  ad1848_tmr_disable,
2242
  ad1848_tmr_restart
2243
};
2244
 
2245
static int
2246
ad1848_tmr_install (int dev)
2247
{
2248
 
2249
  if (timer_installed != -1)
2250
    return 0;                    /* Don't install another timer */
2251
 
2252
  timer_installed = ad1848_tmr.dev = dev;
2253
  sound_timer_init (&ad1848_tmr, audio_devs[dev]->name);
2254
 
2255
  return 1;
2256
}
2257
#endif
2258
#endif

powered by: WebSVN 2.1.0

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