hda_generic.c 158 KB

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