soc-dapm.c 81 KB

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