hda_generic.c 114 KB

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