ens1370.c 81 KB

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