soc-dapm.c 58 KB

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