2 /* This is a modified version of linux/drivers/sound/dmasound.c to
3 * support the CS4218 codec on the 8xx TDM port. Thanks to everyone
4 * that contributed to the dmasound software (which includes me :-).
6 * The CS4218 is configured in Mode 4, sub-mode 0. This provides
7 * left/right data only on the TDM port, as a 32-bit word, per frame
8 * pulse. The control of the CS4218 is provided by some other means,
10 * Dan Malek (dmalek@jlc.net)
13 #include <linux/module.h>
14 #include <linux/sched.h>
15 #include <linux/timer.h>
16 #include <linux/major.h>
17 #include <linux/config.h>
18 #include <linux/fcntl.h>
19 #include <linux/errno.h>
21 #include <linux/slab.h>
22 #include <linux/sound.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
26 #include <asm/system.h>
28 #include <asm/pgtable.h>
29 #include <asm/uaccess.h>
32 /* Should probably do something different with this path name.....
33 * Actually, I should just stop using it...
36 #include <linux/soundcard.h>
38 #include <asm/mpc8xx.h>
39 #include <asm/8xx_immap.h>
40 #include <asm/commproc.h>
42 #define DMASND_CS4218 5
44 #define MAX_CATCH_RADIUS 10
47 #define MAX_BUFSIZE 128
49 #define HAS_8BIT_TABLES
51 static int sq_unit = -1;
52 static int mixer_unit = -1;
53 static int state_unit = -1;
54 static int irq_installed = 0;
55 static char **sound_buffers = NULL;
56 static char **sound_read_buffers = NULL;
58 /* Local copies of things we put in the control register. Output
59 * volume, like most codecs is really attenuation.
61 static int cs4218_rate_index;
64 * Stuff for outputting a beep. The values range from -327 to +327
65 * so we can multiply by an amplitude in the range 0..100 to get a
66 * signed short value to put in the output buffer.
68 static short beep_wform[256] = {
69 0, 40, 79, 117, 153, 187, 218, 245,
70 269, 288, 304, 316, 323, 327, 327, 324,
71 318, 310, 299, 288, 275, 262, 249, 236,
72 224, 213, 204, 196, 190, 186, 183, 182,
73 182, 183, 186, 189, 192, 196, 200, 203,
74 206, 208, 209, 209, 209, 207, 204, 201,
75 197, 193, 188, 183, 179, 174, 170, 166,
76 163, 161, 160, 159, 159, 160, 161, 162,
77 164, 166, 168, 169, 171, 171, 171, 170,
78 169, 167, 163, 159, 155, 150, 144, 139,
79 133, 128, 122, 117, 113, 110, 107, 105,
80 103, 103, 103, 103, 104, 104, 105, 105,
81 105, 103, 101, 97, 92, 86, 78, 68,
82 58, 45, 32, 18, 3, -11, -26, -41,
83 -55, -68, -79, -88, -95, -100, -102, -102,
84 -99, -93, -85, -75, -62, -48, -33, -16,
85 0, 16, 33, 48, 62, 75, 85, 93,
86 99, 102, 102, 100, 95, 88, 79, 68,
87 55, 41, 26, 11, -3, -18, -32, -45,
88 -58, -68, -78, -86, -92, -97, -101, -103,
89 -105, -105, -105, -104, -104, -103, -103, -103,
90 -103, -105, -107, -110, -113, -117, -122, -128,
91 -133, -139, -144, -150, -155, -159, -163, -167,
92 -169, -170, -171, -171, -171, -169, -168, -166,
93 -164, -162, -161, -160, -159, -159, -160, -161,
94 -163, -166, -170, -174, -179, -183, -188, -193,
95 -197, -201, -204, -207, -209, -209, -209, -208,
96 -206, -203, -200, -196, -192, -189, -186, -183,
97 -182, -182, -183, -186, -190, -196, -204, -213,
98 -224, -236, -249, -262, -275, -288, -299, -310,
99 -318, -324, -327, -327, -323, -316, -304, -288,
100 -269, -245, -218, -187, -153, -117, -79, -40,
103 #define BEEP_SPEED 5 /* 22050 Hz sample rate */
104 #define BEEP_BUFLEN 512
105 #define BEEP_VOLUME 15 /* 0 - 100 */
107 static int beep_volume = BEEP_VOLUME;
108 static int beep_playing = 0;
109 static int beep_state = 0;
110 static short *beep_buf;
111 static void (*orig_mksound)(unsigned int, unsigned int);
113 /* This is found someplace else......I guess in the keyboard driver
116 static void (*kd_mksound)(unsigned int, unsigned int);
118 static int catchRadius = 0;
119 static int numBufs = 4, bufSize = 32;
120 static int numReadBufs = 4, readbufSize = 32;
123 /* TDM/Serial transmit and receive buffer descriptors.
125 static volatile cbd_t *rx_base, *rx_cur, *tx_base, *tx_cur;
127 MODULE_PARM(catchRadius, "i");
128 MODULE_PARM(numBufs, "i");
129 MODULE_PARM(bufSize, "i");
130 MODULE_PARM(numreadBufs, "i");
131 MODULE_PARM(readbufSize, "i");
133 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
134 #define le2be16(x) (((x)<<8 & 0xff00) | ((x)>>8 & 0x00ff))
135 #define le2be16dbl(x) (((x)<<8 & 0xff00ff00) | ((x)>>8 & 0x00ff00ff))
137 #define IOCTL_IN(arg, ret) \
138 do { int error = get_user(ret, (int *)(arg)); \
139 if (error) return error; \
141 #define IOCTL_OUT(arg, ret) ioctl_return((int *)(arg), ret)
143 /* CS4218 serial port control in mode 4.
145 #define CS_INTMASK ((uint)0x40000000)
146 #define CS_DO1 ((uint)0x20000000)
147 #define CS_LATTEN ((uint)0x1f000000)
148 #define CS_RATTEN ((uint)0x00f80000)
149 #define CS_MUTE ((uint)0x00040000)
150 #define CS_ISL ((uint)0x00020000)
151 #define CS_ISR ((uint)0x00010000)
152 #define CS_LGAIN ((uint)0x0000f000)
153 #define CS_RGAIN ((uint)0x00000f00)
155 #define CS_LATTEN_SET(X) (((X) & 0x1f) << 24)
156 #define CS_RATTEN_SET(X) (((X) & 0x1f) << 19)
157 #define CS_LGAIN_SET(X) (((X) & 0x0f) << 12)
158 #define CS_RGAIN_SET(X) (((X) & 0x0f) << 8)
160 #define CS_LATTEN_GET(X) (((X) >> 24) & 0x1f)
161 #define CS_RATTEN_GET(X) (((X) >> 19) & 0x1f)
162 #define CS_LGAIN_GET(X) (((X) >> 12) & 0x0f)
163 #define CS_RGAIN_GET(X) (((X) >> 8) & 0x0f)
165 /* The control register is effectively write only. We have to keep a copy
168 static uint cs4218_control;
170 /* A place to store expanding information.
172 static int expand_bal;
173 static int expand_data;
175 /* Since I can't make the microcode patch work for the SPI, I just
176 * clock the bits using software.
178 static void sw_spi_init(void);
179 static void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt);
180 static uint cs4218_ctl_write(uint ctlreg);
182 /*** Some low level helpers **************************************************/
186 static short ulaw2dma16[] = {
187 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
188 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
189 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
190 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
191 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
192 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
193 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
194 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
195 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
196 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
197 -876, -844, -812, -780, -748, -716, -684, -652,
198 -620, -588, -556, -524, -492, -460, -428, -396,
199 -372, -356, -340, -324, -308, -292, -276, -260,
200 -244, -228, -212, -196, -180, -164, -148, -132,
201 -120, -112, -104, -96, -88, -80, -72, -64,
202 -56, -48, -40, -32, -24, -16, -8, 0,
203 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
204 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
205 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
206 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
207 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
208 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
209 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
210 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
211 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
212 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
213 876, 844, 812, 780, 748, 716, 684, 652,
214 620, 588, 556, 524, 492, 460, 428, 396,
215 372, 356, 340, 324, 308, 292, 276, 260,
216 244, 228, 212, 196, 180, 164, 148, 132,
217 120, 112, 104, 96, 88, 80, 72, 64,
218 56, 48, 40, 32, 24, 16, 8, 0,
223 static short alaw2dma16[] = {
224 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
225 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
226 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
227 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
228 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
229 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
230 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
231 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
232 -344, -328, -376, -360, -280, -264, -312, -296,
233 -472, -456, -504, -488, -408, -392, -440, -424,
234 -88, -72, -120, -104, -24, -8, -56, -40,
235 -216, -200, -248, -232, -152, -136, -184, -168,
236 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
237 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
238 -688, -656, -752, -720, -560, -528, -624, -592,
239 -944, -912, -1008, -976, -816, -784, -880, -848,
240 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
241 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
242 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
243 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
244 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
245 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
246 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
247 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
248 344, 328, 376, 360, 280, 264, 312, 296,
249 472, 456, 504, 488, 408, 392, 440, 424,
250 88, 72, 120, 104, 24, 8, 56, 40,
251 216, 200, 248, 232, 152, 136, 184, 168,
252 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
253 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
254 688, 656, 752, 720, 560, 528, 624, 592,
255 944, 912, 1008, 976, 816, 784, 880, 848,
259 /*** Translations ************************************************************/
262 static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
263 u_char frame[], ssize_t *frameUsed,
265 static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
266 u_char frame[], ssize_t *frameUsed,
268 static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
269 u_char frame[], ssize_t *frameUsed,
271 static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
272 u_char frame[], ssize_t *frameUsed,
274 static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
275 u_char frame[], ssize_t *frameUsed,
277 static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
278 u_char frame[], ssize_t *frameUsed,
280 static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
281 u_char frame[], ssize_t *frameUsed,
283 static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
284 u_char frame[], ssize_t *frameUsed,
286 static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
287 u_char frame[], ssize_t *frameUsed,
289 static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
290 u_char frame[], ssize_t *frameUsed,
292 static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
293 u_char frame[], ssize_t *frameUsed,
295 static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
296 u_char frame[], ssize_t *frameUsed,
300 /*** Low level stuff *********************************************************/
302 struct cs_sound_settings {
303 MACHINE mach; /* machine dependent things */
304 SETTINGS hard; /* hardware settings */
305 SETTINGS soft; /* software settings */
306 SETTINGS dsp; /* /dev/dsp default settings */
307 TRANS *trans_write; /* supported translations for playback */
308 TRANS *trans_read; /* supported translations for record */
309 int volume_left; /* volume (range is machine dependent) */
311 int bass; /* tone (range is machine dependent) */
314 int minDev; /* minor device number currently open */
317 static struct cs_sound_settings sound;
319 static void *CS_Alloc(unsigned int size, int flags);
320 static void CS_Free(void *ptr, unsigned int size);
321 static int CS_IrqInit(void);
323 static void CS_IrqCleanup(void);
325 static void CS_Silence(void);
326 static void CS_Init(void);
327 static void CS_Play(void);
328 static void CS_Record(void);
329 static int CS_SetFormat(int format);
330 static int CS_SetVolume(int volume);
331 static void cs4218_tdm_tx_intr(void *devid);
332 static void cs4218_tdm_rx_intr(void *devid);
333 static void cs4218_intr(void *devid, struct pt_regs *regs);
334 static int cs_get_volume(uint reg);
335 static int cs_volume_setter(int volume, int mute);
336 static int cs_get_gain(uint reg);
337 static int cs_set_gain(int gain);
338 static void cs_mksound(unsigned int hz, unsigned int ticks);
339 static void cs_nosound(unsigned long xx);
341 /*** Mid level stuff *********************************************************/
344 static void sound_silence(void);
345 static void sound_init(void);
346 static int sound_set_format(int format);
347 static int sound_set_speed(int speed);
348 static int sound_set_stereo(int stereo);
349 static int sound_set_volume(int volume);
351 static ssize_t sound_copy_translate(const u_char *userPtr,
353 u_char frame[], ssize_t *frameUsed,
355 static ssize_t sound_copy_translate_read(const u_char *userPtr,
357 u_char frame[], ssize_t *frameUsed,
362 * /dev/mixer abstraction
370 static struct sound_mixer mixer;
372 static struct sound_queue sq;
373 static struct sound_queue read_sq;
375 #define sq_block_address(i) (sq.buffers[i])
376 #define SIGNAL_RECEIVED (signal_pending(current))
377 #define NON_BLOCKING(open_mode) (open_mode & O_NONBLOCK)
378 #define ONE_SECOND HZ /* in jiffies (100ths of a second) */
379 #define NO_TIME_LIMIT 0xffffffff
391 static struct sound_state state;
393 /*** Common stuff ********************************************************/
395 static long long sound_lseek(struct file *file, long long offset, int orig);
397 /*** Config & Setup **********************************************************/
399 void dmasound_setup(char *str, int *ints);
401 /*** Translations ************************************************************/
404 /* ++TeSche: radically changed for new expanding purposes...
406 * These two routines now deal with copying/expanding/translating the samples
407 * from user space into our buffer at the right frequency. They take care about
408 * how much data there's actually to read, how much buffer space there is and
409 * to convert samples into the right frequency/encoding. They will only work on
410 * complete samples so it may happen they leave some bytes in the input stream
411 * if the user didn't write a multiple of the current sample size. They both
412 * return the number of bytes they've used from both streams so you may detect
413 * such a situation. Luckily all programs should be able to cope with that.
415 * I think I've optimized anything as far as one can do in plain C, all
416 * variables should fit in registers and the loops are really short. There's
417 * one loop for every possible situation. Writing a more generalized and thus
418 * parameterized loop would only produce slower code. Feel free to optimize
419 * this in assembler if you like. :)
421 * I think these routines belong here because they're not yet really hardware
422 * independent, especially the fact that the Falcon can play 16bit samples
423 * only in stereo is hardcoded in both of them!
425 * ++geert: split in even more functions (one per format)
428 static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
429 u_char frame[], ssize_t *frameUsed,
432 short *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16;
434 short *p = (short *) &frame[*frameUsed];
435 int val, stereo = sound.soft.stereo;
440 used = count = min(userCount, frameLeft);
443 if (get_user(data, userPtr++))
448 if (get_user(data, userPtr++))
455 *frameUsed += used * 4;
456 return stereo? used * 2: used;
460 static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
461 u_char frame[], ssize_t *frameUsed,
465 short *p = (short *) &frame[*frameUsed];
466 int val, stereo = sound.soft.stereo;
471 used = count = min(userCount, frameLeft);
474 if (get_user(data, userPtr++))
479 if (get_user(data, userPtr++))
486 *frameUsed += used * 4;
487 return stereo? used * 2: used;
491 static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
492 u_char frame[], ssize_t *frameUsed,
496 short *p = (short *) &frame[*frameUsed];
497 int val, stereo = sound.soft.stereo;
502 used = count = min(userCount, frameLeft);
505 if (get_user(data, userPtr++))
507 val = (data ^ 0x80) << 8;
510 if (get_user(data, userPtr++))
512 val = (data ^ 0x80) << 8;
517 *frameUsed += used * 4;
518 return stereo? used * 2: used;
522 /* This is the default format of the codec. Signed, 16-bit stereo
523 * generated by an application shouldn't have to be copied at all.
524 * We should just get the phsical address of the buffers and update
525 * the TDM BDs directly.
527 static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
528 u_char frame[], ssize_t *frameUsed,
532 int stereo = sound.soft.stereo;
533 short *fp = (short *) &frame[*frameUsed];
536 userCount >>= (stereo? 2: 1);
537 used = count = min(userCount, frameLeft);
539 short *up = (short *) userPtr;
542 if (get_user(data, up++))
549 if (copy_from_user(fp, userPtr, count * 4))
552 *frameUsed += used * 4;
553 return stereo? used * 4: used * 2;
556 static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
557 u_char frame[], ssize_t *frameUsed,
561 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
562 int stereo = sound.soft.stereo;
563 short *fp = (short *) &frame[*frameUsed];
564 short *up = (short *) userPtr;
567 userCount >>= (stereo? 2: 1);
568 used = count = min(userCount, frameLeft);
571 if (get_user(data, up++))
576 if (get_user(data, up++))
583 *frameUsed += used * 4;
584 return stereo? used * 4: used * 2;
588 static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
589 u_char frame[], ssize_t *frameUsed,
592 unsigned short *table = (unsigned short *)
593 (sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16);
594 unsigned int data = expand_data;
595 unsigned int *p = (unsigned int *) &frame[*frameUsed];
596 int bal = expand_bal;
597 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
599 int stereo = sound.soft.stereo;
611 if (get_user(c, userPtr++))
615 if (get_user(c, userPtr++))
617 data = (data << 16) + table[c];
619 data = (data << 16) + data;
629 *frameUsed += (ftotal - frameLeft) * 4;
631 return stereo? utotal * 2: utotal;
635 static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
636 u_char frame[], ssize_t *frameUsed,
639 unsigned int *p = (unsigned int *) &frame[*frameUsed];
640 unsigned int data = expand_data;
641 int bal = expand_bal;
642 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
643 int stereo = sound.soft.stereo;
656 if (get_user(c, userPtr++))
660 if (get_user(c, userPtr++))
662 data = (data << 16) + (c << 8);
664 data = (data << 16) + data;
674 *frameUsed += (ftotal - frameLeft) * 4;
676 return stereo? utotal * 2: utotal;
680 static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
681 u_char frame[], ssize_t *frameUsed,
684 unsigned int *p = (unsigned int *) &frame[*frameUsed];
685 unsigned int data = expand_data;
686 int bal = expand_bal;
687 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
688 int stereo = sound.soft.stereo;
701 if (get_user(c, userPtr++))
703 data = (c ^ 0x80) << 8;
705 if (get_user(c, userPtr++))
707 data = (data << 16) + ((c ^ 0x80) << 8);
709 data = (data << 16) + data;
719 *frameUsed += (ftotal - frameLeft) * 4;
721 return stereo? utotal * 2: utotal;
725 static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
726 u_char frame[], ssize_t *frameUsed,
729 unsigned int *p = (unsigned int *) &frame[*frameUsed];
730 unsigned int data = expand_data;
731 unsigned short *up = (unsigned short *) userPtr;
732 int bal = expand_bal;
733 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
734 int stereo = sound.soft.stereo;
738 userCount >>= (stereo? 2: 1);
746 if (get_user(data, up++))
749 if (get_user(c, up++))
751 data = (data << 16) + c;
753 data = (data << 16) + data;
763 *frameUsed += (ftotal - frameLeft) * 4;
765 return stereo? utotal * 4: utotal * 2;
769 static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
770 u_char frame[], ssize_t *frameUsed,
773 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
774 unsigned int *p = (unsigned int *) &frame[*frameUsed];
775 unsigned int data = expand_data;
776 unsigned short *up = (unsigned short *) userPtr;
777 int bal = expand_bal;
778 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
779 int stereo = sound.soft.stereo;
783 userCount >>= (stereo? 2: 1);
791 if (get_user(data, up++))
795 if (get_user(c, up++))
797 data = (data << 16) + (c ^ mask);
799 data = (data << 16) + data;
809 *frameUsed += (ftotal - frameLeft) * 4;
811 return stereo? utotal * 4: utotal * 2;
814 static ssize_t cs4218_ct_s8_read(const u_char *userPtr, size_t userCount,
815 u_char frame[], ssize_t *frameUsed,
819 short *p = (short *) &frame[*frameUsed];
820 int val, stereo = sound.soft.stereo;
825 used = count = min(userCount, frameLeft);
831 if (put_user(data, (u_char *)userPtr++))
836 if (put_user(data, (u_char *)userPtr++))
842 *frameUsed += used * 4;
843 return stereo? used * 2: used;
847 static ssize_t cs4218_ct_u8_read(const u_char *userPtr, size_t userCount,
848 u_char frame[], ssize_t *frameUsed,
852 short *p = (short *) &frame[*frameUsed];
853 int val, stereo = sound.soft.stereo;
858 used = count = min(userCount, frameLeft);
863 data = (val >> 8) ^ 0x80;
864 if (put_user(data, (u_char *)userPtr++))
868 data = (val >> 8) ^ 0x80;
869 if (put_user(data, (u_char *)userPtr++))
875 *frameUsed += used * 4;
876 return stereo? used * 2: used;
880 static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
881 u_char frame[], ssize_t *frameUsed,
885 int stereo = sound.soft.stereo;
886 short *fp = (short *) &frame[*frameUsed];
889 userCount >>= (stereo? 2: 1);
890 used = count = min(userCount, frameLeft);
892 short *up = (short *) userPtr;
896 if (put_user(data, up++))
902 if (copy_to_user((u_char *)userPtr, fp, count * 4))
905 *frameUsed += used * 4;
906 return stereo? used * 4: used * 2;
909 static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
910 u_char frame[], ssize_t *frameUsed,
914 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
915 int stereo = sound.soft.stereo;
916 short *fp = (short *) &frame[*frameUsed];
917 short *up = (short *) userPtr;
920 userCount >>= (stereo? 2: 1);
921 used = count = min(userCount, frameLeft);
927 if (put_user(data, up++))
932 if (put_user(data, up++))
938 *frameUsed += used * 4;
939 return stereo? used * 4: used * 2;
942 static TRANS transCSNormal = {
943 cs4218_ct_law, cs4218_ct_law, cs4218_ct_s8, cs4218_ct_u8,
944 cs4218_ct_s16, cs4218_ct_u16, cs4218_ct_s16, cs4218_ct_u16
947 static TRANS transCSExpand = {
948 cs4218_ctx_law, cs4218_ctx_law, cs4218_ctx_s8, cs4218_ctx_u8,
949 cs4218_ctx_s16, cs4218_ctx_u16, cs4218_ctx_s16, cs4218_ctx_u16
952 static TRANS transCSNormalRead = {
953 NULL, NULL, cs4218_ct_s8_read, cs4218_ct_u8_read,
954 cs4218_ct_s16_read, cs4218_ct_u16_read,
955 cs4218_ct_s16_read, cs4218_ct_u16_read
958 /*** Low level stuff *********************************************************/
960 static void *CS_Alloc(unsigned int size, int flags)
965 for (order=0; order < 5; order++) {
970 return (void *)__get_free_pages(flags, order);
973 static void CS_Free(void *ptr, unsigned int size)
978 for (order=0; order < 5; order++) {
983 free_pages((ulong)ptr, order);
986 static int __init CS_IrqInit(void)
988 cpm_install_handler(CPMVEC_SMC2, cs4218_intr, NULL);
993 static void CS_IrqCleanup(void)
996 volatile cpm8xx_t *cp;
998 /* First disable transmitter and receiver.
1000 sp = &cpmp->cp_smc[1];
1001 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1003 /* And now shut down the SMC.
1005 cp = cpmp; /* Get pointer to Communication Processor */
1006 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
1007 CPM_CR_STOP_TX) | CPM_CR_FLG;
1008 while (cp->cp_cpcr & CPM_CR_FLG);
1010 /* Release the interrupt handler.
1012 cpm_free_handler(CPMVEC_SMC2);
1016 kd_mksound = orig_mksound;
1020 static void CS_Silence(void)
1024 /* Disable transmitter.
1026 sp = &cpmp->cp_smc[1];
1027 sp->smc_smcmr &= ~SMCMR_TEN;
1030 /* Frequencies depend upon external oscillator. There are two
1031 * choices, 12.288 and 11.2896 MHz. The RPCG audio supports both through
1032 * and external control register selection bit.
1034 static int cs4218_freqs[] = {
1035 /* 12.288 11.2896 */
1046 static void CS_Init(void)
1050 switch (sound.soft.format) {
1053 sound.hard.format = AFMT_S16_LE;
1056 sound.hard.format = AFMT_S16_BE;
1059 sound.hard.stereo = 1;
1060 sound.hard.size = 16;
1063 * If we have a sample rate which is within catchRadius percent
1064 * of the requested value, we don't have to expand the samples.
1065 * Otherwise choose the next higher rate.
1067 i = (sizeof(cs4218_freqs) / sizeof(int));
1069 tolerance = catchRadius * cs4218_freqs[--i] / 100;
1070 } while (sound.soft.speed > cs4218_freqs[i] + tolerance && i > 0);
1071 if (sound.soft.speed >= cs4218_freqs[i] - tolerance)
1072 sound.trans_write = &transCSNormal;
1074 sound.trans_write = &transCSExpand;
1075 sound.trans_read = &transCSNormalRead;
1076 sound.hard.speed = cs4218_freqs[i];
1077 cs4218_rate_index = i;
1079 /* The CS4218 has seven selectable clock dividers for the sample
1080 * clock. The HIOX then provides one of two external rates.
1081 * An even numbered frequency table index uses the high external
1084 *(uint *)HIOX_CSR4_ADDR &= ~(HIOX_CSR4_AUDCLKHI | HIOX_CSR4_AUDCLKSEL);
1086 *(uint *)HIOX_CSR4_ADDR |= HIOX_CSR4_AUDCLKHI;
1088 *(uint *)HIOX_CSR4_ADDR |= (i & HIOX_CSR4_AUDCLKSEL);
1090 expand_bal = -sound.soft.speed;
1093 static int CS_SetFormat(int format)
1099 return sound.soft.format;
1113 printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
1119 sound.soft.format = format;
1120 sound.soft.size = size;
1121 if (sound.minDev == SND_DEV_DSP) {
1122 sound.dsp.format = format;
1123 sound.dsp.size = size;
1131 /* Volume is the amount of attenuation we tell the codec to impose
1132 * on the outputs. There are 32 levels, with 0 the "loudest".
1134 #define CS_VOLUME_TO_MASK(x) (31 - ((((x) - 1) * 31) / 99))
1135 #define CS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 31))
1137 static int cs_get_volume(uint reg)
1141 volume = CS_MASK_TO_VOLUME(CS_LATTEN_GET(reg));
1142 volume |= CS_MASK_TO_VOLUME(CS_RATTEN_GET(reg)) << 8;
1146 static int cs_volume_setter(int volume, int mute)
1150 if (mute && volume == 0) {
1151 tempctl = cs4218_control | CS_MUTE;
1153 tempctl = cs4218_control & ~CS_MUTE;
1154 tempctl = tempctl & ~(CS_LATTEN | CS_RATTEN);
1155 tempctl |= CS_LATTEN_SET(CS_VOLUME_TO_MASK(volume & 0xff));
1156 tempctl |= CS_RATTEN_SET(CS_VOLUME_TO_MASK((volume >> 8) & 0xff));
1157 volume = cs_get_volume(tempctl);
1159 if (tempctl != cs4218_control) {
1160 cs4218_ctl_write(tempctl);
1166 /* Gain has 16 steps from 0 to 15. These are in 1.5dB increments from
1167 * 0 (no gain) to 22.5 dB.
1169 #define CS_RECLEVEL_TO_GAIN(v) \
1170 ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
1171 #define CS_GAIN_TO_RECLEVEL(v) (((v) * 20 + 2) / 3)
1173 static int cs_get_gain(uint reg)
1177 gain = CS_GAIN_TO_RECLEVEL(CS_LGAIN_GET(reg));
1178 gain |= CS_GAIN_TO_RECLEVEL(CS_RGAIN_GET(reg)) << 8;
1182 static int cs_set_gain(int gain)
1186 tempctl = cs4218_control & ~(CS_LGAIN | CS_RGAIN);
1187 tempctl |= CS_LGAIN_SET(CS_RECLEVEL_TO_GAIN(gain & 0xff));
1188 tempctl |= CS_RGAIN_SET(CS_RECLEVEL_TO_GAIN((gain >> 8) & 0xff));
1189 gain = cs_get_gain(tempctl);
1191 if (tempctl != cs4218_control) {
1192 cs4218_ctl_write(tempctl);
1197 static int CS_SetVolume(int volume)
1199 return cs_volume_setter(volume, CS_MUTE);
1202 static void CS_Play(void)
1205 unsigned long flags;
1206 volatile cbd_t *bdp;
1207 volatile cpm8xx_t *cp;
1209 save_flags(flags); cli();
1211 if (awacs_beep_state) {
1212 /* sound takes precedence over beeps */
1213 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
1214 out_le32(&awacs->control,
1215 (in_le32(&awacs->control) & ~0x1f00)
1216 | (awacs_rate_index << 8));
1217 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
1218 out_le32(&awacs_txdma->cmdptr, virt_to_bus(&(awacs_tx_cmds[(sq.front+sq.active) % sq.max_count])));
1221 awacs_beep_state = 0;
1224 i = sq.front + sq.active;
1225 if (i >= sq.max_count)
1227 while (sq.active < 2 && sq.active < sq.count) {
1228 count = (sq.count == sq.active + 1)?sq.rear_size:sq.block_size;
1229 if (count < sq.block_size && !sq.syncing)
1230 /* last block not yet filled, and we're not syncing. */
1234 bdp->cbd_datlen = count;
1236 flush_dcache_range((ulong)sound_buffers[i],
1237 (ulong)(sound_buffers[i] + count));
1239 if (++i >= sq.max_count)
1242 if (sq.active == 0) {
1243 /* The SMC does not load its fifo until the first
1244 * TDM frame pulse, so the transmit data gets shifted
1245 * by one word. To compensate for this, we incorrectly
1246 * transmit the first buffer and shorten it by one
1247 * word. Subsequent buffers are then aligned properly.
1249 bdp->cbd_datlen -= 2;
1251 /* Start up the SMC Transmitter.
1254 cp->cp_smc[1].smc_smcmr |= SMCMR_TEN;
1255 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
1256 CPM_CR_RESTART_TX) | CPM_CR_FLG;
1257 while (cp->cp_cpcr & CPM_CR_FLG);
1260 /* Buffer is ready now.
1262 bdp->cbd_sc |= BD_SC_READY;
1266 restore_flags(flags);
1270 static void CS_Record(void)
1272 unsigned long flags;
1278 save_flags(flags); cli();
1280 /* This is all we have to do......Just start it up.
1282 sp = &cpmp->cp_smc[1];
1283 sp->smc_smcmr |= SMCMR_REN;
1287 restore_flags(flags);
1292 cs4218_tdm_tx_intr(void *devid)
1295 volatile cbd_t *bdp;
1297 while (sq.active > 0) {
1299 if (bdp->cbd_sc & BD_SC_READY)
1300 break; /* this frame is still going */
1303 if (++i >= sq.max_count)
1307 WAKE_UP(sq.action_queue);
1313 WAKE_UP(sq.sync_queue);
1318 cs4218_tdm_rx_intr(void *devid)
1321 /* We want to blow 'em off when shutting down.
1323 if (read_sq.active == 0)
1326 /* Check multiple buffers in case we were held off from
1327 * interrupt processing for a long time. Geeze, I really hope
1328 * this doesn't happen.
1330 while ((rx_base[read_sq.rear].cbd_sc & BD_SC_EMPTY) == 0) {
1332 /* Invalidate the data cache range for this buffer.
1334 invalidate_dcache_range(
1335 (uint)(sound_read_buffers[read_sq.rear]),
1336 (uint)(sound_read_buffers[read_sq.rear] + read_sq.block_size));
1338 /* Make buffer available again and move on.
1340 rx_base[read_sq.rear].cbd_sc |= BD_SC_EMPTY;
1343 /* Wrap the buffer ring.
1345 if (read_sq.rear >= read_sq.max_active)
1348 /* If we have caught up to the front buffer, bump it.
1349 * This will cause weird (but not fatal) results if the
1350 * read loop is currently using this buffer. The user is
1351 * behind in this case anyway, so weird things are going
1354 if (read_sq.rear == read_sq.front) {
1356 if (read_sq.front >= read_sq.max_active)
1361 WAKE_UP(read_sq.action_queue);
1364 static void cs_nosound(unsigned long xx)
1366 unsigned long flags;
1368 save_flags(flags); cli();
1371 st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
1375 restore_flags(flags);
1378 static struct timer_list beep_timer = TIMER_INITIALIZER(cs_nosound, 0, 0);
1381 static void cs_mksound(unsigned int hz, unsigned int ticks)
1383 unsigned long flags;
1384 int beep_speed = BEEP_SPEED;
1385 int srate = cs4218_freqs[beep_speed];
1386 int period, ncycles, nsamples;
1389 static int beep_hz_cache;
1390 static int beep_nsamples_cache;
1391 static int beep_volume_cache;
1393 if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
1395 /* this is a hack for broken X server code */
1399 /* cancel beep currently playing */
1404 save_flags(flags); cli();
1405 del_timer(&beep_timer);
1407 beep_timer.expires = jiffies + ticks;
1408 add_timer(&beep_timer);
1410 if (beep_playing || sq.active || beep_buf == NULL) {
1411 restore_flags(flags);
1412 return; /* too hard, sorry :-( */
1416 st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
1418 restore_flags(flags);
1420 if (hz == beep_hz_cache && beep_volume == beep_volume_cache) {
1421 nsamples = beep_nsamples_cache;
1423 period = srate * 256 / hz; /* fixed point */
1424 ncycles = BEEP_BUFLEN * 256 / period;
1425 nsamples = (period * ncycles) >> 8;
1426 f = ncycles * 65536 / nsamples;
1429 for (i = 0; i < nsamples; ++i, p += 2) {
1430 p[0] = p[1] = beep_wform[j >> 8] * beep_volume;
1431 j = (j + f) & 0xffff;
1434 beep_volume_cache = beep_volume;
1435 beep_nsamples_cache = nsamples;
1439 st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
1440 st_le16(&beep_dbdma_cmd->xfer_status, 0);
1441 st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
1442 st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
1443 awacs_beep_state = 1;
1445 save_flags(flags); cli();
1446 if (beep_playing) { /* i.e. haven't been terminated already */
1447 out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
1448 out_le32(&awacs->control,
1449 (in_le32(&awacs->control) & ~0x1f00)
1450 | (beep_speed << 8));
1451 out_le32(&awacs->byteswap, 0);
1452 out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
1453 out_le32(&awacs_txdma->control, RUN | (RUN << 16));
1456 restore_flags(flags);
1459 static MACHINE mach_cs4218 = {
1460 .owner = THIS_MODULE,
1461 .name = "HIOX CS4218",
1462 .name2 = "Built-in Sound",
1463 .dma_alloc = CS_Alloc,
1464 .dma_free = CS_Free,
1465 .irqinit = CS_IrqInit,
1467 .irqcleanup = CS_IrqCleanup,
1470 .silence = CS_Silence,
1471 .setFormat = CS_SetFormat,
1472 .setVolume = CS_SetVolume,
1477 /*** Mid level stuff *********************************************************/
1480 static void sound_silence(void)
1482 /* update hardware settings one more */
1483 (*sound.mach.init)();
1485 (*sound.mach.silence)();
1489 static void sound_init(void)
1491 (*sound.mach.init)();
1495 static int sound_set_format(int format)
1497 return(*sound.mach.setFormat)(format);
1501 static int sound_set_speed(int speed)
1504 return(sound.soft.speed);
1506 sound.soft.speed = speed;
1507 (*sound.mach.init)();
1508 if (sound.minDev == SND_DEV_DSP)
1509 sound.dsp.speed = sound.soft.speed;
1511 return(sound.soft.speed);
1515 static int sound_set_stereo(int stereo)
1518 return(sound.soft.stereo);
1520 stereo = !!stereo; /* should be 0 or 1 now */
1522 sound.soft.stereo = stereo;
1523 if (sound.minDev == SND_DEV_DSP)
1524 sound.dsp.stereo = stereo;
1525 (*sound.mach.init)();
1531 static int sound_set_volume(int volume)
1533 return(*sound.mach.setVolume)(volume);
1536 static ssize_t sound_copy_translate(const u_char *userPtr,
1538 u_char frame[], ssize_t *frameUsed,
1541 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
1543 switch (sound.soft.format) {
1545 ct_func = sound.trans_write->ct_ulaw;
1548 ct_func = sound.trans_write->ct_alaw;
1551 ct_func = sound.trans_write->ct_s8;
1554 ct_func = sound.trans_write->ct_u8;
1557 ct_func = sound.trans_write->ct_s16be;
1560 ct_func = sound.trans_write->ct_u16be;
1563 ct_func = sound.trans_write->ct_s16le;
1566 ct_func = sound.trans_write->ct_u16le;
1570 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
1575 static ssize_t sound_copy_translate_read(const u_char *userPtr,
1577 u_char frame[], ssize_t *frameUsed,
1580 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
1582 switch (sound.soft.format) {
1584 ct_func = sound.trans_read->ct_ulaw;
1587 ct_func = sound.trans_read->ct_alaw;
1590 ct_func = sound.trans_read->ct_s8;
1593 ct_func = sound.trans_read->ct_u8;
1596 ct_func = sound.trans_read->ct_s16be;
1599 ct_func = sound.trans_read->ct_u16be;
1602 ct_func = sound.trans_read->ct_s16le;
1605 ct_func = sound.trans_read->ct_u16le;
1609 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
1616 * /dev/mixer abstraction
1619 static int mixer_open(struct inode *inode, struct file *file)
1622 return nonseekable_open(inode, file);
1626 static int mixer_release(struct inode *inode, struct file *file)
1633 static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
1639 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
1640 mixer.modify_counter++;
1641 if (cmd == OSS_GETVERSION)
1642 return IOCTL_OUT(arg, SOUND_VERSION);
1644 case SOUND_MIXER_INFO: {
1646 strlcpy(info.id, "CS4218_TDM", sizeof(info.id));
1647 strlcpy(info.name, "CS4218_TDM", sizeof(info.name));
1648 info.name[sizeof(info.name)-1] = 0;
1649 info.modify_counter = mixer.modify_counter;
1650 if (copy_to_user((int *)arg, &info, sizeof(info)))
1654 case SOUND_MIXER_READ_DEVMASK:
1655 data = SOUND_MASK_VOLUME | SOUND_MASK_LINE
1656 | SOUND_MASK_MIC | SOUND_MASK_RECLEV
1657 | SOUND_MASK_ALTPCM;
1658 return IOCTL_OUT(arg, data);
1659 case SOUND_MIXER_READ_RECMASK:
1660 data = SOUND_MASK_LINE | SOUND_MASK_MIC;
1661 return IOCTL_OUT(arg, data);
1662 case SOUND_MIXER_READ_RECSRC:
1663 if (cs4218_control & CS_DO1)
1664 data = SOUND_MASK_LINE;
1666 data = SOUND_MASK_MIC;
1667 return IOCTL_OUT(arg, data);
1668 case SOUND_MIXER_WRITE_RECSRC:
1669 IOCTL_IN(arg, data);
1670 data &= (SOUND_MASK_LINE | SOUND_MASK_MIC);
1671 if (data & SOUND_MASK_LINE)
1672 tmpcs = cs4218_control |
1673 (CS_ISL | CS_ISR | CS_DO1);
1674 if (data & SOUND_MASK_MIC)
1675 tmpcs = cs4218_control &
1676 ~(CS_ISL | CS_ISR | CS_DO1);
1677 if (tmpcs != cs4218_control)
1678 cs4218_ctl_write(tmpcs);
1679 return IOCTL_OUT(arg, data);
1680 case SOUND_MIXER_READ_STEREODEVS:
1681 data = SOUND_MASK_VOLUME | SOUND_MASK_RECLEV;
1682 return IOCTL_OUT(arg, data);
1683 case SOUND_MIXER_READ_CAPS:
1684 return IOCTL_OUT(arg, 0);
1685 case SOUND_MIXER_READ_VOLUME:
1686 data = (cs4218_control & CS_MUTE)? 0:
1687 cs_get_volume(cs4218_control);
1688 return IOCTL_OUT(arg, data);
1689 case SOUND_MIXER_WRITE_VOLUME:
1690 IOCTL_IN(arg, data);
1691 return IOCTL_OUT(arg, sound_set_volume(data));
1692 case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
1693 IOCTL_IN(arg, data);
1694 beep_volume = data & 0xff;
1696 case SOUND_MIXER_READ_ALTPCM:
1697 return IOCTL_OUT(arg, beep_volume);
1698 case SOUND_MIXER_WRITE_RECLEV:
1699 IOCTL_IN(arg, data);
1700 data = cs_set_gain(data);
1701 return IOCTL_OUT(arg, data);
1702 case SOUND_MIXER_READ_RECLEV:
1703 data = cs_get_gain(cs4218_control);
1704 return IOCTL_OUT(arg, data);
1711 static struct file_operations mixer_fops =
1713 .owner = THIS_MODULE,
1714 .llseek = sound_lseek,
1715 .ioctl = mixer_ioctl,
1717 .release = mixer_release,
1721 static void __init mixer_init(void)
1723 mixer_unit = register_sound_mixer(&mixer_fops, -1);
1731 /* Set Line input, no gain, no attenuation.
1733 cs4218_control = CS_ISL | CS_ISR | CS_DO1;
1734 cs4218_control |= CS_LGAIN_SET(0) | CS_RGAIN_SET(0);
1735 cs4218_control |= CS_LATTEN_SET(0) | CS_RATTEN_SET(0);
1736 cs4218_ctl_write(cs4218_control);
1741 * Sound queue stuff, the heart of the driver
1745 static int sq_allocate_buffers(void)
1751 sound_buffers = kmalloc (numBufs * sizeof(char *), GFP_KERNEL);
1754 for (i = 0; i < numBufs; i++) {
1755 sound_buffers[i] = sound.mach.dma_alloc (bufSize << 10, GFP_KERNEL);
1756 if (!sound_buffers[i]) {
1758 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
1759 kfree (sound_buffers);
1768 static void sq_release_buffers(void)
1772 if (sound_buffers) {
1773 for (i = 0; i < numBufs; i++)
1774 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
1775 kfree (sound_buffers);
1781 static int sq_allocate_read_buffers(void)
1785 if (sound_read_buffers)
1787 sound_read_buffers = kmalloc(numReadBufs * sizeof(char *), GFP_KERNEL);
1788 if (!sound_read_buffers)
1790 for (i = 0; i < numBufs; i++) {
1791 sound_read_buffers[i] = sound.mach.dma_alloc (readbufSize<<10,
1793 if (!sound_read_buffers[i]) {
1795 sound.mach.dma_free (sound_read_buffers[i],
1797 kfree (sound_read_buffers);
1798 sound_read_buffers = 0;
1805 static void sq_release_read_buffers(void)
1809 if (sound_read_buffers) {
1810 cpmp->cp_smc[1].smc_smcmr &= ~SMCMR_REN;
1811 for (i = 0; i < numReadBufs; i++)
1812 sound.mach.dma_free (sound_read_buffers[i],
1814 kfree (sound_read_buffers);
1815 sound_read_buffers = 0;
1820 static void sq_setup(int numBufs, int bufSize, char **write_buffers)
1823 volatile cbd_t *bdp;
1824 volatile cpm8xx_t *cp;
1827 /* Make sure the SMC transmit is shut down.
1830 sp = &cpmp->cp_smc[1];
1831 sp->smc_smcmr &= ~SMCMR_TEN;
1833 sq.max_count = numBufs;
1834 sq.max_active = numBufs;
1835 sq.block_size = bufSize;
1836 sq.buffers = write_buffers;
1838 sq.front = sq.count = 0;
1844 for (i=0; i<numBufs; i++) {
1845 bdp->cbd_bufaddr = virt_to_bus(write_buffers[i]);
1849 /* This causes the SMC to sync up with the first buffer again.
1851 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_TX) | CPM_CR_FLG;
1852 while (cp->cp_cpcr & CPM_CR_FLG);
1855 static void read_sq_setup(int numBufs, int bufSize, char **read_buffers)
1858 volatile cbd_t *bdp;
1859 volatile cpm8xx_t *cp;
1862 /* Make sure the SMC receive is shut down.
1865 sp = &cpmp->cp_smc[1];
1866 sp->smc_smcmr &= ~SMCMR_REN;
1868 read_sq.max_count = numBufs;
1869 read_sq.max_active = numBufs;
1870 read_sq.block_size = bufSize;
1871 read_sq.buffers = read_buffers;
1873 read_sq.front = read_sq.count = 0;
1875 read_sq.rear_size = 0;
1876 read_sq.syncing = 0;
1880 for (i=0; i<numReadBufs; i++) {
1881 bdp->cbd_bufaddr = virt_to_bus(read_buffers[i]);
1882 bdp->cbd_datlen = read_sq.block_size;
1886 /* This causes the SMC to sync up with the first buffer again.
1888 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_RX) | CPM_CR_FLG;
1889 while (cp->cp_cpcr & CPM_CR_FLG);
1893 static void sq_play(void)
1895 (*sound.mach.play)();
1899 /* ++TeSche: radically changed this one too */
1901 static ssize_t sq_write(struct file *file, const char *src, size_t uLeft,
1904 ssize_t uWritten = 0;
1906 ssize_t uUsed, bUsed, bLeft;
1908 /* ++TeSche: Is something like this necessary?
1909 * Hey, that's an honest question! Or does any other part of the
1910 * filesystem already checks this situation? I really don't know.
1915 /* The interrupt doesn't start to play the last, incomplete frame.
1916 * Thus we can append to it without disabling the interrupts! (Note
1917 * also that sq.rear isn't affected by the interrupt.)
1920 if (sq.count > 0 && (bLeft = sq.block_size-sq.rear_size) > 0) {
1921 dest = sq_block_address(sq.rear);
1922 bUsed = sq.rear_size;
1923 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
1929 sq.rear_size = bUsed;
1933 while (sq.count == sq.max_active) {
1935 if (NON_BLOCKING(sq.open_mode))
1936 return uWritten > 0 ? uWritten : -EAGAIN;
1937 SLEEP(sq.action_queue);
1938 if (SIGNAL_RECEIVED)
1939 return uWritten > 0 ? uWritten : -EINTR;
1942 /* Here, we can avoid disabling the interrupt by first
1943 * copying and translating the data, and then updating
1944 * the sq variables. Until this is done, the interrupt
1945 * won't see the new frame and we can work on it
1949 dest = sq_block_address((sq.rear+1) % sq.max_count);
1951 bLeft = sq.block_size;
1952 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
1959 sq.rear = (sq.rear+1) % sq.max_count;
1960 sq.rear_size = bUsed;
1963 } while (bUsed); /* uUsed may have been 0 */
1967 return uUsed < 0? uUsed: uWritten;
1973 /* Here is how the values are used for reading.
1974 * The value 'active' simply indicates the DMA is running. This is
1975 * done so the driver semantics are DMA starts when the first read is
1976 * posted. The value 'front' indicates the buffer we should next
1977 * send to the user. The value 'rear' indicates the buffer the DMA is
1978 * currently filling. When 'front' == 'rear' the buffer "ring" is
1979 * empty (we always have an empty available). The 'rear_size' is used
1980 * to track partial offsets into the current buffer. Right now, I just keep
1981 * The DMA running. If the reader can't keep up, the interrupt tosses
1982 * the oldest buffer. We could also shut down the DMA in this case.
1984 static ssize_t sq_read(struct file *file, char *dst, size_t uLeft,
1988 ssize_t uRead, bLeft, bUsed, uUsed;
1993 if (!read_sq.active)
1994 CS_Record(); /* Kick off the record process. */
1998 /* Move what the user requests, depending upon other options.
2002 /* When front == rear, the DMA is not done yet.
2004 while (read_sq.front == read_sq.rear) {
2005 if (NON_BLOCKING(read_sq.open_mode)) {
2006 return uRead > 0 ? uRead : -EAGAIN;
2008 SLEEP(read_sq.action_queue);
2009 if (SIGNAL_RECEIVED)
2010 return uRead > 0 ? uRead : -EINTR;
2013 /* The amount we move is either what is left in the
2014 * current buffer or what the user wants.
2016 bLeft = read_sq.block_size - read_sq.rear_size;
2017 bUsed = read_sq.rear_size;
2018 uUsed = sound_copy_translate_read(dst, uLeft,
2019 read_sq.buffers[read_sq.front], &bUsed, bLeft);
2025 read_sq.rear_size += bUsed;
2026 if (read_sq.rear_size >= read_sq.block_size) {
2027 read_sq.rear_size = 0;
2029 if (read_sq.front >= read_sq.max_active)
2036 static int sq_open(struct inode *inode, struct file *file)
2040 if (file->f_mode & FMODE_WRITE) {
2043 if (NON_BLOCKING(file->f_flags))
2047 SLEEP(sq.open_queue);
2048 if (SIGNAL_RECEIVED)
2052 sq.busy = 1; /* Let's play spot-the-race-condition */
2054 if (sq_allocate_buffers()) goto err_out_nobusy;
2056 sq_setup(numBufs, bufSize<<10,sound_buffers);
2057 sq.open_mode = file->f_mode;
2061 if (file->f_mode & FMODE_READ) {
2064 if (NON_BLOCKING(file->f_flags))
2067 while (read_sq.busy) {
2068 SLEEP(read_sq.open_queue);
2069 if (SIGNAL_RECEIVED)
2075 if (sq_allocate_read_buffers()) goto err_out_nobusy;
2077 read_sq_setup(numReadBufs,readbufSize<<10, sound_read_buffers);
2078 read_sq.open_mode = file->f_mode;
2081 /* Start up the 4218 by:
2085 *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_RSTAUDIO;
2087 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_ENAUDIO;
2089 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
2091 /* We need to send the current control word in case someone
2092 * opened /dev/mixer and changed things while we were shut
2093 * down. Chances are good the initialization that follows
2094 * would have done this, but it is still possible it wouldn't.
2096 cs4218_ctl_write(cs4218_control);
2098 sound.minDev = iminor(inode) & 0x0f;
2099 sound.soft = sound.dsp;
2100 sound.hard = sound.dsp;
2102 if ((iminor(inode) & 0x0f) == SND_DEV_AUDIO) {
2103 sound_set_speed(8000);
2104 sound_set_stereo(0);
2105 sound_set_format(AFMT_MU_LAW);
2108 return nonseekable_open(inode, file);
2111 if (file->f_mode & FMODE_WRITE) {
2113 WAKE_UP(sq.open_queue);
2115 if (file->f_mode & FMODE_READ) {
2117 WAKE_UP(read_sq.open_queue);
2124 static void sq_reset(void)
2129 sq.front = (sq.rear+1) % sq.max_count;
2136 static int sq_fsync(struct file *filp, struct dentry *dentry)
2141 sq_play(); /* there may be an incomplete frame waiting */
2144 SLEEP(sq.sync_queue);
2145 if (SIGNAL_RECEIVED) {
2146 /* While waiting for audio output to drain, an
2147 * interrupt occurred. Stop audio output immediately
2148 * and clear the queue. */
2159 static int sq_release(struct inode *inode, struct file *file)
2164 rc = sq_fsync(file, file->f_dentry);
2165 sound.soft = sound.dsp;
2166 sound.hard = sound.dsp;
2169 sq_release_read_buffers();
2170 sq_release_buffers();
2172 if (file->f_mode & FMODE_READ) {
2174 WAKE_UP(read_sq.open_queue);
2177 if (file->f_mode & FMODE_WRITE) {
2179 WAKE_UP(sq.open_queue);
2182 /* Shut down the SMC.
2184 cpmp->cp_smc[1].smc_smcmr &= ~(SMCMR_TEN | SMCMR_REN);
2186 /* Shut down the codec.
2188 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
2190 *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_ENAUDIO;
2192 /* Wake up a process waiting for the queue being released.
2193 * Note: There may be several processes waiting for a call
2194 * to open() returning. */
2200 static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
2212 case SNDCTL_DSP_RESET:
2215 case SNDCTL_DSP_POST:
2216 case SNDCTL_DSP_SYNC:
2217 return sq_fsync(file, file->f_dentry);
2219 /* ++TeSche: before changing any of these it's
2220 * probably wise to wait until sound playing has
2222 case SNDCTL_DSP_SPEED:
2223 sq_fsync(file, file->f_dentry);
2224 IOCTL_IN(arg, data);
2225 return IOCTL_OUT(arg, sound_set_speed(data));
2226 case SNDCTL_DSP_STEREO:
2227 sq_fsync(file, file->f_dentry);
2228 IOCTL_IN(arg, data);
2229 return IOCTL_OUT(arg, sound_set_stereo(data));
2230 case SOUND_PCM_WRITE_CHANNELS:
2231 sq_fsync(file, file->f_dentry);
2232 IOCTL_IN(arg, data);
2233 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
2234 case SNDCTL_DSP_SETFMT:
2235 sq_fsync(file, file->f_dentry);
2236 IOCTL_IN(arg, data);
2237 return IOCTL_OUT(arg, sound_set_format(data));
2238 case SNDCTL_DSP_GETFMTS:
2240 if (sound.trans_write) {
2241 if (sound.trans_write->ct_ulaw)
2243 if (sound.trans_write->ct_alaw)
2245 if (sound.trans_write->ct_s8)
2247 if (sound.trans_write->ct_u8)
2249 if (sound.trans_write->ct_s16be)
2251 if (sound.trans_write->ct_u16be)
2253 if (sound.trans_write->ct_s16le)
2255 if (sound.trans_write->ct_u16le)
2258 return IOCTL_OUT(arg, fmt);
2259 case SNDCTL_DSP_GETBLKSIZE:
2260 size = sq.block_size
2261 * sound.soft.size * (sound.soft.stereo + 1)
2262 / (sound.hard.size * (sound.hard.stereo + 1));
2263 return IOCTL_OUT(arg, size);
2264 case SNDCTL_DSP_SUBDIVIDE:
2266 #if 0 /* Sorry can't do this at the moment. The CPM allocated buffers
2267 * long ago that can't be changed.
2269 case SNDCTL_DSP_SETFRAGMENT:
2270 if (sq.count || sq.active || sq.syncing)
2272 IOCTL_IN(arg, size);
2274 if (nbufs < 2 || nbufs > numBufs)
2277 if (size >= 8 && size <= 30) {
2279 size *= sound.hard.size * (sound.hard.stereo + 1);
2280 size /= sound.soft.size * (sound.soft.stereo + 1);
2281 if (size > (bufSize << 10))
2282 size = bufSize << 10;
2284 size = bufSize << 10;
2285 sq_setup(numBufs, size, sound_buffers);
2286 sq.max_active = nbufs;
2291 return mixer_ioctl(inode, file, cmd, arg);
2298 static struct file_operations sq_fops =
2300 .owner = THIS_MODULE,
2301 .llseek = sound_lseek,
2302 .read = sq_read, /* sq_read */
2306 .release = sq_release,
2310 static void __init sq_init(void)
2312 sq_unit = register_sound_dsp(&sq_fops, -1);
2316 init_waitqueue_head(&sq.action_queue);
2317 init_waitqueue_head(&sq.open_queue);
2318 init_waitqueue_head(&sq.sync_queue);
2319 init_waitqueue_head(&read_sq.action_queue);
2320 init_waitqueue_head(&read_sq.open_queue);
2321 init_waitqueue_head(&read_sq.sync_queue);
2326 /* whatever you like as startup mode for /dev/dsp,
2327 * (/dev/audio hasn't got a startup mode). note that
2328 * once changed a new open() will *not* restore these!
2330 sound.dsp.format = AFMT_S16_BE;
2331 sound.dsp.stereo = 1;
2332 sound.dsp.size = 16;
2334 /* set minimum rate possible without expanding */
2335 sound.dsp.speed = 8000;
2337 /* before the first open to /dev/dsp this wouldn't be set */
2338 sound.soft = sound.dsp;
2339 sound.hard = sound.dsp;
2349 /* state.buf should not overflow! */
2351 static int state_open(struct inode *inode, struct file *file)
2353 char *buffer = state.buf, *mach = "", cs4218_buf[50];
2362 sprintf(cs4218_buf, "Crystal CS4218 on TDM, ");
2365 len += sprintf(buffer+len, "%sDMA sound driver:\n", mach);
2367 len += sprintf(buffer+len, "\tsound.format = 0x%x", sound.soft.format);
2368 switch (sound.soft.format) {
2370 len += sprintf(buffer+len, " (mu-law)");
2373 len += sprintf(buffer+len, " (A-law)");
2376 len += sprintf(buffer+len, " (unsigned 8 bit)");
2379 len += sprintf(buffer+len, " (signed 8 bit)");
2382 len += sprintf(buffer+len, " (signed 16 bit big)");
2385 len += sprintf(buffer+len, " (unsigned 16 bit big)");
2388 len += sprintf(buffer+len, " (signed 16 bit little)");
2391 len += sprintf(buffer+len, " (unsigned 16 bit little)");
2394 len += sprintf(buffer+len, "\n");
2395 len += sprintf(buffer+len, "\tsound.speed = %dHz (phys. %dHz)\n",
2396 sound.soft.speed, sound.hard.speed);
2397 len += sprintf(buffer+len, "\tsound.stereo = 0x%x (%s)\n",
2398 sound.soft.stereo, sound.soft.stereo ? "stereo" : "mono");
2399 len += sprintf(buffer+len, "\tsq.block_size = %d sq.max_count = %d"
2400 " sq.max_active = %d\n",
2401 sq.block_size, sq.max_count, sq.max_active);
2402 len += sprintf(buffer+len, "\tsq.count = %d sq.rear_size = %d\n", sq.count,
2404 len += sprintf(buffer+len, "\tsq.active = %d sq.syncing = %d\n",
2405 sq.active, sq.syncing);
2407 return nonseekable_open(inode, file);
2411 static int state_release(struct inode *inode, struct file *file)
2418 static ssize_t state_read(struct file *file, char *buf, size_t count,
2421 int n = state.len - state.ptr;
2426 if (copy_to_user(buf, &state.buf[state.ptr], n))
2433 static struct file_operations state_fops =
2435 .owner = THIS_MODULE,
2436 .llseek = sound_lseek,
2439 .release = state_release,
2443 static void __init state_init(void)
2445 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
2452 /*** Common stuff ********************************************************/
2454 static long long sound_lseek(struct file *file, long long offset, int orig)
2460 /*** Config & Setup **********************************************************/
2463 int __init tdm8xx_sound_init(void)
2467 volatile uint *sirp;
2468 volatile cbd_t *bdp;
2469 volatile cpm8xx_t *cp;
2471 volatile smc_uart_t *up;
2472 volatile immap_t *immap;
2476 /* Program the SI/TSA to use TDMa, connected to SMC2, for 4 bytes.
2478 cp = cpmp; /* Get pointer to Communication Processor */
2479 immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
2481 /* Set all TDMa control bits to zero. This enables most features
2484 cp->cp_simode &= ~0x00000fff;
2486 /* Enable common receive/transmit clock pins, use IDL format.
2487 * Sync on falling edge, transmit rising clock, receive falling
2488 * clock, delay 1 bit on both Tx and Rx. Common Tx/Rx clocks and
2490 * Connect SMC2 to TSA.
2492 cp->cp_simode |= 0x80000141;
2494 /* Configure port A pins for TDMa operation.
2495 * The RPX-Lite (MPC850/823) loses SMC2 when TDM is used.
2497 immap->im_ioport.iop_papar |= 0x01c0; /* Enable TDMa functions */
2498 immap->im_ioport.iop_padir |= 0x00c0; /* Enable TDMa Tx/Rx */
2499 immap->im_ioport.iop_padir &= ~0x0100; /* Enable L1RCLKa */
2501 immap->im_ioport.iop_pcpar |= 0x0800; /* Enable L1RSYNCa */
2502 immap->im_ioport.iop_pcdir &= ~0x0800;
2504 /* Initialize the SI TDM routing table. We use TDMa only.
2505 * The receive table and transmit table each have only one
2506 * entry, to capture/send four bytes after each frame pulse.
2507 * The 16-bit ram entry is 0000 0001 1000 1111. (SMC2)
2510 sirp = (uint *)cp->cp_siram;
2512 *sirp = 0x018f0000; /* Receive entry */
2514 *sirp = 0x018f0000; /* Tramsmit entry */
2516 /* Enable single TDMa routing.
2518 cp->cp_sigmr = 0x04;
2520 /* Initialize the SMC for transparent operation.
2522 sp = &cpmp->cp_smc[1];
2523 up = (smc_uart_t *)&cp->cp_dparam[PROFF_SMC2];
2525 /* We need to allocate a transmit and receive buffer
2526 * descriptors from dual port ram.
2528 dp_addr = cpm_dpalloc(sizeof(cbd_t) * numReadBufs, 8);
2530 /* Set the physical address of the host memory
2531 * buffers in the buffer descriptors, and the
2532 * virtual address for us to work with.
2534 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2535 up->smc_rbase = dp_offset;
2536 rx_cur = rx_base = (cbd_t *)bdp;
2538 for (i=0; i<(numReadBufs-1); i++) {
2539 bdp->cbd_bufaddr = 0;
2540 bdp->cbd_datlen = 0;
2541 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2544 bdp->cbd_bufaddr = 0;
2545 bdp->cbd_datlen = 0;
2546 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2548 /* Now, do the same for the transmit buffers.
2550 dp_offset = cpm_dpalloc(sizeof(cbd_t) * numBufs, 8);
2552 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2553 up->smc_tbase = dp_offset;
2554 tx_cur = tx_base = (cbd_t *)bdp;
2556 for (i=0; i<(numBufs-1); i++) {
2557 bdp->cbd_bufaddr = 0;
2558 bdp->cbd_datlen = 0;
2559 bdp->cbd_sc = BD_SC_INTRPT;
2562 bdp->cbd_bufaddr = 0;
2563 bdp->cbd_datlen = 0;
2564 bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2566 /* Set transparent SMC mode.
2567 * A few things are specific to our application. The codec interface
2568 * is MSB first, hence the REVD selection. The CD/CTS pulse are
2569 * used by the TSA to indicate the frame start to the SMC.
2571 up->smc_rfcr = SCC_EB;
2572 up->smc_tfcr = SCC_EB;
2573 up->smc_mrblr = readbufSize * 1024;
2575 /* Set 16-bit reversed data, transparent mode.
2577 sp->smc_smcmr = smcr_mk_clen(15) |
2578 SMCMR_SM_TRANS | SMCMR_REVD | SMCMR_BS;
2580 /* Enable and clear events.
2581 * Because of FIFO delays, all we need is the receive interrupt
2582 * and we can process both the current receive and current
2583 * transmit interrupt within a few microseconds of the transmit.
2585 sp->smc_smce = 0xff;
2586 sp->smc_smcm = SMCM_TXE | SMCM_TX | SMCM_RX;
2588 /* Send the CPM an initialize command.
2590 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
2591 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2592 while (cp->cp_cpcr & CPM_CR_FLG);
2594 sound.mach = mach_cs4218;
2597 /* Initialize beep stuff */
2598 orig_mksound = kd_mksound;
2599 kd_mksound = cs_mksound;
2600 beep_buf = (short *) kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
2601 if (beep_buf == NULL)
2602 printk(KERN_WARNING "dmasound: no memory for "
2608 /* Initialize the software SPI.
2612 /* Set up sound queue, /dev/audio and /dev/dsp. */
2614 /* Set default settings. */
2617 /* Set up /dev/sndstat. */
2620 /* Set up /dev/mixer. */
2623 if (!sound.mach.irqinit()) {
2624 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
2631 printk(KERN_INFO "DMA sound driver installed, using %d buffers of %dk.\n",
2637 /* Due to FIFOs and bit delays, the transmit interrupt occurs a few
2638 * microseconds ahead of the receive interrupt.
2639 * When we get an interrupt, we service the transmit first, then
2640 * check for a receive to prevent the overhead of returning through
2641 * the interrupt handler only to get back here right away during
2642 * full duplex operation.
2645 cs4218_intr(void *dev_id, struct pt_regs *regs)
2648 volatile cpm8xx_t *cp;
2650 sp = &cpmp->cp_smc[1];
2652 if (sp->smc_smce & SCCM_TX) {
2653 sp->smc_smce = SCCM_TX;
2654 cs4218_tdm_tx_intr((void *)sp);
2657 if (sp->smc_smce & SCCM_RX) {
2658 sp->smc_smce = SCCM_RX;
2659 cs4218_tdm_rx_intr((void *)sp);
2662 if (sp->smc_smce & SCCM_TXE) {
2663 /* Transmit underrun. This happens with the application
2664 * didn't keep up sending buffers. We tell the SMC to
2665 * restart, which will cause it to poll the current (next)
2666 * BD. If the user supplied data since this occurred,
2667 * we just start running again. If they didn't, the SMC
2668 * will poll the descriptor until data is placed there.
2670 sp->smc_smce = SCCM_TXE;
2671 cp = cpmp; /* Get pointer to Communication Processor */
2672 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
2673 CPM_CR_RESTART_TX) | CPM_CR_FLG;
2674 while (cp->cp_cpcr & CPM_CR_FLG);
2679 #define MAXARGS 8 /* Should be sufficient for now */
2681 void __init dmasound_setup(char *str, int *ints)
2683 /* check the bootstrap parameter for "dmasound=" */
2687 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
2688 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
2690 catchRadius = ints[3];
2693 if (ints[1] < MIN_BUFFERS)
2694 printk("dmasound_setup: invalid number of buffers, using default = %d\n", numBufs);
2697 if (ints[2] < MIN_BUFSIZE || ints[2] > MAX_BUFSIZE)
2698 printk("dmasound_setup: invalid buffer size, using default = %d\n", bufSize);
2705 printk("dmasound_setup: invalid number of arguments\n");
2709 /* Software SPI functions.
2710 * These are on Port B.
2712 #define PB_SPICLK ((uint)0x00000002)
2713 #define PB_SPIMOSI ((uint)0x00000004)
2714 #define PB_SPIMISO ((uint)0x00000008)
2717 void sw_spi_init(void)
2719 volatile cpm8xx_t *cp;
2720 volatile uint *hcsr4;
2722 hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
2723 cp = cpmp; /* Get pointer to Communication Processor */
2725 *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
2727 /* Make these Port B signals general purpose I/O.
2728 * First, make sure the clock is low.
2730 cp->cp_pbdat &= ~PB_SPICLK;
2731 cp->cp_pbpar &= ~(PB_SPICLK | PB_SPIMOSI | PB_SPIMISO);
2733 /* Clock and Master Output are outputs.
2735 cp->cp_pbdir |= (PB_SPICLK | PB_SPIMOSI);
2739 cp->cp_pbdir &= ~PB_SPIMISO;
2743 /* Write the CS4218 control word out the SPI port. While the
2744 * the control word is going out, the status word is arriving.
2747 uint cs4218_ctl_write(uint ctlreg)
2751 sw_spi_io((u_char *)&ctlreg, (u_char *)&status, 4);
2753 /* Shadow the control register.....I guess we could do
2754 * the same for the status, but for now we just return it
2755 * and let the caller decide.
2757 cs4218_control = ctlreg;
2762 void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt)
2765 u_char outbyte, inbyte;
2766 volatile cpm8xx_t *cp;
2767 volatile uint *hcsr4;
2769 hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
2770 cp = cpmp; /* Get pointer to Communication Processor */
2772 /* The timing on the bus is pretty slow. Code inefficiency
2773 * and eieio() is our friend here :-).
2775 cp->cp_pbdat &= ~PB_SPICLK;
2776 *hcsr4 |= HIOX_CSR4_AUDSPISEL; /* Enable SPI select */
2779 /* Clock in/out the bytes. Data is valid on the falling edge
2780 * of the clock. Data is MSB first.
2782 for (i=0; i<bcnt; i++) {
2785 for (bits=0; bits<8; bits++) {
2787 cp->cp_pbdat |= PB_SPICLK;
2790 cp->cp_pbdat |= PB_SPIMOSI;
2792 cp->cp_pbdat &= ~PB_SPIMOSI;
2794 cp->cp_pbdat &= ~PB_SPICLK;
2798 if (cp->cp_pbdat & PB_SPIMISO)
2804 *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
2808 void cleanup_module(void)
2810 if (irq_installed) {
2813 sound.mach.irqcleanup();
2817 sq_release_read_buffers();
2818 sq_release_buffers();
2820 if (mixer_unit >= 0)
2821 unregister_sound_mixer(mixer_unit);
2822 if (state_unit >= 0)
2823 unregister_sound_special(state_unit);
2825 unregister_sound_dsp(sq_unit);
2828 module_init(tdm8xx_sound_init);
2829 module_exit(cleanup_module);