hda_generic.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Generic widget tree parser
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This driver is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This driver is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <linux/sort.h>
  26. #include <linux/ctype.h>
  27. #include <linux/string.h>
  28. #include <linux/bitops.h>
  29. #include <sound/core.h>
  30. #include <sound/jack.h>
  31. #include "hda_codec.h"
  32. #include "hda_local.h"
  33. #include "hda_auto_parser.h"
  34. #include "hda_jack.h"
  35. #include "hda_generic.h"
  36. /* initialize hda_gen_spec struct */
  37. int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
  38. {
  39. snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
  40. snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
  41. mutex_init(&spec->pcm_mutex);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
  45. struct snd_kcontrol_new *
  46. snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
  47. const struct snd_kcontrol_new *temp)
  48. {
  49. struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
  50. if (!knew)
  51. return NULL;
  52. *knew = *temp;
  53. if (name)
  54. knew->name = kstrdup(name, GFP_KERNEL);
  55. else if (knew->name)
  56. knew->name = kstrdup(knew->name, GFP_KERNEL);
  57. if (!knew->name)
  58. return NULL;
  59. return knew;
  60. }
  61. EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
  62. static void free_kctls(struct hda_gen_spec *spec)
  63. {
  64. if (spec->kctls.list) {
  65. struct snd_kcontrol_new *kctl = spec->kctls.list;
  66. int i;
  67. for (i = 0; i < spec->kctls.used; i++)
  68. kfree(kctl[i].name);
  69. }
  70. snd_array_free(&spec->kctls);
  71. }
  72. void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
  73. {
  74. if (!spec)
  75. return;
  76. free_kctls(spec);
  77. snd_array_free(&spec->paths);
  78. }
  79. EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
  80. /*
  81. * store user hints
  82. */
  83. static void parse_user_hints(struct hda_codec *codec)
  84. {
  85. struct hda_gen_spec *spec = codec->spec;
  86. int val;
  87. val = snd_hda_get_bool_hint(codec, "jack_detect");
  88. if (val >= 0)
  89. codec->no_jack_detect = !val;
  90. val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
  91. if (val >= 0)
  92. codec->inv_jack_detect = !!val;
  93. val = snd_hda_get_bool_hint(codec, "trigger_sense");
  94. if (val >= 0)
  95. codec->no_trigger_sense = !val;
  96. val = snd_hda_get_bool_hint(codec, "inv_eapd");
  97. if (val >= 0)
  98. codec->inv_eapd = !!val;
  99. val = snd_hda_get_bool_hint(codec, "pcm_format_first");
  100. if (val >= 0)
  101. codec->pcm_format_first = !!val;
  102. val = snd_hda_get_bool_hint(codec, "sticky_stream");
  103. if (val >= 0)
  104. codec->no_sticky_stream = !val;
  105. val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
  106. if (val >= 0)
  107. codec->spdif_status_reset = !!val;
  108. val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
  109. if (val >= 0)
  110. codec->pin_amp_workaround = !!val;
  111. val = snd_hda_get_bool_hint(codec, "single_adc_amp");
  112. if (val >= 0)
  113. codec->single_adc_amp = !!val;
  114. val = snd_hda_get_bool_hint(codec, "auto_mute");
  115. if (val >= 0)
  116. spec->suppress_auto_mute = !val;
  117. val = snd_hda_get_bool_hint(codec, "auto_mic");
  118. if (val >= 0)
  119. spec->suppress_auto_mic = !val;
  120. val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
  121. if (val >= 0)
  122. spec->line_in_auto_switch = !!val;
  123. val = snd_hda_get_bool_hint(codec, "need_dac_fix");
  124. if (val >= 0)
  125. spec->need_dac_fix = !!val;
  126. val = snd_hda_get_bool_hint(codec, "primary_hp");
  127. if (val >= 0)
  128. spec->no_primary_hp = !val;
  129. val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
  130. if (val >= 0)
  131. spec->multi_cap_vol = !!val;
  132. val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
  133. if (val >= 0)
  134. spec->inv_dmic_split = !!val;
  135. val = snd_hda_get_bool_hint(codec, "indep_hp");
  136. if (val >= 0)
  137. spec->indep_hp = !!val;
  138. val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
  139. if (val >= 0)
  140. spec->add_stereo_mix_input = !!val;
  141. val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
  142. if (val >= 0)
  143. spec->add_out_jack_modes = !!val;
  144. val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
  145. if (val >= 0)
  146. spec->add_in_jack_modes = !!val;
  147. if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
  148. spec->mixer_nid = val;
  149. }
  150. /*
  151. * pin control value accesses
  152. */
  153. #define update_pin_ctl(codec, pin, val) \
  154. snd_hda_codec_update_cache(codec, pin, 0, \
  155. AC_VERB_SET_PIN_WIDGET_CONTROL, val)
  156. /* restore the pinctl based on the cached value */
  157. static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
  158. {
  159. update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
  160. }
  161. /* set the pinctl target value and write it if requested */
  162. static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
  163. unsigned int val, bool do_write)
  164. {
  165. if (!pin)
  166. return;
  167. val = snd_hda_correct_pin_ctl(codec, pin, val);
  168. snd_hda_codec_set_pin_target(codec, pin, val);
  169. if (do_write)
  170. update_pin_ctl(codec, pin, val);
  171. }
  172. /* set pinctl target values for all given pins */
  173. static void set_pin_targets(struct hda_codec *codec, int num_pins,
  174. hda_nid_t *pins, unsigned int val)
  175. {
  176. int i;
  177. for (i = 0; i < num_pins; i++)
  178. set_pin_target(codec, pins[i], val, false);
  179. }
  180. /*
  181. * parsing paths
  182. */
  183. /* return the position of NID in the list, or -1 if not found */
  184. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  185. {
  186. int i;
  187. for (i = 0; i < nums; i++)
  188. if (list[i] == nid)
  189. return i;
  190. return -1;
  191. }
  192. /* return true if the given NID is contained in the path */
  193. static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
  194. {
  195. return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
  196. }
  197. static struct nid_path *get_nid_path(struct hda_codec *codec,
  198. hda_nid_t from_nid, hda_nid_t to_nid,
  199. int anchor_nid)
  200. {
  201. struct hda_gen_spec *spec = codec->spec;
  202. int i;
  203. for (i = 0; i < spec->paths.used; i++) {
  204. struct nid_path *path = snd_array_elem(&spec->paths, i);
  205. if (path->depth <= 0)
  206. continue;
  207. if ((!from_nid || path->path[0] == from_nid) &&
  208. (!to_nid || path->path[path->depth - 1] == to_nid)) {
  209. if (!anchor_nid ||
  210. (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
  211. (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
  212. return path;
  213. }
  214. }
  215. return NULL;
  216. }
  217. /* get the path between the given NIDs;
  218. * passing 0 to either @pin or @dac behaves as a wildcard
  219. */
  220. struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
  221. hda_nid_t from_nid, hda_nid_t to_nid)
  222. {
  223. return get_nid_path(codec, from_nid, to_nid, 0);
  224. }
  225. EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
  226. /* get the index number corresponding to the path instance;
  227. * the index starts from 1, for easier checking the invalid value
  228. */
  229. int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
  230. {
  231. struct hda_gen_spec *spec = codec->spec;
  232. struct nid_path *array = spec->paths.list;
  233. ssize_t idx;
  234. if (!spec->paths.used)
  235. return 0;
  236. idx = path - array;
  237. if (idx < 0 || idx >= spec->paths.used)
  238. return 0;
  239. return idx + 1;
  240. }
  241. /* get the path instance corresponding to the given index number */
  242. struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
  243. {
  244. struct hda_gen_spec *spec = codec->spec;
  245. if (idx <= 0 || idx > spec->paths.used)
  246. return NULL;
  247. return snd_array_elem(&spec->paths, idx - 1);
  248. }
  249. /* check whether the given DAC is already found in any existing paths */
  250. static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
  251. {
  252. struct hda_gen_spec *spec = codec->spec;
  253. int i;
  254. for (i = 0; i < spec->paths.used; i++) {
  255. struct nid_path *path = snd_array_elem(&spec->paths, i);
  256. if (path->path[0] == nid)
  257. return true;
  258. }
  259. return false;
  260. }
  261. /* check whether the given two widgets can be connected */
  262. static bool is_reachable_path(struct hda_codec *codec,
  263. hda_nid_t from_nid, hda_nid_t to_nid)
  264. {
  265. if (!from_nid || !to_nid)
  266. return false;
  267. return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
  268. }
  269. /* nid, dir and idx */
  270. #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
  271. /* check whether the given ctl is already assigned in any path elements */
  272. static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
  273. {
  274. struct hda_gen_spec *spec = codec->spec;
  275. int i;
  276. val &= AMP_VAL_COMPARE_MASK;
  277. for (i = 0; i < spec->paths.used; i++) {
  278. struct nid_path *path = snd_array_elem(&spec->paths, i);
  279. if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
  280. return true;
  281. }
  282. return false;
  283. }
  284. /* check whether a control with the given (nid, dir, idx) was assigned */
  285. static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
  286. int dir, int idx, int type)
  287. {
  288. unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
  289. return is_ctl_used(codec, val, type);
  290. }
  291. static void print_nid_path(const char *pfx, struct nid_path *path)
  292. {
  293. char buf[40];
  294. int i;
  295. buf[0] = 0;
  296. for (i = 0; i < path->depth; i++) {
  297. char tmp[4];
  298. sprintf(tmp, ":%02x", path->path[i]);
  299. strlcat(buf, tmp, sizeof(buf));
  300. }
  301. snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
  302. }
  303. /* called recursively */
  304. static bool __parse_nid_path(struct hda_codec *codec,
  305. hda_nid_t from_nid, hda_nid_t to_nid,
  306. int anchor_nid, struct nid_path *path,
  307. int depth)
  308. {
  309. const hda_nid_t *conn;
  310. int i, nums;
  311. if (to_nid == anchor_nid)
  312. anchor_nid = 0; /* anchor passed */
  313. else if (to_nid == (hda_nid_t)(-anchor_nid))
  314. return false; /* hit the exclusive nid */
  315. nums = snd_hda_get_conn_list(codec, to_nid, &conn);
  316. for (i = 0; i < nums; i++) {
  317. if (conn[i] != from_nid) {
  318. /* special case: when from_nid is 0,
  319. * try to find an empty DAC
  320. */
  321. if (from_nid ||
  322. get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
  323. is_dac_already_used(codec, conn[i]))
  324. continue;
  325. }
  326. /* anchor is not requested or already passed? */
  327. if (anchor_nid <= 0)
  328. goto found;
  329. }
  330. if (depth >= MAX_NID_PATH_DEPTH)
  331. return false;
  332. for (i = 0; i < nums; i++) {
  333. unsigned int type;
  334. type = get_wcaps_type(get_wcaps(codec, conn[i]));
  335. if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
  336. type == AC_WID_PIN)
  337. continue;
  338. if (__parse_nid_path(codec, from_nid, conn[i],
  339. anchor_nid, path, depth + 1))
  340. goto found;
  341. }
  342. return false;
  343. found:
  344. path->path[path->depth] = conn[i];
  345. path->idx[path->depth + 1] = i;
  346. if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
  347. path->multi[path->depth + 1] = 1;
  348. path->depth++;
  349. return true;
  350. }
  351. /* parse the widget path from the given nid to the target nid;
  352. * when @from_nid is 0, try to find an empty DAC;
  353. * when @anchor_nid is set to a positive value, only paths through the widget
  354. * with the given value are evaluated.
  355. * when @anchor_nid is set to a negative value, paths through the widget
  356. * with the negative of given value are excluded, only other paths are chosen.
  357. * when @anchor_nid is zero, no special handling about path selection.
  358. */
  359. bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
  360. hda_nid_t to_nid, int anchor_nid,
  361. struct nid_path *path)
  362. {
  363. if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
  364. path->path[path->depth] = to_nid;
  365. path->depth++;
  366. return true;
  367. }
  368. return false;
  369. }
  370. EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
  371. /*
  372. * parse the path between the given NIDs and add to the path list.
  373. * if no valid path is found, return NULL
  374. */
  375. struct nid_path *
  376. snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
  377. hda_nid_t to_nid, int anchor_nid)
  378. {
  379. struct hda_gen_spec *spec = codec->spec;
  380. struct nid_path *path;
  381. if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
  382. return NULL;
  383. /* check whether the path has been already added */
  384. path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
  385. if (path)
  386. return path;
  387. path = snd_array_new(&spec->paths);
  388. if (!path)
  389. return NULL;
  390. memset(path, 0, sizeof(*path));
  391. if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
  392. return path;
  393. /* push back */
  394. spec->paths.used--;
  395. return NULL;
  396. }
  397. EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
  398. /* clear the given path as invalid so that it won't be picked up later */
  399. static void invalidate_nid_path(struct hda_codec *codec, int idx)
  400. {
  401. struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
  402. if (!path)
  403. return;
  404. memset(path, 0, sizeof(*path));
  405. }
  406. /* look for an empty DAC slot */
  407. static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
  408. bool is_digital)
  409. {
  410. struct hda_gen_spec *spec = codec->spec;
  411. bool cap_digital;
  412. int i;
  413. for (i = 0; i < spec->num_all_dacs; i++) {
  414. hda_nid_t nid = spec->all_dacs[i];
  415. if (!nid || is_dac_already_used(codec, nid))
  416. continue;
  417. cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
  418. if (is_digital != cap_digital)
  419. continue;
  420. if (is_reachable_path(codec, nid, pin))
  421. return nid;
  422. }
  423. return 0;
  424. }
  425. /* replace the channels in the composed amp value with the given number */
  426. static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
  427. {
  428. val &= ~(0x3U << 16);
  429. val |= chs << 16;
  430. return val;
  431. }
  432. /* check whether the widget has the given amp capability for the direction */
  433. static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
  434. int dir, unsigned int bits)
  435. {
  436. if (!nid)
  437. return false;
  438. if (get_wcaps(codec, nid) & (1 << (dir + 1)))
  439. if (query_amp_caps(codec, nid, dir) & bits)
  440. return true;
  441. return false;
  442. }
  443. static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
  444. hda_nid_t nid2, int dir)
  445. {
  446. if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
  447. return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
  448. return (query_amp_caps(codec, nid1, dir) ==
  449. query_amp_caps(codec, nid2, dir));
  450. }
  451. #define nid_has_mute(codec, nid, dir) \
  452. check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
  453. #define nid_has_volume(codec, nid, dir) \
  454. check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
  455. /* look for a widget suitable for assigning a mute switch in the path */
  456. static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
  457. struct nid_path *path)
  458. {
  459. int i;
  460. for (i = path->depth - 1; i >= 0; i--) {
  461. if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
  462. return path->path[i];
  463. if (i != path->depth - 1 && i != 0 &&
  464. nid_has_mute(codec, path->path[i], HDA_INPUT))
  465. return path->path[i];
  466. }
  467. return 0;
  468. }
  469. /* look for a widget suitable for assigning a volume ctl in the path */
  470. static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
  471. struct nid_path *path)
  472. {
  473. int i;
  474. for (i = path->depth - 1; i >= 0; i--) {
  475. if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
  476. return path->path[i];
  477. }
  478. return 0;
  479. }
  480. /*
  481. * path activation / deactivation
  482. */
  483. /* can have the amp-in capability? */
  484. static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
  485. {
  486. hda_nid_t nid = path->path[idx];
  487. unsigned int caps = get_wcaps(codec, nid);
  488. unsigned int type = get_wcaps_type(caps);
  489. if (!(caps & AC_WCAP_IN_AMP))
  490. return false;
  491. if (type == AC_WID_PIN && idx > 0) /* only for input pins */
  492. return false;
  493. return true;
  494. }
  495. /* can have the amp-out capability? */
  496. static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
  497. {
  498. hda_nid_t nid = path->path[idx];
  499. unsigned int caps = get_wcaps(codec, nid);
  500. unsigned int type = get_wcaps_type(caps);
  501. if (!(caps & AC_WCAP_OUT_AMP))
  502. return false;
  503. if (type == AC_WID_PIN && !idx) /* only for output pins */
  504. return false;
  505. return true;
  506. }
  507. /* check whether the given (nid,dir,idx) is active */
  508. static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
  509. unsigned int idx, unsigned int dir)
  510. {
  511. struct hda_gen_spec *spec = codec->spec;
  512. int i, n;
  513. for (n = 0; n < spec->paths.used; n++) {
  514. struct nid_path *path = snd_array_elem(&spec->paths, n);
  515. if (!path->active)
  516. continue;
  517. for (i = 0; i < path->depth; i++) {
  518. if (path->path[i] == nid) {
  519. if (dir == HDA_OUTPUT || path->idx[i] == idx)
  520. return true;
  521. break;
  522. }
  523. }
  524. }
  525. return false;
  526. }
  527. /* get the default amp value for the target state */
  528. static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
  529. int dir, unsigned int caps, bool enable)
  530. {
  531. unsigned int val = 0;
  532. if (caps & AC_AMPCAP_NUM_STEPS) {
  533. /* set to 0dB */
  534. if (enable)
  535. val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  536. }
  537. if (caps & AC_AMPCAP_MUTE) {
  538. if (!enable)
  539. val |= HDA_AMP_MUTE;
  540. }
  541. return val;
  542. }
  543. /* initialize the amp value (only at the first time) */
  544. static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
  545. {
  546. unsigned int caps = query_amp_caps(codec, nid, dir);
  547. int val = get_amp_val_to_activate(codec, nid, dir, caps, false);
  548. snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
  549. }
  550. /* calculate amp value mask we can modify;
  551. * if the given amp is controlled by mixers, don't touch it
  552. */
  553. static unsigned int get_amp_mask_to_modify(struct hda_codec *codec,
  554. hda_nid_t nid, int dir, int idx,
  555. unsigned int caps)
  556. {
  557. unsigned int mask = 0xff;
  558. if (caps & AC_AMPCAP_MUTE) {
  559. if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_MUTE_CTL))
  560. mask &= ~0x80;
  561. }
  562. if (caps & AC_AMPCAP_NUM_STEPS) {
  563. if (is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
  564. is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
  565. mask &= ~0x7f;
  566. }
  567. return mask;
  568. }
  569. static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
  570. int idx, int idx_to_check, bool enable)
  571. {
  572. unsigned int caps;
  573. unsigned int mask, val;
  574. if (!enable && is_active_nid(codec, nid, dir, idx))
  575. return;
  576. caps = query_amp_caps(codec, nid, dir);
  577. val = get_amp_val_to_activate(codec, nid, dir, caps, enable);
  578. mask = get_amp_mask_to_modify(codec, nid, dir, idx_to_check, caps);
  579. if (!mask)
  580. return;
  581. val &= mask;
  582. snd_hda_codec_amp_stereo(codec, nid, dir, idx, mask, val);
  583. }
  584. static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
  585. int i, bool enable)
  586. {
  587. hda_nid_t nid = path->path[i];
  588. init_amp(codec, nid, HDA_OUTPUT, 0);
  589. activate_amp(codec, nid, HDA_OUTPUT, 0, 0, enable);
  590. }
  591. static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
  592. int i, bool enable, bool add_aamix)
  593. {
  594. struct hda_gen_spec *spec = codec->spec;
  595. const hda_nid_t *conn;
  596. int n, nums, idx;
  597. int type;
  598. hda_nid_t nid = path->path[i];
  599. nums = snd_hda_get_conn_list(codec, nid, &conn);
  600. type = get_wcaps_type(get_wcaps(codec, nid));
  601. if (type == AC_WID_PIN ||
  602. (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
  603. nums = 1;
  604. idx = 0;
  605. } else
  606. idx = path->idx[i];
  607. for (n = 0; n < nums; n++)
  608. init_amp(codec, nid, HDA_INPUT, n);
  609. /* here is a little bit tricky in comparison with activate_amp_out();
  610. * when aa-mixer is available, we need to enable the path as well
  611. */
  612. for (n = 0; n < nums; n++) {
  613. if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
  614. continue;
  615. activate_amp(codec, nid, HDA_INPUT, n, idx, enable);
  616. }
  617. }
  618. /* activate or deactivate the given path
  619. * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
  620. */
  621. void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
  622. bool enable, bool add_aamix)
  623. {
  624. int i;
  625. if (!enable)
  626. path->active = false;
  627. for (i = path->depth - 1; i >= 0; i--) {
  628. if (enable && path->multi[i])
  629. snd_hda_codec_write_cache(codec, path->path[i], 0,
  630. AC_VERB_SET_CONNECT_SEL,
  631. path->idx[i]);
  632. if (has_amp_in(codec, path, i))
  633. activate_amp_in(codec, path, i, enable, add_aamix);
  634. if (has_amp_out(codec, path, i))
  635. activate_amp_out(codec, path, i, enable);
  636. }
  637. if (enable)
  638. path->active = true;
  639. }
  640. EXPORT_SYMBOL_HDA(snd_hda_activate_path);
  641. /* turn on/off EAPD on the given pin */
  642. static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
  643. {
  644. struct hda_gen_spec *spec = codec->spec;
  645. if (spec->own_eapd_ctl ||
  646. !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
  647. return;
  648. if (codec->inv_eapd)
  649. enable = !enable;
  650. snd_hda_codec_update_cache(codec, pin, 0,
  651. AC_VERB_SET_EAPD_BTLENABLE,
  652. enable ? 0x02 : 0x00);
  653. }
  654. /*
  655. * Helper functions for creating mixer ctl elements
  656. */
  657. enum {
  658. HDA_CTL_WIDGET_VOL,
  659. HDA_CTL_WIDGET_MUTE,
  660. HDA_CTL_BIND_MUTE,
  661. };
  662. static const struct snd_kcontrol_new control_templates[] = {
  663. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  664. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  665. HDA_BIND_MUTE(NULL, 0, 0, 0),
  666. };
  667. /* add dynamic controls from template */
  668. static struct snd_kcontrol_new *
  669. add_control(struct hda_gen_spec *spec, int type, const char *name,
  670. int cidx, unsigned long val)
  671. {
  672. struct snd_kcontrol_new *knew;
  673. knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
  674. if (!knew)
  675. return NULL;
  676. knew->index = cidx;
  677. if (get_amp_nid_(val))
  678. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  679. knew->private_value = val;
  680. return knew;
  681. }
  682. static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
  683. const char *pfx, const char *dir,
  684. const char *sfx, int cidx, unsigned long val)
  685. {
  686. char name[32];
  687. snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
  688. if (!add_control(spec, type, name, cidx, val))
  689. return -ENOMEM;
  690. return 0;
  691. }
  692. #define add_pb_vol_ctrl(spec, type, pfx, val) \
  693. add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
  694. #define add_pb_sw_ctrl(spec, type, pfx, val) \
  695. add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
  696. #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
  697. add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
  698. #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
  699. add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
  700. static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
  701. unsigned int chs, struct nid_path *path)
  702. {
  703. unsigned int val;
  704. if (!path)
  705. return 0;
  706. val = path->ctls[NID_PATH_VOL_CTL];
  707. if (!val)
  708. return 0;
  709. val = amp_val_replace_channels(val, chs);
  710. return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
  711. }
  712. /* return the channel bits suitable for the given path->ctls[] */
  713. static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
  714. int type)
  715. {
  716. int chs = 1; /* mono (left only) */
  717. if (path) {
  718. hda_nid_t nid = get_amp_nid_(path->ctls[type]);
  719. if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
  720. chs = 3; /* stereo */
  721. }
  722. return chs;
  723. }
  724. static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
  725. struct nid_path *path)
  726. {
  727. int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
  728. return add_vol_ctl(codec, pfx, cidx, chs, path);
  729. }
  730. /* create a mute-switch for the given mixer widget;
  731. * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
  732. */
  733. static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
  734. unsigned int chs, struct nid_path *path)
  735. {
  736. unsigned int val;
  737. int type = HDA_CTL_WIDGET_MUTE;
  738. if (!path)
  739. return 0;
  740. val = path->ctls[NID_PATH_MUTE_CTL];
  741. if (!val)
  742. return 0;
  743. val = amp_val_replace_channels(val, chs);
  744. if (get_amp_direction_(val) == HDA_INPUT) {
  745. hda_nid_t nid = get_amp_nid_(val);
  746. int nums = snd_hda_get_num_conns(codec, nid);
  747. if (nums > 1) {
  748. type = HDA_CTL_BIND_MUTE;
  749. val |= nums << 19;
  750. }
  751. }
  752. return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
  753. }
  754. static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
  755. int cidx, struct nid_path *path)
  756. {
  757. int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
  758. return add_sw_ctl(codec, pfx, cidx, chs, path);
  759. }
  760. /* any ctl assigned to the path with the given index? */
  761. static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
  762. {
  763. struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
  764. return path && path->ctls[ctl_type];
  765. }
  766. static const char * const channel_name[4] = {
  767. "Front", "Surround", "CLFE", "Side"
  768. };
  769. /* give some appropriate ctl name prefix for the given line out channel */
  770. static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
  771. int *index, int ctl_type)
  772. {
  773. struct hda_gen_spec *spec = codec->spec;
  774. struct auto_pin_cfg *cfg = &spec->autocfg;
  775. *index = 0;
  776. if (cfg->line_outs == 1 && !spec->multi_ios &&
  777. !cfg->hp_outs && !cfg->speaker_outs)
  778. return spec->vmaster_mute.hook ? "PCM" : "Master";
  779. /* if there is really a single DAC used in the whole output paths,
  780. * use it master (or "PCM" if a vmaster hook is present)
  781. */
  782. if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
  783. !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
  784. return spec->vmaster_mute.hook ? "PCM" : "Master";
  785. /* multi-io channels */
  786. if (ch >= cfg->line_outs)
  787. return channel_name[ch];
  788. switch (cfg->line_out_type) {
  789. case AUTO_PIN_SPEAKER_OUT:
  790. /* if the primary channel vol/mute is shared with HP volume,
  791. * don't name it as Speaker
  792. */
  793. if (!ch && cfg->hp_outs &&
  794. !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
  795. break;
  796. if (cfg->line_outs == 1)
  797. return "Speaker";
  798. if (cfg->line_outs == 2)
  799. return ch ? "Bass Speaker" : "Speaker";
  800. break;
  801. case AUTO_PIN_HP_OUT:
  802. /* if the primary channel vol/mute is shared with spk volume,
  803. * don't name it as Headphone
  804. */
  805. if (!ch && cfg->speaker_outs &&
  806. !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
  807. break;
  808. /* for multi-io case, only the primary out */
  809. if (ch && spec->multi_ios)
  810. break;
  811. *index = ch;
  812. return "Headphone";
  813. }
  814. /* for a single channel output, we don't have to name the channel */
  815. if (cfg->line_outs == 1 && !spec->multi_ios)
  816. return "PCM";
  817. if (ch >= ARRAY_SIZE(channel_name)) {
  818. snd_BUG();
  819. return "PCM";
  820. }
  821. return channel_name[ch];
  822. }
  823. /*
  824. * Parse output paths
  825. */
  826. /* badness definition */
  827. enum {
  828. /* No primary DAC is found for the main output */
  829. BAD_NO_PRIMARY_DAC = 0x10000,
  830. /* No DAC is found for the extra output */
  831. BAD_NO_DAC = 0x4000,
  832. /* No possible multi-ios */
  833. BAD_MULTI_IO = 0x103,
  834. /* No individual DAC for extra output */
  835. BAD_NO_EXTRA_DAC = 0x102,
  836. /* No individual DAC for extra surrounds */
  837. BAD_NO_EXTRA_SURR_DAC = 0x101,
  838. /* Primary DAC shared with main surrounds */
  839. BAD_SHARED_SURROUND = 0x100,
  840. /* Primary DAC shared with main CLFE */
  841. BAD_SHARED_CLFE = 0x10,
  842. /* Primary DAC shared with extra surrounds */
  843. BAD_SHARED_EXTRA_SURROUND = 0x10,
  844. /* Volume widget is shared */
  845. BAD_SHARED_VOL = 0x10,
  846. };
  847. /* look for widgets in the given path which are appropriate for
  848. * volume and mute controls, and assign the values to ctls[].
  849. *
  850. * When no appropriate widget is found in the path, the badness value
  851. * is incremented depending on the situation. The function returns the
  852. * total badness for both volume and mute controls.
  853. */
  854. static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
  855. {
  856. hda_nid_t nid;
  857. unsigned int val;
  858. int badness = 0;
  859. if (!path)
  860. return BAD_SHARED_VOL * 2;
  861. if (path->ctls[NID_PATH_VOL_CTL] ||
  862. path->ctls[NID_PATH_MUTE_CTL])
  863. return 0; /* already evaluated */
  864. nid = look_for_out_vol_nid(codec, path);
  865. if (nid) {
  866. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  867. if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
  868. badness += BAD_SHARED_VOL;
  869. else
  870. path->ctls[NID_PATH_VOL_CTL] = val;
  871. } else
  872. badness += BAD_SHARED_VOL;
  873. nid = look_for_out_mute_nid(codec, path);
  874. if (nid) {
  875. unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
  876. if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
  877. nid_has_mute(codec, nid, HDA_OUTPUT))
  878. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  879. else
  880. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
  881. if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
  882. badness += BAD_SHARED_VOL;
  883. else
  884. path->ctls[NID_PATH_MUTE_CTL] = val;
  885. } else
  886. badness += BAD_SHARED_VOL;
  887. return badness;
  888. }
  889. struct badness_table {
  890. int no_primary_dac; /* no primary DAC */
  891. int no_dac; /* no secondary DACs */
  892. int shared_primary; /* primary DAC is shared with main output */
  893. int shared_surr; /* secondary DAC shared with main or primary */
  894. int shared_clfe; /* third DAC shared with main or primary */
  895. int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
  896. };
  897. static struct badness_table main_out_badness = {
  898. .no_primary_dac = BAD_NO_PRIMARY_DAC,
  899. .no_dac = BAD_NO_DAC,
  900. .shared_primary = BAD_NO_PRIMARY_DAC,
  901. .shared_surr = BAD_SHARED_SURROUND,
  902. .shared_clfe = BAD_SHARED_CLFE,
  903. .shared_surr_main = BAD_SHARED_SURROUND,
  904. };
  905. static struct badness_table extra_out_badness = {
  906. .no_primary_dac = BAD_NO_DAC,
  907. .no_dac = BAD_NO_DAC,
  908. .shared_primary = BAD_NO_EXTRA_DAC,
  909. .shared_surr = BAD_SHARED_EXTRA_SURROUND,
  910. .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
  911. .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
  912. };
  913. /* get the DAC of the primary output corresponding to the given array index */
  914. static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
  915. {
  916. struct hda_gen_spec *spec = codec->spec;
  917. struct auto_pin_cfg *cfg = &spec->autocfg;
  918. if (cfg->line_outs > idx)
  919. return spec->private_dac_nids[idx];
  920. idx -= cfg->line_outs;
  921. if (spec->multi_ios > idx)
  922. return spec->multi_io[idx].dac;
  923. return 0;
  924. }
  925. /* return the DAC if it's reachable, otherwise zero */
  926. static inline hda_nid_t try_dac(struct hda_codec *codec,
  927. hda_nid_t dac, hda_nid_t pin)
  928. {
  929. return is_reachable_path(codec, dac, pin) ? dac : 0;
  930. }
  931. /* try to assign DACs to pins and return the resultant badness */
  932. static int try_assign_dacs(struct hda_codec *codec, int num_outs,
  933. const hda_nid_t *pins, hda_nid_t *dacs,
  934. int *path_idx,
  935. const struct badness_table *bad)
  936. {
  937. struct hda_gen_spec *spec = codec->spec;
  938. int i, j;
  939. int badness = 0;
  940. hda_nid_t dac;
  941. if (!num_outs)
  942. return 0;
  943. for (i = 0; i < num_outs; i++) {
  944. struct nid_path *path;
  945. hda_nid_t pin = pins[i];
  946. path = snd_hda_get_path_from_idx(codec, path_idx[i]);
  947. if (path) {
  948. badness += assign_out_path_ctls(codec, path);
  949. continue;
  950. }
  951. dacs[i] = look_for_dac(codec, pin, false);
  952. if (!dacs[i] && !i) {
  953. /* try to steal the DAC of surrounds for the front */
  954. for (j = 1; j < num_outs; j++) {
  955. if (is_reachable_path(codec, dacs[j], pin)) {
  956. dacs[0] = dacs[j];
  957. dacs[j] = 0;
  958. invalidate_nid_path(codec, path_idx[j]);
  959. path_idx[j] = 0;
  960. break;
  961. }
  962. }
  963. }
  964. dac = dacs[i];
  965. if (!dac) {
  966. if (num_outs > 2)
  967. dac = try_dac(codec, get_primary_out(codec, i), pin);
  968. if (!dac)
  969. dac = try_dac(codec, dacs[0], pin);
  970. if (!dac)
  971. dac = try_dac(codec, get_primary_out(codec, i), pin);
  972. if (dac) {
  973. if (!i)
  974. badness += bad->shared_primary;
  975. else if (i == 1)
  976. badness += bad->shared_surr;
  977. else
  978. badness += bad->shared_clfe;
  979. } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
  980. dac = spec->private_dac_nids[0];
  981. badness += bad->shared_surr_main;
  982. } else if (!i)
  983. badness += bad->no_primary_dac;
  984. else
  985. badness += bad->no_dac;
  986. }
  987. if (!dac)
  988. continue;
  989. path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
  990. if (!path && !i && spec->mixer_nid) {
  991. /* try with aamix */
  992. path = snd_hda_add_new_path(codec, dac, pin, 0);
  993. }
  994. if (!path) {
  995. dac = dacs[i] = 0;
  996. badness += bad->no_dac;
  997. } else {
  998. /* print_nid_path("output", path); */
  999. path->active = true;
  1000. path_idx[i] = snd_hda_get_path_idx(codec, path);
  1001. badness += assign_out_path_ctls(codec, path);
  1002. }
  1003. }
  1004. return badness;
  1005. }
  1006. /* return NID if the given pin has only a single connection to a certain DAC */
  1007. static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
  1008. {
  1009. struct hda_gen_spec *spec = codec->spec;
  1010. int i;
  1011. hda_nid_t nid_found = 0;
  1012. for (i = 0; i < spec->num_all_dacs; i++) {
  1013. hda_nid_t nid = spec->all_dacs[i];
  1014. if (!nid || is_dac_already_used(codec, nid))
  1015. continue;
  1016. if (is_reachable_path(codec, nid, pin)) {
  1017. if (nid_found)
  1018. return 0;
  1019. nid_found = nid;
  1020. }
  1021. }
  1022. return nid_found;
  1023. }
  1024. /* check whether the given pin can be a multi-io pin */
  1025. static bool can_be_multiio_pin(struct hda_codec *codec,
  1026. unsigned int location, hda_nid_t nid)
  1027. {
  1028. unsigned int defcfg, caps;
  1029. defcfg = snd_hda_codec_get_pincfg(codec, nid);
  1030. if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
  1031. return false;
  1032. if (location && get_defcfg_location(defcfg) != location)
  1033. return false;
  1034. caps = snd_hda_query_pin_caps(codec, nid);
  1035. if (!(caps & AC_PINCAP_OUT))
  1036. return false;
  1037. return true;
  1038. }
  1039. /* count the number of input pins that are capable to be multi-io */
  1040. static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
  1041. {
  1042. struct hda_gen_spec *spec = codec->spec;
  1043. struct auto_pin_cfg *cfg = &spec->autocfg;
  1044. unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
  1045. unsigned int location = get_defcfg_location(defcfg);
  1046. int type, i;
  1047. int num_pins = 0;
  1048. for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
  1049. for (i = 0; i < cfg->num_inputs; i++) {
  1050. if (cfg->inputs[i].type != type)
  1051. continue;
  1052. if (can_be_multiio_pin(codec, location,
  1053. cfg->inputs[i].pin))
  1054. num_pins++;
  1055. }
  1056. }
  1057. return num_pins;
  1058. }
  1059. /*
  1060. * multi-io helper
  1061. *
  1062. * When hardwired is set, try to fill ony hardwired pins, and returns
  1063. * zero if any pins are filled, non-zero if nothing found.
  1064. * When hardwired is off, try to fill possible input pins, and returns
  1065. * the badness value.
  1066. */
  1067. static int fill_multi_ios(struct hda_codec *codec,
  1068. hda_nid_t reference_pin,
  1069. bool hardwired)
  1070. {
  1071. struct hda_gen_spec *spec = codec->spec;
  1072. struct auto_pin_cfg *cfg = &spec->autocfg;
  1073. int type, i, j, num_pins, old_pins;
  1074. unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
  1075. unsigned int location = get_defcfg_location(defcfg);
  1076. int badness = 0;
  1077. struct nid_path *path;
  1078. old_pins = spec->multi_ios;
  1079. if (old_pins >= 2)
  1080. goto end_fill;
  1081. num_pins = count_multiio_pins(codec, reference_pin);
  1082. if (num_pins < 2)
  1083. goto end_fill;
  1084. for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
  1085. for (i = 0; i < cfg->num_inputs; i++) {
  1086. hda_nid_t nid = cfg->inputs[i].pin;
  1087. hda_nid_t dac = 0;
  1088. if (cfg->inputs[i].type != type)
  1089. continue;
  1090. if (!can_be_multiio_pin(codec, location, nid))
  1091. continue;
  1092. for (j = 0; j < spec->multi_ios; j++) {
  1093. if (nid == spec->multi_io[j].pin)
  1094. break;
  1095. }
  1096. if (j < spec->multi_ios)
  1097. continue;
  1098. if (hardwired)
  1099. dac = get_dac_if_single(codec, nid);
  1100. else if (!dac)
  1101. dac = look_for_dac(codec, nid, false);
  1102. if (!dac) {
  1103. badness++;
  1104. continue;
  1105. }
  1106. path = snd_hda_add_new_path(codec, dac, nid,
  1107. -spec->mixer_nid);
  1108. if (!path) {
  1109. badness++;
  1110. continue;
  1111. }
  1112. /* print_nid_path("multiio", path); */
  1113. spec->multi_io[spec->multi_ios].pin = nid;
  1114. spec->multi_io[spec->multi_ios].dac = dac;
  1115. spec->out_paths[cfg->line_outs + spec->multi_ios] =
  1116. snd_hda_get_path_idx(codec, path);
  1117. spec->multi_ios++;
  1118. if (spec->multi_ios >= 2)
  1119. break;
  1120. }
  1121. }
  1122. end_fill:
  1123. if (badness)
  1124. badness = BAD_MULTI_IO;
  1125. if (old_pins == spec->multi_ios) {
  1126. if (hardwired)
  1127. return 1; /* nothing found */
  1128. else
  1129. return badness; /* no badness if nothing found */
  1130. }
  1131. if (!hardwired && spec->multi_ios < 2) {
  1132. /* cancel newly assigned paths */
  1133. spec->paths.used -= spec->multi_ios - old_pins;
  1134. spec->multi_ios = old_pins;
  1135. return badness;
  1136. }
  1137. /* assign volume and mute controls */
  1138. for (i = old_pins; i < spec->multi_ios; i++) {
  1139. path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
  1140. badness += assign_out_path_ctls(codec, path);
  1141. }
  1142. return badness;
  1143. }
  1144. /* map DACs for all pins in the list if they are single connections */
  1145. static bool map_singles(struct hda_codec *codec, int outs,
  1146. const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
  1147. {
  1148. struct hda_gen_spec *spec = codec->spec;
  1149. int i;
  1150. bool found = false;
  1151. for (i = 0; i < outs; i++) {
  1152. struct nid_path *path;
  1153. hda_nid_t dac;
  1154. if (dacs[i])
  1155. continue;
  1156. dac = get_dac_if_single(codec, pins[i]);
  1157. if (!dac)
  1158. continue;
  1159. path = snd_hda_add_new_path(codec, dac, pins[i],
  1160. -spec->mixer_nid);
  1161. if (!path && !i && spec->mixer_nid)
  1162. path = snd_hda_add_new_path(codec, dac, pins[i], 0);
  1163. if (path) {
  1164. dacs[i] = dac;
  1165. found = true;
  1166. /* print_nid_path("output", path); */
  1167. path->active = true;
  1168. path_idx[i] = snd_hda_get_path_idx(codec, path);
  1169. }
  1170. }
  1171. return found;
  1172. }
  1173. /* create a new path including aamix if available, and return its index */
  1174. static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
  1175. {
  1176. struct hda_gen_spec *spec = codec->spec;
  1177. struct nid_path *path;
  1178. hda_nid_t dac, pin;
  1179. path = snd_hda_get_path_from_idx(codec, path_idx);
  1180. if (!path || !path->depth ||
  1181. is_nid_contained(path, spec->mixer_nid))
  1182. return 0;
  1183. dac = path->path[0];
  1184. pin = path->path[path->depth - 1];
  1185. path = snd_hda_add_new_path(codec, dac, pin, spec->mixer_nid);
  1186. if (!path) {
  1187. if (dac != spec->multiout.dac_nids[0])
  1188. dac = spec->multiout.dac_nids[0];
  1189. else if (spec->multiout.hp_out_nid[0])
  1190. dac = spec->multiout.hp_out_nid[0];
  1191. else if (spec->multiout.extra_out_nid[0])
  1192. dac = spec->multiout.extra_out_nid[0];
  1193. if (dac)
  1194. path = snd_hda_add_new_path(codec, dac, pin,
  1195. spec->mixer_nid);
  1196. }
  1197. if (!path)
  1198. return 0;
  1199. /* print_nid_path("output-aamix", path); */
  1200. path->active = false; /* unused as default */
  1201. return snd_hda_get_path_idx(codec, path);
  1202. }
  1203. /* fill the empty entries in the dac array for speaker/hp with the
  1204. * shared dac pointed by the paths
  1205. */
  1206. static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
  1207. hda_nid_t *dacs, int *path_idx)
  1208. {
  1209. struct nid_path *path;
  1210. int i;
  1211. for (i = 0; i < num_outs; i++) {
  1212. if (dacs[i])
  1213. continue;
  1214. path = snd_hda_get_path_from_idx(codec, path_idx[i]);
  1215. if (!path)
  1216. continue;
  1217. dacs[i] = path->path[0];
  1218. }
  1219. }
  1220. /* fill in the dac_nids table from the parsed pin configuration */
  1221. static int fill_and_eval_dacs(struct hda_codec *codec,
  1222. bool fill_hardwired,
  1223. bool fill_mio_first)
  1224. {
  1225. struct hda_gen_spec *spec = codec->spec;
  1226. struct auto_pin_cfg *cfg = &spec->autocfg;
  1227. int i, err, badness;
  1228. /* set num_dacs once to full for look_for_dac() */
  1229. spec->multiout.num_dacs = cfg->line_outs;
  1230. spec->multiout.dac_nids = spec->private_dac_nids;
  1231. memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
  1232. memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
  1233. memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
  1234. spec->multi_ios = 0;
  1235. snd_array_free(&spec->paths);
  1236. /* clear path indices */
  1237. memset(spec->out_paths, 0, sizeof(spec->out_paths));
  1238. memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
  1239. memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
  1240. memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
  1241. memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
  1242. memset(spec->input_paths, 0, sizeof(spec->input_paths));
  1243. memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
  1244. memset(&spec->digin_path, 0, sizeof(spec->digin_path));
  1245. badness = 0;
  1246. /* fill hard-wired DACs first */
  1247. if (fill_hardwired) {
  1248. bool mapped;
  1249. do {
  1250. mapped = map_singles(codec, cfg->line_outs,
  1251. cfg->line_out_pins,
  1252. spec->private_dac_nids,
  1253. spec->out_paths);
  1254. mapped |= map_singles(codec, cfg->hp_outs,
  1255. cfg->hp_pins,
  1256. spec->multiout.hp_out_nid,
  1257. spec->hp_paths);
  1258. mapped |= map_singles(codec, cfg->speaker_outs,
  1259. cfg->speaker_pins,
  1260. spec->multiout.extra_out_nid,
  1261. spec->speaker_paths);
  1262. if (fill_mio_first && cfg->line_outs == 1 &&
  1263. cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1264. err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
  1265. if (!err)
  1266. mapped = true;
  1267. }
  1268. } while (mapped);
  1269. }
  1270. badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
  1271. spec->private_dac_nids, spec->out_paths,
  1272. &main_out_badness);
  1273. if (fill_mio_first &&
  1274. cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1275. /* try to fill multi-io first */
  1276. err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
  1277. if (err < 0)
  1278. return err;
  1279. /* we don't count badness at this stage yet */
  1280. }
  1281. if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
  1282. err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
  1283. spec->multiout.hp_out_nid,
  1284. spec->hp_paths,
  1285. &extra_out_badness);
  1286. if (err < 0)
  1287. return err;
  1288. badness += err;
  1289. }
  1290. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1291. err = try_assign_dacs(codec, cfg->speaker_outs,
  1292. cfg->speaker_pins,
  1293. spec->multiout.extra_out_nid,
  1294. spec->speaker_paths,
  1295. &extra_out_badness);
  1296. if (err < 0)
  1297. return err;
  1298. badness += err;
  1299. }
  1300. if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1301. err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
  1302. if (err < 0)
  1303. return err;
  1304. badness += err;
  1305. }
  1306. if (spec->mixer_nid) {
  1307. spec->aamix_out_paths[0] =
  1308. check_aamix_out_path(codec, spec->out_paths[0]);
  1309. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1310. spec->aamix_out_paths[1] =
  1311. check_aamix_out_path(codec, spec->hp_paths[0]);
  1312. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  1313. spec->aamix_out_paths[2] =
  1314. check_aamix_out_path(codec, spec->speaker_paths[0]);
  1315. }
  1316. if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
  1317. if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
  1318. spec->multi_ios = 1; /* give badness */
  1319. /* re-count num_dacs and squash invalid entries */
  1320. spec->multiout.num_dacs = 0;
  1321. for (i = 0; i < cfg->line_outs; i++) {
  1322. if (spec->private_dac_nids[i])
  1323. spec->multiout.num_dacs++;
  1324. else {
  1325. memmove(spec->private_dac_nids + i,
  1326. spec->private_dac_nids + i + 1,
  1327. sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
  1328. spec->private_dac_nids[cfg->line_outs - 1] = 0;
  1329. }
  1330. }
  1331. spec->ext_channel_count = spec->min_channel_count =
  1332. spec->multiout.num_dacs * 2;
  1333. if (spec->multi_ios == 2) {
  1334. for (i = 0; i < 2; i++)
  1335. spec->private_dac_nids[spec->multiout.num_dacs++] =
  1336. spec->multi_io[i].dac;
  1337. } else if (spec->multi_ios) {
  1338. spec->multi_ios = 0;
  1339. badness += BAD_MULTI_IO;
  1340. }
  1341. /* re-fill the shared DAC for speaker / headphone */
  1342. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1343. refill_shared_dacs(codec, cfg->hp_outs,
  1344. spec->multiout.hp_out_nid,
  1345. spec->hp_paths);
  1346. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  1347. refill_shared_dacs(codec, cfg->speaker_outs,
  1348. spec->multiout.extra_out_nid,
  1349. spec->speaker_paths);
  1350. return badness;
  1351. }
  1352. #define DEBUG_BADNESS
  1353. #ifdef DEBUG_BADNESS
  1354. #define debug_badness snd_printdd
  1355. #else
  1356. #define debug_badness(...)
  1357. #endif
  1358. #ifdef DEBUG_BADNESS
  1359. static inline void print_nid_path_idx(struct hda_codec *codec,
  1360. const char *pfx, int idx)
  1361. {
  1362. struct nid_path *path;
  1363. path = snd_hda_get_path_from_idx(codec, idx);
  1364. if (path)
  1365. print_nid_path(pfx, path);
  1366. }
  1367. static void debug_show_configs(struct hda_codec *codec,
  1368. struct auto_pin_cfg *cfg)
  1369. {
  1370. struct hda_gen_spec *spec = codec->spec;
  1371. #ifdef CONFIG_SND_DEBUG_VERBOSE
  1372. static const char * const lo_type[3] = { "LO", "SP", "HP" };
  1373. #endif
  1374. int i;
  1375. debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x (type %s)\n",
  1376. cfg->line_out_pins[0], cfg->line_out_pins[1],
  1377. cfg->line_out_pins[2], cfg->line_out_pins[3],
  1378. spec->multiout.dac_nids[0],
  1379. spec->multiout.dac_nids[1],
  1380. spec->multiout.dac_nids[2],
  1381. spec->multiout.dac_nids[3],
  1382. lo_type[cfg->line_out_type]);
  1383. for (i = 0; i < cfg->line_outs; i++)
  1384. print_nid_path_idx(codec, " out", spec->out_paths[i]);
  1385. if (spec->multi_ios > 0)
  1386. debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
  1387. spec->multi_ios,
  1388. spec->multi_io[0].pin, spec->multi_io[1].pin,
  1389. spec->multi_io[0].dac, spec->multi_io[1].dac);
  1390. for (i = 0; i < spec->multi_ios; i++)
  1391. print_nid_path_idx(codec, " mio",
  1392. spec->out_paths[cfg->line_outs + i]);
  1393. if (cfg->hp_outs)
  1394. debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
  1395. cfg->hp_pins[0], cfg->hp_pins[1],
  1396. cfg->hp_pins[2], cfg->hp_pins[3],
  1397. spec->multiout.hp_out_nid[0],
  1398. spec->multiout.hp_out_nid[1],
  1399. spec->multiout.hp_out_nid[2],
  1400. spec->multiout.hp_out_nid[3]);
  1401. for (i = 0; i < cfg->hp_outs; i++)
  1402. print_nid_path_idx(codec, " hp ", spec->hp_paths[i]);
  1403. if (cfg->speaker_outs)
  1404. debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
  1405. cfg->speaker_pins[0], cfg->speaker_pins[1],
  1406. cfg->speaker_pins[2], cfg->speaker_pins[3],
  1407. spec->multiout.extra_out_nid[0],
  1408. spec->multiout.extra_out_nid[1],
  1409. spec->multiout.extra_out_nid[2],
  1410. spec->multiout.extra_out_nid[3]);
  1411. for (i = 0; i < cfg->speaker_outs; i++)
  1412. print_nid_path_idx(codec, " spk", spec->speaker_paths[i]);
  1413. for (i = 0; i < 3; i++)
  1414. print_nid_path_idx(codec, " mix", spec->aamix_out_paths[i]);
  1415. }
  1416. #else
  1417. #define debug_show_configs(codec, cfg) /* NOP */
  1418. #endif
  1419. /* find all available DACs of the codec */
  1420. static void fill_all_dac_nids(struct hda_codec *codec)
  1421. {
  1422. struct hda_gen_spec *spec = codec->spec;
  1423. int i;
  1424. hda_nid_t nid = codec->start_nid;
  1425. spec->num_all_dacs = 0;
  1426. memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
  1427. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1428. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
  1429. continue;
  1430. if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
  1431. snd_printk(KERN_ERR "hda: Too many DACs!\n");
  1432. break;
  1433. }
  1434. spec->all_dacs[spec->num_all_dacs++] = nid;
  1435. }
  1436. }
  1437. static int parse_output_paths(struct hda_codec *codec)
  1438. {
  1439. struct hda_gen_spec *spec = codec->spec;
  1440. struct auto_pin_cfg *cfg = &spec->autocfg;
  1441. struct auto_pin_cfg *best_cfg;
  1442. unsigned int val;
  1443. int best_badness = INT_MAX;
  1444. int badness;
  1445. bool fill_hardwired = true, fill_mio_first = true;
  1446. bool best_wired = true, best_mio = true;
  1447. bool hp_spk_swapped = false;
  1448. best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
  1449. if (!best_cfg)
  1450. return -ENOMEM;
  1451. *best_cfg = *cfg;
  1452. for (;;) {
  1453. badness = fill_and_eval_dacs(codec, fill_hardwired,
  1454. fill_mio_first);
  1455. if (badness < 0) {
  1456. kfree(best_cfg);
  1457. return badness;
  1458. }
  1459. debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
  1460. cfg->line_out_type, fill_hardwired, fill_mio_first,
  1461. badness);
  1462. debug_show_configs(codec, cfg);
  1463. if (badness < best_badness) {
  1464. best_badness = badness;
  1465. *best_cfg = *cfg;
  1466. best_wired = fill_hardwired;
  1467. best_mio = fill_mio_first;
  1468. }
  1469. if (!badness)
  1470. break;
  1471. fill_mio_first = !fill_mio_first;
  1472. if (!fill_mio_first)
  1473. continue;
  1474. fill_hardwired = !fill_hardwired;
  1475. if (!fill_hardwired)
  1476. continue;
  1477. if (hp_spk_swapped)
  1478. break;
  1479. hp_spk_swapped = true;
  1480. if (cfg->speaker_outs > 0 &&
  1481. cfg->line_out_type == AUTO_PIN_HP_OUT) {
  1482. cfg->hp_outs = cfg->line_outs;
  1483. memcpy(cfg->hp_pins, cfg->line_out_pins,
  1484. sizeof(cfg->hp_pins));
  1485. cfg->line_outs = cfg->speaker_outs;
  1486. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  1487. sizeof(cfg->speaker_pins));
  1488. cfg->speaker_outs = 0;
  1489. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  1490. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  1491. fill_hardwired = true;
  1492. continue;
  1493. }
  1494. if (cfg->hp_outs > 0 &&
  1495. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  1496. cfg->speaker_outs = cfg->line_outs;
  1497. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  1498. sizeof(cfg->speaker_pins));
  1499. cfg->line_outs = cfg->hp_outs;
  1500. memcpy(cfg->line_out_pins, cfg->hp_pins,
  1501. sizeof(cfg->hp_pins));
  1502. cfg->hp_outs = 0;
  1503. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  1504. cfg->line_out_type = AUTO_PIN_HP_OUT;
  1505. fill_hardwired = true;
  1506. continue;
  1507. }
  1508. break;
  1509. }
  1510. if (badness) {
  1511. debug_badness("==> restoring best_cfg\n");
  1512. *cfg = *best_cfg;
  1513. fill_and_eval_dacs(codec, best_wired, best_mio);
  1514. }
  1515. debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
  1516. cfg->line_out_type, best_wired, best_mio);
  1517. debug_show_configs(codec, cfg);
  1518. if (cfg->line_out_pins[0]) {
  1519. struct nid_path *path;
  1520. path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
  1521. if (path)
  1522. spec->vmaster_nid = look_for_out_vol_nid(codec, path);
  1523. if (spec->vmaster_nid)
  1524. snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
  1525. HDA_OUTPUT, spec->vmaster_tlv);
  1526. }
  1527. /* set initial pinctl targets */
  1528. if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
  1529. val = PIN_HP;
  1530. else
  1531. val = PIN_OUT;
  1532. set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
  1533. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1534. set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
  1535. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1536. val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
  1537. set_pin_targets(codec, cfg->speaker_outs,
  1538. cfg->speaker_pins, val);
  1539. }
  1540. kfree(best_cfg);
  1541. return 0;
  1542. }
  1543. /* add playback controls from the parsed DAC table */
  1544. static int create_multi_out_ctls(struct hda_codec *codec,
  1545. const struct auto_pin_cfg *cfg)
  1546. {
  1547. struct hda_gen_spec *spec = codec->spec;
  1548. int i, err, noutputs;
  1549. noutputs = cfg->line_outs;
  1550. if (spec->multi_ios > 0 && cfg->line_outs < 3)
  1551. noutputs += spec->multi_ios;
  1552. for (i = 0; i < noutputs; i++) {
  1553. const char *name;
  1554. int index;
  1555. struct nid_path *path;
  1556. path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
  1557. if (!path)
  1558. continue;
  1559. name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
  1560. if (!name || !strcmp(name, "CLFE")) {
  1561. /* Center/LFE */
  1562. err = add_vol_ctl(codec, "Center", 0, 1, path);
  1563. if (err < 0)
  1564. return err;
  1565. err = add_vol_ctl(codec, "LFE", 0, 2, path);
  1566. if (err < 0)
  1567. return err;
  1568. } else {
  1569. err = add_stereo_vol(codec, name, index, path);
  1570. if (err < 0)
  1571. return err;
  1572. }
  1573. name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
  1574. if (!name || !strcmp(name, "CLFE")) {
  1575. err = add_sw_ctl(codec, "Center", 0, 1, path);
  1576. if (err < 0)
  1577. return err;
  1578. err = add_sw_ctl(codec, "LFE", 0, 2, path);
  1579. if (err < 0)
  1580. return err;
  1581. } else {
  1582. err = add_stereo_sw(codec, name, index, path);
  1583. if (err < 0)
  1584. return err;
  1585. }
  1586. }
  1587. return 0;
  1588. }
  1589. static int create_extra_out(struct hda_codec *codec, int path_idx,
  1590. const char *pfx, int cidx)
  1591. {
  1592. struct nid_path *path;
  1593. int err;
  1594. path = snd_hda_get_path_from_idx(codec, path_idx);
  1595. if (!path)
  1596. return 0;
  1597. err = add_stereo_vol(codec, pfx, cidx, path);
  1598. if (err < 0)
  1599. return err;
  1600. err = add_stereo_sw(codec, pfx, cidx, path);
  1601. if (err < 0)
  1602. return err;
  1603. return 0;
  1604. }
  1605. /* add playback controls for speaker and HP outputs */
  1606. static int create_extra_outs(struct hda_codec *codec, int num_pins,
  1607. const int *paths, const char *pfx)
  1608. {
  1609. int i;
  1610. for (i = 0; i < num_pins; i++) {
  1611. const char *name;
  1612. char tmp[44];
  1613. int err, idx = 0;
  1614. if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
  1615. name = "Bass Speaker";
  1616. else if (num_pins >= 3) {
  1617. snprintf(tmp, sizeof(tmp), "%s %s",
  1618. pfx, channel_name[i]);
  1619. name = tmp;
  1620. } else {
  1621. name = pfx;
  1622. idx = i;
  1623. }
  1624. err = create_extra_out(codec, paths[i], name, idx);
  1625. if (err < 0)
  1626. return err;
  1627. }
  1628. return 0;
  1629. }
  1630. static int create_hp_out_ctls(struct hda_codec *codec)
  1631. {
  1632. struct hda_gen_spec *spec = codec->spec;
  1633. return create_extra_outs(codec, spec->autocfg.hp_outs,
  1634. spec->hp_paths,
  1635. "Headphone");
  1636. }
  1637. static int create_speaker_out_ctls(struct hda_codec *codec)
  1638. {
  1639. struct hda_gen_spec *spec = codec->spec;
  1640. return create_extra_outs(codec, spec->autocfg.speaker_outs,
  1641. spec->speaker_paths,
  1642. "Speaker");
  1643. }
  1644. /*
  1645. * independent HP controls
  1646. */
  1647. static int indep_hp_info(struct snd_kcontrol *kcontrol,
  1648. struct snd_ctl_elem_info *uinfo)
  1649. {
  1650. return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
  1651. }
  1652. static int indep_hp_get(struct snd_kcontrol *kcontrol,
  1653. struct snd_ctl_elem_value *ucontrol)
  1654. {
  1655. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1656. struct hda_gen_spec *spec = codec->spec;
  1657. ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
  1658. return 0;
  1659. }
  1660. static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
  1661. int nomix_path_idx, int mix_path_idx,
  1662. int out_type);
  1663. static int indep_hp_put(struct snd_kcontrol *kcontrol,
  1664. struct snd_ctl_elem_value *ucontrol)
  1665. {
  1666. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1667. struct hda_gen_spec *spec = codec->spec;
  1668. unsigned int select = ucontrol->value.enumerated.item[0];
  1669. int ret = 0;
  1670. mutex_lock(&spec->pcm_mutex);
  1671. if (spec->active_streams) {
  1672. ret = -EBUSY;
  1673. goto unlock;
  1674. }
  1675. if (spec->indep_hp_enabled != select) {
  1676. hda_nid_t *dacp;
  1677. if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
  1678. dacp = &spec->private_dac_nids[0];
  1679. else
  1680. dacp = &spec->multiout.hp_out_nid[0];
  1681. /* update HP aamix paths in case it conflicts with indep HP */
  1682. if (spec->have_aamix_ctl) {
  1683. if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
  1684. update_aamix_paths(codec, spec->aamix_mode,
  1685. spec->out_paths[0],
  1686. spec->aamix_out_paths[0],
  1687. spec->autocfg.line_out_type);
  1688. else
  1689. update_aamix_paths(codec, spec->aamix_mode,
  1690. spec->hp_paths[0],
  1691. spec->aamix_out_paths[1],
  1692. AUTO_PIN_HP_OUT);
  1693. }
  1694. spec->indep_hp_enabled = select;
  1695. if (spec->indep_hp_enabled)
  1696. *dacp = 0;
  1697. else
  1698. *dacp = spec->alt_dac_nid;
  1699. ret = 1;
  1700. }
  1701. unlock:
  1702. mutex_unlock(&spec->pcm_mutex);
  1703. return ret;
  1704. }
  1705. static const struct snd_kcontrol_new indep_hp_ctl = {
  1706. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1707. .name = "Independent HP",
  1708. .info = indep_hp_info,
  1709. .get = indep_hp_get,
  1710. .put = indep_hp_put,
  1711. };
  1712. static int create_indep_hp_ctls(struct hda_codec *codec)
  1713. {
  1714. struct hda_gen_spec *spec = codec->spec;
  1715. hda_nid_t dac;
  1716. if (!spec->indep_hp)
  1717. return 0;
  1718. if (spec->autocfg.line_out_type == AUTO_PIN_HP_OUT)
  1719. dac = spec->multiout.dac_nids[0];
  1720. else
  1721. dac = spec->multiout.hp_out_nid[0];
  1722. if (!dac) {
  1723. spec->indep_hp = 0;
  1724. return 0;
  1725. }
  1726. spec->indep_hp_enabled = false;
  1727. spec->alt_dac_nid = dac;
  1728. if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
  1729. return -ENOMEM;
  1730. return 0;
  1731. }
  1732. /*
  1733. * channel mode enum control
  1734. */
  1735. static int ch_mode_info(struct snd_kcontrol *kcontrol,
  1736. struct snd_ctl_elem_info *uinfo)
  1737. {
  1738. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1739. struct hda_gen_spec *spec = codec->spec;
  1740. int chs;
  1741. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1742. uinfo->count = 1;
  1743. uinfo->value.enumerated.items = spec->multi_ios + 1;
  1744. if (uinfo->value.enumerated.item > spec->multi_ios)
  1745. uinfo->value.enumerated.item = spec->multi_ios;
  1746. chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
  1747. sprintf(uinfo->value.enumerated.name, "%dch", chs);
  1748. return 0;
  1749. }
  1750. static int ch_mode_get(struct snd_kcontrol *kcontrol,
  1751. struct snd_ctl_elem_value *ucontrol)
  1752. {
  1753. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1754. struct hda_gen_spec *spec = codec->spec;
  1755. ucontrol->value.enumerated.item[0] =
  1756. (spec->ext_channel_count - spec->min_channel_count) / 2;
  1757. return 0;
  1758. }
  1759. static inline struct nid_path *
  1760. get_multiio_path(struct hda_codec *codec, int idx)
  1761. {
  1762. struct hda_gen_spec *spec = codec->spec;
  1763. return snd_hda_get_path_from_idx(codec,
  1764. spec->out_paths[spec->autocfg.line_outs + idx]);
  1765. }
  1766. static void update_automute_all(struct hda_codec *codec);
  1767. static int set_multi_io(struct hda_codec *codec, int idx, bool output)
  1768. {
  1769. struct hda_gen_spec *spec = codec->spec;
  1770. hda_nid_t nid = spec->multi_io[idx].pin;
  1771. struct nid_path *path;
  1772. path = get_multiio_path(codec, idx);
  1773. if (!path)
  1774. return -EINVAL;
  1775. if (path->active == output)
  1776. return 0;
  1777. if (output) {
  1778. set_pin_target(codec, nid, PIN_OUT, true);
  1779. snd_hda_activate_path(codec, path, true, true);
  1780. set_pin_eapd(codec, nid, true);
  1781. } else {
  1782. set_pin_eapd(codec, nid, false);
  1783. snd_hda_activate_path(codec, path, false, true);
  1784. set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
  1785. }
  1786. /* update jack retasking in case it modifies any of them */
  1787. update_automute_all(codec);
  1788. return 0;
  1789. }
  1790. static int ch_mode_put(struct snd_kcontrol *kcontrol,
  1791. struct snd_ctl_elem_value *ucontrol)
  1792. {
  1793. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1794. struct hda_gen_spec *spec = codec->spec;
  1795. int i, ch;
  1796. ch = ucontrol->value.enumerated.item[0];
  1797. if (ch < 0 || ch > spec->multi_ios)
  1798. return -EINVAL;
  1799. if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
  1800. return 0;
  1801. spec->ext_channel_count = ch * 2 + spec->min_channel_count;
  1802. for (i = 0; i < spec->multi_ios; i++)
  1803. set_multi_io(codec, i, i < ch);
  1804. spec->multiout.max_channels = max(spec->ext_channel_count,
  1805. spec->const_channel_count);
  1806. if (spec->need_dac_fix)
  1807. spec->multiout.num_dacs = spec->multiout.max_channels / 2;
  1808. return 1;
  1809. }
  1810. static const struct snd_kcontrol_new channel_mode_enum = {
  1811. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1812. .name = "Channel Mode",
  1813. .info = ch_mode_info,
  1814. .get = ch_mode_get,
  1815. .put = ch_mode_put,
  1816. };
  1817. static int create_multi_channel_mode(struct hda_codec *codec)
  1818. {
  1819. struct hda_gen_spec *spec = codec->spec;
  1820. if (spec->multi_ios > 0) {
  1821. if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
  1822. return -ENOMEM;
  1823. }
  1824. return 0;
  1825. }
  1826. /*
  1827. * aamix loopback enable/disable switch
  1828. */
  1829. #define loopback_mixing_info indep_hp_info
  1830. static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
  1831. struct snd_ctl_elem_value *ucontrol)
  1832. {
  1833. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1834. struct hda_gen_spec *spec = codec->spec;
  1835. ucontrol->value.enumerated.item[0] = spec->aamix_mode;
  1836. return 0;
  1837. }
  1838. static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
  1839. int nomix_path_idx, int mix_path_idx,
  1840. int out_type)
  1841. {
  1842. struct hda_gen_spec *spec = codec->spec;
  1843. struct nid_path *nomix_path, *mix_path;
  1844. nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
  1845. mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
  1846. if (!nomix_path || !mix_path)
  1847. return;
  1848. /* if HP aamix path is driven from a different DAC and the
  1849. * independent HP mode is ON, can't turn on aamix path
  1850. */
  1851. if (out_type == AUTO_PIN_HP_OUT && spec->indep_hp_enabled &&
  1852. mix_path->path[0] != spec->alt_dac_nid)
  1853. do_mix = false;
  1854. if (do_mix) {
  1855. snd_hda_activate_path(codec, nomix_path, false, true);
  1856. snd_hda_activate_path(codec, mix_path, true, true);
  1857. } else {
  1858. snd_hda_activate_path(codec, mix_path, false, true);
  1859. snd_hda_activate_path(codec, nomix_path, true, true);
  1860. }
  1861. }
  1862. static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
  1863. struct snd_ctl_elem_value *ucontrol)
  1864. {
  1865. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1866. struct hda_gen_spec *spec = codec->spec;
  1867. unsigned int val = ucontrol->value.enumerated.item[0];
  1868. if (val == spec->aamix_mode)
  1869. return 0;
  1870. spec->aamix_mode = val;
  1871. update_aamix_paths(codec, val, spec->out_paths[0],
  1872. spec->aamix_out_paths[0],
  1873. spec->autocfg.line_out_type);
  1874. update_aamix_paths(codec, val, spec->hp_paths[0],
  1875. spec->aamix_out_paths[1],
  1876. AUTO_PIN_HP_OUT);
  1877. update_aamix_paths(codec, val, spec->speaker_paths[0],
  1878. spec->aamix_out_paths[2],
  1879. AUTO_PIN_SPEAKER_OUT);
  1880. return 1;
  1881. }
  1882. static const struct snd_kcontrol_new loopback_mixing_enum = {
  1883. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1884. .name = "Loopback Mixing",
  1885. .info = loopback_mixing_info,
  1886. .get = loopback_mixing_get,
  1887. .put = loopback_mixing_put,
  1888. };
  1889. static int create_loopback_mixing_ctl(struct hda_codec *codec)
  1890. {
  1891. struct hda_gen_spec *spec = codec->spec;
  1892. if (!spec->mixer_nid)
  1893. return 0;
  1894. if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
  1895. spec->aamix_out_paths[2]))
  1896. return 0;
  1897. if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
  1898. return -ENOMEM;
  1899. spec->have_aamix_ctl = 1;
  1900. return 0;
  1901. }
  1902. /*
  1903. * shared headphone/mic handling
  1904. */
  1905. static void call_update_outputs(struct hda_codec *codec);
  1906. /* for shared I/O, change the pin-control accordingly */
  1907. static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
  1908. {
  1909. struct hda_gen_spec *spec = codec->spec;
  1910. unsigned int val;
  1911. hda_nid_t pin = spec->autocfg.inputs[1].pin;
  1912. /* NOTE: this assumes that there are only two inputs, the
  1913. * first is the real internal mic and the second is HP/mic jack.
  1914. */
  1915. val = snd_hda_get_default_vref(codec, pin);
  1916. /* This pin does not have vref caps - let's enable vref on pin 0x18
  1917. instead, as suggested by Realtek */
  1918. if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
  1919. const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
  1920. unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
  1921. if (vref_val != AC_PINCTL_VREF_HIZ)
  1922. snd_hda_set_pin_ctl_cache(codec, vref_pin,
  1923. PIN_IN | (set_as_mic ? vref_val : 0));
  1924. }
  1925. val = set_as_mic ? val | PIN_IN : PIN_HP;
  1926. set_pin_target(codec, pin, val, true);
  1927. spec->automute_speaker = !set_as_mic;
  1928. call_update_outputs(codec);
  1929. }
  1930. /* create a shared input with the headphone out */
  1931. static int create_shared_input(struct hda_codec *codec)
  1932. {
  1933. struct hda_gen_spec *spec = codec->spec;
  1934. struct auto_pin_cfg *cfg = &spec->autocfg;
  1935. unsigned int defcfg;
  1936. hda_nid_t nid;
  1937. /* only one internal input pin? */
  1938. if (cfg->num_inputs != 1)
  1939. return 0;
  1940. defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
  1941. if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
  1942. return 0;
  1943. if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
  1944. nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
  1945. else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
  1946. nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
  1947. else
  1948. return 0; /* both not available */
  1949. if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
  1950. return 0; /* no input */
  1951. cfg->inputs[1].pin = nid;
  1952. cfg->inputs[1].type = AUTO_PIN_MIC;
  1953. cfg->num_inputs = 2;
  1954. spec->shared_mic_hp = 1;
  1955. snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
  1956. return 0;
  1957. }
  1958. /*
  1959. * output jack mode
  1960. */
  1961. static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
  1962. struct snd_ctl_elem_info *uinfo)
  1963. {
  1964. static const char * const texts[] = {
  1965. "Line Out", "Headphone Out",
  1966. };
  1967. return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
  1968. }
  1969. static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
  1970. struct snd_ctl_elem_value *ucontrol)
  1971. {
  1972. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1973. hda_nid_t nid = kcontrol->private_value;
  1974. if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
  1975. ucontrol->value.enumerated.item[0] = 1;
  1976. else
  1977. ucontrol->value.enumerated.item[0] = 0;
  1978. return 0;
  1979. }
  1980. static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
  1981. struct snd_ctl_elem_value *ucontrol)
  1982. {
  1983. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1984. hda_nid_t nid = kcontrol->private_value;
  1985. unsigned int val;
  1986. val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
  1987. if (snd_hda_codec_get_pin_target(codec, nid) == val)
  1988. return 0;
  1989. snd_hda_set_pin_ctl_cache(codec, nid, val);
  1990. return 1;
  1991. }
  1992. static const struct snd_kcontrol_new out_jack_mode_enum = {
  1993. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1994. .info = out_jack_mode_info,
  1995. .get = out_jack_mode_get,
  1996. .put = out_jack_mode_put,
  1997. };
  1998. static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
  1999. {
  2000. struct hda_gen_spec *spec = codec->spec;
  2001. int i;
  2002. for (i = 0; i < spec->kctls.used; i++) {
  2003. struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
  2004. if (!strcmp(kctl->name, name) && kctl->index == idx)
  2005. return true;
  2006. }
  2007. return false;
  2008. }
  2009. static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
  2010. char *name, size_t name_len)
  2011. {
  2012. struct hda_gen_spec *spec = codec->spec;
  2013. int idx = 0;
  2014. snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
  2015. strlcat(name, " Jack Mode", name_len);
  2016. for (; find_kctl_name(codec, name, idx); idx++)
  2017. ;
  2018. }
  2019. static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
  2020. hda_nid_t *pins)
  2021. {
  2022. struct hda_gen_spec *spec = codec->spec;
  2023. int i;
  2024. for (i = 0; i < num_pins; i++) {
  2025. hda_nid_t pin = pins[i];
  2026. unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
  2027. if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
  2028. struct snd_kcontrol_new *knew;
  2029. char name[44];
  2030. get_jack_mode_name(codec, pin, name, sizeof(name));
  2031. knew = snd_hda_gen_add_kctl(spec, name,
  2032. &out_jack_mode_enum);
  2033. if (!knew)
  2034. return -ENOMEM;
  2035. knew->private_value = pin;
  2036. }
  2037. }
  2038. return 0;
  2039. }
  2040. /*
  2041. * input jack mode
  2042. */
  2043. /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
  2044. #define NUM_VREFS 6
  2045. static const char * const vref_texts[NUM_VREFS] = {
  2046. "Line In", "Mic 50pc Bias", "Mic 0V Bias",
  2047. "", "Mic 80pc Bias", "Mic 100pc Bias"
  2048. };
  2049. static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
  2050. {
  2051. unsigned int pincap;
  2052. pincap = snd_hda_query_pin_caps(codec, pin);
  2053. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  2054. /* filter out unusual vrefs */
  2055. pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
  2056. return pincap;
  2057. }
  2058. /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
  2059. static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
  2060. {
  2061. unsigned int i, n = 0;
  2062. for (i = 0; i < NUM_VREFS; i++) {
  2063. if (vref_caps & (1 << i)) {
  2064. if (n == item_idx)
  2065. return i;
  2066. n++;
  2067. }
  2068. }
  2069. return 0;
  2070. }
  2071. /* convert back from the vref ctl index to the enum item index */
  2072. static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
  2073. {
  2074. unsigned int i, n = 0;
  2075. for (i = 0; i < NUM_VREFS; i++) {
  2076. if (i == idx)
  2077. return n;
  2078. if (vref_caps & (1 << i))
  2079. n++;
  2080. }
  2081. return 0;
  2082. }
  2083. static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
  2084. struct snd_ctl_elem_info *uinfo)
  2085. {
  2086. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2087. hda_nid_t nid = kcontrol->private_value;
  2088. unsigned int vref_caps = get_vref_caps(codec, nid);
  2089. snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
  2090. vref_texts);
  2091. /* set the right text */
  2092. strcpy(uinfo->value.enumerated.name,
  2093. vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
  2094. return 0;
  2095. }
  2096. static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
  2097. struct snd_ctl_elem_value *ucontrol)
  2098. {
  2099. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2100. hda_nid_t nid = kcontrol->private_value;
  2101. unsigned int vref_caps = get_vref_caps(codec, nid);
  2102. unsigned int idx;
  2103. idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
  2104. ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
  2105. return 0;
  2106. }
  2107. static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
  2108. struct snd_ctl_elem_value *ucontrol)
  2109. {
  2110. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2111. hda_nid_t nid = kcontrol->private_value;
  2112. unsigned int vref_caps = get_vref_caps(codec, nid);
  2113. unsigned int val, idx;
  2114. val = snd_hda_codec_get_pin_target(codec, nid);
  2115. idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
  2116. if (idx == ucontrol->value.enumerated.item[0])
  2117. return 0;
  2118. val &= ~AC_PINCTL_VREFEN;
  2119. val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
  2120. snd_hda_set_pin_ctl_cache(codec, nid, val);
  2121. return 1;
  2122. }
  2123. static const struct snd_kcontrol_new in_jack_mode_enum = {
  2124. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2125. .info = in_jack_mode_info,
  2126. .get = in_jack_mode_get,
  2127. .put = in_jack_mode_put,
  2128. };
  2129. static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
  2130. {
  2131. struct hda_gen_spec *spec = codec->spec;
  2132. unsigned int defcfg;
  2133. struct snd_kcontrol_new *knew;
  2134. char name[44];
  2135. /* no jack mode for fixed pins */
  2136. defcfg = snd_hda_codec_get_pincfg(codec, pin);
  2137. if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
  2138. return 0;
  2139. /* no multiple vref caps? */
  2140. if (hweight32(get_vref_caps(codec, pin)) <= 1)
  2141. return 0;
  2142. get_jack_mode_name(codec, pin, name, sizeof(name));
  2143. knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
  2144. if (!knew)
  2145. return -ENOMEM;
  2146. knew->private_value = pin;
  2147. return 0;
  2148. }
  2149. /*
  2150. * Parse input paths
  2151. */
  2152. #ifdef CONFIG_PM
  2153. /* add the powersave loopback-list entry */
  2154. static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
  2155. {
  2156. struct hda_amp_list *list;
  2157. if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
  2158. return;
  2159. list = spec->loopback_list + spec->num_loopbacks;
  2160. list->nid = mix;
  2161. list->dir = HDA_INPUT;
  2162. list->idx = idx;
  2163. spec->num_loopbacks++;
  2164. spec->loopback.amplist = spec->loopback_list;
  2165. }
  2166. #else
  2167. #define add_loopback_list(spec, mix, idx) /* NOP */
  2168. #endif
  2169. /* create input playback/capture controls for the given pin */
  2170. static int new_analog_input(struct hda_codec *codec, int input_idx,
  2171. hda_nid_t pin, const char *ctlname, int ctlidx,
  2172. hda_nid_t mix_nid)
  2173. {
  2174. struct hda_gen_spec *spec = codec->spec;
  2175. struct nid_path *path;
  2176. unsigned int val;
  2177. int err, idx;
  2178. if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
  2179. !nid_has_mute(codec, mix_nid, HDA_INPUT))
  2180. return 0; /* no need for analog loopback */
  2181. path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
  2182. if (!path)
  2183. return -EINVAL;
  2184. print_nid_path("loopback", path);
  2185. spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
  2186. idx = path->idx[path->depth - 1];
  2187. if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
  2188. val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
  2189. err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
  2190. if (err < 0)
  2191. return err;
  2192. path->ctls[NID_PATH_VOL_CTL] = val;
  2193. }
  2194. if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
  2195. val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
  2196. err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
  2197. if (err < 0)
  2198. return err;
  2199. path->ctls[NID_PATH_MUTE_CTL] = val;
  2200. }
  2201. path->active = true;
  2202. add_loopback_list(spec, mix_nid, idx);
  2203. return 0;
  2204. }
  2205. static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
  2206. {
  2207. unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
  2208. return (pincap & AC_PINCAP_IN) != 0;
  2209. }
  2210. /* Parse the codec tree and retrieve ADCs */
  2211. static int fill_adc_nids(struct hda_codec *codec)
  2212. {
  2213. struct hda_gen_spec *spec = codec->spec;
  2214. hda_nid_t nid;
  2215. hda_nid_t *adc_nids = spec->adc_nids;
  2216. int max_nums = ARRAY_SIZE(spec->adc_nids);
  2217. int i, nums = 0;
  2218. nid = codec->start_nid;
  2219. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2220. unsigned int caps = get_wcaps(codec, nid);
  2221. int type = get_wcaps_type(caps);
  2222. if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
  2223. continue;
  2224. adc_nids[nums] = nid;
  2225. if (++nums >= max_nums)
  2226. break;
  2227. }
  2228. spec->num_adc_nids = nums;
  2229. /* copy the detected ADCs to all_adcs[] */
  2230. spec->num_all_adcs = nums;
  2231. memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
  2232. return nums;
  2233. }
  2234. /* filter out invalid adc_nids that don't give all active input pins;
  2235. * if needed, check whether dynamic ADC-switching is available
  2236. */
  2237. static int check_dyn_adc_switch(struct hda_codec *codec)
  2238. {
  2239. struct hda_gen_spec *spec = codec->spec;
  2240. struct hda_input_mux *imux = &spec->input_mux;
  2241. unsigned int ok_bits;
  2242. int i, n, nums;
  2243. again:
  2244. nums = 0;
  2245. ok_bits = 0;
  2246. for (n = 0; n < spec->num_adc_nids; n++) {
  2247. for (i = 0; i < imux->num_items; i++) {
  2248. if (!spec->input_paths[i][n])
  2249. break;
  2250. }
  2251. if (i >= imux->num_items) {
  2252. ok_bits |= (1 << n);
  2253. nums++;
  2254. }
  2255. }
  2256. if (!ok_bits) {
  2257. if (spec->shared_mic_hp) {
  2258. spec->shared_mic_hp = 0;
  2259. imux->num_items = 1;
  2260. goto again;
  2261. }
  2262. /* check whether ADC-switch is possible */
  2263. for (i = 0; i < imux->num_items; i++) {
  2264. for (n = 0; n < spec->num_adc_nids; n++) {
  2265. if (spec->input_paths[i][n]) {
  2266. spec->dyn_adc_idx[i] = n;
  2267. break;
  2268. }
  2269. }
  2270. }
  2271. snd_printdd("hda-codec: enabling ADC switching\n");
  2272. spec->dyn_adc_switch = 1;
  2273. } else if (nums != spec->num_adc_nids) {
  2274. /* shrink the invalid adcs and input paths */
  2275. nums = 0;
  2276. for (n = 0; n < spec->num_adc_nids; n++) {
  2277. if (!(ok_bits & (1 << n)))
  2278. continue;
  2279. if (n != nums) {
  2280. spec->adc_nids[nums] = spec->adc_nids[n];
  2281. for (i = 0; i < imux->num_items; i++) {
  2282. invalidate_nid_path(codec,
  2283. spec->input_paths[i][nums]);
  2284. spec->input_paths[i][nums] =
  2285. spec->input_paths[i][n];
  2286. }
  2287. }
  2288. nums++;
  2289. }
  2290. spec->num_adc_nids = nums;
  2291. }
  2292. if (imux->num_items == 1 || spec->shared_mic_hp) {
  2293. snd_printdd("hda-codec: reducing to a single ADC\n");
  2294. spec->num_adc_nids = 1; /* reduce to a single ADC */
  2295. }
  2296. /* single index for individual volumes ctls */
  2297. if (!spec->dyn_adc_switch && spec->multi_cap_vol)
  2298. spec->num_adc_nids = 1;
  2299. return 0;
  2300. }
  2301. /* parse capture source paths from the given pin and create imux items */
  2302. static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
  2303. int cfg_idx, int num_adcs,
  2304. const char *label, int anchor)
  2305. {
  2306. struct hda_gen_spec *spec = codec->spec;
  2307. struct hda_input_mux *imux = &spec->input_mux;
  2308. int imux_idx = imux->num_items;
  2309. bool imux_added = false;
  2310. int c;
  2311. for (c = 0; c < num_adcs; c++) {
  2312. struct nid_path *path;
  2313. hda_nid_t adc = spec->adc_nids[c];
  2314. if (!is_reachable_path(codec, pin, adc))
  2315. continue;
  2316. path = snd_hda_add_new_path(codec, pin, adc, anchor);
  2317. if (!path)
  2318. continue;
  2319. print_nid_path("input", path);
  2320. spec->input_paths[imux_idx][c] =
  2321. snd_hda_get_path_idx(codec, path);
  2322. if (!imux_added) {
  2323. spec->imux_pins[imux->num_items] = pin;
  2324. snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
  2325. imux_added = true;
  2326. }
  2327. }
  2328. return 0;
  2329. }
  2330. /*
  2331. * create playback/capture controls for input pins
  2332. */
  2333. /* fill the label for each input at first */
  2334. static int fill_input_pin_labels(struct hda_codec *codec)
  2335. {
  2336. struct hda_gen_spec *spec = codec->spec;
  2337. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2338. int i;
  2339. for (i = 0; i < cfg->num_inputs; i++) {
  2340. hda_nid_t pin = cfg->inputs[i].pin;
  2341. const char *label;
  2342. int j, idx;
  2343. if (!is_input_pin(codec, pin))
  2344. continue;
  2345. label = hda_get_autocfg_input_label(codec, cfg, i);
  2346. idx = 0;
  2347. for (j = i - 1; j >= 0; j--) {
  2348. if (spec->input_labels[j] &&
  2349. !strcmp(spec->input_labels[j], label)) {
  2350. idx = spec->input_label_idxs[j] + 1;
  2351. break;
  2352. }
  2353. }
  2354. spec->input_labels[i] = label;
  2355. spec->input_label_idxs[i] = idx;
  2356. }
  2357. return 0;
  2358. }
  2359. #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
  2360. static int create_input_ctls(struct hda_codec *codec)
  2361. {
  2362. struct hda_gen_spec *spec = codec->spec;
  2363. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2364. hda_nid_t mixer = spec->mixer_nid;
  2365. int num_adcs;
  2366. int i, err;
  2367. unsigned int val;
  2368. num_adcs = fill_adc_nids(codec);
  2369. if (num_adcs < 0)
  2370. return 0;
  2371. err = fill_input_pin_labels(codec);
  2372. if (err < 0)
  2373. return err;
  2374. for (i = 0; i < cfg->num_inputs; i++) {
  2375. hda_nid_t pin;
  2376. pin = cfg->inputs[i].pin;
  2377. if (!is_input_pin(codec, pin))
  2378. continue;
  2379. val = PIN_IN;
  2380. if (cfg->inputs[i].type == AUTO_PIN_MIC)
  2381. val |= snd_hda_get_default_vref(codec, pin);
  2382. set_pin_target(codec, pin, val, false);
  2383. if (mixer) {
  2384. if (is_reachable_path(codec, pin, mixer)) {
  2385. err = new_analog_input(codec, i, pin,
  2386. spec->input_labels[i],
  2387. spec->input_label_idxs[i],
  2388. mixer);
  2389. if (err < 0)
  2390. return err;
  2391. }
  2392. }
  2393. err = parse_capture_source(codec, pin, i, num_adcs,
  2394. spec->input_labels[i], -mixer);
  2395. if (err < 0)
  2396. return err;
  2397. if (spec->add_in_jack_modes) {
  2398. err = create_in_jack_mode(codec, pin);
  2399. if (err < 0)
  2400. return err;
  2401. }
  2402. }
  2403. if (mixer && spec->add_stereo_mix_input) {
  2404. err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
  2405. "Stereo Mix", 0);
  2406. if (err < 0)
  2407. return err;
  2408. }
  2409. return 0;
  2410. }
  2411. /*
  2412. * input source mux
  2413. */
  2414. /* get the input path specified by the given adc and imux indices */
  2415. static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
  2416. {
  2417. struct hda_gen_spec *spec = codec->spec;
  2418. if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
  2419. snd_BUG();
  2420. return NULL;
  2421. }
  2422. if (spec->dyn_adc_switch)
  2423. adc_idx = spec->dyn_adc_idx[imux_idx];
  2424. if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_INS) {
  2425. snd_BUG();
  2426. return NULL;
  2427. }
  2428. return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
  2429. }
  2430. static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
  2431. unsigned int idx);
  2432. static int mux_enum_info(struct snd_kcontrol *kcontrol,
  2433. struct snd_ctl_elem_info *uinfo)
  2434. {
  2435. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2436. struct hda_gen_spec *spec = codec->spec;
  2437. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  2438. }
  2439. static int mux_enum_get(struct snd_kcontrol *kcontrol,
  2440. struct snd_ctl_elem_value *ucontrol)
  2441. {
  2442. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2443. struct hda_gen_spec *spec = codec->spec;
  2444. /* the ctls are created at once with multiple counts */
  2445. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2446. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  2447. return 0;
  2448. }
  2449. static int mux_enum_put(struct snd_kcontrol *kcontrol,
  2450. struct snd_ctl_elem_value *ucontrol)
  2451. {
  2452. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2453. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2454. return mux_select(codec, adc_idx,
  2455. ucontrol->value.enumerated.item[0]);
  2456. }
  2457. static const struct snd_kcontrol_new cap_src_temp = {
  2458. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2459. .name = "Input Source",
  2460. .info = mux_enum_info,
  2461. .get = mux_enum_get,
  2462. .put = mux_enum_put,
  2463. };
  2464. /*
  2465. * capture volume and capture switch ctls
  2466. */
  2467. typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
  2468. struct snd_ctl_elem_value *ucontrol);
  2469. /* call the given amp update function for all amps in the imux list at once */
  2470. static int cap_put_caller(struct snd_kcontrol *kcontrol,
  2471. struct snd_ctl_elem_value *ucontrol,
  2472. put_call_t func, int type)
  2473. {
  2474. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2475. struct hda_gen_spec *spec = codec->spec;
  2476. const struct hda_input_mux *imux;
  2477. struct nid_path *path;
  2478. int i, adc_idx, err = 0;
  2479. imux = &spec->input_mux;
  2480. adc_idx = kcontrol->id.index;
  2481. mutex_lock(&codec->control_mutex);
  2482. /* we use the cache-only update at first since multiple input paths
  2483. * may shared the same amp; by updating only caches, the redundant
  2484. * writes to hardware can be reduced.
  2485. */
  2486. codec->cached_write = 1;
  2487. for (i = 0; i < imux->num_items; i++) {
  2488. path = get_input_path(codec, adc_idx, i);
  2489. if (!path || !path->ctls[type])
  2490. continue;
  2491. kcontrol->private_value = path->ctls[type];
  2492. err = func(kcontrol, ucontrol);
  2493. if (err < 0)
  2494. goto error;
  2495. }
  2496. error:
  2497. codec->cached_write = 0;
  2498. mutex_unlock(&codec->control_mutex);
  2499. snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
  2500. if (err >= 0 && spec->cap_sync_hook)
  2501. spec->cap_sync_hook(codec, ucontrol);
  2502. return err;
  2503. }
  2504. /* capture volume ctl callbacks */
  2505. #define cap_vol_info snd_hda_mixer_amp_volume_info
  2506. #define cap_vol_get snd_hda_mixer_amp_volume_get
  2507. #define cap_vol_tlv snd_hda_mixer_amp_tlv
  2508. static int cap_vol_put(struct snd_kcontrol *kcontrol,
  2509. struct snd_ctl_elem_value *ucontrol)
  2510. {
  2511. return cap_put_caller(kcontrol, ucontrol,
  2512. snd_hda_mixer_amp_volume_put,
  2513. NID_PATH_VOL_CTL);
  2514. }
  2515. static const struct snd_kcontrol_new cap_vol_temp = {
  2516. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2517. .name = "Capture Volume",
  2518. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2519. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2520. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
  2521. .info = cap_vol_info,
  2522. .get = cap_vol_get,
  2523. .put = cap_vol_put,
  2524. .tlv = { .c = cap_vol_tlv },
  2525. };
  2526. /* capture switch ctl callbacks */
  2527. #define cap_sw_info snd_ctl_boolean_stereo_info
  2528. #define cap_sw_get snd_hda_mixer_amp_switch_get
  2529. static int cap_sw_put(struct snd_kcontrol *kcontrol,
  2530. struct snd_ctl_elem_value *ucontrol)
  2531. {
  2532. return cap_put_caller(kcontrol, ucontrol,
  2533. snd_hda_mixer_amp_switch_put,
  2534. NID_PATH_MUTE_CTL);
  2535. }
  2536. static const struct snd_kcontrol_new cap_sw_temp = {
  2537. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2538. .name = "Capture Switch",
  2539. .info = cap_sw_info,
  2540. .get = cap_sw_get,
  2541. .put = cap_sw_put,
  2542. };
  2543. static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
  2544. {
  2545. hda_nid_t nid;
  2546. int i, depth;
  2547. path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
  2548. for (depth = 0; depth < 3; depth++) {
  2549. if (depth >= path->depth)
  2550. return -EINVAL;
  2551. i = path->depth - depth - 1;
  2552. nid = path->path[i];
  2553. if (!path->ctls[NID_PATH_VOL_CTL]) {
  2554. if (nid_has_volume(codec, nid, HDA_OUTPUT))
  2555. path->ctls[NID_PATH_VOL_CTL] =
  2556. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  2557. else if (nid_has_volume(codec, nid, HDA_INPUT)) {
  2558. int idx = path->idx[i];
  2559. if (!depth && codec->single_adc_amp)
  2560. idx = 0;
  2561. path->ctls[NID_PATH_VOL_CTL] =
  2562. HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
  2563. }
  2564. }
  2565. if (!path->ctls[NID_PATH_MUTE_CTL]) {
  2566. if (nid_has_mute(codec, nid, HDA_OUTPUT))
  2567. path->ctls[NID_PATH_MUTE_CTL] =
  2568. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  2569. else if (nid_has_mute(codec, nid, HDA_INPUT)) {
  2570. int idx = path->idx[i];
  2571. if (!depth && codec->single_adc_amp)
  2572. idx = 0;
  2573. path->ctls[NID_PATH_MUTE_CTL] =
  2574. HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
  2575. }
  2576. }
  2577. }
  2578. return 0;
  2579. }
  2580. static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
  2581. {
  2582. struct hda_gen_spec *spec = codec->spec;
  2583. struct auto_pin_cfg *cfg = &spec->autocfg;
  2584. unsigned int val;
  2585. int i;
  2586. if (!spec->inv_dmic_split)
  2587. return false;
  2588. for (i = 0; i < cfg->num_inputs; i++) {
  2589. if (cfg->inputs[i].pin != nid)
  2590. continue;
  2591. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  2592. return false;
  2593. val = snd_hda_codec_get_pincfg(codec, nid);
  2594. return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
  2595. }
  2596. return false;
  2597. }
  2598. /* capture switch put callback for a single control with hook call */
  2599. static int cap_single_sw_put(struct snd_kcontrol *kcontrol,
  2600. struct snd_ctl_elem_value *ucontrol)
  2601. {
  2602. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2603. struct hda_gen_spec *spec = codec->spec;
  2604. int ret;
  2605. ret = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  2606. if (ret < 0)
  2607. return ret;
  2608. if (spec->cap_sync_hook)
  2609. spec->cap_sync_hook(codec, ucontrol);
  2610. return ret;
  2611. }
  2612. static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
  2613. int idx, bool is_switch, unsigned int ctl,
  2614. bool inv_dmic)
  2615. {
  2616. struct hda_gen_spec *spec = codec->spec;
  2617. char tmpname[44];
  2618. int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
  2619. const char *sfx = is_switch ? "Switch" : "Volume";
  2620. unsigned int chs = inv_dmic ? 1 : 3;
  2621. struct snd_kcontrol_new *knew;
  2622. if (!ctl)
  2623. return 0;
  2624. if (label)
  2625. snprintf(tmpname, sizeof(tmpname),
  2626. "%s Capture %s", label, sfx);
  2627. else
  2628. snprintf(tmpname, sizeof(tmpname),
  2629. "Capture %s", sfx);
  2630. knew = add_control(spec, type, tmpname, idx,
  2631. amp_val_replace_channels(ctl, chs));
  2632. if (!knew)
  2633. return -ENOMEM;
  2634. if (is_switch)
  2635. knew->put = cap_single_sw_put;
  2636. if (!inv_dmic)
  2637. return 0;
  2638. /* Make independent right kcontrol */
  2639. if (label)
  2640. snprintf(tmpname, sizeof(tmpname),
  2641. "Inverted %s Capture %s", label, sfx);
  2642. else
  2643. snprintf(tmpname, sizeof(tmpname),
  2644. "Inverted Capture %s", sfx);
  2645. knew = add_control(spec, type, tmpname, idx,
  2646. amp_val_replace_channels(ctl, 2));
  2647. if (!knew)
  2648. return -ENOMEM;
  2649. if (is_switch)
  2650. knew->put = cap_single_sw_put;
  2651. return 0;
  2652. }
  2653. /* create single (and simple) capture volume and switch controls */
  2654. static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
  2655. unsigned int vol_ctl, unsigned int sw_ctl,
  2656. bool inv_dmic)
  2657. {
  2658. int err;
  2659. err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
  2660. if (err < 0)
  2661. return err;
  2662. err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
  2663. if (err < 0)
  2664. return err;
  2665. return 0;
  2666. }
  2667. /* create bound capture volume and switch controls */
  2668. static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
  2669. unsigned int vol_ctl, unsigned int sw_ctl)
  2670. {
  2671. struct hda_gen_spec *spec = codec->spec;
  2672. struct snd_kcontrol_new *knew;
  2673. if (vol_ctl) {
  2674. knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
  2675. if (!knew)
  2676. return -ENOMEM;
  2677. knew->index = idx;
  2678. knew->private_value = vol_ctl;
  2679. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  2680. }
  2681. if (sw_ctl) {
  2682. knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
  2683. if (!knew)
  2684. return -ENOMEM;
  2685. knew->index = idx;
  2686. knew->private_value = sw_ctl;
  2687. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  2688. }
  2689. return 0;
  2690. }
  2691. /* return the vol ctl when used first in the imux list */
  2692. static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
  2693. {
  2694. struct nid_path *path;
  2695. unsigned int ctl;
  2696. int i;
  2697. path = get_input_path(codec, 0, idx);
  2698. if (!path)
  2699. return 0;
  2700. ctl = path->ctls[type];
  2701. if (!ctl)
  2702. return 0;
  2703. for (i = 0; i < idx - 1; i++) {
  2704. path = get_input_path(codec, 0, i);
  2705. if (path && path->ctls[type] == ctl)
  2706. return 0;
  2707. }
  2708. return ctl;
  2709. }
  2710. /* create individual capture volume and switch controls per input */
  2711. static int create_multi_cap_vol_ctl(struct hda_codec *codec)
  2712. {
  2713. struct hda_gen_spec *spec = codec->spec;
  2714. struct hda_input_mux *imux = &spec->input_mux;
  2715. int i, err, type;
  2716. for (i = 0; i < imux->num_items; i++) {
  2717. bool inv_dmic;
  2718. int idx;
  2719. idx = imux->items[i].index;
  2720. if (idx >= spec->autocfg.num_inputs)
  2721. continue;
  2722. inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
  2723. for (type = 0; type < 2; type++) {
  2724. err = add_single_cap_ctl(codec,
  2725. spec->input_labels[idx],
  2726. spec->input_label_idxs[idx],
  2727. type,
  2728. get_first_cap_ctl(codec, i, type),
  2729. inv_dmic);
  2730. if (err < 0)
  2731. return err;
  2732. }
  2733. }
  2734. return 0;
  2735. }
  2736. static int create_capture_mixers(struct hda_codec *codec)
  2737. {
  2738. struct hda_gen_spec *spec = codec->spec;
  2739. struct hda_input_mux *imux = &spec->input_mux;
  2740. int i, n, nums, err;
  2741. if (spec->dyn_adc_switch)
  2742. nums = 1;
  2743. else
  2744. nums = spec->num_adc_nids;
  2745. if (!spec->auto_mic && imux->num_items > 1) {
  2746. struct snd_kcontrol_new *knew;
  2747. const char *name;
  2748. name = nums > 1 ? "Input Source" : "Capture Source";
  2749. knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
  2750. if (!knew)
  2751. return -ENOMEM;
  2752. knew->count = nums;
  2753. }
  2754. for (n = 0; n < nums; n++) {
  2755. bool multi = false;
  2756. bool multi_cap_vol = spec->multi_cap_vol;
  2757. bool inv_dmic = false;
  2758. int vol, sw;
  2759. vol = sw = 0;
  2760. for (i = 0; i < imux->num_items; i++) {
  2761. struct nid_path *path;
  2762. path = get_input_path(codec, n, i);
  2763. if (!path)
  2764. continue;
  2765. parse_capvol_in_path(codec, path);
  2766. if (!vol)
  2767. vol = path->ctls[NID_PATH_VOL_CTL];
  2768. else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
  2769. multi = true;
  2770. if (!same_amp_caps(codec, vol,
  2771. path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
  2772. multi_cap_vol = true;
  2773. }
  2774. if (!sw)
  2775. sw = path->ctls[NID_PATH_MUTE_CTL];
  2776. else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
  2777. multi = true;
  2778. if (!same_amp_caps(codec, sw,
  2779. path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
  2780. multi_cap_vol = true;
  2781. }
  2782. if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
  2783. inv_dmic = true;
  2784. }
  2785. if (!multi)
  2786. err = create_single_cap_vol_ctl(codec, n, vol, sw,
  2787. inv_dmic);
  2788. else if (!multi_cap_vol)
  2789. err = create_bind_cap_vol_ctl(codec, n, vol, sw);
  2790. else
  2791. err = create_multi_cap_vol_ctl(codec);
  2792. if (err < 0)
  2793. return err;
  2794. }
  2795. return 0;
  2796. }
  2797. /*
  2798. * add mic boosts if needed
  2799. */
  2800. /* check whether the given amp is feasible as a boost volume */
  2801. static bool check_boost_vol(struct hda_codec *codec, hda_nid_t nid,
  2802. int dir, int idx)
  2803. {
  2804. unsigned int step;
  2805. if (!nid_has_volume(codec, nid, dir) ||
  2806. is_ctl_associated(codec, nid, dir, idx, NID_PATH_VOL_CTL) ||
  2807. is_ctl_associated(codec, nid, dir, idx, NID_PATH_BOOST_CTL))
  2808. return false;
  2809. step = (query_amp_caps(codec, nid, dir) & AC_AMPCAP_STEP_SIZE)
  2810. >> AC_AMPCAP_STEP_SIZE_SHIFT;
  2811. if (step < 0x20)
  2812. return false;
  2813. return true;
  2814. }
  2815. /* look for a boost amp in a widget close to the pin */
  2816. static unsigned int look_for_boost_amp(struct hda_codec *codec,
  2817. struct nid_path *path)
  2818. {
  2819. unsigned int val = 0;
  2820. hda_nid_t nid;
  2821. int depth;
  2822. for (depth = 0; depth < 3; depth++) {
  2823. if (depth >= path->depth - 1)
  2824. break;
  2825. nid = path->path[depth];
  2826. if (depth && check_boost_vol(codec, nid, HDA_OUTPUT, 0)) {
  2827. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  2828. break;
  2829. } else if (check_boost_vol(codec, nid, HDA_INPUT,
  2830. path->idx[depth])) {
  2831. val = HDA_COMPOSE_AMP_VAL(nid, 3, path->idx[depth],
  2832. HDA_INPUT);
  2833. break;
  2834. }
  2835. }
  2836. return val;
  2837. }
  2838. static int parse_mic_boost(struct hda_codec *codec)
  2839. {
  2840. struct hda_gen_spec *spec = codec->spec;
  2841. struct auto_pin_cfg *cfg = &spec->autocfg;
  2842. struct hda_input_mux *imux = &spec->input_mux;
  2843. int i;
  2844. if (!spec->num_adc_nids)
  2845. return 0;
  2846. for (i = 0; i < imux->num_items; i++) {
  2847. struct nid_path *path;
  2848. unsigned int val;
  2849. int idx;
  2850. char boost_label[44];
  2851. idx = imux->items[i].index;
  2852. if (idx >= imux->num_items)
  2853. continue;
  2854. /* check only line-in and mic pins */
  2855. if (cfg->inputs[idx].type > AUTO_PIN_LINE_IN)
  2856. continue;
  2857. path = get_input_path(codec, 0, i);
  2858. if (!path)
  2859. continue;
  2860. val = look_for_boost_amp(codec, path);
  2861. if (!val)
  2862. continue;
  2863. /* create a boost control */
  2864. snprintf(boost_label, sizeof(boost_label),
  2865. "%s Boost Volume", spec->input_labels[idx]);
  2866. if (!add_control(spec, HDA_CTL_WIDGET_VOL, boost_label,
  2867. spec->input_label_idxs[idx], val))
  2868. return -ENOMEM;
  2869. path->ctls[NID_PATH_BOOST_CTL] = val;
  2870. }
  2871. return 0;
  2872. }
  2873. /*
  2874. * parse digital I/Os and set up NIDs in BIOS auto-parse mode
  2875. */
  2876. static void parse_digital(struct hda_codec *codec)
  2877. {
  2878. struct hda_gen_spec *spec = codec->spec;
  2879. struct nid_path *path;
  2880. int i, nums;
  2881. hda_nid_t dig_nid, pin;
  2882. /* support multiple SPDIFs; the secondary is set up as a slave */
  2883. nums = 0;
  2884. for (i = 0; i < spec->autocfg.dig_outs; i++) {
  2885. pin = spec->autocfg.dig_out_pins[i];
  2886. dig_nid = look_for_dac(codec, pin, true);
  2887. if (!dig_nid)
  2888. continue;
  2889. path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
  2890. if (!path)
  2891. continue;
  2892. print_nid_path("digout", path);
  2893. path->active = true;
  2894. spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
  2895. set_pin_target(codec, pin, PIN_OUT, false);
  2896. if (!nums) {
  2897. spec->multiout.dig_out_nid = dig_nid;
  2898. spec->dig_out_type = spec->autocfg.dig_out_type[0];
  2899. } else {
  2900. spec->multiout.slave_dig_outs = spec->slave_dig_outs;
  2901. if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
  2902. break;
  2903. spec->slave_dig_outs[nums - 1] = dig_nid;
  2904. }
  2905. nums++;
  2906. }
  2907. if (spec->autocfg.dig_in_pin) {
  2908. pin = spec->autocfg.dig_in_pin;
  2909. dig_nid = codec->start_nid;
  2910. for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
  2911. unsigned int wcaps = get_wcaps(codec, dig_nid);
  2912. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  2913. continue;
  2914. if (!(wcaps & AC_WCAP_DIGITAL))
  2915. continue;
  2916. path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
  2917. if (path) {
  2918. print_nid_path("digin", path);
  2919. path->active = true;
  2920. spec->dig_in_nid = dig_nid;
  2921. spec->digin_path = snd_hda_get_path_idx(codec, path);
  2922. set_pin_target(codec, pin, PIN_IN, false);
  2923. break;
  2924. }
  2925. }
  2926. }
  2927. }
  2928. /*
  2929. * input MUX handling
  2930. */
  2931. static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
  2932. /* select the given imux item; either unmute exclusively or select the route */
  2933. static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
  2934. unsigned int idx)
  2935. {
  2936. struct hda_gen_spec *spec = codec->spec;
  2937. const struct hda_input_mux *imux;
  2938. struct nid_path *path;
  2939. imux = &spec->input_mux;
  2940. if (!imux->num_items)
  2941. return 0;
  2942. if (idx >= imux->num_items)
  2943. idx = imux->num_items - 1;
  2944. if (spec->cur_mux[adc_idx] == idx)
  2945. return 0;
  2946. path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
  2947. if (!path)
  2948. return 0;
  2949. if (path->active)
  2950. snd_hda_activate_path(codec, path, false, false);
  2951. spec->cur_mux[adc_idx] = idx;
  2952. if (spec->shared_mic_hp)
  2953. update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
  2954. if (spec->dyn_adc_switch)
  2955. dyn_adc_pcm_resetup(codec, idx);
  2956. path = get_input_path(codec, adc_idx, idx);
  2957. if (!path)
  2958. return 0;
  2959. if (path->active)
  2960. return 0;
  2961. snd_hda_activate_path(codec, path, true, false);
  2962. if (spec->cap_sync_hook)
  2963. spec->cap_sync_hook(codec, NULL);
  2964. return 1;
  2965. }
  2966. /*
  2967. * Jack detections for HP auto-mute and mic-switch
  2968. */
  2969. /* check each pin in the given array; returns true if any of them is plugged */
  2970. static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
  2971. {
  2972. int i, present = 0;
  2973. for (i = 0; i < num_pins; i++) {
  2974. hda_nid_t nid = pins[i];
  2975. if (!nid)
  2976. break;
  2977. /* don't detect pins retasked as inputs */
  2978. if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
  2979. continue;
  2980. present |= snd_hda_jack_detect(codec, nid);
  2981. }
  2982. return present;
  2983. }
  2984. /* standard HP/line-out auto-mute helper */
  2985. static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
  2986. bool mute)
  2987. {
  2988. struct hda_gen_spec *spec = codec->spec;
  2989. int i;
  2990. for (i = 0; i < num_pins; i++) {
  2991. hda_nid_t nid = pins[i];
  2992. unsigned int val;
  2993. if (!nid)
  2994. break;
  2995. /* don't reset VREF value in case it's controlling
  2996. * the amp (see alc861_fixup_asus_amp_vref_0f())
  2997. */
  2998. if (spec->keep_vref_in_automute)
  2999. val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
  3000. else
  3001. val = 0;
  3002. if (!mute)
  3003. val |= snd_hda_codec_get_pin_target(codec, nid);
  3004. /* here we call update_pin_ctl() so that the pinctl is changed
  3005. * without changing the pinctl target value;
  3006. * the original target value will be still referred at the
  3007. * init / resume again
  3008. */
  3009. update_pin_ctl(codec, nid, val);
  3010. set_pin_eapd(codec, nid, !mute);
  3011. }
  3012. }
  3013. /* Toggle outputs muting */
  3014. void snd_hda_gen_update_outputs(struct hda_codec *codec)
  3015. {
  3016. struct hda_gen_spec *spec = codec->spec;
  3017. int on;
  3018. /* Control HP pins/amps depending on master_mute state;
  3019. * in general, HP pins/amps control should be enabled in all cases,
  3020. * but currently set only for master_mute, just to be safe
  3021. */
  3022. if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
  3023. do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
  3024. spec->autocfg.hp_pins, spec->master_mute);
  3025. if (!spec->automute_speaker)
  3026. on = 0;
  3027. else
  3028. on = spec->hp_jack_present | spec->line_jack_present;
  3029. on |= spec->master_mute;
  3030. spec->speaker_muted = on;
  3031. do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
  3032. spec->autocfg.speaker_pins, on);
  3033. /* toggle line-out mutes if needed, too */
  3034. /* if LO is a copy of either HP or Speaker, don't need to handle it */
  3035. if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
  3036. spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
  3037. return;
  3038. if (!spec->automute_lo)
  3039. on = 0;
  3040. else
  3041. on = spec->hp_jack_present;
  3042. on |= spec->master_mute;
  3043. spec->line_out_muted = on;
  3044. do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
  3045. spec->autocfg.line_out_pins, on);
  3046. }
  3047. EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
  3048. static void call_update_outputs(struct hda_codec *codec)
  3049. {
  3050. struct hda_gen_spec *spec = codec->spec;
  3051. if (spec->automute_hook)
  3052. spec->automute_hook(codec);
  3053. else
  3054. snd_hda_gen_update_outputs(codec);
  3055. }
  3056. /* standard HP-automute helper */
  3057. void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
  3058. {
  3059. struct hda_gen_spec *spec = codec->spec;
  3060. spec->hp_jack_present =
  3061. detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
  3062. spec->autocfg.hp_pins);
  3063. if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
  3064. return;
  3065. call_update_outputs(codec);
  3066. }
  3067. EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
  3068. /* standard line-out-automute helper */
  3069. void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
  3070. {
  3071. struct hda_gen_spec *spec = codec->spec;
  3072. if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
  3073. return;
  3074. /* check LO jack only when it's different from HP */
  3075. if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
  3076. return;
  3077. spec->line_jack_present =
  3078. detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
  3079. spec->autocfg.line_out_pins);
  3080. if (!spec->automute_speaker || !spec->detect_lo)
  3081. return;
  3082. call_update_outputs(codec);
  3083. }
  3084. EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
  3085. /* standard mic auto-switch helper */
  3086. void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
  3087. {
  3088. struct hda_gen_spec *spec = codec->spec;
  3089. int i;
  3090. if (!spec->auto_mic)
  3091. return;
  3092. for (i = spec->am_num_entries - 1; i > 0; i--) {
  3093. hda_nid_t pin = spec->am_entry[i].pin;
  3094. /* don't detect pins retasked as outputs */
  3095. if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
  3096. continue;
  3097. if (snd_hda_jack_detect(codec, pin)) {
  3098. mux_select(codec, 0, spec->am_entry[i].idx);
  3099. return;
  3100. }
  3101. }
  3102. mux_select(codec, 0, spec->am_entry[0].idx);
  3103. }
  3104. EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
  3105. /* update jack retasking */
  3106. static void update_automute_all(struct hda_codec *codec)
  3107. {
  3108. struct hda_gen_spec *spec = codec->spec;
  3109. if (spec->hp_automute_hook)
  3110. spec->hp_automute_hook(codec, NULL);
  3111. else
  3112. snd_hda_gen_hp_automute(codec, NULL);
  3113. if (spec->line_automute_hook)
  3114. spec->line_automute_hook(codec, NULL);
  3115. else
  3116. snd_hda_gen_line_automute(codec, NULL);
  3117. if (spec->mic_autoswitch_hook)
  3118. spec->mic_autoswitch_hook(codec, NULL);
  3119. else
  3120. snd_hda_gen_mic_autoswitch(codec, NULL);
  3121. }
  3122. /*
  3123. * Auto-Mute mode mixer enum support
  3124. */
  3125. static int automute_mode_info(struct snd_kcontrol *kcontrol,
  3126. struct snd_ctl_elem_info *uinfo)
  3127. {
  3128. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3129. struct hda_gen_spec *spec = codec->spec;
  3130. static const char * const texts3[] = {
  3131. "Disabled", "Speaker Only", "Line Out+Speaker"
  3132. };
  3133. if (spec->automute_speaker_possible && spec->automute_lo_possible)
  3134. return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
  3135. return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
  3136. }
  3137. static int automute_mode_get(struct snd_kcontrol *kcontrol,
  3138. struct snd_ctl_elem_value *ucontrol)
  3139. {
  3140. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3141. struct hda_gen_spec *spec = codec->spec;
  3142. unsigned int val = 0;
  3143. if (spec->automute_speaker)
  3144. val++;
  3145. if (spec->automute_lo)
  3146. val++;
  3147. ucontrol->value.enumerated.item[0] = val;
  3148. return 0;
  3149. }
  3150. static int automute_mode_put(struct snd_kcontrol *kcontrol,
  3151. struct snd_ctl_elem_value *ucontrol)
  3152. {
  3153. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  3154. struct hda_gen_spec *spec = codec->spec;
  3155. switch (ucontrol->value.enumerated.item[0]) {
  3156. case 0:
  3157. if (!spec->automute_speaker && !spec->automute_lo)
  3158. return 0;
  3159. spec->automute_speaker = 0;
  3160. spec->automute_lo = 0;
  3161. break;
  3162. case 1:
  3163. if (spec->automute_speaker_possible) {
  3164. if (!spec->automute_lo && spec->automute_speaker)
  3165. return 0;
  3166. spec->automute_speaker = 1;
  3167. spec->automute_lo = 0;
  3168. } else if (spec->automute_lo_possible) {
  3169. if (spec->automute_lo)
  3170. return 0;
  3171. spec->automute_lo = 1;
  3172. } else
  3173. return -EINVAL;
  3174. break;
  3175. case 2:
  3176. if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
  3177. return -EINVAL;
  3178. if (spec->automute_speaker && spec->automute_lo)
  3179. return 0;
  3180. spec->automute_speaker = 1;
  3181. spec->automute_lo = 1;
  3182. break;
  3183. default:
  3184. return -EINVAL;
  3185. }
  3186. call_update_outputs(codec);
  3187. return 1;
  3188. }
  3189. static const struct snd_kcontrol_new automute_mode_enum = {
  3190. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3191. .name = "Auto-Mute Mode",
  3192. .info = automute_mode_info,
  3193. .get = automute_mode_get,
  3194. .put = automute_mode_put,
  3195. };
  3196. static int add_automute_mode_enum(struct hda_codec *codec)
  3197. {
  3198. struct hda_gen_spec *spec = codec->spec;
  3199. if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
  3200. return -ENOMEM;
  3201. return 0;
  3202. }
  3203. /*
  3204. * Check the availability of HP/line-out auto-mute;
  3205. * Set up appropriately if really supported
  3206. */
  3207. static int check_auto_mute_availability(struct hda_codec *codec)
  3208. {
  3209. struct hda_gen_spec *spec = codec->spec;
  3210. struct auto_pin_cfg *cfg = &spec->autocfg;
  3211. int present = 0;
  3212. int i, err;
  3213. if (spec->suppress_auto_mute)
  3214. return 0;
  3215. if (cfg->hp_pins[0])
  3216. present++;
  3217. if (cfg->line_out_pins[0])
  3218. present++;
  3219. if (cfg->speaker_pins[0])
  3220. present++;
  3221. if (present < 2) /* need two different output types */
  3222. return 0;
  3223. if (!cfg->speaker_pins[0] &&
  3224. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  3225. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  3226. sizeof(cfg->speaker_pins));
  3227. cfg->speaker_outs = cfg->line_outs;
  3228. }
  3229. if (!cfg->hp_pins[0] &&
  3230. cfg->line_out_type == AUTO_PIN_HP_OUT) {
  3231. memcpy(cfg->hp_pins, cfg->line_out_pins,
  3232. sizeof(cfg->hp_pins));
  3233. cfg->hp_outs = cfg->line_outs;
  3234. }
  3235. for (i = 0; i < cfg->hp_outs; i++) {
  3236. hda_nid_t nid = cfg->hp_pins[i];
  3237. if (!is_jack_detectable(codec, nid))
  3238. continue;
  3239. snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
  3240. nid);
  3241. snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
  3242. spec->hp_automute_hook ?
  3243. spec->hp_automute_hook :
  3244. snd_hda_gen_hp_automute);
  3245. spec->detect_hp = 1;
  3246. }
  3247. if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
  3248. if (cfg->speaker_outs)
  3249. for (i = 0; i < cfg->line_outs; i++) {
  3250. hda_nid_t nid = cfg->line_out_pins[i];
  3251. if (!is_jack_detectable(codec, nid))
  3252. continue;
  3253. snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
  3254. snd_hda_jack_detect_enable_callback(codec, nid,
  3255. HDA_GEN_FRONT_EVENT,
  3256. spec->line_automute_hook ?
  3257. spec->line_automute_hook :
  3258. snd_hda_gen_line_automute);
  3259. spec->detect_lo = 1;
  3260. }
  3261. spec->automute_lo_possible = spec->detect_hp;
  3262. }
  3263. spec->automute_speaker_possible = cfg->speaker_outs &&
  3264. (spec->detect_hp || spec->detect_lo);
  3265. spec->automute_lo = spec->automute_lo_possible;
  3266. spec->automute_speaker = spec->automute_speaker_possible;
  3267. if (spec->automute_speaker_possible || spec->automute_lo_possible) {
  3268. /* create a control for automute mode */
  3269. err = add_automute_mode_enum(codec);
  3270. if (err < 0)
  3271. return err;
  3272. }
  3273. return 0;
  3274. }
  3275. /* check whether all auto-mic pins are valid; setup indices if OK */
  3276. static bool auto_mic_check_imux(struct hda_codec *codec)
  3277. {
  3278. struct hda_gen_spec *spec = codec->spec;
  3279. const struct hda_input_mux *imux;
  3280. int i;
  3281. imux = &spec->input_mux;
  3282. for (i = 0; i < spec->am_num_entries; i++) {
  3283. spec->am_entry[i].idx =
  3284. find_idx_in_nid_list(spec->am_entry[i].pin,
  3285. spec->imux_pins, imux->num_items);
  3286. if (spec->am_entry[i].idx < 0)
  3287. return false; /* no corresponding imux */
  3288. }
  3289. /* we don't need the jack detection for the first pin */
  3290. for (i = 1; i < spec->am_num_entries; i++)
  3291. snd_hda_jack_detect_enable_callback(codec,
  3292. spec->am_entry[i].pin,
  3293. HDA_GEN_MIC_EVENT,
  3294. spec->mic_autoswitch_hook ?
  3295. spec->mic_autoswitch_hook :
  3296. snd_hda_gen_mic_autoswitch);
  3297. return true;
  3298. }
  3299. static int compare_attr(const void *ap, const void *bp)
  3300. {
  3301. const struct automic_entry *a = ap;
  3302. const struct automic_entry *b = bp;
  3303. return (int)(a->attr - b->attr);
  3304. }
  3305. /*
  3306. * Check the availability of auto-mic switch;
  3307. * Set up if really supported
  3308. */
  3309. static int check_auto_mic_availability(struct hda_codec *codec)
  3310. {
  3311. struct hda_gen_spec *spec = codec->spec;
  3312. struct auto_pin_cfg *cfg = &spec->autocfg;
  3313. unsigned int types;
  3314. int i, num_pins;
  3315. if (spec->suppress_auto_mic)
  3316. return 0;
  3317. types = 0;
  3318. num_pins = 0;
  3319. for (i = 0; i < cfg->num_inputs; i++) {
  3320. hda_nid_t nid = cfg->inputs[i].pin;
  3321. unsigned int attr;
  3322. attr = snd_hda_codec_get_pincfg(codec, nid);
  3323. attr = snd_hda_get_input_pin_attr(attr);
  3324. if (types & (1 << attr))
  3325. return 0; /* already occupied */
  3326. switch (attr) {
  3327. case INPUT_PIN_ATTR_INT:
  3328. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  3329. return 0; /* invalid type */
  3330. break;
  3331. case INPUT_PIN_ATTR_UNUSED:
  3332. return 0; /* invalid entry */
  3333. default:
  3334. if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
  3335. return 0; /* invalid type */
  3336. if (!spec->line_in_auto_switch &&
  3337. cfg->inputs[i].type != AUTO_PIN_MIC)
  3338. return 0; /* only mic is allowed */
  3339. if (!is_jack_detectable(codec, nid))
  3340. return 0; /* no unsol support */
  3341. break;
  3342. }
  3343. if (num_pins >= MAX_AUTO_MIC_PINS)
  3344. return 0;
  3345. types |= (1 << attr);
  3346. spec->am_entry[num_pins].pin = nid;
  3347. spec->am_entry[num_pins].attr = attr;
  3348. num_pins++;
  3349. }
  3350. if (num_pins < 2)
  3351. return 0;
  3352. spec->am_num_entries = num_pins;
  3353. /* sort the am_entry in the order of attr so that the pin with a
  3354. * higher attr will be selected when the jack is plugged.
  3355. */
  3356. sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
  3357. compare_attr, NULL);
  3358. if (!auto_mic_check_imux(codec))
  3359. return 0;
  3360. spec->auto_mic = 1;
  3361. spec->num_adc_nids = 1;
  3362. spec->cur_mux[0] = spec->am_entry[0].idx;
  3363. snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
  3364. spec->am_entry[0].pin,
  3365. spec->am_entry[1].pin,
  3366. spec->am_entry[2].pin);
  3367. return 0;
  3368. }
  3369. /*
  3370. * Parse the given BIOS configuration and set up the hda_gen_spec
  3371. *
  3372. * return 1 if successful, 0 if the proper config is not found,
  3373. * or a negative error code
  3374. */
  3375. int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
  3376. struct auto_pin_cfg *cfg)
  3377. {
  3378. struct hda_gen_spec *spec = codec->spec;
  3379. int err;
  3380. parse_user_hints(codec);
  3381. if (cfg != &spec->autocfg) {
  3382. spec->autocfg = *cfg;
  3383. cfg = &spec->autocfg;
  3384. }
  3385. fill_all_dac_nids(codec);
  3386. if (!cfg->line_outs) {
  3387. if (cfg->dig_outs || cfg->dig_in_pin) {
  3388. spec->multiout.max_channels = 2;
  3389. spec->no_analog = 1;
  3390. goto dig_only;
  3391. }
  3392. return 0; /* can't find valid BIOS pin config */
  3393. }
  3394. if (!spec->no_primary_hp &&
  3395. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
  3396. cfg->line_outs <= cfg->hp_outs) {
  3397. /* use HP as primary out */
  3398. cfg->speaker_outs = cfg->line_outs;
  3399. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  3400. sizeof(cfg->speaker_pins));
  3401. cfg->line_outs = cfg->hp_outs;
  3402. memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
  3403. cfg->hp_outs = 0;
  3404. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  3405. cfg->line_out_type = AUTO_PIN_HP_OUT;
  3406. }
  3407. err = parse_output_paths(codec);
  3408. if (err < 0)
  3409. return err;
  3410. err = create_multi_channel_mode(codec);
  3411. if (err < 0)
  3412. return err;
  3413. err = create_multi_out_ctls(codec, cfg);
  3414. if (err < 0)
  3415. return err;
  3416. err = create_hp_out_ctls(codec);
  3417. if (err < 0)
  3418. return err;
  3419. err = create_speaker_out_ctls(codec);
  3420. if (err < 0)
  3421. return err;
  3422. err = create_indep_hp_ctls(codec);
  3423. if (err < 0)
  3424. return err;
  3425. err = create_loopback_mixing_ctl(codec);
  3426. if (err < 0)
  3427. return err;
  3428. err = create_shared_input(codec);
  3429. if (err < 0)
  3430. return err;
  3431. err = create_input_ctls(codec);
  3432. if (err < 0)
  3433. return err;
  3434. spec->const_channel_count = spec->ext_channel_count;
  3435. /* check the multiple speaker and headphone pins */
  3436. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  3437. spec->const_channel_count = max(spec->const_channel_count,
  3438. cfg->speaker_outs * 2);
  3439. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  3440. spec->const_channel_count = max(spec->const_channel_count,
  3441. cfg->hp_outs * 2);
  3442. spec->multiout.max_channels = max(spec->ext_channel_count,
  3443. spec->const_channel_count);
  3444. err = check_auto_mute_availability(codec);
  3445. if (err < 0)
  3446. return err;
  3447. err = check_dyn_adc_switch(codec);
  3448. if (err < 0)
  3449. return err;
  3450. if (!spec->shared_mic_hp) {
  3451. err = check_auto_mic_availability(codec);
  3452. if (err < 0)
  3453. return err;
  3454. }
  3455. err = create_capture_mixers(codec);
  3456. if (err < 0)
  3457. return err;
  3458. err = parse_mic_boost(codec);
  3459. if (err < 0)
  3460. return err;
  3461. if (spec->add_out_jack_modes) {
  3462. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  3463. err = create_out_jack_modes(codec, cfg->line_outs,
  3464. cfg->line_out_pins);
  3465. if (err < 0)
  3466. return err;
  3467. }
  3468. if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
  3469. err = create_out_jack_modes(codec, cfg->hp_outs,
  3470. cfg->hp_pins);
  3471. if (err < 0)
  3472. return err;
  3473. }
  3474. }
  3475. dig_only:
  3476. parse_digital(codec);
  3477. return 1;
  3478. }
  3479. EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
  3480. /*
  3481. * Build control elements
  3482. */
  3483. /* slave controls for virtual master */
  3484. static const char * const slave_pfxs[] = {
  3485. "Front", "Surround", "Center", "LFE", "Side",
  3486. "Headphone", "Speaker", "Mono", "Line Out",
  3487. "CLFE", "Bass Speaker", "PCM",
  3488. "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
  3489. "Headphone Front", "Headphone Surround", "Headphone CLFE",
  3490. "Headphone Side",
  3491. NULL,
  3492. };
  3493. int snd_hda_gen_build_controls(struct hda_codec *codec)
  3494. {
  3495. struct hda_gen_spec *spec = codec->spec;
  3496. int err;
  3497. if (spec->kctls.used) {
  3498. err = snd_hda_add_new_ctls(codec, spec->kctls.list);
  3499. if (err < 0)
  3500. return err;
  3501. }
  3502. if (spec->multiout.dig_out_nid) {
  3503. err = snd_hda_create_dig_out_ctls(codec,
  3504. spec->multiout.dig_out_nid,
  3505. spec->multiout.dig_out_nid,
  3506. spec->pcm_rec[1].pcm_type);
  3507. if (err < 0)
  3508. return err;
  3509. if (!spec->no_analog) {
  3510. err = snd_hda_create_spdif_share_sw(codec,
  3511. &spec->multiout);
  3512. if (err < 0)
  3513. return err;
  3514. spec->multiout.share_spdif = 1;
  3515. }
  3516. }
  3517. if (spec->dig_in_nid) {
  3518. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  3519. if (err < 0)
  3520. return err;
  3521. }
  3522. /* if we have no master control, let's create it */
  3523. if (!spec->no_analog &&
  3524. !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
  3525. err = snd_hda_add_vmaster(codec, "Master Playback Volume",
  3526. spec->vmaster_tlv, slave_pfxs,
  3527. "Playback Volume");
  3528. if (err < 0)
  3529. return err;
  3530. }
  3531. if (!spec->no_analog &&
  3532. !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
  3533. err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
  3534. NULL, slave_pfxs,
  3535. "Playback Switch",
  3536. true, &spec->vmaster_mute.sw_kctl);
  3537. if (err < 0)
  3538. return err;
  3539. if (spec->vmaster_mute.hook)
  3540. snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
  3541. spec->vmaster_mute_enum);
  3542. }
  3543. free_kctls(spec); /* no longer needed */
  3544. if (spec->shared_mic_hp) {
  3545. int err;
  3546. int nid = spec->autocfg.inputs[1].pin;
  3547. err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
  3548. if (err < 0)
  3549. return err;
  3550. err = snd_hda_jack_detect_enable(codec, nid, 0);
  3551. if (err < 0)
  3552. return err;
  3553. }
  3554. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  3555. if (err < 0)
  3556. return err;
  3557. return 0;
  3558. }
  3559. EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
  3560. /*
  3561. * PCM definitions
  3562. */
  3563. static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
  3564. struct hda_codec *codec,
  3565. struct snd_pcm_substream *substream,
  3566. int action)
  3567. {
  3568. struct hda_gen_spec *spec = codec->spec;
  3569. if (spec->pcm_playback_hook)
  3570. spec->pcm_playback_hook(hinfo, codec, substream, action);
  3571. }
  3572. static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
  3573. struct hda_codec *codec,
  3574. struct snd_pcm_substream *substream,
  3575. int action)
  3576. {
  3577. struct hda_gen_spec *spec = codec->spec;
  3578. if (spec->pcm_capture_hook)
  3579. spec->pcm_capture_hook(hinfo, codec, substream, action);
  3580. }
  3581. /*
  3582. * Analog playback callbacks
  3583. */
  3584. static int playback_pcm_open(struct hda_pcm_stream *hinfo,
  3585. struct hda_codec *codec,
  3586. struct snd_pcm_substream *substream)
  3587. {
  3588. struct hda_gen_spec *spec = codec->spec;
  3589. int err;
  3590. mutex_lock(&spec->pcm_mutex);
  3591. err = snd_hda_multi_out_analog_open(codec,
  3592. &spec->multiout, substream,
  3593. hinfo);
  3594. if (!err) {
  3595. spec->active_streams |= 1 << STREAM_MULTI_OUT;
  3596. call_pcm_playback_hook(hinfo, codec, substream,
  3597. HDA_GEN_PCM_ACT_OPEN);
  3598. }
  3599. mutex_unlock(&spec->pcm_mutex);
  3600. return err;
  3601. }
  3602. static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3603. struct hda_codec *codec,
  3604. unsigned int stream_tag,
  3605. unsigned int format,
  3606. struct snd_pcm_substream *substream)
  3607. {
  3608. struct hda_gen_spec *spec = codec->spec;
  3609. int err;
  3610. err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  3611. stream_tag, format, substream);
  3612. if (!err)
  3613. call_pcm_playback_hook(hinfo, codec, substream,
  3614. HDA_GEN_PCM_ACT_PREPARE);
  3615. return err;
  3616. }
  3617. static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3618. struct hda_codec *codec,
  3619. struct snd_pcm_substream *substream)
  3620. {
  3621. struct hda_gen_spec *spec = codec->spec;
  3622. int err;
  3623. err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  3624. if (!err)
  3625. call_pcm_playback_hook(hinfo, codec, substream,
  3626. HDA_GEN_PCM_ACT_CLEANUP);
  3627. return err;
  3628. }
  3629. static int playback_pcm_close(struct hda_pcm_stream *hinfo,
  3630. struct hda_codec *codec,
  3631. struct snd_pcm_substream *substream)
  3632. {
  3633. struct hda_gen_spec *spec = codec->spec;
  3634. mutex_lock(&spec->pcm_mutex);
  3635. spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
  3636. call_pcm_playback_hook(hinfo, codec, substream,
  3637. HDA_GEN_PCM_ACT_CLOSE);
  3638. mutex_unlock(&spec->pcm_mutex);
  3639. return 0;
  3640. }
  3641. static int capture_pcm_open(struct hda_pcm_stream *hinfo,
  3642. struct hda_codec *codec,
  3643. struct snd_pcm_substream *substream)
  3644. {
  3645. call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
  3646. return 0;
  3647. }
  3648. static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3649. struct hda_codec *codec,
  3650. unsigned int stream_tag,
  3651. unsigned int format,
  3652. struct snd_pcm_substream *substream)
  3653. {
  3654. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3655. call_pcm_capture_hook(hinfo, codec, substream,
  3656. HDA_GEN_PCM_ACT_PREPARE);
  3657. return 0;
  3658. }
  3659. static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3660. struct hda_codec *codec,
  3661. struct snd_pcm_substream *substream)
  3662. {
  3663. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3664. call_pcm_capture_hook(hinfo, codec, substream,
  3665. HDA_GEN_PCM_ACT_CLEANUP);
  3666. return 0;
  3667. }
  3668. static int capture_pcm_close(struct hda_pcm_stream *hinfo,
  3669. struct hda_codec *codec,
  3670. struct snd_pcm_substream *substream)
  3671. {
  3672. call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
  3673. return 0;
  3674. }
  3675. static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
  3676. struct hda_codec *codec,
  3677. struct snd_pcm_substream *substream)
  3678. {
  3679. struct hda_gen_spec *spec = codec->spec;
  3680. int err = 0;
  3681. mutex_lock(&spec->pcm_mutex);
  3682. if (!spec->indep_hp_enabled)
  3683. err = -EBUSY;
  3684. else
  3685. spec->active_streams |= 1 << STREAM_INDEP_HP;
  3686. call_pcm_playback_hook(hinfo, codec, substream,
  3687. HDA_GEN_PCM_ACT_OPEN);
  3688. mutex_unlock(&spec->pcm_mutex);
  3689. return err;
  3690. }
  3691. static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
  3692. struct hda_codec *codec,
  3693. struct snd_pcm_substream *substream)
  3694. {
  3695. struct hda_gen_spec *spec = codec->spec;
  3696. mutex_lock(&spec->pcm_mutex);
  3697. spec->active_streams &= ~(1 << STREAM_INDEP_HP);
  3698. call_pcm_playback_hook(hinfo, codec, substream,
  3699. HDA_GEN_PCM_ACT_CLOSE);
  3700. mutex_unlock(&spec->pcm_mutex);
  3701. return 0;
  3702. }
  3703. static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3704. struct hda_codec *codec,
  3705. unsigned int stream_tag,
  3706. unsigned int format,
  3707. struct snd_pcm_substream *substream)
  3708. {
  3709. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3710. call_pcm_playback_hook(hinfo, codec, substream,
  3711. HDA_GEN_PCM_ACT_PREPARE);
  3712. return 0;
  3713. }
  3714. static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3715. struct hda_codec *codec,
  3716. struct snd_pcm_substream *substream)
  3717. {
  3718. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3719. call_pcm_playback_hook(hinfo, codec, substream,
  3720. HDA_GEN_PCM_ACT_CLEANUP);
  3721. return 0;
  3722. }
  3723. /*
  3724. * Digital out
  3725. */
  3726. static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  3727. struct hda_codec *codec,
  3728. struct snd_pcm_substream *substream)
  3729. {
  3730. struct hda_gen_spec *spec = codec->spec;
  3731. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  3732. }
  3733. static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3734. struct hda_codec *codec,
  3735. unsigned int stream_tag,
  3736. unsigned int format,
  3737. struct snd_pcm_substream *substream)
  3738. {
  3739. struct hda_gen_spec *spec = codec->spec;
  3740. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  3741. stream_tag, format, substream);
  3742. }
  3743. static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3744. struct hda_codec *codec,
  3745. struct snd_pcm_substream *substream)
  3746. {
  3747. struct hda_gen_spec *spec = codec->spec;
  3748. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  3749. }
  3750. static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  3751. struct hda_codec *codec,
  3752. struct snd_pcm_substream *substream)
  3753. {
  3754. struct hda_gen_spec *spec = codec->spec;
  3755. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  3756. }
  3757. /*
  3758. * Analog capture
  3759. */
  3760. #define alt_capture_pcm_open capture_pcm_open
  3761. #define alt_capture_pcm_close capture_pcm_close
  3762. static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3763. struct hda_codec *codec,
  3764. unsigned int stream_tag,
  3765. unsigned int format,
  3766. struct snd_pcm_substream *substream)
  3767. {
  3768. struct hda_gen_spec *spec = codec->spec;
  3769. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
  3770. stream_tag, 0, format);
  3771. call_pcm_capture_hook(hinfo, codec, substream,
  3772. HDA_GEN_PCM_ACT_PREPARE);
  3773. return 0;
  3774. }
  3775. static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3776. struct hda_codec *codec,
  3777. struct snd_pcm_substream *substream)
  3778. {
  3779. struct hda_gen_spec *spec = codec->spec;
  3780. snd_hda_codec_cleanup_stream(codec,
  3781. spec->adc_nids[substream->number + 1]);
  3782. call_pcm_capture_hook(hinfo, codec, substream,
  3783. HDA_GEN_PCM_ACT_CLEANUP);
  3784. return 0;
  3785. }
  3786. /*
  3787. */
  3788. static const struct hda_pcm_stream pcm_analog_playback = {
  3789. .substreams = 1,
  3790. .channels_min = 2,
  3791. .channels_max = 8,
  3792. /* NID is set in build_pcms */
  3793. .ops = {
  3794. .open = playback_pcm_open,
  3795. .close = playback_pcm_close,
  3796. .prepare = playback_pcm_prepare,
  3797. .cleanup = playback_pcm_cleanup
  3798. },
  3799. };
  3800. static const struct hda_pcm_stream pcm_analog_capture = {
  3801. .substreams = 1,
  3802. .channels_min = 2,
  3803. .channels_max = 2,
  3804. /* NID is set in build_pcms */
  3805. .ops = {
  3806. .open = capture_pcm_open,
  3807. .close = capture_pcm_close,
  3808. .prepare = capture_pcm_prepare,
  3809. .cleanup = capture_pcm_cleanup
  3810. },
  3811. };
  3812. static const struct hda_pcm_stream pcm_analog_alt_playback = {
  3813. .substreams = 1,
  3814. .channels_min = 2,
  3815. .channels_max = 2,
  3816. /* NID is set in build_pcms */
  3817. .ops = {
  3818. .open = alt_playback_pcm_open,
  3819. .close = alt_playback_pcm_close,
  3820. .prepare = alt_playback_pcm_prepare,
  3821. .cleanup = alt_playback_pcm_cleanup
  3822. },
  3823. };
  3824. static const struct hda_pcm_stream pcm_analog_alt_capture = {
  3825. .substreams = 2, /* can be overridden */
  3826. .channels_min = 2,
  3827. .channels_max = 2,
  3828. /* NID is set in build_pcms */
  3829. .ops = {
  3830. .open = alt_capture_pcm_open,
  3831. .close = alt_capture_pcm_close,
  3832. .prepare = alt_capture_pcm_prepare,
  3833. .cleanup = alt_capture_pcm_cleanup
  3834. },
  3835. };
  3836. static const struct hda_pcm_stream pcm_digital_playback = {
  3837. .substreams = 1,
  3838. .channels_min = 2,
  3839. .channels_max = 2,
  3840. /* NID is set in build_pcms */
  3841. .ops = {
  3842. .open = dig_playback_pcm_open,
  3843. .close = dig_playback_pcm_close,
  3844. .prepare = dig_playback_pcm_prepare,
  3845. .cleanup = dig_playback_pcm_cleanup
  3846. },
  3847. };
  3848. static const struct hda_pcm_stream pcm_digital_capture = {
  3849. .substreams = 1,
  3850. .channels_min = 2,
  3851. .channels_max = 2,
  3852. /* NID is set in build_pcms */
  3853. };
  3854. /* Used by build_pcms to flag that a PCM has no playback stream */
  3855. static const struct hda_pcm_stream pcm_null_stream = {
  3856. .substreams = 0,
  3857. .channels_min = 0,
  3858. .channels_max = 0,
  3859. };
  3860. /*
  3861. * dynamic changing ADC PCM streams
  3862. */
  3863. static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
  3864. {
  3865. struct hda_gen_spec *spec = codec->spec;
  3866. hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
  3867. if (spec->cur_adc && spec->cur_adc != new_adc) {
  3868. /* stream is running, let's swap the current ADC */
  3869. __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
  3870. spec->cur_adc = new_adc;
  3871. snd_hda_codec_setup_stream(codec, new_adc,
  3872. spec->cur_adc_stream_tag, 0,
  3873. spec->cur_adc_format);
  3874. return true;
  3875. }
  3876. return false;
  3877. }
  3878. /* analog capture with dynamic dual-adc changes */
  3879. static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3880. struct hda_codec *codec,
  3881. unsigned int stream_tag,
  3882. unsigned int format,
  3883. struct snd_pcm_substream *substream)
  3884. {
  3885. struct hda_gen_spec *spec = codec->spec;
  3886. spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
  3887. spec->cur_adc_stream_tag = stream_tag;
  3888. spec->cur_adc_format = format;
  3889. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  3890. return 0;
  3891. }
  3892. static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3893. struct hda_codec *codec,
  3894. struct snd_pcm_substream *substream)
  3895. {
  3896. struct hda_gen_spec *spec = codec->spec;
  3897. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  3898. spec->cur_adc = 0;
  3899. return 0;
  3900. }
  3901. static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
  3902. .substreams = 1,
  3903. .channels_min = 2,
  3904. .channels_max = 2,
  3905. .nid = 0, /* fill later */
  3906. .ops = {
  3907. .prepare = dyn_adc_capture_pcm_prepare,
  3908. .cleanup = dyn_adc_capture_pcm_cleanup
  3909. },
  3910. };
  3911. static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
  3912. const char *chip_name)
  3913. {
  3914. char *p;
  3915. if (*str)
  3916. return;
  3917. strlcpy(str, chip_name, len);
  3918. /* drop non-alnum chars after a space */
  3919. for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
  3920. if (!isalnum(p[1])) {
  3921. *p = 0;
  3922. break;
  3923. }
  3924. }
  3925. strlcat(str, sfx, len);
  3926. }
  3927. /* build PCM streams based on the parsed results */
  3928. int snd_hda_gen_build_pcms(struct hda_codec *codec)
  3929. {
  3930. struct hda_gen_spec *spec = codec->spec;
  3931. struct hda_pcm *info = spec->pcm_rec;
  3932. const struct hda_pcm_stream *p;
  3933. bool have_multi_adcs;
  3934. codec->num_pcms = 1;
  3935. codec->pcm_info = info;
  3936. if (spec->no_analog)
  3937. goto skip_analog;
  3938. fill_pcm_stream_name(spec->stream_name_analog,
  3939. sizeof(spec->stream_name_analog),
  3940. " Analog", codec->chip_name);
  3941. info->name = spec->stream_name_analog;
  3942. if (spec->multiout.num_dacs > 0) {
  3943. p = spec->stream_analog_playback;
  3944. if (!p)
  3945. p = &pcm_analog_playback;
  3946. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  3947. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
  3948. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  3949. spec->multiout.max_channels;
  3950. if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
  3951. spec->autocfg.line_outs == 2)
  3952. info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
  3953. snd_pcm_2_1_chmaps;
  3954. }
  3955. if (spec->num_adc_nids) {
  3956. p = spec->stream_analog_capture;
  3957. if (!p) {
  3958. if (spec->dyn_adc_switch)
  3959. p = &dyn_adc_pcm_analog_capture;
  3960. else
  3961. p = &pcm_analog_capture;
  3962. }
  3963. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  3964. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
  3965. }
  3966. skip_analog:
  3967. /* SPDIF for stream index #1 */
  3968. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  3969. fill_pcm_stream_name(spec->stream_name_digital,
  3970. sizeof(spec->stream_name_digital),
  3971. " Digital", codec->chip_name);
  3972. codec->num_pcms = 2;
  3973. codec->slave_dig_outs = spec->multiout.slave_dig_outs;
  3974. info = spec->pcm_rec + 1;
  3975. info->name = spec->stream_name_digital;
  3976. if (spec->dig_out_type)
  3977. info->pcm_type = spec->dig_out_type;
  3978. else
  3979. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  3980. if (spec->multiout.dig_out_nid) {
  3981. p = spec->stream_digital_playback;
  3982. if (!p)
  3983. p = &pcm_digital_playback;
  3984. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  3985. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  3986. }
  3987. if (spec->dig_in_nid) {
  3988. p = spec->stream_digital_capture;
  3989. if (!p)
  3990. p = &pcm_digital_capture;
  3991. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  3992. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  3993. }
  3994. }
  3995. if (spec->no_analog)
  3996. return 0;
  3997. /* If the use of more than one ADC is requested for the current
  3998. * model, configure a second analog capture-only PCM.
  3999. */
  4000. have_multi_adcs = (spec->num_adc_nids > 1) &&
  4001. !spec->dyn_adc_switch && !spec->auto_mic;
  4002. /* Additional Analaog capture for index #2 */
  4003. if (spec->alt_dac_nid || have_multi_adcs) {
  4004. codec->num_pcms = 3;
  4005. info = spec->pcm_rec + 2;
  4006. info->name = spec->stream_name_analog;
  4007. if (spec->alt_dac_nid) {
  4008. p = spec->stream_analog_alt_playback;
  4009. if (!p)
  4010. p = &pcm_analog_alt_playback;
  4011. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  4012. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  4013. spec->alt_dac_nid;
  4014. } else {
  4015. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  4016. pcm_null_stream;
  4017. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
  4018. }
  4019. if (have_multi_adcs) {
  4020. p = spec->stream_analog_alt_capture;
  4021. if (!p)
  4022. p = &pcm_analog_alt_capture;
  4023. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  4024. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  4025. spec->adc_nids[1];
  4026. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
  4027. spec->num_adc_nids - 1;
  4028. } else {
  4029. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  4030. pcm_null_stream;
  4031. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
  4032. }
  4033. }
  4034. return 0;
  4035. }
  4036. EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
  4037. /*
  4038. * Standard auto-parser initializations
  4039. */
  4040. /* configure the given path as a proper output */
  4041. static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
  4042. {
  4043. struct nid_path *path;
  4044. hda_nid_t pin;
  4045. path = snd_hda_get_path_from_idx(codec, path_idx);
  4046. if (!path || !path->depth)
  4047. return;
  4048. pin = path->path[path->depth - 1];
  4049. restore_pin_ctl(codec, pin);
  4050. snd_hda_activate_path(codec, path, path->active, true);
  4051. set_pin_eapd(codec, pin, path->active);
  4052. }
  4053. /* initialize primary output paths */
  4054. static void init_multi_out(struct hda_codec *codec)
  4055. {
  4056. struct hda_gen_spec *spec = codec->spec;
  4057. int i;
  4058. for (i = 0; i < spec->autocfg.line_outs; i++)
  4059. set_output_and_unmute(codec, spec->out_paths[i]);
  4060. }
  4061. static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
  4062. {
  4063. int i;
  4064. for (i = 0; i < num_outs; i++)
  4065. set_output_and_unmute(codec, paths[i]);
  4066. }
  4067. /* initialize hp and speaker paths */
  4068. static void init_extra_out(struct hda_codec *codec)
  4069. {
  4070. struct hda_gen_spec *spec = codec->spec;
  4071. if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
  4072. __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
  4073. if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
  4074. __init_extra_out(codec, spec->autocfg.speaker_outs,
  4075. spec->speaker_paths);
  4076. }
  4077. /* initialize multi-io paths */
  4078. static void init_multi_io(struct hda_codec *codec)
  4079. {
  4080. struct hda_gen_spec *spec = codec->spec;
  4081. int i;
  4082. for (i = 0; i < spec->multi_ios; i++) {
  4083. hda_nid_t pin = spec->multi_io[i].pin;
  4084. struct nid_path *path;
  4085. path = get_multiio_path(codec, i);
  4086. if (!path)
  4087. continue;
  4088. if (!spec->multi_io[i].ctl_in)
  4089. spec->multi_io[i].ctl_in =
  4090. snd_hda_codec_get_pin_target(codec, pin);
  4091. snd_hda_activate_path(codec, path, path->active, true);
  4092. }
  4093. }
  4094. /* set up input pins and loopback paths */
  4095. static void init_analog_input(struct hda_codec *codec)
  4096. {
  4097. struct hda_gen_spec *spec = codec->spec;
  4098. struct auto_pin_cfg *cfg = &spec->autocfg;
  4099. int i;
  4100. for (i = 0; i < cfg->num_inputs; i++) {
  4101. hda_nid_t nid = cfg->inputs[i].pin;
  4102. if (is_input_pin(codec, nid))
  4103. restore_pin_ctl(codec, nid);
  4104. /* init loopback inputs */
  4105. if (spec->mixer_nid) {
  4106. struct nid_path *path;
  4107. path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
  4108. if (path)
  4109. snd_hda_activate_path(codec, path,
  4110. path->active, false);
  4111. }
  4112. }
  4113. }
  4114. /* initialize ADC paths */
  4115. static void init_input_src(struct hda_codec *codec)
  4116. {
  4117. struct hda_gen_spec *spec = codec->spec;
  4118. struct hda_input_mux *imux = &spec->input_mux;
  4119. struct nid_path *path;
  4120. int i, c, nums;
  4121. if (spec->dyn_adc_switch)
  4122. nums = 1;
  4123. else
  4124. nums = spec->num_adc_nids;
  4125. for (c = 0; c < nums; c++) {
  4126. for (i = 0; i < imux->num_items; i++) {
  4127. path = get_input_path(codec, c, i);
  4128. if (path) {
  4129. bool active = path->active;
  4130. if (i == spec->cur_mux[c])
  4131. active = true;
  4132. snd_hda_activate_path(codec, path, active, false);
  4133. }
  4134. }
  4135. }
  4136. if (spec->shared_mic_hp)
  4137. update_shared_mic_hp(codec, spec->cur_mux[0]);
  4138. if (spec->cap_sync_hook)
  4139. spec->cap_sync_hook(codec, NULL);
  4140. }
  4141. /* set right pin controls for digital I/O */
  4142. static void init_digital(struct hda_codec *codec)
  4143. {
  4144. struct hda_gen_spec *spec = codec->spec;
  4145. int i;
  4146. hda_nid_t pin;
  4147. for (i = 0; i < spec->autocfg.dig_outs; i++)
  4148. set_output_and_unmute(codec, spec->digout_paths[i]);
  4149. pin = spec->autocfg.dig_in_pin;
  4150. if (pin) {
  4151. struct nid_path *path;
  4152. restore_pin_ctl(codec, pin);
  4153. path = snd_hda_get_path_from_idx(codec, spec->digin_path);
  4154. if (path)
  4155. snd_hda_activate_path(codec, path, path->active, false);
  4156. }
  4157. }
  4158. /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
  4159. * invalid unsol tags by some reason
  4160. */
  4161. static void clear_unsol_on_unused_pins(struct hda_codec *codec)
  4162. {
  4163. int i;
  4164. for (i = 0; i < codec->init_pins.used; i++) {
  4165. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  4166. hda_nid_t nid = pin->nid;
  4167. if (is_jack_detectable(codec, nid) &&
  4168. !snd_hda_jack_tbl_get(codec, nid))
  4169. snd_hda_codec_update_cache(codec, nid, 0,
  4170. AC_VERB_SET_UNSOLICITED_ENABLE, 0);
  4171. }
  4172. }
  4173. /*
  4174. * initialize the generic spec;
  4175. * this can be put as patch_ops.init function
  4176. */
  4177. int snd_hda_gen_init(struct hda_codec *codec)
  4178. {
  4179. struct hda_gen_spec *spec = codec->spec;
  4180. if (spec->init_hook)
  4181. spec->init_hook(codec);
  4182. snd_hda_apply_verbs(codec);
  4183. codec->cached_write = 1;
  4184. init_multi_out(codec);
  4185. init_extra_out(codec);
  4186. init_multi_io(codec);
  4187. init_analog_input(codec);
  4188. init_input_src(codec);
  4189. init_digital(codec);
  4190. clear_unsol_on_unused_pins(codec);
  4191. /* call init functions of standard auto-mute helpers */
  4192. update_automute_all(codec);
  4193. snd_hda_codec_flush_amp_cache(codec);
  4194. snd_hda_codec_flush_cmd_cache(codec);
  4195. if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
  4196. snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
  4197. hda_call_check_power_status(codec, 0x01);
  4198. return 0;
  4199. }
  4200. EXPORT_SYMBOL_HDA(snd_hda_gen_init);
  4201. /*
  4202. * free the generic spec;
  4203. * this can be put as patch_ops.free function
  4204. */
  4205. void snd_hda_gen_free(struct hda_codec *codec)
  4206. {
  4207. snd_hda_gen_spec_free(codec->spec);
  4208. kfree(codec->spec);
  4209. codec->spec = NULL;
  4210. }
  4211. EXPORT_SYMBOL_HDA(snd_hda_gen_free);
  4212. #ifdef CONFIG_PM
  4213. /*
  4214. * check the loopback power save state;
  4215. * this can be put as patch_ops.check_power_status function
  4216. */
  4217. int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  4218. {
  4219. struct hda_gen_spec *spec = codec->spec;
  4220. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  4221. }
  4222. EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
  4223. #endif
  4224. /*
  4225. * the generic codec support
  4226. */
  4227. static const struct hda_codec_ops generic_patch_ops = {
  4228. .build_controls = snd_hda_gen_build_controls,
  4229. .build_pcms = snd_hda_gen_build_pcms,
  4230. .init = snd_hda_gen_init,
  4231. .free = snd_hda_gen_free,
  4232. .unsol_event = snd_hda_jack_unsol_event,
  4233. #ifdef CONFIG_PM
  4234. .check_power_status = snd_hda_gen_check_power_status,
  4235. #endif
  4236. };
  4237. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  4238. {
  4239. struct hda_gen_spec *spec;
  4240. int err;
  4241. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  4242. if (!spec)
  4243. return -ENOMEM;
  4244. snd_hda_gen_spec_init(spec);
  4245. codec->spec = spec;
  4246. err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
  4247. if (err < 0)
  4248. return err;
  4249. err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
  4250. if (err < 0)
  4251. goto error;
  4252. codec->patch_ops = generic_patch_ops;
  4253. return 0;
  4254. error:
  4255. snd_hda_gen_free(codec);
  4256. return err;
  4257. }
  4258. EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);