hda_generic.c 157 KB

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