soc-dapm.c 56 KB

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