soc-dapm.c 94 KB

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