hda_generic.c 139 KB

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