cs46xx_lib.c 106 KB

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