soc-dapm.c 80 KB

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