hda_codec.c 111 KB

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