cs46xx_lib.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  3. * Abramo Bagnara <abramo@alsa-project.org>
  4. * Cirrus Logic, Inc.
  5. * Routines for control of Cirrus Logic CS461x chips
  6. *
  7. * KNOWN BUGS:
  8. * - Sometimes the SPDIF input DSP tasks get's unsynchronized
  9. * and the SPDIF get somewhat "distorcionated", or/and left right channel
  10. * are swapped. To get around this problem when it happens, mute and unmute
  11. * the SPDIF input mixer control.
  12. * - On the Hercules Game Theater XP the amplifier are sometimes turned
  13. * off on inadecuate moments which causes distorcions on sound.
  14. *
  15. * TODO:
  16. * - Secondary CODEC on some soundcards
  17. * - SPDIF input support for other sample rates then 48khz
  18. * - Posibility to mix the SPDIF output with analog sources.
  19. * - PCM channels for Center and LFE on secondary codec
  20. *
  21. * NOTE: with CONFIG_SND_CS46XX_NEW_DSP unset uses old DSP image (which
  22. * is default configuration), no SPDIF, no secondary codec, no
  23. * multi channel PCM. But known to work.
  24. *
  25. * FINALLY: A credit to the developers Tom and Jordan
  26. * at Cirrus for have helping me out with the DSP, however we
  27. * still don't have sufficient documentation and technical
  28. * references to be able to implement all fancy feutures
  29. * supported by the cs46xx DSP's.
  30. * Benny <benny@hostmobility.com>
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  45. *
  46. */
  47. #include <linux/delay.h>
  48. #include <linux/pci.h>
  49. #include <linux/pm.h>
  50. #include <linux/init.h>
  51. #include <linux/interrupt.h>
  52. #include <linux/slab.h>
  53. #include <linux/gameport.h>
  54. #include <linux/mutex.h>
  55. #include <linux/export.h>
  56. #include <linux/module.h>
  57. #include <linux/firmware.h>
  58. #include <linux/vmalloc.h>
  59. #include <linux/io.h>
  60. #include <sound/core.h>
  61. #include <sound/control.h>
  62. #include <sound/info.h>
  63. #include <sound/pcm.h>
  64. #include <sound/pcm_params.h>
  65. #include "cs46xx.h"
  66. #include "cs46xx_lib.h"
  67. #include "dsp_spos.h"
  68. static void amp_voyetra(struct snd_cs46xx *chip, int change);
  69. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  70. static const struct snd_pcm_ops snd_cs46xx_playback_rear_ops;
  71. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops;
  72. static const struct snd_pcm_ops snd_cs46xx_playback_clfe_ops;
  73. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops;
  74. static const struct snd_pcm_ops snd_cs46xx_playback_iec958_ops;
  75. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops;
  76. #endif
  77. static const struct snd_pcm_ops snd_cs46xx_playback_ops;
  78. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_ops;
  79. static const struct snd_pcm_ops snd_cs46xx_capture_ops;
  80. static const struct snd_pcm_ops snd_cs46xx_capture_indirect_ops;
  81. static unsigned short snd_cs46xx_codec_read(struct snd_cs46xx *chip,
  82. unsigned short reg,
  83. int codec_index)
  84. {
  85. int count;
  86. unsigned short result,tmp;
  87. u32 offset = 0;
  88. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  89. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  90. return 0xffff;
  91. chip->active_ctrl(chip, 1);
  92. if (codec_index == CS46XX_SECONDARY_CODEC_INDEX)
  93. offset = CS46XX_SECONDARY_CODEC_OFFSET;
  94. /*
  95. * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
  96. * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
  97. * 3. Write ACCTL = Control Register = 460h for initiating the write7---55
  98. * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 17h
  99. * 5. if DCV not cleared, break and return error
  100. * 6. Read ACSTS = Status Register = 464h, check VSTS bit
  101. */
  102. snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
  103. tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL);
  104. if ((tmp & ACCTL_VFRM) == 0) {
  105. dev_warn(chip->card->dev, "ACCTL_VFRM not set 0x%x\n", tmp);
  106. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, (tmp & (~ACCTL_ESYN)) | ACCTL_VFRM );
  107. msleep(50);
  108. tmp = snd_cs46xx_peekBA0(chip, BA0_ACCTL + offset);
  109. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, tmp | ACCTL_ESYN | ACCTL_VFRM );
  110. }
  111. /*
  112. * Setup the AC97 control registers on the CS461x to send the
  113. * appropriate command to the AC97 to perform the read.
  114. * ACCAD = Command Address Register = 46Ch
  115. * ACCDA = Command Data Register = 470h
  116. * ACCTL = Control Register = 460h
  117. * set DCV - will clear when process completed
  118. * set CRW - Read command
  119. * set VFRM - valid frame enabled
  120. * set ESYN - ASYNC generation enabled
  121. * set RSTN - ARST# inactive, AC97 codec not reset
  122. */
  123. snd_cs46xx_pokeBA0(chip, BA0_ACCAD, reg);
  124. snd_cs46xx_pokeBA0(chip, BA0_ACCDA, 0);
  125. if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
  126. snd_cs46xx_pokeBA0(chip, BA0_ACCTL,/* clear ACCTL_DCV */ ACCTL_CRW |
  127. ACCTL_VFRM | ACCTL_ESYN |
  128. ACCTL_RSTN);
  129. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_CRW |
  130. ACCTL_VFRM | ACCTL_ESYN |
  131. ACCTL_RSTN);
  132. } else {
  133. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
  134. ACCTL_CRW | ACCTL_VFRM | ACCTL_ESYN |
  135. ACCTL_RSTN);
  136. }
  137. /*
  138. * Wait for the read to occur.
  139. */
  140. for (count = 0; count < 1000; count++) {
  141. /*
  142. * First, we want to wait for a short time.
  143. */
  144. udelay(10);
  145. /*
  146. * Now, check to see if the read has completed.
  147. * ACCTL = 460h, DCV should be reset by now and 460h = 17h
  148. */
  149. if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV))
  150. goto ok1;
  151. }
  152. dev_err(chip->card->dev,
  153. "AC'97 read problem (ACCTL_DCV), reg = 0x%x\n", reg);
  154. result = 0xffff;
  155. goto end;
  156. ok1:
  157. /*
  158. * Wait for the valid status bit to go active.
  159. */
  160. for (count = 0; count < 100; count++) {
  161. /*
  162. * Read the AC97 status register.
  163. * ACSTS = Status Register = 464h
  164. * VSTS - Valid Status
  165. */
  166. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS + offset) & ACSTS_VSTS)
  167. goto ok2;
  168. udelay(10);
  169. }
  170. dev_err(chip->card->dev,
  171. "AC'97 read problem (ACSTS_VSTS), codec_index %d, reg = 0x%x\n",
  172. codec_index, reg);
  173. result = 0xffff;
  174. goto end;
  175. ok2:
  176. /*
  177. * Read the data returned from the AC97 register.
  178. * ACSDA = Status Data Register = 474h
  179. */
  180. #if 0
  181. dev_dbg(chip->card->dev,
  182. "e) reg = 0x%x, val = 0x%x, BA0_ACCAD = 0x%x\n", reg,
  183. snd_cs46xx_peekBA0(chip, BA0_ACSDA),
  184. snd_cs46xx_peekBA0(chip, BA0_ACCAD));
  185. #endif
  186. //snd_cs46xx_peekBA0(chip, BA0_ACCAD);
  187. result = snd_cs46xx_peekBA0(chip, BA0_ACSDA + offset);
  188. end:
  189. chip->active_ctrl(chip, -1);
  190. return result;
  191. }
  192. static unsigned short snd_cs46xx_ac97_read(struct snd_ac97 * ac97,
  193. unsigned short reg)
  194. {
  195. struct snd_cs46xx *chip = ac97->private_data;
  196. unsigned short val;
  197. int codec_index = ac97->num;
  198. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  199. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  200. return 0xffff;
  201. val = snd_cs46xx_codec_read(chip, reg, codec_index);
  202. return val;
  203. }
  204. static void snd_cs46xx_codec_write(struct snd_cs46xx *chip,
  205. unsigned short reg,
  206. unsigned short val,
  207. int codec_index)
  208. {
  209. int count;
  210. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  211. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  212. return;
  213. chip->active_ctrl(chip, 1);
  214. /*
  215. * 1. Write ACCAD = Command Address Register = 46Ch for AC97 register address
  216. * 2. Write ACCDA = Command Data Register = 470h for data to write to AC97
  217. * 3. Write ACCTL = Control Register = 460h for initiating the write
  218. * 4. Read ACCTL = 460h, DCV should be reset by now and 460h = 07h
  219. * 5. if DCV not cleared, break and return error
  220. */
  221. /*
  222. * Setup the AC97 control registers on the CS461x to send the
  223. * appropriate command to the AC97 to perform the read.
  224. * ACCAD = Command Address Register = 46Ch
  225. * ACCDA = Command Data Register = 470h
  226. * ACCTL = Control Register = 460h
  227. * set DCV - will clear when process completed
  228. * reset CRW - Write command
  229. * set VFRM - valid frame enabled
  230. * set ESYN - ASYNC generation enabled
  231. * set RSTN - ARST# inactive, AC97 codec not reset
  232. */
  233. snd_cs46xx_pokeBA0(chip, BA0_ACCAD , reg);
  234. snd_cs46xx_pokeBA0(chip, BA0_ACCDA , val);
  235. snd_cs46xx_peekBA0(chip, BA0_ACCTL);
  236. if (codec_index == CS46XX_PRIMARY_CODEC_INDEX) {
  237. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, /* clear ACCTL_DCV */ ACCTL_VFRM |
  238. ACCTL_ESYN | ACCTL_RSTN);
  239. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_VFRM |
  240. ACCTL_ESYN | ACCTL_RSTN);
  241. } else {
  242. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_DCV | ACCTL_TC |
  243. ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  244. }
  245. for (count = 0; count < 4000; count++) {
  246. /*
  247. * First, we want to wait for a short time.
  248. */
  249. udelay(10);
  250. /*
  251. * Now, check to see if the write has completed.
  252. * ACCTL = 460h, DCV should be reset by now and 460h = 07h
  253. */
  254. if (!(snd_cs46xx_peekBA0(chip, BA0_ACCTL) & ACCTL_DCV)) {
  255. goto end;
  256. }
  257. }
  258. dev_err(chip->card->dev,
  259. "AC'97 write problem, codec_index = %d, reg = 0x%x, val = 0x%x\n",
  260. codec_index, reg, val);
  261. end:
  262. chip->active_ctrl(chip, -1);
  263. }
  264. static void snd_cs46xx_ac97_write(struct snd_ac97 *ac97,
  265. unsigned short reg,
  266. unsigned short val)
  267. {
  268. struct snd_cs46xx *chip = ac97->private_data;
  269. int codec_index = ac97->num;
  270. if (snd_BUG_ON(codec_index != CS46XX_PRIMARY_CODEC_INDEX &&
  271. codec_index != CS46XX_SECONDARY_CODEC_INDEX))
  272. return;
  273. snd_cs46xx_codec_write(chip, reg, val, codec_index);
  274. }
  275. /*
  276. * Chip initialization
  277. */
  278. int snd_cs46xx_download(struct snd_cs46xx *chip,
  279. u32 *src,
  280. unsigned long offset,
  281. unsigned long len)
  282. {
  283. void __iomem *dst;
  284. unsigned int bank = offset >> 16;
  285. offset = offset & 0xffff;
  286. if (snd_BUG_ON((offset & 3) || (len & 3)))
  287. return -EINVAL;
  288. dst = chip->region.idx[bank+1].remap_addr + offset;
  289. len /= sizeof(u32);
  290. /* writel already converts 32-bit value to right endianess */
  291. while (len-- > 0) {
  292. writel(*src++, dst);
  293. dst += sizeof(u32);
  294. }
  295. return 0;
  296. }
  297. static inline void memcpy_le32(void *dst, const void *src, unsigned int len)
  298. {
  299. #ifdef __LITTLE_ENDIAN
  300. memcpy(dst, src, len);
  301. #else
  302. u32 *_dst = dst;
  303. const __le32 *_src = src;
  304. len /= 4;
  305. while (len-- > 0)
  306. *_dst++ = le32_to_cpu(*_src++);
  307. #endif
  308. }
  309. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  310. static const char *module_names[CS46XX_DSP_MODULES] = {
  311. "cwc4630", "cwcasync", "cwcsnoop", "cwcbinhack", "cwcdma"
  312. };
  313. MODULE_FIRMWARE("cs46xx/cwc4630");
  314. MODULE_FIRMWARE("cs46xx/cwcasync");
  315. MODULE_FIRMWARE("cs46xx/cwcsnoop");
  316. MODULE_FIRMWARE("cs46xx/cwcbinhack");
  317. MODULE_FIRMWARE("cs46xx/cwcdma");
  318. static void free_module_desc(struct dsp_module_desc *module)
  319. {
  320. if (!module)
  321. return;
  322. kfree(module->module_name);
  323. kfree(module->symbol_table.symbols);
  324. if (module->segments) {
  325. int i;
  326. for (i = 0; i < module->nsegments; i++)
  327. kfree(module->segments[i].data);
  328. kfree(module->segments);
  329. }
  330. kfree(module);
  331. }
  332. /* firmware binary format:
  333. * le32 nsymbols;
  334. * struct {
  335. * le32 address;
  336. * char symbol_name[DSP_MAX_SYMBOL_NAME];
  337. * le32 symbol_type;
  338. * } symbols[nsymbols];
  339. * le32 nsegments;
  340. * struct {
  341. * le32 segment_type;
  342. * le32 offset;
  343. * le32 size;
  344. * le32 data[size];
  345. * } segments[nsegments];
  346. */
  347. static int load_firmware(struct snd_cs46xx *chip,
  348. struct dsp_module_desc **module_ret,
  349. const char *fw_name)
  350. {
  351. int i, err;
  352. unsigned int nums, fwlen, fwsize;
  353. const __le32 *fwdat;
  354. struct dsp_module_desc *module = NULL;
  355. const struct firmware *fw;
  356. char fw_path[32];
  357. sprintf(fw_path, "cs46xx/%s", fw_name);
  358. err = request_firmware(&fw, fw_path, &chip->pci->dev);
  359. if (err < 0)
  360. return err;
  361. fwsize = fw->size / 4;
  362. if (fwsize < 2) {
  363. err = -EINVAL;
  364. goto error;
  365. }
  366. err = -ENOMEM;
  367. module = kzalloc(sizeof(*module), GFP_KERNEL);
  368. if (!module)
  369. goto error;
  370. module->module_name = kstrdup(fw_name, GFP_KERNEL);
  371. if (!module->module_name)
  372. goto error;
  373. fwlen = 0;
  374. fwdat = (const __le32 *)fw->data;
  375. nums = module->symbol_table.nsymbols = le32_to_cpu(fwdat[fwlen++]);
  376. if (nums >= 40)
  377. goto error_inval;
  378. module->symbol_table.symbols =
  379. kcalloc(nums, sizeof(struct dsp_symbol_entry), GFP_KERNEL);
  380. if (!module->symbol_table.symbols)
  381. goto error;
  382. for (i = 0; i < nums; i++) {
  383. struct dsp_symbol_entry *entry =
  384. &module->symbol_table.symbols[i];
  385. if (fwlen + 2 + DSP_MAX_SYMBOL_NAME / 4 > fwsize)
  386. goto error_inval;
  387. entry->address = le32_to_cpu(fwdat[fwlen++]);
  388. memcpy(entry->symbol_name, &fwdat[fwlen], DSP_MAX_SYMBOL_NAME - 1);
  389. fwlen += DSP_MAX_SYMBOL_NAME / 4;
  390. entry->symbol_type = le32_to_cpu(fwdat[fwlen++]);
  391. }
  392. if (fwlen >= fwsize)
  393. goto error_inval;
  394. nums = module->nsegments = le32_to_cpu(fwdat[fwlen++]);
  395. if (nums > 10)
  396. goto error_inval;
  397. module->segments =
  398. kcalloc(nums, sizeof(struct dsp_segment_desc), GFP_KERNEL);
  399. if (!module->segments)
  400. goto error;
  401. for (i = 0; i < nums; i++) {
  402. struct dsp_segment_desc *entry = &module->segments[i];
  403. if (fwlen + 3 > fwsize)
  404. goto error_inval;
  405. entry->segment_type = le32_to_cpu(fwdat[fwlen++]);
  406. entry->offset = le32_to_cpu(fwdat[fwlen++]);
  407. entry->size = le32_to_cpu(fwdat[fwlen++]);
  408. if (fwlen + entry->size > fwsize)
  409. goto error_inval;
  410. entry->data = kmalloc(entry->size * 4, GFP_KERNEL);
  411. if (!entry->data)
  412. goto error;
  413. memcpy_le32(entry->data, &fwdat[fwlen], entry->size * 4);
  414. fwlen += entry->size;
  415. }
  416. *module_ret = module;
  417. release_firmware(fw);
  418. return 0;
  419. error_inval:
  420. err = -EINVAL;
  421. error:
  422. free_module_desc(module);
  423. release_firmware(fw);
  424. return err;
  425. }
  426. int snd_cs46xx_clear_BA1(struct snd_cs46xx *chip,
  427. unsigned long offset,
  428. unsigned long len)
  429. {
  430. void __iomem *dst;
  431. unsigned int bank = offset >> 16;
  432. offset = offset & 0xffff;
  433. if (snd_BUG_ON((offset & 3) || (len & 3)))
  434. return -EINVAL;
  435. dst = chip->region.idx[bank+1].remap_addr + offset;
  436. len /= sizeof(u32);
  437. /* writel already converts 32-bit value to right endianess */
  438. while (len-- > 0) {
  439. writel(0, dst);
  440. dst += sizeof(u32);
  441. }
  442. return 0;
  443. }
  444. #else /* old DSP image */
  445. struct ba1_struct {
  446. struct {
  447. u32 offset;
  448. u32 size;
  449. } memory[BA1_MEMORY_COUNT];
  450. u32 map[BA1_DWORD_SIZE];
  451. };
  452. MODULE_FIRMWARE("cs46xx/ba1");
  453. static int load_firmware(struct snd_cs46xx *chip)
  454. {
  455. const struct firmware *fw;
  456. int i, size, err;
  457. err = request_firmware(&fw, "cs46xx/ba1", &chip->pci->dev);
  458. if (err < 0)
  459. return err;
  460. if (fw->size != sizeof(*chip->ba1)) {
  461. err = -EINVAL;
  462. goto error;
  463. }
  464. chip->ba1 = vmalloc(sizeof(*chip->ba1));
  465. if (!chip->ba1) {
  466. err = -ENOMEM;
  467. goto error;
  468. }
  469. memcpy_le32(chip->ba1, fw->data, sizeof(*chip->ba1));
  470. /* sanity check */
  471. size = 0;
  472. for (i = 0; i < BA1_MEMORY_COUNT; i++)
  473. size += chip->ba1->memory[i].size;
  474. if (size > BA1_DWORD_SIZE * 4)
  475. err = -EINVAL;
  476. error:
  477. release_firmware(fw);
  478. return err;
  479. }
  480. int snd_cs46xx_download_image(struct snd_cs46xx *chip)
  481. {
  482. int idx, err;
  483. unsigned int offset = 0;
  484. struct ba1_struct *ba1 = chip->ba1;
  485. for (idx = 0; idx < BA1_MEMORY_COUNT; idx++) {
  486. err = snd_cs46xx_download(chip,
  487. &ba1->map[offset],
  488. ba1->memory[idx].offset,
  489. ba1->memory[idx].size);
  490. if (err < 0)
  491. return err;
  492. offset += ba1->memory[idx].size >> 2;
  493. }
  494. return 0;
  495. }
  496. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  497. /*
  498. * Chip reset
  499. */
  500. static void snd_cs46xx_reset(struct snd_cs46xx *chip)
  501. {
  502. int idx;
  503. /*
  504. * Write the reset bit of the SP control register.
  505. */
  506. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RSTSP);
  507. /*
  508. * Write the control register.
  509. */
  510. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_DRQEN);
  511. /*
  512. * Clear the trap registers.
  513. */
  514. for (idx = 0; idx < 8; idx++) {
  515. snd_cs46xx_poke(chip, BA1_DREG, DREG_REGID_TRAP_SELECT + idx);
  516. snd_cs46xx_poke(chip, BA1_TWPR, 0xFFFF);
  517. }
  518. snd_cs46xx_poke(chip, BA1_DREG, 0);
  519. /*
  520. * Set the frame timer to reflect the number of cycles per frame.
  521. */
  522. snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
  523. }
  524. static int cs46xx_wait_for_fifo(struct snd_cs46xx * chip,int retry_timeout)
  525. {
  526. u32 i, status = 0;
  527. /*
  528. * Make sure the previous FIFO write operation has completed.
  529. */
  530. for(i = 0; i < 50; i++){
  531. status = snd_cs46xx_peekBA0(chip, BA0_SERBST);
  532. if( !(status & SERBST_WBSY) )
  533. break;
  534. mdelay(retry_timeout);
  535. }
  536. if(status & SERBST_WBSY) {
  537. dev_err(chip->card->dev,
  538. "failure waiting for FIFO command to complete\n");
  539. return -EINVAL;
  540. }
  541. return 0;
  542. }
  543. static void snd_cs46xx_clear_serial_FIFOs(struct snd_cs46xx *chip)
  544. {
  545. int idx, powerdown = 0;
  546. unsigned int tmp;
  547. /*
  548. * See if the devices are powered down. If so, we must power them up first
  549. * or they will not respond.
  550. */
  551. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  552. if (!(tmp & CLKCR1_SWCE)) {
  553. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  554. powerdown = 1;
  555. }
  556. /*
  557. * We want to clear out the serial port FIFOs so we don't end up playing
  558. * whatever random garbage happens to be in them. We fill the sample FIFOS
  559. * with zero (silence).
  560. */
  561. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0);
  562. /*
  563. * Fill all 256 sample FIFO locations.
  564. */
  565. for (idx = 0; idx < 0xFF; idx++) {
  566. /*
  567. * Make sure the previous FIFO write operation has completed.
  568. */
  569. if (cs46xx_wait_for_fifo(chip,1)) {
  570. dev_dbg(chip->card->dev,
  571. "failed waiting for FIFO at addr (%02X)\n",
  572. idx);
  573. if (powerdown)
  574. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  575. break;
  576. }
  577. /*
  578. * Write the serial port FIFO index.
  579. */
  580. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  581. /*
  582. * Tell the serial port to load the new value into the FIFO location.
  583. */
  584. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  585. }
  586. /*
  587. * Now, if we powered up the devices, then power them back down again.
  588. * This is kinda ugly, but should never happen.
  589. */
  590. if (powerdown)
  591. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  592. }
  593. static void snd_cs46xx_proc_start(struct snd_cs46xx *chip)
  594. {
  595. int cnt;
  596. /*
  597. * Set the frame timer to reflect the number of cycles per frame.
  598. */
  599. snd_cs46xx_poke(chip, BA1_FRMT, 0xadf);
  600. /*
  601. * Turn on the run, run at frame, and DMA enable bits in the local copy of
  602. * the SP control register.
  603. */
  604. snd_cs46xx_poke(chip, BA1_SPCR, SPCR_RUN | SPCR_RUNFR | SPCR_DRQEN);
  605. /*
  606. * Wait until the run at frame bit resets itself in the SP control
  607. * register.
  608. */
  609. for (cnt = 0; cnt < 25; cnt++) {
  610. udelay(50);
  611. if (!(snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR))
  612. break;
  613. }
  614. if (snd_cs46xx_peek(chip, BA1_SPCR) & SPCR_RUNFR)
  615. dev_err(chip->card->dev, "SPCR_RUNFR never reset\n");
  616. }
  617. static void snd_cs46xx_proc_stop(struct snd_cs46xx *chip)
  618. {
  619. /*
  620. * Turn off the run, run at frame, and DMA enable bits in the local copy of
  621. * the SP control register.
  622. */
  623. snd_cs46xx_poke(chip, BA1_SPCR, 0);
  624. }
  625. /*
  626. * Sample rate routines
  627. */
  628. #define GOF_PER_SEC 200
  629. static void snd_cs46xx_set_play_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
  630. {
  631. unsigned long flags;
  632. unsigned int tmp1, tmp2;
  633. unsigned int phiIncr;
  634. unsigned int correctionPerGOF, correctionPerSec;
  635. /*
  636. * Compute the values used to drive the actual sample rate conversion.
  637. * The following formulas are being computed, using inline assembly
  638. * since we need to use 64 bit arithmetic to compute the values:
  639. *
  640. * phiIncr = floor((Fs,in * 2^26) / Fs,out)
  641. * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
  642. * GOF_PER_SEC)
  643. * ulCorrectionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -M
  644. * GOF_PER_SEC * correctionPerGOF
  645. *
  646. * i.e.
  647. *
  648. * phiIncr:other = dividend:remainder((Fs,in * 2^26) / Fs,out)
  649. * correctionPerGOF:correctionPerSec =
  650. * dividend:remainder(ulOther / GOF_PER_SEC)
  651. */
  652. tmp1 = rate << 16;
  653. phiIncr = tmp1 / 48000;
  654. tmp1 -= phiIncr * 48000;
  655. tmp1 <<= 10;
  656. phiIncr <<= 10;
  657. tmp2 = tmp1 / 48000;
  658. phiIncr += tmp2;
  659. tmp1 -= tmp2 * 48000;
  660. correctionPerGOF = tmp1 / GOF_PER_SEC;
  661. tmp1 -= correctionPerGOF * GOF_PER_SEC;
  662. correctionPerSec = tmp1;
  663. /*
  664. * Fill in the SampleRateConverter control block.
  665. */
  666. spin_lock_irqsave(&chip->reg_lock, flags);
  667. snd_cs46xx_poke(chip, BA1_PSRC,
  668. ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
  669. snd_cs46xx_poke(chip, BA1_PPI, phiIncr);
  670. spin_unlock_irqrestore(&chip->reg_lock, flags);
  671. }
  672. static void snd_cs46xx_set_capture_sample_rate(struct snd_cs46xx *chip, unsigned int rate)
  673. {
  674. unsigned long flags;
  675. unsigned int phiIncr, coeffIncr, tmp1, tmp2;
  676. unsigned int correctionPerGOF, correctionPerSec, initialDelay;
  677. unsigned int frameGroupLength, cnt;
  678. /*
  679. * We can only decimate by up to a factor of 1/9th the hardware rate.
  680. * Correct the value if an attempt is made to stray outside that limit.
  681. */
  682. if ((rate * 9) < 48000)
  683. rate = 48000 / 9;
  684. /*
  685. * We can not capture at a rate greater than the Input Rate (48000).
  686. * Return an error if an attempt is made to stray outside that limit.
  687. */
  688. if (rate > 48000)
  689. rate = 48000;
  690. /*
  691. * Compute the values used to drive the actual sample rate conversion.
  692. * The following formulas are being computed, using inline assembly
  693. * since we need to use 64 bit arithmetic to compute the values:
  694. *
  695. * coeffIncr = -floor((Fs,out * 2^23) / Fs,in)
  696. * phiIncr = floor((Fs,in * 2^26) / Fs,out)
  697. * correctionPerGOF = floor((Fs,in * 2^26 - Fs,out * phiIncr) /
  698. * GOF_PER_SEC)
  699. * correctionPerSec = Fs,in * 2^26 - Fs,out * phiIncr -
  700. * GOF_PER_SEC * correctionPerGOF
  701. * initialDelay = ceil((24 * Fs,in) / Fs,out)
  702. *
  703. * i.e.
  704. *
  705. * coeffIncr = neg(dividend((Fs,out * 2^23) / Fs,in))
  706. * phiIncr:ulOther = dividend:remainder((Fs,in * 2^26) / Fs,out)
  707. * correctionPerGOF:correctionPerSec =
  708. * dividend:remainder(ulOther / GOF_PER_SEC)
  709. * initialDelay = dividend(((24 * Fs,in) + Fs,out - 1) / Fs,out)
  710. */
  711. tmp1 = rate << 16;
  712. coeffIncr = tmp1 / 48000;
  713. tmp1 -= coeffIncr * 48000;
  714. tmp1 <<= 7;
  715. coeffIncr <<= 7;
  716. coeffIncr += tmp1 / 48000;
  717. coeffIncr ^= 0xFFFFFFFF;
  718. coeffIncr++;
  719. tmp1 = 48000 << 16;
  720. phiIncr = tmp1 / rate;
  721. tmp1 -= phiIncr * rate;
  722. tmp1 <<= 10;
  723. phiIncr <<= 10;
  724. tmp2 = tmp1 / rate;
  725. phiIncr += tmp2;
  726. tmp1 -= tmp2 * rate;
  727. correctionPerGOF = tmp1 / GOF_PER_SEC;
  728. tmp1 -= correctionPerGOF * GOF_PER_SEC;
  729. correctionPerSec = tmp1;
  730. initialDelay = ((48000 * 24) + rate - 1) / rate;
  731. /*
  732. * Fill in the VariDecimate control block.
  733. */
  734. spin_lock_irqsave(&chip->reg_lock, flags);
  735. snd_cs46xx_poke(chip, BA1_CSRC,
  736. ((correctionPerSec << 16) & 0xFFFF0000) | (correctionPerGOF & 0xFFFF));
  737. snd_cs46xx_poke(chip, BA1_CCI, coeffIncr);
  738. snd_cs46xx_poke(chip, BA1_CD,
  739. (((BA1_VARIDEC_BUF_1 + (initialDelay << 2)) << 16) & 0xFFFF0000) | 0x80);
  740. snd_cs46xx_poke(chip, BA1_CPI, phiIncr);
  741. spin_unlock_irqrestore(&chip->reg_lock, flags);
  742. /*
  743. * Figure out the frame group length for the write back task. Basically,
  744. * this is just the factors of 24000 (2^6*3*5^3) that are not present in
  745. * the output sample rate.
  746. */
  747. frameGroupLength = 1;
  748. for (cnt = 2; cnt <= 64; cnt *= 2) {
  749. if (((rate / cnt) * cnt) != rate)
  750. frameGroupLength *= 2;
  751. }
  752. if (((rate / 3) * 3) != rate) {
  753. frameGroupLength *= 3;
  754. }
  755. for (cnt = 5; cnt <= 125; cnt *= 5) {
  756. if (((rate / cnt) * cnt) != rate)
  757. frameGroupLength *= 5;
  758. }
  759. /*
  760. * Fill in the WriteBack control block.
  761. */
  762. spin_lock_irqsave(&chip->reg_lock, flags);
  763. snd_cs46xx_poke(chip, BA1_CFG1, frameGroupLength);
  764. snd_cs46xx_poke(chip, BA1_CFG2, (0x00800000 | frameGroupLength));
  765. snd_cs46xx_poke(chip, BA1_CCST, 0x0000FFFF);
  766. snd_cs46xx_poke(chip, BA1_CSPB, ((65536 * rate) / 24000));
  767. snd_cs46xx_poke(chip, (BA1_CSPB + 4), 0x0000FFFF);
  768. spin_unlock_irqrestore(&chip->reg_lock, flags);
  769. }
  770. /*
  771. * PCM part
  772. */
  773. static void snd_cs46xx_pb_trans_copy(struct snd_pcm_substream *substream,
  774. struct snd_pcm_indirect *rec, size_t bytes)
  775. {
  776. struct snd_pcm_runtime *runtime = substream->runtime;
  777. struct snd_cs46xx_pcm * cpcm = runtime->private_data;
  778. memcpy(cpcm->hw_buf.area + rec->hw_data, runtime->dma_area + rec->sw_data, bytes);
  779. }
  780. static int snd_cs46xx_playback_transfer(struct snd_pcm_substream *substream)
  781. {
  782. struct snd_pcm_runtime *runtime = substream->runtime;
  783. struct snd_cs46xx_pcm * cpcm = runtime->private_data;
  784. return snd_pcm_indirect_playback_transfer(substream, &cpcm->pcm_rec,
  785. snd_cs46xx_pb_trans_copy);
  786. }
  787. static void snd_cs46xx_cp_trans_copy(struct snd_pcm_substream *substream,
  788. struct snd_pcm_indirect *rec, size_t bytes)
  789. {
  790. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  791. struct snd_pcm_runtime *runtime = substream->runtime;
  792. memcpy(runtime->dma_area + rec->sw_data,
  793. chip->capt.hw_buf.area + rec->hw_data, bytes);
  794. }
  795. static int snd_cs46xx_capture_transfer(struct snd_pcm_substream *substream)
  796. {
  797. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  798. return snd_pcm_indirect_capture_transfer(substream, &chip->capt.pcm_rec,
  799. snd_cs46xx_cp_trans_copy);
  800. }
  801. static snd_pcm_uframes_t snd_cs46xx_playback_direct_pointer(struct snd_pcm_substream *substream)
  802. {
  803. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  804. size_t ptr;
  805. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  806. if (snd_BUG_ON(!cpcm->pcm_channel))
  807. return -ENXIO;
  808. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  809. ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
  810. #else
  811. ptr = snd_cs46xx_peek(chip, BA1_PBA);
  812. #endif
  813. ptr -= cpcm->hw_buf.addr;
  814. return ptr >> cpcm->shift;
  815. }
  816. static snd_pcm_uframes_t snd_cs46xx_playback_indirect_pointer(struct snd_pcm_substream *substream)
  817. {
  818. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  819. size_t ptr;
  820. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  821. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  822. if (snd_BUG_ON(!cpcm->pcm_channel))
  823. return -ENXIO;
  824. ptr = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 2) << 2);
  825. #else
  826. ptr = snd_cs46xx_peek(chip, BA1_PBA);
  827. #endif
  828. ptr -= cpcm->hw_buf.addr;
  829. return snd_pcm_indirect_playback_pointer(substream, &cpcm->pcm_rec, ptr);
  830. }
  831. static snd_pcm_uframes_t snd_cs46xx_capture_direct_pointer(struct snd_pcm_substream *substream)
  832. {
  833. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  834. size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
  835. return ptr >> chip->capt.shift;
  836. }
  837. static snd_pcm_uframes_t snd_cs46xx_capture_indirect_pointer(struct snd_pcm_substream *substream)
  838. {
  839. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  840. size_t ptr = snd_cs46xx_peek(chip, BA1_CBA) - chip->capt.hw_buf.addr;
  841. return snd_pcm_indirect_capture_pointer(substream, &chip->capt.pcm_rec, ptr);
  842. }
  843. static int snd_cs46xx_playback_trigger(struct snd_pcm_substream *substream,
  844. int cmd)
  845. {
  846. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  847. /*struct snd_pcm_runtime *runtime = substream->runtime;*/
  848. int result = 0;
  849. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  850. struct snd_cs46xx_pcm *cpcm = substream->runtime->private_data;
  851. if (! cpcm->pcm_channel) {
  852. return -ENXIO;
  853. }
  854. #endif
  855. switch (cmd) {
  856. case SNDRV_PCM_TRIGGER_START:
  857. case SNDRV_PCM_TRIGGER_RESUME:
  858. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  859. /* magic value to unmute PCM stream playback volume */
  860. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
  861. SCBVolumeCtrl) << 2, 0x80008000);
  862. if (cpcm->pcm_channel->unlinked)
  863. cs46xx_dsp_pcm_link(chip,cpcm->pcm_channel);
  864. if (substream->runtime->periods != CS46XX_FRAGS)
  865. snd_cs46xx_playback_transfer(substream);
  866. #else
  867. spin_lock(&chip->reg_lock);
  868. if (substream->runtime->periods != CS46XX_FRAGS)
  869. snd_cs46xx_playback_transfer(substream);
  870. { unsigned int tmp;
  871. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  872. tmp &= 0x0000ffff;
  873. snd_cs46xx_poke(chip, BA1_PCTL, chip->play_ctl | tmp);
  874. }
  875. spin_unlock(&chip->reg_lock);
  876. #endif
  877. break;
  878. case SNDRV_PCM_TRIGGER_STOP:
  879. case SNDRV_PCM_TRIGGER_SUSPEND:
  880. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  881. /* magic mute channel */
  882. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address +
  883. SCBVolumeCtrl) << 2, 0xffffffff);
  884. if (!cpcm->pcm_channel->unlinked)
  885. cs46xx_dsp_pcm_unlink(chip,cpcm->pcm_channel);
  886. #else
  887. spin_lock(&chip->reg_lock);
  888. { unsigned int tmp;
  889. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  890. tmp &= 0x0000ffff;
  891. snd_cs46xx_poke(chip, BA1_PCTL, tmp);
  892. }
  893. spin_unlock(&chip->reg_lock);
  894. #endif
  895. break;
  896. default:
  897. result = -EINVAL;
  898. break;
  899. }
  900. return result;
  901. }
  902. static int snd_cs46xx_capture_trigger(struct snd_pcm_substream *substream,
  903. int cmd)
  904. {
  905. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  906. unsigned int tmp;
  907. int result = 0;
  908. spin_lock(&chip->reg_lock);
  909. switch (cmd) {
  910. case SNDRV_PCM_TRIGGER_START:
  911. case SNDRV_PCM_TRIGGER_RESUME:
  912. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  913. tmp &= 0xffff0000;
  914. snd_cs46xx_poke(chip, BA1_CCTL, chip->capt.ctl | tmp);
  915. break;
  916. case SNDRV_PCM_TRIGGER_STOP:
  917. case SNDRV_PCM_TRIGGER_SUSPEND:
  918. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  919. tmp &= 0xffff0000;
  920. snd_cs46xx_poke(chip, BA1_CCTL, tmp);
  921. break;
  922. default:
  923. result = -EINVAL;
  924. break;
  925. }
  926. spin_unlock(&chip->reg_lock);
  927. return result;
  928. }
  929. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  930. static int _cs46xx_adjust_sample_rate (struct snd_cs46xx *chip, struct snd_cs46xx_pcm *cpcm,
  931. int sample_rate)
  932. {
  933. /* If PCMReaderSCB and SrcTaskSCB not created yet ... */
  934. if ( cpcm->pcm_channel == NULL) {
  935. cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate,
  936. cpcm, cpcm->hw_buf.addr,cpcm->pcm_channel_id);
  937. if (cpcm->pcm_channel == NULL) {
  938. dev_err(chip->card->dev,
  939. "failed to create virtual PCM channel\n");
  940. return -ENOMEM;
  941. }
  942. cpcm->pcm_channel->sample_rate = sample_rate;
  943. } else
  944. /* if sample rate is changed */
  945. if ((int)cpcm->pcm_channel->sample_rate != sample_rate) {
  946. int unlinked = cpcm->pcm_channel->unlinked;
  947. cs46xx_dsp_destroy_pcm_channel (chip,cpcm->pcm_channel);
  948. if ( (cpcm->pcm_channel = cs46xx_dsp_create_pcm_channel (chip, sample_rate, cpcm,
  949. cpcm->hw_buf.addr,
  950. cpcm->pcm_channel_id)) == NULL) {
  951. dev_err(chip->card->dev,
  952. "failed to re-create virtual PCM channel\n");
  953. return -ENOMEM;
  954. }
  955. if (!unlinked) cs46xx_dsp_pcm_link (chip,cpcm->pcm_channel);
  956. cpcm->pcm_channel->sample_rate = sample_rate;
  957. }
  958. return 0;
  959. }
  960. #endif
  961. static int snd_cs46xx_playback_hw_params(struct snd_pcm_substream *substream,
  962. struct snd_pcm_hw_params *hw_params)
  963. {
  964. struct snd_pcm_runtime *runtime = substream->runtime;
  965. struct snd_cs46xx_pcm *cpcm;
  966. int err;
  967. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  968. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  969. int sample_rate = params_rate(hw_params);
  970. int period_size = params_period_bytes(hw_params);
  971. #endif
  972. cpcm = runtime->private_data;
  973. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  974. if (snd_BUG_ON(!sample_rate))
  975. return -ENXIO;
  976. mutex_lock(&chip->spos_mutex);
  977. if (_cs46xx_adjust_sample_rate (chip,cpcm,sample_rate)) {
  978. mutex_unlock(&chip->spos_mutex);
  979. return -ENXIO;
  980. }
  981. snd_BUG_ON(!cpcm->pcm_channel);
  982. if (!cpcm->pcm_channel) {
  983. mutex_unlock(&chip->spos_mutex);
  984. return -ENXIO;
  985. }
  986. if (cs46xx_dsp_pcm_channel_set_period (chip,cpcm->pcm_channel,period_size)) {
  987. mutex_unlock(&chip->spos_mutex);
  988. return -EINVAL;
  989. }
  990. dev_dbg(chip->card->dev,
  991. "period_size (%d), periods (%d) buffer_size(%d)\n",
  992. period_size, params_periods(hw_params),
  993. params_buffer_bytes(hw_params));
  994. #endif
  995. if (params_periods(hw_params) == CS46XX_FRAGS) {
  996. if (runtime->dma_area != cpcm->hw_buf.area)
  997. snd_pcm_lib_free_pages(substream);
  998. runtime->dma_area = cpcm->hw_buf.area;
  999. runtime->dma_addr = cpcm->hw_buf.addr;
  1000. runtime->dma_bytes = cpcm->hw_buf.bytes;
  1001. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1002. if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
  1003. substream->ops = &snd_cs46xx_playback_ops;
  1004. } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
  1005. substream->ops = &snd_cs46xx_playback_rear_ops;
  1006. } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
  1007. substream->ops = &snd_cs46xx_playback_clfe_ops;
  1008. } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
  1009. substream->ops = &snd_cs46xx_playback_iec958_ops;
  1010. } else {
  1011. snd_BUG();
  1012. }
  1013. #else
  1014. substream->ops = &snd_cs46xx_playback_ops;
  1015. #endif
  1016. } else {
  1017. if (runtime->dma_area == cpcm->hw_buf.area) {
  1018. runtime->dma_area = NULL;
  1019. runtime->dma_addr = 0;
  1020. runtime->dma_bytes = 0;
  1021. }
  1022. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0) {
  1023. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1024. mutex_unlock(&chip->spos_mutex);
  1025. #endif
  1026. return err;
  1027. }
  1028. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1029. if (cpcm->pcm_channel_id == DSP_PCM_MAIN_CHANNEL) {
  1030. substream->ops = &snd_cs46xx_playback_indirect_ops;
  1031. } else if (cpcm->pcm_channel_id == DSP_PCM_REAR_CHANNEL) {
  1032. substream->ops = &snd_cs46xx_playback_indirect_rear_ops;
  1033. } else if (cpcm->pcm_channel_id == DSP_PCM_CENTER_LFE_CHANNEL) {
  1034. substream->ops = &snd_cs46xx_playback_indirect_clfe_ops;
  1035. } else if (cpcm->pcm_channel_id == DSP_IEC958_CHANNEL) {
  1036. substream->ops = &snd_cs46xx_playback_indirect_iec958_ops;
  1037. } else {
  1038. snd_BUG();
  1039. }
  1040. #else
  1041. substream->ops = &snd_cs46xx_playback_indirect_ops;
  1042. #endif
  1043. }
  1044. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1045. mutex_unlock(&chip->spos_mutex);
  1046. #endif
  1047. return 0;
  1048. }
  1049. static int snd_cs46xx_playback_hw_free(struct snd_pcm_substream *substream)
  1050. {
  1051. /*struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);*/
  1052. struct snd_pcm_runtime *runtime = substream->runtime;
  1053. struct snd_cs46xx_pcm *cpcm;
  1054. cpcm = runtime->private_data;
  1055. /* if play_back open fails, then this function
  1056. is called and cpcm can actually be NULL here */
  1057. if (!cpcm) return -ENXIO;
  1058. if (runtime->dma_area != cpcm->hw_buf.area)
  1059. snd_pcm_lib_free_pages(substream);
  1060. runtime->dma_area = NULL;
  1061. runtime->dma_addr = 0;
  1062. runtime->dma_bytes = 0;
  1063. return 0;
  1064. }
  1065. static int snd_cs46xx_playback_prepare(struct snd_pcm_substream *substream)
  1066. {
  1067. unsigned int tmp;
  1068. unsigned int pfie;
  1069. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1070. struct snd_pcm_runtime *runtime = substream->runtime;
  1071. struct snd_cs46xx_pcm *cpcm;
  1072. cpcm = runtime->private_data;
  1073. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1074. if (snd_BUG_ON(!cpcm->pcm_channel))
  1075. return -ENXIO;
  1076. pfie = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2 );
  1077. pfie &= ~0x0000f03f;
  1078. #else
  1079. /* old dsp */
  1080. pfie = snd_cs46xx_peek(chip, BA1_PFIE);
  1081. pfie &= ~0x0000f03f;
  1082. #endif
  1083. cpcm->shift = 2;
  1084. /* if to convert from stereo to mono */
  1085. if (runtime->channels == 1) {
  1086. cpcm->shift--;
  1087. pfie |= 0x00002000;
  1088. }
  1089. /* if to convert from 8 bit to 16 bit */
  1090. if (snd_pcm_format_width(runtime->format) == 8) {
  1091. cpcm->shift--;
  1092. pfie |= 0x00001000;
  1093. }
  1094. /* if to convert to unsigned */
  1095. if (snd_pcm_format_unsigned(runtime->format))
  1096. pfie |= 0x00008000;
  1097. /* Never convert byte order when sample stream is 8 bit */
  1098. if (snd_pcm_format_width(runtime->format) != 8) {
  1099. /* convert from big endian to little endian */
  1100. if (snd_pcm_format_big_endian(runtime->format))
  1101. pfie |= 0x00004000;
  1102. }
  1103. memset(&cpcm->pcm_rec, 0, sizeof(cpcm->pcm_rec));
  1104. cpcm->pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1105. cpcm->pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << cpcm->shift;
  1106. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1107. tmp = snd_cs46xx_peek(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2);
  1108. tmp &= ~0x000003ff;
  1109. tmp |= (4 << cpcm->shift) - 1;
  1110. /* playback transaction count register */
  1111. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address) << 2, tmp);
  1112. /* playback format && interrupt enable */
  1113. snd_cs46xx_poke(chip, (cpcm->pcm_channel->pcm_reader_scb->address + 1) << 2, pfie | cpcm->pcm_channel->pcm_slot);
  1114. #else
  1115. snd_cs46xx_poke(chip, BA1_PBA, cpcm->hw_buf.addr);
  1116. tmp = snd_cs46xx_peek(chip, BA1_PDTC);
  1117. tmp &= ~0x000003ff;
  1118. tmp |= (4 << cpcm->shift) - 1;
  1119. snd_cs46xx_poke(chip, BA1_PDTC, tmp);
  1120. snd_cs46xx_poke(chip, BA1_PFIE, pfie);
  1121. snd_cs46xx_set_play_sample_rate(chip, runtime->rate);
  1122. #endif
  1123. return 0;
  1124. }
  1125. static int snd_cs46xx_capture_hw_params(struct snd_pcm_substream *substream,
  1126. struct snd_pcm_hw_params *hw_params)
  1127. {
  1128. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1129. struct snd_pcm_runtime *runtime = substream->runtime;
  1130. int err;
  1131. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1132. cs46xx_dsp_pcm_ostream_set_period (chip, params_period_bytes(hw_params));
  1133. #endif
  1134. if (runtime->periods == CS46XX_FRAGS) {
  1135. if (runtime->dma_area != chip->capt.hw_buf.area)
  1136. snd_pcm_lib_free_pages(substream);
  1137. runtime->dma_area = chip->capt.hw_buf.area;
  1138. runtime->dma_addr = chip->capt.hw_buf.addr;
  1139. runtime->dma_bytes = chip->capt.hw_buf.bytes;
  1140. substream->ops = &snd_cs46xx_capture_ops;
  1141. } else {
  1142. if (runtime->dma_area == chip->capt.hw_buf.area) {
  1143. runtime->dma_area = NULL;
  1144. runtime->dma_addr = 0;
  1145. runtime->dma_bytes = 0;
  1146. }
  1147. if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  1148. return err;
  1149. substream->ops = &snd_cs46xx_capture_indirect_ops;
  1150. }
  1151. return 0;
  1152. }
  1153. static int snd_cs46xx_capture_hw_free(struct snd_pcm_substream *substream)
  1154. {
  1155. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1156. struct snd_pcm_runtime *runtime = substream->runtime;
  1157. if (runtime->dma_area != chip->capt.hw_buf.area)
  1158. snd_pcm_lib_free_pages(substream);
  1159. runtime->dma_area = NULL;
  1160. runtime->dma_addr = 0;
  1161. runtime->dma_bytes = 0;
  1162. return 0;
  1163. }
  1164. static int snd_cs46xx_capture_prepare(struct snd_pcm_substream *substream)
  1165. {
  1166. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1167. struct snd_pcm_runtime *runtime = substream->runtime;
  1168. snd_cs46xx_poke(chip, BA1_CBA, chip->capt.hw_buf.addr);
  1169. chip->capt.shift = 2;
  1170. memset(&chip->capt.pcm_rec, 0, sizeof(chip->capt.pcm_rec));
  1171. chip->capt.pcm_rec.sw_buffer_size = snd_pcm_lib_buffer_bytes(substream);
  1172. chip->capt.pcm_rec.hw_buffer_size = runtime->period_size * CS46XX_FRAGS << 2;
  1173. snd_cs46xx_set_capture_sample_rate(chip, runtime->rate);
  1174. return 0;
  1175. }
  1176. static irqreturn_t snd_cs46xx_interrupt(int irq, void *dev_id)
  1177. {
  1178. struct snd_cs46xx *chip = dev_id;
  1179. u32 status1;
  1180. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1181. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1182. u32 status2;
  1183. int i;
  1184. struct snd_cs46xx_pcm *cpcm = NULL;
  1185. #endif
  1186. /*
  1187. * Read the Interrupt Status Register to clear the interrupt
  1188. */
  1189. status1 = snd_cs46xx_peekBA0(chip, BA0_HISR);
  1190. if ((status1 & 0x7fffffff) == 0) {
  1191. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
  1192. return IRQ_NONE;
  1193. }
  1194. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1195. status2 = snd_cs46xx_peekBA0(chip, BA0_HSR0);
  1196. for (i = 0; i < DSP_MAX_PCM_CHANNELS; ++i) {
  1197. if (i <= 15) {
  1198. if ( status1 & (1 << i) ) {
  1199. if (i == CS46XX_DSP_CAPTURE_CHANNEL) {
  1200. if (chip->capt.substream)
  1201. snd_pcm_period_elapsed(chip->capt.substream);
  1202. } else {
  1203. if (ins->pcm_channels[i].active &&
  1204. ins->pcm_channels[i].private_data &&
  1205. !ins->pcm_channels[i].unlinked) {
  1206. cpcm = ins->pcm_channels[i].private_data;
  1207. snd_pcm_period_elapsed(cpcm->substream);
  1208. }
  1209. }
  1210. }
  1211. } else {
  1212. if ( status2 & (1 << (i - 16))) {
  1213. if (ins->pcm_channels[i].active &&
  1214. ins->pcm_channels[i].private_data &&
  1215. !ins->pcm_channels[i].unlinked) {
  1216. cpcm = ins->pcm_channels[i].private_data;
  1217. snd_pcm_period_elapsed(cpcm->substream);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. #else
  1223. /* old dsp */
  1224. if ((status1 & HISR_VC0) && chip->playback_pcm) {
  1225. if (chip->playback_pcm->substream)
  1226. snd_pcm_period_elapsed(chip->playback_pcm->substream);
  1227. }
  1228. if ((status1 & HISR_VC1) && chip->pcm) {
  1229. if (chip->capt.substream)
  1230. snd_pcm_period_elapsed(chip->capt.substream);
  1231. }
  1232. #endif
  1233. if ((status1 & HISR_MIDI) && chip->rmidi) {
  1234. unsigned char c;
  1235. spin_lock(&chip->reg_lock);
  1236. while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_RBE) == 0) {
  1237. c = snd_cs46xx_peekBA0(chip, BA0_MIDRP);
  1238. if ((chip->midcr & MIDCR_RIE) == 0)
  1239. continue;
  1240. snd_rawmidi_receive(chip->midi_input, &c, 1);
  1241. }
  1242. while ((snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  1243. if ((chip->midcr & MIDCR_TIE) == 0)
  1244. break;
  1245. if (snd_rawmidi_transmit(chip->midi_output, &c, 1) != 1) {
  1246. chip->midcr &= ~MIDCR_TIE;
  1247. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  1248. break;
  1249. }
  1250. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, c);
  1251. }
  1252. spin_unlock(&chip->reg_lock);
  1253. }
  1254. /*
  1255. * EOI to the PCI part....reenables interrupts
  1256. */
  1257. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_CHGM | HICR_IEV);
  1258. return IRQ_HANDLED;
  1259. }
  1260. static const struct snd_pcm_hardware snd_cs46xx_playback =
  1261. {
  1262. .info = (SNDRV_PCM_INFO_MMAP |
  1263. SNDRV_PCM_INFO_INTERLEAVED |
  1264. SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
  1265. /*SNDRV_PCM_INFO_RESUME*/),
  1266. .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
  1267. SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S16_BE |
  1268. SNDRV_PCM_FMTBIT_U16_LE | SNDRV_PCM_FMTBIT_U16_BE),
  1269. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1270. .rate_min = 5500,
  1271. .rate_max = 48000,
  1272. .channels_min = 1,
  1273. .channels_max = 2,
  1274. .buffer_bytes_max = (256 * 1024),
  1275. .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
  1276. .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
  1277. .periods_min = CS46XX_FRAGS,
  1278. .periods_max = 1024,
  1279. .fifo_size = 0,
  1280. };
  1281. static const struct snd_pcm_hardware snd_cs46xx_capture =
  1282. {
  1283. .info = (SNDRV_PCM_INFO_MMAP |
  1284. SNDRV_PCM_INFO_INTERLEAVED |
  1285. SNDRV_PCM_INFO_BLOCK_TRANSFER /*|*/
  1286. /*SNDRV_PCM_INFO_RESUME*/),
  1287. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1288. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1289. .rate_min = 5500,
  1290. .rate_max = 48000,
  1291. .channels_min = 2,
  1292. .channels_max = 2,
  1293. .buffer_bytes_max = (256 * 1024),
  1294. .period_bytes_min = CS46XX_MIN_PERIOD_SIZE,
  1295. .period_bytes_max = CS46XX_MAX_PERIOD_SIZE,
  1296. .periods_min = CS46XX_FRAGS,
  1297. .periods_max = 1024,
  1298. .fifo_size = 0,
  1299. };
  1300. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1301. static const unsigned int period_sizes[] = { 32, 64, 128, 256, 512, 1024, 2048 };
  1302. static const struct snd_pcm_hw_constraint_list hw_constraints_period_sizes = {
  1303. .count = ARRAY_SIZE(period_sizes),
  1304. .list = period_sizes,
  1305. .mask = 0
  1306. };
  1307. #endif
  1308. static void snd_cs46xx_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1309. {
  1310. kfree(runtime->private_data);
  1311. }
  1312. static int _cs46xx_playback_open_channel (struct snd_pcm_substream *substream,int pcm_channel_id)
  1313. {
  1314. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1315. struct snd_cs46xx_pcm * cpcm;
  1316. struct snd_pcm_runtime *runtime = substream->runtime;
  1317. cpcm = kzalloc(sizeof(*cpcm), GFP_KERNEL);
  1318. if (cpcm == NULL)
  1319. return -ENOMEM;
  1320. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1321. PAGE_SIZE, &cpcm->hw_buf) < 0) {
  1322. kfree(cpcm);
  1323. return -ENOMEM;
  1324. }
  1325. runtime->hw = snd_cs46xx_playback;
  1326. runtime->private_data = cpcm;
  1327. runtime->private_free = snd_cs46xx_pcm_free_substream;
  1328. cpcm->substream = substream;
  1329. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1330. mutex_lock(&chip->spos_mutex);
  1331. cpcm->pcm_channel = NULL;
  1332. cpcm->pcm_channel_id = pcm_channel_id;
  1333. snd_pcm_hw_constraint_list(runtime, 0,
  1334. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1335. &hw_constraints_period_sizes);
  1336. mutex_unlock(&chip->spos_mutex);
  1337. #else
  1338. chip->playback_pcm = cpcm; /* HACK */
  1339. #endif
  1340. if (chip->accept_valid)
  1341. substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
  1342. chip->active_ctrl(chip, 1);
  1343. return 0;
  1344. }
  1345. static int snd_cs46xx_playback_open(struct snd_pcm_substream *substream)
  1346. {
  1347. dev_dbg(substream->pcm->card->dev, "open front channel\n");
  1348. return _cs46xx_playback_open_channel(substream,DSP_PCM_MAIN_CHANNEL);
  1349. }
  1350. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1351. static int snd_cs46xx_playback_open_rear(struct snd_pcm_substream *substream)
  1352. {
  1353. dev_dbg(substream->pcm->card->dev, "open rear channel\n");
  1354. return _cs46xx_playback_open_channel(substream,DSP_PCM_REAR_CHANNEL);
  1355. }
  1356. static int snd_cs46xx_playback_open_clfe(struct snd_pcm_substream *substream)
  1357. {
  1358. dev_dbg(substream->pcm->card->dev, "open center - LFE channel\n");
  1359. return _cs46xx_playback_open_channel(substream,DSP_PCM_CENTER_LFE_CHANNEL);
  1360. }
  1361. static int snd_cs46xx_playback_open_iec958(struct snd_pcm_substream *substream)
  1362. {
  1363. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1364. dev_dbg(chip->card->dev, "open raw iec958 channel\n");
  1365. mutex_lock(&chip->spos_mutex);
  1366. cs46xx_iec958_pre_open (chip);
  1367. mutex_unlock(&chip->spos_mutex);
  1368. return _cs46xx_playback_open_channel(substream,DSP_IEC958_CHANNEL);
  1369. }
  1370. static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream);
  1371. static int snd_cs46xx_playback_close_iec958(struct snd_pcm_substream *substream)
  1372. {
  1373. int err;
  1374. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1375. dev_dbg(chip->card->dev, "close raw iec958 channel\n");
  1376. err = snd_cs46xx_playback_close(substream);
  1377. mutex_lock(&chip->spos_mutex);
  1378. cs46xx_iec958_post_close (chip);
  1379. mutex_unlock(&chip->spos_mutex);
  1380. return err;
  1381. }
  1382. #endif
  1383. static int snd_cs46xx_capture_open(struct snd_pcm_substream *substream)
  1384. {
  1385. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1386. if (snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
  1387. PAGE_SIZE, &chip->capt.hw_buf) < 0)
  1388. return -ENOMEM;
  1389. chip->capt.substream = substream;
  1390. substream->runtime->hw = snd_cs46xx_capture;
  1391. if (chip->accept_valid)
  1392. substream->runtime->hw.info |= SNDRV_PCM_INFO_MMAP_VALID;
  1393. chip->active_ctrl(chip, 1);
  1394. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1395. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1396. SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
  1397. &hw_constraints_period_sizes);
  1398. #endif
  1399. return 0;
  1400. }
  1401. static int snd_cs46xx_playback_close(struct snd_pcm_substream *substream)
  1402. {
  1403. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1404. struct snd_pcm_runtime *runtime = substream->runtime;
  1405. struct snd_cs46xx_pcm * cpcm;
  1406. cpcm = runtime->private_data;
  1407. /* when playback_open fails, then cpcm can be NULL */
  1408. if (!cpcm) return -ENXIO;
  1409. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1410. mutex_lock(&chip->spos_mutex);
  1411. if (cpcm->pcm_channel) {
  1412. cs46xx_dsp_destroy_pcm_channel(chip,cpcm->pcm_channel);
  1413. cpcm->pcm_channel = NULL;
  1414. }
  1415. mutex_unlock(&chip->spos_mutex);
  1416. #else
  1417. chip->playback_pcm = NULL;
  1418. #endif
  1419. cpcm->substream = NULL;
  1420. snd_dma_free_pages(&cpcm->hw_buf);
  1421. chip->active_ctrl(chip, -1);
  1422. return 0;
  1423. }
  1424. static int snd_cs46xx_capture_close(struct snd_pcm_substream *substream)
  1425. {
  1426. struct snd_cs46xx *chip = snd_pcm_substream_chip(substream);
  1427. chip->capt.substream = NULL;
  1428. snd_dma_free_pages(&chip->capt.hw_buf);
  1429. chip->active_ctrl(chip, -1);
  1430. return 0;
  1431. }
  1432. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1433. static const struct snd_pcm_ops snd_cs46xx_playback_rear_ops = {
  1434. .open = snd_cs46xx_playback_open_rear,
  1435. .close = snd_cs46xx_playback_close,
  1436. .ioctl = snd_pcm_lib_ioctl,
  1437. .hw_params = snd_cs46xx_playback_hw_params,
  1438. .hw_free = snd_cs46xx_playback_hw_free,
  1439. .prepare = snd_cs46xx_playback_prepare,
  1440. .trigger = snd_cs46xx_playback_trigger,
  1441. .pointer = snd_cs46xx_playback_direct_pointer,
  1442. };
  1443. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_rear_ops = {
  1444. .open = snd_cs46xx_playback_open_rear,
  1445. .close = snd_cs46xx_playback_close,
  1446. .ioctl = snd_pcm_lib_ioctl,
  1447. .hw_params = snd_cs46xx_playback_hw_params,
  1448. .hw_free = snd_cs46xx_playback_hw_free,
  1449. .prepare = snd_cs46xx_playback_prepare,
  1450. .trigger = snd_cs46xx_playback_trigger,
  1451. .pointer = snd_cs46xx_playback_indirect_pointer,
  1452. .ack = snd_cs46xx_playback_transfer,
  1453. };
  1454. static const struct snd_pcm_ops snd_cs46xx_playback_clfe_ops = {
  1455. .open = snd_cs46xx_playback_open_clfe,
  1456. .close = snd_cs46xx_playback_close,
  1457. .ioctl = snd_pcm_lib_ioctl,
  1458. .hw_params = snd_cs46xx_playback_hw_params,
  1459. .hw_free = snd_cs46xx_playback_hw_free,
  1460. .prepare = snd_cs46xx_playback_prepare,
  1461. .trigger = snd_cs46xx_playback_trigger,
  1462. .pointer = snd_cs46xx_playback_direct_pointer,
  1463. };
  1464. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_clfe_ops = {
  1465. .open = snd_cs46xx_playback_open_clfe,
  1466. .close = snd_cs46xx_playback_close,
  1467. .ioctl = snd_pcm_lib_ioctl,
  1468. .hw_params = snd_cs46xx_playback_hw_params,
  1469. .hw_free = snd_cs46xx_playback_hw_free,
  1470. .prepare = snd_cs46xx_playback_prepare,
  1471. .trigger = snd_cs46xx_playback_trigger,
  1472. .pointer = snd_cs46xx_playback_indirect_pointer,
  1473. .ack = snd_cs46xx_playback_transfer,
  1474. };
  1475. static const struct snd_pcm_ops snd_cs46xx_playback_iec958_ops = {
  1476. .open = snd_cs46xx_playback_open_iec958,
  1477. .close = snd_cs46xx_playback_close_iec958,
  1478. .ioctl = snd_pcm_lib_ioctl,
  1479. .hw_params = snd_cs46xx_playback_hw_params,
  1480. .hw_free = snd_cs46xx_playback_hw_free,
  1481. .prepare = snd_cs46xx_playback_prepare,
  1482. .trigger = snd_cs46xx_playback_trigger,
  1483. .pointer = snd_cs46xx_playback_direct_pointer,
  1484. };
  1485. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_iec958_ops = {
  1486. .open = snd_cs46xx_playback_open_iec958,
  1487. .close = snd_cs46xx_playback_close_iec958,
  1488. .ioctl = snd_pcm_lib_ioctl,
  1489. .hw_params = snd_cs46xx_playback_hw_params,
  1490. .hw_free = snd_cs46xx_playback_hw_free,
  1491. .prepare = snd_cs46xx_playback_prepare,
  1492. .trigger = snd_cs46xx_playback_trigger,
  1493. .pointer = snd_cs46xx_playback_indirect_pointer,
  1494. .ack = snd_cs46xx_playback_transfer,
  1495. };
  1496. #endif
  1497. static const struct snd_pcm_ops snd_cs46xx_playback_ops = {
  1498. .open = snd_cs46xx_playback_open,
  1499. .close = snd_cs46xx_playback_close,
  1500. .ioctl = snd_pcm_lib_ioctl,
  1501. .hw_params = snd_cs46xx_playback_hw_params,
  1502. .hw_free = snd_cs46xx_playback_hw_free,
  1503. .prepare = snd_cs46xx_playback_prepare,
  1504. .trigger = snd_cs46xx_playback_trigger,
  1505. .pointer = snd_cs46xx_playback_direct_pointer,
  1506. };
  1507. static const struct snd_pcm_ops snd_cs46xx_playback_indirect_ops = {
  1508. .open = snd_cs46xx_playback_open,
  1509. .close = snd_cs46xx_playback_close,
  1510. .ioctl = snd_pcm_lib_ioctl,
  1511. .hw_params = snd_cs46xx_playback_hw_params,
  1512. .hw_free = snd_cs46xx_playback_hw_free,
  1513. .prepare = snd_cs46xx_playback_prepare,
  1514. .trigger = snd_cs46xx_playback_trigger,
  1515. .pointer = snd_cs46xx_playback_indirect_pointer,
  1516. .ack = snd_cs46xx_playback_transfer,
  1517. };
  1518. static const struct snd_pcm_ops snd_cs46xx_capture_ops = {
  1519. .open = snd_cs46xx_capture_open,
  1520. .close = snd_cs46xx_capture_close,
  1521. .ioctl = snd_pcm_lib_ioctl,
  1522. .hw_params = snd_cs46xx_capture_hw_params,
  1523. .hw_free = snd_cs46xx_capture_hw_free,
  1524. .prepare = snd_cs46xx_capture_prepare,
  1525. .trigger = snd_cs46xx_capture_trigger,
  1526. .pointer = snd_cs46xx_capture_direct_pointer,
  1527. };
  1528. static const struct snd_pcm_ops snd_cs46xx_capture_indirect_ops = {
  1529. .open = snd_cs46xx_capture_open,
  1530. .close = snd_cs46xx_capture_close,
  1531. .ioctl = snd_pcm_lib_ioctl,
  1532. .hw_params = snd_cs46xx_capture_hw_params,
  1533. .hw_free = snd_cs46xx_capture_hw_free,
  1534. .prepare = snd_cs46xx_capture_prepare,
  1535. .trigger = snd_cs46xx_capture_trigger,
  1536. .pointer = snd_cs46xx_capture_indirect_pointer,
  1537. .ack = snd_cs46xx_capture_transfer,
  1538. };
  1539. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1540. #define MAX_PLAYBACK_CHANNELS (DSP_MAX_PCM_CHANNELS - 1)
  1541. #else
  1542. #define MAX_PLAYBACK_CHANNELS 1
  1543. #endif
  1544. int snd_cs46xx_pcm(struct snd_cs46xx *chip, int device)
  1545. {
  1546. struct snd_pcm *pcm;
  1547. int err;
  1548. if ((err = snd_pcm_new(chip->card, "CS46xx", device, MAX_PLAYBACK_CHANNELS, 1, &pcm)) < 0)
  1549. return err;
  1550. pcm->private_data = chip;
  1551. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_ops);
  1552. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs46xx_capture_ops);
  1553. /* global setup */
  1554. pcm->info_flags = 0;
  1555. strcpy(pcm->name, "CS46xx");
  1556. chip->pcm = pcm;
  1557. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1558. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1559. return 0;
  1560. }
  1561. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1562. int snd_cs46xx_pcm_rear(struct snd_cs46xx *chip, int device)
  1563. {
  1564. struct snd_pcm *pcm;
  1565. int err;
  1566. if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1567. return err;
  1568. pcm->private_data = chip;
  1569. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
  1570. /* global setup */
  1571. pcm->info_flags = 0;
  1572. strcpy(pcm->name, "CS46xx - Rear");
  1573. chip->pcm_rear = pcm;
  1574. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1575. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1576. return 0;
  1577. }
  1578. int snd_cs46xx_pcm_center_lfe(struct snd_cs46xx *chip, int device)
  1579. {
  1580. struct snd_pcm *pcm;
  1581. int err;
  1582. if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1583. return err;
  1584. pcm->private_data = chip;
  1585. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
  1586. /* global setup */
  1587. pcm->info_flags = 0;
  1588. strcpy(pcm->name, "CS46xx - Center LFE");
  1589. chip->pcm_center_lfe = pcm;
  1590. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1591. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1592. return 0;
  1593. }
  1594. int snd_cs46xx_pcm_iec958(struct snd_cs46xx *chip, int device)
  1595. {
  1596. struct snd_pcm *pcm;
  1597. int err;
  1598. if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
  1599. return err;
  1600. pcm->private_data = chip;
  1601. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
  1602. /* global setup */
  1603. pcm->info_flags = 0;
  1604. strcpy(pcm->name, "CS46xx - IEC958");
  1605. chip->pcm_iec958 = pcm;
  1606. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1607. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1608. return 0;
  1609. }
  1610. #endif
  1611. /*
  1612. * Mixer routines
  1613. */
  1614. static void snd_cs46xx_mixer_free_ac97_bus(struct snd_ac97_bus *bus)
  1615. {
  1616. struct snd_cs46xx *chip = bus->private_data;
  1617. chip->ac97_bus = NULL;
  1618. }
  1619. static void snd_cs46xx_mixer_free_ac97(struct snd_ac97 *ac97)
  1620. {
  1621. struct snd_cs46xx *chip = ac97->private_data;
  1622. if (snd_BUG_ON(ac97 != chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] &&
  1623. ac97 != chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]))
  1624. return;
  1625. if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
  1626. chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
  1627. chip->eapd_switch = NULL;
  1628. }
  1629. else
  1630. chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
  1631. }
  1632. static int snd_cs46xx_vol_info(struct snd_kcontrol *kcontrol,
  1633. struct snd_ctl_elem_info *uinfo)
  1634. {
  1635. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1636. uinfo->count = 2;
  1637. uinfo->value.integer.min = 0;
  1638. uinfo->value.integer.max = 0x7fff;
  1639. return 0;
  1640. }
  1641. static int snd_cs46xx_vol_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1642. {
  1643. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1644. int reg = kcontrol->private_value;
  1645. unsigned int val = snd_cs46xx_peek(chip, reg);
  1646. ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
  1647. ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
  1648. return 0;
  1649. }
  1650. static int snd_cs46xx_vol_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1651. {
  1652. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1653. int reg = kcontrol->private_value;
  1654. unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
  1655. (0xffff - ucontrol->value.integer.value[1]));
  1656. unsigned int old = snd_cs46xx_peek(chip, reg);
  1657. int change = (old != val);
  1658. if (change) {
  1659. snd_cs46xx_poke(chip, reg, val);
  1660. }
  1661. return change;
  1662. }
  1663. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1664. static int snd_cs46xx_vol_dac_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1665. {
  1666. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1667. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
  1668. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
  1669. return 0;
  1670. }
  1671. static int snd_cs46xx_vol_dac_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1672. {
  1673. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1674. int change = 0;
  1675. if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
  1676. chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
  1677. cs46xx_dsp_set_dac_volume(chip,
  1678. ucontrol->value.integer.value[0],
  1679. ucontrol->value.integer.value[1]);
  1680. change = 1;
  1681. }
  1682. return change;
  1683. }
  1684. #if 0
  1685. static int snd_cs46xx_vol_iec958_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1686. {
  1687. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1688. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
  1689. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
  1690. return 0;
  1691. }
  1692. static int snd_cs46xx_vol_iec958_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  1693. {
  1694. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1695. int change = 0;
  1696. if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
  1697. chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
  1698. cs46xx_dsp_set_iec958_volume (chip,
  1699. ucontrol->value.integer.value[0],
  1700. ucontrol->value.integer.value[1]);
  1701. change = 1;
  1702. }
  1703. return change;
  1704. }
  1705. #endif
  1706. #define snd_mixer_boolean_info snd_ctl_boolean_mono_info
  1707. static int snd_cs46xx_iec958_get(struct snd_kcontrol *kcontrol,
  1708. struct snd_ctl_elem_value *ucontrol)
  1709. {
  1710. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1711. int reg = kcontrol->private_value;
  1712. if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
  1713. ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1714. else
  1715. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
  1716. return 0;
  1717. }
  1718. static int snd_cs46xx_iec958_put(struct snd_kcontrol *kcontrol,
  1719. struct snd_ctl_elem_value *ucontrol)
  1720. {
  1721. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1722. int change, res;
  1723. switch (kcontrol->private_value) {
  1724. case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
  1725. mutex_lock(&chip->spos_mutex);
  1726. change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1727. if (ucontrol->value.integer.value[0] && !change)
  1728. cs46xx_dsp_enable_spdif_out(chip);
  1729. else if (change && !ucontrol->value.integer.value[0])
  1730. cs46xx_dsp_disable_spdif_out(chip);
  1731. res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
  1732. mutex_unlock(&chip->spos_mutex);
  1733. break;
  1734. case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
  1735. change = chip->dsp_spos_instance->spdif_status_in;
  1736. if (ucontrol->value.integer.value[0] && !change) {
  1737. cs46xx_dsp_enable_spdif_in(chip);
  1738. /* restore volume */
  1739. }
  1740. else if (change && !ucontrol->value.integer.value[0])
  1741. cs46xx_dsp_disable_spdif_in(chip);
  1742. res = (change != chip->dsp_spos_instance->spdif_status_in);
  1743. break;
  1744. default:
  1745. res = -EINVAL;
  1746. snd_BUG(); /* should never happen ... */
  1747. }
  1748. return res;
  1749. }
  1750. static int snd_cs46xx_adc_capture_get(struct snd_kcontrol *kcontrol,
  1751. struct snd_ctl_elem_value *ucontrol)
  1752. {
  1753. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1754. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1755. if (ins->adc_input != NULL)
  1756. ucontrol->value.integer.value[0] = 1;
  1757. else
  1758. ucontrol->value.integer.value[0] = 0;
  1759. return 0;
  1760. }
  1761. static int snd_cs46xx_adc_capture_put(struct snd_kcontrol *kcontrol,
  1762. struct snd_ctl_elem_value *ucontrol)
  1763. {
  1764. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1765. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1766. int change = 0;
  1767. if (ucontrol->value.integer.value[0] && !ins->adc_input) {
  1768. cs46xx_dsp_enable_adc_capture(chip);
  1769. change = 1;
  1770. } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
  1771. cs46xx_dsp_disable_adc_capture(chip);
  1772. change = 1;
  1773. }
  1774. return change;
  1775. }
  1776. static int snd_cs46xx_pcm_capture_get(struct snd_kcontrol *kcontrol,
  1777. struct snd_ctl_elem_value *ucontrol)
  1778. {
  1779. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1780. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1781. if (ins->pcm_input != NULL)
  1782. ucontrol->value.integer.value[0] = 1;
  1783. else
  1784. ucontrol->value.integer.value[0] = 0;
  1785. return 0;
  1786. }
  1787. static int snd_cs46xx_pcm_capture_put(struct snd_kcontrol *kcontrol,
  1788. struct snd_ctl_elem_value *ucontrol)
  1789. {
  1790. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1791. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1792. int change = 0;
  1793. if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
  1794. cs46xx_dsp_enable_pcm_capture(chip);
  1795. change = 1;
  1796. } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
  1797. cs46xx_dsp_disable_pcm_capture(chip);
  1798. change = 1;
  1799. }
  1800. return change;
  1801. }
  1802. static int snd_herc_spdif_select_get(struct snd_kcontrol *kcontrol,
  1803. struct snd_ctl_elem_value *ucontrol)
  1804. {
  1805. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1806. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1807. if (val1 & EGPIODR_GPOE0)
  1808. ucontrol->value.integer.value[0] = 1;
  1809. else
  1810. ucontrol->value.integer.value[0] = 0;
  1811. return 0;
  1812. }
  1813. /*
  1814. * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
  1815. */
  1816. static int snd_herc_spdif_select_put(struct snd_kcontrol *kcontrol,
  1817. struct snd_ctl_elem_value *ucontrol)
  1818. {
  1819. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1820. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1821. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  1822. if (ucontrol->value.integer.value[0]) {
  1823. /* optical is default */
  1824. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  1825. EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
  1826. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  1827. EGPIOPTR_GPPT0 | val2); /* open-drain on output */
  1828. } else {
  1829. /* coaxial */
  1830. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
  1831. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
  1832. }
  1833. /* checking diff from the EGPIO direction register
  1834. should be enough */
  1835. return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
  1836. }
  1837. static int snd_cs46xx_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  1838. {
  1839. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1840. uinfo->count = 1;
  1841. return 0;
  1842. }
  1843. static int snd_cs46xx_spdif_default_get(struct snd_kcontrol *kcontrol,
  1844. struct snd_ctl_elem_value *ucontrol)
  1845. {
  1846. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1847. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1848. mutex_lock(&chip->spos_mutex);
  1849. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
  1850. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
  1851. ucontrol->value.iec958.status[2] = 0;
  1852. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
  1853. mutex_unlock(&chip->spos_mutex);
  1854. return 0;
  1855. }
  1856. static int snd_cs46xx_spdif_default_put(struct snd_kcontrol *kcontrol,
  1857. struct snd_ctl_elem_value *ucontrol)
  1858. {
  1859. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1860. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1861. unsigned int val;
  1862. int change;
  1863. mutex_lock(&chip->spos_mutex);
  1864. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1865. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
  1866. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1867. /* left and right validity bit */
  1868. (1 << 13) | (1 << 12);
  1869. change = (unsigned int)ins->spdif_csuv_default != val;
  1870. ins->spdif_csuv_default = val;
  1871. if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
  1872. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1873. mutex_unlock(&chip->spos_mutex);
  1874. return change;
  1875. }
  1876. static int snd_cs46xx_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1877. struct snd_ctl_elem_value *ucontrol)
  1878. {
  1879. ucontrol->value.iec958.status[0] = 0xff;
  1880. ucontrol->value.iec958.status[1] = 0xff;
  1881. ucontrol->value.iec958.status[2] = 0x00;
  1882. ucontrol->value.iec958.status[3] = 0xff;
  1883. return 0;
  1884. }
  1885. static int snd_cs46xx_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1886. struct snd_ctl_elem_value *ucontrol)
  1887. {
  1888. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  1889. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1890. mutex_lock(&chip->spos_mutex);
  1891. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
  1892. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
  1893. ucontrol->value.iec958.status[2] = 0;
  1894. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
  1895. mutex_unlock(&chip->spos_mutex);
  1896. return 0;
  1897. }
  1898. static int snd_cs46xx_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1899. struct snd_ctl_elem_value *ucontrol)
  1900. {
  1901. struct snd_cs46xx * chip = snd_kcontrol_chip(kcontrol);
  1902. struct dsp_spos_instance * ins = chip->dsp_spos_instance;
  1903. unsigned int val;
  1904. int change;
  1905. mutex_lock(&chip->spos_mutex);
  1906. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1907. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
  1908. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1909. /* left and right validity bit */
  1910. (1 << 13) | (1 << 12);
  1911. change = ins->spdif_csuv_stream != val;
  1912. ins->spdif_csuv_stream = val;
  1913. if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
  1914. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1915. mutex_unlock(&chip->spos_mutex);
  1916. return change;
  1917. }
  1918. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  1919. static struct snd_kcontrol_new snd_cs46xx_controls[] = {
  1920. {
  1921. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1922. .name = "DAC Volume",
  1923. .info = snd_cs46xx_vol_info,
  1924. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1925. .get = snd_cs46xx_vol_get,
  1926. .put = snd_cs46xx_vol_put,
  1927. .private_value = BA1_PVOL,
  1928. #else
  1929. .get = snd_cs46xx_vol_dac_get,
  1930. .put = snd_cs46xx_vol_dac_put,
  1931. #endif
  1932. },
  1933. {
  1934. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1935. .name = "ADC Volume",
  1936. .info = snd_cs46xx_vol_info,
  1937. .get = snd_cs46xx_vol_get,
  1938. .put = snd_cs46xx_vol_put,
  1939. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1940. .private_value = BA1_CVOL,
  1941. #else
  1942. .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
  1943. #endif
  1944. },
  1945. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1946. {
  1947. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1948. .name = "ADC Capture Switch",
  1949. .info = snd_mixer_boolean_info,
  1950. .get = snd_cs46xx_adc_capture_get,
  1951. .put = snd_cs46xx_adc_capture_put
  1952. },
  1953. {
  1954. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1955. .name = "DAC Capture Switch",
  1956. .info = snd_mixer_boolean_info,
  1957. .get = snd_cs46xx_pcm_capture_get,
  1958. .put = snd_cs46xx_pcm_capture_put
  1959. },
  1960. {
  1961. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1962. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1963. .info = snd_mixer_boolean_info,
  1964. .get = snd_cs46xx_iec958_get,
  1965. .put = snd_cs46xx_iec958_put,
  1966. .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
  1967. },
  1968. {
  1969. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1970. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,SWITCH),
  1971. .info = snd_mixer_boolean_info,
  1972. .get = snd_cs46xx_iec958_get,
  1973. .put = snd_cs46xx_iec958_put,
  1974. .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
  1975. },
  1976. #if 0
  1977. /* Input IEC958 volume does not work for the moment. (Benny) */
  1978. {
  1979. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1980. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,VOLUME),
  1981. .info = snd_cs46xx_vol_info,
  1982. .get = snd_cs46xx_vol_iec958_get,
  1983. .put = snd_cs46xx_vol_iec958_put,
  1984. .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
  1985. },
  1986. #endif
  1987. {
  1988. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1989. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1990. .info = snd_cs46xx_spdif_info,
  1991. .get = snd_cs46xx_spdif_default_get,
  1992. .put = snd_cs46xx_spdif_default_put,
  1993. },
  1994. {
  1995. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1996. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1997. .info = snd_cs46xx_spdif_info,
  1998. .get = snd_cs46xx_spdif_mask_get,
  1999. .access = SNDRV_CTL_ELEM_ACCESS_READ
  2000. },
  2001. {
  2002. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2003. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2004. .info = snd_cs46xx_spdif_info,
  2005. .get = snd_cs46xx_spdif_stream_get,
  2006. .put = snd_cs46xx_spdif_stream_put
  2007. },
  2008. #endif
  2009. };
  2010. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2011. /* set primary cs4294 codec into Extended Audio Mode */
  2012. static int snd_cs46xx_front_dup_get(struct snd_kcontrol *kcontrol,
  2013. struct snd_ctl_elem_value *ucontrol)
  2014. {
  2015. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  2016. unsigned short val;
  2017. val = snd_ac97_read(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX], AC97_CSR_ACMODE);
  2018. ucontrol->value.integer.value[0] = (val & 0x200) ? 0 : 1;
  2019. return 0;
  2020. }
  2021. static int snd_cs46xx_front_dup_put(struct snd_kcontrol *kcontrol,
  2022. struct snd_ctl_elem_value *ucontrol)
  2023. {
  2024. struct snd_cs46xx *chip = snd_kcontrol_chip(kcontrol);
  2025. return snd_ac97_update_bits(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2026. AC97_CSR_ACMODE, 0x200,
  2027. ucontrol->value.integer.value[0] ? 0 : 0x200);
  2028. }
  2029. static const struct snd_kcontrol_new snd_cs46xx_front_dup_ctl = {
  2030. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2031. .name = "Duplicate Front",
  2032. .info = snd_mixer_boolean_info,
  2033. .get = snd_cs46xx_front_dup_get,
  2034. .put = snd_cs46xx_front_dup_put,
  2035. };
  2036. #endif
  2037. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2038. /* Only available on the Hercules Game Theater XP soundcard */
  2039. static struct snd_kcontrol_new snd_hercules_controls[] = {
  2040. {
  2041. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2042. .name = "Optical/Coaxial SPDIF Input Switch",
  2043. .info = snd_mixer_boolean_info,
  2044. .get = snd_herc_spdif_select_get,
  2045. .put = snd_herc_spdif_select_put,
  2046. },
  2047. };
  2048. static void snd_cs46xx_codec_reset (struct snd_ac97 * ac97)
  2049. {
  2050. unsigned long end_time;
  2051. int err;
  2052. /* reset to defaults */
  2053. snd_ac97_write(ac97, AC97_RESET, 0);
  2054. /* set the desired CODEC mode */
  2055. if (ac97->num == CS46XX_PRIMARY_CODEC_INDEX) {
  2056. dev_dbg(ac97->bus->card->dev, "CODEC1 mode %04x\n", 0x0);
  2057. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x0);
  2058. } else if (ac97->num == CS46XX_SECONDARY_CODEC_INDEX) {
  2059. dev_dbg(ac97->bus->card->dev, "CODEC2 mode %04x\n", 0x3);
  2060. snd_cs46xx_ac97_write(ac97, AC97_CSR_ACMODE, 0x3);
  2061. } else {
  2062. snd_BUG(); /* should never happen ... */
  2063. }
  2064. udelay(50);
  2065. /* it's necessary to wait awhile until registers are accessible after RESET */
  2066. /* because the PCM or MASTER volume registers can be modified, */
  2067. /* the REC_GAIN register is used for tests */
  2068. end_time = jiffies + HZ;
  2069. do {
  2070. unsigned short ext_mid;
  2071. /* use preliminary reads to settle the communication */
  2072. snd_ac97_read(ac97, AC97_RESET);
  2073. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  2074. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  2075. /* modem? */
  2076. ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  2077. if (ext_mid != 0xffff && (ext_mid & 1) != 0)
  2078. return;
  2079. /* test if we can write to the record gain volume register */
  2080. snd_ac97_write(ac97, AC97_REC_GAIN, 0x8a05);
  2081. if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
  2082. return;
  2083. msleep(10);
  2084. } while (time_after_eq(end_time, jiffies));
  2085. dev_err(ac97->bus->card->dev,
  2086. "CS46xx secondary codec doesn't respond!\n");
  2087. }
  2088. #endif
  2089. static int cs46xx_detect_codec(struct snd_cs46xx *chip, int codec)
  2090. {
  2091. int idx, err;
  2092. struct snd_ac97_template ac97;
  2093. memset(&ac97, 0, sizeof(ac97));
  2094. ac97.private_data = chip;
  2095. ac97.private_free = snd_cs46xx_mixer_free_ac97;
  2096. ac97.num = codec;
  2097. if (chip->amplifier_ctrl == amp_voyetra)
  2098. ac97.scaps = AC97_SCAP_INV_EAPD;
  2099. if (codec == CS46XX_SECONDARY_CODEC_INDEX) {
  2100. snd_cs46xx_codec_write(chip, AC97_RESET, 0, codec);
  2101. udelay(10);
  2102. if (snd_cs46xx_codec_read(chip, AC97_RESET, codec) & 0x8000) {
  2103. dev_dbg(chip->card->dev,
  2104. "secondary codec not present\n");
  2105. return -ENXIO;
  2106. }
  2107. }
  2108. snd_cs46xx_codec_write(chip, AC97_MASTER, 0x8000, codec);
  2109. for (idx = 0; idx < 100; ++idx) {
  2110. if (snd_cs46xx_codec_read(chip, AC97_MASTER, codec) == 0x8000) {
  2111. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[codec]);
  2112. return err;
  2113. }
  2114. msleep(10);
  2115. }
  2116. dev_dbg(chip->card->dev, "codec %d detection timeout\n", codec);
  2117. return -ENXIO;
  2118. }
  2119. int snd_cs46xx_mixer(struct snd_cs46xx *chip, int spdif_device)
  2120. {
  2121. struct snd_card *card = chip->card;
  2122. struct snd_ctl_elem_id id;
  2123. int err;
  2124. unsigned int idx;
  2125. static struct snd_ac97_bus_ops ops = {
  2126. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2127. .reset = snd_cs46xx_codec_reset,
  2128. #endif
  2129. .write = snd_cs46xx_ac97_write,
  2130. .read = snd_cs46xx_ac97_read,
  2131. };
  2132. /* detect primary codec */
  2133. chip->nr_ac97_codecs = 0;
  2134. dev_dbg(chip->card->dev, "detecting primary codec\n");
  2135. if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  2136. return err;
  2137. chip->ac97_bus->private_free = snd_cs46xx_mixer_free_ac97_bus;
  2138. if (cs46xx_detect_codec(chip, CS46XX_PRIMARY_CODEC_INDEX) < 0)
  2139. return -ENXIO;
  2140. chip->nr_ac97_codecs = 1;
  2141. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2142. dev_dbg(chip->card->dev, "detecting secondary codec\n");
  2143. /* try detect a secondary codec */
  2144. if (! cs46xx_detect_codec(chip, CS46XX_SECONDARY_CODEC_INDEX))
  2145. chip->nr_ac97_codecs = 2;
  2146. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  2147. /* add cs4630 mixer controls */
  2148. for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
  2149. struct snd_kcontrol *kctl;
  2150. kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
  2151. if (kctl && kctl->id.iface == SNDRV_CTL_ELEM_IFACE_PCM)
  2152. kctl->id.device = spdif_device;
  2153. if ((err = snd_ctl_add(card, kctl)) < 0)
  2154. return err;
  2155. }
  2156. /* get EAPD mixer switch (for voyetra hack) */
  2157. memset(&id, 0, sizeof(id));
  2158. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2159. strcpy(id.name, "External Amplifier");
  2160. chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
  2161. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2162. if (chip->nr_ac97_codecs == 1) {
  2163. unsigned int id2 = chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]->id & 0xffff;
  2164. if ((id2 & 0xfff0) == 0x5920) { /* CS4294 and CS4298 */
  2165. err = snd_ctl_add(card, snd_ctl_new1(&snd_cs46xx_front_dup_ctl, chip));
  2166. if (err < 0)
  2167. return err;
  2168. snd_ac97_write_cache(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2169. AC97_CSR_ACMODE, 0x200);
  2170. }
  2171. }
  2172. /* do soundcard specific mixer setup */
  2173. if (chip->mixer_init) {
  2174. dev_dbg(chip->card->dev, "calling chip->mixer_init(chip);\n");
  2175. chip->mixer_init(chip);
  2176. }
  2177. #endif
  2178. /* turn on amplifier */
  2179. chip->amplifier_ctrl(chip, 1);
  2180. return 0;
  2181. }
  2182. /*
  2183. * RawMIDI interface
  2184. */
  2185. static void snd_cs46xx_midi_reset(struct snd_cs46xx *chip)
  2186. {
  2187. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
  2188. udelay(100);
  2189. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2190. }
  2191. static int snd_cs46xx_midi_input_open(struct snd_rawmidi_substream *substream)
  2192. {
  2193. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2194. chip->active_ctrl(chip, 1);
  2195. spin_lock_irq(&chip->reg_lock);
  2196. chip->uartm |= CS46XX_MODE_INPUT;
  2197. chip->midcr |= MIDCR_RXE;
  2198. chip->midi_input = substream;
  2199. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2200. snd_cs46xx_midi_reset(chip);
  2201. } else {
  2202. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2203. }
  2204. spin_unlock_irq(&chip->reg_lock);
  2205. return 0;
  2206. }
  2207. static int snd_cs46xx_midi_input_close(struct snd_rawmidi_substream *substream)
  2208. {
  2209. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2210. spin_lock_irq(&chip->reg_lock);
  2211. chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
  2212. chip->midi_input = NULL;
  2213. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2214. snd_cs46xx_midi_reset(chip);
  2215. } else {
  2216. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2217. }
  2218. chip->uartm &= ~CS46XX_MODE_INPUT;
  2219. spin_unlock_irq(&chip->reg_lock);
  2220. chip->active_ctrl(chip, -1);
  2221. return 0;
  2222. }
  2223. static int snd_cs46xx_midi_output_open(struct snd_rawmidi_substream *substream)
  2224. {
  2225. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2226. chip->active_ctrl(chip, 1);
  2227. spin_lock_irq(&chip->reg_lock);
  2228. chip->uartm |= CS46XX_MODE_OUTPUT;
  2229. chip->midcr |= MIDCR_TXE;
  2230. chip->midi_output = substream;
  2231. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2232. snd_cs46xx_midi_reset(chip);
  2233. } else {
  2234. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2235. }
  2236. spin_unlock_irq(&chip->reg_lock);
  2237. return 0;
  2238. }
  2239. static int snd_cs46xx_midi_output_close(struct snd_rawmidi_substream *substream)
  2240. {
  2241. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2242. spin_lock_irq(&chip->reg_lock);
  2243. chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
  2244. chip->midi_output = NULL;
  2245. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2246. snd_cs46xx_midi_reset(chip);
  2247. } else {
  2248. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2249. }
  2250. chip->uartm &= ~CS46XX_MODE_OUTPUT;
  2251. spin_unlock_irq(&chip->reg_lock);
  2252. chip->active_ctrl(chip, -1);
  2253. return 0;
  2254. }
  2255. static void snd_cs46xx_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  2256. {
  2257. unsigned long flags;
  2258. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2259. spin_lock_irqsave(&chip->reg_lock, flags);
  2260. if (up) {
  2261. if ((chip->midcr & MIDCR_RIE) == 0) {
  2262. chip->midcr |= MIDCR_RIE;
  2263. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2264. }
  2265. } else {
  2266. if (chip->midcr & MIDCR_RIE) {
  2267. chip->midcr &= ~MIDCR_RIE;
  2268. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2269. }
  2270. }
  2271. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2272. }
  2273. static void snd_cs46xx_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  2274. {
  2275. unsigned long flags;
  2276. struct snd_cs46xx *chip = substream->rmidi->private_data;
  2277. unsigned char byte;
  2278. spin_lock_irqsave(&chip->reg_lock, flags);
  2279. if (up) {
  2280. if ((chip->midcr & MIDCR_TIE) == 0) {
  2281. chip->midcr |= MIDCR_TIE;
  2282. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  2283. while ((chip->midcr & MIDCR_TIE) &&
  2284. (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  2285. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  2286. chip->midcr &= ~MIDCR_TIE;
  2287. } else {
  2288. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
  2289. }
  2290. }
  2291. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2292. }
  2293. } else {
  2294. if (chip->midcr & MIDCR_TIE) {
  2295. chip->midcr &= ~MIDCR_TIE;
  2296. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2297. }
  2298. }
  2299. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2300. }
  2301. static const struct snd_rawmidi_ops snd_cs46xx_midi_output =
  2302. {
  2303. .open = snd_cs46xx_midi_output_open,
  2304. .close = snd_cs46xx_midi_output_close,
  2305. .trigger = snd_cs46xx_midi_output_trigger,
  2306. };
  2307. static const struct snd_rawmidi_ops snd_cs46xx_midi_input =
  2308. {
  2309. .open = snd_cs46xx_midi_input_open,
  2310. .close = snd_cs46xx_midi_input_close,
  2311. .trigger = snd_cs46xx_midi_input_trigger,
  2312. };
  2313. int snd_cs46xx_midi(struct snd_cs46xx *chip, int device)
  2314. {
  2315. struct snd_rawmidi *rmidi;
  2316. int err;
  2317. if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
  2318. return err;
  2319. strcpy(rmidi->name, "CS46XX");
  2320. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
  2321. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
  2322. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  2323. rmidi->private_data = chip;
  2324. chip->rmidi = rmidi;
  2325. return 0;
  2326. }
  2327. /*
  2328. * gameport interface
  2329. */
  2330. #if IS_REACHABLE(CONFIG_GAMEPORT)
  2331. static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
  2332. {
  2333. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2334. if (snd_BUG_ON(!chip))
  2335. return;
  2336. snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
  2337. }
  2338. static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
  2339. {
  2340. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2341. if (snd_BUG_ON(!chip))
  2342. return 0;
  2343. return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
  2344. }
  2345. static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2346. {
  2347. struct snd_cs46xx *chip = gameport_get_port_data(gameport);
  2348. unsigned js1, js2, jst;
  2349. if (snd_BUG_ON(!chip))
  2350. return 0;
  2351. js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
  2352. js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
  2353. jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
  2354. *buttons = (~jst >> 4) & 0x0F;
  2355. axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
  2356. axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
  2357. axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
  2358. axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
  2359. for(jst=0;jst<4;++jst)
  2360. if(axes[jst]==0xFFFF) axes[jst] = -1;
  2361. return 0;
  2362. }
  2363. static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
  2364. {
  2365. switch (mode) {
  2366. case GAMEPORT_MODE_COOKED:
  2367. return 0;
  2368. case GAMEPORT_MODE_RAW:
  2369. return 0;
  2370. default:
  2371. return -1;
  2372. }
  2373. return 0;
  2374. }
  2375. int snd_cs46xx_gameport(struct snd_cs46xx *chip)
  2376. {
  2377. struct gameport *gp;
  2378. chip->gameport = gp = gameport_allocate_port();
  2379. if (!gp) {
  2380. dev_err(chip->card->dev,
  2381. "cannot allocate memory for gameport\n");
  2382. return -ENOMEM;
  2383. }
  2384. gameport_set_name(gp, "CS46xx Gameport");
  2385. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2386. gameport_set_dev_parent(gp, &chip->pci->dev);
  2387. gameport_set_port_data(gp, chip);
  2388. gp->open = snd_cs46xx_gameport_open;
  2389. gp->read = snd_cs46xx_gameport_read;
  2390. gp->trigger = snd_cs46xx_gameport_trigger;
  2391. gp->cooked_read = snd_cs46xx_gameport_cooked_read;
  2392. snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
  2393. snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
  2394. gameport_register_port(gp);
  2395. return 0;
  2396. }
  2397. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip)
  2398. {
  2399. if (chip->gameport) {
  2400. gameport_unregister_port(chip->gameport);
  2401. chip->gameport = NULL;
  2402. }
  2403. }
  2404. #else
  2405. int snd_cs46xx_gameport(struct snd_cs46xx *chip) { return -ENOSYS; }
  2406. static inline void snd_cs46xx_remove_gameport(struct snd_cs46xx *chip) { }
  2407. #endif /* CONFIG_GAMEPORT */
  2408. #ifdef CONFIG_SND_PROC_FS
  2409. /*
  2410. * proc interface
  2411. */
  2412. static ssize_t snd_cs46xx_io_read(struct snd_info_entry *entry,
  2413. void *file_private_data,
  2414. struct file *file, char __user *buf,
  2415. size_t count, loff_t pos)
  2416. {
  2417. struct snd_cs46xx_region *region = entry->private_data;
  2418. if (copy_to_user_fromio(buf, region->remap_addr + pos, count))
  2419. return -EFAULT;
  2420. return count;
  2421. }
  2422. static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
  2423. .read = snd_cs46xx_io_read,
  2424. };
  2425. static int snd_cs46xx_proc_init(struct snd_card *card, struct snd_cs46xx *chip)
  2426. {
  2427. struct snd_info_entry *entry;
  2428. int idx;
  2429. for (idx = 0; idx < 5; idx++) {
  2430. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2431. if (! snd_card_proc_new(card, region->name, &entry)) {
  2432. entry->content = SNDRV_INFO_CONTENT_DATA;
  2433. entry->private_data = chip;
  2434. entry->c.ops = &snd_cs46xx_proc_io_ops;
  2435. entry->size = region->size;
  2436. entry->mode = S_IFREG | S_IRUSR;
  2437. }
  2438. }
  2439. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2440. cs46xx_dsp_proc_init(card, chip);
  2441. #endif
  2442. return 0;
  2443. }
  2444. static int snd_cs46xx_proc_done(struct snd_cs46xx *chip)
  2445. {
  2446. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2447. cs46xx_dsp_proc_done(chip);
  2448. #endif
  2449. return 0;
  2450. }
  2451. #else /* !CONFIG_SND_PROC_FS */
  2452. #define snd_cs46xx_proc_init(card, chip)
  2453. #define snd_cs46xx_proc_done(chip)
  2454. #endif
  2455. /*
  2456. * stop the h/w
  2457. */
  2458. static void snd_cs46xx_hw_stop(struct snd_cs46xx *chip)
  2459. {
  2460. unsigned int tmp;
  2461. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2462. tmp &= ~0x0000f03f;
  2463. tmp |= 0x00000010;
  2464. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
  2465. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2466. tmp &= ~0x0000003f;
  2467. tmp |= 0x00000011;
  2468. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
  2469. /*
  2470. * Stop playback DMA.
  2471. */
  2472. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2473. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2474. /*
  2475. * Stop capture DMA.
  2476. */
  2477. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2478. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2479. /*
  2480. * Reset the processor.
  2481. */
  2482. snd_cs46xx_reset(chip);
  2483. snd_cs46xx_proc_stop(chip);
  2484. /*
  2485. * Power down the PLL.
  2486. */
  2487. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2488. /*
  2489. * Turn off the Processor by turning off the software clock enable flag in
  2490. * the clock control register.
  2491. */
  2492. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
  2493. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2494. }
  2495. static int snd_cs46xx_free(struct snd_cs46xx *chip)
  2496. {
  2497. int idx;
  2498. if (snd_BUG_ON(!chip))
  2499. return -EINVAL;
  2500. if (chip->active_ctrl)
  2501. chip->active_ctrl(chip, 1);
  2502. snd_cs46xx_remove_gameport(chip);
  2503. if (chip->amplifier_ctrl)
  2504. chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
  2505. snd_cs46xx_proc_done(chip);
  2506. if (chip->region.idx[0].resource)
  2507. snd_cs46xx_hw_stop(chip);
  2508. if (chip->irq >= 0)
  2509. free_irq(chip->irq, chip);
  2510. if (chip->active_ctrl)
  2511. chip->active_ctrl(chip, -chip->amplifier);
  2512. for (idx = 0; idx < 5; idx++) {
  2513. struct snd_cs46xx_region *region = &chip->region.idx[idx];
  2514. iounmap(region->remap_addr);
  2515. release_and_free_resource(region->resource);
  2516. }
  2517. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2518. if (chip->dsp_spos_instance) {
  2519. cs46xx_dsp_spos_destroy(chip);
  2520. chip->dsp_spos_instance = NULL;
  2521. }
  2522. for (idx = 0; idx < CS46XX_DSP_MODULES; idx++)
  2523. free_module_desc(chip->modules[idx]);
  2524. #else
  2525. vfree(chip->ba1);
  2526. #endif
  2527. #ifdef CONFIG_PM_SLEEP
  2528. kfree(chip->saved_regs);
  2529. #endif
  2530. pci_disable_device(chip->pci);
  2531. kfree(chip);
  2532. return 0;
  2533. }
  2534. static int snd_cs46xx_dev_free(struct snd_device *device)
  2535. {
  2536. struct snd_cs46xx *chip = device->device_data;
  2537. return snd_cs46xx_free(chip);
  2538. }
  2539. /*
  2540. * initialize chip
  2541. */
  2542. static int snd_cs46xx_chip_init(struct snd_cs46xx *chip)
  2543. {
  2544. int timeout;
  2545. /*
  2546. * First, blast the clock control register to zero so that the PLL starts
  2547. * out in a known state, and blast the master serial port control register
  2548. * to zero so that the serial ports also start out in a known state.
  2549. */
  2550. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2551. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
  2552. /*
  2553. * If we are in AC97 mode, then we must set the part to a host controlled
  2554. * AC-link. Otherwise, we won't be able to bring up the link.
  2555. */
  2556. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2557. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
  2558. SERACC_TWO_CODECS); /* 2.00 dual codecs */
  2559. /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
  2560. #else
  2561. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
  2562. #endif
  2563. /*
  2564. * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
  2565. * spec) and then drive it high. This is done for non AC97 modes since
  2566. * there might be logic external to the CS461x that uses the ARST# line
  2567. * for a reset.
  2568. */
  2569. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
  2570. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2571. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
  2572. #endif
  2573. udelay(50);
  2574. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
  2575. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2576. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
  2577. #endif
  2578. /*
  2579. * The first thing we do here is to enable sync generation. As soon
  2580. * as we start receiving bit clock, we'll start producing the SYNC
  2581. * signal.
  2582. */
  2583. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
  2584. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2585. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
  2586. #endif
  2587. /*
  2588. * Now wait for a short while to allow the AC97 part to start
  2589. * generating bit clock (so we don't try to start the PLL without an
  2590. * input clock).
  2591. */
  2592. mdelay(10);
  2593. /*
  2594. * Set the serial port timing configuration, so that
  2595. * the clock control circuit gets its clock from the correct place.
  2596. */
  2597. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
  2598. /*
  2599. * Write the selected clock control setup to the hardware. Do not turn on
  2600. * SWCE yet (if requested), so that the devices clocked by the output of
  2601. * PLL are not clocked until the PLL is stable.
  2602. */
  2603. snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
  2604. snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
  2605. snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
  2606. /*
  2607. * Power up the PLL.
  2608. */
  2609. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
  2610. /*
  2611. * Wait until the PLL has stabilized.
  2612. */
  2613. msleep(100);
  2614. /*
  2615. * Turn on clocking of the core so that we can setup the serial ports.
  2616. */
  2617. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
  2618. /*
  2619. * Enable FIFO Host Bypass
  2620. */
  2621. snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
  2622. /*
  2623. * Fill the serial port FIFOs with silence.
  2624. */
  2625. snd_cs46xx_clear_serial_FIFOs(chip);
  2626. /*
  2627. * Set the serial port FIFO pointer to the first sample in the FIFO.
  2628. */
  2629. /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
  2630. /*
  2631. * Write the serial port configuration to the part. The master
  2632. * enable bit is not set until all other values have been written.
  2633. */
  2634. snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
  2635. snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
  2636. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
  2637. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2638. snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
  2639. snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
  2640. snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
  2641. snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
  2642. snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
  2643. #endif
  2644. mdelay(5);
  2645. /*
  2646. * Wait for the codec ready signal from the AC97 codec.
  2647. */
  2648. timeout = 150;
  2649. while (timeout-- > 0) {
  2650. /*
  2651. * Read the AC97 status register to see if we've seen a CODEC READY
  2652. * signal from the AC97 codec.
  2653. */
  2654. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
  2655. goto ok1;
  2656. msleep(10);
  2657. }
  2658. dev_err(chip->card->dev,
  2659. "create - never read codec ready from AC'97\n");
  2660. dev_err(chip->card->dev,
  2661. "it is not probably bug, try to use CS4236 driver\n");
  2662. return -EIO;
  2663. ok1:
  2664. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2665. {
  2666. int count;
  2667. for (count = 0; count < 150; count++) {
  2668. /* First, we want to wait for a short time. */
  2669. udelay(25);
  2670. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
  2671. break;
  2672. }
  2673. /*
  2674. * Make sure CODEC is READY.
  2675. */
  2676. if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
  2677. dev_dbg(chip->card->dev,
  2678. "never read card ready from secondary AC'97\n");
  2679. }
  2680. #endif
  2681. /*
  2682. * Assert the vaid frame signal so that we can start sending commands
  2683. * to the AC97 codec.
  2684. */
  2685. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2686. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2687. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2688. #endif
  2689. /*
  2690. * Wait until we've sampled input slots 3 and 4 as valid, meaning that
  2691. * the codec is pumping ADC data across the AC-link.
  2692. */
  2693. timeout = 150;
  2694. while (timeout-- > 0) {
  2695. /*
  2696. * Read the input slot valid register and see if input slots 3 and
  2697. * 4 are valid yet.
  2698. */
  2699. if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
  2700. goto ok2;
  2701. msleep(10);
  2702. }
  2703. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2704. dev_err(chip->card->dev,
  2705. "create - never read ISV3 & ISV4 from AC'97\n");
  2706. return -EIO;
  2707. #else
  2708. /* This may happen on a cold boot with a Terratec SiXPack 5.1.
  2709. Reloading the driver may help, if there's other soundcards
  2710. with the same problem I would like to know. (Benny) */
  2711. dev_err(chip->card->dev, "never read ISV3 & ISV4 from AC'97\n");
  2712. dev_err(chip->card->dev,
  2713. "Try reloading the ALSA driver, if you find something\n");
  2714. dev_err(chip->card->dev,
  2715. "broken or not working on your soundcard upon\n");
  2716. dev_err(chip->card->dev,
  2717. "this message please report to alsa-devel@alsa-project.org\n");
  2718. return -EIO;
  2719. #endif
  2720. ok2:
  2721. /*
  2722. * Now, assert valid frame and the slot 3 and 4 valid bits. This will
  2723. * commense the transfer of digital audio data to the AC97 codec.
  2724. */
  2725. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
  2726. /*
  2727. * Power down the DAC and ADC. We will power them up (if) when we need
  2728. * them.
  2729. */
  2730. /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
  2731. /*
  2732. * Turn off the Processor by turning off the software clock enable flag in
  2733. * the clock control register.
  2734. */
  2735. /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
  2736. /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
  2737. return 0;
  2738. }
  2739. /*
  2740. * start and load DSP
  2741. */
  2742. static void cs46xx_enable_stream_irqs(struct snd_cs46xx *chip)
  2743. {
  2744. unsigned int tmp;
  2745. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
  2746. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2747. tmp &= ~0x0000f03f;
  2748. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
  2749. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2750. tmp &= ~0x0000003f;
  2751. tmp |= 0x00000001;
  2752. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
  2753. }
  2754. int snd_cs46xx_start_dsp(struct snd_cs46xx *chip)
  2755. {
  2756. unsigned int tmp;
  2757. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2758. int i;
  2759. #endif
  2760. int err;
  2761. /*
  2762. * Reset the processor.
  2763. */
  2764. snd_cs46xx_reset(chip);
  2765. /*
  2766. * Download the image to the processor.
  2767. */
  2768. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2769. for (i = 0; i < CS46XX_DSP_MODULES; i++) {
  2770. err = load_firmware(chip, &chip->modules[i], module_names[i]);
  2771. if (err < 0) {
  2772. dev_err(chip->card->dev, "firmware load error [%s]\n",
  2773. module_names[i]);
  2774. return err;
  2775. }
  2776. err = cs46xx_dsp_load_module(chip, chip->modules[i]);
  2777. if (err < 0) {
  2778. dev_err(chip->card->dev, "image download error [%s]\n",
  2779. module_names[i]);
  2780. return err;
  2781. }
  2782. }
  2783. if (cs46xx_dsp_scb_and_task_init(chip) < 0)
  2784. return -EIO;
  2785. #else
  2786. err = load_firmware(chip);
  2787. if (err < 0)
  2788. return err;
  2789. /* old image */
  2790. err = snd_cs46xx_download_image(chip);
  2791. if (err < 0) {
  2792. dev_err(chip->card->dev, "image download error\n");
  2793. return err;
  2794. }
  2795. /*
  2796. * Stop playback DMA.
  2797. */
  2798. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2799. chip->play_ctl = tmp & 0xffff0000;
  2800. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2801. #endif
  2802. /*
  2803. * Stop capture DMA.
  2804. */
  2805. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2806. chip->capt.ctl = tmp & 0x0000ffff;
  2807. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2808. mdelay(5);
  2809. snd_cs46xx_set_play_sample_rate(chip, 8000);
  2810. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  2811. snd_cs46xx_proc_start(chip);
  2812. cs46xx_enable_stream_irqs(chip);
  2813. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2814. /* set the attenuation to 0dB */
  2815. snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
  2816. snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
  2817. #endif
  2818. return 0;
  2819. }
  2820. /*
  2821. * AMP control - null AMP
  2822. */
  2823. static void amp_none(struct snd_cs46xx *chip, int change)
  2824. {
  2825. }
  2826. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2827. static int voyetra_setup_eapd_slot(struct snd_cs46xx *chip)
  2828. {
  2829. u32 idx, valid_slots,tmp,powerdown = 0;
  2830. u16 modem_power,pin_config,logic_type;
  2831. dev_dbg(chip->card->dev, "cs46xx_setup_eapd_slot()+\n");
  2832. /*
  2833. * See if the devices are powered down. If so, we must power them up first
  2834. * or they will not respond.
  2835. */
  2836. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  2837. if (!(tmp & CLKCR1_SWCE)) {
  2838. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  2839. powerdown = 1;
  2840. }
  2841. /*
  2842. * Clear PRA. The Bonzo chip will be used for GPIO not for modem
  2843. * stuff.
  2844. */
  2845. if(chip->nr_ac97_codecs != 2) {
  2846. dev_err(chip->card->dev,
  2847. "cs46xx_setup_eapd_slot() - no secondary codec configured\n");
  2848. return -EINVAL;
  2849. }
  2850. modem_power = snd_cs46xx_codec_read (chip,
  2851. AC97_EXTENDED_MSTATUS,
  2852. CS46XX_SECONDARY_CODEC_INDEX);
  2853. modem_power &=0xFEFF;
  2854. snd_cs46xx_codec_write(chip,
  2855. AC97_EXTENDED_MSTATUS, modem_power,
  2856. CS46XX_SECONDARY_CODEC_INDEX);
  2857. /*
  2858. * Set GPIO pin's 7 and 8 so that they are configured for output.
  2859. */
  2860. pin_config = snd_cs46xx_codec_read (chip,
  2861. AC97_GPIO_CFG,
  2862. CS46XX_SECONDARY_CODEC_INDEX);
  2863. pin_config &=0x27F;
  2864. snd_cs46xx_codec_write(chip,
  2865. AC97_GPIO_CFG, pin_config,
  2866. CS46XX_SECONDARY_CODEC_INDEX);
  2867. /*
  2868. * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
  2869. */
  2870. logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
  2871. CS46XX_SECONDARY_CODEC_INDEX);
  2872. logic_type &=0x27F;
  2873. snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
  2874. CS46XX_SECONDARY_CODEC_INDEX);
  2875. valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
  2876. valid_slots |= 0x200;
  2877. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
  2878. if ( cs46xx_wait_for_fifo(chip,1) ) {
  2879. dev_dbg(chip->card->dev, "FIFO is busy\n");
  2880. return -EINVAL;
  2881. }
  2882. /*
  2883. * Fill slots 12 with the correct value for the GPIO pins.
  2884. */
  2885. for(idx = 0x90; idx <= 0x9F; idx++) {
  2886. /*
  2887. * Initialize the fifo so that bits 7 and 8 are on.
  2888. *
  2889. * Remember that the GPIO pins in bonzo are shifted by 4 bits to
  2890. * the left. 0x1800 corresponds to bits 7 and 8.
  2891. */
  2892. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
  2893. /*
  2894. * Wait for command to complete
  2895. */
  2896. if ( cs46xx_wait_for_fifo(chip,200) ) {
  2897. dev_dbg(chip->card->dev,
  2898. "failed waiting for FIFO at addr (%02X)\n",
  2899. idx);
  2900. return -EINVAL;
  2901. }
  2902. /*
  2903. * Write the serial port FIFO index.
  2904. */
  2905. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  2906. /*
  2907. * Tell the serial port to load the new value into the FIFO location.
  2908. */
  2909. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  2910. }
  2911. /* wait for last command to complete */
  2912. cs46xx_wait_for_fifo(chip,200);
  2913. /*
  2914. * Now, if we powered up the devices, then power them back down again.
  2915. * This is kinda ugly, but should never happen.
  2916. */
  2917. if (powerdown)
  2918. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2919. return 0;
  2920. }
  2921. #endif
  2922. /*
  2923. * Crystal EAPD mode
  2924. */
  2925. static void amp_voyetra(struct snd_cs46xx *chip, int change)
  2926. {
  2927. /* Manage the EAPD bit on the Crystal 4297
  2928. and the Analog AD1885 */
  2929. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2930. int old = chip->amplifier;
  2931. #endif
  2932. int oval, val;
  2933. chip->amplifier += change;
  2934. oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
  2935. CS46XX_PRIMARY_CODEC_INDEX);
  2936. val = oval;
  2937. if (chip->amplifier) {
  2938. /* Turn the EAPD amp on */
  2939. val |= 0x8000;
  2940. } else {
  2941. /* Turn the EAPD amp off */
  2942. val &= ~0x8000;
  2943. }
  2944. if (val != oval) {
  2945. snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
  2946. CS46XX_PRIMARY_CODEC_INDEX);
  2947. if (chip->eapd_switch)
  2948. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2949. &chip->eapd_switch->id);
  2950. }
  2951. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2952. if (chip->amplifier && !old) {
  2953. voyetra_setup_eapd_slot(chip);
  2954. }
  2955. #endif
  2956. }
  2957. static void hercules_init(struct snd_cs46xx *chip)
  2958. {
  2959. /* default: AMP off, and SPDIF input optical */
  2960. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2961. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2962. }
  2963. /*
  2964. * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
  2965. */
  2966. static void amp_hercules(struct snd_cs46xx *chip, int change)
  2967. {
  2968. int old = chip->amplifier;
  2969. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  2970. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  2971. chip->amplifier += change;
  2972. if (chip->amplifier && !old) {
  2973. dev_dbg(chip->card->dev, "Hercules amplifier ON\n");
  2974. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  2975. EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
  2976. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  2977. EGPIOPTR_GPPT2 | val2); /* open-drain on output */
  2978. } else if (old && !chip->amplifier) {
  2979. dev_dbg(chip->card->dev, "Hercules amplifier OFF\n");
  2980. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
  2981. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
  2982. }
  2983. }
  2984. static void voyetra_mixer_init (struct snd_cs46xx *chip)
  2985. {
  2986. dev_dbg(chip->card->dev, "initializing Voyetra mixer\n");
  2987. /* Enable SPDIF out */
  2988. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2989. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2990. }
  2991. static void hercules_mixer_init (struct snd_cs46xx *chip)
  2992. {
  2993. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2994. unsigned int idx;
  2995. int err;
  2996. struct snd_card *card = chip->card;
  2997. #endif
  2998. /* set EGPIO to default */
  2999. hercules_init(chip);
  3000. dev_dbg(chip->card->dev, "initializing Hercules mixer\n");
  3001. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3002. if (chip->in_suspend)
  3003. return;
  3004. for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
  3005. struct snd_kcontrol *kctl;
  3006. kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
  3007. if ((err = snd_ctl_add(card, kctl)) < 0) {
  3008. dev_err(card->dev,
  3009. "failed to initialize Hercules mixer (%d)\n",
  3010. err);
  3011. break;
  3012. }
  3013. }
  3014. #endif
  3015. }
  3016. #if 0
  3017. /*
  3018. * Untested
  3019. */
  3020. static void amp_voyetra_4294(struct snd_cs46xx *chip, int change)
  3021. {
  3022. chip->amplifier += change;
  3023. if (chip->amplifier) {
  3024. /* Switch the GPIO pins 7 and 8 to open drain */
  3025. snd_cs46xx_codec_write(chip, 0x4C,
  3026. snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
  3027. snd_cs46xx_codec_write(chip, 0x4E,
  3028. snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
  3029. /* Now wake the AMP (this might be backwards) */
  3030. snd_cs46xx_codec_write(chip, 0x54,
  3031. snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
  3032. } else {
  3033. snd_cs46xx_codec_write(chip, 0x54,
  3034. snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
  3035. }
  3036. }
  3037. #endif
  3038. /*
  3039. * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
  3040. * whenever we need to beat on the chip.
  3041. *
  3042. * The original idea and code for this hack comes from David Kaiser at
  3043. * Linuxcare. Perhaps one day Crystal will document their chips well
  3044. * enough to make them useful.
  3045. */
  3046. static void clkrun_hack(struct snd_cs46xx *chip, int change)
  3047. {
  3048. u16 control, nval;
  3049. if (!chip->acpi_port)
  3050. return;
  3051. chip->amplifier += change;
  3052. /* Read ACPI port */
  3053. nval = control = inw(chip->acpi_port + 0x10);
  3054. /* Flip CLKRUN off while running */
  3055. if (! chip->amplifier)
  3056. nval |= 0x2000;
  3057. else
  3058. nval &= ~0x2000;
  3059. if (nval != control)
  3060. outw(nval, chip->acpi_port + 0x10);
  3061. }
  3062. /*
  3063. * detect intel piix4
  3064. */
  3065. static void clkrun_init(struct snd_cs46xx *chip)
  3066. {
  3067. struct pci_dev *pdev;
  3068. u8 pp;
  3069. chip->acpi_port = 0;
  3070. pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
  3071. PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  3072. if (pdev == NULL)
  3073. return; /* Not a thinkpad thats for sure */
  3074. /* Find the control port */
  3075. pci_read_config_byte(pdev, 0x41, &pp);
  3076. chip->acpi_port = pp << 8;
  3077. pci_dev_put(pdev);
  3078. }
  3079. /*
  3080. * Card subid table
  3081. */
  3082. struct cs_card_type
  3083. {
  3084. u16 vendor;
  3085. u16 id;
  3086. char *name;
  3087. void (*init)(struct snd_cs46xx *);
  3088. void (*amp)(struct snd_cs46xx *, int);
  3089. void (*active)(struct snd_cs46xx *, int);
  3090. void (*mixer_init)(struct snd_cs46xx *);
  3091. };
  3092. static struct cs_card_type cards[] = {
  3093. {
  3094. .vendor = 0x1489,
  3095. .id = 0x7001,
  3096. .name = "Genius Soundmaker 128 value",
  3097. /* nothing special */
  3098. },
  3099. {
  3100. .vendor = 0x5053,
  3101. .id = 0x3357,
  3102. .name = "Voyetra",
  3103. .amp = amp_voyetra,
  3104. .mixer_init = voyetra_mixer_init,
  3105. },
  3106. {
  3107. .vendor = 0x1071,
  3108. .id = 0x6003,
  3109. .name = "Mitac MI6020/21",
  3110. .amp = amp_voyetra,
  3111. },
  3112. /* Hercules Game Theatre XP */
  3113. {
  3114. .vendor = 0x14af, /* Guillemot Corporation */
  3115. .id = 0x0050,
  3116. .name = "Hercules Game Theatre XP",
  3117. .amp = amp_hercules,
  3118. .mixer_init = hercules_mixer_init,
  3119. },
  3120. {
  3121. .vendor = 0x1681,
  3122. .id = 0x0050,
  3123. .name = "Hercules Game Theatre XP",
  3124. .amp = amp_hercules,
  3125. .mixer_init = hercules_mixer_init,
  3126. },
  3127. {
  3128. .vendor = 0x1681,
  3129. .id = 0x0051,
  3130. .name = "Hercules Game Theatre XP",
  3131. .amp = amp_hercules,
  3132. .mixer_init = hercules_mixer_init,
  3133. },
  3134. {
  3135. .vendor = 0x1681,
  3136. .id = 0x0052,
  3137. .name = "Hercules Game Theatre XP",
  3138. .amp = amp_hercules,
  3139. .mixer_init = hercules_mixer_init,
  3140. },
  3141. {
  3142. .vendor = 0x1681,
  3143. .id = 0x0053,
  3144. .name = "Hercules Game Theatre XP",
  3145. .amp = amp_hercules,
  3146. .mixer_init = hercules_mixer_init,
  3147. },
  3148. {
  3149. .vendor = 0x1681,
  3150. .id = 0x0054,
  3151. .name = "Hercules Game Theatre XP",
  3152. .amp = amp_hercules,
  3153. .mixer_init = hercules_mixer_init,
  3154. },
  3155. /* Herculess Fortissimo */
  3156. {
  3157. .vendor = 0x1681,
  3158. .id = 0xa010,
  3159. .name = "Hercules Gamesurround Fortissimo II",
  3160. },
  3161. {
  3162. .vendor = 0x1681,
  3163. .id = 0xa011,
  3164. .name = "Hercules Gamesurround Fortissimo III 7.1",
  3165. },
  3166. /* Teratec */
  3167. {
  3168. .vendor = 0x153b,
  3169. .id = 0x112e,
  3170. .name = "Terratec DMX XFire 1024",
  3171. },
  3172. {
  3173. .vendor = 0x153b,
  3174. .id = 0x1136,
  3175. .name = "Terratec SiXPack 5.1",
  3176. },
  3177. /* Not sure if the 570 needs the clkrun hack */
  3178. {
  3179. .vendor = PCI_VENDOR_ID_IBM,
  3180. .id = 0x0132,
  3181. .name = "Thinkpad 570",
  3182. .init = clkrun_init,
  3183. .active = clkrun_hack,
  3184. },
  3185. {
  3186. .vendor = PCI_VENDOR_ID_IBM,
  3187. .id = 0x0153,
  3188. .name = "Thinkpad 600X/A20/T20",
  3189. .init = clkrun_init,
  3190. .active = clkrun_hack,
  3191. },
  3192. {
  3193. .vendor = PCI_VENDOR_ID_IBM,
  3194. .id = 0x1010,
  3195. .name = "Thinkpad 600E (unsupported)",
  3196. },
  3197. {} /* terminator */
  3198. };
  3199. /*
  3200. * APM support
  3201. */
  3202. #ifdef CONFIG_PM_SLEEP
  3203. static unsigned int saved_regs[] = {
  3204. BA0_ACOSV,
  3205. /*BA0_ASER_FADDR,*/
  3206. BA0_ASER_MASTER,
  3207. BA1_PVOL,
  3208. BA1_CVOL,
  3209. };
  3210. static int snd_cs46xx_suspend(struct device *dev)
  3211. {
  3212. struct snd_card *card = dev_get_drvdata(dev);
  3213. struct snd_cs46xx *chip = card->private_data;
  3214. int i, amp_saved;
  3215. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3216. chip->in_suspend = 1;
  3217. snd_pcm_suspend_all(chip->pcm);
  3218. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3219. snd_pcm_suspend_all(chip->pcm_rear);
  3220. snd_pcm_suspend_all(chip->pcm_center_lfe);
  3221. snd_pcm_suspend_all(chip->pcm_iec958);
  3222. #endif
  3223. // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
  3224. // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
  3225. snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3226. snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3227. /* save some registers */
  3228. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3229. chip->saved_regs[i] = snd_cs46xx_peekBA0(chip, saved_regs[i]);
  3230. amp_saved = chip->amplifier;
  3231. /* turn off amp */
  3232. chip->amplifier_ctrl(chip, -chip->amplifier);
  3233. snd_cs46xx_hw_stop(chip);
  3234. /* disable CLKRUN */
  3235. chip->active_ctrl(chip, -chip->amplifier);
  3236. chip->amplifier = amp_saved; /* restore the status */
  3237. return 0;
  3238. }
  3239. static int snd_cs46xx_resume(struct device *dev)
  3240. {
  3241. struct snd_card *card = dev_get_drvdata(dev);
  3242. struct snd_cs46xx *chip = card->private_data;
  3243. int amp_saved;
  3244. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3245. int i;
  3246. #endif
  3247. unsigned int tmp;
  3248. amp_saved = chip->amplifier;
  3249. chip->amplifier = 0;
  3250. chip->active_ctrl(chip, 1); /* force to on */
  3251. snd_cs46xx_chip_init(chip);
  3252. snd_cs46xx_reset(chip);
  3253. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3254. cs46xx_dsp_resume(chip);
  3255. /* restore some registers */
  3256. for (i = 0; i < ARRAY_SIZE(saved_regs); i++)
  3257. snd_cs46xx_pokeBA0(chip, saved_regs[i], chip->saved_regs[i]);
  3258. #else
  3259. snd_cs46xx_download_image(chip);
  3260. #endif
  3261. #if 0
  3262. snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
  3263. chip->ac97_general_purpose);
  3264. snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
  3265. chip->ac97_powerdown);
  3266. mdelay(10);
  3267. snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
  3268. chip->ac97_powerdown);
  3269. mdelay(5);
  3270. #endif
  3271. snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3272. snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3273. /*
  3274. * Stop capture DMA.
  3275. */
  3276. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  3277. chip->capt.ctl = tmp & 0x0000ffff;
  3278. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  3279. mdelay(5);
  3280. /* reset playback/capture */
  3281. snd_cs46xx_set_play_sample_rate(chip, 8000);
  3282. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  3283. snd_cs46xx_proc_start(chip);
  3284. cs46xx_enable_stream_irqs(chip);
  3285. if (amp_saved)
  3286. chip->amplifier_ctrl(chip, 1); /* turn amp on */
  3287. else
  3288. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3289. chip->amplifier = amp_saved;
  3290. chip->in_suspend = 0;
  3291. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3292. return 0;
  3293. }
  3294. SIMPLE_DEV_PM_OPS(snd_cs46xx_pm, snd_cs46xx_suspend, snd_cs46xx_resume);
  3295. #endif /* CONFIG_PM_SLEEP */
  3296. /*
  3297. */
  3298. int snd_cs46xx_create(struct snd_card *card,
  3299. struct pci_dev *pci,
  3300. int external_amp, int thinkpad,
  3301. struct snd_cs46xx **rchip)
  3302. {
  3303. struct snd_cs46xx *chip;
  3304. int err, idx;
  3305. struct snd_cs46xx_region *region;
  3306. struct cs_card_type *cp;
  3307. u16 ss_card, ss_vendor;
  3308. static struct snd_device_ops ops = {
  3309. .dev_free = snd_cs46xx_dev_free,
  3310. };
  3311. *rchip = NULL;
  3312. /* enable PCI device */
  3313. if ((err = pci_enable_device(pci)) < 0)
  3314. return err;
  3315. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  3316. if (chip == NULL) {
  3317. pci_disable_device(pci);
  3318. return -ENOMEM;
  3319. }
  3320. spin_lock_init(&chip->reg_lock);
  3321. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3322. mutex_init(&chip->spos_mutex);
  3323. #endif
  3324. chip->card = card;
  3325. chip->pci = pci;
  3326. chip->irq = -1;
  3327. chip->ba0_addr = pci_resource_start(pci, 0);
  3328. chip->ba1_addr = pci_resource_start(pci, 1);
  3329. if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
  3330. chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
  3331. dev_err(chip->card->dev,
  3332. "wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n",
  3333. chip->ba0_addr, chip->ba1_addr);
  3334. snd_cs46xx_free(chip);
  3335. return -ENOMEM;
  3336. }
  3337. region = &chip->region.name.ba0;
  3338. strcpy(region->name, "CS46xx_BA0");
  3339. region->base = chip->ba0_addr;
  3340. region->size = CS46XX_BA0_SIZE;
  3341. region = &chip->region.name.data0;
  3342. strcpy(region->name, "CS46xx_BA1_data0");
  3343. region->base = chip->ba1_addr + BA1_SP_DMEM0;
  3344. region->size = CS46XX_BA1_DATA0_SIZE;
  3345. region = &chip->region.name.data1;
  3346. strcpy(region->name, "CS46xx_BA1_data1");
  3347. region->base = chip->ba1_addr + BA1_SP_DMEM1;
  3348. region->size = CS46XX_BA1_DATA1_SIZE;
  3349. region = &chip->region.name.pmem;
  3350. strcpy(region->name, "CS46xx_BA1_pmem");
  3351. region->base = chip->ba1_addr + BA1_SP_PMEM;
  3352. region->size = CS46XX_BA1_PRG_SIZE;
  3353. region = &chip->region.name.reg;
  3354. strcpy(region->name, "CS46xx_BA1_reg");
  3355. region->base = chip->ba1_addr + BA1_SP_REG;
  3356. region->size = CS46XX_BA1_REG_SIZE;
  3357. /* set up amp and clkrun hack */
  3358. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
  3359. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
  3360. for (cp = &cards[0]; cp->name; cp++) {
  3361. if (cp->vendor == ss_vendor && cp->id == ss_card) {
  3362. dev_dbg(chip->card->dev, "hack for %s enabled\n",
  3363. cp->name);
  3364. chip->amplifier_ctrl = cp->amp;
  3365. chip->active_ctrl = cp->active;
  3366. chip->mixer_init = cp->mixer_init;
  3367. if (cp->init)
  3368. cp->init(chip);
  3369. break;
  3370. }
  3371. }
  3372. if (external_amp) {
  3373. dev_info(chip->card->dev,
  3374. "Crystal EAPD support forced on.\n");
  3375. chip->amplifier_ctrl = amp_voyetra;
  3376. }
  3377. if (thinkpad) {
  3378. dev_info(chip->card->dev,
  3379. "Activating CLKRUN hack for Thinkpad.\n");
  3380. chip->active_ctrl = clkrun_hack;
  3381. clkrun_init(chip);
  3382. }
  3383. if (chip->amplifier_ctrl == NULL)
  3384. chip->amplifier_ctrl = amp_none;
  3385. if (chip->active_ctrl == NULL)
  3386. chip->active_ctrl = amp_none;
  3387. chip->active_ctrl(chip, 1); /* enable CLKRUN */
  3388. pci_set_master(pci);
  3389. for (idx = 0; idx < 5; idx++) {
  3390. region = &chip->region.idx[idx];
  3391. if ((region->resource = request_mem_region(region->base, region->size,
  3392. region->name)) == NULL) {
  3393. dev_err(chip->card->dev,
  3394. "unable to request memory region 0x%lx-0x%lx\n",
  3395. region->base, region->base + region->size - 1);
  3396. snd_cs46xx_free(chip);
  3397. return -EBUSY;
  3398. }
  3399. region->remap_addr = ioremap_nocache(region->base, region->size);
  3400. if (region->remap_addr == NULL) {
  3401. dev_err(chip->card->dev,
  3402. "%s ioremap problem\n", region->name);
  3403. snd_cs46xx_free(chip);
  3404. return -ENOMEM;
  3405. }
  3406. }
  3407. if (request_irq(pci->irq, snd_cs46xx_interrupt, IRQF_SHARED,
  3408. KBUILD_MODNAME, chip)) {
  3409. dev_err(chip->card->dev, "unable to grab IRQ %d\n", pci->irq);
  3410. snd_cs46xx_free(chip);
  3411. return -EBUSY;
  3412. }
  3413. chip->irq = pci->irq;
  3414. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3415. chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
  3416. if (chip->dsp_spos_instance == NULL) {
  3417. snd_cs46xx_free(chip);
  3418. return -ENOMEM;
  3419. }
  3420. #endif
  3421. err = snd_cs46xx_chip_init(chip);
  3422. if (err < 0) {
  3423. snd_cs46xx_free(chip);
  3424. return err;
  3425. }
  3426. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  3427. snd_cs46xx_free(chip);
  3428. return err;
  3429. }
  3430. snd_cs46xx_proc_init(card, chip);
  3431. #ifdef CONFIG_PM_SLEEP
  3432. chip->saved_regs = kmalloc(sizeof(*chip->saved_regs) *
  3433. ARRAY_SIZE(saved_regs), GFP_KERNEL);
  3434. if (!chip->saved_regs) {
  3435. snd_cs46xx_free(chip);
  3436. return -ENOMEM;
  3437. }
  3438. #endif
  3439. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3440. *rchip = chip;
  3441. return 0;
  3442. }