soc-dapm.c 54 KB

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