hda_generic.c 108 KB

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