soc-dapm.c 105 KB

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