ens1370.c 79 KB

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