ens1370.c 77 KB

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