ens1370.c 79 KB

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