ens1370.c 77 KB

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