hda_generic.c 135 KB

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