hda_generic.c 139 KB

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