soc-dapm.c 87 KB

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