soc-dapm.c 115 KB

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