hda_generic.c 115 KB

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