cs46xx_lib.c 109 KB

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