hda_generic.c 116 KB

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