hda_generic.c 147 KB

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