hda_generic.c 128 KB

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