hda_generic.c 107 KB

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