hda_codec.c 107 KB

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