soc-dapm.c 105 KB

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