hda_generic.c 163 KB

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