hda_generic.c 95 KB

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