hda_codec.c 146 KB

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