hda_generic.c 119 KB

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