2 * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
4 * Routines for OPL2/OPL3/OPL4 control
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.
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.
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
22 #include <sound/opl3.h>
23 #include <sound/asound_fm.h>
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.
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.
40 * Register offset table for OPL2/3 voices,
41 * OPL2 / one OPL3 register array side only
44 char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
47 /* ------------------------ */
48 { 0x00, 0x03, 0x08, 0x0b },
49 { 0x01, 0x04, 0x09, 0x0c },
50 { 0x02, 0x05, 0x0a, 0x0d },
52 { 0x08, 0x0b, 0x00, 0x00 },
53 { 0x09, 0x0c, 0x00, 0x00 },
54 { 0x0a, 0x0d, 0x00, 0x00 },
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) */
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);
70 /* ------------------------------ */
73 * open the device exclusively
75 int snd_opl3_open(snd_hwdep_t * hw, struct file *file)
77 opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
79 down(&opl3->access_mutex);
81 up(&opl3->access_mutex);
85 up(&opl3->access_mutex);
91 * ioctl for hwdep device:
93 int snd_opl3_ioctl(snd_hwdep_t * hw, struct file *file,
94 unsigned int cmd, unsigned long arg)
96 opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
98 snd_assert(opl3 != NULL, return -EINVAL);
101 /* get information */
102 case SNDRV_DM_FM_IOCTL_INFO:
104 snd_dm_fm_info_t info;
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)))
113 case SNDRV_DM_FM_IOCTL_RESET:
114 #ifdef CONFIG_SND_OSSEMUL
115 case SNDRV_DM_FM_OSS_IOCTL_RESET:
117 snd_opl3_reset(opl3);
120 case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
121 #ifdef CONFIG_SND_OSSEMUL
122 case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
125 snd_dm_fm_note_t note;
126 if (copy_from_user(¬e, (snd_dm_fm_note_t *) arg, sizeof(snd_dm_fm_note_t)))
128 return snd_opl3_play_note(opl3, ¬e);
131 case SNDRV_DM_FM_IOCTL_SET_VOICE:
132 #ifdef CONFIG_SND_OSSEMUL
133 case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
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)))
139 return snd_opl3_set_voice(opl3, &voice);
142 case SNDRV_DM_FM_IOCTL_SET_PARAMS:
143 #ifdef CONFIG_SND_OSSEMUL
144 case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
147 snd_dm_fm_params_t params;
148 if (copy_from_user(¶ms, (snd_dm_fm_params_t *) arg, sizeof(snd_dm_fm_params_t)))
150 return snd_opl3_set_params(opl3, ¶ms);
153 case SNDRV_DM_FM_IOCTL_SET_MODE:
154 #ifdef CONFIG_SND_OSSEMUL
155 case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
157 return snd_opl3_set_mode(opl3, (int) arg);
159 case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
160 #ifdef CONFIG_SND_OSSEMUL
161 case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
163 return snd_opl3_set_connection(opl3, (int) arg);
165 #ifdef CONFIG_SND_DEBUG
167 snd_printk("unknown IOCTL: 0x%x\n", cmd);
176 int snd_opl3_release(snd_hwdep_t * hw, struct file *file)
178 opl3_t *opl3 = snd_magic_cast(opl3_t, hw->private_data, return -ENXIO);
180 snd_opl3_reset(opl3);
181 down(&opl3->access_mutex);
183 up(&opl3->access_mutex);
188 /* ------------------------------ */
190 void snd_opl3_reset(opl3_t * opl3)
192 unsigned short opl3_reg;
194 unsigned short reg_side;
195 unsigned char voice_offset;
199 max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
200 MAX_OPL2_VOICES : MAX_OPL3_VOICES;
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;
209 /* Right register block for voices 9 .. 17 */
210 reg_side = OPL3_RIGHT;
211 voice_offset = i - MAX_OPL2_VOICES;
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 */
218 opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
219 opl3->command(opl3, opl3_reg, 0x00); /* Note off */
222 opl3->max_voices = MAX_OPL2_VOICES;
223 opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
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 */
231 static int snd_opl3_play_note(opl3_t * opl3, snd_dm_fm_note_t * note)
233 unsigned short reg_side;
234 unsigned char voice_offset;
236 unsigned short opl3_reg;
237 unsigned char reg_val;
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))
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;
251 /* Right register block for voices 9 .. 17 */
252 reg_side = OPL3_RIGHT;
253 voice_offset = note->voice - MAX_OPL2_VOICES;
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);
262 /* Set output sound flag */
264 reg_val |= OPL3_KEYON_BIT;
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;
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);
278 static int snd_opl3_set_voice(opl3_t * opl3, snd_dm_fm_voice_t * voice)
280 unsigned short reg_side;
281 unsigned char op_offset;
282 unsigned char voice_offset;
284 unsigned short opl3_reg;
285 unsigned char reg_val;
287 /* Only operators 1 and 2 */
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))
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;
302 /* Right register block for voices 9 .. 17 */
303 reg_side = OPL3_RIGHT;
304 voice_offset = voice->voice - MAX_OPL2_VOICES;
306 /* Get register offset of operator */
307 op_offset = snd_opl3_regmap[voice_offset][voice->op];
310 /* Set amplitude modulation (tremolo) effect */
312 reg_val |= OPL3_TREMOLO_ON;
313 /* Set vibrato effect */
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)
322 /* Set harmonic or frequency multiplier */
323 reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
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);
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;
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);
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;
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);
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;
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);
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;
362 if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
364 reg_val |= OPL3_VOICE_TO_LEFT;
366 reg_val |= OPL3_VOICE_TO_RIGHT;
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);
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);
380 static int snd_opl3_set_params(opl3_t * opl3, snd_dm_fm_params_t * params)
382 unsigned char reg_val;
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);
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;
404 /* Play percussion instruments */
406 reg_val |= OPL3_BASSDRUM_ON;
408 reg_val |= OPL3_SNAREDRUM_ON;
410 reg_val |= OPL3_TOMTOM_ON;
412 reg_val |= OPL3_CYMBAL_ON;
414 reg_val |= OPL3_HIHAT_ON;
416 opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
420 static int snd_opl3_set_mode(opl3_t * opl3, int mode)
422 if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
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 */
432 static int snd_opl3_set_connection(opl3_t * opl3, int connection)
434 unsigned char reg_val;
437 if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
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);