opl3sa2.c 29 KB

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