soc-dapm.c 97 KB

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