blob: f07e38da59b852748d31e3dfaf91140e943d57d6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Functions for accessing OPL4 devices
3 * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include "opl4_local.h"
21#include <sound/initval.h>
22#include <linux/ioport.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/init.h>
25#include <asm/io.h>
26
27MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
28MODULE_DESCRIPTION("OPL4 driver");
29MODULE_LICENSE("GPL");
30
Takashi Iwaia42dd422005-11-17 14:13:47 +010031static void inline snd_opl4_wait(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
33 int timeout = 10;
34 while ((inb(opl4->fm_port) & OPL4_STATUS_BUSY) && --timeout > 0)
35 ;
36}
37
Takashi Iwaia42dd422005-11-17 14:13:47 +010038void snd_opl4_write(struct snd_opl4 *opl4, u8 reg, u8 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
40 snd_opl4_wait(opl4);
41 outb(reg, opl4->pcm_port);
42
43 snd_opl4_wait(opl4);
44 outb(value, opl4->pcm_port + 1);
45}
46
Takashi Iwai4181e5f2006-04-28 15:13:39 +020047EXPORT_SYMBOL(snd_opl4_write);
48
Takashi Iwaia42dd422005-11-17 14:13:47 +010049u8 snd_opl4_read(struct snd_opl4 *opl4, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
51 snd_opl4_wait(opl4);
52 outb(reg, opl4->pcm_port);
53
54 snd_opl4_wait(opl4);
55 return inb(opl4->pcm_port + 1);
56}
57
Takashi Iwai4181e5f2006-04-28 15:13:39 +020058EXPORT_SYMBOL(snd_opl4_read);
59
Takashi Iwaia42dd422005-11-17 14:13:47 +010060void snd_opl4_read_memory(struct snd_opl4 *opl4, char *buf, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 unsigned long flags;
63 u8 memcfg;
64
65 spin_lock_irqsave(&opl4->reg_lock, flags);
66
67 memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
68 snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
69
70 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
71 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
72 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
73
74 snd_opl4_wait(opl4);
75 outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
76 snd_opl4_wait(opl4);
77 insb(opl4->pcm_port + 1, buf, size);
78
79 snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
80
81 spin_unlock_irqrestore(&opl4->reg_lock, flags);
82}
83
Takashi Iwai4181e5f2006-04-28 15:13:39 +020084EXPORT_SYMBOL(snd_opl4_read_memory);
85
Takashi Iwaia42dd422005-11-17 14:13:47 +010086void snd_opl4_write_memory(struct snd_opl4 *opl4, const char *buf, int offset, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 unsigned long flags;
89 u8 memcfg;
90
91 spin_lock_irqsave(&opl4->reg_lock, flags);
92
93 memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
94 snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
95
96 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
97 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
98 snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
99
100 snd_opl4_wait(opl4);
101 outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
102 snd_opl4_wait(opl4);
103 outsb(opl4->pcm_port + 1, buf, size);
104
105 snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
106
107 spin_unlock_irqrestore(&opl4->reg_lock, flags);
108}
109
Takashi Iwai4181e5f2006-04-28 15:13:39 +0200110EXPORT_SYMBOL(snd_opl4_write_memory);
111
Takashi Iwaia42dd422005-11-17 14:13:47 +0100112static void snd_opl4_enable_opl4(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 outb(OPL3_REG_MODE, opl4->fm_port + 2);
115 inb(opl4->fm_port);
116 inb(opl4->fm_port);
117 outb(OPL3_OPL3_ENABLE | OPL3_OPL4_ENABLE, opl4->fm_port + 3);
118 inb(opl4->fm_port);
119 inb(opl4->fm_port);
120}
121
Takashi Iwaia42dd422005-11-17 14:13:47 +0100122static int snd_opl4_detect(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 u8 id1, id2;
125
126 snd_opl4_enable_opl4(opl4);
127
128 id1 = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
129 snd_printdd("OPL4[02]=%02x\n", id1);
130 switch (id1 & OPL4_DEVICE_ID_MASK) {
131 case 0x20:
132 opl4->hardware = OPL3_HW_OPL4;
133 break;
134 case 0x40:
135 opl4->hardware = OPL3_HW_OPL4_ML;
136 break;
137 default:
138 return -ENODEV;
139 }
140
141 snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x00);
142 snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0xff);
143 id1 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_FM);
144 id2 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_PCM);
145 snd_printdd("OPL4 id1=%02x id2=%02x\n", id1, id2);
146 if (id1 != 0x00 || id2 != 0xff)
147 return -ENODEV;
148
149 snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x3f);
150 snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0x3f);
151 snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, 0x00);
152 return 0;
153}
154
155#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
Takashi Iwaia42dd422005-11-17 14:13:47 +0100156static void snd_opl4_seq_dev_free(struct snd_seq_device *seq_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100158 struct snd_opl4 *opl4 = seq_dev->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 opl4->seq_dev = NULL;
160}
161
Takashi Iwaia42dd422005-11-17 14:13:47 +0100162static int snd_opl4_create_seq_dev(struct snd_opl4 *opl4, int seq_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 opl4->seq_dev_num = seq_device;
165 if (snd_seq_device_new(opl4->card, seq_device, SNDRV_SEQ_DEV_ID_OPL4,
Takashi Iwaia42dd422005-11-17 14:13:47 +0100166 sizeof(struct snd_opl4 *), &opl4->seq_dev) >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 strcpy(opl4->seq_dev->name, "OPL4 Wavetable");
Takashi Iwaia42dd422005-11-17 14:13:47 +0100168 *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(opl4->seq_dev) = opl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 opl4->seq_dev->private_data = opl4;
170 opl4->seq_dev->private_free = snd_opl4_seq_dev_free;
171 }
172 return 0;
173}
174#endif
175
Takashi Iwaia42dd422005-11-17 14:13:47 +0100176static void snd_opl4_free(struct snd_opl4 *opl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178#ifdef CONFIG_PROC_FS
179 snd_opl4_free_proc(opl4);
180#endif
Takashi Iwaib1d57762005-10-10 11:56:31 +0200181 release_and_free_resource(opl4->res_fm_port);
182 release_and_free_resource(opl4->res_pcm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 kfree(opl4);
184}
185
Takashi Iwaia42dd422005-11-17 14:13:47 +0100186static int snd_opl4_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100188 struct snd_opl4 *opl4 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 snd_opl4_free(opl4);
190 return 0;
191}
192
Takashi Iwaia42dd422005-11-17 14:13:47 +0100193int snd_opl4_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 unsigned long fm_port, unsigned long pcm_port,
195 int seq_device,
Takashi Iwaia42dd422005-11-17 14:13:47 +0100196 struct snd_opl3 **ropl3, struct snd_opl4 **ropl4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Takashi Iwaia42dd422005-11-17 14:13:47 +0100198 struct snd_opl4 *opl4;
199 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int err;
Takashi Iwaia42dd422005-11-17 14:13:47 +0100201 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 .dev_free = snd_opl4_dev_free
203 };
204
205 if (ropl3)
206 *ropl3 = NULL;
207 if (ropl4)
208 *ropl4 = NULL;
209
Takashi Iwai561b2202005-09-09 14:22:34 +0200210 opl4 = kzalloc(sizeof(*opl4), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!opl4)
212 return -ENOMEM;
213
214 opl4->res_fm_port = request_region(fm_port, 8, "OPL4 FM");
215 opl4->res_pcm_port = request_region(pcm_port, 8, "OPL4 PCM/MIX");
216 if (!opl4->res_fm_port || !opl4->res_pcm_port) {
217 snd_printk(KERN_ERR "opl4: can't grab ports 0x%lx, 0x%lx\n", fm_port, pcm_port);
218 snd_opl4_free(opl4);
219 return -EBUSY;
220 }
221
222 opl4->card = card;
223 opl4->fm_port = fm_port;
224 opl4->pcm_port = pcm_port;
225 spin_lock_init(&opl4->reg_lock);
Ingo Molnaref9f0a42006-01-16 16:31:42 +0100226 mutex_init(&opl4->access_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 err = snd_opl4_detect(opl4);
229 if (err < 0) {
230 snd_opl4_free(opl4);
231 snd_printd("OPL4 chip not detected at %#lx/%#lx\n", fm_port, pcm_port);
232 return err;
233 }
234
235 err = snd_device_new(card, SNDRV_DEV_CODEC, opl4, &ops);
236 if (err < 0) {
237 snd_opl4_free(opl4);
238 return err;
239 }
240
241 err = snd_opl3_create(card, fm_port, fm_port + 2, opl4->hardware, 1, &opl3);
242 if (err < 0) {
243 snd_device_free(card, opl4);
244 return err;
245 }
246
247 /* opl3 initialization disabled opl4, so reenable */
248 snd_opl4_enable_opl4(opl4);
249
250 snd_opl4_create_mixer(opl4);
251#ifdef CONFIG_PROC_FS
252 snd_opl4_create_proc(opl4);
253#endif
254
255#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
256 opl4->seq_client = -1;
257 if (opl4->hardware < OPL3_HW_OPL4_ML)
258 snd_opl4_create_seq_dev(opl4, seq_device);
259#endif
260
261 if (ropl3)
262 *ropl3 = opl3;
263 if (ropl4)
264 *ropl4 = opl4;
265 return 0;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268EXPORT_SYMBOL(snd_opl4_create);
269
270static int __init alsa_opl4_init(void)
271{
272 return 0;
273}
274
275static void __exit alsa_opl4_exit(void)
276{
277}
278
279module_init(alsa_opl4_init)
280module_exit(alsa_opl4_exit)