soc-dapm.c 94 KB

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