ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / drivers / opl4 / opl4_lib.c
1 /*
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>
23 #include <linux/init.h>
24 #include <asm/io.h>
25
26 MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
27 MODULE_DESCRIPTION("OPL4 driver");
28 MODULE_LICENSE("GPL");
29 MODULE_CLASSES("{sound}");
30
31 static void inline snd_opl4_wait(opl4_t *opl4)
32 {
33         int timeout = 10;
34         while ((inb(opl4->fm_port) & OPL4_STATUS_BUSY) && --timeout > 0)
35                 ;
36 }
37
38 void snd_opl4_write(opl4_t *opl4, u8 reg, u8 value)
39 {
40         unsigned long flags;
41
42         spin_lock_irqsave(&opl4->reg_lock, flags);
43
44         snd_opl4_wait(opl4);
45         outb(reg, opl4->pcm_port);
46
47         snd_opl4_wait(opl4);
48         outb(value, opl4->pcm_port + 1);
49
50         spin_unlock_irqrestore(&opl4->reg_lock, flags);
51 }
52
53 u8 snd_opl4_read(opl4_t *opl4, u8 reg)
54 {
55         unsigned long flags;
56         u8 value;
57
58         spin_lock_irqsave(&opl4->reg_lock, flags);
59
60         snd_opl4_wait(opl4);
61         outb(reg, opl4->pcm_port);
62
63         snd_opl4_wait(opl4);
64         value = inb(opl4->pcm_port + 1);
65
66         spin_unlock_irqrestore(&opl4->reg_lock, flags);
67         return value;
68 }
69
70 void snd_opl4_read_memory(opl4_t *opl4, char *buf, int offset, int size)
71 {
72         u8 memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
73         snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
74
75         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
76         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
77         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
78         for (; size > 0; size--)
79                 *buf++ = snd_opl4_read(opl4, OPL4_REG_MEMORY_DATA);
80
81         snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
82 }
83
84 void snd_opl4_write_memory(opl4_t *opl4, const char *buf, int offset, int size)
85 {
86         u8 memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
87         snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
88
89         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
90         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
91         snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
92         for (; size > 0; size--)
93                 snd_opl4_write(opl4, OPL4_REG_MEMORY_DATA, *buf++);
94
95         snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
96 }
97
98 static void snd_opl4_enable_opl4(opl4_t *opl4)
99 {
100         outb(OPL3_REG_MODE, opl4->fm_port + 2);
101         inb(opl4->fm_port);
102         inb(opl4->fm_port);
103         outb(OPL3_OPL3_ENABLE | OPL3_OPL4_ENABLE, opl4->fm_port + 3);
104         inb(opl4->fm_port);
105         inb(opl4->fm_port);
106 }
107
108 static int snd_opl4_detect(opl4_t *opl4)
109 {
110         u8 id1, id2;
111
112         snd_opl4_enable_opl4(opl4);
113
114         id1 = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
115         snd_printdd("OPL4[02]=%02x\n", id1);
116         switch (id1 & OPL4_DEVICE_ID_MASK) {
117         case 0x20:
118                 opl4->hardware = OPL3_HW_OPL4;
119                 break;
120         case 0x40:
121                 opl4->hardware = OPL3_HW_OPL4_ML;
122                 break;
123         default:
124                 return -ENODEV;
125         }
126
127         snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x00);
128         snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0xff);
129         id1 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_FM);
130         id2 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_PCM);
131         snd_printdd("OPL4 id1=%02x id2=%02x\n", id1, id2);
132         if (id1 != 0x00 || id2 != 0xff)
133                 return -ENODEV;
134
135         snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x3f);
136         snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0x3f);
137         snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, 0x00);
138         return 0;
139 }
140
141 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
142 static void snd_opl4_seq_dev_free(snd_seq_device_t *seq_dev)
143 {
144         opl4_t *opl4 = snd_magic_cast(opl4_t, seq_dev->private_data, return);
145         opl4->seq_dev = NULL;
146 }
147
148 static int snd_opl4_create_seq_dev(opl4_t *opl4, int seq_device)
149 {
150         opl4->seq_dev_num = seq_device;
151         if (snd_seq_device_new(opl4->card, seq_device, SNDRV_SEQ_DEV_ID_OPL4,
152                                sizeof(opl4_t *), &opl4->seq_dev) >= 0) {
153                 strcpy(opl4->seq_dev->name, "OPL4 Wavetable");
154                 *(opl4_t **)SNDRV_SEQ_DEVICE_ARGPTR(opl4->seq_dev) = opl4;
155                 opl4->seq_dev->private_data = opl4;
156                 opl4->seq_dev->private_free = snd_opl4_seq_dev_free;
157         }
158         return 0;
159 }
160 #endif
161
162 static void snd_opl4_free(opl4_t *opl4)
163 {
164 #ifdef CONFIG_PROC_FS
165         snd_opl4_free_proc(opl4);
166 #endif
167         if (opl4->res_fm_port) {
168                 release_resource(opl4->res_fm_port);
169                 kfree_nocheck(opl4->res_fm_port);
170         }
171         if (opl4->res_pcm_port) {
172                 release_resource(opl4->res_pcm_port);
173                 kfree_nocheck(opl4->res_pcm_port);
174         }
175         snd_magic_kfree(opl4);
176 }
177
178 static int snd_opl4_dev_free(snd_device_t *device)
179 {
180         opl4_t *opl4 = snd_magic_cast(opl4_t, device->device_data, return -ENXIO);
181         snd_opl4_free(opl4);
182         return 0;
183 }
184
185 int snd_opl4_create(snd_card_t *card,
186                     unsigned long fm_port, unsigned long pcm_port,
187                     int seq_device,
188                     opl3_t **ropl3, opl4_t **ropl4)
189 {
190         opl4_t *opl4;
191         opl3_t *opl3;
192         int err;
193         static snd_device_ops_t ops = {
194                 .dev_free = snd_opl4_dev_free
195         };
196
197         if (ropl3)
198                 *ropl3 = NULL;
199         if (ropl4)
200                 *ropl4 = NULL;
201
202         opl4 = snd_magic_kcalloc(opl4_t, 0, GFP_KERNEL);
203         if (!opl4)
204                 return -ENOMEM;
205
206         opl4->res_fm_port = request_region(fm_port, 8, "OPL4 FM");
207         opl4->res_pcm_port = request_region(pcm_port, 8, "OPL4 PCM/MIX");
208         if (!opl4->res_fm_port || !opl4->res_pcm_port) {
209                 snd_printk(KERN_ERR "opl4: can't grab ports 0x%lx, 0x%lx\n", fm_port, pcm_port);
210                 snd_opl4_free(opl4);
211                 return -EBUSY;
212         }
213
214         opl4->card = card;
215         opl4->fm_port = fm_port;
216         opl4->pcm_port = pcm_port;
217         spin_lock_init(&opl4->reg_lock);
218         init_MUTEX(&opl4->access_mutex);
219
220         err = snd_opl4_detect(opl4);
221         if (err < 0) {
222                 snd_opl4_free(opl4);
223                 snd_printd("OPL4 chip not detected at %#lx/%#lx\n", fm_port, pcm_port);
224                 return err;
225         }
226
227         err = snd_opl3_create(card, fm_port, fm_port + 2, opl4->hardware, 1, &opl3);
228         if (err < 0) {
229                 snd_opl4_free(opl4);
230                 return err;
231         }
232
233         /* opl3 initialization disabled opl4, so reenable */
234         snd_opl4_enable_opl4(opl4); 
235
236         err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, opl4, &ops);
237         if (err < 0) {
238                 snd_device_free(card, opl3);
239                 snd_opl4_free(opl4);
240                 return err;
241         }
242
243         snd_opl4_create_mixer(opl4);
244 #ifdef CONFIG_PROC_FS
245         snd_opl4_create_proc(opl4);
246 #endif
247
248 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
249         opl4->seq_client = -1;
250         if (opl4->hardware < OPL3_HW_OPL4_ML)
251                 snd_opl4_create_seq_dev(opl4, seq_device);
252 #endif
253
254         if (ropl3)
255                 *ropl3 = opl3;
256         if (ropl4)
257                 *ropl4 = opl4;
258         return 0;
259 }
260
261 EXPORT_SYMBOL(snd_opl4_write);
262 EXPORT_SYMBOL(snd_opl4_read);
263 EXPORT_SYMBOL(snd_opl4_write_memory);
264 EXPORT_SYMBOL(snd_opl4_read_memory);
265 EXPORT_SYMBOL(snd_opl4_create);
266
267 static int __init alsa_opl4_init(void)
268 {
269         return 0;
270 }
271
272 static void __exit alsa_opl4_exit(void)
273 {
274 }
275
276 module_init(alsa_opl4_init)
277 module_exit(alsa_opl4_exit)