hda_generic.c 142 KB

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