opl4_lib.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <linux/module.h>
  25. #include <linux/io.h>
  26. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  27. MODULE_DESCRIPTION("OPL4 driver");
  28. MODULE_LICENSE("GPL");
  29. static void inline snd_opl4_wait(struct snd_opl4 *opl4)
  30. {
  31. int timeout = 10;
  32. while ((inb(opl4->fm_port) & OPL4_STATUS_BUSY) && --timeout > 0)
  33. ;
  34. }
  35. void snd_opl4_write(struct snd_opl4 *opl4, u8 reg, u8 value)
  36. {
  37. snd_opl4_wait(opl4);
  38. outb(reg, opl4->pcm_port);
  39. snd_opl4_wait(opl4);
  40. outb(value, opl4->pcm_port + 1);
  41. }
  42. EXPORT_SYMBOL(snd_opl4_write);
  43. u8 snd_opl4_read(struct snd_opl4 *opl4, u8 reg)
  44. {
  45. snd_opl4_wait(opl4);
  46. outb(reg, opl4->pcm_port);
  47. snd_opl4_wait(opl4);
  48. return inb(opl4->pcm_port + 1);
  49. }
  50. EXPORT_SYMBOL(snd_opl4_read);
  51. void snd_opl4_read_memory(struct snd_opl4 *opl4, char *buf, int offset, int size)
  52. {
  53. unsigned long flags;
  54. u8 memcfg;
  55. spin_lock_irqsave(&opl4->reg_lock, flags);
  56. memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  57. snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
  58. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
  59. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
  60. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
  61. snd_opl4_wait(opl4);
  62. outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
  63. snd_opl4_wait(opl4);
  64. insb(opl4->pcm_port + 1, buf, size);
  65. snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
  66. spin_unlock_irqrestore(&opl4->reg_lock, flags);
  67. }
  68. EXPORT_SYMBOL(snd_opl4_read_memory);
  69. void snd_opl4_write_memory(struct snd_opl4 *opl4, const char *buf, int offset, int size)
  70. {
  71. unsigned long flags;
  72. u8 memcfg;
  73. spin_lock_irqsave(&opl4->reg_lock, flags);
  74. memcfg = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  75. snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg | OPL4_MODE_BIT);
  76. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_HIGH, offset >> 16);
  77. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_MID, offset >> 8);
  78. snd_opl4_write(opl4, OPL4_REG_MEMORY_ADDRESS_LOW, offset);
  79. snd_opl4_wait(opl4);
  80. outb(OPL4_REG_MEMORY_DATA, opl4->pcm_port);
  81. snd_opl4_wait(opl4);
  82. outsb(opl4->pcm_port + 1, buf, size);
  83. snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, memcfg);
  84. spin_unlock_irqrestore(&opl4->reg_lock, flags);
  85. }
  86. EXPORT_SYMBOL(snd_opl4_write_memory);
  87. static void snd_opl4_enable_opl4(struct snd_opl4 *opl4)
  88. {
  89. outb(OPL3_REG_MODE, opl4->fm_port + 2);
  90. inb(opl4->fm_port);
  91. inb(opl4->fm_port);
  92. outb(OPL3_OPL3_ENABLE | OPL3_OPL4_ENABLE, opl4->fm_port + 3);
  93. inb(opl4->fm_port);
  94. inb(opl4->fm_port);
  95. }
  96. static int snd_opl4_detect(struct snd_opl4 *opl4)
  97. {
  98. u8 id1, id2;
  99. snd_opl4_enable_opl4(opl4);
  100. id1 = snd_opl4_read(opl4, OPL4_REG_MEMORY_CONFIGURATION);
  101. snd_printdd("OPL4[02]=%02x\n", id1);
  102. switch (id1 & OPL4_DEVICE_ID_MASK) {
  103. case 0x20:
  104. opl4->hardware = OPL3_HW_OPL4;
  105. break;
  106. case 0x40:
  107. opl4->hardware = OPL3_HW_OPL4_ML;
  108. break;
  109. default:
  110. return -ENODEV;
  111. }
  112. snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x00);
  113. snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0xff);
  114. id1 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_FM);
  115. id2 = snd_opl4_read(opl4, OPL4_REG_MIX_CONTROL_PCM);
  116. snd_printdd("OPL4 id1=%02x id2=%02x\n", id1, id2);
  117. if (id1 != 0x00 || id2 != 0xff)
  118. return -ENODEV;
  119. snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_FM, 0x3f);
  120. snd_opl4_write(opl4, OPL4_REG_MIX_CONTROL_PCM, 0x3f);
  121. snd_opl4_write(opl4, OPL4_REG_MEMORY_CONFIGURATION, 0x00);
  122. return 0;
  123. }
  124. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  125. static void snd_opl4_seq_dev_free(struct snd_seq_device *seq_dev)
  126. {
  127. struct snd_opl4 *opl4 = seq_dev->private_data;
  128. opl4->seq_dev = NULL;
  129. }
  130. static int snd_opl4_create_seq_dev(struct snd_opl4 *opl4, int seq_device)
  131. {
  132. opl4->seq_dev_num = seq_device;
  133. if (snd_seq_device_new(opl4->card, seq_device, SNDRV_SEQ_DEV_ID_OPL4,
  134. sizeof(struct snd_opl4 *), &opl4->seq_dev) >= 0) {
  135. strcpy(opl4->seq_dev->name, "OPL4 Wavetable");
  136. *(struct snd_opl4 **)SNDRV_SEQ_DEVICE_ARGPTR(opl4->seq_dev) = opl4;
  137. opl4->seq_dev->private_data = opl4;
  138. opl4->seq_dev->private_free = snd_opl4_seq_dev_free;
  139. }
  140. return 0;
  141. }
  142. #endif
  143. static void snd_opl4_free(struct snd_opl4 *opl4)
  144. {
  145. snd_opl4_free_proc(opl4);
  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. snd_opl4_create_proc(opl4);
  206. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  207. opl4->seq_client = -1;
  208. if (opl4->hardware < OPL3_HW_OPL4_ML)
  209. snd_opl4_create_seq_dev(opl4, seq_device);
  210. #endif
  211. if (ropl3)
  212. *ropl3 = opl3;
  213. if (ropl4)
  214. *ropl4 = opl4;
  215. return 0;
  216. }
  217. EXPORT_SYMBOL(snd_opl4_create);
  218. static int __init alsa_opl4_init(void)
  219. {
  220. return 0;
  221. }
  222. static void __exit alsa_opl4_exit(void)
  223. {
  224. }
  225. module_init(alsa_opl4_init)
  226. module_exit(alsa_opl4_exit)