hda_generic.c 115 KB

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