soc-dapm.c 87 KB

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