cmipci.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072
  1. /*
  2. * Driver for C-Media CMI8338 and 8738 PCI soundcards.
  3. * Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /* Does not work. Warning may block system in capture mode */
  20. /* #define USE_VAR48KRATE */
  21. #include <sound/driver.h>
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/slab.h>
  28. #include <linux/gameport.h>
  29. #include <linux/moduleparam.h>
  30. #include <sound/core.h>
  31. #include <sound/info.h>
  32. #include <sound/control.h>
  33. #include <sound/pcm.h>
  34. #include <sound/rawmidi.h>
  35. #include <sound/mpu401.h>
  36. #include <sound/opl3.h>
  37. #include <sound/sb.h>
  38. #include <sound/asoundef.h>
  39. #include <sound/initval.h>
  40. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  41. MODULE_DESCRIPTION("C-Media CMI8x38 PCI");
  42. MODULE_LICENSE("GPL");
  43. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8738},"
  44. "{C-Media,CMI8738B},"
  45. "{C-Media,CMI8338A},"
  46. "{C-Media,CMI8338B}}");
  47. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  48. #define SUPPORT_JOYSTICK 1
  49. #endif
  50. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  51. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  52. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable switches */
  53. static long mpu_port[SNDRV_CARDS];
  54. static long fm_port[SNDRV_CARDS];
  55. static int soft_ac3[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)]=1};
  56. #ifdef SUPPORT_JOYSTICK
  57. static int joystick_port[SNDRV_CARDS];
  58. #endif
  59. module_param_array(index, int, NULL, 0444);
  60. MODULE_PARM_DESC(index, "Index value for C-Media PCI soundcard.");
  61. module_param_array(id, charp, NULL, 0444);
  62. MODULE_PARM_DESC(id, "ID string for C-Media PCI soundcard.");
  63. module_param_array(enable, bool, NULL, 0444);
  64. MODULE_PARM_DESC(enable, "Enable C-Media PCI soundcard.");
  65. module_param_array(mpu_port, long, NULL, 0444);
  66. MODULE_PARM_DESC(mpu_port, "MPU-401 port.");
  67. module_param_array(fm_port, long, NULL, 0444);
  68. MODULE_PARM_DESC(fm_port, "FM port.");
  69. module_param_array(soft_ac3, bool, NULL, 0444);
  70. MODULE_PARM_DESC(soft_ac3, "Sofware-conversion of raw SPDIF packets (model 033 only).");
  71. #ifdef SUPPORT_JOYSTICK
  72. module_param_array(joystick_port, int, NULL, 0444);
  73. MODULE_PARM_DESC(joystick_port, "Joystick port address.");
  74. #endif
  75. /*
  76. * CM8x38 registers definition
  77. */
  78. #define CM_REG_FUNCTRL0 0x00
  79. #define CM_RST_CH1 0x00080000
  80. #define CM_RST_CH0 0x00040000
  81. #define CM_CHEN1 0x00020000 /* ch1: enable */
  82. #define CM_CHEN0 0x00010000 /* ch0: enable */
  83. #define CM_PAUSE1 0x00000008 /* ch1: pause */
  84. #define CM_PAUSE0 0x00000004 /* ch0: pause */
  85. #define CM_CHADC1 0x00000002 /* ch1, 0:playback, 1:record */
  86. #define CM_CHADC0 0x00000001 /* ch0, 0:playback, 1:record */
  87. #define CM_REG_FUNCTRL1 0x04
  88. #define CM_ASFC_MASK 0x0000E000 /* ADC sampling frequency */
  89. #define CM_ASFC_SHIFT 13
  90. #define CM_DSFC_MASK 0x00001C00 /* DAC sampling frequency */
  91. #define CM_DSFC_SHIFT 10
  92. #define CM_SPDF_1 0x00000200 /* SPDIF IN/OUT at channel B */
  93. #define CM_SPDF_0 0x00000100 /* SPDIF OUT only channel A */
  94. #define CM_SPDFLOOP 0x00000080 /* ext. SPDIIF/OUT -> IN loopback */
  95. #define CM_SPDO2DAC 0x00000040 /* SPDIF/OUT can be heard from internal DAC */
  96. #define CM_INTRM 0x00000020 /* master control block (MCB) interrupt enabled */
  97. #define CM_BREQ 0x00000010 /* bus master enabled */
  98. #define CM_VOICE_EN 0x00000008 /* legacy voice (SB16,FM) */
  99. #define CM_UART_EN 0x00000004 /* UART */
  100. #define CM_JYSTK_EN 0x00000002 /* joy stick */
  101. #define CM_REG_CHFORMAT 0x08
  102. #define CM_CHB3D5C 0x80000000 /* 5,6 channels */
  103. #define CM_CHB3D 0x20000000 /* 4 channels */
  104. #define CM_CHIP_MASK1 0x1f000000
  105. #define CM_CHIP_037 0x01000000
  106. #define CM_SPDIF_SELECT1 0x00080000 /* for model <= 037 ? */
  107. #define CM_AC3EN1 0x00100000 /* enable AC3: model 037 */
  108. #define CM_SPD24SEL 0x00020000 /* 24bit spdif: model 037 */
  109. /* #define CM_SPDIF_INVERSE 0x00010000 */ /* ??? */
  110. #define CM_ADCBITLEN_MASK 0x0000C000
  111. #define CM_ADCBITLEN_16 0x00000000
  112. #define CM_ADCBITLEN_15 0x00004000
  113. #define CM_ADCBITLEN_14 0x00008000
  114. #define CM_ADCBITLEN_13 0x0000C000
  115. #define CM_ADCDACLEN_MASK 0x00003000
  116. #define CM_ADCDACLEN_060 0x00000000
  117. #define CM_ADCDACLEN_066 0x00001000
  118. #define CM_ADCDACLEN_130 0x00002000
  119. #define CM_ADCDACLEN_280 0x00003000
  120. #define CM_CH1_SRATE_176K 0x00000800
  121. #define CM_CH1_SRATE_88K 0x00000400
  122. #define CM_CH0_SRATE_176K 0x00000200
  123. #define CM_CH0_SRATE_88K 0x00000100
  124. #define CM_SPDIF_INVERSE2 0x00000080 /* model 055? */
  125. #define CM_CH1FMT_MASK 0x0000000C
  126. #define CM_CH1FMT_SHIFT 2
  127. #define CM_CH0FMT_MASK 0x00000003
  128. #define CM_CH0FMT_SHIFT 0
  129. #define CM_REG_INT_HLDCLR 0x0C
  130. #define CM_CHIP_MASK2 0xff000000
  131. #define CM_CHIP_039 0x04000000
  132. #define CM_CHIP_039_6CH 0x01000000
  133. #define CM_CHIP_055 0x08000000
  134. #define CM_CHIP_8768 0x20000000
  135. #define CM_TDMA_INT_EN 0x00040000
  136. #define CM_CH1_INT_EN 0x00020000
  137. #define CM_CH0_INT_EN 0x00010000
  138. #define CM_INT_HOLD 0x00000002
  139. #define CM_INT_CLEAR 0x00000001
  140. #define CM_REG_INT_STATUS 0x10
  141. #define CM_INTR 0x80000000
  142. #define CM_VCO 0x08000000 /* Voice Control? CMI8738 */
  143. #define CM_MCBINT 0x04000000 /* Master Control Block abort cond.? */
  144. #define CM_UARTINT 0x00010000
  145. #define CM_LTDMAINT 0x00008000
  146. #define CM_HTDMAINT 0x00004000
  147. #define CM_XDO46 0x00000080 /* Modell 033? Direct programming EEPROM (read data register) */
  148. #define CM_LHBTOG 0x00000040 /* High/Low status from DMA ctrl register */
  149. #define CM_LEG_HDMA 0x00000020 /* Legacy is in High DMA channel */
  150. #define CM_LEG_STEREO 0x00000010 /* Legacy is in Stereo mode */
  151. #define CM_CH1BUSY 0x00000008
  152. #define CM_CH0BUSY 0x00000004
  153. #define CM_CHINT1 0x00000002
  154. #define CM_CHINT0 0x00000001
  155. #define CM_REG_LEGACY_CTRL 0x14
  156. #define CM_NXCHG 0x80000000 /* h/w multi channels? */
  157. #define CM_VMPU_MASK 0x60000000 /* MPU401 i/o port address */
  158. #define CM_VMPU_330 0x00000000
  159. #define CM_VMPU_320 0x20000000
  160. #define CM_VMPU_310 0x40000000
  161. #define CM_VMPU_300 0x60000000
  162. #define CM_VSBSEL_MASK 0x0C000000 /* SB16 base address */
  163. #define CM_VSBSEL_220 0x00000000
  164. #define CM_VSBSEL_240 0x04000000
  165. #define CM_VSBSEL_260 0x08000000
  166. #define CM_VSBSEL_280 0x0C000000
  167. #define CM_FMSEL_MASK 0x03000000 /* FM OPL3 base address */
  168. #define CM_FMSEL_388 0x00000000
  169. #define CM_FMSEL_3C8 0x01000000
  170. #define CM_FMSEL_3E0 0x02000000
  171. #define CM_FMSEL_3E8 0x03000000
  172. #define CM_ENSPDOUT 0x00800000 /* enable XPDIF/OUT to I/O interface */
  173. #define CM_SPDCOPYRHT 0x00400000 /* set copyright spdif in/out */
  174. #define CM_DAC2SPDO 0x00200000 /* enable wave+fm_midi -> SPDIF/OUT */
  175. #define CM_SETRETRY 0x00010000 /* 0: legacy i/o wait (default), 1: legacy i/o bus retry */
  176. #define CM_CHB3D6C 0x00008000 /* 5.1 channels support */
  177. #define CM_LINE_AS_BASS 0x00006000 /* use line-in as bass */
  178. #define CM_REG_MISC_CTRL 0x18
  179. #define CM_PWD 0x80000000
  180. #define CM_RESET 0x40000000
  181. #define CM_SFIL_MASK 0x30000000
  182. #define CM_TXVX 0x08000000
  183. #define CM_N4SPK3D 0x04000000 /* 4ch output */
  184. #define CM_SPDO5V 0x02000000 /* 5V spdif output (1 = 0.5v (coax)) */
  185. #define CM_SPDIF48K 0x01000000 /* write */
  186. #define CM_SPATUS48K 0x01000000 /* read */
  187. #define CM_ENDBDAC 0x00800000 /* enable dual dac */
  188. #define CM_XCHGDAC 0x00400000 /* 0: front=ch0, 1: front=ch1 */
  189. #define CM_SPD32SEL 0x00200000 /* 0: 16bit SPDIF, 1: 32bit */
  190. #define CM_SPDFLOOPI 0x00100000 /* int. SPDIF-IN -> int. OUT */
  191. #define CM_FM_EN 0x00080000 /* enalbe FM */
  192. #define CM_AC3EN2 0x00040000 /* enable AC3: model 039 */
  193. #define CM_VIDWPDSB 0x00010000
  194. #define CM_SPDF_AC97 0x00008000 /* 0: SPDIF/OUT 44.1K, 1: 48K */
  195. #define CM_MASK_EN 0x00004000
  196. #define CM_VIDWPPRT 0x00002000
  197. #define CM_SFILENB 0x00001000
  198. #define CM_MMODE_MASK 0x00000E00
  199. #define CM_SPDIF_SELECT2 0x00000100 /* for model > 039 ? */
  200. #define CM_ENCENTER 0x00000080
  201. #define CM_FLINKON 0x00000040
  202. #define CM_FLINKOFF 0x00000020
  203. #define CM_MIDSMP 0x00000010
  204. #define CM_UPDDMA_MASK 0x0000000C
  205. #define CM_TWAIT_MASK 0x00000003
  206. /* byte */
  207. #define CM_REG_MIXER0 0x20
  208. #define CM_REG_SB16_DATA 0x22
  209. #define CM_REG_SB16_ADDR 0x23
  210. #define CM_REFFREQ_XIN (315*1000*1000)/22 /* 14.31818 Mhz reference clock frequency pin XIN */
  211. #define CM_ADCMULT_XIN 512 /* Guessed (487 best for 44.1kHz, not for 88/176kHz) */
  212. #define CM_TOLERANCE_RATE 0.001 /* Tolerance sample rate pitch (1000ppm) */
  213. #define CM_MAXIMUM_RATE 80000000 /* Note more than 80MHz */
  214. #define CM_REG_MIXER1 0x24
  215. #define CM_FMMUTE 0x80 /* mute FM */
  216. #define CM_FMMUTE_SHIFT 7
  217. #define CM_WSMUTE 0x40 /* mute PCM */
  218. #define CM_WSMUTE_SHIFT 6
  219. #define CM_SPK4 0x20 /* lin-in -> rear line out */
  220. #define CM_SPK4_SHIFT 5
  221. #define CM_REAR2FRONT 0x10 /* exchange rear/front */
  222. #define CM_REAR2FRONT_SHIFT 4
  223. #define CM_WAVEINL 0x08 /* digital wave rec. left chan */
  224. #define CM_WAVEINL_SHIFT 3
  225. #define CM_WAVEINR 0x04 /* digical wave rec. right */
  226. #define CM_WAVEINR_SHIFT 2
  227. #define CM_X3DEN 0x02 /* 3D surround enable */
  228. #define CM_X3DEN_SHIFT 1
  229. #define CM_CDPLAY 0x01 /* enable SPDIF/IN PCM -> DAC */
  230. #define CM_CDPLAY_SHIFT 0
  231. #define CM_REG_MIXER2 0x25
  232. #define CM_RAUXREN 0x80 /* AUX right capture */
  233. #define CM_RAUXREN_SHIFT 7
  234. #define CM_RAUXLEN 0x40 /* AUX left capture */
  235. #define CM_RAUXLEN_SHIFT 6
  236. #define CM_VAUXRM 0x20 /* AUX right mute */
  237. #define CM_VAUXRM_SHIFT 5
  238. #define CM_VAUXLM 0x10 /* AUX left mute */
  239. #define CM_VAUXLM_SHIFT 4
  240. #define CM_VADMIC_MASK 0x0e /* mic gain level (0-3) << 1 */
  241. #define CM_VADMIC_SHIFT 1
  242. #define CM_MICGAINZ 0x01 /* mic boost */
  243. #define CM_MICGAINZ_SHIFT 0
  244. #define CM_REG_AUX_VOL 0x26
  245. #define CM_VAUXL_MASK 0xf0
  246. #define CM_VAUXR_MASK 0x0f
  247. #define CM_REG_MISC 0x27
  248. #define CM_XGPO1 0x20
  249. // #define CM_XGPBIO 0x04
  250. #define CM_MIC_CENTER_LFE 0x04 /* mic as center/lfe out? (model 039 or later?) */
  251. #define CM_SPDIF_INVERSE 0x04 /* spdif input phase inverse (model 037) */
  252. #define CM_SPDVALID 0x02 /* spdif input valid check */
  253. #define CM_DMAUTO 0x01
  254. #define CM_REG_AC97 0x28 /* hmmm.. do we have ac97 link? */
  255. /*
  256. * For CMI-8338 (0x28 - 0x2b) .. is this valid for CMI-8738
  257. * or identical with AC97 codec?
  258. */
  259. #define CM_REG_EXTERN_CODEC CM_REG_AC97
  260. /*
  261. * MPU401 pci port index address 0x40 - 0x4f (CMI-8738 spec ver. 0.6)
  262. */
  263. #define CM_REG_MPU_PCI 0x40
  264. /*
  265. * FM pci port index address 0x50 - 0x5f (CMI-8738 spec ver. 0.6)
  266. */
  267. #define CM_REG_FM_PCI 0x50
  268. /*
  269. * access from SB-mixer port
  270. */
  271. #define CM_REG_EXTENT_IND 0xf0
  272. #define CM_VPHONE_MASK 0xe0 /* Phone volume control (0-3) << 5 */
  273. #define CM_VPHONE_SHIFT 5
  274. #define CM_VPHOM 0x10 /* Phone mute control */
  275. #define CM_VSPKM 0x08 /* Speaker mute control, default high */
  276. #define CM_RLOOPREN 0x04 /* Rec. R-channel enable */
  277. #define CM_RLOOPLEN 0x02 /* Rec. L-channel enable */
  278. #define CM_VADMIC3 0x01 /* Mic record boost */
  279. /*
  280. * CMI-8338 spec ver 0.5 (this is not valid for CMI-8738):
  281. * the 8 registers 0xf8 - 0xff are used for programming m/n counter by the PLL
  282. * unit (readonly?).
  283. */
  284. #define CM_REG_PLL 0xf8
  285. /*
  286. * extended registers
  287. */
  288. #define CM_REG_CH0_FRAME1 0x80 /* base address */
  289. #define CM_REG_CH0_FRAME2 0x84
  290. #define CM_REG_CH1_FRAME1 0x88 /* 0-15: count of samples at bus master; buffer size */
  291. #define CM_REG_CH1_FRAME2 0x8C /* 16-31: count of samples at codec; fragment size */
  292. #define CM_REG_MISC_CTRL_8768 0x92 /* reg. name the same as 0x18 */
  293. #define CM_CHB3D8C 0x20 /* 7.1 channels support */
  294. #define CM_SPD32FMT 0x10 /* SPDIF/IN 32k */
  295. #define CM_ADC2SPDIF 0x08 /* ADC output to SPDIF/OUT */
  296. #define CM_SHAREADC 0x04 /* DAC in ADC as Center/LFE */
  297. #define CM_REALTCMP 0x02 /* monitor the CMPL/CMPR of ADC */
  298. #define CM_INVLRCK 0x01 /* invert ZVPORT's LRCK */
  299. /*
  300. * size of i/o region
  301. */
  302. #define CM_EXTENT_CODEC 0x100
  303. #define CM_EXTENT_MIDI 0x2
  304. #define CM_EXTENT_SYNTH 0x4
  305. /*
  306. * channels for playback / capture
  307. */
  308. #define CM_CH_PLAY 0
  309. #define CM_CH_CAPT 1
  310. /*
  311. * flags to check device open/close
  312. */
  313. #define CM_OPEN_NONE 0
  314. #define CM_OPEN_CH_MASK 0x01
  315. #define CM_OPEN_DAC 0x10
  316. #define CM_OPEN_ADC 0x20
  317. #define CM_OPEN_SPDIF 0x40
  318. #define CM_OPEN_MCHAN 0x80
  319. #define CM_OPEN_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC)
  320. #define CM_OPEN_PLAYBACK2 (CM_CH_CAPT | CM_OPEN_DAC)
  321. #define CM_OPEN_PLAYBACK_MULTI (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_MCHAN)
  322. #define CM_OPEN_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC)
  323. #define CM_OPEN_SPDIF_PLAYBACK (CM_CH_PLAY | CM_OPEN_DAC | CM_OPEN_SPDIF)
  324. #define CM_OPEN_SPDIF_CAPTURE (CM_CH_CAPT | CM_OPEN_ADC | CM_OPEN_SPDIF)
  325. #if CM_CH_PLAY == 1
  326. #define CM_PLAYBACK_SRATE_176K CM_CH1_SRATE_176K
  327. #define CM_PLAYBACK_SPDF CM_SPDF_1
  328. #define CM_CAPTURE_SPDF CM_SPDF_0
  329. #else
  330. #define CM_PLAYBACK_SRATE_176K CM_CH0_SRATE_176K
  331. #define CM_PLAYBACK_SPDF CM_SPDF_0
  332. #define CM_CAPTURE_SPDF CM_SPDF_1
  333. #endif
  334. /*
  335. * driver data
  336. */
  337. typedef struct snd_stru_cmipci cmipci_t;
  338. typedef struct snd_stru_cmipci_pcm cmipci_pcm_t;
  339. struct snd_stru_cmipci_pcm {
  340. snd_pcm_substream_t *substream;
  341. int running; /* dac/adc running? */
  342. unsigned int dma_size; /* in frames */
  343. unsigned int period_size; /* in frames */
  344. unsigned int offset; /* physical address of the buffer */
  345. unsigned int fmt; /* format bits */
  346. int ch; /* channel (0/1) */
  347. unsigned int is_dac; /* is dac? */
  348. int bytes_per_frame;
  349. int shift;
  350. };
  351. /* mixer elements toggled/resumed during ac3 playback */
  352. struct cmipci_mixer_auto_switches {
  353. const char *name; /* switch to toggle */
  354. int toggle_on; /* value to change when ac3 mode */
  355. };
  356. static const struct cmipci_mixer_auto_switches cm_saved_mixer[] = {
  357. {"PCM Playback Switch", 0},
  358. {"IEC958 Output Switch", 1},
  359. {"IEC958 Mix Analog", 0},
  360. // {"IEC958 Out To DAC", 1}, // no longer used
  361. {"IEC958 Loop", 0},
  362. };
  363. #define CM_SAVED_MIXERS ARRAY_SIZE(cm_saved_mixer)
  364. struct snd_stru_cmipci {
  365. snd_card_t *card;
  366. struct pci_dev *pci;
  367. unsigned int device; /* device ID */
  368. int irq;
  369. unsigned long iobase;
  370. unsigned int ctrl; /* FUNCTRL0 current value */
  371. snd_pcm_t *pcm; /* DAC/ADC PCM */
  372. snd_pcm_t *pcm2; /* 2nd DAC */
  373. snd_pcm_t *pcm_spdif; /* SPDIF */
  374. int chip_version;
  375. int max_channels;
  376. unsigned int has_dual_dac: 1;
  377. unsigned int can_ac3_sw: 1;
  378. unsigned int can_ac3_hw: 1;
  379. unsigned int can_multi_ch: 1;
  380. unsigned int do_soft_ac3: 1;
  381. unsigned int spdif_playback_avail: 1; /* spdif ready? */
  382. unsigned int spdif_playback_enabled: 1; /* spdif switch enabled? */
  383. int spdif_counter; /* for software AC3 */
  384. unsigned int dig_status;
  385. unsigned int dig_pcm_status;
  386. snd_pcm_hardware_t *hw_info[3]; /* for playbacks */
  387. int opened[2]; /* open mode */
  388. struct semaphore open_mutex;
  389. unsigned int mixer_insensitive: 1;
  390. snd_kcontrol_t *mixer_res_ctl[CM_SAVED_MIXERS];
  391. int mixer_res_status[CM_SAVED_MIXERS];
  392. cmipci_pcm_t channel[2]; /* ch0 - DAC, ch1 - ADC or 2nd DAC */
  393. /* external MIDI */
  394. snd_rawmidi_t *rmidi;
  395. #ifdef SUPPORT_JOYSTICK
  396. struct gameport *gameport;
  397. #endif
  398. spinlock_t reg_lock;
  399. };
  400. /* read/write operations for dword register */
  401. static inline void snd_cmipci_write(cmipci_t *cm, unsigned int cmd, unsigned int data)
  402. {
  403. outl(data, cm->iobase + cmd);
  404. }
  405. static inline unsigned int snd_cmipci_read(cmipci_t *cm, unsigned int cmd)
  406. {
  407. return inl(cm->iobase + cmd);
  408. }
  409. /* read/write operations for word register */
  410. static inline void snd_cmipci_write_w(cmipci_t *cm, unsigned int cmd, unsigned short data)
  411. {
  412. outw(data, cm->iobase + cmd);
  413. }
  414. static inline unsigned short snd_cmipci_read_w(cmipci_t *cm, unsigned int cmd)
  415. {
  416. return inw(cm->iobase + cmd);
  417. }
  418. /* read/write operations for byte register */
  419. static inline void snd_cmipci_write_b(cmipci_t *cm, unsigned int cmd, unsigned char data)
  420. {
  421. outb(data, cm->iobase + cmd);
  422. }
  423. static inline unsigned char snd_cmipci_read_b(cmipci_t *cm, unsigned int cmd)
  424. {
  425. return inb(cm->iobase + cmd);
  426. }
  427. /* bit operations for dword register */
  428. static int snd_cmipci_set_bit(cmipci_t *cm, unsigned int cmd, unsigned int flag)
  429. {
  430. unsigned int val, oval;
  431. val = oval = inl(cm->iobase + cmd);
  432. val |= flag;
  433. if (val == oval)
  434. return 0;
  435. outl(val, cm->iobase + cmd);
  436. return 1;
  437. }
  438. static int snd_cmipci_clear_bit(cmipci_t *cm, unsigned int cmd, unsigned int flag)
  439. {
  440. unsigned int val, oval;
  441. val = oval = inl(cm->iobase + cmd);
  442. val &= ~flag;
  443. if (val == oval)
  444. return 0;
  445. outl(val, cm->iobase + cmd);
  446. return 1;
  447. }
  448. /* bit operations for byte register */
  449. static int snd_cmipci_set_bit_b(cmipci_t *cm, unsigned int cmd, unsigned char flag)
  450. {
  451. unsigned char val, oval;
  452. val = oval = inb(cm->iobase + cmd);
  453. val |= flag;
  454. if (val == oval)
  455. return 0;
  456. outb(val, cm->iobase + cmd);
  457. return 1;
  458. }
  459. static int snd_cmipci_clear_bit_b(cmipci_t *cm, unsigned int cmd, unsigned char flag)
  460. {
  461. unsigned char val, oval;
  462. val = oval = inb(cm->iobase + cmd);
  463. val &= ~flag;
  464. if (val == oval)
  465. return 0;
  466. outb(val, cm->iobase + cmd);
  467. return 1;
  468. }
  469. /*
  470. * PCM interface
  471. */
  472. /*
  473. * calculate frequency
  474. */
  475. static unsigned int rates[] = { 5512, 11025, 22050, 44100, 8000, 16000, 32000, 48000 };
  476. static unsigned int snd_cmipci_rate_freq(unsigned int rate)
  477. {
  478. unsigned int i;
  479. for (i = 0; i < ARRAY_SIZE(rates); i++) {
  480. if (rates[i] == rate)
  481. return i;
  482. }
  483. snd_BUG();
  484. return 0;
  485. }
  486. #ifdef USE_VAR48KRATE
  487. /*
  488. * Determine PLL values for frequency setup, maybe the CMI8338 (CMI8738???)
  489. * does it this way .. maybe not. Never get any information from C-Media about
  490. * that <werner@suse.de>.
  491. */
  492. static int snd_cmipci_pll_rmn(unsigned int rate, unsigned int adcmult, int *r, int *m, int *n)
  493. {
  494. unsigned int delta, tolerance;
  495. int xm, xn, xr;
  496. for (*r = 0; rate < CM_MAXIMUM_RATE/adcmult; *r += (1<<5))
  497. rate <<= 1;
  498. *n = -1;
  499. if (*r > 0xff)
  500. goto out;
  501. tolerance = rate*CM_TOLERANCE_RATE;
  502. for (xn = (1+2); xn < (0x1f+2); xn++) {
  503. for (xm = (1+2); xm < (0xff+2); xm++) {
  504. xr = ((CM_REFFREQ_XIN/adcmult) * xm) / xn;
  505. if (xr < rate)
  506. delta = rate - xr;
  507. else
  508. delta = xr - rate;
  509. /*
  510. * If we found one, remember this,
  511. * and try to find a closer one
  512. */
  513. if (delta < tolerance) {
  514. tolerance = delta;
  515. *m = xm - 2;
  516. *n = xn - 2;
  517. }
  518. }
  519. }
  520. out:
  521. return (*n > -1);
  522. }
  523. /*
  524. * Program pll register bits, I assume that the 8 registers 0xf8 upto 0xff
  525. * are mapped onto the 8 ADC/DAC sampling frequency which can be choosen
  526. * at the register CM_REG_FUNCTRL1 (0x04).
  527. * Problem: other ways are also possible (any information about that?)
  528. */
  529. static void snd_cmipci_set_pll(cmipci_t *cm, unsigned int rate, unsigned int slot)
  530. {
  531. unsigned int reg = CM_REG_PLL + slot;
  532. /*
  533. * Guess that this programs at reg. 0x04 the pos 15:13/12:10
  534. * for DSFC/ASFC (000 upto 111).
  535. */
  536. /* FIXME: Init (Do we've to set an other register first before programming?) */
  537. /* FIXME: Is this correct? Or shouldn't the m/n/r values be used for that? */
  538. snd_cmipci_write_b(cm, reg, rate>>8);
  539. snd_cmipci_write_b(cm, reg, rate&0xff);
  540. /* FIXME: Setup (Do we've to set an other register first to enable this?) */
  541. }
  542. #endif /* USE_VAR48KRATE */
  543. static int snd_cmipci_hw_params(snd_pcm_substream_t * substream,
  544. snd_pcm_hw_params_t * hw_params)
  545. {
  546. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  547. }
  548. static int snd_cmipci_playback2_hw_params(snd_pcm_substream_t * substream,
  549. snd_pcm_hw_params_t * hw_params)
  550. {
  551. cmipci_t *cm = snd_pcm_substream_chip(substream);
  552. if (params_channels(hw_params) > 2) {
  553. down(&cm->open_mutex);
  554. if (cm->opened[CM_CH_PLAY]) {
  555. up(&cm->open_mutex);
  556. return -EBUSY;
  557. }
  558. /* reserve the channel A */
  559. cm->opened[CM_CH_PLAY] = CM_OPEN_PLAYBACK_MULTI;
  560. up(&cm->open_mutex);
  561. }
  562. return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
  563. }
  564. static void snd_cmipci_ch_reset(cmipci_t *cm, int ch)
  565. {
  566. int reset = CM_RST_CH0 << (cm->channel[ch].ch);
  567. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  568. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  569. udelay(10);
  570. }
  571. static int snd_cmipci_hw_free(snd_pcm_substream_t * substream)
  572. {
  573. return snd_pcm_lib_free_pages(substream);
  574. }
  575. /*
  576. */
  577. static unsigned int hw_channels[] = {1, 2, 4, 5, 6, 8};
  578. static snd_pcm_hw_constraint_list_t hw_constraints_channels_4 = {
  579. .count = 3,
  580. .list = hw_channels,
  581. .mask = 0,
  582. };
  583. static snd_pcm_hw_constraint_list_t hw_constraints_channels_6 = {
  584. .count = 5,
  585. .list = hw_channels,
  586. .mask = 0,
  587. };
  588. static snd_pcm_hw_constraint_list_t hw_constraints_channels_8 = {
  589. .count = 6,
  590. .list = hw_channels,
  591. .mask = 0,
  592. };
  593. static int set_dac_channels(cmipci_t *cm, cmipci_pcm_t *rec, int channels)
  594. {
  595. if (channels > 2) {
  596. if (! cm->can_multi_ch)
  597. return -EINVAL;
  598. if (rec->fmt != 0x03) /* stereo 16bit only */
  599. return -EINVAL;
  600. spin_lock_irq(&cm->reg_lock);
  601. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  602. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  603. if (channels > 4) {
  604. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  605. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  606. } else {
  607. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  608. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  609. }
  610. if (channels >= 6) {
  611. snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  612. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  613. } else {
  614. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  615. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  616. }
  617. if (cm->chip_version == 68) {
  618. if (channels == 8) {
  619. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL_8768, CM_CHB3D8C);
  620. } else {
  621. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL_8768, CM_CHB3D8C);
  622. }
  623. }
  624. spin_unlock_irq(&cm->reg_lock);
  625. } else {
  626. if (cm->can_multi_ch) {
  627. spin_lock_irq(&cm->reg_lock);
  628. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_NXCHG);
  629. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D);
  630. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_CHB3D5C);
  631. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_CHB3D6C);
  632. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENCENTER);
  633. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  634. spin_unlock_irq(&cm->reg_lock);
  635. }
  636. }
  637. return 0;
  638. }
  639. /*
  640. * prepare playback/capture channel
  641. * channel to be used must have been set in rec->ch.
  642. */
  643. static int snd_cmipci_pcm_prepare(cmipci_t *cm, cmipci_pcm_t *rec,
  644. snd_pcm_substream_t *substream)
  645. {
  646. unsigned int reg, freq, val;
  647. snd_pcm_runtime_t *runtime = substream->runtime;
  648. rec->fmt = 0;
  649. rec->shift = 0;
  650. if (snd_pcm_format_width(runtime->format) >= 16) {
  651. rec->fmt |= 0x02;
  652. if (snd_pcm_format_width(runtime->format) > 16)
  653. rec->shift++; /* 24/32bit */
  654. }
  655. if (runtime->channels > 1)
  656. rec->fmt |= 0x01;
  657. if (rec->is_dac && set_dac_channels(cm, rec, runtime->channels) < 0) {
  658. snd_printd("cannot set dac channels\n");
  659. return -EINVAL;
  660. }
  661. rec->offset = runtime->dma_addr;
  662. /* buffer and period sizes in frame */
  663. rec->dma_size = runtime->buffer_size << rec->shift;
  664. rec->period_size = runtime->period_size << rec->shift;
  665. if (runtime->channels > 2) {
  666. /* multi-channels */
  667. rec->dma_size = (rec->dma_size * runtime->channels) / 2;
  668. rec->period_size = (rec->period_size * runtime->channels) / 2;
  669. }
  670. spin_lock_irq(&cm->reg_lock);
  671. /* set buffer address */
  672. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  673. snd_cmipci_write(cm, reg, rec->offset);
  674. /* program sample counts */
  675. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  676. snd_cmipci_write_w(cm, reg, rec->dma_size - 1);
  677. snd_cmipci_write_w(cm, reg + 2, rec->period_size - 1);
  678. /* set adc/dac flag */
  679. val = rec->ch ? CM_CHADC1 : CM_CHADC0;
  680. if (rec->is_dac)
  681. cm->ctrl &= ~val;
  682. else
  683. cm->ctrl |= val;
  684. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  685. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  686. /* set sample rate */
  687. freq = snd_cmipci_rate_freq(runtime->rate);
  688. val = snd_cmipci_read(cm, CM_REG_FUNCTRL1);
  689. if (rec->ch) {
  690. val &= ~CM_ASFC_MASK;
  691. val |= (freq << CM_ASFC_SHIFT) & CM_ASFC_MASK;
  692. } else {
  693. val &= ~CM_DSFC_MASK;
  694. val |= (freq << CM_DSFC_SHIFT) & CM_DSFC_MASK;
  695. }
  696. snd_cmipci_write(cm, CM_REG_FUNCTRL1, val);
  697. //snd_printd("cmipci: functrl1 = %08x\n", val);
  698. /* set format */
  699. val = snd_cmipci_read(cm, CM_REG_CHFORMAT);
  700. if (rec->ch) {
  701. val &= ~CM_CH1FMT_MASK;
  702. val |= rec->fmt << CM_CH1FMT_SHIFT;
  703. } else {
  704. val &= ~CM_CH0FMT_MASK;
  705. val |= rec->fmt << CM_CH0FMT_SHIFT;
  706. }
  707. snd_cmipci_write(cm, CM_REG_CHFORMAT, val);
  708. //snd_printd("cmipci: chformat = %08x\n", val);
  709. rec->running = 0;
  710. spin_unlock_irq(&cm->reg_lock);
  711. return 0;
  712. }
  713. /*
  714. * PCM trigger/stop
  715. */
  716. static int snd_cmipci_pcm_trigger(cmipci_t *cm, cmipci_pcm_t *rec,
  717. snd_pcm_substream_t *substream, int cmd)
  718. {
  719. unsigned int inthld, chen, reset, pause;
  720. int result = 0;
  721. inthld = CM_CH0_INT_EN << rec->ch;
  722. chen = CM_CHEN0 << rec->ch;
  723. reset = CM_RST_CH0 << rec->ch;
  724. pause = CM_PAUSE0 << rec->ch;
  725. spin_lock(&cm->reg_lock);
  726. switch (cmd) {
  727. case SNDRV_PCM_TRIGGER_START:
  728. rec->running = 1;
  729. /* set interrupt */
  730. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, inthld);
  731. cm->ctrl |= chen;
  732. /* enable channel */
  733. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  734. //snd_printd("cmipci: functrl0 = %08x\n", cm->ctrl);
  735. break;
  736. case SNDRV_PCM_TRIGGER_STOP:
  737. rec->running = 0;
  738. /* disable interrupt */
  739. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, inthld);
  740. /* reset */
  741. cm->ctrl &= ~chen;
  742. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl | reset);
  743. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl & ~reset);
  744. break;
  745. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  746. cm->ctrl |= pause;
  747. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  748. break;
  749. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  750. cm->ctrl &= ~pause;
  751. snd_cmipci_write(cm, CM_REG_FUNCTRL0, cm->ctrl);
  752. break;
  753. default:
  754. result = -EINVAL;
  755. break;
  756. }
  757. spin_unlock(&cm->reg_lock);
  758. return result;
  759. }
  760. /*
  761. * return the current pointer
  762. */
  763. static snd_pcm_uframes_t snd_cmipci_pcm_pointer(cmipci_t *cm, cmipci_pcm_t *rec,
  764. snd_pcm_substream_t *substream)
  765. {
  766. size_t ptr;
  767. unsigned int reg;
  768. if (!rec->running)
  769. return 0;
  770. #if 1 // this seems better..
  771. reg = rec->ch ? CM_REG_CH1_FRAME2 : CM_REG_CH0_FRAME2;
  772. ptr = rec->dma_size - (snd_cmipci_read_w(cm, reg) + 1);
  773. ptr >>= rec->shift;
  774. #else
  775. reg = rec->ch ? CM_REG_CH1_FRAME1 : CM_REG_CH0_FRAME1;
  776. ptr = snd_cmipci_read(cm, reg) - rec->offset;
  777. ptr = bytes_to_frames(substream->runtime, ptr);
  778. #endif
  779. if (substream->runtime->channels > 2)
  780. ptr = (ptr * 2) / substream->runtime->channels;
  781. return ptr;
  782. }
  783. /*
  784. * playback
  785. */
  786. static int snd_cmipci_playback_trigger(snd_pcm_substream_t *substream,
  787. int cmd)
  788. {
  789. cmipci_t *cm = snd_pcm_substream_chip(substream);
  790. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_PLAY], substream, cmd);
  791. }
  792. static snd_pcm_uframes_t snd_cmipci_playback_pointer(snd_pcm_substream_t *substream)
  793. {
  794. cmipci_t *cm = snd_pcm_substream_chip(substream);
  795. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_PLAY], substream);
  796. }
  797. /*
  798. * capture
  799. */
  800. static int snd_cmipci_capture_trigger(snd_pcm_substream_t *substream,
  801. int cmd)
  802. {
  803. cmipci_t *cm = snd_pcm_substream_chip(substream);
  804. return snd_cmipci_pcm_trigger(cm, &cm->channel[CM_CH_CAPT], substream, cmd);
  805. }
  806. static snd_pcm_uframes_t snd_cmipci_capture_pointer(snd_pcm_substream_t *substream)
  807. {
  808. cmipci_t *cm = snd_pcm_substream_chip(substream);
  809. return snd_cmipci_pcm_pointer(cm, &cm->channel[CM_CH_CAPT], substream);
  810. }
  811. /*
  812. * hw preparation for spdif
  813. */
  814. static int snd_cmipci_spdif_default_info(snd_kcontrol_t *kcontrol,
  815. snd_ctl_elem_info_t *uinfo)
  816. {
  817. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  818. uinfo->count = 1;
  819. return 0;
  820. }
  821. static int snd_cmipci_spdif_default_get(snd_kcontrol_t *kcontrol,
  822. snd_ctl_elem_value_t *ucontrol)
  823. {
  824. cmipci_t *chip = snd_kcontrol_chip(kcontrol);
  825. int i;
  826. spin_lock_irq(&chip->reg_lock);
  827. for (i = 0; i < 4; i++)
  828. ucontrol->value.iec958.status[i] = (chip->dig_status >> (i * 8)) & 0xff;
  829. spin_unlock_irq(&chip->reg_lock);
  830. return 0;
  831. }
  832. static int snd_cmipci_spdif_default_put(snd_kcontrol_t * kcontrol,
  833. snd_ctl_elem_value_t * ucontrol)
  834. {
  835. cmipci_t *chip = snd_kcontrol_chip(kcontrol);
  836. int i, change;
  837. unsigned int val;
  838. val = 0;
  839. spin_lock_irq(&chip->reg_lock);
  840. for (i = 0; i < 4; i++)
  841. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  842. change = val != chip->dig_status;
  843. chip->dig_status = val;
  844. spin_unlock_irq(&chip->reg_lock);
  845. return change;
  846. }
  847. static snd_kcontrol_new_t snd_cmipci_spdif_default __devinitdata =
  848. {
  849. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  850. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  851. .info = snd_cmipci_spdif_default_info,
  852. .get = snd_cmipci_spdif_default_get,
  853. .put = snd_cmipci_spdif_default_put
  854. };
  855. static int snd_cmipci_spdif_mask_info(snd_kcontrol_t *kcontrol,
  856. snd_ctl_elem_info_t *uinfo)
  857. {
  858. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  859. uinfo->count = 1;
  860. return 0;
  861. }
  862. static int snd_cmipci_spdif_mask_get(snd_kcontrol_t * kcontrol,
  863. snd_ctl_elem_value_t *ucontrol)
  864. {
  865. ucontrol->value.iec958.status[0] = 0xff;
  866. ucontrol->value.iec958.status[1] = 0xff;
  867. ucontrol->value.iec958.status[2] = 0xff;
  868. ucontrol->value.iec958.status[3] = 0xff;
  869. return 0;
  870. }
  871. static snd_kcontrol_new_t snd_cmipci_spdif_mask __devinitdata =
  872. {
  873. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  874. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  875. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
  876. .info = snd_cmipci_spdif_mask_info,
  877. .get = snd_cmipci_spdif_mask_get,
  878. };
  879. static int snd_cmipci_spdif_stream_info(snd_kcontrol_t *kcontrol,
  880. snd_ctl_elem_info_t *uinfo)
  881. {
  882. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  883. uinfo->count = 1;
  884. return 0;
  885. }
  886. static int snd_cmipci_spdif_stream_get(snd_kcontrol_t *kcontrol,
  887. snd_ctl_elem_value_t *ucontrol)
  888. {
  889. cmipci_t *chip = snd_kcontrol_chip(kcontrol);
  890. int i;
  891. spin_lock_irq(&chip->reg_lock);
  892. for (i = 0; i < 4; i++)
  893. ucontrol->value.iec958.status[i] = (chip->dig_pcm_status >> (i * 8)) & 0xff;
  894. spin_unlock_irq(&chip->reg_lock);
  895. return 0;
  896. }
  897. static int snd_cmipci_spdif_stream_put(snd_kcontrol_t *kcontrol,
  898. snd_ctl_elem_value_t *ucontrol)
  899. {
  900. cmipci_t *chip = snd_kcontrol_chip(kcontrol);
  901. int i, change;
  902. unsigned int val;
  903. val = 0;
  904. spin_lock_irq(&chip->reg_lock);
  905. for (i = 0; i < 4; i++)
  906. val |= (unsigned int)ucontrol->value.iec958.status[i] << (i * 8);
  907. change = val != chip->dig_pcm_status;
  908. chip->dig_pcm_status = val;
  909. spin_unlock_irq(&chip->reg_lock);
  910. return change;
  911. }
  912. static snd_kcontrol_new_t snd_cmipci_spdif_stream __devinitdata =
  913. {
  914. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  915. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  916. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  917. .info = snd_cmipci_spdif_stream_info,
  918. .get = snd_cmipci_spdif_stream_get,
  919. .put = snd_cmipci_spdif_stream_put
  920. };
  921. /*
  922. */
  923. /* save mixer setting and mute for AC3 playback */
  924. static int save_mixer_state(cmipci_t *cm)
  925. {
  926. if (! cm->mixer_insensitive) {
  927. snd_ctl_elem_value_t *val;
  928. unsigned int i;
  929. val = kmalloc(sizeof(*val), GFP_ATOMIC);
  930. if (!val)
  931. return -ENOMEM;
  932. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  933. snd_kcontrol_t *ctl = cm->mixer_res_ctl[i];
  934. if (ctl) {
  935. int event;
  936. memset(val, 0, sizeof(*val));
  937. ctl->get(ctl, val);
  938. cm->mixer_res_status[i] = val->value.integer.value[0];
  939. val->value.integer.value[0] = cm_saved_mixer[i].toggle_on;
  940. event = SNDRV_CTL_EVENT_MASK_INFO;
  941. if (cm->mixer_res_status[i] != val->value.integer.value[0]) {
  942. ctl->put(ctl, val); /* toggle */
  943. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  944. }
  945. ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  946. snd_ctl_notify(cm->card, event, &ctl->id);
  947. }
  948. }
  949. kfree(val);
  950. cm->mixer_insensitive = 1;
  951. }
  952. return 0;
  953. }
  954. /* restore the previously saved mixer status */
  955. static void restore_mixer_state(cmipci_t *cm)
  956. {
  957. if (cm->mixer_insensitive) {
  958. snd_ctl_elem_value_t *val;
  959. unsigned int i;
  960. val = kmalloc(sizeof(*val), GFP_KERNEL);
  961. if (!val)
  962. return;
  963. cm->mixer_insensitive = 0; /* at first clear this;
  964. otherwise the changes will be ignored */
  965. for (i = 0; i < CM_SAVED_MIXERS; i++) {
  966. snd_kcontrol_t *ctl = cm->mixer_res_ctl[i];
  967. if (ctl) {
  968. int event;
  969. memset(val, 0, sizeof(*val));
  970. ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  971. ctl->get(ctl, val);
  972. event = SNDRV_CTL_EVENT_MASK_INFO;
  973. if (val->value.integer.value[0] != cm->mixer_res_status[i]) {
  974. val->value.integer.value[0] = cm->mixer_res_status[i];
  975. ctl->put(ctl, val);
  976. event |= SNDRV_CTL_EVENT_MASK_VALUE;
  977. }
  978. snd_ctl_notify(cm->card, event, &ctl->id);
  979. }
  980. }
  981. kfree(val);
  982. }
  983. }
  984. /* spinlock held! */
  985. static void setup_ac3(cmipci_t *cm, snd_pcm_substream_t *subs, int do_ac3, int rate)
  986. {
  987. if (do_ac3) {
  988. /* AC3EN for 037 */
  989. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  990. /* AC3EN for 039 */
  991. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  992. if (cm->can_ac3_hw) {
  993. /* SPD24SEL for 037, 0x02 */
  994. /* SPD24SEL for 039, 0x20, but cannot be set */
  995. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  996. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  997. } else { /* can_ac3_sw */
  998. /* SPD32SEL for 037 & 039, 0x20 */
  999. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1000. /* set 176K sample rate to fix 033 HW bug */
  1001. if (cm->chip_version == 33) {
  1002. if (rate >= 48000) {
  1003. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1004. } else {
  1005. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1006. }
  1007. }
  1008. }
  1009. } else {
  1010. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_AC3EN1);
  1011. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_AC3EN2);
  1012. if (cm->can_ac3_hw) {
  1013. /* chip model >= 37 */
  1014. if (snd_pcm_format_width(subs->runtime->format) > 16) {
  1015. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1016. snd_cmipci_set_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1017. } else {
  1018. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1019. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1020. }
  1021. } else {
  1022. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPD32SEL);
  1023. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_SPD24SEL);
  1024. snd_cmipci_clear_bit(cm, CM_REG_CHFORMAT, CM_PLAYBACK_SRATE_176K);
  1025. }
  1026. }
  1027. }
  1028. static int setup_spdif_playback(cmipci_t *cm, snd_pcm_substream_t *subs, int up, int do_ac3)
  1029. {
  1030. int rate, err;
  1031. rate = subs->runtime->rate;
  1032. if (up && do_ac3)
  1033. if ((err = save_mixer_state(cm)) < 0)
  1034. return err;
  1035. spin_lock_irq(&cm->reg_lock);
  1036. cm->spdif_playback_avail = up;
  1037. if (up) {
  1038. /* they are controlled via "IEC958 Output Switch" */
  1039. /* snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1040. /* snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1041. if (cm->spdif_playback_enabled)
  1042. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1043. setup_ac3(cm, subs, do_ac3, rate);
  1044. if (rate == 48000)
  1045. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1046. else
  1047. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K | CM_SPDF_AC97);
  1048. } else {
  1049. /* they are controlled via "IEC958 Output Switch" */
  1050. /* snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT); */
  1051. /* snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_SPDO2DAC); */
  1052. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1053. setup_ac3(cm, subs, 0, 0);
  1054. }
  1055. spin_unlock_irq(&cm->reg_lock);
  1056. return 0;
  1057. }
  1058. /*
  1059. * preparation
  1060. */
  1061. /* playback - enable spdif only on the certain condition */
  1062. static int snd_cmipci_playback_prepare(snd_pcm_substream_t *substream)
  1063. {
  1064. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1065. int rate = substream->runtime->rate;
  1066. int err, do_spdif, do_ac3 = 0;
  1067. do_spdif = ((rate == 44100 || rate == 48000) &&
  1068. substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE &&
  1069. substream->runtime->channels == 2);
  1070. if (do_spdif && cm->can_ac3_hw)
  1071. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1072. if ((err = setup_spdif_playback(cm, substream, do_spdif, do_ac3)) < 0)
  1073. return err;
  1074. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1075. }
  1076. /* playback (via device #2) - enable spdif always */
  1077. static int snd_cmipci_playback_spdif_prepare(snd_pcm_substream_t *substream)
  1078. {
  1079. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1080. int err, do_ac3;
  1081. if (cm->can_ac3_hw)
  1082. do_ac3 = cm->dig_pcm_status & IEC958_AES0_NONAUDIO;
  1083. else
  1084. do_ac3 = 1; /* doesn't matter */
  1085. if ((err = setup_spdif_playback(cm, substream, 1, do_ac3)) < 0)
  1086. return err;
  1087. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_PLAY], substream);
  1088. }
  1089. static int snd_cmipci_playback_hw_free(snd_pcm_substream_t *substream)
  1090. {
  1091. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1092. setup_spdif_playback(cm, substream, 0, 0);
  1093. restore_mixer_state(cm);
  1094. return snd_cmipci_hw_free(substream);
  1095. }
  1096. /* capture */
  1097. static int snd_cmipci_capture_prepare(snd_pcm_substream_t *substream)
  1098. {
  1099. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1100. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1101. }
  1102. /* capture with spdif (via device #2) */
  1103. static int snd_cmipci_capture_spdif_prepare(snd_pcm_substream_t *substream)
  1104. {
  1105. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1106. spin_lock_irq(&cm->reg_lock);
  1107. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1108. spin_unlock_irq(&cm->reg_lock);
  1109. return snd_cmipci_pcm_prepare(cm, &cm->channel[CM_CH_CAPT], substream);
  1110. }
  1111. static int snd_cmipci_capture_spdif_hw_free(snd_pcm_substream_t *subs)
  1112. {
  1113. cmipci_t *cm = snd_pcm_substream_chip(subs);
  1114. spin_lock_irq(&cm->reg_lock);
  1115. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_CAPTURE_SPDF);
  1116. spin_unlock_irq(&cm->reg_lock);
  1117. return snd_cmipci_hw_free(subs);
  1118. }
  1119. /*
  1120. * interrupt handler
  1121. */
  1122. static irqreturn_t snd_cmipci_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1123. {
  1124. cmipci_t *cm = dev_id;
  1125. unsigned int status, mask = 0;
  1126. /* fastpath out, to ease interrupt sharing */
  1127. status = snd_cmipci_read(cm, CM_REG_INT_STATUS);
  1128. if (!(status & CM_INTR))
  1129. return IRQ_NONE;
  1130. /* acknowledge interrupt */
  1131. spin_lock(&cm->reg_lock);
  1132. if (status & CM_CHINT0)
  1133. mask |= CM_CH0_INT_EN;
  1134. if (status & CM_CHINT1)
  1135. mask |= CM_CH1_INT_EN;
  1136. snd_cmipci_clear_bit(cm, CM_REG_INT_HLDCLR, mask);
  1137. snd_cmipci_set_bit(cm, CM_REG_INT_HLDCLR, mask);
  1138. spin_unlock(&cm->reg_lock);
  1139. if (cm->rmidi && (status & CM_UARTINT))
  1140. snd_mpu401_uart_interrupt(irq, cm->rmidi->private_data, regs);
  1141. if (cm->pcm) {
  1142. if ((status & CM_CHINT0) && cm->channel[0].running)
  1143. snd_pcm_period_elapsed(cm->channel[0].substream);
  1144. if ((status & CM_CHINT1) && cm->channel[1].running)
  1145. snd_pcm_period_elapsed(cm->channel[1].substream);
  1146. }
  1147. return IRQ_HANDLED;
  1148. }
  1149. /*
  1150. * h/w infos
  1151. */
  1152. /* playback on channel A */
  1153. static snd_pcm_hardware_t snd_cmipci_playback =
  1154. {
  1155. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1156. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1157. SNDRV_PCM_INFO_MMAP_VALID),
  1158. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1159. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1160. .rate_min = 5512,
  1161. .rate_max = 48000,
  1162. .channels_min = 1,
  1163. .channels_max = 2,
  1164. .buffer_bytes_max = (128*1024),
  1165. .period_bytes_min = 64,
  1166. .period_bytes_max = (128*1024),
  1167. .periods_min = 2,
  1168. .periods_max = 1024,
  1169. .fifo_size = 0,
  1170. };
  1171. /* capture on channel B */
  1172. static snd_pcm_hardware_t snd_cmipci_capture =
  1173. {
  1174. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1175. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1176. SNDRV_PCM_INFO_MMAP_VALID),
  1177. .formats = SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
  1178. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1179. .rate_min = 5512,
  1180. .rate_max = 48000,
  1181. .channels_min = 1,
  1182. .channels_max = 2,
  1183. .buffer_bytes_max = (128*1024),
  1184. .period_bytes_min = 64,
  1185. .period_bytes_max = (128*1024),
  1186. .periods_min = 2,
  1187. .periods_max = 1024,
  1188. .fifo_size = 0,
  1189. };
  1190. /* playback on channel B - stereo 16bit only? */
  1191. static snd_pcm_hardware_t snd_cmipci_playback2 =
  1192. {
  1193. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1194. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1195. SNDRV_PCM_INFO_MMAP_VALID),
  1196. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1197. .rates = SNDRV_PCM_RATE_5512 | SNDRV_PCM_RATE_8000_48000,
  1198. .rate_min = 5512,
  1199. .rate_max = 48000,
  1200. .channels_min = 2,
  1201. .channels_max = 2,
  1202. .buffer_bytes_max = (128*1024),
  1203. .period_bytes_min = 64,
  1204. .period_bytes_max = (128*1024),
  1205. .periods_min = 2,
  1206. .periods_max = 1024,
  1207. .fifo_size = 0,
  1208. };
  1209. /* spdif playback on channel A */
  1210. static snd_pcm_hardware_t snd_cmipci_playback_spdif =
  1211. {
  1212. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1213. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1214. SNDRV_PCM_INFO_MMAP_VALID),
  1215. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1216. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1217. .rate_min = 44100,
  1218. .rate_max = 48000,
  1219. .channels_min = 2,
  1220. .channels_max = 2,
  1221. .buffer_bytes_max = (128*1024),
  1222. .period_bytes_min = 64,
  1223. .period_bytes_max = (128*1024),
  1224. .periods_min = 2,
  1225. .periods_max = 1024,
  1226. .fifo_size = 0,
  1227. };
  1228. /* spdif playback on channel A (32bit, IEC958 subframes) */
  1229. static snd_pcm_hardware_t snd_cmipci_playback_iec958_subframe =
  1230. {
  1231. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1232. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1233. SNDRV_PCM_INFO_MMAP_VALID),
  1234. .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE,
  1235. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1236. .rate_min = 44100,
  1237. .rate_max = 48000,
  1238. .channels_min = 2,
  1239. .channels_max = 2,
  1240. .buffer_bytes_max = (128*1024),
  1241. .period_bytes_min = 64,
  1242. .period_bytes_max = (128*1024),
  1243. .periods_min = 2,
  1244. .periods_max = 1024,
  1245. .fifo_size = 0,
  1246. };
  1247. /* spdif capture on channel B */
  1248. static snd_pcm_hardware_t snd_cmipci_capture_spdif =
  1249. {
  1250. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1251. SNDRV_PCM_INFO_BLOCK_TRANSFER | SNDRV_PCM_INFO_PAUSE |
  1252. SNDRV_PCM_INFO_MMAP_VALID),
  1253. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1254. .rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
  1255. .rate_min = 44100,
  1256. .rate_max = 48000,
  1257. .channels_min = 2,
  1258. .channels_max = 2,
  1259. .buffer_bytes_max = (128*1024),
  1260. .period_bytes_min = 64,
  1261. .period_bytes_max = (128*1024),
  1262. .periods_min = 2,
  1263. .periods_max = 1024,
  1264. .fifo_size = 0,
  1265. };
  1266. /*
  1267. * check device open/close
  1268. */
  1269. static int open_device_check(cmipci_t *cm, int mode, snd_pcm_substream_t *subs)
  1270. {
  1271. int ch = mode & CM_OPEN_CH_MASK;
  1272. /* FIXME: a file should wait until the device becomes free
  1273. * when it's opened on blocking mode. however, since the current
  1274. * pcm framework doesn't pass file pointer before actually opened,
  1275. * we can't know whether blocking mode or not in open callback..
  1276. */
  1277. down(&cm->open_mutex);
  1278. if (cm->opened[ch]) {
  1279. up(&cm->open_mutex);
  1280. return -EBUSY;
  1281. }
  1282. cm->opened[ch] = mode;
  1283. cm->channel[ch].substream = subs;
  1284. if (! (mode & CM_OPEN_DAC)) {
  1285. /* disable dual DAC mode */
  1286. cm->channel[ch].is_dac = 0;
  1287. spin_lock_irq(&cm->reg_lock);
  1288. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1289. spin_unlock_irq(&cm->reg_lock);
  1290. }
  1291. up(&cm->open_mutex);
  1292. return 0;
  1293. }
  1294. static void close_device_check(cmipci_t *cm, int mode)
  1295. {
  1296. int ch = mode & CM_OPEN_CH_MASK;
  1297. down(&cm->open_mutex);
  1298. if (cm->opened[ch] == mode) {
  1299. if (cm->channel[ch].substream) {
  1300. snd_cmipci_ch_reset(cm, ch);
  1301. cm->channel[ch].running = 0;
  1302. cm->channel[ch].substream = NULL;
  1303. }
  1304. cm->opened[ch] = 0;
  1305. if (! cm->channel[ch].is_dac) {
  1306. /* enable dual DAC mode again */
  1307. cm->channel[ch].is_dac = 1;
  1308. spin_lock_irq(&cm->reg_lock);
  1309. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC);
  1310. spin_unlock_irq(&cm->reg_lock);
  1311. }
  1312. }
  1313. up(&cm->open_mutex);
  1314. }
  1315. /*
  1316. */
  1317. static int snd_cmipci_playback_open(snd_pcm_substream_t *substream)
  1318. {
  1319. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1320. snd_pcm_runtime_t *runtime = substream->runtime;
  1321. int err;
  1322. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK, substream)) < 0)
  1323. return err;
  1324. runtime->hw = snd_cmipci_playback;
  1325. runtime->hw.channels_max = cm->max_channels;
  1326. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1327. cm->dig_pcm_status = cm->dig_status;
  1328. return 0;
  1329. }
  1330. static int snd_cmipci_capture_open(snd_pcm_substream_t *substream)
  1331. {
  1332. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1333. snd_pcm_runtime_t *runtime = substream->runtime;
  1334. int err;
  1335. if ((err = open_device_check(cm, CM_OPEN_CAPTURE, substream)) < 0)
  1336. return err;
  1337. runtime->hw = snd_cmipci_capture;
  1338. if (cm->chip_version == 68) { // 8768 only supports 44k/48k recording
  1339. runtime->hw.rate_min = 41000;
  1340. runtime->hw.rates = SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000;
  1341. }
  1342. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1343. return 0;
  1344. }
  1345. static int snd_cmipci_playback2_open(snd_pcm_substream_t *substream)
  1346. {
  1347. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1348. snd_pcm_runtime_t *runtime = substream->runtime;
  1349. int err;
  1350. if ((err = open_device_check(cm, CM_OPEN_PLAYBACK2, substream)) < 0) /* use channel B */
  1351. return err;
  1352. runtime->hw = snd_cmipci_playback2;
  1353. down(&cm->open_mutex);
  1354. if (! cm->opened[CM_CH_PLAY]) {
  1355. if (cm->can_multi_ch) {
  1356. runtime->hw.channels_max = cm->max_channels;
  1357. if (cm->max_channels == 4)
  1358. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_4);
  1359. else if (cm->max_channels == 6)
  1360. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_6);
  1361. else if (cm->max_channels == 8)
  1362. snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS, &hw_constraints_channels_8);
  1363. }
  1364. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x10000);
  1365. }
  1366. up(&cm->open_mutex);
  1367. return 0;
  1368. }
  1369. static int snd_cmipci_playback_spdif_open(snd_pcm_substream_t *substream)
  1370. {
  1371. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1372. snd_pcm_runtime_t *runtime = substream->runtime;
  1373. int err;
  1374. if ((err = open_device_check(cm, CM_OPEN_SPDIF_PLAYBACK, substream)) < 0) /* use channel A */
  1375. return err;
  1376. if (cm->can_ac3_hw) {
  1377. runtime->hw = snd_cmipci_playback_spdif;
  1378. if (cm->chip_version >= 37)
  1379. runtime->hw.formats |= SNDRV_PCM_FMTBIT_S32_LE;
  1380. } else {
  1381. runtime->hw = snd_cmipci_playback_iec958_subframe;
  1382. }
  1383. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1384. cm->dig_pcm_status = cm->dig_status;
  1385. return 0;
  1386. }
  1387. static int snd_cmipci_capture_spdif_open(snd_pcm_substream_t * substream)
  1388. {
  1389. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1390. snd_pcm_runtime_t *runtime = substream->runtime;
  1391. int err;
  1392. if ((err = open_device_check(cm, CM_OPEN_SPDIF_CAPTURE, substream)) < 0) /* use channel B */
  1393. return err;
  1394. runtime->hw = snd_cmipci_capture_spdif;
  1395. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 0x40000);
  1396. return 0;
  1397. }
  1398. /*
  1399. */
  1400. static int snd_cmipci_playback_close(snd_pcm_substream_t * substream)
  1401. {
  1402. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1403. close_device_check(cm, CM_OPEN_PLAYBACK);
  1404. return 0;
  1405. }
  1406. static int snd_cmipci_capture_close(snd_pcm_substream_t * substream)
  1407. {
  1408. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1409. close_device_check(cm, CM_OPEN_CAPTURE);
  1410. return 0;
  1411. }
  1412. static int snd_cmipci_playback2_close(snd_pcm_substream_t * substream)
  1413. {
  1414. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1415. close_device_check(cm, CM_OPEN_PLAYBACK2);
  1416. close_device_check(cm, CM_OPEN_PLAYBACK_MULTI);
  1417. return 0;
  1418. }
  1419. static int snd_cmipci_playback_spdif_close(snd_pcm_substream_t * substream)
  1420. {
  1421. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1422. close_device_check(cm, CM_OPEN_SPDIF_PLAYBACK);
  1423. return 0;
  1424. }
  1425. static int snd_cmipci_capture_spdif_close(snd_pcm_substream_t * substream)
  1426. {
  1427. cmipci_t *cm = snd_pcm_substream_chip(substream);
  1428. close_device_check(cm, CM_OPEN_SPDIF_CAPTURE);
  1429. return 0;
  1430. }
  1431. /*
  1432. */
  1433. static snd_pcm_ops_t snd_cmipci_playback_ops = {
  1434. .open = snd_cmipci_playback_open,
  1435. .close = snd_cmipci_playback_close,
  1436. .ioctl = snd_pcm_lib_ioctl,
  1437. .hw_params = snd_cmipci_hw_params,
  1438. .hw_free = snd_cmipci_playback_hw_free,
  1439. .prepare = snd_cmipci_playback_prepare,
  1440. .trigger = snd_cmipci_playback_trigger,
  1441. .pointer = snd_cmipci_playback_pointer,
  1442. };
  1443. static snd_pcm_ops_t snd_cmipci_capture_ops = {
  1444. .open = snd_cmipci_capture_open,
  1445. .close = snd_cmipci_capture_close,
  1446. .ioctl = snd_pcm_lib_ioctl,
  1447. .hw_params = snd_cmipci_hw_params,
  1448. .hw_free = snd_cmipci_hw_free,
  1449. .prepare = snd_cmipci_capture_prepare,
  1450. .trigger = snd_cmipci_capture_trigger,
  1451. .pointer = snd_cmipci_capture_pointer,
  1452. };
  1453. static snd_pcm_ops_t snd_cmipci_playback2_ops = {
  1454. .open = snd_cmipci_playback2_open,
  1455. .close = snd_cmipci_playback2_close,
  1456. .ioctl = snd_pcm_lib_ioctl,
  1457. .hw_params = snd_cmipci_playback2_hw_params,
  1458. .hw_free = snd_cmipci_hw_free,
  1459. .prepare = snd_cmipci_capture_prepare, /* channel B */
  1460. .trigger = snd_cmipci_capture_trigger, /* channel B */
  1461. .pointer = snd_cmipci_capture_pointer, /* channel B */
  1462. };
  1463. static snd_pcm_ops_t snd_cmipci_playback_spdif_ops = {
  1464. .open = snd_cmipci_playback_spdif_open,
  1465. .close = snd_cmipci_playback_spdif_close,
  1466. .ioctl = snd_pcm_lib_ioctl,
  1467. .hw_params = snd_cmipci_hw_params,
  1468. .hw_free = snd_cmipci_playback_hw_free,
  1469. .prepare = snd_cmipci_playback_spdif_prepare, /* set up rate */
  1470. .trigger = snd_cmipci_playback_trigger,
  1471. .pointer = snd_cmipci_playback_pointer,
  1472. };
  1473. static snd_pcm_ops_t snd_cmipci_capture_spdif_ops = {
  1474. .open = snd_cmipci_capture_spdif_open,
  1475. .close = snd_cmipci_capture_spdif_close,
  1476. .ioctl = snd_pcm_lib_ioctl,
  1477. .hw_params = snd_cmipci_hw_params,
  1478. .hw_free = snd_cmipci_capture_spdif_hw_free,
  1479. .prepare = snd_cmipci_capture_spdif_prepare,
  1480. .trigger = snd_cmipci_capture_trigger,
  1481. .pointer = snd_cmipci_capture_pointer,
  1482. };
  1483. /*
  1484. */
  1485. static void snd_cmipci_pcm_free(snd_pcm_t *pcm)
  1486. {
  1487. snd_pcm_lib_preallocate_free_for_all(pcm);
  1488. }
  1489. static int __devinit snd_cmipci_pcm_new(cmipci_t *cm, int device)
  1490. {
  1491. snd_pcm_t *pcm;
  1492. int err;
  1493. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1494. if (err < 0)
  1495. return err;
  1496. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_ops);
  1497. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_ops);
  1498. pcm->private_data = cm;
  1499. pcm->private_free = snd_cmipci_pcm_free;
  1500. pcm->info_flags = 0;
  1501. strcpy(pcm->name, "C-Media PCI DAC/ADC");
  1502. cm->pcm = pcm;
  1503. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1504. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1505. return 0;
  1506. }
  1507. static int __devinit snd_cmipci_pcm2_new(cmipci_t *cm, int device)
  1508. {
  1509. snd_pcm_t *pcm;
  1510. int err;
  1511. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 0, &pcm);
  1512. if (err < 0)
  1513. return err;
  1514. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback2_ops);
  1515. pcm->private_data = cm;
  1516. pcm->private_free = snd_cmipci_pcm_free;
  1517. pcm->info_flags = 0;
  1518. strcpy(pcm->name, "C-Media PCI 2nd DAC");
  1519. cm->pcm2 = pcm;
  1520. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1521. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1522. return 0;
  1523. }
  1524. static int __devinit snd_cmipci_pcm_spdif_new(cmipci_t *cm, int device)
  1525. {
  1526. snd_pcm_t *pcm;
  1527. int err;
  1528. err = snd_pcm_new(cm->card, cm->card->driver, device, 1, 1, &pcm);
  1529. if (err < 0)
  1530. return err;
  1531. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cmipci_playback_spdif_ops);
  1532. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cmipci_capture_spdif_ops);
  1533. pcm->private_data = cm;
  1534. pcm->private_free = snd_cmipci_pcm_free;
  1535. pcm->info_flags = 0;
  1536. strcpy(pcm->name, "C-Media PCI IEC958");
  1537. cm->pcm_spdif = pcm;
  1538. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1539. snd_dma_pci_data(cm->pci), 64*1024, 128*1024);
  1540. return 0;
  1541. }
  1542. /*
  1543. * mixer interface:
  1544. * - CM8338/8738 has a compatible mixer interface with SB16, but
  1545. * lack of some elements like tone control, i/o gain and AGC.
  1546. * - Access to native registers:
  1547. * - A 3D switch
  1548. * - Output mute switches
  1549. */
  1550. static void snd_cmipci_mixer_write(cmipci_t *s, unsigned char idx, unsigned char data)
  1551. {
  1552. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1553. outb(data, s->iobase + CM_REG_SB16_DATA);
  1554. }
  1555. static unsigned char snd_cmipci_mixer_read(cmipci_t *s, unsigned char idx)
  1556. {
  1557. unsigned char v;
  1558. outb(idx, s->iobase + CM_REG_SB16_ADDR);
  1559. v = inb(s->iobase + CM_REG_SB16_DATA);
  1560. return v;
  1561. }
  1562. /*
  1563. * general mixer element
  1564. */
  1565. typedef struct cmipci_sb_reg {
  1566. unsigned int left_reg, right_reg;
  1567. unsigned int left_shift, right_shift;
  1568. unsigned int mask;
  1569. unsigned int invert: 1;
  1570. unsigned int stereo: 1;
  1571. } cmipci_sb_reg_t;
  1572. #define COMPOSE_SB_REG(lreg,rreg,lshift,rshift,mask,invert,stereo) \
  1573. ((lreg) | ((rreg) << 8) | (lshift << 16) | (rshift << 19) | (mask << 24) | (invert << 22) | (stereo << 23))
  1574. #define CMIPCI_DOUBLE(xname, left_reg, right_reg, left_shift, right_shift, mask, invert, stereo) \
  1575. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1576. .info = snd_cmipci_info_volume, \
  1577. .get = snd_cmipci_get_volume, .put = snd_cmipci_put_volume, \
  1578. .private_value = COMPOSE_SB_REG(left_reg, right_reg, left_shift, right_shift, mask, invert, stereo), \
  1579. }
  1580. #define CMIPCI_SB_VOL_STEREO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg+1, shift, shift, mask, 0, 1)
  1581. #define CMIPCI_SB_VOL_MONO(xname,reg,shift,mask) CMIPCI_DOUBLE(xname, reg, reg, shift, shift, mask, 0, 0)
  1582. #define CMIPCI_SB_SW_STEREO(xname,lshift,rshift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, lshift, rshift, 1, 0, 1)
  1583. #define CMIPCI_SB_SW_MONO(xname,shift) CMIPCI_DOUBLE(xname, SB_DSP4_OUTPUT_SW, SB_DSP4_OUTPUT_SW, shift, shift, 1, 0, 0)
  1584. static void cmipci_sb_reg_decode(cmipci_sb_reg_t *r, unsigned long val)
  1585. {
  1586. r->left_reg = val & 0xff;
  1587. r->right_reg = (val >> 8) & 0xff;
  1588. r->left_shift = (val >> 16) & 0x07;
  1589. r->right_shift = (val >> 19) & 0x07;
  1590. r->invert = (val >> 22) & 1;
  1591. r->stereo = (val >> 23) & 1;
  1592. r->mask = (val >> 24) & 0xff;
  1593. }
  1594. static int snd_cmipci_info_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  1595. {
  1596. cmipci_sb_reg_t reg;
  1597. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1598. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1599. uinfo->count = reg.stereo + 1;
  1600. uinfo->value.integer.min = 0;
  1601. uinfo->value.integer.max = reg.mask;
  1602. return 0;
  1603. }
  1604. static int snd_cmipci_get_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1605. {
  1606. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1607. cmipci_sb_reg_t reg;
  1608. int val;
  1609. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1610. spin_lock_irq(&cm->reg_lock);
  1611. val = (snd_cmipci_mixer_read(cm, reg.left_reg) >> reg.left_shift) & reg.mask;
  1612. if (reg.invert)
  1613. val = reg.mask - val;
  1614. ucontrol->value.integer.value[0] = val;
  1615. if (reg.stereo) {
  1616. val = (snd_cmipci_mixer_read(cm, reg.right_reg) >> reg.right_shift) & reg.mask;
  1617. if (reg.invert)
  1618. val = reg.mask - val;
  1619. ucontrol->value.integer.value[1] = val;
  1620. }
  1621. spin_unlock_irq(&cm->reg_lock);
  1622. return 0;
  1623. }
  1624. static int snd_cmipci_put_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1625. {
  1626. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1627. cmipci_sb_reg_t reg;
  1628. int change;
  1629. int left, right, oleft, oright;
  1630. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1631. left = ucontrol->value.integer.value[0] & reg.mask;
  1632. if (reg.invert)
  1633. left = reg.mask - left;
  1634. left <<= reg.left_shift;
  1635. if (reg.stereo) {
  1636. right = ucontrol->value.integer.value[1] & reg.mask;
  1637. if (reg.invert)
  1638. right = reg.mask - right;
  1639. right <<= reg.right_shift;
  1640. } else
  1641. right = 0;
  1642. spin_lock_irq(&cm->reg_lock);
  1643. oleft = snd_cmipci_mixer_read(cm, reg.left_reg);
  1644. left |= oleft & ~(reg.mask << reg.left_shift);
  1645. change = left != oleft;
  1646. if (reg.stereo) {
  1647. if (reg.left_reg != reg.right_reg) {
  1648. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1649. oright = snd_cmipci_mixer_read(cm, reg.right_reg);
  1650. } else
  1651. oright = left;
  1652. right |= oright & ~(reg.mask << reg.right_shift);
  1653. change |= right != oright;
  1654. snd_cmipci_mixer_write(cm, reg.right_reg, right);
  1655. } else
  1656. snd_cmipci_mixer_write(cm, reg.left_reg, left);
  1657. spin_unlock_irq(&cm->reg_lock);
  1658. return change;
  1659. }
  1660. /*
  1661. * input route (left,right) -> (left,right)
  1662. */
  1663. #define CMIPCI_SB_INPUT_SW(xname, left_shift, right_shift) \
  1664. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1665. .info = snd_cmipci_info_input_sw, \
  1666. .get = snd_cmipci_get_input_sw, .put = snd_cmipci_put_input_sw, \
  1667. .private_value = COMPOSE_SB_REG(SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, left_shift, right_shift, 1, 0, 1), \
  1668. }
  1669. static int snd_cmipci_info_input_sw(snd_kcontrol_t * kcontrol, snd_ctl_elem_info_t * uinfo)
  1670. {
  1671. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1672. uinfo->count = 4;
  1673. uinfo->value.integer.min = 0;
  1674. uinfo->value.integer.max = 1;
  1675. return 0;
  1676. }
  1677. static int snd_cmipci_get_input_sw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1678. {
  1679. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1680. cmipci_sb_reg_t reg;
  1681. int val1, val2;
  1682. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1683. spin_lock_irq(&cm->reg_lock);
  1684. val1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1685. val2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1686. spin_unlock_irq(&cm->reg_lock);
  1687. ucontrol->value.integer.value[0] = (val1 >> reg.left_shift) & 1;
  1688. ucontrol->value.integer.value[1] = (val2 >> reg.left_shift) & 1;
  1689. ucontrol->value.integer.value[2] = (val1 >> reg.right_shift) & 1;
  1690. ucontrol->value.integer.value[3] = (val2 >> reg.right_shift) & 1;
  1691. return 0;
  1692. }
  1693. static int snd_cmipci_put_input_sw(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1694. {
  1695. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1696. cmipci_sb_reg_t reg;
  1697. int change;
  1698. int val1, val2, oval1, oval2;
  1699. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1700. spin_lock_irq(&cm->reg_lock);
  1701. oval1 = snd_cmipci_mixer_read(cm, reg.left_reg);
  1702. oval2 = snd_cmipci_mixer_read(cm, reg.right_reg);
  1703. val1 = oval1 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1704. val2 = oval2 & ~((1 << reg.left_shift) | (1 << reg.right_shift));
  1705. val1 |= (ucontrol->value.integer.value[0] & 1) << reg.left_shift;
  1706. val2 |= (ucontrol->value.integer.value[1] & 1) << reg.left_shift;
  1707. val1 |= (ucontrol->value.integer.value[2] & 1) << reg.right_shift;
  1708. val2 |= (ucontrol->value.integer.value[3] & 1) << reg.right_shift;
  1709. change = val1 != oval1 || val2 != oval2;
  1710. snd_cmipci_mixer_write(cm, reg.left_reg, val1);
  1711. snd_cmipci_mixer_write(cm, reg.right_reg, val2);
  1712. spin_unlock_irq(&cm->reg_lock);
  1713. return change;
  1714. }
  1715. /*
  1716. * native mixer switches/volumes
  1717. */
  1718. #define CMIPCI_MIXER_SW_STEREO(xname, reg, lshift, rshift, invert) \
  1719. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1720. .info = snd_cmipci_info_native_mixer, \
  1721. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1722. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, 1, invert, 1), \
  1723. }
  1724. #define CMIPCI_MIXER_SW_MONO(xname, reg, shift, invert) \
  1725. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1726. .info = snd_cmipci_info_native_mixer, \
  1727. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1728. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, 1, invert, 0), \
  1729. }
  1730. #define CMIPCI_MIXER_VOL_STEREO(xname, reg, lshift, rshift, mask) \
  1731. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1732. .info = snd_cmipci_info_native_mixer, \
  1733. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1734. .private_value = COMPOSE_SB_REG(reg, reg, lshift, rshift, mask, 0, 1), \
  1735. }
  1736. #define CMIPCI_MIXER_VOL_MONO(xname, reg, shift, mask) \
  1737. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
  1738. .info = snd_cmipci_info_native_mixer, \
  1739. .get = snd_cmipci_get_native_mixer, .put = snd_cmipci_put_native_mixer, \
  1740. .private_value = COMPOSE_SB_REG(reg, reg, shift, shift, mask, 0, 0), \
  1741. }
  1742. static int snd_cmipci_info_native_mixer(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1743. {
  1744. cmipci_sb_reg_t reg;
  1745. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1746. uinfo->type = reg.mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  1747. uinfo->count = reg.stereo + 1;
  1748. uinfo->value.integer.min = 0;
  1749. uinfo->value.integer.max = reg.mask;
  1750. return 0;
  1751. }
  1752. static int snd_cmipci_get_native_mixer(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1753. {
  1754. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1755. cmipci_sb_reg_t reg;
  1756. unsigned char oreg, val;
  1757. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1758. spin_lock_irq(&cm->reg_lock);
  1759. oreg = inb(cm->iobase + reg.left_reg);
  1760. val = (oreg >> reg.left_shift) & reg.mask;
  1761. if (reg.invert)
  1762. val = reg.mask - val;
  1763. ucontrol->value.integer.value[0] = val;
  1764. if (reg.stereo) {
  1765. val = (oreg >> reg.right_shift) & reg.mask;
  1766. if (reg.invert)
  1767. val = reg.mask - val;
  1768. ucontrol->value.integer.value[1] = val;
  1769. }
  1770. spin_unlock_irq(&cm->reg_lock);
  1771. return 0;
  1772. }
  1773. static int snd_cmipci_put_native_mixer(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1774. {
  1775. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1776. cmipci_sb_reg_t reg;
  1777. unsigned char oreg, nreg, val;
  1778. cmipci_sb_reg_decode(&reg, kcontrol->private_value);
  1779. spin_lock_irq(&cm->reg_lock);
  1780. oreg = inb(cm->iobase + reg.left_reg);
  1781. val = ucontrol->value.integer.value[0] & reg.mask;
  1782. if (reg.invert)
  1783. val = reg.mask - val;
  1784. nreg = oreg & ~(reg.mask << reg.left_shift);
  1785. nreg |= (val << reg.left_shift);
  1786. if (reg.stereo) {
  1787. val = ucontrol->value.integer.value[1] & reg.mask;
  1788. if (reg.invert)
  1789. val = reg.mask - val;
  1790. nreg &= ~(reg.mask << reg.right_shift);
  1791. nreg |= (val << reg.right_shift);
  1792. }
  1793. outb(nreg, cm->iobase + reg.left_reg);
  1794. spin_unlock_irq(&cm->reg_lock);
  1795. return (nreg != oreg);
  1796. }
  1797. /*
  1798. * special case - check mixer sensitivity
  1799. */
  1800. static int snd_cmipci_get_native_mixer_sensitive(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1801. {
  1802. //cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1803. return snd_cmipci_get_native_mixer(kcontrol, ucontrol);
  1804. }
  1805. static int snd_cmipci_put_native_mixer_sensitive(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1806. {
  1807. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1808. if (cm->mixer_insensitive) {
  1809. /* ignored */
  1810. return 0;
  1811. }
  1812. return snd_cmipci_put_native_mixer(kcontrol, ucontrol);
  1813. }
  1814. static snd_kcontrol_new_t snd_cmipci_mixers[] __devinitdata = {
  1815. CMIPCI_SB_VOL_STEREO("Master Playback Volume", SB_DSP4_MASTER_DEV, 3, 31),
  1816. CMIPCI_MIXER_SW_MONO("3D Control - Switch", CM_REG_MIXER1, CM_X3DEN_SHIFT, 0),
  1817. CMIPCI_SB_VOL_STEREO("PCM Playback Volume", SB_DSP4_PCM_DEV, 3, 31),
  1818. //CMIPCI_MIXER_SW_MONO("PCM Playback Switch", CM_REG_MIXER1, CM_WSMUTE_SHIFT, 1),
  1819. { /* switch with sensitivity */
  1820. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1821. .name = "PCM Playback Switch",
  1822. .info = snd_cmipci_info_native_mixer,
  1823. .get = snd_cmipci_get_native_mixer_sensitive,
  1824. .put = snd_cmipci_put_native_mixer_sensitive,
  1825. .private_value = COMPOSE_SB_REG(CM_REG_MIXER1, CM_REG_MIXER1, CM_WSMUTE_SHIFT, CM_WSMUTE_SHIFT, 1, 1, 0),
  1826. },
  1827. CMIPCI_MIXER_SW_STEREO("PCM Capture Switch", CM_REG_MIXER1, CM_WAVEINL_SHIFT, CM_WAVEINR_SHIFT, 0),
  1828. CMIPCI_SB_VOL_STEREO("Synth Playback Volume", SB_DSP4_SYNTH_DEV, 3, 31),
  1829. CMIPCI_MIXER_SW_MONO("Synth Playback Switch", CM_REG_MIXER1, CM_FMMUTE_SHIFT, 1),
  1830. CMIPCI_SB_INPUT_SW("Synth Capture Route", 6, 5),
  1831. CMIPCI_SB_VOL_STEREO("CD Playback Volume", SB_DSP4_CD_DEV, 3, 31),
  1832. CMIPCI_SB_SW_STEREO("CD Playback Switch", 2, 1),
  1833. CMIPCI_SB_INPUT_SW("CD Capture Route", 2, 1),
  1834. CMIPCI_SB_VOL_STEREO("Line Playback Volume", SB_DSP4_LINE_DEV, 3, 31),
  1835. CMIPCI_SB_SW_STEREO("Line Playback Switch", 4, 3),
  1836. CMIPCI_SB_INPUT_SW("Line Capture Route", 4, 3),
  1837. CMIPCI_SB_VOL_MONO("Mic Playback Volume", SB_DSP4_MIC_DEV, 3, 31),
  1838. CMIPCI_SB_SW_MONO("Mic Playback Switch", 0),
  1839. CMIPCI_DOUBLE("Mic Capture Switch", SB_DSP4_INPUT_LEFT, SB_DSP4_INPUT_RIGHT, 0, 0, 1, 0, 0),
  1840. CMIPCI_SB_VOL_MONO("PC Speaker Playback Volume", SB_DSP4_SPEAKER_DEV, 6, 3),
  1841. CMIPCI_MIXER_VOL_STEREO("Aux Playback Volume", CM_REG_AUX_VOL, 4, 0, 15),
  1842. CMIPCI_MIXER_SW_STEREO("Aux Playback Switch", CM_REG_MIXER2, CM_VAUXLM_SHIFT, CM_VAUXRM_SHIFT, 0),
  1843. CMIPCI_MIXER_SW_STEREO("Aux Capture Switch", CM_REG_MIXER2, CM_RAUXLEN_SHIFT, CM_RAUXREN_SHIFT, 0),
  1844. CMIPCI_MIXER_SW_MONO("Mic Boost Playback Switch", CM_REG_MIXER2, CM_MICGAINZ_SHIFT, 1),
  1845. CMIPCI_MIXER_VOL_MONO("Mic Capture Volume", CM_REG_MIXER2, CM_VADMIC_SHIFT, 7),
  1846. CMIPCI_SB_VOL_MONO("Phone Playback Volume", CM_REG_EXTENT_IND, 5, 7),
  1847. CMIPCI_DOUBLE("Phone Playback Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 4, 4, 1, 0, 0),
  1848. CMIPCI_DOUBLE("PC Speaker Playnack Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 3, 3, 1, 0, 0),
  1849. CMIPCI_DOUBLE("Mic Boost Capture Switch", CM_REG_EXTENT_IND, CM_REG_EXTENT_IND, 0, 0, 1, 0, 0),
  1850. };
  1851. /*
  1852. * other switches
  1853. */
  1854. typedef struct snd_cmipci_switch_args {
  1855. int reg; /* register index */
  1856. unsigned int mask; /* mask bits */
  1857. unsigned int mask_on; /* mask bits to turn on */
  1858. unsigned int is_byte: 1; /* byte access? */
  1859. unsigned int ac3_sensitive: 1; /* access forbidden during non-audio operation? */
  1860. } snd_cmipci_switch_args_t;
  1861. static int snd_cmipci_uswitch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  1862. {
  1863. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1864. uinfo->count = 1;
  1865. uinfo->value.integer.min = 0;
  1866. uinfo->value.integer.max = 1;
  1867. return 0;
  1868. }
  1869. static int _snd_cmipci_uswitch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol, snd_cmipci_switch_args_t *args)
  1870. {
  1871. unsigned int val;
  1872. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1873. spin_lock_irq(&cm->reg_lock);
  1874. if (args->ac3_sensitive && cm->mixer_insensitive) {
  1875. ucontrol->value.integer.value[0] = 0;
  1876. spin_unlock_irq(&cm->reg_lock);
  1877. return 0;
  1878. }
  1879. if (args->is_byte)
  1880. val = inb(cm->iobase + args->reg);
  1881. else
  1882. val = snd_cmipci_read(cm, args->reg);
  1883. ucontrol->value.integer.value[0] = ((val & args->mask) == args->mask_on) ? 1 : 0;
  1884. spin_unlock_irq(&cm->reg_lock);
  1885. return 0;
  1886. }
  1887. static int snd_cmipci_uswitch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1888. {
  1889. snd_cmipci_switch_args_t *args = (snd_cmipci_switch_args_t*)kcontrol->private_value;
  1890. snd_assert(args != NULL, return -EINVAL);
  1891. return _snd_cmipci_uswitch_get(kcontrol, ucontrol, args);
  1892. }
  1893. static int _snd_cmipci_uswitch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol, snd_cmipci_switch_args_t *args)
  1894. {
  1895. unsigned int val;
  1896. int change;
  1897. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  1898. spin_lock_irq(&cm->reg_lock);
  1899. if (args->ac3_sensitive && cm->mixer_insensitive) {
  1900. /* ignored */
  1901. spin_unlock_irq(&cm->reg_lock);
  1902. return 0;
  1903. }
  1904. if (args->is_byte)
  1905. val = inb(cm->iobase + args->reg);
  1906. else
  1907. val = snd_cmipci_read(cm, args->reg);
  1908. change = (val & args->mask) != (ucontrol->value.integer.value[0] ? args->mask : 0);
  1909. if (change) {
  1910. val &= ~args->mask;
  1911. if (ucontrol->value.integer.value[0])
  1912. val |= args->mask_on;
  1913. else
  1914. val |= (args->mask & ~args->mask_on);
  1915. if (args->is_byte)
  1916. outb((unsigned char)val, cm->iobase + args->reg);
  1917. else
  1918. snd_cmipci_write(cm, args->reg, val);
  1919. }
  1920. spin_unlock_irq(&cm->reg_lock);
  1921. return change;
  1922. }
  1923. static int snd_cmipci_uswitch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1924. {
  1925. snd_cmipci_switch_args_t *args = (snd_cmipci_switch_args_t*)kcontrol->private_value;
  1926. snd_assert(args != NULL, return -EINVAL);
  1927. return _snd_cmipci_uswitch_put(kcontrol, ucontrol, args);
  1928. }
  1929. #define DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask_on, xis_byte, xac3) \
  1930. static snd_cmipci_switch_args_t cmipci_switch_arg_##sname = { \
  1931. .reg = xreg, \
  1932. .mask = xmask, \
  1933. .mask_on = xmask_on, \
  1934. .is_byte = xis_byte, \
  1935. .ac3_sensitive = xac3, \
  1936. }
  1937. #define DEFINE_BIT_SWITCH_ARG(sname, xreg, xmask, xis_byte, xac3) \
  1938. DEFINE_SWITCH_ARG(sname, xreg, xmask, xmask, xis_byte, xac3)
  1939. #if 0 /* these will be controlled in pcm device */
  1940. DEFINE_BIT_SWITCH_ARG(spdif_in, CM_REG_FUNCTRL1, CM_SPDF_1, 0, 0);
  1941. DEFINE_BIT_SWITCH_ARG(spdif_out, CM_REG_FUNCTRL1, CM_SPDF_0, 0, 0);
  1942. #endif
  1943. DEFINE_BIT_SWITCH_ARG(spdif_in_sel1, CM_REG_CHFORMAT, CM_SPDIF_SELECT1, 0, 0);
  1944. DEFINE_BIT_SWITCH_ARG(spdif_in_sel2, CM_REG_MISC_CTRL, CM_SPDIF_SELECT2, 0, 0);
  1945. DEFINE_BIT_SWITCH_ARG(spdif_enable, CM_REG_LEGACY_CTRL, CM_ENSPDOUT, 0, 0);
  1946. DEFINE_BIT_SWITCH_ARG(spdo2dac, CM_REG_FUNCTRL1, CM_SPDO2DAC, 0, 1);
  1947. DEFINE_BIT_SWITCH_ARG(spdi_valid, CM_REG_MISC, CM_SPDVALID, 1, 0);
  1948. DEFINE_BIT_SWITCH_ARG(spdif_copyright, CM_REG_LEGACY_CTRL, CM_SPDCOPYRHT, 0, 0);
  1949. DEFINE_BIT_SWITCH_ARG(spdif_dac_out, CM_REG_LEGACY_CTRL, CM_DAC2SPDO, 0, 1);
  1950. DEFINE_SWITCH_ARG(spdo_5v, CM_REG_MISC_CTRL, CM_SPDO5V, 0, 0, 0); /* inverse: 0 = 5V */
  1951. // DEFINE_BIT_SWITCH_ARG(spdo_48k, CM_REG_MISC_CTRL, CM_SPDF_AC97|CM_SPDIF48K, 0, 1);
  1952. DEFINE_BIT_SWITCH_ARG(spdif_loop, CM_REG_FUNCTRL1, CM_SPDFLOOP, 0, 1);
  1953. DEFINE_BIT_SWITCH_ARG(spdi_monitor, CM_REG_MIXER1, CM_CDPLAY, 1, 0);
  1954. /* DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_CHFORMAT, CM_SPDIF_INVERSE, 0, 0); */
  1955. DEFINE_BIT_SWITCH_ARG(spdi_phase, CM_REG_MISC, CM_SPDIF_INVERSE, 1, 0);
  1956. DEFINE_BIT_SWITCH_ARG(spdi_phase2, CM_REG_CHFORMAT, CM_SPDIF_INVERSE2, 0, 0);
  1957. #if CM_CH_PLAY == 1
  1958. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, 0, 0, 0); /* reversed */
  1959. #else
  1960. DEFINE_SWITCH_ARG(exchange_dac, CM_REG_MISC_CTRL, CM_XCHGDAC, CM_XCHGDAC, 0, 0);
  1961. #endif
  1962. DEFINE_BIT_SWITCH_ARG(fourch, CM_REG_MISC_CTRL, CM_N4SPK3D, 0, 0);
  1963. // DEFINE_BIT_SWITCH_ARG(line_rear, CM_REG_MIXER1, CM_SPK4, 1, 0);
  1964. // DEFINE_BIT_SWITCH_ARG(line_bass, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS, 0, 0);
  1965. // DEFINE_BIT_SWITCH_ARG(joystick, CM_REG_FUNCTRL1, CM_JYSTK_EN, 0, 0); /* now module option */
  1966. DEFINE_SWITCH_ARG(modem, CM_REG_MISC_CTRL, CM_FLINKON|CM_FLINKOFF, CM_FLINKON, 0, 0);
  1967. #define DEFINE_SWITCH(sname, stype, sarg) \
  1968. { .name = sname, \
  1969. .iface = stype, \
  1970. .info = snd_cmipci_uswitch_info, \
  1971. .get = snd_cmipci_uswitch_get, \
  1972. .put = snd_cmipci_uswitch_put, \
  1973. .private_value = (unsigned long)&cmipci_switch_arg_##sarg,\
  1974. }
  1975. #define DEFINE_CARD_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_CARD, sarg)
  1976. #define DEFINE_MIXER_SWITCH(sname, sarg) DEFINE_SWITCH(sname, SNDRV_CTL_ELEM_IFACE_MIXER, sarg)
  1977. /*
  1978. * callbacks for spdif output switch
  1979. * needs toggle two registers..
  1980. */
  1981. static int snd_cmipci_spdout_enable_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1982. {
  1983. int changed;
  1984. changed = _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  1985. changed |= _snd_cmipci_uswitch_get(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  1986. return changed;
  1987. }
  1988. static int snd_cmipci_spdout_enable_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  1989. {
  1990. cmipci_t *chip = snd_kcontrol_chip(kcontrol);
  1991. int changed;
  1992. changed = _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdif_enable);
  1993. changed |= _snd_cmipci_uswitch_put(kcontrol, ucontrol, &cmipci_switch_arg_spdo2dac);
  1994. if (changed) {
  1995. if (ucontrol->value.integer.value[0]) {
  1996. if (chip->spdif_playback_avail)
  1997. snd_cmipci_set_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  1998. } else {
  1999. if (chip->spdif_playback_avail)
  2000. snd_cmipci_clear_bit(chip, CM_REG_FUNCTRL1, CM_PLAYBACK_SPDF);
  2001. }
  2002. }
  2003. chip->spdif_playback_enabled = ucontrol->value.integer.value[0];
  2004. return changed;
  2005. }
  2006. static int snd_cmipci_line_in_mode_info(snd_kcontrol_t *kcontrol,
  2007. snd_ctl_elem_info_t *uinfo)
  2008. {
  2009. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  2010. static char *texts[3] = { "Line-In", "Rear Output", "Bass Output" };
  2011. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2012. uinfo->count = 1;
  2013. uinfo->value.enumerated.items = cm->chip_version >= 39 ? 3 : 2;
  2014. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2015. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2016. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2017. return 0;
  2018. }
  2019. static inline unsigned int get_line_in_mode(cmipci_t *cm)
  2020. {
  2021. unsigned int val;
  2022. if (cm->chip_version >= 39) {
  2023. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL);
  2024. if (val & CM_LINE_AS_BASS)
  2025. return 2;
  2026. }
  2027. val = snd_cmipci_read_b(cm, CM_REG_MIXER1);
  2028. if (val & CM_SPK4)
  2029. return 1;
  2030. return 0;
  2031. }
  2032. static int snd_cmipci_line_in_mode_get(snd_kcontrol_t *kcontrol,
  2033. snd_ctl_elem_value_t *ucontrol)
  2034. {
  2035. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  2036. spin_lock_irq(&cm->reg_lock);
  2037. ucontrol->value.enumerated.item[0] = get_line_in_mode(cm);
  2038. spin_unlock_irq(&cm->reg_lock);
  2039. return 0;
  2040. }
  2041. static int snd_cmipci_line_in_mode_put(snd_kcontrol_t *kcontrol,
  2042. snd_ctl_elem_value_t *ucontrol)
  2043. {
  2044. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  2045. int change;
  2046. spin_lock_irq(&cm->reg_lock);
  2047. if (ucontrol->value.enumerated.item[0] == 2)
  2048. change = snd_cmipci_set_bit(cm, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS);
  2049. else
  2050. change = snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_LINE_AS_BASS);
  2051. if (ucontrol->value.enumerated.item[0] == 1)
  2052. change |= snd_cmipci_set_bit_b(cm, CM_REG_MIXER1, CM_SPK4);
  2053. else
  2054. change |= snd_cmipci_clear_bit_b(cm, CM_REG_MIXER1, CM_SPK4);
  2055. spin_unlock_irq(&cm->reg_lock);
  2056. return change;
  2057. }
  2058. static int snd_cmipci_mic_in_mode_info(snd_kcontrol_t *kcontrol,
  2059. snd_ctl_elem_info_t *uinfo)
  2060. {
  2061. static char *texts[2] = { "Mic-In", "Center/LFE Output" };
  2062. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2063. uinfo->count = 1;
  2064. uinfo->value.enumerated.items = 2;
  2065. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  2066. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  2067. strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
  2068. return 0;
  2069. }
  2070. static int snd_cmipci_mic_in_mode_get(snd_kcontrol_t *kcontrol,
  2071. snd_ctl_elem_value_t *ucontrol)
  2072. {
  2073. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  2074. /* same bit as spdi_phase */
  2075. spin_lock_irq(&cm->reg_lock);
  2076. ucontrol->value.enumerated.item[0] =
  2077. (snd_cmipci_read_b(cm, CM_REG_MISC) & CM_SPDIF_INVERSE) ? 1 : 0;
  2078. spin_unlock_irq(&cm->reg_lock);
  2079. return 0;
  2080. }
  2081. static int snd_cmipci_mic_in_mode_put(snd_kcontrol_t *kcontrol,
  2082. snd_ctl_elem_value_t *ucontrol)
  2083. {
  2084. cmipci_t *cm = snd_kcontrol_chip(kcontrol);
  2085. int change;
  2086. spin_lock_irq(&cm->reg_lock);
  2087. if (ucontrol->value.enumerated.item[0])
  2088. change = snd_cmipci_set_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2089. else
  2090. change = snd_cmipci_clear_bit_b(cm, CM_REG_MISC, CM_SPDIF_INVERSE);
  2091. spin_unlock_irq(&cm->reg_lock);
  2092. return change;
  2093. }
  2094. /* both for CM8338/8738 */
  2095. static snd_kcontrol_new_t snd_cmipci_mixer_switches[] __devinitdata = {
  2096. DEFINE_MIXER_SWITCH("Four Channel Mode", fourch),
  2097. {
  2098. .name = "Line-In Mode",
  2099. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2100. .info = snd_cmipci_line_in_mode_info,
  2101. .get = snd_cmipci_line_in_mode_get,
  2102. .put = snd_cmipci_line_in_mode_put,
  2103. },
  2104. };
  2105. /* for non-multichannel chips */
  2106. static snd_kcontrol_new_t snd_cmipci_nomulti_switch __devinitdata =
  2107. DEFINE_MIXER_SWITCH("Exchange DAC", exchange_dac);
  2108. /* only for CM8738 */
  2109. static snd_kcontrol_new_t snd_cmipci_8738_mixer_switches[] __devinitdata = {
  2110. #if 0 /* controlled in pcm device */
  2111. DEFINE_MIXER_SWITCH("IEC958 In Record", spdif_in),
  2112. DEFINE_MIXER_SWITCH("IEC958 Out", spdif_out),
  2113. DEFINE_MIXER_SWITCH("IEC958 Out To DAC", spdo2dac),
  2114. #endif
  2115. // DEFINE_MIXER_SWITCH("IEC958 Output Switch", spdif_enable),
  2116. { .name = "IEC958 Output Switch",
  2117. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2118. .info = snd_cmipci_uswitch_info,
  2119. .get = snd_cmipci_spdout_enable_get,
  2120. .put = snd_cmipci_spdout_enable_put,
  2121. },
  2122. DEFINE_MIXER_SWITCH("IEC958 In Valid", spdi_valid),
  2123. DEFINE_MIXER_SWITCH("IEC958 Copyright", spdif_copyright),
  2124. DEFINE_MIXER_SWITCH("IEC958 5V", spdo_5v),
  2125. // DEFINE_MIXER_SWITCH("IEC958 In/Out 48KHz", spdo_48k),
  2126. DEFINE_MIXER_SWITCH("IEC958 Loop", spdif_loop),
  2127. DEFINE_MIXER_SWITCH("IEC958 In Monitor", spdi_monitor),
  2128. };
  2129. /* only for model 033/037 */
  2130. static snd_kcontrol_new_t snd_cmipci_old_mixer_switches[] __devinitdata = {
  2131. DEFINE_MIXER_SWITCH("IEC958 Mix Analog", spdif_dac_out),
  2132. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase),
  2133. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel1),
  2134. };
  2135. /* only for model 039 or later */
  2136. static snd_kcontrol_new_t snd_cmipci_extra_mixer_switches[] __devinitdata = {
  2137. DEFINE_MIXER_SWITCH("IEC958 In Select", spdif_in_sel2),
  2138. DEFINE_MIXER_SWITCH("IEC958 In Phase Inverse", spdi_phase2),
  2139. {
  2140. .name = "Mic-In Mode",
  2141. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2142. .info = snd_cmipci_mic_in_mode_info,
  2143. .get = snd_cmipci_mic_in_mode_get,
  2144. .put = snd_cmipci_mic_in_mode_put,
  2145. }
  2146. };
  2147. /* card control switches */
  2148. static snd_kcontrol_new_t snd_cmipci_control_switches[] __devinitdata = {
  2149. // DEFINE_CARD_SWITCH("Joystick", joystick), /* now module option */
  2150. DEFINE_CARD_SWITCH("Modem", modem),
  2151. };
  2152. static int __devinit snd_cmipci_mixer_new(cmipci_t *cm, int pcm_spdif_device)
  2153. {
  2154. snd_card_t *card;
  2155. snd_kcontrol_new_t *sw;
  2156. snd_kcontrol_t *kctl;
  2157. unsigned int idx;
  2158. int err;
  2159. snd_assert(cm != NULL && cm->card != NULL, return -EINVAL);
  2160. card = cm->card;
  2161. strcpy(card->mixername, "CMedia PCI");
  2162. spin_lock_irq(&cm->reg_lock);
  2163. snd_cmipci_mixer_write(cm, 0x00, 0x00); /* mixer reset */
  2164. spin_unlock_irq(&cm->reg_lock);
  2165. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixers); idx++) {
  2166. if (cm->chip_version == 68) { // 8768 has no PCM volume
  2167. if (!strcmp(snd_cmipci_mixers[idx].name,
  2168. "PCM Playback Volume"))
  2169. continue;
  2170. }
  2171. if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_cmipci_mixers[idx], cm))) < 0)
  2172. return err;
  2173. }
  2174. /* mixer switches */
  2175. sw = snd_cmipci_mixer_switches;
  2176. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_mixer_switches); idx++, sw++) {
  2177. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2178. if (err < 0)
  2179. return err;
  2180. }
  2181. if (! cm->can_multi_ch) {
  2182. err = snd_ctl_add(cm->card, snd_ctl_new1(&snd_cmipci_nomulti_switch, cm));
  2183. if (err < 0)
  2184. return err;
  2185. }
  2186. if (cm->device == PCI_DEVICE_ID_CMEDIA_CM8738 ||
  2187. cm->device == PCI_DEVICE_ID_CMEDIA_CM8738B) {
  2188. sw = snd_cmipci_8738_mixer_switches;
  2189. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_8738_mixer_switches); idx++, sw++) {
  2190. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2191. if (err < 0)
  2192. return err;
  2193. }
  2194. if (cm->can_ac3_hw) {
  2195. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_default, cm))) < 0)
  2196. return err;
  2197. kctl->id.device = pcm_spdif_device;
  2198. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_mask, cm))) < 0)
  2199. return err;
  2200. kctl->id.device = pcm_spdif_device;
  2201. if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_cmipci_spdif_stream, cm))) < 0)
  2202. return err;
  2203. kctl->id.device = pcm_spdif_device;
  2204. }
  2205. if (cm->chip_version <= 37) {
  2206. sw = snd_cmipci_old_mixer_switches;
  2207. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_old_mixer_switches); idx++, sw++) {
  2208. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2209. if (err < 0)
  2210. return err;
  2211. }
  2212. }
  2213. }
  2214. if (cm->chip_version >= 39) {
  2215. sw = snd_cmipci_extra_mixer_switches;
  2216. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_extra_mixer_switches); idx++, sw++) {
  2217. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2218. if (err < 0)
  2219. return err;
  2220. }
  2221. }
  2222. /* card switches */
  2223. sw = snd_cmipci_control_switches;
  2224. for (idx = 0; idx < ARRAY_SIZE(snd_cmipci_control_switches); idx++, sw++) {
  2225. err = snd_ctl_add(cm->card, snd_ctl_new1(sw, cm));
  2226. if (err < 0)
  2227. return err;
  2228. }
  2229. for (idx = 0; idx < CM_SAVED_MIXERS; idx++) {
  2230. snd_ctl_elem_id_t id;
  2231. snd_kcontrol_t *ctl;
  2232. memset(&id, 0, sizeof(id));
  2233. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2234. strcpy(id.name, cm_saved_mixer[idx].name);
  2235. if ((ctl = snd_ctl_find_id(cm->card, &id)) != NULL)
  2236. cm->mixer_res_ctl[idx] = ctl;
  2237. }
  2238. return 0;
  2239. }
  2240. /*
  2241. * proc interface
  2242. */
  2243. #ifdef CONFIG_PROC_FS
  2244. static void snd_cmipci_proc_read(snd_info_entry_t *entry,
  2245. snd_info_buffer_t *buffer)
  2246. {
  2247. cmipci_t *cm = entry->private_data;
  2248. int i;
  2249. snd_iprintf(buffer, "%s\n\n", cm->card->longname);
  2250. for (i = 0; i < 0x40; i++) {
  2251. int v = inb(cm->iobase + i);
  2252. if (i % 4 == 0)
  2253. snd_iprintf(buffer, "%02x: ", i);
  2254. snd_iprintf(buffer, "%02x", v);
  2255. if (i % 4 == 3)
  2256. snd_iprintf(buffer, "\n");
  2257. else
  2258. snd_iprintf(buffer, " ");
  2259. }
  2260. }
  2261. static void __devinit snd_cmipci_proc_init(cmipci_t *cm)
  2262. {
  2263. snd_info_entry_t *entry;
  2264. if (! snd_card_proc_new(cm->card, "cmipci", &entry))
  2265. snd_info_set_text_ops(entry, cm, 1024, snd_cmipci_proc_read);
  2266. }
  2267. #else /* !CONFIG_PROC_FS */
  2268. static inline void snd_cmipci_proc_init(cmipci_t *cm) {}
  2269. #endif
  2270. static struct pci_device_id snd_cmipci_ids[] = {
  2271. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338A, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2272. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8338B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2273. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2274. {PCI_VENDOR_ID_CMEDIA, PCI_DEVICE_ID_CMEDIA_CM8738B, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2275. {PCI_VENDOR_ID_AL, PCI_DEVICE_ID_CMEDIA_CM8738, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
  2276. {0,},
  2277. };
  2278. /*
  2279. * check chip version and capabilities
  2280. * driver name is modified according to the chip model
  2281. */
  2282. static void __devinit query_chip(cmipci_t *cm)
  2283. {
  2284. unsigned int detect;
  2285. /* check reg 0Ch, bit 24-31 */
  2286. detect = snd_cmipci_read(cm, CM_REG_INT_HLDCLR) & CM_CHIP_MASK2;
  2287. if (! detect) {
  2288. /* check reg 08h, bit 24-28 */
  2289. detect = snd_cmipci_read(cm, CM_REG_CHFORMAT) & CM_CHIP_MASK1;
  2290. if (! detect) {
  2291. cm->chip_version = 33;
  2292. cm->max_channels = 2;
  2293. if (cm->do_soft_ac3)
  2294. cm->can_ac3_sw = 1;
  2295. else
  2296. cm->can_ac3_hw = 1;
  2297. cm->has_dual_dac = 1;
  2298. } else {
  2299. cm->chip_version = 37;
  2300. cm->max_channels = 2;
  2301. cm->can_ac3_hw = 1;
  2302. cm->has_dual_dac = 1;
  2303. }
  2304. } else {
  2305. /* check reg 0Ch, bit 26 */
  2306. if (detect & CM_CHIP_8768) {
  2307. cm->chip_version = 68;
  2308. cm->max_channels = 8;
  2309. cm->can_ac3_hw = 1;
  2310. cm->has_dual_dac = 1;
  2311. cm->can_multi_ch = 1;
  2312. } else if (detect & CM_CHIP_055) {
  2313. cm->chip_version = 55;
  2314. cm->max_channels = 6;
  2315. cm->can_ac3_hw = 1;
  2316. cm->has_dual_dac = 1;
  2317. cm->can_multi_ch = 1;
  2318. } else if (detect & CM_CHIP_039) {
  2319. cm->chip_version = 39;
  2320. if (detect & CM_CHIP_039_6CH) /* 4 or 6 channels */
  2321. cm->max_channels = 6;
  2322. else
  2323. cm->max_channels = 4;
  2324. cm->can_ac3_hw = 1;
  2325. cm->has_dual_dac = 1;
  2326. cm->can_multi_ch = 1;
  2327. } else {
  2328. printk(KERN_ERR "chip %x version not supported\n", detect);
  2329. }
  2330. }
  2331. }
  2332. #ifdef SUPPORT_JOYSTICK
  2333. static int __devinit snd_cmipci_create_gameport(cmipci_t *cm, int dev)
  2334. {
  2335. static int ports[] = { 0x201, 0x200, 0 }; /* FIXME: majority is 0x201? */
  2336. struct gameport *gp;
  2337. struct resource *r = NULL;
  2338. int i, io_port = 0;
  2339. if (joystick_port[dev] == 0)
  2340. return -ENODEV;
  2341. if (joystick_port[dev] == 1) { /* auto-detect */
  2342. for (i = 0; ports[i]; i++) {
  2343. io_port = ports[i];
  2344. r = request_region(io_port, 1, "CMIPCI gameport");
  2345. if (r)
  2346. break;
  2347. }
  2348. } else {
  2349. io_port = joystick_port[dev];
  2350. r = request_region(io_port, 1, "CMIPCI gameport");
  2351. }
  2352. if (!r) {
  2353. printk(KERN_WARNING "cmipci: cannot reserve joystick ports\n");
  2354. return -EBUSY;
  2355. }
  2356. cm->gameport = gp = gameport_allocate_port();
  2357. if (!gp) {
  2358. printk(KERN_ERR "cmipci: cannot allocate memory for gameport\n");
  2359. release_and_free_resource(r);
  2360. return -ENOMEM;
  2361. }
  2362. gameport_set_name(gp, "C-Media Gameport");
  2363. gameport_set_phys(gp, "pci%s/gameport0", pci_name(cm->pci));
  2364. gameport_set_dev_parent(gp, &cm->pci->dev);
  2365. gp->io = io_port;
  2366. gameport_set_port_data(gp, r);
  2367. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2368. gameport_register_port(cm->gameport);
  2369. return 0;
  2370. }
  2371. static void snd_cmipci_free_gameport(cmipci_t *cm)
  2372. {
  2373. if (cm->gameport) {
  2374. struct resource *r = gameport_get_port_data(cm->gameport);
  2375. gameport_unregister_port(cm->gameport);
  2376. cm->gameport = NULL;
  2377. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2378. release_and_free_resource(r);
  2379. }
  2380. }
  2381. #else
  2382. static inline int snd_cmipci_create_gameport(cmipci_t *cm, int dev) { return -ENOSYS; }
  2383. static inline void snd_cmipci_free_gameport(cmipci_t *cm) { }
  2384. #endif
  2385. static int snd_cmipci_free(cmipci_t *cm)
  2386. {
  2387. if (cm->irq >= 0) {
  2388. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2389. snd_cmipci_clear_bit(cm, CM_REG_LEGACY_CTRL, CM_ENSPDOUT);
  2390. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2391. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2392. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2393. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2394. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2395. /* reset mixer */
  2396. snd_cmipci_mixer_write(cm, 0, 0);
  2397. synchronize_irq(cm->irq);
  2398. free_irq(cm->irq, (void *)cm);
  2399. }
  2400. snd_cmipci_free_gameport(cm);
  2401. pci_release_regions(cm->pci);
  2402. pci_disable_device(cm->pci);
  2403. kfree(cm);
  2404. return 0;
  2405. }
  2406. static int snd_cmipci_dev_free(snd_device_t *device)
  2407. {
  2408. cmipci_t *cm = device->device_data;
  2409. return snd_cmipci_free(cm);
  2410. }
  2411. static int __devinit snd_cmipci_create_fm(cmipci_t *cm, long fm_port)
  2412. {
  2413. long iosynth;
  2414. unsigned int val;
  2415. opl3_t *opl3;
  2416. int err;
  2417. /* first try FM regs in PCI port range */
  2418. iosynth = cm->iobase + CM_REG_FM_PCI;
  2419. err = snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2420. OPL3_HW_OPL3, 1, &opl3);
  2421. if (err < 0) {
  2422. /* then try legacy ports */
  2423. val = snd_cmipci_read(cm, CM_REG_LEGACY_CTRL) & ~CM_FMSEL_MASK;
  2424. iosynth = fm_port;
  2425. switch (iosynth) {
  2426. case 0x3E8: val |= CM_FMSEL_3E8; break;
  2427. case 0x3E0: val |= CM_FMSEL_3E0; break;
  2428. case 0x3C8: val |= CM_FMSEL_3C8; break;
  2429. case 0x388: val |= CM_FMSEL_388; break;
  2430. default:
  2431. return 0;
  2432. }
  2433. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2434. /* enable FM */
  2435. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2436. if (snd_opl3_create(cm->card, iosynth, iosynth + 2,
  2437. OPL3_HW_OPL3, 0, &opl3) < 0) {
  2438. printk(KERN_ERR "cmipci: no OPL device at %#lx, "
  2439. "skipping...\n", iosynth);
  2440. /* disable FM */
  2441. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL,
  2442. val & ~CM_FMSEL_MASK);
  2443. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_FM_EN);
  2444. return 0;
  2445. }
  2446. }
  2447. if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  2448. printk(KERN_ERR "cmipci: cannot create OPL3 hwdep\n");
  2449. return err;
  2450. }
  2451. return 0;
  2452. }
  2453. static int __devinit snd_cmipci_create(snd_card_t *card, struct pci_dev *pci,
  2454. int dev, cmipci_t **rcmipci)
  2455. {
  2456. cmipci_t *cm;
  2457. int err;
  2458. static snd_device_ops_t ops = {
  2459. .dev_free = snd_cmipci_dev_free,
  2460. };
  2461. unsigned int val = 0;
  2462. long iomidi;
  2463. int integrated_midi;
  2464. int pcm_index, pcm_spdif_index;
  2465. static struct pci_device_id intel_82437vx[] = {
  2466. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437VX) },
  2467. { },
  2468. };
  2469. *rcmipci = NULL;
  2470. if ((err = pci_enable_device(pci)) < 0)
  2471. return err;
  2472. cm = kzalloc(sizeof(*cm), GFP_KERNEL);
  2473. if (cm == NULL) {
  2474. pci_disable_device(pci);
  2475. return -ENOMEM;
  2476. }
  2477. spin_lock_init(&cm->reg_lock);
  2478. init_MUTEX(&cm->open_mutex);
  2479. cm->device = pci->device;
  2480. cm->card = card;
  2481. cm->pci = pci;
  2482. cm->irq = -1;
  2483. cm->channel[0].ch = 0;
  2484. cm->channel[1].ch = 1;
  2485. cm->channel[0].is_dac = cm->channel[1].is_dac = 1; /* dual DAC mode */
  2486. if ((err = pci_request_regions(pci, card->driver)) < 0) {
  2487. kfree(cm);
  2488. pci_disable_device(pci);
  2489. return err;
  2490. }
  2491. cm->iobase = pci_resource_start(pci, 0);
  2492. if (request_irq(pci->irq, snd_cmipci_interrupt, SA_INTERRUPT|SA_SHIRQ, card->driver, (void *)cm)) {
  2493. snd_printk(KERN_ERR "unable to grab IRQ %d\n", pci->irq);
  2494. snd_cmipci_free(cm);
  2495. return -EBUSY;
  2496. }
  2497. cm->irq = pci->irq;
  2498. pci_set_master(cm->pci);
  2499. /*
  2500. * check chip version, max channels and capabilities
  2501. */
  2502. cm->chip_version = 0;
  2503. cm->max_channels = 2;
  2504. cm->do_soft_ac3 = soft_ac3[dev];
  2505. if (pci->device != PCI_DEVICE_ID_CMEDIA_CM8338A &&
  2506. pci->device != PCI_DEVICE_ID_CMEDIA_CM8338B)
  2507. query_chip(cm);
  2508. /* added -MCx suffix for chip supporting multi-channels */
  2509. if (cm->can_multi_ch)
  2510. sprintf(cm->card->driver + strlen(cm->card->driver),
  2511. "-MC%d", cm->max_channels);
  2512. else if (cm->can_ac3_sw)
  2513. strcpy(cm->card->driver + strlen(cm->card->driver), "-SWIEC");
  2514. cm->dig_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2515. cm->dig_pcm_status = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2516. #if CM_CH_PLAY == 1
  2517. cm->ctrl = CM_CHADC0; /* default FUNCNTRL0 */
  2518. #else
  2519. cm->ctrl = CM_CHADC1; /* default FUNCNTRL0 */
  2520. #endif
  2521. /* initialize codec registers */
  2522. snd_cmipci_write(cm, CM_REG_INT_HLDCLR, 0); /* disable ints */
  2523. snd_cmipci_ch_reset(cm, CM_CH_PLAY);
  2524. snd_cmipci_ch_reset(cm, CM_CH_CAPT);
  2525. snd_cmipci_write(cm, CM_REG_FUNCTRL0, 0); /* disable channels */
  2526. snd_cmipci_write(cm, CM_REG_FUNCTRL1, 0);
  2527. snd_cmipci_write(cm, CM_REG_CHFORMAT, 0);
  2528. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_ENDBDAC|CM_N4SPK3D);
  2529. #if CM_CH_PLAY == 1
  2530. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2531. #else
  2532. snd_cmipci_clear_bit(cm, CM_REG_MISC_CTRL, CM_XCHGDAC);
  2533. #endif
  2534. /* Set Bus Master Request */
  2535. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_BREQ);
  2536. /* Assume TX and compatible chip set (Autodetection required for VX chip sets) */
  2537. switch (pci->device) {
  2538. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2539. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2540. if (!pci_dev_present(intel_82437vx))
  2541. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_TXVX);
  2542. break;
  2543. default:
  2544. break;
  2545. }
  2546. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, cm, &ops)) < 0) {
  2547. snd_cmipci_free(cm);
  2548. return err;
  2549. }
  2550. integrated_midi = snd_cmipci_read_b(cm, CM_REG_MPU_PCI) != 0xff;
  2551. if (integrated_midi)
  2552. iomidi = cm->iobase + CM_REG_MPU_PCI;
  2553. else {
  2554. iomidi = mpu_port[dev];
  2555. switch (iomidi) {
  2556. case 0x320: val = CM_VMPU_320; break;
  2557. case 0x310: val = CM_VMPU_310; break;
  2558. case 0x300: val = CM_VMPU_300; break;
  2559. case 0x330: val = CM_VMPU_330; break;
  2560. default:
  2561. iomidi = 0; break;
  2562. }
  2563. if (iomidi > 0) {
  2564. snd_cmipci_write(cm, CM_REG_LEGACY_CTRL, val);
  2565. /* enable UART */
  2566. snd_cmipci_set_bit(cm, CM_REG_FUNCTRL1, CM_UART_EN);
  2567. }
  2568. }
  2569. if ((err = snd_cmipci_create_fm(cm, fm_port[dev])) < 0)
  2570. return err;
  2571. /* reset mixer */
  2572. snd_cmipci_mixer_write(cm, 0, 0);
  2573. snd_cmipci_proc_init(cm);
  2574. /* create pcm devices */
  2575. pcm_index = pcm_spdif_index = 0;
  2576. if ((err = snd_cmipci_pcm_new(cm, pcm_index)) < 0)
  2577. return err;
  2578. pcm_index++;
  2579. if (cm->has_dual_dac) {
  2580. if ((err = snd_cmipci_pcm2_new(cm, pcm_index)) < 0)
  2581. return err;
  2582. pcm_index++;
  2583. }
  2584. if (cm->can_ac3_hw || cm->can_ac3_sw) {
  2585. pcm_spdif_index = pcm_index;
  2586. if ((err = snd_cmipci_pcm_spdif_new(cm, pcm_index)) < 0)
  2587. return err;
  2588. }
  2589. /* create mixer interface & switches */
  2590. if ((err = snd_cmipci_mixer_new(cm, pcm_spdif_index)) < 0)
  2591. return err;
  2592. if (iomidi > 0) {
  2593. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_CMIPCI,
  2594. iomidi, integrated_midi,
  2595. cm->irq, 0, &cm->rmidi)) < 0) {
  2596. printk(KERN_ERR "cmipci: no UART401 device at 0x%lx\n", iomidi);
  2597. }
  2598. }
  2599. #ifdef USE_VAR48KRATE
  2600. for (val = 0; val < ARRAY_SIZE(rates); val++)
  2601. snd_cmipci_set_pll(cm, rates[val], val);
  2602. /*
  2603. * (Re-)Enable external switch spdo_48k
  2604. */
  2605. snd_cmipci_set_bit(cm, CM_REG_MISC_CTRL, CM_SPDIF48K|CM_SPDF_AC97);
  2606. #endif /* USE_VAR48KRATE */
  2607. if (snd_cmipci_create_gameport(cm, dev) < 0)
  2608. snd_cmipci_clear_bit(cm, CM_REG_FUNCTRL1, CM_JYSTK_EN);
  2609. snd_card_set_dev(card, &pci->dev);
  2610. *rcmipci = cm;
  2611. return 0;
  2612. }
  2613. /*
  2614. */
  2615. MODULE_DEVICE_TABLE(pci, snd_cmipci_ids);
  2616. static int __devinit snd_cmipci_probe(struct pci_dev *pci,
  2617. const struct pci_device_id *pci_id)
  2618. {
  2619. static int dev;
  2620. snd_card_t *card;
  2621. cmipci_t *cm;
  2622. int err;
  2623. if (dev >= SNDRV_CARDS)
  2624. return -ENODEV;
  2625. if (! enable[dev]) {
  2626. dev++;
  2627. return -ENOENT;
  2628. }
  2629. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  2630. if (card == NULL)
  2631. return -ENOMEM;
  2632. switch (pci->device) {
  2633. case PCI_DEVICE_ID_CMEDIA_CM8738:
  2634. case PCI_DEVICE_ID_CMEDIA_CM8738B:
  2635. strcpy(card->driver, "CMI8738");
  2636. break;
  2637. case PCI_DEVICE_ID_CMEDIA_CM8338A:
  2638. case PCI_DEVICE_ID_CMEDIA_CM8338B:
  2639. strcpy(card->driver, "CMI8338");
  2640. break;
  2641. default:
  2642. strcpy(card->driver, "CMIPCI");
  2643. break;
  2644. }
  2645. if ((err = snd_cmipci_create(card, pci, dev, &cm)) < 0) {
  2646. snd_card_free(card);
  2647. return err;
  2648. }
  2649. sprintf(card->shortname, "C-Media PCI %s", card->driver);
  2650. sprintf(card->longname, "%s (model %d) at 0x%lx, irq %i",
  2651. card->shortname,
  2652. cm->chip_version,
  2653. cm->iobase,
  2654. cm->irq);
  2655. //snd_printd("%s is detected\n", card->longname);
  2656. if ((err = snd_card_register(card)) < 0) {
  2657. snd_card_free(card);
  2658. return err;
  2659. }
  2660. pci_set_drvdata(pci, card);
  2661. dev++;
  2662. return 0;
  2663. }
  2664. static void __devexit snd_cmipci_remove(struct pci_dev *pci)
  2665. {
  2666. snd_card_free(pci_get_drvdata(pci));
  2667. pci_set_drvdata(pci, NULL);
  2668. }
  2669. static struct pci_driver driver = {
  2670. .name = "C-Media PCI",
  2671. .id_table = snd_cmipci_ids,
  2672. .probe = snd_cmipci_probe,
  2673. .remove = __devexit_p(snd_cmipci_remove),
  2674. };
  2675. static int __init alsa_card_cmipci_init(void)
  2676. {
  2677. return pci_register_driver(&driver);
  2678. }
  2679. static void __exit alsa_card_cmipci_exit(void)
  2680. {
  2681. pci_unregister_driver(&driver);
  2682. }
  2683. module_init(alsa_card_cmipci_init)
  2684. module_exit(alsa_card_cmipci_exit)