hda_generic.c 144 KB

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