soc-dapm.c 56 KB

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