hda_generic.c 126 KB

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