soc-dapm.c 96 KB

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