hda_codec.c 111 KB

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