hda_codec.c 108 KB

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