opl3sa2.c 30 KB

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