ens1370.c 77 KB

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