opl3sa2.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. * sound/opl3sa2.c
  3. *
  4. * A low level driver for Yamaha OPL3-SA2 and SA3 cards.
  5. * NOTE: All traces of the name OPL3-SAx have now (December 2000) been
  6. * removed from the driver code, as an email exchange with Yamaha
  7. * provided the information that the YMF-719 is indeed just a
  8. * re-badged 715.
  9. *
  10. * Copyright 1998-2001 Scott Murray <scott@spiteful.org>
  11. *
  12. * Originally based on the CS4232 driver (in cs4232.c) by Hannu Savolainen
  13. * and others. Now incorporates code/ideas from pss.c, also by Hannu
  14. * Savolainen. Both of those files are distributed with the following
  15. * license:
  16. *
  17. * "Copyright (C) by Hannu Savolainen 1993-1997
  18. *
  19. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  20. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  21. * for more info."
  22. *
  23. * As such, in accordance with the above license, this file, opl3sa2.c, is
  24. * distributed under the GNU GENERAL PUBLIC LICENSE (GPL) Version 2 (June 1991).
  25. * See the "COPYING" file distributed with this software for more information.
  26. *
  27. * Change History
  28. * --------------
  29. * Scott Murray Original driver (Jun 14, 1998)
  30. * Paul J.Y. Lahaie Changed probing / attach code order
  31. * Scott Murray Added mixer support (Dec 03, 1998)
  32. * Scott Murray Changed detection code to be more forgiving,
  33. * added force option as last resort,
  34. * fixed ioctl return values. (Dec 30, 1998)
  35. * Scott Murray Simpler detection code should work all the time now
  36. * (with thanks to Ben Hutchings for the heuristic),
  37. * removed now unnecessary force option. (Jan 5, 1999)
  38. * Christoph Hellwig Adapted to module_init/module_exit (Mar 4, 2000)
  39. * Scott Murray Reworked SA2 versus SA3 mixer code, updated chipset
  40. * version detection code (again!). (Dec 5, 2000)
  41. * Scott Murray Adjusted master volume mixer scaling. (Dec 6, 2000)
  42. * Scott Murray Based on a patch by Joel Yliluoma (aka Bisqwit),
  43. * integrated wide mixer and adjusted mic, bass, treble
  44. * scaling. (Dec 6, 2000)
  45. * Scott Murray Based on a patch by Peter Englmaier, integrated
  46. * ymode and loopback options. (Dec 6, 2000)
  47. * Scott Murray Inspired by a patch by Peter Englmaier, and based on
  48. * what ALSA does, added initialization code for the
  49. * default DMA and IRQ settings. (Dec 6, 2000)
  50. * Scott Murray Added some more checks to the card detection code,
  51. * based on what ALSA does. (Dec 12, 2000)
  52. * Scott Murray Inspired by similar patches from John Fremlin,
  53. * Jim Radford, Mike Rolig, and Ingmar Steen, added 2.4
  54. * ISA PnP API support, mainly based on bits from
  55. * sb_card.c and awe_wave.c. (Dec 12, 2000)
  56. * Scott Murray Some small cleanups to the init code output.
  57. * (Jan 7, 2001)
  58. * Zwane Mwaikambo Added PM support. (Dec 4 2001)
  59. *
  60. * Adam Belay Converted driver to new PnP Layer (Oct 12, 2002)
  61. * Zwane Mwaikambo Code, data structure cleanups. (Feb 15 2002)
  62. * Zwane Mwaikambo Free resources during auxiliary device probe
  63. * failures (Apr 29 2002)
  64. *
  65. */
  66. #include <linux/config.h>
  67. #include <linux/pnp.h>
  68. #include <linux/init.h>
  69. #include <linux/module.h>
  70. #include <linux/delay.h>
  71. #include <linux/pm.h>
  72. #include "sound_config.h"
  73. #include "ad1848.h"
  74. #include "mpu401.h"
  75. #define OPL3SA2_MODULE_NAME "opl3sa2"
  76. #define PFX OPL3SA2_MODULE_NAME ": "
  77. /* Useful control port indexes: */
  78. #define OPL3SA2_PM 0x01
  79. #define OPL3SA2_SYS_CTRL 0x02
  80. #define OPL3SA2_IRQ_CONFIG 0x03
  81. #define OPL3SA2_DMA_CONFIG 0x06
  82. #define OPL3SA2_MASTER_LEFT 0x07
  83. #define OPL3SA2_MASTER_RIGHT 0x08
  84. #define OPL3SA2_MIC 0x09
  85. #define OPL3SA2_MISC 0x0A
  86. #define OPL3SA3_WIDE 0x14
  87. #define OPL3SA3_BASS 0x15
  88. #define OPL3SA3_TREBLE 0x16
  89. /* Useful constants: */
  90. #define DEFAULT_VOLUME 50
  91. #define DEFAULT_MIC 50
  92. #define DEFAULT_TIMBRE 0
  93. /* Power saving modes */
  94. #define OPL3SA2_PM_MODE0 0x00
  95. #define OPL3SA2_PM_MODE1 0x04 /* PSV */
  96. #define OPL3SA2_PM_MODE2 0x05 /* PSV | PDX */
  97. #define OPL3SA2_PM_MODE3 0x27 /* ADOWN | PSV | PDN | PDX */
  98. /* For checking against what the card returns: */
  99. #define VERSION_UNKNOWN 0
  100. #define VERSION_YMF711 1
  101. #define VERSION_YMF715 2
  102. #define VERSION_YMF715B 3
  103. #define VERSION_YMF715E 4
  104. /* also assuming that anything > 4 but <= 7 is a 715E */
  105. /* Chipset type constants for use below */
  106. #define CHIPSET_UNKNOWN -1
  107. #define CHIPSET_OPL3SA2 0
  108. #define CHIPSET_OPL3SA3 1
  109. static const char *CHIPSET_TABLE[] = {"OPL3-SA2", "OPL3-SA3"};
  110. #ifdef CONFIG_PNP
  111. #define OPL3SA2_CARDS_MAX 4
  112. #else
  113. #define OPL3SA2_CARDS_MAX 1
  114. #endif
  115. /* This should be pretty obvious */
  116. static int opl3sa2_cards_num;
  117. typedef struct {
  118. /* device resources */
  119. unsigned short cfg_port;
  120. struct address_info cfg;
  121. struct address_info cfg_mss;
  122. struct address_info cfg_mpu;
  123. #ifdef CONFIG_PNP
  124. /* PnP Stuff */
  125. struct pnp_dev* pdev;
  126. int activated; /* Whether said devices have been activated */
  127. #endif
  128. #ifdef CONFIG_PM
  129. unsigned int in_suspend;
  130. struct pm_dev *pmdev;
  131. #endif
  132. unsigned int card;
  133. int chipset; /* What's my version(s)? */
  134. char *chipset_name;
  135. /* mixer data */
  136. int mixer;
  137. unsigned int volume_l;
  138. unsigned int volume_r;
  139. unsigned int mic;
  140. unsigned int bass_l;
  141. unsigned int bass_r;
  142. unsigned int treble_l;
  143. unsigned int treble_r;
  144. unsigned int wide_l;
  145. unsigned int wide_r;
  146. } opl3sa2_state_t;
  147. static opl3sa2_state_t opl3sa2_state[OPL3SA2_CARDS_MAX];
  148. /* Our parameters */
  149. static int __initdata io = -1;
  150. static int __initdata mss_io = -1;
  151. static int __initdata mpu_io = -1;
  152. static int __initdata irq = -1;
  153. static int __initdata dma = -1;
  154. static int __initdata dma2 = -1;
  155. static int __initdata ymode = -1;
  156. static int __initdata loopback = -1;
  157. #ifdef CONFIG_PNP
  158. /* PnP specific parameters */
  159. static int __initdata isapnp = 1;
  160. static int __initdata multiple = 1;
  161. /* Whether said devices have been activated */
  162. static int opl3sa2_activated[OPL3SA2_CARDS_MAX];
  163. #else
  164. static int __initdata isapnp; /* = 0 */
  165. static int __initdata multiple; /* = 0 */
  166. #endif
  167. MODULE_DESCRIPTION("Module for OPL3-SA2 and SA3 sound cards (uses AD1848 MSS driver).");
  168. MODULE_AUTHOR("Scott Murray <scott@spiteful.org>");
  169. MODULE_LICENSE("GPL");
  170. module_param(io, int, 0);
  171. MODULE_PARM_DESC(io, "Set I/O base of OPL3-SA2 or SA3 card (usually 0x370. Address must be even and must be from 0x100 to 0xFFE)");
  172. module_param(mss_io, int, 0);
  173. MODULE_PARM_DESC(mss_io, "Set MSS (audio) I/O base (0x530, 0xE80, or other. Address must end in 0 or 4 and must be from 0x530 to 0xF48)");
  174. module_param(mpu_io, int, 0);
  175. MODULE_PARM_DESC(mpu_io, "Set MIDI I/O base (0x330 or other. Address must be even and must be from 0x300 to 0x334)");
  176. module_param(irq, int, 0);
  177. MODULE_PARM_DESC(irq, "Set MSS (audio) IRQ (5, 7, 9, 10, 11, 12)");
  178. module_param(dma, int, 0);
  179. MODULE_PARM_DESC(dma, "Set MSS (audio) first DMA channel (0, 1, 3)");
  180. module_param(dma2, int, 0);
  181. MODULE_PARM_DESC(dma2, "Set MSS (audio) second DMA channel (0, 1, 3)");
  182. module_param(ymode, int, 0);
  183. MODULE_PARM_DESC(ymode, "Set Yamaha 3D enhancement mode (0 = Desktop/Normal, 1 = Notebook PC (1), 2 = Notebook PC (2), 3 = Hi-Fi)");
  184. module_param(loopback, int, 0);
  185. MODULE_PARM_DESC(loopback, "Set A/D input source. Useful for echo cancellation (0 = Mic Rch (default), 1 = Mono output loopback)");
  186. #ifdef CONFIG_PNP
  187. module_param(isapnp, bool, 0);
  188. MODULE_PARM_DESC(isapnp, "When set to 0, ISA PnP support will be disabled");
  189. module_param(multiple, bool, 0);
  190. MODULE_PARM_DESC(multiple, "When set to 0, will not search for multiple cards");
  191. #endif
  192. /*
  193. * Standard read and write functions
  194. */
  195. static inline void opl3sa2_write(unsigned short port,
  196. unsigned char index,
  197. unsigned char data)
  198. {
  199. outb_p(index, port);
  200. outb(data, port + 1);
  201. }
  202. static inline void opl3sa2_read(unsigned short port,
  203. unsigned char index,
  204. unsigned char* data)
  205. {
  206. outb_p(index, port);
  207. *data = inb(port + 1);
  208. }
  209. /*
  210. * All of the mixer functions...
  211. */
  212. static void opl3sa2_set_volume(opl3sa2_state_t* devc, int left, int right)
  213. {
  214. static unsigned char scale[101] = {
  215. 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0e, 0x0e, 0x0e,
  216. 0x0e, 0x0e, 0x0e, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0d, 0x0c,
  217. 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0b, 0x0b, 0x0b, 0x0b,
  218. 0x0b, 0x0b, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x09, 0x09,
  219. 0x09, 0x09, 0x09, 0x09, 0x09, 0x08, 0x08, 0x08, 0x08, 0x08,
  220. 0x08, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x06, 0x06, 0x06,
  221. 0x06, 0x06, 0x06, 0x06, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  222. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x03, 0x03, 0x03, 0x03,
  223. 0x03, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01,
  224. 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  225. 0x00
  226. };
  227. unsigned char vol;
  228. vol = scale[left];
  229. /* If level is zero, turn on mute */
  230. if(!left)
  231. vol |= 0x80;
  232. opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_LEFT, vol);
  233. vol = scale[right];
  234. /* If level is zero, turn on mute */
  235. if(!right)
  236. vol |= 0x80;
  237. opl3sa2_write(devc->cfg_port, OPL3SA2_MASTER_RIGHT, vol);
  238. }
  239. static void opl3sa2_set_mic(opl3sa2_state_t* devc, int level)
  240. {
  241. unsigned char vol = 0x1F;
  242. if((level >= 0) && (level <= 100))
  243. vol = 0x1F - (unsigned char) (32 * level / 101);
  244. /* If level is zero, turn on mute */
  245. if(!level)
  246. vol |= 0x80;
  247. opl3sa2_write(devc->cfg_port, OPL3SA2_MIC, vol);
  248. }
  249. static void opl3sa3_set_bass(opl3sa2_state_t* devc, int left, int right)
  250. {
  251. unsigned char bass;
  252. bass = left ? ((unsigned char) (8 * left / 101)) : 0;
  253. bass |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
  254. opl3sa2_write(devc->cfg_port, OPL3SA3_BASS, bass);
  255. }
  256. static void opl3sa3_set_treble(opl3sa2_state_t* devc, int left, int right)
  257. {
  258. unsigned char treble;
  259. treble = left ? ((unsigned char) (8 * left / 101)) : 0;
  260. treble |= (right ? ((unsigned char) (8 * right / 101)) : 0) << 4;
  261. opl3sa2_write(devc->cfg_port, OPL3SA3_TREBLE, treble);
  262. }
  263. static void opl3sa2_mixer_reset(opl3sa2_state_t* devc)
  264. {
  265. if (devc) {
  266. opl3sa2_set_volume(devc, DEFAULT_VOLUME, DEFAULT_VOLUME);
  267. devc->volume_l = devc->volume_r = DEFAULT_VOLUME;
  268. opl3sa2_set_mic(devc, DEFAULT_MIC);
  269. devc->mic = DEFAULT_MIC;
  270. if (devc->chipset == CHIPSET_OPL3SA3) {
  271. opl3sa3_set_bass(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
  272. devc->bass_l = devc->bass_r = DEFAULT_TIMBRE;
  273. opl3sa3_set_treble(devc, DEFAULT_TIMBRE, DEFAULT_TIMBRE);
  274. devc->treble_l = devc->treble_r = DEFAULT_TIMBRE;
  275. }
  276. }
  277. }
  278. /* Currently only used for power management */
  279. #ifdef CONFIG_PM
  280. static void opl3sa2_mixer_restore(opl3sa2_state_t* devc)
  281. {
  282. if (devc) {
  283. opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
  284. opl3sa2_set_mic(devc, devc->mic);
  285. if (devc->chipset == CHIPSET_OPL3SA3) {
  286. opl3sa3_set_bass(devc, devc->bass_l, devc->bass_r);
  287. opl3sa3_set_treble(devc, devc->treble_l, devc->treble_r);
  288. }
  289. }
  290. }
  291. #endif
  292. static inline void arg_to_vol_mono(unsigned int vol, int* value)
  293. {
  294. int left;
  295. left = vol & 0x00ff;
  296. if (left > 100)
  297. left = 100;
  298. *value = left;
  299. }
  300. static inline void arg_to_vol_stereo(unsigned int vol, int* aleft, int* aright)
  301. {
  302. arg_to_vol_mono(vol, aleft);
  303. arg_to_vol_mono(vol >> 8, aright);
  304. }
  305. static inline int ret_vol_mono(int vol)
  306. {
  307. return ((vol << 8) | vol);
  308. }
  309. static inline int ret_vol_stereo(int left, int right)
  310. {
  311. return ((right << 8) | left);
  312. }
  313. static int opl3sa2_mixer_ioctl(int dev, unsigned int cmd, void __user *arg)
  314. {
  315. int retval, value, cmdf = cmd & 0xff;
  316. int __user *p = (int __user *)arg;
  317. opl3sa2_state_t* devc = &opl3sa2_state[dev];
  318. switch (cmdf) {
  319. case SOUND_MIXER_VOLUME:
  320. case SOUND_MIXER_MIC:
  321. case SOUND_MIXER_DEVMASK:
  322. case SOUND_MIXER_STEREODEVS:
  323. case SOUND_MIXER_RECMASK:
  324. case SOUND_MIXER_RECSRC:
  325. case SOUND_MIXER_CAPS:
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. if (((cmd >> 8) & 0xff) != 'M')
  331. return -EINVAL;
  332. retval = 0;
  333. if (_SIOC_DIR (cmd) & _SIOC_WRITE) {
  334. switch (cmdf) {
  335. case SOUND_MIXER_VOLUME:
  336. retval = get_user(value, (unsigned __user *) arg);
  337. if (retval)
  338. break;
  339. arg_to_vol_stereo(value, &devc->volume_l, &devc->volume_r);
  340. opl3sa2_set_volume(devc, devc->volume_l, devc->volume_r);
  341. value = ret_vol_stereo(devc->volume_l, devc->volume_r);
  342. retval = put_user(value, p);
  343. break;
  344. case SOUND_MIXER_MIC:
  345. retval = get_user(value, (unsigned __user *) arg);
  346. if (retval)
  347. break;
  348. arg_to_vol_mono(value, &devc->mic);
  349. opl3sa2_set_mic(devc, devc->mic);
  350. value = ret_vol_mono(devc->mic);
  351. retval = put_user(value, p);
  352. break;
  353. default:
  354. retval = -EINVAL;
  355. }
  356. }
  357. else {
  358. /*
  359. * Return parameters
  360. */
  361. switch (cmdf) {
  362. case SOUND_MIXER_DEVMASK:
  363. retval = put_user(SOUND_MASK_VOLUME | SOUND_MASK_MIC, p);
  364. break;
  365. case SOUND_MIXER_STEREODEVS:
  366. retval = put_user(SOUND_MASK_VOLUME, p);
  367. break;
  368. case SOUND_MIXER_RECMASK:
  369. /* No recording devices */
  370. retval = put_user(0, p);
  371. break;
  372. case SOUND_MIXER_CAPS:
  373. retval = put_user(SOUND_CAP_EXCL_INPUT, p);
  374. break;
  375. case SOUND_MIXER_RECSRC:
  376. /* No recording source */
  377. retval = put_user(0, p);
  378. break;
  379. case SOUND_MIXER_VOLUME:
  380. value = ret_vol_stereo(devc->volume_l, devc->volume_r);
  381. retval = put_user(value, p);
  382. break;
  383. case SOUND_MIXER_MIC:
  384. value = ret_vol_mono(devc->mic);
  385. put_user(value, p);
  386. break;
  387. default:
  388. retval = -EINVAL;
  389. }
  390. }
  391. return retval;
  392. }
  393. /* opl3sa2_mixer_ioctl end */
  394. static int opl3sa3_mixer_ioctl(int dev, unsigned int cmd, void __user * arg)
  395. {
  396. int value, retval, cmdf = cmd & 0xff;
  397. opl3sa2_state_t* devc = &opl3sa2_state[dev];
  398. switch (cmdf) {
  399. case SOUND_MIXER_BASS:
  400. value = ret_vol_stereo(devc->bass_l, devc->bass_r);
  401. retval = put_user(value, (int __user *) arg);
  402. break;
  403. case SOUND_MIXER_TREBLE:
  404. value = ret_vol_stereo(devc->treble_l, devc->treble_r);
  405. retval = put_user(value, (int __user *) arg);
  406. break;
  407. case SOUND_MIXER_DIGITAL1:
  408. value = ret_vol_stereo(devc->wide_l, devc->wide_r);
  409. retval = put_user(value, (int __user *) arg);
  410. break;
  411. default:
  412. retval = -EINVAL;
  413. }
  414. return retval;
  415. }
  416. /* opl3sa3_mixer_ioctl end */
  417. static struct mixer_operations opl3sa2_mixer_operations =
  418. {
  419. .owner = THIS_MODULE,
  420. .id = "OPL3-SA2",
  421. .name = "Yamaha OPL3-SA2",
  422. .ioctl = opl3sa2_mixer_ioctl
  423. };
  424. static struct mixer_operations opl3sa3_mixer_operations =
  425. {
  426. .owner = THIS_MODULE,
  427. .id = "OPL3-SA3",
  428. .name = "Yamaha OPL3-SA3",
  429. .ioctl = opl3sa3_mixer_ioctl
  430. };
  431. /* End of mixer-related stuff */
  432. /*
  433. * Component probe, attach, unload functions
  434. */
  435. static inline void __exit unload_opl3sa2_mpu(struct address_info *hw_config)
  436. {
  437. unload_mpu401(hw_config);
  438. }
  439. static void __init attach_opl3sa2_mss(struct address_info* hw_config, struct resource *ports)
  440. {
  441. int initial_mixers;
  442. initial_mixers = num_mixers;
  443. attach_ms_sound(hw_config, ports, THIS_MODULE); /* Slot 0 */
  444. if (hw_config->slots[0] != -1) {
  445. /* Did the MSS driver install? */
  446. if(num_mixers == (initial_mixers + 1)) {
  447. /* The MSS mixer is installed, reroute mixers appropiately */
  448. AD1848_REROUTE(SOUND_MIXER_LINE1, SOUND_MIXER_CD);
  449. AD1848_REROUTE(SOUND_MIXER_LINE2, SOUND_MIXER_SYNTH);
  450. AD1848_REROUTE(SOUND_MIXER_LINE3, SOUND_MIXER_LINE);
  451. }
  452. else {
  453. printk(KERN_ERR PFX "MSS mixer not installed?\n");
  454. }
  455. }
  456. }
  457. static inline void __exit unload_opl3sa2_mss(struct address_info* hw_config)
  458. {
  459. unload_ms_sound(hw_config);
  460. }
  461. static int __init probe_opl3sa2(struct address_info* hw_config, int card)
  462. {
  463. unsigned char misc;
  464. unsigned char tmp;
  465. unsigned char version;
  466. /*
  467. * Try and allocate our I/O port range.
  468. */
  469. if (!request_region(hw_config->io_base, 2, OPL3SA2_MODULE_NAME)) {
  470. printk(KERN_ERR PFX "Control I/O port %#x not free\n",
  471. hw_config->io_base);
  472. goto out_nodev;
  473. }
  474. /*
  475. * Check if writing to the read-only version bits of the miscellaneous
  476. * register succeeds or not (it should not).
  477. */
  478. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
  479. opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc ^ 0x07);
  480. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &tmp);
  481. if(tmp != misc) {
  482. printk(KERN_ERR PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
  483. hw_config->io_base);
  484. goto out_region;
  485. }
  486. /*
  487. * Check if the MIC register is accessible.
  488. */
  489. opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
  490. opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, 0x8a);
  491. opl3sa2_read(hw_config->io_base, OPL3SA2_MIC, &tmp);
  492. if((tmp & 0x9f) != 0x8a) {
  493. printk(KERN_ERR
  494. PFX "Control I/O port %#x is not a YMF7xx chipset!\n",
  495. hw_config->io_base);
  496. goto out_region;
  497. }
  498. opl3sa2_write(hw_config->io_base, OPL3SA2_MIC, tmp);
  499. /*
  500. * Determine chipset type (SA2 or SA3)
  501. *
  502. * This is done by looking at the chipset version in the lower 3 bits
  503. * of the miscellaneous register.
  504. */
  505. version = misc & 0x07;
  506. printk(KERN_DEBUG PFX "Chipset version = %#x\n", version);
  507. switch (version) {
  508. case 0:
  509. opl3sa2_state[card].chipset = CHIPSET_UNKNOWN;
  510. printk(KERN_ERR
  511. PFX "Unknown Yamaha audio controller version\n");
  512. break;
  513. case VERSION_YMF711:
  514. opl3sa2_state[card].chipset = CHIPSET_OPL3SA2;
  515. printk(KERN_INFO PFX "Found OPL3-SA2 (YMF711)\n");
  516. break;
  517. case VERSION_YMF715:
  518. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  519. printk(KERN_INFO
  520. PFX "Found OPL3-SA3 (YMF715 or YMF719)\n");
  521. break;
  522. case VERSION_YMF715B:
  523. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  524. printk(KERN_INFO
  525. PFX "Found OPL3-SA3 (YMF715B or YMF719B)\n");
  526. break;
  527. case VERSION_YMF715E:
  528. default:
  529. opl3sa2_state[card].chipset = CHIPSET_OPL3SA3;
  530. printk(KERN_INFO
  531. PFX "Found OPL3-SA3 (YMF715E or YMF719E)\n");
  532. break;
  533. }
  534. if (opl3sa2_state[card].chipset != CHIPSET_UNKNOWN) {
  535. /* Generate a pretty name */
  536. opl3sa2_state[card].chipset_name = (char *)CHIPSET_TABLE[opl3sa2_state[card].chipset];
  537. return 0;
  538. }
  539. out_region:
  540. release_region(hw_config->io_base, 2);
  541. out_nodev:
  542. return -ENODEV;
  543. }
  544. static void __init attach_opl3sa2(struct address_info* hw_config, int card)
  545. {
  546. /* Initialize IRQ configuration to IRQ-B: -, IRQ-A: WSS+MPU+OPL3 */
  547. opl3sa2_write(hw_config->io_base, OPL3SA2_IRQ_CONFIG, 0x0d);
  548. /* Initialize DMA configuration */
  549. if(hw_config->dma2 == hw_config->dma) {
  550. /* Want DMA configuration DMA-B: -, DMA-A: WSS-P+WSS-R */
  551. opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x03);
  552. }
  553. else {
  554. /* Want DMA configuration DMA-B: WSS-R, DMA-A: WSS-P */
  555. opl3sa2_write(hw_config->io_base, OPL3SA2_DMA_CONFIG, 0x21);
  556. }
  557. }
  558. static void __init attach_opl3sa2_mixer(struct address_info *hw_config, int card)
  559. {
  560. struct mixer_operations* mixer_operations;
  561. opl3sa2_state_t* devc = &opl3sa2_state[card];
  562. /* Install master mixer */
  563. if (devc->chipset == CHIPSET_OPL3SA3) {
  564. mixer_operations = &opl3sa3_mixer_operations;
  565. }
  566. else {
  567. mixer_operations = &opl3sa2_mixer_operations;
  568. }
  569. devc->cfg_port = hw_config->io_base;
  570. devc->mixer = sound_install_mixer(MIXER_DRIVER_VERSION,
  571. mixer_operations->name,
  572. mixer_operations,
  573. sizeof(struct mixer_operations),
  574. devc);
  575. if(devc->mixer < 0) {
  576. printk(KERN_ERR PFX "Could not install %s master mixer\n",
  577. mixer_operations->name);
  578. }
  579. else {
  580. opl3sa2_mixer_reset(devc);
  581. }
  582. }
  583. static void opl3sa2_clear_slots(struct address_info* hw_config)
  584. {
  585. int i;
  586. for(i = 0; i < 6; i++) {
  587. hw_config->slots[i] = -1;
  588. }
  589. }
  590. static void __init opl3sa2_set_ymode(struct address_info* hw_config, int ymode)
  591. {
  592. /*
  593. * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
  594. * it's supported.
  595. *
  596. * 0: Desktop (aka normal) 5-12 cm speakers
  597. * 1: Notebook PC mode 1 3 cm speakers
  598. * 2: Notebook PC mode 2 1.5 cm speakers
  599. * 3: Hi-fi 16-38 cm speakers
  600. */
  601. if(ymode >= 0 && ymode <= 3) {
  602. unsigned char sys_ctrl;
  603. opl3sa2_read(hw_config->io_base, OPL3SA2_SYS_CTRL, &sys_ctrl);
  604. sys_ctrl = (sys_ctrl & 0xcf) | ((ymode & 3) << 4);
  605. opl3sa2_write(hw_config->io_base, OPL3SA2_SYS_CTRL, sys_ctrl);
  606. }
  607. else {
  608. printk(KERN_ERR PFX "not setting ymode, it must be one of 0,1,2,3\n");
  609. }
  610. }
  611. static void __init opl3sa2_set_loopback(struct address_info* hw_config, int loopback)
  612. {
  613. if(loopback >= 0 && loopback <= 1) {
  614. unsigned char misc;
  615. opl3sa2_read(hw_config->io_base, OPL3SA2_MISC, &misc);
  616. misc = (misc & 0xef) | ((loopback & 1) << 4);
  617. opl3sa2_write(hw_config->io_base, OPL3SA2_MISC, misc);
  618. }
  619. else {
  620. printk(KERN_ERR PFX "not setting loopback, it must be either 0 or 1\n");
  621. }
  622. }
  623. static void __exit unload_opl3sa2(struct address_info* hw_config, int card)
  624. {
  625. /* Release control ports */
  626. release_region(hw_config->io_base, 2);
  627. /* Unload mixer */
  628. if(opl3sa2_state[card].mixer >= 0)
  629. sound_unload_mixerdev(opl3sa2_state[card].mixer);
  630. }
  631. #ifdef CONFIG_PNP
  632. static struct pnp_device_id pnp_opl3sa2_list[] = {
  633. {.id = "YMH0021", .driver_data = 0},
  634. {.id = ""}
  635. };
  636. MODULE_DEVICE_TABLE(pnp, pnp_opl3sa2_list);
  637. static int opl3sa2_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *dev_id)
  638. {
  639. int card = opl3sa2_cards_num;
  640. /* we don't actually want to return an error as the user may have specified
  641. * no multiple card search
  642. */
  643. if (opl3sa2_cards_num == OPL3SA2_CARDS_MAX)
  644. return 0;
  645. opl3sa2_activated[card] = 1;
  646. /* Our own config: */
  647. opl3sa2_state[card].cfg.io_base = pnp_port_start(dev, 4);
  648. opl3sa2_state[card].cfg.irq = pnp_irq(dev, 0);
  649. opl3sa2_state[card].cfg.dma = pnp_dma(dev, 0);
  650. opl3sa2_state[card].cfg.dma2 = pnp_dma(dev, 1);
  651. /* The MSS config: */
  652. opl3sa2_state[card].cfg_mss.io_base = pnp_port_start(dev, 1);
  653. opl3sa2_state[card].cfg_mss.irq = pnp_irq(dev, 0);
  654. opl3sa2_state[card].cfg_mss.dma = pnp_dma(dev, 0);
  655. opl3sa2_state[card].cfg_mss.dma2 = pnp_dma(dev, 1);
  656. opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
  657. opl3sa2_state[card].cfg_mpu.io_base = pnp_port_start(dev, 3);
  658. opl3sa2_state[card].cfg_mpu.irq = pnp_irq(dev, 0);
  659. opl3sa2_state[card].cfg_mpu.dma = -1;
  660. opl3sa2_state[card].cfg_mpu.dma2 = -1;
  661. opl3sa2_state[card].cfg_mpu.always_detect = 1; /* It's there, so use shared IRQs */
  662. /* Call me paranoid: */
  663. opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
  664. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
  665. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
  666. opl3sa2_state[card].pdev = dev;
  667. opl3sa2_cards_num++;
  668. return 0;
  669. }
  670. static struct pnp_driver opl3sa2_driver = {
  671. .name = "opl3sa2",
  672. .id_table = pnp_opl3sa2_list,
  673. .probe = opl3sa2_pnp_probe,
  674. };
  675. #endif /* CONFIG_PNP */
  676. /* End of component functions */
  677. #ifdef CONFIG_PM
  678. static DEFINE_SPINLOCK(opl3sa2_lock);
  679. /* Power Management support functions */
  680. static int opl3sa2_suspend(struct pm_dev *pdev, unsigned int pm_mode)
  681. {
  682. unsigned long flags;
  683. opl3sa2_state_t *p;
  684. if (!pdev)
  685. return -EINVAL;
  686. spin_lock_irqsave(&opl3sa2_lock,flags);
  687. p = (opl3sa2_state_t *) pdev->data;
  688. switch (pm_mode) {
  689. case 1:
  690. pm_mode = OPL3SA2_PM_MODE1;
  691. break;
  692. case 2:
  693. pm_mode = OPL3SA2_PM_MODE2;
  694. break;
  695. case 3:
  696. pm_mode = OPL3SA2_PM_MODE3;
  697. break;
  698. default:
  699. /* we don't know howto handle this... */
  700. spin_unlock_irqrestore(&opl3sa2_lock, flags);
  701. return -EBUSY;
  702. }
  703. p->in_suspend = 1;
  704. /* its supposed to automute before suspending, so we won't bother */
  705. opl3sa2_write(p->cfg_port, OPL3SA2_PM, pm_mode);
  706. /* wait a while for the clock oscillator to stabilise */
  707. mdelay(10);
  708. spin_unlock_irqrestore(&opl3sa2_lock,flags);
  709. return 0;
  710. }
  711. static int opl3sa2_resume(struct pm_dev *pdev)
  712. {
  713. unsigned long flags;
  714. opl3sa2_state_t *p;
  715. if (!pdev)
  716. return -EINVAL;
  717. p = (opl3sa2_state_t *) pdev->data;
  718. spin_lock_irqsave(&opl3sa2_lock,flags);
  719. /* I don't think this is necessary */
  720. opl3sa2_write(p->cfg_port, OPL3SA2_PM, OPL3SA2_PM_MODE0);
  721. opl3sa2_mixer_restore(p);
  722. p->in_suspend = 0;
  723. spin_unlock_irqrestore(&opl3sa2_lock,flags);
  724. return 0;
  725. }
  726. static int opl3sa2_pm_callback(struct pm_dev *pdev, pm_request_t rqst, void *data)
  727. {
  728. unsigned long mode = (unsigned long)data;
  729. switch (rqst) {
  730. case PM_SUSPEND:
  731. return opl3sa2_suspend(pdev, mode);
  732. case PM_RESUME:
  733. return opl3sa2_resume(pdev);
  734. }
  735. return 0;
  736. }
  737. #endif /* CONFIG_PM */
  738. /*
  739. * Install OPL3-SA2 based card(s).
  740. *
  741. * Need to have ad1848 and mpu401 loaded ready.
  742. */
  743. static int __init init_opl3sa2(void)
  744. {
  745. int card, max;
  746. /* Sanitize isapnp and multiple settings */
  747. isapnp = isapnp != 0 ? 1 : 0;
  748. multiple = multiple != 0 ? 1 : 0;
  749. max = (multiple && isapnp) ? OPL3SA2_CARDS_MAX : 1;
  750. #ifdef CONFIG_PNP
  751. if (isapnp){
  752. pnp_register_driver(&opl3sa2_driver);
  753. if(!opl3sa2_cards_num){
  754. printk(KERN_INFO PFX "No PnP cards found\n");
  755. isapnp = 0;
  756. }
  757. max = opl3sa2_cards_num;
  758. }
  759. #endif
  760. for (card = 0; card < max; card++) {
  761. /* If a user wants an I/O then assume they meant it */
  762. struct resource *ports;
  763. int base;
  764. if (!isapnp) {
  765. if (io == -1 || irq == -1 || dma == -1 ||
  766. dma2 == -1 || mss_io == -1) {
  767. printk(KERN_ERR
  768. PFX "io, mss_io, irq, dma, and dma2 must be set\n");
  769. return -EINVAL;
  770. }
  771. opl3sa2_cards_num++;
  772. /*
  773. * Our own config:
  774. * (NOTE: IRQ and DMA aren't used, so they're set to
  775. * give pretty output from conf_printf. :)
  776. */
  777. opl3sa2_state[card].cfg.io_base = io;
  778. opl3sa2_state[card].cfg.irq = irq;
  779. opl3sa2_state[card].cfg.dma = dma;
  780. opl3sa2_state[card].cfg.dma2 = dma2;
  781. /* The MSS config: */
  782. opl3sa2_state[card].cfg_mss.io_base = mss_io;
  783. opl3sa2_state[card].cfg_mss.irq = irq;
  784. opl3sa2_state[card].cfg_mss.dma = dma;
  785. opl3sa2_state[card].cfg_mss.dma2 = dma2;
  786. opl3sa2_state[card].cfg_mss.card_subtype = 1; /* No IRQ or DMA setup */
  787. opl3sa2_state[card].cfg_mpu.io_base = mpu_io;
  788. opl3sa2_state[card].cfg_mpu.irq = irq;
  789. opl3sa2_state[card].cfg_mpu.dma = -1;
  790. opl3sa2_state[card].cfg_mpu.always_detect = 1; /* Use shared IRQs */
  791. /* Call me paranoid: */
  792. opl3sa2_clear_slots(&opl3sa2_state[card].cfg);
  793. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mss);
  794. opl3sa2_clear_slots(&opl3sa2_state[card].cfg_mpu);
  795. }
  796. /* FIXME: leak */
  797. if (probe_opl3sa2(&opl3sa2_state[card].cfg, card))
  798. return -ENODEV;
  799. base = opl3sa2_state[card].cfg_mss.io_base;
  800. if (!request_region(base, 4, "WSS config"))
  801. goto failed;
  802. ports = request_region(base + 4, 4, "ad1848");
  803. if (!ports)
  804. goto failed2;
  805. if (!probe_ms_sound(&opl3sa2_state[card].cfg_mss, ports)) {
  806. /*
  807. * If one or more cards are already registered, don't
  808. * return an error but print a warning. Note, this
  809. * should never really happen unless the hardware or
  810. * ISA PnP screwed up.
  811. */
  812. release_region(base + 4, 4);
  813. failed2:
  814. release_region(base, 4);
  815. failed:
  816. release_region(opl3sa2_state[card].cfg.io_base, 2);
  817. if (opl3sa2_cards_num) {
  818. printk(KERN_WARNING
  819. PFX "There was a problem probing one "
  820. " of the ISA PNP cards, continuing\n");
  821. opl3sa2_cards_num--;
  822. continue;
  823. } else
  824. return -ENODEV;
  825. }
  826. attach_opl3sa2(&opl3sa2_state[card].cfg, card);
  827. conf_printf(opl3sa2_state[card].chipset_name, &opl3sa2_state[card].cfg);
  828. attach_opl3sa2_mixer(&opl3sa2_state[card].cfg, card);
  829. attach_opl3sa2_mss(&opl3sa2_state[card].cfg_mss, ports);
  830. /* ewww =) */
  831. opl3sa2_state[card].card = card;
  832. #ifdef CONFIG_PM
  833. /* register our power management capabilities */
  834. opl3sa2_state[card].pmdev = pm_register(PM_ISA_DEV, card, opl3sa2_pm_callback);
  835. if (opl3sa2_state[card].pmdev)
  836. opl3sa2_state[card].pmdev->data = &opl3sa2_state[card];
  837. #endif /* CONFIG_PM */
  838. /*
  839. * Set the Yamaha 3D enhancement mode (aka Ymersion) if asked to and
  840. * it's supported.
  841. */
  842. if (ymode != -1) {
  843. if (opl3sa2_state[card].chipset == CHIPSET_OPL3SA2) {
  844. printk(KERN_ERR
  845. PFX "ymode not supported on OPL3-SA2\n");
  846. }
  847. else {
  848. opl3sa2_set_ymode(&opl3sa2_state[card].cfg, ymode);
  849. }
  850. }
  851. /* Set A/D input to Mono loopback if asked to. */
  852. if (loopback != -1) {
  853. opl3sa2_set_loopback(&opl3sa2_state[card].cfg, loopback);
  854. }
  855. /* Attach MPU if we've been asked to do so, failure isn't fatal */
  856. if (opl3sa2_state[card].cfg_mpu.io_base != -1) {
  857. int base = opl3sa2_state[card].cfg_mpu.io_base;
  858. struct resource *ports;
  859. ports = request_region(base, 2, "mpu401");
  860. if (!ports)
  861. goto out;
  862. if (!probe_mpu401(&opl3sa2_state[card].cfg_mpu, ports)) {
  863. release_region(base, 2);
  864. goto out;
  865. }
  866. if (attach_mpu401(&opl3sa2_state[card].cfg_mpu, THIS_MODULE)) {
  867. printk(KERN_ERR PFX "failed to attach MPU401\n");
  868. opl3sa2_state[card].cfg_mpu.slots[1] = -1;
  869. }
  870. }
  871. }
  872. out:
  873. if (isapnp) {
  874. printk(KERN_NOTICE PFX "%d PnP card(s) found.\n", opl3sa2_cards_num);
  875. }
  876. return 0;
  877. }
  878. /*
  879. * Uninstall OPL3-SA2 based card(s).
  880. */
  881. static void __exit cleanup_opl3sa2(void)
  882. {
  883. int card;
  884. for(card = 0; card < opl3sa2_cards_num; card++) {
  885. #ifdef CONFIG_PM
  886. if (opl3sa2_state[card].pmdev)
  887. pm_unregister(opl3sa2_state[card].pmdev);
  888. #endif
  889. if (opl3sa2_state[card].cfg_mpu.slots[1] != -1) {
  890. unload_opl3sa2_mpu(&opl3sa2_state[card].cfg_mpu);
  891. }
  892. unload_opl3sa2_mss(&opl3sa2_state[card].cfg_mss);
  893. unload_opl3sa2(&opl3sa2_state[card].cfg, card);
  894. #ifdef CONFIG_PNP
  895. pnp_unregister_driver(&opl3sa2_driver);
  896. #endif
  897. }
  898. }
  899. module_init(init_opl3sa2);
  900. module_exit(cleanup_opl3sa2);
  901. #ifndef MODULE
  902. static int __init setup_opl3sa2(char *str)
  903. {
  904. /* io, irq, dma, dma2,... */
  905. #ifdef CONFIG_PNP
  906. int ints[11];
  907. #else
  908. int ints[9];
  909. #endif
  910. str = get_options(str, ARRAY_SIZE(ints), ints);
  911. io = ints[1];
  912. irq = ints[2];
  913. dma = ints[3];
  914. dma2 = ints[4];
  915. mss_io = ints[5];
  916. mpu_io = ints[6];
  917. ymode = ints[7];
  918. loopback = ints[8];
  919. #ifdef CONFIG_PNP
  920. isapnp = ints[9];
  921. multiple = ints[10];
  922. #endif
  923. return 1;
  924. }
  925. __setup("opl3sa2=", setup_opl3sa2);
  926. #endif