ens1370.c 78 KB

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