soc-dapm.c 72 KB

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