hda_generic.c 144 KB

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