ens1370.c 80 KB

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