trident_main.c 118 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Maintained by Jaroslav Kysela <[email protected]>
  4. * Originated by [email protected]
  5. * Fri Feb 19 15:55:28 MST 1999
  6. * Routines for control of Trident 4DWave (DX and NX) chip
  7. *
  8. * BUGS:
  9. *
  10. * TODO:
  11. * ---
  12. *
  13. * SiS7018 S/PDIF support by Thomas Winischhofer <[email protected]>
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/gameport.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/export.h>
  24. #include <linux/io.h>
  25. #include <sound/core.h>
  26. #include <sound/info.h>
  27. #include <sound/control.h>
  28. #include <sound/tlv.h>
  29. #include "trident.h"
  30. #include <sound/asoundef.h>
  31. static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  32. struct snd_trident_voice * voice,
  33. struct snd_pcm_substream *substream);
  34. static int snd_trident_pcm_mixer_free(struct snd_trident *trident,
  35. struct snd_trident_voice * voice,
  36. struct snd_pcm_substream *substream);
  37. static irqreturn_t snd_trident_interrupt(int irq, void *dev_id);
  38. static int snd_trident_sis_reset(struct snd_trident *trident);
  39. static void snd_trident_clear_voices(struct snd_trident * trident,
  40. unsigned short v_min, unsigned short v_max);
  41. static void snd_trident_free(struct snd_card *card);
  42. /*
  43. * common I/O routines
  44. */
  45. #if 0
  46. static void snd_trident_print_voice_regs(struct snd_trident *trident, int voice)
  47. {
  48. unsigned int val, tmp;
  49. dev_dbg(trident->card->dev, "Trident voice %i:\n", voice);
  50. outb(voice, TRID_REG(trident, T4D_LFO_GC_CIR));
  51. val = inl(TRID_REG(trident, CH_LBA));
  52. dev_dbg(trident->card->dev, "LBA: 0x%x\n", val);
  53. val = inl(TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  54. dev_dbg(trident->card->dev, "GVSel: %i\n", val >> 31);
  55. dev_dbg(trident->card->dev, "Pan: 0x%x\n", (val >> 24) & 0x7f);
  56. dev_dbg(trident->card->dev, "Vol: 0x%x\n", (val >> 16) & 0xff);
  57. dev_dbg(trident->card->dev, "CTRL: 0x%x\n", (val >> 12) & 0x0f);
  58. dev_dbg(trident->card->dev, "EC: 0x%x\n", val & 0x0fff);
  59. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  60. val = inl(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS));
  61. dev_dbg(trident->card->dev, "CSO: 0x%x\n", val >> 16);
  62. dev_dbg(trident->card->dev, "Alpha: 0x%x\n", (val >> 4) & 0x0fff);
  63. dev_dbg(trident->card->dev, "FMS: 0x%x\n", val & 0x0f);
  64. val = inl(TRID_REG(trident, CH_DX_ESO_DELTA));
  65. dev_dbg(trident->card->dev, "ESO: 0x%x\n", val >> 16);
  66. dev_dbg(trident->card->dev, "Delta: 0x%x\n", val & 0xffff);
  67. val = inl(TRID_REG(trident, CH_DX_FMC_RVOL_CVOL));
  68. } else { // TRIDENT_DEVICE_ID_NX
  69. val = inl(TRID_REG(trident, CH_NX_DELTA_CSO));
  70. tmp = (val >> 24) & 0xff;
  71. dev_dbg(trident->card->dev, "CSO: 0x%x\n", val & 0x00ffffff);
  72. val = inl(TRID_REG(trident, CH_NX_DELTA_ESO));
  73. tmp |= (val >> 16) & 0xff00;
  74. dev_dbg(trident->card->dev, "Delta: 0x%x\n", tmp);
  75. dev_dbg(trident->card->dev, "ESO: 0x%x\n", val & 0x00ffffff);
  76. val = inl(TRID_REG(trident, CH_NX_ALPHA_FMS_FMC_RVOL_CVOL));
  77. dev_dbg(trident->card->dev, "Alpha: 0x%x\n", val >> 20);
  78. dev_dbg(trident->card->dev, "FMS: 0x%x\n", (val >> 16) & 0x0f);
  79. }
  80. dev_dbg(trident->card->dev, "FMC: 0x%x\n", (val >> 14) & 3);
  81. dev_dbg(trident->card->dev, "RVol: 0x%x\n", (val >> 7) & 0x7f);
  82. dev_dbg(trident->card->dev, "CVol: 0x%x\n", val & 0x7f);
  83. }
  84. #endif
  85. /*---------------------------------------------------------------------------
  86. unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  87. Description: This routine will do all of the reading from the external
  88. CODEC (AC97).
  89. Parameters: ac97 - ac97 codec structure
  90. reg - CODEC register index, from AC97 Hal.
  91. returns: 16 bit value read from the AC97.
  92. ---------------------------------------------------------------------------*/
  93. static unsigned short snd_trident_codec_read(struct snd_ac97 *ac97, unsigned short reg)
  94. {
  95. unsigned int data = 0, treg;
  96. unsigned short count = 0xffff;
  97. unsigned long flags;
  98. struct snd_trident *trident = ac97->private_data;
  99. spin_lock_irqsave(&trident->reg_lock, flags);
  100. if (trident->device == TRIDENT_DEVICE_ID_DX) {
  101. data = (DX_AC97_BUSY_READ | (reg & 0x000000ff));
  102. outl(data, TRID_REG(trident, DX_ACR1_AC97_R));
  103. do {
  104. data = inl(TRID_REG(trident, DX_ACR1_AC97_R));
  105. if ((data & DX_AC97_BUSY_READ) == 0)
  106. break;
  107. } while (--count);
  108. } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  109. data = (NX_AC97_BUSY_READ | (reg & 0x000000ff));
  110. treg = ac97->num == 0 ? NX_ACR2_AC97_R_PRIMARY : NX_ACR3_AC97_R_SECONDARY;
  111. outl(data, TRID_REG(trident, treg));
  112. do {
  113. data = inl(TRID_REG(trident, treg));
  114. if ((data & 0x00000C00) == 0)
  115. break;
  116. } while (--count);
  117. } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  118. data = SI_AC97_BUSY_READ | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  119. if (ac97->num == 1)
  120. data |= SI_AC97_SECONDARY;
  121. outl(data, TRID_REG(trident, SI_AC97_READ));
  122. do {
  123. data = inl(TRID_REG(trident, SI_AC97_READ));
  124. if ((data & (SI_AC97_BUSY_READ)) == 0)
  125. break;
  126. } while (--count);
  127. }
  128. if (count == 0 && !trident->ac97_detect) {
  129. dev_err(trident->card->dev,
  130. "ac97 codec read TIMEOUT [0x%x/0x%x]!!!\n",
  131. reg, data);
  132. data = 0;
  133. }
  134. spin_unlock_irqrestore(&trident->reg_lock, flags);
  135. return ((unsigned short) (data >> 16));
  136. }
  137. /*---------------------------------------------------------------------------
  138. void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  139. unsigned short wdata)
  140. Description: This routine will do all of the writing to the external
  141. CODEC (AC97).
  142. Parameters: ac97 - ac97 codec structure
  143. reg - CODEC register index, from AC97 Hal.
  144. data - Lower 16 bits are the data to write to CODEC.
  145. returns: TRUE if everything went ok, else FALSE.
  146. ---------------------------------------------------------------------------*/
  147. static void snd_trident_codec_write(struct snd_ac97 *ac97, unsigned short reg,
  148. unsigned short wdata)
  149. {
  150. unsigned int address, data;
  151. unsigned short count = 0xffff;
  152. unsigned long flags;
  153. struct snd_trident *trident = ac97->private_data;
  154. data = ((unsigned long) wdata) << 16;
  155. spin_lock_irqsave(&trident->reg_lock, flags);
  156. if (trident->device == TRIDENT_DEVICE_ID_DX) {
  157. address = DX_ACR0_AC97_W;
  158. /* read AC-97 write register status */
  159. do {
  160. if ((inw(TRID_REG(trident, address)) & DX_AC97_BUSY_WRITE) == 0)
  161. break;
  162. } while (--count);
  163. data |= (DX_AC97_BUSY_WRITE | (reg & 0x000000ff));
  164. } else if (trident->device == TRIDENT_DEVICE_ID_NX) {
  165. address = NX_ACR1_AC97_W;
  166. /* read AC-97 write register status */
  167. do {
  168. if ((inw(TRID_REG(trident, address)) & NX_AC97_BUSY_WRITE) == 0)
  169. break;
  170. } while (--count);
  171. data |= (NX_AC97_BUSY_WRITE | (ac97->num << 8) | (reg & 0x000000ff));
  172. } else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  173. address = SI_AC97_WRITE;
  174. /* read AC-97 write register status */
  175. do {
  176. if ((inw(TRID_REG(trident, address)) & (SI_AC97_BUSY_WRITE)) == 0)
  177. break;
  178. } while (--count);
  179. data |= SI_AC97_BUSY_WRITE | SI_AC97_AUDIO_BUSY | (reg & 0x000000ff);
  180. if (ac97->num == 1)
  181. data |= SI_AC97_SECONDARY;
  182. } else {
  183. address = 0; /* keep GCC happy */
  184. count = 0; /* return */
  185. }
  186. if (count == 0) {
  187. spin_unlock_irqrestore(&trident->reg_lock, flags);
  188. return;
  189. }
  190. outl(data, TRID_REG(trident, address));
  191. spin_unlock_irqrestore(&trident->reg_lock, flags);
  192. }
  193. /*---------------------------------------------------------------------------
  194. void snd_trident_enable_eso(struct snd_trident *trident)
  195. Description: This routine will enable end of loop interrupts.
  196. End of loop interrupts will occur when a running
  197. channel reaches ESO.
  198. Also enables middle of loop interrupts.
  199. Parameters: trident - pointer to target device class for 4DWave.
  200. ---------------------------------------------------------------------------*/
  201. static void snd_trident_enable_eso(struct snd_trident * trident)
  202. {
  203. unsigned int val;
  204. val = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  205. val |= ENDLP_IE;
  206. val |= MIDLP_IE;
  207. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  208. val |= BANK_B_EN;
  209. outl(val, TRID_REG(trident, T4D_LFO_GC_CIR));
  210. }
  211. /*---------------------------------------------------------------------------
  212. void snd_trident_disable_eso(struct snd_trident *trident)
  213. Description: This routine will disable end of loop interrupts.
  214. End of loop interrupts will occur when a running
  215. channel reaches ESO.
  216. Also disables middle of loop interrupts.
  217. Parameters:
  218. trident - pointer to target device class for 4DWave.
  219. returns: TRUE if everything went ok, else FALSE.
  220. ---------------------------------------------------------------------------*/
  221. static void snd_trident_disable_eso(struct snd_trident * trident)
  222. {
  223. unsigned int tmp;
  224. tmp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  225. tmp &= ~ENDLP_IE;
  226. tmp &= ~MIDLP_IE;
  227. outl(tmp, TRID_REG(trident, T4D_LFO_GC_CIR));
  228. }
  229. /*---------------------------------------------------------------------------
  230. void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
  231. Description: Start a voice, any channel 0 thru 63.
  232. This routine automatically handles the fact that there are
  233. more than 32 channels available.
  234. Parameters : voice - Voice number 0 thru n.
  235. trident - pointer to target device class for 4DWave.
  236. Return Value: None.
  237. ---------------------------------------------------------------------------*/
  238. void snd_trident_start_voice(struct snd_trident * trident, unsigned int voice)
  239. {
  240. unsigned int mask = 1 << (voice & 0x1f);
  241. unsigned int reg = (voice & 0x20) ? T4D_START_B : T4D_START_A;
  242. outl(mask, TRID_REG(trident, reg));
  243. }
  244. EXPORT_SYMBOL(snd_trident_start_voice);
  245. /*---------------------------------------------------------------------------
  246. void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
  247. Description: Stop a voice, any channel 0 thru 63.
  248. This routine automatically handles the fact that there are
  249. more than 32 channels available.
  250. Parameters : voice - Voice number 0 thru n.
  251. trident - pointer to target device class for 4DWave.
  252. Return Value: None.
  253. ---------------------------------------------------------------------------*/
  254. void snd_trident_stop_voice(struct snd_trident * trident, unsigned int voice)
  255. {
  256. unsigned int mask = 1 << (voice & 0x1f);
  257. unsigned int reg = (voice & 0x20) ? T4D_STOP_B : T4D_STOP_A;
  258. outl(mask, TRID_REG(trident, reg));
  259. }
  260. EXPORT_SYMBOL(snd_trident_stop_voice);
  261. /*---------------------------------------------------------------------------
  262. int snd_trident_allocate_pcm_channel(struct snd_trident *trident)
  263. Description: Allocate hardware channel in Bank B (32-63).
  264. Parameters : trident - pointer to target device class for 4DWave.
  265. Return Value: hardware channel - 32-63 or -1 when no channel is available
  266. ---------------------------------------------------------------------------*/
  267. static int snd_trident_allocate_pcm_channel(struct snd_trident * trident)
  268. {
  269. int idx;
  270. if (trident->ChanPCMcnt >= trident->ChanPCM)
  271. return -1;
  272. for (idx = 31; idx >= 0; idx--) {
  273. if (!(trident->ChanMap[T4D_BANK_B] & (1 << idx))) {
  274. trident->ChanMap[T4D_BANK_B] |= 1 << idx;
  275. trident->ChanPCMcnt++;
  276. return idx + 32;
  277. }
  278. }
  279. return -1;
  280. }
  281. /*---------------------------------------------------------------------------
  282. void snd_trident_free_pcm_channel(int channel)
  283. Description: Free hardware channel in Bank B (32-63)
  284. Parameters : trident - pointer to target device class for 4DWave.
  285. channel - hardware channel number 0-63
  286. Return Value: none
  287. ---------------------------------------------------------------------------*/
  288. static void snd_trident_free_pcm_channel(struct snd_trident *trident, int channel)
  289. {
  290. if (channel < 32 || channel > 63)
  291. return;
  292. channel &= 0x1f;
  293. if (trident->ChanMap[T4D_BANK_B] & (1 << channel)) {
  294. trident->ChanMap[T4D_BANK_B] &= ~(1 << channel);
  295. trident->ChanPCMcnt--;
  296. }
  297. }
  298. /*---------------------------------------------------------------------------
  299. unsigned int snd_trident_allocate_synth_channel(void)
  300. Description: Allocate hardware channel in Bank A (0-31).
  301. Parameters : trident - pointer to target device class for 4DWave.
  302. Return Value: hardware channel - 0-31 or -1 when no channel is available
  303. ---------------------------------------------------------------------------*/
  304. static int snd_trident_allocate_synth_channel(struct snd_trident * trident)
  305. {
  306. int idx;
  307. for (idx = 31; idx >= 0; idx--) {
  308. if (!(trident->ChanMap[T4D_BANK_A] & (1 << idx))) {
  309. trident->ChanMap[T4D_BANK_A] |= 1 << idx;
  310. trident->synth.ChanSynthCount++;
  311. return idx;
  312. }
  313. }
  314. return -1;
  315. }
  316. /*---------------------------------------------------------------------------
  317. void snd_trident_free_synth_channel( int channel )
  318. Description: Free hardware channel in Bank B (0-31).
  319. Parameters : trident - pointer to target device class for 4DWave.
  320. channel - hardware channel number 0-63
  321. Return Value: none
  322. ---------------------------------------------------------------------------*/
  323. static void snd_trident_free_synth_channel(struct snd_trident *trident, int channel)
  324. {
  325. if (channel < 0 || channel > 31)
  326. return;
  327. channel &= 0x1f;
  328. if (trident->ChanMap[T4D_BANK_A] & (1 << channel)) {
  329. trident->ChanMap[T4D_BANK_A] &= ~(1 << channel);
  330. trident->synth.ChanSynthCount--;
  331. }
  332. }
  333. /*---------------------------------------------------------------------------
  334. snd_trident_write_voice_regs
  335. Description: This routine will complete and write the 5 hardware channel
  336. registers to hardware.
  337. Parameters: trident - pointer to target device class for 4DWave.
  338. voice - synthesizer voice structure
  339. Each register field.
  340. ---------------------------------------------------------------------------*/
  341. void snd_trident_write_voice_regs(struct snd_trident * trident,
  342. struct snd_trident_voice * voice)
  343. {
  344. unsigned int FmcRvolCvol;
  345. unsigned int regs[5];
  346. regs[1] = voice->LBA;
  347. regs[4] = (voice->GVSel << 31) |
  348. ((voice->Pan & 0x0000007f) << 24) |
  349. ((voice->CTRL & 0x0000000f) << 12);
  350. FmcRvolCvol = ((voice->FMC & 3) << 14) |
  351. ((voice->RVol & 0x7f) << 7) |
  352. (voice->CVol & 0x7f);
  353. switch (trident->device) {
  354. case TRIDENT_DEVICE_ID_SI7018:
  355. regs[4] |= voice->number > 31 ?
  356. (voice->Vol & 0x000003ff) :
  357. ((voice->Vol & 0x00003fc) << (16-2)) |
  358. (voice->EC & 0x00000fff);
  359. regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  360. (voice->FMS & 0x0000000f);
  361. regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  362. regs[3] = (voice->Attribute << 16) | FmcRvolCvol;
  363. break;
  364. case TRIDENT_DEVICE_ID_DX:
  365. regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  366. (voice->EC & 0x00000fff);
  367. regs[0] = (voice->CSO << 16) | ((voice->Alpha & 0x00000fff) << 4) |
  368. (voice->FMS & 0x0000000f);
  369. regs[2] = (voice->ESO << 16) | (voice->Delta & 0x0ffff);
  370. regs[3] = FmcRvolCvol;
  371. break;
  372. case TRIDENT_DEVICE_ID_NX:
  373. regs[4] |= ((voice->Vol & 0x000003fc) << (16-2)) |
  374. (voice->EC & 0x00000fff);
  375. regs[0] = (voice->Delta << 24) | (voice->CSO & 0x00ffffff);
  376. regs[2] = ((voice->Delta << 16) & 0xff000000) |
  377. (voice->ESO & 0x00ffffff);
  378. regs[3] = (voice->Alpha << 20) |
  379. ((voice->FMS & 0x0000000f) << 16) | FmcRvolCvol;
  380. break;
  381. default:
  382. snd_BUG();
  383. return;
  384. }
  385. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  386. outl(regs[0], TRID_REG(trident, CH_START + 0));
  387. outl(regs[1], TRID_REG(trident, CH_START + 4));
  388. outl(regs[2], TRID_REG(trident, CH_START + 8));
  389. outl(regs[3], TRID_REG(trident, CH_START + 12));
  390. outl(regs[4], TRID_REG(trident, CH_START + 16));
  391. #if 0
  392. dev_dbg(trident->card->dev, "written %i channel:\n", voice->number);
  393. dev_dbg(trident->card->dev, " regs[0] = 0x%x/0x%x\n",
  394. regs[0], inl(TRID_REG(trident, CH_START + 0)));
  395. dev_dbg(trident->card->dev, " regs[1] = 0x%x/0x%x\n",
  396. regs[1], inl(TRID_REG(trident, CH_START + 4)));
  397. dev_dbg(trident->card->dev, " regs[2] = 0x%x/0x%x\n",
  398. regs[2], inl(TRID_REG(trident, CH_START + 8)));
  399. dev_dbg(trident->card->dev, " regs[3] = 0x%x/0x%x\n",
  400. regs[3], inl(TRID_REG(trident, CH_START + 12)));
  401. dev_dbg(trident->card->dev, " regs[4] = 0x%x/0x%x\n",
  402. regs[4], inl(TRID_REG(trident, CH_START + 16)));
  403. #endif
  404. }
  405. EXPORT_SYMBOL(snd_trident_write_voice_regs);
  406. /*---------------------------------------------------------------------------
  407. snd_trident_write_cso_reg
  408. Description: This routine will write the new CSO offset
  409. register to hardware.
  410. Parameters: trident - pointer to target device class for 4DWave.
  411. voice - synthesizer voice structure
  412. CSO - new CSO value
  413. ---------------------------------------------------------------------------*/
  414. static void snd_trident_write_cso_reg(struct snd_trident * trident,
  415. struct snd_trident_voice * voice,
  416. unsigned int CSO)
  417. {
  418. voice->CSO = CSO;
  419. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  420. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  421. outw(voice->CSO, TRID_REG(trident, CH_DX_CSO_ALPHA_FMS) + 2);
  422. } else {
  423. outl((voice->Delta << 24) |
  424. (voice->CSO & 0x00ffffff), TRID_REG(trident, CH_NX_DELTA_CSO));
  425. }
  426. }
  427. /*---------------------------------------------------------------------------
  428. snd_trident_write_eso_reg
  429. Description: This routine will write the new ESO offset
  430. register to hardware.
  431. Parameters: trident - pointer to target device class for 4DWave.
  432. voice - synthesizer voice structure
  433. ESO - new ESO value
  434. ---------------------------------------------------------------------------*/
  435. static void snd_trident_write_eso_reg(struct snd_trident * trident,
  436. struct snd_trident_voice * voice,
  437. unsigned int ESO)
  438. {
  439. voice->ESO = ESO;
  440. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  441. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  442. outw(voice->ESO, TRID_REG(trident, CH_DX_ESO_DELTA) + 2);
  443. } else {
  444. outl(((voice->Delta << 16) & 0xff000000) | (voice->ESO & 0x00ffffff),
  445. TRID_REG(trident, CH_NX_DELTA_ESO));
  446. }
  447. }
  448. /*---------------------------------------------------------------------------
  449. snd_trident_write_vol_reg
  450. Description: This routine will write the new voice volume
  451. register to hardware.
  452. Parameters: trident - pointer to target device class for 4DWave.
  453. voice - synthesizer voice structure
  454. Vol - new voice volume
  455. ---------------------------------------------------------------------------*/
  456. static void snd_trident_write_vol_reg(struct snd_trident * trident,
  457. struct snd_trident_voice * voice,
  458. unsigned int Vol)
  459. {
  460. voice->Vol = Vol;
  461. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  462. switch (trident->device) {
  463. case TRIDENT_DEVICE_ID_DX:
  464. case TRIDENT_DEVICE_ID_NX:
  465. outb(voice->Vol >> 2, TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 2));
  466. break;
  467. case TRIDENT_DEVICE_ID_SI7018:
  468. /* dev_dbg(trident->card->dev, "voice->Vol = 0x%x\n", voice->Vol); */
  469. outw((voice->CTRL << 12) | voice->Vol,
  470. TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC));
  471. break;
  472. }
  473. }
  474. /*---------------------------------------------------------------------------
  475. snd_trident_write_pan_reg
  476. Description: This routine will write the new voice pan
  477. register to hardware.
  478. Parameters: trident - pointer to target device class for 4DWave.
  479. voice - synthesizer voice structure
  480. Pan - new pan value
  481. ---------------------------------------------------------------------------*/
  482. static void snd_trident_write_pan_reg(struct snd_trident * trident,
  483. struct snd_trident_voice * voice,
  484. unsigned int Pan)
  485. {
  486. voice->Pan = Pan;
  487. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  488. outb(((voice->GVSel & 0x01) << 7) | (voice->Pan & 0x7f),
  489. TRID_REG(trident, CH_GVSEL_PAN_VOL_CTRL_EC + 3));
  490. }
  491. /*---------------------------------------------------------------------------
  492. snd_trident_write_rvol_reg
  493. Description: This routine will write the new reverb volume
  494. register to hardware.
  495. Parameters: trident - pointer to target device class for 4DWave.
  496. voice - synthesizer voice structure
  497. RVol - new reverb volume
  498. ---------------------------------------------------------------------------*/
  499. static void snd_trident_write_rvol_reg(struct snd_trident * trident,
  500. struct snd_trident_voice * voice,
  501. unsigned int RVol)
  502. {
  503. voice->RVol = RVol;
  504. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  505. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  506. (voice->CVol & 0x007f),
  507. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  508. CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  509. }
  510. /*---------------------------------------------------------------------------
  511. snd_trident_write_cvol_reg
  512. Description: This routine will write the new chorus volume
  513. register to hardware.
  514. Parameters: trident - pointer to target device class for 4DWave.
  515. voice - synthesizer voice structure
  516. CVol - new chorus volume
  517. ---------------------------------------------------------------------------*/
  518. static void snd_trident_write_cvol_reg(struct snd_trident * trident,
  519. struct snd_trident_voice * voice,
  520. unsigned int CVol)
  521. {
  522. voice->CVol = CVol;
  523. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  524. outw(((voice->FMC & 0x0003) << 14) | ((voice->RVol & 0x007f) << 7) |
  525. (voice->CVol & 0x007f),
  526. TRID_REG(trident, trident->device == TRIDENT_DEVICE_ID_NX ?
  527. CH_NX_ALPHA_FMS_FMC_RVOL_CVOL : CH_DX_FMC_RVOL_CVOL));
  528. }
  529. /*---------------------------------------------------------------------------
  530. snd_trident_convert_rate
  531. Description: This routine converts rate in HZ to hardware delta value.
  532. Parameters: trident - pointer to target device class for 4DWave.
  533. rate - Real or Virtual channel number.
  534. Returns: Delta value.
  535. ---------------------------------------------------------------------------*/
  536. static unsigned int snd_trident_convert_rate(unsigned int rate)
  537. {
  538. unsigned int delta;
  539. // We special case 44100 and 8000 since rounding with the equation
  540. // does not give us an accurate enough value. For 11025 and 22050
  541. // the equation gives us the best answer. All other frequencies will
  542. // also use the equation. JDW
  543. if (rate == 44100)
  544. delta = 0xeb3;
  545. else if (rate == 8000)
  546. delta = 0x2ab;
  547. else if (rate == 48000)
  548. delta = 0x1000;
  549. else
  550. delta = DIV_ROUND_CLOSEST(rate << 12, 48000) & 0x0000ffff;
  551. return delta;
  552. }
  553. /*---------------------------------------------------------------------------
  554. snd_trident_convert_adc_rate
  555. Description: This routine converts rate in HZ to hardware delta value.
  556. Parameters: trident - pointer to target device class for 4DWave.
  557. rate - Real or Virtual channel number.
  558. Returns: Delta value.
  559. ---------------------------------------------------------------------------*/
  560. static unsigned int snd_trident_convert_adc_rate(unsigned int rate)
  561. {
  562. unsigned int delta;
  563. // We special case 44100 and 8000 since rounding with the equation
  564. // does not give us an accurate enough value. For 11025 and 22050
  565. // the equation gives us the best answer. All other frequencies will
  566. // also use the equation. JDW
  567. if (rate == 44100)
  568. delta = 0x116a;
  569. else if (rate == 8000)
  570. delta = 0x6000;
  571. else if (rate == 48000)
  572. delta = 0x1000;
  573. else
  574. delta = ((48000 << 12) / rate) & 0x0000ffff;
  575. return delta;
  576. }
  577. /*---------------------------------------------------------------------------
  578. snd_trident_spurious_threshold
  579. Description: This routine converts rate in HZ to spurious threshold.
  580. Parameters: trident - pointer to target device class for 4DWave.
  581. rate - Real or Virtual channel number.
  582. Returns: Delta value.
  583. ---------------------------------------------------------------------------*/
  584. static unsigned int snd_trident_spurious_threshold(unsigned int rate,
  585. unsigned int period_size)
  586. {
  587. unsigned int res = (rate * period_size) / 48000;
  588. if (res < 64)
  589. res = res / 2;
  590. else
  591. res -= 32;
  592. return res;
  593. }
  594. /*---------------------------------------------------------------------------
  595. snd_trident_control_mode
  596. Description: This routine returns a control mode for a PCM channel.
  597. Parameters: trident - pointer to target device class for 4DWave.
  598. substream - PCM substream
  599. Returns: Control value.
  600. ---------------------------------------------------------------------------*/
  601. static unsigned int snd_trident_control_mode(struct snd_pcm_substream *substream)
  602. {
  603. unsigned int CTRL;
  604. struct snd_pcm_runtime *runtime = substream->runtime;
  605. /* set ctrl mode
  606. CTRL default: 8-bit (unsigned) mono, loop mode enabled
  607. */
  608. CTRL = 0x00000001;
  609. if (snd_pcm_format_width(runtime->format) == 16)
  610. CTRL |= 0x00000008; // 16-bit data
  611. if (snd_pcm_format_signed(runtime->format))
  612. CTRL |= 0x00000002; // signed data
  613. if (runtime->channels > 1)
  614. CTRL |= 0x00000004; // stereo data
  615. return CTRL;
  616. }
  617. /*
  618. * PCM part
  619. */
  620. /*---------------------------------------------------------------------------
  621. snd_trident_allocate_pcm_mem
  622. Description: Allocate PCM ring buffer for given substream
  623. Parameters: substream - PCM substream class
  624. hw_params - hardware parameters
  625. Returns: Error status
  626. ---------------------------------------------------------------------------*/
  627. static int snd_trident_allocate_pcm_mem(struct snd_pcm_substream *substream,
  628. struct snd_pcm_hw_params *hw_params)
  629. {
  630. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  631. struct snd_pcm_runtime *runtime = substream->runtime;
  632. struct snd_trident_voice *voice = runtime->private_data;
  633. if (trident->tlb.entries) {
  634. if (runtime->buffer_changed) {
  635. if (voice->memblk)
  636. snd_trident_free_pages(trident, voice->memblk);
  637. voice->memblk = snd_trident_alloc_pages(trident, substream);
  638. if (voice->memblk == NULL)
  639. return -ENOMEM;
  640. }
  641. }
  642. return 0;
  643. }
  644. /*---------------------------------------------------------------------------
  645. snd_trident_allocate_evoice
  646. Description: Allocate extra voice as interrupt generator
  647. Parameters: substream - PCM substream class
  648. hw_params - hardware parameters
  649. Returns: Error status
  650. ---------------------------------------------------------------------------*/
  651. static int snd_trident_allocate_evoice(struct snd_pcm_substream *substream,
  652. struct snd_pcm_hw_params *hw_params)
  653. {
  654. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  655. struct snd_pcm_runtime *runtime = substream->runtime;
  656. struct snd_trident_voice *voice = runtime->private_data;
  657. struct snd_trident_voice *evoice = voice->extra;
  658. /* voice management */
  659. if (params_buffer_size(hw_params) / 2 != params_period_size(hw_params)) {
  660. if (evoice == NULL) {
  661. evoice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  662. if (evoice == NULL)
  663. return -ENOMEM;
  664. voice->extra = evoice;
  665. evoice->substream = substream;
  666. }
  667. } else {
  668. if (evoice != NULL) {
  669. snd_trident_free_voice(trident, evoice);
  670. voice->extra = evoice = NULL;
  671. }
  672. }
  673. return 0;
  674. }
  675. /*---------------------------------------------------------------------------
  676. snd_trident_hw_params
  677. Description: Set the hardware parameters for the playback device.
  678. Parameters: substream - PCM substream class
  679. hw_params - hardware parameters
  680. Returns: Error status
  681. ---------------------------------------------------------------------------*/
  682. static int snd_trident_hw_params(struct snd_pcm_substream *substream,
  683. struct snd_pcm_hw_params *hw_params)
  684. {
  685. int err;
  686. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  687. if (err >= 0)
  688. err = snd_trident_allocate_evoice(substream, hw_params);
  689. return err;
  690. }
  691. /*---------------------------------------------------------------------------
  692. snd_trident_playback_hw_free
  693. Description: Release the hardware resources for the playback device.
  694. Parameters: substream - PCM substream class
  695. Returns: Error status
  696. ---------------------------------------------------------------------------*/
  697. static int snd_trident_hw_free(struct snd_pcm_substream *substream)
  698. {
  699. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  700. struct snd_pcm_runtime *runtime = substream->runtime;
  701. struct snd_trident_voice *voice = runtime->private_data;
  702. struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
  703. if (trident->tlb.entries) {
  704. if (voice && voice->memblk) {
  705. snd_trident_free_pages(trident, voice->memblk);
  706. voice->memblk = NULL;
  707. }
  708. }
  709. if (evoice != NULL) {
  710. snd_trident_free_voice(trident, evoice);
  711. voice->extra = NULL;
  712. }
  713. return 0;
  714. }
  715. /*---------------------------------------------------------------------------
  716. snd_trident_playback_prepare
  717. Description: Prepare playback device for playback.
  718. Parameters: substream - PCM substream class
  719. Returns: Error status
  720. ---------------------------------------------------------------------------*/
  721. static int snd_trident_playback_prepare(struct snd_pcm_substream *substream)
  722. {
  723. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  724. struct snd_pcm_runtime *runtime = substream->runtime;
  725. struct snd_trident_voice *voice = runtime->private_data;
  726. struct snd_trident_voice *evoice = voice->extra;
  727. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
  728. spin_lock_irq(&trident->reg_lock);
  729. /* set delta (rate) value */
  730. voice->Delta = snd_trident_convert_rate(runtime->rate);
  731. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  732. /* set Loop Begin Address */
  733. if (voice->memblk)
  734. voice->LBA = voice->memblk->offset;
  735. else
  736. voice->LBA = runtime->dma_addr;
  737. voice->CSO = 0;
  738. voice->ESO = runtime->buffer_size - 1; /* in samples */
  739. voice->CTRL = snd_trident_control_mode(substream);
  740. voice->FMC = 3;
  741. voice->GVSel = 1;
  742. voice->EC = 0;
  743. voice->Alpha = 0;
  744. voice->FMS = 0;
  745. voice->Vol = mix->vol;
  746. voice->RVol = mix->rvol;
  747. voice->CVol = mix->cvol;
  748. voice->Pan = mix->pan;
  749. voice->Attribute = 0;
  750. #if 0
  751. voice->Attribute = (1<<(30-16))|(2<<(26-16))|
  752. (0<<(24-16))|(0x1f<<(19-16));
  753. #else
  754. voice->Attribute = 0;
  755. #endif
  756. snd_trident_write_voice_regs(trident, voice);
  757. if (evoice != NULL) {
  758. evoice->Delta = voice->Delta;
  759. evoice->spurious_threshold = voice->spurious_threshold;
  760. evoice->LBA = voice->LBA;
  761. evoice->CSO = 0;
  762. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  763. evoice->CTRL = voice->CTRL;
  764. evoice->FMC = 3;
  765. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  766. evoice->EC = 0;
  767. evoice->Alpha = 0;
  768. evoice->FMS = 0;
  769. evoice->Vol = 0x3ff; /* mute */
  770. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  771. evoice->Pan = 0x7f; /* mute */
  772. #if 0
  773. evoice->Attribute = (1<<(30-16))|(2<<(26-16))|
  774. (0<<(24-16))|(0x1f<<(19-16));
  775. #else
  776. evoice->Attribute = 0;
  777. #endif
  778. snd_trident_write_voice_regs(trident, evoice);
  779. evoice->isync2 = 1;
  780. evoice->isync_mark = runtime->period_size;
  781. evoice->ESO = (runtime->period_size * 2) - 1;
  782. }
  783. spin_unlock_irq(&trident->reg_lock);
  784. return 0;
  785. }
  786. /*---------------------------------------------------------------------------
  787. snd_trident_capture_hw_params
  788. Description: Set the hardware parameters for the capture device.
  789. Parameters: substream - PCM substream class
  790. hw_params - hardware parameters
  791. Returns: Error status
  792. ---------------------------------------------------------------------------*/
  793. static int snd_trident_capture_hw_params(struct snd_pcm_substream *substream,
  794. struct snd_pcm_hw_params *hw_params)
  795. {
  796. return snd_trident_allocate_pcm_mem(substream, hw_params);
  797. }
  798. /*---------------------------------------------------------------------------
  799. snd_trident_capture_prepare
  800. Description: Prepare capture device for playback.
  801. Parameters: substream - PCM substream class
  802. Returns: Error status
  803. ---------------------------------------------------------------------------*/
  804. static int snd_trident_capture_prepare(struct snd_pcm_substream *substream)
  805. {
  806. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  807. struct snd_pcm_runtime *runtime = substream->runtime;
  808. struct snd_trident_voice *voice = runtime->private_data;
  809. unsigned int val, ESO_bytes;
  810. spin_lock_irq(&trident->reg_lock);
  811. // Initialize the channel and set channel Mode
  812. outb(0, TRID_REG(trident, LEGACY_DMAR15));
  813. // Set DMA channel operation mode register
  814. outb(0x54, TRID_REG(trident, LEGACY_DMAR11));
  815. // Set channel buffer Address, DMAR0 expects contiguous PCI memory area
  816. voice->LBA = runtime->dma_addr;
  817. outl(voice->LBA, TRID_REG(trident, LEGACY_DMAR0));
  818. if (voice->memblk)
  819. voice->LBA = voice->memblk->offset;
  820. // set ESO
  821. ESO_bytes = snd_pcm_lib_buffer_bytes(substream) - 1;
  822. outb((ESO_bytes & 0x00ff0000) >> 16, TRID_REG(trident, LEGACY_DMAR6));
  823. outw((ESO_bytes & 0x0000ffff), TRID_REG(trident, LEGACY_DMAR4));
  824. ESO_bytes++;
  825. // Set channel sample rate, 4.12 format
  826. val = DIV_ROUND_CLOSEST(48000U << 12, runtime->rate);
  827. outw(val, TRID_REG(trident, T4D_SBDELTA_DELTA_R));
  828. // Set channel interrupt blk length
  829. if (snd_pcm_format_width(runtime->format) == 16) {
  830. val = (unsigned short) ((ESO_bytes >> 1) - 1);
  831. } else {
  832. val = (unsigned short) (ESO_bytes - 1);
  833. }
  834. outl((val << 16) | val, TRID_REG(trident, T4D_SBBL_SBCL));
  835. // Right now, set format and start to run captureing,
  836. // continuous run loop enable.
  837. trident->bDMAStart = 0x19; // 0001 1001b
  838. if (snd_pcm_format_width(runtime->format) == 16)
  839. trident->bDMAStart |= 0x80;
  840. if (snd_pcm_format_signed(runtime->format))
  841. trident->bDMAStart |= 0x20;
  842. if (runtime->channels > 1)
  843. trident->bDMAStart |= 0x40;
  844. // Prepare capture intr channel
  845. voice->Delta = snd_trident_convert_rate(runtime->rate);
  846. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  847. voice->isync = 1;
  848. voice->isync_mark = runtime->period_size;
  849. voice->isync_max = runtime->buffer_size;
  850. // Set voice parameters
  851. voice->CSO = 0;
  852. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  853. voice->CTRL = snd_trident_control_mode(substream);
  854. voice->FMC = 3;
  855. voice->RVol = 0x7f;
  856. voice->CVol = 0x7f;
  857. voice->GVSel = 1;
  858. voice->Pan = 0x7f; /* mute */
  859. voice->Vol = 0x3ff; /* mute */
  860. voice->EC = 0;
  861. voice->Alpha = 0;
  862. voice->FMS = 0;
  863. voice->Attribute = 0;
  864. snd_trident_write_voice_regs(trident, voice);
  865. spin_unlock_irq(&trident->reg_lock);
  866. return 0;
  867. }
  868. /*---------------------------------------------------------------------------
  869. snd_trident_si7018_capture_hw_params
  870. Description: Set the hardware parameters for the capture device.
  871. Parameters: substream - PCM substream class
  872. hw_params - hardware parameters
  873. Returns: Error status
  874. ---------------------------------------------------------------------------*/
  875. static int snd_trident_si7018_capture_hw_params(struct snd_pcm_substream *substream,
  876. struct snd_pcm_hw_params *hw_params)
  877. {
  878. return snd_trident_allocate_evoice(substream, hw_params);
  879. }
  880. /*---------------------------------------------------------------------------
  881. snd_trident_si7018_capture_hw_free
  882. Description: Release the hardware resources for the capture device.
  883. Parameters: substream - PCM substream class
  884. Returns: Error status
  885. ---------------------------------------------------------------------------*/
  886. static int snd_trident_si7018_capture_hw_free(struct snd_pcm_substream *substream)
  887. {
  888. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  889. struct snd_pcm_runtime *runtime = substream->runtime;
  890. struct snd_trident_voice *voice = runtime->private_data;
  891. struct snd_trident_voice *evoice = voice ? voice->extra : NULL;
  892. if (evoice != NULL) {
  893. snd_trident_free_voice(trident, evoice);
  894. voice->extra = NULL;
  895. }
  896. return 0;
  897. }
  898. /*---------------------------------------------------------------------------
  899. snd_trident_si7018_capture_prepare
  900. Description: Prepare capture device for playback.
  901. Parameters: substream - PCM substream class
  902. Returns: Error status
  903. ---------------------------------------------------------------------------*/
  904. static int snd_trident_si7018_capture_prepare(struct snd_pcm_substream *substream)
  905. {
  906. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  907. struct snd_pcm_runtime *runtime = substream->runtime;
  908. struct snd_trident_voice *voice = runtime->private_data;
  909. struct snd_trident_voice *evoice = voice->extra;
  910. spin_lock_irq(&trident->reg_lock);
  911. voice->LBA = runtime->dma_addr;
  912. voice->Delta = snd_trident_convert_adc_rate(runtime->rate);
  913. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  914. // Set voice parameters
  915. voice->CSO = 0;
  916. voice->ESO = runtime->buffer_size - 1; /* in samples */
  917. voice->CTRL = snd_trident_control_mode(substream);
  918. voice->FMC = 0;
  919. voice->RVol = 0;
  920. voice->CVol = 0;
  921. voice->GVSel = 1;
  922. voice->Pan = T4D_DEFAULT_PCM_PAN;
  923. voice->Vol = 0;
  924. voice->EC = 0;
  925. voice->Alpha = 0;
  926. voice->FMS = 0;
  927. voice->Attribute = (2 << (30-16)) |
  928. (2 << (26-16)) |
  929. (2 << (24-16)) |
  930. (1 << (23-16));
  931. snd_trident_write_voice_regs(trident, voice);
  932. if (evoice != NULL) {
  933. evoice->Delta = snd_trident_convert_rate(runtime->rate);
  934. evoice->spurious_threshold = voice->spurious_threshold;
  935. evoice->LBA = voice->LBA;
  936. evoice->CSO = 0;
  937. evoice->ESO = (runtime->period_size * 2) + 20 - 1; /* in samples, 20 means correction */
  938. evoice->CTRL = voice->CTRL;
  939. evoice->FMC = 3;
  940. evoice->GVSel = 0;
  941. evoice->EC = 0;
  942. evoice->Alpha = 0;
  943. evoice->FMS = 0;
  944. evoice->Vol = 0x3ff; /* mute */
  945. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  946. evoice->Pan = 0x7f; /* mute */
  947. evoice->Attribute = 0;
  948. snd_trident_write_voice_regs(trident, evoice);
  949. evoice->isync2 = 1;
  950. evoice->isync_mark = runtime->period_size;
  951. evoice->ESO = (runtime->period_size * 2) - 1;
  952. }
  953. spin_unlock_irq(&trident->reg_lock);
  954. return 0;
  955. }
  956. /*---------------------------------------------------------------------------
  957. snd_trident_foldback_prepare
  958. Description: Prepare foldback capture device for playback.
  959. Parameters: substream - PCM substream class
  960. Returns: Error status
  961. ---------------------------------------------------------------------------*/
  962. static int snd_trident_foldback_prepare(struct snd_pcm_substream *substream)
  963. {
  964. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  965. struct snd_pcm_runtime *runtime = substream->runtime;
  966. struct snd_trident_voice *voice = runtime->private_data;
  967. struct snd_trident_voice *evoice = voice->extra;
  968. spin_lock_irq(&trident->reg_lock);
  969. /* Set channel buffer Address */
  970. if (voice->memblk)
  971. voice->LBA = voice->memblk->offset;
  972. else
  973. voice->LBA = runtime->dma_addr;
  974. /* set target ESO for channel */
  975. voice->ESO = runtime->buffer_size - 1; /* in samples */
  976. /* set sample rate */
  977. voice->Delta = 0x1000;
  978. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  979. voice->CSO = 0;
  980. voice->CTRL = snd_trident_control_mode(substream);
  981. voice->FMC = 3;
  982. voice->RVol = 0x7f;
  983. voice->CVol = 0x7f;
  984. voice->GVSel = 1;
  985. voice->Pan = 0x7f; /* mute */
  986. voice->Vol = 0x3ff; /* mute */
  987. voice->EC = 0;
  988. voice->Alpha = 0;
  989. voice->FMS = 0;
  990. voice->Attribute = 0;
  991. /* set up capture channel */
  992. outb(((voice->number & 0x3f) | 0x80), TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  993. snd_trident_write_voice_regs(trident, voice);
  994. if (evoice != NULL) {
  995. evoice->Delta = voice->Delta;
  996. evoice->spurious_threshold = voice->spurious_threshold;
  997. evoice->LBA = voice->LBA;
  998. evoice->CSO = 0;
  999. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  1000. evoice->CTRL = voice->CTRL;
  1001. evoice->FMC = 3;
  1002. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1003. evoice->EC = 0;
  1004. evoice->Alpha = 0;
  1005. evoice->FMS = 0;
  1006. evoice->Vol = 0x3ff; /* mute */
  1007. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1008. evoice->Pan = 0x7f; /* mute */
  1009. evoice->Attribute = 0;
  1010. snd_trident_write_voice_regs(trident, evoice);
  1011. evoice->isync2 = 1;
  1012. evoice->isync_mark = runtime->period_size;
  1013. evoice->ESO = (runtime->period_size * 2) - 1;
  1014. }
  1015. spin_unlock_irq(&trident->reg_lock);
  1016. return 0;
  1017. }
  1018. /*---------------------------------------------------------------------------
  1019. snd_trident_spdif_hw_params
  1020. Description: Set the hardware parameters for the spdif device.
  1021. Parameters: substream - PCM substream class
  1022. hw_params - hardware parameters
  1023. Returns: Error status
  1024. ---------------------------------------------------------------------------*/
  1025. static int snd_trident_spdif_hw_params(struct snd_pcm_substream *substream,
  1026. struct snd_pcm_hw_params *hw_params)
  1027. {
  1028. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1029. unsigned int old_bits = 0, change = 0;
  1030. int err;
  1031. err = snd_trident_allocate_pcm_mem(substream, hw_params);
  1032. if (err < 0)
  1033. return err;
  1034. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1035. err = snd_trident_allocate_evoice(substream, hw_params);
  1036. if (err < 0)
  1037. return err;
  1038. }
  1039. /* prepare SPDIF channel */
  1040. spin_lock_irq(&trident->reg_lock);
  1041. old_bits = trident->spdif_pcm_bits;
  1042. if (old_bits & IEC958_AES0_PROFESSIONAL)
  1043. trident->spdif_pcm_bits &= ~IEC958_AES0_PRO_FS;
  1044. else
  1045. trident->spdif_pcm_bits &= ~(IEC958_AES3_CON_FS << 24);
  1046. if (params_rate(hw_params) >= 48000) {
  1047. trident->spdif_pcm_ctrl = 0x3c; // 48000 Hz
  1048. trident->spdif_pcm_bits |=
  1049. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1050. IEC958_AES0_PRO_FS_48000 :
  1051. (IEC958_AES3_CON_FS_48000 << 24);
  1052. }
  1053. else if (params_rate(hw_params) >= 44100) {
  1054. trident->spdif_pcm_ctrl = 0x3e; // 44100 Hz
  1055. trident->spdif_pcm_bits |=
  1056. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1057. IEC958_AES0_PRO_FS_44100 :
  1058. (IEC958_AES3_CON_FS_44100 << 24);
  1059. }
  1060. else {
  1061. trident->spdif_pcm_ctrl = 0x3d; // 32000 Hz
  1062. trident->spdif_pcm_bits |=
  1063. trident->spdif_bits & IEC958_AES0_PROFESSIONAL ?
  1064. IEC958_AES0_PRO_FS_32000 :
  1065. (IEC958_AES3_CON_FS_32000 << 24);
  1066. }
  1067. change = old_bits != trident->spdif_pcm_bits;
  1068. spin_unlock_irq(&trident->reg_lock);
  1069. if (change)
  1070. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE, &trident->spdif_pcm_ctl->id);
  1071. return 0;
  1072. }
  1073. /*---------------------------------------------------------------------------
  1074. snd_trident_spdif_prepare
  1075. Description: Prepare SPDIF device for playback.
  1076. Parameters: substream - PCM substream class
  1077. Returns: Error status
  1078. ---------------------------------------------------------------------------*/
  1079. static int snd_trident_spdif_prepare(struct snd_pcm_substream *substream)
  1080. {
  1081. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1082. struct snd_pcm_runtime *runtime = substream->runtime;
  1083. struct snd_trident_voice *voice = runtime->private_data;
  1084. struct snd_trident_voice *evoice = voice->extra;
  1085. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[substream->number];
  1086. unsigned int RESO, LBAO;
  1087. unsigned int temp;
  1088. spin_lock_irq(&trident->reg_lock);
  1089. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1090. /* set delta (rate) value */
  1091. voice->Delta = snd_trident_convert_rate(runtime->rate);
  1092. voice->spurious_threshold = snd_trident_spurious_threshold(runtime->rate, runtime->period_size);
  1093. /* set Loop Back Address */
  1094. LBAO = runtime->dma_addr;
  1095. if (voice->memblk)
  1096. voice->LBA = voice->memblk->offset;
  1097. else
  1098. voice->LBA = LBAO;
  1099. voice->isync = 1;
  1100. voice->isync3 = 1;
  1101. voice->isync_mark = runtime->period_size;
  1102. voice->isync_max = runtime->buffer_size;
  1103. /* set target ESO for channel */
  1104. RESO = runtime->buffer_size - 1;
  1105. voice->ESO = voice->isync_ESO = (runtime->period_size * 2) + 6 - 1;
  1106. /* set ctrl mode */
  1107. voice->CTRL = snd_trident_control_mode(substream);
  1108. voice->FMC = 3;
  1109. voice->RVol = 0x7f;
  1110. voice->CVol = 0x7f;
  1111. voice->GVSel = 1;
  1112. voice->Pan = 0x7f;
  1113. voice->Vol = 0x3ff;
  1114. voice->EC = 0;
  1115. voice->CSO = 0;
  1116. voice->Alpha = 0;
  1117. voice->FMS = 0;
  1118. voice->Attribute = 0;
  1119. /* prepare surrogate IRQ channel */
  1120. snd_trident_write_voice_regs(trident, voice);
  1121. outw((RESO & 0xffff), TRID_REG(trident, NX_SPESO));
  1122. outb((RESO >> 16), TRID_REG(trident, NX_SPESO + 2));
  1123. outl((LBAO & 0xfffffffc), TRID_REG(trident, NX_SPLBA));
  1124. outw((voice->CSO & 0xffff), TRID_REG(trident, NX_SPCTRL_SPCSO));
  1125. outb((voice->CSO >> 16), TRID_REG(trident, NX_SPCTRL_SPCSO + 2));
  1126. /* set SPDIF setting */
  1127. outb(trident->spdif_pcm_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1128. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1129. } else { /* SiS */
  1130. /* set delta (rate) value */
  1131. voice->Delta = 0x800;
  1132. voice->spurious_threshold = snd_trident_spurious_threshold(48000, runtime->period_size);
  1133. /* set Loop Begin Address */
  1134. if (voice->memblk)
  1135. voice->LBA = voice->memblk->offset;
  1136. else
  1137. voice->LBA = runtime->dma_addr;
  1138. voice->CSO = 0;
  1139. voice->ESO = runtime->buffer_size - 1; /* in samples */
  1140. voice->CTRL = snd_trident_control_mode(substream);
  1141. voice->FMC = 3;
  1142. voice->GVSel = 1;
  1143. voice->EC = 0;
  1144. voice->Alpha = 0;
  1145. voice->FMS = 0;
  1146. voice->Vol = mix->vol;
  1147. voice->RVol = mix->rvol;
  1148. voice->CVol = mix->cvol;
  1149. voice->Pan = mix->pan;
  1150. voice->Attribute = (1<<(30-16))|(7<<(26-16))|
  1151. (0<<(24-16))|(0<<(19-16));
  1152. snd_trident_write_voice_regs(trident, voice);
  1153. if (evoice != NULL) {
  1154. evoice->Delta = voice->Delta;
  1155. evoice->spurious_threshold = voice->spurious_threshold;
  1156. evoice->LBA = voice->LBA;
  1157. evoice->CSO = 0;
  1158. evoice->ESO = (runtime->period_size * 2) + 4 - 1; /* in samples */
  1159. evoice->CTRL = voice->CTRL;
  1160. evoice->FMC = 3;
  1161. evoice->GVSel = trident->device == TRIDENT_DEVICE_ID_SI7018 ? 0 : 1;
  1162. evoice->EC = 0;
  1163. evoice->Alpha = 0;
  1164. evoice->FMS = 0;
  1165. evoice->Vol = 0x3ff; /* mute */
  1166. evoice->RVol = evoice->CVol = 0x7f; /* mute */
  1167. evoice->Pan = 0x7f; /* mute */
  1168. evoice->Attribute = 0;
  1169. snd_trident_write_voice_regs(trident, evoice);
  1170. evoice->isync2 = 1;
  1171. evoice->isync_mark = runtime->period_size;
  1172. evoice->ESO = (runtime->period_size * 2) - 1;
  1173. }
  1174. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1175. temp = inl(TRID_REG(trident, T4D_LFO_GC_CIR));
  1176. temp &= ~(1<<19);
  1177. outl(temp, TRID_REG(trident, T4D_LFO_GC_CIR));
  1178. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1179. temp |= SPDIF_EN;
  1180. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1181. }
  1182. spin_unlock_irq(&trident->reg_lock);
  1183. return 0;
  1184. }
  1185. /*---------------------------------------------------------------------------
  1186. snd_trident_trigger
  1187. Description: Start/stop devices
  1188. Parameters: substream - PCM substream class
  1189. cmd - trigger command (STOP, GO)
  1190. Returns: Error status
  1191. ---------------------------------------------------------------------------*/
  1192. static int snd_trident_trigger(struct snd_pcm_substream *substream,
  1193. int cmd)
  1194. {
  1195. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1196. struct snd_pcm_substream *s;
  1197. unsigned int what, whati, capture_flag, spdif_flag;
  1198. struct snd_trident_voice *voice, *evoice;
  1199. unsigned int val, go;
  1200. switch (cmd) {
  1201. case SNDRV_PCM_TRIGGER_START:
  1202. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  1203. case SNDRV_PCM_TRIGGER_RESUME:
  1204. go = 1;
  1205. break;
  1206. case SNDRV_PCM_TRIGGER_STOP:
  1207. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  1208. case SNDRV_PCM_TRIGGER_SUSPEND:
  1209. go = 0;
  1210. break;
  1211. default:
  1212. return -EINVAL;
  1213. }
  1214. what = whati = capture_flag = spdif_flag = 0;
  1215. spin_lock(&trident->reg_lock);
  1216. val = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  1217. snd_pcm_group_for_each_entry(s, substream) {
  1218. if ((struct snd_trident *) snd_pcm_substream_chip(s) == trident) {
  1219. voice = s->runtime->private_data;
  1220. evoice = voice->extra;
  1221. what |= 1 << (voice->number & 0x1f);
  1222. if (evoice == NULL) {
  1223. whati |= 1 << (voice->number & 0x1f);
  1224. } else {
  1225. what |= 1 << (evoice->number & 0x1f);
  1226. whati |= 1 << (evoice->number & 0x1f);
  1227. if (go)
  1228. evoice->stimer = val;
  1229. }
  1230. if (go) {
  1231. voice->running = 1;
  1232. voice->stimer = val;
  1233. } else {
  1234. voice->running = 0;
  1235. }
  1236. snd_pcm_trigger_done(s, substream);
  1237. if (voice->capture)
  1238. capture_flag = 1;
  1239. if (voice->spdif)
  1240. spdif_flag = 1;
  1241. }
  1242. }
  1243. if (spdif_flag) {
  1244. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1245. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1246. val = trident->spdif_pcm_ctrl;
  1247. if (!go)
  1248. val &= ~(0x28);
  1249. outb(val, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1250. } else {
  1251. outl(trident->spdif_pcm_bits, TRID_REG(trident, SI_SPDIF_CS));
  1252. val = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) | SPDIF_EN;
  1253. outl(val, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1254. }
  1255. }
  1256. if (!go)
  1257. outl(what, TRID_REG(trident, T4D_STOP_B));
  1258. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  1259. if (go) {
  1260. val |= whati;
  1261. } else {
  1262. val &= ~whati;
  1263. }
  1264. outl(val, TRID_REG(trident, T4D_AINTEN_B));
  1265. if (go) {
  1266. outl(what, TRID_REG(trident, T4D_START_B));
  1267. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1268. outb(trident->bDMAStart, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1269. } else {
  1270. if (capture_flag && trident->device != TRIDENT_DEVICE_ID_SI7018)
  1271. outb(0x00, TRID_REG(trident, T4D_SBCTRL_SBE2R_SBDD));
  1272. }
  1273. spin_unlock(&trident->reg_lock);
  1274. return 0;
  1275. }
  1276. /*---------------------------------------------------------------------------
  1277. snd_trident_playback_pointer
  1278. Description: This routine return the playback position
  1279. Parameters: substream - PCM substream class
  1280. Returns: position of buffer
  1281. ---------------------------------------------------------------------------*/
  1282. static snd_pcm_uframes_t snd_trident_playback_pointer(struct snd_pcm_substream *substream)
  1283. {
  1284. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1285. struct snd_pcm_runtime *runtime = substream->runtime;
  1286. struct snd_trident_voice *voice = runtime->private_data;
  1287. unsigned int cso;
  1288. if (!voice->running)
  1289. return 0;
  1290. spin_lock(&trident->reg_lock);
  1291. outb(voice->number, TRID_REG(trident, T4D_LFO_GC_CIR));
  1292. if (trident->device != TRIDENT_DEVICE_ID_NX) {
  1293. cso = inw(TRID_REG(trident, CH_DX_CSO_ALPHA_FMS + 2));
  1294. } else { // ID_4DWAVE_NX
  1295. cso = (unsigned int) inl(TRID_REG(trident, CH_NX_DELTA_CSO)) & 0x00ffffff;
  1296. }
  1297. spin_unlock(&trident->reg_lock);
  1298. if (cso >= runtime->buffer_size)
  1299. cso = 0;
  1300. return cso;
  1301. }
  1302. /*---------------------------------------------------------------------------
  1303. snd_trident_capture_pointer
  1304. Description: This routine return the capture position
  1305. Parameters: pcm1 - PCM device class
  1306. Returns: position of buffer
  1307. ---------------------------------------------------------------------------*/
  1308. static snd_pcm_uframes_t snd_trident_capture_pointer(struct snd_pcm_substream *substream)
  1309. {
  1310. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1311. struct snd_pcm_runtime *runtime = substream->runtime;
  1312. struct snd_trident_voice *voice = runtime->private_data;
  1313. unsigned int result;
  1314. if (!voice->running)
  1315. return 0;
  1316. result = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  1317. if (runtime->channels > 1)
  1318. result >>= 1;
  1319. if (result > 0)
  1320. result = runtime->buffer_size - result;
  1321. return result;
  1322. }
  1323. /*---------------------------------------------------------------------------
  1324. snd_trident_spdif_pointer
  1325. Description: This routine return the SPDIF playback position
  1326. Parameters: substream - PCM substream class
  1327. Returns: position of buffer
  1328. ---------------------------------------------------------------------------*/
  1329. static snd_pcm_uframes_t snd_trident_spdif_pointer(struct snd_pcm_substream *substream)
  1330. {
  1331. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1332. struct snd_pcm_runtime *runtime = substream->runtime;
  1333. struct snd_trident_voice *voice = runtime->private_data;
  1334. unsigned int result;
  1335. if (!voice->running)
  1336. return 0;
  1337. result = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  1338. return result;
  1339. }
  1340. /*
  1341. * Playback support device description
  1342. */
  1343. static const struct snd_pcm_hardware snd_trident_playback =
  1344. {
  1345. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1346. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1347. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1348. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1349. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1350. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1351. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1352. .rate_min = 4000,
  1353. .rate_max = 48000,
  1354. .channels_min = 1,
  1355. .channels_max = 2,
  1356. .buffer_bytes_max = (256*1024),
  1357. .period_bytes_min = 64,
  1358. .period_bytes_max = (256*1024),
  1359. .periods_min = 1,
  1360. .periods_max = 1024,
  1361. .fifo_size = 0,
  1362. };
  1363. /*
  1364. * Capture support device description
  1365. */
  1366. static const struct snd_pcm_hardware snd_trident_capture =
  1367. {
  1368. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1369. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1370. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1371. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1372. .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE |
  1373. SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE),
  1374. .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  1375. .rate_min = 4000,
  1376. .rate_max = 48000,
  1377. .channels_min = 1,
  1378. .channels_max = 2,
  1379. .buffer_bytes_max = (128*1024),
  1380. .period_bytes_min = 64,
  1381. .period_bytes_max = (128*1024),
  1382. .periods_min = 1,
  1383. .periods_max = 1024,
  1384. .fifo_size = 0,
  1385. };
  1386. /*
  1387. * Foldback capture support device description
  1388. */
  1389. static const struct snd_pcm_hardware snd_trident_foldback =
  1390. {
  1391. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1392. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1393. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1394. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1395. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1396. .rates = SNDRV_PCM_RATE_48000,
  1397. .rate_min = 48000,
  1398. .rate_max = 48000,
  1399. .channels_min = 2,
  1400. .channels_max = 2,
  1401. .buffer_bytes_max = (128*1024),
  1402. .period_bytes_min = 64,
  1403. .period_bytes_max = (128*1024),
  1404. .periods_min = 1,
  1405. .periods_max = 1024,
  1406. .fifo_size = 0,
  1407. };
  1408. /*
  1409. * SPDIF playback support device description
  1410. */
  1411. static const struct snd_pcm_hardware snd_trident_spdif =
  1412. {
  1413. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1414. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1415. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1416. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1417. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1418. .rates = (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  1419. SNDRV_PCM_RATE_48000),
  1420. .rate_min = 32000,
  1421. .rate_max = 48000,
  1422. .channels_min = 2,
  1423. .channels_max = 2,
  1424. .buffer_bytes_max = (128*1024),
  1425. .period_bytes_min = 64,
  1426. .period_bytes_max = (128*1024),
  1427. .periods_min = 1,
  1428. .periods_max = 1024,
  1429. .fifo_size = 0,
  1430. };
  1431. static const struct snd_pcm_hardware snd_trident_spdif_7018 =
  1432. {
  1433. .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  1434. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  1435. SNDRV_PCM_INFO_MMAP_VALID | SNDRV_PCM_INFO_SYNC_START |
  1436. SNDRV_PCM_INFO_PAUSE /* | SNDRV_PCM_INFO_RESUME */),
  1437. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1438. .rates = SNDRV_PCM_RATE_48000,
  1439. .rate_min = 48000,
  1440. .rate_max = 48000,
  1441. .channels_min = 2,
  1442. .channels_max = 2,
  1443. .buffer_bytes_max = (128*1024),
  1444. .period_bytes_min = 64,
  1445. .period_bytes_max = (128*1024),
  1446. .periods_min = 1,
  1447. .periods_max = 1024,
  1448. .fifo_size = 0,
  1449. };
  1450. static void snd_trident_pcm_free_substream(struct snd_pcm_runtime *runtime)
  1451. {
  1452. struct snd_trident_voice *voice = runtime->private_data;
  1453. struct snd_trident *trident;
  1454. if (voice) {
  1455. trident = voice->trident;
  1456. snd_trident_free_voice(trident, voice);
  1457. }
  1458. }
  1459. static int snd_trident_playback_open(struct snd_pcm_substream *substream)
  1460. {
  1461. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1462. struct snd_pcm_runtime *runtime = substream->runtime;
  1463. struct snd_trident_voice *voice;
  1464. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1465. if (voice == NULL)
  1466. return -EAGAIN;
  1467. snd_trident_pcm_mixer_build(trident, voice, substream);
  1468. voice->substream = substream;
  1469. runtime->private_data = voice;
  1470. runtime->private_free = snd_trident_pcm_free_substream;
  1471. runtime->hw = snd_trident_playback;
  1472. snd_pcm_set_sync(substream);
  1473. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1474. return 0;
  1475. }
  1476. /*---------------------------------------------------------------------------
  1477. snd_trident_playback_close
  1478. Description: This routine will close the 4DWave playback device. For now
  1479. we will simply free the dma transfer buffer.
  1480. Parameters: substream - PCM substream class
  1481. ---------------------------------------------------------------------------*/
  1482. static int snd_trident_playback_close(struct snd_pcm_substream *substream)
  1483. {
  1484. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1485. struct snd_pcm_runtime *runtime = substream->runtime;
  1486. struct snd_trident_voice *voice = runtime->private_data;
  1487. snd_trident_pcm_mixer_free(trident, voice, substream);
  1488. return 0;
  1489. }
  1490. /*---------------------------------------------------------------------------
  1491. snd_trident_spdif_open
  1492. Description: This routine will open the 4DWave SPDIF device.
  1493. Parameters: substream - PCM substream class
  1494. Returns: status - success or failure flag
  1495. ---------------------------------------------------------------------------*/
  1496. static int snd_trident_spdif_open(struct snd_pcm_substream *substream)
  1497. {
  1498. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1499. struct snd_trident_voice *voice;
  1500. struct snd_pcm_runtime *runtime = substream->runtime;
  1501. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1502. if (voice == NULL)
  1503. return -EAGAIN;
  1504. voice->spdif = 1;
  1505. voice->substream = substream;
  1506. spin_lock_irq(&trident->reg_lock);
  1507. trident->spdif_pcm_bits = trident->spdif_bits;
  1508. spin_unlock_irq(&trident->reg_lock);
  1509. runtime->private_data = voice;
  1510. runtime->private_free = snd_trident_pcm_free_substream;
  1511. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  1512. runtime->hw = snd_trident_spdif;
  1513. } else {
  1514. runtime->hw = snd_trident_spdif_7018;
  1515. }
  1516. trident->spdif_pcm_ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1517. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1518. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1519. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1520. return 0;
  1521. }
  1522. /*---------------------------------------------------------------------------
  1523. snd_trident_spdif_close
  1524. Description: This routine will close the 4DWave SPDIF device.
  1525. Parameters: substream - PCM substream class
  1526. ---------------------------------------------------------------------------*/
  1527. static int snd_trident_spdif_close(struct snd_pcm_substream *substream)
  1528. {
  1529. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1530. unsigned int temp;
  1531. spin_lock_irq(&trident->reg_lock);
  1532. // restore default SPDIF setting
  1533. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1534. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1535. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1536. } else {
  1537. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1538. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1539. if (trident->spdif_ctrl) {
  1540. temp |= SPDIF_EN;
  1541. } else {
  1542. temp &= ~SPDIF_EN;
  1543. }
  1544. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1545. }
  1546. spin_unlock_irq(&trident->reg_lock);
  1547. trident->spdif_pcm_ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  1548. snd_ctl_notify(trident->card, SNDRV_CTL_EVENT_MASK_VALUE |
  1549. SNDRV_CTL_EVENT_MASK_INFO, &trident->spdif_pcm_ctl->id);
  1550. return 0;
  1551. }
  1552. /*---------------------------------------------------------------------------
  1553. snd_trident_capture_open
  1554. Description: This routine will open the 4DWave capture device.
  1555. Parameters: substream - PCM substream class
  1556. Returns: status - success or failure flag
  1557. ---------------------------------------------------------------------------*/
  1558. static int snd_trident_capture_open(struct snd_pcm_substream *substream)
  1559. {
  1560. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1561. struct snd_trident_voice *voice;
  1562. struct snd_pcm_runtime *runtime = substream->runtime;
  1563. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1564. if (voice == NULL)
  1565. return -EAGAIN;
  1566. voice->capture = 1;
  1567. voice->substream = substream;
  1568. runtime->private_data = voice;
  1569. runtime->private_free = snd_trident_pcm_free_substream;
  1570. runtime->hw = snd_trident_capture;
  1571. snd_pcm_set_sync(substream);
  1572. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1573. return 0;
  1574. }
  1575. /*---------------------------------------------------------------------------
  1576. snd_trident_capture_close
  1577. Description: This routine will close the 4DWave capture device. For now
  1578. we will simply free the dma transfer buffer.
  1579. Parameters: substream - PCM substream class
  1580. ---------------------------------------------------------------------------*/
  1581. static int snd_trident_capture_close(struct snd_pcm_substream *substream)
  1582. {
  1583. return 0;
  1584. }
  1585. /*---------------------------------------------------------------------------
  1586. snd_trident_foldback_open
  1587. Description: This routine will open the 4DWave foldback capture device.
  1588. Parameters: substream - PCM substream class
  1589. Returns: status - success or failure flag
  1590. ---------------------------------------------------------------------------*/
  1591. static int snd_trident_foldback_open(struct snd_pcm_substream *substream)
  1592. {
  1593. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1594. struct snd_trident_voice *voice;
  1595. struct snd_pcm_runtime *runtime = substream->runtime;
  1596. voice = snd_trident_alloc_voice(trident, SNDRV_TRIDENT_VOICE_TYPE_PCM, 0, 0);
  1597. if (voice == NULL)
  1598. return -EAGAIN;
  1599. voice->foldback_chan = substream->number;
  1600. voice->substream = substream;
  1601. runtime->private_data = voice;
  1602. runtime->private_free = snd_trident_pcm_free_substream;
  1603. runtime->hw = snd_trident_foldback;
  1604. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_SIZE, 0, 64*1024);
  1605. return 0;
  1606. }
  1607. /*---------------------------------------------------------------------------
  1608. snd_trident_foldback_close
  1609. Description: This routine will close the 4DWave foldback capture device.
  1610. For now we will simply free the dma transfer buffer.
  1611. Parameters: substream - PCM substream class
  1612. ---------------------------------------------------------------------------*/
  1613. static int snd_trident_foldback_close(struct snd_pcm_substream *substream)
  1614. {
  1615. struct snd_trident *trident = snd_pcm_substream_chip(substream);
  1616. struct snd_trident_voice *voice;
  1617. struct snd_pcm_runtime *runtime = substream->runtime;
  1618. voice = runtime->private_data;
  1619. /* stop capture channel */
  1620. spin_lock_irq(&trident->reg_lock);
  1621. outb(0x00, TRID_REG(trident, T4D_RCI + voice->foldback_chan));
  1622. spin_unlock_irq(&trident->reg_lock);
  1623. return 0;
  1624. }
  1625. /*---------------------------------------------------------------------------
  1626. PCM operations
  1627. ---------------------------------------------------------------------------*/
  1628. static const struct snd_pcm_ops snd_trident_playback_ops = {
  1629. .open = snd_trident_playback_open,
  1630. .close = snd_trident_playback_close,
  1631. .hw_params = snd_trident_hw_params,
  1632. .hw_free = snd_trident_hw_free,
  1633. .prepare = snd_trident_playback_prepare,
  1634. .trigger = snd_trident_trigger,
  1635. .pointer = snd_trident_playback_pointer,
  1636. };
  1637. static const struct snd_pcm_ops snd_trident_nx_playback_ops = {
  1638. .open = snd_trident_playback_open,
  1639. .close = snd_trident_playback_close,
  1640. .hw_params = snd_trident_hw_params,
  1641. .hw_free = snd_trident_hw_free,
  1642. .prepare = snd_trident_playback_prepare,
  1643. .trigger = snd_trident_trigger,
  1644. .pointer = snd_trident_playback_pointer,
  1645. };
  1646. static const struct snd_pcm_ops snd_trident_capture_ops = {
  1647. .open = snd_trident_capture_open,
  1648. .close = snd_trident_capture_close,
  1649. .hw_params = snd_trident_capture_hw_params,
  1650. .hw_free = snd_trident_hw_free,
  1651. .prepare = snd_trident_capture_prepare,
  1652. .trigger = snd_trident_trigger,
  1653. .pointer = snd_trident_capture_pointer,
  1654. };
  1655. static const struct snd_pcm_ops snd_trident_si7018_capture_ops = {
  1656. .open = snd_trident_capture_open,
  1657. .close = snd_trident_capture_close,
  1658. .hw_params = snd_trident_si7018_capture_hw_params,
  1659. .hw_free = snd_trident_si7018_capture_hw_free,
  1660. .prepare = snd_trident_si7018_capture_prepare,
  1661. .trigger = snd_trident_trigger,
  1662. .pointer = snd_trident_playback_pointer,
  1663. };
  1664. static const struct snd_pcm_ops snd_trident_foldback_ops = {
  1665. .open = snd_trident_foldback_open,
  1666. .close = snd_trident_foldback_close,
  1667. .hw_params = snd_trident_hw_params,
  1668. .hw_free = snd_trident_hw_free,
  1669. .prepare = snd_trident_foldback_prepare,
  1670. .trigger = snd_trident_trigger,
  1671. .pointer = snd_trident_playback_pointer,
  1672. };
  1673. static const struct snd_pcm_ops snd_trident_nx_foldback_ops = {
  1674. .open = snd_trident_foldback_open,
  1675. .close = snd_trident_foldback_close,
  1676. .hw_params = snd_trident_hw_params,
  1677. .hw_free = snd_trident_hw_free,
  1678. .prepare = snd_trident_foldback_prepare,
  1679. .trigger = snd_trident_trigger,
  1680. .pointer = snd_trident_playback_pointer,
  1681. };
  1682. static const struct snd_pcm_ops snd_trident_spdif_ops = {
  1683. .open = snd_trident_spdif_open,
  1684. .close = snd_trident_spdif_close,
  1685. .hw_params = snd_trident_spdif_hw_params,
  1686. .hw_free = snd_trident_hw_free,
  1687. .prepare = snd_trident_spdif_prepare,
  1688. .trigger = snd_trident_trigger,
  1689. .pointer = snd_trident_spdif_pointer,
  1690. };
  1691. static const struct snd_pcm_ops snd_trident_spdif_7018_ops = {
  1692. .open = snd_trident_spdif_open,
  1693. .close = snd_trident_spdif_close,
  1694. .hw_params = snd_trident_spdif_hw_params,
  1695. .hw_free = snd_trident_hw_free,
  1696. .prepare = snd_trident_spdif_prepare,
  1697. .trigger = snd_trident_trigger,
  1698. .pointer = snd_trident_playback_pointer,
  1699. };
  1700. /*---------------------------------------------------------------------------
  1701. snd_trident_pcm
  1702. Description: This routine registers the 4DWave device for PCM support.
  1703. Parameters: trident - pointer to target device class for 4DWave.
  1704. Returns: None
  1705. ---------------------------------------------------------------------------*/
  1706. int snd_trident_pcm(struct snd_trident *trident, int device)
  1707. {
  1708. struct snd_pcm *pcm;
  1709. int err;
  1710. err = snd_pcm_new(trident->card, "trident_dx_nx", device, trident->ChanPCM, 1, &pcm);
  1711. if (err < 0)
  1712. return err;
  1713. pcm->private_data = trident;
  1714. if (trident->tlb.entries) {
  1715. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_nx_playback_ops);
  1716. } else {
  1717. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_playback_ops);
  1718. }
  1719. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE,
  1720. trident->device != TRIDENT_DEVICE_ID_SI7018 ?
  1721. &snd_trident_capture_ops :
  1722. &snd_trident_si7018_capture_ops);
  1723. pcm->info_flags = 0;
  1724. pcm->dev_subclass = SNDRV_PCM_SUBCLASS_GENERIC_MIX;
  1725. strcpy(pcm->name, "Trident 4DWave");
  1726. trident->pcm = pcm;
  1727. if (trident->tlb.entries) {
  1728. struct snd_pcm_substream *substream;
  1729. for (substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; substream; substream = substream->next)
  1730. snd_pcm_set_managed_buffer(substream, SNDRV_DMA_TYPE_DEV_SG,
  1731. &trident->pci->dev,
  1732. 64*1024, 128*1024);
  1733. snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream,
  1734. SNDRV_DMA_TYPE_DEV,
  1735. &trident->pci->dev,
  1736. 64*1024, 128*1024);
  1737. } else {
  1738. snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
  1739. &trident->pci->dev,
  1740. 64*1024, 128*1024);
  1741. }
  1742. return 0;
  1743. }
  1744. /*---------------------------------------------------------------------------
  1745. snd_trident_foldback_pcm
  1746. Description: This routine registers the 4DWave device for foldback PCM support.
  1747. Parameters: trident - pointer to target device class for 4DWave.
  1748. Returns: None
  1749. ---------------------------------------------------------------------------*/
  1750. int snd_trident_foldback_pcm(struct snd_trident *trident, int device)
  1751. {
  1752. struct snd_pcm *foldback;
  1753. int err;
  1754. int num_chan = 3;
  1755. struct snd_pcm_substream *substream;
  1756. if (trident->device == TRIDENT_DEVICE_ID_NX)
  1757. num_chan = 4;
  1758. err = snd_pcm_new(trident->card, "trident_dx_nx", device, 0, num_chan, &foldback);
  1759. if (err < 0)
  1760. return err;
  1761. foldback->private_data = trident;
  1762. if (trident->tlb.entries)
  1763. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_nx_foldback_ops);
  1764. else
  1765. snd_pcm_set_ops(foldback, SNDRV_PCM_STREAM_CAPTURE, &snd_trident_foldback_ops);
  1766. foldback->info_flags = 0;
  1767. strcpy(foldback->name, "Trident 4DWave");
  1768. substream = foldback->streams[SNDRV_PCM_STREAM_CAPTURE].substream;
  1769. strcpy(substream->name, "Front Mixer");
  1770. substream = substream->next;
  1771. strcpy(substream->name, "Reverb Mixer");
  1772. substream = substream->next;
  1773. strcpy(substream->name, "Chorus Mixer");
  1774. if (num_chan == 4) {
  1775. substream = substream->next;
  1776. strcpy(substream->name, "Second AC'97 ADC");
  1777. }
  1778. trident->foldback = foldback;
  1779. if (trident->tlb.entries)
  1780. snd_pcm_set_managed_buffer_all(foldback, SNDRV_DMA_TYPE_DEV_SG,
  1781. &trident->pci->dev,
  1782. 0, 128*1024);
  1783. else
  1784. snd_pcm_set_managed_buffer_all(foldback, SNDRV_DMA_TYPE_DEV,
  1785. &trident->pci->dev,
  1786. 64*1024, 128*1024);
  1787. return 0;
  1788. }
  1789. /*---------------------------------------------------------------------------
  1790. snd_trident_spdif
  1791. Description: This routine registers the 4DWave-NX device for SPDIF support.
  1792. Parameters: trident - pointer to target device class for 4DWave-NX.
  1793. Returns: None
  1794. ---------------------------------------------------------------------------*/
  1795. int snd_trident_spdif_pcm(struct snd_trident *trident, int device)
  1796. {
  1797. struct snd_pcm *spdif;
  1798. int err;
  1799. err = snd_pcm_new(trident->card, "trident_dx_nx IEC958", device, 1, 0, &spdif);
  1800. if (err < 0)
  1801. return err;
  1802. spdif->private_data = trident;
  1803. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1804. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_ops);
  1805. } else {
  1806. snd_pcm_set_ops(spdif, SNDRV_PCM_STREAM_PLAYBACK, &snd_trident_spdif_7018_ops);
  1807. }
  1808. spdif->info_flags = 0;
  1809. strcpy(spdif->name, "Trident 4DWave IEC958");
  1810. trident->spdif = spdif;
  1811. snd_pcm_set_managed_buffer_all(spdif, SNDRV_DMA_TYPE_DEV,
  1812. &trident->pci->dev, 64*1024, 128*1024);
  1813. return 0;
  1814. }
  1815. /*
  1816. * Mixer part
  1817. */
  1818. /*---------------------------------------------------------------------------
  1819. snd_trident_spdif_control
  1820. Description: enable/disable S/PDIF out from ac97 mixer
  1821. ---------------------------------------------------------------------------*/
  1822. #define snd_trident_spdif_control_info snd_ctl_boolean_mono_info
  1823. static int snd_trident_spdif_control_get(struct snd_kcontrol *kcontrol,
  1824. struct snd_ctl_elem_value *ucontrol)
  1825. {
  1826. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1827. unsigned char val;
  1828. spin_lock_irq(&trident->reg_lock);
  1829. val = trident->spdif_ctrl;
  1830. ucontrol->value.integer.value[0] = val == kcontrol->private_value;
  1831. spin_unlock_irq(&trident->reg_lock);
  1832. return 0;
  1833. }
  1834. static int snd_trident_spdif_control_put(struct snd_kcontrol *kcontrol,
  1835. struct snd_ctl_elem_value *ucontrol)
  1836. {
  1837. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1838. unsigned char val;
  1839. int change;
  1840. val = ucontrol->value.integer.value[0] ? (unsigned char) kcontrol->private_value : 0x00;
  1841. spin_lock_irq(&trident->reg_lock);
  1842. /* S/PDIF C Channel bits 0-31 : 48khz, SCMS disabled */
  1843. change = trident->spdif_ctrl != val;
  1844. trident->spdif_ctrl = val;
  1845. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1846. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0) {
  1847. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1848. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  1849. }
  1850. } else {
  1851. if (trident->spdif == NULL) {
  1852. unsigned int temp;
  1853. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1854. temp = inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & ~SPDIF_EN;
  1855. if (val)
  1856. temp |= SPDIF_EN;
  1857. outl(temp, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  1858. }
  1859. }
  1860. spin_unlock_irq(&trident->reg_lock);
  1861. return change;
  1862. }
  1863. static const struct snd_kcontrol_new snd_trident_spdif_control =
  1864. {
  1865. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1866. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
  1867. .info = snd_trident_spdif_control_info,
  1868. .get = snd_trident_spdif_control_get,
  1869. .put = snd_trident_spdif_control_put,
  1870. .private_value = 0x28,
  1871. };
  1872. /*---------------------------------------------------------------------------
  1873. snd_trident_spdif_default
  1874. Description: put/get the S/PDIF default settings
  1875. ---------------------------------------------------------------------------*/
  1876. static int snd_trident_spdif_default_info(struct snd_kcontrol *kcontrol,
  1877. struct snd_ctl_elem_info *uinfo)
  1878. {
  1879. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1880. uinfo->count = 1;
  1881. return 0;
  1882. }
  1883. static int snd_trident_spdif_default_get(struct snd_kcontrol *kcontrol,
  1884. struct snd_ctl_elem_value *ucontrol)
  1885. {
  1886. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1887. spin_lock_irq(&trident->reg_lock);
  1888. ucontrol->value.iec958.status[0] = (trident->spdif_bits >> 0) & 0xff;
  1889. ucontrol->value.iec958.status[1] = (trident->spdif_bits >> 8) & 0xff;
  1890. ucontrol->value.iec958.status[2] = (trident->spdif_bits >> 16) & 0xff;
  1891. ucontrol->value.iec958.status[3] = (trident->spdif_bits >> 24) & 0xff;
  1892. spin_unlock_irq(&trident->reg_lock);
  1893. return 0;
  1894. }
  1895. static int snd_trident_spdif_default_put(struct snd_kcontrol *kcontrol,
  1896. struct snd_ctl_elem_value *ucontrol)
  1897. {
  1898. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1899. unsigned int val;
  1900. int change;
  1901. val = (ucontrol->value.iec958.status[0] << 0) |
  1902. (ucontrol->value.iec958.status[1] << 8) |
  1903. (ucontrol->value.iec958.status[2] << 16) |
  1904. (ucontrol->value.iec958.status[3] << 24);
  1905. spin_lock_irq(&trident->reg_lock);
  1906. change = trident->spdif_bits != val;
  1907. trident->spdif_bits = val;
  1908. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1909. if ((inb(TRID_REG(trident, NX_SPCTRL_SPCSO + 3)) & 0x10) == 0)
  1910. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  1911. } else {
  1912. if (trident->spdif == NULL)
  1913. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1914. }
  1915. spin_unlock_irq(&trident->reg_lock);
  1916. return change;
  1917. }
  1918. static const struct snd_kcontrol_new snd_trident_spdif_default =
  1919. {
  1920. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1921. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
  1922. .info = snd_trident_spdif_default_info,
  1923. .get = snd_trident_spdif_default_get,
  1924. .put = snd_trident_spdif_default_put
  1925. };
  1926. /*---------------------------------------------------------------------------
  1927. snd_trident_spdif_mask
  1928. Description: put/get the S/PDIF mask
  1929. ---------------------------------------------------------------------------*/
  1930. static int snd_trident_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1931. struct snd_ctl_elem_info *uinfo)
  1932. {
  1933. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1934. uinfo->count = 1;
  1935. return 0;
  1936. }
  1937. static int snd_trident_spdif_mask_get(struct snd_kcontrol *kcontrol,
  1938. struct snd_ctl_elem_value *ucontrol)
  1939. {
  1940. ucontrol->value.iec958.status[0] = 0xff;
  1941. ucontrol->value.iec958.status[1] = 0xff;
  1942. ucontrol->value.iec958.status[2] = 0xff;
  1943. ucontrol->value.iec958.status[3] = 0xff;
  1944. return 0;
  1945. }
  1946. static const struct snd_kcontrol_new snd_trident_spdif_mask =
  1947. {
  1948. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  1949. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  1950. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
  1951. .info = snd_trident_spdif_mask_info,
  1952. .get = snd_trident_spdif_mask_get,
  1953. };
  1954. /*---------------------------------------------------------------------------
  1955. snd_trident_spdif_stream
  1956. Description: put/get the S/PDIF stream settings
  1957. ---------------------------------------------------------------------------*/
  1958. static int snd_trident_spdif_stream_info(struct snd_kcontrol *kcontrol,
  1959. struct snd_ctl_elem_info *uinfo)
  1960. {
  1961. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1962. uinfo->count = 1;
  1963. return 0;
  1964. }
  1965. static int snd_trident_spdif_stream_get(struct snd_kcontrol *kcontrol,
  1966. struct snd_ctl_elem_value *ucontrol)
  1967. {
  1968. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1969. spin_lock_irq(&trident->reg_lock);
  1970. ucontrol->value.iec958.status[0] = (trident->spdif_pcm_bits >> 0) & 0xff;
  1971. ucontrol->value.iec958.status[1] = (trident->spdif_pcm_bits >> 8) & 0xff;
  1972. ucontrol->value.iec958.status[2] = (trident->spdif_pcm_bits >> 16) & 0xff;
  1973. ucontrol->value.iec958.status[3] = (trident->spdif_pcm_bits >> 24) & 0xff;
  1974. spin_unlock_irq(&trident->reg_lock);
  1975. return 0;
  1976. }
  1977. static int snd_trident_spdif_stream_put(struct snd_kcontrol *kcontrol,
  1978. struct snd_ctl_elem_value *ucontrol)
  1979. {
  1980. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  1981. unsigned int val;
  1982. int change;
  1983. val = (ucontrol->value.iec958.status[0] << 0) |
  1984. (ucontrol->value.iec958.status[1] << 8) |
  1985. (ucontrol->value.iec958.status[2] << 16) |
  1986. (ucontrol->value.iec958.status[3] << 24);
  1987. spin_lock_irq(&trident->reg_lock);
  1988. change = trident->spdif_pcm_bits != val;
  1989. trident->spdif_pcm_bits = val;
  1990. if (trident->spdif != NULL) {
  1991. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  1992. outl(trident->spdif_pcm_bits, TRID_REG(trident, NX_SPCSTATUS));
  1993. } else {
  1994. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  1995. }
  1996. }
  1997. spin_unlock_irq(&trident->reg_lock);
  1998. return change;
  1999. }
  2000. static const struct snd_kcontrol_new snd_trident_spdif_stream =
  2001. {
  2002. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2003. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  2004. .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PCM_STREAM),
  2005. .info = snd_trident_spdif_stream_info,
  2006. .get = snd_trident_spdif_stream_get,
  2007. .put = snd_trident_spdif_stream_put
  2008. };
  2009. /*---------------------------------------------------------------------------
  2010. snd_trident_ac97_control
  2011. Description: enable/disable rear path for ac97
  2012. ---------------------------------------------------------------------------*/
  2013. #define snd_trident_ac97_control_info snd_ctl_boolean_mono_info
  2014. static int snd_trident_ac97_control_get(struct snd_kcontrol *kcontrol,
  2015. struct snd_ctl_elem_value *ucontrol)
  2016. {
  2017. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2018. unsigned char val;
  2019. spin_lock_irq(&trident->reg_lock);
  2020. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2021. ucontrol->value.integer.value[0] = (val & (1 << kcontrol->private_value)) ? 1 : 0;
  2022. spin_unlock_irq(&trident->reg_lock);
  2023. return 0;
  2024. }
  2025. static int snd_trident_ac97_control_put(struct snd_kcontrol *kcontrol,
  2026. struct snd_ctl_elem_value *ucontrol)
  2027. {
  2028. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2029. unsigned char val;
  2030. int change = 0;
  2031. spin_lock_irq(&trident->reg_lock);
  2032. val = trident->ac97_ctrl = inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2033. val &= ~(1 << kcontrol->private_value);
  2034. if (ucontrol->value.integer.value[0])
  2035. val |= 1 << kcontrol->private_value;
  2036. change = val != trident->ac97_ctrl;
  2037. trident->ac97_ctrl = val;
  2038. outl(trident->ac97_ctrl = val, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2039. spin_unlock_irq(&trident->reg_lock);
  2040. return change;
  2041. }
  2042. static const struct snd_kcontrol_new snd_trident_ac97_rear_control =
  2043. {
  2044. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2045. .name = "Rear Path",
  2046. .info = snd_trident_ac97_control_info,
  2047. .get = snd_trident_ac97_control_get,
  2048. .put = snd_trident_ac97_control_put,
  2049. .private_value = 4,
  2050. };
  2051. /*---------------------------------------------------------------------------
  2052. snd_trident_vol_control
  2053. Description: wave & music volume control
  2054. ---------------------------------------------------------------------------*/
  2055. static int snd_trident_vol_control_info(struct snd_kcontrol *kcontrol,
  2056. struct snd_ctl_elem_info *uinfo)
  2057. {
  2058. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2059. uinfo->count = 2;
  2060. uinfo->value.integer.min = 0;
  2061. uinfo->value.integer.max = 255;
  2062. return 0;
  2063. }
  2064. static int snd_trident_vol_control_get(struct snd_kcontrol *kcontrol,
  2065. struct snd_ctl_elem_value *ucontrol)
  2066. {
  2067. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2068. unsigned int val;
  2069. val = trident->musicvol_wavevol;
  2070. ucontrol->value.integer.value[0] = 255 - ((val >> kcontrol->private_value) & 0xff);
  2071. ucontrol->value.integer.value[1] = 255 - ((val >> (kcontrol->private_value + 8)) & 0xff);
  2072. return 0;
  2073. }
  2074. static const DECLARE_TLV_DB_SCALE(db_scale_gvol, -6375, 25, 0);
  2075. static int snd_trident_vol_control_put(struct snd_kcontrol *kcontrol,
  2076. struct snd_ctl_elem_value *ucontrol)
  2077. {
  2078. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2079. unsigned int val;
  2080. int change = 0;
  2081. spin_lock_irq(&trident->reg_lock);
  2082. val = trident->musicvol_wavevol;
  2083. val &= ~(0xffff << kcontrol->private_value);
  2084. val |= ((255 - (ucontrol->value.integer.value[0] & 0xff)) |
  2085. ((255 - (ucontrol->value.integer.value[1] & 0xff)) << 8)) << kcontrol->private_value;
  2086. change = val != trident->musicvol_wavevol;
  2087. outl(trident->musicvol_wavevol = val, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2088. spin_unlock_irq(&trident->reg_lock);
  2089. return change;
  2090. }
  2091. static const struct snd_kcontrol_new snd_trident_vol_music_control =
  2092. {
  2093. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2094. .name = "Music Playback Volume",
  2095. .info = snd_trident_vol_control_info,
  2096. .get = snd_trident_vol_control_get,
  2097. .put = snd_trident_vol_control_put,
  2098. .private_value = 16,
  2099. .tlv = { .p = db_scale_gvol },
  2100. };
  2101. static const struct snd_kcontrol_new snd_trident_vol_wave_control =
  2102. {
  2103. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2104. .name = "Wave Playback Volume",
  2105. .info = snd_trident_vol_control_info,
  2106. .get = snd_trident_vol_control_get,
  2107. .put = snd_trident_vol_control_put,
  2108. .private_value = 0,
  2109. .tlv = { .p = db_scale_gvol },
  2110. };
  2111. /*---------------------------------------------------------------------------
  2112. snd_trident_pcm_vol_control
  2113. Description: PCM front volume control
  2114. ---------------------------------------------------------------------------*/
  2115. static int snd_trident_pcm_vol_control_info(struct snd_kcontrol *kcontrol,
  2116. struct snd_ctl_elem_info *uinfo)
  2117. {
  2118. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2119. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2120. uinfo->count = 1;
  2121. uinfo->value.integer.min = 0;
  2122. uinfo->value.integer.max = 255;
  2123. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2124. uinfo->value.integer.max = 1023;
  2125. return 0;
  2126. }
  2127. static int snd_trident_pcm_vol_control_get(struct snd_kcontrol *kcontrol,
  2128. struct snd_ctl_elem_value *ucontrol)
  2129. {
  2130. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2131. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2132. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2133. ucontrol->value.integer.value[0] = 1023 - mix->vol;
  2134. } else {
  2135. ucontrol->value.integer.value[0] = 255 - (mix->vol>>2);
  2136. }
  2137. return 0;
  2138. }
  2139. static int snd_trident_pcm_vol_control_put(struct snd_kcontrol *kcontrol,
  2140. struct snd_ctl_elem_value *ucontrol)
  2141. {
  2142. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2143. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2144. unsigned int val;
  2145. int change = 0;
  2146. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2147. val = 1023 - (ucontrol->value.integer.value[0] & 1023);
  2148. } else {
  2149. val = (255 - (ucontrol->value.integer.value[0] & 255)) << 2;
  2150. }
  2151. spin_lock_irq(&trident->reg_lock);
  2152. change = val != mix->vol;
  2153. mix->vol = val;
  2154. if (mix->voice != NULL)
  2155. snd_trident_write_vol_reg(trident, mix->voice, val);
  2156. spin_unlock_irq(&trident->reg_lock);
  2157. return change;
  2158. }
  2159. static const struct snd_kcontrol_new snd_trident_pcm_vol_control =
  2160. {
  2161. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2162. .name = "PCM Front Playback Volume",
  2163. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2164. .count = 32,
  2165. .info = snd_trident_pcm_vol_control_info,
  2166. .get = snd_trident_pcm_vol_control_get,
  2167. .put = snd_trident_pcm_vol_control_put,
  2168. /* FIXME: no tlv yet */
  2169. };
  2170. /*---------------------------------------------------------------------------
  2171. snd_trident_pcm_pan_control
  2172. Description: PCM front pan control
  2173. ---------------------------------------------------------------------------*/
  2174. static int snd_trident_pcm_pan_control_info(struct snd_kcontrol *kcontrol,
  2175. struct snd_ctl_elem_info *uinfo)
  2176. {
  2177. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2178. uinfo->count = 1;
  2179. uinfo->value.integer.min = 0;
  2180. uinfo->value.integer.max = 127;
  2181. return 0;
  2182. }
  2183. static int snd_trident_pcm_pan_control_get(struct snd_kcontrol *kcontrol,
  2184. struct snd_ctl_elem_value *ucontrol)
  2185. {
  2186. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2187. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2188. ucontrol->value.integer.value[0] = mix->pan;
  2189. if (ucontrol->value.integer.value[0] & 0x40) {
  2190. ucontrol->value.integer.value[0] = (0x3f - (ucontrol->value.integer.value[0] & 0x3f));
  2191. } else {
  2192. ucontrol->value.integer.value[0] |= 0x40;
  2193. }
  2194. return 0;
  2195. }
  2196. static int snd_trident_pcm_pan_control_put(struct snd_kcontrol *kcontrol,
  2197. struct snd_ctl_elem_value *ucontrol)
  2198. {
  2199. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2200. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2201. unsigned char val;
  2202. int change = 0;
  2203. if (ucontrol->value.integer.value[0] & 0x40)
  2204. val = ucontrol->value.integer.value[0] & 0x3f;
  2205. else
  2206. val = (0x3f - (ucontrol->value.integer.value[0] & 0x3f)) | 0x40;
  2207. spin_lock_irq(&trident->reg_lock);
  2208. change = val != mix->pan;
  2209. mix->pan = val;
  2210. if (mix->voice != NULL)
  2211. snd_trident_write_pan_reg(trident, mix->voice, val);
  2212. spin_unlock_irq(&trident->reg_lock);
  2213. return change;
  2214. }
  2215. static const struct snd_kcontrol_new snd_trident_pcm_pan_control =
  2216. {
  2217. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2218. .name = "PCM Pan Playback Control",
  2219. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2220. .count = 32,
  2221. .info = snd_trident_pcm_pan_control_info,
  2222. .get = snd_trident_pcm_pan_control_get,
  2223. .put = snd_trident_pcm_pan_control_put,
  2224. };
  2225. /*---------------------------------------------------------------------------
  2226. snd_trident_pcm_rvol_control
  2227. Description: PCM reverb volume control
  2228. ---------------------------------------------------------------------------*/
  2229. static int snd_trident_pcm_rvol_control_info(struct snd_kcontrol *kcontrol,
  2230. struct snd_ctl_elem_info *uinfo)
  2231. {
  2232. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2233. uinfo->count = 1;
  2234. uinfo->value.integer.min = 0;
  2235. uinfo->value.integer.max = 127;
  2236. return 0;
  2237. }
  2238. static int snd_trident_pcm_rvol_control_get(struct snd_kcontrol *kcontrol,
  2239. struct snd_ctl_elem_value *ucontrol)
  2240. {
  2241. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2242. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2243. ucontrol->value.integer.value[0] = 127 - mix->rvol;
  2244. return 0;
  2245. }
  2246. static int snd_trident_pcm_rvol_control_put(struct snd_kcontrol *kcontrol,
  2247. struct snd_ctl_elem_value *ucontrol)
  2248. {
  2249. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2250. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2251. unsigned short val;
  2252. int change = 0;
  2253. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2254. spin_lock_irq(&trident->reg_lock);
  2255. change = val != mix->rvol;
  2256. mix->rvol = val;
  2257. if (mix->voice != NULL)
  2258. snd_trident_write_rvol_reg(trident, mix->voice, val);
  2259. spin_unlock_irq(&trident->reg_lock);
  2260. return change;
  2261. }
  2262. static const DECLARE_TLV_DB_SCALE(db_scale_crvol, -3175, 25, 1);
  2263. static const struct snd_kcontrol_new snd_trident_pcm_rvol_control =
  2264. {
  2265. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2266. .name = "PCM Reverb Playback Volume",
  2267. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2268. .count = 32,
  2269. .info = snd_trident_pcm_rvol_control_info,
  2270. .get = snd_trident_pcm_rvol_control_get,
  2271. .put = snd_trident_pcm_rvol_control_put,
  2272. .tlv = { .p = db_scale_crvol },
  2273. };
  2274. /*---------------------------------------------------------------------------
  2275. snd_trident_pcm_cvol_control
  2276. Description: PCM chorus volume control
  2277. ---------------------------------------------------------------------------*/
  2278. static int snd_trident_pcm_cvol_control_info(struct snd_kcontrol *kcontrol,
  2279. struct snd_ctl_elem_info *uinfo)
  2280. {
  2281. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  2282. uinfo->count = 1;
  2283. uinfo->value.integer.min = 0;
  2284. uinfo->value.integer.max = 127;
  2285. return 0;
  2286. }
  2287. static int snd_trident_pcm_cvol_control_get(struct snd_kcontrol *kcontrol,
  2288. struct snd_ctl_elem_value *ucontrol)
  2289. {
  2290. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2291. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2292. ucontrol->value.integer.value[0] = 127 - mix->cvol;
  2293. return 0;
  2294. }
  2295. static int snd_trident_pcm_cvol_control_put(struct snd_kcontrol *kcontrol,
  2296. struct snd_ctl_elem_value *ucontrol)
  2297. {
  2298. struct snd_trident *trident = snd_kcontrol_chip(kcontrol);
  2299. struct snd_trident_pcm_mixer *mix = &trident->pcm_mixer[snd_ctl_get_ioffnum(kcontrol, &ucontrol->id)];
  2300. unsigned short val;
  2301. int change = 0;
  2302. val = 0x7f - (ucontrol->value.integer.value[0] & 0x7f);
  2303. spin_lock_irq(&trident->reg_lock);
  2304. change = val != mix->cvol;
  2305. mix->cvol = val;
  2306. if (mix->voice != NULL)
  2307. snd_trident_write_cvol_reg(trident, mix->voice, val);
  2308. spin_unlock_irq(&trident->reg_lock);
  2309. return change;
  2310. }
  2311. static const struct snd_kcontrol_new snd_trident_pcm_cvol_control =
  2312. {
  2313. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2314. .name = "PCM Chorus Playback Volume",
  2315. .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_INACTIVE,
  2316. .count = 32,
  2317. .info = snd_trident_pcm_cvol_control_info,
  2318. .get = snd_trident_pcm_cvol_control_get,
  2319. .put = snd_trident_pcm_cvol_control_put,
  2320. .tlv = { .p = db_scale_crvol },
  2321. };
  2322. static void snd_trident_notify_pcm_change1(struct snd_card *card,
  2323. struct snd_kcontrol *kctl,
  2324. int num, int activate)
  2325. {
  2326. struct snd_ctl_elem_id id;
  2327. if (! kctl)
  2328. return;
  2329. if (activate)
  2330. kctl->vd[num].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2331. else
  2332. kctl->vd[num].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
  2333. snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE |
  2334. SNDRV_CTL_EVENT_MASK_INFO,
  2335. snd_ctl_build_ioff(&id, kctl, num));
  2336. }
  2337. static void snd_trident_notify_pcm_change(struct snd_trident *trident,
  2338. struct snd_trident_pcm_mixer *tmix,
  2339. int num, int activate)
  2340. {
  2341. snd_trident_notify_pcm_change1(trident->card, trident->ctl_vol, num, activate);
  2342. snd_trident_notify_pcm_change1(trident->card, trident->ctl_pan, num, activate);
  2343. snd_trident_notify_pcm_change1(trident->card, trident->ctl_rvol, num, activate);
  2344. snd_trident_notify_pcm_change1(trident->card, trident->ctl_cvol, num, activate);
  2345. }
  2346. static int snd_trident_pcm_mixer_build(struct snd_trident *trident,
  2347. struct snd_trident_voice *voice,
  2348. struct snd_pcm_substream *substream)
  2349. {
  2350. struct snd_trident_pcm_mixer *tmix;
  2351. if (snd_BUG_ON(!trident || !voice || !substream))
  2352. return -EINVAL;
  2353. tmix = &trident->pcm_mixer[substream->number];
  2354. tmix->voice = voice;
  2355. tmix->vol = T4D_DEFAULT_PCM_VOL;
  2356. tmix->pan = T4D_DEFAULT_PCM_PAN;
  2357. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  2358. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  2359. snd_trident_notify_pcm_change(trident, tmix, substream->number, 1);
  2360. return 0;
  2361. }
  2362. static int snd_trident_pcm_mixer_free(struct snd_trident *trident, struct snd_trident_voice *voice, struct snd_pcm_substream *substream)
  2363. {
  2364. struct snd_trident_pcm_mixer *tmix;
  2365. if (snd_BUG_ON(!trident || !substream))
  2366. return -EINVAL;
  2367. tmix = &trident->pcm_mixer[substream->number];
  2368. tmix->voice = NULL;
  2369. snd_trident_notify_pcm_change(trident, tmix, substream->number, 0);
  2370. return 0;
  2371. }
  2372. /*---------------------------------------------------------------------------
  2373. snd_trident_mixer
  2374. Description: This routine registers the 4DWave device for mixer support.
  2375. Parameters: trident - pointer to target device class for 4DWave.
  2376. Returns: None
  2377. ---------------------------------------------------------------------------*/
  2378. static int snd_trident_mixer(struct snd_trident *trident, int pcm_spdif_device)
  2379. {
  2380. struct snd_ac97_template _ac97;
  2381. struct snd_card *card = trident->card;
  2382. struct snd_kcontrol *kctl;
  2383. struct snd_ctl_elem_value *uctl;
  2384. int idx, err, retries = 2;
  2385. static const struct snd_ac97_bus_ops ops = {
  2386. .write = snd_trident_codec_write,
  2387. .read = snd_trident_codec_read,
  2388. };
  2389. uctl = kzalloc(sizeof(*uctl), GFP_KERNEL);
  2390. if (!uctl)
  2391. return -ENOMEM;
  2392. err = snd_ac97_bus(trident->card, 0, &ops, NULL, &trident->ac97_bus);
  2393. if (err < 0)
  2394. goto __out;
  2395. memset(&_ac97, 0, sizeof(_ac97));
  2396. _ac97.private_data = trident;
  2397. trident->ac97_detect = 1;
  2398. __again:
  2399. err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97);
  2400. if (err < 0) {
  2401. if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2402. err = snd_trident_sis_reset(trident);
  2403. if (err < 0)
  2404. goto __out;
  2405. if (retries-- > 0)
  2406. goto __again;
  2407. err = -EIO;
  2408. }
  2409. goto __out;
  2410. }
  2411. /* secondary codec? */
  2412. if (trident->device == TRIDENT_DEVICE_ID_SI7018 &&
  2413. (inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0) {
  2414. _ac97.num = 1;
  2415. err = snd_ac97_mixer(trident->ac97_bus, &_ac97, &trident->ac97_sec);
  2416. if (err < 0)
  2417. dev_err(trident->card->dev,
  2418. "SI7018: the secondary codec - invalid access\n");
  2419. #if 0 // only for my testing purpose --jk
  2420. {
  2421. struct snd_ac97 *mc97;
  2422. err = snd_ac97_modem(trident->card, &_ac97, &mc97);
  2423. if (err < 0)
  2424. dev_err(trident->card->dev,
  2425. "snd_ac97_modem returned error %i\n", err);
  2426. }
  2427. #endif
  2428. }
  2429. trident->ac97_detect = 0;
  2430. if (trident->device != TRIDENT_DEVICE_ID_SI7018) {
  2431. kctl = snd_ctl_new1(&snd_trident_vol_wave_control, trident);
  2432. err = snd_ctl_add(card, kctl);
  2433. if (err < 0)
  2434. goto __out;
  2435. kctl->put(kctl, uctl);
  2436. kctl = snd_ctl_new1(&snd_trident_vol_music_control, trident);
  2437. err = snd_ctl_add(card, kctl);
  2438. if (err < 0)
  2439. goto __out;
  2440. kctl->put(kctl, uctl);
  2441. outl(trident->musicvol_wavevol = 0x00000000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2442. } else {
  2443. outl(trident->musicvol_wavevol = 0xffff0000, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  2444. }
  2445. for (idx = 0; idx < 32; idx++) {
  2446. struct snd_trident_pcm_mixer *tmix;
  2447. tmix = &trident->pcm_mixer[idx];
  2448. tmix->voice = NULL;
  2449. }
  2450. trident->ctl_vol = snd_ctl_new1(&snd_trident_pcm_vol_control, trident);
  2451. if (!trident->ctl_vol)
  2452. goto __nomem;
  2453. err = snd_ctl_add(card, trident->ctl_vol);
  2454. if (err)
  2455. goto __out;
  2456. trident->ctl_pan = snd_ctl_new1(&snd_trident_pcm_pan_control, trident);
  2457. if (!trident->ctl_pan)
  2458. goto __nomem;
  2459. err = snd_ctl_add(card, trident->ctl_pan);
  2460. if (err)
  2461. goto __out;
  2462. trident->ctl_rvol = snd_ctl_new1(&snd_trident_pcm_rvol_control, trident);
  2463. if (!trident->ctl_rvol)
  2464. goto __nomem;
  2465. err = snd_ctl_add(card, trident->ctl_rvol);
  2466. if (err)
  2467. goto __out;
  2468. trident->ctl_cvol = snd_ctl_new1(&snd_trident_pcm_cvol_control, trident);
  2469. if (!trident->ctl_cvol)
  2470. goto __nomem;
  2471. err = snd_ctl_add(card, trident->ctl_cvol);
  2472. if (err)
  2473. goto __out;
  2474. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2475. kctl = snd_ctl_new1(&snd_trident_ac97_rear_control, trident);
  2476. err = snd_ctl_add(card, kctl);
  2477. if (err < 0)
  2478. goto __out;
  2479. kctl->put(kctl, uctl);
  2480. }
  2481. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2482. kctl = snd_ctl_new1(&snd_trident_spdif_control, trident);
  2483. if (kctl == NULL) {
  2484. err = -ENOMEM;
  2485. goto __out;
  2486. }
  2487. if (trident->ac97->ext_id & AC97_EI_SPDIF)
  2488. kctl->id.index++;
  2489. if (trident->ac97_sec && (trident->ac97_sec->ext_id & AC97_EI_SPDIF))
  2490. kctl->id.index++;
  2491. idx = kctl->id.index;
  2492. err = snd_ctl_add(card, kctl);
  2493. if (err < 0)
  2494. goto __out;
  2495. kctl->put(kctl, uctl);
  2496. kctl = snd_ctl_new1(&snd_trident_spdif_default, trident);
  2497. if (kctl == NULL) {
  2498. err = -ENOMEM;
  2499. goto __out;
  2500. }
  2501. kctl->id.index = idx;
  2502. kctl->id.device = pcm_spdif_device;
  2503. err = snd_ctl_add(card, kctl);
  2504. if (err < 0)
  2505. goto __out;
  2506. kctl = snd_ctl_new1(&snd_trident_spdif_mask, trident);
  2507. if (kctl == NULL) {
  2508. err = -ENOMEM;
  2509. goto __out;
  2510. }
  2511. kctl->id.index = idx;
  2512. kctl->id.device = pcm_spdif_device;
  2513. err = snd_ctl_add(card, kctl);
  2514. if (err < 0)
  2515. goto __out;
  2516. kctl = snd_ctl_new1(&snd_trident_spdif_stream, trident);
  2517. if (kctl == NULL) {
  2518. err = -ENOMEM;
  2519. goto __out;
  2520. }
  2521. kctl->id.index = idx;
  2522. kctl->id.device = pcm_spdif_device;
  2523. err = snd_ctl_add(card, kctl);
  2524. if (err < 0)
  2525. goto __out;
  2526. trident->spdif_pcm_ctl = kctl;
  2527. }
  2528. err = 0;
  2529. goto __out;
  2530. __nomem:
  2531. err = -ENOMEM;
  2532. __out:
  2533. kfree(uctl);
  2534. return err;
  2535. }
  2536. /*
  2537. * gameport interface
  2538. */
  2539. #if IS_REACHABLE(CONFIG_GAMEPORT)
  2540. static unsigned char snd_trident_gameport_read(struct gameport *gameport)
  2541. {
  2542. struct snd_trident *chip = gameport_get_port_data(gameport);
  2543. if (snd_BUG_ON(!chip))
  2544. return 0;
  2545. return inb(TRID_REG(chip, GAMEPORT_LEGACY));
  2546. }
  2547. static void snd_trident_gameport_trigger(struct gameport *gameport)
  2548. {
  2549. struct snd_trident *chip = gameport_get_port_data(gameport);
  2550. if (snd_BUG_ON(!chip))
  2551. return;
  2552. outb(0xff, TRID_REG(chip, GAMEPORT_LEGACY));
  2553. }
  2554. static int snd_trident_gameport_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  2555. {
  2556. struct snd_trident *chip = gameport_get_port_data(gameport);
  2557. int i;
  2558. if (snd_BUG_ON(!chip))
  2559. return 0;
  2560. *buttons = (~inb(TRID_REG(chip, GAMEPORT_LEGACY)) >> 4) & 0xf;
  2561. for (i = 0; i < 4; i++) {
  2562. axes[i] = inw(TRID_REG(chip, GAMEPORT_AXES + i * 2));
  2563. if (axes[i] == 0xffff) axes[i] = -1;
  2564. }
  2565. return 0;
  2566. }
  2567. static int snd_trident_gameport_open(struct gameport *gameport, int mode)
  2568. {
  2569. struct snd_trident *chip = gameport_get_port_data(gameport);
  2570. if (snd_BUG_ON(!chip))
  2571. return 0;
  2572. switch (mode) {
  2573. case GAMEPORT_MODE_COOKED:
  2574. outb(GAMEPORT_MODE_ADC, TRID_REG(chip, GAMEPORT_GCR));
  2575. msleep(20);
  2576. return 0;
  2577. case GAMEPORT_MODE_RAW:
  2578. outb(0, TRID_REG(chip, GAMEPORT_GCR));
  2579. return 0;
  2580. default:
  2581. return -1;
  2582. }
  2583. }
  2584. int snd_trident_create_gameport(struct snd_trident *chip)
  2585. {
  2586. struct gameport *gp;
  2587. chip->gameport = gp = gameport_allocate_port();
  2588. if (!gp) {
  2589. dev_err(chip->card->dev,
  2590. "cannot allocate memory for gameport\n");
  2591. return -ENOMEM;
  2592. }
  2593. gameport_set_name(gp, "Trident 4DWave");
  2594. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  2595. gameport_set_dev_parent(gp, &chip->pci->dev);
  2596. gameport_set_port_data(gp, chip);
  2597. gp->fuzz = 64;
  2598. gp->read = snd_trident_gameport_read;
  2599. gp->trigger = snd_trident_gameport_trigger;
  2600. gp->cooked_read = snd_trident_gameport_cooked_read;
  2601. gp->open = snd_trident_gameport_open;
  2602. gameport_register_port(gp);
  2603. return 0;
  2604. }
  2605. static inline void snd_trident_free_gameport(struct snd_trident *chip)
  2606. {
  2607. if (chip->gameport) {
  2608. gameport_unregister_port(chip->gameport);
  2609. chip->gameport = NULL;
  2610. }
  2611. }
  2612. #else
  2613. int snd_trident_create_gameport(struct snd_trident *chip) { return -ENOSYS; }
  2614. static inline void snd_trident_free_gameport(struct snd_trident *chip) { }
  2615. #endif /* CONFIG_GAMEPORT */
  2616. /*
  2617. * delay for 1 tick
  2618. */
  2619. static inline void do_delay(struct snd_trident *chip)
  2620. {
  2621. schedule_timeout_uninterruptible(1);
  2622. }
  2623. /*
  2624. * SiS reset routine
  2625. */
  2626. static int snd_trident_sis_reset(struct snd_trident *trident)
  2627. {
  2628. unsigned long end_time;
  2629. unsigned int i;
  2630. int r;
  2631. r = trident->in_suspend ? 0 : 2; /* count of retries */
  2632. __si7018_retry:
  2633. pci_write_config_byte(trident->pci, 0x46, 0x04); /* SOFTWARE RESET */
  2634. udelay(100);
  2635. pci_write_config_byte(trident->pci, 0x46, 0x00);
  2636. udelay(100);
  2637. /* disable AC97 GPIO interrupt */
  2638. outb(0x00, TRID_REG(trident, SI_AC97_GPIO));
  2639. /* initialize serial interface, force cold reset */
  2640. i = PCMOUT|SURROUT|CENTEROUT|LFEOUT|SECONDARY_ID|COLD_RESET;
  2641. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2642. udelay(1000);
  2643. /* remove cold reset */
  2644. i &= ~COLD_RESET;
  2645. outl(i, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2646. udelay(2000);
  2647. /* wait, until the codec is ready */
  2648. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2649. do {
  2650. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_PRIMARY_READY) != 0)
  2651. goto __si7018_ok;
  2652. do_delay(trident);
  2653. } while (time_after_eq(end_time, jiffies));
  2654. dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
  2655. inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)));
  2656. if (r-- > 0) {
  2657. end_time = jiffies + HZ;
  2658. do {
  2659. do_delay(trident);
  2660. } while (time_after_eq(end_time, jiffies));
  2661. goto __si7018_retry;
  2662. }
  2663. __si7018_ok:
  2664. /* wait for the second codec */
  2665. do {
  2666. if ((inl(TRID_REG(trident, SI_SERIAL_INTF_CTRL)) & SI_AC97_SECONDARY_READY) != 0)
  2667. break;
  2668. do_delay(trident);
  2669. } while (time_after_eq(end_time, jiffies));
  2670. /* enable 64 channel mode */
  2671. outl(BANK_B_EN, TRID_REG(trident, T4D_LFO_GC_CIR));
  2672. return 0;
  2673. }
  2674. /*
  2675. * /proc interface
  2676. */
  2677. static void snd_trident_proc_read(struct snd_info_entry *entry,
  2678. struct snd_info_buffer *buffer)
  2679. {
  2680. struct snd_trident *trident = entry->private_data;
  2681. char *s;
  2682. switch (trident->device) {
  2683. case TRIDENT_DEVICE_ID_SI7018:
  2684. s = "SiS 7018 Audio";
  2685. break;
  2686. case TRIDENT_DEVICE_ID_DX:
  2687. s = "Trident 4DWave PCI DX";
  2688. break;
  2689. case TRIDENT_DEVICE_ID_NX:
  2690. s = "Trident 4DWave PCI NX";
  2691. break;
  2692. default:
  2693. s = "???";
  2694. }
  2695. snd_iprintf(buffer, "%s\n\n", s);
  2696. snd_iprintf(buffer, "Spurious IRQs : %d\n", trident->spurious_irq_count);
  2697. snd_iprintf(buffer, "Spurious IRQ dlta: %d\n", trident->spurious_irq_max_delta);
  2698. if (trident->device == TRIDENT_DEVICE_ID_NX || trident->device == TRIDENT_DEVICE_ID_SI7018)
  2699. snd_iprintf(buffer, "IEC958 Mixer Out : %s\n", trident->spdif_ctrl == 0x28 ? "on" : "off");
  2700. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2701. snd_iprintf(buffer, "Rear Speakers : %s\n", trident->ac97_ctrl & 0x00000010 ? "on" : "off");
  2702. if (trident->tlb.entries) {
  2703. snd_iprintf(buffer,"\nVirtual Memory\n");
  2704. snd_iprintf(buffer, "Memory Maximum : %d\n", trident->tlb.memhdr->size);
  2705. snd_iprintf(buffer, "Memory Used : %d\n", trident->tlb.memhdr->used);
  2706. snd_iprintf(buffer, "Memory Free : %d\n", snd_util_mem_avail(trident->tlb.memhdr));
  2707. }
  2708. }
  2709. }
  2710. static void snd_trident_proc_init(struct snd_trident *trident)
  2711. {
  2712. const char *s = "trident";
  2713. if (trident->device == TRIDENT_DEVICE_ID_SI7018)
  2714. s = "sis7018";
  2715. snd_card_ro_proc_new(trident->card, s, trident, snd_trident_proc_read);
  2716. }
  2717. /*---------------------------------------------------------------------------
  2718. snd_trident_tlb_alloc
  2719. Description: Allocate and set up the TLB page table on 4D NX.
  2720. Each entry has 4 bytes (physical PCI address).
  2721. Parameters: trident - pointer to target device class for 4DWave.
  2722. Returns: 0 or negative error code
  2723. ---------------------------------------------------------------------------*/
  2724. static int snd_trident_tlb_alloc(struct snd_trident *trident)
  2725. {
  2726. int i;
  2727. /* TLB array must be aligned to 16kB !!! so we allocate
  2728. 32kB region and correct offset when necessary */
  2729. trident->tlb.buffer =
  2730. snd_devm_alloc_pages(&trident->pci->dev, SNDRV_DMA_TYPE_DEV,
  2731. 2 * SNDRV_TRIDENT_MAX_PAGES * 4);
  2732. if (!trident->tlb.buffer) {
  2733. dev_err(trident->card->dev, "unable to allocate TLB buffer\n");
  2734. return -ENOMEM;
  2735. }
  2736. trident->tlb.entries = (__le32 *)ALIGN((unsigned long)trident->tlb.buffer->area, SNDRV_TRIDENT_MAX_PAGES * 4);
  2737. trident->tlb.entries_dmaaddr = ALIGN(trident->tlb.buffer->addr, SNDRV_TRIDENT_MAX_PAGES * 4);
  2738. /* allocate and setup silent page and initialise TLB entries */
  2739. trident->tlb.silent_page =
  2740. snd_devm_alloc_pages(&trident->pci->dev, SNDRV_DMA_TYPE_DEV,
  2741. SNDRV_TRIDENT_PAGE_SIZE);
  2742. if (!trident->tlb.silent_page) {
  2743. dev_err(trident->card->dev, "unable to allocate silent page\n");
  2744. return -ENOMEM;
  2745. }
  2746. memset(trident->tlb.silent_page->area, 0, SNDRV_TRIDENT_PAGE_SIZE);
  2747. for (i = 0; i < SNDRV_TRIDENT_MAX_PAGES; i++)
  2748. trident->tlb.entries[i] = cpu_to_le32(trident->tlb.silent_page->addr & ~(SNDRV_TRIDENT_PAGE_SIZE-1));
  2749. /* use emu memory block manager code to manage tlb page allocation */
  2750. trident->tlb.memhdr = snd_util_memhdr_new(SNDRV_TRIDENT_PAGE_SIZE * SNDRV_TRIDENT_MAX_PAGES);
  2751. if (trident->tlb.memhdr == NULL)
  2752. return -ENOMEM;
  2753. trident->tlb.memhdr->block_extra_size = sizeof(struct snd_trident_memblk_arg);
  2754. return 0;
  2755. }
  2756. /*
  2757. * initialize 4D DX chip
  2758. */
  2759. static void snd_trident_stop_all_voices(struct snd_trident *trident)
  2760. {
  2761. outl(0xffffffff, TRID_REG(trident, T4D_STOP_A));
  2762. outl(0xffffffff, TRID_REG(trident, T4D_STOP_B));
  2763. outl(0, TRID_REG(trident, T4D_AINTEN_A));
  2764. outl(0, TRID_REG(trident, T4D_AINTEN_B));
  2765. }
  2766. static int snd_trident_4d_dx_init(struct snd_trident *trident)
  2767. {
  2768. struct pci_dev *pci = trident->pci;
  2769. unsigned long end_time;
  2770. /* reset the legacy configuration and whole audio/wavetable block */
  2771. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2772. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2773. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2774. pci_write_config_byte(pci, 0x46, 4); /* reset */
  2775. udelay(100);
  2776. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2777. udelay(100);
  2778. /* warm reset of the AC'97 codec */
  2779. outl(0x00000001, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2780. udelay(100);
  2781. outl(0x00000000, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2782. /* DAC on, disable SB IRQ and try to force ADC valid signal */
  2783. trident->ac97_ctrl = 0x0000004a;
  2784. outl(trident->ac97_ctrl, TRID_REG(trident, DX_ACR2_AC97_COM_STAT));
  2785. /* wait, until the codec is ready */
  2786. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2787. do {
  2788. if ((inl(TRID_REG(trident, DX_ACR2_AC97_COM_STAT)) & 0x0010) != 0)
  2789. goto __dx_ok;
  2790. do_delay(trident);
  2791. } while (time_after_eq(end_time, jiffies));
  2792. dev_err(trident->card->dev, "AC'97 codec ready error\n");
  2793. return -EIO;
  2794. __dx_ok:
  2795. snd_trident_stop_all_voices(trident);
  2796. return 0;
  2797. }
  2798. /*
  2799. * initialize 4D NX chip
  2800. */
  2801. static int snd_trident_4d_nx_init(struct snd_trident *trident)
  2802. {
  2803. struct pci_dev *pci = trident->pci;
  2804. unsigned long end_time;
  2805. /* reset the legacy configuration and whole audio/wavetable block */
  2806. pci_write_config_dword(pci, 0x40, 0); /* DDMA */
  2807. pci_write_config_byte(pci, 0x44, 0); /* ports */
  2808. pci_write_config_byte(pci, 0x45, 0); /* Legacy DMA */
  2809. pci_write_config_byte(pci, 0x46, 1); /* reset */
  2810. udelay(100);
  2811. pci_write_config_byte(pci, 0x46, 0); /* release reset */
  2812. udelay(100);
  2813. /* warm reset of the AC'97 codec */
  2814. outl(0x00000001, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2815. udelay(100);
  2816. outl(0x00000000, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2817. /* wait, until the codec is ready */
  2818. end_time = (jiffies + (HZ * 3) / 4) + 1;
  2819. do {
  2820. if ((inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)) & 0x0008) != 0)
  2821. goto __nx_ok;
  2822. do_delay(trident);
  2823. } while (time_after_eq(end_time, jiffies));
  2824. dev_err(trident->card->dev, "AC'97 codec ready error [0x%x]\n",
  2825. inl(TRID_REG(trident, NX_ACR0_AC97_COM_STAT)));
  2826. return -EIO;
  2827. __nx_ok:
  2828. /* DAC on */
  2829. trident->ac97_ctrl = 0x00000002;
  2830. outl(trident->ac97_ctrl, TRID_REG(trident, NX_ACR0_AC97_COM_STAT));
  2831. /* disable SB IRQ */
  2832. outl(NX_SB_IRQ_DISABLE, TRID_REG(trident, T4D_MISCINT));
  2833. snd_trident_stop_all_voices(trident);
  2834. if (trident->tlb.entries != NULL) {
  2835. unsigned int i;
  2836. /* enable virtual addressing via TLB */
  2837. i = trident->tlb.entries_dmaaddr;
  2838. i |= 0x00000001;
  2839. outl(i, TRID_REG(trident, NX_TLBC));
  2840. } else {
  2841. outl(0, TRID_REG(trident, NX_TLBC));
  2842. }
  2843. /* initialize S/PDIF */
  2844. outl(trident->spdif_bits, TRID_REG(trident, NX_SPCSTATUS));
  2845. outb(trident->spdif_ctrl, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  2846. return 0;
  2847. }
  2848. /*
  2849. * initialize sis7018 chip
  2850. */
  2851. static int snd_trident_sis_init(struct snd_trident *trident)
  2852. {
  2853. int err;
  2854. err = snd_trident_sis_reset(trident);
  2855. if (err < 0)
  2856. return err;
  2857. snd_trident_stop_all_voices(trident);
  2858. /* initialize S/PDIF */
  2859. outl(trident->spdif_bits, TRID_REG(trident, SI_SPDIF_CS));
  2860. return 0;
  2861. }
  2862. /*---------------------------------------------------------------------------
  2863. snd_trident_create
  2864. Description: This routine will create the device specific class for
  2865. the 4DWave card. It will also perform basic initialization.
  2866. Parameters: card - which card to create
  2867. pci - interface to PCI bus resource info
  2868. dma1ptr - playback dma buffer
  2869. dma2ptr - capture dma buffer
  2870. irqptr - interrupt resource info
  2871. Returns: 4DWave device class private data
  2872. ---------------------------------------------------------------------------*/
  2873. int snd_trident_create(struct snd_card *card,
  2874. struct pci_dev *pci,
  2875. int pcm_streams,
  2876. int pcm_spdif_device,
  2877. int max_wavetable_size)
  2878. {
  2879. struct snd_trident *trident = card->private_data;
  2880. int i, err;
  2881. struct snd_trident_voice *voice;
  2882. struct snd_trident_pcm_mixer *tmix;
  2883. /* enable PCI device */
  2884. err = pcim_enable_device(pci);
  2885. if (err < 0)
  2886. return err;
  2887. /* check, if we can restrict PCI DMA transfers to 30 bits */
  2888. if (dma_set_mask_and_coherent(&pci->dev, DMA_BIT_MASK(30))) {
  2889. dev_err(card->dev,
  2890. "architecture does not support 30bit PCI busmaster DMA\n");
  2891. return -ENXIO;
  2892. }
  2893. trident->device = (pci->vendor << 16) | pci->device;
  2894. trident->card = card;
  2895. trident->pci = pci;
  2896. spin_lock_init(&trident->reg_lock);
  2897. spin_lock_init(&trident->event_lock);
  2898. spin_lock_init(&trident->voice_alloc);
  2899. if (pcm_streams < 1)
  2900. pcm_streams = 1;
  2901. if (pcm_streams > 32)
  2902. pcm_streams = 32;
  2903. trident->ChanPCM = pcm_streams;
  2904. if (max_wavetable_size < 0 )
  2905. max_wavetable_size = 0;
  2906. trident->synth.max_size = max_wavetable_size * 1024;
  2907. trident->irq = -1;
  2908. card->private_free = snd_trident_free;
  2909. trident->midi_port = TRID_REG(trident, T4D_MPU401_BASE);
  2910. pci_set_master(pci);
  2911. err = pci_request_regions(pci, "Trident Audio");
  2912. if (err < 0)
  2913. return err;
  2914. trident->port = pci_resource_start(pci, 0);
  2915. if (devm_request_irq(&pci->dev, pci->irq, snd_trident_interrupt,
  2916. IRQF_SHARED, KBUILD_MODNAME, trident)) {
  2917. dev_err(card->dev, "unable to grab IRQ %d\n", pci->irq);
  2918. return -EBUSY;
  2919. }
  2920. trident->irq = pci->irq;
  2921. card->sync_irq = trident->irq;
  2922. /* allocate 16k-aligned TLB for NX cards */
  2923. trident->tlb.entries = NULL;
  2924. if (trident->device == TRIDENT_DEVICE_ID_NX) {
  2925. err = snd_trident_tlb_alloc(trident);
  2926. if (err < 0)
  2927. return err;
  2928. }
  2929. trident->spdif_bits = trident->spdif_pcm_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
  2930. /* initialize chip */
  2931. switch (trident->device) {
  2932. case TRIDENT_DEVICE_ID_DX:
  2933. err = snd_trident_4d_dx_init(trident);
  2934. break;
  2935. case TRIDENT_DEVICE_ID_NX:
  2936. err = snd_trident_4d_nx_init(trident);
  2937. break;
  2938. case TRIDENT_DEVICE_ID_SI7018:
  2939. err = snd_trident_sis_init(trident);
  2940. break;
  2941. default:
  2942. snd_BUG();
  2943. break;
  2944. }
  2945. if (err < 0)
  2946. return err;
  2947. err = snd_trident_mixer(trident, pcm_spdif_device);
  2948. if (err < 0)
  2949. return err;
  2950. /* initialise synth voices */
  2951. for (i = 0; i < 64; i++) {
  2952. voice = &trident->synth.voices[i];
  2953. voice->number = i;
  2954. voice->trident = trident;
  2955. }
  2956. /* initialize pcm mixer entries */
  2957. for (i = 0; i < 32; i++) {
  2958. tmix = &trident->pcm_mixer[i];
  2959. tmix->vol = T4D_DEFAULT_PCM_VOL;
  2960. tmix->pan = T4D_DEFAULT_PCM_PAN;
  2961. tmix->rvol = T4D_DEFAULT_PCM_RVOL;
  2962. tmix->cvol = T4D_DEFAULT_PCM_CVOL;
  2963. }
  2964. snd_trident_enable_eso(trident);
  2965. snd_trident_proc_init(trident);
  2966. return 0;
  2967. }
  2968. /*---------------------------------------------------------------------------
  2969. snd_trident_free
  2970. Description: This routine will free the device specific class for
  2971. the 4DWave card.
  2972. Parameters: card - card to release
  2973. Returns: None.
  2974. ---------------------------------------------------------------------------*/
  2975. static void snd_trident_free(struct snd_card *card)
  2976. {
  2977. struct snd_trident *trident = card->private_data;
  2978. snd_trident_free_gameport(trident);
  2979. snd_trident_disable_eso(trident);
  2980. // Disable S/PDIF out
  2981. if (trident->device == TRIDENT_DEVICE_ID_NX)
  2982. outb(0x00, TRID_REG(trident, NX_SPCTRL_SPCSO + 3));
  2983. else if (trident->device == TRIDENT_DEVICE_ID_SI7018) {
  2984. outl(0, TRID_REG(trident, SI_SERIAL_INTF_CTRL));
  2985. }
  2986. if (trident->tlb.buffer) {
  2987. outl(0, TRID_REG(trident, NX_TLBC));
  2988. snd_util_memhdr_free(trident->tlb.memhdr);
  2989. }
  2990. }
  2991. /*---------------------------------------------------------------------------
  2992. snd_trident_interrupt
  2993. Description: ISR for Trident 4DWave device
  2994. Parameters: trident - device specific private data for 4DWave card
  2995. Problems: It seems that Trident chips generates interrupts more than
  2996. one time in special cases. The spurious interrupts are
  2997. detected via sample timer (T4D_STIMER) and computing
  2998. corresponding delta value. The limits are detected with
  2999. the method try & fail so it is possible that it won't
  3000. work on all computers. [jaroslav]
  3001. Returns: None.
  3002. ---------------------------------------------------------------------------*/
  3003. static irqreturn_t snd_trident_interrupt(int irq, void *dev_id)
  3004. {
  3005. struct snd_trident *trident = dev_id;
  3006. unsigned int audio_int, chn_int, stimer, channel, mask, tmp;
  3007. int delta;
  3008. struct snd_trident_voice *voice;
  3009. audio_int = inl(TRID_REG(trident, T4D_MISCINT));
  3010. if ((audio_int & (ADDRESS_IRQ|MPU401_IRQ)) == 0)
  3011. return IRQ_NONE;
  3012. if (audio_int & ADDRESS_IRQ) {
  3013. // get interrupt status for all channels
  3014. spin_lock(&trident->reg_lock);
  3015. stimer = inl(TRID_REG(trident, T4D_STIMER)) & 0x00ffffff;
  3016. chn_int = inl(TRID_REG(trident, T4D_AINT_A));
  3017. if (chn_int == 0)
  3018. goto __skip1;
  3019. outl(chn_int, TRID_REG(trident, T4D_AINT_A)); /* ack */
  3020. __skip1:
  3021. chn_int = inl(TRID_REG(trident, T4D_AINT_B));
  3022. if (chn_int == 0)
  3023. goto __skip2;
  3024. for (channel = 63; channel >= 32; channel--) {
  3025. mask = 1 << (channel&0x1f);
  3026. if ((chn_int & mask) == 0)
  3027. continue;
  3028. voice = &trident->synth.voices[channel];
  3029. if (!voice->pcm || voice->substream == NULL) {
  3030. outl(mask, TRID_REG(trident, T4D_STOP_B));
  3031. continue;
  3032. }
  3033. delta = (int)stimer - (int)voice->stimer;
  3034. if (delta < 0)
  3035. delta = -delta;
  3036. if ((unsigned int)delta < voice->spurious_threshold) {
  3037. /* do some statistics here */
  3038. trident->spurious_irq_count++;
  3039. if (trident->spurious_irq_max_delta < (unsigned int)delta)
  3040. trident->spurious_irq_max_delta = delta;
  3041. continue;
  3042. }
  3043. voice->stimer = stimer;
  3044. if (voice->isync) {
  3045. if (!voice->isync3) {
  3046. tmp = inw(TRID_REG(trident, T4D_SBBL_SBCL));
  3047. if (trident->bDMAStart & 0x40)
  3048. tmp >>= 1;
  3049. if (tmp > 0)
  3050. tmp = voice->isync_max - tmp;
  3051. } else {
  3052. tmp = inl(TRID_REG(trident, NX_SPCTRL_SPCSO)) & 0x00ffffff;
  3053. }
  3054. if (tmp < voice->isync_mark) {
  3055. if (tmp > 0x10)
  3056. tmp = voice->isync_ESO - 7;
  3057. else
  3058. tmp = voice->isync_ESO + 2;
  3059. /* update ESO for IRQ voice to preserve sync */
  3060. snd_trident_stop_voice(trident, voice->number);
  3061. snd_trident_write_eso_reg(trident, voice, tmp);
  3062. snd_trident_start_voice(trident, voice->number);
  3063. }
  3064. } else if (voice->isync2) {
  3065. voice->isync2 = 0;
  3066. /* write original ESO and update CSO for IRQ voice to preserve sync */
  3067. snd_trident_stop_voice(trident, voice->number);
  3068. snd_trident_write_cso_reg(trident, voice, voice->isync_mark);
  3069. snd_trident_write_eso_reg(trident, voice, voice->ESO);
  3070. snd_trident_start_voice(trident, voice->number);
  3071. }
  3072. #if 0
  3073. if (voice->extra) {
  3074. /* update CSO for extra voice to preserve sync */
  3075. snd_trident_stop_voice(trident, voice->extra->number);
  3076. snd_trident_write_cso_reg(trident, voice->extra, 0);
  3077. snd_trident_start_voice(trident, voice->extra->number);
  3078. }
  3079. #endif
  3080. spin_unlock(&trident->reg_lock);
  3081. snd_pcm_period_elapsed(voice->substream);
  3082. spin_lock(&trident->reg_lock);
  3083. }
  3084. outl(chn_int, TRID_REG(trident, T4D_AINT_B)); /* ack */
  3085. __skip2:
  3086. spin_unlock(&trident->reg_lock);
  3087. }
  3088. if (audio_int & MPU401_IRQ) {
  3089. if (trident->rmidi) {
  3090. snd_mpu401_uart_interrupt(irq, trident->rmidi->private_data);
  3091. } else {
  3092. inb(TRID_REG(trident, T4D_MPUR0));
  3093. }
  3094. }
  3095. // outl((ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW), TRID_REG(trident, T4D_MISCINT));
  3096. return IRQ_HANDLED;
  3097. }
  3098. struct snd_trident_voice *snd_trident_alloc_voice(struct snd_trident * trident, int type, int client, int port)
  3099. {
  3100. struct snd_trident_voice *pvoice;
  3101. unsigned long flags;
  3102. int idx;
  3103. spin_lock_irqsave(&trident->voice_alloc, flags);
  3104. if (type == SNDRV_TRIDENT_VOICE_TYPE_PCM) {
  3105. idx = snd_trident_allocate_pcm_channel(trident);
  3106. if(idx < 0) {
  3107. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3108. return NULL;
  3109. }
  3110. pvoice = &trident->synth.voices[idx];
  3111. pvoice->use = 1;
  3112. pvoice->pcm = 1;
  3113. pvoice->capture = 0;
  3114. pvoice->spdif = 0;
  3115. pvoice->memblk = NULL;
  3116. pvoice->substream = NULL;
  3117. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3118. return pvoice;
  3119. }
  3120. if (type == SNDRV_TRIDENT_VOICE_TYPE_SYNTH) {
  3121. idx = snd_trident_allocate_synth_channel(trident);
  3122. if(idx < 0) {
  3123. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3124. return NULL;
  3125. }
  3126. pvoice = &trident->synth.voices[idx];
  3127. pvoice->use = 1;
  3128. pvoice->synth = 1;
  3129. pvoice->client = client;
  3130. pvoice->port = port;
  3131. pvoice->memblk = NULL;
  3132. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3133. return pvoice;
  3134. }
  3135. if (type == SNDRV_TRIDENT_VOICE_TYPE_MIDI) {
  3136. }
  3137. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3138. return NULL;
  3139. }
  3140. EXPORT_SYMBOL(snd_trident_alloc_voice);
  3141. void snd_trident_free_voice(struct snd_trident * trident, struct snd_trident_voice *voice)
  3142. {
  3143. unsigned long flags;
  3144. void (*private_free)(struct snd_trident_voice *);
  3145. if (voice == NULL || !voice->use)
  3146. return;
  3147. snd_trident_clear_voices(trident, voice->number, voice->number);
  3148. spin_lock_irqsave(&trident->voice_alloc, flags);
  3149. private_free = voice->private_free;
  3150. voice->private_free = NULL;
  3151. voice->private_data = NULL;
  3152. if (voice->pcm)
  3153. snd_trident_free_pcm_channel(trident, voice->number);
  3154. if (voice->synth)
  3155. snd_trident_free_synth_channel(trident, voice->number);
  3156. voice->use = voice->pcm = voice->synth = voice->midi = 0;
  3157. voice->capture = voice->spdif = 0;
  3158. voice->sample_ops = NULL;
  3159. voice->substream = NULL;
  3160. voice->extra = NULL;
  3161. spin_unlock_irqrestore(&trident->voice_alloc, flags);
  3162. if (private_free)
  3163. private_free(voice);
  3164. }
  3165. EXPORT_SYMBOL(snd_trident_free_voice);
  3166. static void snd_trident_clear_voices(struct snd_trident * trident, unsigned short v_min, unsigned short v_max)
  3167. {
  3168. unsigned int i, val, mask[2] = { 0, 0 };
  3169. if (snd_BUG_ON(v_min > 63 || v_max > 63))
  3170. return;
  3171. for (i = v_min; i <= v_max; i++)
  3172. mask[i >> 5] |= 1 << (i & 0x1f);
  3173. if (mask[0]) {
  3174. outl(mask[0], TRID_REG(trident, T4D_STOP_A));
  3175. val = inl(TRID_REG(trident, T4D_AINTEN_A));
  3176. outl(val & ~mask[0], TRID_REG(trident, T4D_AINTEN_A));
  3177. }
  3178. if (mask[1]) {
  3179. outl(mask[1], TRID_REG(trident, T4D_STOP_B));
  3180. val = inl(TRID_REG(trident, T4D_AINTEN_B));
  3181. outl(val & ~mask[1], TRID_REG(trident, T4D_AINTEN_B));
  3182. }
  3183. }
  3184. #ifdef CONFIG_PM_SLEEP
  3185. static int snd_trident_suspend(struct device *dev)
  3186. {
  3187. struct snd_card *card = dev_get_drvdata(dev);
  3188. struct snd_trident *trident = card->private_data;
  3189. trident->in_suspend = 1;
  3190. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  3191. snd_ac97_suspend(trident->ac97);
  3192. snd_ac97_suspend(trident->ac97_sec);
  3193. return 0;
  3194. }
  3195. static int snd_trident_resume(struct device *dev)
  3196. {
  3197. struct snd_card *card = dev_get_drvdata(dev);
  3198. struct snd_trident *trident = card->private_data;
  3199. switch (trident->device) {
  3200. case TRIDENT_DEVICE_ID_DX:
  3201. snd_trident_4d_dx_init(trident);
  3202. break;
  3203. case TRIDENT_DEVICE_ID_NX:
  3204. snd_trident_4d_nx_init(trident);
  3205. break;
  3206. case TRIDENT_DEVICE_ID_SI7018:
  3207. snd_trident_sis_init(trident);
  3208. break;
  3209. }
  3210. snd_ac97_resume(trident->ac97);
  3211. snd_ac97_resume(trident->ac97_sec);
  3212. /* restore some registers */
  3213. outl(trident->musicvol_wavevol, TRID_REG(trident, T4D_MUSICVOL_WAVEVOL));
  3214. snd_trident_enable_eso(trident);
  3215. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  3216. trident->in_suspend = 0;
  3217. return 0;
  3218. }
  3219. SIMPLE_DEV_PM_OPS(snd_trident_pm, snd_trident_suspend, snd_trident_resume);
  3220. #endif /* CONFIG_PM_SLEEP */