1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Functions for accessing OPL4 devices
4*4882a593Smuzhiyun * Copyright (c) 2003 by Clemens Ladisch <clemens@ladisch.de>
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include "opl4_local.h"
8*4882a593Smuzhiyun #include <sound/initval.h>
9*4882a593Smuzhiyun #include <linux/ioport.h>
10*4882a593Smuzhiyun #include <linux/slab.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/module.h>
13*4882a593Smuzhiyun #include <linux/io.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
16*4882a593Smuzhiyun MODULE_DESCRIPTION("OPL4 driver");
17*4882a593Smuzhiyun MODULE_LICENSE("GPL");
18*4882a593Smuzhiyun
snd_opl4_wait(struct snd_opl4 * opl4)19*4882a593Smuzhiyun static inline void snd_opl4_wait(struct snd_opl4 *opl4)
20*4882a593Smuzhiyun {
21*4882a593Smuzhiyun int timeout = 10;
22*4882a593Smuzhiyun while ((inb(opl4->fm_port) & OPL4_STATUS_BUSY) && --timeout > 0)
23*4882a593Smuzhiyun ;
24*4882a593Smuzhiyun }
25*4882a593Smuzhiyun
snd_opl4_write(struct snd_opl4 * opl4,u8 reg,u8 value)26*4882a593Smuzhiyun void snd_opl4_write(struct snd_opl4 *opl4, u8 reg, u8 value)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun snd_opl4_wait(opl4);
29*4882a593Smuzhiyun outb(reg, opl4->pcm_port);
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun snd_opl4_wait(opl4);
32*4882a593Smuzhiyun outb(value, opl4->pcm_port + 1);
33*4882a593Smuzhiyun }
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl4_write);
36*4882a593Smuzhiyun
snd_opl4_read(struct snd_opl4 * opl4,u8 reg)37*4882a593Smuzhiyun u8 snd_opl4_read(struct snd_opl4 *opl4, u8 reg)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun snd_opl4_wait(opl4);
40*4882a593Smuzhiyun outb(reg, opl4->pcm_port);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun snd_opl4_wait(opl4);
43*4882a593Smuzhiyun return inb(opl4->pcm_port + 1);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl4_read);
47*4882a593Smuzhiyun
snd_opl4_read_memory(struct snd_opl4 * opl4,char * buf,int offset,int size)48*4882a593Smuzhiyun void snd_opl4_read_memory(struct snd_opl4 *opl4, char *buf, int offset, int size)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun unsigned long flags;
51*4882a593Smuzhiyun u8 memcfg;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun spin_lock_irqsave(&opl4->reg_lock, flags);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
56*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
59*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
60*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun snd_opl4_wait(opl4);
63*4882a593Smuzhiyun outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
64*4882a593Smuzhiyun snd_opl4_wait(opl4);
65*4882a593Smuzhiyun insb(opl4->pcm_port + 1, buf, size);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun spin_unlock_irqrestore(&opl4->reg_lock, flags);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl4_read_memory);
73*4882a593Smuzhiyun
snd_opl4_write_memory(struct snd_opl4 * opl4,const char * buf,int offset,int size)74*4882a593Smuzhiyun void snd_opl4_write_memory(struct snd_opl4 *opl4, const char *buf, int offset, int size)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun unsigned long flags;
77*4882a593Smuzhiyun u8 memcfg;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun spin_lock_irqsave(&opl4->reg_lock, flags);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
82*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
85*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
86*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun snd_opl4_wait(opl4);
89*4882a593Smuzhiyun outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
90*4882a593Smuzhiyun snd_opl4_wait(opl4);
91*4882a593Smuzhiyun outsb(opl4->pcm_port + 1, buf, size);
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun spin_unlock_irqrestore(&opl4->reg_lock, flags);
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl4_write_memory);
99*4882a593Smuzhiyun
snd_opl4_enable_opl4(struct snd_opl4 * opl4)100*4882a593Smuzhiyun static void snd_opl4_enable_opl4(struct snd_opl4 *opl4)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun outb(OPL3_REG_MODE, opl4->fm_port + 2);
103*4882a593Smuzhiyun inb(opl4->fm_port);
104*4882a593Smuzhiyun inb(opl4->fm_port);
105*4882a593Smuzhiyun outb(OPL3_OPL3_ENABLE | OPL3_OPL4_ENABLE, opl4->fm_port + 3);
106*4882a593Smuzhiyun inb(opl4->fm_port);
107*4882a593Smuzhiyun inb(opl4->fm_port);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
snd_opl4_detect(struct snd_opl4 * opl4)110*4882a593Smuzhiyun static int snd_opl4_detect(struct snd_opl4 *opl4)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun u8 id1, id2;
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun snd_opl4_enable_opl4(opl4);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun id1 = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
117*4882a593Smuzhiyun snd_printdd("OPL4[02]=%02x\n", id1);
118*4882a593Smuzhiyun switch (id1 & OPL4_DEVICE_ID_MASK) {
119*4882a593Smuzhiyun case 0x20:
120*4882a593Smuzhiyun opl4->hardware = OPL3_HW_OPL4;
121*4882a593Smuzhiyun break;
122*4882a593Smuzhiyun case 0x40:
123*4882a593Smuzhiyun opl4->hardware = OPL3_HW_OPL4_ML;
124*4882a593Smuzhiyun break;
125*4882a593Smuzhiyun default:
126*4882a593Smuzhiyun return -ENODEV;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x00);
130*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0xff);
131*4882a593Smuzhiyun id1 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_FM);
132*4882a593Smuzhiyun id2 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_PCM);
133*4882a593Smuzhiyun snd_printdd("OPL4 id1=%02x id2=%02x\n", id1, id2);
134*4882a593Smuzhiyun if (id1 != 0x00 || id2 != 0xff)
135*4882a593Smuzhiyun return -ENODEV;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x3f);
138*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0x3f);
139*4882a593Smuzhiyun snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, 0x00);
140*4882a593Smuzhiyun return 0;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SEQUENCER)
snd_opl4_seq_dev_free(struct snd_seq_device * seq_dev)144*4882a593Smuzhiyun static void snd_opl4_seq_dev_free(struct snd_seq_device *seq_dev)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct snd_opl4 *opl4 = seq_dev->private_data;
147*4882a593Smuzhiyun opl4->seq_dev = NULL;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
snd_opl4_create_seq_dev(struct snd_opl4 * opl4,int seq_device)150*4882a593Smuzhiyun static int snd_opl4_create_seq_dev(struct snd_opl4 *opl4, int seq_device)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun opl4->seq_dev_num = seq_device;
153*4882a593Smuzhiyun if (snd_seq_device_new(opl4->card, seq_device, SNDRV_SEQ_DEV_ID_OPL4,
154*4882a593Smuzhiyun sizeof(struct snd_opl4 *), &opl4->seq_dev) >= 0) {
155*4882a593Smuzhiyun strcpy(opl4->seq_dev->name, "OPL4 Wavetable");
156*4882a593Smuzhiyun *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(opl4->seq_dev) = opl4;
157*4882a593Smuzhiyun opl4->seq_dev->private_data = opl4;
158*4882a593Smuzhiyun opl4->seq_dev->private_free = snd_opl4_seq_dev_free;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun return 0;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun #endif
163*4882a593Smuzhiyun
snd_opl4_free(struct snd_opl4 * opl4)164*4882a593Smuzhiyun static void snd_opl4_free(struct snd_opl4 *opl4)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun snd_opl4_free_proc(opl4);
167*4882a593Smuzhiyun release_and_free_resource(opl4->res_fm_port);
168*4882a593Smuzhiyun release_and_free_resource(opl4->res_pcm_port);
169*4882a593Smuzhiyun kfree(opl4);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
snd_opl4_dev_free(struct snd_device * device)172*4882a593Smuzhiyun static int snd_opl4_dev_free(struct snd_device *device)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun struct snd_opl4 *opl4 = device->device_data;
175*4882a593Smuzhiyun snd_opl4_free(opl4);
176*4882a593Smuzhiyun return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
snd_opl4_create(struct snd_card * card,unsigned long fm_port,unsigned long pcm_port,int seq_device,struct snd_opl3 ** ropl3,struct snd_opl4 ** ropl4)179*4882a593Smuzhiyun int snd_opl4_create(struct snd_card *card,
180*4882a593Smuzhiyun unsigned long fm_port, unsigned long pcm_port,
181*4882a593Smuzhiyun int seq_device,
182*4882a593Smuzhiyun struct snd_opl3 **ropl3, struct snd_opl4 **ropl4)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun struct snd_opl4 *opl4;
185*4882a593Smuzhiyun struct snd_opl3 *opl3;
186*4882a593Smuzhiyun int err;
187*4882a593Smuzhiyun static const struct snd_device_ops ops = {
188*4882a593Smuzhiyun .dev_free = snd_opl4_dev_free
189*4882a593Smuzhiyun };
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (ropl3)
192*4882a593Smuzhiyun *ropl3 = NULL;
193*4882a593Smuzhiyun if (ropl4)
194*4882a593Smuzhiyun *ropl4 = NULL;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun opl4 = kzalloc(sizeof(*opl4), GFP_KERNEL);
197*4882a593Smuzhiyun if (!opl4)
198*4882a593Smuzhiyun return -ENOMEM;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun opl4->res_fm_port = request_region(fm_port, 8, "OPL4 FM");
201*4882a593Smuzhiyun opl4->res_pcm_port = request_region(pcm_port, 8, "OPL4 PCM/MIX");
202*4882a593Smuzhiyun if (!opl4->res_fm_port || !opl4->res_pcm_port) {
203*4882a593Smuzhiyun snd_printk(KERN_ERR "opl4: can't grab ports 0x%lx, 0x%lx\n", fm_port, pcm_port);
204*4882a593Smuzhiyun snd_opl4_free(opl4);
205*4882a593Smuzhiyun return -EBUSY;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun opl4->card = card;
209*4882a593Smuzhiyun opl4->fm_port = fm_port;
210*4882a593Smuzhiyun opl4->pcm_port = pcm_port;
211*4882a593Smuzhiyun spin_lock_init(&opl4->reg_lock);
212*4882a593Smuzhiyun mutex_init(&opl4->access_mutex);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun err = snd_opl4_detect(opl4);
215*4882a593Smuzhiyun if (err < 0) {
216*4882a593Smuzhiyun snd_opl4_free(opl4);
217*4882a593Smuzhiyun snd_printd("OPL4 chip not detected at %#lx/%#lx\n", fm_port, pcm_port);
218*4882a593Smuzhiyun return err;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun err = snd_device_new(card, SNDRV_DEV_CODEC, opl4, &ops);
222*4882a593Smuzhiyun if (err < 0) {
223*4882a593Smuzhiyun snd_opl4_free(opl4);
224*4882a593Smuzhiyun return err;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun err = snd_opl3_create(card, fm_port, fm_port + 2, opl4->hardware, 1, &opl3);
228*4882a593Smuzhiyun if (err < 0) {
229*4882a593Smuzhiyun snd_device_free(card, opl4);
230*4882a593Smuzhiyun return err;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun /* opl3 initialization disabled opl4, so reenable */
234*4882a593Smuzhiyun snd_opl4_enable_opl4(opl4);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun snd_opl4_create_mixer(opl4);
237*4882a593Smuzhiyun snd_opl4_create_proc(opl4);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SEQUENCER)
240*4882a593Smuzhiyun opl4->seq_client = -1;
241*4882a593Smuzhiyun if (opl4->hardware < OPL3_HW_OPL4_ML)
242*4882a593Smuzhiyun snd_opl4_create_seq_dev(opl4, seq_device);
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun if (ropl3)
246*4882a593Smuzhiyun *ropl3 = opl3;
247*4882a593Smuzhiyun if (ropl4)
248*4882a593Smuzhiyun *ropl4 = opl4;
249*4882a593Smuzhiyun return 0;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun EXPORT_SYMBOL(snd_opl4_create);
253