opl3sa2.c 27 KB

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