cs46xx_lib.c 105 KB

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