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 static DEFINE_SPINLOCK(cs4218_lock);
60 /* Local copies of things we put in the control register. Output
61 * volume, like most codecs is really attenuation.
63 static int cs4218_rate_index;
66 * Stuff for outputting a beep. The values range from -327 to +327
67 * so we can multiply by an amplitude in the range 0..100 to get a
68 * signed short value to put in the output buffer.
70 static short beep_wform[256] = {
71 0, 40, 79, 117, 153, 187, 218, 245,
72 269, 288, 304, 316, 323, 327, 327, 324,
73 318, 310, 299, 288, 275, 262, 249, 236,
74 224, 213, 204, 196, 190, 186, 183, 182,
75 182, 183, 186, 189, 192, 196, 200, 203,
76 206, 208, 209, 209, 209, 207, 204, 201,
77 197, 193, 188, 183, 179, 174, 170, 166,
78 163, 161, 160, 159, 159, 160, 161, 162,
79 164, 166, 168, 169, 171, 171, 171, 170,
80 169, 167, 163, 159, 155, 150, 144, 139,
81 133, 128, 122, 117, 113, 110, 107, 105,
82 103, 103, 103, 103, 104, 104, 105, 105,
83 105, 103, 101, 97, 92, 86, 78, 68,
84 58, 45, 32, 18, 3, -11, -26, -41,
85 -55, -68, -79, -88, -95, -100, -102, -102,
86 -99, -93, -85, -75, -62, -48, -33, -16,
87 0, 16, 33, 48, 62, 75, 85, 93,
88 99, 102, 102, 100, 95, 88, 79, 68,
89 55, 41, 26, 11, -3, -18, -32, -45,
90 -58, -68, -78, -86, -92, -97, -101, -103,
91 -105, -105, -105, -104, -104, -103, -103, -103,
92 -103, -105, -107, -110, -113, -117, -122, -128,
93 -133, -139, -144, -150, -155, -159, -163, -167,
94 -169, -170, -171, -171, -171, -169, -168, -166,
95 -164, -162, -161, -160, -159, -159, -160, -161,
96 -163, -166, -170, -174, -179, -183, -188, -193,
97 -197, -201, -204, -207, -209, -209, -209, -208,
98 -206, -203, -200, -196, -192, -189, -186, -183,
99 -182, -182, -183, -186, -190, -196, -204, -213,
100 -224, -236, -249, -262, -275, -288, -299, -310,
101 -318, -324, -327, -327, -323, -316, -304, -288,
102 -269, -245, -218, -187, -153, -117, -79, -40,
105 #define BEEP_SPEED 5 /* 22050 Hz sample rate */
106 #define BEEP_BUFLEN 512
107 #define BEEP_VOLUME 15 /* 0 - 100 */
109 static int beep_volume = BEEP_VOLUME;
110 static int beep_playing = 0;
111 static int beep_state = 0;
112 static short *beep_buf;
113 static void (*orig_mksound)(unsigned int, unsigned int);
115 /* This is found someplace else......I guess in the keyboard driver
118 static void (*kd_mksound)(unsigned int, unsigned int);
120 static int catchRadius = 0;
121 static int numBufs = 4, bufSize = 32;
122 static int numReadBufs = 4, readbufSize = 32;
125 /* TDM/Serial transmit and receive buffer descriptors.
127 static volatile cbd_t *rx_base, *rx_cur, *tx_base, *tx_cur;
129 MODULE_PARM(catchRadius, "i");
130 MODULE_PARM(numBufs, "i");
131 MODULE_PARM(bufSize, "i");
132 MODULE_PARM(numreadBufs, "i");
133 MODULE_PARM(readbufSize, "i");
135 #define arraysize(x) (sizeof(x)/sizeof(*(x)))
136 #define le2be16(x) (((x)<<8 & 0xff00) | ((x)>>8 & 0x00ff))
137 #define le2be16dbl(x) (((x)<<8 & 0xff00ff00) | ((x)>>8 & 0x00ff00ff))
139 #define IOCTL_IN(arg, ret) \
140 do { int error = get_user(ret, (int *)(arg)); \
141 if (error) return error; \
143 #define IOCTL_OUT(arg, ret) ioctl_return((int *)(arg), ret)
145 /* CS4218 serial port control in mode 4.
147 #define CS_INTMASK ((uint)0x40000000)
148 #define CS_DO1 ((uint)0x20000000)
149 #define CS_LATTEN ((uint)0x1f000000)
150 #define CS_RATTEN ((uint)0x00f80000)
151 #define CS_MUTE ((uint)0x00040000)
152 #define CS_ISL ((uint)0x00020000)
153 #define CS_ISR ((uint)0x00010000)
154 #define CS_LGAIN ((uint)0x0000f000)
155 #define CS_RGAIN ((uint)0x00000f00)
157 #define CS_LATTEN_SET(X) (((X) & 0x1f) << 24)
158 #define CS_RATTEN_SET(X) (((X) & 0x1f) << 19)
159 #define CS_LGAIN_SET(X) (((X) & 0x0f) << 12)
160 #define CS_RGAIN_SET(X) (((X) & 0x0f) << 8)
162 #define CS_LATTEN_GET(X) (((X) >> 24) & 0x1f)
163 #define CS_RATTEN_GET(X) (((X) >> 19) & 0x1f)
164 #define CS_LGAIN_GET(X) (((X) >> 12) & 0x0f)
165 #define CS_RGAIN_GET(X) (((X) >> 8) & 0x0f)
167 /* The control register is effectively write only. We have to keep a copy
170 static uint cs4218_control;
172 /* A place to store expanding information.
174 static int expand_bal;
175 static int expand_data;
177 /* Since I can't make the microcode patch work for the SPI, I just
178 * clock the bits using software.
180 static void sw_spi_init(void);
181 static void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt);
182 static uint cs4218_ctl_write(uint ctlreg);
184 /*** Some low level helpers **************************************************/
188 static short ulaw2dma16[] = {
189 -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956,
190 -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764,
191 -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412,
192 -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316,
193 -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
194 -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
195 -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
196 -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
197 -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
198 -1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
199 -876, -844, -812, -780, -748, -716, -684, -652,
200 -620, -588, -556, -524, -492, -460, -428, -396,
201 -372, -356, -340, -324, -308, -292, -276, -260,
202 -244, -228, -212, -196, -180, -164, -148, -132,
203 -120, -112, -104, -96, -88, -80, -72, -64,
204 -56, -48, -40, -32, -24, -16, -8, 0,
205 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
206 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
207 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
208 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
209 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
210 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
211 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
212 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
213 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
214 1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
215 876, 844, 812, 780, 748, 716, 684, 652,
216 620, 588, 556, 524, 492, 460, 428, 396,
217 372, 356, 340, 324, 308, 292, 276, 260,
218 244, 228, 212, 196, 180, 164, 148, 132,
219 120, 112, 104, 96, 88, 80, 72, 64,
220 56, 48, 40, 32, 24, 16, 8, 0,
225 static short alaw2dma16[] = {
226 -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
227 -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
228 -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
229 -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
230 -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944,
231 -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136,
232 -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472,
233 -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568,
234 -344, -328, -376, -360, -280, -264, -312, -296,
235 -472, -456, -504, -488, -408, -392, -440, -424,
236 -88, -72, -120, -104, -24, -8, -56, -40,
237 -216, -200, -248, -232, -152, -136, -184, -168,
238 -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
239 -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
240 -688, -656, -752, -720, -560, -528, -624, -592,
241 -944, -912, -1008, -976, -816, -784, -880, -848,
242 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
243 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
244 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
245 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
246 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
247 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
248 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
249 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
250 344, 328, 376, 360, 280, 264, 312, 296,
251 472, 456, 504, 488, 408, 392, 440, 424,
252 88, 72, 120, 104, 24, 8, 56, 40,
253 216, 200, 248, 232, 152, 136, 184, 168,
254 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
255 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
256 688, 656, 752, 720, 560, 528, 624, 592,
257 944, 912, 1008, 976, 816, 784, 880, 848,
261 /*** Translations ************************************************************/
264 static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
265 u_char frame[], ssize_t *frameUsed,
267 static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
268 u_char frame[], ssize_t *frameUsed,
270 static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
271 u_char frame[], ssize_t *frameUsed,
273 static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
274 u_char frame[], ssize_t *frameUsed,
276 static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
277 u_char frame[], ssize_t *frameUsed,
279 static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
280 u_char frame[], ssize_t *frameUsed,
282 static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
283 u_char frame[], ssize_t *frameUsed,
285 static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
286 u_char frame[], ssize_t *frameUsed,
288 static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
289 u_char frame[], ssize_t *frameUsed,
291 static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
292 u_char frame[], ssize_t *frameUsed,
294 static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
295 u_char frame[], ssize_t *frameUsed,
297 static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
298 u_char frame[], ssize_t *frameUsed,
302 /*** Low level stuff *********************************************************/
304 struct cs_sound_settings {
305 MACHINE mach; /* machine dependent things */
306 SETTINGS hard; /* hardware settings */
307 SETTINGS soft; /* software settings */
308 SETTINGS dsp; /* /dev/dsp default settings */
309 TRANS *trans_write; /* supported translations for playback */
310 TRANS *trans_read; /* supported translations for record */
311 int volume_left; /* volume (range is machine dependent) */
313 int bass; /* tone (range is machine dependent) */
316 int minDev; /* minor device number currently open */
319 static struct cs_sound_settings sound;
321 static void *CS_Alloc(unsigned int size, gfp_t flags);
322 static void CS_Free(void *ptr, unsigned int size);
323 static int CS_IrqInit(void);
325 static void CS_IrqCleanup(void);
327 static void CS_Silence(void);
328 static void CS_Init(void);
329 static void CS_Play(void);
330 static void CS_Record(void);
331 static int CS_SetFormat(int format);
332 static int CS_SetVolume(int volume);
333 static void cs4218_tdm_tx_intr(void *devid);
334 static void cs4218_tdm_rx_intr(void *devid);
335 static void cs4218_intr(void *devid, struct pt_regs *regs);
336 static int cs_get_volume(uint reg);
337 static int cs_volume_setter(int volume, int mute);
338 static int cs_get_gain(uint reg);
339 static int cs_set_gain(int gain);
340 static void cs_mksound(unsigned int hz, unsigned int ticks);
341 static void cs_nosound(unsigned long xx);
343 /*** Mid level stuff *********************************************************/
346 static void sound_silence(void);
347 static void sound_init(void);
348 static int sound_set_format(int format);
349 static int sound_set_speed(int speed);
350 static int sound_set_stereo(int stereo);
351 static int sound_set_volume(int volume);
353 static ssize_t sound_copy_translate(const u_char *userPtr,
355 u_char frame[], ssize_t *frameUsed,
357 static ssize_t sound_copy_translate_read(const u_char *userPtr,
359 u_char frame[], ssize_t *frameUsed,
364 * /dev/mixer abstraction
372 static struct sound_mixer mixer;
374 static struct sound_queue sq;
375 static struct sound_queue read_sq;
377 #define sq_block_address(i) (sq.buffers[i])
378 #define SIGNAL_RECEIVED (signal_pending(current))
379 #define NON_BLOCKING(open_mode) (open_mode & O_NONBLOCK)
380 #define ONE_SECOND HZ /* in jiffies (100ths of a second) */
381 #define NO_TIME_LIMIT 0xffffffff
393 static struct sound_state state;
395 /*** Common stuff ********************************************************/
397 static long long sound_lseek(struct file *file, long long offset, int orig);
399 /*** Config & Setup **********************************************************/
401 void dmasound_setup(char *str, int *ints);
403 /*** Translations ************************************************************/
406 /* ++TeSche: radically changed for new expanding purposes...
408 * These two routines now deal with copying/expanding/translating the samples
409 * from user space into our buffer at the right frequency. They take care about
410 * how much data there's actually to read, how much buffer space there is and
411 * to convert samples into the right frequency/encoding. They will only work on
412 * complete samples so it may happen they leave some bytes in the input stream
413 * if the user didn't write a multiple of the current sample size. They both
414 * return the number of bytes they've used from both streams so you may detect
415 * such a situation. Luckily all programs should be able to cope with that.
417 * I think I've optimized anything as far as one can do in plain C, all
418 * variables should fit in registers and the loops are really short. There's
419 * one loop for every possible situation. Writing a more generalized and thus
420 * parameterized loop would only produce slower code. Feel free to optimize
421 * this in assembler if you like. :)
423 * I think these routines belong here because they're not yet really hardware
424 * independent, especially the fact that the Falcon can play 16bit samples
425 * only in stereo is hardcoded in both of them!
427 * ++geert: split in even more functions (one per format)
430 static ssize_t cs4218_ct_law(const u_char *userPtr, size_t userCount,
431 u_char frame[], ssize_t *frameUsed,
434 short *table = sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16;
436 short *p = (short *) &frame[*frameUsed];
437 int val, stereo = sound.soft.stereo;
442 used = count = min(userCount, frameLeft);
445 if (get_user(data, userPtr++))
450 if (get_user(data, userPtr++))
457 *frameUsed += used * 4;
458 return stereo? used * 2: used;
462 static ssize_t cs4218_ct_s8(const u_char *userPtr, size_t userCount,
463 u_char frame[], ssize_t *frameUsed,
467 short *p = (short *) &frame[*frameUsed];
468 int val, stereo = sound.soft.stereo;
473 used = count = min(userCount, frameLeft);
476 if (get_user(data, userPtr++))
481 if (get_user(data, userPtr++))
488 *frameUsed += used * 4;
489 return stereo? used * 2: used;
493 static ssize_t cs4218_ct_u8(const u_char *userPtr, size_t userCount,
494 u_char frame[], ssize_t *frameUsed,
498 short *p = (short *) &frame[*frameUsed];
499 int val, stereo = sound.soft.stereo;
504 used = count = min(userCount, frameLeft);
507 if (get_user(data, userPtr++))
509 val = (data ^ 0x80) << 8;
512 if (get_user(data, userPtr++))
514 val = (data ^ 0x80) << 8;
519 *frameUsed += used * 4;
520 return stereo? used * 2: used;
524 /* This is the default format of the codec. Signed, 16-bit stereo
525 * generated by an application shouldn't have to be copied at all.
526 * We should just get the phsical address of the buffers and update
527 * the TDM BDs directly.
529 static ssize_t cs4218_ct_s16(const u_char *userPtr, size_t userCount,
530 u_char frame[], ssize_t *frameUsed,
534 int stereo = sound.soft.stereo;
535 short *fp = (short *) &frame[*frameUsed];
538 userCount >>= (stereo? 2: 1);
539 used = count = min(userCount, frameLeft);
541 short *up = (short *) userPtr;
544 if (get_user(data, up++))
551 if (copy_from_user(fp, userPtr, count * 4))
554 *frameUsed += used * 4;
555 return stereo? used * 4: used * 2;
558 static ssize_t cs4218_ct_u16(const u_char *userPtr, size_t userCount,
559 u_char frame[], ssize_t *frameUsed,
563 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
564 int stereo = sound.soft.stereo;
565 short *fp = (short *) &frame[*frameUsed];
566 short *up = (short *) userPtr;
569 userCount >>= (stereo? 2: 1);
570 used = count = min(userCount, frameLeft);
573 if (get_user(data, up++))
578 if (get_user(data, up++))
585 *frameUsed += used * 4;
586 return stereo? used * 4: used * 2;
590 static ssize_t cs4218_ctx_law(const u_char *userPtr, size_t userCount,
591 u_char frame[], ssize_t *frameUsed,
594 unsigned short *table = (unsigned short *)
595 (sound.soft.format == AFMT_MU_LAW ? ulaw2dma16: alaw2dma16);
596 unsigned int data = expand_data;
597 unsigned int *p = (unsigned int *) &frame[*frameUsed];
598 int bal = expand_bal;
599 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
601 int stereo = sound.soft.stereo;
613 if (get_user(c, userPtr++))
617 if (get_user(c, userPtr++))
619 data = (data << 16) + table[c];
621 data = (data << 16) + data;
631 *frameUsed += (ftotal - frameLeft) * 4;
633 return stereo? utotal * 2: utotal;
637 static ssize_t cs4218_ctx_s8(const u_char *userPtr, size_t userCount,
638 u_char frame[], ssize_t *frameUsed,
641 unsigned int *p = (unsigned int *) &frame[*frameUsed];
642 unsigned int data = expand_data;
643 int bal = expand_bal;
644 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
645 int stereo = sound.soft.stereo;
658 if (get_user(c, userPtr++))
662 if (get_user(c, userPtr++))
664 data = (data << 16) + (c << 8);
666 data = (data << 16) + data;
676 *frameUsed += (ftotal - frameLeft) * 4;
678 return stereo? utotal * 2: utotal;
682 static ssize_t cs4218_ctx_u8(const u_char *userPtr, size_t userCount,
683 u_char frame[], ssize_t *frameUsed,
686 unsigned int *p = (unsigned int *) &frame[*frameUsed];
687 unsigned int data = expand_data;
688 int bal = expand_bal;
689 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
690 int stereo = sound.soft.stereo;
703 if (get_user(c, userPtr++))
705 data = (c ^ 0x80) << 8;
707 if (get_user(c, userPtr++))
709 data = (data << 16) + ((c ^ 0x80) << 8);
711 data = (data << 16) + data;
721 *frameUsed += (ftotal - frameLeft) * 4;
723 return stereo? utotal * 2: utotal;
727 static ssize_t cs4218_ctx_s16(const u_char *userPtr, size_t userCount,
728 u_char frame[], ssize_t *frameUsed,
731 unsigned int *p = (unsigned int *) &frame[*frameUsed];
732 unsigned int data = expand_data;
733 unsigned short *up = (unsigned short *) userPtr;
734 int bal = expand_bal;
735 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
736 int stereo = sound.soft.stereo;
740 userCount >>= (stereo? 2: 1);
748 if (get_user(data, up++))
751 if (get_user(c, up++))
753 data = (data << 16) + c;
755 data = (data << 16) + data;
765 *frameUsed += (ftotal - frameLeft) * 4;
767 return stereo? utotal * 4: utotal * 2;
771 static ssize_t cs4218_ctx_u16(const u_char *userPtr, size_t userCount,
772 u_char frame[], ssize_t *frameUsed,
775 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
776 unsigned int *p = (unsigned int *) &frame[*frameUsed];
777 unsigned int data = expand_data;
778 unsigned short *up = (unsigned short *) userPtr;
779 int bal = expand_bal;
780 int hSpeed = sound.hard.speed, sSpeed = sound.soft.speed;
781 int stereo = sound.soft.stereo;
785 userCount >>= (stereo? 2: 1);
793 if (get_user(data, up++))
797 if (get_user(c, up++))
799 data = (data << 16) + (c ^ mask);
801 data = (data << 16) + data;
811 *frameUsed += (ftotal - frameLeft) * 4;
813 return stereo? utotal * 4: utotal * 2;
816 static ssize_t cs4218_ct_s8_read(const u_char *userPtr, size_t userCount,
817 u_char frame[], ssize_t *frameUsed,
821 short *p = (short *) &frame[*frameUsed];
822 int val, stereo = sound.soft.stereo;
827 used = count = min(userCount, frameLeft);
833 if (put_user(data, (u_char *)userPtr++))
838 if (put_user(data, (u_char *)userPtr++))
844 *frameUsed += used * 4;
845 return stereo? used * 2: used;
849 static ssize_t cs4218_ct_u8_read(const u_char *userPtr, size_t userCount,
850 u_char frame[], ssize_t *frameUsed,
854 short *p = (short *) &frame[*frameUsed];
855 int val, stereo = sound.soft.stereo;
860 used = count = min(userCount, frameLeft);
865 data = (val >> 8) ^ 0x80;
866 if (put_user(data, (u_char *)userPtr++))
870 data = (val >> 8) ^ 0x80;
871 if (put_user(data, (u_char *)userPtr++))
877 *frameUsed += used * 4;
878 return stereo? used * 2: used;
882 static ssize_t cs4218_ct_s16_read(const u_char *userPtr, size_t userCount,
883 u_char frame[], ssize_t *frameUsed,
887 int stereo = sound.soft.stereo;
888 short *fp = (short *) &frame[*frameUsed];
891 userCount >>= (stereo? 2: 1);
892 used = count = min(userCount, frameLeft);
894 short *up = (short *) userPtr;
898 if (put_user(data, up++))
904 if (copy_to_user((u_char *)userPtr, fp, count * 4))
907 *frameUsed += used * 4;
908 return stereo? used * 4: used * 2;
911 static ssize_t cs4218_ct_u16_read(const u_char *userPtr, size_t userCount,
912 u_char frame[], ssize_t *frameUsed,
916 int mask = (sound.soft.format == AFMT_U16_LE? 0x0080: 0x8000);
917 int stereo = sound.soft.stereo;
918 short *fp = (short *) &frame[*frameUsed];
919 short *up = (short *) userPtr;
922 userCount >>= (stereo? 2: 1);
923 used = count = min(userCount, frameLeft);
929 if (put_user(data, up++))
934 if (put_user(data, up++))
940 *frameUsed += used * 4;
941 return stereo? used * 4: used * 2;
944 static TRANS transCSNormal = {
945 cs4218_ct_law, cs4218_ct_law, cs4218_ct_s8, cs4218_ct_u8,
946 cs4218_ct_s16, cs4218_ct_u16, cs4218_ct_s16, cs4218_ct_u16
949 static TRANS transCSExpand = {
950 cs4218_ctx_law, cs4218_ctx_law, cs4218_ctx_s8, cs4218_ctx_u8,
951 cs4218_ctx_s16, cs4218_ctx_u16, cs4218_ctx_s16, cs4218_ctx_u16
954 static TRANS transCSNormalRead = {
955 NULL, NULL, cs4218_ct_s8_read, cs4218_ct_u8_read,
956 cs4218_ct_s16_read, cs4218_ct_u16_read,
957 cs4218_ct_s16_read, cs4218_ct_u16_read
960 /*** Low level stuff *********************************************************/
962 static void *CS_Alloc(unsigned int size, gfp_t flags)
967 for (order=0; order < 5; order++) {
972 return (void *)__get_free_pages(flags, order);
975 static void CS_Free(void *ptr, unsigned int size)
980 for (order=0; order < 5; order++) {
985 free_pages((ulong)ptr, order);
988 static int __init CS_IrqInit(void)
990 cpm_install_handler(CPMVEC_SMC2, cs4218_intr, NULL);
995 static void CS_IrqCleanup(void)
998 volatile cpm8xx_t *cp;
1000 /* First disable transmitter and receiver.
1002 sp = &cpmp->cp_smc[1];
1003 sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
1005 /* And now shut down the SMC.
1007 cp = cpmp; /* Get pointer to Communication Processor */
1008 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
1009 CPM_CR_STOP_TX) | CPM_CR_FLG;
1010 while (cp->cp_cpcr & CPM_CR_FLG);
1012 /* Release the interrupt handler.
1014 cpm_free_handler(CPMVEC_SMC2);
1017 kd_mksound = orig_mksound;
1021 static void CS_Silence(void)
1025 /* Disable transmitter.
1027 sp = &cpmp->cp_smc[1];
1028 sp->smc_smcmr &= ~SMCMR_TEN;
1031 /* Frequencies depend upon external oscillator. There are two
1032 * choices, 12.288 and 11.2896 MHz. The RPCG audio supports both through
1033 * and external control register selection bit.
1035 static int cs4218_freqs[] = {
1036 /* 12.288 11.2896 */
1047 static void CS_Init(void)
1051 switch (sound.soft.format) {
1054 sound.hard.format = AFMT_S16_LE;
1057 sound.hard.format = AFMT_S16_BE;
1060 sound.hard.stereo = 1;
1061 sound.hard.size = 16;
1064 * If we have a sample rate which is within catchRadius percent
1065 * of the requested value, we don't have to expand the samples.
1066 * Otherwise choose the next higher rate.
1068 i = (sizeof(cs4218_freqs) / sizeof(int));
1070 tolerance = catchRadius * cs4218_freqs[--i] / 100;
1071 } while (sound.soft.speed > cs4218_freqs[i] + tolerance && i > 0);
1072 if (sound.soft.speed >= cs4218_freqs[i] - tolerance)
1073 sound.trans_write = &transCSNormal;
1075 sound.trans_write = &transCSExpand;
1076 sound.trans_read = &transCSNormalRead;
1077 sound.hard.speed = cs4218_freqs[i];
1078 cs4218_rate_index = i;
1080 /* The CS4218 has seven selectable clock dividers for the sample
1081 * clock. The HIOX then provides one of two external rates.
1082 * An even numbered frequency table index uses the high external
1085 *(uint *)HIOX_CSR4_ADDR &= ~(HIOX_CSR4_AUDCLKHI | HIOX_CSR4_AUDCLKSEL);
1087 *(uint *)HIOX_CSR4_ADDR |= HIOX_CSR4_AUDCLKHI;
1089 *(uint *)HIOX_CSR4_ADDR |= (i & HIOX_CSR4_AUDCLKSEL);
1091 expand_bal = -sound.soft.speed;
1094 static int CS_SetFormat(int format)
1100 return sound.soft.format;
1114 printk(KERN_ERR "dmasound: unknown format 0x%x, using AFMT_U8\n",
1120 sound.soft.format = format;
1121 sound.soft.size = size;
1122 if (sound.minDev == SND_DEV_DSP) {
1123 sound.dsp.format = format;
1124 sound.dsp.size = size;
1132 /* Volume is the amount of attenuation we tell the codec to impose
1133 * on the outputs. There are 32 levels, with 0 the "loudest".
1135 #define CS_VOLUME_TO_MASK(x) (31 - ((((x) - 1) * 31) / 99))
1136 #define CS_MASK_TO_VOLUME(y) (100 - ((y) * 99 / 31))
1138 static int cs_get_volume(uint reg)
1142 volume = CS_MASK_TO_VOLUME(CS_LATTEN_GET(reg));
1143 volume |= CS_MASK_TO_VOLUME(CS_RATTEN_GET(reg)) << 8;
1147 static int cs_volume_setter(int volume, int mute)
1151 if (mute && volume == 0) {
1152 tempctl = cs4218_control | CS_MUTE;
1154 tempctl = cs4218_control & ~CS_MUTE;
1155 tempctl = tempctl & ~(CS_LATTEN | CS_RATTEN);
1156 tempctl |= CS_LATTEN_SET(CS_VOLUME_TO_MASK(volume & 0xff));
1157 tempctl |= CS_RATTEN_SET(CS_VOLUME_TO_MASK((volume >> 8) & 0xff));
1158 volume = cs_get_volume(tempctl);
1160 if (tempctl != cs4218_control) {
1161 cs4218_ctl_write(tempctl);
1167 /* Gain has 16 steps from 0 to 15. These are in 1.5dB increments from
1168 * 0 (no gain) to 22.5 dB.
1170 #define CS_RECLEVEL_TO_GAIN(v) \
1171 ((v) < 0 ? 0 : (v) > 100 ? 15 : (v) * 3 / 20)
1172 #define CS_GAIN_TO_RECLEVEL(v) (((v) * 20 + 2) / 3)
1174 static int cs_get_gain(uint reg)
1178 gain = CS_GAIN_TO_RECLEVEL(CS_LGAIN_GET(reg));
1179 gain |= CS_GAIN_TO_RECLEVEL(CS_RGAIN_GET(reg)) << 8;
1183 static int cs_set_gain(int gain)
1187 tempctl = cs4218_control & ~(CS_LGAIN | CS_RGAIN);
1188 tempctl |= CS_LGAIN_SET(CS_RECLEVEL_TO_GAIN(gain & 0xff));
1189 tempctl |= CS_RGAIN_SET(CS_RECLEVEL_TO_GAIN((gain >> 8) & 0xff));
1190 gain = cs_get_gain(tempctl);
1192 if (tempctl != cs4218_control) {
1193 cs4218_ctl_write(tempctl);
1198 static int CS_SetVolume(int volume)
1200 return cs_volume_setter(volume, CS_MUTE);
1203 static void CS_Play(void)
1206 unsigned long flags;
1207 volatile cbd_t *bdp;
1208 volatile cpm8xx_t *cp;
1210 /* Protect buffer */
1211 spin_lock_irqsave(&cs4218_lock, flags);
1213 if (awacs_beep_state) {
1214 /* sound takes precedence over beeps */
1215 out_le32(&awacs_txdma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
1216 out_le32(&awacs->control,
1217 (in_le32(&awacs->control) & ~0x1f00)
1218 | (awacs_rate_index << 8));
1219 out_le32(&awacs->byteswap, sound.hard.format != AFMT_S16_BE);
1220 out_le32(&awacs_txdma->cmdptr, virt_to_bus(&(awacs_tx_cmds[(sq.front+sq.active) % sq.max_count])));
1223 awacs_beep_state = 0;
1226 i = sq.front + sq.active;
1227 if (i >= sq.max_count)
1229 while (sq.active < 2 && sq.active < sq.count) {
1230 count = (sq.count == sq.active + 1)?sq.rear_size:sq.block_size;
1231 if (count < sq.block_size && !sq.syncing)
1232 /* last block not yet filled, and we're not syncing. */
1236 bdp->cbd_datlen = count;
1238 flush_dcache_range((ulong)sound_buffers[i],
1239 (ulong)(sound_buffers[i] + count));
1241 if (++i >= sq.max_count)
1244 if (sq.active == 0) {
1245 /* The SMC does not load its fifo until the first
1246 * TDM frame pulse, so the transmit data gets shifted
1247 * by one word. To compensate for this, we incorrectly
1248 * transmit the first buffer and shorten it by one
1249 * word. Subsequent buffers are then aligned properly.
1251 bdp->cbd_datlen -= 2;
1253 /* Start up the SMC Transmitter.
1256 cp->cp_smc[1].smc_smcmr |= SMCMR_TEN;
1257 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
1258 CPM_CR_RESTART_TX) | CPM_CR_FLG;
1259 while (cp->cp_cpcr & CPM_CR_FLG);
1262 /* Buffer is ready now.
1264 bdp->cbd_sc |= BD_SC_READY;
1268 spin_unlock_irqrestore(&cs4218_lock, flags);
1272 static void CS_Record(void)
1274 unsigned long flags;
1280 /* Protect buffer */
1281 spin_lock_irqsave(&cs4218_lock, flags);
1283 /* This is all we have to do......Just start it up.
1285 sp = &cpmp->cp_smc[1];
1286 sp->smc_smcmr |= SMCMR_REN;
1290 spin_unlock_irqrestore(&cs4218_lock, flags);
1295 cs4218_tdm_tx_intr(void *devid)
1298 volatile cbd_t *bdp;
1300 while (sq.active > 0) {
1302 if (bdp->cbd_sc & BD_SC_READY)
1303 break; /* this frame is still going */
1306 if (++i >= sq.max_count)
1310 WAKE_UP(sq.action_queue);
1316 WAKE_UP(sq.sync_queue);
1321 cs4218_tdm_rx_intr(void *devid)
1324 /* We want to blow 'em off when shutting down.
1326 if (read_sq.active == 0)
1329 /* Check multiple buffers in case we were held off from
1330 * interrupt processing for a long time. Geeze, I really hope
1331 * this doesn't happen.
1333 while ((rx_base[read_sq.rear].cbd_sc & BD_SC_EMPTY) == 0) {
1335 /* Invalidate the data cache range for this buffer.
1337 invalidate_dcache_range(
1338 (uint)(sound_read_buffers[read_sq.rear]),
1339 (uint)(sound_read_buffers[read_sq.rear] + read_sq.block_size));
1341 /* Make buffer available again and move on.
1343 rx_base[read_sq.rear].cbd_sc |= BD_SC_EMPTY;
1346 /* Wrap the buffer ring.
1348 if (read_sq.rear >= read_sq.max_active)
1351 /* If we have caught up to the front buffer, bump it.
1352 * This will cause weird (but not fatal) results if the
1353 * read loop is currently using this buffer. The user is
1354 * behind in this case anyway, so weird things are going
1357 if (read_sq.rear == read_sq.front) {
1359 if (read_sq.front >= read_sq.max_active)
1364 WAKE_UP(read_sq.action_queue);
1367 static void cs_nosound(unsigned long xx)
1369 unsigned long flags;
1371 /* not sure if this is needed, since hardware command is #if 0'd */
1372 spin_lock_irqsave(&cs4218_lock, flags);
1375 st_le16(&beep_dbdma_cmd->command, DBDMA_STOP);
1379 spin_unlock_irqrestore(&cs4218_lock, flags);
1382 static DEFINE_TIMER(beep_timer, cs_nosound, 0, 0);
1385 static void cs_mksound(unsigned int hz, unsigned int ticks)
1387 unsigned long flags;
1388 int beep_speed = BEEP_SPEED;
1389 int srate = cs4218_freqs[beep_speed];
1390 int period, ncycles, nsamples;
1393 static int beep_hz_cache;
1394 static int beep_nsamples_cache;
1395 static int beep_volume_cache;
1397 if (hz <= srate / BEEP_BUFLEN || hz > srate / 2) {
1399 /* this is a hack for broken X server code */
1403 /* cancel beep currently playing */
1408 /* lock while modifying beep_timer */
1409 spin_lock_irqsave(&cs4218_lock, flags);
1410 del_timer(&beep_timer);
1412 beep_timer.expires = jiffies + ticks;
1413 add_timer(&beep_timer);
1415 if (beep_playing || sq.active || beep_buf == NULL) {
1416 spin_unlock_irqrestore(&cs4218_lock, flags);
1417 return; /* too hard, sorry :-( */
1421 st_le16(&beep_dbdma_cmd->command, OUTPUT_MORE + BR_ALWAYS);
1423 spin_unlock_irqrestore(&cs4218_lock, flags);
1425 if (hz == beep_hz_cache && beep_volume == beep_volume_cache) {
1426 nsamples = beep_nsamples_cache;
1428 period = srate * 256 / hz; /* fixed point */
1429 ncycles = BEEP_BUFLEN * 256 / period;
1430 nsamples = (period * ncycles) >> 8;
1431 f = ncycles * 65536 / nsamples;
1434 for (i = 0; i < nsamples; ++i, p += 2) {
1435 p[0] = p[1] = beep_wform[j >> 8] * beep_volume;
1436 j = (j + f) & 0xffff;
1439 beep_volume_cache = beep_volume;
1440 beep_nsamples_cache = nsamples;
1444 st_le16(&beep_dbdma_cmd->req_count, nsamples*4);
1445 st_le16(&beep_dbdma_cmd->xfer_status, 0);
1446 st_le32(&beep_dbdma_cmd->cmd_dep, virt_to_bus(beep_dbdma_cmd));
1447 st_le32(&beep_dbdma_cmd->phy_addr, virt_to_bus(beep_buf));
1448 awacs_beep_state = 1;
1450 spin_lock_irqsave(&cs4218_lock, flags);
1451 if (beep_playing) { /* i.e. haven't been terminated already */
1452 out_le32(&awacs_txdma->control, (RUN|WAKE|FLUSH|PAUSE) << 16);
1453 out_le32(&awacs->control,
1454 (in_le32(&awacs->control) & ~0x1f00)
1455 | (beep_speed << 8));
1456 out_le32(&awacs->byteswap, 0);
1457 out_le32(&awacs_txdma->cmdptr, virt_to_bus(beep_dbdma_cmd));
1458 out_le32(&awacs_txdma->control, RUN | (RUN << 16));
1460 spin_unlock_irqrestore(&cs4218_lock, flags);
1464 static MACHINE mach_cs4218 = {
1465 .owner = THIS_MODULE,
1466 .name = "HIOX CS4218",
1467 .name2 = "Built-in Sound",
1468 .dma_alloc = CS_Alloc,
1469 .dma_free = CS_Free,
1470 .irqinit = CS_IrqInit,
1472 .irqcleanup = CS_IrqCleanup,
1475 .silence = CS_Silence,
1476 .setFormat = CS_SetFormat,
1477 .setVolume = CS_SetVolume,
1482 /*** Mid level stuff *********************************************************/
1485 static void sound_silence(void)
1487 /* update hardware settings one more */
1488 (*sound.mach.init)();
1490 (*sound.mach.silence)();
1494 static void sound_init(void)
1496 (*sound.mach.init)();
1500 static int sound_set_format(int format)
1502 return(*sound.mach.setFormat)(format);
1506 static int sound_set_speed(int speed)
1509 return(sound.soft.speed);
1511 sound.soft.speed = speed;
1512 (*sound.mach.init)();
1513 if (sound.minDev == SND_DEV_DSP)
1514 sound.dsp.speed = sound.soft.speed;
1516 return(sound.soft.speed);
1520 static int sound_set_stereo(int stereo)
1523 return(sound.soft.stereo);
1525 stereo = !!stereo; /* should be 0 or 1 now */
1527 sound.soft.stereo = stereo;
1528 if (sound.minDev == SND_DEV_DSP)
1529 sound.dsp.stereo = stereo;
1530 (*sound.mach.init)();
1536 static int sound_set_volume(int volume)
1538 return(*sound.mach.setVolume)(volume);
1541 static ssize_t sound_copy_translate(const u_char *userPtr,
1543 u_char frame[], ssize_t *frameUsed,
1546 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
1548 switch (sound.soft.format) {
1550 ct_func = sound.trans_write->ct_ulaw;
1553 ct_func = sound.trans_write->ct_alaw;
1556 ct_func = sound.trans_write->ct_s8;
1559 ct_func = sound.trans_write->ct_u8;
1562 ct_func = sound.trans_write->ct_s16be;
1565 ct_func = sound.trans_write->ct_u16be;
1568 ct_func = sound.trans_write->ct_s16le;
1571 ct_func = sound.trans_write->ct_u16le;
1575 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
1580 static ssize_t sound_copy_translate_read(const u_char *userPtr,
1582 u_char frame[], ssize_t *frameUsed,
1585 ssize_t (*ct_func)(const u_char *, size_t, u_char *, ssize_t *, ssize_t) = NULL;
1587 switch (sound.soft.format) {
1589 ct_func = sound.trans_read->ct_ulaw;
1592 ct_func = sound.trans_read->ct_alaw;
1595 ct_func = sound.trans_read->ct_s8;
1598 ct_func = sound.trans_read->ct_u8;
1601 ct_func = sound.trans_read->ct_s16be;
1604 ct_func = sound.trans_read->ct_u16be;
1607 ct_func = sound.trans_read->ct_s16le;
1610 ct_func = sound.trans_read->ct_u16le;
1614 return ct_func(userPtr, userCount, frame, frameUsed, frameLeft);
1621 * /dev/mixer abstraction
1624 static int mixer_open(struct inode *inode, struct file *file)
1627 return nonseekable_open(inode, file);
1631 static int mixer_release(struct inode *inode, struct file *file)
1638 static int mixer_ioctl(struct inode *inode, struct file *file, u_int cmd,
1644 if (_SIOC_DIR(cmd) & _SIOC_WRITE)
1645 mixer.modify_counter++;
1646 if (cmd == OSS_GETVERSION)
1647 return IOCTL_OUT(arg, SOUND_VERSION);
1649 case SOUND_MIXER_INFO: {
1651 strlcpy(info.id, "CS4218_TDM", sizeof(info.id));
1652 strlcpy(info.name, "CS4218_TDM", sizeof(info.name));
1653 info.name[sizeof(info.name)-1] = 0;
1654 info.modify_counter = mixer.modify_counter;
1655 if (copy_to_user((int *)arg, &info, sizeof(info)))
1659 case SOUND_MIXER_READ_DEVMASK:
1660 data = SOUND_MASK_VOLUME | SOUND_MASK_LINE
1661 | SOUND_MASK_MIC | SOUND_MASK_RECLEV
1662 | SOUND_MASK_ALTPCM;
1663 return IOCTL_OUT(arg, data);
1664 case SOUND_MIXER_READ_RECMASK:
1665 data = SOUND_MASK_LINE | SOUND_MASK_MIC;
1666 return IOCTL_OUT(arg, data);
1667 case SOUND_MIXER_READ_RECSRC:
1668 if (cs4218_control & CS_DO1)
1669 data = SOUND_MASK_LINE;
1671 data = SOUND_MASK_MIC;
1672 return IOCTL_OUT(arg, data);
1673 case SOUND_MIXER_WRITE_RECSRC:
1674 IOCTL_IN(arg, data);
1675 data &= (SOUND_MASK_LINE | SOUND_MASK_MIC);
1676 if (data & SOUND_MASK_LINE)
1677 tmpcs = cs4218_control |
1678 (CS_ISL | CS_ISR | CS_DO1);
1679 if (data & SOUND_MASK_MIC)
1680 tmpcs = cs4218_control &
1681 ~(CS_ISL | CS_ISR | CS_DO1);
1682 if (tmpcs != cs4218_control)
1683 cs4218_ctl_write(tmpcs);
1684 return IOCTL_OUT(arg, data);
1685 case SOUND_MIXER_READ_STEREODEVS:
1686 data = SOUND_MASK_VOLUME | SOUND_MASK_RECLEV;
1687 return IOCTL_OUT(arg, data);
1688 case SOUND_MIXER_READ_CAPS:
1689 return IOCTL_OUT(arg, 0);
1690 case SOUND_MIXER_READ_VOLUME:
1691 data = (cs4218_control & CS_MUTE)? 0:
1692 cs_get_volume(cs4218_control);
1693 return IOCTL_OUT(arg, data);
1694 case SOUND_MIXER_WRITE_VOLUME:
1695 IOCTL_IN(arg, data);
1696 return IOCTL_OUT(arg, sound_set_volume(data));
1697 case SOUND_MIXER_WRITE_ALTPCM: /* really bell volume */
1698 IOCTL_IN(arg, data);
1699 beep_volume = data & 0xff;
1701 case SOUND_MIXER_READ_ALTPCM:
1702 return IOCTL_OUT(arg, beep_volume);
1703 case SOUND_MIXER_WRITE_RECLEV:
1704 IOCTL_IN(arg, data);
1705 data = cs_set_gain(data);
1706 return IOCTL_OUT(arg, data);
1707 case SOUND_MIXER_READ_RECLEV:
1708 data = cs_get_gain(cs4218_control);
1709 return IOCTL_OUT(arg, data);
1716 static struct file_operations mixer_fops =
1718 .owner = THIS_MODULE,
1719 .llseek = sound_lseek,
1720 .ioctl = mixer_ioctl,
1722 .release = mixer_release,
1726 static void __init mixer_init(void)
1728 mixer_unit = register_sound_mixer(&mixer_fops, -1);
1736 /* Set Line input, no gain, no attenuation.
1738 cs4218_control = CS_ISL | CS_ISR | CS_DO1;
1739 cs4218_control |= CS_LGAIN_SET(0) | CS_RGAIN_SET(0);
1740 cs4218_control |= CS_LATTEN_SET(0) | CS_RATTEN_SET(0);
1741 cs4218_ctl_write(cs4218_control);
1746 * Sound queue stuff, the heart of the driver
1750 static int sq_allocate_buffers(void)
1756 sound_buffers = kmalloc (numBufs * sizeof(char *), GFP_KERNEL);
1759 for (i = 0; i < numBufs; i++) {
1760 sound_buffers[i] = sound.mach.dma_alloc (bufSize << 10, GFP_KERNEL);
1761 if (!sound_buffers[i]) {
1763 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
1764 kfree (sound_buffers);
1773 static void sq_release_buffers(void)
1777 if (sound_buffers) {
1778 for (i = 0; i < numBufs; i++)
1779 sound.mach.dma_free (sound_buffers[i], bufSize << 10);
1780 kfree (sound_buffers);
1786 static int sq_allocate_read_buffers(void)
1790 if (sound_read_buffers)
1792 sound_read_buffers = kmalloc(numReadBufs * sizeof(char *), GFP_KERNEL);
1793 if (!sound_read_buffers)
1795 for (i = 0; i < numBufs; i++) {
1796 sound_read_buffers[i] = sound.mach.dma_alloc (readbufSize<<10,
1798 if (!sound_read_buffers[i]) {
1800 sound.mach.dma_free (sound_read_buffers[i],
1802 kfree (sound_read_buffers);
1803 sound_read_buffers = 0;
1810 static void sq_release_read_buffers(void)
1814 if (sound_read_buffers) {
1815 cpmp->cp_smc[1].smc_smcmr &= ~SMCMR_REN;
1816 for (i = 0; i < numReadBufs; i++)
1817 sound.mach.dma_free (sound_read_buffers[i],
1819 kfree (sound_read_buffers);
1820 sound_read_buffers = 0;
1825 static void sq_setup(int numBufs, int bufSize, char **write_buffers)
1828 volatile cbd_t *bdp;
1829 volatile cpm8xx_t *cp;
1832 /* Make sure the SMC transmit is shut down.
1835 sp = &cpmp->cp_smc[1];
1836 sp->smc_smcmr &= ~SMCMR_TEN;
1838 sq.max_count = numBufs;
1839 sq.max_active = numBufs;
1840 sq.block_size = bufSize;
1841 sq.buffers = write_buffers;
1843 sq.front = sq.count = 0;
1849 for (i=0; i<numBufs; i++) {
1850 bdp->cbd_bufaddr = virt_to_bus(write_buffers[i]);
1854 /* This causes the SMC to sync up with the first buffer again.
1856 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_TX) | CPM_CR_FLG;
1857 while (cp->cp_cpcr & CPM_CR_FLG);
1860 static void read_sq_setup(int numBufs, int bufSize, char **read_buffers)
1863 volatile cbd_t *bdp;
1864 volatile cpm8xx_t *cp;
1867 /* Make sure the SMC receive is shut down.
1870 sp = &cpmp->cp_smc[1];
1871 sp->smc_smcmr &= ~SMCMR_REN;
1873 read_sq.max_count = numBufs;
1874 read_sq.max_active = numBufs;
1875 read_sq.block_size = bufSize;
1876 read_sq.buffers = read_buffers;
1878 read_sq.front = read_sq.count = 0;
1880 read_sq.rear_size = 0;
1881 read_sq.syncing = 0;
1885 for (i=0; i<numReadBufs; i++) {
1886 bdp->cbd_bufaddr = virt_to_bus(read_buffers[i]);
1887 bdp->cbd_datlen = read_sq.block_size;
1891 /* This causes the SMC to sync up with the first buffer again.
1893 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2, CPM_CR_INIT_RX) | CPM_CR_FLG;
1894 while (cp->cp_cpcr & CPM_CR_FLG);
1898 static void sq_play(void)
1900 (*sound.mach.play)();
1904 /* ++TeSche: radically changed this one too */
1906 static ssize_t sq_write(struct file *file, const char *src, size_t uLeft,
1909 ssize_t uWritten = 0;
1911 ssize_t uUsed, bUsed, bLeft;
1913 /* ++TeSche: Is something like this necessary?
1914 * Hey, that's an honest question! Or does any other part of the
1915 * filesystem already checks this situation? I really don't know.
1920 /* The interrupt doesn't start to play the last, incomplete frame.
1921 * Thus we can append to it without disabling the interrupts! (Note
1922 * also that sq.rear isn't affected by the interrupt.)
1925 if (sq.count > 0 && (bLeft = sq.block_size-sq.rear_size) > 0) {
1926 dest = sq_block_address(sq.rear);
1927 bUsed = sq.rear_size;
1928 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
1934 sq.rear_size = bUsed;
1938 while (sq.count == sq.max_active) {
1940 if (NON_BLOCKING(sq.open_mode))
1941 return uWritten > 0 ? uWritten : -EAGAIN;
1942 SLEEP(sq.action_queue);
1943 if (SIGNAL_RECEIVED)
1944 return uWritten > 0 ? uWritten : -EINTR;
1947 /* Here, we can avoid disabling the interrupt by first
1948 * copying and translating the data, and then updating
1949 * the sq variables. Until this is done, the interrupt
1950 * won't see the new frame and we can work on it
1954 dest = sq_block_address((sq.rear+1) % sq.max_count);
1956 bLeft = sq.block_size;
1957 uUsed = sound_copy_translate(src, uLeft, dest, &bUsed, bLeft);
1964 sq.rear = (sq.rear+1) % sq.max_count;
1965 sq.rear_size = bUsed;
1968 } while (bUsed); /* uUsed may have been 0 */
1972 return uUsed < 0? uUsed: uWritten;
1978 /* Here is how the values are used for reading.
1979 * The value 'active' simply indicates the DMA is running. This is
1980 * done so the driver semantics are DMA starts when the first read is
1981 * posted. The value 'front' indicates the buffer we should next
1982 * send to the user. The value 'rear' indicates the buffer the DMA is
1983 * currently filling. When 'front' == 'rear' the buffer "ring" is
1984 * empty (we always have an empty available). The 'rear_size' is used
1985 * to track partial offsets into the current buffer. Right now, I just keep
1986 * The DMA running. If the reader can't keep up, the interrupt tosses
1987 * the oldest buffer. We could also shut down the DMA in this case.
1989 static ssize_t sq_read(struct file *file, char *dst, size_t uLeft,
1993 ssize_t uRead, bLeft, bUsed, uUsed;
1998 if (!read_sq.active)
1999 CS_Record(); /* Kick off the record process. */
2003 /* Move what the user requests, depending upon other options.
2007 /* When front == rear, the DMA is not done yet.
2009 while (read_sq.front == read_sq.rear) {
2010 if (NON_BLOCKING(read_sq.open_mode)) {
2011 return uRead > 0 ? uRead : -EAGAIN;
2013 SLEEP(read_sq.action_queue);
2014 if (SIGNAL_RECEIVED)
2015 return uRead > 0 ? uRead : -EINTR;
2018 /* The amount we move is either what is left in the
2019 * current buffer or what the user wants.
2021 bLeft = read_sq.block_size - read_sq.rear_size;
2022 bUsed = read_sq.rear_size;
2023 uUsed = sound_copy_translate_read(dst, uLeft,
2024 read_sq.buffers[read_sq.front], &bUsed, bLeft);
2030 read_sq.rear_size += bUsed;
2031 if (read_sq.rear_size >= read_sq.block_size) {
2032 read_sq.rear_size = 0;
2034 if (read_sq.front >= read_sq.max_active)
2041 static int sq_open(struct inode *inode, struct file *file)
2045 if (file->f_mode & FMODE_WRITE) {
2048 if (NON_BLOCKING(file->f_flags))
2052 SLEEP(sq.open_queue);
2053 if (SIGNAL_RECEIVED)
2057 sq.busy = 1; /* Let's play spot-the-race-condition */
2059 if (sq_allocate_buffers()) goto err_out_nobusy;
2061 sq_setup(numBufs, bufSize<<10,sound_buffers);
2062 sq.open_mode = file->f_mode;
2066 if (file->f_mode & FMODE_READ) {
2069 if (NON_BLOCKING(file->f_flags))
2072 while (read_sq.busy) {
2073 SLEEP(read_sq.open_queue);
2074 if (SIGNAL_RECEIVED)
2080 if (sq_allocate_read_buffers()) goto err_out_nobusy;
2082 read_sq_setup(numReadBufs,readbufSize<<10, sound_read_buffers);
2083 read_sq.open_mode = file->f_mode;
2086 /* Start up the 4218 by:
2090 *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_RSTAUDIO;
2092 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_ENAUDIO;
2094 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
2096 /* We need to send the current control word in case someone
2097 * opened /dev/mixer and changed things while we were shut
2098 * down. Chances are good the initialization that follows
2099 * would have done this, but it is still possible it wouldn't.
2101 cs4218_ctl_write(cs4218_control);
2103 sound.minDev = iminor(inode) & 0x0f;
2104 sound.soft = sound.dsp;
2105 sound.hard = sound.dsp;
2107 if ((iminor(inode) & 0x0f) == SND_DEV_AUDIO) {
2108 sound_set_speed(8000);
2109 sound_set_stereo(0);
2110 sound_set_format(AFMT_MU_LAW);
2113 return nonseekable_open(inode, file);
2116 if (file->f_mode & FMODE_WRITE) {
2118 WAKE_UP(sq.open_queue);
2120 if (file->f_mode & FMODE_READ) {
2122 WAKE_UP(read_sq.open_queue);
2129 static void sq_reset(void)
2134 sq.front = (sq.rear+1) % sq.max_count;
2141 static int sq_fsync(struct file *filp, struct dentry *dentry)
2146 sq_play(); /* there may be an incomplete frame waiting */
2149 SLEEP(sq.sync_queue);
2150 if (SIGNAL_RECEIVED) {
2151 /* While waiting for audio output to drain, an
2152 * interrupt occurred. Stop audio output immediately
2153 * and clear the queue. */
2164 static int sq_release(struct inode *inode, struct file *file)
2169 rc = sq_fsync(file, file->f_dentry);
2170 sound.soft = sound.dsp;
2171 sound.hard = sound.dsp;
2174 sq_release_read_buffers();
2175 sq_release_buffers();
2177 if (file->f_mode & FMODE_READ) {
2179 WAKE_UP(read_sq.open_queue);
2182 if (file->f_mode & FMODE_WRITE) {
2184 WAKE_UP(sq.open_queue);
2187 /* Shut down the SMC.
2189 cpmp->cp_smc[1].smc_smcmr &= ~(SMCMR_TEN | SMCMR_REN);
2191 /* Shut down the codec.
2193 *((volatile uint *)HIOX_CSR4_ADDR) |= HIOX_CSR4_RSTAUDIO;
2195 *((volatile uint *)HIOX_CSR4_ADDR) &= ~HIOX_CSR4_ENAUDIO;
2197 /* Wake up a process waiting for the queue being released.
2198 * Note: There may be several processes waiting for a call
2199 * to open() returning. */
2205 static int sq_ioctl(struct inode *inode, struct file *file, u_int cmd,
2217 case SNDCTL_DSP_RESET:
2220 case SNDCTL_DSP_POST:
2221 case SNDCTL_DSP_SYNC:
2222 return sq_fsync(file, file->f_dentry);
2224 /* ++TeSche: before changing any of these it's
2225 * probably wise to wait until sound playing has
2227 case SNDCTL_DSP_SPEED:
2228 sq_fsync(file, file->f_dentry);
2229 IOCTL_IN(arg, data);
2230 return IOCTL_OUT(arg, sound_set_speed(data));
2231 case SNDCTL_DSP_STEREO:
2232 sq_fsync(file, file->f_dentry);
2233 IOCTL_IN(arg, data);
2234 return IOCTL_OUT(arg, sound_set_stereo(data));
2235 case SOUND_PCM_WRITE_CHANNELS:
2236 sq_fsync(file, file->f_dentry);
2237 IOCTL_IN(arg, data);
2238 return IOCTL_OUT(arg, sound_set_stereo(data-1)+1);
2239 case SNDCTL_DSP_SETFMT:
2240 sq_fsync(file, file->f_dentry);
2241 IOCTL_IN(arg, data);
2242 return IOCTL_OUT(arg, sound_set_format(data));
2243 case SNDCTL_DSP_GETFMTS:
2245 if (sound.trans_write) {
2246 if (sound.trans_write->ct_ulaw)
2248 if (sound.trans_write->ct_alaw)
2250 if (sound.trans_write->ct_s8)
2252 if (sound.trans_write->ct_u8)
2254 if (sound.trans_write->ct_s16be)
2256 if (sound.trans_write->ct_u16be)
2258 if (sound.trans_write->ct_s16le)
2260 if (sound.trans_write->ct_u16le)
2263 return IOCTL_OUT(arg, fmt);
2264 case SNDCTL_DSP_GETBLKSIZE:
2265 size = sq.block_size
2266 * sound.soft.size * (sound.soft.stereo + 1)
2267 / (sound.hard.size * (sound.hard.stereo + 1));
2268 return IOCTL_OUT(arg, size);
2269 case SNDCTL_DSP_SUBDIVIDE:
2271 #if 0 /* Sorry can't do this at the moment. The CPM allocated buffers
2272 * long ago that can't be changed.
2274 case SNDCTL_DSP_SETFRAGMENT:
2275 if (sq.count || sq.active || sq.syncing)
2277 IOCTL_IN(arg, size);
2279 if (nbufs < 2 || nbufs > numBufs)
2282 if (size >= 8 && size <= 30) {
2284 size *= sound.hard.size * (sound.hard.stereo + 1);
2285 size /= sound.soft.size * (sound.soft.stereo + 1);
2286 if (size > (bufSize << 10))
2287 size = bufSize << 10;
2289 size = bufSize << 10;
2290 sq_setup(numBufs, size, sound_buffers);
2291 sq.max_active = nbufs;
2296 return mixer_ioctl(inode, file, cmd, arg);
2303 static struct file_operations sq_fops =
2305 .owner = THIS_MODULE,
2306 .llseek = sound_lseek,
2307 .read = sq_read, /* sq_read */
2311 .release = sq_release,
2315 static void __init sq_init(void)
2317 sq_unit = register_sound_dsp(&sq_fops, -1);
2321 init_waitqueue_head(&sq.action_queue);
2322 init_waitqueue_head(&sq.open_queue);
2323 init_waitqueue_head(&sq.sync_queue);
2324 init_waitqueue_head(&read_sq.action_queue);
2325 init_waitqueue_head(&read_sq.open_queue);
2326 init_waitqueue_head(&read_sq.sync_queue);
2331 /* whatever you like as startup mode for /dev/dsp,
2332 * (/dev/audio hasn't got a startup mode). note that
2333 * once changed a new open() will *not* restore these!
2335 sound.dsp.format = AFMT_S16_BE;
2336 sound.dsp.stereo = 1;
2337 sound.dsp.size = 16;
2339 /* set minimum rate possible without expanding */
2340 sound.dsp.speed = 8000;
2342 /* before the first open to /dev/dsp this wouldn't be set */
2343 sound.soft = sound.dsp;
2344 sound.hard = sound.dsp;
2354 /* state.buf should not overflow! */
2356 static int state_open(struct inode *inode, struct file *file)
2358 char *buffer = state.buf, *mach = "", cs4218_buf[50];
2367 sprintf(cs4218_buf, "Crystal CS4218 on TDM, ");
2370 len += sprintf(buffer+len, "%sDMA sound driver:\n", mach);
2372 len += sprintf(buffer+len, "\tsound.format = 0x%x", sound.soft.format);
2373 switch (sound.soft.format) {
2375 len += sprintf(buffer+len, " (mu-law)");
2378 len += sprintf(buffer+len, " (A-law)");
2381 len += sprintf(buffer+len, " (unsigned 8 bit)");
2384 len += sprintf(buffer+len, " (signed 8 bit)");
2387 len += sprintf(buffer+len, " (signed 16 bit big)");
2390 len += sprintf(buffer+len, " (unsigned 16 bit big)");
2393 len += sprintf(buffer+len, " (signed 16 bit little)");
2396 len += sprintf(buffer+len, " (unsigned 16 bit little)");
2399 len += sprintf(buffer+len, "\n");
2400 len += sprintf(buffer+len, "\tsound.speed = %dHz (phys. %dHz)\n",
2401 sound.soft.speed, sound.hard.speed);
2402 len += sprintf(buffer+len, "\tsound.stereo = 0x%x (%s)\n",
2403 sound.soft.stereo, sound.soft.stereo ? "stereo" : "mono");
2404 len += sprintf(buffer+len, "\tsq.block_size = %d sq.max_count = %d"
2405 " sq.max_active = %d\n",
2406 sq.block_size, sq.max_count, sq.max_active);
2407 len += sprintf(buffer+len, "\tsq.count = %d sq.rear_size = %d\n", sq.count,
2409 len += sprintf(buffer+len, "\tsq.active = %d sq.syncing = %d\n",
2410 sq.active, sq.syncing);
2412 return nonseekable_open(inode, file);
2416 static int state_release(struct inode *inode, struct file *file)
2423 static ssize_t state_read(struct file *file, char *buf, size_t count,
2426 int n = state.len - state.ptr;
2431 if (copy_to_user(buf, &state.buf[state.ptr], n))
2438 static struct file_operations state_fops =
2440 .owner = THIS_MODULE,
2441 .llseek = sound_lseek,
2444 .release = state_release,
2448 static void __init state_init(void)
2450 state_unit = register_sound_special(&state_fops, SND_DEV_STATUS);
2457 /*** Common stuff ********************************************************/
2459 static long long sound_lseek(struct file *file, long long offset, int orig)
2465 /*** Config & Setup **********************************************************/
2468 int __init tdm8xx_sound_init(void)
2472 volatile uint *sirp;
2473 volatile cbd_t *bdp;
2474 volatile cpm8xx_t *cp;
2476 volatile smc_uart_t *up;
2477 volatile immap_t *immap;
2481 /* Program the SI/TSA to use TDMa, connected to SMC2, for 4 bytes.
2483 cp = cpmp; /* Get pointer to Communication Processor */
2484 immap = (immap_t *)IMAP_ADDR; /* and to internal registers */
2486 /* Set all TDMa control bits to zero. This enables most features
2489 cp->cp_simode &= ~0x00000fff;
2491 /* Enable common receive/transmit clock pins, use IDL format.
2492 * Sync on falling edge, transmit rising clock, receive falling
2493 * clock, delay 1 bit on both Tx and Rx. Common Tx/Rx clocks and
2495 * Connect SMC2 to TSA.
2497 cp->cp_simode |= 0x80000141;
2499 /* Configure port A pins for TDMa operation.
2500 * The RPX-Lite (MPC850/823) loses SMC2 when TDM is used.
2502 immap->im_ioport.iop_papar |= 0x01c0; /* Enable TDMa functions */
2503 immap->im_ioport.iop_padir |= 0x00c0; /* Enable TDMa Tx/Rx */
2504 immap->im_ioport.iop_padir &= ~0x0100; /* Enable L1RCLKa */
2506 immap->im_ioport.iop_pcpar |= 0x0800; /* Enable L1RSYNCa */
2507 immap->im_ioport.iop_pcdir &= ~0x0800;
2509 /* Initialize the SI TDM routing table. We use TDMa only.
2510 * The receive table and transmit table each have only one
2511 * entry, to capture/send four bytes after each frame pulse.
2512 * The 16-bit ram entry is 0000 0001 1000 1111. (SMC2)
2515 sirp = (uint *)cp->cp_siram;
2517 *sirp = 0x018f0000; /* Receive entry */
2519 *sirp = 0x018f0000; /* Tramsmit entry */
2521 /* Enable single TDMa routing.
2523 cp->cp_sigmr = 0x04;
2525 /* Initialize the SMC for transparent operation.
2527 sp = &cpmp->cp_smc[1];
2528 up = (smc_uart_t *)&cp->cp_dparam[PROFF_SMC2];
2530 /* We need to allocate a transmit and receive buffer
2531 * descriptors from dual port ram.
2533 dp_addr = cpm_dpalloc(sizeof(cbd_t) * numReadBufs, 8);
2535 /* Set the physical address of the host memory
2536 * buffers in the buffer descriptors, and the
2537 * virtual address for us to work with.
2539 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2540 up->smc_rbase = dp_offset;
2541 rx_cur = rx_base = (cbd_t *)bdp;
2543 for (i=0; i<(numReadBufs-1); i++) {
2544 bdp->cbd_bufaddr = 0;
2545 bdp->cbd_datlen = 0;
2546 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2549 bdp->cbd_bufaddr = 0;
2550 bdp->cbd_datlen = 0;
2551 bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2553 /* Now, do the same for the transmit buffers.
2555 dp_offset = cpm_dpalloc(sizeof(cbd_t) * numBufs, 8);
2557 bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2558 up->smc_tbase = dp_offset;
2559 tx_cur = tx_base = (cbd_t *)bdp;
2561 for (i=0; i<(numBufs-1); i++) {
2562 bdp->cbd_bufaddr = 0;
2563 bdp->cbd_datlen = 0;
2564 bdp->cbd_sc = BD_SC_INTRPT;
2567 bdp->cbd_bufaddr = 0;
2568 bdp->cbd_datlen = 0;
2569 bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2571 /* Set transparent SMC mode.
2572 * A few things are specific to our application. The codec interface
2573 * is MSB first, hence the REVD selection. The CD/CTS pulse are
2574 * used by the TSA to indicate the frame start to the SMC.
2576 up->smc_rfcr = SCC_EB;
2577 up->smc_tfcr = SCC_EB;
2578 up->smc_mrblr = readbufSize * 1024;
2580 /* Set 16-bit reversed data, transparent mode.
2582 sp->smc_smcmr = smcr_mk_clen(15) |
2583 SMCMR_SM_TRANS | SMCMR_REVD | SMCMR_BS;
2585 /* Enable and clear events.
2586 * Because of FIFO delays, all we need is the receive interrupt
2587 * and we can process both the current receive and current
2588 * transmit interrupt within a few microseconds of the transmit.
2590 sp->smc_smce = 0xff;
2591 sp->smc_smcm = SMCM_TXE | SMCM_TX | SMCM_RX;
2593 /* Send the CPM an initialize command.
2595 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
2596 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2597 while (cp->cp_cpcr & CPM_CR_FLG);
2599 sound.mach = mach_cs4218;
2602 /* Initialize beep stuff */
2603 orig_mksound = kd_mksound;
2604 kd_mksound = cs_mksound;
2605 beep_buf = (short *) kmalloc(BEEP_BUFLEN * 4, GFP_KERNEL);
2606 if (beep_buf == NULL)
2607 printk(KERN_WARNING "dmasound: no memory for "
2613 /* Initialize the software SPI.
2617 /* Set up sound queue, /dev/audio and /dev/dsp. */
2619 /* Set default settings. */
2622 /* Set up /dev/sndstat. */
2625 /* Set up /dev/mixer. */
2628 if (!sound.mach.irqinit()) {
2629 printk(KERN_ERR "DMA sound driver: Interrupt initialization failed\n");
2636 printk(KERN_INFO "DMA sound driver installed, using %d buffers of %dk.\n",
2642 /* Due to FIFOs and bit delays, the transmit interrupt occurs a few
2643 * microseconds ahead of the receive interrupt.
2644 * When we get an interrupt, we service the transmit first, then
2645 * check for a receive to prevent the overhead of returning through
2646 * the interrupt handler only to get back here right away during
2647 * full duplex operation.
2650 cs4218_intr(void *dev_id, struct pt_regs *regs)
2653 volatile cpm8xx_t *cp;
2655 sp = &cpmp->cp_smc[1];
2657 if (sp->smc_smce & SCCM_TX) {
2658 sp->smc_smce = SCCM_TX;
2659 cs4218_tdm_tx_intr((void *)sp);
2662 if (sp->smc_smce & SCCM_RX) {
2663 sp->smc_smce = SCCM_RX;
2664 cs4218_tdm_rx_intr((void *)sp);
2667 if (sp->smc_smce & SCCM_TXE) {
2668 /* Transmit underrun. This happens with the application
2669 * didn't keep up sending buffers. We tell the SMC to
2670 * restart, which will cause it to poll the current (next)
2671 * BD. If the user supplied data since this occurred,
2672 * we just start running again. If they didn't, the SMC
2673 * will poll the descriptor until data is placed there.
2675 sp->smc_smce = SCCM_TXE;
2676 cp = cpmp; /* Get pointer to Communication Processor */
2677 cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SMC2,
2678 CPM_CR_RESTART_TX) | CPM_CR_FLG;
2679 while (cp->cp_cpcr & CPM_CR_FLG);
2684 #define MAXARGS 8 /* Should be sufficient for now */
2686 void __init dmasound_setup(char *str, int *ints)
2688 /* check the bootstrap parameter for "dmasound=" */
2692 if ((ints[3] < 0) || (ints[3] > MAX_CATCH_RADIUS))
2693 printk("dmasound_setup: invalid catch radius, using default = %d\n", catchRadius);
2695 catchRadius = ints[3];
2698 if (ints[1] < MIN_BUFFERS)
2699 printk("dmasound_setup: invalid number of buffers, using default = %d\n", numBufs);
2702 if (ints[2] < MIN_BUFSIZE || ints[2] > MAX_BUFSIZE)
2703 printk("dmasound_setup: invalid buffer size, using default = %d\n", bufSize);
2710 printk("dmasound_setup: invalid number of arguments\n");
2714 /* Software SPI functions.
2715 * These are on Port B.
2717 #define PB_SPICLK ((uint)0x00000002)
2718 #define PB_SPIMOSI ((uint)0x00000004)
2719 #define PB_SPIMISO ((uint)0x00000008)
2722 void sw_spi_init(void)
2724 volatile cpm8xx_t *cp;
2725 volatile uint *hcsr4;
2727 hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
2728 cp = cpmp; /* Get pointer to Communication Processor */
2730 *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
2732 /* Make these Port B signals general purpose I/O.
2733 * First, make sure the clock is low.
2735 cp->cp_pbdat &= ~PB_SPICLK;
2736 cp->cp_pbpar &= ~(PB_SPICLK | PB_SPIMOSI | PB_SPIMISO);
2738 /* Clock and Master Output are outputs.
2740 cp->cp_pbdir |= (PB_SPICLK | PB_SPIMOSI);
2744 cp->cp_pbdir &= ~PB_SPIMISO;
2748 /* Write the CS4218 control word out the SPI port. While the
2749 * the control word is going out, the status word is arriving.
2752 uint cs4218_ctl_write(uint ctlreg)
2756 sw_spi_io((u_char *)&ctlreg, (u_char *)&status, 4);
2758 /* Shadow the control register.....I guess we could do
2759 * the same for the status, but for now we just return it
2760 * and let the caller decide.
2762 cs4218_control = ctlreg;
2767 void sw_spi_io(u_char *obuf, u_char *ibuf, uint bcnt)
2770 u_char outbyte, inbyte;
2771 volatile cpm8xx_t *cp;
2772 volatile uint *hcsr4;
2774 hcsr4 = (volatile uint *)HIOX_CSR4_ADDR;
2775 cp = cpmp; /* Get pointer to Communication Processor */
2777 /* The timing on the bus is pretty slow. Code inefficiency
2778 * and eieio() is our friend here :-).
2780 cp->cp_pbdat &= ~PB_SPICLK;
2781 *hcsr4 |= HIOX_CSR4_AUDSPISEL; /* Enable SPI select */
2784 /* Clock in/out the bytes. Data is valid on the falling edge
2785 * of the clock. Data is MSB first.
2787 for (i=0; i<bcnt; i++) {
2790 for (bits=0; bits<8; bits++) {
2792 cp->cp_pbdat |= PB_SPICLK;
2795 cp->cp_pbdat |= PB_SPIMOSI;
2797 cp->cp_pbdat &= ~PB_SPIMOSI;
2799 cp->cp_pbdat &= ~PB_SPICLK;
2803 if (cp->cp_pbdat & PB_SPIMISO)
2809 *hcsr4 &= ~HIOX_CSR4_AUDSPISEL; /* Disable SPI select */
2813 void cleanup_module(void)
2815 if (irq_installed) {
2818 sound.mach.irqcleanup();
2822 sq_release_read_buffers();
2823 sq_release_buffers();
2825 if (mixer_unit >= 0)
2826 unregister_sound_mixer(mixer_unit);
2827 if (state_unit >= 0)
2828 unregister_sound_special(state_unit);
2830 unregister_sound_dsp(sq_unit);
2833 module_init(tdm8xx_sound_init);
2834 module_exit(cleanup_module);