soc-dapm.c 93 KB

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