opl4_lib.c 7.2 KB

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