soc-dapm.c 94 KB

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