hda_generic.c 95 KB

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