hda_generic.c 159 KB

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