hda_codec.c 107 KB

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