hda_generic.c 101 KB

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