cmipci.c 99 KB

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