hda_generic.c 122 KB

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