ens1370.c 75 KB

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