ens1370.c 79 KB

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