hda_generic.c 107 KB

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