ens1370.c 80 KB

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