hda_generic.c 126 KB

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