sonicvibes.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for S3 SonicVibes soundcard
  4. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  5. *
  6. * BUGS:
  7. * It looks like 86c617 rev 3 doesn't supports DDMA buffers above 16MB?
  8. * Driver sometimes hangs... Nobody knows why at this moment...
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pci.h>
  14. #include <linux/slab.h>
  15. #include <linux/gameport.h>
  16. #include <linux/module.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/io.h>
  19. #include <sound/core.h>
  20. #include <sound/pcm.h>
  21. #include <sound/info.h>
  22. #include <sound/control.h>
  23. #include <sound/mpu401.h>
  24. #include <sound/opl3.h>
  25. #include <sound/initval.h>
  26. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  27. MODULE_DESCRIPTION("S3 SonicVibes PCI");
  28. MODULE_LICENSE("GPL");
  29. MODULE_SUPPORTED_DEVICE("{{S3,SonicVibes PCI}}");
  30. #if IS_REACHABLE(CONFIG_GAMEPORT)
  31. #define SUPPORT_JOYSTICK 1
  32. #endif
  33. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  34. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  35. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  36. static bool reverb[SNDRV_CARDS];
  37. static bool mge[SNDRV_CARDS];
  38. static unsigned int dmaio = 0x7a00; /* DDMA i/o address */
  39. module_param_array(index, int, NULL, 0444);
  40. MODULE_PARM_DESC(index, "Index value for S3 SonicVibes soundcard.");
  41. module_param_array(id, charp, NULL, 0444);
  42. MODULE_PARM_DESC(id, "ID string for S3 SonicVibes soundcard.");
  43. module_param_array(enable, bool, NULL, 0444);
  44. MODULE_PARM_DESC(enable, "Enable S3 SonicVibes soundcard.");
  45. module_param_array(reverb, bool, NULL, 0444);
  46. MODULE_PARM_DESC(reverb, "Enable reverb (SRAM is present) for S3 SonicVibes soundcard.");
  47. module_param_array(mge, bool, NULL, 0444);
  48. MODULE_PARM_DESC(mge, "MIC Gain Enable for S3 SonicVibes soundcard.");
  49. module_param_hw(dmaio, uint, ioport, 0444);
  50. MODULE_PARM_DESC(dmaio, "DDMA i/o base address for S3 SonicVibes soundcard.");
  51. /*
  52. * Enhanced port direct registers
  53. */
  54. #define SV_REG(sonic, x) ((sonic)->enh_port + SV_REG_##x)
  55. #define SV_REG_CONTROL 0x00 /* R/W: CODEC/Mixer control register */
  56. #define SV_ENHANCED 0x01 /* audio mode select - enhanced mode */
  57. #define SV_TEST 0x02 /* test bit */
  58. #define SV_REVERB 0x04 /* reverb enable */
  59. #define SV_WAVETABLE 0x08 /* wavetable active / FM active if not set */
  60. #define SV_INTA 0x20 /* INTA driving - should be always 1 */
  61. #define SV_RESET 0x80 /* reset chip */
  62. #define SV_REG_IRQMASK 0x01 /* R/W: CODEC/Mixer interrupt mask register */
  63. #define SV_DMAA_MASK 0x01 /* mask DMA-A interrupt */
  64. #define SV_DMAC_MASK 0x04 /* mask DMA-C interrupt */
  65. #define SV_SPEC_MASK 0x08 /* special interrupt mask - should be always masked */
  66. #define SV_UD_MASK 0x40 /* Up/Down button interrupt mask */
  67. #define SV_MIDI_MASK 0x80 /* mask MIDI interrupt */
  68. #define SV_REG_STATUS 0x02 /* R/O: CODEC/Mixer status register */
  69. #define SV_DMAA_IRQ 0x01 /* DMA-A interrupt */
  70. #define SV_DMAC_IRQ 0x04 /* DMA-C interrupt */
  71. #define SV_SPEC_IRQ 0x08 /* special interrupt */
  72. #define SV_UD_IRQ 0x40 /* Up/Down interrupt */
  73. #define SV_MIDI_IRQ 0x80 /* MIDI interrupt */
  74. #define SV_REG_INDEX 0x04 /* R/W: CODEC/Mixer index address register */
  75. #define SV_MCE 0x40 /* mode change enable */
  76. #define SV_TRD 0x80 /* DMA transfer request disabled */
  77. #define SV_REG_DATA 0x05 /* R/W: CODEC/Mixer index data register */
  78. /*
  79. * Enhanced port indirect registers
  80. */
  81. #define SV_IREG_LEFT_ADC 0x00 /* Left ADC Input Control */
  82. #define SV_IREG_RIGHT_ADC 0x01 /* Right ADC Input Control */
  83. #define SV_IREG_LEFT_AUX1 0x02 /* Left AUX1 Input Control */
  84. #define SV_IREG_RIGHT_AUX1 0x03 /* Right AUX1 Input Control */
  85. #define SV_IREG_LEFT_CD 0x04 /* Left CD Input Control */
  86. #define SV_IREG_RIGHT_CD 0x05 /* Right CD Input Control */
  87. #define SV_IREG_LEFT_LINE 0x06 /* Left Line Input Control */
  88. #define SV_IREG_RIGHT_LINE 0x07 /* Right Line Input Control */
  89. #define SV_IREG_MIC 0x08 /* MIC Input Control */
  90. #define SV_IREG_GAME_PORT 0x09 /* Game Port Control */
  91. #define SV_IREG_LEFT_SYNTH 0x0a /* Left Synth Input Control */
  92. #define SV_IREG_RIGHT_SYNTH 0x0b /* Right Synth Input Control */
  93. #define SV_IREG_LEFT_AUX2 0x0c /* Left AUX2 Input Control */
  94. #define SV_IREG_RIGHT_AUX2 0x0d /* Right AUX2 Input Control */
  95. #define SV_IREG_LEFT_ANALOG 0x0e /* Left Analog Mixer Output Control */
  96. #define SV_IREG_RIGHT_ANALOG 0x0f /* Right Analog Mixer Output Control */
  97. #define SV_IREG_LEFT_PCM 0x10 /* Left PCM Input Control */
  98. #define SV_IREG_RIGHT_PCM 0x11 /* Right PCM Input Control */
  99. #define SV_IREG_DMA_DATA_FMT 0x12 /* DMA Data Format */
  100. #define SV_IREG_PC_ENABLE 0x13 /* Playback/Capture Enable Register */
  101. #define SV_IREG_UD_BUTTON 0x14 /* Up/Down Button Register */
  102. #define SV_IREG_REVISION 0x15 /* Revision */
  103. #define SV_IREG_ADC_OUTPUT_CTRL 0x16 /* ADC Output Control */
  104. #define SV_IREG_DMA_A_UPPER 0x18 /* DMA A Upper Base Count */
  105. #define SV_IREG_DMA_A_LOWER 0x19 /* DMA A Lower Base Count */
  106. #define SV_IREG_DMA_C_UPPER 0x1c /* DMA C Upper Base Count */
  107. #define SV_IREG_DMA_C_LOWER 0x1d /* DMA C Lower Base Count */
  108. #define SV_IREG_PCM_RATE_LOW 0x1e /* PCM Sampling Rate Low Byte */
  109. #define SV_IREG_PCM_RATE_HIGH 0x1f /* PCM Sampling Rate High Byte */
  110. #define SV_IREG_SYNTH_RATE_LOW 0x20 /* Synthesizer Sampling Rate Low Byte */
  111. #define SV_IREG_SYNTH_RATE_HIGH 0x21 /* Synthesizer Sampling Rate High Byte */
  112. #define SV_IREG_ADC_CLOCK 0x22 /* ADC Clock Source Selection */
  113. #define SV_IREG_ADC_ALT_RATE 0x23 /* ADC Alternative Sampling Rate Selection */
  114. #define SV_IREG_ADC_PLL_M 0x24 /* ADC PLL M Register */
  115. #define SV_IREG_ADC_PLL_N 0x25 /* ADC PLL N Register */
  116. #define SV_IREG_SYNTH_PLL_M 0x26 /* Synthesizer PLL M Register */
  117. #define SV_IREG_SYNTH_PLL_N 0x27 /* Synthesizer PLL N Register */
  118. #define SV_IREG_MPU401 0x2a /* MPU-401 UART Operation */
  119. #define SV_IREG_DRIVE_CTRL 0x2b /* Drive Control */
  120. #define SV_IREG_SRS_SPACE 0x2c /* SRS Space Control */
  121. #define SV_IREG_SRS_CENTER 0x2d /* SRS Center Control */
  122. #define SV_IREG_WAVE_SOURCE 0x2e /* Wavetable Sample Source Select */
  123. #define SV_IREG_ANALOG_POWER 0x30 /* Analog Power Down Control */
  124. #define SV_IREG_DIGITAL_POWER 0x31 /* Digital Power Down Control */
  125. #define SV_IREG_ADC_PLL SV_IREG_ADC_PLL_M
  126. #define SV_IREG_SYNTH_PLL SV_IREG_SYNTH_PLL_M
  127. /*
  128. * DMA registers
  129. */
  130. #define SV_DMA_ADDR0 0x00
  131. #define SV_DMA_ADDR1 0x01
  132. #define SV_DMA_ADDR2 0x02
  133. #define SV_DMA_ADDR3 0x03
  134. #define SV_DMA_COUNT0 0x04
  135. #define SV_DMA_COUNT1 0x05
  136. #define SV_DMA_COUNT2 0x06
  137. #define SV_DMA_MODE 0x0b
  138. #define SV_DMA_RESET 0x0d
  139. #define SV_DMA_MASK 0x0f
  140. /*
  141. * Record sources
  142. */
  143. #define SV_RECSRC_RESERVED (0x00<<5)
  144. #define SV_RECSRC_CD (0x01<<5)
  145. #define SV_RECSRC_DAC (0x02<<5)
  146. #define SV_RECSRC_AUX2 (0x03<<5)
  147. #define SV_RECSRC_LINE (0x04<<5)
  148. #define SV_RECSRC_AUX1 (0x05<<5)
  149. #define SV_RECSRC_MIC (0x06<<5)
  150. #define SV_RECSRC_OUT (0x07<<5)
  151. /*
  152. * constants
  153. */
  154. #define SV_FULLRATE 48000
  155. #define SV_REFFREQUENCY 24576000
  156. #define SV_ADCMULT 512
  157. #define SV_MODE_PLAY 1
  158. #define SV_MODE_CAPTURE 2
  159. /*
  160. */
  161. struct sonicvibes {
  162. unsigned long dma1size;
  163. unsigned long dma2size;
  164. int irq;
  165. unsigned long sb_port;
  166. unsigned long enh_port;
  167. unsigned long synth_port;
  168. unsigned long midi_port;
  169. unsigned long game_port;
  170. unsigned int dmaa_port;
  171. struct resource *res_dmaa;
  172. unsigned int dmac_port;
  173. struct resource *res_dmac;
  174. unsigned char enable;
  175. unsigned char irqmask;
  176. unsigned char revision;
  177. unsigned char format;
  178. unsigned char srs_space;
  179. unsigned char srs_center;
  180. unsigned char mpu_switch;
  181. unsigned char wave_source;
  182. unsigned int mode;
  183. struct pci_dev *pci;
  184. struct snd_card *card;
  185. struct snd_pcm *pcm;
  186. struct snd_pcm_substream *playback_substream;
  187. struct snd_pcm_substream *capture_substream;
  188. struct snd_rawmidi *rmidi;
  189. struct snd_hwdep *fmsynth; /* S3FM */
  190. spinlock_t reg_lock;
  191. unsigned int p_dma_size;
  192. unsigned int c_dma_size;
  193. struct snd_kcontrol *master_mute;
  194. struct snd_kcontrol *master_volume;
  195. #ifdef SUPPORT_JOYSTICK
  196. struct gameport *gameport;
  197. #endif
  198. };
  199. static const struct pci_device_id snd_sonic_ids[] = {
  200. { PCI_VDEVICE(S3, 0xca00), 0, },
  201. { 0, }
  202. };
  203. MODULE_DEVICE_TABLE(pci, snd_sonic_ids);
  204. static const struct snd_ratden sonicvibes_adc_clock = {
  205. .num_min = 4000 * 65536,
  206. .num_max = 48000UL * 65536,
  207. .num_step = 1,
  208. .den = 65536,
  209. };
  210. static const struct snd_pcm_hw_constraint_ratdens snd_sonicvibes_hw_constraints_adc_clock = {
  211. .nrats = 1,
  212. .rats = &sonicvibes_adc_clock,
  213. };
  214. /*
  215. * common I/O routines
  216. */
  217. static inline void snd_sonicvibes_setdmaa(struct sonicvibes * sonic,
  218. unsigned int addr,
  219. unsigned int count)
  220. {
  221. count--;
  222. outl(addr, sonic->dmaa_port + SV_DMA_ADDR0);
  223. outl(count, sonic->dmaa_port + SV_DMA_COUNT0);
  224. outb(0x18, sonic->dmaa_port + SV_DMA_MODE);
  225. #if 0
  226. dev_dbg(sonic->card->dev, "program dmaa: addr = 0x%x, paddr = 0x%x\n",
  227. addr, inl(sonic->dmaa_port + SV_DMA_ADDR0));
  228. #endif
  229. }
  230. static inline void snd_sonicvibes_setdmac(struct sonicvibes * sonic,
  231. unsigned int addr,
  232. unsigned int count)
  233. {
  234. /* note: dmac is working in word mode!!! */
  235. count >>= 1;
  236. count--;
  237. outl(addr, sonic->dmac_port + SV_DMA_ADDR0);
  238. outl(count, sonic->dmac_port + SV_DMA_COUNT0);
  239. outb(0x14, sonic->dmac_port + SV_DMA_MODE);
  240. #if 0
  241. dev_dbg(sonic->card->dev, "program dmac: addr = 0x%x, paddr = 0x%x\n",
  242. addr, inl(sonic->dmac_port + SV_DMA_ADDR0));
  243. #endif
  244. }
  245. static inline unsigned int snd_sonicvibes_getdmaa(struct sonicvibes * sonic)
  246. {
  247. return (inl(sonic->dmaa_port + SV_DMA_COUNT0) & 0xffffff) + 1;
  248. }
  249. static inline unsigned int snd_sonicvibes_getdmac(struct sonicvibes * sonic)
  250. {
  251. /* note: dmac is working in word mode!!! */
  252. return ((inl(sonic->dmac_port + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
  253. }
  254. static void snd_sonicvibes_out1(struct sonicvibes * sonic,
  255. unsigned char reg,
  256. unsigned char value)
  257. {
  258. outb(reg, SV_REG(sonic, INDEX));
  259. udelay(10);
  260. outb(value, SV_REG(sonic, DATA));
  261. udelay(10);
  262. }
  263. static void snd_sonicvibes_out(struct sonicvibes * sonic,
  264. unsigned char reg,
  265. unsigned char value)
  266. {
  267. unsigned long flags;
  268. spin_lock_irqsave(&sonic->reg_lock, flags);
  269. outb(reg, SV_REG(sonic, INDEX));
  270. udelay(10);
  271. outb(value, SV_REG(sonic, DATA));
  272. udelay(10);
  273. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  274. }
  275. static unsigned char snd_sonicvibes_in1(struct sonicvibes * sonic, unsigned char reg)
  276. {
  277. unsigned char value;
  278. outb(reg, SV_REG(sonic, INDEX));
  279. udelay(10);
  280. value = inb(SV_REG(sonic, DATA));
  281. udelay(10);
  282. return value;
  283. }
  284. static unsigned char snd_sonicvibes_in(struct sonicvibes * sonic, unsigned char reg)
  285. {
  286. unsigned long flags;
  287. unsigned char value;
  288. spin_lock_irqsave(&sonic->reg_lock, flags);
  289. outb(reg, SV_REG(sonic, INDEX));
  290. udelay(10);
  291. value = inb(SV_REG(sonic, DATA));
  292. udelay(10);
  293. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  294. return value;
  295. }
  296. #if 0
  297. static void snd_sonicvibes_debug(struct sonicvibes * sonic)
  298. {
  299. dev_dbg(sonic->card->dev,
  300. "SV REGS: INDEX = 0x%02x STATUS = 0x%02x\n",
  301. inb(SV_REG(sonic, INDEX)), inb(SV_REG(sonic, STATUS)));
  302. dev_dbg(sonic->card->dev,
  303. " 0x00: left input = 0x%02x 0x20: synth rate low = 0x%02x\n",
  304. snd_sonicvibes_in(sonic, 0x00), snd_sonicvibes_in(sonic, 0x20));
  305. dev_dbg(sonic->card->dev,
  306. " 0x01: right input = 0x%02x 0x21: synth rate high = 0x%02x\n",
  307. snd_sonicvibes_in(sonic, 0x01), snd_sonicvibes_in(sonic, 0x21));
  308. dev_dbg(sonic->card->dev,
  309. " 0x02: left AUX1 = 0x%02x 0x22: ADC clock = 0x%02x\n",
  310. snd_sonicvibes_in(sonic, 0x02), snd_sonicvibes_in(sonic, 0x22));
  311. dev_dbg(sonic->card->dev,
  312. " 0x03: right AUX1 = 0x%02x 0x23: ADC alt rate = 0x%02x\n",
  313. snd_sonicvibes_in(sonic, 0x03), snd_sonicvibes_in(sonic, 0x23));
  314. dev_dbg(sonic->card->dev,
  315. " 0x04: left CD = 0x%02x 0x24: ADC pll M = 0x%02x\n",
  316. snd_sonicvibes_in(sonic, 0x04), snd_sonicvibes_in(sonic, 0x24));
  317. dev_dbg(sonic->card->dev,
  318. " 0x05: right CD = 0x%02x 0x25: ADC pll N = 0x%02x\n",
  319. snd_sonicvibes_in(sonic, 0x05), snd_sonicvibes_in(sonic, 0x25));
  320. dev_dbg(sonic->card->dev,
  321. " 0x06: left line = 0x%02x 0x26: Synth pll M = 0x%02x\n",
  322. snd_sonicvibes_in(sonic, 0x06), snd_sonicvibes_in(sonic, 0x26));
  323. dev_dbg(sonic->card->dev,
  324. " 0x07: right line = 0x%02x 0x27: Synth pll N = 0x%02x\n",
  325. snd_sonicvibes_in(sonic, 0x07), snd_sonicvibes_in(sonic, 0x27));
  326. dev_dbg(sonic->card->dev,
  327. " 0x08: MIC = 0x%02x 0x28: --- = 0x%02x\n",
  328. snd_sonicvibes_in(sonic, 0x08), snd_sonicvibes_in(sonic, 0x28));
  329. dev_dbg(sonic->card->dev,
  330. " 0x09: Game port = 0x%02x 0x29: --- = 0x%02x\n",
  331. snd_sonicvibes_in(sonic, 0x09), snd_sonicvibes_in(sonic, 0x29));
  332. dev_dbg(sonic->card->dev,
  333. " 0x0a: left synth = 0x%02x 0x2a: MPU401 = 0x%02x\n",
  334. snd_sonicvibes_in(sonic, 0x0a), snd_sonicvibes_in(sonic, 0x2a));
  335. dev_dbg(sonic->card->dev,
  336. " 0x0b: right synth = 0x%02x 0x2b: drive ctrl = 0x%02x\n",
  337. snd_sonicvibes_in(sonic, 0x0b), snd_sonicvibes_in(sonic, 0x2b));
  338. dev_dbg(sonic->card->dev,
  339. " 0x0c: left AUX2 = 0x%02x 0x2c: SRS space = 0x%02x\n",
  340. snd_sonicvibes_in(sonic, 0x0c), snd_sonicvibes_in(sonic, 0x2c));
  341. dev_dbg(sonic->card->dev,
  342. " 0x0d: right AUX2 = 0x%02x 0x2d: SRS center = 0x%02x\n",
  343. snd_sonicvibes_in(sonic, 0x0d), snd_sonicvibes_in(sonic, 0x2d));
  344. dev_dbg(sonic->card->dev,
  345. " 0x0e: left analog = 0x%02x 0x2e: wave source = 0x%02x\n",
  346. snd_sonicvibes_in(sonic, 0x0e), snd_sonicvibes_in(sonic, 0x2e));
  347. dev_dbg(sonic->card->dev,
  348. " 0x0f: right analog = 0x%02x 0x2f: --- = 0x%02x\n",
  349. snd_sonicvibes_in(sonic, 0x0f), snd_sonicvibes_in(sonic, 0x2f));
  350. dev_dbg(sonic->card->dev,
  351. " 0x10: left PCM = 0x%02x 0x30: analog power = 0x%02x\n",
  352. snd_sonicvibes_in(sonic, 0x10), snd_sonicvibes_in(sonic, 0x30));
  353. dev_dbg(sonic->card->dev,
  354. " 0x11: right PCM = 0x%02x 0x31: analog power = 0x%02x\n",
  355. snd_sonicvibes_in(sonic, 0x11), snd_sonicvibes_in(sonic, 0x31));
  356. dev_dbg(sonic->card->dev,
  357. " 0x12: DMA data format = 0x%02x 0x32: --- = 0x%02x\n",
  358. snd_sonicvibes_in(sonic, 0x12), snd_sonicvibes_in(sonic, 0x32));
  359. dev_dbg(sonic->card->dev,
  360. " 0x13: P/C enable = 0x%02x 0x33: --- = 0x%02x\n",
  361. snd_sonicvibes_in(sonic, 0x13), snd_sonicvibes_in(sonic, 0x33));
  362. dev_dbg(sonic->card->dev,
  363. " 0x14: U/D button = 0x%02x 0x34: --- = 0x%02x\n",
  364. snd_sonicvibes_in(sonic, 0x14), snd_sonicvibes_in(sonic, 0x34));
  365. dev_dbg(sonic->card->dev,
  366. " 0x15: revision = 0x%02x 0x35: --- = 0x%02x\n",
  367. snd_sonicvibes_in(sonic, 0x15), snd_sonicvibes_in(sonic, 0x35));
  368. dev_dbg(sonic->card->dev,
  369. " 0x16: ADC output ctrl = 0x%02x 0x36: --- = 0x%02x\n",
  370. snd_sonicvibes_in(sonic, 0x16), snd_sonicvibes_in(sonic, 0x36));
  371. dev_dbg(sonic->card->dev,
  372. " 0x17: --- = 0x%02x 0x37: --- = 0x%02x\n",
  373. snd_sonicvibes_in(sonic, 0x17), snd_sonicvibes_in(sonic, 0x37));
  374. dev_dbg(sonic->card->dev,
  375. " 0x18: DMA A upper cnt = 0x%02x 0x38: --- = 0x%02x\n",
  376. snd_sonicvibes_in(sonic, 0x18), snd_sonicvibes_in(sonic, 0x38));
  377. dev_dbg(sonic->card->dev,
  378. " 0x19: DMA A lower cnt = 0x%02x 0x39: --- = 0x%02x\n",
  379. snd_sonicvibes_in(sonic, 0x19), snd_sonicvibes_in(sonic, 0x39));
  380. dev_dbg(sonic->card->dev,
  381. " 0x1a: --- = 0x%02x 0x3a: --- = 0x%02x\n",
  382. snd_sonicvibes_in(sonic, 0x1a), snd_sonicvibes_in(sonic, 0x3a));
  383. dev_dbg(sonic->card->dev,
  384. " 0x1b: --- = 0x%02x 0x3b: --- = 0x%02x\n",
  385. snd_sonicvibes_in(sonic, 0x1b), snd_sonicvibes_in(sonic, 0x3b));
  386. dev_dbg(sonic->card->dev,
  387. " 0x1c: DMA C upper cnt = 0x%02x 0x3c: --- = 0x%02x\n",
  388. snd_sonicvibes_in(sonic, 0x1c), snd_sonicvibes_in(sonic, 0x3c));
  389. dev_dbg(sonic->card->dev,
  390. " 0x1d: DMA C upper cnt = 0x%02x 0x3d: --- = 0x%02x\n",
  391. snd_sonicvibes_in(sonic, 0x1d), snd_sonicvibes_in(sonic, 0x3d));
  392. dev_dbg(sonic->card->dev,
  393. " 0x1e: PCM rate low = 0x%02x 0x3e: --- = 0x%02x\n",
  394. snd_sonicvibes_in(sonic, 0x1e), snd_sonicvibes_in(sonic, 0x3e));
  395. dev_dbg(sonic->card->dev,
  396. " 0x1f: PCM rate high = 0x%02x 0x3f: --- = 0x%02x\n",
  397. snd_sonicvibes_in(sonic, 0x1f), snd_sonicvibes_in(sonic, 0x3f));
  398. }
  399. #endif
  400. static void snd_sonicvibes_setfmt(struct sonicvibes * sonic,
  401. unsigned char mask,
  402. unsigned char value)
  403. {
  404. unsigned long flags;
  405. spin_lock_irqsave(&sonic->reg_lock, flags);
  406. outb(SV_MCE | SV_IREG_DMA_DATA_FMT, SV_REG(sonic, INDEX));
  407. if (mask) {
  408. sonic->format = inb(SV_REG(sonic, DATA));
  409. udelay(10);
  410. }
  411. sonic->format = (sonic->format & mask) | value;
  412. outb(sonic->format, SV_REG(sonic, DATA));
  413. udelay(10);
  414. outb(0, SV_REG(sonic, INDEX));
  415. udelay(10);
  416. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  417. }
  418. static void snd_sonicvibes_pll(unsigned int rate,
  419. unsigned int *res_r,
  420. unsigned int *res_m,
  421. unsigned int *res_n)
  422. {
  423. unsigned int r, m = 0, n = 0;
  424. unsigned int xm, xn, xr, xd, metric = ~0U;
  425. if (rate < 625000 / SV_ADCMULT)
  426. rate = 625000 / SV_ADCMULT;
  427. if (rate > 150000000 / SV_ADCMULT)
  428. rate = 150000000 / SV_ADCMULT;
  429. /* slight violation of specs, needed for continuous sampling rates */
  430. for (r = 0; rate < 75000000 / SV_ADCMULT; r += 0x20, rate <<= 1);
  431. for (xn = 3; xn < 33; xn++) /* 35 */
  432. for (xm = 3; xm < 257; xm++) {
  433. xr = ((SV_REFFREQUENCY / SV_ADCMULT) * xm) / xn;
  434. if (xr >= rate)
  435. xd = xr - rate;
  436. else
  437. xd = rate - xr;
  438. if (xd < metric) {
  439. metric = xd;
  440. m = xm - 2;
  441. n = xn - 2;
  442. }
  443. }
  444. *res_r = r;
  445. *res_m = m;
  446. *res_n = n;
  447. #if 0
  448. dev_dbg(sonic->card->dev,
  449. "metric = %i, xm = %i, xn = %i\n", metric, xm, xn);
  450. dev_dbg(sonic->card->dev,
  451. "pll: m = 0x%x, r = 0x%x, n = 0x%x\n", reg, m, r, n);
  452. #endif
  453. }
  454. static void snd_sonicvibes_setpll(struct sonicvibes * sonic,
  455. unsigned char reg,
  456. unsigned int rate)
  457. {
  458. unsigned long flags;
  459. unsigned int r, m, n;
  460. snd_sonicvibes_pll(rate, &r, &m, &n);
  461. if (sonic != NULL) {
  462. spin_lock_irqsave(&sonic->reg_lock, flags);
  463. snd_sonicvibes_out1(sonic, reg, m);
  464. snd_sonicvibes_out1(sonic, reg + 1, r | n);
  465. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  466. }
  467. }
  468. static void snd_sonicvibes_set_adc_rate(struct sonicvibes * sonic, unsigned int rate)
  469. {
  470. unsigned long flags;
  471. unsigned int div;
  472. unsigned char clock;
  473. div = 48000 / rate;
  474. if (div > 8)
  475. div = 8;
  476. if ((48000 / div) == rate) { /* use the alternate clock */
  477. clock = 0x10;
  478. } else { /* use the PLL source */
  479. clock = 0x00;
  480. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, rate);
  481. }
  482. spin_lock_irqsave(&sonic->reg_lock, flags);
  483. snd_sonicvibes_out1(sonic, SV_IREG_ADC_ALT_RATE, (div - 1) << 4);
  484. snd_sonicvibes_out1(sonic, SV_IREG_ADC_CLOCK, clock);
  485. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  486. }
  487. static int snd_sonicvibes_hw_constraint_dac_rate(struct snd_pcm_hw_params *params,
  488. struct snd_pcm_hw_rule *rule)
  489. {
  490. unsigned int rate, div, r, m, n;
  491. if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min ==
  492. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max) {
  493. rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min;
  494. div = 48000 / rate;
  495. if (div > 8)
  496. div = 8;
  497. if ((48000 / div) == rate) {
  498. params->rate_num = rate;
  499. params->rate_den = 1;
  500. } else {
  501. snd_sonicvibes_pll(rate, &r, &m, &n);
  502. snd_BUG_ON(SV_REFFREQUENCY % 16);
  503. snd_BUG_ON(SV_ADCMULT % 512);
  504. params->rate_num = (SV_REFFREQUENCY/16) * (n+2) * r;
  505. params->rate_den = (SV_ADCMULT/512) * (m+2);
  506. }
  507. }
  508. return 0;
  509. }
  510. static void snd_sonicvibes_set_dac_rate(struct sonicvibes * sonic, unsigned int rate)
  511. {
  512. unsigned int div;
  513. unsigned long flags;
  514. div = (rate * 65536 + SV_FULLRATE / 2) / SV_FULLRATE;
  515. if (div > 65535)
  516. div = 65535;
  517. spin_lock_irqsave(&sonic->reg_lock, flags);
  518. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_HIGH, div >> 8);
  519. snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_LOW, div);
  520. spin_unlock_irqrestore(&sonic->reg_lock, flags);
  521. }
  522. static int snd_sonicvibes_trigger(struct sonicvibes * sonic, int what, int cmd)
  523. {
  524. int result = 0;
  525. spin_lock(&sonic->reg_lock);
  526. if (cmd == SNDRV_PCM_TRIGGER_START) {
  527. if (!(sonic->enable & what)) {
  528. sonic->enable |= what;
  529. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  530. }
  531. } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
  532. if (sonic->enable & what) {
  533. sonic->enable &= ~what;
  534. snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
  535. }
  536. } else {
  537. result = -EINVAL;
  538. }
  539. spin_unlock(&sonic->reg_lock);
  540. return result;
  541. }
  542. static irqreturn_t snd_sonicvibes_interrupt(int irq, void *dev_id)
  543. {
  544. struct sonicvibes *sonic = dev_id;
  545. unsigned char status;
  546. status = inb(SV_REG(sonic, STATUS));
  547. if (!(status & (SV_DMAA_IRQ | SV_DMAC_IRQ | SV_MIDI_IRQ)))
  548. return IRQ_NONE;
  549. if (status == 0xff) { /* failure */
  550. outb(sonic->irqmask = ~0, SV_REG(sonic, IRQMASK));
  551. dev_err(sonic->card->dev,
  552. "IRQ failure - interrupts disabled!!\n");
  553. return IRQ_HANDLED;
  554. }
  555. if (sonic->pcm) {
  556. if (status & SV_DMAA_IRQ)
  557. snd_pcm_period_elapsed(sonic->playback_substream);
  558. if (status & SV_DMAC_IRQ)
  559. snd_pcm_period_elapsed(sonic->capture_substream);
  560. }
  561. if (sonic->rmidi) {
  562. if (status & SV_MIDI_IRQ)
  563. snd_mpu401_uart_interrupt(irq, sonic->rmidi->private_data);
  564. }
  565. if (status & SV_UD_IRQ) {
  566. unsigned char udreg;
  567. int vol, oleft, oright, mleft, mright;
  568. spin_lock(&sonic->reg_lock);
  569. udreg = snd_sonicvibes_in1(sonic, SV_IREG_UD_BUTTON);
  570. vol = udreg & 0x3f;
  571. if (!(udreg & 0x40))
  572. vol = -vol;
  573. oleft = mleft = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ANALOG);
  574. oright = mright = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ANALOG);
  575. oleft &= 0x1f;
  576. oright &= 0x1f;
  577. oleft += vol;
  578. if (oleft < 0)
  579. oleft = 0;
  580. if (oleft > 0x1f)
  581. oleft = 0x1f;
  582. oright += vol;
  583. if (oright < 0)
  584. oright = 0;
  585. if (oright > 0x1f)
  586. oright = 0x1f;
  587. if (udreg & 0x80) {
  588. mleft ^= 0x80;
  589. mright ^= 0x80;
  590. }
  591. oleft |= mleft & 0x80;
  592. oright |= mright & 0x80;
  593. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ANALOG, oleft);
  594. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ANALOG, oright);
  595. spin_unlock(&sonic->reg_lock);
  596. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_mute->id);
  597. snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_volume->id);
  598. }
  599. return IRQ_HANDLED;
  600. }
  601. /*
  602. * PCM part
  603. */
  604. static int snd_sonicvibes_playback_trigger(struct snd_pcm_substream *substream,
  605. int cmd)
  606. {
  607. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  608. return snd_sonicvibes_trigger(sonic, 1, cmd);
  609. }
  610. static int snd_sonicvibes_capture_trigger(struct snd_pcm_substream *substream,
  611. int cmd)
  612. {
  613. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  614. return snd_sonicvibes_trigger(sonic, 2, cmd);
  615. }
  616. static int snd_sonicvibes_playback_prepare(struct snd_pcm_substream *substream)
  617. {
  618. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  619. struct snd_pcm_runtime *runtime = substream->runtime;
  620. unsigned char fmt = 0;
  621. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  622. unsigned int count = snd_pcm_lib_period_bytes(substream);
  623. sonic->p_dma_size = size;
  624. count--;
  625. if (runtime->channels > 1)
  626. fmt |= 1;
  627. if (snd_pcm_format_width(runtime->format) == 16)
  628. fmt |= 2;
  629. snd_sonicvibes_setfmt(sonic, ~3, fmt);
  630. snd_sonicvibes_set_dac_rate(sonic, runtime->rate);
  631. spin_lock_irq(&sonic->reg_lock);
  632. snd_sonicvibes_setdmaa(sonic, runtime->dma_addr, size);
  633. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_UPPER, count >> 8);
  634. snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_LOWER, count);
  635. spin_unlock_irq(&sonic->reg_lock);
  636. return 0;
  637. }
  638. static int snd_sonicvibes_capture_prepare(struct snd_pcm_substream *substream)
  639. {
  640. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  641. struct snd_pcm_runtime *runtime = substream->runtime;
  642. unsigned char fmt = 0;
  643. unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  644. unsigned int count = snd_pcm_lib_period_bytes(substream);
  645. sonic->c_dma_size = size;
  646. count >>= 1;
  647. count--;
  648. if (runtime->channels > 1)
  649. fmt |= 0x10;
  650. if (snd_pcm_format_width(runtime->format) == 16)
  651. fmt |= 0x20;
  652. snd_sonicvibes_setfmt(sonic, ~0x30, fmt);
  653. snd_sonicvibes_set_adc_rate(sonic, runtime->rate);
  654. spin_lock_irq(&sonic->reg_lock);
  655. snd_sonicvibes_setdmac(sonic, runtime->dma_addr, size);
  656. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_UPPER, count >> 8);
  657. snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_LOWER, count);
  658. spin_unlock_irq(&sonic->reg_lock);
  659. return 0;
  660. }
  661. static snd_pcm_uframes_t snd_sonicvibes_playback_pointer(struct snd_pcm_substream *substream)
  662. {
  663. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  664. size_t ptr;
  665. if (!(sonic->enable & 1))
  666. return 0;
  667. ptr = sonic->p_dma_size - snd_sonicvibes_getdmaa(sonic);
  668. return bytes_to_frames(substream->runtime, ptr);
  669. }
  670. static snd_pcm_uframes_t snd_sonicvibes_capture_pointer(struct snd_pcm_substream *substream)
  671. {
  672. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  673. size_t ptr;
  674. if (!(sonic->enable & 2))
  675. return 0;
  676. ptr = sonic->c_dma_size - snd_sonicvibes_getdmac(sonic);
  677. return bytes_to_frames(substream->runtime, ptr);
  678. }
  679. static const struct snd_pcm_hardware snd_sonicvibes_playback =
  680. {
  681. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  682. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  683. SNDRV_PCM_INFO_MMAP_VALID),
  684. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  685. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  686. .rate_min = 4000,
  687. .rate_max = 48000,
  688. .channels_min = 1,
  689. .channels_max = 2,
  690. .buffer_bytes_max = (128*1024),
  691. .period_bytes_min = 32,
  692. .period_bytes_max = (128*1024),
  693. .periods_min = 1,
  694. .periods_max = 1024,
  695. .fifo_size = 0,
  696. };
  697. static const struct snd_pcm_hardware snd_sonicvibes_capture =
  698. {
  699. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  700. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  701. SNDRV_PCM_INFO_MMAP_VALID),
  702. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  703. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  704. .rate_min = 4000,
  705. .rate_max = 48000,
  706. .channels_min = 1,
  707. .channels_max = 2,
  708. .buffer_bytes_max = (128*1024),
  709. .period_bytes_min = 32,
  710. .period_bytes_max = (128*1024),
  711. .periods_min = 1,
  712. .periods_max = 1024,
  713. .fifo_size = 0,
  714. };
  715. static int snd_sonicvibes_playback_open(struct snd_pcm_substream *substream)
  716. {
  717. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  718. struct snd_pcm_runtime *runtime = substream->runtime;
  719. sonic->mode |= SV_MODE_PLAY;
  720. sonic->playback_substream = substream;
  721. runtime->hw = snd_sonicvibes_playback;
  722. snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, snd_sonicvibes_hw_constraint_dac_rate, NULL, SNDRV_PCM_HW_PARAM_RATE, -1);
  723. return 0;
  724. }
  725. static int snd_sonicvibes_capture_open(struct snd_pcm_substream *substream)
  726. {
  727. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  728. struct snd_pcm_runtime *runtime = substream->runtime;
  729. sonic->mode |= SV_MODE_CAPTURE;
  730. sonic->capture_substream = substream;
  731. runtime->hw = snd_sonicvibes_capture;
  732. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  733. &snd_sonicvibes_hw_constraints_adc_clock);
  734. return 0;
  735. }
  736. static int snd_sonicvibes_playback_close(struct snd_pcm_substream *substream)
  737. {
  738. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  739. sonic->playback_substream = NULL;
  740. sonic->mode &= ~SV_MODE_PLAY;
  741. return 0;
  742. }
  743. static int snd_sonicvibes_capture_close(struct snd_pcm_substream *substream)
  744. {
  745. struct sonicvibes *sonic = snd_pcm_substream_chip(substream);
  746. sonic->capture_substream = NULL;
  747. sonic->mode &= ~SV_MODE_CAPTURE;
  748. return 0;
  749. }
  750. static const struct snd_pcm_ops snd_sonicvibes_playback_ops = {
  751. .open = snd_sonicvibes_playback_open,
  752. .close = snd_sonicvibes_playback_close,
  753. .prepare = snd_sonicvibes_playback_prepare,
  754. .trigger = snd_sonicvibes_playback_trigger,
  755. .pointer = snd_sonicvibes_playback_pointer,
  756. };
  757. static const struct snd_pcm_ops snd_sonicvibes_capture_ops = {
  758. .open = snd_sonicvibes_capture_open,
  759. .close = snd_sonicvibes_capture_close,
  760. .prepare = snd_sonicvibes_capture_prepare,
  761. .trigger = snd_sonicvibes_capture_trigger,
  762. .pointer = snd_sonicvibes_capture_pointer,
  763. };
  764. static int snd_sonicvibes_pcm(struct sonicvibes *sonic, int device)
  765. {
  766. struct snd_pcm *pcm;
  767. int err;
  768. if ((err = snd_pcm_new(sonic->card, "s3_86c617", device, 1, 1, &pcm)) < 0)
  769. return err;
  770. if (snd_BUG_ON(!pcm))
  771. return -EINVAL;
  772. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sonicvibes_playback_ops);
  773. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sonicvibes_capture_ops);
  774. pcm->private_data = sonic;
  775. pcm->info_flags = 0;
  776. strcpy(pcm->name, "S3 SonicVibes");
  777. sonic->pcm = pcm;
  778. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  779. &sonic->pci->dev, 64*1024, 128*1024);
  780. return 0;
  781. }
  782. /*
  783. * Mixer part
  784. */
  785. #define SONICVIBES_MUX(xname, xindex) \
  786. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  787. .info = snd_sonicvibes_info_mux, \
  788. .get = snd_sonicvibes_get_mux, .put = snd_sonicvibes_put_mux }
  789. static int snd_sonicvibes_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  790. {
  791. static const char * const texts[7] = {
  792. "CD", "PCM", "Aux1", "Line", "Aux0", "Mic", "Mix"
  793. };
  794. return snd_ctl_enum_info(uinfo, 2, 7, texts);
  795. }
  796. static int snd_sonicvibes_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  797. {
  798. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  799. spin_lock_irq(&sonic->reg_lock);
  800. ucontrol->value.enumerated.item[0] = ((snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  801. ucontrol->value.enumerated.item[1] = ((snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
  802. spin_unlock_irq(&sonic->reg_lock);
  803. return 0;
  804. }
  805. static int snd_sonicvibes_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  806. {
  807. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  808. unsigned short left, right, oval1, oval2;
  809. int change;
  810. if (ucontrol->value.enumerated.item[0] >= 7 ||
  811. ucontrol->value.enumerated.item[1] >= 7)
  812. return -EINVAL;
  813. left = (ucontrol->value.enumerated.item[0] + 1) << 5;
  814. right = (ucontrol->value.enumerated.item[1] + 1) << 5;
  815. spin_lock_irq(&sonic->reg_lock);
  816. oval1 = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC);
  817. oval2 = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC);
  818. left = (oval1 & ~SV_RECSRC_OUT) | left;
  819. right = (oval2 & ~SV_RECSRC_OUT) | right;
  820. change = left != oval1 || right != oval2;
  821. snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ADC, left);
  822. snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ADC, right);
  823. spin_unlock_irq(&sonic->reg_lock);
  824. return change;
  825. }
  826. #define SONICVIBES_SINGLE(xname, xindex, reg, shift, mask, invert) \
  827. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  828. .info = snd_sonicvibes_info_single, \
  829. .get = snd_sonicvibes_get_single, .put = snd_sonicvibes_put_single, \
  830. .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
  831. static int snd_sonicvibes_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  832. {
  833. int mask = (kcontrol->private_value >> 16) & 0xff;
  834. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  835. uinfo->count = 1;
  836. uinfo->value.integer.min = 0;
  837. uinfo->value.integer.max = mask;
  838. return 0;
  839. }
  840. static int snd_sonicvibes_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  841. {
  842. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  843. int reg = kcontrol->private_value & 0xff;
  844. int shift = (kcontrol->private_value >> 8) & 0xff;
  845. int mask = (kcontrol->private_value >> 16) & 0xff;
  846. int invert = (kcontrol->private_value >> 24) & 0xff;
  847. spin_lock_irq(&sonic->reg_lock);
  848. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, reg)>> shift) & mask;
  849. spin_unlock_irq(&sonic->reg_lock);
  850. if (invert)
  851. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  852. return 0;
  853. }
  854. static int snd_sonicvibes_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  855. {
  856. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  857. int reg = kcontrol->private_value & 0xff;
  858. int shift = (kcontrol->private_value >> 8) & 0xff;
  859. int mask = (kcontrol->private_value >> 16) & 0xff;
  860. int invert = (kcontrol->private_value >> 24) & 0xff;
  861. int change;
  862. unsigned short val, oval;
  863. val = (ucontrol->value.integer.value[0] & mask);
  864. if (invert)
  865. val = mask - val;
  866. val <<= shift;
  867. spin_lock_irq(&sonic->reg_lock);
  868. oval = snd_sonicvibes_in1(sonic, reg);
  869. val = (oval & ~(mask << shift)) | val;
  870. change = val != oval;
  871. snd_sonicvibes_out1(sonic, reg, val);
  872. spin_unlock_irq(&sonic->reg_lock);
  873. return change;
  874. }
  875. #define SONICVIBES_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  876. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
  877. .info = snd_sonicvibes_info_double, \
  878. .get = snd_sonicvibes_get_double, .put = snd_sonicvibes_put_double, \
  879. .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
  880. static int snd_sonicvibes_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  881. {
  882. int mask = (kcontrol->private_value >> 24) & 0xff;
  883. uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  884. uinfo->count = 2;
  885. uinfo->value.integer.min = 0;
  886. uinfo->value.integer.max = mask;
  887. return 0;
  888. }
  889. static int snd_sonicvibes_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  890. {
  891. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  892. int left_reg = kcontrol->private_value & 0xff;
  893. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  894. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  895. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  896. int mask = (kcontrol->private_value >> 24) & 0xff;
  897. int invert = (kcontrol->private_value >> 22) & 1;
  898. spin_lock_irq(&sonic->reg_lock);
  899. ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, left_reg) >> shift_left) & mask;
  900. ucontrol->value.integer.value[1] = (snd_sonicvibes_in1(sonic, right_reg) >> shift_right) & mask;
  901. spin_unlock_irq(&sonic->reg_lock);
  902. if (invert) {
  903. ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  904. ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  905. }
  906. return 0;
  907. }
  908. static int snd_sonicvibes_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  909. {
  910. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  911. int left_reg = kcontrol->private_value & 0xff;
  912. int right_reg = (kcontrol->private_value >> 8) & 0xff;
  913. int shift_left = (kcontrol->private_value >> 16) & 0x07;
  914. int shift_right = (kcontrol->private_value >> 19) & 0x07;
  915. int mask = (kcontrol->private_value >> 24) & 0xff;
  916. int invert = (kcontrol->private_value >> 22) & 1;
  917. int change;
  918. unsigned short val1, val2, oval1, oval2;
  919. val1 = ucontrol->value.integer.value[0] & mask;
  920. val2 = ucontrol->value.integer.value[1] & mask;
  921. if (invert) {
  922. val1 = mask - val1;
  923. val2 = mask - val2;
  924. }
  925. val1 <<= shift_left;
  926. val2 <<= shift_right;
  927. spin_lock_irq(&sonic->reg_lock);
  928. oval1 = snd_sonicvibes_in1(sonic, left_reg);
  929. oval2 = snd_sonicvibes_in1(sonic, right_reg);
  930. val1 = (oval1 & ~(mask << shift_left)) | val1;
  931. val2 = (oval2 & ~(mask << shift_right)) | val2;
  932. change = val1 != oval1 || val2 != oval2;
  933. snd_sonicvibes_out1(sonic, left_reg, val1);
  934. snd_sonicvibes_out1(sonic, right_reg, val2);
  935. spin_unlock_irq(&sonic->reg_lock);
  936. return change;
  937. }
  938. static const struct snd_kcontrol_new snd_sonicvibes_controls[] = {
  939. SONICVIBES_DOUBLE("Capture Volume", 0, SV_IREG_LEFT_ADC, SV_IREG_RIGHT_ADC, 0, 0, 15, 0),
  940. SONICVIBES_DOUBLE("Aux Playback Switch", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 7, 7, 1, 1),
  941. SONICVIBES_DOUBLE("Aux Playback Volume", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 0, 0, 31, 1),
  942. SONICVIBES_DOUBLE("CD Playback Switch", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 7, 7, 1, 1),
  943. SONICVIBES_DOUBLE("CD Playback Volume", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 0, 0, 31, 1),
  944. SONICVIBES_DOUBLE("Line Playback Switch", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 7, 7, 1, 1),
  945. SONICVIBES_DOUBLE("Line Playback Volume", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 0, 0, 31, 1),
  946. SONICVIBES_SINGLE("Mic Playback Switch", 0, SV_IREG_MIC, 7, 1, 1),
  947. SONICVIBES_SINGLE("Mic Playback Volume", 0, SV_IREG_MIC, 0, 15, 1),
  948. SONICVIBES_SINGLE("Mic Boost", 0, SV_IREG_LEFT_ADC, 4, 1, 0),
  949. SONICVIBES_DOUBLE("Synth Playback Switch", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 7, 7, 1, 1),
  950. SONICVIBES_DOUBLE("Synth Playback Volume", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 0, 0, 31, 1),
  951. SONICVIBES_DOUBLE("Aux Playback Switch", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 7, 7, 1, 1),
  952. SONICVIBES_DOUBLE("Aux Playback Volume", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 0, 0, 31, 1),
  953. SONICVIBES_DOUBLE("Master Playback Switch", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 7, 7, 1, 1),
  954. SONICVIBES_DOUBLE("Master Playback Volume", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 0, 0, 31, 1),
  955. SONICVIBES_DOUBLE("PCM Playback Switch", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 7, 7, 1, 1),
  956. SONICVIBES_DOUBLE("PCM Playback Volume", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 0, 0, 63, 1),
  957. SONICVIBES_SINGLE("Loopback Capture Switch", 0, SV_IREG_ADC_OUTPUT_CTRL, 0, 1, 0),
  958. SONICVIBES_SINGLE("Loopback Capture Volume", 0, SV_IREG_ADC_OUTPUT_CTRL, 2, 63, 1),
  959. SONICVIBES_MUX("Capture Source", 0)
  960. };
  961. static void snd_sonicvibes_master_free(struct snd_kcontrol *kcontrol)
  962. {
  963. struct sonicvibes *sonic = snd_kcontrol_chip(kcontrol);
  964. sonic->master_mute = NULL;
  965. sonic->master_volume = NULL;
  966. }
  967. static int snd_sonicvibes_mixer(struct sonicvibes *sonic)
  968. {
  969. struct snd_card *card;
  970. struct snd_kcontrol *kctl;
  971. unsigned int idx;
  972. int err;
  973. if (snd_BUG_ON(!sonic || !sonic->card))
  974. return -EINVAL;
  975. card = sonic->card;
  976. strcpy(card->mixername, "S3 SonicVibes");
  977. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_controls); idx++) {
  978. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_sonicvibes_controls[idx], sonic))) < 0)
  979. return err;
  980. switch (idx) {
  981. case 0:
  982. case 1: kctl->private_free = snd_sonicvibes_master_free; break;
  983. }
  984. }
  985. return 0;
  986. }
  987. /*
  988. */
  989. static void snd_sonicvibes_proc_read(struct snd_info_entry *entry,
  990. struct snd_info_buffer *buffer)
  991. {
  992. struct sonicvibes *sonic = entry->private_data;
  993. unsigned char tmp;
  994. tmp = sonic->srs_space & 0x0f;
  995. snd_iprintf(buffer, "SRS 3D : %s\n",
  996. sonic->srs_space & 0x80 ? "off" : "on");
  997. snd_iprintf(buffer, "SRS Space : %s\n",
  998. tmp == 0x00 ? "100%" :
  999. tmp == 0x01 ? "75%" :
  1000. tmp == 0x02 ? "50%" :
  1001. tmp == 0x03 ? "25%" : "0%");
  1002. tmp = sonic->srs_center & 0x0f;
  1003. snd_iprintf(buffer, "SRS Center : %s\n",
  1004. tmp == 0x00 ? "100%" :
  1005. tmp == 0x01 ? "75%" :
  1006. tmp == 0x02 ? "50%" :
  1007. tmp == 0x03 ? "25%" : "0%");
  1008. tmp = sonic->wave_source & 0x03;
  1009. snd_iprintf(buffer, "WaveTable Source : %s\n",
  1010. tmp == 0x00 ? "on-board ROM" :
  1011. tmp == 0x01 ? "PCI bus" : "on-board ROM + PCI bus");
  1012. tmp = sonic->mpu_switch;
  1013. snd_iprintf(buffer, "Onboard synth : %s\n", tmp & 0x01 ? "on" : "off");
  1014. snd_iprintf(buffer, "Ext. Rx to synth : %s\n", tmp & 0x02 ? "on" : "off");
  1015. snd_iprintf(buffer, "MIDI to ext. Tx : %s\n", tmp & 0x04 ? "on" : "off");
  1016. }
  1017. static void snd_sonicvibes_proc_init(struct sonicvibes *sonic)
  1018. {
  1019. snd_card_ro_proc_new(sonic->card, "sonicvibes", sonic,
  1020. snd_sonicvibes_proc_read);
  1021. }
  1022. /*
  1023. */
  1024. #ifdef SUPPORT_JOYSTICK
  1025. static const struct snd_kcontrol_new snd_sonicvibes_game_control =
  1026. SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
  1027. static int snd_sonicvibes_create_gameport(struct sonicvibes *sonic)
  1028. {
  1029. struct gameport *gp;
  1030. int err;
  1031. sonic->gameport = gp = gameport_allocate_port();
  1032. if (!gp) {
  1033. dev_err(sonic->card->dev,
  1034. "sonicvibes: cannot allocate memory for gameport\n");
  1035. return -ENOMEM;
  1036. }
  1037. gameport_set_name(gp, "SonicVibes Gameport");
  1038. gameport_set_phys(gp, "pci%s/gameport0", pci_name(sonic->pci));
  1039. gameport_set_dev_parent(gp, &sonic->pci->dev);
  1040. gp->io = sonic->game_port;
  1041. gameport_register_port(gp);
  1042. err = snd_ctl_add(sonic->card,
  1043. snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
  1044. if (err < 0)
  1045. return err;
  1046. return 0;
  1047. }
  1048. static void snd_sonicvibes_free_gameport(struct sonicvibes *sonic)
  1049. {
  1050. if (sonic->gameport) {
  1051. gameport_unregister_port(sonic->gameport);
  1052. sonic->gameport = NULL;
  1053. }
  1054. }
  1055. #else
  1056. static inline int snd_sonicvibes_create_gameport(struct sonicvibes *sonic) { return -ENOSYS; }
  1057. static inline void snd_sonicvibes_free_gameport(struct sonicvibes *sonic) { }
  1058. #endif
  1059. static int snd_sonicvibes_free(struct sonicvibes *sonic)
  1060. {
  1061. snd_sonicvibes_free_gameport(sonic);
  1062. pci_write_config_dword(sonic->pci, 0x40, sonic->dmaa_port);
  1063. pci_write_config_dword(sonic->pci, 0x48, sonic->dmac_port);
  1064. if (sonic->irq >= 0)
  1065. free_irq(sonic->irq, sonic);
  1066. release_and_free_resource(sonic->res_dmaa);
  1067. release_and_free_resource(sonic->res_dmac);
  1068. pci_release_regions(sonic->pci);
  1069. pci_disable_device(sonic->pci);
  1070. kfree(sonic);
  1071. return 0;
  1072. }
  1073. static int snd_sonicvibes_dev_free(struct snd_device *device)
  1074. {
  1075. struct sonicvibes *sonic = device->device_data;
  1076. return snd_sonicvibes_free(sonic);
  1077. }
  1078. static int snd_sonicvibes_create(struct snd_card *card,
  1079. struct pci_dev *pci,
  1080. int reverb,
  1081. int mge,
  1082. struct sonicvibes **rsonic)
  1083. {
  1084. struct sonicvibes *sonic;
  1085. unsigned int dmaa, dmac;
  1086. int err;
  1087. static const struct snd_device_ops ops = {
  1088. .dev_free = snd_sonicvibes_dev_free,
  1089. };
  1090. *rsonic = NULL;
  1091. /* enable PCI device */
  1092. if ((err = pci_enable_device(pci)) < 0)
  1093. return err;
  1094. /* check, if we can restrict PCI DMA transfers to 24 bits */
  1095. if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
  1096. dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
  1097. dev_err(card->dev,
  1098. "architecture does not support 24bit PCI busmaster DMA\n");
  1099. pci_disable_device(pci);
  1100. return -ENXIO;
  1101. }
  1102. sonic = kzalloc(sizeof(*sonic), GFP_KERNEL);
  1103. if (sonic == NULL) {
  1104. pci_disable_device(pci);
  1105. return -ENOMEM;
  1106. }
  1107. spin_lock_init(&sonic->reg_lock);
  1108. sonic->card = card;
  1109. sonic->pci = pci;
  1110. sonic->irq = -1;
  1111. if ((err = pci_request_regions(pci, "S3 SonicVibes")) < 0) {
  1112. kfree(sonic);
  1113. pci_disable_device(pci);
  1114. return err;
  1115. }
  1116. sonic->sb_port = pci_resource_start(pci, 0);
  1117. sonic->enh_port = pci_resource_start(pci, 1);
  1118. sonic->synth_port = pci_resource_start(pci, 2);
  1119. sonic->midi_port = pci_resource_start(pci, 3);
  1120. sonic->game_port = pci_resource_start(pci, 4);
  1121. if (request_irq(pci->irq, snd_sonicvibes_interrupt, IRQF_SHARED,
  1122. KBUILD_MODNAME, sonic)) {
  1123. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  1124. snd_sonicvibes_free(sonic);
  1125. return -EBUSY;
  1126. }
  1127. sonic->irq = pci->irq;
  1128. card->sync_irq = sonic->irq;
  1129. pci_read_config_dword(pci, 0x40, &dmaa);
  1130. pci_read_config_dword(pci, 0x48, &dmac);
  1131. dmaio &= ~0x0f;
  1132. dmaa &= ~0x0f;
  1133. dmac &= ~0x0f;
  1134. if (!dmaa) {
  1135. dmaa = dmaio;
  1136. dmaio += 0x10;
  1137. dev_info(card->dev,
  1138. "BIOS did not allocate DDMA channel A i/o, allocated at 0x%x\n",
  1139. dmaa);
  1140. }
  1141. if (!dmac) {
  1142. dmac = dmaio;
  1143. dmaio += 0x10;
  1144. dev_info(card->dev,
  1145. "BIOS did not allocate DDMA channel C i/o, allocated at 0x%x\n",
  1146. dmac);
  1147. }
  1148. pci_write_config_dword(pci, 0x40, dmaa);
  1149. pci_write_config_dword(pci, 0x48, dmac);
  1150. if ((sonic->res_dmaa = request_region(dmaa, 0x10, "S3 SonicVibes DDMA-A")) == NULL) {
  1151. snd_sonicvibes_free(sonic);
  1152. dev_err(card->dev,
  1153. "unable to grab DDMA-A port at 0x%x-0x%x\n",
  1154. dmaa, dmaa + 0x10 - 1);
  1155. return -EBUSY;
  1156. }
  1157. if ((sonic->res_dmac = request_region(dmac, 0x10, "S3 SonicVibes DDMA-C")) == NULL) {
  1158. snd_sonicvibes_free(sonic);
  1159. dev_err(card->dev,
  1160. "unable to grab DDMA-C port at 0x%x-0x%x\n",
  1161. dmac, dmac + 0x10 - 1);
  1162. return -EBUSY;
  1163. }
  1164. pci_read_config_dword(pci, 0x40, &sonic->dmaa_port);
  1165. pci_read_config_dword(pci, 0x48, &sonic->dmac_port);
  1166. sonic->dmaa_port &= ~0x0f;
  1167. sonic->dmac_port &= ~0x0f;
  1168. pci_write_config_dword(pci, 0x40, sonic->dmaa_port | 9); /* enable + enhanced */
  1169. pci_write_config_dword(pci, 0x48, sonic->dmac_port | 9); /* enable */
  1170. /* ok.. initialize S3 SonicVibes chip */
  1171. outb(SV_RESET, SV_REG(sonic, CONTROL)); /* reset chip */
  1172. udelay(100);
  1173. outb(0, SV_REG(sonic, CONTROL)); /* release reset */
  1174. udelay(100);
  1175. outb(SV_ENHANCED | SV_INTA | (reverb ? SV_REVERB : 0), SV_REG(sonic, CONTROL));
  1176. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1177. #if 1
  1178. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0); /* drive current 16mA */
  1179. #else
  1180. snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0x40); /* drive current 8mA */
  1181. #endif
  1182. snd_sonicvibes_out(sonic, SV_IREG_PC_ENABLE, sonic->enable = 0); /* disable playback & capture */
  1183. outb(sonic->irqmask = ~(SV_DMAA_MASK | SV_DMAC_MASK | SV_UD_MASK), SV_REG(sonic, IRQMASK));
  1184. inb(SV_REG(sonic, STATUS)); /* clear IRQs */
  1185. snd_sonicvibes_out(sonic, SV_IREG_ADC_CLOCK, 0); /* use PLL as clock source */
  1186. snd_sonicvibes_out(sonic, SV_IREG_ANALOG_POWER, 0); /* power up analog parts */
  1187. snd_sonicvibes_out(sonic, SV_IREG_DIGITAL_POWER, 0); /* power up digital parts */
  1188. snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, 8000);
  1189. snd_sonicvibes_out(sonic, SV_IREG_SRS_SPACE, sonic->srs_space = 0x80); /* SRS space off */
  1190. snd_sonicvibes_out(sonic, SV_IREG_SRS_CENTER, sonic->srs_center = 0x00);/* SRS center off */
  1191. snd_sonicvibes_out(sonic, SV_IREG_MPU401, sonic->mpu_switch = 0x05); /* MPU-401 switch */
  1192. snd_sonicvibes_out(sonic, SV_IREG_WAVE_SOURCE, sonic->wave_source = 0x00); /* onboard ROM */
  1193. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_LOW, (8000 * 65536 / SV_FULLRATE) & 0xff);
  1194. snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_HIGH, ((8000 * 65536 / SV_FULLRATE) >> 8) & 0xff);
  1195. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ADC, mge ? 0xd0 : 0xc0);
  1196. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ADC, 0xc0);
  1197. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX1, 0x9f);
  1198. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX1, 0x9f);
  1199. snd_sonicvibes_out(sonic, SV_IREG_LEFT_CD, 0x9f);
  1200. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_CD, 0x9f);
  1201. snd_sonicvibes_out(sonic, SV_IREG_LEFT_LINE, 0x9f);
  1202. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_LINE, 0x9f);
  1203. snd_sonicvibes_out(sonic, SV_IREG_MIC, 0x8f);
  1204. snd_sonicvibes_out(sonic, SV_IREG_LEFT_SYNTH, 0x9f);
  1205. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_SYNTH, 0x9f);
  1206. snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX2, 0x9f);
  1207. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX2, 0x9f);
  1208. snd_sonicvibes_out(sonic, SV_IREG_LEFT_ANALOG, 0x9f);
  1209. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ANALOG, 0x9f);
  1210. snd_sonicvibes_out(sonic, SV_IREG_LEFT_PCM, 0xbf);
  1211. snd_sonicvibes_out(sonic, SV_IREG_RIGHT_PCM, 0xbf);
  1212. snd_sonicvibes_out(sonic, SV_IREG_ADC_OUTPUT_CTRL, 0xfc);
  1213. #if 0
  1214. snd_sonicvibes_debug(sonic);
  1215. #endif
  1216. sonic->revision = snd_sonicvibes_in(sonic, SV_IREG_REVISION);
  1217. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sonic, &ops)) < 0) {
  1218. snd_sonicvibes_free(sonic);
  1219. return err;
  1220. }
  1221. snd_sonicvibes_proc_init(sonic);
  1222. *rsonic = sonic;
  1223. return 0;
  1224. }
  1225. /*
  1226. * MIDI section
  1227. */
  1228. static const struct snd_kcontrol_new snd_sonicvibes_midi_controls[] = {
  1229. SONICVIBES_SINGLE("SonicVibes Wave Source RAM", 0, SV_IREG_WAVE_SOURCE, 0, 1, 0),
  1230. SONICVIBES_SINGLE("SonicVibes Wave Source RAM+ROM", 0, SV_IREG_WAVE_SOURCE, 1, 1, 0),
  1231. SONICVIBES_SINGLE("SonicVibes Onboard Synth", 0, SV_IREG_MPU401, 0, 1, 0),
  1232. SONICVIBES_SINGLE("SonicVibes External Rx to Synth", 0, SV_IREG_MPU401, 1, 1, 0),
  1233. SONICVIBES_SINGLE("SonicVibes External Tx", 0, SV_IREG_MPU401, 2, 1, 0)
  1234. };
  1235. static int snd_sonicvibes_midi_input_open(struct snd_mpu401 * mpu)
  1236. {
  1237. struct sonicvibes *sonic = mpu->private_data;
  1238. outb(sonic->irqmask &= ~SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1239. return 0;
  1240. }
  1241. static void snd_sonicvibes_midi_input_close(struct snd_mpu401 * mpu)
  1242. {
  1243. struct sonicvibes *sonic = mpu->private_data;
  1244. outb(sonic->irqmask |= SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
  1245. }
  1246. static int snd_sonicvibes_midi(struct sonicvibes *sonic,
  1247. struct snd_rawmidi *rmidi)
  1248. {
  1249. struct snd_mpu401 * mpu = rmidi->private_data;
  1250. struct snd_card *card = sonic->card;
  1251. unsigned int idx;
  1252. int err;
  1253. mpu->private_data = sonic;
  1254. mpu->open_input = snd_sonicvibes_midi_input_open;
  1255. mpu->close_input = snd_sonicvibes_midi_input_close;
  1256. for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_midi_controls); idx++)
  1257. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_sonicvibes_midi_controls[idx], sonic))) < 0)
  1258. return err;
  1259. return 0;
  1260. }
  1261. static int snd_sonic_probe(struct pci_dev *pci,
  1262. const struct pci_device_id *pci_id)
  1263. {
  1264. static int dev;
  1265. struct snd_card *card;
  1266. struct sonicvibes *sonic;
  1267. struct snd_rawmidi *midi_uart;
  1268. struct snd_opl3 *opl3;
  1269. int idx, err;
  1270. if (dev >= SNDRV_CARDS)
  1271. return -ENODEV;
  1272. if (!enable[dev]) {
  1273. dev++;
  1274. return -ENOENT;
  1275. }
  1276. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  1277. 0, &card);
  1278. if (err < 0)
  1279. return err;
  1280. for (idx = 0; idx < 5; idx++) {
  1281. if (pci_resource_start(pci, idx) == 0 ||
  1282. !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
  1283. snd_card_free(card);
  1284. return -ENODEV;
  1285. }
  1286. }
  1287. if ((err = snd_sonicvibes_create(card, pci,
  1288. reverb[dev] ? 1 : 0,
  1289. mge[dev] ? 1 : 0,
  1290. &sonic)) < 0) {
  1291. snd_card_free(card);
  1292. return err;
  1293. }
  1294. strcpy(card->driver, "SonicVibes");
  1295. strcpy(card->shortname, "S3 SonicVibes");
  1296. sprintf(card->longname, "%s rev %i at 0x%llx, irq %i",
  1297. card->shortname,
  1298. sonic->revision,
  1299. (unsigned long long)pci_resource_start(pci, 1),
  1300. sonic->irq);
  1301. if ((err = snd_sonicvibes_pcm(sonic, 0)) < 0) {
  1302. snd_card_free(card);
  1303. return err;
  1304. }
  1305. if ((err = snd_sonicvibes_mixer(sonic)) < 0) {
  1306. snd_card_free(card);
  1307. return err;
  1308. }
  1309. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SONICVIBES,
  1310. sonic->midi_port,
  1311. MPU401_INFO_INTEGRATED |
  1312. MPU401_INFO_IRQ_HOOK,
  1313. -1, &midi_uart)) < 0) {
  1314. snd_card_free(card);
  1315. return err;
  1316. }
  1317. snd_sonicvibes_midi(sonic, midi_uart);
  1318. if ((err = snd_opl3_create(card, sonic->synth_port,
  1319. sonic->synth_port + 2,
  1320. OPL3_HW_OPL3_SV, 1, &opl3)) < 0) {
  1321. snd_card_free(card);
  1322. return err;
  1323. }
  1324. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  1325. snd_card_free(card);
  1326. return err;
  1327. }
  1328. err = snd_sonicvibes_create_gameport(sonic);
  1329. if (err < 0) {
  1330. snd_card_free(card);
  1331. return err;
  1332. }
  1333. if ((err = snd_card_register(card)) < 0) {
  1334. snd_card_free(card);
  1335. return err;
  1336. }
  1337. pci_set_drvdata(pci, card);
  1338. dev++;
  1339. return 0;
  1340. }
  1341. static void snd_sonic_remove(struct pci_dev *pci)
  1342. {
  1343. snd_card_free(pci_get_drvdata(pci));
  1344. }
  1345. static struct pci_driver sonicvibes_driver = {
  1346. .name = KBUILD_MODNAME,
  1347. .id_table = snd_sonic_ids,
  1348. .probe = snd_sonic_probe,
  1349. .remove = snd_sonic_remove,
  1350. };
  1351. module_pci_driver(sonicvibes_driver);