codec.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Universal Interface for Intel High Definition Audio Codec
  4. *
  5. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  6. */
  7. #include <linux/init.h>
  8. #include <linux/delay.h>
  9. #include <linux/slab.h>
  10. #include <linux/minmax.h>
  11. #include <linux/mutex.h>
  12. #include <linux/module.h>
  13. #include <linux/pm.h>
  14. #include <linux/pm_runtime.h>
  15. #include <sound/core.h>
  16. #include <sound/hda_codec.h>
  17. #include <sound/asoundef.h>
  18. #include <sound/tlv.h>
  19. #include <sound/initval.h>
  20. #include <sound/jack.h>
  21. #include "hda_local.h"
  22. #include "hda_beep.h"
  23. #include "hda_jack.h"
  24. #include <sound/hda_hwdep.h>
  25. #include <sound/hda_component.h>
  26. #define codec_in_pm(codec) snd_hdac_is_in_pm(&codec->core)
  27. #define hda_codec_is_power_on(codec) snd_hdac_is_power_on(&codec->core)
  28. #define codec_has_epss(codec) \
  29. ((codec)->core.power_caps & AC_PWRST_EPSS)
  30. #define codec_has_clkstop(codec) \
  31. ((codec)->core.power_caps & AC_PWRST_CLKSTOP)
  32. static int call_exec_verb(struct hda_bus *bus, struct hda_codec *codec,
  33. unsigned int cmd, unsigned int flags,
  34. unsigned int *res)
  35. {
  36. int err;
  37. CLASS(snd_hda_power_pm, pm)(codec);
  38. guard(mutex)(&bus->core.cmd_mutex);
  39. if (flags & HDA_RW_NO_RESPONSE_FALLBACK)
  40. bus->no_response_fallback = 1;
  41. err = snd_hdac_bus_exec_verb_unlocked(&bus->core, codec->core.addr,
  42. cmd, res);
  43. bus->no_response_fallback = 0;
  44. return err;
  45. }
  46. /*
  47. * Send and receive a verb - passed to exec_verb override for hdac_device
  48. */
  49. static int codec_exec_verb(struct hdac_device *dev, unsigned int cmd,
  50. unsigned int flags, unsigned int *res)
  51. {
  52. struct hda_codec *codec = container_of(dev, struct hda_codec, core);
  53. struct hda_bus *bus = codec->bus;
  54. int err;
  55. if (cmd == ~0)
  56. return -1;
  57. again:
  58. err = call_exec_verb(bus, codec, cmd, flags, res);
  59. if (!codec_in_pm(codec) && res && err == -EAGAIN) {
  60. if (bus->response_reset) {
  61. codec_dbg(codec,
  62. "resetting BUS due to fatal communication error\n");
  63. snd_hda_bus_reset(bus);
  64. }
  65. goto again;
  66. }
  67. /* clear reset-flag when the communication gets recovered */
  68. if (!err || codec_in_pm(codec))
  69. bus->response_reset = 0;
  70. return err;
  71. }
  72. /**
  73. * snd_hda_sequence_write - sequence writes
  74. * @codec: the HDA codec
  75. * @seq: VERB array to send
  76. *
  77. * Send the commands sequentially from the given array.
  78. * The array must be terminated with NID=0.
  79. */
  80. void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
  81. {
  82. for (; seq->nid; seq++)
  83. snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
  84. }
  85. EXPORT_SYMBOL_GPL(snd_hda_sequence_write);
  86. /* connection list element */
  87. struct hda_conn_list {
  88. struct list_head list;
  89. int len;
  90. hda_nid_t nid;
  91. hda_nid_t conns[] __counted_by(len);
  92. };
  93. /* look up the cached results */
  94. static struct hda_conn_list *
  95. lookup_conn_list(struct hda_codec *codec, hda_nid_t nid)
  96. {
  97. struct hda_conn_list *p;
  98. list_for_each_entry(p, &codec->conn_list, list) {
  99. if (p->nid == nid)
  100. return p;
  101. }
  102. return NULL;
  103. }
  104. static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  105. const hda_nid_t *list)
  106. {
  107. struct hda_conn_list *p;
  108. p = kmalloc_flex(*p, conns, len);
  109. if (!p)
  110. return -ENOMEM;
  111. p->len = len;
  112. p->nid = nid;
  113. memcpy(p->conns, list, len * sizeof(hda_nid_t));
  114. list_add(&p->list, &codec->conn_list);
  115. return 0;
  116. }
  117. static void remove_conn_list(struct hda_codec *codec)
  118. {
  119. while (!list_empty(&codec->conn_list)) {
  120. struct hda_conn_list *p;
  121. p = list_first_entry(&codec->conn_list, typeof(*p), list);
  122. list_del(&p->list);
  123. kfree(p);
  124. }
  125. }
  126. /* read the connection and add to the cache */
  127. static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid)
  128. {
  129. hda_nid_t list[32];
  130. hda_nid_t *result = list;
  131. int len;
  132. len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list));
  133. if (len == -ENOSPC) {
  134. len = snd_hda_get_num_raw_conns(codec, nid);
  135. result = kmalloc_objs(hda_nid_t, len);
  136. if (!result)
  137. return -ENOMEM;
  138. len = snd_hda_get_raw_connections(codec, nid, result, len);
  139. }
  140. if (len >= 0)
  141. len = snd_hda_override_conn_list(codec, nid, len, result);
  142. if (result != list)
  143. kfree(result);
  144. return len;
  145. }
  146. /**
  147. * snd_hda_get_conn_list - get connection list
  148. * @codec: the HDA codec
  149. * @nid: NID to parse
  150. * @listp: the pointer to store NID list
  151. *
  152. * Parses the connection list of the given widget and stores the pointer
  153. * to the list of NIDs.
  154. *
  155. * Returns the number of connections, or a negative error code.
  156. *
  157. * Note that the returned pointer isn't protected against the list
  158. * modification. If snd_hda_override_conn_list() might be called
  159. * concurrently, protect with a mutex appropriately.
  160. */
  161. int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
  162. const hda_nid_t **listp)
  163. {
  164. bool added = false;
  165. for (;;) {
  166. int err;
  167. const struct hda_conn_list *p;
  168. /* if the connection-list is already cached, read it */
  169. p = lookup_conn_list(codec, nid);
  170. if (p) {
  171. if (listp)
  172. *listp = p->conns;
  173. return p->len;
  174. }
  175. if (snd_BUG_ON(added))
  176. return -EINVAL;
  177. err = read_and_add_raw_conns(codec, nid);
  178. if (err < 0)
  179. return err;
  180. added = true;
  181. }
  182. }
  183. EXPORT_SYMBOL_GPL(snd_hda_get_conn_list);
  184. /**
  185. * snd_hda_get_connections - copy connection list
  186. * @codec: the HDA codec
  187. * @nid: NID to parse
  188. * @conn_list: connection list array; when NULL, checks only the size
  189. * @max_conns: max. number of connections to store
  190. *
  191. * Parses the connection list of the given widget and stores the list
  192. * of NIDs.
  193. *
  194. * Returns the number of connections, or a negative error code.
  195. */
  196. int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
  197. hda_nid_t *conn_list, int max_conns)
  198. {
  199. const hda_nid_t *list;
  200. int len = snd_hda_get_conn_list(codec, nid, &list);
  201. if (len > 0 && conn_list) {
  202. if (len > max_conns) {
  203. codec_err(codec, "Too many connections %d for NID 0x%x\n",
  204. len, nid);
  205. return -EINVAL;
  206. }
  207. memcpy(conn_list, list, len * sizeof(hda_nid_t));
  208. }
  209. return len;
  210. }
  211. EXPORT_SYMBOL_GPL(snd_hda_get_connections);
  212. /**
  213. * snd_hda_override_conn_list - add/modify the connection-list to cache
  214. * @codec: the HDA codec
  215. * @nid: NID to parse
  216. * @len: number of connection list entries
  217. * @list: the list of connection entries
  218. *
  219. * Add or modify the given connection-list to the cache. If the corresponding
  220. * cache already exists, invalidate it and append a new one.
  221. *
  222. * Returns zero or a negative error code.
  223. */
  224. int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
  225. const hda_nid_t *list)
  226. {
  227. struct hda_conn_list *p;
  228. p = lookup_conn_list(codec, nid);
  229. if (p) {
  230. list_del(&p->list);
  231. kfree(p);
  232. }
  233. return add_conn_list(codec, nid, len, list);
  234. }
  235. EXPORT_SYMBOL_GPL(snd_hda_override_conn_list);
  236. /**
  237. * snd_hda_get_conn_index - get the connection index of the given NID
  238. * @codec: the HDA codec
  239. * @mux: NID containing the list
  240. * @nid: NID to select
  241. * @recursive: 1 when searching NID recursively, otherwise 0
  242. *
  243. * Parses the connection list of the widget @mux and checks whether the
  244. * widget @nid is present. If it is, return the connection index.
  245. * Otherwise it returns -1.
  246. */
  247. int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
  248. hda_nid_t nid, int recursive)
  249. {
  250. const hda_nid_t *conn;
  251. int i, nums;
  252. nums = snd_hda_get_conn_list(codec, mux, &conn);
  253. for (i = 0; i < nums; i++)
  254. if (conn[i] == nid)
  255. return i;
  256. if (!recursive)
  257. return -1;
  258. if (recursive > 10) {
  259. codec_dbg(codec, "too deep connection for 0x%x\n", nid);
  260. return -1;
  261. }
  262. recursive++;
  263. for (i = 0; i < nums; i++) {
  264. unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
  265. if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
  266. continue;
  267. if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
  268. return i;
  269. }
  270. return -1;
  271. }
  272. EXPORT_SYMBOL_GPL(snd_hda_get_conn_index);
  273. /**
  274. * snd_hda_get_num_devices - get DEVLIST_LEN parameter of the given widget
  275. * @codec: the HDA codec
  276. * @nid: NID of the pin to parse
  277. *
  278. * Get the device entry number on the given widget. This is a feature of
  279. * DP MST audio. Each pin can have several device entries in it.
  280. */
  281. unsigned int snd_hda_get_num_devices(struct hda_codec *codec, hda_nid_t nid)
  282. {
  283. unsigned int wcaps = get_wcaps(codec, nid);
  284. int parm;
  285. if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) ||
  286. get_wcaps_type(wcaps) != AC_WID_PIN)
  287. return 0;
  288. parm = snd_hdac_read_parm_uncached(&codec->core, nid, AC_PAR_DEVLIST_LEN);
  289. if (parm == -1)
  290. parm = 0;
  291. return parm & AC_DEV_LIST_LEN_MASK;
  292. }
  293. EXPORT_SYMBOL_GPL(snd_hda_get_num_devices);
  294. /**
  295. * snd_hda_get_devices - copy device list without cache
  296. * @codec: the HDA codec
  297. * @nid: NID of the pin to parse
  298. * @dev_list: device list array
  299. * @max_devices: max. number of devices to store
  300. *
  301. * Copy the device list. This info is dynamic and so not cached.
  302. * Currently called only from hda_proc.c, so not exported.
  303. */
  304. unsigned int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid,
  305. u8 *dev_list, unsigned int max_devices)
  306. {
  307. unsigned int parm, i, dev_len, devices;
  308. parm = snd_hda_get_num_devices(codec, nid);
  309. if (!parm) /* not multi-stream capable */
  310. return 0;
  311. dev_len = min(parm + 1, max_devices);
  312. devices = 0;
  313. while (devices < dev_len) {
  314. if (snd_hdac_read(&codec->core, nid,
  315. AC_VERB_GET_DEVICE_LIST, devices, &parm))
  316. break; /* error */
  317. for (i = 0; i < 8; i++) {
  318. dev_list[devices] = (u8)parm;
  319. parm >>= 4;
  320. devices++;
  321. if (devices >= dev_len)
  322. break;
  323. }
  324. }
  325. return devices;
  326. }
  327. /**
  328. * snd_hda_get_dev_select - get device entry select on the pin
  329. * @codec: the HDA codec
  330. * @nid: NID of the pin to get device entry select
  331. *
  332. * Get the devcie entry select on the pin. Return the device entry
  333. * id selected on the pin. Return 0 means the first device entry
  334. * is selected or MST is not supported.
  335. */
  336. int snd_hda_get_dev_select(struct hda_codec *codec, hda_nid_t nid)
  337. {
  338. /* not support dp_mst will always return 0, using first dev_entry */
  339. if (!codec->dp_mst)
  340. return 0;
  341. return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DEVICE_SEL, 0);
  342. }
  343. EXPORT_SYMBOL_GPL(snd_hda_get_dev_select);
  344. /**
  345. * snd_hda_set_dev_select - set device entry select on the pin
  346. * @codec: the HDA codec
  347. * @nid: NID of the pin to set device entry select
  348. * @dev_id: device entry id to be set
  349. *
  350. * Set the device entry select on the pin nid.
  351. */
  352. int snd_hda_set_dev_select(struct hda_codec *codec, hda_nid_t nid, int dev_id)
  353. {
  354. int ret, num_devices;
  355. /* not support dp_mst will always return 0, using first dev_entry */
  356. if (!codec->dp_mst)
  357. return 0;
  358. /* AC_PAR_DEVLIST_LEN is 0 based. */
  359. num_devices = snd_hda_get_num_devices(codec, nid) + 1;
  360. /* If Device List Length is 0 (num_device = 1),
  361. * the pin is not multi stream capable.
  362. * Do nothing in this case.
  363. */
  364. if (num_devices == 1)
  365. return 0;
  366. /* Behavior of setting index being equal to or greater than
  367. * Device List Length is not predictable
  368. */
  369. if (num_devices <= dev_id)
  370. return -EINVAL;
  371. ret = snd_hda_codec_write(codec, nid, 0,
  372. AC_VERB_SET_DEVICE_SEL, dev_id);
  373. return ret;
  374. }
  375. EXPORT_SYMBOL_GPL(snd_hda_set_dev_select);
  376. /*
  377. * read widget caps for each widget and store in cache
  378. */
  379. static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
  380. {
  381. int i;
  382. hda_nid_t nid;
  383. codec->wcaps = kmalloc_array(codec->core.num_nodes, 4, GFP_KERNEL);
  384. if (!codec->wcaps)
  385. return -ENOMEM;
  386. nid = codec->core.start_nid;
  387. for (i = 0; i < codec->core.num_nodes; i++, nid++)
  388. codec->wcaps[i] = snd_hdac_read_parm_uncached(&codec->core,
  389. nid, AC_PAR_AUDIO_WIDGET_CAP);
  390. return 0;
  391. }
  392. /* read all pin default configurations and save codec->init_pins */
  393. static int read_pin_defaults(struct hda_codec *codec)
  394. {
  395. hda_nid_t nid;
  396. for_each_hda_codec_node(nid, codec) {
  397. struct hda_pincfg *pin;
  398. unsigned int wcaps = get_wcaps(codec, nid);
  399. unsigned int wid_type = get_wcaps_type(wcaps);
  400. if (wid_type != AC_WID_PIN)
  401. continue;
  402. pin = snd_array_new(&codec->init_pins);
  403. if (!pin)
  404. return -ENOMEM;
  405. pin->nid = nid;
  406. pin->cfg = snd_hda_codec_read(codec, nid, 0,
  407. AC_VERB_GET_CONFIG_DEFAULT, 0);
  408. /*
  409. * all device entries are the same widget control so far
  410. * fixme: if any codec is different, need fix here
  411. */
  412. pin->ctrl = snd_hda_codec_read(codec, nid, 0,
  413. AC_VERB_GET_PIN_WIDGET_CONTROL,
  414. 0);
  415. }
  416. return 0;
  417. }
  418. /* look up the given pin config list and return the item matching with NID */
  419. static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
  420. struct snd_array *array,
  421. hda_nid_t nid)
  422. {
  423. struct hda_pincfg *pin;
  424. int i;
  425. snd_array_for_each(array, i, pin) {
  426. if (pin->nid == nid)
  427. return pin;
  428. }
  429. return NULL;
  430. }
  431. /* set the current pin config value for the given NID.
  432. * the value is cached, and read via snd_hda_codec_get_pincfg()
  433. */
  434. int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
  435. hda_nid_t nid, unsigned int cfg)
  436. {
  437. struct hda_pincfg *pin;
  438. pin = look_up_pincfg(codec, list, nid);
  439. if (!pin) {
  440. pin = snd_array_new(list);
  441. if (!pin)
  442. return -ENOMEM;
  443. pin->nid = nid;
  444. }
  445. pin->cfg = cfg;
  446. return 0;
  447. }
  448. /**
  449. * snd_hda_codec_set_pincfg - Override a pin default configuration
  450. * @codec: the HDA codec
  451. * @nid: NID to set the pin config
  452. * @cfg: the pin default config value
  453. *
  454. * Override a pin default configuration value in the cache.
  455. * This value can be read by snd_hda_codec_get_pincfg() in a higher
  456. * priority than the real hardware value.
  457. */
  458. int snd_hda_codec_set_pincfg(struct hda_codec *codec,
  459. hda_nid_t nid, unsigned int cfg)
  460. {
  461. return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
  462. }
  463. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg);
  464. /**
  465. * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
  466. * @codec: the HDA codec
  467. * @nid: NID to get the pin config
  468. *
  469. * Get the current pin config value of the given pin NID.
  470. * If the pincfg value is cached or overridden via sysfs or driver,
  471. * returns the cached value.
  472. */
  473. unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
  474. {
  475. struct hda_pincfg *pin;
  476. #ifdef CONFIG_SND_HDA_RECONFIG
  477. {
  478. unsigned int cfg = 0;
  479. scoped_guard(mutex, &codec->user_mutex) {
  480. pin = look_up_pincfg(codec, &codec->user_pins, nid);
  481. if (pin)
  482. cfg = pin->cfg;
  483. }
  484. if (cfg)
  485. return cfg;
  486. }
  487. #endif
  488. pin = look_up_pincfg(codec, &codec->driver_pins, nid);
  489. if (pin)
  490. return pin->cfg;
  491. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  492. if (pin)
  493. return pin->cfg;
  494. return 0;
  495. }
  496. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg);
  497. /**
  498. * snd_hda_codec_set_pin_target - remember the current pinctl target value
  499. * @codec: the HDA codec
  500. * @nid: pin NID
  501. * @val: assigned pinctl value
  502. *
  503. * This function stores the given value to a pinctl target value in the
  504. * pincfg table. This isn't always as same as the actually written value
  505. * but can be referred at any time via snd_hda_codec_get_pin_target().
  506. */
  507. int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid,
  508. unsigned int val)
  509. {
  510. struct hda_pincfg *pin;
  511. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  512. if (!pin)
  513. return -EINVAL;
  514. pin->target = val;
  515. return 0;
  516. }
  517. EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target);
  518. /**
  519. * snd_hda_codec_get_pin_target - return the current pinctl target value
  520. * @codec: the HDA codec
  521. * @nid: pin NID
  522. */
  523. int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid)
  524. {
  525. struct hda_pincfg *pin;
  526. pin = look_up_pincfg(codec, &codec->init_pins, nid);
  527. if (!pin)
  528. return 0;
  529. return pin->target;
  530. }
  531. EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target);
  532. /**
  533. * snd_hda_shutup_pins - Shut up all pins
  534. * @codec: the HDA codec
  535. *
  536. * Clear all pin controls to shup up before suspend for avoiding click noise.
  537. * The controls aren't cached so that they can be resumed properly.
  538. */
  539. void snd_hda_shutup_pins(struct hda_codec *codec)
  540. {
  541. const struct hda_pincfg *pin;
  542. int i;
  543. /* don't shut up pins when unloading the driver; otherwise it breaks
  544. * the default pin setup at the next load of the driver
  545. */
  546. if (codec->bus->shutdown)
  547. return;
  548. snd_array_for_each(&codec->init_pins, i, pin) {
  549. /* use read here for syncing after issuing each verb */
  550. snd_hda_codec_read(codec, pin->nid, 0,
  551. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  552. }
  553. codec->pins_shutup = 1;
  554. }
  555. EXPORT_SYMBOL_GPL(snd_hda_shutup_pins);
  556. /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
  557. static void restore_shutup_pins(struct hda_codec *codec)
  558. {
  559. const struct hda_pincfg *pin;
  560. int i;
  561. if (!codec->pins_shutup)
  562. return;
  563. if (codec->bus->shutdown)
  564. return;
  565. snd_array_for_each(&codec->init_pins, i, pin) {
  566. snd_hda_codec_write(codec, pin->nid, 0,
  567. AC_VERB_SET_PIN_WIDGET_CONTROL,
  568. pin->ctrl);
  569. }
  570. codec->pins_shutup = 0;
  571. }
  572. static void hda_jackpoll_work(struct work_struct *work)
  573. {
  574. struct hda_codec *codec =
  575. container_of(work, struct hda_codec, jackpoll_work.work);
  576. if (!codec->jackpoll_interval)
  577. return;
  578. /* the power-up/down sequence triggers the runtime resume */
  579. CLASS(snd_hda_power, pm)(codec);
  580. /* update jacks manually if polling is required, too */
  581. snd_hda_jack_set_dirty_all(codec);
  582. snd_hda_jack_poll_all(codec);
  583. schedule_delayed_work(&codec->jackpoll_work, codec->jackpoll_interval);
  584. }
  585. /* release all pincfg lists */
  586. static void free_init_pincfgs(struct hda_codec *codec)
  587. {
  588. snd_array_free(&codec->driver_pins);
  589. #ifdef CONFIG_SND_HDA_RECONFIG
  590. snd_array_free(&codec->user_pins);
  591. #endif
  592. snd_array_free(&codec->init_pins);
  593. }
  594. /*
  595. * audio-converter setup caches
  596. */
  597. struct hda_cvt_setup {
  598. hda_nid_t nid;
  599. u8 stream_tag;
  600. u8 channel_id;
  601. u16 format_id;
  602. unsigned char active; /* cvt is currently used */
  603. unsigned char dirty; /* setups should be cleared */
  604. };
  605. /* get or create a cache entry for the given audio converter NID */
  606. static struct hda_cvt_setup *
  607. get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
  608. {
  609. struct hda_cvt_setup *p;
  610. int i;
  611. snd_array_for_each(&codec->cvt_setups, i, p) {
  612. if (p->nid == nid)
  613. return p;
  614. }
  615. p = snd_array_new(&codec->cvt_setups);
  616. if (p)
  617. p->nid = nid;
  618. return p;
  619. }
  620. /*
  621. * PCM device
  622. */
  623. void snd_hda_codec_pcm_put(struct hda_pcm *pcm)
  624. {
  625. if (refcount_dec_and_test(&pcm->codec->pcm_ref))
  626. wake_up(&pcm->codec->remove_sleep);
  627. }
  628. EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_put);
  629. struct hda_pcm *snd_hda_codec_pcm_new(struct hda_codec *codec,
  630. const char *fmt, ...)
  631. {
  632. struct hda_pcm *pcm;
  633. va_list args;
  634. pcm = kzalloc_obj(*pcm);
  635. if (!pcm)
  636. return NULL;
  637. pcm->codec = codec;
  638. va_start(args, fmt);
  639. pcm->name = kvasprintf(GFP_KERNEL, fmt, args);
  640. va_end(args);
  641. if (!pcm->name) {
  642. kfree(pcm);
  643. return NULL;
  644. }
  645. list_add_tail(&pcm->list, &codec->pcm_list_head);
  646. refcount_inc(&codec->pcm_ref);
  647. return pcm;
  648. }
  649. EXPORT_SYMBOL_GPL(snd_hda_codec_pcm_new);
  650. /*
  651. * codec destructor
  652. */
  653. void snd_hda_codec_disconnect_pcms(struct hda_codec *codec)
  654. {
  655. struct hda_pcm *pcm;
  656. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  657. if (pcm->disconnected)
  658. continue;
  659. if (pcm->pcm)
  660. snd_device_disconnect(codec->card, pcm->pcm);
  661. snd_hda_codec_pcm_put(pcm);
  662. pcm->disconnected = 1;
  663. }
  664. }
  665. static void codec_release_pcms(struct hda_codec *codec)
  666. {
  667. struct hda_pcm *pcm, *n;
  668. list_for_each_entry_safe(pcm, n, &codec->pcm_list_head, list) {
  669. list_del(&pcm->list);
  670. if (pcm->pcm)
  671. snd_device_free(pcm->codec->card, pcm->pcm);
  672. clear_bit(pcm->device, pcm->codec->bus->pcm_dev_bits);
  673. kfree(pcm->name);
  674. kfree(pcm);
  675. }
  676. }
  677. /**
  678. * snd_hda_codec_cleanup_for_unbind - Prepare codec for removal
  679. * @codec: codec device to cleanup
  680. */
  681. void snd_hda_codec_cleanup_for_unbind(struct hda_codec *codec)
  682. {
  683. if (codec->core.registered) {
  684. /* pm_runtime_put() is called in snd_hdac_device_exit() */
  685. pm_runtime_get_noresume(hda_codec_dev(codec));
  686. pm_runtime_disable(hda_codec_dev(codec));
  687. codec->core.registered = 0;
  688. }
  689. snd_hda_codec_disconnect_pcms(codec);
  690. cancel_delayed_work_sync(&codec->jackpoll_work);
  691. if (!codec->in_freeing)
  692. snd_hda_ctls_clear(codec);
  693. codec_release_pcms(codec);
  694. snd_hda_detach_beep_device(codec);
  695. snd_hda_jack_tbl_clear(codec);
  696. codec->proc_widget_hook = NULL;
  697. codec->spec = NULL;
  698. /* free only driver_pins so that init_pins + user_pins are restored */
  699. snd_array_free(&codec->driver_pins);
  700. snd_array_free(&codec->cvt_setups);
  701. snd_array_free(&codec->spdif_out);
  702. snd_array_free(&codec->verbs);
  703. codec->follower_dig_outs = NULL;
  704. codec->spdif_status_reset = 0;
  705. snd_array_free(&codec->mixers);
  706. snd_array_free(&codec->nids);
  707. remove_conn_list(codec);
  708. snd_hdac_regmap_exit(&codec->core);
  709. codec->configured = 0;
  710. refcount_set(&codec->pcm_ref, 1); /* reset refcount */
  711. }
  712. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup_for_unbind);
  713. static unsigned int hda_set_power_state(struct hda_codec *codec,
  714. unsigned int power_state);
  715. /* enable/disable display power per codec */
  716. void snd_hda_codec_display_power(struct hda_codec *codec, bool enable)
  717. {
  718. if (codec->display_power_control)
  719. snd_hdac_display_power(&codec->bus->core, codec->addr, enable);
  720. }
  721. /**
  722. * snd_hda_codec_register - Finalize codec initialization
  723. * @codec: codec device to register
  724. *
  725. * Also called from hda_bind.c
  726. */
  727. void snd_hda_codec_register(struct hda_codec *codec)
  728. {
  729. if (codec->core.registered)
  730. return;
  731. if (device_is_registered(hda_codec_dev(codec))) {
  732. snd_hda_codec_display_power(codec, true);
  733. pm_runtime_enable(hda_codec_dev(codec));
  734. /* it was powered up in snd_hda_codec_new(), now all done */
  735. snd_hda_power_down(codec);
  736. codec->core.registered = 1;
  737. }
  738. }
  739. EXPORT_SYMBOL_GPL(snd_hda_codec_register);
  740. static int snd_hda_codec_dev_register(struct snd_device *device)
  741. {
  742. snd_hda_codec_register(device->device_data);
  743. return 0;
  744. }
  745. /**
  746. * snd_hda_codec_unregister - Unregister specified codec device
  747. * @codec: codec device to unregister
  748. */
  749. void snd_hda_codec_unregister(struct hda_codec *codec)
  750. {
  751. codec->in_freeing = 1;
  752. /*
  753. * snd_hda_codec_device_new() is used by legacy HDA and ASoC driver.
  754. * We can't unregister ASoC device since it will be unregistered in
  755. * snd_hdac_ext_bus_device_remove().
  756. */
  757. if (codec->core.type == HDA_DEV_LEGACY)
  758. snd_hdac_device_unregister(&codec->core);
  759. snd_hda_codec_display_power(codec, false);
  760. /*
  761. * In the case of ASoC HD-audio bus, the device refcount is released in
  762. * snd_hdac_ext_bus_device_remove() explicitly.
  763. */
  764. if (codec->core.type == HDA_DEV_LEGACY)
  765. put_device(hda_codec_dev(codec));
  766. }
  767. EXPORT_SYMBOL_GPL(snd_hda_codec_unregister);
  768. static int snd_hda_codec_dev_free(struct snd_device *device)
  769. {
  770. snd_hda_codec_unregister(device->device_data);
  771. return 0;
  772. }
  773. static void snd_hda_codec_dev_release(struct device *dev)
  774. {
  775. struct hda_codec *codec = dev_to_hda_codec(dev);
  776. free_init_pincfgs(codec);
  777. snd_hdac_device_exit(&codec->core);
  778. snd_hda_sysfs_clear(codec);
  779. kfree(codec->modelname);
  780. kfree(codec->wcaps);
  781. kfree(codec);
  782. }
  783. #define DEV_NAME_LEN 31
  784. /**
  785. * snd_hda_codec_device_init - allocate HDA codec device
  786. * @bus: codec's parent bus
  787. * @codec_addr: the codec address on the parent bus
  788. * @fmt: format string for the device's name
  789. *
  790. * Returns newly allocated codec device or ERR_PTR() on failure.
  791. */
  792. struct hda_codec *
  793. snd_hda_codec_device_init(struct hda_bus *bus, unsigned int codec_addr,
  794. const char *fmt, ...)
  795. {
  796. va_list vargs;
  797. char name[DEV_NAME_LEN];
  798. struct hda_codec *codec;
  799. int err;
  800. if (snd_BUG_ON(!bus))
  801. return ERR_PTR(-EINVAL);
  802. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  803. return ERR_PTR(-EINVAL);
  804. codec = kzalloc_obj(*codec);
  805. if (!codec)
  806. return ERR_PTR(-ENOMEM);
  807. va_start(vargs, fmt);
  808. vsprintf(name, fmt, vargs);
  809. va_end(vargs);
  810. err = snd_hdac_device_init(&codec->core, &bus->core, name, codec_addr);
  811. if (err < 0) {
  812. kfree(codec);
  813. return ERR_PTR(err);
  814. }
  815. codec->bus = bus;
  816. codec->depop_delay = -1;
  817. codec->fixup_id = HDA_FIXUP_ID_NOT_SET;
  818. codec->core.dev.release = snd_hda_codec_dev_release;
  819. codec->core.type = HDA_DEV_LEGACY;
  820. mutex_init(&codec->spdif_mutex);
  821. mutex_init(&codec->control_mutex);
  822. snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
  823. snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
  824. snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
  825. snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
  826. snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
  827. snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
  828. snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16);
  829. snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8);
  830. INIT_LIST_HEAD(&codec->conn_list);
  831. INIT_LIST_HEAD(&codec->pcm_list_head);
  832. INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work);
  833. refcount_set(&codec->pcm_ref, 1);
  834. init_waitqueue_head(&codec->remove_sleep);
  835. return codec;
  836. }
  837. EXPORT_SYMBOL_GPL(snd_hda_codec_device_init);
  838. /**
  839. * snd_hda_codec_new - create a HDA codec
  840. * @bus: the bus to assign
  841. * @card: card for this codec
  842. * @codec_addr: the codec address
  843. * @codecp: the pointer to store the generated codec
  844. *
  845. * Returns 0 if successful, or a negative error code.
  846. */
  847. int snd_hda_codec_new(struct hda_bus *bus, struct snd_card *card,
  848. unsigned int codec_addr, struct hda_codec **codecp)
  849. {
  850. struct hda_codec *codec;
  851. int ret;
  852. codec = snd_hda_codec_device_init(bus, codec_addr, "hdaudioC%dD%d",
  853. card->number, codec_addr);
  854. if (IS_ERR(codec))
  855. return PTR_ERR(codec);
  856. *codecp = codec;
  857. ret = snd_hda_codec_device_new(bus, card, codec_addr, *codecp, true);
  858. if (ret)
  859. put_device(hda_codec_dev(*codecp));
  860. return ret;
  861. }
  862. EXPORT_SYMBOL_GPL(snd_hda_codec_new);
  863. int snd_hda_codec_device_new(struct hda_bus *bus, struct snd_card *card,
  864. unsigned int codec_addr, struct hda_codec *codec,
  865. bool snddev_managed)
  866. {
  867. char component[31];
  868. hda_nid_t fg;
  869. int err;
  870. static const struct snd_device_ops dev_ops = {
  871. .dev_register = snd_hda_codec_dev_register,
  872. .dev_free = snd_hda_codec_dev_free,
  873. };
  874. dev_dbg(card->dev, "%s: entry\n", __func__);
  875. if (snd_BUG_ON(!bus))
  876. return -EINVAL;
  877. if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
  878. return -EINVAL;
  879. codec->core.exec_verb = codec_exec_verb;
  880. codec->card = card;
  881. codec->addr = codec_addr;
  882. codec->power_jiffies = jiffies;
  883. snd_hda_sysfs_init(codec);
  884. if (codec->bus->modelname) {
  885. codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
  886. if (!codec->modelname)
  887. return -ENOMEM;
  888. }
  889. fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  890. err = read_widget_caps(codec, fg);
  891. if (err < 0)
  892. return err;
  893. err = read_pin_defaults(codec);
  894. if (err < 0)
  895. return err;
  896. /* power-up all before initialization */
  897. hda_set_power_state(codec, AC_PWRST_D0);
  898. codec->core.dev.power.power_state = PMSG_ON;
  899. snd_hda_codec_proc_new(codec);
  900. snd_hda_create_hwdep(codec);
  901. sprintf(component, "HDA:%08x,%08x,%08x", codec->core.vendor_id,
  902. codec->core.subsystem_id, codec->core.revision_id);
  903. snd_component_add(card, component);
  904. if (snddev_managed) {
  905. /* ASoC features component management instead */
  906. err = snd_device_new(card, SNDRV_DEV_CODEC, codec, &dev_ops);
  907. if (err < 0)
  908. return err;
  909. }
  910. #ifdef CONFIG_PM
  911. /* PM runtime needs to be enabled later after binding codec */
  912. if (codec->core.dev.power.runtime_auto)
  913. pm_runtime_forbid(&codec->core.dev);
  914. else
  915. /* Keep the usage_count consistent across subsequent probing */
  916. pm_runtime_get_noresume(&codec->core.dev);
  917. #endif
  918. return 0;
  919. }
  920. EXPORT_SYMBOL_GPL(snd_hda_codec_device_new);
  921. /**
  922. * snd_hda_codec_update_widgets - Refresh widget caps and pin defaults
  923. * @codec: the HDA codec
  924. *
  925. * Forcibly refresh the all widget caps and the init pin configurations of
  926. * the given codec.
  927. */
  928. int snd_hda_codec_update_widgets(struct hda_codec *codec)
  929. {
  930. hda_nid_t fg;
  931. int err;
  932. err = snd_hdac_refresh_widgets(&codec->core);
  933. if (err < 0)
  934. return err;
  935. /* Assume the function group node does not change,
  936. * only the widget nodes may change.
  937. */
  938. kfree(codec->wcaps);
  939. fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  940. err = read_widget_caps(codec, fg);
  941. if (err < 0)
  942. return err;
  943. snd_array_free(&codec->init_pins);
  944. err = read_pin_defaults(codec);
  945. return err;
  946. }
  947. EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets);
  948. /* update the stream-id if changed */
  949. static void update_pcm_stream_id(struct hda_codec *codec,
  950. struct hda_cvt_setup *p, hda_nid_t nid,
  951. u32 stream_tag, int channel_id)
  952. {
  953. unsigned int oldval, newval;
  954. if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
  955. oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
  956. newval = (stream_tag << 4) | channel_id;
  957. if (oldval != newval)
  958. snd_hda_codec_write(codec, nid, 0,
  959. AC_VERB_SET_CHANNEL_STREAMID,
  960. newval);
  961. p->stream_tag = stream_tag;
  962. p->channel_id = channel_id;
  963. }
  964. }
  965. /* update the format-id if changed */
  966. static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p,
  967. hda_nid_t nid, int format)
  968. {
  969. unsigned int oldval;
  970. if (p->format_id != format) {
  971. oldval = snd_hda_codec_read(codec, nid, 0,
  972. AC_VERB_GET_STREAM_FORMAT, 0);
  973. if (oldval != format) {
  974. msleep(1);
  975. snd_hda_codec_write(codec, nid, 0,
  976. AC_VERB_SET_STREAM_FORMAT,
  977. format);
  978. }
  979. p->format_id = format;
  980. }
  981. }
  982. /**
  983. * snd_hda_codec_setup_stream - set up the codec for streaming
  984. * @codec: the CODEC to set up
  985. * @nid: the NID to set up
  986. * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
  987. * @channel_id: channel id to pass, zero based.
  988. * @format: stream format.
  989. */
  990. void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  991. u32 stream_tag,
  992. int channel_id, int format)
  993. {
  994. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  995. struct hda_codec *c;
  996. struct hda_cvt_setup *p;
  997. int type;
  998. int i;
  999. if (!nid)
  1000. return;
  1001. codec_dbg(codec,
  1002. "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
  1003. nid, stream_tag, channel_id, format);
  1004. p = get_hda_cvt_setup(codec, nid);
  1005. if (!p)
  1006. return;
  1007. if (driver->ops->stream_pm)
  1008. driver->ops->stream_pm(codec, nid, true);
  1009. if (codec->pcm_format_first)
  1010. update_pcm_format(codec, p, nid, format);
  1011. update_pcm_stream_id(codec, p, nid, stream_tag, channel_id);
  1012. if (!codec->pcm_format_first)
  1013. update_pcm_format(codec, p, nid, format);
  1014. p->active = 1;
  1015. p->dirty = 0;
  1016. /* make other inactive cvts with the same stream-tag dirty */
  1017. type = get_wcaps_type(get_wcaps(codec, nid));
  1018. list_for_each_codec(c, codec->bus) {
  1019. snd_array_for_each(&c->cvt_setups, i, p) {
  1020. if (!p->active && p->stream_tag == stream_tag &&
  1021. get_wcaps_type(get_wcaps(c, p->nid)) == type)
  1022. p->dirty = 1;
  1023. }
  1024. }
  1025. }
  1026. EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream);
  1027. static void really_cleanup_stream(struct hda_codec *codec,
  1028. struct hda_cvt_setup *q);
  1029. /**
  1030. * __snd_hda_codec_cleanup_stream - clean up the codec for closing
  1031. * @codec: the CODEC to clean up
  1032. * @nid: the NID to clean up
  1033. * @do_now: really clean up the stream instead of clearing the active flag
  1034. */
  1035. void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
  1036. int do_now)
  1037. {
  1038. struct hda_cvt_setup *p;
  1039. if (!nid)
  1040. return;
  1041. if (codec->no_sticky_stream)
  1042. do_now = 1;
  1043. codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid);
  1044. p = get_hda_cvt_setup(codec, nid);
  1045. if (p) {
  1046. /* here we just clear the active flag when do_now isn't set;
  1047. * actual clean-ups will be done later in
  1048. * purify_inactive_streams() called from snd_hda_codec_prpapre()
  1049. */
  1050. if (do_now)
  1051. really_cleanup_stream(codec, p);
  1052. else
  1053. p->active = 0;
  1054. }
  1055. }
  1056. EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream);
  1057. static void really_cleanup_stream(struct hda_codec *codec,
  1058. struct hda_cvt_setup *q)
  1059. {
  1060. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  1061. hda_nid_t nid = q->nid;
  1062. if (q->stream_tag || q->channel_id)
  1063. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1064. if (q->format_id)
  1065. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
  1066. );
  1067. memset(q, 0, sizeof(*q));
  1068. q->nid = nid;
  1069. if (driver->ops->stream_pm)
  1070. driver->ops->stream_pm(codec, nid, false);
  1071. }
  1072. /* clean up the all conflicting obsolete streams */
  1073. static void purify_inactive_streams(struct hda_codec *codec)
  1074. {
  1075. struct hda_codec *c;
  1076. struct hda_cvt_setup *p;
  1077. int i;
  1078. list_for_each_codec(c, codec->bus) {
  1079. snd_array_for_each(&c->cvt_setups, i, p) {
  1080. if (p->dirty)
  1081. really_cleanup_stream(c, p);
  1082. }
  1083. }
  1084. }
  1085. /* clean up all streams; called from suspend */
  1086. static void hda_cleanup_all_streams(struct hda_codec *codec)
  1087. {
  1088. struct hda_cvt_setup *p;
  1089. int i;
  1090. snd_array_for_each(&codec->cvt_setups, i, p) {
  1091. if (p->stream_tag)
  1092. really_cleanup_stream(codec, p);
  1093. }
  1094. }
  1095. /*
  1096. * amp access functions
  1097. */
  1098. /**
  1099. * query_amp_caps - query AMP capabilities
  1100. * @codec: the HD-auio codec
  1101. * @nid: the NID to query
  1102. * @direction: either #HDA_INPUT or #HDA_OUTPUT
  1103. *
  1104. * Query AMP capabilities for the given widget and direction.
  1105. * Returns the obtained capability bits.
  1106. *
  1107. * When cap bits have been already read, this doesn't read again but
  1108. * returns the cached value.
  1109. */
  1110. u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
  1111. {
  1112. if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
  1113. nid = codec->core.afg;
  1114. return snd_hda_param_read(codec, nid,
  1115. direction == HDA_OUTPUT ?
  1116. AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
  1117. }
  1118. EXPORT_SYMBOL_GPL(query_amp_caps);
  1119. /**
  1120. * snd_hda_check_amp_caps - query AMP capabilities
  1121. * @codec: the HD-audio codec
  1122. * @nid: the NID to query
  1123. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1124. * @bits: bit mask to check the result
  1125. *
  1126. * Check whether the widget has the given amp capability for the direction.
  1127. */
  1128. bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
  1129. int dir, unsigned int bits)
  1130. {
  1131. if (!nid)
  1132. return false;
  1133. if (get_wcaps(codec, nid) & (1 << (dir + 1)))
  1134. if (query_amp_caps(codec, nid, dir) & bits)
  1135. return true;
  1136. return false;
  1137. }
  1138. EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps);
  1139. /**
  1140. * snd_hda_override_amp_caps - Override the AMP capabilities
  1141. * @codec: the CODEC to clean up
  1142. * @nid: the NID to clean up
  1143. * @dir: either #HDA_INPUT or #HDA_OUTPUT
  1144. * @caps: the capability bits to set
  1145. *
  1146. * Override the cached AMP caps bits value by the given one.
  1147. * This function is useful if the driver needs to adjust the AMP ranges,
  1148. * e.g. limit to 0dB, etc.
  1149. *
  1150. * Returns zero if successful or a negative error code.
  1151. */
  1152. int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  1153. unsigned int caps)
  1154. {
  1155. unsigned int parm;
  1156. snd_hda_override_wcaps(codec, nid,
  1157. get_wcaps(codec, nid) | AC_WCAP_AMP_OVRD);
  1158. parm = dir == HDA_OUTPUT ? AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP;
  1159. return snd_hdac_override_parm(&codec->core, nid, parm, caps);
  1160. }
  1161. EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps);
  1162. static unsigned int encode_amp(struct hda_codec *codec, hda_nid_t nid,
  1163. int ch, int dir, int idx)
  1164. {
  1165. unsigned int cmd = snd_hdac_regmap_encode_amp(nid, ch, dir, idx);
  1166. /* enable fake mute if no h/w mute but min=mute */
  1167. if ((query_amp_caps(codec, nid, dir) &
  1168. (AC_AMPCAP_MUTE | AC_AMPCAP_MIN_MUTE)) == AC_AMPCAP_MIN_MUTE)
  1169. cmd |= AC_AMP_FAKE_MUTE;
  1170. return cmd;
  1171. }
  1172. /**
  1173. * snd_hda_codec_amp_update - update the AMP mono value
  1174. * @codec: HD-audio codec
  1175. * @nid: NID to read the AMP value
  1176. * @ch: channel to update (0 or 1)
  1177. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1178. * @idx: the index value (only for input direction)
  1179. * @mask: bit mask to set
  1180. * @val: the bits value to set
  1181. *
  1182. * Update the AMP values for the given channel, direction and index.
  1183. */
  1184. int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid,
  1185. int ch, int dir, int idx, int mask, int val)
  1186. {
  1187. unsigned int cmd = encode_amp(codec, nid, ch, dir, idx);
  1188. return snd_hdac_regmap_update_raw(&codec->core, cmd, mask, val);
  1189. }
  1190. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update);
  1191. /**
  1192. * snd_hda_codec_amp_stereo - update the AMP stereo values
  1193. * @codec: HD-audio codec
  1194. * @nid: NID to read the AMP value
  1195. * @direction: #HDA_INPUT or #HDA_OUTPUT
  1196. * @idx: the index value (only for input direction)
  1197. * @mask: bit mask to set
  1198. * @val: the bits value to set
  1199. *
  1200. * Update the AMP values like snd_hda_codec_amp_update(), but for a
  1201. * stereo widget with the same mask and value.
  1202. */
  1203. int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
  1204. int direction, int idx, int mask, int val)
  1205. {
  1206. int ch, ret = 0;
  1207. if (snd_BUG_ON(mask & ~0xff))
  1208. mask &= 0xff;
  1209. for (ch = 0; ch < 2; ch++)
  1210. ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
  1211. idx, mask, val);
  1212. return ret;
  1213. }
  1214. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo);
  1215. /**
  1216. * snd_hda_codec_amp_init - initialize the AMP value
  1217. * @codec: the HDA codec
  1218. * @nid: NID to read the AMP value
  1219. * @ch: channel (left=0 or right=1)
  1220. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1221. * @idx: the index value (only for input direction)
  1222. * @mask: bit mask to set
  1223. * @val: the bits value to set
  1224. *
  1225. * Works like snd_hda_codec_amp_update() but it writes the value only at
  1226. * the first access. If the amp was already initialized / updated beforehand,
  1227. * this does nothing.
  1228. */
  1229. int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch,
  1230. int dir, int idx, int mask, int val)
  1231. {
  1232. unsigned int cmd = encode_amp(codec, nid, ch, dir, idx);
  1233. if (!codec->core.regmap)
  1234. return -EINVAL;
  1235. return snd_hdac_regmap_update_raw_once(&codec->core, cmd, mask, val);
  1236. }
  1237. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init);
  1238. /**
  1239. * snd_hda_codec_amp_init_stereo - initialize the stereo AMP value
  1240. * @codec: the HDA codec
  1241. * @nid: NID to read the AMP value
  1242. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1243. * @idx: the index value (only for input direction)
  1244. * @mask: bit mask to set
  1245. * @val: the bits value to set
  1246. *
  1247. * Call snd_hda_codec_amp_init() for both stereo channels.
  1248. */
  1249. int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid,
  1250. int dir, int idx, int mask, int val)
  1251. {
  1252. int ch, ret = 0;
  1253. if (snd_BUG_ON(mask & ~0xff))
  1254. mask &= 0xff;
  1255. for (ch = 0; ch < 2; ch++)
  1256. ret |= snd_hda_codec_amp_init(codec, nid, ch, dir,
  1257. idx, mask, val);
  1258. return ret;
  1259. }
  1260. EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo);
  1261. static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
  1262. unsigned int ofs)
  1263. {
  1264. u32 caps = query_amp_caps(codec, nid, dir);
  1265. /* get num steps */
  1266. caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1267. if (ofs < caps)
  1268. caps -= ofs;
  1269. return caps;
  1270. }
  1271. /**
  1272. * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
  1273. * @kcontrol: referred ctl element
  1274. * @uinfo: pointer to get/store the data
  1275. *
  1276. * The control element is supposed to have the private_value field
  1277. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1278. */
  1279. int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
  1280. struct snd_ctl_elem_info *uinfo)
  1281. {
  1282. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1283. u16 nid = get_amp_nid(kcontrol);
  1284. u8 chs = get_amp_channels(kcontrol);
  1285. int dir = get_amp_direction(kcontrol);
  1286. unsigned int ofs = get_amp_offset(kcontrol);
  1287. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1288. uinfo->count = chs == 3 ? 2 : 1;
  1289. uinfo->value.integer.min = 0;
  1290. uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
  1291. if (!uinfo->value.integer.max) {
  1292. codec_warn(codec,
  1293. "num_steps = 0 for NID=0x%x (ctl = %s)\n",
  1294. nid, kcontrol->id.name);
  1295. return -EINVAL;
  1296. }
  1297. return 0;
  1298. }
  1299. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info);
  1300. static inline unsigned int
  1301. read_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1302. int ch, int dir, int idx, unsigned int ofs)
  1303. {
  1304. unsigned int val;
  1305. val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
  1306. val &= HDA_AMP_VOLMASK;
  1307. if (val >= ofs)
  1308. val -= ofs;
  1309. else
  1310. val = 0;
  1311. return val;
  1312. }
  1313. static inline int
  1314. update_amp_value(struct hda_codec *codec, hda_nid_t nid,
  1315. int ch, int dir, int idx, unsigned int ofs,
  1316. unsigned int val)
  1317. {
  1318. unsigned int maxval;
  1319. if (val > 0)
  1320. val += ofs;
  1321. /* ofs = 0: raw max value */
  1322. maxval = get_amp_max_value(codec, nid, dir, 0);
  1323. if (val > maxval)
  1324. return -EINVAL;
  1325. return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
  1326. HDA_AMP_VOLMASK, val);
  1327. }
  1328. /**
  1329. * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
  1330. * @kcontrol: ctl element
  1331. * @ucontrol: pointer to get/store the data
  1332. *
  1333. * The control element is supposed to have the private_value field
  1334. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1335. */
  1336. int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
  1337. struct snd_ctl_elem_value *ucontrol)
  1338. {
  1339. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1340. hda_nid_t nid = get_amp_nid(kcontrol);
  1341. int chs = get_amp_channels(kcontrol);
  1342. int dir = get_amp_direction(kcontrol);
  1343. int idx = get_amp_index(kcontrol);
  1344. unsigned int ofs = get_amp_offset(kcontrol);
  1345. long *valp = ucontrol->value.integer.value;
  1346. if (chs & 1)
  1347. *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
  1348. if (chs & 2)
  1349. *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
  1350. return 0;
  1351. }
  1352. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get);
  1353. /**
  1354. * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
  1355. * @kcontrol: ctl element
  1356. * @ucontrol: pointer to get/store the data
  1357. *
  1358. * The control element is supposed to have the private_value field
  1359. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1360. */
  1361. int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
  1362. struct snd_ctl_elem_value *ucontrol)
  1363. {
  1364. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1365. hda_nid_t nid = get_amp_nid(kcontrol);
  1366. int chs = get_amp_channels(kcontrol);
  1367. int dir = get_amp_direction(kcontrol);
  1368. int idx = get_amp_index(kcontrol);
  1369. unsigned int ofs = get_amp_offset(kcontrol);
  1370. long *valp = ucontrol->value.integer.value;
  1371. int change = 0;
  1372. int err;
  1373. if (chs & 1) {
  1374. err = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
  1375. if (err < 0)
  1376. return err;
  1377. change |= err;
  1378. valp++;
  1379. }
  1380. if (chs & 2) {
  1381. err = update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
  1382. if (err < 0)
  1383. return err;
  1384. change |= err;
  1385. }
  1386. return change;
  1387. }
  1388. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put);
  1389. /* inquiry the amp caps and convert to TLV */
  1390. static void get_ctl_amp_tlv(struct snd_kcontrol *kcontrol, unsigned int *tlv)
  1391. {
  1392. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1393. hda_nid_t nid = get_amp_nid(kcontrol);
  1394. int dir = get_amp_direction(kcontrol);
  1395. unsigned int ofs = get_amp_offset(kcontrol);
  1396. bool min_mute = get_amp_min_mute(kcontrol);
  1397. u32 caps, val1, val2;
  1398. caps = query_amp_caps(codec, nid, dir);
  1399. val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1400. val2 = (val2 + 1) * 25;
  1401. val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
  1402. val1 += ofs;
  1403. val1 = ((int)val1) * ((int)val2);
  1404. if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
  1405. val2 |= TLV_DB_SCALE_MUTE;
  1406. tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
  1407. tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
  1408. tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = val1;
  1409. tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = val2;
  1410. }
  1411. /**
  1412. * snd_hda_mixer_amp_tlv - TLV callback for a standard AMP mixer volume
  1413. * @kcontrol: ctl element
  1414. * @op_flag: operation flag
  1415. * @size: byte size of input TLV
  1416. * @_tlv: TLV data
  1417. *
  1418. * The control element is supposed to have the private_value field
  1419. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1420. */
  1421. int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1422. unsigned int size, unsigned int __user *_tlv)
  1423. {
  1424. unsigned int tlv[4];
  1425. if (size < 4 * sizeof(unsigned int))
  1426. return -ENOMEM;
  1427. get_ctl_amp_tlv(kcontrol, tlv);
  1428. if (copy_to_user(_tlv, tlv, sizeof(tlv)))
  1429. return -EFAULT;
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv);
  1433. /**
  1434. * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
  1435. * @codec: HD-audio codec
  1436. * @nid: NID of a reference widget
  1437. * @dir: #HDA_INPUT or #HDA_OUTPUT
  1438. * @tlv: TLV data to be stored, at least 4 elements
  1439. *
  1440. * Set (static) TLV data for a virtual master volume using the AMP caps
  1441. * obtained from the reference NID.
  1442. * The volume range is recalculated as if the max volume is 0dB.
  1443. */
  1444. void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
  1445. unsigned int *tlv)
  1446. {
  1447. u32 caps;
  1448. int nums, step;
  1449. caps = query_amp_caps(codec, nid, dir);
  1450. nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  1451. step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
  1452. step = (step + 1) * 25;
  1453. tlv[SNDRV_CTL_TLVO_TYPE] = SNDRV_CTL_TLVT_DB_SCALE;
  1454. tlv[SNDRV_CTL_TLVO_LEN] = 2 * sizeof(unsigned int);
  1455. tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] = -nums * step;
  1456. tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP] = step;
  1457. }
  1458. EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv);
  1459. /* find a mixer control element with the given name */
  1460. static struct snd_kcontrol *
  1461. find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx)
  1462. {
  1463. struct snd_ctl_elem_id id;
  1464. memset(&id, 0, sizeof(id));
  1465. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  1466. id.device = dev;
  1467. id.index = idx;
  1468. if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
  1469. return NULL;
  1470. strscpy(id.name, name);
  1471. return snd_ctl_find_id(codec->card, &id);
  1472. }
  1473. /**
  1474. * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
  1475. * @codec: HD-audio codec
  1476. * @name: ctl id name string
  1477. *
  1478. * Get the control element with the given id string and IFACE_MIXER.
  1479. */
  1480. struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
  1481. const char *name)
  1482. {
  1483. return find_mixer_ctl(codec, name, 0, 0);
  1484. }
  1485. EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl);
  1486. static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name,
  1487. int start_idx)
  1488. {
  1489. int i, idx;
  1490. /* 16 ctlrs should be large enough */
  1491. for (i = 0, idx = start_idx; i < 16; i++, idx++) {
  1492. if (!find_mixer_ctl(codec, name, 0, idx))
  1493. return idx;
  1494. }
  1495. return -EBUSY;
  1496. }
  1497. /**
  1498. * snd_hda_ctl_add - Add a control element and assign to the codec
  1499. * @codec: HD-audio codec
  1500. * @nid: corresponding NID (optional)
  1501. * @kctl: the control element to assign
  1502. *
  1503. * Add the given control element to an array inside the codec instance.
  1504. * All control elements belonging to a codec are supposed to be added
  1505. * by this function so that a proper clean-up works at the free or
  1506. * reconfiguration time.
  1507. *
  1508. * If non-zero @nid is passed, the NID is assigned to the control element.
  1509. * The assignment is shown in the codec proc file.
  1510. *
  1511. * snd_hda_ctl_add() checks the control subdev id field whether
  1512. * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower
  1513. * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
  1514. * specifies if kctl->private_value is a HDA amplifier value.
  1515. */
  1516. int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
  1517. struct snd_kcontrol *kctl)
  1518. {
  1519. int err;
  1520. unsigned short flags = 0;
  1521. struct hda_nid_item *item;
  1522. if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
  1523. flags |= HDA_NID_ITEM_AMP;
  1524. if (nid == 0)
  1525. nid = get_amp_nid_(kctl->private_value);
  1526. }
  1527. if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
  1528. nid = kctl->id.subdevice & 0xffff;
  1529. if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
  1530. kctl->id.subdevice = 0;
  1531. err = snd_ctl_add(codec->card, kctl);
  1532. if (err < 0)
  1533. return err;
  1534. item = snd_array_new(&codec->mixers);
  1535. if (!item)
  1536. return -ENOMEM;
  1537. item->kctl = kctl;
  1538. item->nid = nid;
  1539. item->flags = flags;
  1540. return 0;
  1541. }
  1542. EXPORT_SYMBOL_GPL(snd_hda_ctl_add);
  1543. /**
  1544. * snd_hda_ctls_clear - Clear all controls assigned to the given codec
  1545. * @codec: HD-audio codec
  1546. */
  1547. void snd_hda_ctls_clear(struct hda_codec *codec)
  1548. {
  1549. int i;
  1550. struct hda_nid_item *items = codec->mixers.list;
  1551. for (i = 0; i < codec->mixers.used; i++)
  1552. snd_ctl_remove(codec->card, items[i].kctl);
  1553. snd_array_free(&codec->mixers);
  1554. snd_array_free(&codec->nids);
  1555. }
  1556. /**
  1557. * snd_hda_lock_devices - pseudo device locking
  1558. * @bus: the BUS
  1559. *
  1560. * toggle card->shutdown to allow/disallow the device access (as a hack)
  1561. */
  1562. int snd_hda_lock_devices(struct hda_bus *bus)
  1563. {
  1564. struct snd_card *card = bus->card;
  1565. struct hda_codec *codec;
  1566. guard(spinlock)(&card->files_lock);
  1567. if (card->shutdown)
  1568. return -EINVAL;
  1569. card->shutdown = 1;
  1570. if (!list_empty(&card->ctl_files))
  1571. goto err_clear;
  1572. list_for_each_codec(codec, bus) {
  1573. struct hda_pcm *cpcm;
  1574. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  1575. if (!cpcm->pcm)
  1576. continue;
  1577. if (cpcm->pcm->streams[0].substream_opened ||
  1578. cpcm->pcm->streams[1].substream_opened)
  1579. goto err_clear;
  1580. }
  1581. }
  1582. return 0;
  1583. err_clear:
  1584. card->shutdown = 0;
  1585. return -EINVAL;
  1586. }
  1587. EXPORT_SYMBOL_GPL(snd_hda_lock_devices);
  1588. /**
  1589. * snd_hda_unlock_devices - pseudo device unlocking
  1590. * @bus: the BUS
  1591. */
  1592. void snd_hda_unlock_devices(struct hda_bus *bus)
  1593. {
  1594. struct snd_card *card = bus->card;
  1595. guard(spinlock)(&card->files_lock);
  1596. card->shutdown = 0;
  1597. }
  1598. EXPORT_SYMBOL_GPL(snd_hda_unlock_devices);
  1599. /**
  1600. * snd_hda_codec_reset - Clear all objects assigned to the codec
  1601. * @codec: HD-audio codec
  1602. *
  1603. * This frees the all PCM and control elements assigned to the codec, and
  1604. * clears the caches and restores the pin default configurations.
  1605. *
  1606. * When a device is being used, it returns -EBSY. If successfully freed,
  1607. * returns zero.
  1608. */
  1609. int snd_hda_codec_reset(struct hda_codec *codec)
  1610. {
  1611. struct hda_bus *bus = codec->bus;
  1612. if (snd_hda_lock_devices(bus) < 0)
  1613. return -EBUSY;
  1614. /* OK, let it free */
  1615. device_release_driver(hda_codec_dev(codec));
  1616. /* allow device access again */
  1617. snd_hda_unlock_devices(bus);
  1618. return 0;
  1619. }
  1620. typedef int (*map_follower_func_t)(struct hda_codec *, void *, struct snd_kcontrol *);
  1621. /* apply the function to all matching follower ctls in the mixer list */
  1622. static int map_followers(struct hda_codec *codec, const char * const *followers,
  1623. const char *suffix, map_follower_func_t func, void *data)
  1624. {
  1625. struct hda_nid_item *items;
  1626. const char * const *s;
  1627. int i, err;
  1628. items = codec->mixers.list;
  1629. for (i = 0; i < codec->mixers.used; i++) {
  1630. struct snd_kcontrol *sctl = items[i].kctl;
  1631. if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
  1632. continue;
  1633. for (s = followers; *s; s++) {
  1634. char tmpname[sizeof(sctl->id.name)];
  1635. const char *name = *s;
  1636. if (suffix) {
  1637. snprintf(tmpname, sizeof(tmpname), "%s %s",
  1638. name, suffix);
  1639. name = tmpname;
  1640. }
  1641. if (!strcmp(sctl->id.name, name)) {
  1642. err = func(codec, data, sctl);
  1643. if (err)
  1644. return err;
  1645. break;
  1646. }
  1647. }
  1648. }
  1649. return 0;
  1650. }
  1651. static int check_follower_present(struct hda_codec *codec,
  1652. void *data, struct snd_kcontrol *sctl)
  1653. {
  1654. return 1;
  1655. }
  1656. /* call kctl->put with the given value(s) */
  1657. static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
  1658. {
  1659. struct snd_ctl_elem_value *ucontrol __free(kfree) =
  1660. kzalloc_obj(*ucontrol);
  1661. if (!ucontrol)
  1662. return -ENOMEM;
  1663. ucontrol->value.integer.value[0] = val;
  1664. ucontrol->value.integer.value[1] = val;
  1665. kctl->put(kctl, ucontrol);
  1666. return 0;
  1667. }
  1668. struct follower_init_arg {
  1669. struct hda_codec *codec;
  1670. int step;
  1671. };
  1672. /* initialize the follower volume with 0dB via snd_ctl_apply_vmaster_followers() */
  1673. static int init_follower_0dB(struct snd_kcontrol *follower,
  1674. struct snd_kcontrol *kctl,
  1675. void *_arg)
  1676. {
  1677. struct follower_init_arg *arg = _arg;
  1678. int _tlv[4];
  1679. const int *tlv = NULL;
  1680. int step;
  1681. int val;
  1682. if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
  1683. if (kctl->tlv.c != snd_hda_mixer_amp_tlv) {
  1684. codec_err(arg->codec,
  1685. "Unexpected TLV callback for follower %s:%d\n",
  1686. kctl->id.name, kctl->id.index);
  1687. return 0; /* ignore */
  1688. }
  1689. get_ctl_amp_tlv(kctl, _tlv);
  1690. tlv = _tlv;
  1691. } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
  1692. tlv = kctl->tlv.p;
  1693. if (!tlv || tlv[SNDRV_CTL_TLVO_TYPE] != SNDRV_CTL_TLVT_DB_SCALE)
  1694. return 0;
  1695. step = tlv[SNDRV_CTL_TLVO_DB_SCALE_MUTE_AND_STEP];
  1696. step &= ~TLV_DB_SCALE_MUTE;
  1697. if (!step)
  1698. return 0;
  1699. if (arg->step && arg->step != step) {
  1700. codec_err(arg->codec,
  1701. "Mismatching dB step for vmaster follower (%d!=%d)\n",
  1702. arg->step, step);
  1703. return 0;
  1704. }
  1705. arg->step = step;
  1706. val = -tlv[SNDRV_CTL_TLVO_DB_SCALE_MIN] / step;
  1707. if (val > 0) {
  1708. put_kctl_with_value(follower, val);
  1709. return val;
  1710. }
  1711. return 0;
  1712. }
  1713. /* unmute the follower via snd_ctl_apply_vmaster_followers() */
  1714. static int init_follower_unmute(struct snd_kcontrol *follower,
  1715. struct snd_kcontrol *kctl,
  1716. void *_arg)
  1717. {
  1718. return put_kctl_with_value(follower, 1);
  1719. }
  1720. static int add_follower(struct hda_codec *codec,
  1721. void *data, struct snd_kcontrol *follower)
  1722. {
  1723. return snd_ctl_add_follower(data, follower);
  1724. }
  1725. /**
  1726. * __snd_hda_add_vmaster - create a virtual master control and add followers
  1727. * @codec: HD-audio codec
  1728. * @name: vmaster control name
  1729. * @tlv: TLV data (optional)
  1730. * @followers: follower control names (optional)
  1731. * @suffix: suffix string to each follower name (optional)
  1732. * @init_follower_vol: initialize followers to unmute/0dB
  1733. * @access: kcontrol access rights
  1734. * @ctl_ret: store the vmaster kcontrol in return
  1735. *
  1736. * Create a virtual master control with the given name. The TLV data
  1737. * must be either NULL or a valid data.
  1738. *
  1739. * @followers is a NULL-terminated array of strings, each of which is a
  1740. * follower control name. All controls with these names are assigned to
  1741. * the new virtual master control.
  1742. *
  1743. * This function returns zero if successful or a negative error code.
  1744. */
  1745. int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
  1746. unsigned int *tlv, const char * const *followers,
  1747. const char *suffix, bool init_follower_vol,
  1748. unsigned int access, struct snd_kcontrol **ctl_ret)
  1749. {
  1750. struct snd_kcontrol *kctl;
  1751. int err;
  1752. if (ctl_ret)
  1753. *ctl_ret = NULL;
  1754. err = map_followers(codec, followers, suffix, check_follower_present, NULL);
  1755. if (err != 1) {
  1756. codec_dbg(codec, "No follower found for %s\n", name);
  1757. return 0;
  1758. }
  1759. kctl = snd_ctl_make_virtual_master(name, tlv);
  1760. if (!kctl)
  1761. return -ENOMEM;
  1762. kctl->vd[0].access |= access;
  1763. err = snd_hda_ctl_add(codec, 0, kctl);
  1764. if (err < 0)
  1765. return err;
  1766. err = map_followers(codec, followers, suffix, add_follower, kctl);
  1767. if (err < 0)
  1768. return err;
  1769. /* init with master mute & zero volume */
  1770. put_kctl_with_value(kctl, 0);
  1771. if (init_follower_vol) {
  1772. struct follower_init_arg arg = {
  1773. .codec = codec,
  1774. .step = 0,
  1775. };
  1776. snd_ctl_apply_vmaster_followers(kctl,
  1777. tlv ? init_follower_0dB : init_follower_unmute,
  1778. &arg);
  1779. }
  1780. if (ctl_ret)
  1781. *ctl_ret = kctl;
  1782. return 0;
  1783. }
  1784. EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster);
  1785. /* meta hook to call each driver's vmaster hook */
  1786. static void vmaster_hook(void *private_data, int enabled)
  1787. {
  1788. struct hda_vmaster_mute_hook *hook = private_data;
  1789. hook->hook(hook->codec, enabled);
  1790. }
  1791. /**
  1792. * snd_hda_add_vmaster_hook - Add a vmaster hw specific hook
  1793. * @codec: the HDA codec
  1794. * @hook: the vmaster hook object
  1795. *
  1796. * Add a hw specific hook (like EAPD) with the given vmaster switch kctl.
  1797. */
  1798. int snd_hda_add_vmaster_hook(struct hda_codec *codec,
  1799. struct hda_vmaster_mute_hook *hook)
  1800. {
  1801. if (!hook->hook || !hook->sw_kctl)
  1802. return 0;
  1803. hook->codec = codec;
  1804. snd_ctl_add_vmaster_hook(hook->sw_kctl, vmaster_hook, hook);
  1805. return 0;
  1806. }
  1807. EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook);
  1808. /**
  1809. * snd_hda_sync_vmaster_hook - Sync vmaster hook
  1810. * @hook: the vmaster hook
  1811. *
  1812. * Call the hook with the current value for synchronization.
  1813. * Should be called in init callback.
  1814. */
  1815. void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
  1816. {
  1817. if (!hook->hook || !hook->codec)
  1818. return;
  1819. /* don't call vmaster hook in the destructor since it might have
  1820. * been already destroyed
  1821. */
  1822. if (hook->codec->bus->shutdown)
  1823. return;
  1824. snd_ctl_sync_vmaster_hook(hook->sw_kctl);
  1825. }
  1826. EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook);
  1827. /**
  1828. * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
  1829. * @kcontrol: referred ctl element
  1830. * @uinfo: pointer to get/store the data
  1831. *
  1832. * The control element is supposed to have the private_value field
  1833. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1834. */
  1835. int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
  1836. struct snd_ctl_elem_info *uinfo)
  1837. {
  1838. int chs = get_amp_channels(kcontrol);
  1839. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1840. uinfo->count = chs == 3 ? 2 : 1;
  1841. uinfo->value.integer.min = 0;
  1842. uinfo->value.integer.max = 1;
  1843. return 0;
  1844. }
  1845. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info);
  1846. /**
  1847. * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
  1848. * @kcontrol: ctl element
  1849. * @ucontrol: pointer to get/store the data
  1850. *
  1851. * The control element is supposed to have the private_value field
  1852. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1853. */
  1854. int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
  1855. struct snd_ctl_elem_value *ucontrol)
  1856. {
  1857. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1858. hda_nid_t nid = get_amp_nid(kcontrol);
  1859. int chs = get_amp_channels(kcontrol);
  1860. int dir = get_amp_direction(kcontrol);
  1861. int idx = get_amp_index(kcontrol);
  1862. long *valp = ucontrol->value.integer.value;
  1863. if (chs & 1)
  1864. *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
  1865. HDA_AMP_MUTE) ? 0 : 1;
  1866. if (chs & 2)
  1867. *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
  1868. HDA_AMP_MUTE) ? 0 : 1;
  1869. return 0;
  1870. }
  1871. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get);
  1872. /**
  1873. * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
  1874. * @kcontrol: ctl element
  1875. * @ucontrol: pointer to get/store the data
  1876. *
  1877. * The control element is supposed to have the private_value field
  1878. * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
  1879. */
  1880. int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
  1881. struct snd_ctl_elem_value *ucontrol)
  1882. {
  1883. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1884. hda_nid_t nid = get_amp_nid(kcontrol);
  1885. int chs = get_amp_channels(kcontrol);
  1886. int dir = get_amp_direction(kcontrol);
  1887. int idx = get_amp_index(kcontrol);
  1888. long *valp = ucontrol->value.integer.value;
  1889. int change = 0;
  1890. if (chs & 1) {
  1891. if (*valp < 0 || *valp > 1)
  1892. return -EINVAL;
  1893. change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
  1894. HDA_AMP_MUTE,
  1895. *valp ? 0 : HDA_AMP_MUTE);
  1896. valp++;
  1897. }
  1898. if (chs & 2) {
  1899. if (*valp < 0 || *valp > 1)
  1900. return -EINVAL;
  1901. change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
  1902. HDA_AMP_MUTE,
  1903. *valp ? 0 : HDA_AMP_MUTE);
  1904. }
  1905. hda_call_check_power_status(codec, nid);
  1906. return change;
  1907. }
  1908. EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put);
  1909. /*
  1910. * SPDIF out controls
  1911. */
  1912. static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
  1913. struct snd_ctl_elem_info *uinfo)
  1914. {
  1915. uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
  1916. uinfo->count = 1;
  1917. return 0;
  1918. }
  1919. static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
  1920. struct snd_ctl_elem_value *ucontrol)
  1921. {
  1922. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1923. IEC958_AES0_NONAUDIO |
  1924. IEC958_AES0_CON_EMPHASIS_5015 |
  1925. IEC958_AES0_CON_NOT_COPYRIGHT;
  1926. ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
  1927. IEC958_AES1_CON_ORIGINAL;
  1928. return 0;
  1929. }
  1930. static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
  1931. struct snd_ctl_elem_value *ucontrol)
  1932. {
  1933. ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
  1934. IEC958_AES0_NONAUDIO |
  1935. IEC958_AES0_PRO_EMPHASIS_5015;
  1936. return 0;
  1937. }
  1938. static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
  1939. struct snd_ctl_elem_value *ucontrol)
  1940. {
  1941. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1942. int idx = kcontrol->private_value;
  1943. struct hda_spdif_out *spdif;
  1944. if (WARN_ON(codec->spdif_out.used <= idx))
  1945. return -EINVAL;
  1946. guard(mutex)(&codec->spdif_mutex);
  1947. spdif = snd_array_elem(&codec->spdif_out, idx);
  1948. ucontrol->value.iec958.status[0] = spdif->status & 0xff;
  1949. ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
  1950. ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
  1951. ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
  1952. return 0;
  1953. }
  1954. /* convert from SPDIF status bits to HDA SPDIF bits
  1955. * bit 0 (DigEn) is always set zero (to be filled later)
  1956. */
  1957. static unsigned short convert_from_spdif_status(unsigned int sbits)
  1958. {
  1959. unsigned short val = 0;
  1960. if (sbits & IEC958_AES0_PROFESSIONAL)
  1961. val |= AC_DIG1_PROFESSIONAL;
  1962. if (sbits & IEC958_AES0_NONAUDIO)
  1963. val |= AC_DIG1_NONAUDIO;
  1964. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1965. if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
  1966. IEC958_AES0_PRO_EMPHASIS_5015)
  1967. val |= AC_DIG1_EMPHASIS;
  1968. } else {
  1969. if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
  1970. IEC958_AES0_CON_EMPHASIS_5015)
  1971. val |= AC_DIG1_EMPHASIS;
  1972. if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
  1973. val |= AC_DIG1_COPYRIGHT;
  1974. if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
  1975. val |= AC_DIG1_LEVEL;
  1976. val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
  1977. }
  1978. return val;
  1979. }
  1980. /* convert to SPDIF status bits from HDA SPDIF bits
  1981. */
  1982. static unsigned int convert_to_spdif_status(unsigned short val)
  1983. {
  1984. unsigned int sbits = 0;
  1985. if (val & AC_DIG1_NONAUDIO)
  1986. sbits |= IEC958_AES0_NONAUDIO;
  1987. if (val & AC_DIG1_PROFESSIONAL)
  1988. sbits |= IEC958_AES0_PROFESSIONAL;
  1989. if (sbits & IEC958_AES0_PROFESSIONAL) {
  1990. if (val & AC_DIG1_EMPHASIS)
  1991. sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
  1992. } else {
  1993. if (val & AC_DIG1_EMPHASIS)
  1994. sbits |= IEC958_AES0_CON_EMPHASIS_5015;
  1995. if (!(val & AC_DIG1_COPYRIGHT))
  1996. sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
  1997. if (val & AC_DIG1_LEVEL)
  1998. sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
  1999. sbits |= val & (0x7f << 8);
  2000. }
  2001. return sbits;
  2002. }
  2003. /* set digital convert verbs both for the given NID and its followers */
  2004. static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
  2005. int mask, int val)
  2006. {
  2007. const hda_nid_t *d;
  2008. snd_hdac_regmap_update(&codec->core, nid, AC_VERB_SET_DIGI_CONVERT_1,
  2009. mask, val);
  2010. d = codec->follower_dig_outs;
  2011. if (!d)
  2012. return;
  2013. for (; *d; d++)
  2014. snd_hdac_regmap_update(&codec->core, *d,
  2015. AC_VERB_SET_DIGI_CONVERT_1, mask, val);
  2016. }
  2017. static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
  2018. int dig1, int dig2)
  2019. {
  2020. unsigned int mask = 0;
  2021. unsigned int val = 0;
  2022. if (dig1 != -1) {
  2023. mask |= 0xff;
  2024. val = dig1;
  2025. }
  2026. if (dig2 != -1) {
  2027. mask |= 0xff00;
  2028. val |= dig2 << 8;
  2029. }
  2030. set_dig_out(codec, nid, mask, val);
  2031. }
  2032. static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
  2033. struct snd_ctl_elem_value *ucontrol)
  2034. {
  2035. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2036. int idx = kcontrol->private_value;
  2037. struct hda_spdif_out *spdif;
  2038. hda_nid_t nid;
  2039. unsigned short val;
  2040. int change;
  2041. if (WARN_ON(codec->spdif_out.used <= idx))
  2042. return -EINVAL;
  2043. guard(mutex)(&codec->spdif_mutex);
  2044. spdif = snd_array_elem(&codec->spdif_out, idx);
  2045. nid = spdif->nid;
  2046. spdif->status = ucontrol->value.iec958.status[0] |
  2047. ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
  2048. ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
  2049. ((unsigned int)ucontrol->value.iec958.status[3] << 24);
  2050. val = convert_from_spdif_status(spdif->status);
  2051. val |= spdif->ctls & 1;
  2052. change = spdif->ctls != val;
  2053. spdif->ctls = val;
  2054. if (change && nid != (u16)-1)
  2055. set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2056. return change;
  2057. }
  2058. #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info
  2059. static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
  2060. struct snd_ctl_elem_value *ucontrol)
  2061. {
  2062. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2063. int idx = kcontrol->private_value;
  2064. struct hda_spdif_out *spdif;
  2065. if (WARN_ON(codec->spdif_out.used <= idx))
  2066. return -EINVAL;
  2067. guard(mutex)(&codec->spdif_mutex);
  2068. spdif = snd_array_elem(&codec->spdif_out, idx);
  2069. ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
  2070. return 0;
  2071. }
  2072. static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
  2073. int dig1, int dig2)
  2074. {
  2075. set_dig_out_convert(codec, nid, dig1, dig2);
  2076. /* unmute amp switch (if any) */
  2077. if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
  2078. (dig1 & AC_DIG1_ENABLE))
  2079. snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
  2080. HDA_AMP_MUTE, 0);
  2081. }
  2082. static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
  2083. struct snd_ctl_elem_value *ucontrol)
  2084. {
  2085. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2086. int idx = kcontrol->private_value;
  2087. struct hda_spdif_out *spdif;
  2088. hda_nid_t nid;
  2089. unsigned short val;
  2090. int change;
  2091. if (WARN_ON(codec->spdif_out.used <= idx))
  2092. return -EINVAL;
  2093. guard(mutex)(&codec->spdif_mutex);
  2094. spdif = snd_array_elem(&codec->spdif_out, idx);
  2095. nid = spdif->nid;
  2096. val = spdif->ctls & ~AC_DIG1_ENABLE;
  2097. if (ucontrol->value.integer.value[0])
  2098. val |= AC_DIG1_ENABLE;
  2099. change = spdif->ctls != val;
  2100. spdif->ctls = val;
  2101. if (change && nid != (u16)-1)
  2102. set_spdif_ctls(codec, nid, val & 0xff, -1);
  2103. return change;
  2104. }
  2105. static const struct snd_kcontrol_new dig_mixes[] = {
  2106. {
  2107. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2108. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2109. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
  2110. .info = snd_hda_spdif_mask_info,
  2111. .get = snd_hda_spdif_cmask_get,
  2112. },
  2113. {
  2114. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2115. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2116. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
  2117. .info = snd_hda_spdif_mask_info,
  2118. .get = snd_hda_spdif_pmask_get,
  2119. },
  2120. {
  2121. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2122. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
  2123. .info = snd_hda_spdif_mask_info,
  2124. .get = snd_hda_spdif_default_get,
  2125. .put = snd_hda_spdif_default_put,
  2126. },
  2127. {
  2128. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2129. .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
  2130. .info = snd_hda_spdif_out_switch_info,
  2131. .get = snd_hda_spdif_out_switch_get,
  2132. .put = snd_hda_spdif_out_switch_put,
  2133. },
  2134. { } /* end */
  2135. };
  2136. /**
  2137. * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls
  2138. * @codec: the HDA codec
  2139. * @associated_nid: NID that new ctls associated with
  2140. * @cvt_nid: converter NID
  2141. * @type: HDA_PCM_TYPE_*
  2142. * Creates controls related with the digital output.
  2143. * Called from each codec driver supporting the digital out.
  2144. *
  2145. * Returns 0 if successful, or a negative error code.
  2146. */
  2147. int snd_hda_create_dig_out_ctls(struct hda_codec *codec,
  2148. hda_nid_t associated_nid,
  2149. hda_nid_t cvt_nid,
  2150. int type)
  2151. {
  2152. int err;
  2153. struct snd_kcontrol *kctl;
  2154. const struct snd_kcontrol_new *dig_mix;
  2155. int idx = 0;
  2156. int val = 0;
  2157. const int spdif_index = 16;
  2158. struct hda_spdif_out *spdif;
  2159. struct hda_bus *bus = codec->bus;
  2160. if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI &&
  2161. type == HDA_PCM_TYPE_SPDIF) {
  2162. idx = spdif_index;
  2163. } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF &&
  2164. type == HDA_PCM_TYPE_HDMI) {
  2165. /* suppose a single SPDIF device */
  2166. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2167. struct snd_ctl_elem_id id;
  2168. kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0);
  2169. if (!kctl)
  2170. break;
  2171. id = kctl->id;
  2172. id.index = spdif_index;
  2173. err = snd_ctl_rename_id(codec->card, &kctl->id, &id);
  2174. if (err < 0)
  2175. return err;
  2176. }
  2177. bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI;
  2178. }
  2179. if (!bus->primary_dig_out_type)
  2180. bus->primary_dig_out_type = type;
  2181. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx);
  2182. if (idx < 0) {
  2183. codec_err(codec, "too many IEC958 outputs\n");
  2184. return -EBUSY;
  2185. }
  2186. spdif = snd_array_new(&codec->spdif_out);
  2187. if (!spdif)
  2188. return -ENOMEM;
  2189. for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
  2190. kctl = snd_ctl_new1(dig_mix, codec);
  2191. if (!kctl)
  2192. return -ENOMEM;
  2193. kctl->id.index = idx;
  2194. kctl->private_value = codec->spdif_out.used - 1;
  2195. err = snd_hda_ctl_add(codec, associated_nid, kctl);
  2196. if (err < 0)
  2197. return err;
  2198. }
  2199. spdif->nid = cvt_nid;
  2200. snd_hdac_regmap_read(&codec->core, cvt_nid,
  2201. AC_VERB_GET_DIGI_CONVERT_1, &val);
  2202. spdif->ctls = val;
  2203. spdif->status = convert_to_spdif_status(spdif->ctls);
  2204. return 0;
  2205. }
  2206. EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls);
  2207. /**
  2208. * snd_hda_spdif_out_of_nid - get the hda_spdif_out entry from the given NID
  2209. * @codec: the HDA codec
  2210. * @nid: widget NID
  2211. *
  2212. * call within spdif_mutex lock
  2213. */
  2214. struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
  2215. hda_nid_t nid)
  2216. {
  2217. struct hda_spdif_out *spdif;
  2218. int i;
  2219. snd_array_for_each(&codec->spdif_out, i, spdif) {
  2220. if (spdif->nid == nid)
  2221. return spdif;
  2222. }
  2223. return NULL;
  2224. }
  2225. EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid);
  2226. /**
  2227. * snd_hda_spdif_ctls_unassign - Unassign the given SPDIF ctl
  2228. * @codec: the HDA codec
  2229. * @idx: the SPDIF ctl index
  2230. *
  2231. * Unassign the widget from the given SPDIF control.
  2232. */
  2233. void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
  2234. {
  2235. struct hda_spdif_out *spdif;
  2236. if (WARN_ON(codec->spdif_out.used <= idx))
  2237. return;
  2238. guard(mutex)(&codec->spdif_mutex);
  2239. spdif = snd_array_elem(&codec->spdif_out, idx);
  2240. spdif->nid = (u16)-1;
  2241. }
  2242. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign);
  2243. /**
  2244. * snd_hda_spdif_ctls_assign - Assign the SPDIF controls to the given NID
  2245. * @codec: the HDA codec
  2246. * @idx: the SPDIF ctl idx
  2247. * @nid: widget NID
  2248. *
  2249. * Assign the widget to the SPDIF control with the given index.
  2250. */
  2251. void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
  2252. {
  2253. struct hda_spdif_out *spdif;
  2254. unsigned short val;
  2255. if (WARN_ON(codec->spdif_out.used <= idx))
  2256. return;
  2257. guard(mutex)(&codec->spdif_mutex);
  2258. spdif = snd_array_elem(&codec->spdif_out, idx);
  2259. if (spdif->nid != nid) {
  2260. spdif->nid = nid;
  2261. val = spdif->ctls;
  2262. set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
  2263. }
  2264. }
  2265. EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign);
  2266. /*
  2267. * SPDIF sharing with analog output
  2268. */
  2269. static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
  2270. struct snd_ctl_elem_value *ucontrol)
  2271. {
  2272. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2273. ucontrol->value.integer.value[0] = mout->share_spdif;
  2274. return 0;
  2275. }
  2276. static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
  2277. struct snd_ctl_elem_value *ucontrol)
  2278. {
  2279. struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
  2280. mout->share_spdif = !!ucontrol->value.integer.value[0];
  2281. return 0;
  2282. }
  2283. static const struct snd_kcontrol_new spdif_share_sw = {
  2284. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2285. .name = "IEC958 Default PCM Playback Switch",
  2286. .info = snd_ctl_boolean_mono_info,
  2287. .get = spdif_share_sw_get,
  2288. .put = spdif_share_sw_put,
  2289. };
  2290. /**
  2291. * snd_hda_create_spdif_share_sw - create Default PCM switch
  2292. * @codec: the HDA codec
  2293. * @mout: multi-out instance
  2294. */
  2295. int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
  2296. struct hda_multi_out *mout)
  2297. {
  2298. struct snd_kcontrol *kctl;
  2299. if (!mout->dig_out_nid)
  2300. return 0;
  2301. kctl = snd_ctl_new1(&spdif_share_sw, mout);
  2302. if (!kctl)
  2303. return -ENOMEM;
  2304. /* ATTENTION: here mout is passed as private_data, instead of codec */
  2305. return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl);
  2306. }
  2307. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw);
  2308. /*
  2309. * SPDIF input
  2310. */
  2311. #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info
  2312. static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
  2313. struct snd_ctl_elem_value *ucontrol)
  2314. {
  2315. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2316. ucontrol->value.integer.value[0] = codec->spdif_in_enable;
  2317. return 0;
  2318. }
  2319. static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
  2320. struct snd_ctl_elem_value *ucontrol)
  2321. {
  2322. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2323. hda_nid_t nid = kcontrol->private_value;
  2324. unsigned int val = !!ucontrol->value.integer.value[0];
  2325. int change;
  2326. guard(mutex)(&codec->spdif_mutex);
  2327. change = codec->spdif_in_enable != val;
  2328. if (change) {
  2329. codec->spdif_in_enable = val;
  2330. snd_hdac_regmap_write(&codec->core, nid,
  2331. AC_VERB_SET_DIGI_CONVERT_1, val);
  2332. }
  2333. return change;
  2334. }
  2335. static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
  2336. struct snd_ctl_elem_value *ucontrol)
  2337. {
  2338. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2339. hda_nid_t nid = kcontrol->private_value;
  2340. unsigned int val;
  2341. unsigned int sbits;
  2342. snd_hdac_regmap_read(&codec->core, nid,
  2343. AC_VERB_GET_DIGI_CONVERT_1, &val);
  2344. sbits = convert_to_spdif_status(val);
  2345. ucontrol->value.iec958.status[0] = sbits;
  2346. ucontrol->value.iec958.status[1] = sbits >> 8;
  2347. ucontrol->value.iec958.status[2] = sbits >> 16;
  2348. ucontrol->value.iec958.status[3] = sbits >> 24;
  2349. return 0;
  2350. }
  2351. static const struct snd_kcontrol_new dig_in_ctls[] = {
  2352. {
  2353. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2354. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
  2355. .info = snd_hda_spdif_in_switch_info,
  2356. .get = snd_hda_spdif_in_switch_get,
  2357. .put = snd_hda_spdif_in_switch_put,
  2358. },
  2359. {
  2360. .access = SNDRV_CTL_ELEM_ACCESS_READ,
  2361. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2362. .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
  2363. .info = snd_hda_spdif_mask_info,
  2364. .get = snd_hda_spdif_in_status_get,
  2365. },
  2366. { } /* end */
  2367. };
  2368. /**
  2369. * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
  2370. * @codec: the HDA codec
  2371. * @nid: audio in widget NID
  2372. *
  2373. * Creates controls related with the SPDIF input.
  2374. * Called from each codec driver supporting the SPDIF in.
  2375. *
  2376. * Returns 0 if successful, or a negative error code.
  2377. */
  2378. int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
  2379. {
  2380. int err;
  2381. struct snd_kcontrol *kctl;
  2382. const struct snd_kcontrol_new *dig_mix;
  2383. int idx;
  2384. idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0);
  2385. if (idx < 0) {
  2386. codec_err(codec, "too many IEC958 inputs\n");
  2387. return -EBUSY;
  2388. }
  2389. for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
  2390. kctl = snd_ctl_new1(dig_mix, codec);
  2391. if (!kctl)
  2392. return -ENOMEM;
  2393. kctl->private_value = nid;
  2394. err = snd_hda_ctl_add(codec, nid, kctl);
  2395. if (err < 0)
  2396. return err;
  2397. }
  2398. codec->spdif_in_enable =
  2399. snd_hda_codec_read(codec, nid, 0,
  2400. AC_VERB_GET_DIGI_CONVERT_1, 0) &
  2401. AC_DIG1_ENABLE;
  2402. return 0;
  2403. }
  2404. EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls);
  2405. /**
  2406. * snd_hda_codec_set_power_to_all - Set the power state to all widgets
  2407. * @codec: the HDA codec
  2408. * @fg: function group (not used now)
  2409. * @power_state: the power state to set (AC_PWRST_*)
  2410. *
  2411. * Set the given power state to all widgets that have the power control.
  2412. * If the codec has power_filter set, it evaluates the power state and
  2413. * filter out if it's unchanged as D3.
  2414. */
  2415. void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
  2416. unsigned int power_state)
  2417. {
  2418. hda_nid_t nid;
  2419. for_each_hda_codec_node(nid, codec) {
  2420. unsigned int wcaps = get_wcaps(codec, nid);
  2421. unsigned int state = power_state;
  2422. if (!(wcaps & AC_WCAP_POWER))
  2423. continue;
  2424. if (codec->power_filter) {
  2425. state = codec->power_filter(codec, nid, power_state);
  2426. if (state != power_state && power_state == AC_PWRST_D3)
  2427. continue;
  2428. }
  2429. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  2430. state);
  2431. }
  2432. }
  2433. EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all);
  2434. /**
  2435. * snd_hda_codec_eapd_power_filter - A power filter callback for EAPD
  2436. * @codec: the HDA codec
  2437. * @nid: widget NID
  2438. * @power_state: power state to evalue
  2439. *
  2440. * Don't power down the widget if it controls eapd and EAPD_BTLENABLE is set.
  2441. * This can be used a codec power_filter callback.
  2442. */
  2443. unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec,
  2444. hda_nid_t nid,
  2445. unsigned int power_state)
  2446. {
  2447. if (nid == codec->core.afg || nid == codec->core.mfg)
  2448. return power_state;
  2449. if (power_state == AC_PWRST_D3 &&
  2450. get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN &&
  2451. (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
  2452. int eapd = snd_hda_codec_read(codec, nid, 0,
  2453. AC_VERB_GET_EAPD_BTLENABLE, 0);
  2454. if (eapd & 0x02)
  2455. return AC_PWRST_D0;
  2456. }
  2457. return power_state;
  2458. }
  2459. EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter);
  2460. /*
  2461. * set power state of the codec, and return the power state
  2462. */
  2463. static unsigned int hda_set_power_state(struct hda_codec *codec,
  2464. unsigned int power_state)
  2465. {
  2466. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  2467. hda_nid_t fg = codec->core.afg ? codec->core.afg : codec->core.mfg;
  2468. int count;
  2469. unsigned int state;
  2470. int flags = 0;
  2471. /* this delay seems necessary to avoid click noise at power-down */
  2472. if (power_state == AC_PWRST_D3) {
  2473. if (codec->depop_delay < 0)
  2474. msleep(codec_has_epss(codec) ? 10 : 100);
  2475. else if (codec->depop_delay > 0)
  2476. msleep(codec->depop_delay);
  2477. flags = HDA_RW_NO_RESPONSE_FALLBACK;
  2478. }
  2479. /* repeat power states setting at most 10 times*/
  2480. for (count = 0; count < 10; count++) {
  2481. /* might be called before binding to driver, too */
  2482. if (driver && driver->ops && driver->ops->set_power_state)
  2483. driver->ops->set_power_state(codec, fg, power_state);
  2484. else {
  2485. state = power_state;
  2486. if (codec->power_filter)
  2487. state = codec->power_filter(codec, fg, state);
  2488. if (state == power_state || power_state != AC_PWRST_D3)
  2489. snd_hda_codec_read(codec, fg, flags,
  2490. AC_VERB_SET_POWER_STATE,
  2491. state);
  2492. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  2493. }
  2494. state = snd_hda_sync_power_state(codec, fg, power_state);
  2495. if (!(state & AC_PWRST_ERROR))
  2496. break;
  2497. }
  2498. return state;
  2499. }
  2500. /* sync power states of all widgets;
  2501. * this is called at the end of codec parsing
  2502. */
  2503. static void sync_power_up_states(struct hda_codec *codec)
  2504. {
  2505. hda_nid_t nid;
  2506. /* don't care if no filter is used */
  2507. if (!codec->power_filter)
  2508. return;
  2509. for_each_hda_codec_node(nid, codec) {
  2510. unsigned int wcaps = get_wcaps(codec, nid);
  2511. unsigned int target;
  2512. if (!(wcaps & AC_WCAP_POWER))
  2513. continue;
  2514. target = codec->power_filter(codec, nid, AC_PWRST_D0);
  2515. if (target == AC_PWRST_D0)
  2516. continue;
  2517. if (!snd_hda_check_power_state(codec, nid, target))
  2518. snd_hda_codec_write(codec, nid, 0,
  2519. AC_VERB_SET_POWER_STATE, target);
  2520. }
  2521. }
  2522. #ifdef CONFIG_SND_HDA_RECONFIG
  2523. /* execute additional init verbs */
  2524. static void hda_exec_init_verbs(struct hda_codec *codec)
  2525. {
  2526. if (codec->init_verbs.list)
  2527. snd_hda_sequence_write(codec, codec->init_verbs.list);
  2528. }
  2529. #else
  2530. static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
  2531. #endif
  2532. /* update the power on/off account with the current jiffies */
  2533. static void update_power_acct(struct hda_codec *codec, bool on)
  2534. {
  2535. unsigned long delta = jiffies - codec->power_jiffies;
  2536. if (on)
  2537. codec->power_on_acct += delta;
  2538. else
  2539. codec->power_off_acct += delta;
  2540. codec->power_jiffies += delta;
  2541. }
  2542. void snd_hda_update_power_acct(struct hda_codec *codec)
  2543. {
  2544. update_power_acct(codec, hda_codec_is_power_on(codec));
  2545. }
  2546. /*
  2547. * call suspend and power-down; used both from PM and power-save
  2548. * this function returns the power state in the end
  2549. */
  2550. static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
  2551. {
  2552. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  2553. unsigned int state;
  2554. snd_hdac_enter_pm(&codec->core);
  2555. if (driver->ops->suspend)
  2556. driver->ops->suspend(codec);
  2557. if (!codec->no_stream_clean_at_suspend)
  2558. hda_cleanup_all_streams(codec);
  2559. state = hda_set_power_state(codec, AC_PWRST_D3);
  2560. update_power_acct(codec, true);
  2561. snd_hdac_leave_pm(&codec->core);
  2562. return state;
  2563. }
  2564. /*
  2565. * kick up codec; used both from PM and power-save
  2566. */
  2567. static void hda_call_codec_resume(struct hda_codec *codec)
  2568. {
  2569. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  2570. snd_hdac_enter_pm(&codec->core);
  2571. if (codec->core.regmap)
  2572. regcache_mark_dirty(codec->core.regmap);
  2573. codec->power_jiffies = jiffies;
  2574. hda_set_power_state(codec, AC_PWRST_D0);
  2575. restore_shutup_pins(codec);
  2576. hda_exec_init_verbs(codec);
  2577. snd_hda_jack_set_dirty_all(codec);
  2578. if (driver->ops->resume)
  2579. driver->ops->resume(codec);
  2580. else {
  2581. snd_hda_codec_init(codec);
  2582. snd_hda_regmap_sync(codec);
  2583. }
  2584. snd_hda_jack_report_sync(codec);
  2585. codec->core.dev.power.power_state = PMSG_ON;
  2586. snd_hdac_leave_pm(&codec->core);
  2587. if (codec->jackpoll_interval)
  2588. schedule_delayed_work(&codec->jackpoll_work,
  2589. codec->jackpoll_interval);
  2590. }
  2591. static int hda_codec_runtime_suspend(struct device *dev)
  2592. {
  2593. struct hda_codec *codec = dev_to_hda_codec(dev);
  2594. unsigned int state;
  2595. /* Nothing to do if card registration fails and the component driver never probes */
  2596. if (!codec->card)
  2597. return 0;
  2598. state = hda_call_codec_suspend(codec);
  2599. if (codec->link_down_at_suspend ||
  2600. (codec_has_clkstop(codec) && codec_has_epss(codec) &&
  2601. (state & AC_PWRST_CLK_STOP_OK)))
  2602. snd_hdac_codec_link_down(&codec->core);
  2603. snd_hda_codec_display_power(codec, false);
  2604. return 0;
  2605. }
  2606. static int hda_codec_runtime_resume(struct device *dev)
  2607. {
  2608. struct hda_codec *codec = dev_to_hda_codec(dev);
  2609. /* Nothing to do if card registration fails and the component driver never probes */
  2610. if (!codec->card)
  2611. return 0;
  2612. snd_hda_codec_display_power(codec, true);
  2613. snd_hdac_codec_link_up(&codec->core);
  2614. hda_call_codec_resume(codec);
  2615. pm_runtime_mark_last_busy(dev);
  2616. return 0;
  2617. }
  2618. static int hda_codec_runtime_idle(struct device *dev)
  2619. {
  2620. struct hda_codec *codec = dev_to_hda_codec(dev);
  2621. if (codec->jackpoll_interval && !codec->bus->jackpoll_in_suspend)
  2622. return -EBUSY;
  2623. return 0;
  2624. }
  2625. static int hda_codec_pm_prepare(struct device *dev)
  2626. {
  2627. struct hda_codec *codec = dev_to_hda_codec(dev);
  2628. cancel_delayed_work_sync(&codec->jackpoll_work);
  2629. dev->power.power_state = PMSG_SUSPEND;
  2630. return pm_runtime_suspended(dev);
  2631. }
  2632. static void hda_codec_pm_complete(struct device *dev)
  2633. {
  2634. struct hda_codec *codec = dev_to_hda_codec(dev);
  2635. /* If no other pm-functions are called between prepare() and complete() */
  2636. if (dev->power.power_state.event == PM_EVENT_SUSPEND)
  2637. dev->power.power_state = PMSG_RESUME;
  2638. if (pm_runtime_suspended(dev) && (codec->jackpoll_interval ||
  2639. hda_codec_need_resume(codec) || codec->forced_resume))
  2640. pm_request_resume(dev);
  2641. }
  2642. static int hda_codec_pm_suspend(struct device *dev)
  2643. {
  2644. dev->power.power_state = PMSG_SUSPEND;
  2645. return pm_runtime_force_suspend(dev);
  2646. }
  2647. static int hda_codec_pm_resume(struct device *dev)
  2648. {
  2649. dev->power.power_state = PMSG_RESUME;
  2650. return pm_runtime_force_resume(dev);
  2651. }
  2652. static int hda_codec_pm_freeze(struct device *dev)
  2653. {
  2654. struct hda_codec *codec = dev_to_hda_codec(dev);
  2655. cancel_delayed_work_sync(&codec->jackpoll_work);
  2656. dev->power.power_state = PMSG_FREEZE;
  2657. return pm_runtime_force_suspend(dev);
  2658. }
  2659. static int hda_codec_pm_thaw(struct device *dev)
  2660. {
  2661. dev->power.power_state = PMSG_THAW;
  2662. return pm_runtime_force_resume(dev);
  2663. }
  2664. static int hda_codec_pm_restore(struct device *dev)
  2665. {
  2666. dev->power.power_state = PMSG_RESTORE;
  2667. return pm_runtime_force_resume(dev);
  2668. }
  2669. /* referred in hda_bind.c */
  2670. const struct dev_pm_ops hda_codec_driver_pm = {
  2671. .prepare = pm_sleep_ptr(hda_codec_pm_prepare),
  2672. .complete = pm_sleep_ptr(hda_codec_pm_complete),
  2673. .suspend = pm_sleep_ptr(hda_codec_pm_suspend),
  2674. .resume = pm_sleep_ptr(hda_codec_pm_resume),
  2675. .freeze = pm_sleep_ptr(hda_codec_pm_freeze),
  2676. .thaw = pm_sleep_ptr(hda_codec_pm_thaw),
  2677. .poweroff = pm_sleep_ptr(hda_codec_pm_suspend),
  2678. .restore = pm_sleep_ptr(hda_codec_pm_restore),
  2679. RUNTIME_PM_OPS(hda_codec_runtime_suspend, hda_codec_runtime_resume,
  2680. hda_codec_runtime_idle)
  2681. };
  2682. /* suspend the codec at shutdown; called from driver's shutdown callback */
  2683. void snd_hda_codec_shutdown(struct hda_codec *codec)
  2684. {
  2685. struct hda_pcm *cpcm;
  2686. /* Skip the shutdown if codec is not registered */
  2687. if (!codec->core.registered)
  2688. return;
  2689. codec->jackpoll_interval = 0; /* don't poll any longer */
  2690. cancel_delayed_work_sync(&codec->jackpoll_work);
  2691. list_for_each_entry(cpcm, &codec->pcm_list_head, list)
  2692. snd_pcm_suspend_all(cpcm->pcm);
  2693. pm_runtime_force_suspend(hda_codec_dev(codec));
  2694. pm_runtime_disable(hda_codec_dev(codec));
  2695. }
  2696. /*
  2697. * add standard channel maps if not specified
  2698. */
  2699. static int add_std_chmaps(struct hda_codec *codec)
  2700. {
  2701. struct hda_pcm *pcm;
  2702. int str, err;
  2703. list_for_each_entry(pcm, &codec->pcm_list_head, list) {
  2704. for (str = 0; str < 2; str++) {
  2705. struct hda_pcm_stream *hinfo = &pcm->stream[str];
  2706. struct snd_pcm_chmap *chmap;
  2707. const struct snd_pcm_chmap_elem *elem;
  2708. if (!pcm->pcm || pcm->own_chmap || !hinfo->substreams)
  2709. continue;
  2710. elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps;
  2711. err = snd_pcm_add_chmap_ctls(pcm->pcm, str, elem,
  2712. hinfo->channels_max,
  2713. 0, &chmap);
  2714. if (err < 0)
  2715. return err;
  2716. chmap->channel_mask = SND_PCM_CHMAP_MASK_2468;
  2717. }
  2718. }
  2719. return 0;
  2720. }
  2721. /* default channel maps for 2.1 speakers;
  2722. * since HD-audio supports only stereo, odd number channels are omitted
  2723. */
  2724. const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = {
  2725. { .channels = 2,
  2726. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } },
  2727. { .channels = 4,
  2728. .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR,
  2729. SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } },
  2730. { }
  2731. };
  2732. EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps);
  2733. int snd_hda_codec_build_controls(struct hda_codec *codec)
  2734. {
  2735. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  2736. int err;
  2737. hda_exec_init_verbs(codec);
  2738. /* continue to initialize... */
  2739. err = snd_hda_codec_init(codec);
  2740. if (err < 0)
  2741. return err;
  2742. if (driver->ops->build_controls) {
  2743. err = driver->ops->build_controls(codec);
  2744. if (err < 0)
  2745. return err;
  2746. }
  2747. /* we create chmaps here instead of build_pcms */
  2748. err = add_std_chmaps(codec);
  2749. if (err < 0)
  2750. return err;
  2751. snd_hda_jack_report_sync(codec); /* call at the last init point */
  2752. if (codec->jackpoll_interval)
  2753. schedule_delayed_work(&codec->jackpoll_work,
  2754. codec->jackpoll_interval);
  2755. sync_power_up_states(codec);
  2756. return 0;
  2757. }
  2758. EXPORT_SYMBOL_GPL(snd_hda_codec_build_controls);
  2759. /*
  2760. * PCM stuff
  2761. */
  2762. static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
  2763. struct hda_codec *codec,
  2764. struct snd_pcm_substream *substream)
  2765. {
  2766. return 0;
  2767. }
  2768. static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
  2769. struct hda_codec *codec,
  2770. unsigned int stream_tag,
  2771. unsigned int format,
  2772. struct snd_pcm_substream *substream)
  2773. {
  2774. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  2775. return 0;
  2776. }
  2777. static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
  2778. struct hda_codec *codec,
  2779. struct snd_pcm_substream *substream)
  2780. {
  2781. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  2782. return 0;
  2783. }
  2784. static int set_pcm_default_values(struct hda_codec *codec,
  2785. struct hda_pcm_stream *info)
  2786. {
  2787. int err;
  2788. /* query support PCM information from the given NID */
  2789. if (info->nid && (!info->rates || !info->formats)) {
  2790. err = snd_hda_query_supported_pcm(codec, info->nid,
  2791. info->rates ? NULL : &info->rates,
  2792. info->formats ? NULL : &info->formats,
  2793. info->subformats ? NULL : &info->subformats,
  2794. info->maxbps ? NULL : &info->maxbps);
  2795. if (err < 0)
  2796. return err;
  2797. }
  2798. if (info->ops.open == NULL)
  2799. info->ops.open = hda_pcm_default_open_close;
  2800. if (info->ops.close == NULL)
  2801. info->ops.close = hda_pcm_default_open_close;
  2802. if (info->ops.prepare == NULL) {
  2803. if (snd_BUG_ON(!info->nid))
  2804. return -EINVAL;
  2805. info->ops.prepare = hda_pcm_default_prepare;
  2806. }
  2807. if (info->ops.cleanup == NULL) {
  2808. if (snd_BUG_ON(!info->nid))
  2809. return -EINVAL;
  2810. info->ops.cleanup = hda_pcm_default_cleanup;
  2811. }
  2812. return 0;
  2813. }
  2814. /*
  2815. * codec prepare/cleanup entries
  2816. */
  2817. /**
  2818. * snd_hda_codec_prepare - Prepare a stream
  2819. * @codec: the HDA codec
  2820. * @hinfo: PCM information
  2821. * @stream: stream tag to assign
  2822. * @format: format id to assign
  2823. * @substream: PCM substream to assign
  2824. *
  2825. * Calls the prepare callback set by the codec with the given arguments.
  2826. * Clean up the inactive streams when successful.
  2827. */
  2828. int snd_hda_codec_prepare(struct hda_codec *codec,
  2829. struct hda_pcm_stream *hinfo,
  2830. unsigned int stream,
  2831. unsigned int format,
  2832. struct snd_pcm_substream *substream)
  2833. {
  2834. int ret;
  2835. guard(mutex)(&codec->bus->prepare_mutex);
  2836. if (hinfo->ops.prepare)
  2837. ret = hinfo->ops.prepare(hinfo, codec, stream, format,
  2838. substream);
  2839. else
  2840. ret = -ENODEV;
  2841. if (ret >= 0)
  2842. purify_inactive_streams(codec);
  2843. return ret;
  2844. }
  2845. EXPORT_SYMBOL_GPL(snd_hda_codec_prepare);
  2846. /**
  2847. * snd_hda_codec_cleanup - Clean up stream resources
  2848. * @codec: the HDA codec
  2849. * @hinfo: PCM information
  2850. * @substream: PCM substream
  2851. *
  2852. * Calls the cleanup callback set by the codec with the given arguments.
  2853. */
  2854. void snd_hda_codec_cleanup(struct hda_codec *codec,
  2855. struct hda_pcm_stream *hinfo,
  2856. struct snd_pcm_substream *substream)
  2857. {
  2858. guard(mutex)(&codec->bus->prepare_mutex);
  2859. if (hinfo->ops.cleanup)
  2860. hinfo->ops.cleanup(hinfo, codec, substream);
  2861. }
  2862. EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup);
  2863. /* global */
  2864. const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
  2865. "Audio", "SPDIF", "HDMI", "Modem"
  2866. };
  2867. /*
  2868. * get the empty PCM device number to assign
  2869. */
  2870. static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type)
  2871. {
  2872. /* audio device indices; not linear to keep compatibility */
  2873. /* assigned to static slots up to dev#10; if more needed, assign
  2874. * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y)
  2875. */
  2876. static const int audio_idx[HDA_PCM_NTYPES][5] = {
  2877. [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
  2878. [HDA_PCM_TYPE_SPDIF] = { 1, -1 },
  2879. [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 },
  2880. [HDA_PCM_TYPE_MODEM] = { 6, -1 },
  2881. };
  2882. int i;
  2883. if (type >= HDA_PCM_NTYPES) {
  2884. dev_err(bus->card->dev, "Invalid PCM type %d\n", type);
  2885. return -EINVAL;
  2886. }
  2887. for (i = 0; audio_idx[type][i] >= 0; i++) {
  2888. #ifndef CONFIG_SND_DYNAMIC_MINORS
  2889. if (audio_idx[type][i] >= 8)
  2890. break;
  2891. #endif
  2892. if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
  2893. return audio_idx[type][i];
  2894. }
  2895. #ifdef CONFIG_SND_DYNAMIC_MINORS
  2896. /* non-fixed slots starting from 10 */
  2897. for (i = 10; i < 32; i++) {
  2898. if (!test_and_set_bit(i, bus->pcm_dev_bits))
  2899. return i;
  2900. }
  2901. #endif
  2902. dev_warn(bus->card->dev, "Too many %s devices\n",
  2903. snd_hda_pcm_type_name[type]);
  2904. #ifndef CONFIG_SND_DYNAMIC_MINORS
  2905. dev_warn(bus->card->dev,
  2906. "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n");
  2907. #endif
  2908. return -EAGAIN;
  2909. }
  2910. /* call build_pcms ops of the given codec and set up the default parameters */
  2911. int snd_hda_codec_parse_pcms(struct hda_codec *codec)
  2912. {
  2913. struct hda_codec_driver *driver = hda_codec_to_driver(codec);
  2914. struct hda_pcm *cpcm;
  2915. int err;
  2916. if (!list_empty(&codec->pcm_list_head))
  2917. return 0; /* already parsed */
  2918. if (!driver->ops->build_pcms)
  2919. return 0;
  2920. err = driver->ops->build_pcms(codec);
  2921. if (err < 0) {
  2922. codec_err(codec, "cannot build PCMs for #%d (error %d)\n",
  2923. codec->core.addr, err);
  2924. return err;
  2925. }
  2926. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  2927. int stream;
  2928. for_each_pcm_streams(stream) {
  2929. struct hda_pcm_stream *info = &cpcm->stream[stream];
  2930. if (!info->substreams)
  2931. continue;
  2932. err = set_pcm_default_values(codec, info);
  2933. if (err < 0) {
  2934. codec_warn(codec,
  2935. "fail to setup default for PCM %s\n",
  2936. cpcm->name);
  2937. return err;
  2938. }
  2939. }
  2940. }
  2941. return 0;
  2942. }
  2943. EXPORT_SYMBOL_GPL(snd_hda_codec_parse_pcms);
  2944. /* assign all PCMs of the given codec */
  2945. int snd_hda_codec_build_pcms(struct hda_codec *codec)
  2946. {
  2947. struct hda_bus *bus = codec->bus;
  2948. struct hda_pcm *cpcm;
  2949. int dev, err;
  2950. err = snd_hda_codec_parse_pcms(codec);
  2951. if (err < 0)
  2952. return err;
  2953. /* attach a new PCM streams */
  2954. list_for_each_entry(cpcm, &codec->pcm_list_head, list) {
  2955. if (cpcm->pcm)
  2956. continue; /* already attached */
  2957. if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
  2958. continue; /* no substreams assigned */
  2959. dev = get_empty_pcm_device(bus, cpcm->pcm_type);
  2960. if (dev < 0) {
  2961. cpcm->device = SNDRV_PCM_INVALID_DEVICE;
  2962. continue; /* no fatal error */
  2963. }
  2964. cpcm->device = dev;
  2965. err = snd_hda_attach_pcm_stream(bus, codec, cpcm);
  2966. if (err < 0) {
  2967. codec_err(codec,
  2968. "cannot attach PCM stream %d for codec #%d\n",
  2969. dev, codec->core.addr);
  2970. continue; /* no fatal error */
  2971. }
  2972. }
  2973. return 0;
  2974. }
  2975. /**
  2976. * snd_hda_add_new_ctls - create controls from the array
  2977. * @codec: the HDA codec
  2978. * @knew: the array of struct snd_kcontrol_new
  2979. *
  2980. * This helper function creates and add new controls in the given array.
  2981. * The array must be terminated with an empty entry as terminator.
  2982. *
  2983. * Returns 0 if successful, or a negative error code.
  2984. */
  2985. int snd_hda_add_new_ctls(struct hda_codec *codec,
  2986. const struct snd_kcontrol_new *knew)
  2987. {
  2988. int err;
  2989. for (; knew->name; knew++) {
  2990. struct snd_kcontrol *kctl;
  2991. int addr = 0, idx = 0;
  2992. if (knew->iface == (__force snd_ctl_elem_iface_t)-1)
  2993. continue; /* skip this codec private value */
  2994. for (;;) {
  2995. kctl = snd_ctl_new1(knew, codec);
  2996. if (!kctl)
  2997. return -ENOMEM;
  2998. /* Do not use the id.device field for MIXER elements.
  2999. * This field is for real device numbers (like PCM) but codecs
  3000. * are hidden components from the user space view (unrelated
  3001. * to the mixer element identification).
  3002. */
  3003. if (addr > 0 && codec->ctl_dev_id)
  3004. kctl->id.device = addr;
  3005. if (idx > 0)
  3006. kctl->id.index = idx;
  3007. err = snd_hda_ctl_add(codec, 0, kctl);
  3008. if (!err)
  3009. break;
  3010. /* try first with another device index corresponding to
  3011. * the codec addr; if it still fails (or it's the
  3012. * primary codec), then try another control index
  3013. */
  3014. if (!addr && codec->core.addr) {
  3015. addr = codec->core.addr;
  3016. if (!codec->ctl_dev_id)
  3017. idx += 10 * addr;
  3018. } else if (!idx && !knew->index) {
  3019. idx = find_empty_mixer_ctl_idx(codec,
  3020. knew->name, 0);
  3021. if (idx <= 0)
  3022. return err;
  3023. } else
  3024. return err;
  3025. }
  3026. }
  3027. return 0;
  3028. }
  3029. EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls);
  3030. /**
  3031. * snd_hda_codec_set_power_save - Configure codec's runtime PM
  3032. * @codec: codec device to configure
  3033. * @delay: autosuspend delay
  3034. */
  3035. void snd_hda_codec_set_power_save(struct hda_codec *codec, int delay)
  3036. {
  3037. struct device *dev = hda_codec_dev(codec);
  3038. if (delay == 0 && codec->auto_runtime_pm)
  3039. delay = 3000;
  3040. if (delay > 0) {
  3041. pm_runtime_set_autosuspend_delay(dev, delay);
  3042. pm_runtime_use_autosuspend(dev);
  3043. pm_runtime_allow(dev);
  3044. if (!pm_runtime_suspended(dev))
  3045. pm_runtime_mark_last_busy(dev);
  3046. } else {
  3047. pm_runtime_dont_use_autosuspend(dev);
  3048. pm_runtime_forbid(dev);
  3049. }
  3050. }
  3051. EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_save);
  3052. /**
  3053. * snd_hda_set_power_save - reprogram autosuspend for the given delay
  3054. * @bus: HD-audio bus
  3055. * @delay: autosuspend delay in msec, 0 = off
  3056. *
  3057. * Synchronize the runtime PM autosuspend state from the power_save option.
  3058. */
  3059. void snd_hda_set_power_save(struct hda_bus *bus, int delay)
  3060. {
  3061. struct hda_codec *c;
  3062. list_for_each_codec(c, bus)
  3063. snd_hda_codec_set_power_save(c, delay);
  3064. }
  3065. EXPORT_SYMBOL_GPL(snd_hda_set_power_save);
  3066. /**
  3067. * snd_hda_check_amp_list_power - Check the amp list and update the power
  3068. * @codec: HD-audio codec
  3069. * @check: the object containing an AMP list and the status
  3070. * @nid: NID to check / update
  3071. *
  3072. * Check whether the given NID is in the amp list. If it's in the list,
  3073. * check the current AMP status, and update the power-status according
  3074. * to the mute status.
  3075. *
  3076. * This function is supposed to be set or called from the check_power_status
  3077. * patch ops.
  3078. */
  3079. int snd_hda_check_amp_list_power(struct hda_codec *codec,
  3080. struct hda_loopback_check *check,
  3081. hda_nid_t nid)
  3082. {
  3083. const struct hda_amp_list *p;
  3084. int ch, v;
  3085. if (!check->amplist)
  3086. return 0;
  3087. for (p = check->amplist; p->nid; p++) {
  3088. if (p->nid == nid)
  3089. break;
  3090. }
  3091. if (!p->nid)
  3092. return 0; /* nothing changed */
  3093. for (p = check->amplist; p->nid; p++) {
  3094. for (ch = 0; ch < 2; ch++) {
  3095. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  3096. p->idx);
  3097. if (!(v & HDA_AMP_MUTE) && v > 0) {
  3098. if (!check->power_on) {
  3099. check->power_on = 1;
  3100. snd_hda_power_up_pm(codec);
  3101. }
  3102. return 1;
  3103. }
  3104. }
  3105. }
  3106. if (check->power_on) {
  3107. check->power_on = 0;
  3108. snd_hda_power_down_pm(codec);
  3109. }
  3110. return 0;
  3111. }
  3112. EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power);
  3113. /*
  3114. * input MUX helper
  3115. */
  3116. /**
  3117. * snd_hda_input_mux_info - Info callback helper for the input-mux enum
  3118. * @imux: imux helper object
  3119. * @uinfo: pointer to get/store the data
  3120. */
  3121. int snd_hda_input_mux_info(const struct hda_input_mux *imux,
  3122. struct snd_ctl_elem_info *uinfo)
  3123. {
  3124. unsigned int index;
  3125. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  3126. uinfo->count = 1;
  3127. uinfo->value.enumerated.items = imux->num_items;
  3128. if (!imux->num_items)
  3129. return 0;
  3130. index = uinfo->value.enumerated.item;
  3131. if (index >= imux->num_items)
  3132. index = imux->num_items - 1;
  3133. strscpy(uinfo->value.enumerated.name, imux->items[index].label);
  3134. return 0;
  3135. }
  3136. EXPORT_SYMBOL_GPL(snd_hda_input_mux_info);
  3137. /**
  3138. * snd_hda_input_mux_put - Put callback helper for the input-mux enum
  3139. * @codec: the HDA codec
  3140. * @imux: imux helper object
  3141. * @ucontrol: pointer to get/store the data
  3142. * @nid: input mux NID
  3143. * @cur_val: pointer to get/store the current imux value
  3144. */
  3145. int snd_hda_input_mux_put(struct hda_codec *codec,
  3146. const struct hda_input_mux *imux,
  3147. struct snd_ctl_elem_value *ucontrol,
  3148. hda_nid_t nid,
  3149. unsigned int *cur_val)
  3150. {
  3151. unsigned int idx;
  3152. if (!imux->num_items)
  3153. return 0;
  3154. idx = ucontrol->value.enumerated.item[0];
  3155. if (idx >= imux->num_items)
  3156. idx = imux->num_items - 1;
  3157. if (*cur_val == idx)
  3158. return 0;
  3159. snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
  3160. imux->items[idx].index);
  3161. *cur_val = idx;
  3162. return 1;
  3163. }
  3164. EXPORT_SYMBOL_GPL(snd_hda_input_mux_put);
  3165. /**
  3166. * snd_hda_enum_helper_info - Helper for simple enum ctls
  3167. * @kcontrol: ctl element
  3168. * @uinfo: pointer to get/store the data
  3169. * @num_items: number of enum items
  3170. * @texts: enum item string array
  3171. *
  3172. * process kcontrol info callback of a simple string enum array
  3173. * when @num_items is 0 or @texts is NULL, assume a boolean enum array
  3174. */
  3175. int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol,
  3176. struct snd_ctl_elem_info *uinfo,
  3177. int num_items, const char * const *texts)
  3178. {
  3179. static const char * const texts_default[] = {
  3180. "Disabled", "Enabled"
  3181. };
  3182. if (!texts || !num_items) {
  3183. num_items = 2;
  3184. texts = texts_default;
  3185. }
  3186. return snd_ctl_enum_info(uinfo, 1, num_items, texts);
  3187. }
  3188. EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info);
  3189. /*
  3190. * Multi-channel / digital-out PCM helper functions
  3191. */
  3192. /* setup SPDIF output stream */
  3193. static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
  3194. unsigned int stream_tag, unsigned int format)
  3195. {
  3196. struct hda_spdif_out *spdif;
  3197. unsigned int curr_fmt;
  3198. bool reset;
  3199. spdif = snd_hda_spdif_out_of_nid(codec, nid);
  3200. /* Add sanity check to pass klockwork check.
  3201. * This should never happen.
  3202. */
  3203. if (WARN_ON(spdif == NULL))
  3204. return;
  3205. curr_fmt = snd_hda_codec_read(codec, nid, 0,
  3206. AC_VERB_GET_STREAM_FORMAT, 0);
  3207. reset = codec->spdif_status_reset &&
  3208. (spdif->ctls & AC_DIG1_ENABLE) &&
  3209. curr_fmt != format;
  3210. /* turn off SPDIF if needed; otherwise the IEC958 bits won't be
  3211. updated */
  3212. if (reset)
  3213. set_dig_out_convert(codec, nid,
  3214. spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
  3215. -1);
  3216. snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
  3217. if (codec->follower_dig_outs) {
  3218. const hda_nid_t *d;
  3219. for (d = codec->follower_dig_outs; *d; d++)
  3220. snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
  3221. format);
  3222. }
  3223. /* turn on again (if needed) */
  3224. if (reset)
  3225. set_dig_out_convert(codec, nid,
  3226. spdif->ctls & 0xff, -1);
  3227. }
  3228. static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
  3229. {
  3230. snd_hda_codec_cleanup_stream(codec, nid);
  3231. if (codec->follower_dig_outs) {
  3232. const hda_nid_t *d;
  3233. for (d = codec->follower_dig_outs; *d; d++)
  3234. snd_hda_codec_cleanup_stream(codec, *d);
  3235. }
  3236. }
  3237. /**
  3238. * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
  3239. * @codec: the HDA codec
  3240. * @mout: hda_multi_out object
  3241. */
  3242. int snd_hda_multi_out_dig_open(struct hda_codec *codec,
  3243. struct hda_multi_out *mout)
  3244. {
  3245. guard(mutex)(&codec->spdif_mutex);
  3246. if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
  3247. /* already opened as analog dup; reset it once */
  3248. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3249. mout->dig_out_used = HDA_DIG_EXCLUSIVE;
  3250. return 0;
  3251. }
  3252. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open);
  3253. /**
  3254. * snd_hda_multi_out_dig_prepare - prepare the digital out stream
  3255. * @codec: the HDA codec
  3256. * @mout: hda_multi_out object
  3257. * @stream_tag: stream tag to assign
  3258. * @format: format id to assign
  3259. * @substream: PCM substream to assign
  3260. */
  3261. int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
  3262. struct hda_multi_out *mout,
  3263. unsigned int stream_tag,
  3264. unsigned int format,
  3265. struct snd_pcm_substream *substream)
  3266. {
  3267. guard(mutex)(&codec->spdif_mutex);
  3268. setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
  3269. return 0;
  3270. }
  3271. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare);
  3272. /**
  3273. * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
  3274. * @codec: the HDA codec
  3275. * @mout: hda_multi_out object
  3276. */
  3277. int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
  3278. struct hda_multi_out *mout)
  3279. {
  3280. guard(mutex)(&codec->spdif_mutex);
  3281. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3282. return 0;
  3283. }
  3284. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup);
  3285. /**
  3286. * snd_hda_multi_out_dig_close - release the digital out stream
  3287. * @codec: the HDA codec
  3288. * @mout: hda_multi_out object
  3289. */
  3290. int snd_hda_multi_out_dig_close(struct hda_codec *codec,
  3291. struct hda_multi_out *mout)
  3292. {
  3293. guard(mutex)(&codec->spdif_mutex);
  3294. mout->dig_out_used = 0;
  3295. return 0;
  3296. }
  3297. EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close);
  3298. /**
  3299. * snd_hda_multi_out_analog_open - open analog outputs
  3300. * @codec: the HDA codec
  3301. * @mout: hda_multi_out object
  3302. * @substream: PCM substream to assign
  3303. * @hinfo: PCM information to assign
  3304. *
  3305. * Open analog outputs and set up the hw-constraints.
  3306. * If the digital outputs can be opened as follower, open the digital
  3307. * outputs, too.
  3308. */
  3309. int snd_hda_multi_out_analog_open(struct hda_codec *codec,
  3310. struct hda_multi_out *mout,
  3311. struct snd_pcm_substream *substream,
  3312. struct hda_pcm_stream *hinfo)
  3313. {
  3314. struct snd_pcm_runtime *runtime = substream->runtime;
  3315. runtime->hw.channels_max = mout->max_channels;
  3316. if (mout->dig_out_nid) {
  3317. if (!mout->analog_rates) {
  3318. mout->analog_rates = hinfo->rates;
  3319. mout->analog_formats = hinfo->formats;
  3320. mout->analog_maxbps = hinfo->maxbps;
  3321. } else {
  3322. runtime->hw.rates = mout->analog_rates;
  3323. runtime->hw.formats = mout->analog_formats;
  3324. hinfo->maxbps = mout->analog_maxbps;
  3325. }
  3326. if (!mout->spdif_rates) {
  3327. snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
  3328. &mout->spdif_rates,
  3329. &mout->spdif_formats,
  3330. NULL,
  3331. &mout->spdif_maxbps);
  3332. }
  3333. guard(mutex)(&codec->spdif_mutex);
  3334. if (mout->share_spdif) {
  3335. if ((runtime->hw.rates & mout->spdif_rates) &&
  3336. (runtime->hw.formats & mout->spdif_formats)) {
  3337. runtime->hw.rates &= mout->spdif_rates;
  3338. runtime->hw.formats &= mout->spdif_formats;
  3339. if (mout->spdif_maxbps < hinfo->maxbps)
  3340. hinfo->maxbps = mout->spdif_maxbps;
  3341. } else {
  3342. mout->share_spdif = 0;
  3343. /* FIXME: need notify? */
  3344. }
  3345. }
  3346. }
  3347. return snd_pcm_hw_constraint_step(substream->runtime, 0,
  3348. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  3349. }
  3350. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open);
  3351. /**
  3352. * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
  3353. * @codec: the HDA codec
  3354. * @mout: hda_multi_out object
  3355. * @stream_tag: stream tag to assign
  3356. * @format: format id to assign
  3357. * @substream: PCM substream to assign
  3358. *
  3359. * Set up the i/o for analog out.
  3360. * When the digital out is available, copy the front out to digital out, too.
  3361. */
  3362. int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
  3363. struct hda_multi_out *mout,
  3364. unsigned int stream_tag,
  3365. unsigned int format,
  3366. struct snd_pcm_substream *substream)
  3367. {
  3368. const hda_nid_t *nids = mout->dac_nids;
  3369. int chs = substream->runtime->channels;
  3370. struct hda_spdif_out *spdif;
  3371. int i;
  3372. scoped_guard(mutex, &codec->spdif_mutex) {
  3373. spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
  3374. if (mout->dig_out_nid && mout->share_spdif &&
  3375. mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  3376. if (chs == 2 && spdif != NULL &&
  3377. snd_hda_is_supported_format(codec, mout->dig_out_nid,
  3378. format) &&
  3379. !(spdif->status & IEC958_AES0_NONAUDIO)) {
  3380. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  3381. setup_dig_out_stream(codec, mout->dig_out_nid,
  3382. stream_tag, format);
  3383. } else {
  3384. mout->dig_out_used = 0;
  3385. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3386. }
  3387. }
  3388. }
  3389. /* front */
  3390. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
  3391. 0, format);
  3392. if (!mout->no_share_stream &&
  3393. mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
  3394. /* headphone out will just decode front left/right (stereo) */
  3395. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
  3396. 0, format);
  3397. /* extra outputs copied from front */
  3398. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  3399. if (!mout->no_share_stream && mout->hp_out_nid[i])
  3400. snd_hda_codec_setup_stream(codec,
  3401. mout->hp_out_nid[i],
  3402. stream_tag, 0, format);
  3403. /* surrounds */
  3404. for (i = 1; i < mout->num_dacs; i++) {
  3405. if (chs >= (i + 1) * 2) /* independent out */
  3406. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3407. i * 2, format);
  3408. else if (!mout->no_share_stream) /* copy front */
  3409. snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
  3410. 0, format);
  3411. }
  3412. /* extra surrounds */
  3413. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) {
  3414. int ch = 0;
  3415. if (!mout->extra_out_nid[i])
  3416. break;
  3417. if (chs >= (i + 1) * 2)
  3418. ch = i * 2;
  3419. else if (!mout->no_share_stream)
  3420. break;
  3421. snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i],
  3422. stream_tag, ch, format);
  3423. }
  3424. return 0;
  3425. }
  3426. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare);
  3427. /**
  3428. * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
  3429. * @codec: the HDA codec
  3430. * @mout: hda_multi_out object
  3431. */
  3432. int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
  3433. struct hda_multi_out *mout)
  3434. {
  3435. const hda_nid_t *nids = mout->dac_nids;
  3436. int i;
  3437. for (i = 0; i < mout->num_dacs; i++)
  3438. snd_hda_codec_cleanup_stream(codec, nids[i]);
  3439. if (mout->hp_nid)
  3440. snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
  3441. for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
  3442. if (mout->hp_out_nid[i])
  3443. snd_hda_codec_cleanup_stream(codec,
  3444. mout->hp_out_nid[i]);
  3445. for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
  3446. if (mout->extra_out_nid[i])
  3447. snd_hda_codec_cleanup_stream(codec,
  3448. mout->extra_out_nid[i]);
  3449. guard(mutex)(&codec->spdif_mutex);
  3450. if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
  3451. cleanup_dig_out_stream(codec, mout->dig_out_nid);
  3452. mout->dig_out_used = 0;
  3453. }
  3454. return 0;
  3455. }
  3456. EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup);
  3457. /**
  3458. * snd_hda_get_default_vref - Get the default (mic) VREF pin bits
  3459. * @codec: the HDA codec
  3460. * @pin: referred pin NID
  3461. *
  3462. * Guess the suitable VREF pin bits to be set as the pin-control value.
  3463. * Note: the function doesn't set the AC_PINCTL_IN_EN bit.
  3464. */
  3465. unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin)
  3466. {
  3467. unsigned int pincap;
  3468. unsigned int oldval;
  3469. oldval = snd_hda_codec_read(codec, pin, 0,
  3470. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  3471. pincap = snd_hda_query_pin_caps(codec, pin);
  3472. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  3473. /* Exception: if the default pin setup is vref50, we give it priority */
  3474. if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50)
  3475. return AC_PINCTL_VREF_80;
  3476. else if (pincap & AC_PINCAP_VREF_50)
  3477. return AC_PINCTL_VREF_50;
  3478. else if (pincap & AC_PINCAP_VREF_100)
  3479. return AC_PINCTL_VREF_100;
  3480. else if (pincap & AC_PINCAP_VREF_GRD)
  3481. return AC_PINCTL_VREF_GRD;
  3482. return AC_PINCTL_VREF_HIZ;
  3483. }
  3484. EXPORT_SYMBOL_GPL(snd_hda_get_default_vref);
  3485. /**
  3486. * snd_hda_correct_pin_ctl - correct the pin ctl value for matching with the pin cap
  3487. * @codec: the HDA codec
  3488. * @pin: referred pin NID
  3489. * @val: pin ctl value to audit
  3490. */
  3491. unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec,
  3492. hda_nid_t pin, unsigned int val)
  3493. {
  3494. static const unsigned int cap_lists[][2] = {
  3495. { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 },
  3496. { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 },
  3497. { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 },
  3498. { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD },
  3499. };
  3500. unsigned int cap;
  3501. if (!val)
  3502. return 0;
  3503. cap = snd_hda_query_pin_caps(codec, pin);
  3504. if (!cap)
  3505. return val; /* don't know what to do... */
  3506. if (val & AC_PINCTL_OUT_EN) {
  3507. if (!(cap & AC_PINCAP_OUT))
  3508. val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  3509. else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV))
  3510. val &= ~AC_PINCTL_HP_EN;
  3511. }
  3512. if (val & AC_PINCTL_IN_EN) {
  3513. if (!(cap & AC_PINCAP_IN))
  3514. val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN);
  3515. else {
  3516. unsigned int vcap, vref;
  3517. int i;
  3518. vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  3519. vref = val & AC_PINCTL_VREFEN;
  3520. for (i = 0; i < ARRAY_SIZE(cap_lists); i++) {
  3521. if (vref == cap_lists[i][0] &&
  3522. !(vcap & cap_lists[i][1])) {
  3523. if (i == ARRAY_SIZE(cap_lists) - 1)
  3524. vref = AC_PINCTL_VREF_HIZ;
  3525. else
  3526. vref = cap_lists[i + 1][0];
  3527. }
  3528. }
  3529. val &= ~AC_PINCTL_VREFEN;
  3530. val |= vref;
  3531. }
  3532. }
  3533. return val;
  3534. }
  3535. EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl);
  3536. /**
  3537. * _snd_hda_set_pin_ctl - Helper to set pin ctl value
  3538. * @codec: the HDA codec
  3539. * @pin: referred pin NID
  3540. * @val: pin control value to set
  3541. * @cached: access over codec pinctl cache or direct write
  3542. *
  3543. * This function is a helper to set a pin ctl value more safely.
  3544. * It corrects the pin ctl value via snd_hda_correct_pin_ctl(), stores the
  3545. * value in pin target array via snd_hda_codec_set_pin_target(), then
  3546. * actually writes the value via either snd_hda_codec_write_cache() or
  3547. * snd_hda_codec_write() depending on @cached flag.
  3548. */
  3549. int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin,
  3550. unsigned int val, bool cached)
  3551. {
  3552. val = snd_hda_correct_pin_ctl(codec, pin, val);
  3553. snd_hda_codec_set_pin_target(codec, pin, val);
  3554. if (cached)
  3555. return snd_hda_codec_write_cache(codec, pin, 0,
  3556. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  3557. else
  3558. return snd_hda_codec_write(codec, pin, 0,
  3559. AC_VERB_SET_PIN_WIDGET_CONTROL, val);
  3560. }
  3561. EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl);
  3562. /**
  3563. * snd_hda_add_imux_item - Add an item to input_mux
  3564. * @codec: the HDA codec
  3565. * @imux: imux helper object
  3566. * @label: the name of imux item to assign
  3567. * @index: index number of imux item to assign
  3568. * @type_idx: pointer to store the resultant label index
  3569. *
  3570. * When the same label is used already in the existing items, the number
  3571. * suffix is appended to the label. This label index number is stored
  3572. * to type_idx when non-NULL pointer is given.
  3573. */
  3574. int snd_hda_add_imux_item(struct hda_codec *codec,
  3575. struct hda_input_mux *imux, const char *label,
  3576. int index, int *type_idx)
  3577. {
  3578. int i, label_idx = 0;
  3579. if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
  3580. codec_err(codec, "hda_codec: Too many imux items!\n");
  3581. return -EINVAL;
  3582. }
  3583. for (i = 0; i < imux->num_items; i++) {
  3584. if (!strncmp(label, imux->items[i].label, strlen(label)))
  3585. label_idx++;
  3586. }
  3587. if (type_idx)
  3588. *type_idx = label_idx;
  3589. if (label_idx > 0)
  3590. snprintf(imux->items[imux->num_items].label,
  3591. sizeof(imux->items[imux->num_items].label),
  3592. "%s %d", label, label_idx);
  3593. else
  3594. strscpy(imux->items[imux->num_items].label, label,
  3595. sizeof(imux->items[imux->num_items].label));
  3596. imux->items[imux->num_items].index = index;
  3597. imux->num_items++;
  3598. return 0;
  3599. }
  3600. EXPORT_SYMBOL_GPL(snd_hda_add_imux_item);
  3601. /**
  3602. * snd_hda_bus_reset_codecs - Reset the bus
  3603. * @bus: HD-audio bus
  3604. */
  3605. void snd_hda_bus_reset_codecs(struct hda_bus *bus)
  3606. {
  3607. struct hda_codec *codec;
  3608. list_for_each_codec(codec, bus) {
  3609. /* FIXME: maybe a better way needed for forced reset */
  3610. if (current_work() != &codec->jackpoll_work.work)
  3611. cancel_delayed_work_sync(&codec->jackpoll_work);
  3612. if (hda_codec_is_power_on(codec)) {
  3613. hda_call_codec_suspend(codec);
  3614. hda_call_codec_resume(codec);
  3615. }
  3616. }
  3617. }
  3618. /**
  3619. * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
  3620. * @pcm: PCM caps bits
  3621. * @buf: the string buffer to write
  3622. * @buflen: the max buffer length
  3623. *
  3624. * used by hda_proc.c and hda_eld.c
  3625. */
  3626. void snd_print_pcm_bits(int pcm, char *buf, int buflen)
  3627. {
  3628. static const unsigned int bits[] = { 8, 16, 20, 24, 32 };
  3629. int i, j;
  3630. for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
  3631. if (pcm & (AC_SUPPCM_BITS_8 << i))
  3632. j += scnprintf(buf + j, buflen - j, " %d", bits[i]);
  3633. buf[j] = '\0'; /* necessary when j == 0 */
  3634. }
  3635. EXPORT_SYMBOL_GPL(snd_print_pcm_bits);
  3636. MODULE_DESCRIPTION("HDA codec core");
  3637. MODULE_LICENSE("GPL");