hda_generic.c 125 KB

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