soc-dapm.c 93 KB

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