hda_generic.c 100 KB

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