soc-dapm.c 70 KB

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