hda_generic.c 132 KB

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