soc-dapm.c 93 KB

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