cs46xx_lib.c 109 KB

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