soc-dapm.c 96 KB

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