soc-dapm.c 96 KB

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