hda_codec.c 148 KB

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