hda_generic.c 140 KB

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