soc-dapm.c 55 KB

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