hda_generic.c 96 KB

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