hda_generic.c 126 KB

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