cmipci.c 95 KB

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