hda_generic.c 100 KB

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