opl3sa2.c 29 KB

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