1 /*******************************************************************************
3 * "swarm_cs4297a.c" -- Cirrus Logic-Crystal CS4297a linux audio driver.
5 * Copyright (C) 2001 Broadcom Corporation.
6 * Copyright (C) 2000,2001 Cirrus Logic Corp.
7 * -- adapted from drivers by Thomas Sailer,
8 * -- but don't bug him; Problems should go to:
9 * -- tom woller (twoller@crystal.cirrus.com) or
10 * (audio@crystal.cirrus.com).
11 * -- adapted from cs4281 PCI driver for cs4297a on
12 * BCM1250 Synchronous Serial interface
13 * (Kip Walker, Broadcom Corp.)
14 * Copyright (C) 2004 Maciej W. Rozycki
15 * Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * Module command line parameters:
35 * /dev/dsp standard /dev/dsp device, (mostly) OSS compatible
36 * /dev/mixer standard /dev/mixer device, (mostly) OSS compatible
37 * /dev/midi simple MIDI UART interface, no ioctl
39 * Modification History
40 * 08/20/00 trw - silence and no stopping DAC until release
41 * 08/23/00 trw - added CS_DBG statements, fix interrupt hang issue on DAC stop.
42 * 09/18/00 trw - added 16bit only record with conversion
43 * 09/24/00 trw - added Enhanced Full duplex (separate simultaneous
44 * capture/playback rates)
45 * 10/03/00 trw - fixed mmap (fixed GRECORD and the XMMS mmap test plugin
47 * 10/11/00 trw - modified for 2.4.0-test9 kernel enhancements (NR_MAP removal)
48 * 11/03/00 trw - fixed interrupt loss/stutter, added debug.
49 * 11/10/00 bkz - added __devinit to cs4297a_hw_init()
50 * 11/10/00 trw - fixed SMP and capture spinlock hang.
51 * 12/04/00 trw - cleaned up CSDEBUG flags and added "defaultorder" moduleparm.
52 * 12/05/00 trw - fixed polling (myth2), and added underrun swptr fix.
53 * 12/08/00 trw - added PM support.
54 * 12/14/00 trw - added wrapper code, builds under 2.4.0, 2.2.17-20, 2.2.17-8
55 * (RH/Dell base), 2.2.18, 2.2.12. cleaned up code mods by ident.
56 * 12/19/00 trw - added PM support for 2.2 base (apm_callback). other PM cleanup.
57 * 12/21/00 trw - added fractional "defaultorder" inputs. if >100 then use
58 * defaultorder-100 as power of 2 for the buffer size. example:
59 * 106 = 2^(106-100) = 2^6 = 64 bytes for the buffer size.
61 *******************************************************************************/
63 #include <linux/list.h>
64 #include <linux/module.h>
65 #include <linux/string.h>
66 #include <linux/ioport.h>
67 #include <linux/sched.h>
68 #include <linux/delay.h>
69 #include <linux/sound.h>
70 #include <linux/slab.h>
71 #include <linux/soundcard.h>
72 #include <linux/ac97_codec.h>
73 #include <linux/pci.h>
74 #include <linux/bitops.h>
75 #include <linux/interrupt.h>
76 #include <linux/init.h>
77 #include <linux/poll.h>
78 #include <linux/mutex.h>
79 #include <linux/kernel.h>
81 #include <asm/byteorder.h>
84 #include <asm/uaccess.h>
86 #include <asm/sibyte/sb1250_regs.h>
87 #include <asm/sibyte/sb1250_int.h>
88 #include <asm/sibyte/sb1250_dma.h>
89 #include <asm/sibyte/sb1250_scd.h>
90 #include <asm/sibyte/sb1250_syncser.h>
91 #include <asm/sibyte/sb1250_mac.h>
92 #include <asm/sibyte/sb1250.h>
96 static void stop_dac(struct cs4297a_state *s);
97 static void stop_adc(struct cs4297a_state *s);
98 static void start_dac(struct cs4297a_state *s);
99 static void start_adc(struct cs4297a_state *s);
100 #undef OSS_DOCUMENTED_MIXER_SEMANTICS
102 // ---------------------------------------------------------------------
104 #define CS4297a_MAGIC 0xf00beef1
106 // buffer order determines the size of the dma buffer for the driver.
107 // under Linux, a smaller buffer allows more responsiveness from many of the
108 // applications (e.g. games). A larger buffer allows some of the apps (esound)
109 // to not underrun the dma buffer as easily. As default, use 32k (order=3)
110 // rather than 64k as some of the games work more responsively.
111 // log base 2( buff sz = 32k).
113 //static unsigned long defaultorder = 3;
114 //MODULE_PARM(defaultorder, "i");
117 // Turn on/off debugging compilation by commenting out "#define CSDEBUG"
121 #define CSDEBUG_INTERFACE 1
123 #undef CSDEBUG_INTERFACE
126 // cs_debugmask areas
128 #define CS_INIT 0x00000001 // initialization and probe functions
129 #define CS_ERROR 0x00000002 // tmp debugging bit placeholder
130 #define CS_INTERRUPT 0x00000004 // interrupt handler (separate from all other)
131 #define CS_FUNCTION 0x00000008 // enter/leave functions
132 #define CS_WAVE_WRITE 0x00000010 // write information for wave
133 #define CS_WAVE_READ 0x00000020 // read information for wave
134 #define CS_AC97 0x00000040 // AC97 register access
135 #define CS_DESCR 0x00000080 // descriptor management
136 #define CS_OPEN 0x00000400 // all open functions in the driver
137 #define CS_RELEASE 0x00000800 // all release functions in the driver
138 #define CS_PARMS 0x00001000 // functional and operational parameters
139 #define CS_IOCTL 0x00002000 // ioctl (non-mixer)
140 #define CS_TMP 0x10000000 // tmp debug mask bit
143 // CSDEBUG is usual mode is set to 1, then use the
144 // cs_debuglevel and cs_debugmask to turn on or off debugging.
145 // Debug level of 1 has been defined to be kernel errors and info
146 // that should be printed on any released driver.
149 #define CS_DBGOUT(mask,level,x) if((cs_debuglevel >= (level)) && ((mask) & cs_debugmask) ) {x;}
151 #define CS_DBGOUT(mask,level,x)
155 static unsigned long cs_debuglevel = 4; // levels range from 1-9
156 static unsigned long cs_debugmask = CS_INIT /*| CS_IOCTL*/;
157 module_param(cs_debuglevel, int, 0);
158 module_param(cs_debugmask, int, 0);
163 #define CS_TYPE_ADC 0
164 #define CS_TYPE_DAC 1
166 #define SER_BASE (A_SER_BASE_1 + KSEG1)
167 #define SS_CSR(t) (SER_BASE+t)
168 #define SS_TXTBL(t) (SER_BASE+R_SER_TX_TABLE_BASE+(t*8))
169 #define SS_RXTBL(t) (SER_BASE+R_SER_RX_TABLE_BASE+(t*8))
171 #define FRAME_BYTES 32
172 #define FRAME_SAMPLE_BYTES 4
174 /* Should this be variable? */
175 #define SAMPLE_BUF_SIZE (16*1024)
176 #define SAMPLE_FRAME_COUNT (SAMPLE_BUF_SIZE / FRAME_SAMPLE_BYTES)
177 /* The driver can explode/shrink the frames to/from a smaller sample
179 #define DMA_BLOAT_FACTOR 1
180 #define DMA_DESCR (SAMPLE_FRAME_COUNT / DMA_BLOAT_FACTOR)
181 #define DMA_BUF_SIZE (DMA_DESCR * FRAME_BYTES)
183 /* Use the maxmium count (255 == 5.1 ms between interrupts) */
184 #define DMA_INT_CNT ((1 << S_DMA_INT_PKTCNT) - 1)
186 /* Figure this out: how many TX DMAs ahead to schedule a reg access */
187 #define REG_LATENCY 150
189 #define FRAME_TX_US 20
191 #define SERDMA_NEXTBUF(d,f) (((d)->f+1) % (d)->ringsz)
193 static const char invalid_magic[] =
194 KERN_CRIT "cs4297a: invalid magic value\n";
196 #define VALIDATE_STATE(s) \
198 if (!(s) || (s)->magic != CS4297a_MAGIC) { \
199 printk(invalid_magic); \
204 struct list_head cs4297a_devs = { &cs4297a_devs, &cs4297a_devs };
206 typedef struct serdma_descr_s {
211 typedef unsigned long paddr_t;
213 typedef struct serdma_s {
215 serdma_descr_t *descrtab;
216 serdma_descr_t *descrtab_end;
217 paddr_t descrtab_phys;
219 serdma_descr_t *descr_add;
220 serdma_descr_t *descr_rem;
222 u64 *dma_buf; // buffer for DMA contents (frames)
223 paddr_t dma_buf_phys;
224 u16 *sample_buf; // tmp buffer for sample conversions
230 // unsigned buforder; // Log base 2 of 'dma_buf' size in bytes..
231 unsigned numfrag; // # of 'fragments' in the buffer.
232 unsigned fragshift; // Log base 2 of fragment size.
233 unsigned hwptr, swptr;
234 unsigned total_bytes; // # bytes process since open.
235 unsigned blocks; // last returned blocks value GETOPTR
236 unsigned wakeup; // interrupt occurred on block
238 unsigned underrun; // underrun flag
239 unsigned error; // over/underrun
240 wait_queue_head_t wait;
241 wait_queue_head_t reg_wait;
242 // redundant, but makes calculations easier
243 unsigned fragsize; // 2**fragshift..
244 unsigned sbufsz; // 2**buforder.
245 unsigned fragsamples;
247 unsigned mapped:1; // Buffer mapped in cs4297a_mmap()?
248 unsigned ready:1; // prog_dmabuf_dac()/adc() successful?
249 unsigned endcleared:1;
250 unsigned type:1; // adc or dac buffer (CS_TYPE_XXX)
251 unsigned ossfragshift;
253 unsigned subdivision;
256 struct cs4297a_state {
260 struct list_head list;
266 // hardware resources
270 unsigned int rx_ovrrn; /* FIFO */
271 unsigned int rx_overflow; /* staging buffer */
272 unsigned int tx_underrun;
274 unsigned int rx_good;
279 unsigned short vol[10];
282 unsigned short micpreamp;
288 unsigned fmt_original; // original requested format
291 } prop_dac, prop_adc;
292 unsigned conversion:1; // conversion from 16 to 8 bit in progress
295 struct mutex open_mutex;
296 struct mutex open_sem_adc;
297 struct mutex open_sem_dac;
299 wait_queue_head_t open_wait;
300 wait_queue_head_t open_wait_adc;
301 wait_queue_head_t open_wait_dac;
303 dma_addr_t dmaaddr_sample_buf;
304 unsigned buforder_sample_buf; // Log base 2 of 'dma_buf' size in bytes..
306 serdma_t dma_dac, dma_adc;
308 volatile u16 read_value;
309 volatile u16 read_reg;
310 volatile u64 reg_request;
314 #define prog_codec(a,b)
315 #define dealloc_dmabuf(a,b);
318 static int prog_dmabuf_adc(struct cs4297a_state *s)
320 s->dma_adc.ready = 1;
325 static int prog_dmabuf_dac(struct cs4297a_state *s)
327 s->dma_dac.ready = 1;
331 static void clear_advance(void *buf, unsigned bsize, unsigned bptr,
332 unsigned len, unsigned char c)
334 if (bptr + len > bsize) {
335 unsigned x = bsize - bptr;
336 memset(((char *) buf) + bptr, c, x);
340 CS_DBGOUT(CS_WAVE_WRITE, 4, printk(KERN_INFO
341 "cs4297a: clear_advance(): memset %d at 0x%.8x for %d size \n",
342 (unsigned)c, (unsigned)((char *) buf) + bptr, len));
343 memset(((char *) buf) + bptr, c, len);
350 #define SOUND_MIXER_CS_GETDBGLEVEL _SIOWR('M',120, int)
351 #define SOUND_MIXER_CS_SETDBGLEVEL _SIOWR('M',121, int)
352 #define SOUND_MIXER_CS_GETDBGMASK _SIOWR('M',122, int)
353 #define SOUND_MIXER_CS_SETDBGMASK _SIOWR('M',123, int)
355 static void cs_printioctl(unsigned int x)
359 // Index of mixtable1[] member is Device ID
360 // and must be <= SOUND_MIXER_NRDEVICES.
361 // Value of array member is index into s->mix.vol[]
362 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
363 [SOUND_MIXER_PCM] = 1, // voice
364 [SOUND_MIXER_LINE1] = 2, // AUX
365 [SOUND_MIXER_CD] = 3, // CD
366 [SOUND_MIXER_LINE] = 4, // Line
367 [SOUND_MIXER_SYNTH] = 5, // FM
368 [SOUND_MIXER_MIC] = 6, // Mic
369 [SOUND_MIXER_SPEAKER] = 7, // Speaker
370 [SOUND_MIXER_RECLEV] = 8, // Recording level
371 [SOUND_MIXER_VOLUME] = 9 // Master Volume
375 case SOUND_MIXER_CS_GETDBGMASK:
376 CS_DBGOUT(CS_IOCTL, 4,
377 printk("SOUND_MIXER_CS_GETDBGMASK:\n"));
379 case SOUND_MIXER_CS_GETDBGLEVEL:
380 CS_DBGOUT(CS_IOCTL, 4,
381 printk("SOUND_MIXER_CS_GETDBGLEVEL:\n"));
383 case SOUND_MIXER_CS_SETDBGMASK:
384 CS_DBGOUT(CS_IOCTL, 4,
385 printk("SOUND_MIXER_CS_SETDBGMASK:\n"));
387 case SOUND_MIXER_CS_SETDBGLEVEL:
388 CS_DBGOUT(CS_IOCTL, 4,
389 printk("SOUND_MIXER_CS_SETDBGLEVEL:\n"));
392 CS_DBGOUT(CS_IOCTL, 4, printk("OSS_GETVERSION:\n"));
394 case SNDCTL_DSP_SYNC:
395 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SYNC:\n"));
397 case SNDCTL_DSP_SETDUPLEX:
398 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETDUPLEX:\n"));
400 case SNDCTL_DSP_GETCAPS:
401 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETCAPS:\n"));
403 case SNDCTL_DSP_RESET:
404 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_RESET:\n"));
406 case SNDCTL_DSP_SPEED:
407 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SPEED:\n"));
409 case SNDCTL_DSP_STEREO:
410 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_STEREO:\n"));
412 case SNDCTL_DSP_CHANNELS:
413 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_CHANNELS:\n"));
415 case SNDCTL_DSP_GETFMTS:
416 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETFMTS:\n"));
418 case SNDCTL_DSP_SETFMT:
419 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETFMT:\n"));
421 case SNDCTL_DSP_POST:
422 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_POST:\n"));
424 case SNDCTL_DSP_GETTRIGGER:
425 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETTRIGGER:\n"));
427 case SNDCTL_DSP_SETTRIGGER:
428 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETTRIGGER:\n"));
430 case SNDCTL_DSP_GETOSPACE:
431 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOSPACE:\n"));
433 case SNDCTL_DSP_GETISPACE:
434 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETISPACE:\n"));
436 case SNDCTL_DSP_NONBLOCK:
437 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_NONBLOCK:\n"));
439 case SNDCTL_DSP_GETODELAY:
440 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETODELAY:\n"));
442 case SNDCTL_DSP_GETIPTR:
443 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETIPTR:\n"));
445 case SNDCTL_DSP_GETOPTR:
446 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETOPTR:\n"));
448 case SNDCTL_DSP_GETBLKSIZE:
449 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_GETBLKSIZE:\n"));
451 case SNDCTL_DSP_SETFRAGMENT:
452 CS_DBGOUT(CS_IOCTL, 4,
453 printk("SNDCTL_DSP_SETFRAGMENT:\n"));
455 case SNDCTL_DSP_SUBDIVIDE:
456 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SUBDIVIDE:\n"));
458 case SOUND_PCM_READ_RATE:
459 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_RATE:\n"));
461 case SOUND_PCM_READ_CHANNELS:
462 CS_DBGOUT(CS_IOCTL, 4,
463 printk("SOUND_PCM_READ_CHANNELS:\n"));
465 case SOUND_PCM_READ_BITS:
466 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_BITS:\n"));
468 case SOUND_PCM_WRITE_FILTER:
469 CS_DBGOUT(CS_IOCTL, 4,
470 printk("SOUND_PCM_WRITE_FILTER:\n"));
472 case SNDCTL_DSP_SETSYNCRO:
473 CS_DBGOUT(CS_IOCTL, 4, printk("SNDCTL_DSP_SETSYNCRO:\n"));
475 case SOUND_PCM_READ_FILTER:
476 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_PCM_READ_FILTER:\n"));
478 case SOUND_MIXER_PRIVATE1:
479 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE1:\n"));
481 case SOUND_MIXER_PRIVATE2:
482 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE2:\n"));
484 case SOUND_MIXER_PRIVATE3:
485 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE3:\n"));
487 case SOUND_MIXER_PRIVATE4:
488 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE4:\n"));
490 case SOUND_MIXER_PRIVATE5:
491 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_PRIVATE5:\n"));
493 case SOUND_MIXER_INFO:
494 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_INFO:\n"));
496 case SOUND_OLD_MIXER_INFO:
497 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_OLD_MIXER_INFO:\n"));
501 switch (_IOC_NR(x)) {
502 case SOUND_MIXER_VOLUME:
503 CS_DBGOUT(CS_IOCTL, 4,
504 printk("SOUND_MIXER_VOLUME:\n"));
506 case SOUND_MIXER_SPEAKER:
507 CS_DBGOUT(CS_IOCTL, 4,
508 printk("SOUND_MIXER_SPEAKER:\n"));
510 case SOUND_MIXER_RECLEV:
511 CS_DBGOUT(CS_IOCTL, 4,
512 printk("SOUND_MIXER_RECLEV:\n"));
514 case SOUND_MIXER_MIC:
515 CS_DBGOUT(CS_IOCTL, 4,
516 printk("SOUND_MIXER_MIC:\n"));
518 case SOUND_MIXER_SYNTH:
519 CS_DBGOUT(CS_IOCTL, 4,
520 printk("SOUND_MIXER_SYNTH:\n"));
522 case SOUND_MIXER_RECSRC:
523 CS_DBGOUT(CS_IOCTL, 4,
524 printk("SOUND_MIXER_RECSRC:\n"));
526 case SOUND_MIXER_DEVMASK:
527 CS_DBGOUT(CS_IOCTL, 4,
528 printk("SOUND_MIXER_DEVMASK:\n"));
530 case SOUND_MIXER_RECMASK:
531 CS_DBGOUT(CS_IOCTL, 4,
532 printk("SOUND_MIXER_RECMASK:\n"));
534 case SOUND_MIXER_STEREODEVS:
535 CS_DBGOUT(CS_IOCTL, 4,
536 printk("SOUND_MIXER_STEREODEVS:\n"));
538 case SOUND_MIXER_CAPS:
539 CS_DBGOUT(CS_IOCTL, 4, printk("SOUND_MIXER_CAPS:\n"));
543 if (i >= SOUND_MIXER_NRDEVICES
544 || !(vidx = mixtable1[i])) {
545 CS_DBGOUT(CS_IOCTL, 4, printk
546 ("UNKNOWN IOCTL: 0x%.8x NR=%d\n",
549 CS_DBGOUT(CS_IOCTL, 4, printk
550 ("SOUND_MIXER_IOCTL AC9x: 0x%.8x NR=%d\n",
560 static int ser_init(struct cs4297a_state *s)
564 CS_DBGOUT(CS_INIT, 2,
565 printk(KERN_INFO "cs4297a: Setting up serial parameters\n"));
567 __raw_writeq(M_SYNCSER_CMD_RX_RESET | M_SYNCSER_CMD_TX_RESET, SS_CSR(R_SER_CMD));
569 __raw_writeq(M_SYNCSER_MSB_FIRST, SS_CSR(R_SER_MODE));
570 __raw_writeq(32, SS_CSR(R_SER_MINFRM_SZ));
571 __raw_writeq(32, SS_CSR(R_SER_MAXFRM_SZ));
573 __raw_writeq(1, SS_CSR(R_SER_TX_RD_THRSH));
574 __raw_writeq(4, SS_CSR(R_SER_TX_WR_THRSH));
575 __raw_writeq(8, SS_CSR(R_SER_RX_RD_THRSH));
577 /* This looks good from experimentation */
578 __raw_writeq((M_SYNCSER_TXSYNC_INT | V_SYNCSER_TXSYNC_DLY(0) | M_SYNCSER_TXCLK_EXT |
579 M_SYNCSER_RXSYNC_INT | V_SYNCSER_RXSYNC_DLY(1) | M_SYNCSER_RXCLK_EXT | M_SYNCSER_RXSYNC_EDGE),
580 SS_CSR(R_SER_LINE_MODE));
582 /* This looks good from experimentation */
583 __raw_writeq(V_SYNCSER_SEQ_COUNT(14) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE,
585 __raw_writeq(V_SYNCSER_SEQ_COUNT(15) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
587 __raw_writeq(V_SYNCSER_SEQ_COUNT(13) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
589 __raw_writeq(V_SYNCSER_SEQ_COUNT( 0) | M_SYNCSER_SEQ_ENABLE |
590 M_SYNCSER_SEQ_STROBE | M_SYNCSER_SEQ_LAST, SS_TXTBL(3));
592 __raw_writeq(V_SYNCSER_SEQ_COUNT(14) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE,
594 __raw_writeq(V_SYNCSER_SEQ_COUNT(15) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
596 __raw_writeq(V_SYNCSER_SEQ_COUNT(13) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_BYTE,
598 __raw_writeq(V_SYNCSER_SEQ_COUNT( 0) | M_SYNCSER_SEQ_ENABLE | M_SYNCSER_SEQ_STROBE |
599 M_SYNCSER_SEQ_LAST, SS_RXTBL(3));
601 for (i=4; i<16; i++) {
602 /* Just in case... */
603 __raw_writeq(M_SYNCSER_SEQ_LAST, SS_TXTBL(i));
604 __raw_writeq(M_SYNCSER_SEQ_LAST, SS_RXTBL(i));
610 static int init_serdma(serdma_t *dma)
612 CS_DBGOUT(CS_INIT, 2,
613 printk(KERN_ERR "cs4297a: desc - %d sbufsize - %d dbufsize - %d\n",
614 DMA_DESCR, SAMPLE_BUF_SIZE, DMA_BUF_SIZE));
617 dma->ringsz = DMA_DESCR;
618 dma->descrtab = kzalloc(dma->ringsz * sizeof(serdma_descr_t), GFP_KERNEL);
619 if (!dma->descrtab) {
620 printk(KERN_ERR "cs4297a: kzalloc descrtab failed\n");
623 dma->descrtab_end = dma->descrtab + dma->ringsz;
624 /* XXX bloddy mess, use proper DMA API here ... */
625 dma->descrtab_phys = CPHYSADDR((long)dma->descrtab);
626 dma->descr_add = dma->descr_rem = dma->descrtab;
628 /* Frame buffer area */
629 dma->dma_buf = kzalloc(DMA_BUF_SIZE, GFP_KERNEL);
631 printk(KERN_ERR "cs4297a: kzalloc dma_buf failed\n");
632 kfree(dma->descrtab);
635 dma->dma_buf_phys = CPHYSADDR((long)dma->dma_buf);
637 /* Samples buffer area */
638 dma->sbufsz = SAMPLE_BUF_SIZE;
639 dma->sample_buf = kmalloc(dma->sbufsz, GFP_KERNEL);
640 if (!dma->sample_buf) {
641 printk(KERN_ERR "cs4297a: kmalloc sample_buf failed\n");
642 kfree(dma->descrtab);
646 dma->sb_swptr = dma->sb_hwptr = dma->sample_buf;
647 dma->sb_end = (u16 *)((void *)dma->sample_buf + dma->sbufsz);
648 dma->fragsize = dma->sbufsz >> 1;
650 CS_DBGOUT(CS_INIT, 4,
651 printk(KERN_ERR "cs4297a: descrtab - %08x dma_buf - %x sample_buf - %x\n",
652 (int)dma->descrtab, (int)dma->dma_buf,
653 (int)dma->sample_buf));
658 static int dma_init(struct cs4297a_state *s)
662 CS_DBGOUT(CS_INIT, 2,
663 printk(KERN_INFO "cs4297a: Setting up DMA\n"));
665 if (init_serdma(&s->dma_adc) ||
666 init_serdma(&s->dma_dac))
669 if (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_RX))||
670 __raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX))) {
671 panic("DMA state corrupted?!");
674 /* Initialize now - the descr/buffer pairings will never
676 for (i=0; i<DMA_DESCR; i++) {
677 s->dma_dac.descrtab[i].descr_a = M_DMA_SERRX_SOP | V_DMA_DSCRA_A_SIZE(1) |
678 (s->dma_dac.dma_buf_phys + i*FRAME_BYTES);
679 s->dma_dac.descrtab[i].descr_b = V_DMA_DSCRB_PKT_SIZE(FRAME_BYTES);
680 s->dma_adc.descrtab[i].descr_a = V_DMA_DSCRA_A_SIZE(1) |
681 (s->dma_adc.dma_buf_phys + i*FRAME_BYTES);
682 s->dma_adc.descrtab[i].descr_b = 0;
685 __raw_writeq((M_DMA_EOP_INT_EN | V_DMA_INT_PKTCNT(DMA_INT_CNT) |
686 V_DMA_RINGSZ(DMA_DESCR) | M_DMA_TDX_EN),
687 SS_CSR(R_SER_DMA_CONFIG0_RX));
688 __raw_writeq(M_DMA_L2CA, SS_CSR(R_SER_DMA_CONFIG1_RX));
689 __raw_writeq(s->dma_adc.descrtab_phys, SS_CSR(R_SER_DMA_DSCR_BASE_RX));
691 __raw_writeq(V_DMA_RINGSZ(DMA_DESCR), SS_CSR(R_SER_DMA_CONFIG0_TX));
692 __raw_writeq(M_DMA_L2CA | M_DMA_NO_DSCR_UPDT, SS_CSR(R_SER_DMA_CONFIG1_TX));
693 __raw_writeq(s->dma_dac.descrtab_phys, SS_CSR(R_SER_DMA_DSCR_BASE_TX));
695 /* Prep the receive DMA descriptor ring */
696 __raw_writeq(DMA_DESCR, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
698 __raw_writeq(M_SYNCSER_DMA_RX_EN | M_SYNCSER_DMA_TX_EN, SS_CSR(R_SER_DMA_ENABLE));
700 __raw_writeq((M_SYNCSER_RX_SYNC_ERR | M_SYNCSER_RX_OVERRUN | M_SYNCSER_RX_EOP_COUNT),
701 SS_CSR(R_SER_INT_MASK));
703 /* Enable the rx/tx; let the codec warm up to the sync and
704 start sending good frames before the receive FIFO is
706 __raw_writeq(M_SYNCSER_CMD_TX_EN, SS_CSR(R_SER_CMD));
708 __raw_writeq(M_SYNCSER_CMD_RX_EN | M_SYNCSER_CMD_TX_EN, SS_CSR(R_SER_CMD));
710 /* XXXKW is this magic? (the "1" part) */
711 while ((__raw_readq(SS_CSR(R_SER_STATUS)) & 0xf1) != 1)
714 CS_DBGOUT(CS_INIT, 4,
715 printk(KERN_INFO "cs4297a: status: %08x\n",
716 (unsigned int)(__raw_readq(SS_CSR(R_SER_STATUS)) & 0xffffffff)));
721 static int serdma_reg_access(struct cs4297a_state *s, u64 data)
723 serdma_t *d = &s->dma_dac;
727 serdma_descr_t *descr;
729 if (s->reg_request) {
730 printk(KERN_ERR "cs4297a: attempt to issue multiple reg_access\n");
734 if (s->ena & FMODE_WRITE) {
735 /* Since a writer has the DSP open, we have to mux the
737 s->reg_request = data;
738 interruptible_sleep_on(&s->dma_dac.reg_wait);
739 /* XXXKW how can I deal with the starvation case where
740 the opener isn't writing? */
742 /* Be safe when changing ring pointers */
743 spin_lock_irqsave(&s->lock, flags);
744 if (d->hwptr != d->swptr) {
745 printk(KERN_ERR "cs4297a: reg access found bookkeeping error (hw/sw = %d/%d\n",
747 spin_unlock_irqrestore(&s->lock, flags);
751 d->hwptr = d->swptr = (d->swptr + 1) % d->ringsz;
752 spin_unlock_irqrestore(&s->lock, flags);
754 descr = &d->descrtab[swptr];
755 data_p = &d->dma_buf[swptr * 4];
756 *data_p = cpu_to_be64(data);
757 __raw_writeq(1, SS_CSR(R_SER_DMA_DSCR_COUNT_TX));
758 CS_DBGOUT(CS_DESCR, 4,
759 printk(KERN_INFO "cs4297a: add_tx %p (%x -> %x)\n",
760 data_p, swptr, d->hwptr));
763 CS_DBGOUT(CS_FUNCTION, 6,
764 printk(KERN_INFO "cs4297a: serdma_reg_access()-\n"));
769 //****************************************************************************
770 // "cs4297a_read_ac97" -- Reads an AC97 register
771 //****************************************************************************
772 static int cs4297a_read_ac97(struct cs4297a_state *s, u32 offset,
775 CS_DBGOUT(CS_AC97, 1,
776 printk(KERN_INFO "cs4297a: read reg %2x\n", offset));
777 if (serdma_reg_access(s, (0xCLL << 60) | (1LL << 47) | ((u64)(offset & 0x7F) << 40)))
780 interruptible_sleep_on(&s->dma_adc.reg_wait);
781 *value = s->read_value;
782 CS_DBGOUT(CS_AC97, 2,
783 printk(KERN_INFO "cs4297a: rdr reg %x -> %x\n", s->read_reg, s->read_value));
789 //****************************************************************************
790 // "cs4297a_write_ac97()"-- writes an AC97 register
791 //****************************************************************************
792 static int cs4297a_write_ac97(struct cs4297a_state *s, u32 offset,
795 CS_DBGOUT(CS_AC97, 1,
796 printk(KERN_INFO "cs4297a: write reg %2x -> %04x\n", offset, value));
797 return (serdma_reg_access(s, (0xELL << 60) | ((u64)(offset & 0x7F) << 40) | ((value & 0xffff) << 12)));
800 static void stop_dac(struct cs4297a_state *s)
804 CS_DBGOUT(CS_WAVE_WRITE, 3, printk(KERN_INFO "cs4297a: stop_dac():\n"));
805 spin_lock_irqsave(&s->lock, flags);
806 s->ena &= ~FMODE_WRITE;
808 /* XXXKW what do I really want here? My theory for now is
809 that I just flip the "ena" bit, and the interrupt handler
810 will stop processing the xmit channel */
811 __raw_writeq((s->ena & FMODE_READ) ? M_SYNCSER_DMA_RX_EN : 0,
812 SS_CSR(R_SER_DMA_ENABLE));
815 spin_unlock_irqrestore(&s->lock, flags);
819 static void start_dac(struct cs4297a_state *s)
823 CS_DBGOUT(CS_FUNCTION, 3, printk(KERN_INFO "cs4297a: start_dac()+\n"));
824 spin_lock_irqsave(&s->lock, flags);
825 if (!(s->ena & FMODE_WRITE) && (s->dma_dac.mapped ||
826 (s->dma_dac.count > 0
827 && s->dma_dac.ready))) {
828 s->ena |= FMODE_WRITE;
829 /* XXXKW what do I really want here? My theory for
830 now is that I just flip the "ena" bit, and the
831 interrupt handler will start processing the xmit
834 CS_DBGOUT(CS_WAVE_WRITE | CS_PARMS, 8, printk(KERN_INFO
835 "cs4297a: start_dac(): start dma\n"));
838 spin_unlock_irqrestore(&s->lock, flags);
839 CS_DBGOUT(CS_FUNCTION, 3,
840 printk(KERN_INFO "cs4297a: start_dac()-\n"));
844 static void stop_adc(struct cs4297a_state *s)
848 CS_DBGOUT(CS_FUNCTION, 3,
849 printk(KERN_INFO "cs4297a: stop_adc()+\n"));
851 spin_lock_irqsave(&s->lock, flags);
852 s->ena &= ~FMODE_READ;
854 if (s->conversion == 1) {
856 s->prop_adc.fmt = s->prop_adc.fmt_original;
858 /* Nothing to do really, I need to keep the DMA going
859 XXXKW when do I get here, and is there more I should do? */
860 spin_unlock_irqrestore(&s->lock, flags);
861 CS_DBGOUT(CS_FUNCTION, 3,
862 printk(KERN_INFO "cs4297a: stop_adc()-\n"));
866 static void start_adc(struct cs4297a_state *s)
870 CS_DBGOUT(CS_FUNCTION, 2,
871 printk(KERN_INFO "cs4297a: start_adc()+\n"));
873 if (!(s->ena & FMODE_READ) &&
874 (s->dma_adc.mapped || s->dma_adc.count <=
875 (signed) (s->dma_adc.sbufsz - 2 * s->dma_adc.fragsize))
876 && s->dma_adc.ready) {
877 if (s->prop_adc.fmt & AFMT_S8 || s->prop_adc.fmt & AFMT_U8) {
879 // now only use 16 bit capture, due to truncation issue
880 // in the chip, noticable distortion occurs.
881 // allocate buffer and then convert from 16 bit to
882 // 8 bit for the user buffer.
884 s->prop_adc.fmt_original = s->prop_adc.fmt;
885 if (s->prop_adc.fmt & AFMT_S8) {
886 s->prop_adc.fmt &= ~AFMT_S8;
887 s->prop_adc.fmt |= AFMT_S16_LE;
889 if (s->prop_adc.fmt & AFMT_U8) {
890 s->prop_adc.fmt &= ~AFMT_U8;
891 s->prop_adc.fmt |= AFMT_U16_LE;
894 // prog_dmabuf_adc performs a stop_adc() but that is
895 // ok since we really haven't started the DMA yet.
897 prog_codec(s, CS_TYPE_ADC);
902 spin_lock_irqsave(&s->lock, flags);
903 s->ena |= FMODE_READ;
904 /* Nothing to do really, I am probably already
905 DMAing... XXXKW when do I get here, and is there
907 spin_unlock_irqrestore(&s->lock, flags);
909 CS_DBGOUT(CS_PARMS, 6, printk(KERN_INFO
910 "cs4297a: start_adc(): start adc\n"));
912 CS_DBGOUT(CS_FUNCTION, 2,
913 printk(KERN_INFO "cs4297a: start_adc()-\n"));
918 // call with spinlock held!
919 static void cs4297a_update_ptr(struct cs4297a_state *s, int intflag)
921 int good_diff, diff, diff2;
927 serdma_descr_t *descr;
929 // update ADC pointer
930 status = intflag ? __raw_readq(SS_CSR(R_SER_STATUS)) : 0;
932 if ((s->ena & FMODE_READ) || (status & (M_SYNCSER_RX_EOP_COUNT))) {
934 hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
935 d->descrtab_phys) / sizeof(serdma_descr_t));
937 if (s->ena & FMODE_READ) {
938 CS_DBGOUT(CS_FUNCTION, 2,
939 printk(KERN_INFO "cs4297a: upd_rcv sw->hw->hw %x/%x/%x (int-%d)n",
940 d->swptr, d->hwptr, hwptr, intflag));
941 /* Number of DMA buffers available for software: */
942 diff2 = diff = (d->ringsz + hwptr - d->hwptr) % d->ringsz;
945 s_ptr = (u32 *)&(d->dma_buf[d->swptr*4]);
946 descr = &d->descrtab[d->swptr];
948 u64 data = be64_to_cpu(*(u64 *)s_ptr);
951 descr_a = descr->descr_a;
952 descr->descr_a &= ~M_DMA_SERRX_SOP;
953 if ((descr_a & M_DMA_DSCRA_A_ADDR) != CPHYSADDR((long)s_ptr)) {
954 printk(KERN_ERR "cs4297a: RX Bad address (read)\n");
956 if (((data & 0x9800000000000000) != 0x9800000000000000) ||
957 (!(descr_a & M_DMA_SERRX_SOP)) ||
958 (G_DMA_DSCRB_PKT_SIZE(descr->descr_b) != FRAME_BYTES)) {
960 printk(KERN_DEBUG "cs4297a: RX Bad attributes (read)\n");
964 if ((data >> 61) == 7) {
965 s->read_value = (data >> 12) & 0xffff;
966 s->read_reg = (data >> 40) & 0x7f;
967 wake_up(&d->reg_wait);
969 if (d->count && (d->sb_hwptr == d->sb_swptr)) {
970 s->stats.rx_overflow++;
971 printk(KERN_DEBUG "cs4297a: RX overflow\n");
975 left = ((be32_to_cpu(s_ptr[1]) & 0xff) << 8) |
976 ((be32_to_cpu(s_ptr[2]) >> 24) & 0xff);
977 right = (be32_to_cpu(s_ptr[2]) >> 4) & 0xffff;
978 *d->sb_hwptr++ = cpu_to_be16(left);
979 *d->sb_hwptr++ = cpu_to_be16(right);
980 if (d->sb_hwptr == d->sb_end)
981 d->sb_hwptr = d->sample_buf;
983 if (descr == d->descrtab_end) {
985 s_ptr = (u32 *)s->dma_adc.dma_buf;
990 d->total_bytes += good_diff * FRAME_SAMPLE_BYTES;
991 d->count += good_diff * FRAME_SAMPLE_BYTES;
992 if (d->count > d->sbufsz) {
993 printk(KERN_ERR "cs4297a: bogus receive overflow!!\n");
995 d->swptr = (d->swptr + diff) % d->ringsz;
996 __raw_writeq(diff, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
998 if (d->count >= (signed) d->fragsize)
1002 CS_DBGOUT(CS_WAVE_READ, 4,
1004 "cs4297a: update count -> %d\n", d->count));
1009 /* Receive is going even if no one is
1010 listening (for register accesses and to
1011 avoid FIFO overrun) */
1012 diff2 = diff = (hwptr + d->ringsz - d->hwptr) % d->ringsz;
1014 printk(KERN_ERR "cs4297a: RX full or empty?\n");
1017 descr = &d->descrtab[d->swptr];
1018 data_p = &d->dma_buf[d->swptr*4];
1020 /* Force this to happen at least once; I got
1021 here because of an interrupt, so there must
1022 be a buffer to process. */
1024 data = be64_to_cpu(*data_p);
1025 if ((descr->descr_a & M_DMA_DSCRA_A_ADDR) != CPHYSADDR((long)data_p)) {
1026 printk(KERN_ERR "cs4297a: RX Bad address %d (%llx %lx)\n", d->swptr,
1027 (long long)(descr->descr_a & M_DMA_DSCRA_A_ADDR),
1028 (long)CPHYSADDR((long)data_p));
1030 if (!(data & (1LL << 63)) ||
1031 !(descr->descr_a & M_DMA_SERRX_SOP) ||
1032 (G_DMA_DSCRB_PKT_SIZE(descr->descr_b) != FRAME_BYTES)) {
1034 printk(KERN_DEBUG "cs4297a: RX Bad attributes\n");
1037 if ((data >> 61) == 7) {
1038 s->read_value = (data >> 12) & 0xffff;
1039 s->read_reg = (data >> 40) & 0x7f;
1040 wake_up(&d->reg_wait);
1043 descr->descr_a &= ~M_DMA_SERRX_SOP;
1047 if (descr == d->descrtab_end) {
1048 descr = d->descrtab;
1050 data_p = d->dma_buf;
1052 __raw_writeq(1, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
1056 CS_DBGOUT(CS_DESCR, 6,
1057 printk(KERN_INFO "cs4297a: hw/sw %x/%x\n", d->hwptr, d->swptr));
1060 CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
1061 "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
1062 (unsigned)s, d->hwptr,
1063 d->total_bytes, d->count));
1066 /* XXXKW worry about s->reg_request -- there is a starvation
1067 case if s->ena has FMODE_WRITE on, but the client isn't
1070 // update DAC pointer
1072 // check for end of buffer, means that we are going to wait for another interrupt
1073 // to allow silence to fill the fifos on the part, to keep pops down to a minimum.
1075 if (s->ena & FMODE_WRITE) {
1076 serdma_t *d = &s->dma_dac;
1077 hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
1078 d->descrtab_phys) / sizeof(serdma_descr_t));
1079 diff = (d->ringsz + hwptr - d->hwptr) % d->ringsz;
1080 CS_DBGOUT(CS_WAVE_WRITE, 4, printk(KERN_INFO
1081 "cs4297a: cs4297a_update_ptr(): hw/hw/sw %x/%x/%x diff %d count %d\n",
1082 d->hwptr, hwptr, d->swptr, diff, d->count));
1084 /* XXXKW stereo? conversion? Just assume 2 16-bit samples for now */
1085 d->total_bytes += diff * FRAME_SAMPLE_BYTES;
1087 d->count += diff * FRAME_SAMPLE_BYTES;
1088 if (d->count >= d->fragsize) {
1091 if (d->count > d->sbufsz)
1092 d->count &= d->sbufsz - 1;
1095 d->count -= diff * FRAME_SAMPLE_BYTES;
1096 if (d->count <= 0) {
1098 // fill with silence, and do not shut down the DAC.
1099 // Continue to play silence until the _release.
1101 CS_DBGOUT(CS_WAVE_WRITE, 6, printk(KERN_INFO
1102 "cs4297a: cs4297a_update_ptr(): memset %d at 0x%.8x for %d size \n",
1103 (unsigned)(s->prop_dac.fmt &
1104 (AFMT_U8 | AFMT_U16_LE)) ? 0x80 : 0,
1105 (unsigned)d->dma_buf,
1107 memset(d->dma_buf, 0, d->ringsz * FRAME_BYTES);
1110 s->stats.tx_underrun++;
1112 CS_DBGOUT(CS_ERROR, 9, printk(KERN_INFO
1113 "cs4297a: cs4297a_update_ptr(): underrun\n"));
1115 } else if (d->count <=
1116 (signed) d->fragsize
1117 && !d->endcleared) {
1118 /* XXXKW what is this for? */
1119 clear_advance(d->dma_buf,
1126 if ( (d->count <= (signed) d->sbufsz/2) || intflag)
1128 CS_DBGOUT(CS_WAVE_WRITE, 4,
1130 "cs4297a: update count -> %d\n", d->count));
1134 CS_DBGOUT(CS_PARMS, 8, printk(KERN_INFO
1135 "cs4297a: cs4297a_update_ptr(): s=0x%.8x hwptr=%d total_bytes=%d count=%d \n",
1136 (unsigned) s, d->hwptr,
1137 d->total_bytes, d->count));
1141 static int mixer_ioctl(struct cs4297a_state *s, unsigned int cmd,
1144 // Index to mixer_src[] is value of AC97 Input Mux Select Reg.
1145 // Value of array member is recording source Device ID Mask.
1146 static const unsigned int mixer_src[8] = {
1147 SOUND_MASK_MIC, SOUND_MASK_CD, 0, SOUND_MASK_LINE1,
1148 SOUND_MASK_LINE, SOUND_MASK_VOLUME, 0, 0
1151 // Index of mixtable1[] member is Device ID
1152 // and must be <= SOUND_MIXER_NRDEVICES.
1153 // Value of array member is index into s->mix.vol[]
1154 static const unsigned char mixtable1[SOUND_MIXER_NRDEVICES] = {
1155 [SOUND_MIXER_PCM] = 1, // voice
1156 [SOUND_MIXER_LINE1] = 2, // AUX
1157 [SOUND_MIXER_CD] = 3, // CD
1158 [SOUND_MIXER_LINE] = 4, // Line
1159 [SOUND_MIXER_SYNTH] = 5, // FM
1160 [SOUND_MIXER_MIC] = 6, // Mic
1161 [SOUND_MIXER_SPEAKER] = 7, // Speaker
1162 [SOUND_MIXER_RECLEV] = 8, // Recording level
1163 [SOUND_MIXER_VOLUME] = 9 // Master Volume
1166 static const unsigned mixreg[] = {
1172 unsigned char l, r, rl, rr, vidx;
1173 unsigned char attentbl[11] =
1174 { 63, 42, 26, 17, 14, 11, 8, 6, 4, 2, 0 };
1179 CS_DBGOUT(CS_FUNCTION, 4, printk(KERN_INFO
1180 "cs4297a: mixer_ioctl(): s=0x%.8x cmd=0x%.8x\n",
1181 (unsigned) s, cmd));
1185 #if CSDEBUG_INTERFACE
1187 if ((cmd == SOUND_MIXER_CS_GETDBGMASK) ||
1188 (cmd == SOUND_MIXER_CS_SETDBGMASK) ||
1189 (cmd == SOUND_MIXER_CS_GETDBGLEVEL) ||
1190 (cmd == SOUND_MIXER_CS_SETDBGLEVEL))
1194 case SOUND_MIXER_CS_GETDBGMASK:
1195 return put_user(cs_debugmask,
1196 (unsigned long *) arg);
1198 case SOUND_MIXER_CS_GETDBGLEVEL:
1199 return put_user(cs_debuglevel,
1200 (unsigned long *) arg);
1202 case SOUND_MIXER_CS_SETDBGMASK:
1203 if (get_user(val, (unsigned long *) arg))
1208 case SOUND_MIXER_CS_SETDBGLEVEL:
1209 if (get_user(val, (unsigned long *) arg))
1211 cs_debuglevel = val;
1214 CS_DBGOUT(CS_ERROR, 1, printk(KERN_INFO
1215 "cs4297a: mixer_ioctl(): ERROR unknown debug cmd\n"));
1221 if (cmd == SOUND_MIXER_PRIVATE1) {
1224 if (cmd == SOUND_MIXER_PRIVATE2) {
1225 // enable/disable/query spatializer
1226 if (get_user(val, (int *) arg))
1229 temp1 = (val & 0x3f) >> 2;
1230 cs4297a_write_ac97(s, AC97_3D_CONTROL, temp1);
1231 cs4297a_read_ac97(s, AC97_GENERAL_PURPOSE,
1233 cs4297a_write_ac97(s, AC97_GENERAL_PURPOSE,
1236 cs4297a_read_ac97(s, AC97_3D_CONTROL, &temp1);
1237 return put_user((temp1 << 2) | 3, (int *) arg);
1239 if (cmd == SOUND_MIXER_INFO) {
1241 memset(&info, 0, sizeof(info));
1242 strlcpy(info.id, "CS4297a", sizeof(info.id));
1243 strlcpy(info.name, "Crystal CS4297a", sizeof(info.name));
1244 info.modify_counter = s->mix.modcnt;
1245 if (copy_to_user((void *) arg, &info, sizeof(info)))
1249 if (cmd == SOUND_OLD_MIXER_INFO) {
1250 _old_mixer_info info;
1251 memset(&info, 0, sizeof(info));
1252 strlcpy(info.id, "CS4297a", sizeof(info.id));
1253 strlcpy(info.name, "Crystal CS4297a", sizeof(info.name));
1254 if (copy_to_user((void *) arg, &info, sizeof(info)))
1258 if (cmd == OSS_GETVERSION)
1259 return put_user(SOUND_VERSION, (int *) arg);
1261 if (_IOC_TYPE(cmd) != 'M' || _SIOC_SIZE(cmd) != sizeof(int))
1264 // If ioctl has only the SIOC_READ bit(bit 31)
1265 // on, process the only-read commands.
1266 if (_SIOC_DIR(cmd) == _SIOC_READ) {
1267 switch (_IOC_NR(cmd)) {
1268 case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
1269 cs4297a_read_ac97(s, AC97_RECORD_SELECT,
1271 return put_user(mixer_src[temp1 & 7], (int *) arg);
1273 case SOUND_MIXER_DEVMASK: // Arg contains a bit for each supported device
1274 return put_user(SOUND_MASK_PCM | SOUND_MASK_LINE |
1275 SOUND_MASK_VOLUME | SOUND_MASK_RECLEV,
1278 case SOUND_MIXER_RECMASK: // Arg contains a bit for each supported recording source
1279 return put_user(SOUND_MASK_LINE | SOUND_MASK_VOLUME,
1282 case SOUND_MIXER_STEREODEVS: // Mixer channels supporting stereo
1283 return put_user(SOUND_MASK_PCM | SOUND_MASK_LINE |
1284 SOUND_MASK_VOLUME | SOUND_MASK_RECLEV,
1287 case SOUND_MIXER_CAPS:
1288 return put_user(SOUND_CAP_EXCL_INPUT, (int *) arg);
1292 if (i >= SOUND_MIXER_NRDEVICES
1293 || !(vidx = mixtable1[i]))
1295 return put_user(s->mix.vol[vidx - 1], (int *) arg);
1298 // If ioctl doesn't have both the SIOC_READ and
1299 // the SIOC_WRITE bit set, return invalid.
1300 if (_SIOC_DIR(cmd) != (_SIOC_READ | _SIOC_WRITE))
1303 // Increment the count of volume writes.
1306 // Isolate the command; it must be a write.
1307 switch (_IOC_NR(cmd)) {
1309 case SOUND_MIXER_RECSRC: // Arg contains a bit for each recording source
1310 if (get_user(val, (int *) arg))
1312 i = hweight32(val); // i = # bits on in val.
1313 if (i != 1) // One & only 1 bit must be on.
1315 for (i = 0; i < sizeof(mixer_src) / sizeof(int); i++) {
1316 if (val == mixer_src[i]) {
1317 temp1 = (i << 8) | i;
1318 cs4297a_write_ac97(s,
1326 case SOUND_MIXER_VOLUME:
1327 if (get_user(val, (int *) arg))
1331 l = 100; // Max soundcard.h vol is 100.
1336 rl = attentbl[(10 * l) / 100]; // Convert 0-100 vol to 63-0 atten.
1338 r = (val >> 8) & 0xff;
1340 r = 100; // Max right volume is 100, too
1345 rr = attentbl[(10 * r) / 100]; // Convert volume to attenuation.
1347 if ((rl > 60) && (rr > 60)) // If both l & r are 'low',
1348 temp1 = 0x8000; // turn on the mute bit.
1352 temp1 |= (rl << 8) | rr;
1354 cs4297a_write_ac97(s, AC97_MASTER_VOL_STEREO, temp1);
1355 cs4297a_write_ac97(s, AC97_PHONE_VOL, temp1);
1357 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1358 s->mix.vol[8] = ((unsigned int) r << 8) | l;
1360 s->mix.vol[8] = val;
1362 return put_user(s->mix.vol[8], (int *) arg);
1364 case SOUND_MIXER_SPEAKER:
1365 if (get_user(val, (int *) arg))
1374 rl = (l * 2 - 5) / 13; // Convert 0-100 range to 0-15.
1375 l = (rl * 13 + 5) / 2;
1383 rl = 15 - rl; // Convert volume to attenuation.
1385 cs4297a_write_ac97(s, AC97_PCBEEP_VOL, temp1);
1387 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1388 s->mix.vol[6] = l << 8;
1390 s->mix.vol[6] = val;
1392 return put_user(s->mix.vol[6], (int *) arg);
1394 case SOUND_MIXER_RECLEV:
1395 if (get_user(val, (int *) arg))
1400 r = (val >> 8) & 0xff;
1403 rl = (l * 2 - 5) / 13; // Convert 0-100 scale to 0-15.
1404 rr = (r * 2 - 5) / 13;
1405 if (rl < 3 && rr < 3)
1410 temp1 = temp1 | (rl << 8) | rr;
1411 cs4297a_write_ac97(s, AC97_RECORD_GAIN, temp1);
1413 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1414 s->mix.vol[7] = ((unsigned int) r << 8) | l;
1416 s->mix.vol[7] = val;
1418 return put_user(s->mix.vol[7], (int *) arg);
1420 case SOUND_MIXER_MIC:
1421 if (get_user(val, (int *) arg))
1430 rl = ((unsigned) l * 5 - 4) / 16; // Convert 0-100 range to 0-31.
1431 l = (rl * 16 + 4) / 5;
1433 cs4297a_read_ac97(s, AC97_MIC_VOL, &temp1);
1434 temp1 &= 0x40; // Isolate 20db gain bit.
1439 rl = 31 - rl; // Convert volume to attenuation.
1441 cs4297a_write_ac97(s, AC97_MIC_VOL, temp1);
1443 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1444 s->mix.vol[5] = val << 8;
1446 s->mix.vol[5] = val;
1448 return put_user(s->mix.vol[5], (int *) arg);
1451 case SOUND_MIXER_SYNTH:
1452 if (get_user(val, (int *) arg))
1457 if (get_user(val, (int *) arg))
1459 r = (val >> 8) & 0xff;
1462 rl = (l * 2 - 11) / 3; // Convert 0-100 range to 0-63.
1463 rr = (r * 2 - 11) / 3;
1464 if (rl < 3) // If l is low, turn on
1465 temp1 = 0x0080; // the mute bit.
1469 rl = 63 - rl; // Convert vol to attenuation.
1470 // writel(temp1 | rl, s->pBA0 + FMLVC);
1471 if (rr < 3) // If rr is low, turn on
1472 temp1 = 0x0080; // the mute bit.
1475 rr = 63 - rr; // Convert vol to attenuation.
1476 // writel(temp1 | rr, s->pBA0 + FMRVC);
1478 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1479 s->mix.vol[4] = (r << 8) | l;
1481 s->mix.vol[4] = val;
1483 return put_user(s->mix.vol[4], (int *) arg);
1487 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
1488 "cs4297a: mixer_ioctl(): default\n"));
1491 if (i >= SOUND_MIXER_NRDEVICES || !(vidx = mixtable1[i]))
1493 if (get_user(val, (int *) arg))
1502 rl = (attentbl[(l * 10) / 100]) >> 1;
1504 r = (val >> 8) & 0xff;
1511 rr = (attentbl[(r * 10) / 100]) >> 1;
1512 if ((rl > 30) && (rr > 30))
1516 temp1 = temp1 | (rl << 8) | rr;
1517 cs4297a_write_ac97(s, mixreg[vidx - 1], temp1);
1519 #ifdef OSS_DOCUMENTED_MIXER_SEMANTICS
1520 s->mix.vol[vidx - 1] = ((unsigned int) r << 8) | l;
1522 s->mix.vol[vidx - 1] = val;
1524 return put_user(s->mix.vol[vidx - 1], (int *) arg);
1529 // ---------------------------------------------------------------------
1531 static int cs4297a_open_mixdev(struct inode *inode, struct file *file)
1533 int minor = iminor(inode);
1534 struct cs4297a_state *s=NULL;
1535 struct list_head *entry;
1537 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
1538 printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()+\n"));
1540 list_for_each(entry, &cs4297a_devs)
1542 s = list_entry(entry, struct cs4297a_state, list);
1543 if(s->dev_mixer == minor)
1548 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2,
1549 printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()- -ENODEV\n"));
1553 file->private_data = s;
1555 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 4,
1556 printk(KERN_INFO "cs4297a: cs4297a_open_mixdev()- 0\n"));
1558 return nonseekable_open(inode, file);
1562 static int cs4297a_release_mixdev(struct inode *inode, struct file *file)
1564 struct cs4297a_state *s =
1565 (struct cs4297a_state *) file->private_data;
1572 static int cs4297a_ioctl_mixdev(struct inode *inode, struct file *file,
1573 unsigned int cmd, unsigned long arg)
1575 return mixer_ioctl((struct cs4297a_state *) file->private_data, cmd,
1580 // ******************************************************************************************
1581 // Mixer file operations struct.
1582 // ******************************************************************************************
1583 static const struct file_operations cs4297a_mixer_fops = {
1584 .owner = THIS_MODULE,
1585 .llseek = no_llseek,
1586 .ioctl = cs4297a_ioctl_mixdev,
1587 .open = cs4297a_open_mixdev,
1588 .release = cs4297a_release_mixdev,
1591 // ---------------------------------------------------------------------
1594 static int drain_adc(struct cs4297a_state *s, int nonblock)
1596 /* This routine serves no purpose currently - any samples
1597 sitting in the receive queue will just be processed by the
1598 background consumer. This would be different if DMA
1599 actually stopped when there were no clients. */
1603 static int drain_dac(struct cs4297a_state *s, int nonblock)
1605 DECLARE_WAITQUEUE(wait, current);
1606 unsigned long flags;
1611 if (s->dma_dac.mapped)
1615 add_wait_queue(&s->dma_dac.wait, &wait);
1616 while ((count = __raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX))) ||
1617 (s->dma_dac.count > 0)) {
1618 if (!signal_pending(current)) {
1619 set_current_state(TASK_INTERRUPTIBLE);
1620 /* XXXKW is this calculation working? */
1621 tmo = ((count * FRAME_TX_US) * HZ) / 1000000;
1622 schedule_timeout(tmo + 1);
1624 /* XXXKW do I care if there is a signal pending? */
1627 spin_lock_irqsave(&s->lock, flags);
1628 /* Reset the bookkeeping */
1629 hwptr = (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
1630 s->dma_dac.descrtab_phys) / sizeof(serdma_descr_t));
1631 s->dma_dac.hwptr = s->dma_dac.swptr = hwptr;
1632 spin_unlock_irqrestore(&s->lock, flags);
1633 remove_wait_queue(&s->dma_dac.wait, &wait);
1634 current->state = TASK_RUNNING;
1639 // ---------------------------------------------------------------------
1641 static ssize_t cs4297a_read(struct file *file, char *buffer, size_t count,
1644 struct cs4297a_state *s =
1645 (struct cs4297a_state *) file->private_data;
1647 unsigned long flags;
1648 int cnt, count_fr, cnt_by;
1649 unsigned copied = 0;
1651 CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
1652 printk(KERN_INFO "cs4297a: cs4297a_read()+ %d \n", count));
1655 if (s->dma_adc.mapped)
1657 if (!s->dma_adc.ready && (ret = prog_dmabuf_adc(s)))
1659 if (!access_ok(VERIFY_WRITE, buffer, count))
1663 // "count" is the amount of bytes to read (from app), is decremented each loop
1664 // by the amount of bytes that have been returned to the user buffer.
1665 // "cnt" is the running total of each read from the buffer (changes each loop)
1666 // "buffer" points to the app's buffer
1667 // "ret" keeps a running total of the amount of bytes that have been copied
1668 // to the user buffer.
1669 // "copied" is the total bytes copied into the user buffer for each loop.
1672 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1673 "_read() count>0 count=%d .count=%d .swptr=%d .hwptr=%d \n",
1674 count, s->dma_adc.count,
1675 s->dma_adc.swptr, s->dma_adc.hwptr));
1676 spin_lock_irqsave(&s->lock, flags);
1678 /* cnt will be the number of available samples (16-bit
1679 stereo); it starts out as the maxmimum consequetive
1681 cnt = (s->dma_adc.sb_end - s->dma_adc.sb_swptr) / 2;
1682 count_fr = s->dma_adc.count / FRAME_SAMPLE_BYTES;
1684 // dma_adc.count is the current total bytes that have not been read.
1685 // if the amount of unread bytes from the current sw pointer to the
1686 // end of the buffer is greater than the current total bytes that
1687 // have not been read, then set the "cnt" (unread bytes) to the
1688 // amount of unread bytes.
1692 cnt_by = cnt * FRAME_SAMPLE_BYTES;
1693 spin_unlock_irqrestore(&s->lock, flags);
1695 // if we are converting from 8/16 then we need to copy
1696 // twice the number of 16 bit bytes then 8 bit bytes.
1698 if (s->conversion) {
1699 if (cnt_by > (count * 2)) {
1700 cnt = (count * 2) / FRAME_SAMPLE_BYTES;
1704 if (cnt_by > count) {
1705 cnt = count / FRAME_SAMPLE_BYTES;
1710 // "cnt" NOW is the smaller of the amount that will be read,
1711 // and the amount that is requested in this read (or partial).
1712 // if there are no bytes in the buffer to read, then start the
1713 // ADC and wait for the interrupt handler to wake us up.
1717 // start up the dma engine and then continue back to the top of
1718 // the loop when wake up occurs.
1720 if (file->f_flags & O_NONBLOCK)
1721 return ret ? ret : -EAGAIN;
1722 interruptible_sleep_on(&s->dma_adc.wait);
1723 if (signal_pending(current))
1724 return ret ? ret : -ERESTARTSYS;
1727 // there are bytes in the buffer to read.
1728 // copy from the hw buffer over to the user buffer.
1729 // user buffer is designated by "buffer"
1730 // virtual address to copy from is dma_buf+swptr
1731 // the "cnt" is the number of bytes to read.
1733 CS_DBGOUT(CS_WAVE_READ, 2, printk(KERN_INFO
1734 "_read() copy_to cnt=%d count=%d ", cnt_by, count));
1735 CS_DBGOUT(CS_WAVE_READ, 8, printk(KERN_INFO
1736 " .sbufsz=%d .count=%d buffer=0x%.8x ret=%d\n",
1737 s->dma_adc.sbufsz, s->dma_adc.count,
1738 (unsigned) buffer, ret));
1740 if (copy_to_user (buffer, ((void *)s->dma_adc.sb_swptr), cnt_by))
1741 return ret ? ret : -EFAULT;
1744 /* Return the descriptors */
1745 spin_lock_irqsave(&s->lock, flags);
1746 CS_DBGOUT(CS_FUNCTION, 2,
1747 printk(KERN_INFO "cs4297a: upd_rcv sw->hw %x/%x\n", s->dma_adc.swptr, s->dma_adc.hwptr));
1748 s->dma_adc.count -= cnt_by;
1749 s->dma_adc.sb_swptr += cnt * 2;
1750 if (s->dma_adc.sb_swptr == s->dma_adc.sb_end)
1751 s->dma_adc.sb_swptr = s->dma_adc.sample_buf;
1752 spin_unlock_irqrestore(&s->lock, flags);
1758 CS_DBGOUT(CS_FUNCTION | CS_WAVE_READ, 2,
1759 printk(KERN_INFO "cs4297a: cs4297a_read()- %d\n", ret));
1764 static ssize_t cs4297a_write(struct file *file, const char *buffer,
1765 size_t count, loff_t * ppos)
1767 struct cs4297a_state *s =
1768 (struct cs4297a_state *) file->private_data;
1770 unsigned long flags;
1771 unsigned swptr, hwptr;
1774 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
1775 printk(KERN_INFO "cs4297a: cs4297a_write()+ count=%d\n",
1779 if (s->dma_dac.mapped)
1781 if (!s->dma_dac.ready && (ret = prog_dmabuf_dac(s)))
1783 if (!access_ok(VERIFY_READ, buffer, count))
1787 serdma_t *d = &s->dma_dac;
1792 int swap = (s->prop_dac.fmt == AFMT_S16_LE) || (s->prop_dac.fmt == AFMT_U16_LE);
1794 /* XXXXXX this is broken for BLOAT_FACTOR */
1795 spin_lock_irqsave(&s->lock, flags);
1798 d->swptr = d->hwptr;
1802 hwptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
1803 d->descrtab_phys) / sizeof(serdma_descr_t));
1804 d->swptr = d->hwptr = hwptr;
1807 cnt = d->sbufsz - (swptr * FRAME_SAMPLE_BYTES);
1808 /* Will this write fill up the buffer? */
1809 if (d->count + cnt > d->sbufsz)
1810 cnt = d->sbufsz - d->count;
1811 spin_unlock_irqrestore(&s->lock, flags);
1816 if (file->f_flags & O_NONBLOCK)
1817 return ret ? ret : -EAGAIN;
1818 interruptible_sleep_on(&d->wait);
1819 if (signal_pending(current))
1820 return ret ? ret : -ERESTARTSYS;
1823 if (copy_from_user(d->sample_buf, buffer, cnt))
1824 return ret ? ret : -EFAULT;
1827 s_tmpl = (u32 *)d->sample_buf;
1828 t_tmpl = (u32 *)(d->dma_buf + (swptr * 4));
1830 /* XXXKW assuming 16-bit stereo! */
1834 t_tmpl[0] = cpu_to_be32(0x98000000);
1836 tmp = be32_to_cpu(s_tmpl[0]);
1837 left = tmp & 0xffff;
1840 left = swab16(left);
1841 right = swab16(right);
1843 t_tmpl[1] = cpu_to_be32(left >> 8);
1844 t_tmpl[2] = cpu_to_be32(((left & 0xff) << 24) |
1852 /* Mux in any pending read/write accesses */
1853 if (s->reg_request) {
1854 *(u64 *)(d->dma_buf + (swptr * 4)) |=
1855 cpu_to_be64(s->reg_request);
1857 wake_up(&s->dma_dac.reg_wait);
1860 CS_DBGOUT(CS_WAVE_WRITE, 4,
1862 "cs4297a: copy in %d to swptr %x\n", cnt, swptr));
1864 swptr = (swptr + (cnt/FRAME_SAMPLE_BYTES)) % d->ringsz;
1865 __raw_writeq(cnt/FRAME_SAMPLE_BYTES, SS_CSR(R_SER_DMA_DSCR_COUNT_TX));
1866 spin_lock_irqsave(&s->lock, flags);
1870 spin_unlock_irqrestore(&s->lock, flags);
1876 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE, 2,
1877 printk(KERN_INFO "cs4297a: cs4297a_write()- %d\n", ret));
1882 static unsigned int cs4297a_poll(struct file *file,
1883 struct poll_table_struct *wait)
1885 struct cs4297a_state *s =
1886 (struct cs4297a_state *) file->private_data;
1887 unsigned long flags;
1888 unsigned int mask = 0;
1890 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
1891 printk(KERN_INFO "cs4297a: cs4297a_poll()+\n"));
1893 if (file->f_mode & FMODE_WRITE) {
1894 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
1896 "cs4297a: cs4297a_poll() wait on FMODE_WRITE\n"));
1897 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
1899 poll_wait(file, &s->dma_dac.wait, wait);
1901 if (file->f_mode & FMODE_READ) {
1902 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
1904 "cs4297a: cs4297a_poll() wait on FMODE_READ\n"));
1905 if(!s->dma_dac.ready && prog_dmabuf_adc(s))
1907 poll_wait(file, &s->dma_adc.wait, wait);
1909 spin_lock_irqsave(&s->lock, flags);
1910 cs4297a_update_ptr(s,CS_FALSE);
1911 if (file->f_mode & FMODE_WRITE) {
1912 if (s->dma_dac.mapped) {
1913 if (s->dma_dac.count >=
1914 (signed) s->dma_dac.fragsize) {
1915 if (s->dma_dac.wakeup)
1916 mask |= POLLOUT | POLLWRNORM;
1919 s->dma_dac.wakeup = 0;
1922 if ((signed) (s->dma_dac.sbufsz/2) >= s->dma_dac.count)
1923 mask |= POLLOUT | POLLWRNORM;
1925 } else if (file->f_mode & FMODE_READ) {
1926 if (s->dma_adc.mapped) {
1927 if (s->dma_adc.count >= (signed) s->dma_adc.fragsize)
1928 mask |= POLLIN | POLLRDNORM;
1930 if (s->dma_adc.count > 0)
1931 mask |= POLLIN | POLLRDNORM;
1934 spin_unlock_irqrestore(&s->lock, flags);
1935 CS_DBGOUT(CS_FUNCTION | CS_WAVE_WRITE | CS_WAVE_READ, 4,
1936 printk(KERN_INFO "cs4297a: cs4297a_poll()- 0x%.8x\n",
1942 static int cs4297a_mmap(struct file *file, struct vm_area_struct *vma)
1944 /* XXXKW currently no mmap support */
1950 static int cs4297a_ioctl(struct inode *inode, struct file *file,
1951 unsigned int cmd, unsigned long arg)
1953 struct cs4297a_state *s =
1954 (struct cs4297a_state *) file->private_data;
1955 unsigned long flags;
1956 audio_buf_info abinfo;
1958 int val, mapped, ret;
1960 CS_DBGOUT(CS_FUNCTION|CS_IOCTL, 4, printk(KERN_INFO
1961 "cs4297a: cs4297a_ioctl(): file=0x%.8x cmd=0x%.8x\n",
1962 (unsigned) file, cmd));
1967 mapped = ((file->f_mode & FMODE_WRITE) && s->dma_dac.mapped) ||
1968 ((file->f_mode & FMODE_READ) && s->dma_adc.mapped);
1970 case OSS_GETVERSION:
1971 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
1972 "cs4297a: cs4297a_ioctl(): SOUND_VERSION=0x%.8x\n",
1974 return put_user(SOUND_VERSION, (int *) arg);
1976 case SNDCTL_DSP_SYNC:
1977 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
1978 "cs4297a: cs4297a_ioctl(): DSP_SYNC\n"));
1979 if (file->f_mode & FMODE_WRITE)
1981 0 /*file->f_flags & O_NONBLOCK */
1985 case SNDCTL_DSP_SETDUPLEX:
1988 case SNDCTL_DSP_GETCAPS:
1989 return put_user(DSP_CAP_DUPLEX | DSP_CAP_REALTIME |
1990 DSP_CAP_TRIGGER | DSP_CAP_MMAP,
1993 case SNDCTL_DSP_RESET:
1994 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
1995 "cs4297a: cs4297a_ioctl(): DSP_RESET\n"));
1996 if (file->f_mode & FMODE_WRITE) {
1998 synchronize_irq(s->irq);
1999 s->dma_dac.count = s->dma_dac.total_bytes =
2000 s->dma_dac.blocks = s->dma_dac.wakeup = 0;
2001 s->dma_dac.swptr = s->dma_dac.hwptr =
2002 (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_TX)) & M_DMA_CURDSCR_ADDR) -
2003 s->dma_dac.descrtab_phys) / sizeof(serdma_descr_t));
2005 if (file->f_mode & FMODE_READ) {
2007 synchronize_irq(s->irq);
2008 s->dma_adc.count = s->dma_adc.total_bytes =
2009 s->dma_adc.blocks = s->dma_dac.wakeup = 0;
2010 s->dma_adc.swptr = s->dma_adc.hwptr =
2011 (int)(((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
2012 s->dma_adc.descrtab_phys) / sizeof(serdma_descr_t));
2016 case SNDCTL_DSP_SPEED:
2017 if (get_user(val, (int *) arg))
2019 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2020 "cs4297a: cs4297a_ioctl(): DSP_SPEED val=%d -> 48000\n", val));
2022 return put_user(val, (int *) arg);
2024 case SNDCTL_DSP_STEREO:
2025 if (get_user(val, (int *) arg))
2027 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2028 "cs4297a: cs4297a_ioctl(): DSP_STEREO val=%d\n", val));
2029 if (file->f_mode & FMODE_READ) {
2031 s->dma_adc.ready = 0;
2032 s->prop_adc.channels = val ? 2 : 1;
2034 if (file->f_mode & FMODE_WRITE) {
2036 s->dma_dac.ready = 0;
2037 s->prop_dac.channels = val ? 2 : 1;
2041 case SNDCTL_DSP_CHANNELS:
2042 if (get_user(val, (int *) arg))
2044 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2045 "cs4297a: cs4297a_ioctl(): DSP_CHANNELS val=%d\n",
2048 if (file->f_mode & FMODE_READ) {
2050 s->dma_adc.ready = 0;
2052 s->prop_adc.channels = 2;
2054 s->prop_adc.channels = 1;
2056 if (file->f_mode & FMODE_WRITE) {
2058 s->dma_dac.ready = 0;
2060 s->prop_dac.channels = 2;
2062 s->prop_dac.channels = 1;
2066 if (file->f_mode & FMODE_WRITE)
2067 val = s->prop_dac.channels;
2068 else if (file->f_mode & FMODE_READ)
2069 val = s->prop_adc.channels;
2071 return put_user(val, (int *) arg);
2073 case SNDCTL_DSP_GETFMTS: // Returns a mask
2074 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2075 "cs4297a: cs4297a_ioctl(): DSP_GETFMT val=0x%.8x\n",
2076 AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
2078 return put_user(AFMT_S16_LE | AFMT_U16_LE | AFMT_S8 |
2079 AFMT_U8, (int *) arg);
2081 case SNDCTL_DSP_SETFMT:
2082 if (get_user(val, (int *) arg))
2084 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2085 "cs4297a: cs4297a_ioctl(): DSP_SETFMT val=0x%.8x\n",
2087 if (val != AFMT_QUERY) {
2088 if (file->f_mode & FMODE_READ) {
2090 s->dma_adc.ready = 0;
2091 if (val != AFMT_S16_LE
2092 && val != AFMT_U16_LE && val != AFMT_S8
2095 s->prop_adc.fmt = val;
2096 s->prop_adc.fmt_original = s->prop_adc.fmt;
2098 if (file->f_mode & FMODE_WRITE) {
2100 s->dma_dac.ready = 0;
2101 if (val != AFMT_S16_LE
2102 && val != AFMT_U16_LE && val != AFMT_S8
2105 s->prop_dac.fmt = val;
2106 s->prop_dac.fmt_original = s->prop_dac.fmt;
2109 if (file->f_mode & FMODE_WRITE)
2110 val = s->prop_dac.fmt_original;
2111 else if (file->f_mode & FMODE_READ)
2112 val = s->prop_adc.fmt_original;
2114 CS_DBGOUT(CS_IOCTL | CS_PARMS, 4, printk(KERN_INFO
2115 "cs4297a: cs4297a_ioctl(): DSP_SETFMT return val=0x%.8x\n",
2117 return put_user(val, (int *) arg);
2119 case SNDCTL_DSP_POST:
2120 CS_DBGOUT(CS_IOCTL, 4, printk(KERN_INFO
2121 "cs4297a: cs4297a_ioctl(): DSP_POST\n"));
2124 case SNDCTL_DSP_GETTRIGGER:
2126 if (file->f_mode & s->ena & FMODE_READ)
2127 val |= PCM_ENABLE_INPUT;
2128 if (file->f_mode & s->ena & FMODE_WRITE)
2129 val |= PCM_ENABLE_OUTPUT;
2130 return put_user(val, (int *) arg);
2132 case SNDCTL_DSP_SETTRIGGER:
2133 if (get_user(val, (int *) arg))
2135 if (file->f_mode & FMODE_READ) {
2136 if (val & PCM_ENABLE_INPUT) {
2137 if (!s->dma_adc.ready
2138 && (ret = prog_dmabuf_adc(s)))
2144 if (file->f_mode & FMODE_WRITE) {
2145 if (val & PCM_ENABLE_OUTPUT) {
2146 if (!s->dma_dac.ready
2147 && (ret = prog_dmabuf_dac(s)))
2155 case SNDCTL_DSP_GETOSPACE:
2156 if (!(file->f_mode & FMODE_WRITE))
2158 if (!s->dma_dac.ready && (val = prog_dmabuf_dac(s)))
2160 spin_lock_irqsave(&s->lock, flags);
2161 cs4297a_update_ptr(s,CS_FALSE);
2162 abinfo.fragsize = s->dma_dac.fragsize;
2163 if (s->dma_dac.mapped)
2164 abinfo.bytes = s->dma_dac.sbufsz;
2167 s->dma_dac.sbufsz - s->dma_dac.count;
2168 abinfo.fragstotal = s->dma_dac.numfrag;
2169 abinfo.fragments = abinfo.bytes >> s->dma_dac.fragshift;
2170 CS_DBGOUT(CS_FUNCTION | CS_PARMS, 4, printk(KERN_INFO
2171 "cs4297a: cs4297a_ioctl(): GETOSPACE .fragsize=%d .bytes=%d .fragstotal=%d .fragments=%d\n",
2172 abinfo.fragsize,abinfo.bytes,abinfo.fragstotal,
2174 spin_unlock_irqrestore(&s->lock, flags);
2175 return copy_to_user((void *) arg, &abinfo,
2176 sizeof(abinfo)) ? -EFAULT : 0;
2178 case SNDCTL_DSP_GETISPACE:
2179 if (!(file->f_mode & FMODE_READ))
2181 if (!s->dma_adc.ready && (val = prog_dmabuf_adc(s)))
2183 spin_lock_irqsave(&s->lock, flags);
2184 cs4297a_update_ptr(s,CS_FALSE);
2185 if (s->conversion) {
2186 abinfo.fragsize = s->dma_adc.fragsize / 2;
2187 abinfo.bytes = s->dma_adc.count / 2;
2188 abinfo.fragstotal = s->dma_adc.numfrag;
2190 abinfo.bytes >> (s->dma_adc.fragshift - 1);
2192 abinfo.fragsize = s->dma_adc.fragsize;
2193 abinfo.bytes = s->dma_adc.count;
2194 abinfo.fragstotal = s->dma_adc.numfrag;
2196 abinfo.bytes >> s->dma_adc.fragshift;
2198 spin_unlock_irqrestore(&s->lock, flags);
2199 return copy_to_user((void *) arg, &abinfo,
2200 sizeof(abinfo)) ? -EFAULT : 0;
2202 case SNDCTL_DSP_NONBLOCK:
2203 file->f_flags |= O_NONBLOCK;
2206 case SNDCTL_DSP_GETODELAY:
2207 if (!(file->f_mode & FMODE_WRITE))
2209 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
2211 spin_lock_irqsave(&s->lock, flags);
2212 cs4297a_update_ptr(s,CS_FALSE);
2213 val = s->dma_dac.count;
2214 spin_unlock_irqrestore(&s->lock, flags);
2215 return put_user(val, (int *) arg);
2217 case SNDCTL_DSP_GETIPTR:
2218 if (!(file->f_mode & FMODE_READ))
2220 if(!s->dma_adc.ready && prog_dmabuf_adc(s))
2222 spin_lock_irqsave(&s->lock, flags);
2223 cs4297a_update_ptr(s,CS_FALSE);
2224 cinfo.bytes = s->dma_adc.total_bytes;
2225 if (s->dma_adc.mapped) {
2227 (cinfo.bytes >> s->dma_adc.fragshift) -
2230 cinfo.bytes >> s->dma_adc.fragshift;
2232 if (s->conversion) {
2235 2 >> (s->dma_adc.fragshift - 1);
2238 s->dma_adc.count >> s->dma_adc.
2242 cinfo.ptr = s->dma_adc.hwptr / 2;
2244 cinfo.ptr = s->dma_adc.hwptr;
2245 if (s->dma_adc.mapped)
2246 s->dma_adc.count &= s->dma_adc.fragsize - 1;
2247 spin_unlock_irqrestore(&s->lock, flags);
2248 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2250 case SNDCTL_DSP_GETOPTR:
2251 if (!(file->f_mode & FMODE_WRITE))
2253 if(!s->dma_dac.ready && prog_dmabuf_dac(s))
2255 spin_lock_irqsave(&s->lock, flags);
2256 cs4297a_update_ptr(s,CS_FALSE);
2257 cinfo.bytes = s->dma_dac.total_bytes;
2258 if (s->dma_dac.mapped) {
2260 (cinfo.bytes >> s->dma_dac.fragshift) -
2263 cinfo.bytes >> s->dma_dac.fragshift;
2266 s->dma_dac.count >> s->dma_dac.fragshift;
2268 cinfo.ptr = s->dma_dac.hwptr;
2269 if (s->dma_dac.mapped)
2270 s->dma_dac.count &= s->dma_dac.fragsize - 1;
2271 spin_unlock_irqrestore(&s->lock, flags);
2272 return copy_to_user((void *) arg, &cinfo, sizeof(cinfo)) ? -EFAULT : 0;
2274 case SNDCTL_DSP_GETBLKSIZE:
2275 if (file->f_mode & FMODE_WRITE) {
2276 if ((val = prog_dmabuf_dac(s)))
2278 return put_user(s->dma_dac.fragsize, (int *) arg);
2280 if ((val = prog_dmabuf_adc(s)))
2283 return put_user(s->dma_adc.fragsize / 2,
2286 return put_user(s->dma_adc.fragsize, (int *) arg);
2288 case SNDCTL_DSP_SETFRAGMENT:
2289 if (get_user(val, (int *) arg))
2291 return 0; // Say OK, but do nothing.
2293 case SNDCTL_DSP_SUBDIVIDE:
2294 if ((file->f_mode & FMODE_READ && s->dma_adc.subdivision)
2295 || (file->f_mode & FMODE_WRITE
2296 && s->dma_dac.subdivision)) return -EINVAL;
2297 if (get_user(val, (int *) arg))
2299 if (val != 1 && val != 2 && val != 4)
2301 if (file->f_mode & FMODE_READ)
2302 s->dma_adc.subdivision = val;
2303 else if (file->f_mode & FMODE_WRITE)
2304 s->dma_dac.subdivision = val;
2307 case SOUND_PCM_READ_RATE:
2308 if (file->f_mode & FMODE_READ)
2309 return put_user(s->prop_adc.rate, (int *) arg);
2310 else if (file->f_mode & FMODE_WRITE)
2311 return put_user(s->prop_dac.rate, (int *) arg);
2313 case SOUND_PCM_READ_CHANNELS:
2314 if (file->f_mode & FMODE_READ)
2315 return put_user(s->prop_adc.channels, (int *) arg);
2316 else if (file->f_mode & FMODE_WRITE)
2317 return put_user(s->prop_dac.channels, (int *) arg);
2319 case SOUND_PCM_READ_BITS:
2320 if (file->f_mode & FMODE_READ)
2324 fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
2326 else if (file->f_mode & FMODE_WRITE)
2330 fmt & (AFMT_S8 | AFMT_U8)) ? 8 : 16,
2333 case SOUND_PCM_WRITE_FILTER:
2334 case SNDCTL_DSP_SETSYNCRO:
2335 case SOUND_PCM_READ_FILTER:
2338 return mixer_ioctl(s, cmd, arg);
2342 static int cs4297a_release(struct inode *inode, struct file *file)
2344 struct cs4297a_state *s =
2345 (struct cs4297a_state *) file->private_data;
2347 CS_DBGOUT(CS_FUNCTION | CS_RELEASE, 2, printk(KERN_INFO
2348 "cs4297a: cs4297a_release(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
2349 (unsigned) inode, (unsigned) file, file->f_mode));
2352 if (file->f_mode & FMODE_WRITE) {
2353 drain_dac(s, file->f_flags & O_NONBLOCK);
2354 mutex_lock(&s->open_sem_dac);
2356 dealloc_dmabuf(s, &s->dma_dac);
2357 s->open_mode &= ~FMODE_WRITE;
2358 mutex_unlock(&s->open_sem_dac);
2359 wake_up(&s->open_wait_dac);
2361 if (file->f_mode & FMODE_READ) {
2362 drain_adc(s, file->f_flags & O_NONBLOCK);
2363 mutex_lock(&s->open_sem_adc);
2365 dealloc_dmabuf(s, &s->dma_adc);
2366 s->open_mode &= ~FMODE_READ;
2367 mutex_unlock(&s->open_sem_adc);
2368 wake_up(&s->open_wait_adc);
2373 static int cs4297a_open(struct inode *inode, struct file *file)
2375 int minor = iminor(inode);
2376 struct cs4297a_state *s=NULL;
2377 struct list_head *entry;
2379 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
2380 "cs4297a: cs4297a_open(): inode=0x%.8x file=0x%.8x f_mode=0x%x\n",
2381 (unsigned) inode, (unsigned) file, file->f_mode));
2382 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
2383 "cs4297a: status = %08x\n", (int)__raw_readq(SS_CSR(R_SER_STATUS_DEBUG))));
2385 list_for_each(entry, &cs4297a_devs)
2387 s = list_entry(entry, struct cs4297a_state, list);
2389 if (!((s->dev_audio ^ minor) & ~0xf))
2392 if (entry == &cs4297a_devs)
2395 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2, printk(KERN_INFO
2396 "cs4297a: cs4297a_open(): Error - unable to find audio state struct\n"));
2400 file->private_data = s;
2402 // wait for device to become free
2403 if (!(file->f_mode & (FMODE_WRITE | FMODE_READ))) {
2404 CS_DBGOUT(CS_FUNCTION | CS_OPEN | CS_ERROR, 2, printk(KERN_INFO
2405 "cs4297a: cs4297a_open(): Error - must open READ and/or WRITE\n"));
2408 if (file->f_mode & FMODE_WRITE) {
2409 if (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX)) != 0) {
2410 printk(KERN_ERR "cs4297a: TX pipe needs to drain\n");
2411 while (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_TX)))
2415 mutex_lock(&s->open_sem_dac);
2416 while (s->open_mode & FMODE_WRITE) {
2417 if (file->f_flags & O_NONBLOCK) {
2418 mutex_unlock(&s->open_sem_dac);
2421 mutex_unlock(&s->open_sem_dac);
2422 interruptible_sleep_on(&s->open_wait_dac);
2424 if (signal_pending(current)) {
2425 printk("open - sig pending\n");
2426 return -ERESTARTSYS;
2428 mutex_lock(&s->open_sem_dac);
2431 if (file->f_mode & FMODE_READ) {
2432 mutex_lock(&s->open_sem_adc);
2433 while (s->open_mode & FMODE_READ) {
2434 if (file->f_flags & O_NONBLOCK) {
2435 mutex_unlock(&s->open_sem_adc);
2438 mutex_unlock(&s->open_sem_adc);
2439 interruptible_sleep_on(&s->open_wait_adc);
2441 if (signal_pending(current)) {
2442 printk("open - sig pending\n");
2443 return -ERESTARTSYS;
2445 mutex_lock(&s->open_sem_adc);
2448 s->open_mode |= file->f_mode & (FMODE_READ | FMODE_WRITE);
2449 if (file->f_mode & FMODE_READ) {
2450 s->prop_adc.fmt = AFMT_S16_BE;
2451 s->prop_adc.fmt_original = s->prop_adc.fmt;
2452 s->prop_adc.channels = 2;
2453 s->prop_adc.rate = 48000;
2455 s->ena &= ~FMODE_READ;
2456 s->dma_adc.ossfragshift = s->dma_adc.ossmaxfrags =
2457 s->dma_adc.subdivision = 0;
2458 mutex_unlock(&s->open_sem_adc);
2460 if (prog_dmabuf_adc(s)) {
2461 CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
2462 "cs4297a: adc Program dmabufs failed.\n"));
2463 cs4297a_release(inode, file);
2467 if (file->f_mode & FMODE_WRITE) {
2468 s->prop_dac.fmt = AFMT_S16_BE;
2469 s->prop_dac.fmt_original = s->prop_dac.fmt;
2470 s->prop_dac.channels = 2;
2471 s->prop_dac.rate = 48000;
2473 s->ena &= ~FMODE_WRITE;
2474 s->dma_dac.ossfragshift = s->dma_dac.ossmaxfrags =
2475 s->dma_dac.subdivision = 0;
2476 mutex_unlock(&s->open_sem_dac);
2478 if (prog_dmabuf_dac(s)) {
2479 CS_DBGOUT(CS_OPEN | CS_ERROR, 2, printk(KERN_ERR
2480 "cs4297a: dac Program dmabufs failed.\n"));
2481 cs4297a_release(inode, file);
2485 CS_DBGOUT(CS_FUNCTION | CS_OPEN, 2,
2486 printk(KERN_INFO "cs4297a: cs4297a_open()- 0\n"));
2487 return nonseekable_open(inode, file);
2491 // ******************************************************************************************
2492 // Wave (audio) file operations struct.
2493 // ******************************************************************************************
2494 static const struct file_operations cs4297a_audio_fops = {
2495 .owner = THIS_MODULE,
2496 .llseek = no_llseek,
2497 .read = cs4297a_read,
2498 .write = cs4297a_write,
2499 .poll = cs4297a_poll,
2500 .ioctl = cs4297a_ioctl,
2501 .mmap = cs4297a_mmap,
2502 .open = cs4297a_open,
2503 .release = cs4297a_release,
2506 static void cs4297a_interrupt(int irq, void *dev_id)
2508 struct cs4297a_state *s = (struct cs4297a_state *) dev_id;
2511 status = __raw_readq(SS_CSR(R_SER_STATUS_DEBUG));
2513 CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
2514 "cs4297a: cs4297a_interrupt() HISR=0x%.8x\n", status));
2517 /* XXXKW what check *should* be done here? */
2518 if (!(status & (M_SYNCSER_RX_EOP_COUNT | M_SYNCSER_RX_OVERRUN | M_SYNCSER_RX_SYNC_ERR))) {
2519 status = __raw_readq(SS_CSR(R_SER_STATUS));
2520 printk(KERN_ERR "cs4297a: unexpected interrupt (status %08x)\n", status);
2525 if (status & M_SYNCSER_RX_SYNC_ERR) {
2526 status = __raw_readq(SS_CSR(R_SER_STATUS));
2527 printk(KERN_ERR "cs4297a: rx sync error (status %08x)\n", status);
2531 if (status & M_SYNCSER_RX_OVERRUN) {
2533 s->stats.rx_ovrrn++;
2534 printk(KERN_ERR "cs4297a: receive FIFO overrun\n");
2536 /* Fix things up: get the receive descriptor pool
2537 clean and give them back to the hardware */
2538 while (__raw_readq(SS_CSR(R_SER_DMA_DSCR_COUNT_RX)))
2540 newptr = (unsigned) (((__raw_readq(SS_CSR(R_SER_DMA_CUR_DSCR_ADDR_RX)) & M_DMA_CURDSCR_ADDR) -
2541 s->dma_adc.descrtab_phys) / sizeof(serdma_descr_t));
2542 for (i=0; i<DMA_DESCR; i++) {
2543 s->dma_adc.descrtab[i].descr_a &= ~M_DMA_SERRX_SOP;
2545 s->dma_adc.swptr = s->dma_adc.hwptr = newptr;
2546 s->dma_adc.count = 0;
2547 s->dma_adc.sb_swptr = s->dma_adc.sb_hwptr = s->dma_adc.sample_buf;
2548 __raw_writeq(DMA_DESCR, SS_CSR(R_SER_DMA_DSCR_COUNT_RX));
2551 spin_lock(&s->lock);
2552 cs4297a_update_ptr(s,CS_TRUE);
2553 spin_unlock(&s->lock);
2555 CS_DBGOUT(CS_INTERRUPT, 6, printk(KERN_INFO
2556 "cs4297a: cs4297a_interrupt()-\n"));
2560 static struct initvol {
2563 } initvol[] __initdata = {
2565 {SOUND_MIXER_WRITE_VOLUME, 0x4040},
2566 {SOUND_MIXER_WRITE_PCM, 0x4040},
2567 {SOUND_MIXER_WRITE_SYNTH, 0x4040},
2568 {SOUND_MIXER_WRITE_CD, 0x4040},
2569 {SOUND_MIXER_WRITE_LINE, 0x4040},
2570 {SOUND_MIXER_WRITE_LINE1, 0x4040},
2571 {SOUND_MIXER_WRITE_RECLEV, 0x0000},
2572 {SOUND_MIXER_WRITE_SPEAKER, 0x4040},
2573 {SOUND_MIXER_WRITE_MIC, 0x0000}
2577 static int __init cs4297a_init(void)
2579 struct cs4297a_state *s;
2583 #ifndef CONFIG_BCM_CS4297A_CSWARM
2588 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2, printk(KERN_INFO
2589 "cs4297a: cs4297a_init_module()+ \n"));
2591 #ifndef CONFIG_BCM_CS4297A_CSWARM
2592 mdio_val = __raw_readq(KSEG1 + A_MAC_REGISTER(2, R_MAC_MDIO)) &
2593 (M_MAC_MDIO_DIR|M_MAC_MDIO_OUT);
2595 /* Check syscfg for synchronous serial on port 1 */
2596 cfg = __raw_readq(KSEG1 + A_SCD_SYSTEM_CFG);
2597 if (!(cfg & M_SYS_SER1_ENABLE)) {
2598 __raw_writeq(cfg | M_SYS_SER1_ENABLE, KSEG1+A_SCD_SYSTEM_CFG);
2599 cfg = __raw_readq(KSEG1 + A_SCD_SYSTEM_CFG);
2600 if (!(cfg & M_SYS_SER1_ENABLE)) {
2601 printk(KERN_INFO "cs4297a: serial port 1 not configured for synchronous operation\n");
2605 printk(KERN_INFO "cs4297a: serial port 1 switching to synchronous operation\n");
2607 /* Force the codec (on SWARM) to reset by clearing
2608 GENO, preserving MDIO (no effect on CSWARM) */
2609 __raw_writeq(mdio_val, KSEG1+A_MAC_REGISTER(2, R_MAC_MDIO));
2614 __raw_writeq(mdio_val | M_MAC_GENC, KSEG1+A_MAC_REGISTER(2, R_MAC_MDIO));
2615 /* Give the codec some time to finish resetting (start the bit clock) */
2619 if (!(s = kzalloc(sizeof(struct cs4297a_state), GFP_KERNEL))) {
2620 CS_DBGOUT(CS_ERROR, 1, printk(KERN_ERR
2621 "cs4297a: probe() no memory for state struct.\n"));
2624 s->magic = CS4297a_MAGIC;
2625 init_waitqueue_head(&s->dma_adc.wait);
2626 init_waitqueue_head(&s->dma_dac.wait);
2627 init_waitqueue_head(&s->dma_adc.reg_wait);
2628 init_waitqueue_head(&s->dma_dac.reg_wait);
2629 init_waitqueue_head(&s->open_wait);
2630 init_waitqueue_head(&s->open_wait_adc);
2631 init_waitqueue_head(&s->open_wait_dac);
2632 mutex_init(&s->open_sem_adc);
2633 mutex_init(&s->open_sem_dac);
2634 spin_lock_init(&s->lock);
2636 s->irq = K_INT_SER_1;
2639 (s->irq, cs4297a_interrupt, 0, "Crystal CS4297a", s)) {
2640 CS_DBGOUT(CS_INIT | CS_ERROR, 1,
2641 printk(KERN_ERR "cs4297a: irq %u in use\n", s->irq));
2644 if ((s->dev_audio = register_sound_dsp(&cs4297a_audio_fops, -1)) <
2646 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
2647 "cs4297a: probe() register_sound_dsp() failed.\n"));
2650 if ((s->dev_mixer = register_sound_mixer(&cs4297a_mixer_fops, -1)) <
2652 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
2653 "cs4297a: probe() register_sound_mixer() failed.\n"));
2657 if (ser_init(s) || dma_init(s)) {
2658 CS_DBGOUT(CS_INIT | CS_ERROR, 1, printk(KERN_ERR
2659 "cs4297a: ser_init failed.\n"));
2665 rval = cs4297a_read_ac97(s, AC97_POWER_CONTROL, &pwr);
2666 } while (!rval && (pwr != 0xf));
2669 char *sb1250_duart_present;
2674 val = SOUND_MASK_LINE;
2675 mixer_ioctl(s, SOUND_MIXER_WRITE_RECSRC, (unsigned long) &val);
2676 for (i = 0; i < ARRAY_SIZE(initvol); i++) {
2677 val = initvol[i].vol;
2678 mixer_ioctl(s, initvol[i].mixch, (unsigned long) &val);
2680 // cs4297a_write_ac97(s, 0x18, 0x0808);
2682 // cs4297a_write_ac97(s, 0x5e, 0x180);
2683 cs4297a_write_ac97(s, 0x02, 0x0808);
2684 cs4297a_write_ac97(s, 0x18, 0x0808);
2688 list_add(&s->list, &cs4297a_devs);
2690 cs4297a_read_ac97(s, AC97_VENDOR_ID1, &id);
2692 sb1250_duart_present = symbol_get(sb1250_duart_present);
2693 if (sb1250_duart_present)
2694 sb1250_duart_present[1] = 0;
2696 printk(KERN_INFO "cs4297a: initialized (vendor id = %x)\n", id);
2698 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
2699 printk(KERN_INFO "cs4297a: cs4297a_init_module()-\n"));
2705 unregister_sound_mixer(s->dev_mixer);
2707 unregister_sound_dsp(s->dev_audio);
2709 free_irq(s->irq, s);
2713 printk(KERN_INFO "cs4297a: initialization failed\n");
2718 static void __exit cs4297a_cleanup(void)
2722 disable_irq, free_irq
2728 CS_DBGOUT(CS_INIT | CS_FUNCTION, 2,
2729 printk(KERN_INFO "cs4297a: cleanup_cs4297a() finished\n"));
2732 // ---------------------------------------------------------------------
2734 MODULE_AUTHOR("Kip Walker, Broadcom Corp.");
2735 MODULE_DESCRIPTION("Cirrus Logic CS4297a Driver for Broadcom SWARM board");
2737 // ---------------------------------------------------------------------
2739 module_init(cs4297a_init);
2740 module_exit(cs4297a_cleanup);