opti92x-ad1848.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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_6bit, -9450, 150, 0);
  477. static struct snd_kcontrol_new snd_opti93x_controls[] = {
  478. WSS_DOUBLE("Master Playback Switch", 0,
  479. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 7, 7, 1, 1),
  480. WSS_DOUBLE_TLV("Master Playback Volume", 0,
  481. OPTi93X_OUT_LEFT, OPTi93X_OUT_RIGHT, 1, 1, 31, 1,
  482. db_scale_6bit),
  483. WSS_DOUBLE("PCM Playback Volume", 0,
  484. CS4231_LEFT_OUTPUT, CS4231_RIGHT_OUTPUT, 0, 0, 31, 1),
  485. WSS_DOUBLE("FM Playback Volume", 0,
  486. CS4231_AUX2_LEFT_INPUT, CS4231_AUX2_RIGHT_INPUT, 1, 1, 15, 1),
  487. WSS_DOUBLE("Line Playback Switch", 0,
  488. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 7, 7, 1, 1),
  489. WSS_DOUBLE("Line Playback Volume", 0,
  490. CS4231_LEFT_LINE_IN, CS4231_RIGHT_LINE_IN, 0, 0, 15, 1),
  491. WSS_DOUBLE("Mic Playback Switch", 0,
  492. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 7, 7, 1, 1),
  493. WSS_DOUBLE("Mic Playback Volume", 0,
  494. OPTi93X_MIC_LEFT_INPUT, OPTi93X_MIC_RIGHT_INPUT, 1, 1, 15, 1),
  495. WSS_DOUBLE("CD Playback Volume", 0,
  496. CS4231_AUX1_LEFT_INPUT, CS4231_AUX1_RIGHT_INPUT, 1, 1, 15, 1),
  497. WSS_DOUBLE("Aux Playback Switch", 0,
  498. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 7, 7, 1, 1),
  499. WSS_DOUBLE("Aux Playback Volume", 0,
  500. OPTi931_AUX_LEFT_INPUT, OPTi931_AUX_RIGHT_INPUT, 1, 1, 15, 1),
  501. };
  502. static int __devinit snd_opti93x_mixer(struct snd_wss *chip)
  503. {
  504. struct snd_card *card;
  505. unsigned int idx;
  506. struct snd_ctl_elem_id id1, id2;
  507. int err;
  508. if (snd_BUG_ON(!chip || !chip->pcm))
  509. return -EINVAL;
  510. card = chip->card;
  511. strcpy(card->mixername, chip->pcm->name);
  512. memset(&id1, 0, sizeof(id1));
  513. memset(&id2, 0, sizeof(id2));
  514. id1.iface = id2.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  515. /* reassign AUX0 switch to CD */
  516. strcpy(id1.name, "Aux Playback Switch");
  517. strcpy(id2.name, "CD Playback Switch");
  518. err = snd_ctl_rename_id(card, &id1, &id2);
  519. if (err < 0) {
  520. snd_printk(KERN_ERR "Cannot rename opti93x control\n");
  521. return err;
  522. }
  523. /* reassign AUX1 switch to FM */
  524. strcpy(id1.name, "Aux Playback Switch"); id1.index = 1;
  525. strcpy(id2.name, "FM 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. /* remove AUX1 volume */
  532. strcpy(id1.name, "Aux Playback Volume"); id1.index = 1;
  533. snd_ctl_remove_id(card, &id1);
  534. /* Replace WSS volume controls with OPTi93x volume controls */
  535. id1.index = 0;
  536. for (idx = 0; idx < ARRAY_SIZE(snd_opti93x_controls); idx++) {
  537. strcpy(id1.name, snd_opti93x_controls[idx].name);
  538. snd_ctl_remove_id(card, &id1);
  539. err = snd_ctl_add(card,
  540. snd_ctl_new1(&snd_opti93x_controls[idx], chip));
  541. if (err < 0)
  542. return err;
  543. }
  544. return 0;
  545. }
  546. static irqreturn_t snd_opti93x_interrupt(int irq, void *dev_id)
  547. {
  548. struct snd_wss *codec = dev_id;
  549. struct snd_opti9xx *chip = codec->card->private_data;
  550. unsigned char status;
  551. status = snd_opti9xx_read(chip, OPTi9XX_MC_REG(11));
  552. if ((status & OPTi93X_IRQ_PLAYBACK) && codec->playback_substream)
  553. snd_pcm_period_elapsed(codec->playback_substream);
  554. if ((status & OPTi93X_IRQ_CAPTURE) && codec->capture_substream) {
  555. snd_wss_overrange(codec);
  556. snd_pcm_period_elapsed(codec->capture_substream);
  557. }
  558. outb(0x00, OPTi93X_PORT(codec, STATUS));
  559. return IRQ_HANDLED;
  560. }
  561. #endif /* OPTi93X */
  562. static int __devinit snd_opti9xx_read_check(struct snd_opti9xx *chip)
  563. {
  564. unsigned char value;
  565. #ifdef OPTi93X
  566. unsigned long flags;
  567. #endif
  568. chip->res_mc_base = request_region(chip->mc_base, chip->mc_base_size,
  569. "OPTi9xx MC");
  570. if (chip->res_mc_base == NULL)
  571. return -EBUSY;
  572. #ifndef OPTi93X
  573. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(1));
  574. if (value != 0xff && value != inb(chip->mc_base + OPTi9XX_MC_REG(1)))
  575. if (value == snd_opti9xx_read(chip, OPTi9XX_MC_REG(1)))
  576. return 0;
  577. #else /* OPTi93X */
  578. chip->res_mc_indir = request_region(chip->mc_indir_index,
  579. chip->mc_indir_size,
  580. "OPTi93x MC");
  581. if (chip->res_mc_indir == NULL)
  582. return -EBUSY;
  583. spin_lock_irqsave(&chip->lock, flags);
  584. outb(chip->password, chip->mc_base + chip->pwd_reg);
  585. outb(((chip->mc_indir_index & 0x1f0) >> 4), chip->mc_base);
  586. spin_unlock_irqrestore(&chip->lock, flags);
  587. value = snd_opti9xx_read(chip, OPTi9XX_MC_REG(7));
  588. snd_opti9xx_write(chip, OPTi9XX_MC_REG(7), 0xff - value);
  589. if (snd_opti9xx_read(chip, OPTi9XX_MC_REG(7)) == 0xff - value)
  590. return 0;
  591. release_and_free_resource(chip->res_mc_indir);
  592. chip->res_mc_indir = NULL;
  593. #endif /* OPTi93X */
  594. release_and_free_resource(chip->res_mc_base);
  595. chip->res_mc_base = NULL;
  596. return -ENODEV;
  597. }
  598. static int __devinit snd_card_opti9xx_detect(struct snd_card *card,
  599. struct snd_opti9xx *chip)
  600. {
  601. int i, err;
  602. #ifndef OPTi93X
  603. for (i = OPTi9XX_HW_82C928; i < OPTi9XX_HW_82C930; i++) {
  604. #else
  605. for (i = OPTi9XX_HW_82C931; i >= OPTi9XX_HW_82C930; i--) {
  606. #endif
  607. err = snd_opti9xx_init(chip, i);
  608. if (err < 0)
  609. return err;
  610. err = snd_opti9xx_read_check(chip);
  611. if (err == 0)
  612. return 1;
  613. #ifdef OPTi93X
  614. chip->mc_indir_index = 0;
  615. #endif
  616. }
  617. return -ENODEV;
  618. }
  619. #ifdef CONFIG_PNP
  620. static int __devinit snd_card_opti9xx_pnp(struct snd_opti9xx *chip,
  621. struct pnp_card_link *card,
  622. const struct pnp_card_device_id *pid)
  623. {
  624. struct pnp_dev *pdev;
  625. int err;
  626. chip->dev = pnp_request_card_device(card, pid->devs[0].id, NULL);
  627. if (chip->dev == NULL)
  628. return -EBUSY;
  629. chip->devmpu = pnp_request_card_device(card, pid->devs[1].id, NULL);
  630. pdev = chip->dev;
  631. err = pnp_activate_dev(pdev);
  632. if (err < 0) {
  633. snd_printk(KERN_ERR "AUDIO pnp configure failure: %d\n", err);
  634. return err;
  635. }
  636. #ifdef OPTi93X
  637. port = pnp_port_start(pdev, 0) - 4;
  638. fm_port = pnp_port_start(pdev, 1) + 8;
  639. chip->mc_indir_index = pnp_port_start(pdev, 3) + 2;
  640. chip->mc_indir_size = pnp_port_len(pdev, 3) - 2;
  641. #else
  642. if (pid->driver_data != 0x0924)
  643. port = pnp_port_start(pdev, 1);
  644. fm_port = pnp_port_start(pdev, 2) + 8;
  645. #endif /* OPTi93X */
  646. irq = pnp_irq(pdev, 0);
  647. dma1 = pnp_dma(pdev, 0);
  648. #if defined(CS4231) || defined(OPTi93X)
  649. dma2 = pnp_dma(pdev, 1);
  650. #endif /* CS4231 || OPTi93X */
  651. pdev = chip->devmpu;
  652. if (pdev && mpu_port > 0) {
  653. err = pnp_activate_dev(pdev);
  654. if (err < 0) {
  655. snd_printk(KERN_ERR "AUDIO pnp configure failure\n");
  656. mpu_port = -1;
  657. chip->devmpu = NULL;
  658. } else {
  659. mpu_port = pnp_port_start(pdev, 0);
  660. mpu_irq = pnp_irq(pdev, 0);
  661. }
  662. }
  663. return pid->driver_data;
  664. }
  665. #endif /* CONFIG_PNP */
  666. static void snd_card_opti9xx_free(struct snd_card *card)
  667. {
  668. struct snd_opti9xx *chip = card->private_data;
  669. if (chip) {
  670. #ifdef OPTi93X
  671. struct snd_wss *codec = chip->codec;
  672. if (codec && codec->irq > 0) {
  673. disable_irq(codec->irq);
  674. free_irq(codec->irq, codec);
  675. }
  676. release_and_free_resource(chip->res_mc_indir);
  677. #endif
  678. release_and_free_resource(chip->res_mc_base);
  679. }
  680. }
  681. static int __devinit snd_opti9xx_probe(struct snd_card *card)
  682. {
  683. static long possible_ports[] = {0x530, 0xe80, 0xf40, 0x604, -1};
  684. int error;
  685. int xdma2;
  686. struct snd_opti9xx *chip = card->private_data;
  687. struct snd_wss *codec;
  688. #ifdef CS4231
  689. struct snd_timer *timer;
  690. #endif
  691. struct snd_pcm *pcm;
  692. struct snd_rawmidi *rmidi;
  693. struct snd_hwdep *synth;
  694. #if defined(CS4231) || defined(OPTi93X)
  695. xdma2 = dma2;
  696. #else
  697. xdma2 = -1;
  698. #endif
  699. if (port == SNDRV_AUTO_PORT) {
  700. port = snd_legacy_find_free_ioport(possible_ports, 4);
  701. if (port < 0) {
  702. snd_printk(KERN_ERR "unable to find a free WSS port\n");
  703. return -EBUSY;
  704. }
  705. }
  706. error = snd_opti9xx_configure(chip, port, irq, dma1, xdma2,
  707. mpu_port, mpu_irq);
  708. if (error)
  709. return error;
  710. error = snd_wss_create(card, port + 4, -1, irq, dma1, xdma2,
  711. #ifdef OPTi93X
  712. WSS_HW_OPTI93X, WSS_HWSHARE_IRQ,
  713. #else
  714. WSS_HW_DETECT, 0,
  715. #endif
  716. &codec);
  717. if (error < 0)
  718. return error;
  719. #ifdef OPTi93X
  720. chip->codec = codec;
  721. #endif
  722. error = snd_wss_pcm(codec, 0, &pcm);
  723. if (error < 0)
  724. return error;
  725. error = snd_wss_mixer(codec);
  726. if (error < 0)
  727. return error;
  728. #ifdef OPTi93X
  729. error = snd_opti93x_mixer(codec);
  730. if (error < 0)
  731. return error;
  732. #endif
  733. #ifdef CS4231
  734. error = snd_wss_timer(codec, 0, &timer);
  735. if (error < 0)
  736. return error;
  737. #endif
  738. #ifdef OPTi93X
  739. error = request_irq(irq, snd_opti93x_interrupt,
  740. IRQF_DISABLED, DEV_NAME" - WSS", codec);
  741. if (error < 0) {
  742. snd_printk(KERN_ERR "opti9xx: can't grab IRQ %d\n", chip->irq);
  743. return error;
  744. }
  745. #endif
  746. chip->irq = irq;
  747. strcpy(card->driver, chip->name);
  748. sprintf(card->shortname, "OPTi %s", card->driver);
  749. #if defined(CS4231) || defined(OPTi93X)
  750. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
  751. card->shortname, pcm->name, port + 4, irq, dma1, xdma2);
  752. #else
  753. sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d",
  754. card->shortname, pcm->name, port + 4, irq, dma1);
  755. #endif /* CS4231 || OPTi93X */
  756. if (mpu_port <= 0 || mpu_port == SNDRV_AUTO_PORT)
  757. rmidi = NULL;
  758. else {
  759. error = snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
  760. mpu_port, 0, mpu_irq, IRQF_DISABLED, &rmidi);
  761. if (error)
  762. snd_printk(KERN_WARNING "no MPU-401 device at 0x%lx?\n",
  763. mpu_port);
  764. }
  765. if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
  766. struct snd_opl3 *opl3 = NULL;
  767. #ifndef OPTi93X
  768. if (chip->hardware == OPTi9XX_HW_82C928 ||
  769. chip->hardware == OPTi9XX_HW_82C929 ||
  770. chip->hardware == OPTi9XX_HW_82C924) {
  771. struct snd_opl4 *opl4;
  772. /* assume we have an OPL4 */
  773. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  774. 0x20, 0x20);
  775. if (snd_opl4_create(card, fm_port, fm_port - 8,
  776. 2, &opl3, &opl4) < 0) {
  777. /* no luck, use OPL3 instead */
  778. snd_opti9xx_write_mask(chip, OPTi9XX_MC_REG(2),
  779. 0x00, 0x20);
  780. }
  781. }
  782. #endif /* !OPTi93X */
  783. if (!opl3 && snd_opl3_create(card, fm_port, fm_port + 2,
  784. OPL3_HW_AUTO, 0, &opl3) < 0) {
  785. snd_printk(KERN_WARNING "no OPL device at 0x%lx-0x%lx\n",
  786. fm_port, fm_port + 4 - 1);
  787. }
  788. if (opl3) {
  789. error = snd_opl3_hwdep_new(opl3, 0, 1, &synth);
  790. if (error < 0)
  791. return error;
  792. }
  793. }
  794. return snd_card_register(card);
  795. }
  796. static int snd_opti9xx_card_new(struct snd_card **cardp)
  797. {
  798. struct snd_card *card;
  799. int err;
  800. err = snd_card_create(index, id, THIS_MODULE,
  801. sizeof(struct snd_opti9xx), &card);
  802. if (err < 0)
  803. return err;
  804. card->private_free = snd_card_opti9xx_free;
  805. *cardp = card;
  806. return 0;
  807. }
  808. static int __devinit snd_opti9xx_isa_match(struct device *devptr,
  809. unsigned int dev)
  810. {
  811. #ifdef CONFIG_PNP
  812. if (snd_opti9xx_pnp_is_probed)
  813. return 0;
  814. if (isapnp)
  815. return 0;
  816. #endif
  817. return 1;
  818. }
  819. static int __devinit snd_opti9xx_isa_probe(struct device *devptr,
  820. unsigned int dev)
  821. {
  822. struct snd_card *card;
  823. int error;
  824. static long possible_mpu_ports[] = {0x300, 0x310, 0x320, 0x330, -1};
  825. #ifdef OPTi93X
  826. static int possible_irqs[] = {5, 9, 10, 11, 7, -1};
  827. #else
  828. static int possible_irqs[] = {9, 10, 11, 7, -1};
  829. #endif /* OPTi93X */
  830. static int possible_mpu_irqs[] = {5, 9, 10, 7, -1};
  831. static int possible_dma1s[] = {3, 1, 0, -1};
  832. #if defined(CS4231) || defined(OPTi93X)
  833. static int possible_dma2s[][2] = {{1,-1}, {0,-1}, {-1,-1}, {0,-1}};
  834. #endif /* CS4231 || OPTi93X */
  835. if (mpu_port == SNDRV_AUTO_PORT) {
  836. if ((mpu_port = snd_legacy_find_free_ioport(possible_mpu_ports, 2)) < 0) {
  837. snd_printk(KERN_ERR "unable to find a free MPU401 port\n");
  838. return -EBUSY;
  839. }
  840. }
  841. if (irq == SNDRV_AUTO_IRQ) {
  842. if ((irq = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  843. snd_printk(KERN_ERR "unable to find a free IRQ\n");
  844. return -EBUSY;
  845. }
  846. }
  847. if (mpu_irq == SNDRV_AUTO_IRQ) {
  848. if ((mpu_irq = snd_legacy_find_free_irq(possible_mpu_irqs)) < 0) {
  849. snd_printk(KERN_ERR "unable to find a free MPU401 IRQ\n");
  850. return -EBUSY;
  851. }
  852. }
  853. if (dma1 == SNDRV_AUTO_DMA) {
  854. if ((dma1 = snd_legacy_find_free_dma(possible_dma1s)) < 0) {
  855. snd_printk(KERN_ERR "unable to find a free DMA1\n");
  856. return -EBUSY;
  857. }
  858. }
  859. #if defined(CS4231) || defined(OPTi93X)
  860. if (dma2 == SNDRV_AUTO_DMA) {
  861. if ((dma2 = snd_legacy_find_free_dma(possible_dma2s[dma1 % 4])) < 0) {
  862. snd_printk(KERN_ERR "unable to find a free DMA2\n");
  863. return -EBUSY;
  864. }
  865. }
  866. #endif
  867. error = snd_opti9xx_card_new(&card);
  868. if (error < 0)
  869. return error;
  870. if ((error = snd_card_opti9xx_detect(card, card->private_data)) < 0) {
  871. snd_card_free(card);
  872. return error;
  873. }
  874. snd_card_set_dev(card, devptr);
  875. if ((error = snd_opti9xx_probe(card)) < 0) {
  876. snd_card_free(card);
  877. return error;
  878. }
  879. dev_set_drvdata(devptr, card);
  880. return 0;
  881. }
  882. static int __devexit snd_opti9xx_isa_remove(struct device *devptr,
  883. unsigned int dev)
  884. {
  885. snd_card_free(dev_get_drvdata(devptr));
  886. dev_set_drvdata(devptr, NULL);
  887. return 0;
  888. }
  889. static struct isa_driver snd_opti9xx_driver = {
  890. .match = snd_opti9xx_isa_match,
  891. .probe = snd_opti9xx_isa_probe,
  892. .remove = __devexit_p(snd_opti9xx_isa_remove),
  893. /* FIXME: suspend/resume */
  894. .driver = {
  895. .name = DEV_NAME
  896. },
  897. };
  898. #ifdef CONFIG_PNP
  899. static int __devinit snd_opti9xx_pnp_probe(struct pnp_card_link *pcard,
  900. const struct pnp_card_device_id *pid)
  901. {
  902. struct snd_card *card;
  903. int error, hw;
  904. struct snd_opti9xx *chip;
  905. if (snd_opti9xx_pnp_is_probed)
  906. return -EBUSY;
  907. if (! isapnp)
  908. return -ENODEV;
  909. error = snd_opti9xx_card_new(&card);
  910. if (error < 0)
  911. return error;
  912. chip = card->private_data;
  913. hw = snd_card_opti9xx_pnp(chip, pcard, pid);
  914. switch (hw) {
  915. case 0x0924:
  916. hw = OPTi9XX_HW_82C924;
  917. break;
  918. case 0x0925:
  919. hw = OPTi9XX_HW_82C925;
  920. break;
  921. case 0x0931:
  922. hw = OPTi9XX_HW_82C931;
  923. break;
  924. default:
  925. snd_card_free(card);
  926. return -ENODEV;
  927. }
  928. if ((error = snd_opti9xx_init(chip, hw))) {
  929. snd_card_free(card);
  930. return error;
  931. }
  932. if (hw <= OPTi9XX_HW_82C930)
  933. chip->mc_base -= 0x80;
  934. error = snd_opti9xx_read_check(chip);
  935. if (error) {
  936. snd_printk(KERN_ERR "OPTI chip not found\n");
  937. snd_card_free(card);
  938. return error;
  939. }
  940. snd_card_set_dev(card, &pcard->card->dev);
  941. if ((error = snd_opti9xx_probe(card)) < 0) {
  942. snd_card_free(card);
  943. return error;
  944. }
  945. pnp_set_card_drvdata(pcard, card);
  946. snd_opti9xx_pnp_is_probed = 1;
  947. return 0;
  948. }
  949. static void __devexit snd_opti9xx_pnp_remove(struct pnp_card_link * pcard)
  950. {
  951. snd_card_free(pnp_get_card_drvdata(pcard));
  952. pnp_set_card_drvdata(pcard, NULL);
  953. snd_opti9xx_pnp_is_probed = 0;
  954. }
  955. static struct pnp_card_driver opti9xx_pnpc_driver = {
  956. .flags = PNP_DRIVER_RES_DISABLE,
  957. .name = "opti9xx",
  958. .id_table = snd_opti9xx_pnpids,
  959. .probe = snd_opti9xx_pnp_probe,
  960. .remove = __devexit_p(snd_opti9xx_pnp_remove),
  961. };
  962. #endif
  963. #ifdef OPTi93X
  964. #define CHIP_NAME "82C93x"
  965. #else
  966. #define CHIP_NAME "82C92x"
  967. #endif
  968. static int __init alsa_card_opti9xx_init(void)
  969. {
  970. #ifdef CONFIG_PNP
  971. pnp_register_card_driver(&opti9xx_pnpc_driver);
  972. if (snd_opti9xx_pnp_is_probed)
  973. return 0;
  974. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  975. #endif
  976. return isa_register_driver(&snd_opti9xx_driver, 1);
  977. }
  978. static void __exit alsa_card_opti9xx_exit(void)
  979. {
  980. if (!snd_opti9xx_pnp_is_probed) {
  981. isa_unregister_driver(&snd_opti9xx_driver);
  982. return;
  983. }
  984. #ifdef CONFIG_PNP
  985. pnp_unregister_card_driver(&opti9xx_pnpc_driver);
  986. #endif
  987. }
  988. module_init(alsa_card_opti9xx_init)
  989. module_exit(alsa_card_opti9xx_exit)