hda_generic.c 135 KB

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