opl4_lib.c 7.2 KB

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