hda_generic.c 137 KB

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