hda_generic.c 140 KB

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