cmipci.c 98 KB

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