hda_generic.c 106 KB

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