soc-dapm.c 86 KB

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