hda_generic.c 101 KB

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