ens1370.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Ensoniq ES1370/ES1371 AudioPCI soundcard
  4. * Copyright (c) by Jaroslav Kysela <[email protected]>,
  5. * Thomas Sailer <[email protected]>
  6. */
  7. /* Power-Management-Code ( CONFIG_PM )
  8. * for ens1371 only ( FIXME )
  9. * derived from cs4281.c, atiixp.c and via82xx.c
  10. * using https://www.kernel.org/doc/html/latest/sound/kernel-api/writing-an-alsa-driver.html
  11. * by Kurt J. Bosch
  12. */
  13. #include <linux/io.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/init.h>
  17. #include <linux/pci.h>
  18. #include <linux/slab.h>
  19. #include <linux/gameport.h>
  20. #include <linux/module.h>
  21. #include <linux/mutex.h>
  22. #include <sound/core.h>
  23. #include <sound/control.h>
  24. #include <sound/pcm.h>
  25. #include <sound/rawmidi.h>
  26. #ifdef CHIP1371
  27. #include <sound/ac97_codec.h>
  28. #else
  29. #include <sound/ak4531_codec.h>
  30. #endif
  31. #include <sound/initval.h>
  32. #include <sound/asoundef.h>
  33. #ifndef CHIP1371
  34. #undef CHIP1370
  35. #define CHIP1370
  36. #endif
  37. #ifdef CHIP1370
  38. #define DRIVER_NAME "ENS1370"
  39. #define CHIP_NAME "ES1370" /* it can be ENS but just to keep compatibility... */
  40. #else
  41. #define DRIVER_NAME "ENS1371"
  42. #define CHIP_NAME "ES1371"
  43. #endif
  44. MODULE_AUTHOR("Jaroslav Kysela <[email protected]>, Thomas Sailer <[email protected]>");
  45. MODULE_LICENSE("GPL");
  46. #ifdef CHIP1370
  47. MODULE_DESCRIPTION("Ensoniq AudioPCI ES1370");
  48. #endif
  49. #ifdef CHIP1371
  50. MODULE_DESCRIPTION("Ensoniq/Creative AudioPCI ES1371+");
  51. #endif
  52. #if IS_REACHABLE(CONFIG_GAMEPORT)
  53. #define SUPPORT_JOYSTICK
  54. #endif
  55. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  56. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  57. static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  58. #ifdef SUPPORT_JOYSTICK
  59. #ifdef CHIP1371
  60. static int joystick_port[SNDRV_CARDS];
  61. #else
  62. static bool joystick[SNDRV_CARDS];
  63. #endif
  64. #endif
  65. #ifdef CHIP1371
  66. static int spdif[SNDRV_CARDS];
  67. static int lineio[SNDRV_CARDS];
  68. #endif
  69. module_param_array(index, int, NULL, 0444);
  70. MODULE_PARM_DESC(index, "Index value for Ensoniq AudioPCI soundcard.");
  71. module_param_array(id, charp, NULL, 0444);
  72. MODULE_PARM_DESC(id, "ID string for Ensoniq AudioPCI soundcard.");
  73. module_param_array(enable, bool, NULL, 0444);
  74. MODULE_PARM_DESC(enable, "Enable Ensoniq AudioPCI soundcard.");
  75. #ifdef SUPPORT_JOYSTICK
  76. #ifdef CHIP1371
  77. module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
  78. MODULE_PARM_DESC(joystick_port, "Joystick port address.");
  79. #else
  80. module_param_array(joystick, bool, NULL, 0444);
  81. MODULE_PARM_DESC(joystick, "Enable joystick.");
  82. #endif
  83. #endif /* SUPPORT_JOYSTICK */
  84. #ifdef CHIP1371
  85. module_param_array(spdif, int, NULL, 0444);
  86. MODULE_PARM_DESC(spdif, "S/PDIF output (-1 = none, 0 = auto, 1 = force).");
  87. module_param_array(lineio, int, NULL, 0444);
  88. MODULE_PARM_DESC(lineio, "Line In to Rear Out (0 = auto, 1 = force).");
  89. #endif
  90. /* ES1371 chip ID */
  91. /* This is a little confusing because all ES1371 compatible chips have the
  92. same DEVICE_ID, the only thing differentiating them is the REV_ID field.
  93. This is only significant if you want to enable features on the later parts.
  94. Yes, I know it's stupid and why didn't we use the sub IDs?
  95. */
  96. #define ES1371REV_ES1373_A 0x04
  97. #define ES1371REV_ES1373_B 0x06
  98. #define ES1371REV_CT5880_A 0x07
  99. #define CT5880REV_CT5880_C 0x02
  100. #define CT5880REV_CT5880_D 0x03 /* ??? -jk */
  101. #define CT5880REV_CT5880_E 0x04 /* mw */
  102. #define ES1371REV_ES1371_B 0x09
  103. #define EV1938REV_EV1938_A 0x00
  104. #define ES1371REV_ES1373_8 0x08
  105. /*
  106. * Direct registers
  107. */
  108. #define ES_REG(ensoniq, x) ((ensoniq)->port + ES_REG_##x)
  109. #define ES_REG_CONTROL 0x00 /* R/W: Interrupt/Chip select control register */
  110. #define ES_1370_ADC_STOP (1<<31) /* disable capture buffer transfers */
  111. #define ES_1370_XCTL1 (1<<30) /* general purpose output bit */
  112. #define ES_1373_BYPASS_P1 (1<<31) /* bypass SRC for PB1 */
  113. #define ES_1373_BYPASS_P2 (1<<30) /* bypass SRC for PB2 */
  114. #define ES_1373_BYPASS_R (1<<29) /* bypass SRC for REC */
  115. #define ES_1373_TEST_BIT (1<<28) /* should be set to 0 for normal operation */
  116. #define ES_1373_RECEN_B (1<<27) /* mix record with playback for I2S/SPDIF out */
  117. #define ES_1373_SPDIF_THRU (1<<26) /* 0 = SPDIF thru mode, 1 = SPDIF == dig out */
  118. #define ES_1371_JOY_ASEL(o) (((o)&0x03)<<24)/* joystick port mapping */
  119. #define ES_1371_JOY_ASELM (0x03<<24) /* mask for above */
  120. #define ES_1371_JOY_ASELI(i) (((i)>>24)&0x03)
  121. #define ES_1371_GPIO_IN(i) (((i)>>20)&0x0f)/* GPIO in [3:0] pins - R/O */
  122. #define ES_1370_PCLKDIVO(o) (((o)&0x1fff)<<16)/* clock divide ratio for DAC2 */
  123. #define ES_1370_PCLKDIVM ((0x1fff)<<16) /* mask for above */
  124. #define ES_1370_PCLKDIVI(i) (((i)>>16)&0x1fff)/* clock divide ratio for DAC2 */
  125. #define ES_1371_GPIO_OUT(o) (((o)&0x0f)<<16)/* GPIO out [3:0] pins - W/R */
  126. #define ES_1371_GPIO_OUTM (0x0f<<16) /* mask for above */
  127. #define ES_MSFMTSEL (1<<15) /* MPEG serial data format; 0 = SONY, 1 = I2S */
  128. #define ES_1370_M_SBB (1<<14) /* clock source for DAC - 0 = clock generator; 1 = MPEG clocks */
  129. #define ES_1371_SYNC_RES (1<<14) /* Warm AC97 reset */
  130. #define ES_1370_WTSRSEL(o) (((o)&0x03)<<12)/* fixed frequency clock for DAC1 */
  131. #define ES_1370_WTSRSELM (0x03<<12) /* mask for above */
  132. #define ES_1371_ADC_STOP (1<<13) /* disable CCB transfer capture information */
  133. #define ES_1371_PWR_INTRM (1<<12) /* power level change interrupts enable */
  134. #define ES_1370_DAC_SYNC (1<<11) /* DAC's are synchronous */
  135. #define ES_1371_M_CB (1<<11) /* capture clock source; 0 = AC'97 ADC; 1 = I2S */
  136. #define ES_CCB_INTRM (1<<10) /* CCB voice interrupts enable */
  137. #define ES_1370_M_CB (1<<9) /* capture clock source; 0 = ADC; 1 = MPEG */
  138. #define ES_1370_XCTL0 (1<<8) /* generap purpose output bit */
  139. #define ES_1371_PDLEV(o) (((o)&0x03)<<8) /* current power down level */
  140. #define ES_1371_PDLEVM (0x03<<8) /* mask for above */
  141. #define ES_BREQ (1<<7) /* memory bus request enable */
  142. #define ES_DAC1_EN (1<<6) /* DAC1 playback channel enable */
  143. #define ES_DAC2_EN (1<<5) /* DAC2 playback channel enable */
  144. #define ES_ADC_EN (1<<4) /* ADC capture channel enable */
  145. #define ES_UART_EN (1<<3) /* UART enable */
  146. #define ES_JYSTK_EN (1<<2) /* Joystick module enable */
  147. #define ES_1370_CDC_EN (1<<1) /* Codec interface enable */
  148. #define ES_1371_XTALCKDIS (1<<1) /* Xtal clock disable */
  149. #define ES_1370_SERR_DISABLE (1<<0) /* PCI serr signal disable */
  150. #define ES_1371_PCICLKDIS (1<<0) /* PCI clock disable */
  151. #define ES_REG_STATUS 0x04 /* R/O: Interrupt/Chip select status register */
  152. #define ES_INTR (1<<31) /* Interrupt is pending */
  153. #define ES_1371_ST_AC97_RST (1<<29) /* CT5880 AC'97 Reset bit */
  154. #define ES_1373_REAR_BIT27 (1<<27) /* rear bits: 000 - front, 010 - mirror, 101 - separate */
  155. #define ES_1373_REAR_BIT26 (1<<26)
  156. #define ES_1373_REAR_BIT24 (1<<24)
  157. #define ES_1373_GPIO_INT_EN(o)(((o)&0x0f)<<20)/* GPIO [3:0] pins - interrupt enable */
  158. #define ES_1373_SPDIF_EN (1<<18) /* SPDIF enable */
  159. #define ES_1373_SPDIF_TEST (1<<17) /* SPDIF test */
  160. #define ES_1371_TEST (1<<16) /* test ASIC */
  161. #define ES_1373_GPIO_INT(i) (((i)&0x0f)>>12)/* GPIO [3:0] pins - interrupt pending */
  162. #define ES_1370_CSTAT (1<<10) /* CODEC is busy or register write in progress */
  163. #define ES_1370_CBUSY (1<<9) /* CODEC is busy */
  164. #define ES_1370_CWRIP (1<<8) /* CODEC register write in progress */
  165. #define ES_1371_SYNC_ERR (1<<8) /* CODEC synchronization error occurred */
  166. #define ES_1371_VC(i) (((i)>>6)&0x03) /* voice code from CCB module */
  167. #define ES_1370_VC(i) (((i)>>5)&0x03) /* voice code from CCB module */
  168. #define ES_1371_MPWR (1<<5) /* power level interrupt pending */
  169. #define ES_MCCB (1<<4) /* CCB interrupt pending */
  170. #define ES_UART (1<<3) /* UART interrupt pending */
  171. #define ES_DAC1 (1<<2) /* DAC1 channel interrupt pending */
  172. #define ES_DAC2 (1<<1) /* DAC2 channel interrupt pending */
  173. #define ES_ADC (1<<0) /* ADC channel interrupt pending */
  174. #define ES_REG_UART_DATA 0x08 /* R/W: UART data register */
  175. #define ES_REG_UART_STATUS 0x09 /* R/O: UART status register */
  176. #define ES_RXINT (1<<7) /* RX interrupt occurred */
  177. #define ES_TXINT (1<<2) /* TX interrupt occurred */
  178. #define ES_TXRDY (1<<1) /* transmitter ready */
  179. #define ES_RXRDY (1<<0) /* receiver ready */
  180. #define ES_REG_UART_CONTROL 0x09 /* W/O: UART control register */
  181. #define ES_RXINTEN (1<<7) /* RX interrupt enable */
  182. #define ES_TXINTENO(o) (((o)&0x03)<<5) /* TX interrupt enable */
  183. #define ES_TXINTENM (0x03<<5) /* mask for above */
  184. #define ES_TXINTENI(i) (((i)>>5)&0x03)
  185. #define ES_CNTRL(o) (((o)&0x03)<<0) /* control */
  186. #define ES_CNTRLM (0x03<<0) /* mask for above */
  187. #define ES_REG_UART_RES 0x0a /* R/W: UART reserver register */
  188. #define ES_TEST_MODE (1<<0) /* test mode enabled */
  189. #define ES_REG_MEM_PAGE 0x0c /* R/W: Memory page register */
  190. #define ES_MEM_PAGEO(o) (((o)&0x0f)<<0) /* memory page select - out */
  191. #define ES_MEM_PAGEM (0x0f<<0) /* mask for above */
  192. #define ES_MEM_PAGEI(i) (((i)>>0)&0x0f) /* memory page select - in */
  193. #define ES_REG_1370_CODEC 0x10 /* W/O: Codec write register address */
  194. #define ES_1370_CODEC_WRITE(a,d) ((((a)&0xff)<<8)|(((d)&0xff)<<0))
  195. #define ES_REG_1371_CODEC 0x14 /* W/R: Codec Read/Write register address */
  196. #define ES_1371_CODEC_RDY (1<<31) /* codec ready */
  197. #define ES_1371_CODEC_WIP (1<<30) /* codec register access in progress */
  198. #define EV_1938_CODEC_MAGIC (1<<26)
  199. #define ES_1371_CODEC_PIRD (1<<23) /* codec read/write select register */
  200. #define ES_1371_CODEC_WRITE(a,d) ((((a)&0x7f)<<16)|(((d)&0xffff)<<0))
  201. #define ES_1371_CODEC_READS(a) ((((a)&0x7f)<<16)|ES_1371_CODEC_PIRD)
  202. #define ES_1371_CODEC_READ(i) (((i)>>0)&0xffff)
  203. #define ES_REG_1371_SMPRATE 0x10 /* W/R: Codec rate converter interface register */
  204. #define ES_1371_SRC_RAM_ADDRO(o) (((o)&0x7f)<<25)/* address of the sample rate converter */
  205. #define ES_1371_SRC_RAM_ADDRM (0x7f<<25) /* mask for above */
  206. #define ES_1371_SRC_RAM_ADDRI(i) (((i)>>25)&0x7f)/* address of the sample rate converter */
  207. #define ES_1371_SRC_RAM_WE (1<<24) /* R/W: read/write control for sample rate converter */
  208. #define ES_1371_SRC_RAM_BUSY (1<<23) /* R/O: sample rate memory is busy */
  209. #define ES_1371_SRC_DISABLE (1<<22) /* sample rate converter disable */
  210. #define ES_1371_DIS_P1 (1<<21) /* playback channel 1 accumulator update disable */
  211. #define ES_1371_DIS_P2 (1<<20) /* playback channel 1 accumulator update disable */
  212. #define ES_1371_DIS_R1 (1<<19) /* capture channel accumulator update disable */
  213. #define ES_1371_SRC_RAM_DATAO(o) (((o)&0xffff)<<0)/* current value of the sample rate converter */
  214. #define ES_1371_SRC_RAM_DATAM (0xffff<<0) /* mask for above */
  215. #define ES_1371_SRC_RAM_DATAI(i) (((i)>>0)&0xffff)/* current value of the sample rate converter */
  216. #define ES_REG_1371_LEGACY 0x18 /* W/R: Legacy control/status register */
  217. #define ES_1371_JFAST (1<<31) /* fast joystick timing */
  218. #define ES_1371_HIB (1<<30) /* host interrupt blocking enable */
  219. #define ES_1371_VSB (1<<29) /* SB; 0 = addr 0x220xH, 1 = 0x22FxH */
  220. #define ES_1371_VMPUO(o) (((o)&0x03)<<27)/* base register address; 0 = 0x320xH; 1 = 0x330xH; 2 = 0x340xH; 3 = 0x350xH */
  221. #define ES_1371_VMPUM (0x03<<27) /* mask for above */
  222. #define ES_1371_VMPUI(i) (((i)>>27)&0x03)/* base register address */
  223. #define ES_1371_VCDCO(o) (((o)&0x03)<<25)/* CODEC; 0 = 0x530xH; 1 = undefined; 2 = 0xe80xH; 3 = 0xF40xH */
  224. #define ES_1371_VCDCM (0x03<<25) /* mask for above */
  225. #define ES_1371_VCDCI(i) (((i)>>25)&0x03)/* CODEC address */
  226. #define ES_1371_FIRQ (1<<24) /* force an interrupt */
  227. #define ES_1371_SDMACAP (1<<23) /* enable event capture for slave DMA controller */
  228. #define ES_1371_SPICAP (1<<22) /* enable event capture for slave IRQ controller */
  229. #define ES_1371_MDMACAP (1<<21) /* enable event capture for master DMA controller */
  230. #define ES_1371_MPICAP (1<<20) /* enable event capture for master IRQ controller */
  231. #define ES_1371_ADCAP (1<<19) /* enable event capture for ADLIB register; 0x388xH */
  232. #define ES_1371_SVCAP (1<<18) /* enable event capture for SB registers */
  233. #define ES_1371_CDCCAP (1<<17) /* enable event capture for CODEC registers */
  234. #define ES_1371_BACAP (1<<16) /* enable event capture for SoundScape base address */
  235. #define ES_1371_EXI(i) (((i)>>8)&0x07) /* event number */
  236. #define ES_1371_AI(i) (((i)>>3)&0x1f) /* event significant I/O address */
  237. #define ES_1371_WR (1<<2) /* event capture; 0 = read; 1 = write */
  238. #define ES_1371_LEGINT (1<<0) /* interrupt for legacy events; 0 = interrupt did occur */
  239. #define ES_REG_CHANNEL_STATUS 0x1c /* R/W: first 32-bits from S/PDIF channel status block, es1373 */
  240. #define ES_REG_SERIAL 0x20 /* R/W: Serial interface control register */
  241. #define ES_1371_DAC_TEST (1<<22) /* DAC test mode enable */
  242. #define ES_P2_END_INCO(o) (((o)&0x07)<<19)/* binary offset value to increment / loop end */
  243. #define ES_P2_END_INCM (0x07<<19) /* mask for above */
  244. #define ES_P2_END_INCI(i) (((i)>>16)&0x07)/* binary offset value to increment / loop end */
  245. #define ES_P2_ST_INCO(o) (((o)&0x07)<<16)/* binary offset value to increment / start */
  246. #define ES_P2_ST_INCM (0x07<<16) /* mask for above */
  247. #define ES_P2_ST_INCI(i) (((i)<<16)&0x07)/* binary offset value to increment / start */
  248. #define ES_R1_LOOP_SEL (1<<15) /* ADC; 0 - loop mode; 1 = stop mode */
  249. #define ES_P2_LOOP_SEL (1<<14) /* DAC2; 0 - loop mode; 1 = stop mode */
  250. #define ES_P1_LOOP_SEL (1<<13) /* DAC1; 0 - loop mode; 1 = stop mode */
  251. #define ES_P2_PAUSE (1<<12) /* DAC2; 0 - play mode; 1 = pause mode */
  252. #define ES_P1_PAUSE (1<<11) /* DAC1; 0 - play mode; 1 = pause mode */
  253. #define ES_R1_INT_EN (1<<10) /* ADC interrupt enable */
  254. #define ES_P2_INT_EN (1<<9) /* DAC2 interrupt enable */
  255. #define ES_P1_INT_EN (1<<8) /* DAC1 interrupt enable */
  256. #define ES_P1_SCT_RLD (1<<7) /* force sample counter reload for DAC1 */
  257. #define ES_P2_DAC_SEN (1<<6) /* when stop mode: 0 - DAC2 play back zeros; 1 = DAC2 play back last sample */
  258. #define ES_R1_MODEO(o) (((o)&0x03)<<4) /* ADC mode; 0 = 8-bit mono; 1 = 8-bit stereo; 2 = 16-bit mono; 3 = 16-bit stereo */
  259. #define ES_R1_MODEM (0x03<<4) /* mask for above */
  260. #define ES_R1_MODEI(i) (((i)>>4)&0x03)
  261. #define ES_P2_MODEO(o) (((o)&0x03)<<2) /* DAC2 mode; -- '' -- */
  262. #define ES_P2_MODEM (0x03<<2) /* mask for above */
  263. #define ES_P2_MODEI(i) (((i)>>2)&0x03)
  264. #define ES_P1_MODEO(o) (((o)&0x03)<<0) /* DAC1 mode; -- '' -- */
  265. #define ES_P1_MODEM (0x03<<0) /* mask for above */
  266. #define ES_P1_MODEI(i) (((i)>>0)&0x03)
  267. #define ES_REG_DAC1_COUNT 0x24 /* R/W: DAC1 sample count register */
  268. #define ES_REG_DAC2_COUNT 0x28 /* R/W: DAC2 sample count register */
  269. #define ES_REG_ADC_COUNT 0x2c /* R/W: ADC sample count register */
  270. #define ES_REG_CURR_COUNT(i) (((i)>>16)&0xffff)
  271. #define ES_REG_COUNTO(o) (((o)&0xffff)<<0)
  272. #define ES_REG_COUNTM (0xffff<<0)
  273. #define ES_REG_COUNTI(i) (((i)>>0)&0xffff)
  274. #define ES_REG_DAC1_FRAME 0x30 /* R/W: PAGE 0x0c; DAC1 frame address */
  275. #define ES_REG_DAC1_SIZE 0x34 /* R/W: PAGE 0x0c; DAC1 frame size */
  276. #define ES_REG_DAC2_FRAME 0x38 /* R/W: PAGE 0x0c; DAC2 frame address */
  277. #define ES_REG_DAC2_SIZE 0x3c /* R/W: PAGE 0x0c; DAC2 frame size */
  278. #define ES_REG_ADC_FRAME 0x30 /* R/W: PAGE 0x0d; ADC frame address */
  279. #define ES_REG_ADC_SIZE 0x34 /* R/W: PAGE 0x0d; ADC frame size */
  280. #define ES_REG_FCURR_COUNTO(o) (((o)&0xffff)<<16)
  281. #define ES_REG_FCURR_COUNTM (0xffff<<16)
  282. #define ES_REG_FCURR_COUNTI(i) (((i)>>14)&0x3fffc)
  283. #define ES_REG_FSIZEO(o) (((o)&0xffff)<<0)
  284. #define ES_REG_FSIZEM (0xffff<<0)
  285. #define ES_REG_FSIZEI(i) (((i)>>0)&0xffff)
  286. #define ES_REG_PHANTOM_FRAME 0x38 /* R/W: PAGE 0x0d: phantom frame address */
  287. #define ES_REG_PHANTOM_COUNT 0x3c /* R/W: PAGE 0x0d: phantom frame count */
  288. #define ES_REG_UART_FIFO 0x30 /* R/W: PAGE 0x0e; UART FIFO register */
  289. #define ES_REG_UF_VALID (1<<8)
  290. #define ES_REG_UF_BYTEO(o) (((o)&0xff)<<0)
  291. #define ES_REG_UF_BYTEM (0xff<<0)
  292. #define ES_REG_UF_BYTEI(i) (((i)>>0)&0xff)
  293. /*
  294. * Pages
  295. */
  296. #define ES_PAGE_DAC 0x0c
  297. #define ES_PAGE_ADC 0x0d
  298. #define ES_PAGE_UART 0x0e
  299. #define ES_PAGE_UART1 0x0f
  300. /*
  301. * Sample rate converter addresses
  302. */
  303. #define ES_SMPREG_DAC1 0x70
  304. #define ES_SMPREG_DAC2 0x74
  305. #define ES_SMPREG_ADC 0x78
  306. #define ES_SMPREG_VOL_ADC 0x6c
  307. #define ES_SMPREG_VOL_DAC1 0x7c
  308. #define ES_SMPREG_VOL_DAC2 0x7e
  309. #define ES_SMPREG_TRUNC_N 0x00
  310. #define ES_SMPREG_INT_REGS 0x01
  311. #define ES_SMPREG_ACCUM_FRAC 0x02
  312. #define ES_SMPREG_VFREQ_FRAC 0x03
  313. /*
  314. * Some contants
  315. */
  316. #define ES_1370_SRCLOCK 1411200
  317. #define ES_1370_SRTODIV(x) (ES_1370_SRCLOCK/(x)-2)
  318. /*
  319. * Open modes
  320. */
  321. #define ES_MODE_PLAY1 0x0001
  322. #define ES_MODE_PLAY2 0x0002
  323. #define ES_MODE_CAPTURE 0x0004
  324. #define ES_MODE_OUTPUT 0x0001 /* for MIDI */
  325. #define ES_MODE_INPUT 0x0002 /* for MIDI */
  326. /*
  327. */
  328. struct ensoniq {
  329. spinlock_t reg_lock;
  330. struct mutex src_mutex;
  331. int irq;
  332. unsigned long playback1size;
  333. unsigned long playback2size;
  334. unsigned long capture3size;
  335. unsigned long port;
  336. unsigned int mode;
  337. unsigned int uartm; /* UART mode */
  338. unsigned int ctrl; /* control register */
  339. unsigned int sctrl; /* serial control register */
  340. unsigned int cssr; /* control status register */
  341. unsigned int uartc; /* uart control register */
  342. unsigned int rev; /* chip revision */
  343. union {
  344. #ifdef CHIP1371
  345. struct {
  346. struct snd_ac97 *ac97;
  347. } es1371;
  348. #else
  349. struct {
  350. int pclkdiv_lock;
  351. struct snd_ak4531 *ak4531;
  352. } es1370;
  353. #endif
  354. } u;
  355. struct pci_dev *pci;
  356. struct snd_card *card;
  357. struct snd_pcm *pcm1; /* DAC1/ADC PCM */
  358. struct snd_pcm *pcm2; /* DAC2 PCM */
  359. struct snd_pcm_substream *playback1_substream;
  360. struct snd_pcm_substream *playback2_substream;
  361. struct snd_pcm_substream *capture_substream;
  362. unsigned int p1_dma_size;
  363. unsigned int p2_dma_size;
  364. unsigned int c_dma_size;
  365. unsigned int p1_period_size;
  366. unsigned int p2_period_size;
  367. unsigned int c_period_size;
  368. struct snd_rawmidi *rmidi;
  369. struct snd_rawmidi_substream *midi_input;
  370. struct snd_rawmidi_substream *midi_output;
  371. unsigned int spdif;
  372. unsigned int spdif_default;
  373. unsigned int spdif_stream;
  374. #ifdef CHIP1370
  375. struct snd_dma_buffer *dma_bug;
  376. #endif
  377. #ifdef SUPPORT_JOYSTICK
  378. struct gameport *gameport;
  379. #endif
  380. };
  381. static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id);
  382. static const struct pci_device_id snd_audiopci_ids[] = {
  383. #ifdef CHIP1370
  384. { PCI_VDEVICE(ENSONIQ, 0x5000), 0, }, /* ES1370 */
  385. #endif
  386. #ifdef CHIP1371
  387. { PCI_VDEVICE(ENSONIQ, 0x1371), 0, }, /* ES1371 */
  388. { PCI_VDEVICE(ENSONIQ, 0x5880), 0, }, /* ES1373 - CT5880 */
  389. { PCI_VDEVICE(ECTIVA, 0x8938), 0, }, /* Ectiva EV1938 */
  390. #endif
  391. { 0, }
  392. };
  393. MODULE_DEVICE_TABLE(pci, snd_audiopci_ids);
  394. /*
  395. * constants
  396. */
  397. #define POLL_COUNT 0xa000
  398. #ifdef CHIP1370
  399. static const unsigned int snd_es1370_fixed_rates[] =
  400. {5512, 11025, 22050, 44100};
  401. static const struct snd_pcm_hw_constraint_list snd_es1370_hw_constraints_rates = {
  402. .count = 4,
  403. .list = snd_es1370_fixed_rates,
  404. .mask = 0,
  405. };
  406. static const struct snd_ratnum es1370_clock = {
  407. .num = ES_1370_SRCLOCK,
  408. .den_min = 29,
  409. .den_max = 353,
  410. .den_step = 1,
  411. };
  412. static const struct snd_pcm_hw_constraint_ratnums snd_es1370_hw_constraints_clock = {
  413. .nrats = 1,
  414. .rats = &es1370_clock,
  415. };
  416. #else
  417. static const struct snd_ratden es1371_dac_clock = {
  418. .num_min = 3000 * (1 << 15),
  419. .num_max = 48000 * (1 << 15),
  420. .num_step = 3000,
  421. .den = 1 << 15,
  422. };
  423. static const struct snd_pcm_hw_constraint_ratdens snd_es1371_hw_constraints_dac_clock = {
  424. .nrats = 1,
  425. .rats = &es1371_dac_clock,
  426. };
  427. static const struct snd_ratnum es1371_adc_clock = {
  428. .num = 48000 << 15,
  429. .den_min = 32768,
  430. .den_max = 393216,
  431. .den_step = 1,
  432. };
  433. static const struct snd_pcm_hw_constraint_ratnums snd_es1371_hw_constraints_adc_clock = {
  434. .nrats = 1,
  435. .rats = &es1371_adc_clock,
  436. };
  437. #endif
  438. static const unsigned int snd_ensoniq_sample_shift[] =
  439. {0, 1, 1, 2};
  440. /*
  441. * common I/O routines
  442. */
  443. #ifdef CHIP1371
  444. static unsigned int snd_es1371_wait_src_ready(struct ensoniq * ensoniq)
  445. {
  446. unsigned int t, r = 0;
  447. for (t = 0; t < POLL_COUNT; t++) {
  448. r = inl(ES_REG(ensoniq, 1371_SMPRATE));
  449. if ((r & ES_1371_SRC_RAM_BUSY) == 0)
  450. return r;
  451. cond_resched();
  452. }
  453. dev_err(ensoniq->card->dev, "wait src ready timeout 0x%lx [0x%x]\n",
  454. ES_REG(ensoniq, 1371_SMPRATE), r);
  455. return 0;
  456. }
  457. static unsigned int snd_es1371_src_read(struct ensoniq * ensoniq, unsigned short reg)
  458. {
  459. unsigned int temp, i, orig, r;
  460. /* wait for ready */
  461. temp = orig = snd_es1371_wait_src_ready(ensoniq);
  462. /* expose the SRC state bits */
  463. r = temp & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  464. ES_1371_DIS_P2 | ES_1371_DIS_R1);
  465. r |= ES_1371_SRC_RAM_ADDRO(reg) | 0x10000;
  466. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  467. /* now, wait for busy and the correct time to read */
  468. temp = snd_es1371_wait_src_ready(ensoniq);
  469. if ((temp & 0x00870000) != 0x00010000) {
  470. /* wait for the right state */
  471. for (i = 0; i < POLL_COUNT; i++) {
  472. temp = inl(ES_REG(ensoniq, 1371_SMPRATE));
  473. if ((temp & 0x00870000) == 0x00010000)
  474. break;
  475. }
  476. }
  477. /* hide the state bits */
  478. r = orig & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  479. ES_1371_DIS_P2 | ES_1371_DIS_R1);
  480. r |= ES_1371_SRC_RAM_ADDRO(reg);
  481. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  482. return temp;
  483. }
  484. static void snd_es1371_src_write(struct ensoniq * ensoniq,
  485. unsigned short reg, unsigned short data)
  486. {
  487. unsigned int r;
  488. r = snd_es1371_wait_src_ready(ensoniq) &
  489. (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  490. ES_1371_DIS_P2 | ES_1371_DIS_R1);
  491. r |= ES_1371_SRC_RAM_ADDRO(reg) | ES_1371_SRC_RAM_DATAO(data);
  492. outl(r | ES_1371_SRC_RAM_WE, ES_REG(ensoniq, 1371_SMPRATE));
  493. }
  494. #endif /* CHIP1371 */
  495. #ifdef CHIP1370
  496. static void snd_es1370_codec_write(struct snd_ak4531 *ak4531,
  497. unsigned short reg, unsigned short val)
  498. {
  499. struct ensoniq *ensoniq = ak4531->private_data;
  500. unsigned long end_time = jiffies + HZ / 10;
  501. #if 0
  502. dev_dbg(ensoniq->card->dev,
  503. "CODEC WRITE: reg = 0x%x, val = 0x%x (0x%x), creg = 0x%x\n",
  504. reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
  505. #endif
  506. do {
  507. if (!(inl(ES_REG(ensoniq, STATUS)) & ES_1370_CSTAT)) {
  508. outw(ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
  509. return;
  510. }
  511. schedule_timeout_uninterruptible(1);
  512. } while (time_after(end_time, jiffies));
  513. dev_err(ensoniq->card->dev, "codec write timeout, status = 0x%x\n",
  514. inl(ES_REG(ensoniq, STATUS)));
  515. }
  516. #endif /* CHIP1370 */
  517. #ifdef CHIP1371
  518. static inline bool is_ev1938(struct ensoniq *ensoniq)
  519. {
  520. return ensoniq->pci->device == 0x8938;
  521. }
  522. static void snd_es1371_codec_write(struct snd_ac97 *ac97,
  523. unsigned short reg, unsigned short val)
  524. {
  525. struct ensoniq *ensoniq = ac97->private_data;
  526. unsigned int t, x, flag;
  527. flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
  528. mutex_lock(&ensoniq->src_mutex);
  529. for (t = 0; t < POLL_COUNT; t++) {
  530. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
  531. /* save the current state for latter */
  532. x = snd_es1371_wait_src_ready(ensoniq);
  533. outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  534. ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
  535. ES_REG(ensoniq, 1371_SMPRATE));
  536. /* wait for not busy (state 0) first to avoid
  537. transition states */
  538. for (t = 0; t < POLL_COUNT; t++) {
  539. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
  540. 0x00000000)
  541. break;
  542. }
  543. /* wait for a SAFE time to write addr/data and then do it, dammit */
  544. for (t = 0; t < POLL_COUNT; t++) {
  545. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
  546. 0x00010000)
  547. break;
  548. }
  549. outl(ES_1371_CODEC_WRITE(reg, val) | flag,
  550. ES_REG(ensoniq, 1371_CODEC));
  551. /* restore SRC reg */
  552. snd_es1371_wait_src_ready(ensoniq);
  553. outl(x, ES_REG(ensoniq, 1371_SMPRATE));
  554. mutex_unlock(&ensoniq->src_mutex);
  555. return;
  556. }
  557. }
  558. mutex_unlock(&ensoniq->src_mutex);
  559. dev_err(ensoniq->card->dev, "codec write timeout at 0x%lx [0x%x]\n",
  560. ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
  561. }
  562. static unsigned short snd_es1371_codec_read(struct snd_ac97 *ac97,
  563. unsigned short reg)
  564. {
  565. struct ensoniq *ensoniq = ac97->private_data;
  566. unsigned int t, x, flag, fail = 0;
  567. flag = is_ev1938(ensoniq) ? EV_1938_CODEC_MAGIC : 0;
  568. __again:
  569. mutex_lock(&ensoniq->src_mutex);
  570. for (t = 0; t < POLL_COUNT; t++) {
  571. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
  572. /* save the current state for latter */
  573. x = snd_es1371_wait_src_ready(ensoniq);
  574. outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  575. ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
  576. ES_REG(ensoniq, 1371_SMPRATE));
  577. /* wait for not busy (state 0) first to avoid
  578. transition states */
  579. for (t = 0; t < POLL_COUNT; t++) {
  580. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
  581. 0x00000000)
  582. break;
  583. }
  584. /* wait for a SAFE time to write addr/data and then do it, dammit */
  585. for (t = 0; t < POLL_COUNT; t++) {
  586. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) ==
  587. 0x00010000)
  588. break;
  589. }
  590. outl(ES_1371_CODEC_READS(reg) | flag,
  591. ES_REG(ensoniq, 1371_CODEC));
  592. /* restore SRC reg */
  593. snd_es1371_wait_src_ready(ensoniq);
  594. outl(x, ES_REG(ensoniq, 1371_SMPRATE));
  595. /* wait for WIP again */
  596. for (t = 0; t < POLL_COUNT; t++) {
  597. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP))
  598. break;
  599. }
  600. /* now wait for the stinkin' data (RDY) */
  601. for (t = 0; t < POLL_COUNT; t++) {
  602. x = inl(ES_REG(ensoniq, 1371_CODEC));
  603. if (x & ES_1371_CODEC_RDY) {
  604. if (is_ev1938(ensoniq)) {
  605. for (t = 0; t < 100; t++)
  606. inl(ES_REG(ensoniq, CONTROL));
  607. x = inl(ES_REG(ensoniq, 1371_CODEC));
  608. }
  609. mutex_unlock(&ensoniq->src_mutex);
  610. return ES_1371_CODEC_READ(x);
  611. }
  612. }
  613. mutex_unlock(&ensoniq->src_mutex);
  614. if (++fail > 10) {
  615. dev_err(ensoniq->card->dev,
  616. "codec read timeout (final) at 0x%lx, reg = 0x%x [0x%x]\n",
  617. ES_REG(ensoniq, 1371_CODEC), reg,
  618. inl(ES_REG(ensoniq, 1371_CODEC)));
  619. return 0;
  620. }
  621. goto __again;
  622. }
  623. }
  624. mutex_unlock(&ensoniq->src_mutex);
  625. dev_err(ensoniq->card->dev, "codec read timeout at 0x%lx [0x%x]\n",
  626. ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
  627. return 0;
  628. }
  629. static void snd_es1371_codec_wait(struct snd_ac97 *ac97)
  630. {
  631. msleep(750);
  632. snd_es1371_codec_read(ac97, AC97_RESET);
  633. snd_es1371_codec_read(ac97, AC97_VENDOR_ID1);
  634. snd_es1371_codec_read(ac97, AC97_VENDOR_ID2);
  635. msleep(50);
  636. }
  637. static void snd_es1371_adc_rate(struct ensoniq * ensoniq, unsigned int rate)
  638. {
  639. unsigned int n, truncm, freq;
  640. mutex_lock(&ensoniq->src_mutex);
  641. n = rate / 3000;
  642. if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
  643. n--;
  644. truncm = (21 * n - 1) | 1;
  645. freq = ((48000UL << 15) / rate) * n;
  646. if (rate >= 24000) {
  647. if (truncm > 239)
  648. truncm = 239;
  649. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
  650. (((239 - truncm) >> 1) << 9) | (n << 4));
  651. } else {
  652. if (truncm > 119)
  653. truncm = 119;
  654. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
  655. 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
  656. }
  657. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
  658. (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC +
  659. ES_SMPREG_INT_REGS) & 0x00ff) |
  660. ((freq >> 5) & 0xfc00));
  661. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
  662. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
  663. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
  664. mutex_unlock(&ensoniq->src_mutex);
  665. }
  666. static void snd_es1371_dac1_rate(struct ensoniq * ensoniq, unsigned int rate)
  667. {
  668. unsigned int freq, r;
  669. mutex_lock(&ensoniq->src_mutex);
  670. freq = DIV_ROUND_CLOSEST(rate << 15, 3000);
  671. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
  672. ES_1371_DIS_P2 | ES_1371_DIS_R1)) |
  673. ES_1371_DIS_P1;
  674. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  675. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
  676. (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 +
  677. ES_SMPREG_INT_REGS) & 0x00ff) |
  678. ((freq >> 5) & 0xfc00));
  679. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
  680. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
  681. ES_1371_DIS_P2 | ES_1371_DIS_R1));
  682. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  683. mutex_unlock(&ensoniq->src_mutex);
  684. }
  685. static void snd_es1371_dac2_rate(struct ensoniq * ensoniq, unsigned int rate)
  686. {
  687. unsigned int freq, r;
  688. mutex_lock(&ensoniq->src_mutex);
  689. freq = DIV_ROUND_CLOSEST(rate << 15, 3000);
  690. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
  691. ES_1371_DIS_P1 | ES_1371_DIS_R1)) |
  692. ES_1371_DIS_P2;
  693. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  694. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
  695. (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 +
  696. ES_SMPREG_INT_REGS) & 0x00ff) |
  697. ((freq >> 5) & 0xfc00));
  698. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC,
  699. freq & 0x7fff);
  700. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE |
  701. ES_1371_DIS_P1 | ES_1371_DIS_R1));
  702. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  703. mutex_unlock(&ensoniq->src_mutex);
  704. }
  705. #endif /* CHIP1371 */
  706. static int snd_ensoniq_trigger(struct snd_pcm_substream *substream, int cmd)
  707. {
  708. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  709. switch (cmd) {
  710. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  711. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  712. {
  713. unsigned int what = 0;
  714. struct snd_pcm_substream *s;
  715. snd_pcm_group_for_each_entry(s, substream) {
  716. if (s == ensoniq->playback1_substream) {
  717. what |= ES_P1_PAUSE;
  718. snd_pcm_trigger_done(s, substream);
  719. } else if (s == ensoniq->playback2_substream) {
  720. what |= ES_P2_PAUSE;
  721. snd_pcm_trigger_done(s, substream);
  722. } else if (s == ensoniq->capture_substream)
  723. return -EINVAL;
  724. }
  725. spin_lock(&ensoniq->reg_lock);
  726. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
  727. ensoniq->sctrl |= what;
  728. else
  729. ensoniq->sctrl &= ~what;
  730. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  731. spin_unlock(&ensoniq->reg_lock);
  732. break;
  733. }
  734. case SNDRV_PCM_TRIGGER_START:
  735. case SNDRV_PCM_TRIGGER_STOP:
  736. {
  737. unsigned int what = 0;
  738. struct snd_pcm_substream *s;
  739. snd_pcm_group_for_each_entry(s, substream) {
  740. if (s == ensoniq->playback1_substream) {
  741. what |= ES_DAC1_EN;
  742. snd_pcm_trigger_done(s, substream);
  743. } else if (s == ensoniq->playback2_substream) {
  744. what |= ES_DAC2_EN;
  745. snd_pcm_trigger_done(s, substream);
  746. } else if (s == ensoniq->capture_substream) {
  747. what |= ES_ADC_EN;
  748. snd_pcm_trigger_done(s, substream);
  749. }
  750. }
  751. spin_lock(&ensoniq->reg_lock);
  752. if (cmd == SNDRV_PCM_TRIGGER_START)
  753. ensoniq->ctrl |= what;
  754. else
  755. ensoniq->ctrl &= ~what;
  756. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  757. spin_unlock(&ensoniq->reg_lock);
  758. break;
  759. }
  760. default:
  761. return -EINVAL;
  762. }
  763. return 0;
  764. }
  765. /*
  766. * PCM part
  767. */
  768. static int snd_ensoniq_playback1_prepare(struct snd_pcm_substream *substream)
  769. {
  770. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  771. struct snd_pcm_runtime *runtime = substream->runtime;
  772. unsigned int mode = 0;
  773. ensoniq->p1_dma_size = snd_pcm_lib_buffer_bytes(substream);
  774. ensoniq->p1_period_size = snd_pcm_lib_period_bytes(substream);
  775. if (snd_pcm_format_width(runtime->format) == 16)
  776. mode |= 0x02;
  777. if (runtime->channels > 1)
  778. mode |= 0x01;
  779. spin_lock_irq(&ensoniq->reg_lock);
  780. ensoniq->ctrl &= ~ES_DAC1_EN;
  781. #ifdef CHIP1371
  782. /* 48k doesn't need SRC (it breaks AC3-passthru) */
  783. if (runtime->rate == 48000)
  784. ensoniq->ctrl |= ES_1373_BYPASS_P1;
  785. else
  786. ensoniq->ctrl &= ~ES_1373_BYPASS_P1;
  787. #endif
  788. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  789. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  790. outl(runtime->dma_addr, ES_REG(ensoniq, DAC1_FRAME));
  791. outl((ensoniq->p1_dma_size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
  792. ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
  793. ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
  794. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  795. outl((ensoniq->p1_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
  796. ES_REG(ensoniq, DAC1_COUNT));
  797. #ifdef CHIP1370
  798. ensoniq->ctrl &= ~ES_1370_WTSRSELM;
  799. switch (runtime->rate) {
  800. case 5512: ensoniq->ctrl |= ES_1370_WTSRSEL(0); break;
  801. case 11025: ensoniq->ctrl |= ES_1370_WTSRSEL(1); break;
  802. case 22050: ensoniq->ctrl |= ES_1370_WTSRSEL(2); break;
  803. case 44100: ensoniq->ctrl |= ES_1370_WTSRSEL(3); break;
  804. default: snd_BUG();
  805. }
  806. #endif
  807. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  808. spin_unlock_irq(&ensoniq->reg_lock);
  809. #ifndef CHIP1370
  810. snd_es1371_dac1_rate(ensoniq, runtime->rate);
  811. #endif
  812. return 0;
  813. }
  814. static int snd_ensoniq_playback2_prepare(struct snd_pcm_substream *substream)
  815. {
  816. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  817. struct snd_pcm_runtime *runtime = substream->runtime;
  818. unsigned int mode = 0;
  819. ensoniq->p2_dma_size = snd_pcm_lib_buffer_bytes(substream);
  820. ensoniq->p2_period_size = snd_pcm_lib_period_bytes(substream);
  821. if (snd_pcm_format_width(runtime->format) == 16)
  822. mode |= 0x02;
  823. if (runtime->channels > 1)
  824. mode |= 0x01;
  825. spin_lock_irq(&ensoniq->reg_lock);
  826. ensoniq->ctrl &= ~ES_DAC2_EN;
  827. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  828. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  829. outl(runtime->dma_addr, ES_REG(ensoniq, DAC2_FRAME));
  830. outl((ensoniq->p2_dma_size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
  831. ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
  832. ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
  833. ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
  834. ES_P2_END_INCO(mode & 2 ? 2 : 1) | ES_P2_ST_INCO(0);
  835. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  836. outl((ensoniq->p2_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
  837. ES_REG(ensoniq, DAC2_COUNT));
  838. #ifdef CHIP1370
  839. if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_CAPTURE)) {
  840. ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
  841. ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
  842. ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
  843. }
  844. #endif
  845. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  846. spin_unlock_irq(&ensoniq->reg_lock);
  847. #ifndef CHIP1370
  848. snd_es1371_dac2_rate(ensoniq, runtime->rate);
  849. #endif
  850. return 0;
  851. }
  852. static int snd_ensoniq_capture_prepare(struct snd_pcm_substream *substream)
  853. {
  854. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  855. struct snd_pcm_runtime *runtime = substream->runtime;
  856. unsigned int mode = 0;
  857. ensoniq->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
  858. ensoniq->c_period_size = snd_pcm_lib_period_bytes(substream);
  859. if (snd_pcm_format_width(runtime->format) == 16)
  860. mode |= 0x02;
  861. if (runtime->channels > 1)
  862. mode |= 0x01;
  863. spin_lock_irq(&ensoniq->reg_lock);
  864. ensoniq->ctrl &= ~ES_ADC_EN;
  865. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  866. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  867. outl(runtime->dma_addr, ES_REG(ensoniq, ADC_FRAME));
  868. outl((ensoniq->c_dma_size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
  869. ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
  870. ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
  871. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  872. outl((ensoniq->c_period_size >> snd_ensoniq_sample_shift[mode]) - 1,
  873. ES_REG(ensoniq, ADC_COUNT));
  874. #ifdef CHIP1370
  875. if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_PLAY2)) {
  876. ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
  877. ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
  878. ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
  879. }
  880. #endif
  881. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  882. spin_unlock_irq(&ensoniq->reg_lock);
  883. #ifndef CHIP1370
  884. snd_es1371_adc_rate(ensoniq, runtime->rate);
  885. #endif
  886. return 0;
  887. }
  888. static snd_pcm_uframes_t snd_ensoniq_playback1_pointer(struct snd_pcm_substream *substream)
  889. {
  890. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  891. size_t ptr;
  892. spin_lock(&ensoniq->reg_lock);
  893. if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC1_EN) {
  894. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  895. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC1_SIZE)));
  896. ptr = bytes_to_frames(substream->runtime, ptr);
  897. } else {
  898. ptr = 0;
  899. }
  900. spin_unlock(&ensoniq->reg_lock);
  901. return ptr;
  902. }
  903. static snd_pcm_uframes_t snd_ensoniq_playback2_pointer(struct snd_pcm_substream *substream)
  904. {
  905. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  906. size_t ptr;
  907. spin_lock(&ensoniq->reg_lock);
  908. if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC2_EN) {
  909. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  910. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC2_SIZE)));
  911. ptr = bytes_to_frames(substream->runtime, ptr);
  912. } else {
  913. ptr = 0;
  914. }
  915. spin_unlock(&ensoniq->reg_lock);
  916. return ptr;
  917. }
  918. static snd_pcm_uframes_t snd_ensoniq_capture_pointer(struct snd_pcm_substream *substream)
  919. {
  920. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  921. size_t ptr;
  922. spin_lock(&ensoniq->reg_lock);
  923. if (inl(ES_REG(ensoniq, CONTROL)) & ES_ADC_EN) {
  924. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  925. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, ADC_SIZE)));
  926. ptr = bytes_to_frames(substream->runtime, ptr);
  927. } else {
  928. ptr = 0;
  929. }
  930. spin_unlock(&ensoniq->reg_lock);
  931. return ptr;
  932. }
  933. static const struct snd_pcm_hardware snd_ensoniq_playback1 =
  934. {
  935. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  936. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  937. SNDRV_PCM_INFO_MMAP_VALID |
  938. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
  939. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  940. .rates =
  941. #ifndef CHIP1370
  942. SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  943. #else
  944. (SNDRV_PCM_RATE_KNOT | /* 5512Hz rate */
  945. SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 |
  946. SNDRV_PCM_RATE_44100),
  947. #endif
  948. .rate_min = 4000,
  949. .rate_max = 48000,
  950. .channels_min = 1,
  951. .channels_max = 2,
  952. .buffer_bytes_max = (128*1024),
  953. .period_bytes_min = 64,
  954. .period_bytes_max = (128*1024),
  955. .periods_min = 1,
  956. .periods_max = 1024,
  957. .fifo_size = 0,
  958. };
  959. static const struct snd_pcm_hardware snd_ensoniq_playback2 =
  960. {
  961. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  962. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  963. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
  964. SNDRV_PCM_INFO_SYNC_START),
  965. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  966. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  967. .rate_min = 4000,
  968. .rate_max = 48000,
  969. .channels_min = 1,
  970. .channels_max = 2,
  971. .buffer_bytes_max = (128*1024),
  972. .period_bytes_min = 64,
  973. .period_bytes_max = (128*1024),
  974. .periods_min = 1,
  975. .periods_max = 1024,
  976. .fifo_size = 0,
  977. };
  978. static const struct snd_pcm_hardware snd_ensoniq_capture =
  979. {
  980. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  981. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  982. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  983. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  984. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  985. .rate_min = 4000,
  986. .rate_max = 48000,
  987. .channels_min = 1,
  988. .channels_max = 2,
  989. .buffer_bytes_max = (128*1024),
  990. .period_bytes_min = 64,
  991. .period_bytes_max = (128*1024),
  992. .periods_min = 1,
  993. .periods_max = 1024,
  994. .fifo_size = 0,
  995. };
  996. static int snd_ensoniq_playback1_open(struct snd_pcm_substream *substream)
  997. {
  998. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  999. struct snd_pcm_runtime *runtime = substream->runtime;
  1000. ensoniq->mode |= ES_MODE_PLAY1;
  1001. ensoniq->playback1_substream = substream;
  1002. runtime->hw = snd_ensoniq_playback1;
  1003. snd_pcm_set_sync(substream);
  1004. spin_lock_irq(&ensoniq->reg_lock);
  1005. if (ensoniq->spdif && ensoniq->playback2_substream == NULL)
  1006. ensoniq->spdif_stream = ensoniq->spdif_default;
  1007. spin_unlock_irq(&ensoniq->reg_lock);
  1008. #ifdef CHIP1370
  1009. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1010. &snd_es1370_hw_constraints_rates);
  1011. #else
  1012. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1013. &snd_es1371_hw_constraints_dac_clock);
  1014. #endif
  1015. return 0;
  1016. }
  1017. static int snd_ensoniq_playback2_open(struct snd_pcm_substream *substream)
  1018. {
  1019. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  1020. struct snd_pcm_runtime *runtime = substream->runtime;
  1021. ensoniq->mode |= ES_MODE_PLAY2;
  1022. ensoniq->playback2_substream = substream;
  1023. runtime->hw = snd_ensoniq_playback2;
  1024. snd_pcm_set_sync(substream);
  1025. spin_lock_irq(&ensoniq->reg_lock);
  1026. if (ensoniq->spdif && ensoniq->playback1_substream == NULL)
  1027. ensoniq->spdif_stream = ensoniq->spdif_default;
  1028. spin_unlock_irq(&ensoniq->reg_lock);
  1029. #ifdef CHIP1370
  1030. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1031. &snd_es1370_hw_constraints_clock);
  1032. #else
  1033. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1034. &snd_es1371_hw_constraints_dac_clock);
  1035. #endif
  1036. return 0;
  1037. }
  1038. static int snd_ensoniq_capture_open(struct snd_pcm_substream *substream)
  1039. {
  1040. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  1041. struct snd_pcm_runtime *runtime = substream->runtime;
  1042. ensoniq->mode |= ES_MODE_CAPTURE;
  1043. ensoniq->capture_substream = substream;
  1044. runtime->hw = snd_ensoniq_capture;
  1045. snd_pcm_set_sync(substream);
  1046. #ifdef CHIP1370
  1047. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1048. &snd_es1370_hw_constraints_clock);
  1049. #else
  1050. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1051. &snd_es1371_hw_constraints_adc_clock);
  1052. #endif
  1053. return 0;
  1054. }
  1055. static int snd_ensoniq_playback1_close(struct snd_pcm_substream *substream)
  1056. {
  1057. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  1058. ensoniq->playback1_substream = NULL;
  1059. ensoniq->mode &= ~ES_MODE_PLAY1;
  1060. return 0;
  1061. }
  1062. static int snd_ensoniq_playback2_close(struct snd_pcm_substream *substream)
  1063. {
  1064. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  1065. ensoniq->playback2_substream = NULL;
  1066. spin_lock_irq(&ensoniq->reg_lock);
  1067. #ifdef CHIP1370
  1068. ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
  1069. #endif
  1070. ensoniq->mode &= ~ES_MODE_PLAY2;
  1071. spin_unlock_irq(&ensoniq->reg_lock);
  1072. return 0;
  1073. }
  1074. static int snd_ensoniq_capture_close(struct snd_pcm_substream *substream)
  1075. {
  1076. struct ensoniq *ensoniq = snd_pcm_substream_chip(substream);
  1077. ensoniq->capture_substream = NULL;
  1078. spin_lock_irq(&ensoniq->reg_lock);
  1079. #ifdef CHIP1370
  1080. ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
  1081. #endif
  1082. ensoniq->mode &= ~ES_MODE_CAPTURE;
  1083. spin_unlock_irq(&ensoniq->reg_lock);
  1084. return 0;
  1085. }
  1086. static const struct snd_pcm_ops snd_ensoniq_playback1_ops = {
  1087. .open = snd_ensoniq_playback1_open,
  1088. .close = snd_ensoniq_playback1_close,
  1089. .prepare = snd_ensoniq_playback1_prepare,
  1090. .trigger = snd_ensoniq_trigger,
  1091. .pointer = snd_ensoniq_playback1_pointer,
  1092. };
  1093. static const struct snd_pcm_ops snd_ensoniq_playback2_ops = {
  1094. .open = snd_ensoniq_playback2_open,
  1095. .close = snd_ensoniq_playback2_close,
  1096. .prepare = snd_ensoniq_playback2_prepare,
  1097. .trigger = snd_ensoniq_trigger,
  1098. .pointer = snd_ensoniq_playback2_pointer,
  1099. };
  1100. static const struct snd_pcm_ops snd_ensoniq_capture_ops = {
  1101. .open = snd_ensoniq_capture_open,
  1102. .close = snd_ensoniq_capture_close,
  1103. .prepare = snd_ensoniq_capture_prepare,
  1104. .trigger = snd_ensoniq_trigger,
  1105. .pointer = snd_ensoniq_capture_pointer,
  1106. };
  1107. static const struct snd_pcm_chmap_elem surround_map[] = {
  1108. { .channels = 1,
  1109. .map = { SNDRV_CHMAP_MONO } },
  1110. { .channels = 2,
  1111. .map = { SNDRV_CHMAP_RL, SNDRV_CHMAP_RR } },
  1112. { }
  1113. };
  1114. static int snd_ensoniq_pcm(struct ensoniq *ensoniq, int device)
  1115. {
  1116. struct snd_pcm *pcm;
  1117. int err;
  1118. err = snd_pcm_new(ensoniq->card, CHIP_NAME "/1", device, 1, 1, &pcm);
  1119. if (err < 0)
  1120. return err;
  1121. #ifdef CHIP1370
  1122. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
  1123. #else
  1124. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
  1125. #endif
  1126. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ensoniq_capture_ops);
  1127. pcm->private_data = ensoniq;
  1128. pcm->info_flags = 0;
  1129. strcpy(pcm->name, CHIP_NAME " DAC2/ADC");
  1130. ensoniq->pcm1 = pcm;
  1131. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  1132. &ensoniq->pci->dev, 64*1024, 128*1024);
  1133. #ifdef CHIP1370
  1134. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1135. surround_map, 2, 0, NULL);
  1136. #else
  1137. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1138. snd_pcm_std_chmaps, 2, 0, NULL);
  1139. #endif
  1140. return err;
  1141. }
  1142. static int snd_ensoniq_pcm2(struct ensoniq *ensoniq, int device)
  1143. {
  1144. struct snd_pcm *pcm;
  1145. int err;
  1146. err = snd_pcm_new(ensoniq->card, CHIP_NAME "/2", device, 1, 0, &pcm);
  1147. if (err < 0)
  1148. return err;
  1149. #ifdef CHIP1370
  1150. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
  1151. #else
  1152. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
  1153. #endif
  1154. pcm->private_data = ensoniq;
  1155. pcm->info_flags = 0;
  1156. strcpy(pcm->name, CHIP_NAME " DAC1");
  1157. ensoniq->pcm2 = pcm;
  1158. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  1159. &ensoniq->pci->dev, 64*1024, 128*1024);
  1160. #ifdef CHIP1370
  1161. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1162. snd_pcm_std_chmaps, 2, 0, NULL);
  1163. #else
  1164. err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
  1165. surround_map, 2, 0, NULL);
  1166. #endif
  1167. return err;
  1168. }
  1169. /*
  1170. * Mixer section
  1171. */
  1172. /*
  1173. * ENS1371 mixer (including SPDIF interface)
  1174. */
  1175. #ifdef CHIP1371
  1176. static int snd_ens1373_spdif_info(struct snd_kcontrol *kcontrol,
  1177. struct snd_ctl_elem_info *uinfo)
  1178. {
  1179. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1180. uinfo->count = 1;
  1181. return 0;
  1182. }
  1183. static int snd_ens1373_spdif_default_get(struct snd_kcontrol *kcontrol,
  1184. struct snd_ctl_elem_value *ucontrol)
  1185. {
  1186. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1187. spin_lock_irq(&ensoniq->reg_lock);
  1188. ucontrol->value.iec958.status[0] = (ensoniq->spdif_default >> 0) & 0xff;
  1189. ucontrol->value.iec958.status[1] = (ensoniq->spdif_default >> 8) & 0xff;
  1190. ucontrol->value.iec958.status[2] = (ensoniq->spdif_default >> 16) & 0xff;
  1191. ucontrol->value.iec958.status[3] = (ensoniq->spdif_default >> 24) & 0xff;
  1192. spin_unlock_irq(&ensoniq->reg_lock);
  1193. return 0;
  1194. }
  1195. static int snd_ens1373_spdif_default_put(struct snd_kcontrol *kcontrol,
  1196. struct snd_ctl_elem_value *ucontrol)
  1197. {
  1198. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1199. unsigned int val;
  1200. int change;
  1201. val = ((u32)ucontrol->value.iec958.status[0] << 0) |
  1202. ((u32)ucontrol->value.iec958.status[1] << 8) |
  1203. ((u32)ucontrol->value.iec958.status[2] << 16) |
  1204. ((u32)ucontrol->value.iec958.status[3] << 24);
  1205. spin_lock_irq(&ensoniq->reg_lock);
  1206. change = ensoniq->spdif_default != val;
  1207. ensoniq->spdif_default = val;
  1208. if (change && ensoniq->playback1_substream == NULL &&
  1209. ensoniq->playback2_substream == NULL)
  1210. outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
  1211. spin_unlock_irq(&ensoniq->reg_lock);
  1212. return change;
  1213. }
  1214. static int snd_ens1373_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1215. struct snd_ctl_elem_value *ucontrol)
  1216. {
  1217. ucontrol->value.iec958.status[0] = 0xff;
  1218. ucontrol->value.iec958.status[1] = 0xff;
  1219. ucontrol->value.iec958.status[2] = 0xff;
  1220. ucontrol->value.iec958.status[3] = 0xff;
  1221. return 0;
  1222. }
  1223. static int snd_ens1373_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1224. struct snd_ctl_elem_value *ucontrol)
  1225. {
  1226. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1227. spin_lock_irq(&ensoniq->reg_lock);
  1228. ucontrol->value.iec958.status[0] = (ensoniq->spdif_stream >> 0) & 0xff;
  1229. ucontrol->value.iec958.status[1] = (ensoniq->spdif_stream >> 8) & 0xff;
  1230. ucontrol->value.iec958.status[2] = (ensoniq->spdif_stream >> 16) & 0xff;
  1231. ucontrol->value.iec958.status[3] = (ensoniq->spdif_stream >> 24) & 0xff;
  1232. spin_unlock_irq(&ensoniq->reg_lock);
  1233. return 0;
  1234. }
  1235. static int snd_ens1373_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1236. struct snd_ctl_elem_value *ucontrol)
  1237. {
  1238. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1239. unsigned int val;
  1240. int change;
  1241. val = ((u32)ucontrol->value.iec958.status[0] << 0) |
  1242. ((u32)ucontrol->value.iec958.status[1] << 8) |
  1243. ((u32)ucontrol->value.iec958.status[2] << 16) |
  1244. ((u32)ucontrol->value.iec958.status[3] << 24);
  1245. spin_lock_irq(&ensoniq->reg_lock);
  1246. change = ensoniq->spdif_stream != val;
  1247. ensoniq->spdif_stream = val;
  1248. if (change && (ensoniq->playback1_substream != NULL ||
  1249. ensoniq->playback2_substream != NULL))
  1250. outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
  1251. spin_unlock_irq(&ensoniq->reg_lock);
  1252. return change;
  1253. }
  1254. #define ES1371_SPDIF(xname) \
  1255. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_es1371_spdif_info, \
  1256. .get = snd_es1371_spdif_get, .put = snd_es1371_spdif_put }
  1257. #define snd_es1371_spdif_info snd_ctl_boolean_mono_info
  1258. static int snd_es1371_spdif_get(struct snd_kcontrol *kcontrol,
  1259. struct snd_ctl_elem_value *ucontrol)
  1260. {
  1261. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1262. spin_lock_irq(&ensoniq->reg_lock);
  1263. ucontrol->value.integer.value[0] = ensoniq->ctrl & ES_1373_SPDIF_THRU ? 1 : 0;
  1264. spin_unlock_irq(&ensoniq->reg_lock);
  1265. return 0;
  1266. }
  1267. static int snd_es1371_spdif_put(struct snd_kcontrol *kcontrol,
  1268. struct snd_ctl_elem_value *ucontrol)
  1269. {
  1270. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1271. unsigned int nval1, nval2;
  1272. int change;
  1273. nval1 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_THRU : 0;
  1274. nval2 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_EN : 0;
  1275. spin_lock_irq(&ensoniq->reg_lock);
  1276. change = (ensoniq->ctrl & ES_1373_SPDIF_THRU) != nval1;
  1277. ensoniq->ctrl &= ~ES_1373_SPDIF_THRU;
  1278. ensoniq->ctrl |= nval1;
  1279. ensoniq->cssr &= ~ES_1373_SPDIF_EN;
  1280. ensoniq->cssr |= nval2;
  1281. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1282. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1283. spin_unlock_irq(&ensoniq->reg_lock);
  1284. return change;
  1285. }
  1286. /* spdif controls */
  1287. static const struct snd_kcontrol_new snd_es1371_mixer_spdif[] = {
  1288. ES1371_SPDIF(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH)),
  1289. {
  1290. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1291. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1292. .info = snd_ens1373_spdif_info,
  1293. .get = snd_ens1373_spdif_default_get,
  1294. .put = snd_ens1373_spdif_default_put,
  1295. },
  1296. {
  1297. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1298. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1299. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1300. .info = snd_ens1373_spdif_info,
  1301. .get = snd_ens1373_spdif_mask_get
  1302. },
  1303. {
  1304. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1305. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  1306. .info = snd_ens1373_spdif_info,
  1307. .get = snd_ens1373_spdif_stream_get,
  1308. .put = snd_ens1373_spdif_stream_put
  1309. },
  1310. };
  1311. #define snd_es1373_rear_info snd_ctl_boolean_mono_info
  1312. static int snd_es1373_rear_get(struct snd_kcontrol *kcontrol,
  1313. struct snd_ctl_elem_value *ucontrol)
  1314. {
  1315. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1316. int val = 0;
  1317. spin_lock_irq(&ensoniq->reg_lock);
  1318. if ((ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|
  1319. ES_1373_REAR_BIT24)) == ES_1373_REAR_BIT26)
  1320. val = 1;
  1321. ucontrol->value.integer.value[0] = val;
  1322. spin_unlock_irq(&ensoniq->reg_lock);
  1323. return 0;
  1324. }
  1325. static int snd_es1373_rear_put(struct snd_kcontrol *kcontrol,
  1326. struct snd_ctl_elem_value *ucontrol)
  1327. {
  1328. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1329. unsigned int nval1;
  1330. int change;
  1331. nval1 = ucontrol->value.integer.value[0] ?
  1332. ES_1373_REAR_BIT26 : (ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
  1333. spin_lock_irq(&ensoniq->reg_lock);
  1334. change = (ensoniq->cssr & (ES_1373_REAR_BIT27|
  1335. ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) != nval1;
  1336. ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24);
  1337. ensoniq->cssr |= nval1;
  1338. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1339. spin_unlock_irq(&ensoniq->reg_lock);
  1340. return change;
  1341. }
  1342. static const struct snd_kcontrol_new snd_ens1373_rear =
  1343. {
  1344. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1345. .name = "AC97 2ch->4ch Copy Switch",
  1346. .info = snd_es1373_rear_info,
  1347. .get = snd_es1373_rear_get,
  1348. .put = snd_es1373_rear_put,
  1349. };
  1350. #define snd_es1373_line_info snd_ctl_boolean_mono_info
  1351. static int snd_es1373_line_get(struct snd_kcontrol *kcontrol,
  1352. struct snd_ctl_elem_value *ucontrol)
  1353. {
  1354. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1355. int val = 0;
  1356. spin_lock_irq(&ensoniq->reg_lock);
  1357. if (ensoniq->ctrl & ES_1371_GPIO_OUT(4))
  1358. val = 1;
  1359. ucontrol->value.integer.value[0] = val;
  1360. spin_unlock_irq(&ensoniq->reg_lock);
  1361. return 0;
  1362. }
  1363. static int snd_es1373_line_put(struct snd_kcontrol *kcontrol,
  1364. struct snd_ctl_elem_value *ucontrol)
  1365. {
  1366. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1367. int changed;
  1368. unsigned int ctrl;
  1369. spin_lock_irq(&ensoniq->reg_lock);
  1370. ctrl = ensoniq->ctrl;
  1371. if (ucontrol->value.integer.value[0])
  1372. ensoniq->ctrl |= ES_1371_GPIO_OUT(4); /* switch line-in -> rear out */
  1373. else
  1374. ensoniq->ctrl &= ~ES_1371_GPIO_OUT(4);
  1375. changed = (ctrl != ensoniq->ctrl);
  1376. if (changed)
  1377. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1378. spin_unlock_irq(&ensoniq->reg_lock);
  1379. return changed;
  1380. }
  1381. static const struct snd_kcontrol_new snd_ens1373_line =
  1382. {
  1383. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1384. .name = "Line In->Rear Out Switch",
  1385. .info = snd_es1373_line_info,
  1386. .get = snd_es1373_line_get,
  1387. .put = snd_es1373_line_put,
  1388. };
  1389. static void snd_ensoniq_mixer_free_ac97(struct snd_ac97 *ac97)
  1390. {
  1391. struct ensoniq *ensoniq = ac97->private_data;
  1392. ensoniq->u.es1371.ac97 = NULL;
  1393. }
  1394. struct es1371_quirk {
  1395. unsigned short vid; /* vendor ID */
  1396. unsigned short did; /* device ID */
  1397. unsigned char rev; /* revision */
  1398. };
  1399. static int es1371_quirk_lookup(struct ensoniq *ensoniq,
  1400. const struct es1371_quirk *list)
  1401. {
  1402. while (list->vid != (unsigned short)PCI_ANY_ID) {
  1403. if (ensoniq->pci->vendor == list->vid &&
  1404. ensoniq->pci->device == list->did &&
  1405. ensoniq->rev == list->rev)
  1406. return 1;
  1407. list++;
  1408. }
  1409. return 0;
  1410. }
  1411. static const struct es1371_quirk es1371_spdif_present[] = {
  1412. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
  1413. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
  1414. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
  1415. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
  1416. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
  1417. { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
  1418. };
  1419. static const struct snd_pci_quirk ens1373_line_quirk[] = {
  1420. SND_PCI_QUIRK_ID(0x1274, 0x2000), /* GA-7DXR */
  1421. SND_PCI_QUIRK_ID(0x1458, 0xa000), /* GA-8IEXP */
  1422. { } /* end */
  1423. };
  1424. static int snd_ensoniq_1371_mixer(struct ensoniq *ensoniq,
  1425. int has_spdif, int has_line)
  1426. {
  1427. struct snd_card *card = ensoniq->card;
  1428. struct snd_ac97_bus *pbus;
  1429. struct snd_ac97_template ac97;
  1430. int err;
  1431. static const struct snd_ac97_bus_ops ops = {
  1432. .write = snd_es1371_codec_write,
  1433. .read = snd_es1371_codec_read,
  1434. .wait = snd_es1371_codec_wait,
  1435. };
  1436. err = snd_ac97_bus(card, 0, &ops, NULL, &pbus);
  1437. if (err < 0)
  1438. return err;
  1439. memset(&ac97, 0, sizeof(ac97));
  1440. ac97.private_data = ensoniq;
  1441. ac97.private_free = snd_ensoniq_mixer_free_ac97;
  1442. ac97.pci = ensoniq->pci;
  1443. ac97.scaps = AC97_SCAP_AUDIO;
  1444. err = snd_ac97_mixer(pbus, &ac97, &ensoniq->u.es1371.ac97);
  1445. if (err < 0)
  1446. return err;
  1447. if (has_spdif > 0 ||
  1448. (!has_spdif && es1371_quirk_lookup(ensoniq, es1371_spdif_present))) {
  1449. struct snd_kcontrol *kctl;
  1450. int i, is_spdif = 0;
  1451. ensoniq->spdif_default = ensoniq->spdif_stream =
  1452. SNDRV_PCM_DEFAULT_CON_SPDIF;
  1453. outl(ensoniq->spdif_default, ES_REG(ensoniq, CHANNEL_STATUS));
  1454. if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SPDIF)
  1455. is_spdif++;
  1456. for (i = 0; i < ARRAY_SIZE(snd_es1371_mixer_spdif); i++) {
  1457. kctl = snd_ctl_new1(&snd_es1371_mixer_spdif[i], ensoniq);
  1458. if (!kctl)
  1459. return -ENOMEM;
  1460. kctl->id.index = is_spdif;
  1461. err = snd_ctl_add(card, kctl);
  1462. if (err < 0)
  1463. return err;
  1464. }
  1465. }
  1466. if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SDAC) {
  1467. /* mirror rear to front speakers */
  1468. ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
  1469. ensoniq->cssr |= ES_1373_REAR_BIT26;
  1470. err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_rear, ensoniq));
  1471. if (err < 0)
  1472. return err;
  1473. }
  1474. if (has_line > 0 ||
  1475. snd_pci_quirk_lookup(ensoniq->pci, ens1373_line_quirk)) {
  1476. err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_line,
  1477. ensoniq));
  1478. if (err < 0)
  1479. return err;
  1480. }
  1481. return 0;
  1482. }
  1483. #endif /* CHIP1371 */
  1484. /* generic control callbacks for ens1370 */
  1485. #ifdef CHIP1370
  1486. #define ENSONIQ_CONTROL(xname, mask) \
  1487. { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, .info = snd_ensoniq_control_info, \
  1488. .get = snd_ensoniq_control_get, .put = snd_ensoniq_control_put, \
  1489. .private_value = mask }
  1490. #define snd_ensoniq_control_info snd_ctl_boolean_mono_info
  1491. static int snd_ensoniq_control_get(struct snd_kcontrol *kcontrol,
  1492. struct snd_ctl_elem_value *ucontrol)
  1493. {
  1494. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1495. int mask = kcontrol->private_value;
  1496. spin_lock_irq(&ensoniq->reg_lock);
  1497. ucontrol->value.integer.value[0] = ensoniq->ctrl & mask ? 1 : 0;
  1498. spin_unlock_irq(&ensoniq->reg_lock);
  1499. return 0;
  1500. }
  1501. static int snd_ensoniq_control_put(struct snd_kcontrol *kcontrol,
  1502. struct snd_ctl_elem_value *ucontrol)
  1503. {
  1504. struct ensoniq *ensoniq = snd_kcontrol_chip(kcontrol);
  1505. int mask = kcontrol->private_value;
  1506. unsigned int nval;
  1507. int change;
  1508. nval = ucontrol->value.integer.value[0] ? mask : 0;
  1509. spin_lock_irq(&ensoniq->reg_lock);
  1510. change = (ensoniq->ctrl & mask) != nval;
  1511. ensoniq->ctrl &= ~mask;
  1512. ensoniq->ctrl |= nval;
  1513. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1514. spin_unlock_irq(&ensoniq->reg_lock);
  1515. return change;
  1516. }
  1517. /*
  1518. * ENS1370 mixer
  1519. */
  1520. static const struct snd_kcontrol_new snd_es1370_controls[2] = {
  1521. ENSONIQ_CONTROL("PCM 0 Output also on Line-In Jack", ES_1370_XCTL0),
  1522. ENSONIQ_CONTROL("Mic +5V bias", ES_1370_XCTL1)
  1523. };
  1524. #define ES1370_CONTROLS ARRAY_SIZE(snd_es1370_controls)
  1525. static void snd_ensoniq_mixer_free_ak4531(struct snd_ak4531 *ak4531)
  1526. {
  1527. struct ensoniq *ensoniq = ak4531->private_data;
  1528. ensoniq->u.es1370.ak4531 = NULL;
  1529. }
  1530. static int snd_ensoniq_1370_mixer(struct ensoniq *ensoniq)
  1531. {
  1532. struct snd_card *card = ensoniq->card;
  1533. struct snd_ak4531 ak4531;
  1534. unsigned int idx;
  1535. int err;
  1536. /* try reset AK4531 */
  1537. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
  1538. inw(ES_REG(ensoniq, 1370_CODEC));
  1539. udelay(100);
  1540. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
  1541. inw(ES_REG(ensoniq, 1370_CODEC));
  1542. udelay(100);
  1543. memset(&ak4531, 0, sizeof(ak4531));
  1544. ak4531.write = snd_es1370_codec_write;
  1545. ak4531.private_data = ensoniq;
  1546. ak4531.private_free = snd_ensoniq_mixer_free_ak4531;
  1547. err = snd_ak4531_mixer(card, &ak4531, &ensoniq->u.es1370.ak4531);
  1548. if (err < 0)
  1549. return err;
  1550. for (idx = 0; idx < ES1370_CONTROLS; idx++) {
  1551. err = snd_ctl_add(card, snd_ctl_new1(&snd_es1370_controls[idx], ensoniq));
  1552. if (err < 0)
  1553. return err;
  1554. }
  1555. return 0;
  1556. }
  1557. #endif /* CHIP1370 */
  1558. #ifdef SUPPORT_JOYSTICK
  1559. #ifdef CHIP1371
  1560. static int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
  1561. {
  1562. switch (joystick_port[dev]) {
  1563. case 0: /* disabled */
  1564. case 1: /* auto-detect */
  1565. case 0x200:
  1566. case 0x208:
  1567. case 0x210:
  1568. case 0x218:
  1569. return joystick_port[dev];
  1570. default:
  1571. dev_err(ensoniq->card->dev,
  1572. "invalid joystick port %#x", joystick_port[dev]);
  1573. return 0;
  1574. }
  1575. }
  1576. #else
  1577. static int snd_ensoniq_get_joystick_port(struct ensoniq *ensoniq, int dev)
  1578. {
  1579. return joystick[dev] ? 0x200 : 0;
  1580. }
  1581. #endif
  1582. static int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, int dev)
  1583. {
  1584. struct gameport *gp;
  1585. int io_port;
  1586. io_port = snd_ensoniq_get_joystick_port(ensoniq, dev);
  1587. switch (io_port) {
  1588. case 0:
  1589. return -ENOSYS;
  1590. case 1: /* auto_detect */
  1591. for (io_port = 0x200; io_port <= 0x218; io_port += 8)
  1592. if (request_region(io_port, 8, "ens137x: gameport"))
  1593. break;
  1594. if (io_port > 0x218) {
  1595. dev_warn(ensoniq->card->dev,
  1596. "no gameport ports available\n");
  1597. return -EBUSY;
  1598. }
  1599. break;
  1600. default:
  1601. if (!request_region(io_port, 8, "ens137x: gameport")) {
  1602. dev_warn(ensoniq->card->dev,
  1603. "gameport io port %#x in use\n",
  1604. io_port);
  1605. return -EBUSY;
  1606. }
  1607. break;
  1608. }
  1609. ensoniq->gameport = gp = gameport_allocate_port();
  1610. if (!gp) {
  1611. dev_err(ensoniq->card->dev,
  1612. "cannot allocate memory for gameport\n");
  1613. release_region(io_port, 8);
  1614. return -ENOMEM;
  1615. }
  1616. gameport_set_name(gp, "ES137x");
  1617. gameport_set_phys(gp, "pci%s/gameport0", pci_name(ensoniq->pci));
  1618. gameport_set_dev_parent(gp, &ensoniq->pci->dev);
  1619. gp->io = io_port;
  1620. ensoniq->ctrl |= ES_JYSTK_EN;
  1621. #ifdef CHIP1371
  1622. ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
  1623. ensoniq->ctrl |= ES_1371_JOY_ASEL((io_port - 0x200) / 8);
  1624. #endif
  1625. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1626. gameport_register_port(ensoniq->gameport);
  1627. return 0;
  1628. }
  1629. static void snd_ensoniq_free_gameport(struct ensoniq *ensoniq)
  1630. {
  1631. if (ensoniq->gameport) {
  1632. int port = ensoniq->gameport->io;
  1633. gameport_unregister_port(ensoniq->gameport);
  1634. ensoniq->gameport = NULL;
  1635. ensoniq->ctrl &= ~ES_JYSTK_EN;
  1636. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1637. release_region(port, 8);
  1638. }
  1639. }
  1640. #else
  1641. static inline int snd_ensoniq_create_gameport(struct ensoniq *ensoniq, long port) { return -ENOSYS; }
  1642. static inline void snd_ensoniq_free_gameport(struct ensoniq *ensoniq) { }
  1643. #endif /* SUPPORT_JOYSTICK */
  1644. /*
  1645. */
  1646. static void snd_ensoniq_proc_read(struct snd_info_entry *entry,
  1647. struct snd_info_buffer *buffer)
  1648. {
  1649. struct ensoniq *ensoniq = entry->private_data;
  1650. snd_iprintf(buffer, "Ensoniq AudioPCI " CHIP_NAME "\n\n");
  1651. snd_iprintf(buffer, "Joystick enable : %s\n",
  1652. ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
  1653. #ifdef CHIP1370
  1654. snd_iprintf(buffer, "MIC +5V bias : %s\n",
  1655. ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
  1656. snd_iprintf(buffer, "Line In to AOUT : %s\n",
  1657. ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
  1658. #else
  1659. snd_iprintf(buffer, "Joystick port : 0x%x\n",
  1660. (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
  1661. #endif
  1662. }
  1663. static void snd_ensoniq_proc_init(struct ensoniq *ensoniq)
  1664. {
  1665. snd_card_ro_proc_new(ensoniq->card, "audiopci", ensoniq,
  1666. snd_ensoniq_proc_read);
  1667. }
  1668. /*
  1669. */
  1670. static void snd_ensoniq_free(struct snd_card *card)
  1671. {
  1672. struct ensoniq *ensoniq = card->private_data;
  1673. snd_ensoniq_free_gameport(ensoniq);
  1674. #ifdef CHIP1370
  1675. outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL)); /* switch everything off */
  1676. outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
  1677. #else
  1678. outl(0, ES_REG(ensoniq, CONTROL)); /* switch everything off */
  1679. outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
  1680. #endif
  1681. }
  1682. #ifdef CHIP1371
  1683. static const struct snd_pci_quirk es1371_amplifier_hack[] = {
  1684. SND_PCI_QUIRK_ID(0x107b, 0x2150), /* Gateway Solo 2150 */
  1685. SND_PCI_QUIRK_ID(0x13bd, 0x100c), /* EV1938 on Mebius PC-MJ100V */
  1686. SND_PCI_QUIRK_ID(0x1102, 0x5938), /* Targa Xtender300 */
  1687. SND_PCI_QUIRK_ID(0x1102, 0x8938), /* IPC Topnote G notebook */
  1688. { } /* end */
  1689. };
  1690. static const struct es1371_quirk es1371_ac97_reset_hack[] = {
  1691. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
  1692. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
  1693. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
  1694. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
  1695. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
  1696. { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
  1697. };
  1698. #endif
  1699. static void snd_ensoniq_chip_init(struct ensoniq *ensoniq)
  1700. {
  1701. #ifdef CHIP1371
  1702. int idx;
  1703. #endif
  1704. /* this code was part of snd_ensoniq_create before intruduction
  1705. * of suspend/resume
  1706. */
  1707. #ifdef CHIP1370
  1708. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1709. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  1710. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  1711. outl(ensoniq->dma_bug->addr, ES_REG(ensoniq, PHANTOM_FRAME));
  1712. outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
  1713. #else
  1714. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1715. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  1716. outl(0, ES_REG(ensoniq, 1371_LEGACY));
  1717. if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack)) {
  1718. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1719. /* need to delay around 20ms(bleech) to give
  1720. some CODECs enough time to wakeup */
  1721. msleep(20);
  1722. }
  1723. /* AC'97 warm reset to start the bitclk */
  1724. outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
  1725. inl(ES_REG(ensoniq, CONTROL));
  1726. udelay(20);
  1727. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1728. /* Init the sample rate converter */
  1729. snd_es1371_wait_src_ready(ensoniq);
  1730. outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
  1731. for (idx = 0; idx < 0x80; idx++)
  1732. snd_es1371_src_write(ensoniq, idx, 0);
  1733. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
  1734. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
  1735. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
  1736. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
  1737. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
  1738. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
  1739. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
  1740. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
  1741. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
  1742. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
  1743. snd_es1371_adc_rate(ensoniq, 22050);
  1744. snd_es1371_dac1_rate(ensoniq, 22050);
  1745. snd_es1371_dac2_rate(ensoniq, 22050);
  1746. /* WARNING:
  1747. * enabling the sample rate converter without properly programming
  1748. * its parameters causes the chip to lock up (the SRC busy bit will
  1749. * be stuck high, and I've found no way to rectify this other than
  1750. * power cycle) - Thomas Sailer
  1751. */
  1752. snd_es1371_wait_src_ready(ensoniq);
  1753. outl(0, ES_REG(ensoniq, 1371_SMPRATE));
  1754. /* try reset codec directly */
  1755. outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
  1756. #endif
  1757. outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
  1758. outb(0x00, ES_REG(ensoniq, UART_RES));
  1759. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1760. }
  1761. #ifdef CONFIG_PM_SLEEP
  1762. static int snd_ensoniq_suspend(struct device *dev)
  1763. {
  1764. struct snd_card *card = dev_get_drvdata(dev);
  1765. struct ensoniq *ensoniq = card->private_data;
  1766. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  1767. #ifdef CHIP1371
  1768. snd_ac97_suspend(ensoniq->u.es1371.ac97);
  1769. #else
  1770. /* try to reset AK4531 */
  1771. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
  1772. inw(ES_REG(ensoniq, 1370_CODEC));
  1773. udelay(100);
  1774. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
  1775. inw(ES_REG(ensoniq, 1370_CODEC));
  1776. udelay(100);
  1777. snd_ak4531_suspend(ensoniq->u.es1370.ak4531);
  1778. #endif
  1779. return 0;
  1780. }
  1781. static int snd_ensoniq_resume(struct device *dev)
  1782. {
  1783. struct snd_card *card = dev_get_drvdata(dev);
  1784. struct ensoniq *ensoniq = card->private_data;
  1785. snd_ensoniq_chip_init(ensoniq);
  1786. #ifdef CHIP1371
  1787. snd_ac97_resume(ensoniq->u.es1371.ac97);
  1788. #else
  1789. snd_ak4531_resume(ensoniq->u.es1370.ak4531);
  1790. #endif
  1791. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  1792. return 0;
  1793. }
  1794. static SIMPLE_DEV_PM_OPS(snd_ensoniq_pm, snd_ensoniq_suspend, snd_ensoniq_resume);
  1795. #define SND_ENSONIQ_PM_OPS &snd_ensoniq_pm
  1796. #else
  1797. #define SND_ENSONIQ_PM_OPS NULL
  1798. #endif /* CONFIG_PM_SLEEP */
  1799. static int snd_ensoniq_create(struct snd_card *card,
  1800. struct pci_dev *pci)
  1801. {
  1802. struct ensoniq *ensoniq = card->private_data;
  1803. int err;
  1804. err = pcim_enable_device(pci);
  1805. if (err < 0)
  1806. return err;
  1807. spin_lock_init(&ensoniq->reg_lock);
  1808. mutex_init(&ensoniq->src_mutex);
  1809. ensoniq->card = card;
  1810. ensoniq->pci = pci;
  1811. ensoniq->irq = -1;
  1812. err = pci_request_regions(pci, "Ensoniq AudioPCI");
  1813. if (err < 0)
  1814. return err;
  1815. ensoniq->port = pci_resource_start(pci, 0);
  1816. if (devm_request_irq(&pci->dev, pci->irq, snd_audiopci_interrupt,
  1817. IRQF_SHARED, KBUILD_MODNAME, ensoniq)) {
  1818. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  1819. return -EBUSY;
  1820. }
  1821. ensoniq->irq = pci->irq;
  1822. card->sync_irq = ensoniq->irq;
  1823. #ifdef CHIP1370
  1824. ensoniq->dma_bug =
  1825. snd_devm_alloc_pages(&pci->dev, SNDRV_DMA_TYPE_DEV, 16);
  1826. if (!ensoniq->dma_bug)
  1827. return -ENOMEM;
  1828. #endif
  1829. pci_set_master(pci);
  1830. ensoniq->rev = pci->revision;
  1831. #ifdef CHIP1370
  1832. #if 0
  1833. ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE |
  1834. ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
  1835. #else /* get microphone working */
  1836. ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
  1837. #endif
  1838. ensoniq->sctrl = 0;
  1839. #else
  1840. ensoniq->ctrl = 0;
  1841. ensoniq->sctrl = 0;
  1842. ensoniq->cssr = 0;
  1843. if (snd_pci_quirk_lookup(pci, es1371_amplifier_hack))
  1844. ensoniq->ctrl |= ES_1371_GPIO_OUT(1); /* turn amplifier on */
  1845. if (es1371_quirk_lookup(ensoniq, es1371_ac97_reset_hack))
  1846. ensoniq->cssr |= ES_1371_ST_AC97_RST;
  1847. #endif
  1848. card->private_free = snd_ensoniq_free;
  1849. snd_ensoniq_chip_init(ensoniq);
  1850. snd_ensoniq_proc_init(ensoniq);
  1851. return 0;
  1852. }
  1853. /*
  1854. * MIDI section
  1855. */
  1856. static void snd_ensoniq_midi_interrupt(struct ensoniq * ensoniq)
  1857. {
  1858. struct snd_rawmidi *rmidi = ensoniq->rmidi;
  1859. unsigned char status, mask, byte;
  1860. if (rmidi == NULL)
  1861. return;
  1862. /* do Rx at first */
  1863. spin_lock(&ensoniq->reg_lock);
  1864. mask = ensoniq->uartm & ES_MODE_INPUT ? ES_RXRDY : 0;
  1865. while (mask) {
  1866. status = inb(ES_REG(ensoniq, UART_STATUS));
  1867. if ((status & mask) == 0)
  1868. break;
  1869. byte = inb(ES_REG(ensoniq, UART_DATA));
  1870. snd_rawmidi_receive(ensoniq->midi_input, &byte, 1);
  1871. }
  1872. spin_unlock(&ensoniq->reg_lock);
  1873. /* do Tx at second */
  1874. spin_lock(&ensoniq->reg_lock);
  1875. mask = ensoniq->uartm & ES_MODE_OUTPUT ? ES_TXRDY : 0;
  1876. while (mask) {
  1877. status = inb(ES_REG(ensoniq, UART_STATUS));
  1878. if ((status & mask) == 0)
  1879. break;
  1880. if (snd_rawmidi_transmit(ensoniq->midi_output, &byte, 1) != 1) {
  1881. ensoniq->uartc &= ~ES_TXINTENM;
  1882. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1883. mask &= ~ES_TXRDY;
  1884. } else {
  1885. outb(byte, ES_REG(ensoniq, UART_DATA));
  1886. }
  1887. }
  1888. spin_unlock(&ensoniq->reg_lock);
  1889. }
  1890. static int snd_ensoniq_midi_input_open(struct snd_rawmidi_substream *substream)
  1891. {
  1892. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1893. spin_lock_irq(&ensoniq->reg_lock);
  1894. ensoniq->uartm |= ES_MODE_INPUT;
  1895. ensoniq->midi_input = substream;
  1896. if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
  1897. outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
  1898. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1899. outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1900. }
  1901. spin_unlock_irq(&ensoniq->reg_lock);
  1902. return 0;
  1903. }
  1904. static int snd_ensoniq_midi_input_close(struct snd_rawmidi_substream *substream)
  1905. {
  1906. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1907. spin_lock_irq(&ensoniq->reg_lock);
  1908. if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
  1909. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1910. outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1911. } else {
  1912. outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
  1913. }
  1914. ensoniq->midi_input = NULL;
  1915. ensoniq->uartm &= ~ES_MODE_INPUT;
  1916. spin_unlock_irq(&ensoniq->reg_lock);
  1917. return 0;
  1918. }
  1919. static int snd_ensoniq_midi_output_open(struct snd_rawmidi_substream *substream)
  1920. {
  1921. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1922. spin_lock_irq(&ensoniq->reg_lock);
  1923. ensoniq->uartm |= ES_MODE_OUTPUT;
  1924. ensoniq->midi_output = substream;
  1925. if (!(ensoniq->uartm & ES_MODE_INPUT)) {
  1926. outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
  1927. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1928. outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1929. }
  1930. spin_unlock_irq(&ensoniq->reg_lock);
  1931. return 0;
  1932. }
  1933. static int snd_ensoniq_midi_output_close(struct snd_rawmidi_substream *substream)
  1934. {
  1935. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1936. spin_lock_irq(&ensoniq->reg_lock);
  1937. if (!(ensoniq->uartm & ES_MODE_INPUT)) {
  1938. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1939. outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1940. } else {
  1941. outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
  1942. }
  1943. ensoniq->midi_output = NULL;
  1944. ensoniq->uartm &= ~ES_MODE_OUTPUT;
  1945. spin_unlock_irq(&ensoniq->reg_lock);
  1946. return 0;
  1947. }
  1948. static void snd_ensoniq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  1949. {
  1950. unsigned long flags;
  1951. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1952. int idx;
  1953. spin_lock_irqsave(&ensoniq->reg_lock, flags);
  1954. if (up) {
  1955. if ((ensoniq->uartc & ES_RXINTEN) == 0) {
  1956. /* empty input FIFO */
  1957. for (idx = 0; idx < 32; idx++)
  1958. inb(ES_REG(ensoniq, UART_DATA));
  1959. ensoniq->uartc |= ES_RXINTEN;
  1960. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1961. }
  1962. } else {
  1963. if (ensoniq->uartc & ES_RXINTEN) {
  1964. ensoniq->uartc &= ~ES_RXINTEN;
  1965. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1966. }
  1967. }
  1968. spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
  1969. }
  1970. static void snd_ensoniq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  1971. {
  1972. unsigned long flags;
  1973. struct ensoniq *ensoniq = substream->rmidi->private_data;
  1974. unsigned char byte;
  1975. spin_lock_irqsave(&ensoniq->reg_lock, flags);
  1976. if (up) {
  1977. if (ES_TXINTENI(ensoniq->uartc) == 0) {
  1978. ensoniq->uartc |= ES_TXINTENO(1);
  1979. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  1980. while (ES_TXINTENI(ensoniq->uartc) == 1 &&
  1981. (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
  1982. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  1983. ensoniq->uartc &= ~ES_TXINTENM;
  1984. } else {
  1985. outb(byte, ES_REG(ensoniq, UART_DATA));
  1986. }
  1987. }
  1988. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1989. }
  1990. } else {
  1991. if (ES_TXINTENI(ensoniq->uartc) == 1) {
  1992. ensoniq->uartc &= ~ES_TXINTENM;
  1993. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1994. }
  1995. }
  1996. spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
  1997. }
  1998. static const struct snd_rawmidi_ops snd_ensoniq_midi_output =
  1999. {
  2000. .open = snd_ensoniq_midi_output_open,
  2001. .close = snd_ensoniq_midi_output_close,
  2002. .trigger = snd_ensoniq_midi_output_trigger,
  2003. };
  2004. static const struct snd_rawmidi_ops snd_ensoniq_midi_input =
  2005. {
  2006. .open = snd_ensoniq_midi_input_open,
  2007. .close = snd_ensoniq_midi_input_close,
  2008. .trigger = snd_ensoniq_midi_input_trigger,
  2009. };
  2010. static int snd_ensoniq_midi(struct ensoniq *ensoniq, int device)
  2011. {
  2012. struct snd_rawmidi *rmidi;
  2013. int err;
  2014. err = snd_rawmidi_new(ensoniq->card, "ES1370/1", device, 1, 1, &rmidi);
  2015. if (err < 0)
  2016. return err;
  2017. strcpy(rmidi->name, CHIP_NAME);
  2018. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_ensoniq_midi_output);
  2019. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_ensoniq_midi_input);
  2020. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT |
  2021. SNDRV_RAWMIDI_INFO_DUPLEX;
  2022. rmidi->private_data = ensoniq;
  2023. ensoniq->rmidi = rmidi;
  2024. return 0;
  2025. }
  2026. /*
  2027. * Interrupt handler
  2028. */
  2029. static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id)
  2030. {
  2031. struct ensoniq *ensoniq = dev_id;
  2032. unsigned int status, sctrl;
  2033. if (ensoniq == NULL)
  2034. return IRQ_NONE;
  2035. status = inl(ES_REG(ensoniq, STATUS));
  2036. if (!(status & ES_INTR))
  2037. return IRQ_NONE;
  2038. spin_lock(&ensoniq->reg_lock);
  2039. sctrl = ensoniq->sctrl;
  2040. if (status & ES_DAC1)
  2041. sctrl &= ~ES_P1_INT_EN;
  2042. if (status & ES_DAC2)
  2043. sctrl &= ~ES_P2_INT_EN;
  2044. if (status & ES_ADC)
  2045. sctrl &= ~ES_R1_INT_EN;
  2046. outl(sctrl, ES_REG(ensoniq, SERIAL));
  2047. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  2048. spin_unlock(&ensoniq->reg_lock);
  2049. if (status & ES_UART)
  2050. snd_ensoniq_midi_interrupt(ensoniq);
  2051. if ((status & ES_DAC2) && ensoniq->playback2_substream)
  2052. snd_pcm_period_elapsed(ensoniq->playback2_substream);
  2053. if ((status & ES_ADC) && ensoniq->capture_substream)
  2054. snd_pcm_period_elapsed(ensoniq->capture_substream);
  2055. if ((status & ES_DAC1) && ensoniq->playback1_substream)
  2056. snd_pcm_period_elapsed(ensoniq->playback1_substream);
  2057. return IRQ_HANDLED;
  2058. }
  2059. static int __snd_audiopci_probe(struct pci_dev *pci,
  2060. const struct pci_device_id *pci_id)
  2061. {
  2062. static int dev;
  2063. struct snd_card *card;
  2064. struct ensoniq *ensoniq;
  2065. int err;
  2066. if (dev >= SNDRV_CARDS)
  2067. return -ENODEV;
  2068. if (!enable[dev]) {
  2069. dev++;
  2070. return -ENOENT;
  2071. }
  2072. err = snd_devm_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  2073. sizeof(*ensoniq), &card);
  2074. if (err < 0)
  2075. return err;
  2076. ensoniq = card->private_data;
  2077. err = snd_ensoniq_create(card, pci);
  2078. if (err < 0)
  2079. return err;
  2080. #ifdef CHIP1370
  2081. err = snd_ensoniq_1370_mixer(ensoniq);
  2082. if (err < 0)
  2083. return err;
  2084. #endif
  2085. #ifdef CHIP1371
  2086. err = snd_ensoniq_1371_mixer(ensoniq, spdif[dev], lineio[dev]);
  2087. if (err < 0)
  2088. return err;
  2089. #endif
  2090. err = snd_ensoniq_pcm(ensoniq, 0);
  2091. if (err < 0)
  2092. return err;
  2093. err = snd_ensoniq_pcm2(ensoniq, 1);
  2094. if (err < 0)
  2095. return err;
  2096. err = snd_ensoniq_midi(ensoniq, 0);
  2097. if (err < 0)
  2098. return err;
  2099. snd_ensoniq_create_gameport(ensoniq, dev);
  2100. strcpy(card->driver, DRIVER_NAME);
  2101. strcpy(card->shortname, "Ensoniq AudioPCI");
  2102. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  2103. card->shortname,
  2104. card->driver,
  2105. ensoniq->port,
  2106. ensoniq->irq);
  2107. err = snd_card_register(card);
  2108. if (err < 0)
  2109. return err;
  2110. pci_set_drvdata(pci, card);
  2111. dev++;
  2112. return 0;
  2113. }
  2114. static int snd_audiopci_probe(struct pci_dev *pci,
  2115. const struct pci_device_id *pci_id)
  2116. {
  2117. return snd_card_free_on_error(&pci->dev, __snd_audiopci_probe(pci, pci_id));
  2118. }
  2119. static struct pci_driver ens137x_driver = {
  2120. .name = KBUILD_MODNAME,
  2121. .id_table = snd_audiopci_ids,
  2122. .probe = snd_audiopci_probe,
  2123. .driver = {
  2124. .pm = SND_ENSONIQ_PM_OPS,
  2125. },
  2126. };
  2127. module_pci_driver(ens137x_driver);