hda_generic.c 135 KB

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