hda_generic.c 97 KB

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