cs46xx_lib.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  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(cs46xx_t *chip, int change);
  65. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  66. static snd_pcm_ops_t snd_cs46xx_playback_rear_ops;
  67. static snd_pcm_ops_t snd_cs46xx_playback_indirect_rear_ops;
  68. static snd_pcm_ops_t snd_cs46xx_playback_clfe_ops;
  69. static snd_pcm_ops_t snd_cs46xx_playback_indirect_clfe_ops;
  70. static snd_pcm_ops_t snd_cs46xx_playback_iec958_ops;
  71. static snd_pcm_ops_t snd_cs46xx_playback_indirect_iec958_ops;
  72. #endif
  73. static snd_pcm_ops_t snd_cs46xx_playback_ops;
  74. static snd_pcm_ops_t snd_cs46xx_playback_indirect_ops;
  75. static snd_pcm_ops_t snd_cs46xx_capture_ops;
  76. static snd_pcm_ops_t snd_cs46xx_capture_indirect_ops;
  77. static unsigned short snd_cs46xx_codec_read(cs46xx_t *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("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("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(ac97_t * ac97,
  185. unsigned short reg)
  186. {
  187. cs46xx_t *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(cs46xx_t *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("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(ac97_t *ac97,
  255. unsigned short reg,
  256. unsigned short val)
  257. {
  258. cs46xx_t *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(cs46xx_t *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(cs46xx_t *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(cs46xx_t *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(cs46xx_t *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(cs46xx_t * 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(cs46xx_t *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(cs46xx_t *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("SPCR_RUNFR never reset\n");
  442. }
  443. static void snd_cs46xx_proc_stop(cs46xx_t *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(cs46xx_t *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(cs46xx_t *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(snd_pcm_substream_t *substream,
  600. snd_pcm_indirect_t *rec, size_t bytes)
  601. {
  602. snd_pcm_runtime_t *runtime = substream->runtime;
  603. cs46xx_pcm_t * 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(snd_pcm_substream_t *substream)
  607. {
  608. snd_pcm_runtime_t *runtime = substream->runtime;
  609. cs46xx_pcm_t * 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(snd_pcm_substream_t *substream,
  614. snd_pcm_indirect_t *rec, size_t bytes)
  615. {
  616. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  617. snd_pcm_runtime_t *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(snd_pcm_substream_t *substream)
  622. {
  623. cs46xx_t *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(snd_pcm_substream_t * substream)
  628. {
  629. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  630. size_t ptr;
  631. cs46xx_pcm_t *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(snd_pcm_substream_t * substream)
  642. {
  643. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  644. size_t ptr;
  645. cs46xx_pcm_t *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(snd_pcm_substream_t * substream)
  656. {
  657. cs46xx_t *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(snd_pcm_substream_t * substream)
  662. {
  663. cs46xx_t *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(snd_pcm_substream_t * substream,
  668. int cmd)
  669. {
  670. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  671. /*snd_pcm_runtime_t *runtime = substream->runtime;*/
  672. int result = 0;
  673. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  674. cs46xx_pcm_t *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(snd_pcm_substream_t * substream,
  727. int cmd)
  728. {
  729. cs46xx_t *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 (cs46xx_t *chip, cs46xx_pcm_t *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(snd_pcm_substream_t * substream,
  784. snd_pcm_hw_params_t * hw_params)
  785. {
  786. snd_pcm_runtime_t *runtime = substream->runtime;
  787. cs46xx_pcm_t *cpcm;
  788. int err;
  789. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  790. cs46xx_t *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(snd_pcm_substream_t * substream)
  870. {
  871. /*cs46xx_t *chip = snd_pcm_substream_chip(substream);*/
  872. snd_pcm_runtime_t *runtime = substream->runtime;
  873. cs46xx_pcm_t *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(snd_pcm_substream_t * substream)
  886. {
  887. unsigned int tmp;
  888. unsigned int pfie;
  889. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  890. snd_pcm_runtime_t *runtime = substream->runtime;
  891. cs46xx_pcm_t *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(snd_pcm_substream_t * substream,
  945. snd_pcm_hw_params_t * hw_params)
  946. {
  947. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  948. snd_pcm_runtime_t *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(snd_pcm_substream_t * substream)
  973. {
  974. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  975. snd_pcm_runtime_t *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(snd_pcm_substream_t * substream)
  984. {
  985. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  986. snd_pcm_runtime_t *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. cs46xx_t *chip = dev_id;
  998. u32 status1;
  999. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1000. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1001. u32 status2;
  1002. int i;
  1003. cs46xx_pcm_t *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 snd_pcm_hardware_t 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 snd_pcm_hardware_t 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 snd_pcm_hw_constraint_list_t 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(snd_pcm_runtime_t *runtime)
  1128. {
  1129. kfree(runtime->private_data);
  1130. }
  1131. static int _cs46xx_playback_open_channel (snd_pcm_substream_t * substream,int pcm_channel_id)
  1132. {
  1133. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  1134. cs46xx_pcm_t * cpcm;
  1135. snd_pcm_runtime_t *runtime = substream->runtime;
  1136. cpcm = kcalloc(1, 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(snd_pcm_substream_t * 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(snd_pcm_substream_t * 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(snd_pcm_substream_t * 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(snd_pcm_substream_t * substream)
  1181. {
  1182. cs46xx_t *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(snd_pcm_substream_t * substream);
  1190. static int snd_cs46xx_playback_close_iec958(snd_pcm_substream_t * substream)
  1191. {
  1192. int err;
  1193. cs46xx_t *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(snd_pcm_substream_t * substream)
  1203. {
  1204. cs46xx_t *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(snd_pcm_substream_t * substream)
  1221. {
  1222. cs46xx_t *chip = snd_pcm_substream_chip(substream);
  1223. snd_pcm_runtime_t *runtime = substream->runtime;
  1224. cs46xx_pcm_t * 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(snd_pcm_substream_t * substream)
  1244. {
  1245. cs46xx_t *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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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 snd_pcm_ops_t 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. static void snd_cs46xx_pcm_free(snd_pcm_t *pcm)
  1359. {
  1360. cs46xx_t *chip = pcm->private_data;
  1361. chip->pcm = NULL;
  1362. snd_pcm_lib_preallocate_free_for_all(pcm);
  1363. }
  1364. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1365. static void snd_cs46xx_pcm_rear_free(snd_pcm_t *pcm)
  1366. {
  1367. cs46xx_t *chip = pcm->private_data;
  1368. chip->pcm_rear = NULL;
  1369. snd_pcm_lib_preallocate_free_for_all(pcm);
  1370. }
  1371. static void snd_cs46xx_pcm_center_lfe_free(snd_pcm_t *pcm)
  1372. {
  1373. cs46xx_t *chip = pcm->private_data;
  1374. chip->pcm_center_lfe = NULL;
  1375. snd_pcm_lib_preallocate_free_for_all(pcm);
  1376. }
  1377. static void snd_cs46xx_pcm_iec958_free(snd_pcm_t *pcm)
  1378. {
  1379. cs46xx_t *chip = pcm->private_data;
  1380. chip->pcm_iec958 = NULL;
  1381. snd_pcm_lib_preallocate_free_for_all(pcm);
  1382. }
  1383. #define MAX_PLAYBACK_CHANNELS (DSP_MAX_PCM_CHANNELS - 1)
  1384. #else
  1385. #define MAX_PLAYBACK_CHANNELS 1
  1386. #endif
  1387. int __devinit snd_cs46xx_pcm(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
  1388. {
  1389. snd_pcm_t *pcm;
  1390. int err;
  1391. if (rpcm)
  1392. *rpcm = NULL;
  1393. if ((err = snd_pcm_new(chip->card, "CS46xx", device, MAX_PLAYBACK_CHANNELS, 1, &pcm)) < 0)
  1394. return err;
  1395. pcm->private_data = chip;
  1396. pcm->private_free = snd_cs46xx_pcm_free;
  1397. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_ops);
  1398. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_cs46xx_capture_ops);
  1399. /* global setup */
  1400. pcm->info_flags = 0;
  1401. strcpy(pcm->name, "CS46xx");
  1402. chip->pcm = pcm;
  1403. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1404. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1405. if (rpcm)
  1406. *rpcm = pcm;
  1407. return 0;
  1408. }
  1409. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1410. int __devinit snd_cs46xx_pcm_rear(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
  1411. {
  1412. snd_pcm_t *pcm;
  1413. int err;
  1414. if (rpcm)
  1415. *rpcm = NULL;
  1416. if ((err = snd_pcm_new(chip->card, "CS46xx - Rear", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1417. return err;
  1418. pcm->private_data = chip;
  1419. pcm->private_free = snd_cs46xx_pcm_rear_free;
  1420. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_rear_ops);
  1421. /* global setup */
  1422. pcm->info_flags = 0;
  1423. strcpy(pcm->name, "CS46xx - Rear");
  1424. chip->pcm_rear = pcm;
  1425. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1426. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1427. if (rpcm)
  1428. *rpcm = pcm;
  1429. return 0;
  1430. }
  1431. int __devinit snd_cs46xx_pcm_center_lfe(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
  1432. {
  1433. snd_pcm_t *pcm;
  1434. int err;
  1435. if (rpcm)
  1436. *rpcm = NULL;
  1437. if ((err = snd_pcm_new(chip->card, "CS46xx - Center LFE", device, MAX_PLAYBACK_CHANNELS, 0, &pcm)) < 0)
  1438. return err;
  1439. pcm->private_data = chip;
  1440. pcm->private_free = snd_cs46xx_pcm_center_lfe_free;
  1441. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_clfe_ops);
  1442. /* global setup */
  1443. pcm->info_flags = 0;
  1444. strcpy(pcm->name, "CS46xx - Center LFE");
  1445. chip->pcm_center_lfe = pcm;
  1446. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1447. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1448. if (rpcm)
  1449. *rpcm = pcm;
  1450. return 0;
  1451. }
  1452. int __devinit snd_cs46xx_pcm_iec958(cs46xx_t *chip, int device, snd_pcm_t ** rpcm)
  1453. {
  1454. snd_pcm_t *pcm;
  1455. int err;
  1456. if (rpcm)
  1457. *rpcm = NULL;
  1458. if ((err = snd_pcm_new(chip->card, "CS46xx - IEC958", device, 1, 0, &pcm)) < 0)
  1459. return err;
  1460. pcm->private_data = chip;
  1461. pcm->private_free = snd_cs46xx_pcm_iec958_free;
  1462. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_cs46xx_playback_iec958_ops);
  1463. /* global setup */
  1464. pcm->info_flags = 0;
  1465. strcpy(pcm->name, "CS46xx - IEC958");
  1466. chip->pcm_rear = pcm;
  1467. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  1468. snd_dma_pci_data(chip->pci), 64*1024, 256*1024);
  1469. if (rpcm)
  1470. *rpcm = pcm;
  1471. return 0;
  1472. }
  1473. #endif
  1474. /*
  1475. * Mixer routines
  1476. */
  1477. static void snd_cs46xx_mixer_free_ac97_bus(ac97_bus_t *bus)
  1478. {
  1479. cs46xx_t *chip = bus->private_data;
  1480. chip->ac97_bus = NULL;
  1481. }
  1482. static void snd_cs46xx_mixer_free_ac97(ac97_t *ac97)
  1483. {
  1484. cs46xx_t *chip = ac97->private_data;
  1485. snd_assert ((ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) ||
  1486. (ac97 == chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]),
  1487. return);
  1488. if (ac97 == chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]) {
  1489. chip->ac97[CS46XX_PRIMARY_CODEC_INDEX] = NULL;
  1490. chip->eapd_switch = NULL;
  1491. }
  1492. else
  1493. chip->ac97[CS46XX_SECONDARY_CODEC_INDEX] = NULL;
  1494. }
  1495. static int snd_cs46xx_vol_info(snd_kcontrol_t *kcontrol,
  1496. snd_ctl_elem_info_t *uinfo)
  1497. {
  1498. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1499. uinfo->count = 2;
  1500. uinfo->value.integer.min = 0;
  1501. uinfo->value.integer.max = 0x7fff;
  1502. return 0;
  1503. }
  1504. static int snd_cs46xx_vol_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1505. {
  1506. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1507. int reg = kcontrol->private_value;
  1508. unsigned int val = snd_cs46xx_peek(chip, reg);
  1509. ucontrol->value.integer.value[0] = 0xffff - (val >> 16);
  1510. ucontrol->value.integer.value[1] = 0xffff - (val & 0xffff);
  1511. return 0;
  1512. }
  1513. static int snd_cs46xx_vol_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1514. {
  1515. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1516. int reg = kcontrol->private_value;
  1517. unsigned int val = ((0xffff - ucontrol->value.integer.value[0]) << 16 |
  1518. (0xffff - ucontrol->value.integer.value[1]));
  1519. unsigned int old = snd_cs46xx_peek(chip, reg);
  1520. int change = (old != val);
  1521. if (change) {
  1522. snd_cs46xx_poke(chip, reg, val);
  1523. }
  1524. return change;
  1525. }
  1526. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1527. static int snd_cs46xx_vol_dac_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1528. {
  1529. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1530. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->dac_volume_left;
  1531. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->dac_volume_right;
  1532. return 0;
  1533. }
  1534. static int snd_cs46xx_vol_dac_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1535. {
  1536. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1537. int change = 0;
  1538. if (chip->dsp_spos_instance->dac_volume_right != ucontrol->value.integer.value[0] ||
  1539. chip->dsp_spos_instance->dac_volume_left != ucontrol->value.integer.value[1]) {
  1540. cs46xx_dsp_set_dac_volume(chip,
  1541. ucontrol->value.integer.value[0],
  1542. ucontrol->value.integer.value[1]);
  1543. change = 1;
  1544. }
  1545. return change;
  1546. }
  1547. #if 0
  1548. static int snd_cs46xx_vol_iec958_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1549. {
  1550. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1551. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_input_volume_left;
  1552. ucontrol->value.integer.value[1] = chip->dsp_spos_instance->spdif_input_volume_right;
  1553. return 0;
  1554. }
  1555. static int snd_cs46xx_vol_iec958_put(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
  1556. {
  1557. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1558. int change = 0;
  1559. if (chip->dsp_spos_instance->spdif_input_volume_left != ucontrol->value.integer.value[0] ||
  1560. chip->dsp_spos_instance->spdif_input_volume_right!= ucontrol->value.integer.value[1]) {
  1561. cs46xx_dsp_set_iec958_volume (chip,
  1562. ucontrol->value.integer.value[0],
  1563. ucontrol->value.integer.value[1]);
  1564. change = 1;
  1565. }
  1566. return change;
  1567. }
  1568. #endif
  1569. static int snd_mixer_boolean_info(snd_kcontrol_t *kcontrol,
  1570. snd_ctl_elem_info_t *uinfo)
  1571. {
  1572. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1573. uinfo->count = 1;
  1574. uinfo->value.integer.min = 0;
  1575. uinfo->value.integer.max = 1;
  1576. return 0;
  1577. }
  1578. static int snd_cs46xx_iec958_get(snd_kcontrol_t *kcontrol,
  1579. snd_ctl_elem_value_t *ucontrol)
  1580. {
  1581. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1582. int reg = kcontrol->private_value;
  1583. if (reg == CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT)
  1584. ucontrol->value.integer.value[0] = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1585. else
  1586. ucontrol->value.integer.value[0] = chip->dsp_spos_instance->spdif_status_in;
  1587. return 0;
  1588. }
  1589. static int snd_cs46xx_iec958_put(snd_kcontrol_t *kcontrol,
  1590. snd_ctl_elem_value_t *ucontrol)
  1591. {
  1592. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1593. int change, res;
  1594. switch (kcontrol->private_value) {
  1595. case CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT:
  1596. down (&chip->spos_mutex);
  1597. change = (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED);
  1598. if (ucontrol->value.integer.value[0] && !change)
  1599. cs46xx_dsp_enable_spdif_out(chip);
  1600. else if (change && !ucontrol->value.integer.value[0])
  1601. cs46xx_dsp_disable_spdif_out(chip);
  1602. res = (change != (chip->dsp_spos_instance->spdif_status_out & DSP_SPDIF_STATUS_OUTPUT_ENABLED));
  1603. up (&chip->spos_mutex);
  1604. break;
  1605. case CS46XX_MIXER_SPDIF_INPUT_ELEMENT:
  1606. change = chip->dsp_spos_instance->spdif_status_in;
  1607. if (ucontrol->value.integer.value[0] && !change) {
  1608. cs46xx_dsp_enable_spdif_in(chip);
  1609. /* restore volume */
  1610. }
  1611. else if (change && !ucontrol->value.integer.value[0])
  1612. cs46xx_dsp_disable_spdif_in(chip);
  1613. res = (change != chip->dsp_spos_instance->spdif_status_in);
  1614. break;
  1615. default:
  1616. res = -EINVAL;
  1617. snd_assert(0, (void)0);
  1618. }
  1619. return res;
  1620. }
  1621. static int snd_cs46xx_adc_capture_get(snd_kcontrol_t *kcontrol,
  1622. snd_ctl_elem_value_t *ucontrol)
  1623. {
  1624. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1625. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1626. if (ins->adc_input != NULL)
  1627. ucontrol->value.integer.value[0] = 1;
  1628. else
  1629. ucontrol->value.integer.value[0] = 0;
  1630. return 0;
  1631. }
  1632. static int snd_cs46xx_adc_capture_put(snd_kcontrol_t *kcontrol,
  1633. snd_ctl_elem_value_t *ucontrol)
  1634. {
  1635. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1636. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1637. int change = 0;
  1638. if (ucontrol->value.integer.value[0] && !ins->adc_input) {
  1639. cs46xx_dsp_enable_adc_capture(chip);
  1640. change = 1;
  1641. } else if (!ucontrol->value.integer.value[0] && ins->adc_input) {
  1642. cs46xx_dsp_disable_adc_capture(chip);
  1643. change = 1;
  1644. }
  1645. return change;
  1646. }
  1647. static int snd_cs46xx_pcm_capture_get(snd_kcontrol_t *kcontrol,
  1648. snd_ctl_elem_value_t *ucontrol)
  1649. {
  1650. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1651. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1652. if (ins->pcm_input != NULL)
  1653. ucontrol->value.integer.value[0] = 1;
  1654. else
  1655. ucontrol->value.integer.value[0] = 0;
  1656. return 0;
  1657. }
  1658. static int snd_cs46xx_pcm_capture_put(snd_kcontrol_t *kcontrol,
  1659. snd_ctl_elem_value_t *ucontrol)
  1660. {
  1661. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1662. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1663. int change = 0;
  1664. if (ucontrol->value.integer.value[0] && !ins->pcm_input) {
  1665. cs46xx_dsp_enable_pcm_capture(chip);
  1666. change = 1;
  1667. } else if (!ucontrol->value.integer.value[0] && ins->pcm_input) {
  1668. cs46xx_dsp_disable_pcm_capture(chip);
  1669. change = 1;
  1670. }
  1671. return change;
  1672. }
  1673. static int snd_herc_spdif_select_get(snd_kcontrol_t *kcontrol,
  1674. snd_ctl_elem_value_t *ucontrol)
  1675. {
  1676. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1677. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1678. if (val1 & EGPIODR_GPOE0)
  1679. ucontrol->value.integer.value[0] = 1;
  1680. else
  1681. ucontrol->value.integer.value[0] = 0;
  1682. return 0;
  1683. }
  1684. /*
  1685. * Game Theatre XP card - EGPIO[0] is used to select SPDIF input optical or coaxial.
  1686. */
  1687. static int snd_herc_spdif_select_put(snd_kcontrol_t *kcontrol,
  1688. snd_ctl_elem_value_t *ucontrol)
  1689. {
  1690. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1691. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  1692. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  1693. if (ucontrol->value.integer.value[0]) {
  1694. /* optical is default */
  1695. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  1696. EGPIODR_GPOE0 | val1); /* enable EGPIO0 output */
  1697. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  1698. EGPIOPTR_GPPT0 | val2); /* open-drain on output */
  1699. } else {
  1700. /* coaxial */
  1701. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE0); /* disable */
  1702. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT0); /* disable */
  1703. }
  1704. /* checking diff from the EGPIO direction register
  1705. should be enough */
  1706. return (val1 != (int)snd_cs46xx_peekBA0(chip, BA0_EGPIODR));
  1707. }
  1708. static int snd_cs46xx_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
  1709. {
  1710. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1711. uinfo->count = 1;
  1712. return 0;
  1713. }
  1714. static int snd_cs46xx_spdif_default_get(snd_kcontrol_t * kcontrol,
  1715. snd_ctl_elem_value_t * ucontrol)
  1716. {
  1717. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1718. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1719. down (&chip->spos_mutex);
  1720. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_default >> 24) & 0xff);
  1721. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_default >> 16) & 0xff);
  1722. ucontrol->value.iec958.status[2] = 0;
  1723. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_default) & 0xff);
  1724. up (&chip->spos_mutex);
  1725. return 0;
  1726. }
  1727. static int snd_cs46xx_spdif_default_put(snd_kcontrol_t * kcontrol,
  1728. snd_ctl_elem_value_t * ucontrol)
  1729. {
  1730. cs46xx_t * chip = snd_kcontrol_chip(kcontrol);
  1731. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1732. unsigned int val;
  1733. int change;
  1734. down (&chip->spos_mutex);
  1735. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1736. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[2]) << 16) |
  1737. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1738. /* left and right validity bit */
  1739. (1 << 13) | (1 << 12);
  1740. change = (unsigned int)ins->spdif_csuv_default != val;
  1741. ins->spdif_csuv_default = val;
  1742. if ( !(ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN) )
  1743. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1744. up (&chip->spos_mutex);
  1745. return change;
  1746. }
  1747. static int snd_cs46xx_spdif_mask_get(snd_kcontrol_t * kcontrol,
  1748. snd_ctl_elem_value_t * ucontrol)
  1749. {
  1750. ucontrol->value.iec958.status[0] = 0xff;
  1751. ucontrol->value.iec958.status[1] = 0xff;
  1752. ucontrol->value.iec958.status[2] = 0x00;
  1753. ucontrol->value.iec958.status[3] = 0xff;
  1754. return 0;
  1755. }
  1756. static int snd_cs46xx_spdif_stream_get(snd_kcontrol_t * kcontrol,
  1757. snd_ctl_elem_value_t * ucontrol)
  1758. {
  1759. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1760. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1761. down (&chip->spos_mutex);
  1762. ucontrol->value.iec958.status[0] = _wrap_all_bits((ins->spdif_csuv_stream >> 24) & 0xff);
  1763. ucontrol->value.iec958.status[1] = _wrap_all_bits((ins->spdif_csuv_stream >> 16) & 0xff);
  1764. ucontrol->value.iec958.status[2] = 0;
  1765. ucontrol->value.iec958.status[3] = _wrap_all_bits((ins->spdif_csuv_stream) & 0xff);
  1766. up (&chip->spos_mutex);
  1767. return 0;
  1768. }
  1769. static int snd_cs46xx_spdif_stream_put(snd_kcontrol_t * kcontrol,
  1770. snd_ctl_elem_value_t * ucontrol)
  1771. {
  1772. cs46xx_t * chip = snd_kcontrol_chip(kcontrol);
  1773. dsp_spos_instance_t * ins = chip->dsp_spos_instance;
  1774. unsigned int val;
  1775. int change;
  1776. down (&chip->spos_mutex);
  1777. val = ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[0]) << 24) |
  1778. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[1]) << 16) |
  1779. ((unsigned int)_wrap_all_bits(ucontrol->value.iec958.status[3])) |
  1780. /* left and right validity bit */
  1781. (1 << 13) | (1 << 12);
  1782. change = ins->spdif_csuv_stream != val;
  1783. ins->spdif_csuv_stream = val;
  1784. if ( ins->spdif_status_out & DSP_SPDIF_STATUS_PLAYBACK_OPEN )
  1785. cs46xx_poke_via_dsp (chip,SP_SPDOUT_CSUV,val);
  1786. up (&chip->spos_mutex);
  1787. return change;
  1788. }
  1789. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  1790. #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
  1791. static int snd_cs46xx_egpio_select_info(snd_kcontrol_t *kcontrol,
  1792. snd_ctl_elem_info_t *uinfo)
  1793. {
  1794. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1795. uinfo->count = 1;
  1796. uinfo->value.integer.min = 0;
  1797. uinfo->value.integer.max = 8;
  1798. return 0;
  1799. }
  1800. static int snd_cs46xx_egpio_select_get(snd_kcontrol_t *kcontrol,
  1801. snd_ctl_elem_value_t *ucontrol)
  1802. {
  1803. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1804. ucontrol->value.integer.value[0] = chip->current_gpio;
  1805. return 0;
  1806. }
  1807. static int snd_cs46xx_egpio_select_put(snd_kcontrol_t *kcontrol,
  1808. snd_ctl_elem_value_t *ucontrol)
  1809. {
  1810. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1811. int change = (chip->current_gpio != ucontrol->value.integer.value[0]);
  1812. chip->current_gpio = ucontrol->value.integer.value[0];
  1813. return change;
  1814. }
  1815. static int snd_cs46xx_egpio_get(snd_kcontrol_t *kcontrol,
  1816. snd_ctl_elem_value_t *ucontrol)
  1817. {
  1818. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1819. int reg = kcontrol->private_value;
  1820. snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
  1821. ucontrol->value.integer.value[0] =
  1822. (snd_cs46xx_peekBA0(chip, reg) & (1 << chip->current_gpio)) ? 1 : 0;
  1823. return 0;
  1824. }
  1825. static int snd_cs46xx_egpio_put(snd_kcontrol_t *kcontrol,
  1826. snd_ctl_elem_value_t *ucontrol)
  1827. {
  1828. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1829. int reg = kcontrol->private_value;
  1830. int val = snd_cs46xx_peekBA0(chip, reg);
  1831. int oldval = val;
  1832. snd_printdd ("put: reg = %04x, gpio %02x\n",reg,chip->current_gpio);
  1833. if (ucontrol->value.integer.value[0])
  1834. val |= (1 << chip->current_gpio);
  1835. else
  1836. val &= ~(1 << chip->current_gpio);
  1837. snd_cs46xx_pokeBA0(chip, reg,val);
  1838. snd_printdd ("put: val %08x oldval %08x\n",val,oldval);
  1839. return (oldval != val);
  1840. }
  1841. #endif /* CONFIG_SND_CS46XX_DEBUG_GPIO */
  1842. static snd_kcontrol_new_t snd_cs46xx_controls[] __devinitdata = {
  1843. {
  1844. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1845. .name = "DAC Volume",
  1846. .info = snd_cs46xx_vol_info,
  1847. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1848. .get = snd_cs46xx_vol_get,
  1849. .put = snd_cs46xx_vol_put,
  1850. .private_value = BA1_PVOL,
  1851. #else
  1852. .get = snd_cs46xx_vol_dac_get,
  1853. .put = snd_cs46xx_vol_dac_put,
  1854. #endif
  1855. },
  1856. {
  1857. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1858. .name = "ADC Volume",
  1859. .info = snd_cs46xx_vol_info,
  1860. .get = snd_cs46xx_vol_get,
  1861. .put = snd_cs46xx_vol_put,
  1862. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  1863. .private_value = BA1_CVOL,
  1864. #else
  1865. .private_value = (VARIDECIMATE_SCB_ADDR + 0xE) << 2,
  1866. #endif
  1867. },
  1868. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1869. {
  1870. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1871. .name = "ADC Capture Switch",
  1872. .info = snd_mixer_boolean_info,
  1873. .get = snd_cs46xx_adc_capture_get,
  1874. .put = snd_cs46xx_adc_capture_put
  1875. },
  1876. {
  1877. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1878. .name = "DAC Capture Switch",
  1879. .info = snd_mixer_boolean_info,
  1880. .get = snd_cs46xx_pcm_capture_get,
  1881. .put = snd_cs46xx_pcm_capture_put
  1882. },
  1883. {
  1884. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1885. .name = SNDRV_CTL_NAME_IEC958("Output ",NONE,SWITCH),
  1886. .info = snd_mixer_boolean_info,
  1887. .get = snd_cs46xx_iec958_get,
  1888. .put = snd_cs46xx_iec958_put,
  1889. .private_value = CS46XX_MIXER_SPDIF_OUTPUT_ELEMENT,
  1890. },
  1891. {
  1892. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1893. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,SWITCH),
  1894. .info = snd_mixer_boolean_info,
  1895. .get = snd_cs46xx_iec958_get,
  1896. .put = snd_cs46xx_iec958_put,
  1897. .private_value = CS46XX_MIXER_SPDIF_INPUT_ELEMENT,
  1898. },
  1899. #if 0
  1900. /* Input IEC958 volume does not work for the moment. (Benny) */
  1901. {
  1902. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1903. .name = SNDRV_CTL_NAME_IEC958("Input ",NONE,VOLUME),
  1904. .info = snd_cs46xx_vol_info,
  1905. .get = snd_cs46xx_vol_iec958_get,
  1906. .put = snd_cs46xx_vol_iec958_put,
  1907. .private_value = (ASYNCRX_SCB_ADDR + 0xE) << 2,
  1908. },
  1909. #endif
  1910. {
  1911. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1912. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1913. .info = snd_cs46xx_spdif_info,
  1914. .get = snd_cs46xx_spdif_default_get,
  1915. .put = snd_cs46xx_spdif_default_put,
  1916. },
  1917. {
  1918. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1919. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1920. .info = snd_cs46xx_spdif_info,
  1921. .get = snd_cs46xx_spdif_mask_get,
  1922. .access = SNDRV_CTL_ELEM_ACCESS_READ
  1923. },
  1924. {
  1925. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1926. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  1927. .info = snd_cs46xx_spdif_info,
  1928. .get = snd_cs46xx_spdif_stream_get,
  1929. .put = snd_cs46xx_spdif_stream_put
  1930. },
  1931. #endif
  1932. #ifdef CONFIG_SND_CS46XX_DEBUG_GPIO
  1933. {
  1934. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1935. .name = "EGPIO select",
  1936. .info = snd_cs46xx_egpio_select_info,
  1937. .get = snd_cs46xx_egpio_select_get,
  1938. .put = snd_cs46xx_egpio_select_put,
  1939. .private_value = 0,
  1940. },
  1941. {
  1942. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1943. .name = "EGPIO Input/Output",
  1944. .info = snd_mixer_boolean_info,
  1945. .get = snd_cs46xx_egpio_get,
  1946. .put = snd_cs46xx_egpio_put,
  1947. .private_value = BA0_EGPIODR,
  1948. },
  1949. {
  1950. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1951. .name = "EGPIO CMOS/Open drain",
  1952. .info = snd_mixer_boolean_info,
  1953. .get = snd_cs46xx_egpio_get,
  1954. .put = snd_cs46xx_egpio_put,
  1955. .private_value = BA0_EGPIOPTR,
  1956. },
  1957. {
  1958. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1959. .name = "EGPIO On/Off",
  1960. .info = snd_mixer_boolean_info,
  1961. .get = snd_cs46xx_egpio_get,
  1962. .put = snd_cs46xx_egpio_put,
  1963. .private_value = BA0_EGPIOSR,
  1964. },
  1965. #endif
  1966. };
  1967. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1968. /* set primary cs4294 codec into Extended Audio Mode */
  1969. static int snd_cs46xx_front_dup_get(snd_kcontrol_t *kcontrol,
  1970. snd_ctl_elem_value_t *ucontrol)
  1971. {
  1972. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1973. unsigned short val;
  1974. val = snd_ac97_read(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX], AC97_CSR_ACMODE);
  1975. ucontrol->value.integer.value[0] = (val & 0x200) ? 0 : 1;
  1976. return 0;
  1977. }
  1978. static int snd_cs46xx_front_dup_put(snd_kcontrol_t *kcontrol,
  1979. snd_ctl_elem_value_t *ucontrol)
  1980. {
  1981. cs46xx_t *chip = snd_kcontrol_chip(kcontrol);
  1982. return snd_ac97_update_bits(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  1983. AC97_CSR_ACMODE, 0x200,
  1984. ucontrol->value.integer.value[0] ? 0 : 0x200);
  1985. }
  1986. static snd_kcontrol_new_t snd_cs46xx_front_dup_ctl = {
  1987. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1988. .name = "Duplicate Front",
  1989. .info = snd_mixer_boolean_info,
  1990. .get = snd_cs46xx_front_dup_get,
  1991. .put = snd_cs46xx_front_dup_put,
  1992. };
  1993. #endif
  1994. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  1995. /* Only available on the Hercules Game Theater XP soundcard */
  1996. static snd_kcontrol_new_t snd_hercules_controls[] __devinitdata = {
  1997. {
  1998. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1999. .name = "Optical/Coaxial SPDIF Input Switch",
  2000. .info = snd_mixer_boolean_info,
  2001. .get = snd_herc_spdif_select_get,
  2002. .put = snd_herc_spdif_select_put,
  2003. },
  2004. };
  2005. static void snd_cs46xx_codec_reset (ac97_t * ac97)
  2006. {
  2007. unsigned long end_time;
  2008. int err;
  2009. /* reset to defaults */
  2010. snd_ac97_write(ac97, AC97_RESET, 0);
  2011. /* set the desired CODEC mode */
  2012. if (ac97->num == CS46XX_PRIMARY_CODEC_INDEX) {
  2013. snd_printdd("cs46xx: CODOEC1 mode %04x\n",0x0);
  2014. snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x0);
  2015. } else if (ac97->num == CS46XX_SECONDARY_CODEC_INDEX) {
  2016. snd_printdd("cs46xx: CODOEC2 mode %04x\n",0x3);
  2017. snd_cs46xx_ac97_write(ac97,AC97_CSR_ACMODE,0x3);
  2018. } else {
  2019. snd_assert(0); /* should never happen ... */
  2020. }
  2021. udelay(50);
  2022. /* it's necessary to wait awhile until registers are accessible after RESET */
  2023. /* because the PCM or MASTER volume registers can be modified, */
  2024. /* the REC_GAIN register is used for tests */
  2025. end_time = jiffies + HZ;
  2026. do {
  2027. unsigned short ext_mid;
  2028. /* use preliminary reads to settle the communication */
  2029. snd_ac97_read(ac97, AC97_RESET);
  2030. snd_ac97_read(ac97, AC97_VENDOR_ID1);
  2031. snd_ac97_read(ac97, AC97_VENDOR_ID2);
  2032. /* modem? */
  2033. ext_mid = snd_ac97_read(ac97, AC97_EXTENDED_MID);
  2034. if (ext_mid != 0xffff && (ext_mid & 1) != 0)
  2035. return;
  2036. /* test if we can write to the record gain volume register */
  2037. snd_ac97_write_cache(ac97, AC97_REC_GAIN, 0x8a05);
  2038. if ((err = snd_ac97_read(ac97, AC97_REC_GAIN)) == 0x8a05)
  2039. return;
  2040. msleep(10);
  2041. } while (time_after_eq(end_time, jiffies));
  2042. snd_printk("CS46xx secondary codec dont respond!\n");
  2043. }
  2044. #endif
  2045. static int __devinit cs46xx_detect_codec(cs46xx_t *chip, int codec)
  2046. {
  2047. int idx, err;
  2048. ac97_template_t ac97;
  2049. memset(&ac97, 0, sizeof(ac97));
  2050. ac97.private_data = chip;
  2051. ac97.private_free = snd_cs46xx_mixer_free_ac97;
  2052. ac97.num = codec;
  2053. if (chip->amplifier_ctrl == amp_voyetra)
  2054. ac97.scaps = AC97_SCAP_INV_EAPD;
  2055. if (codec == CS46XX_SECONDARY_CODEC_INDEX) {
  2056. snd_cs46xx_codec_write(chip, AC97_RESET, 0, codec);
  2057. udelay(10);
  2058. if (snd_cs46xx_codec_read(chip, AC97_RESET, codec) & 0x8000) {
  2059. snd_printdd("snd_cs46xx: seconadry codec not present\n");
  2060. return -ENXIO;
  2061. }
  2062. }
  2063. snd_cs46xx_codec_write(chip, AC97_MASTER, 0x8000, codec);
  2064. for (idx = 0; idx < 100; ++idx) {
  2065. if (snd_cs46xx_codec_read(chip, AC97_MASTER, codec) == 0x8000) {
  2066. err = snd_ac97_mixer(chip->ac97_bus, &ac97, &chip->ac97[codec]);
  2067. return err;
  2068. }
  2069. msleep(10);
  2070. }
  2071. snd_printdd("snd_cs46xx: codec %d detection timeout\n", codec);
  2072. return -ENXIO;
  2073. }
  2074. int __devinit snd_cs46xx_mixer(cs46xx_t *chip, int spdif_device)
  2075. {
  2076. snd_card_t *card = chip->card;
  2077. snd_ctl_elem_id_t id;
  2078. int err;
  2079. unsigned int idx;
  2080. static ac97_bus_ops_t ops = {
  2081. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2082. .reset = snd_cs46xx_codec_reset,
  2083. #endif
  2084. .write = snd_cs46xx_ac97_write,
  2085. .read = snd_cs46xx_ac97_read,
  2086. };
  2087. /* detect primary codec */
  2088. chip->nr_ac97_codecs = 0;
  2089. snd_printdd("snd_cs46xx: detecting primary codec\n");
  2090. if ((err = snd_ac97_bus(card, 0, &ops, chip, &chip->ac97_bus)) < 0)
  2091. return err;
  2092. chip->ac97_bus->private_free = snd_cs46xx_mixer_free_ac97_bus;
  2093. if (cs46xx_detect_codec(chip, CS46XX_PRIMARY_CODEC_INDEX) < 0)
  2094. return -ENXIO;
  2095. chip->nr_ac97_codecs = 1;
  2096. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2097. snd_printdd("snd_cs46xx: detecting seconadry codec\n");
  2098. /* try detect a secondary codec */
  2099. if (! cs46xx_detect_codec(chip, CS46XX_SECONDARY_CODEC_INDEX))
  2100. chip->nr_ac97_codecs = 2;
  2101. #endif /* CONFIG_SND_CS46XX_NEW_DSP */
  2102. /* add cs4630 mixer controls */
  2103. for (idx = 0; idx < ARRAY_SIZE(snd_cs46xx_controls); idx++) {
  2104. snd_kcontrol_t *kctl;
  2105. kctl = snd_ctl_new1(&snd_cs46xx_controls[idx], chip);
  2106. if (kctl && kctl->id.iface == SNDRV_CTL_ELEM_IFACE_PCM)
  2107. kctl->id.device = spdif_device;
  2108. if ((err = snd_ctl_add(card, kctl)) < 0)
  2109. return err;
  2110. }
  2111. /* get EAPD mixer switch (for voyetra hack) */
  2112. memset(&id, 0, sizeof(id));
  2113. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2114. strcpy(id.name, "External Amplifier");
  2115. chip->eapd_switch = snd_ctl_find_id(chip->card, &id);
  2116. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2117. if (chip->nr_ac97_codecs == 1) {
  2118. unsigned int id2 = chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]->id & 0xffff;
  2119. if (id2 == 0x592b || id2 == 0x592d) {
  2120. err = snd_ctl_add(card, snd_ctl_new1(&snd_cs46xx_front_dup_ctl, chip));
  2121. if (err < 0)
  2122. return err;
  2123. snd_ac97_write_cache(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX],
  2124. AC97_CSR_ACMODE, 0x200);
  2125. }
  2126. }
  2127. /* do soundcard specific mixer setup */
  2128. if (chip->mixer_init) {
  2129. snd_printdd ("calling chip->mixer_init(chip);\n");
  2130. chip->mixer_init(chip);
  2131. }
  2132. #endif
  2133. /* turn on amplifier */
  2134. chip->amplifier_ctrl(chip, 1);
  2135. return 0;
  2136. }
  2137. /*
  2138. * RawMIDI interface
  2139. */
  2140. static void snd_cs46xx_midi_reset(cs46xx_t *chip)
  2141. {
  2142. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, MIDCR_MRST);
  2143. udelay(100);
  2144. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2145. }
  2146. static int snd_cs46xx_midi_input_open(snd_rawmidi_substream_t * substream)
  2147. {
  2148. cs46xx_t *chip = substream->rmidi->private_data;
  2149. chip->active_ctrl(chip, 1);
  2150. spin_lock_irq(&chip->reg_lock);
  2151. chip->uartm |= CS46XX_MODE_INPUT;
  2152. chip->midcr |= MIDCR_RXE;
  2153. chip->midi_input = substream;
  2154. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2155. snd_cs46xx_midi_reset(chip);
  2156. } else {
  2157. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2158. }
  2159. spin_unlock_irq(&chip->reg_lock);
  2160. return 0;
  2161. }
  2162. static int snd_cs46xx_midi_input_close(snd_rawmidi_substream_t * substream)
  2163. {
  2164. cs46xx_t *chip = substream->rmidi->private_data;
  2165. spin_lock_irq(&chip->reg_lock);
  2166. chip->midcr &= ~(MIDCR_RXE | MIDCR_RIE);
  2167. chip->midi_input = NULL;
  2168. if (!(chip->uartm & CS46XX_MODE_OUTPUT)) {
  2169. snd_cs46xx_midi_reset(chip);
  2170. } else {
  2171. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2172. }
  2173. chip->uartm &= ~CS46XX_MODE_INPUT;
  2174. spin_unlock_irq(&chip->reg_lock);
  2175. chip->active_ctrl(chip, -1);
  2176. return 0;
  2177. }
  2178. static int snd_cs46xx_midi_output_open(snd_rawmidi_substream_t * substream)
  2179. {
  2180. cs46xx_t *chip = substream->rmidi->private_data;
  2181. chip->active_ctrl(chip, 1);
  2182. spin_lock_irq(&chip->reg_lock);
  2183. chip->uartm |= CS46XX_MODE_OUTPUT;
  2184. chip->midcr |= MIDCR_TXE;
  2185. chip->midi_output = substream;
  2186. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2187. snd_cs46xx_midi_reset(chip);
  2188. } else {
  2189. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2190. }
  2191. spin_unlock_irq(&chip->reg_lock);
  2192. return 0;
  2193. }
  2194. static int snd_cs46xx_midi_output_close(snd_rawmidi_substream_t * substream)
  2195. {
  2196. cs46xx_t *chip = substream->rmidi->private_data;
  2197. spin_lock_irq(&chip->reg_lock);
  2198. chip->midcr &= ~(MIDCR_TXE | MIDCR_TIE);
  2199. chip->midi_output = NULL;
  2200. if (!(chip->uartm & CS46XX_MODE_INPUT)) {
  2201. snd_cs46xx_midi_reset(chip);
  2202. } else {
  2203. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2204. }
  2205. chip->uartm &= ~CS46XX_MODE_OUTPUT;
  2206. spin_unlock_irq(&chip->reg_lock);
  2207. chip->active_ctrl(chip, -1);
  2208. return 0;
  2209. }
  2210. static void snd_cs46xx_midi_input_trigger(snd_rawmidi_substream_t * substream, int up)
  2211. {
  2212. unsigned long flags;
  2213. cs46xx_t *chip = substream->rmidi->private_data;
  2214. spin_lock_irqsave(&chip->reg_lock, flags);
  2215. if (up) {
  2216. if ((chip->midcr & MIDCR_RIE) == 0) {
  2217. chip->midcr |= MIDCR_RIE;
  2218. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2219. }
  2220. } else {
  2221. if (chip->midcr & MIDCR_RIE) {
  2222. chip->midcr &= ~MIDCR_RIE;
  2223. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2224. }
  2225. }
  2226. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2227. }
  2228. static void snd_cs46xx_midi_output_trigger(snd_rawmidi_substream_t * substream, int up)
  2229. {
  2230. unsigned long flags;
  2231. cs46xx_t *chip = substream->rmidi->private_data;
  2232. unsigned char byte;
  2233. spin_lock_irqsave(&chip->reg_lock, flags);
  2234. if (up) {
  2235. if ((chip->midcr & MIDCR_TIE) == 0) {
  2236. chip->midcr |= MIDCR_TIE;
  2237. /* fill UART FIFO buffer at first, and turn Tx interrupts only if necessary */
  2238. while ((chip->midcr & MIDCR_TIE) &&
  2239. (snd_cs46xx_peekBA0(chip, BA0_MIDSR) & MIDSR_TBF) == 0) {
  2240. if (snd_rawmidi_transmit(substream, &byte, 1) != 1) {
  2241. chip->midcr &= ~MIDCR_TIE;
  2242. } else {
  2243. snd_cs46xx_pokeBA0(chip, BA0_MIDWP, byte);
  2244. }
  2245. }
  2246. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2247. }
  2248. } else {
  2249. if (chip->midcr & MIDCR_TIE) {
  2250. chip->midcr &= ~MIDCR_TIE;
  2251. snd_cs46xx_pokeBA0(chip, BA0_MIDCR, chip->midcr);
  2252. }
  2253. }
  2254. spin_unlock_irqrestore(&chip->reg_lock, flags);
  2255. }
  2256. static snd_rawmidi_ops_t snd_cs46xx_midi_output =
  2257. {
  2258. .open = snd_cs46xx_midi_output_open,
  2259. .close = snd_cs46xx_midi_output_close,
  2260. .trigger = snd_cs46xx_midi_output_trigger,
  2261. };
  2262. static snd_rawmidi_ops_t snd_cs46xx_midi_input =
  2263. {
  2264. .open = snd_cs46xx_midi_input_open,
  2265. .close = snd_cs46xx_midi_input_close,
  2266. .trigger = snd_cs46xx_midi_input_trigger,
  2267. };
  2268. int __devinit snd_cs46xx_midi(cs46xx_t *chip, int device, snd_rawmidi_t **rrawmidi)
  2269. {
  2270. snd_rawmidi_t *rmidi;
  2271. int err;
  2272. if (rrawmidi)
  2273. *rrawmidi = NULL;
  2274. if ((err = snd_rawmidi_new(chip->card, "CS46XX", device, 1, 1, &rmidi)) < 0)
  2275. return err;
  2276. strcpy(rmidi->name, "CS46XX");
  2277. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_cs46xx_midi_output);
  2278. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_cs46xx_midi_input);
  2279. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT | SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  2280. rmidi->private_data = chip;
  2281. chip->rmidi = rmidi;
  2282. if (rrawmidi)
  2283. *rrawmidi = NULL;
  2284. return 0;
  2285. }
  2286. /*
  2287. * gameport interface
  2288. */
  2289. #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
  2290. static void snd_cs46xx_gameport_trigger(struct gameport *gameport)
  2291. {
  2292. cs46xx_t *chip = gameport_get_port_data(gameport);
  2293. snd_assert(chip, return);
  2294. snd_cs46xx_pokeBA0(chip, BA0_JSPT, 0xFF); //outb(gameport->io, 0xFF);
  2295. }
  2296. static unsigned char snd_cs46xx_gameport_read(struct gameport *gameport)
  2297. {
  2298. cs46xx_t *chip = gameport_get_port_data(gameport);
  2299. snd_assert(chip, return 0);
  2300. return snd_cs46xx_peekBA0(chip, BA0_JSPT); //inb(gameport->io);
  2301. }
  2302. static int snd_cs46xx_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2303. {
  2304. cs46xx_t *chip = gameport_get_port_data(gameport);
  2305. unsigned js1, js2, jst;
  2306. snd_assert(chip, return 0);
  2307. js1 = snd_cs46xx_peekBA0(chip, BA0_JSC1);
  2308. js2 = snd_cs46xx_peekBA0(chip, BA0_JSC2);
  2309. jst = snd_cs46xx_peekBA0(chip, BA0_JSPT);
  2310. *buttons = (~jst >> 4) & 0x0F;
  2311. axes[0] = ((js1 & JSC1_Y1V_MASK) >> JSC1_Y1V_SHIFT) & 0xFFFF;
  2312. axes[1] = ((js1 & JSC1_X1V_MASK) >> JSC1_X1V_SHIFT) & 0xFFFF;
  2313. axes[2] = ((js2 & JSC2_Y2V_MASK) >> JSC2_Y2V_SHIFT) & 0xFFFF;
  2314. axes[3] = ((js2 & JSC2_X2V_MASK) >> JSC2_X2V_SHIFT) & 0xFFFF;
  2315. for(jst=0;jst<4;++jst)
  2316. if(axes[jst]==0xFFFF) axes[jst] = -1;
  2317. return 0;
  2318. }
  2319. static int snd_cs46xx_gameport_open(struct gameport *gameport, int mode)
  2320. {
  2321. switch (mode) {
  2322. case GAMEPORT_MODE_COOKED:
  2323. return 0;
  2324. case GAMEPORT_MODE_RAW:
  2325. return 0;
  2326. default:
  2327. return -1;
  2328. }
  2329. return 0;
  2330. }
  2331. int __devinit snd_cs46xx_gameport(cs46xx_t *chip)
  2332. {
  2333. struct gameport *gp;
  2334. chip->gameport = gp = gameport_allocate_port();
  2335. if (!gp) {
  2336. printk(KERN_ERR "cs46xx: cannot allocate memory for gameport\n");
  2337. return -ENOMEM;
  2338. }
  2339. gameport_set_name(gp, "CS46xx Gameport");
  2340. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2341. gameport_set_dev_parent(gp, &chip->pci->dev);
  2342. gameport_set_port_data(gp, chip);
  2343. gp->open = snd_cs46xx_gameport_open;
  2344. gp->read = snd_cs46xx_gameport_read;
  2345. gp->trigger = snd_cs46xx_gameport_trigger;
  2346. gp->cooked_read = snd_cs46xx_gameport_cooked_read;
  2347. snd_cs46xx_pokeBA0(chip, BA0_JSIO, 0xFF); // ?
  2348. snd_cs46xx_pokeBA0(chip, BA0_JSCTL, JSCTL_SP_MEDIUM_SLOW);
  2349. gameport_register_port(gp);
  2350. return 0;
  2351. }
  2352. static inline void snd_cs46xx_remove_gameport(cs46xx_t *chip)
  2353. {
  2354. if (chip->gameport) {
  2355. gameport_unregister_port(chip->gameport);
  2356. chip->gameport = NULL;
  2357. }
  2358. }
  2359. #else
  2360. int __devinit snd_cs46xx_gameport(cs46xx_t *chip) { return -ENOSYS; }
  2361. static inline void snd_cs46xx_remove_gameport(cs46xx_t *chip) { }
  2362. #endif /* CONFIG_GAMEPORT */
  2363. /*
  2364. * proc interface
  2365. */
  2366. static long snd_cs46xx_io_read(snd_info_entry_t *entry, void *file_private_data,
  2367. struct file *file, char __user *buf,
  2368. unsigned long count, unsigned long pos)
  2369. {
  2370. long size;
  2371. snd_cs46xx_region_t *region = (snd_cs46xx_region_t *)entry->private_data;
  2372. size = count;
  2373. if (pos + (size_t)size > region->size)
  2374. size = region->size - pos;
  2375. if (size > 0) {
  2376. if (copy_to_user_fromio(buf, region->remap_addr + pos, size))
  2377. return -EFAULT;
  2378. }
  2379. return size;
  2380. }
  2381. static struct snd_info_entry_ops snd_cs46xx_proc_io_ops = {
  2382. .read = snd_cs46xx_io_read,
  2383. };
  2384. static int __devinit snd_cs46xx_proc_init(snd_card_t * card, cs46xx_t *chip)
  2385. {
  2386. snd_info_entry_t *entry;
  2387. int idx;
  2388. for (idx = 0; idx < 5; idx++) {
  2389. snd_cs46xx_region_t *region = &chip->region.idx[idx];
  2390. if (! snd_card_proc_new(card, region->name, &entry)) {
  2391. entry->content = SNDRV_INFO_CONTENT_DATA;
  2392. entry->private_data = chip;
  2393. entry->c.ops = &snd_cs46xx_proc_io_ops;
  2394. entry->size = region->size;
  2395. entry->mode = S_IFREG | S_IRUSR;
  2396. }
  2397. }
  2398. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2399. cs46xx_dsp_proc_init(card, chip);
  2400. #endif
  2401. return 0;
  2402. }
  2403. static int snd_cs46xx_proc_done(cs46xx_t *chip)
  2404. {
  2405. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2406. cs46xx_dsp_proc_done(chip);
  2407. #endif
  2408. return 0;
  2409. }
  2410. /*
  2411. * stop the h/w
  2412. */
  2413. static void snd_cs46xx_hw_stop(cs46xx_t *chip)
  2414. {
  2415. unsigned int tmp;
  2416. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2417. tmp &= ~0x0000f03f;
  2418. tmp |= 0x00000010;
  2419. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt disable */
  2420. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2421. tmp &= ~0x0000003f;
  2422. tmp |= 0x00000011;
  2423. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt disable */
  2424. /*
  2425. * Stop playback DMA.
  2426. */
  2427. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2428. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2429. /*
  2430. * Stop capture DMA.
  2431. */
  2432. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2433. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2434. /*
  2435. * Reset the processor.
  2436. */
  2437. snd_cs46xx_reset(chip);
  2438. snd_cs46xx_proc_stop(chip);
  2439. /*
  2440. * Power down the PLL.
  2441. */
  2442. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2443. /*
  2444. * Turn off the Processor by turning off the software clock enable flag in
  2445. * the clock control register.
  2446. */
  2447. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE;
  2448. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2449. }
  2450. static int snd_cs46xx_free(cs46xx_t *chip)
  2451. {
  2452. int idx;
  2453. snd_assert(chip != NULL, return -EINVAL);
  2454. if (chip->active_ctrl)
  2455. chip->active_ctrl(chip, 1);
  2456. snd_cs46xx_remove_gameport(chip);
  2457. if (chip->amplifier_ctrl)
  2458. chip->amplifier_ctrl(chip, -chip->amplifier); /* force to off */
  2459. snd_cs46xx_proc_done(chip);
  2460. if (chip->region.idx[0].resource)
  2461. snd_cs46xx_hw_stop(chip);
  2462. for (idx = 0; idx < 5; idx++) {
  2463. snd_cs46xx_region_t *region = &chip->region.idx[idx];
  2464. if (region->remap_addr)
  2465. iounmap(region->remap_addr);
  2466. if (region->resource) {
  2467. release_resource(region->resource);
  2468. kfree_nocheck(region->resource);
  2469. }
  2470. }
  2471. if (chip->irq >= 0)
  2472. free_irq(chip->irq, (void *)chip);
  2473. if (chip->active_ctrl)
  2474. chip->active_ctrl(chip, -chip->amplifier);
  2475. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2476. if (chip->dsp_spos_instance) {
  2477. cs46xx_dsp_spos_destroy(chip);
  2478. chip->dsp_spos_instance = NULL;
  2479. }
  2480. #endif
  2481. pci_disable_device(chip->pci);
  2482. kfree(chip);
  2483. return 0;
  2484. }
  2485. static int snd_cs46xx_dev_free(snd_device_t *device)
  2486. {
  2487. cs46xx_t *chip = device->device_data;
  2488. return snd_cs46xx_free(chip);
  2489. }
  2490. /*
  2491. * initialize chip
  2492. */
  2493. static int snd_cs46xx_chip_init(cs46xx_t *chip)
  2494. {
  2495. int timeout;
  2496. /*
  2497. * First, blast the clock control register to zero so that the PLL starts
  2498. * out in a known state, and blast the master serial port control register
  2499. * to zero so that the serial ports also start out in a known state.
  2500. */
  2501. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, 0);
  2502. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, 0);
  2503. /*
  2504. * If we are in AC97 mode, then we must set the part to a host controlled
  2505. * AC-link. Otherwise, we won't be able to bring up the link.
  2506. */
  2507. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2508. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0 |
  2509. SERACC_TWO_CODECS); /* 2.00 dual codecs */
  2510. /* snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_2_0); */ /* 2.00 codec */
  2511. #else
  2512. snd_cs46xx_pokeBA0(chip, BA0_SERACC, SERACC_HSP | SERACC_CHIP_TYPE_1_03); /* 1.03 codec */
  2513. #endif
  2514. /*
  2515. * Drive the ARST# pin low for a minimum of 1uS (as defined in the AC97
  2516. * spec) and then drive it high. This is done for non AC97 modes since
  2517. * there might be logic external to the CS461x that uses the ARST# line
  2518. * for a reset.
  2519. */
  2520. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, 0);
  2521. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2522. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, 0);
  2523. #endif
  2524. udelay(50);
  2525. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_RSTN);
  2526. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2527. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_RSTN);
  2528. #endif
  2529. /*
  2530. * The first thing we do here is to enable sync generation. As soon
  2531. * as we start receiving bit clock, we'll start producing the SYNC
  2532. * signal.
  2533. */
  2534. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_ESYN | ACCTL_RSTN);
  2535. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2536. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_ESYN | ACCTL_RSTN);
  2537. #endif
  2538. /*
  2539. * Now wait for a short while to allow the AC97 part to start
  2540. * generating bit clock (so we don't try to start the PLL without an
  2541. * input clock).
  2542. */
  2543. mdelay(10);
  2544. /*
  2545. * Set the serial port timing configuration, so that
  2546. * the clock control circuit gets its clock from the correct place.
  2547. */
  2548. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97);
  2549. /*
  2550. * Write the selected clock control setup to the hardware. Do not turn on
  2551. * SWCE yet (if requested), so that the devices clocked by the output of
  2552. * PLL are not clocked until the PLL is stable.
  2553. */
  2554. snd_cs46xx_pokeBA0(chip, BA0_PLLCC, PLLCC_LPF_1050_2780_KHZ | PLLCC_CDR_73_104_MHZ);
  2555. snd_cs46xx_pokeBA0(chip, BA0_PLLM, 0x3a);
  2556. snd_cs46xx_pokeBA0(chip, BA0_CLKCR2, CLKCR2_PDIVS_8);
  2557. /*
  2558. * Power up the PLL.
  2559. */
  2560. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP);
  2561. /*
  2562. * Wait until the PLL has stabilized.
  2563. */
  2564. msleep(100);
  2565. /*
  2566. * Turn on clocking of the core so that we can setup the serial ports.
  2567. */
  2568. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, CLKCR1_PLLP | CLKCR1_SWCE);
  2569. /*
  2570. * Enable FIFO Host Bypass
  2571. */
  2572. snd_cs46xx_pokeBA0(chip, BA0_SERBCF, SERBCF_HBP);
  2573. /*
  2574. * Fill the serial port FIFOs with silence.
  2575. */
  2576. snd_cs46xx_clear_serial_FIFOs(chip);
  2577. /*
  2578. * Set the serial port FIFO pointer to the first sample in the FIFO.
  2579. */
  2580. /* snd_cs46xx_pokeBA0(chip, BA0_SERBSP, 0); */
  2581. /*
  2582. * Write the serial port configuration to the part. The master
  2583. * enable bit is not set until all other values have been written.
  2584. */
  2585. snd_cs46xx_pokeBA0(chip, BA0_SERC1, SERC1_SO1F_AC97 | SERC1_SO1EN);
  2586. snd_cs46xx_pokeBA0(chip, BA0_SERC2, SERC2_SI1F_AC97 | SERC1_SO1EN);
  2587. snd_cs46xx_pokeBA0(chip, BA0_SERMC1, SERMC1_PTC_AC97 | SERMC1_MSPE);
  2588. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2589. snd_cs46xx_pokeBA0(chip, BA0_SERC7, SERC7_ASDI2EN);
  2590. snd_cs46xx_pokeBA0(chip, BA0_SERC3, 0);
  2591. snd_cs46xx_pokeBA0(chip, BA0_SERC4, 0);
  2592. snd_cs46xx_pokeBA0(chip, BA0_SERC5, 0);
  2593. snd_cs46xx_pokeBA0(chip, BA0_SERC6, 1);
  2594. #endif
  2595. mdelay(5);
  2596. /*
  2597. * Wait for the codec ready signal from the AC97 codec.
  2598. */
  2599. timeout = 150;
  2600. while (timeout-- > 0) {
  2601. /*
  2602. * Read the AC97 status register to see if we've seen a CODEC READY
  2603. * signal from the AC97 codec.
  2604. */
  2605. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS) & ACSTS_CRDY)
  2606. goto ok1;
  2607. msleep(10);
  2608. }
  2609. snd_printk("create - never read codec ready from AC'97\n");
  2610. snd_printk("it is not probably bug, try to use CS4236 driver\n");
  2611. return -EIO;
  2612. ok1:
  2613. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2614. {
  2615. int count;
  2616. for (count = 0; count < 150; count++) {
  2617. /* First, we want to wait for a short time. */
  2618. udelay(25);
  2619. if (snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY)
  2620. break;
  2621. }
  2622. /*
  2623. * Make sure CODEC is READY.
  2624. */
  2625. if (!(snd_cs46xx_peekBA0(chip, BA0_ACSTS2) & ACSTS_CRDY))
  2626. snd_printdd("cs46xx: never read card ready from secondary AC'97\n");
  2627. }
  2628. #endif
  2629. /*
  2630. * Assert the vaid frame signal so that we can start sending commands
  2631. * to the AC97 codec.
  2632. */
  2633. snd_cs46xx_pokeBA0(chip, BA0_ACCTL, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2634. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2635. snd_cs46xx_pokeBA0(chip, BA0_ACCTL2, ACCTL_VFRM | ACCTL_ESYN | ACCTL_RSTN);
  2636. #endif
  2637. /*
  2638. * Wait until we've sampled input slots 3 and 4 as valid, meaning that
  2639. * the codec is pumping ADC data across the AC-link.
  2640. */
  2641. timeout = 150;
  2642. while (timeout-- > 0) {
  2643. /*
  2644. * Read the input slot valid register and see if input slots 3 and
  2645. * 4 are valid yet.
  2646. */
  2647. if ((snd_cs46xx_peekBA0(chip, BA0_ACISV) & (ACISV_ISV3 | ACISV_ISV4)) == (ACISV_ISV3 | ACISV_ISV4))
  2648. goto ok2;
  2649. msleep(10);
  2650. }
  2651. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2652. snd_printk("create - never read ISV3 & ISV4 from AC'97\n");
  2653. return -EIO;
  2654. #else
  2655. /* This may happen on a cold boot with a Terratec SiXPack 5.1.
  2656. Reloading the driver may help, if there's other soundcards
  2657. with the same problem I would like to know. (Benny) */
  2658. snd_printk("ERROR: snd-cs46xx: never read ISV3 & ISV4 from AC'97\n");
  2659. snd_printk(" Try reloading the ALSA driver, if you find something\n");
  2660. snd_printk(" broken or not working on your soundcard upon\n");
  2661. snd_printk(" this message please report to alsa-devel@lists.sourceforge.net\n");
  2662. return -EIO;
  2663. #endif
  2664. ok2:
  2665. /*
  2666. * Now, assert valid frame and the slot 3 and 4 valid bits. This will
  2667. * commense the transfer of digital audio data to the AC97 codec.
  2668. */
  2669. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, ACOSV_SLV3 | ACOSV_SLV4);
  2670. /*
  2671. * Power down the DAC and ADC. We will power them up (if) when we need
  2672. * them.
  2673. */
  2674. /* snd_cs46xx_pokeBA0(chip, BA0_AC97_POWERDOWN, 0x300); */
  2675. /*
  2676. * Turn off the Processor by turning off the software clock enable flag in
  2677. * the clock control register.
  2678. */
  2679. /* tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1) & ~CLKCR1_SWCE; */
  2680. /* snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp); */
  2681. return 0;
  2682. }
  2683. /*
  2684. * start and load DSP
  2685. */
  2686. int __devinit snd_cs46xx_start_dsp(cs46xx_t *chip)
  2687. {
  2688. unsigned int tmp;
  2689. /*
  2690. * Reset the processor.
  2691. */
  2692. snd_cs46xx_reset(chip);
  2693. /*
  2694. * Download the image to the processor.
  2695. */
  2696. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2697. #if 0
  2698. if (cs46xx_dsp_load_module(chip, &cwcemb80_module) < 0) {
  2699. snd_printk(KERN_ERR "image download error\n");
  2700. return -EIO;
  2701. }
  2702. #endif
  2703. if (cs46xx_dsp_load_module(chip, &cwc4630_module) < 0) {
  2704. snd_printk(KERN_ERR "image download error [cwc4630]\n");
  2705. return -EIO;
  2706. }
  2707. if (cs46xx_dsp_load_module(chip, &cwcasync_module) < 0) {
  2708. snd_printk(KERN_ERR "image download error [cwcasync]\n");
  2709. return -EIO;
  2710. }
  2711. if (cs46xx_dsp_load_module(chip, &cwcsnoop_module) < 0) {
  2712. snd_printk(KERN_ERR "image download error [cwcsnoop]\n");
  2713. return -EIO;
  2714. }
  2715. if (cs46xx_dsp_load_module(chip, &cwcbinhack_module) < 0) {
  2716. snd_printk(KERN_ERR "image download error [cwcbinhack]\n");
  2717. return -EIO;
  2718. }
  2719. if (cs46xx_dsp_load_module(chip, &cwcdma_module) < 0) {
  2720. snd_printk(KERN_ERR "image download error [cwcdma]\n");
  2721. return -EIO;
  2722. }
  2723. if (cs46xx_dsp_scb_and_task_init(chip) < 0)
  2724. return -EIO;
  2725. #else
  2726. /* old image */
  2727. if (snd_cs46xx_download_image(chip) < 0) {
  2728. snd_printk("image download error\n");
  2729. return -EIO;
  2730. }
  2731. /*
  2732. * Stop playback DMA.
  2733. */
  2734. tmp = snd_cs46xx_peek(chip, BA1_PCTL);
  2735. chip->play_ctl = tmp & 0xffff0000;
  2736. snd_cs46xx_poke(chip, BA1_PCTL, tmp & 0x0000ffff);
  2737. #endif
  2738. /*
  2739. * Stop capture DMA.
  2740. */
  2741. tmp = snd_cs46xx_peek(chip, BA1_CCTL);
  2742. chip->capt.ctl = tmp & 0x0000ffff;
  2743. snd_cs46xx_poke(chip, BA1_CCTL, tmp & 0xffff0000);
  2744. mdelay(5);
  2745. snd_cs46xx_set_play_sample_rate(chip, 8000);
  2746. snd_cs46xx_set_capture_sample_rate(chip, 8000);
  2747. snd_cs46xx_proc_start(chip);
  2748. /*
  2749. * Enable interrupts on the part.
  2750. */
  2751. snd_cs46xx_pokeBA0(chip, BA0_HICR, HICR_IEV | HICR_CHGM);
  2752. tmp = snd_cs46xx_peek(chip, BA1_PFIE);
  2753. tmp &= ~0x0000f03f;
  2754. snd_cs46xx_poke(chip, BA1_PFIE, tmp); /* playback interrupt enable */
  2755. tmp = snd_cs46xx_peek(chip, BA1_CIE);
  2756. tmp &= ~0x0000003f;
  2757. tmp |= 0x00000001;
  2758. snd_cs46xx_poke(chip, BA1_CIE, tmp); /* capture interrupt enable */
  2759. #ifndef CONFIG_SND_CS46XX_NEW_DSP
  2760. /* set the attenuation to 0dB */
  2761. snd_cs46xx_poke(chip, BA1_PVOL, 0x80008000);
  2762. snd_cs46xx_poke(chip, BA1_CVOL, 0x80008000);
  2763. #endif
  2764. return 0;
  2765. }
  2766. /*
  2767. * AMP control - null AMP
  2768. */
  2769. static void amp_none(cs46xx_t *chip, int change)
  2770. {
  2771. }
  2772. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2773. static int voyetra_setup_eapd_slot(cs46xx_t *chip)
  2774. {
  2775. u32 idx, valid_slots,tmp,powerdown = 0;
  2776. u16 modem_power,pin_config,logic_type;
  2777. snd_printdd ("cs46xx: cs46xx_setup_eapd_slot()+\n");
  2778. /*
  2779. * See if the devices are powered down. If so, we must power them up first
  2780. * or they will not respond.
  2781. */
  2782. tmp = snd_cs46xx_peekBA0(chip, BA0_CLKCR1);
  2783. if (!(tmp & CLKCR1_SWCE)) {
  2784. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp | CLKCR1_SWCE);
  2785. powerdown = 1;
  2786. }
  2787. /*
  2788. * Clear PRA. The Bonzo chip will be used for GPIO not for modem
  2789. * stuff.
  2790. */
  2791. if(chip->nr_ac97_codecs != 2) {
  2792. snd_printk (KERN_ERR "cs46xx: cs46xx_setup_eapd_slot() - no secondary codec configured\n");
  2793. return -EINVAL;
  2794. }
  2795. modem_power = snd_cs46xx_codec_read (chip,
  2796. AC97_EXTENDED_MSTATUS,
  2797. CS46XX_SECONDARY_CODEC_INDEX);
  2798. modem_power &=0xFEFF;
  2799. snd_cs46xx_codec_write(chip,
  2800. AC97_EXTENDED_MSTATUS, modem_power,
  2801. CS46XX_SECONDARY_CODEC_INDEX);
  2802. /*
  2803. * Set GPIO pin's 7 and 8 so that they are configured for output.
  2804. */
  2805. pin_config = snd_cs46xx_codec_read (chip,
  2806. AC97_GPIO_CFG,
  2807. CS46XX_SECONDARY_CODEC_INDEX);
  2808. pin_config &=0x27F;
  2809. snd_cs46xx_codec_write(chip,
  2810. AC97_GPIO_CFG, pin_config,
  2811. CS46XX_SECONDARY_CODEC_INDEX);
  2812. /*
  2813. * Set GPIO pin's 7 and 8 so that they are compatible with CMOS logic.
  2814. */
  2815. logic_type = snd_cs46xx_codec_read(chip, AC97_GPIO_POLARITY,
  2816. CS46XX_SECONDARY_CODEC_INDEX);
  2817. logic_type &=0x27F;
  2818. snd_cs46xx_codec_write (chip, AC97_GPIO_POLARITY, logic_type,
  2819. CS46XX_SECONDARY_CODEC_INDEX);
  2820. valid_slots = snd_cs46xx_peekBA0(chip, BA0_ACOSV);
  2821. valid_slots |= 0x200;
  2822. snd_cs46xx_pokeBA0(chip, BA0_ACOSV, valid_slots);
  2823. if ( cs46xx_wait_for_fifo(chip,1) ) {
  2824. snd_printdd("FIFO is busy\n");
  2825. return -EINVAL;
  2826. }
  2827. /*
  2828. * Fill slots 12 with the correct value for the GPIO pins.
  2829. */
  2830. for(idx = 0x90; idx <= 0x9F; idx++) {
  2831. /*
  2832. * Initialize the fifo so that bits 7 and 8 are on.
  2833. *
  2834. * Remember that the GPIO pins in bonzo are shifted by 4 bits to
  2835. * the left. 0x1800 corresponds to bits 7 and 8.
  2836. */
  2837. snd_cs46xx_pokeBA0(chip, BA0_SERBWP, 0x1800);
  2838. /*
  2839. * Wait for command to complete
  2840. */
  2841. if ( cs46xx_wait_for_fifo(chip,200) ) {
  2842. snd_printdd("failed waiting for FIFO at addr (%02X)\n",idx);
  2843. return -EINVAL;
  2844. }
  2845. /*
  2846. * Write the serial port FIFO index.
  2847. */
  2848. snd_cs46xx_pokeBA0(chip, BA0_SERBAD, idx);
  2849. /*
  2850. * Tell the serial port to load the new value into the FIFO location.
  2851. */
  2852. snd_cs46xx_pokeBA0(chip, BA0_SERBCM, SERBCM_WRC);
  2853. }
  2854. /* wait for last command to complete */
  2855. cs46xx_wait_for_fifo(chip,200);
  2856. /*
  2857. * Now, if we powered up the devices, then power them back down again.
  2858. * This is kinda ugly, but should never happen.
  2859. */
  2860. if (powerdown)
  2861. snd_cs46xx_pokeBA0(chip, BA0_CLKCR1, tmp);
  2862. return 0;
  2863. }
  2864. #endif
  2865. /*
  2866. * Crystal EAPD mode
  2867. */
  2868. static void amp_voyetra(cs46xx_t *chip, int change)
  2869. {
  2870. /* Manage the EAPD bit on the Crystal 4297
  2871. and the Analog AD1885 */
  2872. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2873. int old = chip->amplifier;
  2874. #endif
  2875. int oval, val;
  2876. chip->amplifier += change;
  2877. oval = snd_cs46xx_codec_read(chip, AC97_POWERDOWN,
  2878. CS46XX_PRIMARY_CODEC_INDEX);
  2879. val = oval;
  2880. if (chip->amplifier) {
  2881. /* Turn the EAPD amp on */
  2882. val |= 0x8000;
  2883. } else {
  2884. /* Turn the EAPD amp off */
  2885. val &= ~0x8000;
  2886. }
  2887. if (val != oval) {
  2888. snd_cs46xx_codec_write(chip, AC97_POWERDOWN, val,
  2889. CS46XX_PRIMARY_CODEC_INDEX);
  2890. if (chip->eapd_switch)
  2891. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  2892. &chip->eapd_switch->id);
  2893. }
  2894. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2895. if (chip->amplifier && !old) {
  2896. voyetra_setup_eapd_slot(chip);
  2897. }
  2898. #endif
  2899. }
  2900. static void hercules_init(cs46xx_t *chip)
  2901. {
  2902. /* default: AMP off, and SPDIF input optical */
  2903. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2904. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2905. }
  2906. /*
  2907. * Game Theatre XP card - EGPIO[2] is used to enable the external amp.
  2908. */
  2909. static void amp_hercules(cs46xx_t *chip, int change)
  2910. {
  2911. int old = chip->amplifier;
  2912. int val1 = snd_cs46xx_peekBA0(chip, BA0_EGPIODR);
  2913. int val2 = snd_cs46xx_peekBA0(chip, BA0_EGPIOPTR);
  2914. chip->amplifier += change;
  2915. if (chip->amplifier && !old) {
  2916. snd_printdd ("Hercules amplifier ON\n");
  2917. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR,
  2918. EGPIODR_GPOE2 | val1); /* enable EGPIO2 output */
  2919. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR,
  2920. EGPIOPTR_GPPT2 | val2); /* open-drain on output */
  2921. } else if (old && !chip->amplifier) {
  2922. snd_printdd ("Hercules amplifier OFF\n");
  2923. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, val1 & ~EGPIODR_GPOE2); /* disable */
  2924. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, val2 & ~EGPIOPTR_GPPT2); /* disable */
  2925. }
  2926. }
  2927. static void voyetra_mixer_init (cs46xx_t *chip)
  2928. {
  2929. snd_printdd ("initializing Voyetra mixer\n");
  2930. /* Enable SPDIF out */
  2931. snd_cs46xx_pokeBA0(chip, BA0_EGPIODR, EGPIODR_GPOE0);
  2932. snd_cs46xx_pokeBA0(chip, BA0_EGPIOPTR, EGPIODR_GPOE0);
  2933. }
  2934. static void hercules_mixer_init (cs46xx_t *chip)
  2935. {
  2936. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2937. unsigned int idx;
  2938. int err;
  2939. snd_card_t *card = chip->card;
  2940. #endif
  2941. /* set EGPIO to default */
  2942. hercules_init(chip);
  2943. snd_printdd ("initializing Hercules mixer\n");
  2944. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  2945. for (idx = 0 ; idx < ARRAY_SIZE(snd_hercules_controls); idx++) {
  2946. snd_kcontrol_t *kctl;
  2947. kctl = snd_ctl_new1(&snd_hercules_controls[idx], chip);
  2948. if ((err = snd_ctl_add(card, kctl)) < 0) {
  2949. printk (KERN_ERR "cs46xx: failed to initialize Hercules mixer (%d)\n",err);
  2950. break;
  2951. }
  2952. }
  2953. #endif
  2954. }
  2955. #if 0
  2956. /*
  2957. * Untested
  2958. */
  2959. static void amp_voyetra_4294(cs46xx_t *chip, int change)
  2960. {
  2961. chip->amplifier += change;
  2962. if (chip->amplifier) {
  2963. /* Switch the GPIO pins 7 and 8 to open drain */
  2964. snd_cs46xx_codec_write(chip, 0x4C,
  2965. snd_cs46xx_codec_read(chip, 0x4C) & 0xFE7F);
  2966. snd_cs46xx_codec_write(chip, 0x4E,
  2967. snd_cs46xx_codec_read(chip, 0x4E) | 0x0180);
  2968. /* Now wake the AMP (this might be backwards) */
  2969. snd_cs46xx_codec_write(chip, 0x54,
  2970. snd_cs46xx_codec_read(chip, 0x54) & ~0x0180);
  2971. } else {
  2972. snd_cs46xx_codec_write(chip, 0x54,
  2973. snd_cs46xx_codec_read(chip, 0x54) | 0x0180);
  2974. }
  2975. }
  2976. #endif
  2977. /*
  2978. * piix4 pci ids
  2979. */
  2980. #ifndef PCI_VENDOR_ID_INTEL
  2981. #define PCI_VENDOR_ID_INTEL 0x8086
  2982. #endif /* PCI_VENDOR_ID_INTEL */
  2983. #ifndef PCI_DEVICE_ID_INTEL_82371AB_3
  2984. #define PCI_DEVICE_ID_INTEL_82371AB_3 0x7113
  2985. #endif /* PCI_DEVICE_ID_INTEL_82371AB_3 */
  2986. /*
  2987. * Handle the CLKRUN on a thinkpad. We must disable CLKRUN support
  2988. * whenever we need to beat on the chip.
  2989. *
  2990. * The original idea and code for this hack comes from David Kaiser at
  2991. * Linuxcare. Perhaps one day Crystal will document their chips well
  2992. * enough to make them useful.
  2993. */
  2994. static void clkrun_hack(cs46xx_t *chip, int change)
  2995. {
  2996. u16 control, nval;
  2997. if (chip->acpi_dev == NULL)
  2998. return;
  2999. chip->amplifier += change;
  3000. /* Read ACPI port */
  3001. nval = control = inw(chip->acpi_port + 0x10);
  3002. /* Flip CLKRUN off while running */
  3003. if (! chip->amplifier)
  3004. nval |= 0x2000;
  3005. else
  3006. nval &= ~0x2000;
  3007. if (nval != control)
  3008. outw(nval, chip->acpi_port + 0x10);
  3009. }
  3010. /*
  3011. * detect intel piix4
  3012. */
  3013. static void clkrun_init(cs46xx_t *chip)
  3014. {
  3015. u8 pp;
  3016. chip->acpi_dev = pci_find_device(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
  3017. if (chip->acpi_dev == NULL)
  3018. return; /* Not a thinkpad thats for sure */
  3019. /* Find the control port */
  3020. pci_read_config_byte(chip->acpi_dev, 0x41, &pp);
  3021. chip->acpi_port = pp << 8;
  3022. }
  3023. /*
  3024. * Card subid table
  3025. */
  3026. struct cs_card_type
  3027. {
  3028. u16 vendor;
  3029. u16 id;
  3030. char *name;
  3031. void (*init)(cs46xx_t *);
  3032. void (*amp)(cs46xx_t *, int);
  3033. void (*active)(cs46xx_t *, int);
  3034. void (*mixer_init)(cs46xx_t *);
  3035. };
  3036. static struct cs_card_type __devinitdata cards[] = {
  3037. {
  3038. .vendor = 0x1489,
  3039. .id = 0x7001,
  3040. .name = "Genius Soundmaker 128 value",
  3041. /* nothing special */
  3042. },
  3043. {
  3044. .vendor = 0x5053,
  3045. .id = 0x3357,
  3046. .name = "Voyetra",
  3047. .amp = amp_voyetra,
  3048. .mixer_init = voyetra_mixer_init,
  3049. },
  3050. {
  3051. .vendor = 0x1071,
  3052. .id = 0x6003,
  3053. .name = "Mitac MI6020/21",
  3054. .amp = amp_voyetra,
  3055. },
  3056. {
  3057. .vendor = 0x14AF,
  3058. .id = 0x0050,
  3059. .name = "Hercules Game Theatre XP",
  3060. .amp = amp_hercules,
  3061. .mixer_init = hercules_mixer_init,
  3062. },
  3063. {
  3064. .vendor = 0x1681,
  3065. .id = 0x0050,
  3066. .name = "Hercules Game Theatre XP",
  3067. .amp = amp_hercules,
  3068. .mixer_init = hercules_mixer_init,
  3069. },
  3070. {
  3071. .vendor = 0x1681,
  3072. .id = 0x0051,
  3073. .name = "Hercules Game Theatre XP",
  3074. .amp = amp_hercules,
  3075. .mixer_init = hercules_mixer_init,
  3076. },
  3077. {
  3078. .vendor = 0x1681,
  3079. .id = 0x0052,
  3080. .name = "Hercules Game Theatre XP",
  3081. .amp = amp_hercules,
  3082. .mixer_init = hercules_mixer_init,
  3083. },
  3084. {
  3085. .vendor = 0x1681,
  3086. .id = 0x0053,
  3087. .name = "Hercules Game Theatre XP",
  3088. .amp = amp_hercules,
  3089. .mixer_init = hercules_mixer_init,
  3090. },
  3091. {
  3092. .vendor = 0x1681,
  3093. .id = 0x0054,
  3094. .name = "Hercules Game Theatre XP",
  3095. .amp = amp_hercules,
  3096. .mixer_init = hercules_mixer_init,
  3097. },
  3098. /* Teratec */
  3099. {
  3100. .vendor = 0x153b,
  3101. .id = 0x1136,
  3102. .name = "Terratec SiXPack 5.1",
  3103. },
  3104. /* Not sure if the 570 needs the clkrun hack */
  3105. {
  3106. .vendor = PCI_VENDOR_ID_IBM,
  3107. .id = 0x0132,
  3108. .name = "Thinkpad 570",
  3109. .init = clkrun_init,
  3110. .active = clkrun_hack,
  3111. },
  3112. {
  3113. .vendor = PCI_VENDOR_ID_IBM,
  3114. .id = 0x0153,
  3115. .name = "Thinkpad 600X/A20/T20",
  3116. .init = clkrun_init,
  3117. .active = clkrun_hack,
  3118. },
  3119. {
  3120. .vendor = PCI_VENDOR_ID_IBM,
  3121. .id = 0x1010,
  3122. .name = "Thinkpad 600E (unsupported)",
  3123. },
  3124. {} /* terminator */
  3125. };
  3126. /*
  3127. * APM support
  3128. */
  3129. #ifdef CONFIG_PM
  3130. static int snd_cs46xx_suspend(snd_card_t *card, pm_message_t state)
  3131. {
  3132. cs46xx_t *chip = card->pm_private_data;
  3133. int amp_saved;
  3134. snd_pcm_suspend_all(chip->pcm);
  3135. // chip->ac97_powerdown = snd_cs46xx_codec_read(chip, AC97_POWER_CONTROL);
  3136. // chip->ac97_general_purpose = snd_cs46xx_codec_read(chip, BA0_AC97_GENERAL_PURPOSE);
  3137. snd_ac97_suspend(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3138. if (chip->ac97[CS46XX_SECONDARY_CODEC_INDEX])
  3139. snd_ac97_suspend(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3140. amp_saved = chip->amplifier;
  3141. /* turn off amp */
  3142. chip->amplifier_ctrl(chip, -chip->amplifier);
  3143. snd_cs46xx_hw_stop(chip);
  3144. /* disable CLKRUN */
  3145. chip->active_ctrl(chip, -chip->amplifier);
  3146. chip->amplifier = amp_saved; /* restore the status */
  3147. pci_disable_device(chip->pci);
  3148. return 0;
  3149. }
  3150. static int snd_cs46xx_resume(snd_card_t *card)
  3151. {
  3152. cs46xx_t *chip = card->pm_private_data;
  3153. int amp_saved;
  3154. pci_enable_device(chip->pci);
  3155. pci_set_master(chip->pci);
  3156. amp_saved = chip->amplifier;
  3157. chip->amplifier = 0;
  3158. chip->active_ctrl(chip, 1); /* force to on */
  3159. snd_cs46xx_chip_init(chip);
  3160. #if 0
  3161. snd_cs46xx_codec_write(chip, BA0_AC97_GENERAL_PURPOSE,
  3162. chip->ac97_general_purpose);
  3163. snd_cs46xx_codec_write(chip, AC97_POWER_CONTROL,
  3164. chip->ac97_powerdown);
  3165. mdelay(10);
  3166. snd_cs46xx_codec_write(chip, BA0_AC97_POWERDOWN,
  3167. chip->ac97_powerdown);
  3168. mdelay(5);
  3169. #endif
  3170. snd_ac97_resume(chip->ac97[CS46XX_PRIMARY_CODEC_INDEX]);
  3171. if (chip->ac97[CS46XX_SECONDARY_CODEC_INDEX])
  3172. snd_ac97_resume(chip->ac97[CS46XX_SECONDARY_CODEC_INDEX]);
  3173. if (amp_saved)
  3174. chip->amplifier_ctrl(chip, 1); /* turn amp on */
  3175. else
  3176. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3177. chip->amplifier = amp_saved;
  3178. return 0;
  3179. }
  3180. #endif /* CONFIG_PM */
  3181. /*
  3182. */
  3183. int __devinit snd_cs46xx_create(snd_card_t * card,
  3184. struct pci_dev * pci,
  3185. int external_amp, int thinkpad,
  3186. cs46xx_t ** rchip)
  3187. {
  3188. cs46xx_t *chip;
  3189. int err, idx;
  3190. snd_cs46xx_region_t *region;
  3191. struct cs_card_type *cp;
  3192. u16 ss_card, ss_vendor;
  3193. static snd_device_ops_t ops = {
  3194. .dev_free = snd_cs46xx_dev_free,
  3195. };
  3196. *rchip = NULL;
  3197. /* enable PCI device */
  3198. if ((err = pci_enable_device(pci)) < 0)
  3199. return err;
  3200. chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
  3201. if (chip == NULL) {
  3202. pci_disable_device(pci);
  3203. return -ENOMEM;
  3204. }
  3205. spin_lock_init(&chip->reg_lock);
  3206. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3207. init_MUTEX(&chip->spos_mutex);
  3208. #endif
  3209. chip->card = card;
  3210. chip->pci = pci;
  3211. chip->irq = -1;
  3212. chip->ba0_addr = pci_resource_start(pci, 0);
  3213. chip->ba1_addr = pci_resource_start(pci, 1);
  3214. if (chip->ba0_addr == 0 || chip->ba0_addr == (unsigned long)~0 ||
  3215. chip->ba1_addr == 0 || chip->ba1_addr == (unsigned long)~0) {
  3216. snd_printk("wrong address(es) - ba0 = 0x%lx, ba1 = 0x%lx\n", chip->ba0_addr, chip->ba1_addr);
  3217. snd_cs46xx_free(chip);
  3218. return -ENOMEM;
  3219. }
  3220. region = &chip->region.name.ba0;
  3221. strcpy(region->name, "CS46xx_BA0");
  3222. region->base = chip->ba0_addr;
  3223. region->size = CS46XX_BA0_SIZE;
  3224. region = &chip->region.name.data0;
  3225. strcpy(region->name, "CS46xx_BA1_data0");
  3226. region->base = chip->ba1_addr + BA1_SP_DMEM0;
  3227. region->size = CS46XX_BA1_DATA0_SIZE;
  3228. region = &chip->region.name.data1;
  3229. strcpy(region->name, "CS46xx_BA1_data1");
  3230. region->base = chip->ba1_addr + BA1_SP_DMEM1;
  3231. region->size = CS46XX_BA1_DATA1_SIZE;
  3232. region = &chip->region.name.pmem;
  3233. strcpy(region->name, "CS46xx_BA1_pmem");
  3234. region->base = chip->ba1_addr + BA1_SP_PMEM;
  3235. region->size = CS46XX_BA1_PRG_SIZE;
  3236. region = &chip->region.name.reg;
  3237. strcpy(region->name, "CS46xx_BA1_reg");
  3238. region->base = chip->ba1_addr + BA1_SP_REG;
  3239. region->size = CS46XX_BA1_REG_SIZE;
  3240. /* set up amp and clkrun hack */
  3241. pci_read_config_word(pci, PCI_SUBSYSTEM_VENDOR_ID, &ss_vendor);
  3242. pci_read_config_word(pci, PCI_SUBSYSTEM_ID, &ss_card);
  3243. for (cp = &cards[0]; cp->name; cp++) {
  3244. if (cp->vendor == ss_vendor && cp->id == ss_card) {
  3245. snd_printdd ("hack for %s enabled\n", cp->name);
  3246. chip->amplifier_ctrl = cp->amp;
  3247. chip->active_ctrl = cp->active;
  3248. chip->mixer_init = cp->mixer_init;
  3249. if (cp->init)
  3250. cp->init(chip);
  3251. break;
  3252. }
  3253. }
  3254. if (external_amp) {
  3255. snd_printk("Crystal EAPD support forced on.\n");
  3256. chip->amplifier_ctrl = amp_voyetra;
  3257. }
  3258. if (thinkpad) {
  3259. snd_printk("Activating CLKRUN hack for Thinkpad.\n");
  3260. chip->active_ctrl = clkrun_hack;
  3261. clkrun_init(chip);
  3262. }
  3263. if (chip->amplifier_ctrl == NULL)
  3264. chip->amplifier_ctrl = amp_none;
  3265. if (chip->active_ctrl == NULL)
  3266. chip->active_ctrl = amp_none;
  3267. chip->active_ctrl(chip, 1); /* enable CLKRUN */
  3268. pci_set_master(pci);
  3269. for (idx = 0; idx < 5; idx++) {
  3270. region = &chip->region.idx[idx];
  3271. if ((region->resource = request_mem_region(region->base, region->size, region->name)) == NULL) {
  3272. snd_printk("unable to request memory region 0x%lx-0x%lx\n", region->base, region->base + region->size - 1);
  3273. snd_cs46xx_free(chip);
  3274. return -EBUSY;
  3275. }
  3276. region->remap_addr = ioremap_nocache(region->base, region->size);
  3277. if (region->remap_addr == NULL) {
  3278. snd_printk("%s ioremap problem\n", region->name);
  3279. snd_cs46xx_free(chip);
  3280. return -ENOMEM;
  3281. }
  3282. }
  3283. if (request_irq(pci->irq, snd_cs46xx_interrupt, SA_INTERRUPT|SA_SHIRQ, "CS46XX", (void *) chip)) {
  3284. snd_printk("unable to grab IRQ %d\n", pci->irq);
  3285. snd_cs46xx_free(chip);
  3286. return -EBUSY;
  3287. }
  3288. chip->irq = pci->irq;
  3289. #ifdef CONFIG_SND_CS46XX_NEW_DSP
  3290. chip->dsp_spos_instance = cs46xx_dsp_spos_create(chip);
  3291. if (chip->dsp_spos_instance == NULL) {
  3292. snd_cs46xx_free(chip);
  3293. return -ENOMEM;
  3294. }
  3295. #endif
  3296. err = snd_cs46xx_chip_init(chip);
  3297. if (err < 0) {
  3298. snd_cs46xx_free(chip);
  3299. return err;
  3300. }
  3301. if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
  3302. snd_cs46xx_free(chip);
  3303. return err;
  3304. }
  3305. snd_cs46xx_proc_init(card, chip);
  3306. snd_card_set_pm_callback(card, snd_cs46xx_suspend, snd_cs46xx_resume, chip);
  3307. chip->active_ctrl(chip, -1); /* disable CLKRUN */
  3308. snd_card_set_dev(card, &pci->dev);
  3309. *rchip = chip;
  3310. return 0;
  3311. }