hda_generic.c 124 KB

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