soc-dapm.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759
  1. /*
  2. * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. *
  12. * Features:
  13. * o Changes power status of internal codec blocks depending on the
  14. * dynamic configuration of codec internal audio paths and active
  15. * DACs/ADCs.
  16. * o Platform power domain - can support external components i.e. amps and
  17. * mic/headphone insertion events.
  18. * o Automatic Mic Bias support
  19. * o Jack insertion power event initiation - e.g. hp insertion will enable
  20. * sinks, dacs, etc
  21. * o Delayed power down of audio subsystem to reduce pops between a quick
  22. * device reopen.
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/init.h>
  28. #include <linux/async.h>
  29. #include <linux/delay.h>
  30. #include <linux/pm.h>
  31. #include <linux/bitops.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/jiffies.h>
  34. #include <linux/debugfs.h>
  35. #include <linux/pm_runtime.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/clk.h>
  38. #include <linux/slab.h>
  39. #include <sound/core.h>
  40. #include <sound/pcm.h>
  41. #include <sound/pcm_params.h>
  42. #include <sound/soc.h>
  43. #include <sound/initval.h>
  44. #include <trace/events/asoc.h>
  45. #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
  46. /* dapm power sequences - make this per codec in the future */
  47. static int dapm_up_seq[] = {
  48. [snd_soc_dapm_pre] = 0,
  49. [snd_soc_dapm_supply] = 1,
  50. [snd_soc_dapm_regulator_supply] = 1,
  51. [snd_soc_dapm_clock_supply] = 1,
  52. [snd_soc_dapm_micbias] = 2,
  53. [snd_soc_dapm_dai_link] = 2,
  54. [snd_soc_dapm_dai] = 3,
  55. [snd_soc_dapm_aif_in] = 3,
  56. [snd_soc_dapm_aif_out] = 3,
  57. [snd_soc_dapm_mic] = 4,
  58. [snd_soc_dapm_mux] = 5,
  59. [snd_soc_dapm_virt_mux] = 5,
  60. [snd_soc_dapm_value_mux] = 5,
  61. [snd_soc_dapm_dac] = 6,
  62. [snd_soc_dapm_mixer] = 7,
  63. [snd_soc_dapm_mixer_named_ctl] = 7,
  64. [snd_soc_dapm_pga] = 8,
  65. [snd_soc_dapm_adc] = 9,
  66. [snd_soc_dapm_out_drv] = 10,
  67. [snd_soc_dapm_hp] = 10,
  68. [snd_soc_dapm_spk] = 10,
  69. [snd_soc_dapm_line] = 10,
  70. [snd_soc_dapm_post] = 11,
  71. };
  72. static int dapm_down_seq[] = {
  73. [snd_soc_dapm_pre] = 0,
  74. [snd_soc_dapm_adc] = 1,
  75. [snd_soc_dapm_hp] = 2,
  76. [snd_soc_dapm_spk] = 2,
  77. [snd_soc_dapm_line] = 2,
  78. [snd_soc_dapm_out_drv] = 2,
  79. [snd_soc_dapm_pga] = 4,
  80. [snd_soc_dapm_mixer_named_ctl] = 5,
  81. [snd_soc_dapm_mixer] = 5,
  82. [snd_soc_dapm_dac] = 6,
  83. [snd_soc_dapm_mic] = 7,
  84. [snd_soc_dapm_micbias] = 8,
  85. [snd_soc_dapm_mux] = 9,
  86. [snd_soc_dapm_virt_mux] = 9,
  87. [snd_soc_dapm_value_mux] = 9,
  88. [snd_soc_dapm_aif_in] = 10,
  89. [snd_soc_dapm_aif_out] = 10,
  90. [snd_soc_dapm_dai] = 10,
  91. [snd_soc_dapm_dai_link] = 11,
  92. [snd_soc_dapm_clock_supply] = 12,
  93. [snd_soc_dapm_regulator_supply] = 12,
  94. [snd_soc_dapm_supply] = 12,
  95. [snd_soc_dapm_post] = 13,
  96. };
  97. static void pop_wait(u32 pop_time)
  98. {
  99. if (pop_time)
  100. schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  101. }
  102. static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
  103. {
  104. va_list args;
  105. char *buf;
  106. if (!pop_time)
  107. return;
  108. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  109. if (buf == NULL)
  110. return;
  111. va_start(args, fmt);
  112. vsnprintf(buf, PAGE_SIZE, fmt, args);
  113. dev_info(dev, "%s", buf);
  114. va_end(args);
  115. kfree(buf);
  116. }
  117. static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
  118. {
  119. return !list_empty(&w->dirty);
  120. }
  121. void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
  122. {
  123. if (!dapm_dirty_widget(w)) {
  124. dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
  125. w->name, reason);
  126. list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
  127. }
  128. }
  129. EXPORT_SYMBOL_GPL(dapm_mark_dirty);
  130. void dapm_mark_io_dirty(struct snd_soc_dapm_context *dapm)
  131. {
  132. struct snd_soc_card *card = dapm->card;
  133. struct snd_soc_dapm_widget *w;
  134. mutex_lock(&card->dapm_mutex);
  135. list_for_each_entry(w, &card->widgets, list) {
  136. switch (w->id) {
  137. case snd_soc_dapm_input:
  138. case snd_soc_dapm_output:
  139. dapm_mark_dirty(w, "Rechecking inputs and outputs");
  140. break;
  141. default:
  142. break;
  143. }
  144. }
  145. mutex_unlock(&card->dapm_mutex);
  146. }
  147. EXPORT_SYMBOL_GPL(dapm_mark_io_dirty);
  148. /* create a new dapm widget */
  149. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  150. const struct snd_soc_dapm_widget *_widget)
  151. {
  152. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  153. }
  154. /* get snd_card from DAPM context */
  155. static inline struct snd_card *dapm_get_snd_card(
  156. struct snd_soc_dapm_context *dapm)
  157. {
  158. if (dapm->codec)
  159. return dapm->codec->card->snd_card;
  160. else if (dapm->platform)
  161. return dapm->platform->card->snd_card;
  162. else
  163. BUG();
  164. /* unreachable */
  165. return NULL;
  166. }
  167. /* get soc_card from DAPM context */
  168. static inline struct snd_soc_card *dapm_get_soc_card(
  169. struct snd_soc_dapm_context *dapm)
  170. {
  171. if (dapm->codec)
  172. return dapm->codec->card;
  173. else if (dapm->platform)
  174. return dapm->platform->card;
  175. else
  176. BUG();
  177. /* unreachable */
  178. return NULL;
  179. }
  180. static void dapm_reset(struct snd_soc_card *card)
  181. {
  182. struct snd_soc_dapm_widget *w;
  183. memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
  184. list_for_each_entry(w, &card->widgets, list) {
  185. w->power_checked = false;
  186. w->inputs = -1;
  187. w->outputs = -1;
  188. }
  189. }
  190. static int soc_widget_read(struct snd_soc_dapm_widget *w, int reg)
  191. {
  192. if (w->codec)
  193. return snd_soc_read(w->codec, reg);
  194. else if (w->platform)
  195. return snd_soc_platform_read(w->platform, reg);
  196. dev_err(w->dapm->dev, "no valid widget read method\n");
  197. return -1;
  198. }
  199. static int soc_widget_write(struct snd_soc_dapm_widget *w, int reg, int val)
  200. {
  201. if (w->codec)
  202. return snd_soc_write(w->codec, reg, val);
  203. else if (w->platform)
  204. return snd_soc_platform_write(w->platform, reg, val);
  205. dev_err(w->dapm->dev, "no valid widget write method\n");
  206. return -1;
  207. }
  208. static inline void soc_widget_lock(struct snd_soc_dapm_widget *w)
  209. {
  210. if (w->codec && !w->codec->using_regmap)
  211. mutex_lock(&w->codec->mutex);
  212. else if (w->platform)
  213. mutex_lock(&w->platform->mutex);
  214. }
  215. static inline void soc_widget_unlock(struct snd_soc_dapm_widget *w)
  216. {
  217. if (w->codec && !w->codec->using_regmap)
  218. mutex_unlock(&w->codec->mutex);
  219. else if (w->platform)
  220. mutex_unlock(&w->platform->mutex);
  221. }
  222. static int soc_widget_update_bits_locked(struct snd_soc_dapm_widget *w,
  223. unsigned short reg, unsigned int mask, unsigned int value)
  224. {
  225. bool change;
  226. unsigned int old, new;
  227. int ret;
  228. if (w->codec && w->codec->using_regmap) {
  229. ret = regmap_update_bits_check(w->codec->control_data,
  230. reg, mask, value, &change);
  231. if (ret != 0)
  232. return ret;
  233. } else {
  234. soc_widget_lock(w);
  235. ret = soc_widget_read(w, reg);
  236. if (ret < 0) {
  237. soc_widget_unlock(w);
  238. return ret;
  239. }
  240. old = ret;
  241. new = (old & ~mask) | (value & mask);
  242. change = old != new;
  243. if (change) {
  244. ret = soc_widget_write(w, reg, new);
  245. if (ret < 0) {
  246. soc_widget_unlock(w);
  247. return ret;
  248. }
  249. }
  250. soc_widget_unlock(w);
  251. }
  252. return change;
  253. }
  254. /**
  255. * snd_soc_dapm_set_bias_level - set the bias level for the system
  256. * @dapm: DAPM context
  257. * @level: level to configure
  258. *
  259. * Configure the bias (power) levels for the SoC audio device.
  260. *
  261. * Returns 0 for success else error.
  262. */
  263. static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
  264. enum snd_soc_bias_level level)
  265. {
  266. struct snd_soc_card *card = dapm->card;
  267. int ret = 0;
  268. trace_snd_soc_bias_level_start(card, level);
  269. if (card && card->set_bias_level)
  270. ret = card->set_bias_level(card, dapm, level);
  271. if (ret != 0)
  272. goto out;
  273. if (dapm->codec) {
  274. if (dapm->codec->driver->set_bias_level)
  275. ret = dapm->codec->driver->set_bias_level(dapm->codec,
  276. level);
  277. else
  278. dapm->bias_level = level;
  279. } else if (!card || dapm != &card->dapm) {
  280. dapm->bias_level = level;
  281. }
  282. if (ret != 0)
  283. goto out;
  284. if (card && card->set_bias_level_post)
  285. ret = card->set_bias_level_post(card, dapm, level);
  286. out:
  287. trace_snd_soc_bias_level_done(card, level);
  288. return ret;
  289. }
  290. /* set up initial codec paths */
  291. static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
  292. struct snd_soc_dapm_path *p, int i)
  293. {
  294. switch (w->id) {
  295. case snd_soc_dapm_switch:
  296. case snd_soc_dapm_mixer:
  297. case snd_soc_dapm_mixer_named_ctl: {
  298. int val;
  299. struct soc_mixer_control *mc = (struct soc_mixer_control *)
  300. w->kcontrol_news[i].private_value;
  301. unsigned int reg = mc->reg;
  302. unsigned int shift = mc->shift;
  303. int max = mc->max;
  304. unsigned int mask = (1 << fls(max)) - 1;
  305. unsigned int invert = mc->invert;
  306. val = soc_widget_read(w, reg);
  307. val = (val >> shift) & mask;
  308. if (invert)
  309. val = max - val;
  310. p->connect = !!val;
  311. }
  312. break;
  313. case snd_soc_dapm_mux: {
  314. struct soc_enum *e = (struct soc_enum *)
  315. w->kcontrol_news[i].private_value;
  316. int val, item;
  317. val = soc_widget_read(w, e->reg);
  318. item = (val >> e->shift_l) & e->mask;
  319. p->connect = 0;
  320. for (i = 0; i < e->max; i++) {
  321. if (!(strcmp(p->name, e->texts[i])) && item == i)
  322. p->connect = 1;
  323. }
  324. }
  325. break;
  326. case snd_soc_dapm_virt_mux: {
  327. struct soc_enum *e = (struct soc_enum *)
  328. w->kcontrol_news[i].private_value;
  329. p->connect = 0;
  330. /* since a virtual mux has no backing registers to
  331. * decide which path to connect, it will try to match
  332. * with the first enumeration. This is to ensure
  333. * that the default mux choice (the first) will be
  334. * correctly powered up during initialization.
  335. */
  336. if (!strcmp(p->name, e->texts[0]))
  337. p->connect = 1;
  338. }
  339. break;
  340. case snd_soc_dapm_value_mux: {
  341. struct soc_enum *e = (struct soc_enum *)
  342. w->kcontrol_news[i].private_value;
  343. int val, item;
  344. val = soc_widget_read(w, e->reg);
  345. val = (val >> e->shift_l) & e->mask;
  346. for (item = 0; item < e->max; item++) {
  347. if (val == e->values[item])
  348. break;
  349. }
  350. p->connect = 0;
  351. for (i = 0; i < e->max; i++) {
  352. if (!(strcmp(p->name, e->texts[i])) && item == i)
  353. p->connect = 1;
  354. }
  355. }
  356. break;
  357. /* does not affect routing - always connected */
  358. case snd_soc_dapm_pga:
  359. case snd_soc_dapm_out_drv:
  360. case snd_soc_dapm_output:
  361. case snd_soc_dapm_adc:
  362. case snd_soc_dapm_input:
  363. case snd_soc_dapm_siggen:
  364. case snd_soc_dapm_dac:
  365. case snd_soc_dapm_micbias:
  366. case snd_soc_dapm_vmid:
  367. case snd_soc_dapm_supply:
  368. case snd_soc_dapm_regulator_supply:
  369. case snd_soc_dapm_clock_supply:
  370. case snd_soc_dapm_aif_in:
  371. case snd_soc_dapm_aif_out:
  372. case snd_soc_dapm_dai:
  373. case snd_soc_dapm_hp:
  374. case snd_soc_dapm_mic:
  375. case snd_soc_dapm_spk:
  376. case snd_soc_dapm_line:
  377. case snd_soc_dapm_dai_link:
  378. p->connect = 1;
  379. break;
  380. /* does affect routing - dynamically connected */
  381. case snd_soc_dapm_pre:
  382. case snd_soc_dapm_post:
  383. p->connect = 0;
  384. break;
  385. }
  386. }
  387. /* connect mux widget to its interconnecting audio paths */
  388. static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
  389. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  390. struct snd_soc_dapm_path *path, const char *control_name,
  391. const struct snd_kcontrol_new *kcontrol)
  392. {
  393. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  394. int i;
  395. for (i = 0; i < e->max; i++) {
  396. if (!(strcmp(control_name, e->texts[i]))) {
  397. list_add(&path->list, &dapm->card->paths);
  398. list_add(&path->list_sink, &dest->sources);
  399. list_add(&path->list_source, &src->sinks);
  400. path->name = (char*)e->texts[i];
  401. dapm_set_path_status(dest, path, 0);
  402. return 0;
  403. }
  404. }
  405. return -ENODEV;
  406. }
  407. /* connect mixer widget to its interconnecting audio paths */
  408. static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
  409. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  410. struct snd_soc_dapm_path *path, const char *control_name)
  411. {
  412. int i;
  413. /* search for mixer kcontrol */
  414. for (i = 0; i < dest->num_kcontrols; i++) {
  415. if (!strcmp(control_name, dest->kcontrol_news[i].name)) {
  416. list_add(&path->list, &dapm->card->paths);
  417. list_add(&path->list_sink, &dest->sources);
  418. list_add(&path->list_source, &src->sinks);
  419. path->name = dest->kcontrol_news[i].name;
  420. dapm_set_path_status(dest, path, i);
  421. return 0;
  422. }
  423. }
  424. return -ENODEV;
  425. }
  426. static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
  427. struct snd_soc_dapm_widget *kcontrolw,
  428. const struct snd_kcontrol_new *kcontrol_new,
  429. struct snd_kcontrol **kcontrol)
  430. {
  431. struct snd_soc_dapm_widget *w;
  432. int i;
  433. *kcontrol = NULL;
  434. list_for_each_entry(w, &dapm->card->widgets, list) {
  435. if (w == kcontrolw || w->dapm != kcontrolw->dapm)
  436. continue;
  437. for (i = 0; i < w->num_kcontrols; i++) {
  438. if (&w->kcontrol_news[i] == kcontrol_new) {
  439. if (w->kcontrols)
  440. *kcontrol = w->kcontrols[i];
  441. return 1;
  442. }
  443. }
  444. }
  445. return 0;
  446. }
  447. /* create new dapm mixer control */
  448. static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
  449. {
  450. struct snd_soc_dapm_context *dapm = w->dapm;
  451. int i, ret = 0;
  452. size_t name_len, prefix_len;
  453. struct snd_soc_dapm_path *path;
  454. struct snd_card *card = dapm->card->snd_card;
  455. const char *prefix;
  456. struct snd_soc_dapm_widget_list *wlist;
  457. size_t wlistsize;
  458. if (dapm->codec)
  459. prefix = dapm->codec->name_prefix;
  460. else
  461. prefix = NULL;
  462. if (prefix)
  463. prefix_len = strlen(prefix) + 1;
  464. else
  465. prefix_len = 0;
  466. /* add kcontrol */
  467. for (i = 0; i < w->num_kcontrols; i++) {
  468. /* match name */
  469. list_for_each_entry(path, &w->sources, list_sink) {
  470. /* mixer/mux paths name must match control name */
  471. if (path->name != (char *)w->kcontrol_news[i].name)
  472. continue;
  473. if (w->kcontrols[i]) {
  474. path->kcontrol = w->kcontrols[i];
  475. continue;
  476. }
  477. wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
  478. sizeof(struct snd_soc_dapm_widget *),
  479. wlist = kzalloc(wlistsize, GFP_KERNEL);
  480. if (wlist == NULL) {
  481. dev_err(dapm->dev,
  482. "asoc: can't allocate widget list for %s\n",
  483. w->name);
  484. return -ENOMEM;
  485. }
  486. wlist->num_widgets = 1;
  487. wlist->widgets[0] = w;
  488. /* add dapm control with long name.
  489. * for dapm_mixer this is the concatenation of the
  490. * mixer and kcontrol name.
  491. * for dapm_mixer_named_ctl this is simply the
  492. * kcontrol name.
  493. */
  494. name_len = strlen(w->kcontrol_news[i].name) + 1;
  495. if (w->id != snd_soc_dapm_mixer_named_ctl)
  496. name_len += 1 + strlen(w->name);
  497. path->long_name = kmalloc(name_len, GFP_KERNEL);
  498. if (path->long_name == NULL) {
  499. kfree(wlist);
  500. return -ENOMEM;
  501. }
  502. switch (w->id) {
  503. default:
  504. /* The control will get a prefix from
  505. * the control creation process but
  506. * we're also using the same prefix
  507. * for widgets so cut the prefix off
  508. * the front of the widget name.
  509. */
  510. snprintf((char *)path->long_name, name_len,
  511. "%s %s", w->name + prefix_len,
  512. w->kcontrol_news[i].name);
  513. break;
  514. case snd_soc_dapm_mixer_named_ctl:
  515. snprintf((char *)path->long_name, name_len,
  516. "%s", w->kcontrol_news[i].name);
  517. break;
  518. }
  519. ((char *)path->long_name)[name_len - 1] = '\0';
  520. path->kcontrol = snd_soc_cnew(&w->kcontrol_news[i],
  521. wlist, path->long_name,
  522. prefix);
  523. ret = snd_ctl_add(card, path->kcontrol);
  524. if (ret < 0) {
  525. dev_err(dapm->dev,
  526. "asoc: failed to add dapm kcontrol %s: %d\n",
  527. path->long_name, ret);
  528. kfree(wlist);
  529. kfree(path->long_name);
  530. path->long_name = NULL;
  531. return ret;
  532. }
  533. w->kcontrols[i] = path->kcontrol;
  534. }
  535. }
  536. return ret;
  537. }
  538. /* create new dapm mux control */
  539. static int dapm_new_mux(struct snd_soc_dapm_widget *w)
  540. {
  541. struct snd_soc_dapm_context *dapm = w->dapm;
  542. struct snd_soc_dapm_path *path = NULL;
  543. struct snd_kcontrol *kcontrol;
  544. struct snd_card *card = dapm->card->snd_card;
  545. const char *prefix;
  546. size_t prefix_len;
  547. int ret;
  548. struct snd_soc_dapm_widget_list *wlist;
  549. int shared, wlistentries;
  550. size_t wlistsize;
  551. const char *name;
  552. if (w->num_kcontrols != 1) {
  553. dev_err(dapm->dev,
  554. "asoc: mux %s has incorrect number of controls\n",
  555. w->name);
  556. return -EINVAL;
  557. }
  558. shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[0],
  559. &kcontrol);
  560. if (kcontrol) {
  561. wlist = kcontrol->private_data;
  562. wlistentries = wlist->num_widgets + 1;
  563. } else {
  564. wlist = NULL;
  565. wlistentries = 1;
  566. }
  567. wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
  568. wlistentries * sizeof(struct snd_soc_dapm_widget *),
  569. wlist = krealloc(wlist, wlistsize, GFP_KERNEL);
  570. if (wlist == NULL) {
  571. dev_err(dapm->dev,
  572. "asoc: can't allocate widget list for %s\n", w->name);
  573. return -ENOMEM;
  574. }
  575. wlist->num_widgets = wlistentries;
  576. wlist->widgets[wlistentries - 1] = w;
  577. if (!kcontrol) {
  578. if (dapm->codec)
  579. prefix = dapm->codec->name_prefix;
  580. else
  581. prefix = NULL;
  582. if (shared) {
  583. name = w->kcontrol_news[0].name;
  584. prefix_len = 0;
  585. } else {
  586. name = w->name;
  587. if (prefix)
  588. prefix_len = strlen(prefix) + 1;
  589. else
  590. prefix_len = 0;
  591. }
  592. /*
  593. * The control will get a prefix from the control creation
  594. * process but we're also using the same prefix for widgets so
  595. * cut the prefix off the front of the widget name.
  596. */
  597. kcontrol = snd_soc_cnew(&w->kcontrol_news[0], wlist,
  598. name + prefix_len, prefix);
  599. ret = snd_ctl_add(card, kcontrol);
  600. if (ret < 0) {
  601. dev_err(dapm->dev, "failed to add kcontrol %s: %d\n",
  602. w->name, ret);
  603. kfree(wlist);
  604. return ret;
  605. }
  606. }
  607. kcontrol->private_data = wlist;
  608. w->kcontrols[0] = kcontrol;
  609. list_for_each_entry(path, &w->sources, list_sink)
  610. path->kcontrol = kcontrol;
  611. return 0;
  612. }
  613. /* create new dapm volume control */
  614. static int dapm_new_pga(struct snd_soc_dapm_widget *w)
  615. {
  616. if (w->num_kcontrols)
  617. dev_err(w->dapm->dev,
  618. "asoc: PGA controls not supported: '%s'\n", w->name);
  619. return 0;
  620. }
  621. /* reset 'walked' bit for each dapm path */
  622. static inline void dapm_clear_walk(struct snd_soc_dapm_context *dapm)
  623. {
  624. struct snd_soc_dapm_path *p;
  625. list_for_each_entry(p, &dapm->card->paths, list)
  626. p->walked = 0;
  627. }
  628. /* We implement power down on suspend by checking the power state of
  629. * the ALSA card - when we are suspending the ALSA state for the card
  630. * is set to D3.
  631. */
  632. static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
  633. {
  634. int level = snd_power_get_state(widget->dapm->card->snd_card);
  635. switch (level) {
  636. case SNDRV_CTL_POWER_D3hot:
  637. case SNDRV_CTL_POWER_D3cold:
  638. if (widget->ignore_suspend)
  639. dev_dbg(widget->dapm->dev, "%s ignoring suspend\n",
  640. widget->name);
  641. return widget->ignore_suspend;
  642. default:
  643. return 1;
  644. }
  645. }
  646. /* add widget to list if it's not already in the list */
  647. static int dapm_list_add_widget(struct snd_soc_dapm_widget_list **list,
  648. struct snd_soc_dapm_widget *w)
  649. {
  650. struct snd_soc_dapm_widget_list *wlist;
  651. int wlistsize, wlistentries, i;
  652. if (*list == NULL)
  653. return -EINVAL;
  654. wlist = *list;
  655. /* is this widget already in the list */
  656. for (i = 0; i < wlist->num_widgets; i++) {
  657. if (wlist->widgets[i] == w)
  658. return 0;
  659. }
  660. /* allocate some new space */
  661. wlistentries = wlist->num_widgets + 1;
  662. wlistsize = sizeof(struct snd_soc_dapm_widget_list) +
  663. wlistentries * sizeof(struct snd_soc_dapm_widget *);
  664. *list = krealloc(wlist, wlistsize, GFP_KERNEL);
  665. if (*list == NULL) {
  666. dev_err(w->dapm->dev, "can't allocate widget list for %s\n",
  667. w->name);
  668. return -ENOMEM;
  669. }
  670. wlist = *list;
  671. /* insert the widget */
  672. dev_dbg(w->dapm->dev, "added %s in widget list pos %d\n",
  673. w->name, wlist->num_widgets);
  674. wlist->widgets[wlist->num_widgets] = w;
  675. wlist->num_widgets++;
  676. return 1;
  677. }
  678. /*
  679. * Recursively check for a completed path to an active or physically connected
  680. * output widget. Returns number of complete paths.
  681. */
  682. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
  683. struct snd_soc_dapm_widget_list **list)
  684. {
  685. struct snd_soc_dapm_path *path;
  686. int con = 0;
  687. if (widget->outputs >= 0)
  688. return widget->outputs;
  689. DAPM_UPDATE_STAT(widget, path_checks);
  690. switch (widget->id) {
  691. case snd_soc_dapm_supply:
  692. case snd_soc_dapm_regulator_supply:
  693. case snd_soc_dapm_clock_supply:
  694. return 0;
  695. default:
  696. break;
  697. }
  698. switch (widget->id) {
  699. case snd_soc_dapm_adc:
  700. case snd_soc_dapm_aif_out:
  701. case snd_soc_dapm_dai:
  702. if (widget->active) {
  703. widget->outputs = snd_soc_dapm_suspend_check(widget);
  704. return widget->outputs;
  705. }
  706. default:
  707. break;
  708. }
  709. if (widget->connected) {
  710. /* connected pin ? */
  711. if (widget->id == snd_soc_dapm_output && !widget->ext) {
  712. widget->outputs = snd_soc_dapm_suspend_check(widget);
  713. return widget->outputs;
  714. }
  715. /* connected jack or spk ? */
  716. if (widget->id == snd_soc_dapm_hp ||
  717. widget->id == snd_soc_dapm_spk ||
  718. (widget->id == snd_soc_dapm_line &&
  719. !list_empty(&widget->sources))) {
  720. widget->outputs = snd_soc_dapm_suspend_check(widget);
  721. return widget->outputs;
  722. }
  723. }
  724. list_for_each_entry(path, &widget->sinks, list_source) {
  725. DAPM_UPDATE_STAT(widget, neighbour_checks);
  726. if (path->weak)
  727. continue;
  728. if (path->walked)
  729. continue;
  730. trace_snd_soc_dapm_output_path(widget, path);
  731. if (path->sink && path->connect) {
  732. path->walked = 1;
  733. /* do we need to add this widget to the list ? */
  734. if (list) {
  735. int err;
  736. err = dapm_list_add_widget(list, path->sink);
  737. if (err < 0) {
  738. dev_err(widget->dapm->dev, "could not add widget %s\n",
  739. widget->name);
  740. return con;
  741. }
  742. }
  743. con += is_connected_output_ep(path->sink, list);
  744. }
  745. }
  746. widget->outputs = con;
  747. return con;
  748. }
  749. /*
  750. * Recursively check for a completed path to an active or physically connected
  751. * input widget. Returns number of complete paths.
  752. */
  753. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
  754. struct snd_soc_dapm_widget_list **list)
  755. {
  756. struct snd_soc_dapm_path *path;
  757. int con = 0;
  758. if (widget->inputs >= 0)
  759. return widget->inputs;
  760. DAPM_UPDATE_STAT(widget, path_checks);
  761. switch (widget->id) {
  762. case snd_soc_dapm_supply:
  763. case snd_soc_dapm_regulator_supply:
  764. case snd_soc_dapm_clock_supply:
  765. return 0;
  766. default:
  767. break;
  768. }
  769. /* active stream ? */
  770. switch (widget->id) {
  771. case snd_soc_dapm_dac:
  772. case snd_soc_dapm_aif_in:
  773. case snd_soc_dapm_dai:
  774. if (widget->active) {
  775. widget->inputs = snd_soc_dapm_suspend_check(widget);
  776. return widget->inputs;
  777. }
  778. default:
  779. break;
  780. }
  781. if (widget->connected) {
  782. /* connected pin ? */
  783. if (widget->id == snd_soc_dapm_input && !widget->ext) {
  784. widget->inputs = snd_soc_dapm_suspend_check(widget);
  785. return widget->inputs;
  786. }
  787. /* connected VMID/Bias for lower pops */
  788. if (widget->id == snd_soc_dapm_vmid) {
  789. widget->inputs = snd_soc_dapm_suspend_check(widget);
  790. return widget->inputs;
  791. }
  792. /* connected jack ? */
  793. if (widget->id == snd_soc_dapm_mic ||
  794. (widget->id == snd_soc_dapm_line &&
  795. !list_empty(&widget->sinks))) {
  796. widget->inputs = snd_soc_dapm_suspend_check(widget);
  797. return widget->inputs;
  798. }
  799. /* signal generator */
  800. if (widget->id == snd_soc_dapm_siggen) {
  801. widget->inputs = snd_soc_dapm_suspend_check(widget);
  802. return widget->inputs;
  803. }
  804. }
  805. list_for_each_entry(path, &widget->sources, list_sink) {
  806. DAPM_UPDATE_STAT(widget, neighbour_checks);
  807. if (path->weak)
  808. continue;
  809. if (path->walked)
  810. continue;
  811. trace_snd_soc_dapm_input_path(widget, path);
  812. if (path->source && path->connect) {
  813. path->walked = 1;
  814. /* do we need to add this widget to the list ? */
  815. if (list) {
  816. int err;
  817. err = dapm_list_add_widget(list, path->source);
  818. if (err < 0) {
  819. dev_err(widget->dapm->dev, "could not add widget %s\n",
  820. widget->name);
  821. return con;
  822. }
  823. }
  824. con += is_connected_input_ep(path->source, list);
  825. }
  826. }
  827. widget->inputs = con;
  828. return con;
  829. }
  830. /**
  831. * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
  832. * @dai: the soc DAI.
  833. * @stream: stream direction.
  834. * @list: list of active widgets for this stream.
  835. *
  836. * Queries DAPM graph as to whether an valid audio stream path exists for
  837. * the initial stream specified by name. This takes into account
  838. * current mixer and mux kcontrol settings. Creates list of valid widgets.
  839. *
  840. * Returns the number of valid paths or negative error.
  841. */
  842. int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
  843. struct snd_soc_dapm_widget_list **list)
  844. {
  845. struct snd_soc_card *card = dai->card;
  846. int paths;
  847. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  848. dapm_reset(card);
  849. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  850. paths = is_connected_output_ep(dai->playback_widget, list);
  851. else
  852. paths = is_connected_input_ep(dai->capture_widget, list);
  853. trace_snd_soc_dapm_connected(paths, stream);
  854. dapm_clear_walk(&card->dapm);
  855. mutex_unlock(&card->dapm_mutex);
  856. return paths;
  857. }
  858. /*
  859. * Handler for generic register modifier widget.
  860. */
  861. int dapm_reg_event(struct snd_soc_dapm_widget *w,
  862. struct snd_kcontrol *kcontrol, int event)
  863. {
  864. unsigned int val;
  865. if (SND_SOC_DAPM_EVENT_ON(event))
  866. val = w->on_val;
  867. else
  868. val = w->off_val;
  869. soc_widget_update_bits_locked(w, -(w->reg + 1),
  870. w->mask << w->shift, val << w->shift);
  871. return 0;
  872. }
  873. EXPORT_SYMBOL_GPL(dapm_reg_event);
  874. /*
  875. * Handler for regulator supply widget.
  876. */
  877. int dapm_regulator_event(struct snd_soc_dapm_widget *w,
  878. struct snd_kcontrol *kcontrol, int event)
  879. {
  880. int ret;
  881. if (SND_SOC_DAPM_EVENT_ON(event)) {
  882. if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
  883. ret = regulator_allow_bypass(w->regulator, true);
  884. if (ret != 0)
  885. dev_warn(w->dapm->dev,
  886. "Failed to bypass %s: %d\n",
  887. w->name, ret);
  888. }
  889. return regulator_enable(w->regulator);
  890. } else {
  891. if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
  892. ret = regulator_allow_bypass(w->regulator, false);
  893. if (ret != 0)
  894. dev_warn(w->dapm->dev,
  895. "Failed to unbypass %s: %d\n",
  896. w->name, ret);
  897. }
  898. return regulator_disable_deferred(w->regulator, w->shift);
  899. }
  900. }
  901. EXPORT_SYMBOL_GPL(dapm_regulator_event);
  902. /*
  903. * Handler for clock supply widget.
  904. */
  905. int dapm_clock_event(struct snd_soc_dapm_widget *w,
  906. struct snd_kcontrol *kcontrol, int event)
  907. {
  908. if (!w->clk)
  909. return -EIO;
  910. #ifdef CONFIG_HAVE_CLK
  911. if (SND_SOC_DAPM_EVENT_ON(event)) {
  912. return clk_enable(w->clk);
  913. } else {
  914. clk_disable(w->clk);
  915. return 0;
  916. }
  917. #endif
  918. return 0;
  919. }
  920. EXPORT_SYMBOL_GPL(dapm_clock_event);
  921. static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
  922. {
  923. if (w->power_checked)
  924. return w->new_power;
  925. if (w->force)
  926. w->new_power = 1;
  927. else
  928. w->new_power = w->power_check(w);
  929. w->power_checked = true;
  930. return w->new_power;
  931. }
  932. /* Generic check to see if a widget should be powered.
  933. */
  934. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  935. {
  936. int in, out;
  937. DAPM_UPDATE_STAT(w, power_checks);
  938. in = is_connected_input_ep(w, NULL);
  939. dapm_clear_walk(w->dapm);
  940. out = is_connected_output_ep(w, NULL);
  941. dapm_clear_walk(w->dapm);
  942. return out != 0 && in != 0;
  943. }
  944. static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
  945. {
  946. DAPM_UPDATE_STAT(w, power_checks);
  947. if (w->active)
  948. return w->active;
  949. return dapm_generic_check_power(w);
  950. }
  951. /* Check to see if an ADC has power */
  952. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  953. {
  954. int in;
  955. DAPM_UPDATE_STAT(w, power_checks);
  956. if (w->active) {
  957. in = is_connected_input_ep(w, NULL);
  958. dapm_clear_walk(w->dapm);
  959. return in != 0;
  960. } else {
  961. return dapm_generic_check_power(w);
  962. }
  963. }
  964. /* Check to see if a DAC has power */
  965. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  966. {
  967. int out;
  968. DAPM_UPDATE_STAT(w, power_checks);
  969. if (w->active) {
  970. out = is_connected_output_ep(w, NULL);
  971. dapm_clear_walk(w->dapm);
  972. return out != 0;
  973. } else {
  974. return dapm_generic_check_power(w);
  975. }
  976. }
  977. /* Check to see if a power supply is needed */
  978. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  979. {
  980. struct snd_soc_dapm_path *path;
  981. DAPM_UPDATE_STAT(w, power_checks);
  982. /* Check if one of our outputs is connected */
  983. list_for_each_entry(path, &w->sinks, list_source) {
  984. DAPM_UPDATE_STAT(w, neighbour_checks);
  985. if (path->weak)
  986. continue;
  987. if (path->connected &&
  988. !path->connected(path->source, path->sink))
  989. continue;
  990. if (!path->sink)
  991. continue;
  992. if (dapm_widget_power_check(path->sink))
  993. return 1;
  994. }
  995. dapm_clear_walk(w->dapm);
  996. return 0;
  997. }
  998. static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
  999. {
  1000. return 1;
  1001. }
  1002. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  1003. struct snd_soc_dapm_widget *b,
  1004. bool power_up)
  1005. {
  1006. int *sort;
  1007. if (power_up)
  1008. sort = dapm_up_seq;
  1009. else
  1010. sort = dapm_down_seq;
  1011. if (sort[a->id] != sort[b->id])
  1012. return sort[a->id] - sort[b->id];
  1013. if (a->subseq != b->subseq) {
  1014. if (power_up)
  1015. return a->subseq - b->subseq;
  1016. else
  1017. return b->subseq - a->subseq;
  1018. }
  1019. if (a->reg != b->reg)
  1020. return a->reg - b->reg;
  1021. if (a->dapm != b->dapm)
  1022. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  1023. return 0;
  1024. }
  1025. /* Insert a widget in order into a DAPM power sequence. */
  1026. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  1027. struct list_head *list,
  1028. bool power_up)
  1029. {
  1030. struct snd_soc_dapm_widget *w;
  1031. list_for_each_entry(w, list, power_list)
  1032. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  1033. list_add_tail(&new_widget->power_list, &w->power_list);
  1034. return;
  1035. }
  1036. list_add_tail(&new_widget->power_list, list);
  1037. }
  1038. static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
  1039. struct snd_soc_dapm_widget *w, int event)
  1040. {
  1041. struct snd_soc_card *card = dapm->card;
  1042. const char *ev_name;
  1043. int power, ret;
  1044. switch (event) {
  1045. case SND_SOC_DAPM_PRE_PMU:
  1046. ev_name = "PRE_PMU";
  1047. power = 1;
  1048. break;
  1049. case SND_SOC_DAPM_POST_PMU:
  1050. ev_name = "POST_PMU";
  1051. power = 1;
  1052. break;
  1053. case SND_SOC_DAPM_PRE_PMD:
  1054. ev_name = "PRE_PMD";
  1055. power = 0;
  1056. break;
  1057. case SND_SOC_DAPM_POST_PMD:
  1058. ev_name = "POST_PMD";
  1059. power = 0;
  1060. break;
  1061. default:
  1062. BUG();
  1063. return;
  1064. }
  1065. if (w->power != power)
  1066. return;
  1067. if (w->event && (w->event_flags & event)) {
  1068. pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
  1069. w->name, ev_name);
  1070. trace_snd_soc_dapm_widget_event_start(w, event);
  1071. ret = w->event(w, NULL, event);
  1072. trace_snd_soc_dapm_widget_event_done(w, event);
  1073. if (ret < 0)
  1074. pr_err("%s: %s event failed: %d\n",
  1075. ev_name, w->name, ret);
  1076. }
  1077. }
  1078. /* Apply the coalesced changes from a DAPM sequence */
  1079. static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
  1080. struct list_head *pending)
  1081. {
  1082. struct snd_soc_card *card = dapm->card;
  1083. struct snd_soc_dapm_widget *w;
  1084. int reg, power;
  1085. unsigned int value = 0;
  1086. unsigned int mask = 0;
  1087. unsigned int cur_mask;
  1088. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  1089. power_list)->reg;
  1090. list_for_each_entry(w, pending, power_list) {
  1091. cur_mask = 1 << w->shift;
  1092. BUG_ON(reg != w->reg);
  1093. if (w->invert)
  1094. power = !w->power;
  1095. else
  1096. power = w->power;
  1097. mask |= cur_mask;
  1098. if (power)
  1099. value |= cur_mask;
  1100. pop_dbg(dapm->dev, card->pop_time,
  1101. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  1102. w->name, reg, value, mask);
  1103. /* Check for events */
  1104. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
  1105. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
  1106. }
  1107. if (reg >= 0) {
  1108. /* Any widget will do, they should all be updating the
  1109. * same register.
  1110. */
  1111. w = list_first_entry(pending, struct snd_soc_dapm_widget,
  1112. power_list);
  1113. pop_dbg(dapm->dev, card->pop_time,
  1114. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  1115. value, mask, reg, card->pop_time);
  1116. pop_wait(card->pop_time);
  1117. soc_widget_update_bits_locked(w, reg, mask, value);
  1118. }
  1119. list_for_each_entry(w, pending, power_list) {
  1120. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
  1121. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
  1122. }
  1123. }
  1124. /* Apply a DAPM power sequence.
  1125. *
  1126. * We walk over a pre-sorted list of widgets to apply power to. In
  1127. * order to minimise the number of writes to the device required
  1128. * multiple widgets will be updated in a single write where possible.
  1129. * Currently anything that requires more than a single write is not
  1130. * handled.
  1131. */
  1132. static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
  1133. struct list_head *list, int event, bool power_up)
  1134. {
  1135. struct snd_soc_dapm_widget *w, *n;
  1136. LIST_HEAD(pending);
  1137. int cur_sort = -1;
  1138. int cur_subseq = -1;
  1139. int cur_reg = SND_SOC_NOPM;
  1140. struct snd_soc_dapm_context *cur_dapm = NULL;
  1141. int ret, i;
  1142. int *sort;
  1143. if (power_up)
  1144. sort = dapm_up_seq;
  1145. else
  1146. sort = dapm_down_seq;
  1147. list_for_each_entry_safe(w, n, list, power_list) {
  1148. ret = 0;
  1149. /* Do we need to apply any queued changes? */
  1150. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  1151. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  1152. if (!list_empty(&pending))
  1153. dapm_seq_run_coalesced(cur_dapm, &pending);
  1154. if (cur_dapm && cur_dapm->seq_notifier) {
  1155. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1156. if (sort[i] == cur_sort)
  1157. cur_dapm->seq_notifier(cur_dapm,
  1158. i,
  1159. cur_subseq);
  1160. }
  1161. INIT_LIST_HEAD(&pending);
  1162. cur_sort = -1;
  1163. cur_subseq = INT_MIN;
  1164. cur_reg = SND_SOC_NOPM;
  1165. cur_dapm = NULL;
  1166. }
  1167. switch (w->id) {
  1168. case snd_soc_dapm_pre:
  1169. if (!w->event)
  1170. list_for_each_entry_safe_continue(w, n, list,
  1171. power_list);
  1172. if (event == SND_SOC_DAPM_STREAM_START)
  1173. ret = w->event(w,
  1174. NULL, SND_SOC_DAPM_PRE_PMU);
  1175. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1176. ret = w->event(w,
  1177. NULL, SND_SOC_DAPM_PRE_PMD);
  1178. break;
  1179. case snd_soc_dapm_post:
  1180. if (!w->event)
  1181. list_for_each_entry_safe_continue(w, n, list,
  1182. power_list);
  1183. if (event == SND_SOC_DAPM_STREAM_START)
  1184. ret = w->event(w,
  1185. NULL, SND_SOC_DAPM_POST_PMU);
  1186. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1187. ret = w->event(w,
  1188. NULL, SND_SOC_DAPM_POST_PMD);
  1189. break;
  1190. default:
  1191. /* Queue it up for application */
  1192. cur_sort = sort[w->id];
  1193. cur_subseq = w->subseq;
  1194. cur_reg = w->reg;
  1195. cur_dapm = w->dapm;
  1196. list_move(&w->power_list, &pending);
  1197. break;
  1198. }
  1199. if (ret < 0)
  1200. dev_err(w->dapm->dev,
  1201. "Failed to apply widget power: %d\n", ret);
  1202. }
  1203. if (!list_empty(&pending))
  1204. dapm_seq_run_coalesced(cur_dapm, &pending);
  1205. if (cur_dapm && cur_dapm->seq_notifier) {
  1206. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1207. if (sort[i] == cur_sort)
  1208. cur_dapm->seq_notifier(cur_dapm,
  1209. i, cur_subseq);
  1210. }
  1211. }
  1212. static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
  1213. {
  1214. struct snd_soc_dapm_update *update = dapm->update;
  1215. struct snd_soc_dapm_widget *w;
  1216. int ret;
  1217. if (!update)
  1218. return;
  1219. w = update->widget;
  1220. if (w->event &&
  1221. (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  1222. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  1223. if (ret != 0)
  1224. pr_err("%s DAPM pre-event failed: %d\n",
  1225. w->name, ret);
  1226. }
  1227. ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
  1228. update->val);
  1229. if (ret < 0)
  1230. pr_err("%s DAPM update failed: %d\n", w->name, ret);
  1231. if (w->event &&
  1232. (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  1233. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  1234. if (ret != 0)
  1235. pr_err("%s DAPM post-event failed: %d\n",
  1236. w->name, ret);
  1237. }
  1238. }
  1239. /* Async callback run prior to DAPM sequences - brings to _PREPARE if
  1240. * they're changing state.
  1241. */
  1242. static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
  1243. {
  1244. struct snd_soc_dapm_context *d = data;
  1245. int ret;
  1246. /* If we're off and we're not supposed to be go into STANDBY */
  1247. if (d->bias_level == SND_SOC_BIAS_OFF &&
  1248. d->target_bias_level != SND_SOC_BIAS_OFF) {
  1249. if (d->dev)
  1250. pm_runtime_get_sync(d->dev);
  1251. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1252. if (ret != 0)
  1253. dev_err(d->dev,
  1254. "Failed to turn on bias: %d\n", ret);
  1255. }
  1256. /* Prepare for a STADDBY->ON or ON->STANDBY transition */
  1257. if (d->bias_level != d->target_bias_level) {
  1258. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
  1259. if (ret != 0)
  1260. dev_err(d->dev,
  1261. "Failed to prepare bias: %d\n", ret);
  1262. }
  1263. }
  1264. /* Async callback run prior to DAPM sequences - brings to their final
  1265. * state.
  1266. */
  1267. static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
  1268. {
  1269. struct snd_soc_dapm_context *d = data;
  1270. int ret;
  1271. /* If we just powered the last thing off drop to standby bias */
  1272. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1273. (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
  1274. d->target_bias_level == SND_SOC_BIAS_OFF)) {
  1275. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1276. if (ret != 0)
  1277. dev_err(d->dev, "Failed to apply standby bias: %d\n",
  1278. ret);
  1279. }
  1280. /* If we're in standby and can support bias off then do that */
  1281. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  1282. d->target_bias_level == SND_SOC_BIAS_OFF) {
  1283. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
  1284. if (ret != 0)
  1285. dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
  1286. if (d->dev)
  1287. pm_runtime_put(d->dev);
  1288. }
  1289. /* If we just powered up then move to active bias */
  1290. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1291. d->target_bias_level == SND_SOC_BIAS_ON) {
  1292. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
  1293. if (ret != 0)
  1294. dev_err(d->dev, "Failed to apply active bias: %d\n",
  1295. ret);
  1296. }
  1297. }
  1298. static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
  1299. bool power, bool connect)
  1300. {
  1301. /* If a connection is being made or broken then that update
  1302. * will have marked the peer dirty, otherwise the widgets are
  1303. * not connected and this update has no impact. */
  1304. if (!connect)
  1305. return;
  1306. /* If the peer is already in the state we're moving to then we
  1307. * won't have an impact on it. */
  1308. if (power != peer->power)
  1309. dapm_mark_dirty(peer, "peer state change");
  1310. }
  1311. static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
  1312. struct list_head *up_list,
  1313. struct list_head *down_list)
  1314. {
  1315. struct snd_soc_dapm_path *path;
  1316. if (w->power == power)
  1317. return;
  1318. trace_snd_soc_dapm_widget_power(w, power);
  1319. /* If we changed our power state perhaps our neigbours changed
  1320. * also.
  1321. */
  1322. list_for_each_entry(path, &w->sources, list_sink) {
  1323. if (path->source) {
  1324. dapm_widget_set_peer_power(path->source, power,
  1325. path->connect);
  1326. }
  1327. }
  1328. switch (w->id) {
  1329. case snd_soc_dapm_supply:
  1330. case snd_soc_dapm_regulator_supply:
  1331. case snd_soc_dapm_clock_supply:
  1332. /* Supplies can't affect their outputs, only their inputs */
  1333. break;
  1334. default:
  1335. list_for_each_entry(path, &w->sinks, list_source) {
  1336. if (path->sink) {
  1337. dapm_widget_set_peer_power(path->sink, power,
  1338. path->connect);
  1339. }
  1340. }
  1341. break;
  1342. }
  1343. if (power)
  1344. dapm_seq_insert(w, up_list, true);
  1345. else
  1346. dapm_seq_insert(w, down_list, false);
  1347. w->power = power;
  1348. }
  1349. static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
  1350. struct list_head *up_list,
  1351. struct list_head *down_list)
  1352. {
  1353. int power;
  1354. switch (w->id) {
  1355. case snd_soc_dapm_pre:
  1356. dapm_seq_insert(w, down_list, false);
  1357. break;
  1358. case snd_soc_dapm_post:
  1359. dapm_seq_insert(w, up_list, true);
  1360. break;
  1361. default:
  1362. power = dapm_widget_power_check(w);
  1363. dapm_widget_set_power(w, power, up_list, down_list);
  1364. break;
  1365. }
  1366. }
  1367. /*
  1368. * Scan each dapm widget for complete audio path.
  1369. * A complete path is a route that has valid endpoints i.e.:-
  1370. *
  1371. * o DAC to output pin.
  1372. * o Input Pin to ADC.
  1373. * o Input pin to Output pin (bypass, sidetone)
  1374. * o DAC to ADC (loopback).
  1375. */
  1376. static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
  1377. {
  1378. struct snd_soc_card *card = dapm->card;
  1379. struct snd_soc_dapm_widget *w;
  1380. struct snd_soc_dapm_context *d;
  1381. LIST_HEAD(up_list);
  1382. LIST_HEAD(down_list);
  1383. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  1384. enum snd_soc_bias_level bias;
  1385. trace_snd_soc_dapm_start(card);
  1386. list_for_each_entry(d, &card->dapm_list, list) {
  1387. if (d->idle_bias_off)
  1388. d->target_bias_level = SND_SOC_BIAS_OFF;
  1389. else
  1390. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1391. }
  1392. dapm_reset(card);
  1393. /* Check which widgets we need to power and store them in
  1394. * lists indicating if they should be powered up or down. We
  1395. * only check widgets that have been flagged as dirty but note
  1396. * that new widgets may be added to the dirty list while we
  1397. * iterate.
  1398. */
  1399. list_for_each_entry(w, &card->dapm_dirty, dirty) {
  1400. dapm_power_one_widget(w, &up_list, &down_list);
  1401. }
  1402. list_for_each_entry(w, &card->widgets, list) {
  1403. switch (w->id) {
  1404. case snd_soc_dapm_pre:
  1405. case snd_soc_dapm_post:
  1406. /* These widgets always need to be powered */
  1407. break;
  1408. default:
  1409. list_del_init(&w->dirty);
  1410. break;
  1411. }
  1412. if (w->power) {
  1413. d = w->dapm;
  1414. /* Supplies and micbiases only bring the
  1415. * context up to STANDBY as unless something
  1416. * else is active and passing audio they
  1417. * generally don't require full power. Signal
  1418. * generators are virtual pins and have no
  1419. * power impact themselves.
  1420. */
  1421. switch (w->id) {
  1422. case snd_soc_dapm_siggen:
  1423. break;
  1424. case snd_soc_dapm_supply:
  1425. case snd_soc_dapm_regulator_supply:
  1426. case snd_soc_dapm_clock_supply:
  1427. case snd_soc_dapm_micbias:
  1428. if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
  1429. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1430. break;
  1431. default:
  1432. d->target_bias_level = SND_SOC_BIAS_ON;
  1433. break;
  1434. }
  1435. }
  1436. }
  1437. /* Force all contexts in the card to the same bias state if
  1438. * they're not ground referenced.
  1439. */
  1440. bias = SND_SOC_BIAS_OFF;
  1441. list_for_each_entry(d, &card->dapm_list, list)
  1442. if (d->target_bias_level > bias)
  1443. bias = d->target_bias_level;
  1444. list_for_each_entry(d, &card->dapm_list, list)
  1445. if (!d->idle_bias_off)
  1446. d->target_bias_level = bias;
  1447. trace_snd_soc_dapm_walk_done(card);
  1448. /* Run all the bias changes in parallel */
  1449. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1450. async_schedule_domain(dapm_pre_sequence_async, d,
  1451. &async_domain);
  1452. async_synchronize_full_domain(&async_domain);
  1453. /* Power down widgets first; try to avoid amplifying pops. */
  1454. dapm_seq_run(dapm, &down_list, event, false);
  1455. dapm_widget_update(dapm);
  1456. /* Now power up. */
  1457. dapm_seq_run(dapm, &up_list, event, true);
  1458. /* Run all the bias changes in parallel */
  1459. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1460. async_schedule_domain(dapm_post_sequence_async, d,
  1461. &async_domain);
  1462. async_synchronize_full_domain(&async_domain);
  1463. /* do we need to notify any clients that DAPM event is complete */
  1464. list_for_each_entry(d, &card->dapm_list, list) {
  1465. if (d->stream_event)
  1466. d->stream_event(d, event);
  1467. }
  1468. pop_dbg(dapm->dev, card->pop_time,
  1469. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1470. pop_wait(card->pop_time);
  1471. trace_snd_soc_dapm_done(card);
  1472. return 0;
  1473. }
  1474. #ifdef CONFIG_DEBUG_FS
  1475. static ssize_t dapm_widget_power_read_file(struct file *file,
  1476. char __user *user_buf,
  1477. size_t count, loff_t *ppos)
  1478. {
  1479. struct snd_soc_dapm_widget *w = file->private_data;
  1480. char *buf;
  1481. int in, out;
  1482. ssize_t ret;
  1483. struct snd_soc_dapm_path *p = NULL;
  1484. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1485. if (!buf)
  1486. return -ENOMEM;
  1487. in = is_connected_input_ep(w, NULL);
  1488. dapm_clear_walk(w->dapm);
  1489. out = is_connected_output_ep(w, NULL);
  1490. dapm_clear_walk(w->dapm);
  1491. ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
  1492. w->name, w->power ? "On" : "Off",
  1493. w->force ? " (forced)" : "", in, out);
  1494. if (w->reg >= 0)
  1495. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1496. " - R%d(0x%x) bit %d",
  1497. w->reg, w->reg, w->shift);
  1498. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1499. if (w->sname)
  1500. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1501. w->sname,
  1502. w->active ? "active" : "inactive");
  1503. list_for_each_entry(p, &w->sources, list_sink) {
  1504. if (p->connected && !p->connected(w, p->sink))
  1505. continue;
  1506. if (p->connect)
  1507. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1508. " in \"%s\" \"%s\"\n",
  1509. p->name ? p->name : "static",
  1510. p->source->name);
  1511. }
  1512. list_for_each_entry(p, &w->sinks, list_source) {
  1513. if (p->connected && !p->connected(w, p->sink))
  1514. continue;
  1515. if (p->connect)
  1516. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1517. " out \"%s\" \"%s\"\n",
  1518. p->name ? p->name : "static",
  1519. p->sink->name);
  1520. }
  1521. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1522. kfree(buf);
  1523. return ret;
  1524. }
  1525. static const struct file_operations dapm_widget_power_fops = {
  1526. .open = simple_open,
  1527. .read = dapm_widget_power_read_file,
  1528. .llseek = default_llseek,
  1529. };
  1530. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1531. size_t count, loff_t *ppos)
  1532. {
  1533. struct snd_soc_dapm_context *dapm = file->private_data;
  1534. char *level;
  1535. switch (dapm->bias_level) {
  1536. case SND_SOC_BIAS_ON:
  1537. level = "On\n";
  1538. break;
  1539. case SND_SOC_BIAS_PREPARE:
  1540. level = "Prepare\n";
  1541. break;
  1542. case SND_SOC_BIAS_STANDBY:
  1543. level = "Standby\n";
  1544. break;
  1545. case SND_SOC_BIAS_OFF:
  1546. level = "Off\n";
  1547. break;
  1548. default:
  1549. BUG();
  1550. level = "Unknown\n";
  1551. break;
  1552. }
  1553. return simple_read_from_buffer(user_buf, count, ppos, level,
  1554. strlen(level));
  1555. }
  1556. static const struct file_operations dapm_bias_fops = {
  1557. .open = simple_open,
  1558. .read = dapm_bias_read_file,
  1559. .llseek = default_llseek,
  1560. };
  1561. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1562. struct dentry *parent)
  1563. {
  1564. struct dentry *d;
  1565. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1566. if (!dapm->debugfs_dapm) {
  1567. dev_warn(dapm->dev,
  1568. "Failed to create DAPM debugfs directory\n");
  1569. return;
  1570. }
  1571. d = debugfs_create_file("bias_level", 0444,
  1572. dapm->debugfs_dapm, dapm,
  1573. &dapm_bias_fops);
  1574. if (!d)
  1575. dev_warn(dapm->dev,
  1576. "ASoC: Failed to create bias level debugfs file\n");
  1577. }
  1578. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1579. {
  1580. struct snd_soc_dapm_context *dapm = w->dapm;
  1581. struct dentry *d;
  1582. if (!dapm->debugfs_dapm || !w->name)
  1583. return;
  1584. d = debugfs_create_file(w->name, 0444,
  1585. dapm->debugfs_dapm, w,
  1586. &dapm_widget_power_fops);
  1587. if (!d)
  1588. dev_warn(w->dapm->dev,
  1589. "ASoC: Failed to create %s debugfs file\n",
  1590. w->name);
  1591. }
  1592. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1593. {
  1594. debugfs_remove_recursive(dapm->debugfs_dapm);
  1595. }
  1596. #else
  1597. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1598. struct dentry *parent)
  1599. {
  1600. }
  1601. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1602. {
  1603. }
  1604. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1605. {
  1606. }
  1607. #endif
  1608. /* test and update the power status of a mux widget */
  1609. static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1610. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1611. {
  1612. struct snd_soc_dapm_path *path;
  1613. int found = 0;
  1614. if (widget->id != snd_soc_dapm_mux &&
  1615. widget->id != snd_soc_dapm_virt_mux &&
  1616. widget->id != snd_soc_dapm_value_mux)
  1617. return -ENODEV;
  1618. /* find dapm widget path assoc with kcontrol */
  1619. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1620. if (path->kcontrol != kcontrol)
  1621. continue;
  1622. if (!path->name || !e->texts[mux])
  1623. continue;
  1624. found = 1;
  1625. /* we now need to match the string in the enum to the path */
  1626. if (!(strcmp(path->name, e->texts[mux]))) {
  1627. path->connect = 1; /* new connection */
  1628. dapm_mark_dirty(path->source, "mux connection");
  1629. } else {
  1630. if (path->connect)
  1631. dapm_mark_dirty(path->source,
  1632. "mux disconnection");
  1633. path->connect = 0; /* old connection must be powered down */
  1634. }
  1635. }
  1636. if (found) {
  1637. dapm_mark_dirty(widget, "mux change");
  1638. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1639. }
  1640. return found;
  1641. }
  1642. int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1643. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1644. {
  1645. struct snd_soc_card *card = widget->dapm->card;
  1646. int ret;
  1647. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1648. ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  1649. mutex_unlock(&card->dapm_mutex);
  1650. if (ret > 0)
  1651. soc_dpcm_runtime_update(widget);
  1652. return ret;
  1653. }
  1654. EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
  1655. /* test and update the power status of a mixer or switch widget */
  1656. static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1657. struct snd_kcontrol *kcontrol, int connect)
  1658. {
  1659. struct snd_soc_dapm_path *path;
  1660. int found = 0;
  1661. if (widget->id != snd_soc_dapm_mixer &&
  1662. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1663. widget->id != snd_soc_dapm_switch)
  1664. return -ENODEV;
  1665. /* find dapm widget path assoc with kcontrol */
  1666. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1667. if (path->kcontrol != kcontrol)
  1668. continue;
  1669. /* found, now check type */
  1670. found = 1;
  1671. path->connect = connect;
  1672. dapm_mark_dirty(path->source, "mixer connection");
  1673. }
  1674. if (found) {
  1675. dapm_mark_dirty(widget, "mixer update");
  1676. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1677. }
  1678. return found;
  1679. }
  1680. int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1681. struct snd_kcontrol *kcontrol, int connect)
  1682. {
  1683. struct snd_soc_card *card = widget->dapm->card;
  1684. int ret;
  1685. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1686. ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
  1687. mutex_unlock(&card->dapm_mutex);
  1688. if (ret > 0)
  1689. soc_dpcm_runtime_update(widget);
  1690. return ret;
  1691. }
  1692. EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
  1693. /* show dapm widget status in sys fs */
  1694. static ssize_t dapm_widget_show(struct device *dev,
  1695. struct device_attribute *attr, char *buf)
  1696. {
  1697. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  1698. struct snd_soc_codec *codec =rtd->codec;
  1699. struct snd_soc_dapm_widget *w;
  1700. int count = 0;
  1701. char *state = "not set";
  1702. list_for_each_entry(w, &codec->card->widgets, list) {
  1703. if (w->dapm != &codec->dapm)
  1704. continue;
  1705. /* only display widgets that burnm power */
  1706. switch (w->id) {
  1707. case snd_soc_dapm_hp:
  1708. case snd_soc_dapm_mic:
  1709. case snd_soc_dapm_spk:
  1710. case snd_soc_dapm_line:
  1711. case snd_soc_dapm_micbias:
  1712. case snd_soc_dapm_dac:
  1713. case snd_soc_dapm_adc:
  1714. case snd_soc_dapm_pga:
  1715. case snd_soc_dapm_out_drv:
  1716. case snd_soc_dapm_mixer:
  1717. case snd_soc_dapm_mixer_named_ctl:
  1718. case snd_soc_dapm_supply:
  1719. case snd_soc_dapm_regulator_supply:
  1720. case snd_soc_dapm_clock_supply:
  1721. if (w->name)
  1722. count += sprintf(buf + count, "%s: %s\n",
  1723. w->name, w->power ? "On":"Off");
  1724. break;
  1725. default:
  1726. break;
  1727. }
  1728. }
  1729. switch (codec->dapm.bias_level) {
  1730. case SND_SOC_BIAS_ON:
  1731. state = "On";
  1732. break;
  1733. case SND_SOC_BIAS_PREPARE:
  1734. state = "Prepare";
  1735. break;
  1736. case SND_SOC_BIAS_STANDBY:
  1737. state = "Standby";
  1738. break;
  1739. case SND_SOC_BIAS_OFF:
  1740. state = "Off";
  1741. break;
  1742. }
  1743. count += sprintf(buf + count, "PM State: %s\n", state);
  1744. return count;
  1745. }
  1746. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1747. int snd_soc_dapm_sys_add(struct device *dev)
  1748. {
  1749. return device_create_file(dev, &dev_attr_dapm_widget);
  1750. }
  1751. static void snd_soc_dapm_sys_remove(struct device *dev)
  1752. {
  1753. device_remove_file(dev, &dev_attr_dapm_widget);
  1754. }
  1755. /* free all dapm widgets and resources */
  1756. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1757. {
  1758. struct snd_soc_dapm_widget *w, *next_w;
  1759. struct snd_soc_dapm_path *p, *next_p;
  1760. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1761. if (w->dapm != dapm)
  1762. continue;
  1763. list_del(&w->list);
  1764. /*
  1765. * remove source and sink paths associated to this widget.
  1766. * While removing the path, remove reference to it from both
  1767. * source and sink widgets so that path is removed only once.
  1768. */
  1769. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1770. list_del(&p->list_sink);
  1771. list_del(&p->list_source);
  1772. list_del(&p->list);
  1773. kfree(p->long_name);
  1774. kfree(p);
  1775. }
  1776. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1777. list_del(&p->list_sink);
  1778. list_del(&p->list_source);
  1779. list_del(&p->list);
  1780. kfree(p->long_name);
  1781. kfree(p);
  1782. }
  1783. kfree(w->kcontrols);
  1784. kfree(w->name);
  1785. kfree(w);
  1786. }
  1787. }
  1788. static struct snd_soc_dapm_widget *dapm_find_widget(
  1789. struct snd_soc_dapm_context *dapm, const char *pin,
  1790. bool search_other_contexts)
  1791. {
  1792. struct snd_soc_dapm_widget *w;
  1793. struct snd_soc_dapm_widget *fallback = NULL;
  1794. list_for_each_entry(w, &dapm->card->widgets, list) {
  1795. if (!strcmp(w->name, pin)) {
  1796. if (w->dapm == dapm)
  1797. return w;
  1798. else
  1799. fallback = w;
  1800. }
  1801. }
  1802. if (search_other_contexts)
  1803. return fallback;
  1804. return NULL;
  1805. }
  1806. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1807. const char *pin, int status)
  1808. {
  1809. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  1810. if (!w) {
  1811. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1812. return -EINVAL;
  1813. }
  1814. if (w->connected != status)
  1815. dapm_mark_dirty(w, "pin configuration");
  1816. w->connected = status;
  1817. if (status == 0)
  1818. w->force = 0;
  1819. return 0;
  1820. }
  1821. /**
  1822. * snd_soc_dapm_sync - scan and power dapm paths
  1823. * @dapm: DAPM context
  1824. *
  1825. * Walks all dapm audio paths and powers widgets according to their
  1826. * stream or path usage.
  1827. *
  1828. * Returns 0 for success.
  1829. */
  1830. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1831. {
  1832. int ret;
  1833. /*
  1834. * Suppress early reports (eg, jacks syncing their state) to avoid
  1835. * silly DAPM runs during card startup.
  1836. */
  1837. if (!dapm->card || !dapm->card->instantiated)
  1838. return 0;
  1839. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1840. ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1841. mutex_unlock(&dapm->card->dapm_mutex);
  1842. return ret;
  1843. }
  1844. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1845. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1846. const struct snd_soc_dapm_route *route)
  1847. {
  1848. struct snd_soc_dapm_path *path;
  1849. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1850. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1851. const char *sink;
  1852. const char *control = route->control;
  1853. const char *source;
  1854. char prefixed_sink[80];
  1855. char prefixed_source[80];
  1856. int ret = 0;
  1857. if (dapm->codec && dapm->codec->name_prefix) {
  1858. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1859. dapm->codec->name_prefix, route->sink);
  1860. sink = prefixed_sink;
  1861. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1862. dapm->codec->name_prefix, route->source);
  1863. source = prefixed_source;
  1864. } else {
  1865. sink = route->sink;
  1866. source = route->source;
  1867. }
  1868. /*
  1869. * find src and dest widgets over all widgets but favor a widget from
  1870. * current DAPM context
  1871. */
  1872. list_for_each_entry(w, &dapm->card->widgets, list) {
  1873. if (!wsink && !(strcmp(w->name, sink))) {
  1874. wtsink = w;
  1875. if (w->dapm == dapm)
  1876. wsink = w;
  1877. continue;
  1878. }
  1879. if (!wsource && !(strcmp(w->name, source))) {
  1880. wtsource = w;
  1881. if (w->dapm == dapm)
  1882. wsource = w;
  1883. }
  1884. }
  1885. /* use widget from another DAPM context if not found from this */
  1886. if (!wsink)
  1887. wsink = wtsink;
  1888. if (!wsource)
  1889. wsource = wtsource;
  1890. if (wsource == NULL || wsink == NULL)
  1891. return -ENODEV;
  1892. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1893. if (!path)
  1894. return -ENOMEM;
  1895. path->source = wsource;
  1896. path->sink = wsink;
  1897. path->connected = route->connected;
  1898. INIT_LIST_HEAD(&path->list);
  1899. INIT_LIST_HEAD(&path->list_source);
  1900. INIT_LIST_HEAD(&path->list_sink);
  1901. /* check for external widgets */
  1902. if (wsink->id == snd_soc_dapm_input) {
  1903. if (wsource->id == snd_soc_dapm_micbias ||
  1904. wsource->id == snd_soc_dapm_mic ||
  1905. wsource->id == snd_soc_dapm_line ||
  1906. wsource->id == snd_soc_dapm_output)
  1907. wsink->ext = 1;
  1908. }
  1909. if (wsource->id == snd_soc_dapm_output) {
  1910. if (wsink->id == snd_soc_dapm_spk ||
  1911. wsink->id == snd_soc_dapm_hp ||
  1912. wsink->id == snd_soc_dapm_line ||
  1913. wsink->id == snd_soc_dapm_input)
  1914. wsource->ext = 1;
  1915. }
  1916. /* connect static paths */
  1917. if (control == NULL) {
  1918. list_add(&path->list, &dapm->card->paths);
  1919. list_add(&path->list_sink, &wsink->sources);
  1920. list_add(&path->list_source, &wsource->sinks);
  1921. path->connect = 1;
  1922. return 0;
  1923. }
  1924. /* connect dynamic paths */
  1925. switch (wsink->id) {
  1926. case snd_soc_dapm_adc:
  1927. case snd_soc_dapm_dac:
  1928. case snd_soc_dapm_pga:
  1929. case snd_soc_dapm_out_drv:
  1930. case snd_soc_dapm_input:
  1931. case snd_soc_dapm_output:
  1932. case snd_soc_dapm_siggen:
  1933. case snd_soc_dapm_micbias:
  1934. case snd_soc_dapm_vmid:
  1935. case snd_soc_dapm_pre:
  1936. case snd_soc_dapm_post:
  1937. case snd_soc_dapm_supply:
  1938. case snd_soc_dapm_regulator_supply:
  1939. case snd_soc_dapm_clock_supply:
  1940. case snd_soc_dapm_aif_in:
  1941. case snd_soc_dapm_aif_out:
  1942. case snd_soc_dapm_dai:
  1943. case snd_soc_dapm_dai_link:
  1944. list_add(&path->list, &dapm->card->paths);
  1945. list_add(&path->list_sink, &wsink->sources);
  1946. list_add(&path->list_source, &wsource->sinks);
  1947. path->connect = 1;
  1948. return 0;
  1949. case snd_soc_dapm_mux:
  1950. case snd_soc_dapm_virt_mux:
  1951. case snd_soc_dapm_value_mux:
  1952. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1953. &wsink->kcontrol_news[0]);
  1954. if (ret != 0)
  1955. goto err;
  1956. break;
  1957. case snd_soc_dapm_switch:
  1958. case snd_soc_dapm_mixer:
  1959. case snd_soc_dapm_mixer_named_ctl:
  1960. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1961. if (ret != 0)
  1962. goto err;
  1963. break;
  1964. case snd_soc_dapm_hp:
  1965. case snd_soc_dapm_mic:
  1966. case snd_soc_dapm_line:
  1967. case snd_soc_dapm_spk:
  1968. list_add(&path->list, &dapm->card->paths);
  1969. list_add(&path->list_sink, &wsink->sources);
  1970. list_add(&path->list_source, &wsource->sinks);
  1971. path->connect = 0;
  1972. return 0;
  1973. }
  1974. dapm_mark_dirty(wsource, "Route added");
  1975. dapm_mark_dirty(wsink, "Route added");
  1976. return 0;
  1977. err:
  1978. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1979. source, control, sink);
  1980. kfree(path);
  1981. return ret;
  1982. }
  1983. static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
  1984. const struct snd_soc_dapm_route *route)
  1985. {
  1986. struct snd_soc_dapm_path *path, *p;
  1987. const char *sink;
  1988. const char *source;
  1989. char prefixed_sink[80];
  1990. char prefixed_source[80];
  1991. if (route->control) {
  1992. dev_err(dapm->dev,
  1993. "Removal of routes with controls not supported\n");
  1994. return -EINVAL;
  1995. }
  1996. if (dapm->codec && dapm->codec->name_prefix) {
  1997. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1998. dapm->codec->name_prefix, route->sink);
  1999. sink = prefixed_sink;
  2000. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  2001. dapm->codec->name_prefix, route->source);
  2002. source = prefixed_source;
  2003. } else {
  2004. sink = route->sink;
  2005. source = route->source;
  2006. }
  2007. path = NULL;
  2008. list_for_each_entry(p, &dapm->card->paths, list) {
  2009. if (strcmp(p->source->name, source) != 0)
  2010. continue;
  2011. if (strcmp(p->sink->name, sink) != 0)
  2012. continue;
  2013. path = p;
  2014. break;
  2015. }
  2016. if (path) {
  2017. dapm_mark_dirty(path->source, "Route removed");
  2018. dapm_mark_dirty(path->sink, "Route removed");
  2019. list_del(&path->list);
  2020. list_del(&path->list_sink);
  2021. list_del(&path->list_source);
  2022. kfree(path);
  2023. } else {
  2024. dev_warn(dapm->dev, "Route %s->%s does not exist\n",
  2025. source, sink);
  2026. }
  2027. return 0;
  2028. }
  2029. /**
  2030. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  2031. * @dapm: DAPM context
  2032. * @route: audio routes
  2033. * @num: number of routes
  2034. *
  2035. * Connects 2 dapm widgets together via a named audio path. The sink is
  2036. * the widget receiving the audio signal, whilst the source is the sender
  2037. * of the audio signal.
  2038. *
  2039. * Returns 0 for success else error. On error all resources can be freed
  2040. * with a call to snd_soc_card_free().
  2041. */
  2042. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  2043. const struct snd_soc_dapm_route *route, int num)
  2044. {
  2045. int i, r, ret = 0;
  2046. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2047. for (i = 0; i < num; i++) {
  2048. r = snd_soc_dapm_add_route(dapm, route);
  2049. if (r < 0) {
  2050. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  2051. route->source, route->sink);
  2052. ret = r;
  2053. }
  2054. route++;
  2055. }
  2056. mutex_unlock(&dapm->card->dapm_mutex);
  2057. return ret;
  2058. }
  2059. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  2060. /**
  2061. * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
  2062. * @dapm: DAPM context
  2063. * @route: audio routes
  2064. * @num: number of routes
  2065. *
  2066. * Removes routes from the DAPM context.
  2067. */
  2068. int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
  2069. const struct snd_soc_dapm_route *route, int num)
  2070. {
  2071. int i, ret = 0;
  2072. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2073. for (i = 0; i < num; i++) {
  2074. snd_soc_dapm_del_route(dapm, route);
  2075. route++;
  2076. }
  2077. mutex_unlock(&dapm->card->dapm_mutex);
  2078. return ret;
  2079. }
  2080. EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
  2081. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  2082. const struct snd_soc_dapm_route *route)
  2083. {
  2084. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  2085. route->source,
  2086. true);
  2087. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  2088. route->sink,
  2089. true);
  2090. struct snd_soc_dapm_path *path;
  2091. int count = 0;
  2092. if (!source) {
  2093. dev_err(dapm->dev, "Unable to find source %s for weak route\n",
  2094. route->source);
  2095. return -ENODEV;
  2096. }
  2097. if (!sink) {
  2098. dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
  2099. route->sink);
  2100. return -ENODEV;
  2101. }
  2102. if (route->control || route->connected)
  2103. dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
  2104. route->source, route->sink);
  2105. list_for_each_entry(path, &source->sinks, list_source) {
  2106. if (path->sink == sink) {
  2107. path->weak = 1;
  2108. count++;
  2109. }
  2110. }
  2111. if (count == 0)
  2112. dev_err(dapm->dev, "No path found for weak route %s->%s\n",
  2113. route->source, route->sink);
  2114. if (count > 1)
  2115. dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
  2116. count, route->source, route->sink);
  2117. return 0;
  2118. }
  2119. /**
  2120. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  2121. * @dapm: DAPM context
  2122. * @route: audio routes
  2123. * @num: number of routes
  2124. *
  2125. * Mark existing routes matching those specified in the passed array
  2126. * as being weak, meaning that they are ignored for the purpose of
  2127. * power decisions. The main intended use case is for sidetone paths
  2128. * which couple audio between other independent paths if they are both
  2129. * active in order to make the combination work better at the user
  2130. * level but which aren't intended to be "used".
  2131. *
  2132. * Note that CODEC drivers should not use this as sidetone type paths
  2133. * can frequently also be used as bypass paths.
  2134. */
  2135. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  2136. const struct snd_soc_dapm_route *route, int num)
  2137. {
  2138. int i, err;
  2139. int ret = 0;
  2140. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2141. for (i = 0; i < num; i++) {
  2142. err = snd_soc_dapm_weak_route(dapm, route);
  2143. if (err)
  2144. ret = err;
  2145. route++;
  2146. }
  2147. mutex_unlock(&dapm->card->dapm_mutex);
  2148. return ret;
  2149. }
  2150. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  2151. /**
  2152. * snd_soc_dapm_new_widgets - add new dapm widgets
  2153. * @dapm: DAPM context
  2154. *
  2155. * Checks the codec for any new dapm widgets and creates them if found.
  2156. *
  2157. * Returns 0 for success.
  2158. */
  2159. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  2160. {
  2161. struct snd_soc_dapm_widget *w;
  2162. unsigned int val;
  2163. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2164. list_for_each_entry(w, &dapm->card->widgets, list)
  2165. {
  2166. if (w->new)
  2167. continue;
  2168. if (w->num_kcontrols) {
  2169. w->kcontrols = kzalloc(w->num_kcontrols *
  2170. sizeof(struct snd_kcontrol *),
  2171. GFP_KERNEL);
  2172. if (!w->kcontrols) {
  2173. mutex_unlock(&dapm->card->dapm_mutex);
  2174. return -ENOMEM;
  2175. }
  2176. }
  2177. switch(w->id) {
  2178. case snd_soc_dapm_switch:
  2179. case snd_soc_dapm_mixer:
  2180. case snd_soc_dapm_mixer_named_ctl:
  2181. dapm_new_mixer(w);
  2182. break;
  2183. case snd_soc_dapm_mux:
  2184. case snd_soc_dapm_virt_mux:
  2185. case snd_soc_dapm_value_mux:
  2186. dapm_new_mux(w);
  2187. break;
  2188. case snd_soc_dapm_pga:
  2189. case snd_soc_dapm_out_drv:
  2190. dapm_new_pga(w);
  2191. break;
  2192. default:
  2193. break;
  2194. }
  2195. /* Read the initial power state from the device */
  2196. if (w->reg >= 0) {
  2197. val = soc_widget_read(w, w->reg);
  2198. val &= 1 << w->shift;
  2199. if (w->invert)
  2200. val = !val;
  2201. if (val)
  2202. w->power = 1;
  2203. }
  2204. w->new = 1;
  2205. dapm_mark_dirty(w, "new widget");
  2206. dapm_debugfs_add_widget(w);
  2207. }
  2208. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  2209. mutex_unlock(&dapm->card->dapm_mutex);
  2210. return 0;
  2211. }
  2212. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  2213. /**
  2214. * snd_soc_dapm_get_volsw - dapm mixer get callback
  2215. * @kcontrol: mixer control
  2216. * @ucontrol: control element information
  2217. *
  2218. * Callback to get the value of a dapm mixer control.
  2219. *
  2220. * Returns 0 for success.
  2221. */
  2222. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  2223. struct snd_ctl_elem_value *ucontrol)
  2224. {
  2225. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2226. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2227. struct soc_mixer_control *mc =
  2228. (struct soc_mixer_control *)kcontrol->private_value;
  2229. unsigned int reg = mc->reg;
  2230. unsigned int shift = mc->shift;
  2231. int max = mc->max;
  2232. unsigned int mask = (1 << fls(max)) - 1;
  2233. unsigned int invert = mc->invert;
  2234. if (snd_soc_volsw_is_stereo(mc))
  2235. dev_warn(widget->dapm->dev,
  2236. "Control '%s' is stereo, which is not supported\n",
  2237. kcontrol->id.name);
  2238. ucontrol->value.integer.value[0] =
  2239. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  2240. if (invert)
  2241. ucontrol->value.integer.value[0] =
  2242. max - ucontrol->value.integer.value[0];
  2243. return 0;
  2244. }
  2245. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  2246. /**
  2247. * snd_soc_dapm_put_volsw - dapm mixer set callback
  2248. * @kcontrol: mixer control
  2249. * @ucontrol: control element information
  2250. *
  2251. * Callback to set the value of a dapm mixer control.
  2252. *
  2253. * Returns 0 for success.
  2254. */
  2255. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  2256. struct snd_ctl_elem_value *ucontrol)
  2257. {
  2258. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2259. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2260. struct snd_soc_codec *codec = widget->codec;
  2261. struct snd_soc_card *card = codec->card;
  2262. struct soc_mixer_control *mc =
  2263. (struct soc_mixer_control *)kcontrol->private_value;
  2264. unsigned int reg = mc->reg;
  2265. unsigned int shift = mc->shift;
  2266. int max = mc->max;
  2267. unsigned int mask = (1 << fls(max)) - 1;
  2268. unsigned int invert = mc->invert;
  2269. unsigned int val;
  2270. int connect, change;
  2271. struct snd_soc_dapm_update update;
  2272. int wi;
  2273. if (snd_soc_volsw_is_stereo(mc))
  2274. dev_warn(widget->dapm->dev,
  2275. "Control '%s' is stereo, which is not supported\n",
  2276. kcontrol->id.name);
  2277. val = (ucontrol->value.integer.value[0] & mask);
  2278. connect = !!val;
  2279. if (invert)
  2280. val = max - val;
  2281. mask = mask << shift;
  2282. val = val << shift;
  2283. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2284. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  2285. if (change) {
  2286. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2287. widget = wlist->widgets[wi];
  2288. widget->value = val;
  2289. update.kcontrol = kcontrol;
  2290. update.widget = widget;
  2291. update.reg = reg;
  2292. update.mask = mask;
  2293. update.val = val;
  2294. widget->dapm->update = &update;
  2295. soc_dapm_mixer_update_power(widget, kcontrol, connect);
  2296. widget->dapm->update = NULL;
  2297. }
  2298. }
  2299. mutex_unlock(&card->dapm_mutex);
  2300. return 0;
  2301. }
  2302. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  2303. /**
  2304. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  2305. * @kcontrol: mixer control
  2306. * @ucontrol: control element information
  2307. *
  2308. * Callback to get the value of a dapm enumerated double mixer control.
  2309. *
  2310. * Returns 0 for success.
  2311. */
  2312. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  2313. struct snd_ctl_elem_value *ucontrol)
  2314. {
  2315. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2316. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2317. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2318. unsigned int val;
  2319. val = snd_soc_read(widget->codec, e->reg);
  2320. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
  2321. if (e->shift_l != e->shift_r)
  2322. ucontrol->value.enumerated.item[1] =
  2323. (val >> e->shift_r) & e->mask;
  2324. return 0;
  2325. }
  2326. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  2327. /**
  2328. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  2329. * @kcontrol: mixer control
  2330. * @ucontrol: control element information
  2331. *
  2332. * Callback to set the value of a dapm enumerated double mixer control.
  2333. *
  2334. * Returns 0 for success.
  2335. */
  2336. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  2337. struct snd_ctl_elem_value *ucontrol)
  2338. {
  2339. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2340. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2341. struct snd_soc_codec *codec = widget->codec;
  2342. struct snd_soc_card *card = codec->card;
  2343. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2344. unsigned int val, mux, change;
  2345. unsigned int mask;
  2346. struct snd_soc_dapm_update update;
  2347. int wi;
  2348. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2349. return -EINVAL;
  2350. mux = ucontrol->value.enumerated.item[0];
  2351. val = mux << e->shift_l;
  2352. mask = e->mask << e->shift_l;
  2353. if (e->shift_l != e->shift_r) {
  2354. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2355. return -EINVAL;
  2356. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2357. mask |= e->mask << e->shift_r;
  2358. }
  2359. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2360. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2361. if (change) {
  2362. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2363. widget = wlist->widgets[wi];
  2364. widget->value = val;
  2365. update.kcontrol = kcontrol;
  2366. update.widget = widget;
  2367. update.reg = e->reg;
  2368. update.mask = mask;
  2369. update.val = val;
  2370. widget->dapm->update = &update;
  2371. soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2372. widget->dapm->update = NULL;
  2373. }
  2374. }
  2375. mutex_unlock(&card->dapm_mutex);
  2376. return change;
  2377. }
  2378. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  2379. /**
  2380. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  2381. * @kcontrol: mixer control
  2382. * @ucontrol: control element information
  2383. *
  2384. * Returns 0 for success.
  2385. */
  2386. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  2387. struct snd_ctl_elem_value *ucontrol)
  2388. {
  2389. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2390. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2391. ucontrol->value.enumerated.item[0] = widget->value;
  2392. return 0;
  2393. }
  2394. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  2395. /**
  2396. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  2397. * @kcontrol: mixer control
  2398. * @ucontrol: control element information
  2399. *
  2400. * Returns 0 for success.
  2401. */
  2402. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  2403. struct snd_ctl_elem_value *ucontrol)
  2404. {
  2405. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2406. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2407. struct snd_soc_codec *codec = widget->codec;
  2408. struct snd_soc_card *card = codec->card;
  2409. struct soc_enum *e =
  2410. (struct soc_enum *)kcontrol->private_value;
  2411. int change;
  2412. int ret = 0;
  2413. int wi;
  2414. if (ucontrol->value.enumerated.item[0] >= e->max)
  2415. return -EINVAL;
  2416. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2417. change = widget->value != ucontrol->value.enumerated.item[0];
  2418. if (change) {
  2419. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2420. widget = wlist->widgets[wi];
  2421. widget->value = ucontrol->value.enumerated.item[0];
  2422. soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
  2423. }
  2424. }
  2425. mutex_unlock(&card->dapm_mutex);
  2426. return ret;
  2427. }
  2428. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  2429. /**
  2430. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  2431. * callback
  2432. * @kcontrol: mixer control
  2433. * @ucontrol: control element information
  2434. *
  2435. * Callback to get the value of a dapm semi enumerated double mixer control.
  2436. *
  2437. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2438. * used for handling bitfield coded enumeration for example.
  2439. *
  2440. * Returns 0 for success.
  2441. */
  2442. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2443. struct snd_ctl_elem_value *ucontrol)
  2444. {
  2445. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2446. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2447. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2448. unsigned int reg_val, val, mux;
  2449. reg_val = snd_soc_read(widget->codec, e->reg);
  2450. val = (reg_val >> e->shift_l) & e->mask;
  2451. for (mux = 0; mux < e->max; mux++) {
  2452. if (val == e->values[mux])
  2453. break;
  2454. }
  2455. ucontrol->value.enumerated.item[0] = mux;
  2456. if (e->shift_l != e->shift_r) {
  2457. val = (reg_val >> e->shift_r) & e->mask;
  2458. for (mux = 0; mux < e->max; mux++) {
  2459. if (val == e->values[mux])
  2460. break;
  2461. }
  2462. ucontrol->value.enumerated.item[1] = mux;
  2463. }
  2464. return 0;
  2465. }
  2466. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  2467. /**
  2468. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  2469. * callback
  2470. * @kcontrol: mixer control
  2471. * @ucontrol: control element information
  2472. *
  2473. * Callback to set the value of a dapm semi enumerated double mixer control.
  2474. *
  2475. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2476. * used for handling bitfield coded enumeration for example.
  2477. *
  2478. * Returns 0 for success.
  2479. */
  2480. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2481. struct snd_ctl_elem_value *ucontrol)
  2482. {
  2483. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2484. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2485. struct snd_soc_codec *codec = widget->codec;
  2486. struct snd_soc_card *card = codec->card;
  2487. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2488. unsigned int val, mux, change;
  2489. unsigned int mask;
  2490. struct snd_soc_dapm_update update;
  2491. int wi;
  2492. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2493. return -EINVAL;
  2494. mux = ucontrol->value.enumerated.item[0];
  2495. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2496. mask = e->mask << e->shift_l;
  2497. if (e->shift_l != e->shift_r) {
  2498. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2499. return -EINVAL;
  2500. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2501. mask |= e->mask << e->shift_r;
  2502. }
  2503. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2504. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2505. if (change) {
  2506. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2507. widget = wlist->widgets[wi];
  2508. widget->value = val;
  2509. update.kcontrol = kcontrol;
  2510. update.widget = widget;
  2511. update.reg = e->reg;
  2512. update.mask = mask;
  2513. update.val = val;
  2514. widget->dapm->update = &update;
  2515. soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2516. widget->dapm->update = NULL;
  2517. }
  2518. }
  2519. mutex_unlock(&card->dapm_mutex);
  2520. return change;
  2521. }
  2522. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  2523. /**
  2524. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2525. *
  2526. * @kcontrol: mixer control
  2527. * @uinfo: control element information
  2528. *
  2529. * Callback to provide information about a pin switch control.
  2530. */
  2531. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2532. struct snd_ctl_elem_info *uinfo)
  2533. {
  2534. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2535. uinfo->count = 1;
  2536. uinfo->value.integer.min = 0;
  2537. uinfo->value.integer.max = 1;
  2538. return 0;
  2539. }
  2540. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2541. /**
  2542. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2543. *
  2544. * @kcontrol: mixer control
  2545. * @ucontrol: Value
  2546. */
  2547. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2548. struct snd_ctl_elem_value *ucontrol)
  2549. {
  2550. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2551. const char *pin = (const char *)kcontrol->private_value;
  2552. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2553. ucontrol->value.integer.value[0] =
  2554. snd_soc_dapm_get_pin_status(&card->dapm, pin);
  2555. mutex_unlock(&card->dapm_mutex);
  2556. return 0;
  2557. }
  2558. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2559. /**
  2560. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2561. *
  2562. * @kcontrol: mixer control
  2563. * @ucontrol: Value
  2564. */
  2565. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2566. struct snd_ctl_elem_value *ucontrol)
  2567. {
  2568. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2569. const char *pin = (const char *)kcontrol->private_value;
  2570. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2571. if (ucontrol->value.integer.value[0])
  2572. snd_soc_dapm_enable_pin(&card->dapm, pin);
  2573. else
  2574. snd_soc_dapm_disable_pin(&card->dapm, pin);
  2575. mutex_unlock(&card->dapm_mutex);
  2576. snd_soc_dapm_sync(&card->dapm);
  2577. return 0;
  2578. }
  2579. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2580. static struct snd_soc_dapm_widget *
  2581. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  2582. const struct snd_soc_dapm_widget *widget)
  2583. {
  2584. struct snd_soc_dapm_widget *w;
  2585. size_t name_len;
  2586. int ret;
  2587. if ((w = dapm_cnew_widget(widget)) == NULL)
  2588. return NULL;
  2589. switch (w->id) {
  2590. case snd_soc_dapm_regulator_supply:
  2591. w->regulator = devm_regulator_get(dapm->dev, w->name);
  2592. if (IS_ERR(w->regulator)) {
  2593. ret = PTR_ERR(w->regulator);
  2594. dev_err(dapm->dev, "Failed to request %s: %d\n",
  2595. w->name, ret);
  2596. return NULL;
  2597. }
  2598. break;
  2599. case snd_soc_dapm_clock_supply:
  2600. #ifdef CONFIG_CLKDEV_LOOKUP
  2601. w->clk = devm_clk_get(dapm->dev, w->name);
  2602. if (IS_ERR(w->clk)) {
  2603. ret = PTR_ERR(w->clk);
  2604. dev_err(dapm->dev, "Failed to request %s: %d\n",
  2605. w->name, ret);
  2606. return NULL;
  2607. }
  2608. #else
  2609. return NULL;
  2610. #endif
  2611. break;
  2612. default:
  2613. break;
  2614. }
  2615. name_len = strlen(widget->name) + 1;
  2616. if (dapm->codec && dapm->codec->name_prefix)
  2617. name_len += 1 + strlen(dapm->codec->name_prefix);
  2618. w->name = kmalloc(name_len, GFP_KERNEL);
  2619. if (w->name == NULL) {
  2620. kfree(w);
  2621. return NULL;
  2622. }
  2623. if (dapm->codec && dapm->codec->name_prefix)
  2624. snprintf((char *)w->name, name_len, "%s %s",
  2625. dapm->codec->name_prefix, widget->name);
  2626. else
  2627. snprintf((char *)w->name, name_len, "%s", widget->name);
  2628. switch (w->id) {
  2629. case snd_soc_dapm_switch:
  2630. case snd_soc_dapm_mixer:
  2631. case snd_soc_dapm_mixer_named_ctl:
  2632. w->power_check = dapm_generic_check_power;
  2633. break;
  2634. case snd_soc_dapm_mux:
  2635. case snd_soc_dapm_virt_mux:
  2636. case snd_soc_dapm_value_mux:
  2637. w->power_check = dapm_generic_check_power;
  2638. break;
  2639. case snd_soc_dapm_adc:
  2640. case snd_soc_dapm_aif_out:
  2641. w->power_check = dapm_adc_check_power;
  2642. break;
  2643. case snd_soc_dapm_dac:
  2644. case snd_soc_dapm_aif_in:
  2645. w->power_check = dapm_dac_check_power;
  2646. break;
  2647. case snd_soc_dapm_pga:
  2648. case snd_soc_dapm_out_drv:
  2649. case snd_soc_dapm_input:
  2650. case snd_soc_dapm_output:
  2651. case snd_soc_dapm_micbias:
  2652. case snd_soc_dapm_spk:
  2653. case snd_soc_dapm_hp:
  2654. case snd_soc_dapm_mic:
  2655. case snd_soc_dapm_line:
  2656. case snd_soc_dapm_dai_link:
  2657. w->power_check = dapm_generic_check_power;
  2658. break;
  2659. case snd_soc_dapm_supply:
  2660. case snd_soc_dapm_regulator_supply:
  2661. case snd_soc_dapm_clock_supply:
  2662. w->power_check = dapm_supply_check_power;
  2663. break;
  2664. case snd_soc_dapm_dai:
  2665. w->power_check = dapm_dai_check_power;
  2666. break;
  2667. default:
  2668. w->power_check = dapm_always_on_check_power;
  2669. break;
  2670. }
  2671. dapm->n_widgets++;
  2672. w->dapm = dapm;
  2673. w->codec = dapm->codec;
  2674. w->platform = dapm->platform;
  2675. INIT_LIST_HEAD(&w->sources);
  2676. INIT_LIST_HEAD(&w->sinks);
  2677. INIT_LIST_HEAD(&w->list);
  2678. INIT_LIST_HEAD(&w->dirty);
  2679. list_add(&w->list, &dapm->card->widgets);
  2680. /* machine layer set ups unconnected pins and insertions */
  2681. w->connected = 1;
  2682. return w;
  2683. }
  2684. /**
  2685. * snd_soc_dapm_new_controls - create new dapm controls
  2686. * @dapm: DAPM context
  2687. * @widget: widget array
  2688. * @num: number of widgets
  2689. *
  2690. * Creates new DAPM controls based upon the templates.
  2691. *
  2692. * Returns 0 for success else error.
  2693. */
  2694. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  2695. const struct snd_soc_dapm_widget *widget,
  2696. int num)
  2697. {
  2698. struct snd_soc_dapm_widget *w;
  2699. int i;
  2700. int ret = 0;
  2701. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2702. for (i = 0; i < num; i++) {
  2703. w = snd_soc_dapm_new_control(dapm, widget);
  2704. if (!w) {
  2705. dev_err(dapm->dev,
  2706. "ASoC: Failed to create DAPM control %s\n",
  2707. widget->name);
  2708. ret = -ENOMEM;
  2709. break;
  2710. }
  2711. widget++;
  2712. }
  2713. mutex_unlock(&dapm->card->dapm_mutex);
  2714. return ret;
  2715. }
  2716. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  2717. static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
  2718. struct snd_kcontrol *kcontrol, int event)
  2719. {
  2720. struct snd_soc_dapm_path *source_p, *sink_p;
  2721. struct snd_soc_dai *source, *sink;
  2722. const struct snd_soc_pcm_stream *config = w->params;
  2723. struct snd_pcm_substream substream;
  2724. struct snd_pcm_hw_params *params = NULL;
  2725. u64 fmt;
  2726. int ret;
  2727. BUG_ON(!config);
  2728. BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
  2729. /* We only support a single source and sink, pick the first */
  2730. source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
  2731. list_sink);
  2732. sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
  2733. list_source);
  2734. BUG_ON(!source_p || !sink_p);
  2735. BUG_ON(!sink_p->source || !source_p->sink);
  2736. BUG_ON(!source_p->source || !sink_p->sink);
  2737. source = source_p->source->priv;
  2738. sink = sink_p->sink->priv;
  2739. /* Be a little careful as we don't want to overflow the mask array */
  2740. if (config->formats) {
  2741. fmt = ffs(config->formats) - 1;
  2742. } else {
  2743. dev_warn(w->dapm->dev, "Invalid format %llx specified\n",
  2744. config->formats);
  2745. fmt = 0;
  2746. }
  2747. /* Currently very limited parameter selection */
  2748. params = kzalloc(sizeof(*params), GFP_KERNEL);
  2749. if (!params) {
  2750. ret = -ENOMEM;
  2751. goto out;
  2752. }
  2753. snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
  2754. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
  2755. config->rate_min;
  2756. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
  2757. config->rate_max;
  2758. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
  2759. = config->channels_min;
  2760. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
  2761. = config->channels_max;
  2762. memset(&substream, 0, sizeof(substream));
  2763. switch (event) {
  2764. case SND_SOC_DAPM_PRE_PMU:
  2765. if (source->driver->ops && source->driver->ops->hw_params) {
  2766. substream.stream = SNDRV_PCM_STREAM_CAPTURE;
  2767. ret = source->driver->ops->hw_params(&substream,
  2768. params, source);
  2769. if (ret != 0) {
  2770. dev_err(source->dev,
  2771. "hw_params() failed: %d\n", ret);
  2772. goto out;
  2773. }
  2774. }
  2775. if (sink->driver->ops && sink->driver->ops->hw_params) {
  2776. substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
  2777. ret = sink->driver->ops->hw_params(&substream, params,
  2778. sink);
  2779. if (ret != 0) {
  2780. dev_err(sink->dev,
  2781. "hw_params() failed: %d\n", ret);
  2782. goto out;
  2783. }
  2784. }
  2785. break;
  2786. case SND_SOC_DAPM_POST_PMU:
  2787. ret = snd_soc_dai_digital_mute(sink, 0);
  2788. if (ret != 0 && ret != -ENOTSUPP)
  2789. dev_warn(sink->dev, "Failed to unmute: %d\n", ret);
  2790. ret = 0;
  2791. break;
  2792. case SND_SOC_DAPM_PRE_PMD:
  2793. ret = snd_soc_dai_digital_mute(sink, 1);
  2794. if (ret != 0 && ret != -ENOTSUPP)
  2795. dev_warn(sink->dev, "Failed to mute: %d\n", ret);
  2796. ret = 0;
  2797. break;
  2798. default:
  2799. BUG();
  2800. return -EINVAL;
  2801. }
  2802. out:
  2803. kfree(params);
  2804. return ret;
  2805. }
  2806. int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
  2807. const struct snd_soc_pcm_stream *params,
  2808. struct snd_soc_dapm_widget *source,
  2809. struct snd_soc_dapm_widget *sink)
  2810. {
  2811. struct snd_soc_dapm_route routes[2];
  2812. struct snd_soc_dapm_widget template;
  2813. struct snd_soc_dapm_widget *w;
  2814. size_t len;
  2815. char *link_name;
  2816. len = strlen(source->name) + strlen(sink->name) + 2;
  2817. link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
  2818. if (!link_name)
  2819. return -ENOMEM;
  2820. snprintf(link_name, len, "%s-%s", source->name, sink->name);
  2821. memset(&template, 0, sizeof(template));
  2822. template.reg = SND_SOC_NOPM;
  2823. template.id = snd_soc_dapm_dai_link;
  2824. template.name = link_name;
  2825. template.event = snd_soc_dai_link_event;
  2826. template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2827. SND_SOC_DAPM_PRE_PMD;
  2828. dev_dbg(card->dev, "adding %s widget\n", link_name);
  2829. w = snd_soc_dapm_new_control(&card->dapm, &template);
  2830. if (!w) {
  2831. dev_err(card->dev, "Failed to create %s widget\n",
  2832. link_name);
  2833. return -ENOMEM;
  2834. }
  2835. w->params = params;
  2836. memset(&routes, 0, sizeof(routes));
  2837. routes[0].source = source->name;
  2838. routes[0].sink = link_name;
  2839. routes[1].source = link_name;
  2840. routes[1].sink = sink->name;
  2841. return snd_soc_dapm_add_routes(&card->dapm, routes,
  2842. ARRAY_SIZE(routes));
  2843. }
  2844. int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
  2845. struct snd_soc_dai *dai)
  2846. {
  2847. struct snd_soc_dapm_widget template;
  2848. struct snd_soc_dapm_widget *w;
  2849. WARN_ON(dapm->dev != dai->dev);
  2850. memset(&template, 0, sizeof(template));
  2851. template.reg = SND_SOC_NOPM;
  2852. if (dai->driver->playback.stream_name) {
  2853. template.id = snd_soc_dapm_dai;
  2854. template.name = dai->driver->playback.stream_name;
  2855. template.sname = dai->driver->playback.stream_name;
  2856. dev_dbg(dai->dev, "adding %s widget\n",
  2857. template.name);
  2858. w = snd_soc_dapm_new_control(dapm, &template);
  2859. if (!w) {
  2860. dev_err(dapm->dev, "Failed to create %s widget\n",
  2861. dai->driver->playback.stream_name);
  2862. }
  2863. w->priv = dai;
  2864. dai->playback_widget = w;
  2865. }
  2866. if (dai->driver->capture.stream_name) {
  2867. template.id = snd_soc_dapm_dai;
  2868. template.name = dai->driver->capture.stream_name;
  2869. template.sname = dai->driver->capture.stream_name;
  2870. dev_dbg(dai->dev, "adding %s widget\n",
  2871. template.name);
  2872. w = snd_soc_dapm_new_control(dapm, &template);
  2873. if (!w) {
  2874. dev_err(dapm->dev, "Failed to create %s widget\n",
  2875. dai->driver->capture.stream_name);
  2876. }
  2877. w->priv = dai;
  2878. dai->capture_widget = w;
  2879. }
  2880. return 0;
  2881. }
  2882. int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
  2883. {
  2884. struct snd_soc_dapm_widget *dai_w, *w;
  2885. struct snd_soc_dai *dai;
  2886. struct snd_soc_dapm_route r;
  2887. memset(&r, 0, sizeof(r));
  2888. /* For each DAI widget... */
  2889. list_for_each_entry(dai_w, &card->widgets, list) {
  2890. if (dai_w->id != snd_soc_dapm_dai)
  2891. continue;
  2892. dai = dai_w->priv;
  2893. /* ...find all widgets with the same stream and link them */
  2894. list_for_each_entry(w, &card->widgets, list) {
  2895. if (w->dapm != dai_w->dapm)
  2896. continue;
  2897. if (w->id == snd_soc_dapm_dai)
  2898. continue;
  2899. if (!w->sname)
  2900. continue;
  2901. if (dai->driver->playback.stream_name &&
  2902. strstr(w->sname,
  2903. dai->driver->playback.stream_name)) {
  2904. r.source = dai->playback_widget->name;
  2905. r.sink = w->name;
  2906. dev_dbg(dai->dev, "%s -> %s\n",
  2907. r.source, r.sink);
  2908. snd_soc_dapm_add_route(w->dapm, &r);
  2909. }
  2910. if (dai->driver->capture.stream_name &&
  2911. strstr(w->sname,
  2912. dai->driver->capture.stream_name)) {
  2913. r.source = w->name;
  2914. r.sink = dai->capture_widget->name;
  2915. dev_dbg(dai->dev, "%s -> %s\n",
  2916. r.source, r.sink);
  2917. snd_soc_dapm_add_route(w->dapm, &r);
  2918. }
  2919. }
  2920. }
  2921. return 0;
  2922. }
  2923. static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  2924. int event)
  2925. {
  2926. struct snd_soc_dapm_widget *w_cpu, *w_codec;
  2927. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2928. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  2929. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2930. w_cpu = cpu_dai->playback_widget;
  2931. w_codec = codec_dai->playback_widget;
  2932. } else {
  2933. w_cpu = cpu_dai->capture_widget;
  2934. w_codec = codec_dai->capture_widget;
  2935. }
  2936. if (w_cpu) {
  2937. dapm_mark_dirty(w_cpu, "stream event");
  2938. switch (event) {
  2939. case SND_SOC_DAPM_STREAM_START:
  2940. w_cpu->active = 1;
  2941. break;
  2942. case SND_SOC_DAPM_STREAM_STOP:
  2943. w_cpu->active = 0;
  2944. break;
  2945. case SND_SOC_DAPM_STREAM_SUSPEND:
  2946. case SND_SOC_DAPM_STREAM_RESUME:
  2947. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2948. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2949. break;
  2950. }
  2951. }
  2952. if (w_codec) {
  2953. dapm_mark_dirty(w_codec, "stream event");
  2954. switch (event) {
  2955. case SND_SOC_DAPM_STREAM_START:
  2956. w_codec->active = 1;
  2957. break;
  2958. case SND_SOC_DAPM_STREAM_STOP:
  2959. w_codec->active = 0;
  2960. break;
  2961. case SND_SOC_DAPM_STREAM_SUSPEND:
  2962. case SND_SOC_DAPM_STREAM_RESUME:
  2963. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2964. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2965. break;
  2966. }
  2967. }
  2968. dapm_power_widgets(&rtd->card->dapm, event);
  2969. }
  2970. /**
  2971. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  2972. * @rtd: PCM runtime data
  2973. * @stream: stream name
  2974. * @event: stream event
  2975. *
  2976. * Sends a stream event to the dapm core. The core then makes any
  2977. * necessary widget power changes.
  2978. *
  2979. * Returns 0 for success else error.
  2980. */
  2981. void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  2982. int event)
  2983. {
  2984. struct snd_soc_card *card = rtd->card;
  2985. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2986. soc_dapm_stream_event(rtd, stream, event);
  2987. mutex_unlock(&card->dapm_mutex);
  2988. }
  2989. /**
  2990. * snd_soc_dapm_enable_pin - enable pin.
  2991. * @dapm: DAPM context
  2992. * @pin: pin name
  2993. *
  2994. * Enables input/output pin and its parents or children widgets iff there is
  2995. * a valid audio route and active audio stream.
  2996. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2997. * do any widget power switching.
  2998. */
  2999. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3000. {
  3001. return snd_soc_dapm_set_pin(dapm, pin, 1);
  3002. }
  3003. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  3004. /**
  3005. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  3006. * @dapm: DAPM context
  3007. * @pin: pin name
  3008. *
  3009. * Enables input/output pin regardless of any other state. This is
  3010. * intended for use with microphone bias supplies used in microphone
  3011. * jack detection.
  3012. *
  3013. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3014. * do any widget power switching.
  3015. */
  3016. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  3017. const char *pin)
  3018. {
  3019. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3020. if (!w) {
  3021. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  3022. return -EINVAL;
  3023. }
  3024. dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
  3025. w->connected = 1;
  3026. w->force = 1;
  3027. dapm_mark_dirty(w, "force enable");
  3028. return 0;
  3029. }
  3030. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  3031. /**
  3032. * snd_soc_dapm_disable_pin - disable pin.
  3033. * @dapm: DAPM context
  3034. * @pin: pin name
  3035. *
  3036. * Disables input/output pin and its parents or children widgets.
  3037. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3038. * do any widget power switching.
  3039. */
  3040. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  3041. const char *pin)
  3042. {
  3043. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3044. }
  3045. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  3046. /**
  3047. * snd_soc_dapm_nc_pin - permanently disable pin.
  3048. * @dapm: DAPM context
  3049. * @pin: pin name
  3050. *
  3051. * Marks the specified pin as being not connected, disabling it along
  3052. * any parent or child widgets. At present this is identical to
  3053. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  3054. * additional things such as disabling controls which only affect
  3055. * paths through the pin.
  3056. *
  3057. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3058. * do any widget power switching.
  3059. */
  3060. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3061. {
  3062. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3063. }
  3064. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  3065. /**
  3066. * snd_soc_dapm_get_pin_status - get audio pin status
  3067. * @dapm: DAPM context
  3068. * @pin: audio signal pin endpoint (or start point)
  3069. *
  3070. * Get audio pin status - connected or disconnected.
  3071. *
  3072. * Returns 1 for connected otherwise 0.
  3073. */
  3074. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  3075. const char *pin)
  3076. {
  3077. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3078. if (w)
  3079. return w->connected;
  3080. return 0;
  3081. }
  3082. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  3083. /**
  3084. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  3085. * @dapm: DAPM context
  3086. * @pin: audio signal pin endpoint (or start point)
  3087. *
  3088. * Mark the given endpoint or pin as ignoring suspend. When the
  3089. * system is disabled a path between two endpoints flagged as ignoring
  3090. * suspend will not be disabled. The path must already be enabled via
  3091. * normal means at suspend time, it will not be turned on if it was not
  3092. * already enabled.
  3093. */
  3094. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  3095. const char *pin)
  3096. {
  3097. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  3098. if (!w) {
  3099. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  3100. return -EINVAL;
  3101. }
  3102. w->ignore_suspend = 1;
  3103. return 0;
  3104. }
  3105. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  3106. static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
  3107. struct snd_soc_dapm_widget *w)
  3108. {
  3109. struct snd_soc_dapm_path *p;
  3110. list_for_each_entry(p, &card->paths, list) {
  3111. if ((p->source == w) || (p->sink == w)) {
  3112. dev_dbg(card->dev,
  3113. "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
  3114. p->source->name, p->source->id, p->source->dapm,
  3115. p->sink->name, p->sink->id, p->sink->dapm);
  3116. /* Connected to something other than the codec */
  3117. if (p->source->dapm != p->sink->dapm)
  3118. return true;
  3119. /*
  3120. * Loopback connection from codec external pin to
  3121. * codec external pin
  3122. */
  3123. if (p->sink->id == snd_soc_dapm_input) {
  3124. switch (p->source->id) {
  3125. case snd_soc_dapm_output:
  3126. case snd_soc_dapm_micbias:
  3127. return true;
  3128. default:
  3129. break;
  3130. }
  3131. }
  3132. }
  3133. }
  3134. return false;
  3135. }
  3136. /**
  3137. * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
  3138. * @codec: The codec whose pins should be processed
  3139. *
  3140. * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
  3141. * which are unused. Pins are used if they are connected externally to the
  3142. * codec, whether that be to some other device, or a loop-back connection to
  3143. * the codec itself.
  3144. */
  3145. void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
  3146. {
  3147. struct snd_soc_card *card = codec->card;
  3148. struct snd_soc_dapm_context *dapm = &codec->dapm;
  3149. struct snd_soc_dapm_widget *w;
  3150. dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
  3151. &card->dapm, &codec->dapm);
  3152. list_for_each_entry(w, &card->widgets, list) {
  3153. if (w->dapm != dapm)
  3154. continue;
  3155. switch (w->id) {
  3156. case snd_soc_dapm_input:
  3157. case snd_soc_dapm_output:
  3158. case snd_soc_dapm_micbias:
  3159. dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
  3160. w->name);
  3161. if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
  3162. dev_dbg(codec->dev,
  3163. "... Not in map; disabling\n");
  3164. snd_soc_dapm_nc_pin(dapm, w->name);
  3165. }
  3166. break;
  3167. default:
  3168. break;
  3169. }
  3170. }
  3171. }
  3172. /**
  3173. * snd_soc_dapm_free - free dapm resources
  3174. * @dapm: DAPM context
  3175. *
  3176. * Free all dapm widgets and resources.
  3177. */
  3178. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  3179. {
  3180. snd_soc_dapm_sys_remove(dapm->dev);
  3181. dapm_debugfs_cleanup(dapm);
  3182. dapm_free_widgets(dapm);
  3183. list_del(&dapm->list);
  3184. }
  3185. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  3186. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  3187. {
  3188. struct snd_soc_card *card = dapm->card;
  3189. struct snd_soc_dapm_widget *w;
  3190. LIST_HEAD(down_list);
  3191. int powerdown = 0;
  3192. mutex_lock(&card->dapm_mutex);
  3193. list_for_each_entry(w, &dapm->card->widgets, list) {
  3194. if (w->dapm != dapm)
  3195. continue;
  3196. if (w->power) {
  3197. dapm_seq_insert(w, &down_list, false);
  3198. w->power = 0;
  3199. powerdown = 1;
  3200. }
  3201. }
  3202. /* If there were no widgets to power down we're already in
  3203. * standby.
  3204. */
  3205. if (powerdown) {
  3206. if (dapm->bias_level == SND_SOC_BIAS_ON)
  3207. snd_soc_dapm_set_bias_level(dapm,
  3208. SND_SOC_BIAS_PREPARE);
  3209. dapm_seq_run(dapm, &down_list, 0, false);
  3210. if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
  3211. snd_soc_dapm_set_bias_level(dapm,
  3212. SND_SOC_BIAS_STANDBY);
  3213. }
  3214. mutex_unlock(&card->dapm_mutex);
  3215. }
  3216. /*
  3217. * snd_soc_dapm_shutdown - callback for system shutdown
  3218. */
  3219. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  3220. {
  3221. struct snd_soc_codec *codec;
  3222. list_for_each_entry(codec, &card->codec_dev_list, list) {
  3223. soc_dapm_shutdown_codec(&codec->dapm);
  3224. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  3225. snd_soc_dapm_set_bias_level(&codec->dapm,
  3226. SND_SOC_BIAS_OFF);
  3227. }
  3228. }
  3229. /* Module information */
  3230. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3231. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  3232. MODULE_LICENSE("GPL");