soc-dapm.c 65 KB

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