hda_generic.c 94 KB

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