soc-dapm.c 88 KB

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