ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / drivers / opl3 / opl3_synth.c
1 /*
2  *  Copyright (c) by Uros Bizjak <uros@kss-loka.si>
3  *                   
4  *  Routines for OPL2/OPL3/OPL4 control
5  *
6  *   This program is free software; you can redistribute it and/or modify 
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/opl3.h>
23 #include <sound/asound_fm.h>
24
25 /*
26  *    There is 18 possible 2 OP voices
27  *      (9 in the left and 9 in the right).
28  *      The first OP is the modulator and 2nd is the carrier.
29  *
30  *      The first three voices in the both sides may be connected
31  *      with another voice to a 4 OP voice. For example voice 0
32  *      can be connected with voice 3. The operators of voice 3 are
33  *      used as operators 3 and 4 of the new 4 OP voice.
34  *      In this case the 2 OP voice number 0 is the 'first half' and
35  *      voice 3 is the second.
36  */
37
38
39 /*
40  *    Register offset table for OPL2/3 voices,
41  *    OPL2 / one OPL3 register array side only
42  */
43
44 char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
45 {
46 /*        OP1   OP2   OP3   OP4         */
47 /*       ------------------------       */
48         { 0x00, 0x03, 0x08, 0x0b },
49         { 0x01, 0x04, 0x09, 0x0c },
50         { 0x02, 0x05, 0x0a, 0x0d },
51
52         { 0x08, 0x0b, 0x00, 0x00 },
53         { 0x09, 0x0c, 0x00, 0x00 },
54         { 0x0a, 0x0d, 0x00, 0x00 },
55
56         { 0x10, 0x13, 0x00, 0x00 },     /* used by percussive voices */
57         { 0x11, 0x14, 0x00, 0x00 },     /* if the percussive mode */
58         { 0x12, 0x15, 0x00, 0x00 }      /* is selected (only left reg block) */
59 };
60
61 /*
62  * prototypes
63  */
64 static int snd_opl3_play_note(opl3_t * opl3, snd_dm_fm_note_t * note);
65 static int snd_opl3_set_voice(opl3_t * opl3, snd_dm_fm_voice_t * voice);
66 static int snd_opl3_set_params(opl3_t * opl3, snd_dm_fm_params_t * params);
67 static int snd_opl3_set_mode(opl3_t * opl3, int mode);
68 static int snd_opl3_set_connection(opl3_t * opl3, int connection);
69
70 /* ------------------------------ */
71
72 /*
73  * open the device exclusively
74  */
75 int snd_opl3_open(snd_hwdep_t * hw, struct file *file)
76 {
77         opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
78
79         down(&opl3->access_mutex);
80         if (opl3->used) {
81                 up(&opl3->access_mutex);
82                 return -EAGAIN;
83         }
84         opl3->used++;
85         up(&opl3->access_mutex);
86
87         return 0;
88 }
89
90 /*
91  * ioctl for hwdep device:
92  */
93 int snd_opl3_ioctl(snd_hwdep_t * hw, struct file *file,
94                    unsigned int cmd, unsigned long arg)
95 {
96         opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
97
98         snd_assert(opl3 != NULL, return -EINVAL);
99
100         switch (cmd) {
101                 /* get information */
102         case SNDRV_DM_FM_IOCTL_INFO:
103                 {
104                         snd_dm_fm_info_t info;
105
106                         info.fm_mode = opl3->fm_mode;
107                         info.rhythm = opl3->rhythm;
108                         if (copy_to_user((snd_dm_fm_info_t *) arg, &info, sizeof(snd_dm_fm_info_t)))
109                                 return -EFAULT;
110                         return 0;
111                 }
112
113         case SNDRV_DM_FM_IOCTL_RESET:
114 #ifdef CONFIG_SND_OSSEMUL
115         case SNDRV_DM_FM_OSS_IOCTL_RESET:
116 #endif
117                 snd_opl3_reset(opl3);
118                 return 0;
119
120         case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
121 #ifdef CONFIG_SND_OSSEMUL
122         case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
123 #endif
124                 {
125                         snd_dm_fm_note_t note;
126                         if (copy_from_user(&note, (snd_dm_fm_note_t *) arg, sizeof(snd_dm_fm_note_t)))
127                                 return -EFAULT;
128                         return snd_opl3_play_note(opl3, &note);
129                 }
130
131         case SNDRV_DM_FM_IOCTL_SET_VOICE:
132 #ifdef CONFIG_SND_OSSEMUL
133         case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
134 #endif
135                 {
136                         snd_dm_fm_voice_t voice;
137                         if (copy_from_user(&voice, (snd_dm_fm_voice_t *) arg, sizeof(snd_dm_fm_voice_t)))
138                                 return -EFAULT;
139                         return snd_opl3_set_voice(opl3, &voice);
140                 }
141
142         case SNDRV_DM_FM_IOCTL_SET_PARAMS:
143 #ifdef CONFIG_SND_OSSEMUL
144         case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
145 #endif
146                 {
147                         snd_dm_fm_params_t params;
148                         if (copy_from_user(&params, (snd_dm_fm_params_t *) arg, sizeof(snd_dm_fm_params_t)))
149                                 return -EFAULT;
150                         return snd_opl3_set_params(opl3, &params);
151                 }
152
153         case SNDRV_DM_FM_IOCTL_SET_MODE:
154 #ifdef CONFIG_SND_OSSEMUL
155         case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
156 #endif
157                 return snd_opl3_set_mode(opl3, (int) arg);
158
159         case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
160 #ifdef CONFIG_SND_OSSEMUL
161         case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
162 #endif
163                 return snd_opl3_set_connection(opl3, (int) arg);
164
165 #ifdef CONFIG_SND_DEBUG
166         default:
167                 snd_printk("unknown IOCTL: 0x%x\n", cmd);
168 #endif
169         }
170         return -ENOTTY;
171 }
172
173 /*
174  * close the device
175  */
176 int snd_opl3_release(snd_hwdep_t * hw, struct file *file)
177 {
178         opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
179
180         snd_opl3_reset(opl3);
181         down(&opl3->access_mutex);
182         opl3->used--;
183         up(&opl3->access_mutex);
184
185         return 0;
186 }
187
188 /* ------------------------------ */
189
190 void snd_opl3_reset(opl3_t * opl3)
191 {
192         unsigned short opl3_reg;
193
194         unsigned short reg_side;
195         unsigned char voice_offset;
196
197         int max_voices, i;
198
199         max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
200                 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
201
202         for (i = 0; i < max_voices; i++) {
203                 /* Get register array side and offset of voice */
204                 if (i < MAX_OPL2_VOICES) {
205                         /* Left register block for voices 0 .. 8 */
206                         reg_side = OPL3_LEFT;
207                         voice_offset = i;
208                 } else {
209                         /* Right register block for voices 9 .. 17 */
210                         reg_side = OPL3_RIGHT;
211                         voice_offset = i - MAX_OPL2_VOICES;
212                 }
213                 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
214                 opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
215                 opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
216                 opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
217
218                 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
219                 opl3->command(opl3, opl3_reg, 0x00);    /* Note off */
220         }
221
222         opl3->max_voices = MAX_OPL2_VOICES;
223         opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
224
225         opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
226         opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);     /* Melodic mode */
227         opl3->rhythm = 0;
228 }
229
230
231 static int snd_opl3_play_note(opl3_t * opl3, snd_dm_fm_note_t * note)
232 {
233         unsigned short reg_side;
234         unsigned char voice_offset;
235
236         unsigned short opl3_reg;
237         unsigned char reg_val;
238
239         /* Voices 0 -  8 in OPL2 mode */
240         /* Voices 0 - 17 in OPL3 mode */
241         if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
242                             MAX_OPL3_VOICES : MAX_OPL2_VOICES))
243                 return -EINVAL;
244
245         /* Get register array side and offset of voice */
246         if (note->voice < MAX_OPL2_VOICES) {
247                 /* Left register block for voices 0 .. 8 */
248                 reg_side = OPL3_LEFT;
249                 voice_offset = note->voice;
250         } else {
251                 /* Right register block for voices 9 .. 17 */
252                 reg_side = OPL3_RIGHT;
253                 voice_offset = note->voice - MAX_OPL2_VOICES;
254         }
255
256         /* Set lower 8 bits of note frequency */
257         reg_val = (unsigned char) note->fnum;
258         opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
259         opl3->command(opl3, opl3_reg, reg_val);
260         
261         reg_val = 0x00;
262         /* Set output sound flag */
263         if (note->key_on)
264                 reg_val |= OPL3_KEYON_BIT;
265         /* Set octave */
266         reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
267         /* Set higher 2 bits of note frequency */
268         reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
269
270         /* Set OPL3 KEYON_BLOCK register of requested voice */ 
271         opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
272         opl3->command(opl3, opl3_reg, reg_val);
273
274         return 0;
275 }
276
277
278 static int snd_opl3_set_voice(opl3_t * opl3, snd_dm_fm_voice_t * voice)
279 {
280         unsigned short reg_side;
281         unsigned char op_offset;
282         unsigned char voice_offset;
283
284         unsigned short opl3_reg;
285         unsigned char reg_val;
286
287         /* Only operators 1 and 2 */
288         if (voice->op > 1)
289                 return -EINVAL;
290         /* Voices 0 -  8 in OPL2 mode */
291         /* Voices 0 - 17 in OPL3 mode */
292         if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
293                              MAX_OPL3_VOICES : MAX_OPL2_VOICES))
294                 return -EINVAL;
295
296         /* Get register array side and offset of voice */
297         if (voice->voice < MAX_OPL2_VOICES) {
298                 /* Left register block for voices 0 .. 8 */
299                 reg_side = OPL3_LEFT;
300                 voice_offset = voice->voice;
301         } else {
302                 /* Right register block for voices 9 .. 17 */
303                 reg_side = OPL3_RIGHT;
304                 voice_offset = voice->voice - MAX_OPL2_VOICES;
305         }
306         /* Get register offset of operator */
307         op_offset = snd_opl3_regmap[voice_offset][voice->op];
308
309         reg_val = 0x00;
310         /* Set amplitude modulation (tremolo) effect */
311         if (voice->am)
312                 reg_val |= OPL3_TREMOLO_ON;
313         /* Set vibrato effect */
314         if (voice->vibrato)
315                 reg_val |= OPL3_VIBRATO_ON;
316         /* Set sustaining sound phase */
317         if (voice->do_sustain)
318                 reg_val |= OPL3_SUSTAIN_ON;
319         /* Set keyboard scaling bit */ 
320         if (voice->kbd_scale)
321                 reg_val |= OPL3_KSR;
322         /* Set harmonic or frequency multiplier */
323         reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
324
325         /* Set OPL3 AM_VIB register of requested voice/operator */ 
326         opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
327         opl3->command(opl3, opl3_reg, reg_val);
328
329         /* Set decreasing volume of higher notes */
330         reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
331         /* Set output volume */
332         reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
333
334         /* Set OPL3 KSL_LEVEL register of requested voice/operator */ 
335         opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
336         opl3->command(opl3, opl3_reg, reg_val);
337
338         /* Set attack phase level */
339         reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
340         /* Set decay phase level */
341         reg_val |= voice->decay & OPL3_DECAY_MASK;
342
343         /* Set OPL3 ATTACK_DECAY register of requested voice/operator */ 
344         opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
345         opl3->command(opl3, opl3_reg, reg_val);
346
347         /* Set sustain phase level */
348         reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
349         /* Set release phase level */
350         reg_val |= voice->release & OPL3_RELEASE_MASK;
351
352         /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */ 
353         opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
354         opl3->command(opl3, opl3_reg, reg_val);
355
356         /* Set inter-operator feedback */
357         reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
358         /* Set inter-operator connection */
359         if (voice->connection)
360                 reg_val |= OPL3_CONNECTION_BIT;
361         /* OPL-3 only */
362         if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
363                 if (voice->left)
364                         reg_val |= OPL3_VOICE_TO_LEFT;
365                 if (voice->right)
366                         reg_val |= OPL3_VOICE_TO_RIGHT;
367         }
368         /* Feedback/connection bits are applicable to voice */
369         opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
370         opl3->command(opl3, opl3_reg, reg_val);
371
372         /* Select waveform */
373         reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
374         opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
375         opl3->command(opl3, opl3_reg, reg_val);
376
377         return 0;
378 }
379
380 static int snd_opl3_set_params(opl3_t * opl3, snd_dm_fm_params_t * params)
381 {
382         unsigned char reg_val;
383
384         reg_val = 0x00;
385         /* Set keyboard split method */
386         if (params->kbd_split)
387                 reg_val |= OPL3_KEYBOARD_SPLIT;
388         opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
389
390         reg_val = 0x00;
391         /* Set amplitude modulation (tremolo) depth */
392         if (params->am_depth)
393                 reg_val |= OPL3_TREMOLO_DEPTH;
394         /* Set vibrato depth */
395         if (params->vib_depth)
396                 reg_val |= OPL3_VIBRATO_DEPTH;
397         /* Set percussion mode */
398         if (params->rhythm) {
399                 reg_val |= OPL3_PERCUSSION_ENABLE;
400                 opl3->rhythm = 1;
401         } else {
402                 opl3->rhythm = 0;
403         }
404         /* Play percussion instruments */
405         if (params->bass)
406                 reg_val |= OPL3_BASSDRUM_ON;
407         if (params->snare)
408                 reg_val |= OPL3_SNAREDRUM_ON;
409         if (params->tomtom)
410                 reg_val |= OPL3_TOMTOM_ON;
411         if (params->cymbal)
412                 reg_val |= OPL3_CYMBAL_ON;
413         if (params->hihat)
414                 reg_val |= OPL3_HIHAT_ON;
415
416         opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
417         return 0;
418 }
419
420 static int snd_opl3_set_mode(opl3_t * opl3, int mode)
421 {
422         if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
423                 return -EINVAL;
424
425         opl3->fm_mode = mode;
426         if (opl3->hardware >= OPL3_HW_OPL3)
427                 opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00);     /* Clear 4-op connections */
428
429         return 0;
430 }
431
432 static int snd_opl3_set_connection(opl3_t * opl3, int connection)
433 {
434         unsigned char reg_val;
435
436         /* OPL-3 only */
437         if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
438                 return -EINVAL;
439
440         reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
441                                 OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
442         /* Set 4-op connections */
443         opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
444
445         return 0;
446 }