hda_generic.c 143 KB

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