1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ALSA driver for Xilinx ML403 AC97 Controller Reference
4 * IP: opb_ac97_controller_ref_v1_00_a (EDK 8.1i)
5 * IP: opb_ac97_controller_ref_v1_00_a (EDK 9.1i)
7 * Copyright (c) by 2007 Joachim Foerster <JOFT@gmx.de>
10 /* Some notes / status of this driver:
12 * - Don't wonder about some strange implementations of things - especially the
13 * (heavy) shadowing of codec registers, with which I tried to reduce read
14 * accesses to a minimum, because after a variable amount of accesses, the AC97
15 * controller doesn't raise the register access finished bit anymore ...
17 * - Playback support seems to be pretty stable - no issues here.
18 * - Capture support "works" now, too. Overruns don't happen any longer so often.
19 * But there might still be some ...
22 #include <linux/init.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
27 #include <linux/ioport.h>
28 #include <linux/slab.h>
30 #include <linux/interrupt.h>
33 #include <linux/param.h>
34 /* jiffies, time_*() */
35 #include <linux/jiffies.h>
36 /* schedule_timeout*() */
37 #include <linux/sched.h>
39 #include <linux/spinlock.h>
40 /* struct mutex, mutex_init(), mutex_*lock() */
41 #include <linux/mutex.h>
43 /* snd_printk(), snd_printd() */
44 #include <sound/core.h>
45 #include <sound/pcm.h>
46 #include <sound/pcm_params.h>
47 #include <sound/initval.h>
48 #include <sound/ac97_codec.h>
50 #include "pcm-indirect2.h"
53 #define SND_ML403_AC97CR_DRIVER "ml403-ac97cr"
55 MODULE_AUTHOR("Joachim Foerster <JOFT@gmx.de>");
56 MODULE_DESCRIPTION("Xilinx ML403 AC97 Controller Reference");
57 MODULE_LICENSE("GPL");
58 MODULE_SUPPORTED_DEVICE("{{Xilinx,ML403 AC97 Controller Reference}}");
60 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
61 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
62 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE;
64 module_param_array(index, int, NULL, 0444);
65 MODULE_PARM_DESC(index, "Index value for ML403 AC97 Controller Reference.");
66 module_param_array(id, charp, NULL, 0444);
67 MODULE_PARM_DESC(id, "ID string for ML403 AC97 Controller Reference.");
68 module_param_array(enable, bool, NULL, 0444);
69 MODULE_PARM_DESC(enable, "Enable this ML403 AC97 Controller Reference.");
71 /* Special feature options */
72 /*#define CODEC_WRITE_CHECK_RAF*/ /* don't return after a write to a codec
73 * register, while RAF bit is not set
75 /* Debug options for code which may be removed completely in a final version */
76 #ifdef CONFIG_SND_DEBUG
77 /*#define CODEC_STAT*/ /* turn on some minimal "statistics"
78 * about codec register usage
80 #define SND_PCM_INDIRECT2_STAT /* turn on some "statistics" about the
81 * process of copying bytes from the
82 * intermediate buffer to the hardware
83 * fifo and the other way round
87 /* Definition of a "level/facility dependent" printk(); may be removed
88 * completely in a final version
91 #ifdef CONFIG_SND_DEBUG
92 /* "facilities" for PDEBUG */
93 #define UNKNOWN (1<<0)
94 #define CODEC_SUCCESS (1<<1)
95 #define CODEC_FAKE (1<<2)
96 #define INIT_INFO (1<<3)
97 #define INIT_FAILURE (1<<4)
98 #define WORK_INFO (1<<5)
99 #define WORK_FAILURE (1<<6)
101 #define PDEBUG_FACILITIES (UNKNOWN | INIT_FAILURE | WORK_FAILURE)
103 #define PDEBUG(fac, fmt, args...) do { \
104 if (fac & PDEBUG_FACILITIES) \
105 snd_printd(KERN_DEBUG SND_ML403_AC97CR_DRIVER ": " \
109 #define PDEBUG(fac, fmt, args...) /* nothing */
114 /* Defines for "waits"/timeouts (portions of HZ=250 on arch/ppc by default) */
115 #define CODEC_TIMEOUT_ON_INIT 5 /* timeout for checking for codec
116 * readiness (after insmod)
118 #ifndef CODEC_WRITE_CHECK_RAF
119 #define CODEC_WAIT_AFTER_WRITE 100 /* general, static wait after a write
120 * access to a codec register, may be
121 * 0 to completely remove wait
124 #define CODEC_TIMEOUT_AFTER_WRITE 5 /* timeout after a write access to a
125 * codec register, if RAF bit is used
128 #define CODEC_TIMEOUT_AFTER_READ 5 /* timeout after a read access to a
129 * codec register (checking RAF bit)
132 /* Infrastructure for codec register shadowing */
133 #define LM4550_REG_OK (1<<0) /* register exists */
134 #define LM4550_REG_DONEREAD (1<<1) /* read register once, value should be
135 * the same currently in the register
137 #define LM4550_REG_NOSAVE (1<<2) /* values written to this register will
138 * not be saved in the register
140 #define LM4550_REG_NOSHADOW (1<<3) /* don't do register shadowing, use plain
143 #define LM4550_REG_READONLY (1<<4) /* register is read only */
144 #define LM4550_REG_FAKEPROBE (1<<5) /* fake write _and_ read actions during
147 #define LM4550_REG_FAKEREAD (1<<6) /* fake read access, always return
150 #define LM4550_REG_ALLFAKE (LM4550_REG_FAKEREAD | LM4550_REG_FAKEPROBE)
159 struct lm4550_reg lm4550_regfile[64] = {
160 [AC97_RESET / 2] = {.flag = LM4550_REG_OK \
161 | LM4550_REG_NOSAVE \
162 | LM4550_REG_FAKEREAD,
164 [AC97_MASTER / 2] = {.flag = LM4550_REG_OK
165 | LM4550_REG_FAKEPROBE,
168 [AC97_HEADPHONE / 2] = {.flag = LM4550_REG_OK \
169 | LM4550_REG_FAKEPROBE,
172 [AC97_MASTER_MONO / 2] = {.flag = LM4550_REG_OK \
173 | LM4550_REG_FAKEPROBE,
176 [AC97_PC_BEEP / 2] = {.flag = LM4550_REG_OK \
177 | LM4550_REG_FAKEPROBE,
180 [AC97_PHONE / 2] = {.flag = LM4550_REG_OK \
181 | LM4550_REG_FAKEPROBE,
184 [AC97_MIC / 2] = {.flag = LM4550_REG_OK \
185 | LM4550_REG_FAKEPROBE,
188 [AC97_LINE / 2] = {.flag = LM4550_REG_OK \
189 | LM4550_REG_FAKEPROBE,
192 [AC97_CD / 2] = {.flag = LM4550_REG_OK \
193 | LM4550_REG_FAKEPROBE,
196 [AC97_VIDEO / 2] = {.flag = LM4550_REG_OK \
197 | LM4550_REG_FAKEPROBE,
200 [AC97_AUX / 2] = {.flag = LM4550_REG_OK \
201 | LM4550_REG_FAKEPROBE,
204 [AC97_PCM / 2] = {.flag = LM4550_REG_OK \
205 | LM4550_REG_FAKEPROBE,
208 [AC97_REC_SEL / 2] = {.flag = LM4550_REG_OK \
209 | LM4550_REG_FAKEPROBE,
212 [AC97_REC_GAIN / 2] = {.flag = LM4550_REG_OK \
213 | LM4550_REG_FAKEPROBE,
216 [AC97_GENERAL_PURPOSE / 2] = {.flag = LM4550_REG_OK \
217 | LM4550_REG_FAKEPROBE,
220 [AC97_3D_CONTROL / 2] = {.flag = LM4550_REG_OK \
221 | LM4550_REG_FAKEREAD \
222 | LM4550_REG_READONLY,
224 [AC97_POWERDOWN / 2] = {.flag = LM4550_REG_OK \
225 | LM4550_REG_NOSHADOW \
228 /* may not write ones to
229 * REF/ANL/DAC/ADC bits
232 [AC97_EXTENDED_ID / 2] = {.flag = LM4550_REG_OK \
233 | LM4550_REG_FAKEREAD \
234 | LM4550_REG_READONLY,
235 .def = 0x0201}, /* primary codec */
236 [AC97_EXTENDED_STATUS / 2] = {.flag = LM4550_REG_OK \
237 | LM4550_REG_NOSHADOW \
240 [AC97_PCM_FRONT_DAC_RATE / 2] = {.flag = LM4550_REG_OK \
241 | LM4550_REG_FAKEPROBE,
244 [AC97_PCM_LR_ADC_RATE / 2] = {.flag = LM4550_REG_OK \
245 | LM4550_REG_FAKEPROBE,
248 [AC97_VENDOR_ID1 / 2] = {.flag = LM4550_REG_OK \
249 | LM4550_REG_READONLY \
250 | LM4550_REG_FAKEREAD,
252 [AC97_VENDOR_ID2 / 2] = {.flag = LM4550_REG_OK \
253 | LM4550_REG_READONLY \
254 | LM4550_REG_FAKEREAD,
258 #define LM4550_RF_OK(reg) (lm4550_regfile[reg / 2].flag & LM4550_REG_OK)
260 static void lm4550_regfile_init(void)
263 for (i = 0; i < 64; i++)
264 if (lm4550_regfile[i].flag & LM4550_REG_FAKEPROBE)
265 lm4550_regfile[i].value = lm4550_regfile[i].def;
268 static void lm4550_regfile_write_values_after_init(struct snd_ac97 *ac97)
271 for (i = 0; i < 64; i++)
272 if ((lm4550_regfile[i].flag & LM4550_REG_FAKEPROBE) &&
273 (lm4550_regfile[i].value != lm4550_regfile[i].def)) {
274 PDEBUG(CODEC_FAKE, "lm4550_regfile_write_values_after_"
275 "init(): reg=0x%x value=0x%x / %d is different "
276 "from def=0x%x / %d\n",
277 i, lm4550_regfile[i].value,
278 lm4550_regfile[i].value, lm4550_regfile[i].def,
279 lm4550_regfile[i].def);
280 snd_ac97_write(ac97, i * 2, lm4550_regfile[i].value);
281 lm4550_regfile[i].flag |= LM4550_REG_DONEREAD;
286 /* direct registers */
287 #define CR_REG(ml403_ac97cr, x) ((ml403_ac97cr)->port + CR_REG_##x)
289 #define CR_REG_PLAYFIFO 0x00
290 #define CR_PLAYDATA(a) ((a) & 0xFFFF)
292 #define CR_REG_RECFIFO 0x04
293 #define CR_RECDATA(a) ((a) & 0xFFFF)
295 #define CR_REG_STATUS 0x08
296 #define CR_RECOVER (1<<7)
297 #define CR_PLAYUNDER (1<<6)
298 #define CR_CODECREADY (1<<5)
299 #define CR_RAF (1<<4)
300 #define CR_RECEMPTY (1<<3)
301 #define CR_RECFULL (1<<2)
302 #define CR_PLAYHALF (1<<1)
303 #define CR_PLAYFULL (1<<0)
305 #define CR_REG_RESETFIFO 0x0C
306 #define CR_RECRESET (1<<1)
307 #define CR_PLAYRESET (1<<0)
309 #define CR_REG_CODEC_ADDR 0x10
311 * #define CR_CODEC_ADDR(a) ((a) << 1)
312 * #define CR_CODEC_READ (1<<0)
313 * #define CR_CODEC_WRITE (0<<0)
315 /* RefDesign example says: */
316 #define CR_CODEC_ADDR(a) ((a) << 0)
317 #define CR_CODEC_READ (1<<7)
318 #define CR_CODEC_WRITE (0<<7)
320 #define CR_REG_CODEC_DATAREAD 0x14
321 #define CR_CODEC_DATAREAD(v) ((v) & 0xFFFF)
323 #define CR_REG_CODEC_DATAWRITE 0x18
324 #define CR_CODEC_DATAWRITE(v) ((v) & 0xFFFF)
326 #define CR_FIFO_SIZE 32
328 struct snd_ml403_ac97cr {
329 /* lock for access to (controller) registers */
331 /* mutex for the whole sequence of accesses to (controller) registers
332 * which affect codec registers
334 struct mutex cdc_mutex;
336 int irq; /* for playback */
337 int enable_irq; /* for playback */
340 int enable_capture_irq;
342 struct resource *res_port;
345 struct snd_ac97 *ac97;
352 struct platform_device *pfdev;
353 struct snd_card *card;
355 struct snd_pcm_substream *playback_substream;
356 struct snd_pcm_substream *capture_substream;
358 struct snd_pcm_indirect2 ind_rec; /* for playback */
359 struct snd_pcm_indirect2 capture_ind2_rec;
362 static const struct snd_pcm_hardware snd_ml403_ac97cr_playback = {
363 .info = (SNDRV_PCM_INFO_MMAP |
364 SNDRV_PCM_INFO_INTERLEAVED |
365 SNDRV_PCM_INFO_MMAP_VALID),
366 .formats = SNDRV_PCM_FMTBIT_S16_BE,
367 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
368 SNDRV_PCM_RATE_8000_48000),
373 .buffer_bytes_max = (128*1024),
374 .period_bytes_min = CR_FIFO_SIZE/2,
375 .period_bytes_max = (64*1024),
377 .periods_max = (128*1024)/(CR_FIFO_SIZE/2),
381 static const struct snd_pcm_hardware snd_ml403_ac97cr_capture = {
382 .info = (SNDRV_PCM_INFO_MMAP |
383 SNDRV_PCM_INFO_INTERLEAVED |
384 SNDRV_PCM_INFO_MMAP_VALID),
385 .formats = SNDRV_PCM_FMTBIT_S16_BE,
386 .rates = (SNDRV_PCM_RATE_CONTINUOUS |
387 SNDRV_PCM_RATE_8000_48000),
392 .buffer_bytes_max = (128*1024),
393 .period_bytes_min = CR_FIFO_SIZE/2,
394 .period_bytes_max = (64*1024),
396 .periods_max = (128*1024)/(CR_FIFO_SIZE/2),
401 snd_ml403_ac97cr_playback_ind2_zero(struct snd_pcm_substream *substream,
402 struct snd_pcm_indirect2 *rec)
404 struct snd_ml403_ac97cr *ml403_ac97cr;
405 int copied_words = 0;
408 ml403_ac97cr = snd_pcm_substream_chip(substream);
410 spin_lock(&ml403_ac97cr->reg_lock);
411 while ((full = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
412 CR_PLAYFULL)) != CR_PLAYFULL) {
413 out_be32(CR_REG(ml403_ac97cr, PLAYFIFO), 0);
417 spin_unlock(&ml403_ac97cr->reg_lock);
419 return (size_t) (copied_words * 2);
423 snd_ml403_ac97cr_playback_ind2_copy(struct snd_pcm_substream *substream,
424 struct snd_pcm_indirect2 *rec,
427 struct snd_ml403_ac97cr *ml403_ac97cr;
429 int copied_words = 0;
432 ml403_ac97cr = snd_pcm_substream_chip(substream);
433 src = (u16 *)(substream->runtime->dma_area + rec->sw_data);
435 spin_lock(&ml403_ac97cr->reg_lock);
436 while (((full = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
437 CR_PLAYFULL)) != CR_PLAYFULL) && (bytes > 1)) {
438 out_be32(CR_REG(ml403_ac97cr, PLAYFIFO),
439 CR_PLAYDATA(src[copied_words]));
443 if (full != CR_PLAYFULL)
447 spin_unlock(&ml403_ac97cr->reg_lock);
449 return (size_t) (copied_words * 2);
453 snd_ml403_ac97cr_capture_ind2_null(struct snd_pcm_substream *substream,
454 struct snd_pcm_indirect2 *rec)
456 struct snd_ml403_ac97cr *ml403_ac97cr;
457 int copied_words = 0;
460 ml403_ac97cr = snd_pcm_substream_chip(substream);
462 spin_lock(&ml403_ac97cr->reg_lock);
463 while ((empty = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
464 CR_RECEMPTY)) != CR_RECEMPTY) {
467 trash = CR_RECDATA(in_be32(CR_REG(ml403_ac97cr, RECFIFO)));
468 /* Hmmmm, really necessary? Don't want call to in_be32()
469 * to be optimised away!
475 spin_unlock(&ml403_ac97cr->reg_lock);
477 return (size_t) (copied_words * 2);
481 snd_ml403_ac97cr_capture_ind2_copy(struct snd_pcm_substream *substream,
482 struct snd_pcm_indirect2 *rec, size_t bytes)
484 struct snd_ml403_ac97cr *ml403_ac97cr;
486 int copied_words = 0;
489 ml403_ac97cr = snd_pcm_substream_chip(substream);
490 dst = (u16 *)(substream->runtime->dma_area + rec->sw_data);
492 spin_lock(&ml403_ac97cr->reg_lock);
493 while (((empty = (in_be32(CR_REG(ml403_ac97cr, STATUS)) &
494 CR_RECEMPTY)) != CR_RECEMPTY) && (bytes > 1)) {
495 dst[copied_words] = CR_RECDATA(in_be32(CR_REG(ml403_ac97cr,
500 if (empty != CR_RECEMPTY)
504 spin_unlock(&ml403_ac97cr->reg_lock);
506 return (size_t) (copied_words * 2);
509 static snd_pcm_uframes_t
510 snd_ml403_ac97cr_pcm_pointer(struct snd_pcm_substream *substream)
512 struct snd_ml403_ac97cr *ml403_ac97cr;
513 struct snd_pcm_indirect2 *ind2_rec = NULL;
515 ml403_ac97cr = snd_pcm_substream_chip(substream);
517 if (substream == ml403_ac97cr->playback_substream)
518 ind2_rec = &ml403_ac97cr->ind_rec;
519 if (substream == ml403_ac97cr->capture_substream)
520 ind2_rec = &ml403_ac97cr->capture_ind2_rec;
522 if (ind2_rec != NULL)
523 return snd_pcm_indirect2_pointer(substream, ind2_rec);
524 return (snd_pcm_uframes_t) 0;
528 snd_ml403_ac97cr_pcm_playback_trigger(struct snd_pcm_substream *substream,
531 struct snd_ml403_ac97cr *ml403_ac97cr;
534 ml403_ac97cr = snd_pcm_substream_chip(substream);
537 case SNDRV_PCM_TRIGGER_START:
538 PDEBUG(WORK_INFO, "trigger(playback): START\n");
539 ml403_ac97cr->ind_rec.hw_ready = 1;
541 /* clear play FIFO */
542 out_be32(CR_REG(ml403_ac97cr, RESETFIFO), CR_PLAYRESET);
544 /* enable play irq */
545 ml403_ac97cr->enable_irq = 1;
546 enable_irq(ml403_ac97cr->irq);
548 case SNDRV_PCM_TRIGGER_STOP:
549 PDEBUG(WORK_INFO, "trigger(playback): STOP\n");
550 ml403_ac97cr->ind_rec.hw_ready = 0;
551 #ifdef SND_PCM_INDIRECT2_STAT
552 snd_pcm_indirect2_stat(substream, &ml403_ac97cr->ind_rec);
554 /* disable play irq */
555 disable_irq_nosync(ml403_ac97cr->irq);
556 ml403_ac97cr->enable_irq = 0;
562 PDEBUG(WORK_INFO, "trigger(playback): (done)\n");
567 snd_ml403_ac97cr_pcm_capture_trigger(struct snd_pcm_substream *substream,
570 struct snd_ml403_ac97cr *ml403_ac97cr;
573 ml403_ac97cr = snd_pcm_substream_chip(substream);
576 case SNDRV_PCM_TRIGGER_START:
577 PDEBUG(WORK_INFO, "trigger(capture): START\n");
578 ml403_ac97cr->capture_ind2_rec.hw_ready = 0;
580 /* clear record FIFO */
581 out_be32(CR_REG(ml403_ac97cr, RESETFIFO), CR_RECRESET);
583 /* enable record irq */
584 ml403_ac97cr->enable_capture_irq = 1;
585 enable_irq(ml403_ac97cr->capture_irq);
587 case SNDRV_PCM_TRIGGER_STOP:
588 PDEBUG(WORK_INFO, "trigger(capture): STOP\n");
589 ml403_ac97cr->capture_ind2_rec.hw_ready = 0;
590 #ifdef SND_PCM_INDIRECT2_STAT
591 snd_pcm_indirect2_stat(substream,
592 &ml403_ac97cr->capture_ind2_rec);
594 /* disable capture irq */
595 disable_irq_nosync(ml403_ac97cr->capture_irq);
596 ml403_ac97cr->enable_capture_irq = 0;
602 PDEBUG(WORK_INFO, "trigger(capture): (done)\n");
607 snd_ml403_ac97cr_pcm_playback_prepare(struct snd_pcm_substream *substream)
609 struct snd_ml403_ac97cr *ml403_ac97cr;
610 struct snd_pcm_runtime *runtime;
612 ml403_ac97cr = snd_pcm_substream_chip(substream);
613 runtime = substream->runtime;
616 "prepare(): period_bytes=%d, minperiod_bytes=%d\n",
617 snd_pcm_lib_period_bytes(substream), CR_FIFO_SIZE / 2);
619 /* set sampling rate */
620 snd_ac97_set_rate(ml403_ac97cr->ac97, AC97_PCM_FRONT_DAC_RATE,
622 PDEBUG(WORK_INFO, "prepare(): rate=%d\n", runtime->rate);
624 /* init struct for intermediate buffer */
625 memset(&ml403_ac97cr->ind_rec, 0,
626 sizeof(struct snd_pcm_indirect2));
627 ml403_ac97cr->ind_rec.hw_buffer_size = CR_FIFO_SIZE;
628 ml403_ac97cr->ind_rec.sw_buffer_size =
629 snd_pcm_lib_buffer_bytes(substream);
630 ml403_ac97cr->ind_rec.min_periods = -1;
631 ml403_ac97cr->ind_rec.min_multiple =
632 snd_pcm_lib_period_bytes(substream) / (CR_FIFO_SIZE / 2);
633 PDEBUG(WORK_INFO, "prepare(): hw_buffer_size=%d, "
634 "sw_buffer_size=%d, min_multiple=%d\n",
635 CR_FIFO_SIZE, ml403_ac97cr->ind_rec.sw_buffer_size,
636 ml403_ac97cr->ind_rec.min_multiple);
641 snd_ml403_ac97cr_pcm_capture_prepare(struct snd_pcm_substream *substream)
643 struct snd_ml403_ac97cr *ml403_ac97cr;
644 struct snd_pcm_runtime *runtime;
646 ml403_ac97cr = snd_pcm_substream_chip(substream);
647 runtime = substream->runtime;
650 "prepare(capture): period_bytes=%d, minperiod_bytes=%d\n",
651 snd_pcm_lib_period_bytes(substream), CR_FIFO_SIZE / 2);
653 /* set sampling rate */
654 snd_ac97_set_rate(ml403_ac97cr->ac97, AC97_PCM_LR_ADC_RATE,
656 PDEBUG(WORK_INFO, "prepare(capture): rate=%d\n", runtime->rate);
658 /* init struct for intermediate buffer */
659 memset(&ml403_ac97cr->capture_ind2_rec, 0,
660 sizeof(struct snd_pcm_indirect2));
661 ml403_ac97cr->capture_ind2_rec.hw_buffer_size = CR_FIFO_SIZE;
662 ml403_ac97cr->capture_ind2_rec.sw_buffer_size =
663 snd_pcm_lib_buffer_bytes(substream);
664 ml403_ac97cr->capture_ind2_rec.min_multiple =
665 snd_pcm_lib_period_bytes(substream) / (CR_FIFO_SIZE / 2);
666 PDEBUG(WORK_INFO, "prepare(capture): hw_buffer_size=%d, "
667 "sw_buffer_size=%d, min_multiple=%d\n", CR_FIFO_SIZE,
668 ml403_ac97cr->capture_ind2_rec.sw_buffer_size,
669 ml403_ac97cr->capture_ind2_rec.min_multiple);
673 static int snd_ml403_ac97cr_playback_open(struct snd_pcm_substream *substream)
675 struct snd_ml403_ac97cr *ml403_ac97cr;
676 struct snd_pcm_runtime *runtime;
678 ml403_ac97cr = snd_pcm_substream_chip(substream);
679 runtime = substream->runtime;
681 PDEBUG(WORK_INFO, "open(playback)\n");
682 ml403_ac97cr->playback_substream = substream;
683 runtime->hw = snd_ml403_ac97cr_playback;
685 snd_pcm_hw_constraint_step(runtime, 0,
686 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
691 static int snd_ml403_ac97cr_capture_open(struct snd_pcm_substream *substream)
693 struct snd_ml403_ac97cr *ml403_ac97cr;
694 struct snd_pcm_runtime *runtime;
696 ml403_ac97cr = snd_pcm_substream_chip(substream);
697 runtime = substream->runtime;
699 PDEBUG(WORK_INFO, "open(capture)\n");
700 ml403_ac97cr->capture_substream = substream;
701 runtime->hw = snd_ml403_ac97cr_capture;
703 snd_pcm_hw_constraint_step(runtime, 0,
704 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
709 static int snd_ml403_ac97cr_playback_close(struct snd_pcm_substream *substream)
711 struct snd_ml403_ac97cr *ml403_ac97cr;
713 ml403_ac97cr = snd_pcm_substream_chip(substream);
715 PDEBUG(WORK_INFO, "close(playback)\n");
716 ml403_ac97cr->playback_substream = NULL;
720 static int snd_ml403_ac97cr_capture_close(struct snd_pcm_substream *substream)
722 struct snd_ml403_ac97cr *ml403_ac97cr;
724 ml403_ac97cr = snd_pcm_substream_chip(substream);
726 PDEBUG(WORK_INFO, "close(capture)\n");
727 ml403_ac97cr->capture_substream = NULL;
731 static const struct snd_pcm_ops snd_ml403_ac97cr_playback_ops = {
732 .open = snd_ml403_ac97cr_playback_open,
733 .close = snd_ml403_ac97cr_playback_close,
734 .prepare = snd_ml403_ac97cr_pcm_playback_prepare,
735 .trigger = snd_ml403_ac97cr_pcm_playback_trigger,
736 .pointer = snd_ml403_ac97cr_pcm_pointer,
739 static const struct snd_pcm_ops snd_ml403_ac97cr_capture_ops = {
740 .open = snd_ml403_ac97cr_capture_open,
741 .close = snd_ml403_ac97cr_capture_close,
742 .prepare = snd_ml403_ac97cr_pcm_capture_prepare,
743 .trigger = snd_ml403_ac97cr_pcm_capture_trigger,
744 .pointer = snd_ml403_ac97cr_pcm_pointer,
747 static irqreturn_t snd_ml403_ac97cr_irq(int irq, void *dev_id)
749 struct snd_ml403_ac97cr *ml403_ac97cr;
750 struct platform_device *pfdev;
753 ml403_ac97cr = (struct snd_ml403_ac97cr *)dev_id;
754 if (ml403_ac97cr == NULL)
757 pfdev = ml403_ac97cr->pfdev;
759 /* playback interrupt */
760 cmp_irq = platform_get_irq(pfdev, 0);
761 if (irq == cmp_irq) {
762 if (ml403_ac97cr->enable_irq)
763 snd_pcm_indirect2_playback_interrupt(
764 ml403_ac97cr->playback_substream,
765 &ml403_ac97cr->ind_rec,
766 snd_ml403_ac97cr_playback_ind2_copy,
767 snd_ml403_ac97cr_playback_ind2_zero);
771 /* record interrupt */
772 cmp_irq = platform_get_irq(pfdev, 1);
773 if (irq == cmp_irq) {
774 if (ml403_ac97cr->enable_capture_irq)
775 snd_pcm_indirect2_capture_interrupt(
776 ml403_ac97cr->capture_substream,
777 &ml403_ac97cr->capture_ind2_rec,
778 snd_ml403_ac97cr_capture_ind2_copy,
779 snd_ml403_ac97cr_capture_ind2_null);
788 PDEBUG(INIT_INFO, "irq(): irq %d is meant to be disabled! So, now try "
789 "to disable it _really_!\n", irq);
790 disable_irq_nosync(irq);
794 static unsigned short
795 snd_ml403_ac97cr_codec_read(struct snd_ac97 *ac97, unsigned short reg)
797 struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
802 unsigned long end_time;
805 if (!LM4550_RF_OK(reg)) {
806 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
807 "access to unknown/unused codec register 0x%x "
811 /* check if we can fake/answer this access from our shadow register */
812 if ((lm4550_regfile[reg / 2].flag &
813 (LM4550_REG_DONEREAD | LM4550_REG_ALLFAKE)) &&
814 !(lm4550_regfile[reg / 2].flag & LM4550_REG_NOSHADOW)) {
815 if (lm4550_regfile[reg / 2].flag & LM4550_REG_FAKEREAD) {
816 PDEBUG(CODEC_FAKE, "codec_read(): faking read from "
817 "reg=0x%x, val=0x%x / %d\n",
818 reg, lm4550_regfile[reg / 2].def,
819 lm4550_regfile[reg / 2].def);
820 return lm4550_regfile[reg / 2].def;
821 } else if ((lm4550_regfile[reg / 2].flag &
822 LM4550_REG_FAKEPROBE) &&
823 ml403_ac97cr->ac97_fake) {
824 PDEBUG(CODEC_FAKE, "codec_read(): faking read from "
825 "reg=0x%x, val=0x%x / %d (probe)\n",
826 reg, lm4550_regfile[reg / 2].value,
827 lm4550_regfile[reg / 2].value);
828 return lm4550_regfile[reg / 2].value;
831 PDEBUG(CODEC_FAKE, "codec_read(): read access "
832 "answered by shadow register 0x%x (value=0x%x "
833 "/ %d) (cw=%d cr=%d)\n",
834 reg, lm4550_regfile[reg / 2].value,
835 lm4550_regfile[reg / 2].value,
836 ml403_ac97cr->ac97_write,
837 ml403_ac97cr->ac97_read);
839 PDEBUG(CODEC_FAKE, "codec_read(): read access "
840 "answered by shadow register 0x%x (value=0x%x "
842 reg, lm4550_regfile[reg / 2].value,
843 lm4550_regfile[reg / 2].value);
845 return lm4550_regfile[reg / 2].value;
848 /* if we are here, we _have_ to access the codec really, no faking */
849 if (mutex_lock_interruptible(&ml403_ac97cr->cdc_mutex) != 0)
852 ml403_ac97cr->ac97_read++;
854 spin_lock(&ml403_ac97cr->reg_lock);
855 out_be32(CR_REG(ml403_ac97cr, CODEC_ADDR),
856 CR_CODEC_ADDR(reg) | CR_CODEC_READ);
857 spin_unlock(&ml403_ac97cr->reg_lock);
858 end_time = jiffies + (HZ / CODEC_TIMEOUT_AFTER_READ);
860 spin_lock(&ml403_ac97cr->reg_lock);
863 stat = in_be32(CR_REG(ml403_ac97cr, STATUS));
864 if ((stat & CR_RAF) == CR_RAF) {
865 value = CR_CODEC_DATAREAD(
866 in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
867 PDEBUG(CODEC_SUCCESS, "codec_read(): (done) reg=0x%x, "
868 "value=0x%x / %d (STATUS=0x%x)\n",
869 reg, value, value, stat);
871 if ((in_be32(CR_REG(ml403_ac97cr, STATUS)) &
873 value = CR_CODEC_DATAREAD(
874 in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
875 PDEBUG(CODEC_SUCCESS, "codec_read(): (done) "
876 "reg=0x%x, value=0x%x / %d\n",
879 lm4550_regfile[reg / 2].value = value;
880 lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
881 spin_unlock(&ml403_ac97cr->reg_lock);
882 mutex_unlock(&ml403_ac97cr->cdc_mutex);
885 spin_unlock(&ml403_ac97cr->reg_lock);
886 schedule_timeout_uninterruptible(1);
887 } while (time_after(end_time, jiffies));
888 /* read the DATAREAD register anyway, see comment below */
889 spin_lock(&ml403_ac97cr->reg_lock);
891 CR_CODEC_DATAREAD(in_be32(CR_REG(ml403_ac97cr, CODEC_DATAREAD)));
892 spin_unlock(&ml403_ac97cr->reg_lock);
894 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
895 "timeout while codec read! "
896 "(reg=0x%x, last STATUS=0x%x, DATAREAD=0x%x / %d, %d) "
898 reg, stat, value, value, rafaccess,
899 ml403_ac97cr->ac97_write, ml403_ac97cr->ac97_read);
901 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
902 "timeout while codec read! "
903 "(reg=0x%x, DATAREAD=0x%x / %d)\n",
906 /* BUG: This is PURE speculation! But after _most_ read timeouts the
907 * value in the register is ok!
909 lm4550_regfile[reg / 2].value = value;
910 lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
911 mutex_unlock(&ml403_ac97cr->cdc_mutex);
916 snd_ml403_ac97cr_codec_write(struct snd_ac97 *ac97, unsigned short reg,
919 struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
925 #ifdef CODEC_WRITE_CHECK_RAF
926 unsigned long end_time;
929 if (!LM4550_RF_OK(reg)) {
930 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
931 "access to unknown/unused codec register 0x%x "
935 if (lm4550_regfile[reg / 2].flag & LM4550_REG_READONLY) {
936 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
937 "write access to read only codec register 0x%x "
941 if ((val & lm4550_regfile[reg / 2].wmask) != val) {
942 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
943 "write access to codec register 0x%x "
944 "with bad value 0x%x / %d!\n",
946 val = val & lm4550_regfile[reg / 2].wmask;
948 if (((lm4550_regfile[reg / 2].flag & LM4550_REG_FAKEPROBE) &&
949 ml403_ac97cr->ac97_fake) &&
950 !(lm4550_regfile[reg / 2].flag & LM4550_REG_NOSHADOW)) {
951 PDEBUG(CODEC_FAKE, "codec_write(): faking write to reg=0x%x, "
952 "val=0x%x / %d\n", reg, val, val);
953 lm4550_regfile[reg / 2].value = (val &
954 lm4550_regfile[reg / 2].wmask);
957 if (mutex_lock_interruptible(&ml403_ac97cr->cdc_mutex) != 0)
960 ml403_ac97cr->ac97_write++;
962 spin_lock(&ml403_ac97cr->reg_lock);
963 out_be32(CR_REG(ml403_ac97cr, CODEC_DATAWRITE),
964 CR_CODEC_DATAWRITE(val));
965 out_be32(CR_REG(ml403_ac97cr, CODEC_ADDR),
966 CR_CODEC_ADDR(reg) | CR_CODEC_WRITE);
967 spin_unlock(&ml403_ac97cr->reg_lock);
968 #ifdef CODEC_WRITE_CHECK_RAF
969 /* check CR_CODEC_RAF bit to see if write access to register is done;
970 * loop until bit is set or timeout happens
972 end_time = jiffies + HZ / CODEC_TIMEOUT_AFTER_WRITE;
974 spin_lock(&ml403_ac97cr->reg_lock);
977 stat = in_be32(CR_REG(ml403_ac97cr, STATUS))
978 if ((stat & CR_RAF) == CR_RAF) {
980 if ((in_be32(CR_REG(ml403_ac97cr, STATUS)) &
983 PDEBUG(CODEC_SUCCESS, "codec_write(): (done) "
984 "reg=0x%x, value=%d / 0x%x\n",
986 if (!(lm4550_regfile[reg / 2].flag &
987 LM4550_REG_NOSHADOW) &&
988 !(lm4550_regfile[reg / 2].flag &
990 lm4550_regfile[reg / 2].value = val;
991 lm4550_regfile[reg / 2].flag |= LM4550_REG_DONEREAD;
992 spin_unlock(&ml403_ac97cr->reg_lock);
993 mutex_unlock(&ml403_ac97cr->cdc_mutex);
996 spin_unlock(&ml403_ac97cr->reg_lock);
997 schedule_timeout_uninterruptible(1);
998 } while (time_after(end_time, jiffies));
1000 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
1001 "timeout while codec write "
1002 "(reg=0x%x, val=0x%x / %d, last STATUS=0x%x, %d) "
1004 reg, val, val, stat, rafaccess, ml403_ac97cr->ac97_write,
1005 ml403_ac97cr->ac97_read);
1007 snd_printk(KERN_WARNING SND_ML403_AC97CR_DRIVER ": "
1008 "timeout while codec write (reg=0x%x, val=0x%x / %d)\n",
1011 #else /* CODEC_WRITE_CHECK_RAF */
1012 #if CODEC_WAIT_AFTER_WRITE > 0
1013 /* officially, in AC97 spec there is no possibility for a AC97
1014 * controller to determine, if write access is done or not - so: How
1015 * is Xilinx able to provide a RAF bit for write access?
1016 * => very strange, thus just don't check RAF bit (compare with
1017 * Xilinx's example app in EDK 8.1i) and wait
1019 schedule_timeout_uninterruptible(HZ / CODEC_WAIT_AFTER_WRITE);
1021 PDEBUG(CODEC_SUCCESS, "codec_write(): (done) "
1022 "reg=0x%x, value=%d / 0x%x (no RAF check)\n",
1025 mutex_unlock(&ml403_ac97cr->cdc_mutex);
1030 snd_ml403_ac97cr_chip_init(struct snd_ml403_ac97cr *ml403_ac97cr)
1032 unsigned long end_time;
1033 PDEBUG(INIT_INFO, "chip_init():\n");
1034 end_time = jiffies + HZ / CODEC_TIMEOUT_ON_INIT;
1036 if (in_be32(CR_REG(ml403_ac97cr, STATUS)) & CR_CODECREADY) {
1037 /* clear both hardware FIFOs */
1038 out_be32(CR_REG(ml403_ac97cr, RESETFIFO),
1039 CR_RECRESET | CR_PLAYRESET);
1040 PDEBUG(INIT_INFO, "chip_init(): (done)\n");
1043 schedule_timeout_uninterruptible(1);
1044 } while (time_after(end_time, jiffies));
1045 snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
1046 "timeout while waiting for codec, "
1051 static int snd_ml403_ac97cr_free(struct snd_ml403_ac97cr *ml403_ac97cr)
1053 PDEBUG(INIT_INFO, "free():\n");
1055 if (ml403_ac97cr->irq >= 0)
1056 free_irq(ml403_ac97cr->irq, ml403_ac97cr);
1057 if (ml403_ac97cr->capture_irq >= 0)
1058 free_irq(ml403_ac97cr->capture_irq, ml403_ac97cr);
1059 /* give back "port" */
1060 iounmap(ml403_ac97cr->port);
1061 kfree(ml403_ac97cr);
1062 PDEBUG(INIT_INFO, "free(): (done)\n");
1066 static int snd_ml403_ac97cr_dev_free(struct snd_device *snddev)
1068 struct snd_ml403_ac97cr *ml403_ac97cr = snddev->device_data;
1069 PDEBUG(INIT_INFO, "dev_free():\n");
1070 return snd_ml403_ac97cr_free(ml403_ac97cr);
1074 snd_ml403_ac97cr_create(struct snd_card *card, struct platform_device *pfdev,
1075 struct snd_ml403_ac97cr **rml403_ac97cr)
1077 struct snd_ml403_ac97cr *ml403_ac97cr;
1079 static const struct snd_device_ops ops = {
1080 .dev_free = snd_ml403_ac97cr_dev_free,
1082 struct resource *resource;
1085 *rml403_ac97cr = NULL;
1086 ml403_ac97cr = kzalloc(sizeof(*ml403_ac97cr), GFP_KERNEL);
1087 if (ml403_ac97cr == NULL)
1089 spin_lock_init(&ml403_ac97cr->reg_lock);
1090 mutex_init(&ml403_ac97cr->cdc_mutex);
1091 ml403_ac97cr->card = card;
1092 ml403_ac97cr->pfdev = pfdev;
1093 ml403_ac97cr->irq = -1;
1094 ml403_ac97cr->enable_irq = 0;
1095 ml403_ac97cr->capture_irq = -1;
1096 ml403_ac97cr->enable_capture_irq = 0;
1097 ml403_ac97cr->port = NULL;
1098 ml403_ac97cr->res_port = NULL;
1100 PDEBUG(INIT_INFO, "Trying to reserve resources now ...\n");
1101 resource = platform_get_resource(pfdev, IORESOURCE_MEM, 0);
1103 ml403_ac97cr->port = ioremap(resource->start,
1105 (resource->start) + 1);
1106 if (ml403_ac97cr->port == NULL) {
1107 snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
1108 "unable to remap memory region (%pR)\n",
1110 snd_ml403_ac97cr_free(ml403_ac97cr);
1113 snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
1114 "remap controller memory region to "
1115 "0x%x done\n", (unsigned int)ml403_ac97cr->port);
1117 irq = platform_get_irq(pfdev, 0);
1118 if (request_irq(irq, snd_ml403_ac97cr_irq, 0,
1119 dev_name(&pfdev->dev), (void *)ml403_ac97cr)) {
1120 snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
1121 "unable to grab IRQ %d\n",
1123 snd_ml403_ac97cr_free(ml403_ac97cr);
1126 ml403_ac97cr->irq = irq;
1127 snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
1128 "request (playback) irq %d done\n",
1130 irq = platform_get_irq(pfdev, 1);
1131 if (request_irq(irq, snd_ml403_ac97cr_irq, 0,
1132 dev_name(&pfdev->dev), (void *)ml403_ac97cr)) {
1133 snd_printk(KERN_ERR SND_ML403_AC97CR_DRIVER ": "
1134 "unable to grab IRQ %d\n",
1136 snd_ml403_ac97cr_free(ml403_ac97cr);
1139 ml403_ac97cr->capture_irq = irq;
1140 snd_printk(KERN_INFO SND_ML403_AC97CR_DRIVER ": "
1141 "request (capture) irq %d done\n",
1142 ml403_ac97cr->capture_irq);
1144 err = snd_ml403_ac97cr_chip_init(ml403_ac97cr);
1146 snd_ml403_ac97cr_free(ml403_ac97cr);
1150 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ml403_ac97cr, &ops);
1152 PDEBUG(INIT_FAILURE, "probe(): snd_device_new() failed!\n");
1153 snd_ml403_ac97cr_free(ml403_ac97cr);
1157 *rml403_ac97cr = ml403_ac97cr;
1161 static void snd_ml403_ac97cr_mixer_free(struct snd_ac97 *ac97)
1163 struct snd_ml403_ac97cr *ml403_ac97cr = ac97->private_data;
1164 PDEBUG(INIT_INFO, "mixer_free():\n");
1165 ml403_ac97cr->ac97 = NULL;
1166 PDEBUG(INIT_INFO, "mixer_free(): (done)\n");
1170 snd_ml403_ac97cr_mixer(struct snd_ml403_ac97cr *ml403_ac97cr)
1172 struct snd_ac97_bus *bus;
1173 struct snd_ac97_template ac97;
1175 static const struct snd_ac97_bus_ops ops = {
1176 .write = snd_ml403_ac97cr_codec_write,
1177 .read = snd_ml403_ac97cr_codec_read,
1179 PDEBUG(INIT_INFO, "mixer():\n");
1180 err = snd_ac97_bus(ml403_ac97cr->card, 0, &ops, NULL, &bus);
1184 memset(&ac97, 0, sizeof(ac97));
1185 ml403_ac97cr->ac97_fake = 1;
1186 lm4550_regfile_init();
1188 ml403_ac97cr->ac97_read = 0;
1189 ml403_ac97cr->ac97_write = 0;
1191 ac97.private_data = ml403_ac97cr;
1192 ac97.private_free = snd_ml403_ac97cr_mixer_free;
1193 ac97.scaps = AC97_SCAP_AUDIO | AC97_SCAP_SKIP_MODEM |
1195 err = snd_ac97_mixer(bus, &ac97, &ml403_ac97cr->ac97);
1196 ml403_ac97cr->ac97_fake = 0;
1197 lm4550_regfile_write_values_after_init(ml403_ac97cr->ac97);
1198 PDEBUG(INIT_INFO, "mixer(): (done) snd_ac97_mixer()=%d\n", err);
1203 snd_ml403_ac97cr_pcm(struct snd_ml403_ac97cr *ml403_ac97cr, int device)
1205 struct snd_pcm *pcm;
1208 err = snd_pcm_new(ml403_ac97cr->card, "ML403AC97CR/1", device, 1, 1,
1212 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1213 &snd_ml403_ac97cr_playback_ops);
1214 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
1215 &snd_ml403_ac97cr_capture_ops);
1216 pcm->private_data = ml403_ac97cr;
1217 pcm->info_flags = 0;
1218 strcpy(pcm->name, "ML403AC97CR DAC/ADC");
1219 ml403_ac97cr->pcm = pcm;
1221 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
1228 static int snd_ml403_ac97cr_probe(struct platform_device *pfdev)
1230 struct snd_card *card;
1231 struct snd_ml403_ac97cr *ml403_ac97cr = NULL;
1233 int dev = pfdev->id;
1235 if (dev >= SNDRV_CARDS)
1240 err = snd_card_new(&pfdev->dev, index[dev], id[dev], THIS_MODULE,
1244 err = snd_ml403_ac97cr_create(card, pfdev, &ml403_ac97cr);
1246 PDEBUG(INIT_FAILURE, "probe(): create failed!\n");
1247 snd_card_free(card);
1250 PDEBUG(INIT_INFO, "probe(): create done\n");
1251 card->private_data = ml403_ac97cr;
1252 err = snd_ml403_ac97cr_mixer(ml403_ac97cr);
1254 snd_card_free(card);
1257 PDEBUG(INIT_INFO, "probe(): mixer done\n");
1258 err = snd_ml403_ac97cr_pcm(ml403_ac97cr, 0);
1260 snd_card_free(card);
1263 PDEBUG(INIT_INFO, "probe(): PCM done\n");
1264 strcpy(card->driver, SND_ML403_AC97CR_DRIVER);
1265 strcpy(card->shortname, "ML403 AC97 Controller Reference");
1266 sprintf(card->longname, "%s %s at 0x%lx, irq %i & %i, device %i",
1267 card->shortname, card->driver,
1268 (unsigned long)ml403_ac97cr->port, ml403_ac97cr->irq,
1269 ml403_ac97cr->capture_irq, dev + 1);
1271 err = snd_card_register(card);
1273 snd_card_free(card);
1276 platform_set_drvdata(pfdev, card);
1277 PDEBUG(INIT_INFO, "probe(): (done)\n");
1281 static int snd_ml403_ac97cr_remove(struct platform_device *pfdev)
1283 snd_card_free(platform_get_drvdata(pfdev));
1287 /* work with hotplug and coldplug */
1288 MODULE_ALIAS("platform:" SND_ML403_AC97CR_DRIVER);
1290 static struct platform_driver snd_ml403_ac97cr_driver = {
1291 .probe = snd_ml403_ac97cr_probe,
1292 .remove = snd_ml403_ac97cr_remove,
1294 .name = SND_ML403_AC97CR_DRIVER,
1298 module_platform_driver(snd_ml403_ac97cr_driver);