soc-dapm.c 94 KB

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