hda_generic.c 100 KB

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