hda_generic.c 145 KB

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