soc-dapm.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795
  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, "ASoC: 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, "ASoC: 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, "ASoC: failed to add widget"
  526. " %s dapm kcontrol %s: %d\n",
  527. w->name, 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, "ASoC: 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, "ASoC: %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, "ASoC: 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, "ASoC: 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->walking)
  729. return 1;
  730. if (path->walked)
  731. continue;
  732. trace_snd_soc_dapm_output_path(widget, path);
  733. if (path->sink && path->connect) {
  734. path->walked = 1;
  735. path->walking = 1;
  736. /* do we need to add this widget to the list ? */
  737. if (list) {
  738. int err;
  739. err = dapm_list_add_widget(list, path->sink);
  740. if (err < 0) {
  741. dev_err(widget->dapm->dev,
  742. "ASoC: could not add widget %s\n",
  743. widget->name);
  744. path->walking = 0;
  745. return con;
  746. }
  747. }
  748. con += is_connected_output_ep(path->sink, list);
  749. path->walking = 0;
  750. }
  751. }
  752. widget->outputs = con;
  753. return con;
  754. }
  755. /*
  756. * Recursively check for a completed path to an active or physically connected
  757. * input widget. Returns number of complete paths.
  758. */
  759. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
  760. struct snd_soc_dapm_widget_list **list)
  761. {
  762. struct snd_soc_dapm_path *path;
  763. int con = 0;
  764. if (widget->inputs >= 0)
  765. return widget->inputs;
  766. DAPM_UPDATE_STAT(widget, path_checks);
  767. switch (widget->id) {
  768. case snd_soc_dapm_supply:
  769. case snd_soc_dapm_regulator_supply:
  770. case snd_soc_dapm_clock_supply:
  771. return 0;
  772. default:
  773. break;
  774. }
  775. /* active stream ? */
  776. switch (widget->id) {
  777. case snd_soc_dapm_dac:
  778. case snd_soc_dapm_aif_in:
  779. case snd_soc_dapm_dai:
  780. if (widget->active) {
  781. widget->inputs = snd_soc_dapm_suspend_check(widget);
  782. return widget->inputs;
  783. }
  784. default:
  785. break;
  786. }
  787. if (widget->connected) {
  788. /* connected pin ? */
  789. if (widget->id == snd_soc_dapm_input && !widget->ext) {
  790. widget->inputs = snd_soc_dapm_suspend_check(widget);
  791. return widget->inputs;
  792. }
  793. /* connected VMID/Bias for lower pops */
  794. if (widget->id == snd_soc_dapm_vmid) {
  795. widget->inputs = snd_soc_dapm_suspend_check(widget);
  796. return widget->inputs;
  797. }
  798. /* connected jack ? */
  799. if (widget->id == snd_soc_dapm_mic ||
  800. (widget->id == snd_soc_dapm_line &&
  801. !list_empty(&widget->sinks))) {
  802. widget->inputs = snd_soc_dapm_suspend_check(widget);
  803. return widget->inputs;
  804. }
  805. /* signal generator */
  806. if (widget->id == snd_soc_dapm_siggen) {
  807. widget->inputs = snd_soc_dapm_suspend_check(widget);
  808. return widget->inputs;
  809. }
  810. }
  811. list_for_each_entry(path, &widget->sources, list_sink) {
  812. DAPM_UPDATE_STAT(widget, neighbour_checks);
  813. if (path->weak)
  814. continue;
  815. if (path->walking)
  816. return 1;
  817. if (path->walked)
  818. continue;
  819. trace_snd_soc_dapm_input_path(widget, path);
  820. if (path->source && path->connect) {
  821. path->walked = 1;
  822. path->walking = 1;
  823. /* do we need to add this widget to the list ? */
  824. if (list) {
  825. int err;
  826. err = dapm_list_add_widget(list, path->source);
  827. if (err < 0) {
  828. dev_err(widget->dapm->dev,
  829. "ASoC: could not add widget %s\n",
  830. widget->name);
  831. path->walking = 0;
  832. return con;
  833. }
  834. }
  835. con += is_connected_input_ep(path->source, list);
  836. path->walking = 0;
  837. }
  838. }
  839. widget->inputs = con;
  840. return con;
  841. }
  842. /**
  843. * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
  844. * @dai: the soc DAI.
  845. * @stream: stream direction.
  846. * @list: list of active widgets for this stream.
  847. *
  848. * Queries DAPM graph as to whether an valid audio stream path exists for
  849. * the initial stream specified by name. This takes into account
  850. * current mixer and mux kcontrol settings. Creates list of valid widgets.
  851. *
  852. * Returns the number of valid paths or negative error.
  853. */
  854. int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
  855. struct snd_soc_dapm_widget_list **list)
  856. {
  857. struct snd_soc_card *card = dai->card;
  858. int paths;
  859. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  860. dapm_reset(card);
  861. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  862. paths = is_connected_output_ep(dai->playback_widget, list);
  863. else
  864. paths = is_connected_input_ep(dai->capture_widget, list);
  865. trace_snd_soc_dapm_connected(paths, stream);
  866. dapm_clear_walk(&card->dapm);
  867. mutex_unlock(&card->dapm_mutex);
  868. return paths;
  869. }
  870. /*
  871. * Handler for generic register modifier widget.
  872. */
  873. int dapm_reg_event(struct snd_soc_dapm_widget *w,
  874. struct snd_kcontrol *kcontrol, int event)
  875. {
  876. unsigned int val;
  877. if (SND_SOC_DAPM_EVENT_ON(event))
  878. val = w->on_val;
  879. else
  880. val = w->off_val;
  881. soc_widget_update_bits_locked(w, -(w->reg + 1),
  882. w->mask << w->shift, val << w->shift);
  883. return 0;
  884. }
  885. EXPORT_SYMBOL_GPL(dapm_reg_event);
  886. /*
  887. * Handler for regulator supply widget.
  888. */
  889. int dapm_regulator_event(struct snd_soc_dapm_widget *w,
  890. struct snd_kcontrol *kcontrol, int event)
  891. {
  892. int ret;
  893. if (SND_SOC_DAPM_EVENT_ON(event)) {
  894. if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
  895. ret = regulator_allow_bypass(w->regulator, false);
  896. if (ret != 0)
  897. dev_warn(w->dapm->dev,
  898. "ASoC: Failed to bypass %s: %d\n",
  899. w->name, ret);
  900. }
  901. return regulator_enable(w->regulator);
  902. } else {
  903. if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
  904. ret = regulator_allow_bypass(w->regulator, true);
  905. if (ret != 0)
  906. dev_warn(w->dapm->dev,
  907. "ASoC: Failed to unbypass %s: %d\n",
  908. w->name, ret);
  909. }
  910. return regulator_disable_deferred(w->regulator, w->shift);
  911. }
  912. }
  913. EXPORT_SYMBOL_GPL(dapm_regulator_event);
  914. /*
  915. * Handler for clock supply widget.
  916. */
  917. int dapm_clock_event(struct snd_soc_dapm_widget *w,
  918. struct snd_kcontrol *kcontrol, int event)
  919. {
  920. if (!w->clk)
  921. return -EIO;
  922. #ifdef CONFIG_HAVE_CLK
  923. if (SND_SOC_DAPM_EVENT_ON(event)) {
  924. return clk_enable(w->clk);
  925. } else {
  926. clk_disable(w->clk);
  927. return 0;
  928. }
  929. #endif
  930. return 0;
  931. }
  932. EXPORT_SYMBOL_GPL(dapm_clock_event);
  933. static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
  934. {
  935. if (w->power_checked)
  936. return w->new_power;
  937. if (w->force)
  938. w->new_power = 1;
  939. else
  940. w->new_power = w->power_check(w);
  941. w->power_checked = true;
  942. return w->new_power;
  943. }
  944. /* Generic check to see if a widget should be powered.
  945. */
  946. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  947. {
  948. int in, out;
  949. DAPM_UPDATE_STAT(w, power_checks);
  950. in = is_connected_input_ep(w, NULL);
  951. dapm_clear_walk(w->dapm);
  952. out = is_connected_output_ep(w, NULL);
  953. dapm_clear_walk(w->dapm);
  954. return out != 0 && in != 0;
  955. }
  956. static int dapm_dai_check_power(struct snd_soc_dapm_widget *w)
  957. {
  958. DAPM_UPDATE_STAT(w, power_checks);
  959. if (w->active)
  960. return w->active;
  961. return dapm_generic_check_power(w);
  962. }
  963. /* Check to see if an ADC has power */
  964. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  965. {
  966. int in;
  967. DAPM_UPDATE_STAT(w, power_checks);
  968. if (w->active) {
  969. in = is_connected_input_ep(w, NULL);
  970. dapm_clear_walk(w->dapm);
  971. return in != 0;
  972. } else {
  973. return dapm_generic_check_power(w);
  974. }
  975. }
  976. /* Check to see if a DAC has power */
  977. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  978. {
  979. int out;
  980. DAPM_UPDATE_STAT(w, power_checks);
  981. if (w->active) {
  982. out = is_connected_output_ep(w, NULL);
  983. dapm_clear_walk(w->dapm);
  984. return out != 0;
  985. } else {
  986. return dapm_generic_check_power(w);
  987. }
  988. }
  989. /* Check to see if a power supply is needed */
  990. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  991. {
  992. struct snd_soc_dapm_path *path;
  993. DAPM_UPDATE_STAT(w, power_checks);
  994. /* Check if one of our outputs is connected */
  995. list_for_each_entry(path, &w->sinks, list_source) {
  996. DAPM_UPDATE_STAT(w, neighbour_checks);
  997. if (path->weak)
  998. continue;
  999. if (path->connected &&
  1000. !path->connected(path->source, path->sink))
  1001. continue;
  1002. if (!path->sink)
  1003. continue;
  1004. if (dapm_widget_power_check(path->sink))
  1005. return 1;
  1006. }
  1007. dapm_clear_walk(w->dapm);
  1008. return 0;
  1009. }
  1010. static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
  1011. {
  1012. return 1;
  1013. }
  1014. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  1015. struct snd_soc_dapm_widget *b,
  1016. bool power_up)
  1017. {
  1018. int *sort;
  1019. if (power_up)
  1020. sort = dapm_up_seq;
  1021. else
  1022. sort = dapm_down_seq;
  1023. if (sort[a->id] != sort[b->id])
  1024. return sort[a->id] - sort[b->id];
  1025. if (a->subseq != b->subseq) {
  1026. if (power_up)
  1027. return a->subseq - b->subseq;
  1028. else
  1029. return b->subseq - a->subseq;
  1030. }
  1031. if (a->reg != b->reg)
  1032. return a->reg - b->reg;
  1033. if (a->dapm != b->dapm)
  1034. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  1035. return 0;
  1036. }
  1037. /* Insert a widget in order into a DAPM power sequence. */
  1038. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  1039. struct list_head *list,
  1040. bool power_up)
  1041. {
  1042. struct snd_soc_dapm_widget *w;
  1043. list_for_each_entry(w, list, power_list)
  1044. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  1045. list_add_tail(&new_widget->power_list, &w->power_list);
  1046. return;
  1047. }
  1048. list_add_tail(&new_widget->power_list, list);
  1049. }
  1050. static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
  1051. struct snd_soc_dapm_widget *w, int event)
  1052. {
  1053. struct snd_soc_card *card = dapm->card;
  1054. const char *ev_name;
  1055. int power, ret;
  1056. switch (event) {
  1057. case SND_SOC_DAPM_PRE_PMU:
  1058. ev_name = "PRE_PMU";
  1059. power = 1;
  1060. break;
  1061. case SND_SOC_DAPM_POST_PMU:
  1062. ev_name = "POST_PMU";
  1063. power = 1;
  1064. break;
  1065. case SND_SOC_DAPM_PRE_PMD:
  1066. ev_name = "PRE_PMD";
  1067. power = 0;
  1068. break;
  1069. case SND_SOC_DAPM_POST_PMD:
  1070. ev_name = "POST_PMD";
  1071. power = 0;
  1072. break;
  1073. default:
  1074. BUG();
  1075. return;
  1076. }
  1077. if (w->power != power)
  1078. return;
  1079. if (w->event && (w->event_flags & event)) {
  1080. pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
  1081. w->name, ev_name);
  1082. trace_snd_soc_dapm_widget_event_start(w, event);
  1083. ret = w->event(w, NULL, event);
  1084. trace_snd_soc_dapm_widget_event_done(w, event);
  1085. if (ret < 0)
  1086. dev_err(dapm->dev, "ASoC: %s: %s event failed: %d\n",
  1087. ev_name, w->name, ret);
  1088. }
  1089. }
  1090. /* Apply the coalesced changes from a DAPM sequence */
  1091. static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
  1092. struct list_head *pending)
  1093. {
  1094. struct snd_soc_card *card = dapm->card;
  1095. struct snd_soc_dapm_widget *w;
  1096. int reg, power;
  1097. unsigned int value = 0;
  1098. unsigned int mask = 0;
  1099. unsigned int cur_mask;
  1100. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  1101. power_list)->reg;
  1102. list_for_each_entry(w, pending, power_list) {
  1103. cur_mask = 1 << w->shift;
  1104. BUG_ON(reg != w->reg);
  1105. if (w->invert)
  1106. power = !w->power;
  1107. else
  1108. power = w->power;
  1109. mask |= cur_mask;
  1110. if (power)
  1111. value |= cur_mask;
  1112. pop_dbg(dapm->dev, card->pop_time,
  1113. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  1114. w->name, reg, value, mask);
  1115. /* Check for events */
  1116. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
  1117. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
  1118. }
  1119. if (reg >= 0) {
  1120. /* Any widget will do, they should all be updating the
  1121. * same register.
  1122. */
  1123. w = list_first_entry(pending, struct snd_soc_dapm_widget,
  1124. power_list);
  1125. pop_dbg(dapm->dev, card->pop_time,
  1126. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  1127. value, mask, reg, card->pop_time);
  1128. pop_wait(card->pop_time);
  1129. soc_widget_update_bits_locked(w, reg, mask, value);
  1130. }
  1131. list_for_each_entry(w, pending, power_list) {
  1132. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
  1133. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
  1134. }
  1135. }
  1136. /* Apply a DAPM power sequence.
  1137. *
  1138. * We walk over a pre-sorted list of widgets to apply power to. In
  1139. * order to minimise the number of writes to the device required
  1140. * multiple widgets will be updated in a single write where possible.
  1141. * Currently anything that requires more than a single write is not
  1142. * handled.
  1143. */
  1144. static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
  1145. struct list_head *list, int event, bool power_up)
  1146. {
  1147. struct snd_soc_dapm_widget *w, *n;
  1148. LIST_HEAD(pending);
  1149. int cur_sort = -1;
  1150. int cur_subseq = -1;
  1151. int cur_reg = SND_SOC_NOPM;
  1152. struct snd_soc_dapm_context *cur_dapm = NULL;
  1153. int ret, i;
  1154. int *sort;
  1155. if (power_up)
  1156. sort = dapm_up_seq;
  1157. else
  1158. sort = dapm_down_seq;
  1159. list_for_each_entry_safe(w, n, list, power_list) {
  1160. ret = 0;
  1161. /* Do we need to apply any queued changes? */
  1162. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  1163. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  1164. if (!list_empty(&pending))
  1165. dapm_seq_run_coalesced(cur_dapm, &pending);
  1166. if (cur_dapm && cur_dapm->seq_notifier) {
  1167. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1168. if (sort[i] == cur_sort)
  1169. cur_dapm->seq_notifier(cur_dapm,
  1170. i,
  1171. cur_subseq);
  1172. }
  1173. INIT_LIST_HEAD(&pending);
  1174. cur_sort = -1;
  1175. cur_subseq = INT_MIN;
  1176. cur_reg = SND_SOC_NOPM;
  1177. cur_dapm = NULL;
  1178. }
  1179. switch (w->id) {
  1180. case snd_soc_dapm_pre:
  1181. if (!w->event)
  1182. list_for_each_entry_safe_continue(w, n, list,
  1183. power_list);
  1184. if (event == SND_SOC_DAPM_STREAM_START)
  1185. ret = w->event(w,
  1186. NULL, SND_SOC_DAPM_PRE_PMU);
  1187. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1188. ret = w->event(w,
  1189. NULL, SND_SOC_DAPM_PRE_PMD);
  1190. break;
  1191. case snd_soc_dapm_post:
  1192. if (!w->event)
  1193. list_for_each_entry_safe_continue(w, n, list,
  1194. power_list);
  1195. if (event == SND_SOC_DAPM_STREAM_START)
  1196. ret = w->event(w,
  1197. NULL, SND_SOC_DAPM_POST_PMU);
  1198. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1199. ret = w->event(w,
  1200. NULL, SND_SOC_DAPM_POST_PMD);
  1201. break;
  1202. default:
  1203. /* Queue it up for application */
  1204. cur_sort = sort[w->id];
  1205. cur_subseq = w->subseq;
  1206. cur_reg = w->reg;
  1207. cur_dapm = w->dapm;
  1208. list_move(&w->power_list, &pending);
  1209. break;
  1210. }
  1211. if (ret < 0)
  1212. dev_err(w->dapm->dev,
  1213. "ASoC: Failed to apply widget power: %d\n", ret);
  1214. }
  1215. if (!list_empty(&pending))
  1216. dapm_seq_run_coalesced(cur_dapm, &pending);
  1217. if (cur_dapm && cur_dapm->seq_notifier) {
  1218. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1219. if (sort[i] == cur_sort)
  1220. cur_dapm->seq_notifier(cur_dapm,
  1221. i, cur_subseq);
  1222. }
  1223. }
  1224. static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
  1225. {
  1226. struct snd_soc_dapm_update *update = dapm->update;
  1227. struct snd_soc_dapm_widget *w;
  1228. int ret;
  1229. if (!update)
  1230. return;
  1231. w = update->widget;
  1232. if (w->event &&
  1233. (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  1234. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  1235. if (ret != 0)
  1236. dev_err(dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
  1237. w->name, ret);
  1238. }
  1239. ret = soc_widget_update_bits_locked(w, update->reg, update->mask,
  1240. update->val);
  1241. if (ret < 0)
  1242. dev_err(dapm->dev, "ASoC: %s DAPM update failed: %d\n",
  1243. w->name, ret);
  1244. if (w->event &&
  1245. (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  1246. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  1247. if (ret != 0)
  1248. dev_err(dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
  1249. w->name, ret);
  1250. }
  1251. }
  1252. /* Async callback run prior to DAPM sequences - brings to _PREPARE if
  1253. * they're changing state.
  1254. */
  1255. static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
  1256. {
  1257. struct snd_soc_dapm_context *d = data;
  1258. int ret;
  1259. /* If we're off and we're not supposed to be go into STANDBY */
  1260. if (d->bias_level == SND_SOC_BIAS_OFF &&
  1261. d->target_bias_level != SND_SOC_BIAS_OFF) {
  1262. if (d->dev)
  1263. pm_runtime_get_sync(d->dev);
  1264. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1265. if (ret != 0)
  1266. dev_err(d->dev,
  1267. "ASoC: Failed to turn on bias: %d\n", ret);
  1268. }
  1269. /* Prepare for a STADDBY->ON or ON->STANDBY transition */
  1270. if (d->bias_level != d->target_bias_level) {
  1271. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
  1272. if (ret != 0)
  1273. dev_err(d->dev,
  1274. "ASoC: Failed to prepare bias: %d\n", ret);
  1275. }
  1276. }
  1277. /* Async callback run prior to DAPM sequences - brings to their final
  1278. * state.
  1279. */
  1280. static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
  1281. {
  1282. struct snd_soc_dapm_context *d = data;
  1283. int ret;
  1284. /* If we just powered the last thing off drop to standby bias */
  1285. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1286. (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
  1287. d->target_bias_level == SND_SOC_BIAS_OFF)) {
  1288. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1289. if (ret != 0)
  1290. dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
  1291. ret);
  1292. }
  1293. /* If we're in standby and can support bias off then do that */
  1294. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  1295. d->target_bias_level == SND_SOC_BIAS_OFF) {
  1296. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
  1297. if (ret != 0)
  1298. dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
  1299. ret);
  1300. if (d->dev)
  1301. pm_runtime_put(d->dev);
  1302. }
  1303. /* If we just powered up then move to active bias */
  1304. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1305. d->target_bias_level == SND_SOC_BIAS_ON) {
  1306. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
  1307. if (ret != 0)
  1308. dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
  1309. ret);
  1310. }
  1311. }
  1312. static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
  1313. bool power, bool connect)
  1314. {
  1315. /* If a connection is being made or broken then that update
  1316. * will have marked the peer dirty, otherwise the widgets are
  1317. * not connected and this update has no impact. */
  1318. if (!connect)
  1319. return;
  1320. /* If the peer is already in the state we're moving to then we
  1321. * won't have an impact on it. */
  1322. if (power != peer->power)
  1323. dapm_mark_dirty(peer, "peer state change");
  1324. }
  1325. static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
  1326. struct list_head *up_list,
  1327. struct list_head *down_list)
  1328. {
  1329. struct snd_soc_dapm_path *path;
  1330. if (w->power == power)
  1331. return;
  1332. trace_snd_soc_dapm_widget_power(w, power);
  1333. /* If we changed our power state perhaps our neigbours changed
  1334. * also.
  1335. */
  1336. list_for_each_entry(path, &w->sources, list_sink) {
  1337. if (path->source) {
  1338. dapm_widget_set_peer_power(path->source, power,
  1339. path->connect);
  1340. }
  1341. }
  1342. switch (w->id) {
  1343. case snd_soc_dapm_supply:
  1344. case snd_soc_dapm_regulator_supply:
  1345. case snd_soc_dapm_clock_supply:
  1346. /* Supplies can't affect their outputs, only their inputs */
  1347. break;
  1348. default:
  1349. list_for_each_entry(path, &w->sinks, list_source) {
  1350. if (path->sink) {
  1351. dapm_widget_set_peer_power(path->sink, power,
  1352. path->connect);
  1353. }
  1354. }
  1355. break;
  1356. }
  1357. if (power)
  1358. dapm_seq_insert(w, up_list, true);
  1359. else
  1360. dapm_seq_insert(w, down_list, false);
  1361. w->power = power;
  1362. }
  1363. static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
  1364. struct list_head *up_list,
  1365. struct list_head *down_list)
  1366. {
  1367. int power;
  1368. switch (w->id) {
  1369. case snd_soc_dapm_pre:
  1370. dapm_seq_insert(w, down_list, false);
  1371. break;
  1372. case snd_soc_dapm_post:
  1373. dapm_seq_insert(w, up_list, true);
  1374. break;
  1375. default:
  1376. power = dapm_widget_power_check(w);
  1377. dapm_widget_set_power(w, power, up_list, down_list);
  1378. break;
  1379. }
  1380. }
  1381. /*
  1382. * Scan each dapm widget for complete audio path.
  1383. * A complete path is a route that has valid endpoints i.e.:-
  1384. *
  1385. * o DAC to output pin.
  1386. * o Input Pin to ADC.
  1387. * o Input pin to Output pin (bypass, sidetone)
  1388. * o DAC to ADC (loopback).
  1389. */
  1390. static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
  1391. {
  1392. struct snd_soc_card *card = dapm->card;
  1393. struct snd_soc_dapm_widget *w;
  1394. struct snd_soc_dapm_context *d;
  1395. LIST_HEAD(up_list);
  1396. LIST_HEAD(down_list);
  1397. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  1398. enum snd_soc_bias_level bias;
  1399. trace_snd_soc_dapm_start(card);
  1400. list_for_each_entry(d, &card->dapm_list, list) {
  1401. if (d->idle_bias_off)
  1402. d->target_bias_level = SND_SOC_BIAS_OFF;
  1403. else
  1404. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1405. }
  1406. dapm_reset(card);
  1407. /* Check which widgets we need to power and store them in
  1408. * lists indicating if they should be powered up or down. We
  1409. * only check widgets that have been flagged as dirty but note
  1410. * that new widgets may be added to the dirty list while we
  1411. * iterate.
  1412. */
  1413. list_for_each_entry(w, &card->dapm_dirty, dirty) {
  1414. dapm_power_one_widget(w, &up_list, &down_list);
  1415. }
  1416. list_for_each_entry(w, &card->widgets, list) {
  1417. switch (w->id) {
  1418. case snd_soc_dapm_pre:
  1419. case snd_soc_dapm_post:
  1420. /* These widgets always need to be powered */
  1421. break;
  1422. default:
  1423. list_del_init(&w->dirty);
  1424. break;
  1425. }
  1426. if (w->power) {
  1427. d = w->dapm;
  1428. /* Supplies and micbiases only bring the
  1429. * context up to STANDBY as unless something
  1430. * else is active and passing audio they
  1431. * generally don't require full power. Signal
  1432. * generators are virtual pins and have no
  1433. * power impact themselves.
  1434. */
  1435. switch (w->id) {
  1436. case snd_soc_dapm_siggen:
  1437. break;
  1438. case snd_soc_dapm_supply:
  1439. case snd_soc_dapm_regulator_supply:
  1440. case snd_soc_dapm_clock_supply:
  1441. case snd_soc_dapm_micbias:
  1442. if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
  1443. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1444. break;
  1445. default:
  1446. d->target_bias_level = SND_SOC_BIAS_ON;
  1447. break;
  1448. }
  1449. }
  1450. }
  1451. /* Force all contexts in the card to the same bias state if
  1452. * they're not ground referenced.
  1453. */
  1454. bias = SND_SOC_BIAS_OFF;
  1455. list_for_each_entry(d, &card->dapm_list, list)
  1456. if (d->target_bias_level > bias)
  1457. bias = d->target_bias_level;
  1458. list_for_each_entry(d, &card->dapm_list, list)
  1459. if (!d->idle_bias_off)
  1460. d->target_bias_level = bias;
  1461. trace_snd_soc_dapm_walk_done(card);
  1462. /* Run all the bias changes in parallel */
  1463. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1464. async_schedule_domain(dapm_pre_sequence_async, d,
  1465. &async_domain);
  1466. async_synchronize_full_domain(&async_domain);
  1467. /* Power down widgets first; try to avoid amplifying pops. */
  1468. dapm_seq_run(dapm, &down_list, event, false);
  1469. dapm_widget_update(dapm);
  1470. /* Now power up. */
  1471. dapm_seq_run(dapm, &up_list, event, true);
  1472. /* Run all the bias changes in parallel */
  1473. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1474. async_schedule_domain(dapm_post_sequence_async, d,
  1475. &async_domain);
  1476. async_synchronize_full_domain(&async_domain);
  1477. /* do we need to notify any clients that DAPM event is complete */
  1478. list_for_each_entry(d, &card->dapm_list, list) {
  1479. if (d->stream_event)
  1480. d->stream_event(d, event);
  1481. }
  1482. pop_dbg(dapm->dev, card->pop_time,
  1483. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1484. pop_wait(card->pop_time);
  1485. trace_snd_soc_dapm_done(card);
  1486. return 0;
  1487. }
  1488. #ifdef CONFIG_DEBUG_FS
  1489. static ssize_t dapm_widget_power_read_file(struct file *file,
  1490. char __user *user_buf,
  1491. size_t count, loff_t *ppos)
  1492. {
  1493. struct snd_soc_dapm_widget *w = file->private_data;
  1494. char *buf;
  1495. int in, out;
  1496. ssize_t ret;
  1497. struct snd_soc_dapm_path *p = NULL;
  1498. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1499. if (!buf)
  1500. return -ENOMEM;
  1501. in = is_connected_input_ep(w, NULL);
  1502. dapm_clear_walk(w->dapm);
  1503. out = is_connected_output_ep(w, NULL);
  1504. dapm_clear_walk(w->dapm);
  1505. ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
  1506. w->name, w->power ? "On" : "Off",
  1507. w->force ? " (forced)" : "", in, out);
  1508. if (w->reg >= 0)
  1509. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1510. " - R%d(0x%x) bit %d",
  1511. w->reg, w->reg, w->shift);
  1512. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1513. if (w->sname)
  1514. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1515. w->sname,
  1516. w->active ? "active" : "inactive");
  1517. list_for_each_entry(p, &w->sources, list_sink) {
  1518. if (p->connected && !p->connected(w, p->sink))
  1519. continue;
  1520. if (p->connect)
  1521. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1522. " in \"%s\" \"%s\"\n",
  1523. p->name ? p->name : "static",
  1524. p->source->name);
  1525. }
  1526. list_for_each_entry(p, &w->sinks, list_source) {
  1527. if (p->connected && !p->connected(w, p->sink))
  1528. continue;
  1529. if (p->connect)
  1530. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1531. " out \"%s\" \"%s\"\n",
  1532. p->name ? p->name : "static",
  1533. p->sink->name);
  1534. }
  1535. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1536. kfree(buf);
  1537. return ret;
  1538. }
  1539. static const struct file_operations dapm_widget_power_fops = {
  1540. .open = simple_open,
  1541. .read = dapm_widget_power_read_file,
  1542. .llseek = default_llseek,
  1543. };
  1544. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1545. size_t count, loff_t *ppos)
  1546. {
  1547. struct snd_soc_dapm_context *dapm = file->private_data;
  1548. char *level;
  1549. switch (dapm->bias_level) {
  1550. case SND_SOC_BIAS_ON:
  1551. level = "On\n";
  1552. break;
  1553. case SND_SOC_BIAS_PREPARE:
  1554. level = "Prepare\n";
  1555. break;
  1556. case SND_SOC_BIAS_STANDBY:
  1557. level = "Standby\n";
  1558. break;
  1559. case SND_SOC_BIAS_OFF:
  1560. level = "Off\n";
  1561. break;
  1562. default:
  1563. BUG();
  1564. level = "Unknown\n";
  1565. break;
  1566. }
  1567. return simple_read_from_buffer(user_buf, count, ppos, level,
  1568. strlen(level));
  1569. }
  1570. static const struct file_operations dapm_bias_fops = {
  1571. .open = simple_open,
  1572. .read = dapm_bias_read_file,
  1573. .llseek = default_llseek,
  1574. };
  1575. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1576. struct dentry *parent)
  1577. {
  1578. struct dentry *d;
  1579. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1580. if (!dapm->debugfs_dapm) {
  1581. dev_warn(dapm->dev,
  1582. "ASoC: Failed to create DAPM debugfs directory\n");
  1583. return;
  1584. }
  1585. d = debugfs_create_file("bias_level", 0444,
  1586. dapm->debugfs_dapm, dapm,
  1587. &dapm_bias_fops);
  1588. if (!d)
  1589. dev_warn(dapm->dev,
  1590. "ASoC: Failed to create bias level debugfs file\n");
  1591. }
  1592. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1593. {
  1594. struct snd_soc_dapm_context *dapm = w->dapm;
  1595. struct dentry *d;
  1596. if (!dapm->debugfs_dapm || !w->name)
  1597. return;
  1598. d = debugfs_create_file(w->name, 0444,
  1599. dapm->debugfs_dapm, w,
  1600. &dapm_widget_power_fops);
  1601. if (!d)
  1602. dev_warn(w->dapm->dev,
  1603. "ASoC: Failed to create %s debugfs file\n",
  1604. w->name);
  1605. }
  1606. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1607. {
  1608. debugfs_remove_recursive(dapm->debugfs_dapm);
  1609. }
  1610. #else
  1611. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1612. struct dentry *parent)
  1613. {
  1614. }
  1615. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1616. {
  1617. }
  1618. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1619. {
  1620. }
  1621. #endif
  1622. /* test and update the power status of a mux widget */
  1623. static int soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1624. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1625. {
  1626. struct snd_soc_dapm_path *path;
  1627. int found = 0;
  1628. if (widget->id != snd_soc_dapm_mux &&
  1629. widget->id != snd_soc_dapm_virt_mux &&
  1630. widget->id != snd_soc_dapm_value_mux)
  1631. return -ENODEV;
  1632. /* find dapm widget path assoc with kcontrol */
  1633. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1634. if (path->kcontrol != kcontrol)
  1635. continue;
  1636. if (!path->name || !e->texts[mux])
  1637. continue;
  1638. found = 1;
  1639. /* we now need to match the string in the enum to the path */
  1640. if (!(strcmp(path->name, e->texts[mux]))) {
  1641. path->connect = 1; /* new connection */
  1642. dapm_mark_dirty(path->source, "mux connection");
  1643. } else {
  1644. if (path->connect)
  1645. dapm_mark_dirty(path->source,
  1646. "mux disconnection");
  1647. path->connect = 0; /* old connection must be powered down */
  1648. }
  1649. }
  1650. if (found) {
  1651. dapm_mark_dirty(widget, "mux change");
  1652. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1653. }
  1654. return found;
  1655. }
  1656. int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1657. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1658. {
  1659. struct snd_soc_card *card = widget->dapm->card;
  1660. int ret;
  1661. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1662. ret = soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  1663. mutex_unlock(&card->dapm_mutex);
  1664. if (ret > 0)
  1665. soc_dpcm_runtime_update(widget);
  1666. return ret;
  1667. }
  1668. EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
  1669. /* test and update the power status of a mixer or switch widget */
  1670. static int soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1671. struct snd_kcontrol *kcontrol, int connect)
  1672. {
  1673. struct snd_soc_dapm_path *path;
  1674. int found = 0;
  1675. if (widget->id != snd_soc_dapm_mixer &&
  1676. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1677. widget->id != snd_soc_dapm_switch)
  1678. return -ENODEV;
  1679. /* find dapm widget path assoc with kcontrol */
  1680. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1681. if (path->kcontrol != kcontrol)
  1682. continue;
  1683. /* found, now check type */
  1684. found = 1;
  1685. path->connect = connect;
  1686. dapm_mark_dirty(path->source, "mixer connection");
  1687. }
  1688. if (found) {
  1689. dapm_mark_dirty(widget, "mixer update");
  1690. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1691. }
  1692. return found;
  1693. }
  1694. int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1695. struct snd_kcontrol *kcontrol, int connect)
  1696. {
  1697. struct snd_soc_card *card = widget->dapm->card;
  1698. int ret;
  1699. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1700. ret = soc_dapm_mixer_update_power(widget, kcontrol, connect);
  1701. mutex_unlock(&card->dapm_mutex);
  1702. if (ret > 0)
  1703. soc_dpcm_runtime_update(widget);
  1704. return ret;
  1705. }
  1706. EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
  1707. /* show dapm widget status in sys fs */
  1708. static ssize_t dapm_widget_show(struct device *dev,
  1709. struct device_attribute *attr, char *buf)
  1710. {
  1711. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  1712. struct snd_soc_codec *codec =rtd->codec;
  1713. struct snd_soc_dapm_widget *w;
  1714. int count = 0;
  1715. char *state = "not set";
  1716. list_for_each_entry(w, &codec->card->widgets, list) {
  1717. if (w->dapm != &codec->dapm)
  1718. continue;
  1719. /* only display widgets that burnm power */
  1720. switch (w->id) {
  1721. case snd_soc_dapm_hp:
  1722. case snd_soc_dapm_mic:
  1723. case snd_soc_dapm_spk:
  1724. case snd_soc_dapm_line:
  1725. case snd_soc_dapm_micbias:
  1726. case snd_soc_dapm_dac:
  1727. case snd_soc_dapm_adc:
  1728. case snd_soc_dapm_pga:
  1729. case snd_soc_dapm_out_drv:
  1730. case snd_soc_dapm_mixer:
  1731. case snd_soc_dapm_mixer_named_ctl:
  1732. case snd_soc_dapm_supply:
  1733. case snd_soc_dapm_regulator_supply:
  1734. case snd_soc_dapm_clock_supply:
  1735. if (w->name)
  1736. count += sprintf(buf + count, "%s: %s\n",
  1737. w->name, w->power ? "On":"Off");
  1738. break;
  1739. default:
  1740. break;
  1741. }
  1742. }
  1743. switch (codec->dapm.bias_level) {
  1744. case SND_SOC_BIAS_ON:
  1745. state = "On";
  1746. break;
  1747. case SND_SOC_BIAS_PREPARE:
  1748. state = "Prepare";
  1749. break;
  1750. case SND_SOC_BIAS_STANDBY:
  1751. state = "Standby";
  1752. break;
  1753. case SND_SOC_BIAS_OFF:
  1754. state = "Off";
  1755. break;
  1756. }
  1757. count += sprintf(buf + count, "PM State: %s\n", state);
  1758. return count;
  1759. }
  1760. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1761. int snd_soc_dapm_sys_add(struct device *dev)
  1762. {
  1763. return device_create_file(dev, &dev_attr_dapm_widget);
  1764. }
  1765. static void snd_soc_dapm_sys_remove(struct device *dev)
  1766. {
  1767. device_remove_file(dev, &dev_attr_dapm_widget);
  1768. }
  1769. /* free all dapm widgets and resources */
  1770. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1771. {
  1772. struct snd_soc_dapm_widget *w, *next_w;
  1773. struct snd_soc_dapm_path *p, *next_p;
  1774. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1775. if (w->dapm != dapm)
  1776. continue;
  1777. list_del(&w->list);
  1778. /*
  1779. * remove source and sink paths associated to this widget.
  1780. * While removing the path, remove reference to it from both
  1781. * source and sink widgets so that path is removed only once.
  1782. */
  1783. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1784. list_del(&p->list_sink);
  1785. list_del(&p->list_source);
  1786. list_del(&p->list);
  1787. kfree(p->long_name);
  1788. kfree(p);
  1789. }
  1790. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1791. list_del(&p->list_sink);
  1792. list_del(&p->list_source);
  1793. list_del(&p->list);
  1794. kfree(p->long_name);
  1795. kfree(p);
  1796. }
  1797. kfree(w->kcontrols);
  1798. kfree(w->name);
  1799. kfree(w);
  1800. }
  1801. }
  1802. static struct snd_soc_dapm_widget *dapm_find_widget(
  1803. struct snd_soc_dapm_context *dapm, const char *pin,
  1804. bool search_other_contexts)
  1805. {
  1806. struct snd_soc_dapm_widget *w;
  1807. struct snd_soc_dapm_widget *fallback = NULL;
  1808. list_for_each_entry(w, &dapm->card->widgets, list) {
  1809. if (!strcmp(w->name, pin)) {
  1810. if (w->dapm == dapm)
  1811. return w;
  1812. else
  1813. fallback = w;
  1814. }
  1815. }
  1816. if (search_other_contexts)
  1817. return fallback;
  1818. return NULL;
  1819. }
  1820. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1821. const char *pin, int status)
  1822. {
  1823. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  1824. if (!w) {
  1825. dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
  1826. return -EINVAL;
  1827. }
  1828. if (w->connected != status)
  1829. dapm_mark_dirty(w, "pin configuration");
  1830. w->connected = status;
  1831. if (status == 0)
  1832. w->force = 0;
  1833. return 0;
  1834. }
  1835. /**
  1836. * snd_soc_dapm_sync - scan and power dapm paths
  1837. * @dapm: DAPM context
  1838. *
  1839. * Walks all dapm audio paths and powers widgets according to their
  1840. * stream or path usage.
  1841. *
  1842. * Returns 0 for success.
  1843. */
  1844. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1845. {
  1846. int ret;
  1847. /*
  1848. * Suppress early reports (eg, jacks syncing their state) to avoid
  1849. * silly DAPM runs during card startup.
  1850. */
  1851. if (!dapm->card || !dapm->card->instantiated)
  1852. return 0;
  1853. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1854. ret = dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1855. mutex_unlock(&dapm->card->dapm_mutex);
  1856. return ret;
  1857. }
  1858. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1859. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1860. const struct snd_soc_dapm_route *route)
  1861. {
  1862. struct snd_soc_dapm_path *path;
  1863. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1864. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1865. const char *sink;
  1866. const char *control = route->control;
  1867. const char *source;
  1868. char prefixed_sink[80];
  1869. char prefixed_source[80];
  1870. int ret = 0;
  1871. if (dapm->codec && dapm->codec->name_prefix) {
  1872. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1873. dapm->codec->name_prefix, route->sink);
  1874. sink = prefixed_sink;
  1875. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1876. dapm->codec->name_prefix, route->source);
  1877. source = prefixed_source;
  1878. } else {
  1879. sink = route->sink;
  1880. source = route->source;
  1881. }
  1882. /*
  1883. * find src and dest widgets over all widgets but favor a widget from
  1884. * current DAPM context
  1885. */
  1886. list_for_each_entry(w, &dapm->card->widgets, list) {
  1887. if (!wsink && !(strcmp(w->name, sink))) {
  1888. wtsink = w;
  1889. if (w->dapm == dapm)
  1890. wsink = w;
  1891. continue;
  1892. }
  1893. if (!wsource && !(strcmp(w->name, source))) {
  1894. wtsource = w;
  1895. if (w->dapm == dapm)
  1896. wsource = w;
  1897. }
  1898. }
  1899. /* use widget from another DAPM context if not found from this */
  1900. if (!wsink)
  1901. wsink = wtsink;
  1902. if (!wsource)
  1903. wsource = wtsource;
  1904. if (wsource == NULL) {
  1905. dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
  1906. route->source);
  1907. return -ENODEV;
  1908. }
  1909. if (wsink == NULL) {
  1910. dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
  1911. route->sink);
  1912. return -ENODEV;
  1913. }
  1914. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1915. if (!path)
  1916. return -ENOMEM;
  1917. path->source = wsource;
  1918. path->sink = wsink;
  1919. path->connected = route->connected;
  1920. INIT_LIST_HEAD(&path->list);
  1921. INIT_LIST_HEAD(&path->list_source);
  1922. INIT_LIST_HEAD(&path->list_sink);
  1923. /* check for external widgets */
  1924. if (wsink->id == snd_soc_dapm_input) {
  1925. if (wsource->id == snd_soc_dapm_micbias ||
  1926. wsource->id == snd_soc_dapm_mic ||
  1927. wsource->id == snd_soc_dapm_line ||
  1928. wsource->id == snd_soc_dapm_output)
  1929. wsink->ext = 1;
  1930. }
  1931. if (wsource->id == snd_soc_dapm_output) {
  1932. if (wsink->id == snd_soc_dapm_spk ||
  1933. wsink->id == snd_soc_dapm_hp ||
  1934. wsink->id == snd_soc_dapm_line ||
  1935. wsink->id == snd_soc_dapm_input)
  1936. wsource->ext = 1;
  1937. }
  1938. /* connect static paths */
  1939. if (control == NULL) {
  1940. list_add(&path->list, &dapm->card->paths);
  1941. list_add(&path->list_sink, &wsink->sources);
  1942. list_add(&path->list_source, &wsource->sinks);
  1943. path->connect = 1;
  1944. return 0;
  1945. }
  1946. /* connect dynamic paths */
  1947. switch (wsink->id) {
  1948. case snd_soc_dapm_adc:
  1949. case snd_soc_dapm_dac:
  1950. case snd_soc_dapm_pga:
  1951. case snd_soc_dapm_out_drv:
  1952. case snd_soc_dapm_input:
  1953. case snd_soc_dapm_output:
  1954. case snd_soc_dapm_siggen:
  1955. case snd_soc_dapm_micbias:
  1956. case snd_soc_dapm_vmid:
  1957. case snd_soc_dapm_pre:
  1958. case snd_soc_dapm_post:
  1959. case snd_soc_dapm_supply:
  1960. case snd_soc_dapm_regulator_supply:
  1961. case snd_soc_dapm_clock_supply:
  1962. case snd_soc_dapm_aif_in:
  1963. case snd_soc_dapm_aif_out:
  1964. case snd_soc_dapm_dai:
  1965. case snd_soc_dapm_dai_link:
  1966. list_add(&path->list, &dapm->card->paths);
  1967. list_add(&path->list_sink, &wsink->sources);
  1968. list_add(&path->list_source, &wsource->sinks);
  1969. path->connect = 1;
  1970. return 0;
  1971. case snd_soc_dapm_mux:
  1972. case snd_soc_dapm_virt_mux:
  1973. case snd_soc_dapm_value_mux:
  1974. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1975. &wsink->kcontrol_news[0]);
  1976. if (ret != 0)
  1977. goto err;
  1978. break;
  1979. case snd_soc_dapm_switch:
  1980. case snd_soc_dapm_mixer:
  1981. case snd_soc_dapm_mixer_named_ctl:
  1982. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1983. if (ret != 0)
  1984. goto err;
  1985. break;
  1986. case snd_soc_dapm_hp:
  1987. case snd_soc_dapm_mic:
  1988. case snd_soc_dapm_line:
  1989. case snd_soc_dapm_spk:
  1990. list_add(&path->list, &dapm->card->paths);
  1991. list_add(&path->list_sink, &wsink->sources);
  1992. list_add(&path->list_source, &wsource->sinks);
  1993. path->connect = 0;
  1994. return 0;
  1995. }
  1996. dapm_mark_dirty(wsource, "Route added");
  1997. dapm_mark_dirty(wsink, "Route added");
  1998. return 0;
  1999. err:
  2000. dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
  2001. source, control, sink);
  2002. kfree(path);
  2003. return ret;
  2004. }
  2005. static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
  2006. const struct snd_soc_dapm_route *route)
  2007. {
  2008. struct snd_soc_dapm_path *path, *p;
  2009. const char *sink;
  2010. const char *source;
  2011. char prefixed_sink[80];
  2012. char prefixed_source[80];
  2013. if (route->control) {
  2014. dev_err(dapm->dev,
  2015. "ASoC: Removal of routes with controls not supported\n");
  2016. return -EINVAL;
  2017. }
  2018. if (dapm->codec && dapm->codec->name_prefix) {
  2019. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  2020. dapm->codec->name_prefix, route->sink);
  2021. sink = prefixed_sink;
  2022. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  2023. dapm->codec->name_prefix, route->source);
  2024. source = prefixed_source;
  2025. } else {
  2026. sink = route->sink;
  2027. source = route->source;
  2028. }
  2029. path = NULL;
  2030. list_for_each_entry(p, &dapm->card->paths, list) {
  2031. if (strcmp(p->source->name, source) != 0)
  2032. continue;
  2033. if (strcmp(p->sink->name, sink) != 0)
  2034. continue;
  2035. path = p;
  2036. break;
  2037. }
  2038. if (path) {
  2039. dapm_mark_dirty(path->source, "Route removed");
  2040. dapm_mark_dirty(path->sink, "Route removed");
  2041. list_del(&path->list);
  2042. list_del(&path->list_sink);
  2043. list_del(&path->list_source);
  2044. kfree(path);
  2045. } else {
  2046. dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
  2047. source, sink);
  2048. }
  2049. return 0;
  2050. }
  2051. /**
  2052. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  2053. * @dapm: DAPM context
  2054. * @route: audio routes
  2055. * @num: number of routes
  2056. *
  2057. * Connects 2 dapm widgets together via a named audio path. The sink is
  2058. * the widget receiving the audio signal, whilst the source is the sender
  2059. * of the audio signal.
  2060. *
  2061. * Returns 0 for success else error. On error all resources can be freed
  2062. * with a call to snd_soc_card_free().
  2063. */
  2064. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  2065. const struct snd_soc_dapm_route *route, int num)
  2066. {
  2067. int i, r, ret = 0;
  2068. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2069. for (i = 0; i < num; i++) {
  2070. r = snd_soc_dapm_add_route(dapm, route);
  2071. if (r < 0) {
  2072. dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
  2073. route->source,
  2074. route->control ? route->control : "direct",
  2075. route->sink);
  2076. ret = r;
  2077. }
  2078. route++;
  2079. }
  2080. mutex_unlock(&dapm->card->dapm_mutex);
  2081. return ret;
  2082. }
  2083. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  2084. /**
  2085. * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
  2086. * @dapm: DAPM context
  2087. * @route: audio routes
  2088. * @num: number of routes
  2089. *
  2090. * Removes routes from the DAPM context.
  2091. */
  2092. int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
  2093. const struct snd_soc_dapm_route *route, int num)
  2094. {
  2095. int i, ret = 0;
  2096. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2097. for (i = 0; i < num; i++) {
  2098. snd_soc_dapm_del_route(dapm, route);
  2099. route++;
  2100. }
  2101. mutex_unlock(&dapm->card->dapm_mutex);
  2102. return ret;
  2103. }
  2104. EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
  2105. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  2106. const struct snd_soc_dapm_route *route)
  2107. {
  2108. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  2109. route->source,
  2110. true);
  2111. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  2112. route->sink,
  2113. true);
  2114. struct snd_soc_dapm_path *path;
  2115. int count = 0;
  2116. if (!source) {
  2117. dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
  2118. route->source);
  2119. return -ENODEV;
  2120. }
  2121. if (!sink) {
  2122. dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
  2123. route->sink);
  2124. return -ENODEV;
  2125. }
  2126. if (route->control || route->connected)
  2127. dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
  2128. route->source, route->sink);
  2129. list_for_each_entry(path, &source->sinks, list_source) {
  2130. if (path->sink == sink) {
  2131. path->weak = 1;
  2132. count++;
  2133. }
  2134. }
  2135. if (count == 0)
  2136. dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
  2137. route->source, route->sink);
  2138. if (count > 1)
  2139. dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
  2140. count, route->source, route->sink);
  2141. return 0;
  2142. }
  2143. /**
  2144. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  2145. * @dapm: DAPM context
  2146. * @route: audio routes
  2147. * @num: number of routes
  2148. *
  2149. * Mark existing routes matching those specified in the passed array
  2150. * as being weak, meaning that they are ignored for the purpose of
  2151. * power decisions. The main intended use case is for sidetone paths
  2152. * which couple audio between other independent paths if they are both
  2153. * active in order to make the combination work better at the user
  2154. * level but which aren't intended to be "used".
  2155. *
  2156. * Note that CODEC drivers should not use this as sidetone type paths
  2157. * can frequently also be used as bypass paths.
  2158. */
  2159. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  2160. const struct snd_soc_dapm_route *route, int num)
  2161. {
  2162. int i, err;
  2163. int ret = 0;
  2164. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2165. for (i = 0; i < num; i++) {
  2166. err = snd_soc_dapm_weak_route(dapm, route);
  2167. if (err)
  2168. ret = err;
  2169. route++;
  2170. }
  2171. mutex_unlock(&dapm->card->dapm_mutex);
  2172. return ret;
  2173. }
  2174. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  2175. /**
  2176. * snd_soc_dapm_new_widgets - add new dapm widgets
  2177. * @dapm: DAPM context
  2178. *
  2179. * Checks the codec for any new dapm widgets and creates them if found.
  2180. *
  2181. * Returns 0 for success.
  2182. */
  2183. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  2184. {
  2185. struct snd_soc_dapm_widget *w;
  2186. unsigned int val;
  2187. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2188. list_for_each_entry(w, &dapm->card->widgets, list)
  2189. {
  2190. if (w->new)
  2191. continue;
  2192. if (w->num_kcontrols) {
  2193. w->kcontrols = kzalloc(w->num_kcontrols *
  2194. sizeof(struct snd_kcontrol *),
  2195. GFP_KERNEL);
  2196. if (!w->kcontrols) {
  2197. mutex_unlock(&dapm->card->dapm_mutex);
  2198. return -ENOMEM;
  2199. }
  2200. }
  2201. switch(w->id) {
  2202. case snd_soc_dapm_switch:
  2203. case snd_soc_dapm_mixer:
  2204. case snd_soc_dapm_mixer_named_ctl:
  2205. dapm_new_mixer(w);
  2206. break;
  2207. case snd_soc_dapm_mux:
  2208. case snd_soc_dapm_virt_mux:
  2209. case snd_soc_dapm_value_mux:
  2210. dapm_new_mux(w);
  2211. break;
  2212. case snd_soc_dapm_pga:
  2213. case snd_soc_dapm_out_drv:
  2214. dapm_new_pga(w);
  2215. break;
  2216. default:
  2217. break;
  2218. }
  2219. /* Read the initial power state from the device */
  2220. if (w->reg >= 0) {
  2221. val = soc_widget_read(w, w->reg);
  2222. val &= 1 << w->shift;
  2223. if (w->invert)
  2224. val = !val;
  2225. if (val)
  2226. w->power = 1;
  2227. }
  2228. w->new = 1;
  2229. dapm_mark_dirty(w, "new widget");
  2230. dapm_debugfs_add_widget(w);
  2231. }
  2232. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  2233. mutex_unlock(&dapm->card->dapm_mutex);
  2234. return 0;
  2235. }
  2236. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  2237. /**
  2238. * snd_soc_dapm_get_volsw - dapm mixer get callback
  2239. * @kcontrol: mixer control
  2240. * @ucontrol: control element information
  2241. *
  2242. * Callback to get the value of a dapm mixer control.
  2243. *
  2244. * Returns 0 for success.
  2245. */
  2246. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  2247. struct snd_ctl_elem_value *ucontrol)
  2248. {
  2249. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2250. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2251. struct soc_mixer_control *mc =
  2252. (struct soc_mixer_control *)kcontrol->private_value;
  2253. unsigned int reg = mc->reg;
  2254. unsigned int shift = mc->shift;
  2255. int max = mc->max;
  2256. unsigned int mask = (1 << fls(max)) - 1;
  2257. unsigned int invert = mc->invert;
  2258. if (snd_soc_volsw_is_stereo(mc))
  2259. dev_warn(widget->dapm->dev,
  2260. "ASoC: Control '%s' is stereo, which is not supported\n",
  2261. kcontrol->id.name);
  2262. ucontrol->value.integer.value[0] =
  2263. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  2264. if (invert)
  2265. ucontrol->value.integer.value[0] =
  2266. max - ucontrol->value.integer.value[0];
  2267. return 0;
  2268. }
  2269. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  2270. /**
  2271. * snd_soc_dapm_put_volsw - dapm mixer set callback
  2272. * @kcontrol: mixer control
  2273. * @ucontrol: control element information
  2274. *
  2275. * Callback to set the value of a dapm mixer control.
  2276. *
  2277. * Returns 0 for success.
  2278. */
  2279. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  2280. struct snd_ctl_elem_value *ucontrol)
  2281. {
  2282. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2283. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2284. struct snd_soc_codec *codec = widget->codec;
  2285. struct snd_soc_card *card = codec->card;
  2286. struct soc_mixer_control *mc =
  2287. (struct soc_mixer_control *)kcontrol->private_value;
  2288. unsigned int reg = mc->reg;
  2289. unsigned int shift = mc->shift;
  2290. int max = mc->max;
  2291. unsigned int mask = (1 << fls(max)) - 1;
  2292. unsigned int invert = mc->invert;
  2293. unsigned int val;
  2294. int connect, change;
  2295. struct snd_soc_dapm_update update;
  2296. int wi;
  2297. if (snd_soc_volsw_is_stereo(mc))
  2298. dev_warn(widget->dapm->dev,
  2299. "ASoC: Control '%s' is stereo, which is not supported\n",
  2300. kcontrol->id.name);
  2301. val = (ucontrol->value.integer.value[0] & mask);
  2302. connect = !!val;
  2303. if (invert)
  2304. val = max - val;
  2305. mask = mask << shift;
  2306. val = val << shift;
  2307. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2308. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  2309. if (change) {
  2310. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2311. widget = wlist->widgets[wi];
  2312. widget->value = val;
  2313. update.kcontrol = kcontrol;
  2314. update.widget = widget;
  2315. update.reg = reg;
  2316. update.mask = mask;
  2317. update.val = val;
  2318. widget->dapm->update = &update;
  2319. soc_dapm_mixer_update_power(widget, kcontrol, connect);
  2320. widget->dapm->update = NULL;
  2321. }
  2322. }
  2323. mutex_unlock(&card->dapm_mutex);
  2324. return 0;
  2325. }
  2326. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  2327. /**
  2328. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  2329. * @kcontrol: mixer control
  2330. * @ucontrol: control element information
  2331. *
  2332. * Callback to get the value of a dapm enumerated double mixer control.
  2333. *
  2334. * Returns 0 for success.
  2335. */
  2336. int snd_soc_dapm_get_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 soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2342. unsigned int val;
  2343. val = snd_soc_read(widget->codec, e->reg);
  2344. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & e->mask;
  2345. if (e->shift_l != e->shift_r)
  2346. ucontrol->value.enumerated.item[1] =
  2347. (val >> e->shift_r) & e->mask;
  2348. return 0;
  2349. }
  2350. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  2351. /**
  2352. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  2353. * @kcontrol: mixer control
  2354. * @ucontrol: control element information
  2355. *
  2356. * Callback to set the value of a dapm enumerated double mixer control.
  2357. *
  2358. * Returns 0 for success.
  2359. */
  2360. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  2361. struct snd_ctl_elem_value *ucontrol)
  2362. {
  2363. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2364. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2365. struct snd_soc_codec *codec = widget->codec;
  2366. struct snd_soc_card *card = codec->card;
  2367. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2368. unsigned int val, mux, change;
  2369. unsigned int mask;
  2370. struct snd_soc_dapm_update update;
  2371. int wi;
  2372. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2373. return -EINVAL;
  2374. mux = ucontrol->value.enumerated.item[0];
  2375. val = mux << e->shift_l;
  2376. mask = e->mask << e->shift_l;
  2377. if (e->shift_l != e->shift_r) {
  2378. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2379. return -EINVAL;
  2380. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2381. mask |= e->mask << e->shift_r;
  2382. }
  2383. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2384. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2385. if (change) {
  2386. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2387. widget = wlist->widgets[wi];
  2388. widget->value = val;
  2389. update.kcontrol = kcontrol;
  2390. update.widget = widget;
  2391. update.reg = e->reg;
  2392. update.mask = mask;
  2393. update.val = val;
  2394. widget->dapm->update = &update;
  2395. soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2396. widget->dapm->update = NULL;
  2397. }
  2398. }
  2399. mutex_unlock(&card->dapm_mutex);
  2400. return change;
  2401. }
  2402. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  2403. /**
  2404. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  2405. * @kcontrol: mixer control
  2406. * @ucontrol: control element information
  2407. *
  2408. * Returns 0 for success.
  2409. */
  2410. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  2411. struct snd_ctl_elem_value *ucontrol)
  2412. {
  2413. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2414. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2415. ucontrol->value.enumerated.item[0] = widget->value;
  2416. return 0;
  2417. }
  2418. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  2419. /**
  2420. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  2421. * @kcontrol: mixer control
  2422. * @ucontrol: control element information
  2423. *
  2424. * Returns 0 for success.
  2425. */
  2426. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  2427. struct snd_ctl_elem_value *ucontrol)
  2428. {
  2429. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2430. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2431. struct snd_soc_codec *codec = widget->codec;
  2432. struct snd_soc_card *card = codec->card;
  2433. struct soc_enum *e =
  2434. (struct soc_enum *)kcontrol->private_value;
  2435. int change;
  2436. int ret = 0;
  2437. int wi;
  2438. if (ucontrol->value.enumerated.item[0] >= e->max)
  2439. return -EINVAL;
  2440. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2441. change = widget->value != ucontrol->value.enumerated.item[0];
  2442. if (change) {
  2443. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2444. widget = wlist->widgets[wi];
  2445. widget->value = ucontrol->value.enumerated.item[0];
  2446. soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
  2447. }
  2448. }
  2449. mutex_unlock(&card->dapm_mutex);
  2450. return ret;
  2451. }
  2452. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  2453. /**
  2454. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  2455. * callback
  2456. * @kcontrol: mixer control
  2457. * @ucontrol: control element information
  2458. *
  2459. * Callback to get the value of a dapm semi enumerated double mixer control.
  2460. *
  2461. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2462. * used for handling bitfield coded enumeration for example.
  2463. *
  2464. * Returns 0 for success.
  2465. */
  2466. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2467. struct snd_ctl_elem_value *ucontrol)
  2468. {
  2469. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2470. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2471. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2472. unsigned int reg_val, val, mux;
  2473. reg_val = snd_soc_read(widget->codec, e->reg);
  2474. val = (reg_val >> e->shift_l) & e->mask;
  2475. for (mux = 0; mux < e->max; mux++) {
  2476. if (val == e->values[mux])
  2477. break;
  2478. }
  2479. ucontrol->value.enumerated.item[0] = mux;
  2480. if (e->shift_l != e->shift_r) {
  2481. val = (reg_val >> e->shift_r) & e->mask;
  2482. for (mux = 0; mux < e->max; mux++) {
  2483. if (val == e->values[mux])
  2484. break;
  2485. }
  2486. ucontrol->value.enumerated.item[1] = mux;
  2487. }
  2488. return 0;
  2489. }
  2490. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  2491. /**
  2492. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  2493. * callback
  2494. * @kcontrol: mixer control
  2495. * @ucontrol: control element information
  2496. *
  2497. * Callback to set the value of a dapm semi enumerated double mixer control.
  2498. *
  2499. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2500. * used for handling bitfield coded enumeration for example.
  2501. *
  2502. * Returns 0 for success.
  2503. */
  2504. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2505. struct snd_ctl_elem_value *ucontrol)
  2506. {
  2507. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2508. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2509. struct snd_soc_codec *codec = widget->codec;
  2510. struct snd_soc_card *card = codec->card;
  2511. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2512. unsigned int val, mux, change;
  2513. unsigned int mask;
  2514. struct snd_soc_dapm_update update;
  2515. int wi;
  2516. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2517. return -EINVAL;
  2518. mux = ucontrol->value.enumerated.item[0];
  2519. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2520. mask = e->mask << e->shift_l;
  2521. if (e->shift_l != e->shift_r) {
  2522. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2523. return -EINVAL;
  2524. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2525. mask |= e->mask << e->shift_r;
  2526. }
  2527. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2528. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2529. if (change) {
  2530. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2531. widget = wlist->widgets[wi];
  2532. widget->value = val;
  2533. update.kcontrol = kcontrol;
  2534. update.widget = widget;
  2535. update.reg = e->reg;
  2536. update.mask = mask;
  2537. update.val = val;
  2538. widget->dapm->update = &update;
  2539. soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2540. widget->dapm->update = NULL;
  2541. }
  2542. }
  2543. mutex_unlock(&card->dapm_mutex);
  2544. return change;
  2545. }
  2546. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  2547. /**
  2548. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2549. *
  2550. * @kcontrol: mixer control
  2551. * @uinfo: control element information
  2552. *
  2553. * Callback to provide information about a pin switch control.
  2554. */
  2555. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2556. struct snd_ctl_elem_info *uinfo)
  2557. {
  2558. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2559. uinfo->count = 1;
  2560. uinfo->value.integer.min = 0;
  2561. uinfo->value.integer.max = 1;
  2562. return 0;
  2563. }
  2564. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2565. /**
  2566. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2567. *
  2568. * @kcontrol: mixer control
  2569. * @ucontrol: Value
  2570. */
  2571. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2572. struct snd_ctl_elem_value *ucontrol)
  2573. {
  2574. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2575. const char *pin = (const char *)kcontrol->private_value;
  2576. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2577. ucontrol->value.integer.value[0] =
  2578. snd_soc_dapm_get_pin_status(&card->dapm, pin);
  2579. mutex_unlock(&card->dapm_mutex);
  2580. return 0;
  2581. }
  2582. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2583. /**
  2584. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2585. *
  2586. * @kcontrol: mixer control
  2587. * @ucontrol: Value
  2588. */
  2589. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2590. struct snd_ctl_elem_value *ucontrol)
  2591. {
  2592. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2593. const char *pin = (const char *)kcontrol->private_value;
  2594. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2595. if (ucontrol->value.integer.value[0])
  2596. snd_soc_dapm_enable_pin(&card->dapm, pin);
  2597. else
  2598. snd_soc_dapm_disable_pin(&card->dapm, pin);
  2599. mutex_unlock(&card->dapm_mutex);
  2600. snd_soc_dapm_sync(&card->dapm);
  2601. return 0;
  2602. }
  2603. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2604. static struct snd_soc_dapm_widget *
  2605. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  2606. const struct snd_soc_dapm_widget *widget)
  2607. {
  2608. struct snd_soc_dapm_widget *w;
  2609. size_t name_len;
  2610. int ret;
  2611. if ((w = dapm_cnew_widget(widget)) == NULL)
  2612. return NULL;
  2613. switch (w->id) {
  2614. case snd_soc_dapm_regulator_supply:
  2615. w->regulator = devm_regulator_get(dapm->dev, w->name);
  2616. if (IS_ERR(w->regulator)) {
  2617. ret = PTR_ERR(w->regulator);
  2618. dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
  2619. w->name, ret);
  2620. return NULL;
  2621. }
  2622. if (w->invert & SND_SOC_DAPM_REGULATOR_BYPASS) {
  2623. ret = regulator_allow_bypass(w->regulator, true);
  2624. if (ret != 0)
  2625. dev_warn(w->dapm->dev,
  2626. "ASoC: Failed to unbypass %s: %d\n",
  2627. w->name, ret);
  2628. }
  2629. break;
  2630. case snd_soc_dapm_clock_supply:
  2631. #ifdef CONFIG_CLKDEV_LOOKUP
  2632. w->clk = devm_clk_get(dapm->dev, w->name);
  2633. if (IS_ERR(w->clk)) {
  2634. ret = PTR_ERR(w->clk);
  2635. dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
  2636. w->name, ret);
  2637. return NULL;
  2638. }
  2639. #else
  2640. return NULL;
  2641. #endif
  2642. break;
  2643. default:
  2644. break;
  2645. }
  2646. name_len = strlen(widget->name) + 1;
  2647. if (dapm->codec && dapm->codec->name_prefix)
  2648. name_len += 1 + strlen(dapm->codec->name_prefix);
  2649. w->name = kmalloc(name_len, GFP_KERNEL);
  2650. if (w->name == NULL) {
  2651. kfree(w);
  2652. return NULL;
  2653. }
  2654. if (dapm->codec && dapm->codec->name_prefix)
  2655. snprintf((char *)w->name, name_len, "%s %s",
  2656. dapm->codec->name_prefix, widget->name);
  2657. else
  2658. snprintf((char *)w->name, name_len, "%s", widget->name);
  2659. switch (w->id) {
  2660. case snd_soc_dapm_switch:
  2661. case snd_soc_dapm_mixer:
  2662. case snd_soc_dapm_mixer_named_ctl:
  2663. w->power_check = dapm_generic_check_power;
  2664. break;
  2665. case snd_soc_dapm_mux:
  2666. case snd_soc_dapm_virt_mux:
  2667. case snd_soc_dapm_value_mux:
  2668. w->power_check = dapm_generic_check_power;
  2669. break;
  2670. case snd_soc_dapm_adc:
  2671. case snd_soc_dapm_aif_out:
  2672. w->power_check = dapm_adc_check_power;
  2673. break;
  2674. case snd_soc_dapm_dac:
  2675. case snd_soc_dapm_aif_in:
  2676. w->power_check = dapm_dac_check_power;
  2677. break;
  2678. case snd_soc_dapm_pga:
  2679. case snd_soc_dapm_out_drv:
  2680. case snd_soc_dapm_input:
  2681. case snd_soc_dapm_output:
  2682. case snd_soc_dapm_micbias:
  2683. case snd_soc_dapm_spk:
  2684. case snd_soc_dapm_hp:
  2685. case snd_soc_dapm_mic:
  2686. case snd_soc_dapm_line:
  2687. case snd_soc_dapm_dai_link:
  2688. w->power_check = dapm_generic_check_power;
  2689. break;
  2690. case snd_soc_dapm_supply:
  2691. case snd_soc_dapm_regulator_supply:
  2692. case snd_soc_dapm_clock_supply:
  2693. w->power_check = dapm_supply_check_power;
  2694. break;
  2695. case snd_soc_dapm_dai:
  2696. w->power_check = dapm_dai_check_power;
  2697. break;
  2698. default:
  2699. w->power_check = dapm_always_on_check_power;
  2700. break;
  2701. }
  2702. dapm->n_widgets++;
  2703. w->dapm = dapm;
  2704. w->codec = dapm->codec;
  2705. w->platform = dapm->platform;
  2706. INIT_LIST_HEAD(&w->sources);
  2707. INIT_LIST_HEAD(&w->sinks);
  2708. INIT_LIST_HEAD(&w->list);
  2709. INIT_LIST_HEAD(&w->dirty);
  2710. list_add(&w->list, &dapm->card->widgets);
  2711. /* machine layer set ups unconnected pins and insertions */
  2712. w->connected = 1;
  2713. return w;
  2714. }
  2715. /**
  2716. * snd_soc_dapm_new_controls - create new dapm controls
  2717. * @dapm: DAPM context
  2718. * @widget: widget array
  2719. * @num: number of widgets
  2720. *
  2721. * Creates new DAPM controls based upon the templates.
  2722. *
  2723. * Returns 0 for success else error.
  2724. */
  2725. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  2726. const struct snd_soc_dapm_widget *widget,
  2727. int num)
  2728. {
  2729. struct snd_soc_dapm_widget *w;
  2730. int i;
  2731. int ret = 0;
  2732. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2733. for (i = 0; i < num; i++) {
  2734. w = snd_soc_dapm_new_control(dapm, widget);
  2735. if (!w) {
  2736. dev_err(dapm->dev,
  2737. "ASoC: Failed to create DAPM control %s\n",
  2738. widget->name);
  2739. ret = -ENOMEM;
  2740. break;
  2741. }
  2742. widget++;
  2743. }
  2744. mutex_unlock(&dapm->card->dapm_mutex);
  2745. return ret;
  2746. }
  2747. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  2748. static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
  2749. struct snd_kcontrol *kcontrol, int event)
  2750. {
  2751. struct snd_soc_dapm_path *source_p, *sink_p;
  2752. struct snd_soc_dai *source, *sink;
  2753. const struct snd_soc_pcm_stream *config = w->params;
  2754. struct snd_pcm_substream substream;
  2755. struct snd_pcm_hw_params *params = NULL;
  2756. u64 fmt;
  2757. int ret;
  2758. BUG_ON(!config);
  2759. BUG_ON(list_empty(&w->sources) || list_empty(&w->sinks));
  2760. /* We only support a single source and sink, pick the first */
  2761. source_p = list_first_entry(&w->sources, struct snd_soc_dapm_path,
  2762. list_sink);
  2763. sink_p = list_first_entry(&w->sinks, struct snd_soc_dapm_path,
  2764. list_source);
  2765. BUG_ON(!source_p || !sink_p);
  2766. BUG_ON(!sink_p->source || !source_p->sink);
  2767. BUG_ON(!source_p->source || !sink_p->sink);
  2768. source = source_p->source->priv;
  2769. sink = sink_p->sink->priv;
  2770. /* Be a little careful as we don't want to overflow the mask array */
  2771. if (config->formats) {
  2772. fmt = ffs(config->formats) - 1;
  2773. } else {
  2774. dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
  2775. config->formats);
  2776. fmt = 0;
  2777. }
  2778. /* Currently very limited parameter selection */
  2779. params = kzalloc(sizeof(*params), GFP_KERNEL);
  2780. if (!params) {
  2781. ret = -ENOMEM;
  2782. goto out;
  2783. }
  2784. snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
  2785. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
  2786. config->rate_min;
  2787. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
  2788. config->rate_max;
  2789. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
  2790. = config->channels_min;
  2791. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
  2792. = config->channels_max;
  2793. memset(&substream, 0, sizeof(substream));
  2794. switch (event) {
  2795. case SND_SOC_DAPM_PRE_PMU:
  2796. if (source->driver->ops && source->driver->ops->hw_params) {
  2797. substream.stream = SNDRV_PCM_STREAM_CAPTURE;
  2798. ret = source->driver->ops->hw_params(&substream,
  2799. params, source);
  2800. if (ret != 0) {
  2801. dev_err(source->dev,
  2802. "ASoC: hw_params() failed: %d\n", ret);
  2803. goto out;
  2804. }
  2805. }
  2806. if (sink->driver->ops && sink->driver->ops->hw_params) {
  2807. substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
  2808. ret = sink->driver->ops->hw_params(&substream, params,
  2809. sink);
  2810. if (ret != 0) {
  2811. dev_err(sink->dev,
  2812. "ASoC: hw_params() failed: %d\n", ret);
  2813. goto out;
  2814. }
  2815. }
  2816. break;
  2817. case SND_SOC_DAPM_POST_PMU:
  2818. ret = snd_soc_dai_digital_mute(sink, 0);
  2819. if (ret != 0 && ret != -ENOTSUPP)
  2820. dev_warn(sink->dev, "ASoC: Failed to unmute: %d\n", ret);
  2821. ret = 0;
  2822. break;
  2823. case SND_SOC_DAPM_PRE_PMD:
  2824. ret = snd_soc_dai_digital_mute(sink, 1);
  2825. if (ret != 0 && ret != -ENOTSUPP)
  2826. dev_warn(sink->dev, "ASoC: Failed to mute: %d\n", ret);
  2827. ret = 0;
  2828. break;
  2829. default:
  2830. BUG();
  2831. return -EINVAL;
  2832. }
  2833. out:
  2834. kfree(params);
  2835. return ret;
  2836. }
  2837. int snd_soc_dapm_new_pcm(struct snd_soc_card *card,
  2838. const struct snd_soc_pcm_stream *params,
  2839. struct snd_soc_dapm_widget *source,
  2840. struct snd_soc_dapm_widget *sink)
  2841. {
  2842. struct snd_soc_dapm_route routes[2];
  2843. struct snd_soc_dapm_widget template;
  2844. struct snd_soc_dapm_widget *w;
  2845. size_t len;
  2846. char *link_name;
  2847. len = strlen(source->name) + strlen(sink->name) + 2;
  2848. link_name = devm_kzalloc(card->dev, len, GFP_KERNEL);
  2849. if (!link_name)
  2850. return -ENOMEM;
  2851. snprintf(link_name, len, "%s-%s", source->name, sink->name);
  2852. memset(&template, 0, sizeof(template));
  2853. template.reg = SND_SOC_NOPM;
  2854. template.id = snd_soc_dapm_dai_link;
  2855. template.name = link_name;
  2856. template.event = snd_soc_dai_link_event;
  2857. template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  2858. SND_SOC_DAPM_PRE_PMD;
  2859. dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
  2860. w = snd_soc_dapm_new_control(&card->dapm, &template);
  2861. if (!w) {
  2862. dev_err(card->dev, "ASoC: Failed to create %s widget\n",
  2863. link_name);
  2864. return -ENOMEM;
  2865. }
  2866. w->params = params;
  2867. memset(&routes, 0, sizeof(routes));
  2868. routes[0].source = source->name;
  2869. routes[0].sink = link_name;
  2870. routes[1].source = link_name;
  2871. routes[1].sink = sink->name;
  2872. return snd_soc_dapm_add_routes(&card->dapm, routes,
  2873. ARRAY_SIZE(routes));
  2874. }
  2875. int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
  2876. struct snd_soc_dai *dai)
  2877. {
  2878. struct snd_soc_dapm_widget template;
  2879. struct snd_soc_dapm_widget *w;
  2880. WARN_ON(dapm->dev != dai->dev);
  2881. memset(&template, 0, sizeof(template));
  2882. template.reg = SND_SOC_NOPM;
  2883. if (dai->driver->playback.stream_name) {
  2884. template.id = snd_soc_dapm_dai;
  2885. template.name = dai->driver->playback.stream_name;
  2886. template.sname = dai->driver->playback.stream_name;
  2887. dev_dbg(dai->dev, "ASoC: adding %s widget\n",
  2888. template.name);
  2889. w = snd_soc_dapm_new_control(dapm, &template);
  2890. if (!w) {
  2891. dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
  2892. dai->driver->playback.stream_name);
  2893. }
  2894. w->priv = dai;
  2895. dai->playback_widget = w;
  2896. }
  2897. if (dai->driver->capture.stream_name) {
  2898. template.id = snd_soc_dapm_dai;
  2899. template.name = dai->driver->capture.stream_name;
  2900. template.sname = dai->driver->capture.stream_name;
  2901. dev_dbg(dai->dev, "ASoC: adding %s widget\n",
  2902. template.name);
  2903. w = snd_soc_dapm_new_control(dapm, &template);
  2904. if (!w) {
  2905. dev_err(dapm->dev, "ASoC: Failed to create %s widget\n",
  2906. dai->driver->capture.stream_name);
  2907. }
  2908. w->priv = dai;
  2909. dai->capture_widget = w;
  2910. }
  2911. return 0;
  2912. }
  2913. int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
  2914. {
  2915. struct snd_soc_dapm_widget *dai_w, *w;
  2916. struct snd_soc_dai *dai;
  2917. struct snd_soc_dapm_route r;
  2918. memset(&r, 0, sizeof(r));
  2919. /* For each DAI widget... */
  2920. list_for_each_entry(dai_w, &card->widgets, list) {
  2921. if (dai_w->id != snd_soc_dapm_dai)
  2922. continue;
  2923. dai = dai_w->priv;
  2924. /* ...find all widgets with the same stream and link them */
  2925. list_for_each_entry(w, &card->widgets, list) {
  2926. if (w->dapm != dai_w->dapm)
  2927. continue;
  2928. if (w->id == snd_soc_dapm_dai)
  2929. continue;
  2930. if (!w->sname)
  2931. continue;
  2932. if (dai->driver->playback.stream_name &&
  2933. strstr(w->sname,
  2934. dai->driver->playback.stream_name)) {
  2935. r.source = dai->playback_widget->name;
  2936. r.sink = w->name;
  2937. dev_dbg(dai->dev, "%s -> %s\n",
  2938. r.source, r.sink);
  2939. snd_soc_dapm_add_route(w->dapm, &r);
  2940. }
  2941. if (dai->driver->capture.stream_name &&
  2942. strstr(w->sname,
  2943. dai->driver->capture.stream_name)) {
  2944. r.source = w->name;
  2945. r.sink = dai->capture_widget->name;
  2946. dev_dbg(dai->dev, "%s -> %s\n",
  2947. r.source, r.sink);
  2948. snd_soc_dapm_add_route(w->dapm, &r);
  2949. }
  2950. }
  2951. }
  2952. return 0;
  2953. }
  2954. static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  2955. int event)
  2956. {
  2957. struct snd_soc_dapm_widget *w_cpu, *w_codec;
  2958. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  2959. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  2960. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  2961. w_cpu = cpu_dai->playback_widget;
  2962. w_codec = codec_dai->playback_widget;
  2963. } else {
  2964. w_cpu = cpu_dai->capture_widget;
  2965. w_codec = codec_dai->capture_widget;
  2966. }
  2967. if (w_cpu) {
  2968. dapm_mark_dirty(w_cpu, "stream event");
  2969. switch (event) {
  2970. case SND_SOC_DAPM_STREAM_START:
  2971. w_cpu->active = 1;
  2972. break;
  2973. case SND_SOC_DAPM_STREAM_STOP:
  2974. w_cpu->active = 0;
  2975. break;
  2976. case SND_SOC_DAPM_STREAM_SUSPEND:
  2977. case SND_SOC_DAPM_STREAM_RESUME:
  2978. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2979. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2980. break;
  2981. }
  2982. }
  2983. if (w_codec) {
  2984. dapm_mark_dirty(w_codec, "stream event");
  2985. switch (event) {
  2986. case SND_SOC_DAPM_STREAM_START:
  2987. w_codec->active = 1;
  2988. break;
  2989. case SND_SOC_DAPM_STREAM_STOP:
  2990. w_codec->active = 0;
  2991. break;
  2992. case SND_SOC_DAPM_STREAM_SUSPEND:
  2993. case SND_SOC_DAPM_STREAM_RESUME:
  2994. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2995. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2996. break;
  2997. }
  2998. }
  2999. dapm_power_widgets(&rtd->card->dapm, event);
  3000. }
  3001. /**
  3002. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  3003. * @rtd: PCM runtime data
  3004. * @stream: stream name
  3005. * @event: stream event
  3006. *
  3007. * Sends a stream event to the dapm core. The core then makes any
  3008. * necessary widget power changes.
  3009. *
  3010. * Returns 0 for success else error.
  3011. */
  3012. void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  3013. int event)
  3014. {
  3015. struct snd_soc_card *card = rtd->card;
  3016. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3017. soc_dapm_stream_event(rtd, stream, event);
  3018. mutex_unlock(&card->dapm_mutex);
  3019. }
  3020. /**
  3021. * snd_soc_dapm_enable_pin - enable pin.
  3022. * @dapm: DAPM context
  3023. * @pin: pin name
  3024. *
  3025. * Enables input/output pin and its parents or children widgets iff there is
  3026. * a valid audio route and active audio stream.
  3027. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3028. * do any widget power switching.
  3029. */
  3030. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3031. {
  3032. return snd_soc_dapm_set_pin(dapm, pin, 1);
  3033. }
  3034. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  3035. /**
  3036. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  3037. * @dapm: DAPM context
  3038. * @pin: pin name
  3039. *
  3040. * Enables input/output pin regardless of any other state. This is
  3041. * intended for use with microphone bias supplies used in microphone
  3042. * jack detection.
  3043. *
  3044. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3045. * do any widget power switching.
  3046. */
  3047. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  3048. const char *pin)
  3049. {
  3050. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3051. if (!w) {
  3052. dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
  3053. return -EINVAL;
  3054. }
  3055. dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
  3056. w->connected = 1;
  3057. w->force = 1;
  3058. dapm_mark_dirty(w, "force enable");
  3059. return 0;
  3060. }
  3061. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  3062. /**
  3063. * snd_soc_dapm_disable_pin - disable pin.
  3064. * @dapm: DAPM context
  3065. * @pin: pin name
  3066. *
  3067. * Disables input/output pin and its parents or children widgets.
  3068. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3069. * do any widget power switching.
  3070. */
  3071. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  3072. const char *pin)
  3073. {
  3074. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3075. }
  3076. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  3077. /**
  3078. * snd_soc_dapm_nc_pin - permanently disable pin.
  3079. * @dapm: DAPM context
  3080. * @pin: pin name
  3081. *
  3082. * Marks the specified pin as being not connected, disabling it along
  3083. * any parent or child widgets. At present this is identical to
  3084. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  3085. * additional things such as disabling controls which only affect
  3086. * paths through the pin.
  3087. *
  3088. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3089. * do any widget power switching.
  3090. */
  3091. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3092. {
  3093. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3094. }
  3095. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  3096. /**
  3097. * snd_soc_dapm_get_pin_status - get audio pin status
  3098. * @dapm: DAPM context
  3099. * @pin: audio signal pin endpoint (or start point)
  3100. *
  3101. * Get audio pin status - connected or disconnected.
  3102. *
  3103. * Returns 1 for connected otherwise 0.
  3104. */
  3105. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  3106. const char *pin)
  3107. {
  3108. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3109. if (w)
  3110. return w->connected;
  3111. return 0;
  3112. }
  3113. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  3114. /**
  3115. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  3116. * @dapm: DAPM context
  3117. * @pin: audio signal pin endpoint (or start point)
  3118. *
  3119. * Mark the given endpoint or pin as ignoring suspend. When the
  3120. * system is disabled a path between two endpoints flagged as ignoring
  3121. * suspend will not be disabled. The path must already be enabled via
  3122. * normal means at suspend time, it will not be turned on if it was not
  3123. * already enabled.
  3124. */
  3125. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  3126. const char *pin)
  3127. {
  3128. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  3129. if (!w) {
  3130. dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
  3131. return -EINVAL;
  3132. }
  3133. w->ignore_suspend = 1;
  3134. return 0;
  3135. }
  3136. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  3137. static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
  3138. struct snd_soc_dapm_widget *w)
  3139. {
  3140. struct snd_soc_dapm_path *p;
  3141. list_for_each_entry(p, &card->paths, list) {
  3142. if ((p->source == w) || (p->sink == w)) {
  3143. dev_dbg(card->dev,
  3144. "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
  3145. p->source->name, p->source->id, p->source->dapm,
  3146. p->sink->name, p->sink->id, p->sink->dapm);
  3147. /* Connected to something other than the codec */
  3148. if (p->source->dapm != p->sink->dapm)
  3149. return true;
  3150. /*
  3151. * Loopback connection from codec external pin to
  3152. * codec external pin
  3153. */
  3154. if (p->sink->id == snd_soc_dapm_input) {
  3155. switch (p->source->id) {
  3156. case snd_soc_dapm_output:
  3157. case snd_soc_dapm_micbias:
  3158. return true;
  3159. default:
  3160. break;
  3161. }
  3162. }
  3163. }
  3164. }
  3165. return false;
  3166. }
  3167. /**
  3168. * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
  3169. * @codec: The codec whose pins should be processed
  3170. *
  3171. * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
  3172. * which are unused. Pins are used if they are connected externally to the
  3173. * codec, whether that be to some other device, or a loop-back connection to
  3174. * the codec itself.
  3175. */
  3176. void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
  3177. {
  3178. struct snd_soc_card *card = codec->card;
  3179. struct snd_soc_dapm_context *dapm = &codec->dapm;
  3180. struct snd_soc_dapm_widget *w;
  3181. dev_dbg(codec->dev, "ASoC: Auto NC: DAPMs: card:%p codec:%p\n",
  3182. &card->dapm, &codec->dapm);
  3183. list_for_each_entry(w, &card->widgets, list) {
  3184. if (w->dapm != dapm)
  3185. continue;
  3186. switch (w->id) {
  3187. case snd_soc_dapm_input:
  3188. case snd_soc_dapm_output:
  3189. case snd_soc_dapm_micbias:
  3190. dev_dbg(codec->dev, "ASoC: Auto NC: Checking widget %s\n",
  3191. w->name);
  3192. if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
  3193. dev_dbg(codec->dev,
  3194. "... Not in map; disabling\n");
  3195. snd_soc_dapm_nc_pin(dapm, w->name);
  3196. }
  3197. break;
  3198. default:
  3199. break;
  3200. }
  3201. }
  3202. }
  3203. /**
  3204. * snd_soc_dapm_free - free dapm resources
  3205. * @dapm: DAPM context
  3206. *
  3207. * Free all dapm widgets and resources.
  3208. */
  3209. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  3210. {
  3211. snd_soc_dapm_sys_remove(dapm->dev);
  3212. dapm_debugfs_cleanup(dapm);
  3213. dapm_free_widgets(dapm);
  3214. list_del(&dapm->list);
  3215. }
  3216. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  3217. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  3218. {
  3219. struct snd_soc_card *card = dapm->card;
  3220. struct snd_soc_dapm_widget *w;
  3221. LIST_HEAD(down_list);
  3222. int powerdown = 0;
  3223. mutex_lock(&card->dapm_mutex);
  3224. list_for_each_entry(w, &dapm->card->widgets, list) {
  3225. if (w->dapm != dapm)
  3226. continue;
  3227. if (w->power) {
  3228. dapm_seq_insert(w, &down_list, false);
  3229. w->power = 0;
  3230. powerdown = 1;
  3231. }
  3232. }
  3233. /* If there were no widgets to power down we're already in
  3234. * standby.
  3235. */
  3236. if (powerdown) {
  3237. if (dapm->bias_level == SND_SOC_BIAS_ON)
  3238. snd_soc_dapm_set_bias_level(dapm,
  3239. SND_SOC_BIAS_PREPARE);
  3240. dapm_seq_run(dapm, &down_list, 0, false);
  3241. if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
  3242. snd_soc_dapm_set_bias_level(dapm,
  3243. SND_SOC_BIAS_STANDBY);
  3244. }
  3245. mutex_unlock(&card->dapm_mutex);
  3246. }
  3247. /*
  3248. * snd_soc_dapm_shutdown - callback for system shutdown
  3249. */
  3250. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  3251. {
  3252. struct snd_soc_codec *codec;
  3253. list_for_each_entry(codec, &card->codec_dev_list, card_list) {
  3254. soc_dapm_shutdown_codec(&codec->dapm);
  3255. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  3256. snd_soc_dapm_set_bias_level(&codec->dapm,
  3257. SND_SOC_BIAS_OFF);
  3258. }
  3259. }
  3260. /* Module information */
  3261. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3262. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  3263. MODULE_LICENSE("GPL");