hda_generic.c 139 KB

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