ens1370.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477
  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. typedef struct _snd_ensoniq ensoniq_t;
  338. struct _snd_ensoniq {
  339. spinlock_t reg_lock;
  340. struct semaphore src_mutex;
  341. int irq;
  342. unsigned long playback1size;
  343. unsigned long playback2size;
  344. unsigned long capture3size;
  345. unsigned long port;
  346. unsigned int mode;
  347. unsigned int uartm; /* UART mode */
  348. unsigned int ctrl; /* control register */
  349. unsigned int sctrl; /* serial control register */
  350. unsigned int cssr; /* control status register */
  351. unsigned int uartc; /* uart control register */
  352. unsigned int rev; /* chip revision */
  353. union {
  354. #ifdef CHIP1371
  355. struct {
  356. ac97_t *ac97;
  357. } es1371;
  358. #else
  359. struct {
  360. int pclkdiv_lock;
  361. ak4531_t *ak4531;
  362. } es1370;
  363. #endif
  364. } u;
  365. struct pci_dev *pci;
  366. unsigned short subsystem_vendor_id;
  367. unsigned short subsystem_device_id;
  368. snd_card_t *card;
  369. snd_pcm_t *pcm1; /* DAC1/ADC PCM */
  370. snd_pcm_t *pcm2; /* DAC2 PCM */
  371. snd_pcm_substream_t *playback1_substream;
  372. snd_pcm_substream_t *playback2_substream;
  373. snd_pcm_substream_t *capture_substream;
  374. unsigned int p1_dma_size;
  375. unsigned int p2_dma_size;
  376. unsigned int c_dma_size;
  377. unsigned int p1_period_size;
  378. unsigned int p2_period_size;
  379. unsigned int c_period_size;
  380. snd_rawmidi_t *rmidi;
  381. snd_rawmidi_substream_t *midi_input;
  382. snd_rawmidi_substream_t *midi_output;
  383. unsigned int spdif;
  384. unsigned int spdif_default;
  385. unsigned int spdif_stream;
  386. #ifdef CHIP1370
  387. struct snd_dma_buffer dma_bug;
  388. #endif
  389. #ifdef SUPPORT_JOYSTICK
  390. struct gameport *gameport;
  391. #endif
  392. };
  393. static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  394. static struct pci_device_id snd_audiopci_ids[] = {
  395. #ifdef CHIP1370
  396. { 0x1274, 0x5000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ES1370 */
  397. #endif
  398. #ifdef CHIP1371
  399. { 0x1274, 0x1371, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ES1371 */
  400. { 0x1274, 0x5880, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ES1373 - CT5880 */
  401. { 0x1102, 0x8938, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* Ectiva EV1938 */
  402. #endif
  403. { 0, }
  404. };
  405. MODULE_DEVICE_TABLE(pci, snd_audiopci_ids);
  406. /*
  407. * constants
  408. */
  409. #define POLL_COUNT 0xa000
  410. #ifdef CHIP1370
  411. static unsigned int snd_es1370_fixed_rates[] =
  412. {5512, 11025, 22050, 44100};
  413. static snd_pcm_hw_constraint_list_t snd_es1370_hw_constraints_rates = {
  414. .count = 4,
  415. .list = snd_es1370_fixed_rates,
  416. .mask = 0,
  417. };
  418. static ratnum_t es1370_clock = {
  419. .num = ES_1370_SRCLOCK,
  420. .den_min = 29,
  421. .den_max = 353,
  422. .den_step = 1,
  423. };
  424. static snd_pcm_hw_constraint_ratnums_t snd_es1370_hw_constraints_clock = {
  425. .nrats = 1,
  426. .rats = &es1370_clock,
  427. };
  428. #else
  429. static ratden_t es1371_dac_clock = {
  430. .num_min = 3000 * (1 << 15),
  431. .num_max = 48000 * (1 << 15),
  432. .num_step = 3000,
  433. .den = 1 << 15,
  434. };
  435. static snd_pcm_hw_constraint_ratdens_t snd_es1371_hw_constraints_dac_clock = {
  436. .nrats = 1,
  437. .rats = &es1371_dac_clock,
  438. };
  439. static ratnum_t es1371_adc_clock = {
  440. .num = 48000 << 15,
  441. .den_min = 32768,
  442. .den_max = 393216,
  443. .den_step = 1,
  444. };
  445. static snd_pcm_hw_constraint_ratnums_t snd_es1371_hw_constraints_adc_clock = {
  446. .nrats = 1,
  447. .rats = &es1371_adc_clock,
  448. };
  449. #endif
  450. static const unsigned int snd_ensoniq_sample_shift[] =
  451. {0, 1, 1, 2};
  452. /*
  453. * common I/O routines
  454. */
  455. #ifdef CHIP1371
  456. static unsigned int snd_es1371_wait_src_ready(ensoniq_t * ensoniq)
  457. {
  458. unsigned int t, r = 0;
  459. for (t = 0; t < POLL_COUNT; t++) {
  460. r = inl(ES_REG(ensoniq, 1371_SMPRATE));
  461. if ((r & ES_1371_SRC_RAM_BUSY) == 0)
  462. return r;
  463. cond_resched();
  464. }
  465. snd_printk(KERN_ERR "wait source ready timeout 0x%lx [0x%x]\n", ES_REG(ensoniq, 1371_SMPRATE), r);
  466. return 0;
  467. }
  468. static unsigned int snd_es1371_src_read(ensoniq_t * 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(ensoniq_t * 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(ak4531_t *ak4531,
  508. unsigned short reg, unsigned short val)
  509. {
  510. ensoniq_t *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", reg, val, ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
  514. #endif
  515. do {
  516. if (!(inl(ES_REG(ensoniq, STATUS)) & ES_1370_CSTAT)) {
  517. outw(ES_1370_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1370_CODEC));
  518. return;
  519. }
  520. schedule_timeout_uninterruptible(1);
  521. } while (time_after(end_time, jiffies));
  522. snd_printk(KERN_ERR "codec write timeout, status = 0x%x\n", inl(ES_REG(ensoniq, STATUS)));
  523. }
  524. #endif /* CHIP1370 */
  525. #ifdef CHIP1371
  526. static void snd_es1371_codec_write(ac97_t *ac97,
  527. unsigned short reg, unsigned short val)
  528. {
  529. ensoniq_t *ensoniq = ac97->private_data;
  530. unsigned int t, x;
  531. down(&ensoniq->src_mutex);
  532. for (t = 0; t < POLL_COUNT; t++) {
  533. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
  534. /* save the current state for latter */
  535. x = snd_es1371_wait_src_ready(ensoniq);
  536. outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  537. ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
  538. ES_REG(ensoniq, 1371_SMPRATE));
  539. /* wait for not busy (state 0) first to avoid
  540. transition states */
  541. for (t = 0; t < POLL_COUNT; t++) {
  542. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) == 0x00000000)
  543. break;
  544. }
  545. /* wait for a SAFE time to write addr/data and then do it, dammit */
  546. for (t = 0; t < POLL_COUNT; t++) {
  547. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) == 0x00010000)
  548. break;
  549. }
  550. outl(ES_1371_CODEC_WRITE(reg, val), ES_REG(ensoniq, 1371_CODEC));
  551. /* restore SRC reg */
  552. snd_es1371_wait_src_ready(ensoniq);
  553. outl(x, ES_REG(ensoniq, 1371_SMPRATE));
  554. up(&ensoniq->src_mutex);
  555. return;
  556. }
  557. }
  558. up(&ensoniq->src_mutex);
  559. snd_printk(KERN_ERR "codec write timeout at 0x%lx [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
  560. }
  561. static unsigned short snd_es1371_codec_read(ac97_t *ac97,
  562. unsigned short reg)
  563. {
  564. ensoniq_t *ensoniq = ac97->private_data;
  565. unsigned int t, x, fail = 0;
  566. __again:
  567. down(&ensoniq->src_mutex);
  568. for (t = 0; t < POLL_COUNT; t++) {
  569. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP)) {
  570. /* save the current state for latter */
  571. x = snd_es1371_wait_src_ready(ensoniq);
  572. outl((x & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 |
  573. ES_1371_DIS_P2 | ES_1371_DIS_R1)) | 0x00010000,
  574. ES_REG(ensoniq, 1371_SMPRATE));
  575. /* wait for not busy (state 0) first to avoid
  576. transition states */
  577. for (t = 0; t < POLL_COUNT; t++) {
  578. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) == 0x00000000)
  579. break;
  580. }
  581. /* wait for a SAFE time to write addr/data and then do it, dammit */
  582. for (t = 0; t < POLL_COUNT; t++) {
  583. if ((inl(ES_REG(ensoniq, 1371_SMPRATE)) & 0x00870000) == 0x00010000)
  584. break;
  585. }
  586. outl(ES_1371_CODEC_READS(reg), ES_REG(ensoniq, 1371_CODEC));
  587. /* restore SRC reg */
  588. snd_es1371_wait_src_ready(ensoniq);
  589. outl(x, ES_REG(ensoniq, 1371_SMPRATE));
  590. /* wait for WIP again */
  591. for (t = 0; t < POLL_COUNT; t++) {
  592. if (!(inl(ES_REG(ensoniq, 1371_CODEC)) & ES_1371_CODEC_WIP))
  593. break;
  594. }
  595. /* now wait for the stinkin' data (RDY) */
  596. for (t = 0; t < POLL_COUNT; t++) {
  597. if ((x = inl(ES_REG(ensoniq, 1371_CODEC))) & ES_1371_CODEC_RDY) {
  598. up(&ensoniq->src_mutex);
  599. return ES_1371_CODEC_READ(x);
  600. }
  601. }
  602. up(&ensoniq->src_mutex);
  603. if (++fail > 10) {
  604. snd_printk(KERN_ERR "codec read timeout (final) at 0x%lx, reg = 0x%x [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), reg, inl(ES_REG(ensoniq, 1371_CODEC)));
  605. return 0;
  606. }
  607. goto __again;
  608. }
  609. }
  610. up(&ensoniq->src_mutex);
  611. snd_printk(KERN_ERR "es1371: codec read timeout at 0x%lx [0x%x]\n", ES_REG(ensoniq, 1371_CODEC), inl(ES_REG(ensoniq, 1371_CODEC)));
  612. return 0;
  613. }
  614. static void snd_es1371_codec_wait(ac97_t *ac97)
  615. {
  616. msleep(750);
  617. snd_es1371_codec_read(ac97, AC97_RESET);
  618. snd_es1371_codec_read(ac97, AC97_VENDOR_ID1);
  619. snd_es1371_codec_read(ac97, AC97_VENDOR_ID2);
  620. msleep(50);
  621. }
  622. static void snd_es1371_adc_rate(ensoniq_t * ensoniq, unsigned int rate)
  623. {
  624. unsigned int n, truncm, freq, result;
  625. down(&ensoniq->src_mutex);
  626. n = rate / 3000;
  627. if ((1 << n) & ((1 << 15) | (1 << 13) | (1 << 11) | (1 << 9)))
  628. n--;
  629. truncm = (21 * n - 1) | 1;
  630. freq = ((48000UL << 15) / rate) * n;
  631. result = (48000UL << 15) / (freq / n);
  632. if (rate >= 24000) {
  633. if (truncm > 239)
  634. truncm = 239;
  635. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
  636. (((239 - truncm) >> 1) << 9) | (n << 4));
  637. } else {
  638. if (truncm > 119)
  639. truncm = 119;
  640. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_TRUNC_N,
  641. 0x8000 | (((119 - truncm) >> 1) << 9) | (n << 4));
  642. }
  643. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS,
  644. (snd_es1371_src_read(ensoniq, ES_SMPREG_ADC + ES_SMPREG_INT_REGS) & 0x00ff) |
  645. ((freq >> 5) & 0xfc00));
  646. snd_es1371_src_write(ensoniq, ES_SMPREG_ADC + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
  647. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, n << 8);
  648. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, n << 8);
  649. up(&ensoniq->src_mutex);
  650. }
  651. static void snd_es1371_dac1_rate(ensoniq_t * ensoniq, unsigned int rate)
  652. {
  653. unsigned int freq, r;
  654. down(&ensoniq->src_mutex);
  655. freq = ((rate << 15) + 1500) / 3000;
  656. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P2 | ES_1371_DIS_R1)) | ES_1371_DIS_P1;
  657. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  658. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS,
  659. (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS) & 0x00ff) |
  660. ((freq >> 5) & 0xfc00));
  661. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
  662. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P2 | ES_1371_DIS_R1));
  663. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  664. up(&ensoniq->src_mutex);
  665. }
  666. static void snd_es1371_dac2_rate(ensoniq_t * ensoniq, unsigned int rate)
  667. {
  668. unsigned int freq, r;
  669. down(&ensoniq->src_mutex);
  670. freq = ((rate << 15) + 1500) / 3000;
  671. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 | ES_1371_DIS_R1)) | ES_1371_DIS_P2;
  672. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  673. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS,
  674. (snd_es1371_src_read(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS) & 0x00ff) |
  675. ((freq >> 5) & 0xfc00));
  676. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_VFREQ_FRAC, freq & 0x7fff);
  677. r = (snd_es1371_wait_src_ready(ensoniq) & (ES_1371_SRC_DISABLE | ES_1371_DIS_P1 | ES_1371_DIS_R1));
  678. outl(r, ES_REG(ensoniq, 1371_SMPRATE));
  679. up(&ensoniq->src_mutex);
  680. }
  681. #endif /* CHIP1371 */
  682. static int snd_ensoniq_trigger(snd_pcm_substream_t *substream, int cmd)
  683. {
  684. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  685. switch (cmd) {
  686. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  687. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  688. {
  689. unsigned int what = 0;
  690. struct list_head *pos;
  691. snd_pcm_substream_t *s;
  692. snd_pcm_group_for_each(pos, substream) {
  693. s = snd_pcm_group_substream_entry(pos);
  694. if (s == ensoniq->playback1_substream) {
  695. what |= ES_P1_PAUSE;
  696. snd_pcm_trigger_done(s, substream);
  697. } else if (s == ensoniq->playback2_substream) {
  698. what |= ES_P2_PAUSE;
  699. snd_pcm_trigger_done(s, substream);
  700. } else if (s == ensoniq->capture_substream)
  701. return -EINVAL;
  702. }
  703. spin_lock(&ensoniq->reg_lock);
  704. if (cmd == SNDRV_PCM_TRIGGER_PAUSE_PUSH)
  705. ensoniq->sctrl |= what;
  706. else
  707. ensoniq->sctrl &= ~what;
  708. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  709. spin_unlock(&ensoniq->reg_lock);
  710. break;
  711. }
  712. case SNDRV_PCM_TRIGGER_START:
  713. case SNDRV_PCM_TRIGGER_STOP:
  714. {
  715. unsigned int what = 0;
  716. struct list_head *pos;
  717. snd_pcm_substream_t *s;
  718. snd_pcm_group_for_each(pos, substream) {
  719. s = snd_pcm_group_substream_entry(pos);
  720. if (s == ensoniq->playback1_substream) {
  721. what |= ES_DAC1_EN;
  722. snd_pcm_trigger_done(s, substream);
  723. } else if (s == ensoniq->playback2_substream) {
  724. what |= ES_DAC2_EN;
  725. snd_pcm_trigger_done(s, substream);
  726. } else if (s == ensoniq->capture_substream) {
  727. what |= ES_ADC_EN;
  728. snd_pcm_trigger_done(s, substream);
  729. }
  730. }
  731. spin_lock(&ensoniq->reg_lock);
  732. if (cmd == SNDRV_PCM_TRIGGER_START)
  733. ensoniq->ctrl |= what;
  734. else
  735. ensoniq->ctrl &= ~what;
  736. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  737. spin_unlock(&ensoniq->reg_lock);
  738. break;
  739. }
  740. default:
  741. return -EINVAL;
  742. }
  743. return 0;
  744. }
  745. /*
  746. * PCM part
  747. */
  748. static int snd_ensoniq_hw_params(snd_pcm_substream_t * substream,
  749. snd_pcm_hw_params_t * hw_params)
  750. {
  751. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  752. }
  753. static int snd_ensoniq_hw_free(snd_pcm_substream_t * substream)
  754. {
  755. return snd_pcm_lib_free_pages(substream);
  756. }
  757. static int snd_ensoniq_playback1_prepare(snd_pcm_substream_t * substream)
  758. {
  759. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  760. snd_pcm_runtime_t *runtime = substream->runtime;
  761. unsigned int mode = 0;
  762. ensoniq->p1_dma_size = snd_pcm_lib_buffer_bytes(substream);
  763. ensoniq->p1_period_size = snd_pcm_lib_period_bytes(substream);
  764. if (snd_pcm_format_width(runtime->format) == 16)
  765. mode |= 0x02;
  766. if (runtime->channels > 1)
  767. mode |= 0x01;
  768. spin_lock_irq(&ensoniq->reg_lock);
  769. ensoniq->ctrl &= ~ES_DAC1_EN;
  770. #ifdef CHIP1371
  771. /* 48k doesn't need SRC (it breaks AC3-passthru) */
  772. if (runtime->rate == 48000)
  773. ensoniq->ctrl |= ES_1373_BYPASS_P1;
  774. else
  775. ensoniq->ctrl &= ~ES_1373_BYPASS_P1;
  776. #endif
  777. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  778. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  779. outl(runtime->dma_addr, ES_REG(ensoniq, DAC1_FRAME));
  780. outl((ensoniq->p1_dma_size >> 2) - 1, ES_REG(ensoniq, DAC1_SIZE));
  781. ensoniq->sctrl &= ~(ES_P1_LOOP_SEL | ES_P1_PAUSE | ES_P1_SCT_RLD | ES_P1_MODEM);
  782. ensoniq->sctrl |= ES_P1_INT_EN | ES_P1_MODEO(mode);
  783. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  784. outl((ensoniq->p1_period_size >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, DAC1_COUNT));
  785. #ifdef CHIP1370
  786. ensoniq->ctrl &= ~ES_1370_WTSRSELM;
  787. switch (runtime->rate) {
  788. case 5512: ensoniq->ctrl |= ES_1370_WTSRSEL(0); break;
  789. case 11025: ensoniq->ctrl |= ES_1370_WTSRSEL(1); break;
  790. case 22050: ensoniq->ctrl |= ES_1370_WTSRSEL(2); break;
  791. case 44100: ensoniq->ctrl |= ES_1370_WTSRSEL(3); break;
  792. default: snd_BUG();
  793. }
  794. #endif
  795. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  796. spin_unlock_irq(&ensoniq->reg_lock);
  797. #ifndef CHIP1370
  798. snd_es1371_dac1_rate(ensoniq, runtime->rate);
  799. #endif
  800. return 0;
  801. }
  802. static int snd_ensoniq_playback2_prepare(snd_pcm_substream_t * substream)
  803. {
  804. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  805. snd_pcm_runtime_t *runtime = substream->runtime;
  806. unsigned int mode = 0;
  807. ensoniq->p2_dma_size = snd_pcm_lib_buffer_bytes(substream);
  808. ensoniq->p2_period_size = snd_pcm_lib_period_bytes(substream);
  809. if (snd_pcm_format_width(runtime->format) == 16)
  810. mode |= 0x02;
  811. if (runtime->channels > 1)
  812. mode |= 0x01;
  813. spin_lock_irq(&ensoniq->reg_lock);
  814. ensoniq->ctrl &= ~ES_DAC2_EN;
  815. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  816. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  817. outl(runtime->dma_addr, ES_REG(ensoniq, DAC2_FRAME));
  818. outl((ensoniq->p2_dma_size >> 2) - 1, ES_REG(ensoniq, DAC2_SIZE));
  819. ensoniq->sctrl &= ~(ES_P2_LOOP_SEL | ES_P2_PAUSE | ES_P2_DAC_SEN |
  820. ES_P2_END_INCM | ES_P2_ST_INCM | ES_P2_MODEM);
  821. ensoniq->sctrl |= ES_P2_INT_EN | ES_P2_MODEO(mode) |
  822. ES_P2_END_INCO(mode & 2 ? 2 : 1) | ES_P2_ST_INCO(0);
  823. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  824. outl((ensoniq->p2_period_size >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, DAC2_COUNT));
  825. #ifdef CHIP1370
  826. if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_CAPTURE)) {
  827. ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
  828. ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
  829. ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_PLAY2;
  830. }
  831. #endif
  832. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  833. spin_unlock_irq(&ensoniq->reg_lock);
  834. #ifndef CHIP1370
  835. snd_es1371_dac2_rate(ensoniq, runtime->rate);
  836. #endif
  837. return 0;
  838. }
  839. static int snd_ensoniq_capture_prepare(snd_pcm_substream_t * substream)
  840. {
  841. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  842. snd_pcm_runtime_t *runtime = substream->runtime;
  843. unsigned int mode = 0;
  844. ensoniq->c_dma_size = snd_pcm_lib_buffer_bytes(substream);
  845. ensoniq->c_period_size = snd_pcm_lib_period_bytes(substream);
  846. if (snd_pcm_format_width(runtime->format) == 16)
  847. mode |= 0x02;
  848. if (runtime->channels > 1)
  849. mode |= 0x01;
  850. spin_lock_irq(&ensoniq->reg_lock);
  851. ensoniq->ctrl &= ~ES_ADC_EN;
  852. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  853. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  854. outl(runtime->dma_addr, ES_REG(ensoniq, ADC_FRAME));
  855. outl((ensoniq->c_dma_size >> 2) - 1, ES_REG(ensoniq, ADC_SIZE));
  856. ensoniq->sctrl &= ~(ES_R1_LOOP_SEL | ES_R1_MODEM);
  857. ensoniq->sctrl |= ES_R1_INT_EN | ES_R1_MODEO(mode);
  858. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  859. outl((ensoniq->c_period_size >> snd_ensoniq_sample_shift[mode]) - 1, ES_REG(ensoniq, ADC_COUNT));
  860. #ifdef CHIP1370
  861. if (!(ensoniq->u.es1370.pclkdiv_lock & ES_MODE_PLAY2)) {
  862. ensoniq->ctrl &= ~ES_1370_PCLKDIVM;
  863. ensoniq->ctrl |= ES_1370_PCLKDIVO(ES_1370_SRTODIV(runtime->rate));
  864. ensoniq->u.es1370.pclkdiv_lock |= ES_MODE_CAPTURE;
  865. }
  866. #endif
  867. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  868. spin_unlock_irq(&ensoniq->reg_lock);
  869. #ifndef CHIP1370
  870. snd_es1371_adc_rate(ensoniq, runtime->rate);
  871. #endif
  872. return 0;
  873. }
  874. static snd_pcm_uframes_t snd_ensoniq_playback1_pointer(snd_pcm_substream_t * substream)
  875. {
  876. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  877. size_t ptr;
  878. spin_lock(&ensoniq->reg_lock);
  879. if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC1_EN) {
  880. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  881. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC1_SIZE)));
  882. ptr = bytes_to_frames(substream->runtime, ptr);
  883. } else {
  884. ptr = 0;
  885. }
  886. spin_unlock(&ensoniq->reg_lock);
  887. return ptr;
  888. }
  889. static snd_pcm_uframes_t snd_ensoniq_playback2_pointer(snd_pcm_substream_t * substream)
  890. {
  891. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  892. size_t ptr;
  893. spin_lock(&ensoniq->reg_lock);
  894. if (inl(ES_REG(ensoniq, CONTROL)) & ES_DAC2_EN) {
  895. outl(ES_MEM_PAGEO(ES_PAGE_DAC), ES_REG(ensoniq, MEM_PAGE));
  896. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, DAC2_SIZE)));
  897. ptr = bytes_to_frames(substream->runtime, ptr);
  898. } else {
  899. ptr = 0;
  900. }
  901. spin_unlock(&ensoniq->reg_lock);
  902. return ptr;
  903. }
  904. static snd_pcm_uframes_t snd_ensoniq_capture_pointer(snd_pcm_substream_t * substream)
  905. {
  906. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  907. size_t ptr;
  908. spin_lock(&ensoniq->reg_lock);
  909. if (inl(ES_REG(ensoniq, CONTROL)) & ES_ADC_EN) {
  910. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  911. ptr = ES_REG_FCURR_COUNTI(inl(ES_REG(ensoniq, ADC_SIZE)));
  912. ptr = bytes_to_frames(substream->runtime, ptr);
  913. } else {
  914. ptr = 0;
  915. }
  916. spin_unlock(&ensoniq->reg_lock);
  917. return ptr;
  918. }
  919. static snd_pcm_hardware_t snd_ensoniq_playback1 =
  920. {
  921. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  922. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  923. SNDRV_PCM_INFO_MMAP_VALID |
  924. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_SYNC_START),
  925. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  926. .rates =
  927. #ifndef CHIP1370
  928. SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  929. #else
  930. (SNDRV_PCM_RATE_KNOT | /* 5512Hz rate */
  931. SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 |
  932. SNDRV_PCM_RATE_44100),
  933. #endif
  934. .rate_min = 4000,
  935. .rate_max = 48000,
  936. .channels_min = 1,
  937. .channels_max = 2,
  938. .buffer_bytes_max = (128*1024),
  939. .period_bytes_min = 64,
  940. .period_bytes_max = (128*1024),
  941. .periods_min = 1,
  942. .periods_max = 1024,
  943. .fifo_size = 0,
  944. };
  945. static snd_pcm_hardware_t snd_ensoniq_playback2 =
  946. {
  947. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  948. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  949. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_PAUSE |
  950. SNDRV_PCM_INFO_SYNC_START),
  951. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  952. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  953. .rate_min = 4000,
  954. .rate_max = 48000,
  955. .channels_min = 1,
  956. .channels_max = 2,
  957. .buffer_bytes_max = (128*1024),
  958. .period_bytes_min = 64,
  959. .period_bytes_max = (128*1024),
  960. .periods_min = 1,
  961. .periods_max = 1024,
  962. .fifo_size = 0,
  963. };
  964. static snd_pcm_hardware_t snd_ensoniq_capture =
  965. {
  966. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  967. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  968. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START),
  969. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  970. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  971. .rate_min = 4000,
  972. .rate_max = 48000,
  973. .channels_min = 1,
  974. .channels_max = 2,
  975. .buffer_bytes_max = (128*1024),
  976. .period_bytes_min = 64,
  977. .period_bytes_max = (128*1024),
  978. .periods_min = 1,
  979. .periods_max = 1024,
  980. .fifo_size = 0,
  981. };
  982. static int snd_ensoniq_playback1_open(snd_pcm_substream_t * substream)
  983. {
  984. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  985. snd_pcm_runtime_t *runtime = substream->runtime;
  986. ensoniq->mode |= ES_MODE_PLAY1;
  987. ensoniq->playback1_substream = substream;
  988. runtime->hw = snd_ensoniq_playback1;
  989. snd_pcm_set_sync(substream);
  990. spin_lock_irq(&ensoniq->reg_lock);
  991. if (ensoniq->spdif && ensoniq->playback2_substream == NULL)
  992. ensoniq->spdif_stream = ensoniq->spdif_default;
  993. spin_unlock_irq(&ensoniq->reg_lock);
  994. #ifdef CHIP1370
  995. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  996. &snd_es1370_hw_constraints_rates);
  997. #else
  998. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  999. &snd_es1371_hw_constraints_dac_clock);
  1000. #endif
  1001. return 0;
  1002. }
  1003. static int snd_ensoniq_playback2_open(snd_pcm_substream_t * substream)
  1004. {
  1005. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  1006. snd_pcm_runtime_t *runtime = substream->runtime;
  1007. ensoniq->mode |= ES_MODE_PLAY2;
  1008. ensoniq->playback2_substream = substream;
  1009. runtime->hw = snd_ensoniq_playback2;
  1010. snd_pcm_set_sync(substream);
  1011. spin_lock_irq(&ensoniq->reg_lock);
  1012. if (ensoniq->spdif && ensoniq->playback1_substream == NULL)
  1013. ensoniq->spdif_stream = ensoniq->spdif_default;
  1014. spin_unlock_irq(&ensoniq->reg_lock);
  1015. #ifdef CHIP1370
  1016. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1017. &snd_es1370_hw_constraints_clock);
  1018. #else
  1019. snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1020. &snd_es1371_hw_constraints_dac_clock);
  1021. #endif
  1022. return 0;
  1023. }
  1024. static int snd_ensoniq_capture_open(snd_pcm_substream_t * substream)
  1025. {
  1026. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  1027. snd_pcm_runtime_t *runtime = substream->runtime;
  1028. ensoniq->mode |= ES_MODE_CAPTURE;
  1029. ensoniq->capture_substream = substream;
  1030. runtime->hw = snd_ensoniq_capture;
  1031. snd_pcm_set_sync(substream);
  1032. #ifdef CHIP1370
  1033. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1034. &snd_es1370_hw_constraints_clock);
  1035. #else
  1036. snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  1037. &snd_es1371_hw_constraints_adc_clock);
  1038. #endif
  1039. return 0;
  1040. }
  1041. static int snd_ensoniq_playback1_close(snd_pcm_substream_t * substream)
  1042. {
  1043. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  1044. ensoniq->playback1_substream = NULL;
  1045. ensoniq->mode &= ~ES_MODE_PLAY1;
  1046. return 0;
  1047. }
  1048. static int snd_ensoniq_playback2_close(snd_pcm_substream_t * substream)
  1049. {
  1050. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  1051. ensoniq->playback2_substream = NULL;
  1052. spin_lock_irq(&ensoniq->reg_lock);
  1053. #ifdef CHIP1370
  1054. ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_PLAY2;
  1055. #endif
  1056. ensoniq->mode &= ~ES_MODE_PLAY2;
  1057. spin_unlock_irq(&ensoniq->reg_lock);
  1058. return 0;
  1059. }
  1060. static int snd_ensoniq_capture_close(snd_pcm_substream_t * substream)
  1061. {
  1062. ensoniq_t *ensoniq = snd_pcm_substream_chip(substream);
  1063. ensoniq->capture_substream = NULL;
  1064. spin_lock_irq(&ensoniq->reg_lock);
  1065. #ifdef CHIP1370
  1066. ensoniq->u.es1370.pclkdiv_lock &= ~ES_MODE_CAPTURE;
  1067. #endif
  1068. ensoniq->mode &= ~ES_MODE_CAPTURE;
  1069. spin_unlock_irq(&ensoniq->reg_lock);
  1070. return 0;
  1071. }
  1072. static snd_pcm_ops_t snd_ensoniq_playback1_ops = {
  1073. .open = snd_ensoniq_playback1_open,
  1074. .close = snd_ensoniq_playback1_close,
  1075. .ioctl = snd_pcm_lib_ioctl,
  1076. .hw_params = snd_ensoniq_hw_params,
  1077. .hw_free = snd_ensoniq_hw_free,
  1078. .prepare = snd_ensoniq_playback1_prepare,
  1079. .trigger = snd_ensoniq_trigger,
  1080. .pointer = snd_ensoniq_playback1_pointer,
  1081. };
  1082. static snd_pcm_ops_t snd_ensoniq_playback2_ops = {
  1083. .open = snd_ensoniq_playback2_open,
  1084. .close = snd_ensoniq_playback2_close,
  1085. .ioctl = snd_pcm_lib_ioctl,
  1086. .hw_params = snd_ensoniq_hw_params,
  1087. .hw_free = snd_ensoniq_hw_free,
  1088. .prepare = snd_ensoniq_playback2_prepare,
  1089. .trigger = snd_ensoniq_trigger,
  1090. .pointer = snd_ensoniq_playback2_pointer,
  1091. };
  1092. static snd_pcm_ops_t snd_ensoniq_capture_ops = {
  1093. .open = snd_ensoniq_capture_open,
  1094. .close = snd_ensoniq_capture_close,
  1095. .ioctl = snd_pcm_lib_ioctl,
  1096. .hw_params = snd_ensoniq_hw_params,
  1097. .hw_free = snd_ensoniq_hw_free,
  1098. .prepare = snd_ensoniq_capture_prepare,
  1099. .trigger = snd_ensoniq_trigger,
  1100. .pointer = snd_ensoniq_capture_pointer,
  1101. };
  1102. static void snd_ensoniq_pcm_free(snd_pcm_t *pcm)
  1103. {
  1104. ensoniq_t *ensoniq = pcm->private_data;
  1105. ensoniq->pcm1 = NULL;
  1106. snd_pcm_lib_preallocate_free_for_all(pcm);
  1107. }
  1108. static int __devinit snd_ensoniq_pcm(ensoniq_t * ensoniq, int device, snd_pcm_t ** rpcm)
  1109. {
  1110. snd_pcm_t *pcm;
  1111. int err;
  1112. if (rpcm)
  1113. *rpcm = NULL;
  1114. #ifdef CHIP1370
  1115. err = snd_pcm_new(ensoniq->card, "ES1370/1", device, 1, 1, &pcm);
  1116. #else
  1117. err = snd_pcm_new(ensoniq->card, "ES1371/1", device, 1, 1, &pcm);
  1118. #endif
  1119. if (err < 0)
  1120. return err;
  1121. #ifdef CHIP1370
  1122. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
  1123. #else
  1124. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
  1125. #endif
  1126. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_ensoniq_capture_ops);
  1127. pcm->private_data = ensoniq;
  1128. pcm->private_free = snd_ensoniq_pcm_free;
  1129. pcm->info_flags = 0;
  1130. #ifdef CHIP1370
  1131. strcpy(pcm->name, "ES1370 DAC2/ADC");
  1132. #else
  1133. strcpy(pcm->name, "ES1371 DAC2/ADC");
  1134. #endif
  1135. ensoniq->pcm1 = pcm;
  1136. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1137. snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
  1138. if (rpcm)
  1139. *rpcm = pcm;
  1140. return 0;
  1141. }
  1142. static void snd_ensoniq_pcm_free2(snd_pcm_t *pcm)
  1143. {
  1144. ensoniq_t *ensoniq = pcm->private_data;
  1145. ensoniq->pcm2 = NULL;
  1146. snd_pcm_lib_preallocate_free_for_all(pcm);
  1147. }
  1148. static int __devinit snd_ensoniq_pcm2(ensoniq_t * ensoniq, int device, snd_pcm_t ** rpcm)
  1149. {
  1150. snd_pcm_t *pcm;
  1151. int err;
  1152. if (rpcm)
  1153. *rpcm = NULL;
  1154. #ifdef CHIP1370
  1155. err = snd_pcm_new(ensoniq->card, "ES1370/2", device, 1, 0, &pcm);
  1156. #else
  1157. err = snd_pcm_new(ensoniq->card, "ES1371/2", device, 1, 0, &pcm);
  1158. #endif
  1159. if (err < 0)
  1160. return err;
  1161. #ifdef CHIP1370
  1162. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback1_ops);
  1163. #else
  1164. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_ensoniq_playback2_ops);
  1165. #endif
  1166. pcm->private_data = ensoniq;
  1167. pcm->private_free = snd_ensoniq_pcm_free2;
  1168. pcm->info_flags = 0;
  1169. #ifdef CHIP1370
  1170. strcpy(pcm->name, "ES1370 DAC1");
  1171. #else
  1172. strcpy(pcm->name, "ES1371 DAC1");
  1173. #endif
  1174. ensoniq->pcm2 = pcm;
  1175. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1176. snd_dma_pci_data(ensoniq->pci), 64*1024, 128*1024);
  1177. if (rpcm)
  1178. *rpcm = pcm;
  1179. return 0;
  1180. }
  1181. /*
  1182. * Mixer section
  1183. */
  1184. /*
  1185. * ENS1371 mixer (including SPDIF interface)
  1186. */
  1187. #ifdef CHIP1371
  1188. static int snd_ens1373_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1189. {
  1190. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1191. uinfo->count = 1;
  1192. return 0;
  1193. }
  1194. static int snd_ens1373_spdif_default_get(snd_kcontrol_t * kcontrol,
  1195. snd_ctl_elem_value_t * ucontrol)
  1196. {
  1197. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1198. spin_lock_irq(&ensoniq->reg_lock);
  1199. ucontrol->value.iec958.status[0] = (ensoniq->spdif_default >> 0) & 0xff;
  1200. ucontrol->value.iec958.status[1] = (ensoniq->spdif_default >> 8) & 0xff;
  1201. ucontrol->value.iec958.status[2] = (ensoniq->spdif_default >> 16) & 0xff;
  1202. ucontrol->value.iec958.status[3] = (ensoniq->spdif_default >> 24) & 0xff;
  1203. spin_unlock_irq(&ensoniq->reg_lock);
  1204. return 0;
  1205. }
  1206. static int snd_ens1373_spdif_default_put(snd_kcontrol_t * kcontrol,
  1207. snd_ctl_elem_value_t * ucontrol)
  1208. {
  1209. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1210. unsigned int val;
  1211. int change;
  1212. val = ((u32)ucontrol->value.iec958.status[0] << 0) |
  1213. ((u32)ucontrol->value.iec958.status[1] << 8) |
  1214. ((u32)ucontrol->value.iec958.status[2] << 16) |
  1215. ((u32)ucontrol->value.iec958.status[3] << 24);
  1216. spin_lock_irq(&ensoniq->reg_lock);
  1217. change = ensoniq->spdif_default != val;
  1218. ensoniq->spdif_default = val;
  1219. if (change && ensoniq->playback1_substream == NULL && ensoniq->playback2_substream == NULL)
  1220. outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
  1221. spin_unlock_irq(&ensoniq->reg_lock);
  1222. return change;
  1223. }
  1224. static int snd_ens1373_spdif_mask_get(snd_kcontrol_t * kcontrol,
  1225. snd_ctl_elem_value_t * ucontrol)
  1226. {
  1227. ucontrol->value.iec958.status[0] = 0xff;
  1228. ucontrol->value.iec958.status[1] = 0xff;
  1229. ucontrol->value.iec958.status[2] = 0xff;
  1230. ucontrol->value.iec958.status[3] = 0xff;
  1231. return 0;
  1232. }
  1233. static int snd_ens1373_spdif_stream_get(snd_kcontrol_t * kcontrol,
  1234. snd_ctl_elem_value_t * ucontrol)
  1235. {
  1236. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1237. spin_lock_irq(&ensoniq->reg_lock);
  1238. ucontrol->value.iec958.status[0] = (ensoniq->spdif_stream >> 0) & 0xff;
  1239. ucontrol->value.iec958.status[1] = (ensoniq->spdif_stream >> 8) & 0xff;
  1240. ucontrol->value.iec958.status[2] = (ensoniq->spdif_stream >> 16) & 0xff;
  1241. ucontrol->value.iec958.status[3] = (ensoniq->spdif_stream >> 24) & 0xff;
  1242. spin_unlock_irq(&ensoniq->reg_lock);
  1243. return 0;
  1244. }
  1245. static int snd_ens1373_spdif_stream_put(snd_kcontrol_t * kcontrol,
  1246. snd_ctl_elem_value_t * ucontrol)
  1247. {
  1248. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1249. unsigned int val;
  1250. int change;
  1251. val = ((u32)ucontrol->value.iec958.status[0] << 0) |
  1252. ((u32)ucontrol->value.iec958.status[1] << 8) |
  1253. ((u32)ucontrol->value.iec958.status[2] << 16) |
  1254. ((u32)ucontrol->value.iec958.status[3] << 24);
  1255. spin_lock_irq(&ensoniq->reg_lock);
  1256. change = ensoniq->spdif_stream != val;
  1257. ensoniq->spdif_stream = val;
  1258. if (change && (ensoniq->playback1_substream != NULL || ensoniq->playback2_substream != NULL))
  1259. outl(val, ES_REG(ensoniq, CHANNEL_STATUS));
  1260. spin_unlock_irq(&ensoniq->reg_lock);
  1261. return change;
  1262. }
  1263. #define ES1371_SPDIF(xname) \
  1264. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .info = snd_es1371_spdif_info, \
  1265. .get = snd_es1371_spdif_get, .put = snd_es1371_spdif_put }
  1266. static int snd_es1371_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1267. {
  1268. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1269. uinfo->count = 1;
  1270. uinfo->value.integer.min = 0;
  1271. uinfo->value.integer.max = 1;
  1272. return 0;
  1273. }
  1274. static int snd_es1371_spdif_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1275. {
  1276. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1277. spin_lock_irq(&ensoniq->reg_lock);
  1278. ucontrol->value.integer.value[0] = ensoniq->ctrl & ES_1373_SPDIF_THRU ? 1 : 0;
  1279. spin_unlock_irq(&ensoniq->reg_lock);
  1280. return 0;
  1281. }
  1282. static int snd_es1371_spdif_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1283. {
  1284. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1285. unsigned int nval1, nval2;
  1286. int change;
  1287. nval1 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_THRU : 0;
  1288. nval2 = ucontrol->value.integer.value[0] ? ES_1373_SPDIF_EN : 0;
  1289. spin_lock_irq(&ensoniq->reg_lock);
  1290. change = (ensoniq->ctrl & ES_1373_SPDIF_THRU) != nval1;
  1291. ensoniq->ctrl &= ~ES_1373_SPDIF_THRU;
  1292. ensoniq->ctrl |= nval1;
  1293. ensoniq->cssr &= ~ES_1373_SPDIF_EN;
  1294. ensoniq->cssr |= nval2;
  1295. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1296. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1297. spin_unlock_irq(&ensoniq->reg_lock);
  1298. return change;
  1299. }
  1300. /* spdif controls */
  1301. static snd_kcontrol_new_t snd_es1371_mixer_spdif[] __devinitdata = {
  1302. ES1371_SPDIF(SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH)),
  1303. {
  1304. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1305. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1306. .info = snd_ens1373_spdif_info,
  1307. .get = snd_ens1373_spdif_default_get,
  1308. .put = snd_ens1373_spdif_default_put,
  1309. },
  1310. {
  1311. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1312. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1313. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1314. .info = snd_ens1373_spdif_info,
  1315. .get = snd_ens1373_spdif_mask_get
  1316. },
  1317. {
  1318. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1319. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  1320. .info = snd_ens1373_spdif_info,
  1321. .get = snd_ens1373_spdif_stream_get,
  1322. .put = snd_ens1373_spdif_stream_put
  1323. },
  1324. };
  1325. static int snd_es1373_rear_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1326. {
  1327. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1328. uinfo->count = 1;
  1329. uinfo->value.integer.min = 0;
  1330. uinfo->value.integer.max = 1;
  1331. return 0;
  1332. }
  1333. static int snd_es1373_rear_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1334. {
  1335. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1336. int val = 0;
  1337. spin_lock_irq(&ensoniq->reg_lock);
  1338. if ((ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) == ES_1373_REAR_BIT26)
  1339. val = 1;
  1340. ucontrol->value.integer.value[0] = val;
  1341. spin_unlock_irq(&ensoniq->reg_lock);
  1342. return 0;
  1343. }
  1344. static int snd_es1373_rear_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1345. {
  1346. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1347. unsigned int nval1;
  1348. int change;
  1349. nval1 = ucontrol->value.integer.value[0] ? ES_1373_REAR_BIT26 : (ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
  1350. spin_lock_irq(&ensoniq->reg_lock);
  1351. change = (ensoniq->cssr & (ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24)) != nval1;
  1352. ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT26|ES_1373_REAR_BIT24);
  1353. ensoniq->cssr |= nval1;
  1354. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1355. spin_unlock_irq(&ensoniq->reg_lock);
  1356. return change;
  1357. }
  1358. static snd_kcontrol_new_t snd_ens1373_rear __devinitdata =
  1359. {
  1360. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1361. .name = "AC97 2ch->4ch Copy Switch",
  1362. .info = snd_es1373_rear_info,
  1363. .get = snd_es1373_rear_get,
  1364. .put = snd_es1373_rear_put,
  1365. };
  1366. static int snd_es1373_line_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1367. {
  1368. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1369. uinfo->count = 1;
  1370. uinfo->value.integer.min = 0;
  1371. uinfo->value.integer.max = 1;
  1372. return 0;
  1373. }
  1374. static int snd_es1373_line_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1375. {
  1376. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1377. int val = 0;
  1378. spin_lock_irq(&ensoniq->reg_lock);
  1379. if ((ensoniq->ctrl & ES_1371_GPIO_OUTM) >= 4)
  1380. val = 1;
  1381. ucontrol->value.integer.value[0] = val;
  1382. spin_unlock_irq(&ensoniq->reg_lock);
  1383. return 0;
  1384. }
  1385. static int snd_es1373_line_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1386. {
  1387. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1388. int changed;
  1389. unsigned int ctrl;
  1390. spin_lock_irq(&ensoniq->reg_lock);
  1391. ctrl = ensoniq->ctrl;
  1392. if (ucontrol->value.integer.value[0])
  1393. ensoniq->ctrl |= ES_1371_GPIO_OUT(4); /* switch line-in -> rear out */
  1394. else
  1395. ensoniq->ctrl &= ~ES_1371_GPIO_OUT(4);
  1396. changed = (ctrl != ensoniq->ctrl);
  1397. if (changed)
  1398. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1399. spin_unlock_irq(&ensoniq->reg_lock);
  1400. return changed;
  1401. }
  1402. static snd_kcontrol_new_t snd_ens1373_line __devinitdata =
  1403. {
  1404. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1405. .name = "Line In->Rear Out Switch",
  1406. .info = snd_es1373_line_info,
  1407. .get = snd_es1373_line_get,
  1408. .put = snd_es1373_line_put,
  1409. };
  1410. static void snd_ensoniq_mixer_free_ac97(ac97_t *ac97)
  1411. {
  1412. ensoniq_t *ensoniq = ac97->private_data;
  1413. ensoniq->u.es1371.ac97 = NULL;
  1414. }
  1415. static struct {
  1416. unsigned short vid; /* vendor ID */
  1417. unsigned short did; /* device ID */
  1418. unsigned char rev; /* revision */
  1419. } es1371_spdif_present[] __devinitdata = {
  1420. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
  1421. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
  1422. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
  1423. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
  1424. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
  1425. { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
  1426. };
  1427. static int snd_ensoniq_1371_mixer(ensoniq_t * ensoniq)
  1428. {
  1429. snd_card_t *card = ensoniq->card;
  1430. ac97_bus_t *pbus;
  1431. ac97_template_t ac97;
  1432. int err, idx;
  1433. static ac97_bus_ops_t ops = {
  1434. .write = snd_es1371_codec_write,
  1435. .read = snd_es1371_codec_read,
  1436. .wait = snd_es1371_codec_wait,
  1437. };
  1438. if ((err = snd_ac97_bus(card, 0, &ops, NULL, &pbus)) < 0)
  1439. return err;
  1440. memset(&ac97, 0, sizeof(ac97));
  1441. ac97.private_data = ensoniq;
  1442. ac97.private_free = snd_ensoniq_mixer_free_ac97;
  1443. ac97.scaps = AC97_SCAP_AUDIO;
  1444. if ((err = snd_ac97_mixer(pbus, &ac97, &ensoniq->u.es1371.ac97)) < 0)
  1445. return err;
  1446. for (idx = 0; es1371_spdif_present[idx].vid != (unsigned short)PCI_ANY_ID; idx++)
  1447. if (ensoniq->pci->vendor == es1371_spdif_present[idx].vid &&
  1448. ensoniq->pci->device == es1371_spdif_present[idx].did &&
  1449. ensoniq->rev == es1371_spdif_present[idx].rev) {
  1450. snd_kcontrol_t *kctl;
  1451. int i, index = 0;
  1452. ensoniq->spdif_default = ensoniq->spdif_stream = SNDRV_PCM_DEFAULT_CON_SPDIF;
  1453. outl(ensoniq->spdif_default, ES_REG(ensoniq, CHANNEL_STATUS));
  1454. if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SPDIF)
  1455. index++;
  1456. for (i = 0; i < (int)ARRAY_SIZE(snd_es1371_mixer_spdif); i++) {
  1457. kctl = snd_ctl_new1(&snd_es1371_mixer_spdif[i], ensoniq);
  1458. if (! kctl)
  1459. return -ENOMEM;
  1460. kctl->id.index = index;
  1461. if ((err = snd_ctl_add(card, kctl)) < 0)
  1462. return err;
  1463. }
  1464. break;
  1465. }
  1466. if (ensoniq->u.es1371.ac97->ext_id & AC97_EI_SDAC) {
  1467. /* mirror rear to front speakers */
  1468. ensoniq->cssr &= ~(ES_1373_REAR_BIT27|ES_1373_REAR_BIT24);
  1469. ensoniq->cssr |= ES_1373_REAR_BIT26;
  1470. err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_rear, ensoniq));
  1471. if (err < 0)
  1472. return err;
  1473. }
  1474. if (((ensoniq->subsystem_vendor_id == 0x1274) &&
  1475. (ensoniq->subsystem_device_id == 0x2000)) || /* GA-7DXR */
  1476. ((ensoniq->subsystem_vendor_id == 0x1458) &&
  1477. (ensoniq->subsystem_device_id == 0xa000))) { /* GA-8IEXP */
  1478. err = snd_ctl_add(card, snd_ctl_new1(&snd_ens1373_line, ensoniq));
  1479. if (err < 0)
  1480. return err;
  1481. }
  1482. return 0;
  1483. }
  1484. #endif /* CHIP1371 */
  1485. /* generic control callbacks for ens1370 */
  1486. #ifdef CHIP1370
  1487. #define ENSONIQ_CONTROL(xname, mask) \
  1488. { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, .info = snd_ensoniq_control_info, \
  1489. .get = snd_ensoniq_control_get, .put = snd_ensoniq_control_put, \
  1490. .private_value = mask }
  1491. static int snd_ensoniq_control_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1492. {
  1493. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1494. uinfo->count = 1;
  1495. uinfo->value.integer.min = 0;
  1496. uinfo->value.integer.max = 1;
  1497. return 0;
  1498. }
  1499. static int snd_ensoniq_control_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1500. {
  1501. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1502. int mask = kcontrol->private_value;
  1503. spin_lock_irq(&ensoniq->reg_lock);
  1504. ucontrol->value.integer.value[0] = ensoniq->ctrl & mask ? 1 : 0;
  1505. spin_unlock_irq(&ensoniq->reg_lock);
  1506. return 0;
  1507. }
  1508. static int snd_ensoniq_control_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1509. {
  1510. ensoniq_t *ensoniq = snd_kcontrol_chip(kcontrol);
  1511. int mask = kcontrol->private_value;
  1512. unsigned int nval;
  1513. int change;
  1514. nval = ucontrol->value.integer.value[0] ? mask : 0;
  1515. spin_lock_irq(&ensoniq->reg_lock);
  1516. change = (ensoniq->ctrl & mask) != nval;
  1517. ensoniq->ctrl &= ~mask;
  1518. ensoniq->ctrl |= nval;
  1519. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1520. spin_unlock_irq(&ensoniq->reg_lock);
  1521. return change;
  1522. }
  1523. /*
  1524. * ENS1370 mixer
  1525. */
  1526. static snd_kcontrol_new_t snd_es1370_controls[2] __devinitdata = {
  1527. ENSONIQ_CONTROL("PCM 0 Output also on Line-In Jack", ES_1370_XCTL0),
  1528. ENSONIQ_CONTROL("Mic +5V bias", ES_1370_XCTL1)
  1529. };
  1530. #define ES1370_CONTROLS ARRAY_SIZE(snd_es1370_controls)
  1531. static void snd_ensoniq_mixer_free_ak4531(ak4531_t *ak4531)
  1532. {
  1533. ensoniq_t *ensoniq = ak4531->private_data;
  1534. ensoniq->u.es1370.ak4531 = NULL;
  1535. }
  1536. static int __devinit snd_ensoniq_1370_mixer(ensoniq_t * ensoniq)
  1537. {
  1538. snd_card_t *card = ensoniq->card;
  1539. ak4531_t ak4531;
  1540. unsigned int idx;
  1541. int err;
  1542. /* try reset AK4531 */
  1543. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x02), ES_REG(ensoniq, 1370_CODEC));
  1544. inw(ES_REG(ensoniq, 1370_CODEC));
  1545. udelay(100);
  1546. outw(ES_1370_CODEC_WRITE(AK4531_RESET, 0x03), ES_REG(ensoniq, 1370_CODEC));
  1547. inw(ES_REG(ensoniq, 1370_CODEC));
  1548. udelay(100);
  1549. memset(&ak4531, 0, sizeof(ak4531));
  1550. ak4531.write = snd_es1370_codec_write;
  1551. ak4531.private_data = ensoniq;
  1552. ak4531.private_free = snd_ensoniq_mixer_free_ak4531;
  1553. if ((err = snd_ak4531_mixer(card, &ak4531, &ensoniq->u.es1370.ak4531)) < 0)
  1554. return err;
  1555. for (idx = 0; idx < ES1370_CONTROLS; idx++) {
  1556. err = snd_ctl_add(card, snd_ctl_new1(&snd_es1370_controls[idx], ensoniq));
  1557. if (err < 0)
  1558. return err;
  1559. }
  1560. return 0;
  1561. }
  1562. #endif /* CHIP1370 */
  1563. #ifdef SUPPORT_JOYSTICK
  1564. #ifdef CHIP1371
  1565. static int __devinit snd_ensoniq_get_joystick_port(int dev)
  1566. {
  1567. switch (joystick_port[dev]) {
  1568. case 0: /* disabled */
  1569. case 1: /* auto-detect */
  1570. case 0x200:
  1571. case 0x208:
  1572. case 0x210:
  1573. case 0x218:
  1574. return joystick_port[dev];
  1575. default:
  1576. printk(KERN_ERR "ens1371: invalid joystick port %#x", joystick_port[dev]);
  1577. return 0;
  1578. }
  1579. }
  1580. #else
  1581. static inline int snd_ensoniq_get_joystick_port(int dev)
  1582. {
  1583. return joystick[dev] ? 0x200 : 0;
  1584. }
  1585. #endif
  1586. static int __devinit snd_ensoniq_create_gameport(ensoniq_t *ensoniq, int dev)
  1587. {
  1588. struct gameport *gp;
  1589. int io_port;
  1590. io_port = snd_ensoniq_get_joystick_port(dev);
  1591. switch (io_port) {
  1592. case 0:
  1593. return -ENOSYS;
  1594. case 1: /* auto_detect */
  1595. for (io_port = 0x200; io_port <= 0x218; io_port += 8)
  1596. if (request_region(io_port, 8, "ens137x: gameport"))
  1597. break;
  1598. if (io_port > 0x218) {
  1599. printk(KERN_WARNING "ens137x: no gameport ports available\n");
  1600. return -EBUSY;
  1601. }
  1602. break;
  1603. default:
  1604. if (!request_region(io_port, 8, "ens137x: gameport")) {
  1605. printk(KERN_WARNING "ens137x: gameport io port 0x%#x in use\n", io_port);
  1606. return -EBUSY;
  1607. }
  1608. break;
  1609. }
  1610. ensoniq->gameport = gp = gameport_allocate_port();
  1611. if (!gp) {
  1612. printk(KERN_ERR "ens137x: cannot allocate memory for gameport\n");
  1613. release_region(io_port, 8);
  1614. return -ENOMEM;
  1615. }
  1616. gameport_set_name(gp, "ES137x");
  1617. gameport_set_phys(gp, "pci%s/gameport0", pci_name(ensoniq->pci));
  1618. gameport_set_dev_parent(gp, &ensoniq->pci->dev);
  1619. gp->io = io_port;
  1620. ensoniq->ctrl |= ES_JYSTK_EN;
  1621. #ifdef CHIP1371
  1622. ensoniq->ctrl &= ~ES_1371_JOY_ASELM;
  1623. ensoniq->ctrl |= ES_1371_JOY_ASEL((io_port - 0x200) / 8);
  1624. #endif
  1625. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1626. gameport_register_port(ensoniq->gameport);
  1627. return 0;
  1628. }
  1629. static void snd_ensoniq_free_gameport(ensoniq_t *ensoniq)
  1630. {
  1631. if (ensoniq->gameport) {
  1632. int port = ensoniq->gameport->io;
  1633. gameport_unregister_port(ensoniq->gameport);
  1634. ensoniq->gameport = NULL;
  1635. ensoniq->ctrl &= ~ES_JYSTK_EN;
  1636. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1637. release_region(port, 8);
  1638. }
  1639. }
  1640. #else
  1641. static inline int snd_ensoniq_create_gameport(ensoniq_t *ensoniq, long port) { return -ENOSYS; }
  1642. static inline void snd_ensoniq_free_gameport(ensoniq_t *ensoniq) { }
  1643. #endif /* SUPPORT_JOYSTICK */
  1644. /*
  1645. */
  1646. static void snd_ensoniq_proc_read(snd_info_entry_t *entry,
  1647. snd_info_buffer_t * buffer)
  1648. {
  1649. ensoniq_t *ensoniq = entry->private_data;
  1650. #ifdef CHIP1370
  1651. snd_iprintf(buffer, "Ensoniq AudioPCI ES1370\n\n");
  1652. #else
  1653. snd_iprintf(buffer, "Ensoniq AudioPCI ES1371\n\n");
  1654. #endif
  1655. snd_iprintf(buffer, "Joystick enable : %s\n", ensoniq->ctrl & ES_JYSTK_EN ? "on" : "off");
  1656. #ifdef CHIP1370
  1657. snd_iprintf(buffer, "MIC +5V bias : %s\n", ensoniq->ctrl & ES_1370_XCTL1 ? "on" : "off");
  1658. snd_iprintf(buffer, "Line In to AOUT : %s\n", ensoniq->ctrl & ES_1370_XCTL0 ? "on" : "off");
  1659. #else
  1660. snd_iprintf(buffer, "Joystick port : 0x%x\n", (ES_1371_JOY_ASELI(ensoniq->ctrl) * 8) + 0x200);
  1661. #endif
  1662. }
  1663. static void __devinit snd_ensoniq_proc_init(ensoniq_t * ensoniq)
  1664. {
  1665. snd_info_entry_t *entry;
  1666. if (! snd_card_proc_new(ensoniq->card, "audiopci", &entry))
  1667. snd_info_set_text_ops(entry, ensoniq, 1024, snd_ensoniq_proc_read);
  1668. }
  1669. /*
  1670. */
  1671. static int snd_ensoniq_free(ensoniq_t *ensoniq)
  1672. {
  1673. snd_ensoniq_free_gameport(ensoniq);
  1674. if (ensoniq->irq < 0)
  1675. goto __hw_end;
  1676. #ifdef CHIP1370
  1677. outl(ES_1370_SERR_DISABLE, ES_REG(ensoniq, CONTROL)); /* switch everything off */
  1678. outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
  1679. #else
  1680. outl(0, ES_REG(ensoniq, CONTROL)); /* switch everything off */
  1681. outl(0, ES_REG(ensoniq, SERIAL)); /* clear serial interface */
  1682. #endif
  1683. synchronize_irq(ensoniq->irq);
  1684. pci_set_power_state(ensoniq->pci, 3);
  1685. __hw_end:
  1686. #ifdef CHIP1370
  1687. if (ensoniq->dma_bug.area)
  1688. snd_dma_free_pages(&ensoniq->dma_bug);
  1689. #endif
  1690. if (ensoniq->irq >= 0)
  1691. free_irq(ensoniq->irq, (void *)ensoniq);
  1692. pci_release_regions(ensoniq->pci);
  1693. pci_disable_device(ensoniq->pci);
  1694. kfree(ensoniq);
  1695. return 0;
  1696. }
  1697. static int snd_ensoniq_dev_free(snd_device_t *device)
  1698. {
  1699. ensoniq_t *ensoniq = device->device_data;
  1700. return snd_ensoniq_free(ensoniq);
  1701. }
  1702. #ifdef CHIP1371
  1703. static struct {
  1704. unsigned short svid; /* subsystem vendor ID */
  1705. unsigned short sdid; /* subsystem device ID */
  1706. } es1371_amplifier_hack[] = {
  1707. { .svid = 0x107b, .sdid = 0x2150 }, /* Gateway Solo 2150 */
  1708. { .svid = 0x13bd, .sdid = 0x100c }, /* EV1938 on Mebius PC-MJ100V */
  1709. { .svid = 0x1102, .sdid = 0x5938 }, /* Targa Xtender300 */
  1710. { .svid = 0x1102, .sdid = 0x8938 }, /* IPC Topnote G notebook */
  1711. { .svid = PCI_ANY_ID, .sdid = PCI_ANY_ID }
  1712. };
  1713. static struct {
  1714. unsigned short vid; /* vendor ID */
  1715. unsigned short did; /* device ID */
  1716. unsigned char rev; /* revision */
  1717. } es1371_ac97_reset_hack[] = {
  1718. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_C },
  1719. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_D },
  1720. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_CT5880, .rev = CT5880REV_CT5880_E },
  1721. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_CT5880_A },
  1722. { .vid = PCI_VENDOR_ID_ENSONIQ, .did = PCI_DEVICE_ID_ENSONIQ_ES1371, .rev = ES1371REV_ES1373_8 },
  1723. { .vid = PCI_ANY_ID, .did = PCI_ANY_ID }
  1724. };
  1725. #endif
  1726. static void snd_ensoniq_chip_init(ensoniq_t * ensoniq)
  1727. {
  1728. #ifdef CHIP1371
  1729. int idx;
  1730. struct pci_dev *pci = ensoniq->pci;
  1731. #endif
  1732. // this code was part of snd_ensoniq_create before intruduction of suspend/resume
  1733. #ifdef CHIP1370
  1734. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1735. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  1736. outl(ES_MEM_PAGEO(ES_PAGE_ADC), ES_REG(ensoniq, MEM_PAGE));
  1737. outl(ensoniq->dma_bug.addr, ES_REG(ensoniq, PHANTOM_FRAME));
  1738. outl(0, ES_REG(ensoniq, PHANTOM_COUNT));
  1739. #else
  1740. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1741. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  1742. outl(0, ES_REG(ensoniq, 1371_LEGACY));
  1743. for (idx = 0; es1371_ac97_reset_hack[idx].vid != (unsigned short)PCI_ANY_ID; idx++)
  1744. if (pci->vendor == es1371_ac97_reset_hack[idx].vid &&
  1745. pci->device == es1371_ac97_reset_hack[idx].did &&
  1746. ensoniq->rev == es1371_ac97_reset_hack[idx].rev) {
  1747. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1748. /* need to delay around 20ms(bleech) to give
  1749. some CODECs enough time to wakeup */
  1750. msleep(20);
  1751. break;
  1752. }
  1753. /* AC'97 warm reset to start the bitclk */
  1754. outl(ensoniq->ctrl | ES_1371_SYNC_RES, ES_REG(ensoniq, CONTROL));
  1755. inl(ES_REG(ensoniq, CONTROL));
  1756. udelay(20);
  1757. outl(ensoniq->ctrl, ES_REG(ensoniq, CONTROL));
  1758. /* Init the sample rate converter */
  1759. snd_es1371_wait_src_ready(ensoniq);
  1760. outl(ES_1371_SRC_DISABLE, ES_REG(ensoniq, 1371_SMPRATE));
  1761. for (idx = 0; idx < 0x80; idx++)
  1762. snd_es1371_src_write(ensoniq, idx, 0);
  1763. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_TRUNC_N, 16 << 4);
  1764. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC1 + ES_SMPREG_INT_REGS, 16 << 10);
  1765. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_TRUNC_N, 16 << 4);
  1766. snd_es1371_src_write(ensoniq, ES_SMPREG_DAC2 + ES_SMPREG_INT_REGS, 16 << 10);
  1767. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC, 1 << 12);
  1768. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_ADC + 1, 1 << 12);
  1769. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1, 1 << 12);
  1770. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC1 + 1, 1 << 12);
  1771. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2, 1 << 12);
  1772. snd_es1371_src_write(ensoniq, ES_SMPREG_VOL_DAC2 + 1, 1 << 12);
  1773. snd_es1371_adc_rate(ensoniq, 22050);
  1774. snd_es1371_dac1_rate(ensoniq, 22050);
  1775. snd_es1371_dac2_rate(ensoniq, 22050);
  1776. /* WARNING:
  1777. * enabling the sample rate converter without properly programming
  1778. * its parameters causes the chip to lock up (the SRC busy bit will
  1779. * be stuck high, and I've found no way to rectify this other than
  1780. * power cycle) - Thomas Sailer
  1781. */
  1782. snd_es1371_wait_src_ready(ensoniq);
  1783. outl(0, ES_REG(ensoniq, 1371_SMPRATE));
  1784. /* try reset codec directly */
  1785. outl(ES_1371_CODEC_WRITE(0, 0), ES_REG(ensoniq, 1371_CODEC));
  1786. #endif
  1787. outb(ensoniq->uartc = 0x00, ES_REG(ensoniq, UART_CONTROL));
  1788. outb(0x00, ES_REG(ensoniq, UART_RES));
  1789. outl(ensoniq->cssr, ES_REG(ensoniq, STATUS));
  1790. synchronize_irq(ensoniq->irq);
  1791. }
  1792. #ifdef CONFIG_PM
  1793. static int snd_ensoniq_suspend (snd_card_t * card,
  1794. pm_message_t state)
  1795. {
  1796. ensoniq_t *ensoniq = card->pm_private_data;
  1797. snd_pcm_suspend_all(ensoniq->pcm1);
  1798. snd_pcm_suspend_all(ensoniq->pcm2);
  1799. #ifdef CHIP1371
  1800. if (ensoniq->u.es1371.ac97)
  1801. snd_ac97_suspend(ensoniq->u.es1371.ac97);
  1802. #else
  1803. /* FIXME */
  1804. #endif
  1805. pci_set_power_state(ensoniq->pci, 3);
  1806. pci_disable_device(ensoniq->pci);
  1807. // snd_power_change_state(card, SNDRV_CTL_POWER_D3hot); // only 2.6.10
  1808. return 0;
  1809. }
  1810. static int snd_ensoniq_resume (snd_card_t * card
  1811. )
  1812. {
  1813. ensoniq_t *ensoniq = card->pm_private_data;
  1814. pci_enable_device(ensoniq->pci);
  1815. pci_set_power_state(ensoniq->pci, 0);
  1816. pci_set_master(ensoniq->pci);
  1817. snd_ensoniq_chip_init(ensoniq);
  1818. #ifdef CHIP1371
  1819. if (ensoniq->u.es1371.ac97)
  1820. snd_ac97_resume(ensoniq->u.es1371.ac97);
  1821. #else
  1822. /* FIXME */
  1823. #endif
  1824. // snd_power_change_state(card, SNDRV_CTL_POWER_D0); // only 2.6.10
  1825. return 0;
  1826. }
  1827. #endif /* CONFIG_PM */
  1828. static int __devinit snd_ensoniq_create(snd_card_t * card,
  1829. struct pci_dev *pci,
  1830. ensoniq_t ** rensoniq)
  1831. {
  1832. ensoniq_t *ensoniq;
  1833. unsigned short cmdw;
  1834. unsigned char cmdb;
  1835. #ifdef CHIP1371
  1836. int idx;
  1837. #endif
  1838. int err;
  1839. static snd_device_ops_t ops = {
  1840. .dev_free = snd_ensoniq_dev_free,
  1841. };
  1842. *rensoniq = NULL;
  1843. if ((err = pci_enable_device(pci)) < 0)
  1844. return err;
  1845. ensoniq = kzalloc(sizeof(*ensoniq), GFP_KERNEL);
  1846. if (ensoniq == NULL) {
  1847. pci_disable_device(pci);
  1848. return -ENOMEM;
  1849. }
  1850. spin_lock_init(&ensoniq->reg_lock);
  1851. init_MUTEX(&ensoniq->src_mutex);
  1852. ensoniq->card = card;
  1853. ensoniq->pci = pci;
  1854. ensoniq->irq = -1;
  1855. if ((err = pci_request_regions(pci, "Ensoniq AudioPCI")) < 0) {
  1856. kfree(ensoniq);
  1857. pci_disable_device(pci);
  1858. return err;
  1859. }
  1860. ensoniq->port = pci_resource_start(pci, 0);
  1861. if (request_irq(pci->irq, snd_audiopci_interrupt, SA_INTERRUPT|SA_SHIRQ, "Ensoniq AudioPCI", (void *)ensoniq)) {
  1862. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  1863. snd_ensoniq_free(ensoniq);
  1864. return -EBUSY;
  1865. }
  1866. ensoniq->irq = pci->irq;
  1867. #ifdef CHIP1370
  1868. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(pci),
  1869. 16, &ensoniq->dma_bug) < 0) {
  1870. snd_printk(KERN_ERR "unable to allocate space for phantom area - dma_bug\n");
  1871. snd_ensoniq_free(ensoniq);
  1872. return -EBUSY;
  1873. }
  1874. #endif
  1875. pci_set_master(pci);
  1876. pci_read_config_byte(pci, PCI_REVISION_ID, &cmdb);
  1877. ensoniq->rev = cmdb;
  1878. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &cmdw);
  1879. ensoniq->subsystem_vendor_id = cmdw;
  1880. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &cmdw);
  1881. ensoniq->subsystem_device_id = cmdw;
  1882. #ifdef CHIP1370
  1883. #if 0
  1884. ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_SERR_DISABLE | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
  1885. #else /* get microphone working */
  1886. ensoniq->ctrl = ES_1370_CDC_EN | ES_1370_PCLKDIVO(ES_1370_SRTODIV(8000));
  1887. #endif
  1888. ensoniq->sctrl = 0;
  1889. #else
  1890. ensoniq->ctrl = 0;
  1891. ensoniq->sctrl = 0;
  1892. ensoniq->cssr = 0;
  1893. for (idx = 0; es1371_amplifier_hack[idx].svid != (unsigned short)PCI_ANY_ID; idx++)
  1894. if (ensoniq->subsystem_vendor_id == es1371_amplifier_hack[idx].svid &&
  1895. ensoniq->subsystem_device_id == es1371_amplifier_hack[idx].sdid) {
  1896. ensoniq->ctrl |= ES_1371_GPIO_OUT(1); /* turn amplifier on */
  1897. break;
  1898. }
  1899. for (idx = 0; es1371_ac97_reset_hack[idx].vid != (unsigned short)PCI_ANY_ID; idx++)
  1900. if (pci->vendor == es1371_ac97_reset_hack[idx].vid &&
  1901. pci->device == es1371_ac97_reset_hack[idx].did &&
  1902. ensoniq->rev == es1371_ac97_reset_hack[idx].rev) {
  1903. ensoniq->cssr |= ES_1371_ST_AC97_RST;
  1904. break;
  1905. }
  1906. #endif
  1907. snd_ensoniq_chip_init(ensoniq);
  1908. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, ensoniq, &ops)) < 0) {
  1909. snd_ensoniq_free(ensoniq);
  1910. return err;
  1911. }
  1912. snd_ensoniq_proc_init(ensoniq);
  1913. snd_card_set_pm_callback(card, snd_ensoniq_suspend, snd_ensoniq_resume, ensoniq);
  1914. snd_card_set_dev(card, &pci->dev);
  1915. *rensoniq = ensoniq;
  1916. return 0;
  1917. }
  1918. /*
  1919. * MIDI section
  1920. */
  1921. static void snd_ensoniq_midi_interrupt(ensoniq_t * ensoniq)
  1922. {
  1923. snd_rawmidi_t * rmidi = ensoniq->rmidi;
  1924. unsigned char status, mask, byte;
  1925. if (rmidi == NULL)
  1926. return;
  1927. /* do Rx at first */
  1928. spin_lock(&ensoniq->reg_lock);
  1929. mask = ensoniq->uartm & ES_MODE_INPUT ? ES_RXRDY : 0;
  1930. while (mask) {
  1931. status = inb(ES_REG(ensoniq, UART_STATUS));
  1932. if ((status & mask) == 0)
  1933. break;
  1934. byte = inb(ES_REG(ensoniq, UART_DATA));
  1935. snd_rawmidi_receive(ensoniq->midi_input, &byte, 1);
  1936. }
  1937. spin_unlock(&ensoniq->reg_lock);
  1938. /* do Tx at second */
  1939. spin_lock(&ensoniq->reg_lock);
  1940. mask = ensoniq->uartm & ES_MODE_OUTPUT ? ES_TXRDY : 0;
  1941. while (mask) {
  1942. status = inb(ES_REG(ensoniq, UART_STATUS));
  1943. if ((status & mask) == 0)
  1944. break;
  1945. if (snd_rawmidi_transmit(ensoniq->midi_output, &byte, 1) != 1) {
  1946. ensoniq->uartc &= ~ES_TXINTENM;
  1947. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  1948. mask &= ~ES_TXRDY;
  1949. } else {
  1950. outb(byte, ES_REG(ensoniq, UART_DATA));
  1951. }
  1952. }
  1953. spin_unlock(&ensoniq->reg_lock);
  1954. }
  1955. static int snd_ensoniq_midi_input_open(snd_rawmidi_substream_t * substream)
  1956. {
  1957. ensoniq_t *ensoniq = substream->rmidi->private_data;
  1958. spin_lock_irq(&ensoniq->reg_lock);
  1959. ensoniq->uartm |= ES_MODE_INPUT;
  1960. ensoniq->midi_input = substream;
  1961. if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
  1962. outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
  1963. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1964. outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1965. }
  1966. spin_unlock_irq(&ensoniq->reg_lock);
  1967. return 0;
  1968. }
  1969. static int snd_ensoniq_midi_input_close(snd_rawmidi_substream_t * substream)
  1970. {
  1971. ensoniq_t *ensoniq = substream->rmidi->private_data;
  1972. spin_lock_irq(&ensoniq->reg_lock);
  1973. if (!(ensoniq->uartm & ES_MODE_OUTPUT)) {
  1974. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1975. outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1976. } else {
  1977. outb(ensoniq->uartc &= ~ES_RXINTEN, ES_REG(ensoniq, UART_CONTROL));
  1978. }
  1979. ensoniq->midi_input = NULL;
  1980. ensoniq->uartm &= ~ES_MODE_INPUT;
  1981. spin_unlock_irq(&ensoniq->reg_lock);
  1982. return 0;
  1983. }
  1984. static int snd_ensoniq_midi_output_open(snd_rawmidi_substream_t * substream)
  1985. {
  1986. ensoniq_t *ensoniq = substream->rmidi->private_data;
  1987. spin_lock_irq(&ensoniq->reg_lock);
  1988. ensoniq->uartm |= ES_MODE_OUTPUT;
  1989. ensoniq->midi_output = substream;
  1990. if (!(ensoniq->uartm & ES_MODE_INPUT)) {
  1991. outb(ES_CNTRL(3), ES_REG(ensoniq, UART_CONTROL));
  1992. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  1993. outl(ensoniq->ctrl |= ES_UART_EN, ES_REG(ensoniq, CONTROL));
  1994. }
  1995. spin_unlock_irq(&ensoniq->reg_lock);
  1996. return 0;
  1997. }
  1998. static int snd_ensoniq_midi_output_close(snd_rawmidi_substream_t * substream)
  1999. {
  2000. ensoniq_t *ensoniq = substream->rmidi->private_data;
  2001. spin_lock_irq(&ensoniq->reg_lock);
  2002. if (!(ensoniq->uartm & ES_MODE_INPUT)) {
  2003. outb(ensoniq->uartc = 0, ES_REG(ensoniq, UART_CONTROL));
  2004. outl(ensoniq->ctrl &= ~ES_UART_EN, ES_REG(ensoniq, CONTROL));
  2005. } else {
  2006. outb(ensoniq->uartc &= ~ES_TXINTENM, ES_REG(ensoniq, UART_CONTROL));
  2007. }
  2008. ensoniq->midi_output = NULL;
  2009. ensoniq->uartm &= ~ES_MODE_OUTPUT;
  2010. spin_unlock_irq(&ensoniq->reg_lock);
  2011. return 0;
  2012. }
  2013. static void snd_ensoniq_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  2014. {
  2015. unsigned long flags;
  2016. ensoniq_t *ensoniq = substream->rmidi->private_data;
  2017. int idx;
  2018. spin_lock_irqsave(&ensoniq->reg_lock, flags);
  2019. if (up) {
  2020. if ((ensoniq->uartc & ES_RXINTEN) == 0) {
  2021. /* empty input FIFO */
  2022. for (idx = 0; idx < 32; idx++)
  2023. inb(ES_REG(ensoniq, UART_DATA));
  2024. ensoniq->uartc |= ES_RXINTEN;
  2025. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  2026. }
  2027. } else {
  2028. if (ensoniq->uartc & ES_RXINTEN) {
  2029. ensoniq->uartc &= ~ES_RXINTEN;
  2030. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  2031. }
  2032. }
  2033. spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
  2034. }
  2035. static void snd_ensoniq_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  2036. {
  2037. unsigned long flags;
  2038. ensoniq_t *ensoniq = substream->rmidi->private_data;
  2039. unsigned char byte;
  2040. spin_lock_irqsave(&ensoniq->reg_lock, flags);
  2041. if (up) {
  2042. if (ES_TXINTENI(ensoniq->uartc) == 0) {
  2043. ensoniq->uartc |= ES_TXINTENO(1);
  2044. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  2045. while (ES_TXINTENI(ensoniq->uartc) == 1 &&
  2046. (inb(ES_REG(ensoniq, UART_STATUS)) & ES_TXRDY)) {
  2047. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  2048. ensoniq->uartc &= ~ES_TXINTENM;
  2049. } else {
  2050. outb(byte, ES_REG(ensoniq, UART_DATA));
  2051. }
  2052. }
  2053. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  2054. }
  2055. } else {
  2056. if (ES_TXINTENI(ensoniq->uartc) == 1) {
  2057. ensoniq->uartc &= ~ES_TXINTENM;
  2058. outb(ensoniq->uartc, ES_REG(ensoniq, UART_CONTROL));
  2059. }
  2060. }
  2061. spin_unlock_irqrestore(&ensoniq->reg_lock, flags);
  2062. }
  2063. static snd_rawmidi_ops_t snd_ensoniq_midi_output =
  2064. {
  2065. .open = snd_ensoniq_midi_output_open,
  2066. .close = snd_ensoniq_midi_output_close,
  2067. .trigger = snd_ensoniq_midi_output_trigger,
  2068. };
  2069. static snd_rawmidi_ops_t snd_ensoniq_midi_input =
  2070. {
  2071. .open = snd_ensoniq_midi_input_open,
  2072. .close = snd_ensoniq_midi_input_close,
  2073. .trigger = snd_ensoniq_midi_input_trigger,
  2074. };
  2075. static int __devinit snd_ensoniq_midi(ensoniq_t * ensoniq, int device, snd_rawmidi_t **rrawmidi)
  2076. {
  2077. snd_rawmidi_t *rmidi;
  2078. int err;
  2079. if (rrawmidi)
  2080. *rrawmidi = NULL;
  2081. if ((err = snd_rawmidi_new(ensoniq->card, "ES1370/1", device, 1, 1, &rmidi)) < 0)
  2082. return err;
  2083. #ifdef CHIP1370
  2084. strcpy(rmidi->name, "ES1370");
  2085. #else
  2086. strcpy(rmidi->name, "ES1371");
  2087. #endif
  2088. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_ensoniq_midi_output);
  2089. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_ensoniq_midi_input);
  2090. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  2091. rmidi->private_data = ensoniq;
  2092. ensoniq->rmidi = rmidi;
  2093. if (rrawmidi)
  2094. *rrawmidi = rmidi;
  2095. return 0;
  2096. }
  2097. /*
  2098. * Interrupt handler
  2099. */
  2100. static irqreturn_t snd_audiopci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  2101. {
  2102. ensoniq_t *ensoniq = dev_id;
  2103. unsigned int status, sctrl;
  2104. if (ensoniq == NULL)
  2105. return IRQ_NONE;
  2106. status = inl(ES_REG(ensoniq, STATUS));
  2107. if (!(status & ES_INTR))
  2108. return IRQ_NONE;
  2109. spin_lock(&ensoniq->reg_lock);
  2110. sctrl = ensoniq->sctrl;
  2111. if (status & ES_DAC1)
  2112. sctrl &= ~ES_P1_INT_EN;
  2113. if (status & ES_DAC2)
  2114. sctrl &= ~ES_P2_INT_EN;
  2115. if (status & ES_ADC)
  2116. sctrl &= ~ES_R1_INT_EN;
  2117. outl(sctrl, ES_REG(ensoniq, SERIAL));
  2118. outl(ensoniq->sctrl, ES_REG(ensoniq, SERIAL));
  2119. spin_unlock(&ensoniq->reg_lock);
  2120. if (status & ES_UART)
  2121. snd_ensoniq_midi_interrupt(ensoniq);
  2122. if ((status & ES_DAC2) && ensoniq->playback2_substream)
  2123. snd_pcm_period_elapsed(ensoniq->playback2_substream);
  2124. if ((status & ES_ADC) && ensoniq->capture_substream)
  2125. snd_pcm_period_elapsed(ensoniq->capture_substream);
  2126. if ((status & ES_DAC1) && ensoniq->playback1_substream)
  2127. snd_pcm_period_elapsed(ensoniq->playback1_substream);
  2128. return IRQ_HANDLED;
  2129. }
  2130. static int __devinit snd_audiopci_probe(struct pci_dev *pci,
  2131. const struct pci_device_id *pci_id)
  2132. {
  2133. static int dev;
  2134. snd_card_t *card;
  2135. ensoniq_t *ensoniq;
  2136. int err, pcm_devs[2];
  2137. if (dev >= SNDRV_CARDS)
  2138. return -ENODEV;
  2139. if (!enable[dev]) {
  2140. dev++;
  2141. return -ENOENT;
  2142. }
  2143. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  2144. if (card == NULL)
  2145. return -ENOMEM;
  2146. if ((err = snd_ensoniq_create(card, pci, &ensoniq)) < 0) {
  2147. snd_card_free(card);
  2148. return err;
  2149. }
  2150. pcm_devs[0] = 0; pcm_devs[1] = 1;
  2151. #ifdef CHIP1370
  2152. if ((err = snd_ensoniq_1370_mixer(ensoniq)) < 0) {
  2153. snd_card_free(card);
  2154. return err;
  2155. }
  2156. #endif
  2157. #ifdef CHIP1371
  2158. if ((err = snd_ensoniq_1371_mixer(ensoniq)) < 0) {
  2159. snd_card_free(card);
  2160. return err;
  2161. }
  2162. #endif
  2163. if ((err = snd_ensoniq_pcm(ensoniq, 0, NULL)) < 0) {
  2164. snd_card_free(card);
  2165. return err;
  2166. }
  2167. if ((err = snd_ensoniq_pcm2(ensoniq, 1, NULL)) < 0) {
  2168. snd_card_free(card);
  2169. return err;
  2170. }
  2171. if ((err = snd_ensoniq_midi(ensoniq, 0, NULL)) < 0) {
  2172. snd_card_free(card);
  2173. return err;
  2174. }
  2175. snd_ensoniq_create_gameport(ensoniq, dev);
  2176. strcpy(card->driver, DRIVER_NAME);
  2177. strcpy(card->shortname, "Ensoniq AudioPCI");
  2178. sprintf(card->longname, "%s %s at 0x%lx, irq %i",
  2179. card->shortname,
  2180. card->driver,
  2181. ensoniq->port,
  2182. ensoniq->irq);
  2183. if ((err = snd_card_register(card)) < 0) {
  2184. snd_card_free(card);
  2185. return err;
  2186. }
  2187. pci_set_drvdata(pci, card);
  2188. dev++;
  2189. return 0;
  2190. }
  2191. static void __devexit snd_audiopci_remove(struct pci_dev *pci)
  2192. {
  2193. snd_card_free(pci_get_drvdata(pci));
  2194. pci_set_drvdata(pci, NULL);
  2195. }
  2196. static struct pci_driver driver = {
  2197. .name = DRIVER_NAME,
  2198. .id_table = snd_audiopci_ids,
  2199. .probe = snd_audiopci_probe,
  2200. .remove = __devexit_p(snd_audiopci_remove),
  2201. SND_PCI_PM_CALLBACKS
  2202. };
  2203. static int __init alsa_card_ens137x_init(void)
  2204. {
  2205. return pci_register_driver(&driver);
  2206. }
  2207. static void __exit alsa_card_ens137x_exit(void)
  2208. {
  2209. pci_unregister_driver(&driver);
  2210. }
  2211. module_init(alsa_card_ens137x_init)
  2212. module_exit(alsa_card_ens137x_exit)