cs46xx_lib.c 105 KB

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