hda_generic.c 106 KB

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