opti92x-ad1848.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  1. /*
  2. card-opti92x-ad1848.c - driver for OPTi 82c92x based soundcards.
  3. Copyright (C) 1998-2000 by Massimo Piccioni <dafastidio@libero.it>
  4. Part of this code was developed at the Italian Ministry of Air Defence,
  5. Sixth Division (oh, che pace ...), Rome.
  6. Thanks to Maria Grazia Pollarini, Salvatore Vassallo.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/isa.h>
  22. #include <linux/delay.h>
  23. #include <linux/slab.h>
  24. #include <linux/pnp.h>
  25. #include <linux/moduleparam.h>
  26. #include <asm/io.h>
  27. #include <asm/dma.h>
  28. #include <sound/core.h>
  29. #include <sound/tlv.h>
  30. #include <sound/wss.h>
  31. #include <sound/mpu401.h>
  32. #include <sound/opl3.h>
  33. #ifndef OPTi93X
  34. #include <sound/opl4.h>
  35. #endif
  36. #define SNDRV_LEGACY_FIND_FREE_IRQ
  37. #define SNDRV_LEGACY_FIND_FREE_DMA
  38. #include <sound/initval.h>
  39. MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
  40. MODULE_LICENSE("GPL");
  41. #ifdef OPTi93X
  42. MODULE_DESCRIPTION("OPTi93X");
  43. MODULE_SUPPORTED_DEVICE("{{OPTi,82C931/3}}");
  44. #else /* OPTi93X */
  45. #ifdef CS4231
  46. MODULE_DESCRIPTION("OPTi92X - CS4231");
  47. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (CS4231)},"
  48. "{OPTi,82C925 (CS4231)}}");
  49. #else /* CS4231 */
  50. MODULE_DESCRIPTION("OPTi92X - AD1848");
  51. MODULE_SUPPORTED_DEVICE("{{OPTi,82C924 (AD1848)},"
  52. "{OPTi,82C925 (AD1848)},"
  53. "{OAK,Mozart}}");
  54. #endif /* CS4231 */
  55. #endif /* OPTi93X */
  56. static int index = SNDRV_DEFAULT_IDX1; /* Index 0-MAX */
  57. static char *id = SNDRV_DEFAULT_STR1; /* ID for this card */
  58. //static int enable = SNDRV_DEFAULT_ENABLE1; /* Enable this card */
  59. #ifdef CONFIG_PNP
  60. static int isapnp = 1; /* Enable ISA PnP detection */
  61. #endif
  62. static long port = SNDRV_DEFAULT_PORT1; /* 0x530,0xe80,0xf40,0x604 */
  63. static long mpu_port = SNDRV_DEFAULT_PORT1; /* 0x300,0x310,0x320,0x330 */
  64. static long fm_port = SNDRV_DEFAULT_PORT1; /* 0x388 */
  65. static int irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10,11 */
  66. static int mpu_irq = SNDRV_DEFAULT_IRQ1; /* 5,7,9,10 */
  67. static int dma1 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  68. #if defined(CS4231) || defined(OPTi93X)
  69. static int dma2 = SNDRV_DEFAULT_DMA1; /* 0,1,3 */
  70. #endif /* CS4231 || OPTi93X */
  71. module_param(index, int, 0444);
  72. MODULE_PARM_DESC(index, "Index value for opti9xx based soundcard.");
  73. module_param(id, charp, 0444);
  74. MODULE_PARM_DESC(id, "ID string for opti9xx based soundcard.");
  75. //module_param(enable, bool, 0444);
  76. //MODULE_PARM_DESC(enable, "Enable opti9xx soundcard.");
  77. #ifdef CONFIG_PNP
  78. module_param(isapnp, bool, 0444);
  79. MODULE_PARM_DESC(isapnp, "Enable ISA PnP detection for specified soundcard.");
  80. #endif
  81. module_param(port, long, 0444);
  82. MODULE_PARM_DESC(port, "WSS port # for opti9xx driver.");
  83. module_param(mpu_port, long, 0444);
  84. MODULE_PARM_DESC(mpu_port, "MPU-401 port # for opti9xx driver.");
  85. module_param(fm_port, long, 0444);
  86. MODULE_PARM_DESC(fm_port, "FM port # for opti9xx driver.");
  87. module_param(irq, int, 0444);
  88. MODULE_PARM_DESC(irq, "WSS irq # for opti9xx driver.");
  89. module_param(mpu_irq, int, 0444);
  90. MODULE_PARM_DESC(mpu_irq, "MPU-401 irq # for opti9xx driver.");
  91. module_param(dma1, int, 0444);
  92. MODULE_PARM_DESC(dma1, "1st dma # for opti9xx driver.");
  93. #if defined(CS4231) || defined(OPTi93X)
  94. module_param(dma2, int, 0444);
  95. MODULE_PARM_DESC(dma2, "2nd dma # for opti9xx driver.");
  96. #endif /* CS4231 || OPTi93X */
  97. #define OPTi9XX_HW_82C928 1
  98. #define OPTi9XX_HW_82C929 2
  99. #define OPTi9XX_HW_82C924 3
  100. #define OPTi9XX_HW_82C925 4
  101. #define OPTi9XX_HW_82C930 5
  102. #define OPTi9XX_HW_82C931 6
  103. #define OPTi9XX_HW_82C933 7
  104. #define OPTi9XX_HW_LAST OPTi9XX_HW_82C933
  105. #define OPTi9XX_MC_REG(n) n
  106. #ifdef OPTi93X
  107. #define OPTi93X_STATUS 0x02
  108. #define OPTi93X_PORT(chip, r) ((chip)->port + OPTi93X_##r)
  109. #define OPTi93X_IRQ_PLAYBACK 0x04
  110. #define OPTi93X_IRQ_CAPTURE 0x08
  111. #endif /* OPTi93X */
  112. struct snd_opti9xx {
  113. unsigned short hardware;
  114. unsigned char password;
  115. char name[7];
  116. unsigned long mc_base;
  117. struct resource *res_mc_base;
  118. unsigned long mc_base_size;
  119. #ifdef OPTi93X
  120. unsigned long mc_indir_index;
  121. unsigned long mc_indir_size;
  122. struct resource *res_mc_indir;
  123. struct snd_wss *codec;
  124. #endif /* OPTi93X */
  125. unsigned long pwd_reg;
  126. spinlock_t lock;
  127. int irq;
  128. #ifdef CONFIG_PNP
  129. struct pnp_dev *dev;
  130. struct pnp_dev *devmpu;
  131. #endif /* CONFIG_PNP */
  132. };
  133. static int snd_opti9xx_pnp_is_probed;
  134. #ifdef CONFIG_PNP
  135. static struct pnp_card_device_id snd_opti9xx_pnpids[] = {
  136. #ifndef OPTi93X
  137. /* OPTi 82C924 */
  138. { .id = "OPT0924", .devs = { { "OPT0000" }, { "OPT0002" } }, .driver_data = 0x0924 },
  139. /* OPTi 82C925 */
  140. { .id = "OPT0925", .devs = { { "OPT9250" }, { "OPT0002" } }, .driver_data = 0x0925 },
  141. #else
  142. /* OPTi 82C931/3 */
  143. { .id = "OPT0931", .devs = { { "OPT9310" }, { "OPT0002" } }, .driver_data = 0x0931 },
  144. #endif /* OPTi93X */
  145. { .id = "" }
  146. };
  147. MODULE_DEVICE_TABLE(pnp_card, snd_opti9xx_pnpids);
  148. #endif /* CONFIG_PNP */
  149. #ifdef OPTi93X
  150. #define DEV_NAME "opti93x"
  151. #else
  152. #define DEV_NAME "opti92x"
  153. #endif
  154. static char * snd_opti9xx_names[] = {
  155. "unknown",
  156. "82C928", "82C929",
  157. "82C924", "82C925",
  158. "82C930", "82C931", "82C933"
  159. };
  160. static long __devinit snd_legacy_find_free_ioport(long *port_table, long size)
  161. {
  162. while (*port_table != -1) {
  163. if (request_region(*port_table, size, "ALSA test")) {
  164. release_region(*port_table, size);
  165. return *port_table;
  166. }
  167. port_table++;
  168. }
  169. return -1;
  170. }
  171. static int __devinit snd_opti9xx_init(struct snd_opti9xx *chip,
  172. unsigned short hardware)
  173. {
  174. static int opti9xx_mc_size[] = {7, 7, 10, 10, 2, 2, 2};
  175. chip->hardware = hardware;
  176. strcpy(chip->name, snd_opti9xx_names[hardware]);
  177. chip->mc_base_size = opti9xx_mc_size[hardware];
  178. spin_lock_init(&chip->lock);
  179. chip->irq = -1;
  180. switch (hardware) {
  181. #ifndef OPTi93X
  182. case OPTi9XX_HW_82C928:
  183. case OPTi9XX_HW_82C929:
  184. chip->mc_base = 0xf8c;
  185. chip->password = (hardware == OPTi9XX_HW_82C928) ? 0xe2 : 0xe3;
  186. chip->pwd_reg = 3;
  187. break;
  188. case OPTi9XX_HW_82C924:
  189. case OPTi9XX_HW_82C925:
  190. chip->mc_base = 0xf8c;
  191. chip->password = 0xe5;
  192. chip->pwd_reg = 3;
  193. break;
  194. #else /* OPTi93X */
  195. case OPTi9XX_HW_82C930:
  196. case OPTi9XX_HW_82C931:
  197. case OPTi9XX_HW_82C933:
  198. chip->mc_base = (hardware == OPTi9XX_HW_82C930) ? 0xf8f : 0xf8d;
  199. if (!chip->mc_indir_index) {
  200. chip->mc_indir_index = 0xe0e;
  201. chip->mc_indir_size = 2;
  202. }
  203. chip->password = 0xe4;
  204. chip->pwd_reg = 0;
  205. break;
  206. #endif /* OPTi93X */
  207. default:
  208. snd_printk(KERN_ERR "chip %d not supported\n", hardware);
  209. return -ENODEV;
  210. }
  211. return 0;
  212. }
  213. static unsigned char snd_opti9xx_read(struct snd_opti9xx *chip,
  214. unsigned char reg)
  215. {
  216. unsigned long flags;
  217. unsigned char retval = 0xff;
  218. spin_lock_irqsave(&chip->lock, flags);
  219. outb(chip->password, chip->mc_base + chip->pwd_reg);
  220. switch (chip->hardware) {
  221. #ifndef OPTi93X
  222. case OPTi9XX_HW_82C924:
  223. case OPTi9XX_HW_82C925:
  224. if (reg > 7) {
  225. outb(reg, chip->mc_base + 8);
  226. outb(chip->password, chip->mc_base + chip->pwd_reg);
  227. retval = inb(chip->mc_base + 9);
  228. break;
  229. }
  230. case OPTi9XX_HW_82C928:
  231. case OPTi9XX_HW_82C929:
  232. retval = inb(chip->mc_base + reg);
  233. break;
  234. #else /* OPTi93X */
  235. case OPTi9XX_HW_82C930:
  236. case OPTi9XX_HW_82C931:
  237. case OPTi9XX_HW_82C933:
  238. outb(reg, chip->mc_indir_index);
  239. outb(chip->password, chip->mc_base + chip->pwd_reg);
  240. retval = inb(chip->mc_indir_index + 1);
  241. break;
  242. #endif /* OPTi93X */
  243. default:
  244. snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
  245. }
  246. spin_unlock_irqrestore(&chip->lock, flags);
  247. return retval;
  248. }
  249. static void snd_opti9xx_write(struct snd_opti9xx *chip, unsigned char reg,
  250. unsigned char value)
  251. {
  252. unsigned long flags;
  253. spin_lock_irqsave(&chip->lock, flags);
  254. outb(chip->password, chip->mc_base + chip->pwd_reg);
  255. switch (chip->hardware) {
  256. #ifndef OPTi93X
  257. case OPTi9XX_HW_82C924:
  258. case OPTi9XX_HW_82C925:
  259. if (reg > 7) {
  260. outb(reg, chip->mc_base + 8);
  261. outb(chip->password, chip->mc_base + chip->pwd_reg);
  262. outb(value, chip->mc_base + 9);
  263. break;
  264. }
  265. case OPTi9XX_HW_82C928:
  266. case OPTi9XX_HW_82C929:
  267. outb(value, chip->mc_base + reg);
  268. break;
  269. #else /* OPTi93X */
  270. case OPTi9XX_HW_82C930:
  271. case OPTi9XX_HW_82C931:
  272. case OPTi9XX_HW_82C933:
  273. outb(reg, chip->mc_indir_index);
  274. outb(chip->password, chip->mc_base + chip->pwd_reg);
  275. outb(value, chip->mc_indir_index + 1);
  276. break;
  277. #endif /* OPTi93X */
  278. default:
  279. snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
  280. }
  281. spin_unlock_irqrestore(&chip->lock, flags);
  282. }
  283. #define snd_opti9xx_write_mask(chip, reg, value, mask) \
  284. snd_opti9xx_write(chip, reg, \
  285. (snd_opti9xx_read(chip, reg) & ~(mask)) | ((value) & (mask)))
  286. static int __devinit snd_opti9xx_configure(struct snd_opti9xx *chip,
  287. long wss_base,
  288. int irq, int dma1, int dma2,
  289. long mpu_port, int mpu_irq)
  290. {
  291. unsigned char wss_base_bits;
  292. unsigned char irq_bits;
  293. unsigned char dma_bits;
  294. unsigned char mpu_port_bits = 0;
  295. unsigned char mpu_irq_bits;
  296. switch (chip->hardware) {
  297. #ifndef OPTi93X
  298. case OPTi9XX_HW_82C924:
  299. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0xf0, 0xfc);
  300. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x02);
  301. case OPTi9XX_HW_82C925:
  302. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  303. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  304. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xf0, 0xff);
  305. #ifdef CS4231
  306. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  307. #else
  308. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  309. #endif /* CS4231 */
  310. break;
  311. case OPTi9XX_HW_82C928:
  312. case OPTi9XX_HW_82C929:
  313. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), 0x80, 0x80);
  314. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2), 0x00, 0x20);
  315. /*
  316. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0xa2, 0xae);
  317. */
  318. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x00, 0x0c);
  319. #ifdef CS4231
  320. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x02, 0x02);
  321. #else
  322. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x00, 0x02);
  323. #endif /* CS4231 */
  324. break;
  325. #else /* OPTi93X */
  326. case OPTi9XX_HW_82C931:
  327. case OPTi9XX_HW_82C933:
  328. /*
  329. * The BTC 1817DW has QS1000 wavetable which is connected
  330. * to the serial digital input of the OPTI931.
  331. */
  332. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(21), 0x82, 0xff);
  333. /*
  334. * This bit sets OPTI931 to automaticaly select FM
  335. * or digital input signal.
  336. */
  337. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(26), 0x01, 0x01);
  338. case OPTi9XX_HW_82C930: /* FALL THROUGH */
  339. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6), 0x02, 0x03);
  340. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(3), 0x00, 0xff);
  341. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(4), 0x10 |
  342. (chip->hardware == OPTi9XX_HW_82C930 ? 0x00 : 0x04),
  343. 0x34);
  344. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(5), 0x20, 0xbf);
  345. break;
  346. #endif /* OPTi93X */
  347. default:
  348. snd_printk(KERN_ERR "chip %d not supported\n", chip->hardware);
  349. return -EINVAL;
  350. }
  351. switch (wss_base) {
  352. case 0x530:
  353. wss_base_bits = 0x00;
  354. break;
  355. case 0x604:
  356. wss_base_bits = 0x03;
  357. break;
  358. case 0xe80:
  359. wss_base_bits = 0x01;
  360. break;
  361. case 0xf40:
  362. wss_base_bits = 0x02;
  363. break;
  364. default:
  365. snd_printk(KERN_WARNING "WSS port 0x%lx not valid\n", wss_base);
  366. goto __skip_base;
  367. }
  368. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(1), wss_base_bits << 4, 0x30);
  369. __skip_base:
  370. switch (irq) {
  371. //#ifdef OPTi93X
  372. case 5:
  373. irq_bits = 0x05;
  374. break;
  375. //#endif /* OPTi93X */
  376. case 7:
  377. irq_bits = 0x01;
  378. break;
  379. case 9:
  380. irq_bits = 0x02;
  381. break;
  382. case 10:
  383. irq_bits = 0x03;
  384. break;
  385. case 11:
  386. irq_bits = 0x04;
  387. break;
  388. default:
  389. snd_printk(KERN_WARNING "WSS irq # %d not valid\n", irq);
  390. goto __skip_resources;
  391. }
  392. switch (dma1) {
  393. case 0:
  394. dma_bits = 0x01;
  395. break;
  396. case 1:
  397. dma_bits = 0x02;
  398. break;
  399. case 3:
  400. dma_bits = 0x03;
  401. break;
  402. default:
  403. snd_printk(KERN_WARNING "WSS dma1 # %d not valid\n", dma1);
  404. goto __skip_resources;
  405. }
  406. #if defined(CS4231) || defined(OPTi93X)
  407. if (dma1 == dma2) {
  408. snd_printk(KERN_ERR "don't want to share dmas\n");
  409. return -EBUSY;
  410. }
  411. switch (dma2) {
  412. case 0:
  413. case 1:
  414. break;
  415. default:
  416. snd_printk(KERN_WARNING "WSS dma2 # %d not valid\n", dma2);
  417. goto __skip_resources;
  418. }
  419. dma_bits |= 0x04;
  420. #endif /* CS4231 || OPTi93X */
  421. #ifndef OPTi93X
  422. outb(irq_bits << 3 | dma_bits, wss_base);
  423. #else /* OPTi93X */
  424. snd_opti9xx_write(chip, OPTi9XX_MC_REG(3), (irq_bits << 3 | dma_bits));
  425. #endif /* OPTi93X */
  426. __skip_resources:
  427. if (chip->hardware > OPTi9XX_HW_82C928) {
  428. switch (mpu_port) {
  429. case 0:
  430. case -1:
  431. break;
  432. case 0x300:
  433. mpu_port_bits = 0x03;
  434. break;
  435. case 0x310:
  436. mpu_port_bits = 0x02;
  437. break;
  438. case 0x320:
  439. mpu_port_bits = 0x01;
  440. break;
  441. case 0x330:
  442. mpu_port_bits = 0x00;
  443. break;
  444. default:
  445. snd_printk(KERN_WARNING
  446. "MPU-401 port 0x%lx not valid\n", mpu_port);
  447. goto __skip_mpu;
  448. }
  449. switch (mpu_irq) {
  450. case 5:
  451. mpu_irq_bits = 0x02;
  452. break;
  453. case 7:
  454. mpu_irq_bits = 0x03;
  455. break;
  456. case 9:
  457. mpu_irq_bits = 0x00;
  458. break;
  459. case 10:
  460. mpu_irq_bits = 0x01;
  461. break;
  462. default:
  463. snd_printk(KERN_WARNING "MPU-401 irq # %d not valid\n",
  464. mpu_irq);
  465. goto __skip_mpu;
  466. }
  467. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(6),
  468. (mpu_port <= 0) ? 0x00 :
  469. 0x80 | mpu_port_bits << 5 | mpu_irq_bits << 3,
  470. 0xf8);
  471. }
  472. __skip_mpu:
  473. return 0;
  474. }
  475. #ifdef OPTi93X
  476. static const DECLARE_TLV_DB_SCALE(db_scale_5bit_3db_step, -9300, 300, 0);
  477. static const DECLARE_TLV_DB_SCALE(db_scale_5bit, -4650, 150, 0);
  478. static const DECLARE_TLV_DB_SCALE(db_scale_4bit_12db_max, -3300, 300, 0);
  479. static struct snd_kcontrol_new snd_opti93x_controls[] = {
  480. WSS_DOUBLE("Master Playback Switch", 0,
  481. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
  482. WSS_DOUBLE_TLV("Master Playback Volume", 0,
  483. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
  484. db_scale_5bit_3db_step),
  485. WSS_DOUBLE_TLV("PCM Playback Volume", 0,
  486. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1,
  487. db_scale_5bit),
  488. WSS_DOUBLE_TLV("FM Playback Volume", 0,
  489. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1,
  490. db_scale_4bit_12db_max),
  491. WSS_DOUBLE("Line Playback Switch", 0,
  492. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  493. WSS_DOUBLE_TLV("Line Playback Volume", 0,
  494. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1,
  495. db_scale_4bit_12db_max),
  496. WSS_DOUBLE("Mic Playback Switch", 0,
  497. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
  498. WSS_DOUBLE_TLV("Mic Playback Volume", 0,
  499. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1,
  500. db_scale_4bit_12db_max),
  501. WSS_DOUBLE_TLV("CD Playback Volume", 0,
  502. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1,
  503. db_scale_4bit_12db_max),
  504. WSS_DOUBLE("Aux Playback Switch", 0,
  505. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
  506. WSS_DOUBLE_TLV("Aux Playback Volume", 0,
  507. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1,
  508. db_scale_4bit_12db_max),
  509. };
  510. static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
  511. {
  512. struct snd_card *card;
  513. unsigned int idx;
  514. struct snd_ctl_elem_id id1, id2;
  515. int err;
  516. if (snd_BUG_ON(!chip || !chip->pcm))
  517. return -EINVAL;
  518. card = chip->card;
  519. strcpy(card->mixername, chip->pcm->name);
  520. memset(&id1, 0, sizeof(id1));
  521. memset(&id2, 0, sizeof(id2));
  522. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  523. /* reassign AUX0 switch to CD */
  524. strcpy(id1.name, "Aux Playback Switch");
  525. strcpy(id2.name, "CD Playback Switch");
  526. err = snd_ctl_rename_id(card, &id1, &id2);
  527. if (err < 0) {
  528. snd_printk(KERN_ERR "Cannot rename opti93x control\n");
  529. return err;
  530. }
  531. /* reassign AUX1 switch to FM */
  532. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  533. strcpy(id2.name, "FM Playback Switch");
  534. err = snd_ctl_rename_id(card, &id1, &id2);
  535. if (err < 0) {
  536. snd_printk(KERN_ERR "Cannot rename opti93x control\n");
  537. return err;
  538. }
  539. /* remove AUX1 volume */
  540. strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
  541. snd_ctl_remove_id(card, &id1);
  542. /* Replace WSS volume controls with OPTi93x volume controls */
  543. id1.index = 0;
  544. for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
  545. strcpy(id1.name, snd_opti93x_controls[idx].name);
  546. snd_ctl_remove_id(card, &id1);
  547. err = snd_ctl_add(card,
  548. snd_ctl_new1(&snd_opti93x_controls[idx], chip));
  549. if (err < 0)
  550. return err;
  551. }
  552. return 0;
  553. }
  554. static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
  555. {
  556. struct snd_opti9xx *chip = dev_id;
  557. struct snd_wss *codec = chip->codec;
  558. unsigned char status;
  559. if (!codec)
  560. return IRQ_HANDLED;
  561. status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
  562. if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
  563. snd_pcm_period_elapsed(codec->playback_substream);
  564. if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
  565. snd_wss_overrange(codec);
  566. snd_pcm_period_elapsed(codec->capture_substream);
  567. }
  568. outb(0x00, OPTi93X_PORT(codec, STATUS));
  569. return IRQ_HANDLED;
  570. }
  571. #endif /* OPTi93X */
  572. static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
  573. {
  574. unsigned char value;
  575. #ifdef OPTi93X
  576. unsigned long flags;
  577. #endif
  578. chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
  579. "OPTi9xx MC");
  580. if (chip->res_mc_base == NULL)
  581. return -EBUSY;
  582. #ifndef OPTi93X
  583. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
  584. if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
  585. if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
  586. return 0;
  587. #else /* OPTi93X */
  588. chip->res_mc_indir = request_region(chip->mc_indir_index,
  589. chip->mc_indir_size,
  590. "OPTi93x MC");
  591. if (chip->res_mc_indir == NULL)
  592. return -EBUSY;
  593. spin_lock_irqsave(&chip->lock, flags);
  594. outb(chip->password, chip->mc_base + chip->pwd_reg);
  595. outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
  596. spin_unlock_irqrestore(&chip->lock, flags);
  597. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
  598. snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
  599. if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
  600. return 0;
  601. release_and_free_resource(chip->res_mc_indir);
  602. chip->res_mc_indir = NULL;
  603. #endif /* OPTi93X */
  604. release_and_free_resource(chip->res_mc_base);
  605. chip->res_mc_base = NULL;
  606. return -ENODEV;
  607. }
  608. static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
  609. struct snd_opti9xx *chip)
  610. {
  611. int i, err;
  612. #ifndef OPTi93X
  613. for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
  614. #else
  615. for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
  616. #endif
  617. err = snd_opti9xx_init(chip, i);
  618. if (err < 0)
  619. return err;
  620. err = snd_opti9xx_read_check(chip);
  621. if (err == 0)
  622. return 1;
  623. #ifdef OPTi93X
  624. chip->mc_indir_index = 0;
  625. #endif
  626. }
  627. return -ENODEV;
  628. }
  629. #ifdef CONFIG_PNP
  630. static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
  631. struct pnp_card_link *card,
  632. const struct pnp_card_device_id *pid)
  633. {
  634. struct pnp_dev *pdev;
  635. int err;
  636. chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
  637. if (chip->dev == NULL)
  638. return -EBUSY;
  639. chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
  640. pdev = chip->dev;
  641. err = pnp_activate_dev(pdev);
  642. if (err < 0) {
  643. snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
  644. return err;
  645. }
  646. #ifdef OPTi93X
  647. port = pnp_port_start(pdev, 0) - 4;
  648. fm_port = pnp_port_start(pdev, 1) + 8;
  649. chip->mc_indir_index = pnp_port_start(pdev, 3) + 2;
  650. chip->mc_indir_size = pnp_port_len(pdev, 3) - 2;
  651. #else
  652. if (pid->driver_data != 0x0924)
  653. port = pnp_port_start(pdev, 1);
  654. fm_port = pnp_port_start(pdev, 2) + 8;
  655. #endif /* OPTi93X */
  656. irq = pnp_irq(pdev, 0);
  657. dma1 = pnp_dma(pdev, 0);
  658. #if defined(CS4231) || defined(OPTi93X)
  659. dma2 = pnp_dma(pdev, 1);
  660. #endif /* CS4231 || OPTi93X */
  661. pdev = chip->devmpu;
  662. if (pdev && mpu_port > 0) {
  663. err = pnp_activate_dev(pdev);
  664. if (err < 0) {
  665. snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
  666. mpu_port = -1;
  667. chip->devmpu = NULL;
  668. } else {
  669. mpu_port = pnp_port_start(pdev, 0);
  670. mpu_irq = pnp_irq(pdev, 0);
  671. }
  672. }
  673. return pid->driver_data;
  674. }
  675. #endif /* CONFIG_PNP */
  676. static void snd_card_opti9xx_free(struct snd_card *card)
  677. {
  678. struct snd_opti9xx *chip = card->private_data;
  679. if (chip) {
  680. #ifdef OPTi93X
  681. if (chip->irq > 0) {
  682. disable_irq(chip->irq);
  683. free_irq(chip->irq, chip);
  684. }
  685. release_and_free_resource(chip->res_mc_indir);
  686. #endif
  687. release_and_free_resource(chip->res_mc_base);
  688. }
  689. }
  690. static int __devinit snd_opti9xx_probe(struct snd_card *card)
  691. {
  692. static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
  693. int error;
  694. int xdma2;
  695. struct snd_opti9xx *chip = card->private_data;
  696. struct snd_wss *codec;
  697. #ifdef CS4231
  698. struct snd_timer *timer;
  699. #endif
  700. struct snd_pcm *pcm;
  701. struct snd_rawmidi *rmidi;
  702. struct snd_hwdep *synth;
  703. #if defined(CS4231) || defined(OPTi93X)
  704. xdma2 = dma2;
  705. #else
  706. xdma2 = -1;
  707. #endif
  708. if (port == SNDRV_AUTO_PORT) {
  709. port = snd_legacy_find_free_ioport(possible_ports, 4);
  710. if (port < 0) {
  711. snd_printk(KERN_ERR "unable to find a free WSS port\n");
  712. return -EBUSY;
  713. }
  714. }
  715. error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
  716. mpu_port, mpu_irq);
  717. if (error)
  718. return error;
  719. error = snd_wss_create(card, port + 4, -1, irq, dma1, xdma2,
  720. #ifdef OPTi93X
  721. WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
  722. #else
  723. WSS_HW_DETECT, 0,
  724. #endif
  725. &codec);
  726. if (error < 0)
  727. return error;
  728. #ifdef OPTi93X
  729. chip->codec = codec;
  730. #endif
  731. error = snd_wss_pcm(codec, 0, &pcm);
  732. if (error < 0)
  733. return error;
  734. error = snd_wss_mixer(codec);
  735. if (error < 0)
  736. return error;
  737. #ifdef OPTi93X
  738. error = snd_opti93x_mixer(codec);
  739. if (error < 0)
  740. return error;
  741. #endif
  742. #ifdef CS4231
  743. error = snd_wss_timer(codec, 0, &timer);
  744. if (error < 0)
  745. return error;
  746. #endif
  747. #ifdef OPTi93X
  748. error = request_irq(irq, snd_opti93x_interrupt,
  749. IRQF_DISABLED, DEV_NAME" - WSS", chip);
  750. if (error < 0) {
  751. snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", irq);
  752. return error;
  753. }
  754. #endif
  755. chip->irq = irq;
  756. strcpy(card->driver, chip->name);
  757. sprintf(card->shortname, "OPTi %s", card->driver);
  758. #if defined(CS4231) || defined(OPTi93X)
  759. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
  760. card->shortname, pcm->name, port + 4, irq, dma1, xdma2);
  761. #else
  762. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
  763. card->shortname, pcm->name, port + 4, irq, dma1);
  764. #endif /* CS4231 || OPTi93X */
  765. if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
  766. rmidi = NULL;
  767. else {
  768. error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  769. mpu_port, 0, mpu_irq, IRQF_DISABLED, &rmidi);
  770. if (error)
  771. snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
  772. mpu_port);
  773. }
  774. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
  775. struct snd_opl3 *opl3 = NULL;
  776. #ifndef OPTi93X
  777. if (chip->hardware == OPTi9XX_HW_82C928 ||
  778. chip->hardware == OPTi9XX_HW_82C929 ||
  779. chip->hardware == OPTi9XX_HW_82C924) {
  780. struct snd_opl4 *opl4;
  781. /* assume we have an OPL4 */
  782. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  783. 0x20, 0x20);
  784. if (snd_opl4_create(card, fm_port, fm_port - 8,
  785. 2, &opl3, &opl4) < 0) {
  786. /* no luck, use OPL3 instead */
  787. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  788. 0x00, 0x20);
  789. }
  790. }
  791. #endif /* !OPTi93X */
  792. if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
  793. OPL3_HW_AUTO, 0, &opl3) < 0) {
  794. snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
  795. fm_port, fm_port + 4 - 1);
  796. }
  797. if (opl3) {
  798. error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
  799. if (error < 0)
  800. return error;
  801. }
  802. }
  803. return snd_card_register(card);
  804. }
  805. static int snd_opti9xx_card_new(struct snd_card **cardp)
  806. {
  807. struct snd_card *card;
  808. int err;
  809. err = snd_card_create(index, id, THIS_MODULE,
  810. sizeof(struct snd_opti9xx), &card);
  811. if (err < 0)
  812. return err;
  813. card->private_free = snd_card_opti9xx_free;
  814. *cardp = card;
  815. return 0;
  816. }
  817. static int __devinit snd_opti9xx_isa_match(struct device *devptr,
  818. unsigned int dev)
  819. {
  820. #ifdef CONFIG_PNP
  821. if (snd_opti9xx_pnp_is_probed)
  822. return 0;
  823. if (isapnp)
  824. return 0;
  825. #endif
  826. return 1;
  827. }
  828. static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
  829. unsigned int dev)
  830. {
  831. struct snd_card *card;
  832. int error;
  833. static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
  834. #ifdef OPTi93X
  835. static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
  836. #else
  837. static int possible_irqs[] = {9, 10, 11, 7, -1};
  838. #endif /* OPTi93X */
  839. static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
  840. static int possible_dma1s[] = {3, 1, 0, -1};
  841. #if defined(CS4231) || defined(OPTi93X)
  842. static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
  843. #endif /* CS4231 || OPTi93X */
  844. if (mpu_port == SNDRV_AUTO_PORT) {
  845. if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
  846. snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
  847. return -EBUSY;
  848. }
  849. }
  850. if (irq == SNDRV_AUTO_IRQ) {
  851. if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  852. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  853. return -EBUSY;
  854. }
  855. }
  856. if (mpu_irq == SNDRV_AUTO_IRQ) {
  857. if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
  858. snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
  859. return -EBUSY;
  860. }
  861. }
  862. if (dma1 == SNDRV_AUTO_DMA) {
  863. if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
  864. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  865. return -EBUSY;
  866. }
  867. }
  868. #if defined(CS4231) || defined(OPTi93X)
  869. if (dma2 == SNDRV_AUTO_DMA) {
  870. if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
  871. snd_printk(KERN_ERR "unable to find a free DMA2\n");
  872. return -EBUSY;
  873. }
  874. }
  875. #endif
  876. error = snd_opti9xx_card_new(&card);
  877. if (error < 0)
  878. return error;
  879. if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
  880. snd_card_free(card);
  881. return error;
  882. }
  883. snd_card_set_dev(card, devptr);
  884. if ((error = snd_opti9xx_probe(card)) < 0) {
  885. snd_card_free(card);
  886. return error;
  887. }
  888. dev_set_drvdata(devptr, card);
  889. return 0;
  890. }
  891. static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
  892. unsigned int dev)
  893. {
  894. snd_card_free(dev_get_drvdata(devptr));
  895. dev_set_drvdata(devptr, NULL);
  896. return 0;
  897. }
  898. static struct isa_driver snd_opti9xx_driver = {
  899. .match = snd_opti9xx_isa_match,
  900. .probe = snd_opti9xx_isa_probe,
  901. .remove = __devexit_p(snd_opti9xx_isa_remove),
  902. /* FIXME: suspend/resume */
  903. .driver = {
  904. .name = DEV_NAME
  905. },
  906. };
  907. #ifdef CONFIG_PNP
  908. static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
  909. const struct pnp_card_device_id *pid)
  910. {
  911. struct snd_card *card;
  912. int error, hw;
  913. struct snd_opti9xx *chip;
  914. if (snd_opti9xx_pnp_is_probed)
  915. return -EBUSY;
  916. if (! isapnp)
  917. return -ENODEV;
  918. error = snd_opti9xx_card_new(&card);
  919. if (error < 0)
  920. return error;
  921. chip = card->private_data;
  922. hw = snd_card_opti9xx_pnp(chip, pcard, pid);
  923. switch (hw) {
  924. case 0x0924:
  925. hw = OPTi9XX_HW_82C924;
  926. break;
  927. case 0x0925:
  928. hw = OPTi9XX_HW_82C925;
  929. break;
  930. case 0x0931:
  931. hw = OPTi9XX_HW_82C931;
  932. break;
  933. default:
  934. snd_card_free(card);
  935. return -ENODEV;
  936. }
  937. if ((error = snd_opti9xx_init(chip, hw))) {
  938. snd_card_free(card);
  939. return error;
  940. }
  941. if (hw <= OPTi9XX_HW_82C930)
  942. chip->mc_base -= 0x80;
  943. error = snd_opti9xx_read_check(chip);
  944. if (error) {
  945. snd_printk(KERN_ERR "OPTI chip not found\n");
  946. snd_card_free(card);
  947. return error;
  948. }
  949. snd_card_set_dev(card, &pcard->card->dev);
  950. if ((error = snd_opti9xx_probe(card)) < 0) {
  951. snd_card_free(card);
  952. return error;
  953. }
  954. pnp_set_card_drvdata(pcard, card);
  955. snd_opti9xx_pnp_is_probed = 1;
  956. return 0;
  957. }
  958. static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
  959. {
  960. snd_card_free(pnp_get_card_drvdata(pcard));
  961. pnp_set_card_drvdata(pcard, NULL);
  962. snd_opti9xx_pnp_is_probed = 0;
  963. }
  964. static struct pnp_card_driver opti9xx_pnpc_driver = {
  965. .flags = PNP_DRIVER_RES_DISABLE,
  966. .name = "opti9xx",
  967. .id_table = snd_opti9xx_pnpids,
  968. .probe = snd_opti9xx_pnp_probe,
  969. .remove = __devexit_p(snd_opti9xx_pnp_remove),
  970. };
  971. #endif
  972. #ifdef OPTi93X
  973. #define CHIP_NAME "82C93x"
  974. #else
  975. #define CHIP_NAME "82C92x"
  976. #endif
  977. static int __init alsa_card_opti9xx_init(void)
  978. {
  979. #ifdef CONFIG_PNP
  980. pnp_register_card_driver(&opti9xx_pnpc_driver);
  981. if (snd_opti9xx_pnp_is_probed)
  982. return 0;
  983. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  984. #endif
  985. return isa_register_driver(&snd_opti9xx_driver, 1);
  986. }
  987. static void __exit alsa_card_opti9xx_exit(void)
  988. {
  989. if (!snd_opti9xx_pnp_is_probed) {
  990. isa_unregister_driver(&snd_opti9xx_driver);
  991. return;
  992. }
  993. #ifdef CONFIG_PNP
  994. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  995. #endif
  996. }
  997. module_init(alsa_card_opti9xx_init)
  998. module_exit(alsa_card_opti9xx_exit)