hda_generic.c 137 KB

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