hda_generic.c 130 KB

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