hda_codec.c 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This driver is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/pci.h>
  26. #include <linux/mutex.h>
  27. #include <linux/module.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include <sound/asoundef.h>
  31. #include <sound/tlv.h>
  32. #include <sound/initval.h>
  33. #include <sound/jack.h>
  34. #include "hda_local.h"
  35. #include "hda_beep.h"
  36. #include "hda_jack.h"
  37. #include <sound/hda_hwdep.h>
  38. #define CREATE_TRACE_POINTS
  39. #include "hda_trace.h"
  40. /*
  41. * vendor / preset table
  42. */
  43. struct hda_vendor_id {
  44. unsigned int id;
  45. const char *name;
  46. };
  47. /* codec vendor labels */
  48. static struct hda_vendor_id hda_vendor_ids[] = {
  49. { 0x1002, "ATI" },
  50. { 0x1013, "Cirrus Logic" },
  51. { 0x1057, "Motorola" },
  52. { 0x1095, "Silicon Image" },
  53. { 0x10de, "Nvidia" },
  54. { 0x10ec, "Realtek" },
  55. { 0x1102, "Creative" },
  56. { 0x1106, "VIA" },
  57. { 0x111d, "IDT" },
  58. { 0x11c1, "LSI" },
  59. { 0x11d4, "Analog Devices" },
  60. { 0x13f6, "C-Media" },
  61. { 0x14f1, "Conexant" },
  62. { 0x17e8, "Chrontel" },
  63. { 0x1854, "LG" },
  64. { 0x1aec, "Wolfson Microelectronics" },
  65. { 0x434d, "C-Media" },
  66. { 0x8086, "Intel" },
  67. { 0x8384, "SigmaTel" },
  68. {} /* terminator */
  69. };
  70. static DEFINE_MUTEX(preset_mutex);
  71. static LIST_HEAD(hda_preset_tables);
  72. int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
  73. {
  74. mutex_lock(&preset_mutex);
  75. list_add_tail(&preset->list, &hda_preset_tables);
  76. mutex_unlock(&preset_mutex);
  77. return 0;
  78. }
  79. EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
  80. int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
  81. {
  82. mutex_lock(&preset_mutex);
  83. list_del(&preset->list);
  84. mutex_unlock(&preset_mutex);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
  88. #ifdef CONFIG_PM
  89. #define codec_in_pm(codec) ((codec)->in_pm)
  90. static void hda_power_work(struct work_struct *work);
  91. static void hda_keep_power_on(struct hda_codec *codec);
  92. #define hda_codec_is_power_on(codec) ((codec)->power_on)
  93. static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
  94. {
  95. if (bus->ops.pm_notify)
  96. bus->ops.pm_notify(bus, power_up);
  97. }
  98. #else
  99. #define codec_in_pm(codec) 0
  100. static inline void hda_keep_power_on(struct hda_codec *codec) {}
  101. #define hda_codec_is_power_on(codec) 1
  102. #define hda_call_pm_notify(bus, state) {}
  103. #endif
  104. /**
  105. * snd_hda_get_jack_location - Give a location string of the jack
  106. * @cfg: pin default config value
  107. *
  108. * Parse the pin default config value and returns the string of the
  109. * jack location, e.g. "Rear", "Front", etc.
  110. */
  111. const char *snd_hda_get_jack_location(u32 cfg)
  112. {
  113. static char *bases[7] = {
  114. "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
  115. };
  116. static unsigned char specials_idx[] = {
  117. 0x07, 0x08,
  118. 0x17, 0x18, 0x19,
  119. 0x37, 0x38
  120. };
  121. static char *specials[] = {
  122. "Rear Panel", "Drive Bar",
  123. "Riser", "HDMI", "ATAPI",
  124. "Mobile-In", "Mobile-Out"
  125. };
  126. int i;
  127. cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
  128. if ((cfg & 0x0f) < 7)
  129. return bases[cfg & 0x0f];
  130. for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
  131. if (cfg == specials_idx[i])
  132. return specials[i];
  133. }
  134. return "UNKNOWN";
  135. }
  136. EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
  137. /**
  138. * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
  139. * @cfg: pin default config value
  140. *
  141. * Parse the pin default config value and returns the string of the
  142. * jack connectivity, i.e. external or internal connection.
  143. */
  144. const char *snd_hda_get_jack_connectivity(u32 cfg)
  145. {
  146. static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
  147. return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
  148. }
  149. EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
  150. /**
  151. * snd_hda_get_jack_type - Give a type string of the jack
  152. * @cfg: pin default config value
  153. *
  154. * Parse the pin default config value and returns the string of the
  155. * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
  156. */
  157. const char *snd_hda_get_jack_type(u32 cfg)
  158. {
  159. static char *jack_types[16] = {
  160. "Line Out", "Speaker", "HP Out", "CD",
  161. "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
  162. "Line In", "Aux", "Mic", "Telephony",
  163. "SPDIF In", "Digital In", "Reserved", "Other"
  164. };
  165. return jack_types[(cfg & AC_DEFCFG_DEVICE)
  166. >> AC_DEFCFG_DEVICE_SHIFT];
  167. }
  168. EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
  169. /*
  170. * Compose a 32bit command word to be sent to the HD-audio controller
  171. */
  172. static inline unsigned int
  173. make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags,
  174. unsigned int verb, unsigned int parm)
  175. {
  176. u32 val;
  177. if ((codec->addr & ~0xf) || (nid & ~0x7f) ||
  178. (verb & ~0xfff) || (parm & ~0xffff)) {
  179. printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x\n",
  180. codec->addr, nid, verb, parm);
  181. return ~0;
  182. }
  183. val = (u32)codec->addr << 28;
  184. val |= (u32)nid << 20;
  185. val |= verb << 8;
  186. val |= parm;
  187. return val;
  188. }
  189. /*
  190. * Send and receive a verb
  191. */
  192. static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
  193. int flags, unsigned int *res)
  194. {
  195. struct hda_bus *bus = codec->bus;
  196. int err;
  197. if (cmd == ~0)
  198. return -1;
  199. if (res)
  200. *res = -1;
  201. again:
  202. snd_hda_power_up(codec);
  203. mutex_lock(&bus->cmd_mutex);
  204. if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
  205. bus->no_response_fallback = 1;
  206. for (;;) {
  207. trace_hda_send_cmd(codec, cmd);
  208. err = bus->ops.command(bus, cmd);
  209. if (err != -EAGAIN)
  210. break;
  211. /* process pending verbs */
  212. bus->ops.get_response(bus, codec->addr);
  213. }
  214. if (!err && res) {
  215. *res = bus->ops.get_response(bus, codec->addr);
  216. trace_hda_get_response(codec, *res);
  217. }
  218. bus->no_response_fallback = 0;
  219. mutex_unlock(&bus->cmd_mutex);
  220. snd_hda_power_down(codec);
  221. if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) {
  222. if (bus->response_reset) {
  223. snd_printd("hda_codec: resetting BUS due to "
  224. "fatal communication error\n");
  225. trace_hda_bus_reset(bus);
  226. bus->ops.bus_reset(bus);
  227. }
  228. goto again;
  229. }
  230. /* clear reset-flag when the communication gets recovered */
  231. if (!err || codec_in_pm(codec))
  232. bus->response_reset = 0;
  233. return err;
  234. }
  235. /**
  236. * snd_hda_codec_read - send a command and get the response
  237. * @codec: the HDA codec
  238. * @nid: NID to send the command
  239. * @flags: optional bit flags
  240. * @verb: the verb to send
  241. * @parm: the parameter for the verb
  242. *
  243. * Send a single command and read the corresponding response.
  244. *
  245. * Returns the obtained response value, or -1 for an error.
  246. */
  247. unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
  248. int flags,
  249. unsigned int verb, unsigned int parm)
  250. {
  251. unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  252. unsigned int res;
  253. if (codec_exec_verb(codec, cmd, flags, &res))
  254. return -1;
  255. return res;
  256. }
  257. EXPORT_SYMBOL_HDA(snd_hda_codec_read);
  258. /**
  259. * snd_hda_codec_write - send a single command without waiting for response
  260. * @codec: the HDA codec
  261. * @nid: NID to send the command
  262. * @flags: optional bit flags
  263. * @verb: the verb to send
  264. * @parm: the parameter for the verb
  265. *
  266. * Send a single command without waiting for response.
  267. *
  268. * Returns 0 if successful, or a negative error code.
  269. */
  270. int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags,
  271. unsigned int verb, unsigned int parm)
  272. {
  273. unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm);
  274. unsigned int res;
  275. return codec_exec_verb(codec, cmd, flags,
  276. codec->bus->sync_write ? &res : NULL);
  277. }
  278. EXPORT_SYMBOL_HDA(snd_hda_codec_write);
  279. /**
  280. * snd_hda_sequence_write - sequence writes
  281. * @codec: the HDA codec
  282. * @seq: VERB array to send
  283. *
  284. * Send the commands sequentially from the given array.
  285. * The array must be terminated with NID=0.
  286. */
  287. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  288. {
  289. for (; seq->nid; seq++)
  290. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  291. }
  292. EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
  293. /**
  294. * snd_hda_get_sub_nodes - get the range of sub nodes
  295. * @codec: the HDA codec
  296. * @nid: NID to parse
  297. * @start_id: the pointer to store the start NID
  298. *
  299. * Parse the NID and store the start NID of its sub-nodes.
  300. * Returns the number of sub-nodes.
  301. */
  302. int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
  303. hda_nid_t *start_id)
  304. {
  305. unsigned int parm;
  306. parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
  307. if (parm == -1)
  308. return 0;
  309. *start_id = (parm >> 16) & 0x7fff;
  310. return (int)(parm & 0x7fff);
  311. }
  312. EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
  313. /* connection list element */
  314. struct hda_conn_list {
  315. struct list_head list;
  316. int len;
  317. hda_nid_t nid;
  318. hda_nid_t conns[0];
  319. };
  320. /* look up the cached results */
  321. static struct hda_conn_list *
  322. lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
  323. {
  324. struct hda_conn_list *p;
  325. list_for_each_entry(p, &codec->conn_list, list) {
  326. if (p->nid == nid)
  327. return p;
  328. }
  329. return NULL;
  330. }
  331. static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  332. const hda_nid_t *list)
  333. {
  334. struct hda_conn_list *p;
  335. p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL);
  336. if (!p)
  337. return -ENOMEM;
  338. p->len = len;
  339. p->nid = nid;
  340. memcpy(p->conns, list, len * sizeof(hda_nid_t));
  341. list_add(&p->list, &codec->conn_list);
  342. return 0;
  343. }
  344. static void remove_conn_list(struct hda_codec *codec)
  345. {
  346. while (!list_empty(&codec->conn_list)) {
  347. struct hda_conn_list *p;
  348. p = list_first_entry(&codec->conn_list, typeof(*p), list);
  349. list_del(&p->list);
  350. kfree(p);
  351. }
  352. }
  353. /* read the connection and add to the cache */
  354. static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  355. {
  356. hda_nid_t list[32];
  357. hda_nid_t *result = list;
  358. int len;
  359. len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
  360. if (len == -ENOSPC) {
  361. len = snd_hda_get_num_raw_conns(codec, nid);
  362. result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL);
  363. if (!result)
  364. return -ENOMEM;
  365. len = snd_hda_get_raw_connections(codec, nid, result, len);
  366. }
  367. if (len >= 0)
  368. len = snd_hda_override_conn_list(codec, nid, len, result);
  369. if (result != list)
  370. kfree(result);
  371. return len;
  372. }
  373. /**
  374. * snd_hda_get_conn_list - get connection list
  375. * @codec: the HDA codec
  376. * @nid: NID to parse
  377. * @len: number of connection list entries
  378. * @listp: the pointer to store NID list
  379. *
  380. * Parses the connection list of the given widget and stores the pointer
  381. * to the list of NIDs.
  382. *
  383. * Returns the number of connections, or a negative error code.
  384. *
  385. * Note that the returned pointer isn't protected against the list
  386. * modification. If snd_hda_override_conn_list() might be called
  387. * concurrently, protect with a mutex appropriately.
  388. */
  389. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  390. const hda_nid_t **listp)
  391. {
  392. bool added = false;
  393. for (;;) {
  394. int err;
  395. const struct hda_conn_list *p;
  396. /* if the connection-list is already cached, read it */
  397. p = lookup_conn_list(codec, nid);
  398. if (p) {
  399. if (listp)
  400. *listp = p->conns;
  401. return p->len;
  402. }
  403. if (snd_BUG_ON(added))
  404. return -EINVAL;
  405. err = read_and_add_raw_conns(codec, nid);
  406. if (err < 0)
  407. return err;
  408. added = true;
  409. }
  410. }
  411. EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
  412. /**
  413. * snd_hda_get_connections - copy connection list
  414. * @codec: the HDA codec
  415. * @nid: NID to parse
  416. * @conn_list: connection list array; when NULL, checks only the size
  417. * @max_conns: max. number of connections to store
  418. *
  419. * Parses the connection list of the given widget and stores the list
  420. * of NIDs.
  421. *
  422. * Returns the number of connections, or a negative error code.
  423. */
  424. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  425. hda_nid_t *conn_list, int max_conns)
  426. {
  427. const hda_nid_t *list;
  428. int len = snd_hda_get_conn_list(codec, nid, &list);
  429. if (len > 0 && conn_list) {
  430. if (len > max_conns) {
  431. snd_printk(KERN_ERR "hda_codec: "
  432. "Too many connections %d for NID 0x%x\n",
  433. len, nid);
  434. return -EINVAL;
  435. }
  436. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  437. }
  438. return len;
  439. }
  440. EXPORT_SYMBOL_HDA(snd_hda_get_connections);
  441. /* return CONNLIST_LEN parameter of the given widget */
  442. static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid)
  443. {
  444. unsigned int wcaps = get_wcaps(codec, nid);
  445. unsigned int parm;
  446. if (!(wcaps & AC_WCAP_CONN_LIST) &&
  447. get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
  448. return 0;
  449. parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
  450. if (parm == -1)
  451. parm = 0;
  452. return parm;
  453. }
  454. int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  455. {
  456. return snd_hda_get_raw_connections(codec, nid, NULL, 0);
  457. }
  458. /**
  459. * snd_hda_get_raw_connections - copy connection list without cache
  460. * @codec: the HDA codec
  461. * @nid: NID to parse
  462. * @conn_list: connection list array
  463. * @max_conns: max. number of connections to store
  464. *
  465. * Like snd_hda_get_connections(), copy the connection list but without
  466. * checking through the connection-list cache.
  467. * Currently called only from hda_proc.c, so not exported.
  468. */
  469. int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
  470. hda_nid_t *conn_list, int max_conns)
  471. {
  472. unsigned int parm;
  473. int i, conn_len, conns;
  474. unsigned int shift, num_elems, mask;
  475. hda_nid_t prev_nid;
  476. int null_count = 0;
  477. parm = get_num_conns(codec, nid);
  478. if (!parm)
  479. return 0;
  480. if (parm & AC_CLIST_LONG) {
  481. /* long form */
  482. shift = 16;
  483. num_elems = 2;
  484. } else {
  485. /* short form */
  486. shift = 8;
  487. num_elems = 4;
  488. }
  489. conn_len = parm & AC_CLIST_LENGTH;
  490. mask = (1 << (shift-1)) - 1;
  491. if (!conn_len)
  492. return 0; /* no connection */
  493. if (conn_len == 1) {
  494. /* single connection */
  495. parm = snd_hda_codec_read(codec, nid, 0,
  496. AC_VERB_GET_CONNECT_LIST, 0);
  497. if (parm == -1 && codec->bus->rirb_error)
  498. return -EIO;
  499. if (conn_list)
  500. conn_list[0] = parm & mask;
  501. return 1;
  502. }
  503. /* multi connection */
  504. conns = 0;
  505. prev_nid = 0;
  506. for (i = 0; i < conn_len; i++) {
  507. int range_val;
  508. hda_nid_t val, n;
  509. if (i % num_elems == 0) {
  510. parm = snd_hda_codec_read(codec, nid, 0,
  511. AC_VERB_GET_CONNECT_LIST, i);
  512. if (parm == -1 && codec->bus->rirb_error)
  513. return -EIO;
  514. }
  515. range_val = !!(parm & (1 << (shift-1))); /* ranges */
  516. val = parm & mask;
  517. if (val == 0 && null_count++) { /* no second chance */
  518. snd_printk(KERN_WARNING "hda_codec: "
  519. "invalid CONNECT_LIST verb %x[%i]:%x\n",
  520. nid, i, parm);
  521. return 0;
  522. }
  523. parm >>= shift;
  524. if (range_val) {
  525. /* ranges between the previous and this one */
  526. if (!prev_nid || prev_nid >= val) {
  527. snd_printk(KERN_WARNING "hda_codec: "
  528. "invalid dep_range_val %x:%x\n",
  529. prev_nid, val);
  530. continue;
  531. }
  532. for (n = prev_nid + 1; n <= val; n++) {
  533. if (conn_list) {
  534. if (conns >= max_conns)
  535. return -ENOSPC;
  536. conn_list[conns] = n;
  537. }
  538. conns++;
  539. }
  540. } else {
  541. if (conn_list) {
  542. if (conns >= max_conns)
  543. return -ENOSPC;
  544. conn_list[conns] = val;
  545. }
  546. conns++;
  547. }
  548. prev_nid = val;
  549. }
  550. return conns;
  551. }
  552. /**
  553. * snd_hda_override_conn_list - add/modify the connection-list to cache
  554. * @codec: the HDA codec
  555. * @nid: NID to parse
  556. * @len: number of connection list entries
  557. * @list: the list of connection entries
  558. *
  559. * Add or modify the given connection-list to the cache. If the corresponding
  560. * cache already exists, invalidate it and append a new one.
  561. *
  562. * Returns zero or a negative error code.
  563. */
  564. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  565. const hda_nid_t *list)
  566. {
  567. struct hda_conn_list *p;
  568. p = lookup_conn_list(codec, nid);
  569. if (p) {
  570. list_del(&p->list);
  571. kfree(p);
  572. }
  573. return add_conn_list(codec, nid, len, list);
  574. }
  575. EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
  576. /**
  577. * snd_hda_get_conn_index - get the connection index of the given NID
  578. * @codec: the HDA codec
  579. * @mux: NID containing the list
  580. * @nid: NID to select
  581. * @recursive: 1 when searching NID recursively, otherwise 0
  582. *
  583. * Parses the connection list of the widget @mux and checks whether the
  584. * widget @nid is present. If it is, return the connection index.
  585. * Otherwise it returns -1.
  586. */
  587. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  588. hda_nid_t nid, int recursive)
  589. {
  590. const hda_nid_t *conn;
  591. int i, nums;
  592. nums = snd_hda_get_conn_list(codec, mux, &conn);
  593. for (i = 0; i < nums; i++)
  594. if (conn[i] == nid)
  595. return i;
  596. if (!recursive)
  597. return -1;
  598. if (recursive > 10) {
  599. snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
  600. return -1;
  601. }
  602. recursive++;
  603. for (i = 0; i < nums; i++) {
  604. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  605. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  606. continue;
  607. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  608. return i;
  609. }
  610. return -1;
  611. }
  612. EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
  613. /**
  614. * snd_hda_queue_unsol_event - add an unsolicited event to queue
  615. * @bus: the BUS
  616. * @res: unsolicited event (lower 32bit of RIRB entry)
  617. * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
  618. *
  619. * Adds the given event to the queue. The events are processed in
  620. * the workqueue asynchronously. Call this function in the interrupt
  621. * hanlder when RIRB receives an unsolicited event.
  622. *
  623. * Returns 0 if successful, or a negative error code.
  624. */
  625. int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
  626. {
  627. struct hda_bus_unsolicited *unsol;
  628. unsigned int wp;
  629. if (!bus || !bus->workq)
  630. return 0;
  631. trace_hda_unsol_event(bus, res, res_ex);
  632. unsol = bus->unsol;
  633. if (!unsol)
  634. return 0;
  635. wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
  636. unsol->wp = wp;
  637. wp <<= 1;
  638. unsol->queue[wp] = res;
  639. unsol->queue[wp + 1] = res_ex;
  640. queue_work(bus->workq, &unsol->work);
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
  644. /*
  645. * process queued unsolicited events
  646. */
  647. static void process_unsol_events(struct work_struct *work)
  648. {
  649. struct hda_bus_unsolicited *unsol =
  650. container_of(work, struct hda_bus_unsolicited, work);
  651. struct hda_bus *bus = unsol->bus;
  652. struct hda_codec *codec;
  653. unsigned int rp, caddr, res;
  654. while (unsol->rp != unsol->wp) {
  655. rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
  656. unsol->rp = rp;
  657. rp <<= 1;
  658. res = unsol->queue[rp];
  659. caddr = unsol->queue[rp + 1];
  660. if (!(caddr & (1 << 4))) /* no unsolicited event? */
  661. continue;
  662. codec = bus->caddr_tbl[caddr & 0x0f];
  663. if (codec && codec->patch_ops.unsol_event)
  664. codec->patch_ops.unsol_event(codec, res);
  665. }
  666. }
  667. /*
  668. * initialize unsolicited queue
  669. */
  670. static int init_unsol_queue(struct hda_bus *bus)
  671. {
  672. struct hda_bus_unsolicited *unsol;
  673. if (bus->unsol) /* already initialized */
  674. return 0;
  675. unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
  676. if (!unsol) {
  677. snd_printk(KERN_ERR "hda_codec: "
  678. "can't allocate unsolicited queue\n");
  679. return -ENOMEM;
  680. }
  681. INIT_WORK(&unsol->work, process_unsol_events);
  682. unsol->bus = bus;
  683. bus->unsol = unsol;
  684. return 0;
  685. }
  686. /*
  687. * destructor
  688. */
  689. static void snd_hda_codec_free(struct hda_codec *codec);
  690. static int snd_hda_bus_free(struct hda_bus *bus)
  691. {
  692. struct hda_codec *codec, *n;
  693. if (!bus)
  694. return 0;
  695. if (bus->workq)
  696. flush_workqueue(bus->workq);
  697. if (bus->unsol)
  698. kfree(bus->unsol);
  699. list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
  700. snd_hda_codec_free(codec);
  701. }
  702. if (bus->ops.private_free)
  703. bus->ops.private_free(bus);
  704. if (bus->workq)
  705. destroy_workqueue(bus->workq);
  706. kfree(bus);
  707. return 0;
  708. }
  709. static int snd_hda_bus_dev_free(struct snd_device *device)
  710. {
  711. struct hda_bus *bus = device->device_data;
  712. bus->shutdown = 1;
  713. return snd_hda_bus_free(bus);
  714. }
  715. #ifdef CONFIG_SND_HDA_HWDEP
  716. static int snd_hda_bus_dev_register(struct snd_device *device)
  717. {
  718. struct hda_bus *bus = device->device_data;
  719. struct hda_codec *codec;
  720. list_for_each_entry(codec, &bus->codec_list, list) {
  721. snd_hda_hwdep_add_sysfs(codec);
  722. snd_hda_hwdep_add_power_sysfs(codec);
  723. }
  724. return 0;
  725. }
  726. #else
  727. #define snd_hda_bus_dev_register NULL
  728. #endif
  729. /**
  730. * snd_hda_bus_new - create a HDA bus
  731. * @card: the card entry
  732. * @temp: the template for hda_bus information
  733. * @busp: the pointer to store the created bus instance
  734. *
  735. * Returns 0 if successful, or a negative error code.
  736. */
  737. int snd_hda_bus_new(struct snd_card *card,
  738. const struct hda_bus_template *temp,
  739. struct hda_bus **busp)
  740. {
  741. struct hda_bus *bus;
  742. int err;
  743. static struct snd_device_ops dev_ops = {
  744. .dev_register = snd_hda_bus_dev_register,
  745. .dev_free = snd_hda_bus_dev_free,
  746. };
  747. if (snd_BUG_ON(!temp))
  748. return -EINVAL;
  749. if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
  750. return -EINVAL;
  751. if (busp)
  752. *busp = NULL;
  753. bus = kzalloc(sizeof(*bus), GFP_KERNEL);
  754. if (bus == NULL) {
  755. snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
  756. return -ENOMEM;
  757. }
  758. bus->card = card;
  759. bus->private_data = temp->private_data;
  760. bus->pci = temp->pci;
  761. bus->modelname = temp->modelname;
  762. bus->power_save = temp->power_save;
  763. bus->ops = temp->ops;
  764. mutex_init(&bus->cmd_mutex);
  765. mutex_init(&bus->prepare_mutex);
  766. INIT_LIST_HEAD(&bus->codec_list);
  767. snprintf(bus->workq_name, sizeof(bus->workq_name),
  768. "hd-audio%d", card->number);
  769. bus->workq = create_singlethread_workqueue(bus->workq_name);
  770. if (!bus->workq) {
  771. snd_printk(KERN_ERR "cannot create workqueue %s\n",
  772. bus->workq_name);
  773. kfree(bus);
  774. return -ENOMEM;
  775. }
  776. err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
  777. if (err < 0) {
  778. snd_hda_bus_free(bus);
  779. return err;
  780. }
  781. if (busp)
  782. *busp = bus;
  783. return 0;
  784. }
  785. EXPORT_SYMBOL_HDA(snd_hda_bus_new);
  786. #ifdef CONFIG_SND_HDA_GENERIC
  787. #define is_generic_config(codec) \
  788. (codec->modelname && !strcmp(codec->modelname, "generic"))
  789. #else
  790. #define is_generic_config(codec) 0
  791. #endif
  792. #ifdef MODULE
  793. #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */
  794. #else
  795. #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */
  796. #endif
  797. /*
  798. * find a matching codec preset
  799. */
  800. static const struct hda_codec_preset *
  801. find_codec_preset(struct hda_codec *codec)
  802. {
  803. struct hda_codec_preset_list *tbl;
  804. const struct hda_codec_preset *preset;
  805. unsigned int mod_requested = 0;
  806. if (is_generic_config(codec))
  807. return NULL; /* use the generic parser */
  808. again:
  809. mutex_lock(&preset_mutex);
  810. list_for_each_entry(tbl, &hda_preset_tables, list) {
  811. if (!try_module_get(tbl->owner)) {
  812. snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
  813. continue;
  814. }
  815. for (preset = tbl->preset; preset->id; preset++) {
  816. u32 mask = preset->mask;
  817. if (preset->afg && preset->afg != codec->afg)
  818. continue;
  819. if (preset->mfg && preset->mfg != codec->mfg)
  820. continue;
  821. if (!mask)
  822. mask = ~0;
  823. if (preset->id == (codec->vendor_id & mask) &&
  824. (!preset->rev ||
  825. preset->rev == codec->revision_id)) {
  826. mutex_unlock(&preset_mutex);
  827. codec->owner = tbl->owner;
  828. return preset;
  829. }
  830. }
  831. module_put(tbl->owner);
  832. }
  833. mutex_unlock(&preset_mutex);
  834. if (mod_requested < HDA_MODREQ_MAX_COUNT) {
  835. char name[32];
  836. if (!mod_requested)
  837. snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
  838. codec->vendor_id);
  839. else
  840. snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
  841. (codec->vendor_id >> 16) & 0xffff);
  842. request_module(name);
  843. mod_requested++;
  844. goto again;
  845. }
  846. return NULL;
  847. }
  848. /*
  849. * get_codec_name - store the codec name
  850. */
  851. static int get_codec_name(struct hda_codec *codec)
  852. {
  853. const struct hda_vendor_id *c;
  854. const char *vendor = NULL;
  855. u16 vendor_id = codec->vendor_id >> 16;
  856. char tmp[16];
  857. if (codec->vendor_name)
  858. goto get_chip_name;
  859. for (c = hda_vendor_ids; c->id; c++) {
  860. if (c->id == vendor_id) {
  861. vendor = c->name;
  862. break;
  863. }
  864. }
  865. if (!vendor) {
  866. sprintf(tmp, "Generic %04x", vendor_id);
  867. vendor = tmp;
  868. }
  869. codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
  870. if (!codec->vendor_name)
  871. return -ENOMEM;
  872. get_chip_name:
  873. if (codec->chip_name)
  874. return 0;
  875. if (codec->preset && codec->preset->name)
  876. codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
  877. else {
  878. sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
  879. codec->chip_name = kstrdup(tmp, GFP_KERNEL);
  880. }
  881. if (!codec->chip_name)
  882. return -ENOMEM;
  883. return 0;
  884. }
  885. /*
  886. * look for an AFG and MFG nodes
  887. */
  888. static void setup_fg_nodes(struct hda_codec *codec)
  889. {
  890. int i, total_nodes, function_id;
  891. hda_nid_t nid;
  892. total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
  893. for (i = 0; i < total_nodes; i++, nid++) {
  894. function_id = snd_hda_param_read(codec, nid,
  895. AC_PAR_FUNCTION_TYPE);
  896. switch (function_id & 0xff) {
  897. case AC_GRP_AUDIO_FUNCTION:
  898. codec->afg = nid;
  899. codec->afg_function_id = function_id & 0xff;
  900. codec->afg_unsol = (function_id >> 8) & 1;
  901. break;
  902. case AC_GRP_MODEM_FUNCTION:
  903. codec->mfg = nid;
  904. codec->mfg_function_id = function_id & 0xff;
  905. codec->mfg_unsol = (function_id >> 8) & 1;
  906. break;
  907. default:
  908. break;
  909. }
  910. }
  911. }
  912. /*
  913. * read widget caps for each widget and store in cache
  914. */
  915. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  916. {
  917. int i;
  918. hda_nid_t nid;
  919. codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
  920. &codec->start_nid);
  921. codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
  922. if (!codec->wcaps)
  923. return -ENOMEM;
  924. nid = codec->start_nid;
  925. for (i = 0; i < codec->num_nodes; i++, nid++)
  926. codec->wcaps[i] = snd_hda_param_read(codec, nid,
  927. AC_PAR_AUDIO_WIDGET_CAP);
  928. return 0;
  929. }
  930. /* read all pin default configurations and save codec->init_pins */
  931. static int read_pin_defaults(struct hda_codec *codec)
  932. {
  933. int i;
  934. hda_nid_t nid = codec->start_nid;
  935. for (i = 0; i < codec->num_nodes; i++, nid++) {
  936. struct hda_pincfg *pin;
  937. unsigned int wcaps = get_wcaps(codec, nid);
  938. unsigned int wid_type = get_wcaps_type(wcaps);
  939. if (wid_type != AC_WID_PIN)
  940. continue;
  941. pin = snd_array_new(&codec->init_pins);
  942. if (!pin)
  943. return -ENOMEM;
  944. pin->nid = nid;
  945. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  946. AC_VERB_GET_CONFIG_DEFAULT, 0);
  947. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  948. AC_VERB_GET_PIN_WIDGET_CONTROL,
  949. 0);
  950. }
  951. return 0;
  952. }
  953. /* look up the given pin config list and return the item matching with NID */
  954. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  955. struct snd_array *array,
  956. hda_nid_t nid)
  957. {
  958. int i;
  959. for (i = 0; i < array->used; i++) {
  960. struct hda_pincfg *pin = snd_array_elem(array, i);
  961. if (pin->nid == nid)
  962. return pin;
  963. }
  964. return NULL;
  965. }
  966. /* set the current pin config value for the given NID.
  967. * the value is cached, and read via snd_hda_codec_get_pincfg()
  968. */
  969. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  970. hda_nid_t nid, unsigned int cfg)
  971. {
  972. struct hda_pincfg *pin;
  973. /* the check below may be invalid when pins are added by a fixup
  974. * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled
  975. * for now
  976. */
  977. /*
  978. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
  979. return -EINVAL;
  980. */
  981. pin = look_up_pincfg(codec, list, nid);
  982. if (!pin) {
  983. pin = snd_array_new(list);
  984. if (!pin)
  985. return -ENOMEM;
  986. pin->nid = nid;
  987. }
  988. pin->cfg = cfg;
  989. return 0;
  990. }
  991. /**
  992. * snd_hda_codec_set_pincfg - Override a pin default configuration
  993. * @codec: the HDA codec
  994. * @nid: NID to set the pin config
  995. * @cfg: the pin default config value
  996. *
  997. * Override a pin default configuration value in the cache.
  998. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  999. * priority than the real hardware value.
  1000. */
  1001. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  1002. hda_nid_t nid, unsigned int cfg)
  1003. {
  1004. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  1005. }
  1006. EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
  1007. /**
  1008. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  1009. * @codec: the HDA codec
  1010. * @nid: NID to get the pin config
  1011. *
  1012. * Get the current pin config value of the given pin NID.
  1013. * If the pincfg value is cached or overridden via sysfs or driver,
  1014. * returns the cached value.
  1015. */
  1016. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  1017. {
  1018. struct hda_pincfg *pin;
  1019. #ifdef CONFIG_SND_HDA_HWDEP
  1020. {
  1021. unsigned int cfg = 0;
  1022. mutex_lock(&codec->user_mutex);
  1023. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  1024. if (pin)
  1025. cfg = pin->cfg;
  1026. mutex_unlock(&codec->user_mutex);
  1027. if (cfg)
  1028. return cfg;
  1029. }
  1030. #endif
  1031. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  1032. if (pin)
  1033. return pin->cfg;
  1034. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1035. if (pin)
  1036. return pin->cfg;
  1037. return 0;
  1038. }
  1039. EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
  1040. /* remember the current pinctl target value */
  1041. int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
  1042. unsigned int val)
  1043. {
  1044. struct hda_pincfg *pin;
  1045. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1046. if (!pin)
  1047. return -EINVAL;
  1048. pin->target = val;
  1049. return 0;
  1050. }
  1051. EXPORT_SYMBOL_HDA(snd_hda_codec_set_pin_target);
  1052. /* return the current pinctl target value */
  1053. int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
  1054. {
  1055. struct hda_pincfg *pin;
  1056. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  1057. if (!pin)
  1058. return 0;
  1059. return pin->target;
  1060. }
  1061. EXPORT_SYMBOL_HDA(snd_hda_codec_get_pin_target);
  1062. /**
  1063. * snd_hda_shutup_pins - Shut up all pins
  1064. * @codec: the HDA codec
  1065. *
  1066. * Clear all pin controls to shup up before suspend for avoiding click noise.
  1067. * The controls aren't cached so that they can be resumed properly.
  1068. */
  1069. void snd_hda_shutup_pins(struct hda_codec *codec)
  1070. {
  1071. int i;
  1072. /* don't shut up pins when unloading the driver; otherwise it breaks
  1073. * the default pin setup at the next load of the driver
  1074. */
  1075. if (codec->bus->shutdown)
  1076. return;
  1077. for (i = 0; i < codec->init_pins.used; i++) {
  1078. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1079. /* use read here for syncing after issuing each verb */
  1080. snd_hda_codec_read(codec, pin->nid, 0,
  1081. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  1082. }
  1083. codec->pins_shutup = 1;
  1084. }
  1085. EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
  1086. #ifdef CONFIG_PM
  1087. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  1088. static void restore_shutup_pins(struct hda_codec *codec)
  1089. {
  1090. int i;
  1091. if (!codec->pins_shutup)
  1092. return;
  1093. if (codec->bus->shutdown)
  1094. return;
  1095. for (i = 0; i < codec->init_pins.used; i++) {
  1096. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  1097. snd_hda_codec_write(codec, pin->nid, 0,
  1098. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1099. pin->ctrl);
  1100. }
  1101. codec->pins_shutup = 0;
  1102. }
  1103. #endif
  1104. static void hda_jackpoll_work(struct work_struct *work)
  1105. {
  1106. struct hda_codec *codec =
  1107. container_of(work, struct hda_codec, jackpoll_work.work);
  1108. if (!codec->jackpoll_interval)
  1109. return;
  1110. snd_hda_jack_set_dirty_all(codec);
  1111. snd_hda_jack_poll_all(codec);
  1112. queue_delayed_work(codec->bus->workq, &codec->jackpoll_work,
  1113. codec->jackpoll_interval);
  1114. }
  1115. static void init_hda_cache(struct hda_cache_rec *cache,
  1116. unsigned int record_size);
  1117. static void free_hda_cache(struct hda_cache_rec *cache);
  1118. /* release all pincfg lists */
  1119. static void free_init_pincfgs(struct hda_codec *codec)
  1120. {
  1121. snd_array_free(&codec->driver_pins);
  1122. #ifdef CONFIG_SND_HDA_HWDEP
  1123. snd_array_free(&codec->user_pins);
  1124. #endif
  1125. snd_array_free(&codec->init_pins);
  1126. }
  1127. /*
  1128. * audio-converter setup caches
  1129. */
  1130. struct hda_cvt_setup {
  1131. hda_nid_t nid;
  1132. u8 stream_tag;
  1133. u8 channel_id;
  1134. u16 format_id;
  1135. unsigned char active; /* cvt is currently used */
  1136. unsigned char dirty; /* setups should be cleared */
  1137. };
  1138. /* get or create a cache entry for the given audio converter NID */
  1139. static struct hda_cvt_setup *
  1140. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  1141. {
  1142. struct hda_cvt_setup *p;
  1143. int i;
  1144. for (i = 0; i < codec->cvt_setups.used; i++) {
  1145. p = snd_array_elem(&codec->cvt_setups, i);
  1146. if (p->nid == nid)
  1147. return p;
  1148. }
  1149. p = snd_array_new(&codec->cvt_setups);
  1150. if (p)
  1151. p->nid = nid;
  1152. return p;
  1153. }
  1154. /*
  1155. * codec destructor
  1156. */
  1157. static void snd_hda_codec_free(struct hda_codec *codec)
  1158. {
  1159. if (!codec)
  1160. return;
  1161. cancel_delayed_work_sync(&codec->jackpoll_work);
  1162. snd_hda_jack_tbl_clear(codec);
  1163. free_init_pincfgs(codec);
  1164. #ifdef CONFIG_PM
  1165. cancel_delayed_work(&codec->power_work);
  1166. flush_workqueue(codec->bus->workq);
  1167. #endif
  1168. list_del(&codec->list);
  1169. snd_array_free(&codec->mixers);
  1170. snd_array_free(&codec->nids);
  1171. snd_array_free(&codec->cvt_setups);
  1172. snd_array_free(&codec->spdif_out);
  1173. remove_conn_list(codec);
  1174. codec->bus->caddr_tbl[codec->addr] = NULL;
  1175. if (codec->patch_ops.free)
  1176. codec->patch_ops.free(codec);
  1177. #ifdef CONFIG_PM
  1178. if (!codec->pm_down_notified) /* cancel leftover refcounts */
  1179. hda_call_pm_notify(codec->bus, false);
  1180. #endif
  1181. module_put(codec->owner);
  1182. free_hda_cache(&codec->amp_cache);
  1183. free_hda_cache(&codec->cmd_cache);
  1184. kfree(codec->vendor_name);
  1185. kfree(codec->chip_name);
  1186. kfree(codec->modelname);
  1187. kfree(codec->wcaps);
  1188. kfree(codec);
  1189. }
  1190. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
  1191. hda_nid_t fg, unsigned int power_state);
  1192. static unsigned int hda_set_power_state(struct hda_codec *codec,
  1193. unsigned int power_state);
  1194. /**
  1195. * snd_hda_codec_new - create a HDA codec
  1196. * @bus: the bus to assign
  1197. * @codec_addr: the codec address
  1198. * @codecp: the pointer to store the generated codec
  1199. *
  1200. * Returns 0 if successful, or a negative error code.
  1201. */
  1202. int snd_hda_codec_new(struct hda_bus *bus,
  1203. unsigned int codec_addr,
  1204. struct hda_codec **codecp)
  1205. {
  1206. struct hda_codec *codec;
  1207. char component[31];
  1208. hda_nid_t fg;
  1209. int err;
  1210. if (snd_BUG_ON(!bus))
  1211. return -EINVAL;
  1212. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  1213. return -EINVAL;
  1214. if (bus->caddr_tbl[codec_addr]) {
  1215. snd_printk(KERN_ERR "hda_codec: "
  1216. "address 0x%x is already occupied\n", codec_addr);
  1217. return -EBUSY;
  1218. }
  1219. codec = kzalloc(sizeof(*codec), GFP_KERNEL);
  1220. if (codec == NULL) {
  1221. snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
  1222. return -ENOMEM;
  1223. }
  1224. codec->bus = bus;
  1225. codec->addr = codec_addr;
  1226. mutex_init(&codec->spdif_mutex);
  1227. mutex_init(&codec->control_mutex);
  1228. mutex_init(&codec->hash_mutex);
  1229. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  1230. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  1231. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  1232. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  1233. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  1234. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  1235. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  1236. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  1237. snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
  1238. snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
  1239. INIT_LIST_HEAD(&codec->conn_list);
  1240. INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
  1241. #ifdef CONFIG_PM
  1242. spin_lock_init(&codec->power_lock);
  1243. INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
  1244. /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
  1245. * the caller has to power down appropriatley after initialization
  1246. * phase.
  1247. */
  1248. hda_keep_power_on(codec);
  1249. hda_call_pm_notify(bus, true);
  1250. #endif
  1251. if (codec->bus->modelname) {
  1252. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  1253. if (!codec->modelname) {
  1254. snd_hda_codec_free(codec);
  1255. return -ENODEV;
  1256. }
  1257. }
  1258. list_add_tail(&codec->list, &bus->codec_list);
  1259. bus->caddr_tbl[codec_addr] = codec;
  1260. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1261. AC_PAR_VENDOR_ID);
  1262. if (codec->vendor_id == -1)
  1263. /* read again, hopefully the access method was corrected
  1264. * in the last read...
  1265. */
  1266. codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1267. AC_PAR_VENDOR_ID);
  1268. codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1269. AC_PAR_SUBSYSTEM_ID);
  1270. codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
  1271. AC_PAR_REV_ID);
  1272. setup_fg_nodes(codec);
  1273. if (!codec->afg && !codec->mfg) {
  1274. snd_printdd("hda_codec: no AFG or MFG node found\n");
  1275. err = -ENODEV;
  1276. goto error;
  1277. }
  1278. fg = codec->afg ? codec->afg : codec->mfg;
  1279. err = read_widget_caps(codec, fg);
  1280. if (err < 0) {
  1281. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  1282. goto error;
  1283. }
  1284. err = read_pin_defaults(codec);
  1285. if (err < 0)
  1286. goto error;
  1287. if (!codec->subsystem_id) {
  1288. codec->subsystem_id =
  1289. snd_hda_codec_read(codec, fg, 0,
  1290. AC_VERB_GET_SUBSYSTEM_ID, 0);
  1291. }
  1292. #ifdef CONFIG_PM
  1293. codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg,
  1294. AC_PWRST_CLKSTOP);
  1295. if (!codec->d3_stop_clk)
  1296. bus->power_keep_link_on = 1;
  1297. #endif
  1298. codec->epss = snd_hda_codec_get_supported_ps(codec, fg,
  1299. AC_PWRST_EPSS);
  1300. /* power-up all before initialization */
  1301. hda_set_power_state(codec, AC_PWRST_D0);
  1302. snd_hda_codec_proc_new(codec);
  1303. snd_hda_create_hwdep(codec);
  1304. sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
  1305. codec->subsystem_id, codec->revision_id);
  1306. snd_component_add(codec->bus->card, component);
  1307. if (codecp)
  1308. *codecp = codec;
  1309. return 0;
  1310. error:
  1311. snd_hda_codec_free(codec);
  1312. return err;
  1313. }
  1314. EXPORT_SYMBOL_HDA(snd_hda_codec_new);
  1315. int snd_hda_codec_update_widgets(struct hda_codec *codec)
  1316. {
  1317. hda_nid_t fg;
  1318. int err;
  1319. /* Assume the function group node does not change,
  1320. * only the widget nodes may change.
  1321. */
  1322. kfree(codec->wcaps);
  1323. fg = codec->afg ? codec->afg : codec->mfg;
  1324. err = read_widget_caps(codec, fg);
  1325. if (err < 0) {
  1326. snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
  1327. return err;
  1328. }
  1329. snd_array_free(&codec->init_pins);
  1330. err = read_pin_defaults(codec);
  1331. return err;
  1332. }
  1333. EXPORT_SYMBOL_HDA(snd_hda_codec_update_widgets);
  1334. /**
  1335. * snd_hda_codec_configure - (Re-)configure the HD-audio codec
  1336. * @codec: the HDA codec
  1337. *
  1338. * Start parsing of the given codec tree and (re-)initialize the whole
  1339. * patch instance.
  1340. *
  1341. * Returns 0 if successful or a negative error code.
  1342. */
  1343. int snd_hda_codec_configure(struct hda_codec *codec)
  1344. {
  1345. int err;
  1346. codec->preset = find_codec_preset(codec);
  1347. if (!codec->vendor_name || !codec->chip_name) {
  1348. err = get_codec_name(codec);
  1349. if (err < 0)
  1350. return err;
  1351. }
  1352. if (is_generic_config(codec)) {
  1353. err = snd_hda_parse_generic_codec(codec);
  1354. goto patched;
  1355. }
  1356. if (codec->preset && codec->preset->patch) {
  1357. err = codec->preset->patch(codec);
  1358. goto patched;
  1359. }
  1360. /* call the default parser */
  1361. err = snd_hda_parse_generic_codec(codec);
  1362. if (err < 0)
  1363. printk(KERN_ERR "hda-codec: No codec parser is available\n");
  1364. patched:
  1365. if (!err && codec->patch_ops.unsol_event)
  1366. err = init_unsol_queue(codec->bus);
  1367. /* audio codec should override the mixer name */
  1368. if (!err && (codec->afg || !*codec->bus->card->mixername))
  1369. snprintf(codec->bus->card->mixername,
  1370. sizeof(codec->bus->card->mixername),
  1371. "%s %s", codec->vendor_name, codec->chip_name);
  1372. return err;
  1373. }
  1374. EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
  1375. /* update the stream-id if changed */
  1376. static void update_pcm_stream_id(struct hda_codec *codec,
  1377. struct hda_cvt_setup *p, hda_nid_t nid,
  1378. u32 stream_tag, int channel_id)
  1379. {
  1380. unsigned int oldval, newval;
  1381. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  1382. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  1383. newval = (stream_tag << 4) | channel_id;
  1384. if (oldval != newval)
  1385. snd_hda_codec_write(codec, nid, 0,
  1386. AC_VERB_SET_CHANNEL_STREAMID,
  1387. newval);
  1388. p->stream_tag = stream_tag;
  1389. p->channel_id = channel_id;
  1390. }
  1391. }
  1392. /* update the format-id if changed */
  1393. static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
  1394. hda_nid_t nid, int format)
  1395. {
  1396. unsigned int oldval;
  1397. if (p->format_id != format) {
  1398. oldval = snd_hda_codec_read(codec, nid, 0,
  1399. AC_VERB_GET_STREAM_FORMAT, 0);
  1400. if (oldval != format) {
  1401. msleep(1);
  1402. snd_hda_codec_write(codec, nid, 0,
  1403. AC_VERB_SET_STREAM_FORMAT,
  1404. format);
  1405. }
  1406. p->format_id = format;
  1407. }
  1408. }
  1409. /**
  1410. * snd_hda_codec_setup_stream - set up the codec for streaming
  1411. * @codec: the CODEC to set up
  1412. * @nid: the NID to set up
  1413. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  1414. * @channel_id: channel id to pass, zero based.
  1415. * @format: stream format.
  1416. */
  1417. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  1418. u32 stream_tag,
  1419. int channel_id, int format)
  1420. {
  1421. struct hda_codec *c;
  1422. struct hda_cvt_setup *p;
  1423. int type;
  1424. int i;
  1425. if (!nid)
  1426. return;
  1427. snd_printdd("hda_codec_setup_stream: "
  1428. "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1429. nid, stream_tag, channel_id, format);
  1430. p = get_hda_cvt_setup(codec, nid);
  1431. if (!p)
  1432. return;
  1433. if (codec->pcm_format_first)
  1434. update_pcm_format(codec, p, nid, format);
  1435. update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
  1436. if (!codec->pcm_format_first)
  1437. update_pcm_format(codec, p, nid, format);
  1438. p->active = 1;
  1439. p->dirty = 0;
  1440. /* make other inactive cvts with the same stream-tag dirty */
  1441. type = get_wcaps_type(get_wcaps(codec, nid));
  1442. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1443. for (i = 0; i < c->cvt_setups.used; i++) {
  1444. p = snd_array_elem(&c->cvt_setups, i);
  1445. if (!p->active && p->stream_tag == stream_tag &&
  1446. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1447. p->dirty = 1;
  1448. }
  1449. }
  1450. }
  1451. EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
  1452. static void really_cleanup_stream(struct hda_codec *codec,
  1453. struct hda_cvt_setup *q);
  1454. /**
  1455. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1456. * @codec: the CODEC to clean up
  1457. * @nid: the NID to clean up
  1458. * @do_now: really clean up the stream instead of clearing the active flag
  1459. */
  1460. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1461. int do_now)
  1462. {
  1463. struct hda_cvt_setup *p;
  1464. if (!nid)
  1465. return;
  1466. if (codec->no_sticky_stream)
  1467. do_now = 1;
  1468. snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1469. p = get_hda_cvt_setup(codec, nid);
  1470. if (p) {
  1471. /* here we just clear the active flag when do_now isn't set;
  1472. * actual clean-ups will be done later in
  1473. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1474. */
  1475. if (do_now)
  1476. really_cleanup_stream(codec, p);
  1477. else
  1478. p->active = 0;
  1479. }
  1480. }
  1481. EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
  1482. static void really_cleanup_stream(struct hda_codec *codec,
  1483. struct hda_cvt_setup *q)
  1484. {
  1485. hda_nid_t nid = q->nid;
  1486. if (q->stream_tag || q->channel_id)
  1487. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1488. if (q->format_id)
  1489. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1490. );
  1491. memset(q, 0, sizeof(*q));
  1492. q->nid = nid;
  1493. }
  1494. /* clean up the all conflicting obsolete streams */
  1495. static void purify_inactive_streams(struct hda_codec *codec)
  1496. {
  1497. struct hda_codec *c;
  1498. int i;
  1499. list_for_each_entry(c, &codec->bus->codec_list, list) {
  1500. for (i = 0; i < c->cvt_setups.used; i++) {
  1501. struct hda_cvt_setup *p;
  1502. p = snd_array_elem(&c->cvt_setups, i);
  1503. if (p->dirty)
  1504. really_cleanup_stream(c, p);
  1505. }
  1506. }
  1507. }
  1508. #ifdef CONFIG_PM
  1509. /* clean up all streams; called from suspend */
  1510. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1511. {
  1512. int i;
  1513. for (i = 0; i < codec->cvt_setups.used; i++) {
  1514. struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
  1515. if (p->stream_tag)
  1516. really_cleanup_stream(codec, p);
  1517. }
  1518. }
  1519. #endif
  1520. /*
  1521. * amp access functions
  1522. */
  1523. /* FIXME: more better hash key? */
  1524. #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
  1525. #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
  1526. #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
  1527. #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
  1528. #define INFO_AMP_CAPS (1<<0)
  1529. #define INFO_AMP_VOL(ch) (1 << (1 + (ch)))
  1530. /* initialize the hash table */
  1531. static void init_hda_cache(struct hda_cache_rec *cache,
  1532. unsigned int record_size)
  1533. {
  1534. memset(cache, 0, sizeof(*cache));
  1535. memset(cache->hash, 0xff, sizeof(cache->hash));
  1536. snd_array_init(&cache->buf, record_size, 64);
  1537. }
  1538. static void free_hda_cache(struct hda_cache_rec *cache)
  1539. {
  1540. snd_array_free(&cache->buf);
  1541. }
  1542. /* query the hash. allocate an entry if not found. */
  1543. static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key)
  1544. {
  1545. u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
  1546. u16 cur = cache->hash[idx];
  1547. struct hda_cache_head *info;
  1548. while (cur != 0xffff) {
  1549. info = snd_array_elem(&cache->buf, cur);
  1550. if (info->key == key)
  1551. return info;
  1552. cur = info->next;
  1553. }
  1554. return NULL;
  1555. }
  1556. /* query the hash. allocate an entry if not found. */
  1557. static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache,
  1558. u32 key)
  1559. {
  1560. struct hda_cache_head *info = get_hash(cache, key);
  1561. if (!info) {
  1562. u16 idx, cur;
  1563. /* add a new hash entry */
  1564. info = snd_array_new(&cache->buf);
  1565. if (!info)
  1566. return NULL;
  1567. cur = snd_array_index(&cache->buf, info);
  1568. info->key = key;
  1569. info->val = 0;
  1570. info->dirty = 0;
  1571. idx = key % (u16)ARRAY_SIZE(cache->hash);
  1572. info->next = cache->hash[idx];
  1573. cache->hash[idx] = cur;
  1574. }
  1575. return info;
  1576. }
  1577. /* query and allocate an amp hash entry */
  1578. static inline struct hda_amp_info *
  1579. get_alloc_amp_hash(struct hda_codec *codec, u32 key)
  1580. {
  1581. return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
  1582. }
  1583. /* overwrite the value with the key in the caps hash */
  1584. static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val)
  1585. {
  1586. struct hda_amp_info *info;
  1587. mutex_lock(&codec->hash_mutex);
  1588. info = get_alloc_amp_hash(codec, key);
  1589. if (!info) {
  1590. mutex_unlock(&codec->hash_mutex);
  1591. return -EINVAL;
  1592. }
  1593. info->amp_caps = val;
  1594. info->head.val |= INFO_AMP_CAPS;
  1595. mutex_unlock(&codec->hash_mutex);
  1596. return 0;
  1597. }
  1598. /* query the value from the caps hash; if not found, fetch the current
  1599. * value from the given function and store in the hash
  1600. */
  1601. static unsigned int
  1602. query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key,
  1603. unsigned int (*func)(struct hda_codec *, hda_nid_t, int))
  1604. {
  1605. struct hda_amp_info *info;
  1606. unsigned int val;
  1607. mutex_lock(&codec->hash_mutex);
  1608. info = get_alloc_amp_hash(codec, key);
  1609. if (!info) {
  1610. mutex_unlock(&codec->hash_mutex);
  1611. return 0;
  1612. }
  1613. if (!(info->head.val & INFO_AMP_CAPS)) {
  1614. mutex_unlock(&codec->hash_mutex); /* for reentrance */
  1615. val = func(codec, nid, dir);
  1616. write_caps_hash(codec, key, val);
  1617. } else {
  1618. val = info->amp_caps;
  1619. mutex_unlock(&codec->hash_mutex);
  1620. }
  1621. return val;
  1622. }
  1623. static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid,
  1624. int direction)
  1625. {
  1626. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1627. nid = codec->afg;
  1628. return snd_hda_param_read(codec, nid,
  1629. direction == HDA_OUTPUT ?
  1630. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  1631. }
  1632. /**
  1633. * query_amp_caps - query AMP capabilities
  1634. * @codec: the HD-auio codec
  1635. * @nid: the NID to query
  1636. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1637. *
  1638. * Query AMP capabilities for the given widget and direction.
  1639. * Returns the obtained capability bits.
  1640. *
  1641. * When cap bits have been already read, this doesn't read again but
  1642. * returns the cached value.
  1643. */
  1644. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1645. {
  1646. return query_caps_hash(codec, nid, direction,
  1647. HDA_HASH_KEY(nid, direction, 0),
  1648. read_amp_cap);
  1649. }
  1650. EXPORT_SYMBOL_HDA(query_amp_caps);
  1651. /**
  1652. * snd_hda_override_amp_caps - Override the AMP capabilities
  1653. * @codec: the CODEC to clean up
  1654. * @nid: the NID to clean up
  1655. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1656. * @caps: the capability bits to set
  1657. *
  1658. * Override the cached AMP caps bits value by the given one.
  1659. * This function is useful if the driver needs to adjust the AMP ranges,
  1660. * e.g. limit to 0dB, etc.
  1661. *
  1662. * Returns zero if successful or a negative error code.
  1663. */
  1664. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1665. unsigned int caps)
  1666. {
  1667. return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps);
  1668. }
  1669. EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
  1670. static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid,
  1671. int dir)
  1672. {
  1673. return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  1674. }
  1675. /**
  1676. * snd_hda_query_pin_caps - Query PIN capabilities
  1677. * @codec: the HD-auio codec
  1678. * @nid: the NID to query
  1679. *
  1680. * Query PIN capabilities for the given widget.
  1681. * Returns the obtained capability bits.
  1682. *
  1683. * When cap bits have been already read, this doesn't read again but
  1684. * returns the cached value.
  1685. */
  1686. u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
  1687. {
  1688. return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid),
  1689. read_pin_cap);
  1690. }
  1691. EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
  1692. /**
  1693. * snd_hda_override_pin_caps - Override the pin capabilities
  1694. * @codec: the CODEC
  1695. * @nid: the NID to override
  1696. * @caps: the capability bits to set
  1697. *
  1698. * Override the cached PIN capabilitiy bits value by the given one.
  1699. *
  1700. * Returns zero if successful or a negative error code.
  1701. */
  1702. int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
  1703. unsigned int caps)
  1704. {
  1705. return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps);
  1706. }
  1707. EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
  1708. /* read or sync the hash value with the current value;
  1709. * call within hash_mutex
  1710. */
  1711. static struct hda_amp_info *
  1712. update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch,
  1713. int direction, int index, bool init_only)
  1714. {
  1715. struct hda_amp_info *info;
  1716. unsigned int parm, val = 0;
  1717. bool val_read = false;
  1718. retry:
  1719. info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
  1720. if (!info)
  1721. return NULL;
  1722. if (!(info->head.val & INFO_AMP_VOL(ch))) {
  1723. if (!val_read) {
  1724. mutex_unlock(&codec->hash_mutex);
  1725. parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
  1726. parm |= direction == HDA_OUTPUT ?
  1727. AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
  1728. parm |= index;
  1729. val = snd_hda_codec_read(codec, nid, 0,
  1730. AC_VERB_GET_AMP_GAIN_MUTE, parm);
  1731. val &= 0xff;
  1732. val_read = true;
  1733. mutex_lock(&codec->hash_mutex);
  1734. goto retry;
  1735. }
  1736. info->vol[ch] = val;
  1737. info->head.val |= INFO_AMP_VOL(ch);
  1738. } else if (init_only)
  1739. return NULL;
  1740. return info;
  1741. }
  1742. /*
  1743. * write the current volume in info to the h/w
  1744. */
  1745. static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps,
  1746. hda_nid_t nid, int ch, int direction, int index,
  1747. int val)
  1748. {
  1749. u32 parm;
  1750. parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
  1751. parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
  1752. parm |= index << AC_AMP_SET_INDEX_SHIFT;
  1753. if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) &&
  1754. (amp_caps & AC_AMPCAP_MIN_MUTE))
  1755. ; /* set the zero value as a fake mute */
  1756. else
  1757. parm |= val;
  1758. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
  1759. }
  1760. /**
  1761. * snd_hda_codec_amp_read - Read AMP value
  1762. * @codec: HD-audio codec
  1763. * @nid: NID to read the AMP value
  1764. * @ch: channel (left=0 or right=1)
  1765. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1766. * @index: the index value (only for input direction)
  1767. *
  1768. * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit.
  1769. */
  1770. int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
  1771. int direction, int index)
  1772. {
  1773. struct hda_amp_info *info;
  1774. unsigned int val = 0;
  1775. mutex_lock(&codec->hash_mutex);
  1776. info = update_amp_hash(codec, nid, ch, direction, index, false);
  1777. if (info)
  1778. val = info->vol[ch];
  1779. mutex_unlock(&codec->hash_mutex);
  1780. return val;
  1781. }
  1782. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
  1783. static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1784. int direction, int idx, int mask, int val,
  1785. bool init_only)
  1786. {
  1787. struct hda_amp_info *info;
  1788. unsigned int caps;
  1789. unsigned int cache_only;
  1790. if (snd_BUG_ON(mask & ~0xff))
  1791. mask &= 0xff;
  1792. val &= mask;
  1793. mutex_lock(&codec->hash_mutex);
  1794. info = update_amp_hash(codec, nid, ch, direction, idx, init_only);
  1795. if (!info) {
  1796. mutex_unlock(&codec->hash_mutex);
  1797. return 0;
  1798. }
  1799. val |= info->vol[ch] & ~mask;
  1800. if (info->vol[ch] == val) {
  1801. mutex_unlock(&codec->hash_mutex);
  1802. return 0;
  1803. }
  1804. info->vol[ch] = val;
  1805. cache_only = info->head.dirty = codec->cached_write;
  1806. caps = info->amp_caps;
  1807. mutex_unlock(&codec->hash_mutex);
  1808. if (!cache_only)
  1809. put_vol_mute(codec, caps, nid, ch, direction, idx, val);
  1810. return 1;
  1811. }
  1812. /**
  1813. * snd_hda_codec_amp_update - update the AMP value
  1814. * @codec: HD-audio codec
  1815. * @nid: NID to read the AMP value
  1816. * @ch: channel (left=0 or right=1)
  1817. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1818. * @idx: the index value (only for input direction)
  1819. * @mask: bit mask to set
  1820. * @val: the bits value to set
  1821. *
  1822. * Update the AMP value with a bit mask.
  1823. * Returns 0 if the value is unchanged, 1 if changed.
  1824. */
  1825. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
  1826. int direction, int idx, int mask, int val)
  1827. {
  1828. return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false);
  1829. }
  1830. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
  1831. /**
  1832. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1833. * @codec: HD-audio codec
  1834. * @nid: NID to read the AMP value
  1835. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1836. * @idx: the index value (only for input direction)
  1837. * @mask: bit mask to set
  1838. * @val: the bits value to set
  1839. *
  1840. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1841. * stereo widget with the same mask and value.
  1842. */
  1843. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1844. int direction, int idx, int mask, int val)
  1845. {
  1846. int ch, ret = 0;
  1847. if (snd_BUG_ON(mask & ~0xff))
  1848. mask &= 0xff;
  1849. for (ch = 0; ch < 2; ch++)
  1850. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1851. idx, mask, val);
  1852. return ret;
  1853. }
  1854. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
  1855. /* Works like snd_hda_codec_amp_update() but it writes the value only at
  1856. * the first access. If the amp was already initialized / updated beforehand,
  1857. * this does nothing.
  1858. */
  1859. int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
  1860. int dir, int idx, int mask, int val)
  1861. {
  1862. return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true);
  1863. }
  1864. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init);
  1865. int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
  1866. int dir, int idx, int mask, int val)
  1867. {
  1868. int ch, ret = 0;
  1869. if (snd_BUG_ON(mask & ~0xff))
  1870. mask &= 0xff;
  1871. for (ch = 0; ch < 2; ch++)
  1872. ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
  1873. idx, mask, val);
  1874. return ret;
  1875. }
  1876. EXPORT_SYMBOL_HDA(snd_hda_codec_amp_init_stereo);
  1877. /**
  1878. * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
  1879. * @codec: HD-audio codec
  1880. *
  1881. * Resume the all amp commands from the cache.
  1882. */
  1883. void snd_hda_codec_resume_amp(struct hda_codec *codec)
  1884. {
  1885. int i;
  1886. mutex_lock(&codec->hash_mutex);
  1887. codec->cached_write = 0;
  1888. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  1889. struct hda_amp_info *buffer;
  1890. u32 key;
  1891. hda_nid_t nid;
  1892. unsigned int idx, dir, ch;
  1893. struct hda_amp_info info;
  1894. buffer = snd_array_elem(&codec->amp_cache.buf, i);
  1895. if (!buffer->head.dirty)
  1896. continue;
  1897. buffer->head.dirty = 0;
  1898. info = *buffer;
  1899. key = info.head.key;
  1900. if (!key)
  1901. continue;
  1902. nid = key & 0xff;
  1903. idx = (key >> 16) & 0xff;
  1904. dir = (key >> 24) & 0xff;
  1905. for (ch = 0; ch < 2; ch++) {
  1906. if (!(info.head.val & INFO_AMP_VOL(ch)))
  1907. continue;
  1908. mutex_unlock(&codec->hash_mutex);
  1909. put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx,
  1910. info.vol[ch]);
  1911. mutex_lock(&codec->hash_mutex);
  1912. }
  1913. }
  1914. mutex_unlock(&codec->hash_mutex);
  1915. }
  1916. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
  1917. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  1918. unsigned int ofs)
  1919. {
  1920. u32 caps = query_amp_caps(codec, nid, dir);
  1921. /* get num steps */
  1922. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1923. if (ofs < caps)
  1924. caps -= ofs;
  1925. return caps;
  1926. }
  1927. /**
  1928. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  1929. *
  1930. * The control element is supposed to have the private_value field
  1931. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1932. */
  1933. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1934. struct snd_ctl_elem_info *uinfo)
  1935. {
  1936. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1937. u16 nid = get_amp_nid(kcontrol);
  1938. u8 chs = get_amp_channels(kcontrol);
  1939. int dir = get_amp_direction(kcontrol);
  1940. unsigned int ofs = get_amp_offset(kcontrol);
  1941. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1942. uinfo->count = chs == 3 ? 2 : 1;
  1943. uinfo->value.integer.min = 0;
  1944. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  1945. if (!uinfo->value.integer.max) {
  1946. printk(KERN_WARNING "hda_codec: "
  1947. "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
  1948. kcontrol->id.name);
  1949. return -EINVAL;
  1950. }
  1951. return 0;
  1952. }
  1953. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
  1954. static inline unsigned int
  1955. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1956. int ch, int dir, int idx, unsigned int ofs)
  1957. {
  1958. unsigned int val;
  1959. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1960. val &= HDA_AMP_VOLMASK;
  1961. if (val >= ofs)
  1962. val -= ofs;
  1963. else
  1964. val = 0;
  1965. return val;
  1966. }
  1967. static inline int
  1968. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1969. int ch, int dir, int idx, unsigned int ofs,
  1970. unsigned int val)
  1971. {
  1972. unsigned int maxval;
  1973. if (val > 0)
  1974. val += ofs;
  1975. /* ofs = 0: raw max value */
  1976. maxval = get_amp_max_value(codec, nid, dir, 0);
  1977. if (val > maxval)
  1978. val = maxval;
  1979. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1980. HDA_AMP_VOLMASK, val);
  1981. }
  1982. /**
  1983. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  1984. *
  1985. * The control element is supposed to have the private_value field
  1986. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1987. */
  1988. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1989. struct snd_ctl_elem_value *ucontrol)
  1990. {
  1991. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1992. hda_nid_t nid = get_amp_nid(kcontrol);
  1993. int chs = get_amp_channels(kcontrol);
  1994. int dir = get_amp_direction(kcontrol);
  1995. int idx = get_amp_index(kcontrol);
  1996. unsigned int ofs = get_amp_offset(kcontrol);
  1997. long *valp = ucontrol->value.integer.value;
  1998. if (chs & 1)
  1999. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  2000. if (chs & 2)
  2001. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  2002. return 0;
  2003. }
  2004. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
  2005. /**
  2006. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  2007. *
  2008. * The control element is supposed to have the private_value field
  2009. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2010. */
  2011. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  2012. struct snd_ctl_elem_value *ucontrol)
  2013. {
  2014. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2015. hda_nid_t nid = get_amp_nid(kcontrol);
  2016. int chs = get_amp_channels(kcontrol);
  2017. int dir = get_amp_direction(kcontrol);
  2018. int idx = get_amp_index(kcontrol);
  2019. unsigned int ofs = get_amp_offset(kcontrol);
  2020. long *valp = ucontrol->value.integer.value;
  2021. int change = 0;
  2022. snd_hda_power_up(codec);
  2023. if (chs & 1) {
  2024. change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  2025. valp++;
  2026. }
  2027. if (chs & 2)
  2028. change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  2029. snd_hda_power_down(codec);
  2030. return change;
  2031. }
  2032. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
  2033. /**
  2034. * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
  2035. *
  2036. * The control element is supposed to have the private_value field
  2037. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2038. */
  2039. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2040. unsigned int size, unsigned int __user *_tlv)
  2041. {
  2042. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2043. hda_nid_t nid = get_amp_nid(kcontrol);
  2044. int dir = get_amp_direction(kcontrol);
  2045. unsigned int ofs = get_amp_offset(kcontrol);
  2046. bool min_mute = get_amp_min_mute(kcontrol);
  2047. u32 caps, val1, val2;
  2048. if (size < 4 * sizeof(unsigned int))
  2049. return -ENOMEM;
  2050. caps = query_amp_caps(codec, nid, dir);
  2051. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2052. val2 = (val2 + 1) * 25;
  2053. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  2054. val1 += ofs;
  2055. val1 = ((int)val1) * ((int)val2);
  2056. if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
  2057. val2 |= TLV_DB_SCALE_MUTE;
  2058. if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
  2059. return -EFAULT;
  2060. if (put_user(2 * sizeof(unsigned int), _tlv + 1))
  2061. return -EFAULT;
  2062. if (put_user(val1, _tlv + 2))
  2063. return -EFAULT;
  2064. if (put_user(val2, _tlv + 3))
  2065. return -EFAULT;
  2066. return 0;
  2067. }
  2068. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
  2069. /**
  2070. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  2071. * @codec: HD-audio codec
  2072. * @nid: NID of a reference widget
  2073. * @dir: #HDA_INPUT or #HDA_OUTPUT
  2074. * @tlv: TLV data to be stored, at least 4 elements
  2075. *
  2076. * Set (static) TLV data for a virtual master volume using the AMP caps
  2077. * obtained from the reference NID.
  2078. * The volume range is recalculated as if the max volume is 0dB.
  2079. */
  2080. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  2081. unsigned int *tlv)
  2082. {
  2083. u32 caps;
  2084. int nums, step;
  2085. caps = query_amp_caps(codec, nid, dir);
  2086. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  2087. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2088. step = (step + 1) * 25;
  2089. tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
  2090. tlv[1] = 2 * sizeof(unsigned int);
  2091. tlv[2] = -nums * step;
  2092. tlv[3] = step;
  2093. }
  2094. EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
  2095. /* find a mixer control element with the given name */
  2096. static struct snd_kcontrol *
  2097. find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
  2098. {
  2099. struct snd_ctl_elem_id id;
  2100. memset(&id, 0, sizeof(id));
  2101. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  2102. id.device = dev;
  2103. id.index = idx;
  2104. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  2105. return NULL;
  2106. strcpy(id.name, name);
  2107. return snd_ctl_find_id(codec->bus->card, &id);
  2108. }
  2109. /**
  2110. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  2111. * @codec: HD-audio codec
  2112. * @name: ctl id name string
  2113. *
  2114. * Get the control element with the given id string and IFACE_MIXER.
  2115. */
  2116. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  2117. const char *name)
  2118. {
  2119. return find_mixer_ctl(codec, name, 0, 0);
  2120. }
  2121. EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
  2122. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
  2123. int start_idx)
  2124. {
  2125. int i, idx;
  2126. /* 16 ctlrs should be large enough */
  2127. for (i = 0, idx = start_idx; i < 16; i++, idx++) {
  2128. if (!find_mixer_ctl(codec, name, 0, idx))
  2129. return idx;
  2130. }
  2131. return -EBUSY;
  2132. }
  2133. /**
  2134. * snd_hda_ctl_add - Add a control element and assign to the codec
  2135. * @codec: HD-audio codec
  2136. * @nid: corresponding NID (optional)
  2137. * @kctl: the control element to assign
  2138. *
  2139. * Add the given control element to an array inside the codec instance.
  2140. * All control elements belonging to a codec are supposed to be added
  2141. * by this function so that a proper clean-up works at the free or
  2142. * reconfiguration time.
  2143. *
  2144. * If non-zero @nid is passed, the NID is assigned to the control element.
  2145. * The assignment is shown in the codec proc file.
  2146. *
  2147. * snd_hda_ctl_add() checks the control subdev id field whether
  2148. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  2149. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  2150. * specifies if kctl->private_value is a HDA amplifier value.
  2151. */
  2152. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  2153. struct snd_kcontrol *kctl)
  2154. {
  2155. int err;
  2156. unsigned short flags = 0;
  2157. struct hda_nid_item *item;
  2158. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  2159. flags |= HDA_NID_ITEM_AMP;
  2160. if (nid == 0)
  2161. nid = get_amp_nid_(kctl->private_value);
  2162. }
  2163. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  2164. nid = kctl->id.subdevice & 0xffff;
  2165. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  2166. kctl->id.subdevice = 0;
  2167. err = snd_ctl_add(codec->bus->card, kctl);
  2168. if (err < 0)
  2169. return err;
  2170. item = snd_array_new(&codec->mixers);
  2171. if (!item)
  2172. return -ENOMEM;
  2173. item->kctl = kctl;
  2174. item->nid = nid;
  2175. item->flags = flags;
  2176. return 0;
  2177. }
  2178. EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
  2179. /**
  2180. * snd_hda_add_nid - Assign a NID to a control element
  2181. * @codec: HD-audio codec
  2182. * @nid: corresponding NID (optional)
  2183. * @kctl: the control element to assign
  2184. * @index: index to kctl
  2185. *
  2186. * Add the given control element to an array inside the codec instance.
  2187. * This function is used when #snd_hda_ctl_add cannot be used for 1:1
  2188. * NID:KCTL mapping - for example "Capture Source" selector.
  2189. */
  2190. int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
  2191. unsigned int index, hda_nid_t nid)
  2192. {
  2193. struct hda_nid_item *item;
  2194. if (nid > 0) {
  2195. item = snd_array_new(&codec->nids);
  2196. if (!item)
  2197. return -ENOMEM;
  2198. item->kctl = kctl;
  2199. item->index = index;
  2200. item->nid = nid;
  2201. return 0;
  2202. }
  2203. printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
  2204. kctl->id.name, kctl->id.index, index);
  2205. return -EINVAL;
  2206. }
  2207. EXPORT_SYMBOL_HDA(snd_hda_add_nid);
  2208. /**
  2209. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  2210. * @codec: HD-audio codec
  2211. */
  2212. void snd_hda_ctls_clear(struct hda_codec *codec)
  2213. {
  2214. int i;
  2215. struct hda_nid_item *items = codec->mixers.list;
  2216. for (i = 0; i < codec->mixers.used; i++)
  2217. snd_ctl_remove(codec->bus->card, items[i].kctl);
  2218. snd_array_free(&codec->mixers);
  2219. snd_array_free(&codec->nids);
  2220. }
  2221. /* pseudo device locking
  2222. * toggle card->shutdown to allow/disallow the device access (as a hack)
  2223. */
  2224. int snd_hda_lock_devices(struct hda_bus *bus)
  2225. {
  2226. struct snd_card *card = bus->card;
  2227. struct hda_codec *codec;
  2228. spin_lock(&card->files_lock);
  2229. if (card->shutdown)
  2230. goto err_unlock;
  2231. card->shutdown = 1;
  2232. if (!list_empty(&card->ctl_files))
  2233. goto err_clear;
  2234. list_for_each_entry(codec, &bus->codec_list, list) {
  2235. int pcm;
  2236. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  2237. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  2238. if (!cpcm->pcm)
  2239. continue;
  2240. if (cpcm->pcm->streams[0].substream_opened ||
  2241. cpcm->pcm->streams[1].substream_opened)
  2242. goto err_clear;
  2243. }
  2244. }
  2245. spin_unlock(&card->files_lock);
  2246. return 0;
  2247. err_clear:
  2248. card->shutdown = 0;
  2249. err_unlock:
  2250. spin_unlock(&card->files_lock);
  2251. return -EINVAL;
  2252. }
  2253. EXPORT_SYMBOL_HDA(snd_hda_lock_devices);
  2254. void snd_hda_unlock_devices(struct hda_bus *bus)
  2255. {
  2256. struct snd_card *card = bus->card;
  2257. card = bus->card;
  2258. spin_lock(&card->files_lock);
  2259. card->shutdown = 0;
  2260. spin_unlock(&card->files_lock);
  2261. }
  2262. EXPORT_SYMBOL_HDA(snd_hda_unlock_devices);
  2263. /**
  2264. * snd_hda_codec_reset - Clear all objects assigned to the codec
  2265. * @codec: HD-audio codec
  2266. *
  2267. * This frees the all PCM and control elements assigned to the codec, and
  2268. * clears the caches and restores the pin default configurations.
  2269. *
  2270. * When a device is being used, it returns -EBSY. If successfully freed,
  2271. * returns zero.
  2272. */
  2273. int snd_hda_codec_reset(struct hda_codec *codec)
  2274. {
  2275. struct hda_bus *bus = codec->bus;
  2276. struct snd_card *card = bus->card;
  2277. int i;
  2278. if (snd_hda_lock_devices(bus) < 0)
  2279. return -EBUSY;
  2280. /* OK, let it free */
  2281. cancel_delayed_work_sync(&codec->jackpoll_work);
  2282. #ifdef CONFIG_PM
  2283. cancel_delayed_work_sync(&codec->power_work);
  2284. codec->power_on = 0;
  2285. codec->power_transition = 0;
  2286. codec->power_jiffies = jiffies;
  2287. flush_workqueue(bus->workq);
  2288. #endif
  2289. snd_hda_ctls_clear(codec);
  2290. /* release PCMs */
  2291. for (i = 0; i < codec->num_pcms; i++) {
  2292. if (codec->pcm_info[i].pcm) {
  2293. snd_device_free(card, codec->pcm_info[i].pcm);
  2294. clear_bit(codec->pcm_info[i].device,
  2295. bus->pcm_dev_bits);
  2296. }
  2297. }
  2298. if (codec->patch_ops.free)
  2299. codec->patch_ops.free(codec);
  2300. memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
  2301. snd_hda_jack_tbl_clear(codec);
  2302. codec->proc_widget_hook = NULL;
  2303. codec->spec = NULL;
  2304. free_hda_cache(&codec->amp_cache);
  2305. free_hda_cache(&codec->cmd_cache);
  2306. init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
  2307. init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
  2308. /* free only driver_pins so that init_pins + user_pins are restored */
  2309. snd_array_free(&codec->driver_pins);
  2310. snd_array_free(&codec->cvt_setups);
  2311. snd_array_free(&codec->spdif_out);
  2312. snd_array_free(&codec->verbs);
  2313. codec->num_pcms = 0;
  2314. codec->pcm_info = NULL;
  2315. codec->preset = NULL;
  2316. codec->slave_dig_outs = NULL;
  2317. codec->spdif_status_reset = 0;
  2318. module_put(codec->owner);
  2319. codec->owner = NULL;
  2320. /* allow device access again */
  2321. snd_hda_unlock_devices(bus);
  2322. return 0;
  2323. }
  2324. typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
  2325. /* apply the function to all matching slave ctls in the mixer list */
  2326. static int map_slaves(struct hda_codec *codec, const char * const *slaves,
  2327. const char *suffix, map_slave_func_t func, void *data)
  2328. {
  2329. struct hda_nid_item *items;
  2330. const char * const *s;
  2331. int i, err;
  2332. items = codec->mixers.list;
  2333. for (i = 0; i < codec->mixers.used; i++) {
  2334. struct snd_kcontrol *sctl = items[i].kctl;
  2335. if (!sctl || !sctl->id.name ||
  2336. sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  2337. continue;
  2338. for (s = slaves; *s; s++) {
  2339. char tmpname[sizeof(sctl->id.name)];
  2340. const char *name = *s;
  2341. if (suffix) {
  2342. snprintf(tmpname, sizeof(tmpname), "%s %s",
  2343. name, suffix);
  2344. name = tmpname;
  2345. }
  2346. if (!strcmp(sctl->id.name, name)) {
  2347. err = func(data, sctl);
  2348. if (err)
  2349. return err;
  2350. break;
  2351. }
  2352. }
  2353. }
  2354. return 0;
  2355. }
  2356. static int check_slave_present(void *data, struct snd_kcontrol *sctl)
  2357. {
  2358. return 1;
  2359. }
  2360. /* guess the value corresponding to 0dB */
  2361. static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
  2362. {
  2363. int _tlv[4];
  2364. const int *tlv = NULL;
  2365. int val = -1;
  2366. if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  2367. /* FIXME: set_fs() hack for obtaining user-space TLV data */
  2368. mm_segment_t fs = get_fs();
  2369. set_fs(get_ds());
  2370. if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
  2371. tlv = _tlv;
  2372. set_fs(fs);
  2373. } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
  2374. tlv = kctl->tlv.p;
  2375. if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
  2376. val = -tlv[2] / tlv[3];
  2377. return val;
  2378. }
  2379. /* call kctl->put with the given value(s) */
  2380. static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
  2381. {
  2382. struct snd_ctl_elem_value *ucontrol;
  2383. ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
  2384. if (!ucontrol)
  2385. return -ENOMEM;
  2386. ucontrol->value.integer.value[0] = val;
  2387. ucontrol->value.integer.value[1] = val;
  2388. kctl->put(kctl, ucontrol);
  2389. kfree(ucontrol);
  2390. return 0;
  2391. }
  2392. /* initialize the slave volume with 0dB */
  2393. static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
  2394. {
  2395. int offset = get_kctl_0dB_offset(slave);
  2396. if (offset > 0)
  2397. put_kctl_with_value(slave, offset);
  2398. return 0;
  2399. }
  2400. /* unmute the slave */
  2401. static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
  2402. {
  2403. return put_kctl_with_value(slave, 1);
  2404. }
  2405. /**
  2406. * snd_hda_add_vmaster - create a virtual master control and add slaves
  2407. * @codec: HD-audio codec
  2408. * @name: vmaster control name
  2409. * @tlv: TLV data (optional)
  2410. * @slaves: slave control names (optional)
  2411. * @suffix: suffix string to each slave name (optional)
  2412. * @init_slave_vol: initialize slaves to unmute/0dB
  2413. * @ctl_ret: store the vmaster kcontrol in return
  2414. *
  2415. * Create a virtual master control with the given name. The TLV data
  2416. * must be either NULL or a valid data.
  2417. *
  2418. * @slaves is a NULL-terminated array of strings, each of which is a
  2419. * slave control name. All controls with these names are assigned to
  2420. * the new virtual master control.
  2421. *
  2422. * This function returns zero if successful or a negative error code.
  2423. */
  2424. int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  2425. unsigned int *tlv, const char * const *slaves,
  2426. const char *suffix, bool init_slave_vol,
  2427. struct snd_kcontrol **ctl_ret)
  2428. {
  2429. struct snd_kcontrol *kctl;
  2430. int err;
  2431. if (ctl_ret)
  2432. *ctl_ret = NULL;
  2433. err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
  2434. if (err != 1) {
  2435. snd_printdd("No slave found for %s\n", name);
  2436. return 0;
  2437. }
  2438. kctl = snd_ctl_make_virtual_master(name, tlv);
  2439. if (!kctl)
  2440. return -ENOMEM;
  2441. err = snd_hda_ctl_add(codec, 0, kctl);
  2442. if (err < 0)
  2443. return err;
  2444. err = map_slaves(codec, slaves, suffix,
  2445. (map_slave_func_t)snd_ctl_add_slave, kctl);
  2446. if (err < 0)
  2447. return err;
  2448. /* init with master mute & zero volume */
  2449. put_kctl_with_value(kctl, 0);
  2450. if (init_slave_vol)
  2451. map_slaves(codec, slaves, suffix,
  2452. tlv ? init_slave_0dB : init_slave_unmute, kctl);
  2453. if (ctl_ret)
  2454. *ctl_ret = kctl;
  2455. return 0;
  2456. }
  2457. EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
  2458. /*
  2459. * mute-LED control using vmaster
  2460. */
  2461. static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
  2462. struct snd_ctl_elem_info *uinfo)
  2463. {
  2464. static const char * const texts[] = {
  2465. "On", "Off", "Follow Master"
  2466. };
  2467. unsigned int index;
  2468. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2469. uinfo->count = 1;
  2470. uinfo->value.enumerated.items = 3;
  2471. index = uinfo->value.enumerated.item;
  2472. if (index >= 3)
  2473. index = 2;
  2474. strcpy(uinfo->value.enumerated.name, texts[index]);
  2475. return 0;
  2476. }
  2477. static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
  2478. struct snd_ctl_elem_value *ucontrol)
  2479. {
  2480. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2481. ucontrol->value.enumerated.item[0] = hook->mute_mode;
  2482. return 0;
  2483. }
  2484. static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
  2485. struct snd_ctl_elem_value *ucontrol)
  2486. {
  2487. struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
  2488. unsigned int old_mode = hook->mute_mode;
  2489. hook->mute_mode = ucontrol->value.enumerated.item[0];
  2490. if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
  2491. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2492. if (old_mode == hook->mute_mode)
  2493. return 0;
  2494. snd_hda_sync_vmaster_hook(hook);
  2495. return 1;
  2496. }
  2497. static struct snd_kcontrol_new vmaster_mute_mode = {
  2498. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2499. .name = "Mute-LED Mode",
  2500. .info = vmaster_mute_mode_info,
  2501. .get = vmaster_mute_mode_get,
  2502. .put = vmaster_mute_mode_put,
  2503. };
  2504. /*
  2505. * Add a mute-LED hook with the given vmaster switch kctl
  2506. * "Mute-LED Mode" control is automatically created and associated with
  2507. * the given hook.
  2508. */
  2509. int snd_hda_add_vmaster_hook(struct hda_codec *codec,
  2510. struct hda_vmaster_mute_hook *hook,
  2511. bool expose_enum_ctl)
  2512. {
  2513. struct snd_kcontrol *kctl;
  2514. if (!hook->hook || !hook->sw_kctl)
  2515. return 0;
  2516. snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
  2517. hook->codec = codec;
  2518. hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
  2519. if (!expose_enum_ctl)
  2520. return 0;
  2521. kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
  2522. if (!kctl)
  2523. return -ENOMEM;
  2524. return snd_hda_ctl_add(codec, 0, kctl);
  2525. }
  2526. EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
  2527. /*
  2528. * Call the hook with the current value for synchronization
  2529. * Should be called in init callback
  2530. */
  2531. void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
  2532. {
  2533. if (!hook->hook || !hook->codec)
  2534. return;
  2535. /* don't call vmaster hook in the destructor since it might have
  2536. * been already destroyed
  2537. */
  2538. if (hook->codec->bus->shutdown)
  2539. return;
  2540. switch (hook->mute_mode) {
  2541. case HDA_VMUTE_FOLLOW_MASTER:
  2542. snd_ctl_sync_vmaster_hook(hook->sw_kctl);
  2543. break;
  2544. default:
  2545. hook->hook(hook->codec, hook->mute_mode);
  2546. break;
  2547. }
  2548. }
  2549. EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
  2550. /**
  2551. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  2552. *
  2553. * The control element is supposed to have the private_value field
  2554. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2555. */
  2556. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  2557. struct snd_ctl_elem_info *uinfo)
  2558. {
  2559. int chs = get_amp_channels(kcontrol);
  2560. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2561. uinfo->count = chs == 3 ? 2 : 1;
  2562. uinfo->value.integer.min = 0;
  2563. uinfo->value.integer.max = 1;
  2564. return 0;
  2565. }
  2566. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
  2567. /**
  2568. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  2569. *
  2570. * The control element is supposed to have the private_value field
  2571. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2572. */
  2573. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  2574. struct snd_ctl_elem_value *ucontrol)
  2575. {
  2576. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2577. hda_nid_t nid = get_amp_nid(kcontrol);
  2578. int chs = get_amp_channels(kcontrol);
  2579. int dir = get_amp_direction(kcontrol);
  2580. int idx = get_amp_index(kcontrol);
  2581. long *valp = ucontrol->value.integer.value;
  2582. if (chs & 1)
  2583. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  2584. HDA_AMP_MUTE) ? 0 : 1;
  2585. if (chs & 2)
  2586. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  2587. HDA_AMP_MUTE) ? 0 : 1;
  2588. return 0;
  2589. }
  2590. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
  2591. /**
  2592. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  2593. *
  2594. * The control element is supposed to have the private_value field
  2595. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  2596. */
  2597. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  2598. struct snd_ctl_elem_value *ucontrol)
  2599. {
  2600. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2601. hda_nid_t nid = get_amp_nid(kcontrol);
  2602. int chs = get_amp_channels(kcontrol);
  2603. int dir = get_amp_direction(kcontrol);
  2604. int idx = get_amp_index(kcontrol);
  2605. long *valp = ucontrol->value.integer.value;
  2606. int change = 0;
  2607. snd_hda_power_up(codec);
  2608. if (chs & 1) {
  2609. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  2610. HDA_AMP_MUTE,
  2611. *valp ? 0 : HDA_AMP_MUTE);
  2612. valp++;
  2613. }
  2614. if (chs & 2)
  2615. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  2616. HDA_AMP_MUTE,
  2617. *valp ? 0 : HDA_AMP_MUTE);
  2618. hda_call_check_power_status(codec, nid);
  2619. snd_hda_power_down(codec);
  2620. return change;
  2621. }
  2622. EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
  2623. /*
  2624. * bound volume controls
  2625. *
  2626. * bind multiple volumes (# indices, from 0)
  2627. */
  2628. #define AMP_VAL_IDX_SHIFT 19
  2629. #define AMP_VAL_IDX_MASK (0x0f<<19)
  2630. /**
  2631. * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
  2632. *
  2633. * The control element is supposed to have the private_value field
  2634. * set up via HDA_BIND_MUTE*() macros.
  2635. */
  2636. int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
  2637. struct snd_ctl_elem_value *ucontrol)
  2638. {
  2639. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2640. unsigned long pval;
  2641. int err;
  2642. mutex_lock(&codec->control_mutex);
  2643. pval = kcontrol->private_value;
  2644. kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
  2645. err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
  2646. kcontrol->private_value = pval;
  2647. mutex_unlock(&codec->control_mutex);
  2648. return err;
  2649. }
  2650. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
  2651. /**
  2652. * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
  2653. *
  2654. * The control element is supposed to have the private_value field
  2655. * set up via HDA_BIND_MUTE*() macros.
  2656. */
  2657. int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
  2658. struct snd_ctl_elem_value *ucontrol)
  2659. {
  2660. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2661. unsigned long pval;
  2662. int i, indices, err = 0, change = 0;
  2663. mutex_lock(&codec->control_mutex);
  2664. pval = kcontrol->private_value;
  2665. indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
  2666. for (i = 0; i < indices; i++) {
  2667. kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
  2668. (i << AMP_VAL_IDX_SHIFT);
  2669. err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2670. if (err < 0)
  2671. break;
  2672. change |= err;
  2673. }
  2674. kcontrol->private_value = pval;
  2675. mutex_unlock(&codec->control_mutex);
  2676. return err < 0 ? err : change;
  2677. }
  2678. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
  2679. /**
  2680. * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
  2681. *
  2682. * The control element is supposed to have the private_value field
  2683. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2684. */
  2685. int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
  2686. struct snd_ctl_elem_info *uinfo)
  2687. {
  2688. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2689. struct hda_bind_ctls *c;
  2690. int err;
  2691. mutex_lock(&codec->control_mutex);
  2692. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2693. kcontrol->private_value = *c->values;
  2694. err = c->ops->info(kcontrol, uinfo);
  2695. kcontrol->private_value = (long)c;
  2696. mutex_unlock(&codec->control_mutex);
  2697. return err;
  2698. }
  2699. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
  2700. /**
  2701. * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
  2702. *
  2703. * The control element is supposed to have the private_value field
  2704. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2705. */
  2706. int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
  2707. struct snd_ctl_elem_value *ucontrol)
  2708. {
  2709. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2710. struct hda_bind_ctls *c;
  2711. int err;
  2712. mutex_lock(&codec->control_mutex);
  2713. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2714. kcontrol->private_value = *c->values;
  2715. err = c->ops->get(kcontrol, ucontrol);
  2716. kcontrol->private_value = (long)c;
  2717. mutex_unlock(&codec->control_mutex);
  2718. return err;
  2719. }
  2720. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
  2721. /**
  2722. * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
  2723. *
  2724. * The control element is supposed to have the private_value field
  2725. * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
  2726. */
  2727. int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
  2728. struct snd_ctl_elem_value *ucontrol)
  2729. {
  2730. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2731. struct hda_bind_ctls *c;
  2732. unsigned long *vals;
  2733. int err = 0, change = 0;
  2734. mutex_lock(&codec->control_mutex);
  2735. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2736. for (vals = c->values; *vals; vals++) {
  2737. kcontrol->private_value = *vals;
  2738. err = c->ops->put(kcontrol, ucontrol);
  2739. if (err < 0)
  2740. break;
  2741. change |= err;
  2742. }
  2743. kcontrol->private_value = (long)c;
  2744. mutex_unlock(&codec->control_mutex);
  2745. return err < 0 ? err : change;
  2746. }
  2747. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
  2748. /**
  2749. * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
  2750. *
  2751. * The control element is supposed to have the private_value field
  2752. * set up via HDA_BIND_VOL() macro.
  2753. */
  2754. int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  2755. unsigned int size, unsigned int __user *tlv)
  2756. {
  2757. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2758. struct hda_bind_ctls *c;
  2759. int err;
  2760. mutex_lock(&codec->control_mutex);
  2761. c = (struct hda_bind_ctls *)kcontrol->private_value;
  2762. kcontrol->private_value = *c->values;
  2763. err = c->ops->tlv(kcontrol, op_flag, size, tlv);
  2764. kcontrol->private_value = (long)c;
  2765. mutex_unlock(&codec->control_mutex);
  2766. return err;
  2767. }
  2768. EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
  2769. struct hda_ctl_ops snd_hda_bind_vol = {
  2770. .info = snd_hda_mixer_amp_volume_info,
  2771. .get = snd_hda_mixer_amp_volume_get,
  2772. .put = snd_hda_mixer_amp_volume_put,
  2773. .tlv = snd_hda_mixer_amp_tlv
  2774. };
  2775. EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
  2776. struct hda_ctl_ops snd_hda_bind_sw = {
  2777. .info = snd_hda_mixer_amp_switch_info,
  2778. .get = snd_hda_mixer_amp_switch_get,
  2779. .put = snd_hda_mixer_amp_switch_put,
  2780. .tlv = snd_hda_mixer_amp_tlv
  2781. };
  2782. EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
  2783. /*
  2784. * SPDIF out controls
  2785. */
  2786. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  2787. struct snd_ctl_elem_info *uinfo)
  2788. {
  2789. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  2790. uinfo->count = 1;
  2791. return 0;
  2792. }
  2793. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  2794. struct snd_ctl_elem_value *ucontrol)
  2795. {
  2796. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2797. IEC958_AES0_NONAUDIO |
  2798. IEC958_AES0_CON_EMPHASIS_5015 |
  2799. IEC958_AES0_CON_NOT_COPYRIGHT;
  2800. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  2801. IEC958_AES1_CON_ORIGINAL;
  2802. return 0;
  2803. }
  2804. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  2805. struct snd_ctl_elem_value *ucontrol)
  2806. {
  2807. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  2808. IEC958_AES0_NONAUDIO |
  2809. IEC958_AES0_PRO_EMPHASIS_5015;
  2810. return 0;
  2811. }
  2812. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  2813. struct snd_ctl_elem_value *ucontrol)
  2814. {
  2815. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2816. int idx = kcontrol->private_value;
  2817. struct hda_spdif_out *spdif;
  2818. mutex_lock(&codec->spdif_mutex);
  2819. spdif = snd_array_elem(&codec->spdif_out, idx);
  2820. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  2821. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  2822. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  2823. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  2824. mutex_unlock(&codec->spdif_mutex);
  2825. return 0;
  2826. }
  2827. /* convert from SPDIF status bits to HDA SPDIF bits
  2828. * bit 0 (DigEn) is always set zero (to be filled later)
  2829. */
  2830. static unsigned short convert_from_spdif_status(unsigned int sbits)
  2831. {
  2832. unsigned short val = 0;
  2833. if (sbits & IEC958_AES0_PROFESSIONAL)
  2834. val |= AC_DIG1_PROFESSIONAL;
  2835. if (sbits & IEC958_AES0_NONAUDIO)
  2836. val |= AC_DIG1_NONAUDIO;
  2837. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2838. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  2839. IEC958_AES0_PRO_EMPHASIS_5015)
  2840. val |= AC_DIG1_EMPHASIS;
  2841. } else {
  2842. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  2843. IEC958_AES0_CON_EMPHASIS_5015)
  2844. val |= AC_DIG1_EMPHASIS;
  2845. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  2846. val |= AC_DIG1_COPYRIGHT;
  2847. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  2848. val |= AC_DIG1_LEVEL;
  2849. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  2850. }
  2851. return val;
  2852. }
  2853. /* convert to SPDIF status bits from HDA SPDIF bits
  2854. */
  2855. static unsigned int convert_to_spdif_status(unsigned short val)
  2856. {
  2857. unsigned int sbits = 0;
  2858. if (val & AC_DIG1_NONAUDIO)
  2859. sbits |= IEC958_AES0_NONAUDIO;
  2860. if (val & AC_DIG1_PROFESSIONAL)
  2861. sbits |= IEC958_AES0_PROFESSIONAL;
  2862. if (sbits & IEC958_AES0_PROFESSIONAL) {
  2863. if (val & AC_DIG1_EMPHASIS)
  2864. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  2865. } else {
  2866. if (val & AC_DIG1_EMPHASIS)
  2867. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  2868. if (!(val & AC_DIG1_COPYRIGHT))
  2869. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  2870. if (val & AC_DIG1_LEVEL)
  2871. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  2872. sbits |= val & (0x7f << 8);
  2873. }
  2874. return sbits;
  2875. }
  2876. /* set digital convert verbs both for the given NID and its slaves */
  2877. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  2878. int verb, int val)
  2879. {
  2880. const hda_nid_t *d;
  2881. snd_hda_codec_write_cache(codec, nid, 0, verb, val);
  2882. d = codec->slave_dig_outs;
  2883. if (!d)
  2884. return;
  2885. for (; *d; d++)
  2886. snd_hda_codec_write_cache(codec, *d, 0, verb, val);
  2887. }
  2888. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  2889. int dig1, int dig2)
  2890. {
  2891. if (dig1 != -1)
  2892. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
  2893. if (dig2 != -1)
  2894. set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
  2895. }
  2896. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  2897. struct snd_ctl_elem_value *ucontrol)
  2898. {
  2899. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2900. int idx = kcontrol->private_value;
  2901. struct hda_spdif_out *spdif;
  2902. hda_nid_t nid;
  2903. unsigned short val;
  2904. int change;
  2905. mutex_lock(&codec->spdif_mutex);
  2906. spdif = snd_array_elem(&codec->spdif_out, idx);
  2907. nid = spdif->nid;
  2908. spdif->status = ucontrol->value.iec958.status[0] |
  2909. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  2910. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  2911. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  2912. val = convert_from_spdif_status(spdif->status);
  2913. val |= spdif->ctls & 1;
  2914. change = spdif->ctls != val;
  2915. spdif->ctls = val;
  2916. if (change && nid != (u16)-1)
  2917. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2918. mutex_unlock(&codec->spdif_mutex);
  2919. return change;
  2920. }
  2921. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  2922. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  2923. struct snd_ctl_elem_value *ucontrol)
  2924. {
  2925. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2926. int idx = kcontrol->private_value;
  2927. struct hda_spdif_out *spdif;
  2928. mutex_lock(&codec->spdif_mutex);
  2929. spdif = snd_array_elem(&codec->spdif_out, idx);
  2930. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  2931. mutex_unlock(&codec->spdif_mutex);
  2932. return 0;
  2933. }
  2934. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  2935. int dig1, int dig2)
  2936. {
  2937. set_dig_out_convert(codec, nid, dig1, dig2);
  2938. /* unmute amp switch (if any) */
  2939. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  2940. (dig1 & AC_DIG1_ENABLE))
  2941. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  2942. HDA_AMP_MUTE, 0);
  2943. }
  2944. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  2945. struct snd_ctl_elem_value *ucontrol)
  2946. {
  2947. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2948. int idx = kcontrol->private_value;
  2949. struct hda_spdif_out *spdif;
  2950. hda_nid_t nid;
  2951. unsigned short val;
  2952. int change;
  2953. mutex_lock(&codec->spdif_mutex);
  2954. spdif = snd_array_elem(&codec->spdif_out, idx);
  2955. nid = spdif->nid;
  2956. val = spdif->ctls & ~AC_DIG1_ENABLE;
  2957. if (ucontrol->value.integer.value[0])
  2958. val |= AC_DIG1_ENABLE;
  2959. change = spdif->ctls != val;
  2960. spdif->ctls = val;
  2961. if (change && nid != (u16)-1)
  2962. set_spdif_ctls(codec, nid, val & 0xff, -1);
  2963. mutex_unlock(&codec->spdif_mutex);
  2964. return change;
  2965. }
  2966. static struct snd_kcontrol_new dig_mixes[] = {
  2967. {
  2968. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2969. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2970. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  2971. .info = snd_hda_spdif_mask_info,
  2972. .get = snd_hda_spdif_cmask_get,
  2973. },
  2974. {
  2975. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2976. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2977. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  2978. .info = snd_hda_spdif_mask_info,
  2979. .get = snd_hda_spdif_pmask_get,
  2980. },
  2981. {
  2982. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2983. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  2984. .info = snd_hda_spdif_mask_info,
  2985. .get = snd_hda_spdif_default_get,
  2986. .put = snd_hda_spdif_default_put,
  2987. },
  2988. {
  2989. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2990. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  2991. .info = snd_hda_spdif_out_switch_info,
  2992. .get = snd_hda_spdif_out_switch_get,
  2993. .put = snd_hda_spdif_out_switch_put,
  2994. },
  2995. { } /* end */
  2996. };
  2997. /**
  2998. * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
  2999. * @codec: the HDA codec
  3000. * @associated_nid: NID that new ctls associated with
  3001. * @cvt_nid: converter NID
  3002. * @type: HDA_PCM_TYPE_*
  3003. * Creates controls related with the digital output.
  3004. * Called from each patch supporting the digital out.
  3005. *
  3006. * Returns 0 if successful, or a negative error code.
  3007. */
  3008. int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
  3009. hda_nid_t associated_nid,
  3010. hda_nid_t cvt_nid,
  3011. int type)
  3012. {
  3013. int err;
  3014. struct snd_kcontrol *kctl;
  3015. struct snd_kcontrol_new *dig_mix;
  3016. int idx = 0;
  3017. const int spdif_index = 16;
  3018. struct hda_spdif_out *spdif;
  3019. struct hda_bus *bus = codec->bus;
  3020. if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
  3021. type == HDA_PCM_TYPE_SPDIF) {
  3022. idx = spdif_index;
  3023. } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
  3024. type == HDA_PCM_TYPE_HDMI) {
  3025. /* suppose a single SPDIF device */
  3026. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3027. kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
  3028. if (!kctl)
  3029. break;
  3030. kctl->id.index = spdif_index;
  3031. }
  3032. bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
  3033. }
  3034. if (!bus->primary_dig_out_type)
  3035. bus->primary_dig_out_type = type;
  3036. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
  3037. if (idx < 0) {
  3038. printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
  3039. return -EBUSY;
  3040. }
  3041. spdif = snd_array_new(&codec->spdif_out);
  3042. if (!spdif)
  3043. return -ENOMEM;
  3044. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  3045. kctl = snd_ctl_new1(dig_mix, codec);
  3046. if (!kctl)
  3047. return -ENOMEM;
  3048. kctl->id.index = idx;
  3049. kctl->private_value = codec->spdif_out.used - 1;
  3050. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  3051. if (err < 0)
  3052. return err;
  3053. }
  3054. spdif->nid = cvt_nid;
  3055. spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
  3056. AC_VERB_GET_DIGI_CONVERT_1, 0);
  3057. spdif->status = convert_to_spdif_status(spdif->ctls);
  3058. return 0;
  3059. }
  3060. EXPORT_SYMBOL_HDA(snd_hda_create_dig_out_ctls);
  3061. /* get the hda_spdif_out entry from the given NID
  3062. * call within spdif_mutex lock
  3063. */
  3064. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  3065. hda_nid_t nid)
  3066. {
  3067. int i;
  3068. for (i = 0; i < codec->spdif_out.used; i++) {
  3069. struct hda_spdif_out *spdif =
  3070. snd_array_elem(&codec->spdif_out, i);
  3071. if (spdif->nid == nid)
  3072. return spdif;
  3073. }
  3074. return NULL;
  3075. }
  3076. EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
  3077. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  3078. {
  3079. struct hda_spdif_out *spdif;
  3080. mutex_lock(&codec->spdif_mutex);
  3081. spdif = snd_array_elem(&codec->spdif_out, idx);
  3082. spdif->nid = (u16)-1;
  3083. mutex_unlock(&codec->spdif_mutex);
  3084. }
  3085. EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
  3086. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  3087. {
  3088. struct hda_spdif_out *spdif;
  3089. unsigned short val;
  3090. mutex_lock(&codec->spdif_mutex);
  3091. spdif = snd_array_elem(&codec->spdif_out, idx);
  3092. if (spdif->nid != nid) {
  3093. spdif->nid = nid;
  3094. val = spdif->ctls;
  3095. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  3096. }
  3097. mutex_unlock(&codec->spdif_mutex);
  3098. }
  3099. EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
  3100. /*
  3101. * SPDIF sharing with analog output
  3102. */
  3103. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  3104. struct snd_ctl_elem_value *ucontrol)
  3105. {
  3106. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3107. ucontrol->value.integer.value[0] = mout->share_spdif;
  3108. return 0;
  3109. }
  3110. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  3111. struct snd_ctl_elem_value *ucontrol)
  3112. {
  3113. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  3114. mout->share_spdif = !!ucontrol->value.integer.value[0];
  3115. return 0;
  3116. }
  3117. static struct snd_kcontrol_new spdif_share_sw = {
  3118. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3119. .name = "IEC958 Default PCM Playback Switch",
  3120. .info = snd_ctl_boolean_mono_info,
  3121. .get = spdif_share_sw_get,
  3122. .put = spdif_share_sw_put,
  3123. };
  3124. /**
  3125. * snd_hda_create_spdif_share_sw - create Default PCM switch
  3126. * @codec: the HDA codec
  3127. * @mout: multi-out instance
  3128. */
  3129. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  3130. struct hda_multi_out *mout)
  3131. {
  3132. struct snd_kcontrol *kctl;
  3133. if (!mout->dig_out_nid)
  3134. return 0;
  3135. kctl = snd_ctl_new1(&spdif_share_sw, mout);
  3136. if (!kctl)
  3137. return -ENOMEM;
  3138. /* ATTENTION: here mout is passed as private_data, instead of codec */
  3139. return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
  3140. }
  3141. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
  3142. /*
  3143. * SPDIF input
  3144. */
  3145. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  3146. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  3147. struct snd_ctl_elem_value *ucontrol)
  3148. {
  3149. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3150. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  3151. return 0;
  3152. }
  3153. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  3154. struct snd_ctl_elem_value *ucontrol)
  3155. {
  3156. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3157. hda_nid_t nid = kcontrol->private_value;
  3158. unsigned int val = !!ucontrol->value.integer.value[0];
  3159. int change;
  3160. mutex_lock(&codec->spdif_mutex);
  3161. change = codec->spdif_in_enable != val;
  3162. if (change) {
  3163. codec->spdif_in_enable = val;
  3164. snd_hda_codec_write_cache(codec, nid, 0,
  3165. AC_VERB_SET_DIGI_CONVERT_1, val);
  3166. }
  3167. mutex_unlock(&codec->spdif_mutex);
  3168. return change;
  3169. }
  3170. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  3171. struct snd_ctl_elem_value *ucontrol)
  3172. {
  3173. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3174. hda_nid_t nid = kcontrol->private_value;
  3175. unsigned short val;
  3176. unsigned int sbits;
  3177. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
  3178. sbits = convert_to_spdif_status(val);
  3179. ucontrol->value.iec958.status[0] = sbits;
  3180. ucontrol->value.iec958.status[1] = sbits >> 8;
  3181. ucontrol->value.iec958.status[2] = sbits >> 16;
  3182. ucontrol->value.iec958.status[3] = sbits >> 24;
  3183. return 0;
  3184. }
  3185. static struct snd_kcontrol_new dig_in_ctls[] = {
  3186. {
  3187. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3188. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  3189. .info = snd_hda_spdif_in_switch_info,
  3190. .get = snd_hda_spdif_in_switch_get,
  3191. .put = snd_hda_spdif_in_switch_put,
  3192. },
  3193. {
  3194. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  3195. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3196. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  3197. .info = snd_hda_spdif_mask_info,
  3198. .get = snd_hda_spdif_in_status_get,
  3199. },
  3200. { } /* end */
  3201. };
  3202. /**
  3203. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  3204. * @codec: the HDA codec
  3205. * @nid: audio in widget NID
  3206. *
  3207. * Creates controls related with the SPDIF input.
  3208. * Called from each patch supporting the SPDIF in.
  3209. *
  3210. * Returns 0 if successful, or a negative error code.
  3211. */
  3212. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  3213. {
  3214. int err;
  3215. struct snd_kcontrol *kctl;
  3216. struct snd_kcontrol_new *dig_mix;
  3217. int idx;
  3218. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
  3219. if (idx < 0) {
  3220. printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
  3221. return -EBUSY;
  3222. }
  3223. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  3224. kctl = snd_ctl_new1(dig_mix, codec);
  3225. if (!kctl)
  3226. return -ENOMEM;
  3227. kctl->private_value = nid;
  3228. err = snd_hda_ctl_add(codec, nid, kctl);
  3229. if (err < 0)
  3230. return err;
  3231. }
  3232. codec->spdif_in_enable =
  3233. snd_hda_codec_read(codec, nid, 0,
  3234. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  3235. AC_DIG1_ENABLE;
  3236. return 0;
  3237. }
  3238. EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
  3239. /*
  3240. * command cache
  3241. */
  3242. /* build a 31bit cache key with the widget id and the command parameter */
  3243. #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid)
  3244. #define get_cmd_cache_nid(key) ((key) & 0xff)
  3245. #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff)
  3246. /**
  3247. * snd_hda_codec_write_cache - send a single command with caching
  3248. * @codec: the HDA codec
  3249. * @nid: NID to send the command
  3250. * @flags: optional bit flags
  3251. * @verb: the verb to send
  3252. * @parm: the parameter for the verb
  3253. *
  3254. * Send a single command without waiting for response.
  3255. *
  3256. * Returns 0 if successful, or a negative error code.
  3257. */
  3258. int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
  3259. int flags, unsigned int verb, unsigned int parm)
  3260. {
  3261. int err;
  3262. struct hda_cache_head *c;
  3263. u32 key;
  3264. unsigned int cache_only;
  3265. cache_only = codec->cached_write;
  3266. if (!cache_only) {
  3267. err = snd_hda_codec_write(codec, nid, flags, verb, parm);
  3268. if (err < 0)
  3269. return err;
  3270. }
  3271. /* parm may contain the verb stuff for get/set amp */
  3272. verb = verb | (parm >> 8);
  3273. parm &= 0xff;
  3274. key = build_cmd_cache_key(nid, verb);
  3275. mutex_lock(&codec->bus->cmd_mutex);
  3276. c = get_alloc_hash(&codec->cmd_cache, key);
  3277. if (c) {
  3278. c->val = parm;
  3279. c->dirty = cache_only;
  3280. }
  3281. mutex_unlock(&codec->bus->cmd_mutex);
  3282. return 0;
  3283. }
  3284. EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
  3285. /**
  3286. * snd_hda_codec_update_cache - check cache and write the cmd only when needed
  3287. * @codec: the HDA codec
  3288. * @nid: NID to send the command
  3289. * @flags: optional bit flags
  3290. * @verb: the verb to send
  3291. * @parm: the parameter for the verb
  3292. *
  3293. * This function works like snd_hda_codec_write_cache(), but it doesn't send
  3294. * command if the parameter is already identical with the cached value.
  3295. * If not, it sends the command and refreshes the cache.
  3296. *
  3297. * Returns 0 if successful, or a negative error code.
  3298. */
  3299. int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
  3300. int flags, unsigned int verb, unsigned int parm)
  3301. {
  3302. struct hda_cache_head *c;
  3303. u32 key;
  3304. /* parm may contain the verb stuff for get/set amp */
  3305. verb = verb | (parm >> 8);
  3306. parm &= 0xff;
  3307. key = build_cmd_cache_key(nid, verb);
  3308. mutex_lock(&codec->bus->cmd_mutex);
  3309. c = get_hash(&codec->cmd_cache, key);
  3310. if (c && c->val == parm) {
  3311. mutex_unlock(&codec->bus->cmd_mutex);
  3312. return 0;
  3313. }
  3314. mutex_unlock(&codec->bus->cmd_mutex);
  3315. return snd_hda_codec_write_cache(codec, nid, flags, verb, parm);
  3316. }
  3317. EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
  3318. /**
  3319. * snd_hda_codec_resume_cache - Resume the all commands from the cache
  3320. * @codec: HD-audio codec
  3321. *
  3322. * Execute all verbs recorded in the command caches to resume.
  3323. */
  3324. void snd_hda_codec_resume_cache(struct hda_codec *codec)
  3325. {
  3326. int i;
  3327. mutex_lock(&codec->hash_mutex);
  3328. codec->cached_write = 0;
  3329. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3330. struct hda_cache_head *buffer;
  3331. u32 key;
  3332. buffer = snd_array_elem(&codec->cmd_cache.buf, i);
  3333. key = buffer->key;
  3334. if (!key)
  3335. continue;
  3336. if (!buffer->dirty)
  3337. continue;
  3338. buffer->dirty = 0;
  3339. mutex_unlock(&codec->hash_mutex);
  3340. snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
  3341. get_cmd_cache_cmd(key), buffer->val);
  3342. mutex_lock(&codec->hash_mutex);
  3343. }
  3344. mutex_unlock(&codec->hash_mutex);
  3345. }
  3346. EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
  3347. /**
  3348. * snd_hda_sequence_write_cache - sequence writes with caching
  3349. * @codec: the HDA codec
  3350. * @seq: VERB array to send
  3351. *
  3352. * Send the commands sequentially from the given array.
  3353. * Thte commands are recorded on cache for power-save and resume.
  3354. * The array must be terminated with NID=0.
  3355. */
  3356. void snd_hda_sequence_write_cache(struct hda_codec *codec,
  3357. const struct hda_verb *seq)
  3358. {
  3359. for (; seq->nid; seq++)
  3360. snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
  3361. seq->param);
  3362. }
  3363. EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
  3364. /**
  3365. * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs
  3366. * @codec: HD-audio codec
  3367. */
  3368. void snd_hda_codec_flush_cache(struct hda_codec *codec)
  3369. {
  3370. snd_hda_codec_resume_amp(codec);
  3371. snd_hda_codec_resume_cache(codec);
  3372. }
  3373. EXPORT_SYMBOL_HDA(snd_hda_codec_flush_cache);
  3374. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  3375. unsigned int power_state)
  3376. {
  3377. hda_nid_t nid = codec->start_nid;
  3378. int i;
  3379. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3380. unsigned int wcaps = get_wcaps(codec, nid);
  3381. unsigned int state = power_state;
  3382. if (!(wcaps & AC_WCAP_POWER))
  3383. continue;
  3384. if (codec->power_filter) {
  3385. state = codec->power_filter(codec, nid, power_state);
  3386. if (state != power_state && power_state == AC_PWRST_D3)
  3387. continue;
  3388. }
  3389. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  3390. state);
  3391. }
  3392. }
  3393. EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
  3394. /*
  3395. * supported power states check
  3396. */
  3397. static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg,
  3398. unsigned int power_state)
  3399. {
  3400. int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE);
  3401. if (sup == -1)
  3402. return false;
  3403. if (sup & power_state)
  3404. return true;
  3405. else
  3406. return false;
  3407. }
  3408. /*
  3409. * wait until the state is reached, returns the current state
  3410. */
  3411. static unsigned int hda_sync_power_state(struct hda_codec *codec,
  3412. hda_nid_t fg,
  3413. unsigned int power_state)
  3414. {
  3415. unsigned long end_time = jiffies + msecs_to_jiffies(500);
  3416. unsigned int state, actual_state;
  3417. for (;;) {
  3418. state = snd_hda_codec_read(codec, fg, 0,
  3419. AC_VERB_GET_POWER_STATE, 0);
  3420. if (state & AC_PWRST_ERROR)
  3421. break;
  3422. actual_state = (state >> 4) & 0x0f;
  3423. if (actual_state == power_state)
  3424. break;
  3425. if (time_after_eq(jiffies, end_time))
  3426. break;
  3427. /* wait until the codec reachs to the target state */
  3428. msleep(1);
  3429. }
  3430. return state;
  3431. }
  3432. /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */
  3433. unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
  3434. hda_nid_t nid,
  3435. unsigned int power_state)
  3436. {
  3437. if (power_state == AC_PWRST_D3 &&
  3438. get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
  3439. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  3440. int eapd = snd_hda_codec_read(codec, nid, 0,
  3441. AC_VERB_GET_EAPD_BTLENABLE, 0);
  3442. if (eapd & 0x02)
  3443. return AC_PWRST_D0;
  3444. }
  3445. return power_state;
  3446. }
  3447. EXPORT_SYMBOL_HDA(snd_hda_codec_eapd_power_filter);
  3448. /*
  3449. * set power state of the codec, and return the power state
  3450. */
  3451. static unsigned int hda_set_power_state(struct hda_codec *codec,
  3452. unsigned int power_state)
  3453. {
  3454. hda_nid_t fg = codec->afg ? codec->afg : codec->mfg;
  3455. int count;
  3456. unsigned int state;
  3457. int flags = 0;
  3458. /* this delay seems necessary to avoid click noise at power-down */
  3459. if (power_state == AC_PWRST_D3) {
  3460. /* transition time less than 10ms for power down */
  3461. msleep(codec->epss ? 10 : 100);
  3462. flags = HDA_RW_NO_RESPONSE_FALLBACK;
  3463. }
  3464. /* repeat power states setting at most 10 times*/
  3465. for (count = 0; count < 10; count++) {
  3466. if (codec->patch_ops.set_power_state)
  3467. codec->patch_ops.set_power_state(codec, fg,
  3468. power_state);
  3469. else {
  3470. snd_hda_codec_read(codec, fg, flags,
  3471. AC_VERB_SET_POWER_STATE,
  3472. power_state);
  3473. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  3474. }
  3475. state = hda_sync_power_state(codec, fg, power_state);
  3476. if (!(state & AC_PWRST_ERROR))
  3477. break;
  3478. }
  3479. return state;
  3480. }
  3481. /* sync power states of all widgets;
  3482. * this is called at the end of codec parsing
  3483. */
  3484. static void sync_power_up_states(struct hda_codec *codec)
  3485. {
  3486. hda_nid_t nid = codec->start_nid;
  3487. int i;
  3488. /* don't care if no filter is used */
  3489. if (!codec->power_filter)
  3490. return;
  3491. for (i = 0; i < codec->num_nodes; i++, nid++) {
  3492. unsigned int wcaps = get_wcaps(codec, nid);
  3493. unsigned int target;
  3494. if (!(wcaps & AC_WCAP_POWER))
  3495. continue;
  3496. target = codec->power_filter(codec, nid, AC_PWRST_D0);
  3497. if (target == AC_PWRST_D0)
  3498. continue;
  3499. if (!snd_hda_check_power_state(codec, nid, target))
  3500. snd_hda_codec_write(codec, nid, 0,
  3501. AC_VERB_SET_POWER_STATE, target);
  3502. }
  3503. }
  3504. #ifdef CONFIG_SND_HDA_HWDEP
  3505. /* execute additional init verbs */
  3506. static void hda_exec_init_verbs(struct hda_codec *codec)
  3507. {
  3508. if (codec->init_verbs.list)
  3509. snd_hda_sequence_write(codec, codec->init_verbs.list);
  3510. }
  3511. #else
  3512. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  3513. #endif
  3514. #ifdef CONFIG_PM
  3515. /*
  3516. * call suspend and power-down; used both from PM and power-save
  3517. * this function returns the power state in the end
  3518. */
  3519. static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq)
  3520. {
  3521. unsigned int state;
  3522. codec->in_pm = 1;
  3523. if (codec->patch_ops.suspend)
  3524. codec->patch_ops.suspend(codec);
  3525. hda_cleanup_all_streams(codec);
  3526. state = hda_set_power_state(codec, AC_PWRST_D3);
  3527. /* Cancel delayed work if we aren't currently running from it. */
  3528. if (!in_wq)
  3529. cancel_delayed_work_sync(&codec->power_work);
  3530. spin_lock(&codec->power_lock);
  3531. snd_hda_update_power_acct(codec);
  3532. trace_hda_power_down(codec);
  3533. codec->power_on = 0;
  3534. codec->power_transition = 0;
  3535. codec->power_jiffies = jiffies;
  3536. spin_unlock(&codec->power_lock);
  3537. codec->in_pm = 0;
  3538. return state;
  3539. }
  3540. /* mark all entries of cmd and amp caches dirty */
  3541. static void hda_mark_cmd_cache_dirty(struct hda_codec *codec)
  3542. {
  3543. int i;
  3544. for (i = 0; i < codec->cmd_cache.buf.used; i++) {
  3545. struct hda_cache_head *cmd;
  3546. cmd = snd_array_elem(&codec->cmd_cache.buf, i);
  3547. cmd->dirty = 1;
  3548. }
  3549. for (i = 0; i < codec->amp_cache.buf.used; i++) {
  3550. struct hda_amp_info *amp;
  3551. amp = snd_array_elem(&codec->amp_cache.buf, i);
  3552. amp->head.dirty = 1;
  3553. }
  3554. }
  3555. /*
  3556. * kick up codec; used both from PM and power-save
  3557. */
  3558. static void hda_call_codec_resume(struct hda_codec *codec)
  3559. {
  3560. codec->in_pm = 1;
  3561. hda_mark_cmd_cache_dirty(codec);
  3562. /* set as if powered on for avoiding re-entering the resume
  3563. * in the resume / power-save sequence
  3564. */
  3565. hda_keep_power_on(codec);
  3566. hda_set_power_state(codec, AC_PWRST_D0);
  3567. restore_shutup_pins(codec);
  3568. hda_exec_init_verbs(codec);
  3569. snd_hda_jack_set_dirty_all(codec);
  3570. if (codec->patch_ops.resume)
  3571. codec->patch_ops.resume(codec);
  3572. else {
  3573. if (codec->patch_ops.init)
  3574. codec->patch_ops.init(codec);
  3575. snd_hda_codec_resume_amp(codec);
  3576. snd_hda_codec_resume_cache(codec);
  3577. }
  3578. if (codec->jackpoll_interval)
  3579. hda_jackpoll_work(&codec->jackpoll_work.work);
  3580. else
  3581. snd_hda_jack_report_sync(codec);
  3582. codec->in_pm = 0;
  3583. snd_hda_power_down(codec); /* flag down before returning */
  3584. }
  3585. #endif /* CONFIG_PM */
  3586. /**
  3587. * snd_hda_build_controls - build mixer controls
  3588. * @bus: the BUS
  3589. *
  3590. * Creates mixer controls for each codec included in the bus.
  3591. *
  3592. * Returns 0 if successful, otherwise a negative error code.
  3593. */
  3594. int snd_hda_build_controls(struct hda_bus *bus)
  3595. {
  3596. struct hda_codec *codec;
  3597. list_for_each_entry(codec, &bus->codec_list, list) {
  3598. int err = snd_hda_codec_build_controls(codec);
  3599. if (err < 0) {
  3600. printk(KERN_ERR "hda_codec: cannot build controls "
  3601. "for #%d (error %d)\n", codec->addr, err);
  3602. err = snd_hda_codec_reset(codec);
  3603. if (err < 0) {
  3604. printk(KERN_ERR
  3605. "hda_codec: cannot revert codec\n");
  3606. return err;
  3607. }
  3608. }
  3609. }
  3610. return 0;
  3611. }
  3612. EXPORT_SYMBOL_HDA(snd_hda_build_controls);
  3613. /*
  3614. * add standard channel maps if not specified
  3615. */
  3616. static int add_std_chmaps(struct hda_codec *codec)
  3617. {
  3618. int i, str, err;
  3619. for (i = 0; i < codec->num_pcms; i++) {
  3620. for (str = 0; str < 2; str++) {
  3621. struct snd_pcm *pcm = codec->pcm_info[i].pcm;
  3622. struct hda_pcm_stream *hinfo =
  3623. &codec->pcm_info[i].stream[str];
  3624. struct snd_pcm_chmap *chmap;
  3625. const struct snd_pcm_chmap_elem *elem;
  3626. if (codec->pcm_info[i].own_chmap)
  3627. continue;
  3628. if (!pcm || !hinfo->substreams)
  3629. continue;
  3630. elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
  3631. err = snd_pcm_add_chmap_ctls(pcm, str, elem,
  3632. hinfo->channels_max,
  3633. 0, &chmap);
  3634. if (err < 0)
  3635. return err;
  3636. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  3637. }
  3638. }
  3639. return 0;
  3640. }
  3641. /* default channel maps for 2.1 speakers;
  3642. * since HD-audio supports only stereo, odd number channels are omitted
  3643. */
  3644. const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
  3645. { .channels = 2,
  3646. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  3647. { .channels = 4,
  3648. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  3649. SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
  3650. { }
  3651. };
  3652. EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
  3653. int snd_hda_codec_build_controls(struct hda_codec *codec)
  3654. {
  3655. int err = 0;
  3656. hda_exec_init_verbs(codec);
  3657. /* continue to initialize... */
  3658. if (codec->patch_ops.init)
  3659. err = codec->patch_ops.init(codec);
  3660. if (!err && codec->patch_ops.build_controls)
  3661. err = codec->patch_ops.build_controls(codec);
  3662. if (err < 0)
  3663. return err;
  3664. /* we create chmaps here instead of build_pcms */
  3665. err = add_std_chmaps(codec);
  3666. if (err < 0)
  3667. return err;
  3668. if (codec->jackpoll_interval)
  3669. hda_jackpoll_work(&codec->jackpoll_work.work);
  3670. else
  3671. snd_hda_jack_report_sync(codec); /* call at the last init point */
  3672. sync_power_up_states(codec);
  3673. return 0;
  3674. }
  3675. /*
  3676. * stream formats
  3677. */
  3678. struct hda_rate_tbl {
  3679. unsigned int hz;
  3680. unsigned int alsa_bits;
  3681. unsigned int hda_fmt;
  3682. };
  3683. /* rate = base * mult / div */
  3684. #define HDA_RATE(base, mult, div) \
  3685. (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
  3686. (((div) - 1) << AC_FMT_DIV_SHIFT))
  3687. static struct hda_rate_tbl rate_bits[] = {
  3688. /* rate in Hz, ALSA rate bitmask, HDA format value */
  3689. /* autodetected value used in snd_hda_query_supported_pcm */
  3690. { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
  3691. { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
  3692. { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
  3693. { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
  3694. { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
  3695. { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
  3696. { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
  3697. { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
  3698. { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
  3699. { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
  3700. { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
  3701. #define AC_PAR_PCM_RATE_BITS 11
  3702. /* up to bits 10, 384kHZ isn't supported properly */
  3703. /* not autodetected value */
  3704. { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
  3705. { 0 } /* terminator */
  3706. };
  3707. /**
  3708. * snd_hda_calc_stream_format - calculate format bitset
  3709. * @rate: the sample rate
  3710. * @channels: the number of channels
  3711. * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
  3712. * @maxbps: the max. bps
  3713. *
  3714. * Calculate the format bitset from the given rate, channels and th PCM format.
  3715. *
  3716. * Return zero if invalid.
  3717. */
  3718. unsigned int snd_hda_calc_stream_format(unsigned int rate,
  3719. unsigned int channels,
  3720. unsigned int format,
  3721. unsigned int maxbps,
  3722. unsigned short spdif_ctls)
  3723. {
  3724. int i;
  3725. unsigned int val = 0;
  3726. for (i = 0; rate_bits[i].hz; i++)
  3727. if (rate_bits[i].hz == rate) {
  3728. val = rate_bits[i].hda_fmt;
  3729. break;
  3730. }
  3731. if (!rate_bits[i].hz) {
  3732. snd_printdd("invalid rate %d\n", rate);
  3733. return 0;
  3734. }
  3735. if (channels == 0 || channels > 8) {
  3736. snd_printdd("invalid channels %d\n", channels);
  3737. return 0;
  3738. }
  3739. val |= channels - 1;
  3740. switch (snd_pcm_format_width(format)) {
  3741. case 8:
  3742. val |= AC_FMT_BITS_8;
  3743. break;
  3744. case 16:
  3745. val |= AC_FMT_BITS_16;
  3746. break;
  3747. case 20:
  3748. case 24:
  3749. case 32:
  3750. if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
  3751. val |= AC_FMT_BITS_32;
  3752. else if (maxbps >= 24)
  3753. val |= AC_FMT_BITS_24;
  3754. else
  3755. val |= AC_FMT_BITS_20;
  3756. break;
  3757. default:
  3758. snd_printdd("invalid format width %d\n",
  3759. snd_pcm_format_width(format));
  3760. return 0;
  3761. }
  3762. if (spdif_ctls & AC_DIG1_NONAUDIO)
  3763. val |= AC_FMT_TYPE_NON_PCM;
  3764. return val;
  3765. }
  3766. EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
  3767. static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid,
  3768. int dir)
  3769. {
  3770. unsigned int val = 0;
  3771. if (nid != codec->afg &&
  3772. (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
  3773. val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
  3774. if (!val || val == -1)
  3775. val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
  3776. if (!val || val == -1)
  3777. return 0;
  3778. return val;
  3779. }
  3780. static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
  3781. {
  3782. return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid),
  3783. get_pcm_param);
  3784. }
  3785. static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid,
  3786. int dir)
  3787. {
  3788. unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
  3789. if (!streams || streams == -1)
  3790. streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
  3791. if (!streams || streams == -1)
  3792. return 0;
  3793. return streams;
  3794. }
  3795. static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
  3796. {
  3797. return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid),
  3798. get_stream_param);
  3799. }
  3800. /**
  3801. * snd_hda_query_supported_pcm - query the supported PCM rates and formats
  3802. * @codec: the HDA codec
  3803. * @nid: NID to query
  3804. * @ratesp: the pointer to store the detected rate bitflags
  3805. * @formatsp: the pointer to store the detected formats
  3806. * @bpsp: the pointer to store the detected format widths
  3807. *
  3808. * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp
  3809. * or @bsps argument is ignored.
  3810. *
  3811. * Returns 0 if successful, otherwise a negative error code.
  3812. */
  3813. int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
  3814. u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
  3815. {
  3816. unsigned int i, val, wcaps;
  3817. wcaps = get_wcaps(codec, nid);
  3818. val = query_pcm_param(codec, nid);
  3819. if (ratesp) {
  3820. u32 rates = 0;
  3821. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
  3822. if (val & (1 << i))
  3823. rates |= rate_bits[i].alsa_bits;
  3824. }
  3825. if (rates == 0) {
  3826. snd_printk(KERN_ERR "hda_codec: rates == 0 "
  3827. "(nid=0x%x, val=0x%x, ovrd=%i)\n",
  3828. nid, val,
  3829. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
  3830. return -EIO;
  3831. }
  3832. *ratesp = rates;
  3833. }
  3834. if (formatsp || bpsp) {
  3835. u64 formats = 0;
  3836. unsigned int streams, bps;
  3837. streams = query_stream_param(codec, nid);
  3838. if (!streams)
  3839. return -EIO;
  3840. bps = 0;
  3841. if (streams & AC_SUPFMT_PCM) {
  3842. if (val & AC_SUPPCM_BITS_8) {
  3843. formats |= SNDRV_PCM_FMTBIT_U8;
  3844. bps = 8;
  3845. }
  3846. if (val & AC_SUPPCM_BITS_16) {
  3847. formats |= SNDRV_PCM_FMTBIT_S16_LE;
  3848. bps = 16;
  3849. }
  3850. if (wcaps & AC_WCAP_DIGITAL) {
  3851. if (val & AC_SUPPCM_BITS_32)
  3852. formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
  3853. if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
  3854. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3855. if (val & AC_SUPPCM_BITS_24)
  3856. bps = 24;
  3857. else if (val & AC_SUPPCM_BITS_20)
  3858. bps = 20;
  3859. } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
  3860. AC_SUPPCM_BITS_32)) {
  3861. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  3862. if (val & AC_SUPPCM_BITS_32)
  3863. bps = 32;
  3864. else if (val & AC_SUPPCM_BITS_24)
  3865. bps = 24;
  3866. else if (val & AC_SUPPCM_BITS_20)
  3867. bps = 20;
  3868. }
  3869. }
  3870. #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */
  3871. if (streams & AC_SUPFMT_FLOAT32) {
  3872. formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
  3873. if (!bps)
  3874. bps = 32;
  3875. }
  3876. #endif
  3877. if (streams == AC_SUPFMT_AC3) {
  3878. /* should be exclusive */
  3879. /* temporary hack: we have still no proper support
  3880. * for the direct AC3 stream...
  3881. */
  3882. formats |= SNDRV_PCM_FMTBIT_U8;
  3883. bps = 8;
  3884. }
  3885. if (formats == 0) {
  3886. snd_printk(KERN_ERR "hda_codec: formats == 0 "
  3887. "(nid=0x%x, val=0x%x, ovrd=%i, "
  3888. "streams=0x%x)\n",
  3889. nid, val,
  3890. (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
  3891. streams);
  3892. return -EIO;
  3893. }
  3894. if (formatsp)
  3895. *formatsp = formats;
  3896. if (bpsp)
  3897. *bpsp = bps;
  3898. }
  3899. return 0;
  3900. }
  3901. EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
  3902. /**
  3903. * snd_hda_is_supported_format - Check the validity of the format
  3904. * @codec: HD-audio codec
  3905. * @nid: NID to check
  3906. * @format: the HD-audio format value to check
  3907. *
  3908. * Check whether the given node supports the format value.
  3909. *
  3910. * Returns 1 if supported, 0 if not.
  3911. */
  3912. int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
  3913. unsigned int format)
  3914. {
  3915. int i;
  3916. unsigned int val = 0, rate, stream;
  3917. val = query_pcm_param(codec, nid);
  3918. if (!val)
  3919. return 0;
  3920. rate = format & 0xff00;
  3921. for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
  3922. if (rate_bits[i].hda_fmt == rate) {
  3923. if (val & (1 << i))
  3924. break;
  3925. return 0;
  3926. }
  3927. if (i >= AC_PAR_PCM_RATE_BITS)
  3928. return 0;
  3929. stream = query_stream_param(codec, nid);
  3930. if (!stream)
  3931. return 0;
  3932. if (stream & AC_SUPFMT_PCM) {
  3933. switch (format & 0xf0) {
  3934. case 0x00:
  3935. if (!(val & AC_SUPPCM_BITS_8))
  3936. return 0;
  3937. break;
  3938. case 0x10:
  3939. if (!(val & AC_SUPPCM_BITS_16))
  3940. return 0;
  3941. break;
  3942. case 0x20:
  3943. if (!(val & AC_SUPPCM_BITS_20))
  3944. return 0;
  3945. break;
  3946. case 0x30:
  3947. if (!(val & AC_SUPPCM_BITS_24))
  3948. return 0;
  3949. break;
  3950. case 0x40:
  3951. if (!(val & AC_SUPPCM_BITS_32))
  3952. return 0;
  3953. break;
  3954. default:
  3955. return 0;
  3956. }
  3957. } else {
  3958. /* FIXME: check for float32 and AC3? */
  3959. }
  3960. return 1;
  3961. }
  3962. EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
  3963. /*
  3964. * PCM stuff
  3965. */
  3966. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  3967. struct hda_codec *codec,
  3968. struct snd_pcm_substream *substream)
  3969. {
  3970. return 0;
  3971. }
  3972. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  3973. struct hda_codec *codec,
  3974. unsigned int stream_tag,
  3975. unsigned int format,
  3976. struct snd_pcm_substream *substream)
  3977. {
  3978. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3979. return 0;
  3980. }
  3981. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  3982. struct hda_codec *codec,
  3983. struct snd_pcm_substream *substream)
  3984. {
  3985. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3986. return 0;
  3987. }
  3988. static int set_pcm_default_values(struct hda_codec *codec,
  3989. struct hda_pcm_stream *info)
  3990. {
  3991. int err;
  3992. /* query support PCM information from the given NID */
  3993. if (info->nid && (!info->rates || !info->formats)) {
  3994. err = snd_hda_query_supported_pcm(codec, info->nid,
  3995. info->rates ? NULL : &info->rates,
  3996. info->formats ? NULL : &info->formats,
  3997. info->maxbps ? NULL : &info->maxbps);
  3998. if (err < 0)
  3999. return err;
  4000. }
  4001. if (info->ops.open == NULL)
  4002. info->ops.open = hda_pcm_default_open_close;
  4003. if (info->ops.close == NULL)
  4004. info->ops.close = hda_pcm_default_open_close;
  4005. if (info->ops.prepare == NULL) {
  4006. if (snd_BUG_ON(!info->nid))
  4007. return -EINVAL;
  4008. info->ops.prepare = hda_pcm_default_prepare;
  4009. }
  4010. if (info->ops.cleanup == NULL) {
  4011. if (snd_BUG_ON(!info->nid))
  4012. return -EINVAL;
  4013. info->ops.cleanup = hda_pcm_default_cleanup;
  4014. }
  4015. return 0;
  4016. }
  4017. /*
  4018. * codec prepare/cleanup entries
  4019. */
  4020. int snd_hda_codec_prepare(struct hda_codec *codec,
  4021. struct hda_pcm_stream *hinfo,
  4022. unsigned int stream,
  4023. unsigned int format,
  4024. struct snd_pcm_substream *substream)
  4025. {
  4026. int ret;
  4027. mutex_lock(&codec->bus->prepare_mutex);
  4028. ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
  4029. if (ret >= 0)
  4030. purify_inactive_streams(codec);
  4031. mutex_unlock(&codec->bus->prepare_mutex);
  4032. return ret;
  4033. }
  4034. EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
  4035. void snd_hda_codec_cleanup(struct hda_codec *codec,
  4036. struct hda_pcm_stream *hinfo,
  4037. struct snd_pcm_substream *substream)
  4038. {
  4039. mutex_lock(&codec->bus->prepare_mutex);
  4040. hinfo->ops.cleanup(hinfo, codec, substream);
  4041. mutex_unlock(&codec->bus->prepare_mutex);
  4042. }
  4043. EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
  4044. /* global */
  4045. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  4046. "Audio", "SPDIF", "HDMI", "Modem"
  4047. };
  4048. /*
  4049. * get the empty PCM device number to assign
  4050. */
  4051. static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
  4052. {
  4053. /* audio device indices; not linear to keep compatibility */
  4054. /* assigned to static slots up to dev#10; if more needed, assign
  4055. * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
  4056. */
  4057. static int audio_idx[HDA_PCM_NTYPES][5] = {
  4058. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  4059. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  4060. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  4061. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  4062. };
  4063. int i;
  4064. if (type >= HDA_PCM_NTYPES) {
  4065. snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
  4066. return -EINVAL;
  4067. }
  4068. for (i = 0; audio_idx[type][i] >= 0; i++) {
  4069. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4070. if (audio_idx[type][i] >= 8)
  4071. break;
  4072. #endif
  4073. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  4074. return audio_idx[type][i];
  4075. }
  4076. #ifdef CONFIG_SND_DYNAMIC_MINORS
  4077. /* non-fixed slots starting from 10 */
  4078. for (i = 10; i < 32; i++) {
  4079. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  4080. return i;
  4081. }
  4082. #endif
  4083. snd_printk(KERN_WARNING "Too many %s devices\n",
  4084. snd_hda_pcm_type_name[type]);
  4085. #ifndef CONFIG_SND_DYNAMIC_MINORS
  4086. snd_printk(KERN_WARNING "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
  4087. #endif
  4088. return -EAGAIN;
  4089. }
  4090. /*
  4091. * attach a new PCM stream
  4092. */
  4093. static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
  4094. {
  4095. struct hda_bus *bus = codec->bus;
  4096. struct hda_pcm_stream *info;
  4097. int stream, err;
  4098. if (snd_BUG_ON(!pcm->name))
  4099. return -EINVAL;
  4100. for (stream = 0; stream < 2; stream++) {
  4101. info = &pcm->stream[stream];
  4102. if (info->substreams) {
  4103. err = set_pcm_default_values(codec, info);
  4104. if (err < 0)
  4105. return err;
  4106. }
  4107. }
  4108. return bus->ops.attach_pcm(bus, codec, pcm);
  4109. }
  4110. /* assign all PCMs of the given codec */
  4111. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  4112. {
  4113. unsigned int pcm;
  4114. int err;
  4115. if (!codec->num_pcms) {
  4116. if (!codec->patch_ops.build_pcms)
  4117. return 0;
  4118. err = codec->patch_ops.build_pcms(codec);
  4119. if (err < 0) {
  4120. printk(KERN_ERR "hda_codec: cannot build PCMs"
  4121. "for #%d (error %d)\n", codec->addr, err);
  4122. err = snd_hda_codec_reset(codec);
  4123. if (err < 0) {
  4124. printk(KERN_ERR
  4125. "hda_codec: cannot revert codec\n");
  4126. return err;
  4127. }
  4128. }
  4129. }
  4130. for (pcm = 0; pcm < codec->num_pcms; pcm++) {
  4131. struct hda_pcm *cpcm = &codec->pcm_info[pcm];
  4132. int dev;
  4133. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  4134. continue; /* no substreams assigned */
  4135. if (!cpcm->pcm) {
  4136. dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
  4137. if (dev < 0)
  4138. continue; /* no fatal error */
  4139. cpcm->device = dev;
  4140. err = snd_hda_attach_pcm(codec, cpcm);
  4141. if (err < 0) {
  4142. printk(KERN_ERR "hda_codec: cannot attach "
  4143. "PCM stream %d for codec #%d\n",
  4144. dev, codec->addr);
  4145. continue; /* no fatal error */
  4146. }
  4147. }
  4148. }
  4149. return 0;
  4150. }
  4151. /**
  4152. * snd_hda_build_pcms - build PCM information
  4153. * @bus: the BUS
  4154. *
  4155. * Create PCM information for each codec included in the bus.
  4156. *
  4157. * The build_pcms codec patch is requested to set up codec->num_pcms and
  4158. * codec->pcm_info properly. The array is referred by the top-level driver
  4159. * to create its PCM instances.
  4160. * The allocated codec->pcm_info should be released in codec->patch_ops.free
  4161. * callback.
  4162. *
  4163. * At least, substreams, channels_min and channels_max must be filled for
  4164. * each stream. substreams = 0 indicates that the stream doesn't exist.
  4165. * When rates and/or formats are zero, the supported values are queried
  4166. * from the given nid. The nid is used also by the default ops.prepare
  4167. * and ops.cleanup callbacks.
  4168. *
  4169. * The driver needs to call ops.open in its open callback. Similarly,
  4170. * ops.close is supposed to be called in the close callback.
  4171. * ops.prepare should be called in the prepare or hw_params callback
  4172. * with the proper parameters for set up.
  4173. * ops.cleanup should be called in hw_free for clean up of streams.
  4174. *
  4175. * This function returns 0 if successful, or a negative error code.
  4176. */
  4177. int snd_hda_build_pcms(struct hda_bus *bus)
  4178. {
  4179. struct hda_codec *codec;
  4180. list_for_each_entry(codec, &bus->codec_list, list) {
  4181. int err = snd_hda_codec_build_pcms(codec);
  4182. if (err < 0)
  4183. return err;
  4184. }
  4185. return 0;
  4186. }
  4187. EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
  4188. /**
  4189. * snd_hda_check_board_config - compare the current codec with the config table
  4190. * @codec: the HDA codec
  4191. * @num_configs: number of config enums
  4192. * @models: array of model name strings
  4193. * @tbl: configuration table, terminated by null entries
  4194. *
  4195. * Compares the modelname or PCI subsystem id of the current codec with the
  4196. * given configuration table. If a matching entry is found, returns its
  4197. * config value (supposed to be 0 or positive).
  4198. *
  4199. * If no entries are matching, the function returns a negative value.
  4200. */
  4201. int snd_hda_check_board_config(struct hda_codec *codec,
  4202. int num_configs, const char * const *models,
  4203. const struct snd_pci_quirk *tbl)
  4204. {
  4205. if (codec->modelname && models) {
  4206. int i;
  4207. for (i = 0; i < num_configs; i++) {
  4208. if (models[i] &&
  4209. !strcmp(codec->modelname, models[i])) {
  4210. snd_printd(KERN_INFO "hda_codec: model '%s' is "
  4211. "selected\n", models[i]);
  4212. return i;
  4213. }
  4214. }
  4215. }
  4216. if (!codec->bus->pci || !tbl)
  4217. return -1;
  4218. tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
  4219. if (!tbl)
  4220. return -1;
  4221. if (tbl->value >= 0 && tbl->value < num_configs) {
  4222. #ifdef CONFIG_SND_DEBUG_VERBOSE
  4223. char tmp[10];
  4224. const char *model = NULL;
  4225. if (models)
  4226. model = models[tbl->value];
  4227. if (!model) {
  4228. sprintf(tmp, "#%d", tbl->value);
  4229. model = tmp;
  4230. }
  4231. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  4232. "for config %x:%x (%s)\n",
  4233. model, tbl->subvendor, tbl->subdevice,
  4234. (tbl->name ? tbl->name : "Unknown device"));
  4235. #endif
  4236. return tbl->value;
  4237. }
  4238. return -1;
  4239. }
  4240. EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
  4241. /**
  4242. * snd_hda_check_board_codec_sid_config - compare the current codec
  4243. subsystem ID with the
  4244. config table
  4245. This is important for Gateway notebooks with SB450 HDA Audio
  4246. where the vendor ID of the PCI device is:
  4247. ATI Technologies Inc SB450 HDA Audio [1002:437b]
  4248. and the vendor/subvendor are found only at the codec.
  4249. * @codec: the HDA codec
  4250. * @num_configs: number of config enums
  4251. * @models: array of model name strings
  4252. * @tbl: configuration table, terminated by null entries
  4253. *
  4254. * Compares the modelname or PCI subsystem id of the current codec with the
  4255. * given configuration table. If a matching entry is found, returns its
  4256. * config value (supposed to be 0 or positive).
  4257. *
  4258. * If no entries are matching, the function returns a negative value.
  4259. */
  4260. int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
  4261. int num_configs, const char * const *models,
  4262. const struct snd_pci_quirk *tbl)
  4263. {
  4264. const struct snd_pci_quirk *q;
  4265. /* Search for codec ID */
  4266. for (q = tbl; q->subvendor; q++) {
  4267. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  4268. unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
  4269. if ((codec->subsystem_id & mask) == id)
  4270. break;
  4271. }
  4272. if (!q->subvendor)
  4273. return -1;
  4274. tbl = q;
  4275. if (tbl->value >= 0 && tbl->value < num_configs) {
  4276. #ifdef CONFIG_SND_DEBUG_VERBOSE
  4277. char tmp[10];
  4278. const char *model = NULL;
  4279. if (models)
  4280. model = models[tbl->value];
  4281. if (!model) {
  4282. sprintf(tmp, "#%d", tbl->value);
  4283. model = tmp;
  4284. }
  4285. snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
  4286. "for config %x:%x (%s)\n",
  4287. model, tbl->subvendor, tbl->subdevice,
  4288. (tbl->name ? tbl->name : "Unknown device"));
  4289. #endif
  4290. return tbl->value;
  4291. }
  4292. return -1;
  4293. }
  4294. EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
  4295. /**
  4296. * snd_hda_add_new_ctls - create controls from the array
  4297. * @codec: the HDA codec
  4298. * @knew: the array of struct snd_kcontrol_new
  4299. *
  4300. * This helper function creates and add new controls in the given array.
  4301. * The array must be terminated with an empty entry as terminator.
  4302. *
  4303. * Returns 0 if successful, or a negative error code.
  4304. */
  4305. int snd_hda_add_new_ctls(struct hda_codec *codec,
  4306. const struct snd_kcontrol_new *knew)
  4307. {
  4308. int err;
  4309. for (; knew->name; knew++) {
  4310. struct snd_kcontrol *kctl;
  4311. int addr = 0, idx = 0;
  4312. if (knew->iface == -1) /* skip this codec private value */
  4313. continue;
  4314. for (;;) {
  4315. kctl = snd_ctl_new1(knew, codec);
  4316. if (!kctl)
  4317. return -ENOMEM;
  4318. if (addr > 0)
  4319. kctl->id.device = addr;
  4320. if (idx > 0)
  4321. kctl->id.index = idx;
  4322. err = snd_hda_ctl_add(codec, 0, kctl);
  4323. if (!err)
  4324. break;
  4325. /* try first with another device index corresponding to
  4326. * the codec addr; if it still fails (or it's the
  4327. * primary codec), then try another control index
  4328. */
  4329. if (!addr && codec->addr)
  4330. addr = codec->addr;
  4331. else if (!idx && !knew->index) {
  4332. idx = find_empty_mixer_ctl_idx(codec,
  4333. knew->name, 0);
  4334. if (idx <= 0)
  4335. return err;
  4336. } else
  4337. return err;
  4338. }
  4339. }
  4340. return 0;
  4341. }
  4342. EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
  4343. #ifdef CONFIG_PM
  4344. static void hda_power_work(struct work_struct *work)
  4345. {
  4346. struct hda_codec *codec =
  4347. container_of(work, struct hda_codec, power_work.work);
  4348. struct hda_bus *bus = codec->bus;
  4349. unsigned int state;
  4350. spin_lock(&codec->power_lock);
  4351. if (codec->power_transition > 0) { /* during power-up sequence? */
  4352. spin_unlock(&codec->power_lock);
  4353. return;
  4354. }
  4355. if (!codec->power_on || codec->power_count) {
  4356. codec->power_transition = 0;
  4357. spin_unlock(&codec->power_lock);
  4358. return;
  4359. }
  4360. spin_unlock(&codec->power_lock);
  4361. state = hda_call_codec_suspend(codec, true);
  4362. codec->pm_down_notified = 0;
  4363. if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
  4364. codec->pm_down_notified = 1;
  4365. hda_call_pm_notify(bus, false);
  4366. }
  4367. }
  4368. static void hda_keep_power_on(struct hda_codec *codec)
  4369. {
  4370. spin_lock(&codec->power_lock);
  4371. codec->power_count++;
  4372. codec->power_on = 1;
  4373. codec->power_jiffies = jiffies;
  4374. spin_unlock(&codec->power_lock);
  4375. }
  4376. /* update the power on/off account with the current jiffies */
  4377. void snd_hda_update_power_acct(struct hda_codec *codec)
  4378. {
  4379. unsigned long delta = jiffies - codec->power_jiffies;
  4380. if (codec->power_on)
  4381. codec->power_on_acct += delta;
  4382. else
  4383. codec->power_off_acct += delta;
  4384. codec->power_jiffies += delta;
  4385. }
  4386. /* Transition to powered up, if wait_power_down then wait for a pending
  4387. * transition to D3 to complete. A pending D3 transition is indicated
  4388. * with power_transition == -1. */
  4389. /* call this with codec->power_lock held! */
  4390. static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down)
  4391. {
  4392. struct hda_bus *bus = codec->bus;
  4393. /* Return if power_on or transitioning to power_on, unless currently
  4394. * powering down. */
  4395. if ((codec->power_on || codec->power_transition > 0) &&
  4396. !(wait_power_down && codec->power_transition < 0))
  4397. return;
  4398. spin_unlock(&codec->power_lock);
  4399. cancel_delayed_work_sync(&codec->power_work);
  4400. spin_lock(&codec->power_lock);
  4401. /* If the power down delayed work was cancelled above before starting,
  4402. * then there is no need to go through power up here.
  4403. */
  4404. if (codec->power_on) {
  4405. if (codec->power_transition < 0)
  4406. codec->power_transition = 0;
  4407. return;
  4408. }
  4409. trace_hda_power_up(codec);
  4410. snd_hda_update_power_acct(codec);
  4411. codec->power_on = 1;
  4412. codec->power_jiffies = jiffies;
  4413. codec->power_transition = 1; /* avoid reentrance */
  4414. spin_unlock(&codec->power_lock);
  4415. if (codec->pm_down_notified) {
  4416. codec->pm_down_notified = 0;
  4417. hda_call_pm_notify(bus, true);
  4418. }
  4419. hda_call_codec_resume(codec);
  4420. spin_lock(&codec->power_lock);
  4421. codec->power_transition = 0;
  4422. }
  4423. #define power_save(codec) \
  4424. ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
  4425. /* Transition to powered down */
  4426. static void __snd_hda_power_down(struct hda_codec *codec)
  4427. {
  4428. if (!codec->power_on || codec->power_count || codec->power_transition)
  4429. return;
  4430. if (power_save(codec)) {
  4431. codec->power_transition = -1; /* avoid reentrance */
  4432. queue_delayed_work(codec->bus->workq, &codec->power_work,
  4433. msecs_to_jiffies(power_save(codec) * 1000));
  4434. }
  4435. }
  4436. /**
  4437. * snd_hda_power_save - Power-up/down/sync the codec
  4438. * @codec: HD-audio codec
  4439. * @delta: the counter delta to change
  4440. *
  4441. * Change the power-up counter via @delta, and power up or down the hardware
  4442. * appropriately. For the power-down, queue to the delayed action.
  4443. * Passing zero to @delta means to synchronize the power state.
  4444. */
  4445. void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait)
  4446. {
  4447. spin_lock(&codec->power_lock);
  4448. codec->power_count += delta;
  4449. trace_hda_power_count(codec);
  4450. if (delta > 0)
  4451. __snd_hda_power_up(codec, d3wait);
  4452. else
  4453. __snd_hda_power_down(codec);
  4454. spin_unlock(&codec->power_lock);
  4455. }
  4456. EXPORT_SYMBOL_HDA(snd_hda_power_save);
  4457. /**
  4458. * snd_hda_check_amp_list_power - Check the amp list and update the power
  4459. * @codec: HD-audio codec
  4460. * @check: the object containing an AMP list and the status
  4461. * @nid: NID to check / update
  4462. *
  4463. * Check whether the given NID is in the amp list. If it's in the list,
  4464. * check the current AMP status, and update the the power-status according
  4465. * to the mute status.
  4466. *
  4467. * This function is supposed to be set or called from the check_power_status
  4468. * patch ops.
  4469. */
  4470. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  4471. struct hda_loopback_check *check,
  4472. hda_nid_t nid)
  4473. {
  4474. const struct hda_amp_list *p;
  4475. int ch, v;
  4476. if (!check->amplist)
  4477. return 0;
  4478. for (p = check->amplist; p->nid; p++) {
  4479. if (p->nid == nid)
  4480. break;
  4481. }
  4482. if (!p->nid)
  4483. return 0; /* nothing changed */
  4484. for (p = check->amplist; p->nid; p++) {
  4485. for (ch = 0; ch < 2; ch++) {
  4486. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  4487. p->idx);
  4488. if (!(v & HDA_AMP_MUTE) && v > 0) {
  4489. if (!check->power_on) {
  4490. check->power_on = 1;
  4491. snd_hda_power_up(codec);
  4492. }
  4493. return 1;
  4494. }
  4495. }
  4496. }
  4497. if (check->power_on) {
  4498. check->power_on = 0;
  4499. snd_hda_power_down(codec);
  4500. }
  4501. return 0;
  4502. }
  4503. EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
  4504. #endif
  4505. /*
  4506. * Channel mode helper
  4507. */
  4508. /**
  4509. * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
  4510. */
  4511. int snd_hda_ch_mode_info(struct hda_codec *codec,
  4512. struct snd_ctl_elem_info *uinfo,
  4513. const struct hda_channel_mode *chmode,
  4514. int num_chmodes)
  4515. {
  4516. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4517. uinfo->count = 1;
  4518. uinfo->value.enumerated.items = num_chmodes;
  4519. if (uinfo->value.enumerated.item >= num_chmodes)
  4520. uinfo->value.enumerated.item = num_chmodes - 1;
  4521. sprintf(uinfo->value.enumerated.name, "%dch",
  4522. chmode[uinfo->value.enumerated.item].channels);
  4523. return 0;
  4524. }
  4525. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
  4526. /**
  4527. * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
  4528. */
  4529. int snd_hda_ch_mode_get(struct hda_codec *codec,
  4530. struct snd_ctl_elem_value *ucontrol,
  4531. const struct hda_channel_mode *chmode,
  4532. int num_chmodes,
  4533. int max_channels)
  4534. {
  4535. int i;
  4536. for (i = 0; i < num_chmodes; i++) {
  4537. if (max_channels == chmode[i].channels) {
  4538. ucontrol->value.enumerated.item[0] = i;
  4539. break;
  4540. }
  4541. }
  4542. return 0;
  4543. }
  4544. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
  4545. /**
  4546. * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
  4547. */
  4548. int snd_hda_ch_mode_put(struct hda_codec *codec,
  4549. struct snd_ctl_elem_value *ucontrol,
  4550. const struct hda_channel_mode *chmode,
  4551. int num_chmodes,
  4552. int *max_channelsp)
  4553. {
  4554. unsigned int mode;
  4555. mode = ucontrol->value.enumerated.item[0];
  4556. if (mode >= num_chmodes)
  4557. return -EINVAL;
  4558. if (*max_channelsp == chmode[mode].channels)
  4559. return 0;
  4560. /* change the current channel setting */
  4561. *max_channelsp = chmode[mode].channels;
  4562. if (chmode[mode].sequence)
  4563. snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
  4564. return 1;
  4565. }
  4566. EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
  4567. /*
  4568. * input MUX helper
  4569. */
  4570. /**
  4571. * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
  4572. */
  4573. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  4574. struct snd_ctl_elem_info *uinfo)
  4575. {
  4576. unsigned int index;
  4577. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4578. uinfo->count = 1;
  4579. uinfo->value.enumerated.items = imux->num_items;
  4580. if (!imux->num_items)
  4581. return 0;
  4582. index = uinfo->value.enumerated.item;
  4583. if (index >= imux->num_items)
  4584. index = imux->num_items - 1;
  4585. strcpy(uinfo->value.enumerated.name, imux->items[index].label);
  4586. return 0;
  4587. }
  4588. EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
  4589. /**
  4590. * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
  4591. */
  4592. int snd_hda_input_mux_put(struct hda_codec *codec,
  4593. const struct hda_input_mux *imux,
  4594. struct snd_ctl_elem_value *ucontrol,
  4595. hda_nid_t nid,
  4596. unsigned int *cur_val)
  4597. {
  4598. unsigned int idx;
  4599. if (!imux->num_items)
  4600. return 0;
  4601. idx = ucontrol->value.enumerated.item[0];
  4602. if (idx >= imux->num_items)
  4603. idx = imux->num_items - 1;
  4604. if (*cur_val == idx)
  4605. return 0;
  4606. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  4607. imux->items[idx].index);
  4608. *cur_val = idx;
  4609. return 1;
  4610. }
  4611. EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
  4612. /*
  4613. * process kcontrol info callback of a simple string enum array
  4614. * when @num_items is 0 or @texts is NULL, assume a boolean enum array
  4615. */
  4616. int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
  4617. struct snd_ctl_elem_info *uinfo,
  4618. int num_items, const char * const *texts)
  4619. {
  4620. static const char * const texts_default[] = {
  4621. "Disabled", "Enabled"
  4622. };
  4623. if (!texts || !num_items) {
  4624. num_items = 2;
  4625. texts = texts_default;
  4626. }
  4627. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  4628. uinfo->count = 1;
  4629. uinfo->value.enumerated.items = num_items;
  4630. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  4631. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  4632. strcpy(uinfo->value.enumerated.name,
  4633. texts[uinfo->value.enumerated.item]);
  4634. return 0;
  4635. }
  4636. EXPORT_SYMBOL_HDA(snd_hda_enum_helper_info);
  4637. /*
  4638. * Multi-channel / digital-out PCM helper functions
  4639. */
  4640. /* setup SPDIF output stream */
  4641. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  4642. unsigned int stream_tag, unsigned int format)
  4643. {
  4644. struct hda_spdif_out *spdif;
  4645. unsigned int curr_fmt;
  4646. bool reset;
  4647. spdif = snd_hda_spdif_out_of_nid(codec, nid);
  4648. curr_fmt = snd_hda_codec_read(codec, nid, 0,
  4649. AC_VERB_GET_STREAM_FORMAT, 0);
  4650. reset = codec->spdif_status_reset &&
  4651. (spdif->ctls & AC_DIG1_ENABLE) &&
  4652. curr_fmt != format;
  4653. /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
  4654. updated */
  4655. if (reset)
  4656. set_dig_out_convert(codec, nid,
  4657. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  4658. -1);
  4659. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  4660. if (codec->slave_dig_outs) {
  4661. const hda_nid_t *d;
  4662. for (d = codec->slave_dig_outs; *d; d++)
  4663. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  4664. format);
  4665. }
  4666. /* turn on again (if needed) */
  4667. if (reset)
  4668. set_dig_out_convert(codec, nid,
  4669. spdif->ctls & 0xff, -1);
  4670. }
  4671. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  4672. {
  4673. snd_hda_codec_cleanup_stream(codec, nid);
  4674. if (codec->slave_dig_outs) {
  4675. const hda_nid_t *d;
  4676. for (d = codec->slave_dig_outs; *d; d++)
  4677. snd_hda_codec_cleanup_stream(codec, *d);
  4678. }
  4679. }
  4680. /**
  4681. * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
  4682. * @bus: HD-audio bus
  4683. */
  4684. void snd_hda_bus_reboot_notify(struct hda_bus *bus)
  4685. {
  4686. struct hda_codec *codec;
  4687. if (!bus)
  4688. return;
  4689. list_for_each_entry(codec, &bus->codec_list, list) {
  4690. if (hda_codec_is_power_on(codec) &&
  4691. codec->patch_ops.reboot_notify)
  4692. codec->patch_ops.reboot_notify(codec);
  4693. }
  4694. }
  4695. EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
  4696. /**
  4697. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  4698. */
  4699. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  4700. struct hda_multi_out *mout)
  4701. {
  4702. mutex_lock(&codec->spdif_mutex);
  4703. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  4704. /* already opened as analog dup; reset it once */
  4705. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4706. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  4707. mutex_unlock(&codec->spdif_mutex);
  4708. return 0;
  4709. }
  4710. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
  4711. /**
  4712. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  4713. */
  4714. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  4715. struct hda_multi_out *mout,
  4716. unsigned int stream_tag,
  4717. unsigned int format,
  4718. struct snd_pcm_substream *substream)
  4719. {
  4720. mutex_lock(&codec->spdif_mutex);
  4721. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  4722. mutex_unlock(&codec->spdif_mutex);
  4723. return 0;
  4724. }
  4725. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
  4726. /**
  4727. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  4728. */
  4729. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  4730. struct hda_multi_out *mout)
  4731. {
  4732. mutex_lock(&codec->spdif_mutex);
  4733. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4734. mutex_unlock(&codec->spdif_mutex);
  4735. return 0;
  4736. }
  4737. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
  4738. /**
  4739. * snd_hda_multi_out_dig_close - release the digital out stream
  4740. */
  4741. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  4742. struct hda_multi_out *mout)
  4743. {
  4744. mutex_lock(&codec->spdif_mutex);
  4745. mout->dig_out_used = 0;
  4746. mutex_unlock(&codec->spdif_mutex);
  4747. return 0;
  4748. }
  4749. EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
  4750. /**
  4751. * snd_hda_multi_out_analog_open - open analog outputs
  4752. *
  4753. * Open analog outputs and set up the hw-constraints.
  4754. * If the digital outputs can be opened as slave, open the digital
  4755. * outputs, too.
  4756. */
  4757. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  4758. struct hda_multi_out *mout,
  4759. struct snd_pcm_substream *substream,
  4760. struct hda_pcm_stream *hinfo)
  4761. {
  4762. struct snd_pcm_runtime *runtime = substream->runtime;
  4763. runtime->hw.channels_max = mout->max_channels;
  4764. if (mout->dig_out_nid) {
  4765. if (!mout->analog_rates) {
  4766. mout->analog_rates = hinfo->rates;
  4767. mout->analog_formats = hinfo->formats;
  4768. mout->analog_maxbps = hinfo->maxbps;
  4769. } else {
  4770. runtime->hw.rates = mout->analog_rates;
  4771. runtime->hw.formats = mout->analog_formats;
  4772. hinfo->maxbps = mout->analog_maxbps;
  4773. }
  4774. if (!mout->spdif_rates) {
  4775. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  4776. &mout->spdif_rates,
  4777. &mout->spdif_formats,
  4778. &mout->spdif_maxbps);
  4779. }
  4780. mutex_lock(&codec->spdif_mutex);
  4781. if (mout->share_spdif) {
  4782. if ((runtime->hw.rates & mout->spdif_rates) &&
  4783. (runtime->hw.formats & mout->spdif_formats)) {
  4784. runtime->hw.rates &= mout->spdif_rates;
  4785. runtime->hw.formats &= mout->spdif_formats;
  4786. if (mout->spdif_maxbps < hinfo->maxbps)
  4787. hinfo->maxbps = mout->spdif_maxbps;
  4788. } else {
  4789. mout->share_spdif = 0;
  4790. /* FIXME: need notify? */
  4791. }
  4792. }
  4793. mutex_unlock(&codec->spdif_mutex);
  4794. }
  4795. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  4796. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  4797. }
  4798. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
  4799. /**
  4800. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  4801. *
  4802. * Set up the i/o for analog out.
  4803. * When the digital out is available, copy the front out to digital out, too.
  4804. */
  4805. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  4806. struct hda_multi_out *mout,
  4807. unsigned int stream_tag,
  4808. unsigned int format,
  4809. struct snd_pcm_substream *substream)
  4810. {
  4811. const hda_nid_t *nids = mout->dac_nids;
  4812. int chs = substream->runtime->channels;
  4813. struct hda_spdif_out *spdif;
  4814. int i;
  4815. mutex_lock(&codec->spdif_mutex);
  4816. spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  4817. if (mout->dig_out_nid && mout->share_spdif &&
  4818. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  4819. if (chs == 2 &&
  4820. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  4821. format) &&
  4822. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  4823. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  4824. setup_dig_out_stream(codec, mout->dig_out_nid,
  4825. stream_tag, format);
  4826. } else {
  4827. mout->dig_out_used = 0;
  4828. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4829. }
  4830. }
  4831. mutex_unlock(&codec->spdif_mutex);
  4832. /* front */
  4833. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  4834. 0, format);
  4835. if (!mout->no_share_stream &&
  4836. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  4837. /* headphone out will just decode front left/right (stereo) */
  4838. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  4839. 0, format);
  4840. /* extra outputs copied from front */
  4841. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  4842. if (!mout->no_share_stream && mout->hp_out_nid[i])
  4843. snd_hda_codec_setup_stream(codec,
  4844. mout->hp_out_nid[i],
  4845. stream_tag, 0, format);
  4846. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  4847. if (!mout->no_share_stream && mout->extra_out_nid[i])
  4848. snd_hda_codec_setup_stream(codec,
  4849. mout->extra_out_nid[i],
  4850. stream_tag, 0, format);
  4851. /* surrounds */
  4852. for (i = 1; i < mout->num_dacs; i++) {
  4853. if (chs >= (i + 1) * 2) /* independent out */
  4854. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  4855. i * 2, format);
  4856. else if (!mout->no_share_stream) /* copy front */
  4857. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  4858. 0, format);
  4859. }
  4860. return 0;
  4861. }
  4862. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
  4863. /**
  4864. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  4865. */
  4866. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  4867. struct hda_multi_out *mout)
  4868. {
  4869. const hda_nid_t *nids = mout->dac_nids;
  4870. int i;
  4871. for (i = 0; i < mout->num_dacs; i++)
  4872. snd_hda_codec_cleanup_stream(codec, nids[i]);
  4873. if (mout->hp_nid)
  4874. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  4875. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  4876. if (mout->hp_out_nid[i])
  4877. snd_hda_codec_cleanup_stream(codec,
  4878. mout->hp_out_nid[i]);
  4879. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  4880. if (mout->extra_out_nid[i])
  4881. snd_hda_codec_cleanup_stream(codec,
  4882. mout->extra_out_nid[i]);
  4883. mutex_lock(&codec->spdif_mutex);
  4884. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  4885. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  4886. mout->dig_out_used = 0;
  4887. }
  4888. mutex_unlock(&codec->spdif_mutex);
  4889. return 0;
  4890. }
  4891. EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
  4892. /**
  4893. * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
  4894. *
  4895. * Guess the suitable VREF pin bits to be set as the pin-control value.
  4896. * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
  4897. */
  4898. unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
  4899. {
  4900. unsigned int pincap;
  4901. unsigned int oldval;
  4902. oldval = snd_hda_codec_read(codec, pin, 0,
  4903. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  4904. pincap = snd_hda_query_pin_caps(codec, pin);
  4905. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  4906. /* Exception: if the default pin setup is vref50, we give it priority */
  4907. if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
  4908. return AC_PINCTL_VREF_80;
  4909. else if (pincap & AC_PINCAP_VREF_50)
  4910. return AC_PINCTL_VREF_50;
  4911. else if (pincap & AC_PINCAP_VREF_100)
  4912. return AC_PINCTL_VREF_100;
  4913. else if (pincap & AC_PINCAP_VREF_GRD)
  4914. return AC_PINCTL_VREF_GRD;
  4915. return AC_PINCTL_VREF_HIZ;
  4916. }
  4917. EXPORT_SYMBOL_HDA(snd_hda_get_default_vref);
  4918. /* correct the pin ctl value for matching with the pin cap */
  4919. unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
  4920. hda_nid_t pin, unsigned int val)
  4921. {
  4922. static unsigned int cap_lists[][2] = {
  4923. { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
  4924. { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
  4925. { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
  4926. { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
  4927. };
  4928. unsigned int cap;
  4929. if (!val)
  4930. return 0;
  4931. cap = snd_hda_query_pin_caps(codec, pin);
  4932. if (!cap)
  4933. return val; /* don't know what to do... */
  4934. if (val & AC_PINCTL_OUT_EN) {
  4935. if (!(cap & AC_PINCAP_OUT))
  4936. val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  4937. else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
  4938. val &= ~AC_PINCTL_HP_EN;
  4939. }
  4940. if (val & AC_PINCTL_IN_EN) {
  4941. if (!(cap & AC_PINCAP_IN))
  4942. val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
  4943. else {
  4944. unsigned int vcap, vref;
  4945. int i;
  4946. vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  4947. vref = val & AC_PINCTL_VREFEN;
  4948. for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
  4949. if (vref == cap_lists[i][0] &&
  4950. !(vcap & cap_lists[i][1])) {
  4951. if (i == ARRAY_SIZE(cap_lists) - 1)
  4952. vref = AC_PINCTL_VREF_HIZ;
  4953. else
  4954. vref = cap_lists[i + 1][0];
  4955. }
  4956. }
  4957. val &= ~AC_PINCTL_VREFEN;
  4958. val |= vref;
  4959. }
  4960. }
  4961. return val;
  4962. }
  4963. EXPORT_SYMBOL_HDA(snd_hda_correct_pin_ctl);
  4964. int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
  4965. unsigned int val, bool cached)
  4966. {
  4967. val = snd_hda_correct_pin_ctl(codec, pin, val);
  4968. snd_hda_codec_set_pin_target(codec, pin, val);
  4969. if (cached)
  4970. return snd_hda_codec_update_cache(codec, pin, 0,
  4971. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  4972. else
  4973. return snd_hda_codec_write(codec, pin, 0,
  4974. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  4975. }
  4976. EXPORT_SYMBOL_HDA(_snd_hda_set_pin_ctl);
  4977. /**
  4978. * snd_hda_add_imux_item - Add an item to input_mux
  4979. *
  4980. * When the same label is used already in the existing items, the number
  4981. * suffix is appended to the label. This label index number is stored
  4982. * to type_idx when non-NULL pointer is given.
  4983. */
  4984. int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
  4985. int index, int *type_idx)
  4986. {
  4987. int i, label_idx = 0;
  4988. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  4989. snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
  4990. return -EINVAL;
  4991. }
  4992. for (i = 0; i < imux->num_items; i++) {
  4993. if (!strncmp(label, imux->items[i].label, strlen(label)))
  4994. label_idx++;
  4995. }
  4996. if (type_idx)
  4997. *type_idx = label_idx;
  4998. if (label_idx > 0)
  4999. snprintf(imux->items[imux->num_items].label,
  5000. sizeof(imux->items[imux->num_items].label),
  5001. "%s %d", label, label_idx);
  5002. else
  5003. strlcpy(imux->items[imux->num_items].label, label,
  5004. sizeof(imux->items[imux->num_items].label));
  5005. imux->items[imux->num_items].index = index;
  5006. imux->num_items++;
  5007. return 0;
  5008. }
  5009. EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
  5010. #ifdef CONFIG_PM
  5011. /*
  5012. * power management
  5013. */
  5014. /**
  5015. * snd_hda_suspend - suspend the codecs
  5016. * @bus: the HDA bus
  5017. *
  5018. * Returns 0 if successful.
  5019. */
  5020. int snd_hda_suspend(struct hda_bus *bus)
  5021. {
  5022. struct hda_codec *codec;
  5023. list_for_each_entry(codec, &bus->codec_list, list) {
  5024. cancel_delayed_work_sync(&codec->jackpoll_work);
  5025. if (hda_codec_is_power_on(codec))
  5026. hda_call_codec_suspend(codec, false);
  5027. }
  5028. return 0;
  5029. }
  5030. EXPORT_SYMBOL_HDA(snd_hda_suspend);
  5031. /**
  5032. * snd_hda_resume - resume the codecs
  5033. * @bus: the HDA bus
  5034. *
  5035. * Returns 0 if successful.
  5036. */
  5037. int snd_hda_resume(struct hda_bus *bus)
  5038. {
  5039. struct hda_codec *codec;
  5040. list_for_each_entry(codec, &bus->codec_list, list) {
  5041. hda_call_codec_resume(codec);
  5042. }
  5043. return 0;
  5044. }
  5045. EXPORT_SYMBOL_HDA(snd_hda_resume);
  5046. #endif /* CONFIG_PM */
  5047. /*
  5048. * generic arrays
  5049. */
  5050. /**
  5051. * snd_array_new - get a new element from the given array
  5052. * @array: the array object
  5053. *
  5054. * Get a new element from the given array. If it exceeds the
  5055. * pre-allocated array size, re-allocate the array.
  5056. *
  5057. * Returns NULL if allocation failed.
  5058. */
  5059. void *snd_array_new(struct snd_array *array)
  5060. {
  5061. if (snd_BUG_ON(!array->elem_size))
  5062. return NULL;
  5063. if (array->used >= array->alloced) {
  5064. int num = array->alloced + array->alloc_align;
  5065. int size = (num + 1) * array->elem_size;
  5066. void *nlist;
  5067. if (snd_BUG_ON(num >= 4096))
  5068. return NULL;
  5069. nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO);
  5070. if (!nlist)
  5071. return NULL;
  5072. array->list = nlist;
  5073. array->alloced = num;
  5074. }
  5075. return snd_array_elem(array, array->used++);
  5076. }
  5077. EXPORT_SYMBOL_HDA(snd_array_new);
  5078. /**
  5079. * snd_array_free - free the given array elements
  5080. * @array: the array object
  5081. */
  5082. void snd_array_free(struct snd_array *array)
  5083. {
  5084. kfree(array->list);
  5085. array->used = 0;
  5086. array->alloced = 0;
  5087. array->list = NULL;
  5088. }
  5089. EXPORT_SYMBOL_HDA(snd_array_free);
  5090. /**
  5091. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  5092. * @pcm: PCM caps bits
  5093. * @buf: the string buffer to write
  5094. * @buflen: the max buffer length
  5095. *
  5096. * used by hda_proc.c and hda_eld.c
  5097. */
  5098. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  5099. {
  5100. static unsigned int bits[] = { 8, 16, 20, 24, 32 };
  5101. int i, j;
  5102. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  5103. if (pcm & (AC_SUPPCM_BITS_8 << i))
  5104. j += snprintf(buf + j, buflen - j, " %d", bits[i]);
  5105. buf[j] = '\0'; /* necessary when j == 0 */
  5106. }
  5107. EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
  5108. MODULE_DESCRIPTION("HDA codec core");
  5109. MODULE_LICENSE("GPL");