hda_codec.c 139 KB

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