hda_generic.c 128 KB

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