soc-dapm.c 94 KB

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