opl3sa2.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*
  2. * Driver for Yamaha OPL3-SA[2,3] soundcards
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/isa.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/pm.h>
  26. #include <linux/pnp.h>
  27. #include <linux/module.h>
  28. #include <linux/io.h>
  29. #include <sound/core.h>
  30. #include <sound/wss.h>
  31. #include <sound/mpu401.h>
  32. #include <sound/opl3.h>
  33. #include <sound/initval.h>
  34. #include <sound/tlv.h>
  35. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  36. MODULE_DESCRIPTION("Yamaha OPL3SA2+");
  37. MODULE_LICENSE("GPL");
  38. MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF719E-S},"
  39. "{Genius,Sound Maker 3DX},"
  40. "{Yamaha,OPL3SA3},"
  41. "{Intel,AL440LX sound},"
  42. "{NeoMagic,MagicWave 3DX}}");
  43. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  44. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  45. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
  46. #ifdef CONFIG_PNP
  47. static bool isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
  48. #endif
  49. static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0xf86,0x370,0x100 */
  50. static long sb_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260 */
  51. static long wss_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x530,0xe80,0xf40,0x604 */
  52. static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x388 */
  53. static long midi_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;/* 0x330,0x300 */
  54. static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 0,1,3,5,9,11,12,15 */
  55. static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  56. static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 1,3,5,6,7 */
  57. static int opl3sa3_ymode[SNDRV_CARDS]; /* 0,1,2,3 */ /*SL Added*/
  58. module_param_array(index, int, NULL, 0444);
  59. MODULE_PARM_DESC(index, "Index value for OPL3-SA soundcard.");
  60. module_param_array(id, charp, NULL, 0444);
  61. MODULE_PARM_DESC(id, "ID string for OPL3-SA soundcard.");
  62. module_param_array(enable, bool, NULL, 0444);
  63. MODULE_PARM_DESC(enable, "Enable OPL3-SA soundcard.");
  64. #ifdef CONFIG_PNP
  65. module_param_array(isapnp, bool, NULL, 0444);
  66. MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  67. #endif
  68. module_param_hw_array(port, long, ioport, NULL, 0444);
  69. MODULE_PARM_DESC(port, "Port # for OPL3-SA driver.");
  70. module_param_hw_array(sb_port, long, ioport, NULL, 0444);
  71. MODULE_PARM_DESC(sb_port, "SB port # for OPL3-SA driver.");
  72. module_param_hw_array(wss_port, long, ioport, NULL, 0444);
  73. MODULE_PARM_DESC(wss_port, "WSS port # for OPL3-SA driver.");
  74. module_param_hw_array(fm_port, long, ioport, NULL, 0444);
  75. MODULE_PARM_DESC(fm_port, "FM port # for OPL3-SA driver.");
  76. module_param_hw_array(midi_port, long, ioport, NULL, 0444);
  77. MODULE_PARM_DESC(midi_port, "MIDI port # for OPL3-SA driver.");
  78. module_param_hw_array(irq, int, irq, NULL, 0444);
  79. MODULE_PARM_DESC(irq, "IRQ # for OPL3-SA driver.");
  80. module_param_hw_array(dma1, int, dma, NULL, 0444);
  81. MODULE_PARM_DESC(dma1, "DMA1 # for OPL3-SA driver.");
  82. module_param_hw_array(dma2, int, dma, NULL, 0444);
  83. MODULE_PARM_DESC(dma2, "DMA2 # for OPL3-SA driver.");
  84. module_param_array(opl3sa3_ymode, int, NULL, 0444);
  85. MODULE_PARM_DESC(opl3sa3_ymode, "Speaker size selection for 3D Enhancement mode: Desktop/Large Notebook/Small Notebook/HiFi.");
  86. #ifdef CONFIG_PNP
  87. static int isa_registered;
  88. static int pnp_registered;
  89. static int pnpc_registered;
  90. #endif
  91. /* control ports */
  92. #define OPL3SA2_PM_CTRL 0x01
  93. #define OPL3SA2_SYS_CTRL 0x02
  94. #define OPL3SA2_IRQ_CONFIG 0x03
  95. #define OPL3SA2_IRQ_STATUS 0x04
  96. #define OPL3SA2_DMA_CONFIG 0x06
  97. #define OPL3SA2_MASTER_LEFT 0x07
  98. #define OPL3SA2_MASTER_RIGHT 0x08
  99. #define OPL3SA2_MIC 0x09
  100. #define OPL3SA2_MISC 0x0A
  101. /* opl3sa3 only */
  102. #define OPL3SA3_DGTL_DOWN 0x12
  103. #define OPL3SA3_ANLG_DOWN 0x13
  104. #define OPL3SA3_WIDE 0x14
  105. #define OPL3SA3_BASS 0x15
  106. #define OPL3SA3_TREBLE 0x16
  107. /* power management bits */
  108. #define OPL3SA2_PM_ADOWN 0x20
  109. #define OPL3SA2_PM_PSV 0x04
  110. #define OPL3SA2_PM_PDN 0x02
  111. #define OPL3SA2_PM_PDX 0x01
  112. #define OPL3SA2_PM_D0 0x00
  113. #define OPL3SA2_PM_D3 (OPL3SA2_PM_ADOWN|OPL3SA2_PM_PSV|OPL3SA2_PM_PDN|OPL3SA2_PM_PDX)
  114. struct snd_opl3sa2 {
  115. int version; /* 2 or 3 */
  116. unsigned long port; /* control port */
  117. struct resource *res_port; /* control port resource */
  118. int irq;
  119. int single_dma;
  120. spinlock_t reg_lock;
  121. struct snd_hwdep *synth;
  122. struct snd_rawmidi *rmidi;
  123. struct snd_wss *wss;
  124. unsigned char ctlregs[0x20];
  125. int ymode; /* SL added */
  126. struct snd_kcontrol *master_switch;
  127. struct snd_kcontrol *master_volume;
  128. };
  129. #define PFX "opl3sa2: "
  130. #ifdef CONFIG_PNP
  131. static const struct pnp_device_id snd_opl3sa2_pnpbiosids[] = {
  132. { .id = "YMH0021" },
  133. { .id = "NMX2210" }, /* Gateway Solo 2500 */
  134. { .id = "" } /* end */
  135. };
  136. MODULE_DEVICE_TABLE(pnp, snd_opl3sa2_pnpbiosids);
  137. static const struct pnp_card_device_id snd_opl3sa2_pnpids[] = {
  138. /* Yamaha YMF719E-S (Genius Sound Maker 3DX) */
  139. { .id = "YMH0020", .devs = { { "YMH0021" } } },
  140. /* Yamaha OPL3-SA3 (integrated on Intel's Pentium II AL440LX motherboard) */
  141. { .id = "YMH0030", .devs = { { "YMH0021" } } },
  142. /* Yamaha OPL3-SA2 */
  143. { .id = "YMH0800", .devs = { { "YMH0021" } } },
  144. /* Yamaha OPL3-SA2 */
  145. { .id = "YMH0801", .devs = { { "YMH0021" } } },
  146. /* NeoMagic MagicWave 3DX */
  147. { .id = "NMX2200", .devs = { { "YMH2210" } } },
  148. /* NeoMagic MagicWave 3D */
  149. { .id = "NMX2200", .devs = { { "NMX2210" } } },
  150. /* --- */
  151. { .id = "" } /* end */
  152. };
  153. MODULE_DEVICE_TABLE(pnp_card, snd_opl3sa2_pnpids);
  154. #endif /* CONFIG_PNP */
  155. /* read control port (w/o spinlock) */
  156. static unsigned char __snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
  157. {
  158. unsigned char result;
  159. #if 0
  160. outb(0x1d, port); /* password */
  161. printk(KERN_DEBUG "read [0x%lx] = 0x%x\n", port, inb(port));
  162. #endif
  163. outb(reg, chip->port); /* register */
  164. result = inb(chip->port + 1);
  165. #if 0
  166. printk(KERN_DEBUG "read [0x%lx] = 0x%x [0x%x]\n",
  167. port, result, inb(port));
  168. #endif
  169. return result;
  170. }
  171. /* read control port (with spinlock) */
  172. static unsigned char snd_opl3sa2_read(struct snd_opl3sa2 *chip, unsigned char reg)
  173. {
  174. unsigned long flags;
  175. unsigned char result;
  176. spin_lock_irqsave(&chip->reg_lock, flags);
  177. result = __snd_opl3sa2_read(chip, reg);
  178. spin_unlock_irqrestore(&chip->reg_lock, flags);
  179. return result;
  180. }
  181. /* write control port (w/o spinlock) */
  182. static void __snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
  183. {
  184. #if 0
  185. outb(0x1d, port); /* password */
  186. #endif
  187. outb(reg, chip->port); /* register */
  188. outb(value, chip->port + 1);
  189. chip->ctlregs[reg] = value;
  190. }
  191. /* write control port (with spinlock) */
  192. static void snd_opl3sa2_write(struct snd_opl3sa2 *chip, unsigned char reg, unsigned char value)
  193. {
  194. unsigned long flags;
  195. spin_lock_irqsave(&chip->reg_lock, flags);
  196. __snd_opl3sa2_write(chip, reg, value);
  197. spin_unlock_irqrestore(&chip->reg_lock, flags);
  198. }
  199. static int snd_opl3sa2_detect(struct snd_card *card)
  200. {
  201. struct snd_opl3sa2 *chip = card->private_data;
  202. unsigned long port;
  203. unsigned char tmp, tmp1;
  204. char str[2];
  205. port = chip->port;
  206. if ((chip->res_port = request_region(port, 2, "OPL3-SA control")) == NULL) {
  207. snd_printk(KERN_ERR PFX "can't grab port 0x%lx\n", port);
  208. return -EBUSY;
  209. }
  210. /*
  211. snd_printk(KERN_DEBUG "REG 0A = 0x%x\n",
  212. snd_opl3sa2_read(chip, 0x0a));
  213. */
  214. chip->version = 0;
  215. tmp = snd_opl3sa2_read(chip, OPL3SA2_MISC);
  216. if (tmp == 0xff) {
  217. snd_printd("OPL3-SA [0x%lx] detect = 0x%x\n", port, tmp);
  218. return -ENODEV;
  219. }
  220. switch (tmp & 0x07) {
  221. case 0x01:
  222. chip->version = 2; /* YMF711 */
  223. break;
  224. default:
  225. chip->version = 3;
  226. /* 0x02 - standard */
  227. /* 0x03 - YM715B */
  228. /* 0x04 - YM719 - OPL-SA4? */
  229. /* 0x05 - OPL3-SA3 - Libretto 100 */
  230. /* 0x07 - unknown - Neomagic MagicWave 3D */
  231. break;
  232. }
  233. str[0] = chip->version + '0';
  234. str[1] = 0;
  235. strcat(card->shortname, str);
  236. snd_opl3sa2_write(chip, OPL3SA2_MISC, tmp ^ 7);
  237. if ((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MISC)) != tmp) {
  238. snd_printd("OPL3-SA [0x%lx] detect (1) = 0x%x (0x%x)\n", port, tmp, tmp1);
  239. return -ENODEV;
  240. }
  241. /* try if the MIC register is accessible */
  242. tmp = snd_opl3sa2_read(chip, OPL3SA2_MIC);
  243. snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x8a);
  244. if (((tmp1 = snd_opl3sa2_read(chip, OPL3SA2_MIC)) & 0x9f) != 0x8a) {
  245. snd_printd("OPL3-SA [0x%lx] detect (2) = 0x%x (0x%x)\n", port, tmp, tmp1);
  246. return -ENODEV;
  247. }
  248. snd_opl3sa2_write(chip, OPL3SA2_MIC, 0x9f);
  249. /* initialization */
  250. /* Power Management - full on */
  251. snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
  252. if (chip->version > 2) {
  253. /* ymode is bits 4&5 (of 0 to 7) on all but opl3sa2 versions */
  254. snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, (chip->ymode << 4));
  255. } else {
  256. /* default for opl3sa2 versions */
  257. snd_opl3sa2_write(chip, OPL3SA2_SYS_CTRL, 0x00);
  258. }
  259. snd_opl3sa2_write(chip, OPL3SA2_IRQ_CONFIG, 0x0d); /* Interrupt Channel Configuration - IRQ A = OPL3 + MPU + WSS */
  260. if (chip->single_dma) {
  261. snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x03); /* DMA Configuration - DMA A = WSS-R + WSS-P */
  262. } else {
  263. snd_opl3sa2_write(chip, OPL3SA2_DMA_CONFIG, 0x21); /* DMA Configuration - DMA B = WSS-R, DMA A = WSS-P */
  264. }
  265. snd_opl3sa2_write(chip, OPL3SA2_MISC, 0x80 | (tmp & 7)); /* Miscellaneous - default */
  266. if (chip->version > 2) {
  267. snd_opl3sa2_write(chip, OPL3SA3_DGTL_DOWN, 0x00); /* Digital Block Partial Power Down - default */
  268. snd_opl3sa2_write(chip, OPL3SA3_ANLG_DOWN, 0x00); /* Analog Block Partial Power Down - default */
  269. }
  270. return 0;
  271. }
  272. static irqreturn_t snd_opl3sa2_interrupt(int irq, void *dev_id)
  273. {
  274. unsigned short status;
  275. struct snd_card *card = dev_id;
  276. struct snd_opl3sa2 *chip;
  277. int handled = 0;
  278. if (card == NULL)
  279. return IRQ_NONE;
  280. chip = card->private_data;
  281. status = snd_opl3sa2_read(chip, OPL3SA2_IRQ_STATUS);
  282. if (status & 0x20) {
  283. handled = 1;
  284. snd_opl3_interrupt(chip->synth);
  285. }
  286. if ((status & 0x10) && chip->rmidi != NULL) {
  287. handled = 1;
  288. snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
  289. }
  290. if (status & 0x07) { /* TI,CI,PI */
  291. handled = 1;
  292. snd_wss_interrupt(irq, chip->wss);
  293. }
  294. if (status & 0x40) { /* hardware volume change */
  295. handled = 1;
  296. /* reading from Master Lch register at 0x07 clears this bit */
  297. snd_opl3sa2_read(chip, OPL3SA2_MASTER_RIGHT);
  298. snd_opl3sa2_read(chip, OPL3SA2_MASTER_LEFT);
  299. if (chip->master_switch && chip->master_volume) {
  300. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  301. &chip->master_switch->id);
  302. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  303. &chip->master_volume->id);
  304. }
  305. }
  306. return IRQ_RETVAL(handled);
  307. }
  308. #define OPL3SA2_SINGLE(xname, xindex, reg, shift, mask, invert) \
  309. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  310. .info = snd_wss_info_single, \
  311. .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
  312. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  313. #define OPL3SA2_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
  314. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  315. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  316. .name = xname, .index = xindex, \
  317. .info = snd_wss_info_single, \
  318. .get = snd_opl3sa2_get_single, .put = snd_opl3sa2_put_single, \
  319. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24), \
  320. .tlv = { .p = (xtlv) } }
  321. static int snd_opl3sa2_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  322. {
  323. struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
  324. unsigned long flags;
  325. int reg = kcontrol->private_value & 0xff;
  326. int shift = (kcontrol->private_value >> 8) & 0xff;
  327. int mask = (kcontrol->private_value >> 16) & 0xff;
  328. int invert = (kcontrol->private_value >> 24) & 0xff;
  329. spin_lock_irqsave(&chip->reg_lock, flags);
  330. ucontrol->value.integer.value[0] = (chip->ctlregs[reg] >> shift) & mask;
  331. spin_unlock_irqrestore(&chip->reg_lock, flags);
  332. if (invert)
  333. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  334. return 0;
  335. }
  336. static int snd_opl3sa2_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  337. {
  338. struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
  339. unsigned long flags;
  340. int reg = kcontrol->private_value & 0xff;
  341. int shift = (kcontrol->private_value >> 8) & 0xff;
  342. int mask = (kcontrol->private_value >> 16) & 0xff;
  343. int invert = (kcontrol->private_value >> 24) & 0xff;
  344. int change;
  345. unsigned short val, oval;
  346. val = (ucontrol->value.integer.value[0] & mask);
  347. if (invert)
  348. val = mask - val;
  349. val <<= shift;
  350. spin_lock_irqsave(&chip->reg_lock, flags);
  351. oval = chip->ctlregs[reg];
  352. val = (oval & ~(mask << shift)) | val;
  353. change = val != oval;
  354. __snd_opl3sa2_write(chip, reg, val);
  355. spin_unlock_irqrestore(&chip->reg_lock, flags);
  356. return change;
  357. }
  358. #define OPL3SA2_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  359. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  360. .info = snd_wss_info_double, \
  361. .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
  362. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  363. #define OPL3SA2_DOUBLE_TLV(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert, xtlv) \
  364. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  365. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
  366. .name = xname, .index = xindex, \
  367. .info = snd_wss_info_double, \
  368. .get = snd_opl3sa2_get_double, .put = snd_opl3sa2_put_double, \
  369. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22), \
  370. .tlv = { .p = (xtlv) } }
  371. static int snd_opl3sa2_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  372. {
  373. struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
  374. unsigned long flags;
  375. int left_reg = kcontrol->private_value & 0xff;
  376. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  377. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  378. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  379. int mask = (kcontrol->private_value >> 24) & 0xff;
  380. int invert = (kcontrol->private_value >> 22) & 1;
  381. spin_lock_irqsave(&chip->reg_lock, flags);
  382. ucontrol->value.integer.value[0] = (chip->ctlregs[left_reg] >> shift_left) & mask;
  383. ucontrol->value.integer.value[1] = (chip->ctlregs[right_reg] >> shift_right) & mask;
  384. spin_unlock_irqrestore(&chip->reg_lock, flags);
  385. if (invert) {
  386. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  387. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  388. }
  389. return 0;
  390. }
  391. static int snd_opl3sa2_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  392. {
  393. struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
  394. unsigned long flags;
  395. int left_reg = kcontrol->private_value & 0xff;
  396. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  397. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  398. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  399. int mask = (kcontrol->private_value >> 24) & 0xff;
  400. int invert = (kcontrol->private_value >> 22) & 1;
  401. int change;
  402. unsigned short val1, val2, oval1, oval2;
  403. val1 = ucontrol->value.integer.value[0] & mask;
  404. val2 = ucontrol->value.integer.value[1] & mask;
  405. if (invert) {
  406. val1 = mask - val1;
  407. val2 = mask - val2;
  408. }
  409. val1 <<= shift_left;
  410. val2 <<= shift_right;
  411. spin_lock_irqsave(&chip->reg_lock, flags);
  412. if (left_reg != right_reg) {
  413. oval1 = chip->ctlregs[left_reg];
  414. oval2 = chip->ctlregs[right_reg];
  415. val1 = (oval1 & ~(mask << shift_left)) | val1;
  416. val2 = (oval2 & ~(mask << shift_right)) | val2;
  417. change = val1 != oval1 || val2 != oval2;
  418. __snd_opl3sa2_write(chip, left_reg, val1);
  419. __snd_opl3sa2_write(chip, right_reg, val2);
  420. } else {
  421. oval1 = chip->ctlregs[left_reg];
  422. val1 = (oval1 & ~((mask << shift_left) | (mask << shift_right))) | val1 | val2;
  423. change = val1 != oval1;
  424. __snd_opl3sa2_write(chip, left_reg, val1);
  425. }
  426. spin_unlock_irqrestore(&chip->reg_lock, flags);
  427. return change;
  428. }
  429. static const DECLARE_TLV_DB_SCALE(db_scale_master, -3000, 200, 0);
  430. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_12db_max, -3450, 150, 0);
  431. static struct snd_kcontrol_new snd_opl3sa2_controls[] = {
  432. OPL3SA2_DOUBLE("Master Playback Switch", 0, 0x07, 0x08, 7, 7, 1, 1),
  433. OPL3SA2_DOUBLE_TLV("Master Playback Volume", 0, 0x07, 0x08, 0, 0, 15, 1,
  434. db_scale_master),
  435. OPL3SA2_SINGLE("Mic Playback Switch", 0, 0x09, 7, 1, 1),
  436. OPL3SA2_SINGLE_TLV("Mic Playback Volume", 0, 0x09, 0, 31, 1,
  437. db_scale_5bit_12db_max),
  438. OPL3SA2_SINGLE("ZV Port Switch", 0, 0x02, 0, 1, 0),
  439. };
  440. static struct snd_kcontrol_new snd_opl3sa2_tone_controls[] = {
  441. OPL3SA2_DOUBLE("3D Control - Wide", 0, 0x14, 0x14, 4, 0, 7, 0),
  442. OPL3SA2_DOUBLE("Tone Control - Bass", 0, 0x15, 0x15, 4, 0, 7, 0),
  443. OPL3SA2_DOUBLE("Tone Control - Treble", 0, 0x16, 0x16, 4, 0, 7, 0)
  444. };
  445. static void snd_opl3sa2_master_free(struct snd_kcontrol *kcontrol)
  446. {
  447. struct snd_opl3sa2 *chip = snd_kcontrol_chip(kcontrol);
  448. chip->master_switch = NULL;
  449. chip->master_volume = NULL;
  450. }
  451. static int snd_opl3sa2_mixer(struct snd_card *card)
  452. {
  453. struct snd_opl3sa2 *chip = card->private_data;
  454. struct snd_ctl_elem_id id1, id2;
  455. struct snd_kcontrol *kctl;
  456. unsigned int idx;
  457. int err;
  458. memset(&id1, 0, sizeof(id1));
  459. memset(&id2, 0, sizeof(id2));
  460. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  461. /* reassign AUX0 to CD */
  462. strcpy(id1.name, "Aux Playback Switch");
  463. strcpy(id2.name, "CD Playback Switch");
  464. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
  465. snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
  466. return err;
  467. }
  468. strcpy(id1.name, "Aux Playback Volume");
  469. strcpy(id2.name, "CD Playback Volume");
  470. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
  471. snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
  472. return err;
  473. }
  474. /* reassign AUX1 to FM */
  475. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  476. strcpy(id2.name, "FM Playback Switch");
  477. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
  478. snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
  479. return err;
  480. }
  481. strcpy(id1.name, "Aux Playback Volume");
  482. strcpy(id2.name, "FM Playback Volume");
  483. if ((err = snd_ctl_rename_id(card, &id1, &id2)) < 0) {
  484. snd_printk(KERN_ERR "Cannot rename opl3sa2 control\n");
  485. return err;
  486. }
  487. /* add OPL3SA2 controls */
  488. for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_controls); idx++) {
  489. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_opl3sa2_controls[idx], chip))) < 0)
  490. return err;
  491. switch (idx) {
  492. case 0: chip->master_switch = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
  493. case 1: chip->master_volume = kctl; kctl->private_free = snd_opl3sa2_master_free; break;
  494. }
  495. }
  496. if (chip->version > 2) {
  497. for (idx = 0; idx < ARRAY_SIZE(snd_opl3sa2_tone_controls); idx++)
  498. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_opl3sa2_tone_controls[idx], chip))) < 0)
  499. return err;
  500. }
  501. return 0;
  502. }
  503. /* Power Management support functions */
  504. #ifdef CONFIG_PM
  505. static int snd_opl3sa2_suspend(struct snd_card *card, pm_message_t state)
  506. {
  507. if (card) {
  508. struct snd_opl3sa2 *chip = card->private_data;
  509. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  510. chip->wss->suspend(chip->wss);
  511. /* power down */
  512. snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D3);
  513. }
  514. return 0;
  515. }
  516. static int snd_opl3sa2_resume(struct snd_card *card)
  517. {
  518. struct snd_opl3sa2 *chip;
  519. int i;
  520. if (!card)
  521. return 0;
  522. chip = card->private_data;
  523. /* power up */
  524. snd_opl3sa2_write(chip, OPL3SA2_PM_CTRL, OPL3SA2_PM_D0);
  525. /* restore registers */
  526. for (i = 2; i <= 0x0a; i++) {
  527. if (i != OPL3SA2_IRQ_STATUS)
  528. snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
  529. }
  530. if (chip->version > 2) {
  531. for (i = 0x12; i <= 0x16; i++)
  532. snd_opl3sa2_write(chip, i, chip->ctlregs[i]);
  533. }
  534. /* restore wss */
  535. chip->wss->resume(chip->wss);
  536. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  537. return 0;
  538. }
  539. #endif /* CONFIG_PM */
  540. #ifdef CONFIG_PNP
  541. static int snd_opl3sa2_pnp(int dev, struct snd_opl3sa2 *chip,
  542. struct pnp_dev *pdev)
  543. {
  544. if (pnp_activate_dev(pdev) < 0) {
  545. snd_printk(KERN_ERR "PnP configure failure (out of resources?)\n");
  546. return -EBUSY;
  547. }
  548. sb_port[dev] = pnp_port_start(pdev, 0);
  549. wss_port[dev] = pnp_port_start(pdev, 1);
  550. fm_port[dev] = pnp_port_start(pdev, 2);
  551. midi_port[dev] = pnp_port_start(pdev, 3);
  552. port[dev] = pnp_port_start(pdev, 4);
  553. dma1[dev] = pnp_dma(pdev, 0);
  554. dma2[dev] = pnp_dma(pdev, 1);
  555. irq[dev] = pnp_irq(pdev, 0);
  556. snd_printdd("%sPnP OPL3-SA: sb port=0x%lx, wss port=0x%lx, fm port=0x%lx, midi port=0x%lx\n",
  557. pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", sb_port[dev], wss_port[dev], fm_port[dev], midi_port[dev]);
  558. snd_printdd("%sPnP OPL3-SA: control port=0x%lx, dma1=%i, dma2=%i, irq=%i\n",
  559. pnp_device_is_pnpbios(pdev) ? "BIOS" : "ISA", port[dev], dma1[dev], dma2[dev], irq[dev]);
  560. return 0;
  561. }
  562. #endif /* CONFIG_PNP */
  563. static void snd_opl3sa2_free(struct snd_card *card)
  564. {
  565. struct snd_opl3sa2 *chip = card->private_data;
  566. if (chip->irq >= 0)
  567. free_irq(chip->irq, card);
  568. release_and_free_resource(chip->res_port);
  569. }
  570. static int snd_opl3sa2_card_new(struct device *pdev, int dev,
  571. struct snd_card **cardp)
  572. {
  573. struct snd_card *card;
  574. struct snd_opl3sa2 *chip;
  575. int err;
  576. err = snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
  577. sizeof(struct snd_opl3sa2), &card);
  578. if (err < 0)
  579. return err;
  580. strcpy(card->driver, "OPL3SA2");
  581. strcpy(card->shortname, "Yamaha OPL3-SA");
  582. chip = card->private_data;
  583. spin_lock_init(&chip->reg_lock);
  584. chip->irq = -1;
  585. card->private_free = snd_opl3sa2_free;
  586. *cardp = card;
  587. return 0;
  588. }
  589. static int snd_opl3sa2_probe(struct snd_card *card, int dev)
  590. {
  591. int xirq, xdma1, xdma2;
  592. struct snd_opl3sa2 *chip;
  593. struct snd_wss *wss;
  594. struct snd_opl3 *opl3;
  595. int err;
  596. /* initialise this card from supplied (or default) parameter*/
  597. chip = card->private_data;
  598. chip->ymode = opl3sa3_ymode[dev] & 0x03 ;
  599. chip->port = port[dev];
  600. xirq = irq[dev];
  601. xdma1 = dma1[dev];
  602. xdma2 = dma2[dev];
  603. if (xdma2 < 0)
  604. chip->single_dma = 1;
  605. err = snd_opl3sa2_detect(card);
  606. if (err < 0)
  607. return err;
  608. err = request_irq(xirq, snd_opl3sa2_interrupt, 0,
  609. "OPL3-SA2", card);
  610. if (err) {
  611. snd_printk(KERN_ERR PFX "can't grab IRQ %d\n", xirq);
  612. return -ENODEV;
  613. }
  614. chip->irq = xirq;
  615. err = snd_wss_create(card,
  616. wss_port[dev] + 4, -1,
  617. xirq, xdma1, xdma2,
  618. WSS_HW_OPL3SA2, WSS_HWSHARE_IRQ, &wss);
  619. if (err < 0) {
  620. snd_printd("Oops, WSS not detected at 0x%lx\n", wss_port[dev] + 4);
  621. return err;
  622. }
  623. chip->wss = wss;
  624. err = snd_wss_pcm(wss, 0);
  625. if (err < 0)
  626. return err;
  627. err = snd_wss_mixer(wss);
  628. if (err < 0)
  629. return err;
  630. err = snd_opl3sa2_mixer(card);
  631. if (err < 0)
  632. return err;
  633. err = snd_wss_timer(wss, 0);
  634. if (err < 0)
  635. return err;
  636. if (fm_port[dev] >= 0x340 && fm_port[dev] < 0x400) {
  637. if ((err = snd_opl3_create(card, fm_port[dev],
  638. fm_port[dev] + 2,
  639. OPL3_HW_OPL3, 0, &opl3)) < 0)
  640. return err;
  641. if ((err = snd_opl3_timer_new(opl3, 1, 2)) < 0)
  642. return err;
  643. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, &chip->synth)) < 0)
  644. return err;
  645. }
  646. if (midi_port[dev] >= 0x300 && midi_port[dev] < 0x340) {
  647. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_OPL3SA2,
  648. midi_port[dev],
  649. MPU401_INFO_IRQ_HOOK, -1,
  650. &chip->rmidi)) < 0)
  651. return err;
  652. }
  653. sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  654. card->shortname, chip->port, xirq, xdma1);
  655. if (xdma2 >= 0)
  656. sprintf(card->longname + strlen(card->longname), "&%d", xdma2);
  657. return snd_card_register(card);
  658. }
  659. #ifdef CONFIG_PNP
  660. static int snd_opl3sa2_pnp_detect(struct pnp_dev *pdev,
  661. const struct pnp_device_id *id)
  662. {
  663. static int dev;
  664. int err;
  665. struct snd_card *card;
  666. if (pnp_device_is_isapnp(pdev))
  667. return -ENOENT; /* we have another procedure - card */
  668. for (; dev < SNDRV_CARDS; dev++) {
  669. if (enable[dev] && isapnp[dev])
  670. break;
  671. }
  672. if (dev >= SNDRV_CARDS)
  673. return -ENODEV;
  674. err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
  675. if (err < 0)
  676. return err;
  677. if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
  678. snd_card_free(card);
  679. return err;
  680. }
  681. if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
  682. snd_card_free(card);
  683. return err;
  684. }
  685. pnp_set_drvdata(pdev, card);
  686. dev++;
  687. return 0;
  688. }
  689. static void snd_opl3sa2_pnp_remove(struct pnp_dev *pdev)
  690. {
  691. snd_card_free(pnp_get_drvdata(pdev));
  692. }
  693. #ifdef CONFIG_PM
  694. static int snd_opl3sa2_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
  695. {
  696. return snd_opl3sa2_suspend(pnp_get_drvdata(pdev), state);
  697. }
  698. static int snd_opl3sa2_pnp_resume(struct pnp_dev *pdev)
  699. {
  700. return snd_opl3sa2_resume(pnp_get_drvdata(pdev));
  701. }
  702. #endif
  703. static struct pnp_driver opl3sa2_pnp_driver = {
  704. .name = "snd-opl3sa2-pnpbios",
  705. .id_table = snd_opl3sa2_pnpbiosids,
  706. .probe = snd_opl3sa2_pnp_detect,
  707. .remove = snd_opl3sa2_pnp_remove,
  708. #ifdef CONFIG_PM
  709. .suspend = snd_opl3sa2_pnp_suspend,
  710. .resume = snd_opl3sa2_pnp_resume,
  711. #endif
  712. };
  713. static int snd_opl3sa2_pnp_cdetect(struct pnp_card_link *pcard,
  714. const struct pnp_card_device_id *id)
  715. {
  716. static int dev;
  717. struct pnp_dev *pdev;
  718. int err;
  719. struct snd_card *card;
  720. pdev = pnp_request_card_device(pcard, id->devs[0].id, NULL);
  721. if (pdev == NULL) {
  722. snd_printk(KERN_ERR PFX "can't get pnp device from id '%s'\n",
  723. id->devs[0].id);
  724. return -EBUSY;
  725. }
  726. for (; dev < SNDRV_CARDS; dev++) {
  727. if (enable[dev] && isapnp[dev])
  728. break;
  729. }
  730. if (dev >= SNDRV_CARDS)
  731. return -ENODEV;
  732. err = snd_opl3sa2_card_new(&pdev->dev, dev, &card);
  733. if (err < 0)
  734. return err;
  735. if ((err = snd_opl3sa2_pnp(dev, card->private_data, pdev)) < 0) {
  736. snd_card_free(card);
  737. return err;
  738. }
  739. if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
  740. snd_card_free(card);
  741. return err;
  742. }
  743. pnp_set_card_drvdata(pcard, card);
  744. dev++;
  745. return 0;
  746. }
  747. static void snd_opl3sa2_pnp_cremove(struct pnp_card_link *pcard)
  748. {
  749. snd_card_free(pnp_get_card_drvdata(pcard));
  750. pnp_set_card_drvdata(pcard, NULL);
  751. }
  752. #ifdef CONFIG_PM
  753. static int snd_opl3sa2_pnp_csuspend(struct pnp_card_link *pcard, pm_message_t state)
  754. {
  755. return snd_opl3sa2_suspend(pnp_get_card_drvdata(pcard), state);
  756. }
  757. static int snd_opl3sa2_pnp_cresume(struct pnp_card_link *pcard)
  758. {
  759. return snd_opl3sa2_resume(pnp_get_card_drvdata(pcard));
  760. }
  761. #endif
  762. static struct pnp_card_driver opl3sa2_pnpc_driver = {
  763. .flags = PNP_DRIVER_RES_DISABLE,
  764. .name = "snd-opl3sa2-cpnp",
  765. .id_table = snd_opl3sa2_pnpids,
  766. .probe = snd_opl3sa2_pnp_cdetect,
  767. .remove = snd_opl3sa2_pnp_cremove,
  768. #ifdef CONFIG_PM
  769. .suspend = snd_opl3sa2_pnp_csuspend,
  770. .resume = snd_opl3sa2_pnp_cresume,
  771. #endif
  772. };
  773. #endif /* CONFIG_PNP */
  774. static int snd_opl3sa2_isa_match(struct device *pdev,
  775. unsigned int dev)
  776. {
  777. if (!enable[dev])
  778. return 0;
  779. #ifdef CONFIG_PNP
  780. if (isapnp[dev])
  781. return 0;
  782. #endif
  783. if (port[dev] == SNDRV_AUTO_PORT) {
  784. snd_printk(KERN_ERR PFX "specify port\n");
  785. return 0;
  786. }
  787. if (wss_port[dev] == SNDRV_AUTO_PORT) {
  788. snd_printk(KERN_ERR PFX "specify wss_port\n");
  789. return 0;
  790. }
  791. if (fm_port[dev] == SNDRV_AUTO_PORT) {
  792. snd_printk(KERN_ERR PFX "specify fm_port\n");
  793. return 0;
  794. }
  795. if (midi_port[dev] == SNDRV_AUTO_PORT) {
  796. snd_printk(KERN_ERR PFX "specify midi_port\n");
  797. return 0;
  798. }
  799. return 1;
  800. }
  801. static int snd_opl3sa2_isa_probe(struct device *pdev,
  802. unsigned int dev)
  803. {
  804. struct snd_card *card;
  805. int err;
  806. err = snd_opl3sa2_card_new(pdev, dev, &card);
  807. if (err < 0)
  808. return err;
  809. if ((err = snd_opl3sa2_probe(card, dev)) < 0) {
  810. snd_card_free(card);
  811. return err;
  812. }
  813. dev_set_drvdata(pdev, card);
  814. return 0;
  815. }
  816. static int snd_opl3sa2_isa_remove(struct device *devptr,
  817. unsigned int dev)
  818. {
  819. snd_card_free(dev_get_drvdata(devptr));
  820. return 0;
  821. }
  822. #ifdef CONFIG_PM
  823. static int snd_opl3sa2_isa_suspend(struct device *dev, unsigned int n,
  824. pm_message_t state)
  825. {
  826. return snd_opl3sa2_suspend(dev_get_drvdata(dev), state);
  827. }
  828. static int snd_opl3sa2_isa_resume(struct device *dev, unsigned int n)
  829. {
  830. return snd_opl3sa2_resume(dev_get_drvdata(dev));
  831. }
  832. #endif
  833. #define DEV_NAME "opl3sa2"
  834. static struct isa_driver snd_opl3sa2_isa_driver = {
  835. .match = snd_opl3sa2_isa_match,
  836. .probe = snd_opl3sa2_isa_probe,
  837. .remove = snd_opl3sa2_isa_remove,
  838. #ifdef CONFIG_PM
  839. .suspend = snd_opl3sa2_isa_suspend,
  840. .resume = snd_opl3sa2_isa_resume,
  841. #endif
  842. .driver = {
  843. .name = DEV_NAME
  844. },
  845. };
  846. static int __init alsa_card_opl3sa2_init(void)
  847. {
  848. int err;
  849. err = isa_register_driver(&snd_opl3sa2_isa_driver, SNDRV_CARDS);
  850. #ifdef CONFIG_PNP
  851. if (!err)
  852. isa_registered = 1;
  853. err = pnp_register_driver(&opl3sa2_pnp_driver);
  854. if (!err)
  855. pnp_registered = 1;
  856. err = pnp_register_card_driver(&opl3sa2_pnpc_driver);
  857. if (!err)
  858. pnpc_registered = 1;
  859. if (isa_registered || pnp_registered)
  860. err = 0;
  861. #endif
  862. return err;
  863. }
  864. static void __exit alsa_card_opl3sa2_exit(void)
  865. {
  866. #ifdef CONFIG_PNP
  867. if (pnpc_registered)
  868. pnp_unregister_card_driver(&opl3sa2_pnpc_driver);
  869. if (pnp_registered)
  870. pnp_unregister_driver(&opl3sa2_pnp_driver);
  871. if (isa_registered)
  872. #endif
  873. isa_unregister_driver(&snd_opl3sa2_isa_driver);
  874. }
  875. module_init(alsa_card_opl3sa2_init)
  876. module_exit(alsa_card_opl3sa2_exit)