soc-dapm.c 61 KB

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