opl4_lib.c 7.2 KB

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