cs46xx_lib.c 108 KB

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