hda_codec.c 139 KB

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