hda_generic.c 164 KB

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