hda_generic.c 159 KB

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