hda_generic.c 159 KB

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