hda_generic.c 129 KB

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