soc-dapm.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Author: Liam Girdwood
  6. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * Revision history
  14. * 12th Aug 2005 Initial version.
  15. * 25th Oct 2005 Implemented path power domain.
  16. * 18th Dec 2005 Implemented machine and stream level power domain.
  17. *
  18. * Features:
  19. * o Changes power status of internal codec blocks depending on the
  20. * dynamic configuration of codec internal audio paths and active
  21. * DAC's/ADC's.
  22. * o Platform power domain - can support external components i.e. amps and
  23. * mic/meadphone insertion events.
  24. * o Automatic Mic Bias support
  25. * o Jack insertion power event initiation - e.g. hp insertion will enable
  26. * sinks, dacs, etc
  27. * o Delayed powerdown of audio susbsystem to reduce pops between a quick
  28. * device reopen.
  29. *
  30. * Todo:
  31. * o DAPM power change sequencing - allow for configurable per
  32. * codec sequences.
  33. * o Support for analogue bias optimisation.
  34. * o Support for reduced codec oversampling rates.
  35. * o Support for reduced codec bias currents.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/init.h>
  40. #include <linux/delay.h>
  41. #include <linux/pm.h>
  42. #include <linux/bitops.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/jiffies.h>
  45. #include <sound/core.h>
  46. #include <sound/pcm.h>
  47. #include <sound/pcm_params.h>
  48. #include <sound/soc-dapm.h>
  49. #include <sound/initval.h>
  50. /* debug */
  51. #define DAPM_DEBUG 0
  52. #if DAPM_DEBUG
  53. #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
  54. #define dbg(format, arg...) printk(format, ## arg)
  55. #else
  56. #define dump_dapm(codec, action)
  57. #define dbg(format, arg...)
  58. #endif
  59. #define POP_DEBUG 0
  60. #if POP_DEBUG
  61. #define POP_TIME 500 /* 500 msecs - change if pop debug is too fast */
  62. #define pop_wait(time) schedule_timeout_uninterruptible(msecs_to_jiffies(time))
  63. #define pop_dbg(format, arg...) printk(format, ## arg); pop_wait(POP_TIME)
  64. #else
  65. #define pop_dbg(format, arg...)
  66. #define pop_wait(time)
  67. #endif
  68. /* dapm power sequences - make this per codec in the future */
  69. static int dapm_up_seq[] = {
  70. snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
  71. snd_soc_dapm_mux, snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_pga,
  72. snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
  73. };
  74. static int dapm_down_seq[] = {
  75. snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
  76. snd_soc_dapm_pga, snd_soc_dapm_mixer, snd_soc_dapm_dac, snd_soc_dapm_mic,
  77. snd_soc_dapm_micbias, snd_soc_dapm_mux, snd_soc_dapm_post
  78. };
  79. static int dapm_status = 1;
  80. module_param(dapm_status, int, 0);
  81. MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
  82. /* create a new dapm widget */
  83. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  84. const struct snd_soc_dapm_widget *_widget)
  85. {
  86. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  87. }
  88. /* set up initial codec paths */
  89. static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
  90. struct snd_soc_dapm_path *p, int i)
  91. {
  92. switch (w->id) {
  93. case snd_soc_dapm_switch:
  94. case snd_soc_dapm_mixer: {
  95. int val;
  96. int reg = w->kcontrols[i].private_value & 0xff;
  97. int shift = (w->kcontrols[i].private_value >> 8) & 0x0f;
  98. int mask = (w->kcontrols[i].private_value >> 16) & 0xff;
  99. int invert = (w->kcontrols[i].private_value >> 24) & 0x01;
  100. val = snd_soc_read(w->codec, reg);
  101. val = (val >> shift) & mask;
  102. if ((invert && !val) || (!invert && val))
  103. p->connect = 1;
  104. else
  105. p->connect = 0;
  106. }
  107. break;
  108. case snd_soc_dapm_mux: {
  109. struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
  110. int val, item, bitmask;
  111. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  112. ;
  113. val = snd_soc_read(w->codec, e->reg);
  114. item = (val >> e->shift_l) & (bitmask - 1);
  115. p->connect = 0;
  116. for (i = 0; i < e->mask; i++) {
  117. if (!(strcmp(p->name, e->texts[i])) && item == i)
  118. p->connect = 1;
  119. }
  120. }
  121. break;
  122. /* does not effect routing - always connected */
  123. case snd_soc_dapm_pga:
  124. case snd_soc_dapm_output:
  125. case snd_soc_dapm_adc:
  126. case snd_soc_dapm_input:
  127. case snd_soc_dapm_dac:
  128. case snd_soc_dapm_micbias:
  129. case snd_soc_dapm_vmid:
  130. p->connect = 1;
  131. break;
  132. /* does effect routing - dynamically connected */
  133. case snd_soc_dapm_hp:
  134. case snd_soc_dapm_mic:
  135. case snd_soc_dapm_spk:
  136. case snd_soc_dapm_line:
  137. case snd_soc_dapm_pre:
  138. case snd_soc_dapm_post:
  139. p->connect = 0;
  140. break;
  141. }
  142. }
  143. /* connect mux widget to it's interconnecting audio paths */
  144. static int dapm_connect_mux(struct snd_soc_codec *codec,
  145. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  146. struct snd_soc_dapm_path *path, const char *control_name,
  147. const struct snd_kcontrol_new *kcontrol)
  148. {
  149. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  150. int i;
  151. for (i = 0; i < e->mask; i++) {
  152. if (!(strcmp(control_name, e->texts[i]))) {
  153. list_add(&path->list, &codec->dapm_paths);
  154. list_add(&path->list_sink, &dest->sources);
  155. list_add(&path->list_source, &src->sinks);
  156. path->name = (char*)e->texts[i];
  157. dapm_set_path_status(dest, path, 0);
  158. return 0;
  159. }
  160. }
  161. return -ENODEV;
  162. }
  163. /* connect mixer widget to it's interconnecting audio paths */
  164. static int dapm_connect_mixer(struct snd_soc_codec *codec,
  165. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  166. struct snd_soc_dapm_path *path, const char *control_name)
  167. {
  168. int i;
  169. /* search for mixer kcontrol */
  170. for (i = 0; i < dest->num_kcontrols; i++) {
  171. if (!strcmp(control_name, dest->kcontrols[i].name)) {
  172. list_add(&path->list, &codec->dapm_paths);
  173. list_add(&path->list_sink, &dest->sources);
  174. list_add(&path->list_source, &src->sinks);
  175. path->name = dest->kcontrols[i].name;
  176. dapm_set_path_status(dest, path, i);
  177. return 0;
  178. }
  179. }
  180. return -ENODEV;
  181. }
  182. /* update dapm codec register bits */
  183. static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
  184. {
  185. int change, power;
  186. unsigned short old, new;
  187. struct snd_soc_codec *codec = widget->codec;
  188. /* check for valid widgets */
  189. if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
  190. widget->id == snd_soc_dapm_output ||
  191. widget->id == snd_soc_dapm_hp ||
  192. widget->id == snd_soc_dapm_mic ||
  193. widget->id == snd_soc_dapm_line ||
  194. widget->id == snd_soc_dapm_spk)
  195. return 0;
  196. power = widget->power;
  197. if (widget->invert)
  198. power = (power ? 0:1);
  199. old = snd_soc_read(codec, widget->reg);
  200. new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
  201. change = old != new;
  202. if (change) {
  203. pop_dbg("pop test %s : %s in %d ms\n", widget->name,
  204. widget->power ? "on" : "off", POP_TIME);
  205. snd_soc_write(codec, widget->reg, new);
  206. pop_wait(POP_TIME);
  207. }
  208. dbg("reg %x old %x new %x change %d\n", widget->reg, old, new, change);
  209. return change;
  210. }
  211. /* ramps the volume up or down to minimise pops before or after a
  212. * DAPM power event */
  213. static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
  214. {
  215. const struct snd_kcontrol_new *k = widget->kcontrols;
  216. if (widget->muted && !power)
  217. return 0;
  218. if (!widget->muted && power)
  219. return 0;
  220. if (widget->num_kcontrols && k) {
  221. int reg = k->private_value & 0xff;
  222. int shift = (k->private_value >> 8) & 0x0f;
  223. int mask = (k->private_value >> 16) & 0xff;
  224. int invert = (k->private_value >> 24) & 0x01;
  225. if (power) {
  226. int i;
  227. /* power up has happended, increase volume to last level */
  228. if (invert) {
  229. for (i = mask; i > widget->saved_value; i--)
  230. snd_soc_update_bits(widget->codec, reg, mask, i);
  231. } else {
  232. for (i = 0; i < widget->saved_value; i++)
  233. snd_soc_update_bits(widget->codec, reg, mask, i);
  234. }
  235. widget->muted = 0;
  236. } else {
  237. /* power down is about to occur, decrease volume to mute */
  238. int val = snd_soc_read(widget->codec, reg);
  239. int i = widget->saved_value = (val >> shift) & mask;
  240. if (invert) {
  241. for (; i < mask; i++)
  242. snd_soc_update_bits(widget->codec, reg, mask, i);
  243. } else {
  244. for (; i > 0; i--)
  245. snd_soc_update_bits(widget->codec, reg, mask, i);
  246. }
  247. widget->muted = 1;
  248. }
  249. }
  250. return 0;
  251. }
  252. /* create new dapm mixer control */
  253. static int dapm_new_mixer(struct snd_soc_codec *codec,
  254. struct snd_soc_dapm_widget *w)
  255. {
  256. int i, ret = 0;
  257. char name[32];
  258. struct snd_soc_dapm_path *path;
  259. /* add kcontrol */
  260. for (i = 0; i < w->num_kcontrols; i++) {
  261. /* match name */
  262. list_for_each_entry(path, &w->sources, list_sink) {
  263. /* mixer/mux paths name must match control name */
  264. if (path->name != (char*)w->kcontrols[i].name)
  265. continue;
  266. /* add dapm control with long name */
  267. snprintf(name, 32, "%s %s", w->name, w->kcontrols[i].name);
  268. path->long_name = kstrdup (name, GFP_KERNEL);
  269. if (path->long_name == NULL)
  270. return -ENOMEM;
  271. path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
  272. path->long_name);
  273. ret = snd_ctl_add(codec->card, path->kcontrol);
  274. if (ret < 0) {
  275. printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
  276. path->long_name);
  277. kfree(path->long_name);
  278. path->long_name = NULL;
  279. return ret;
  280. }
  281. }
  282. }
  283. return ret;
  284. }
  285. /* create new dapm mux control */
  286. static int dapm_new_mux(struct snd_soc_codec *codec,
  287. struct snd_soc_dapm_widget *w)
  288. {
  289. struct snd_soc_dapm_path *path = NULL;
  290. struct snd_kcontrol *kcontrol;
  291. int ret = 0;
  292. if (!w->num_kcontrols) {
  293. printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
  294. return -EINVAL;
  295. }
  296. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  297. ret = snd_ctl_add(codec->card, kcontrol);
  298. if (ret < 0)
  299. goto err;
  300. list_for_each_entry(path, &w->sources, list_sink)
  301. path->kcontrol = kcontrol;
  302. return ret;
  303. err:
  304. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  305. return ret;
  306. }
  307. /* create new dapm volume control */
  308. static int dapm_new_pga(struct snd_soc_codec *codec,
  309. struct snd_soc_dapm_widget *w)
  310. {
  311. struct snd_kcontrol *kcontrol;
  312. int ret = 0;
  313. if (!w->num_kcontrols)
  314. return -EINVAL;
  315. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  316. ret = snd_ctl_add(codec->card, kcontrol);
  317. if (ret < 0) {
  318. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  319. return ret;
  320. }
  321. return ret;
  322. }
  323. /* reset 'walked' bit for each dapm path */
  324. static inline void dapm_clear_walk(struct snd_soc_codec *codec)
  325. {
  326. struct snd_soc_dapm_path *p;
  327. list_for_each_entry(p, &codec->dapm_paths, list)
  328. p->walked = 0;
  329. }
  330. /*
  331. * Recursively check for a completed path to an active or physically connected
  332. * output widget. Returns number of complete paths.
  333. */
  334. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
  335. {
  336. struct snd_soc_dapm_path *path;
  337. int con = 0;
  338. if (widget->id == snd_soc_dapm_adc && widget->active)
  339. return 1;
  340. if (widget->connected) {
  341. /* connected pin ? */
  342. if (widget->id == snd_soc_dapm_output && !widget->ext)
  343. return 1;
  344. /* connected jack or spk ? */
  345. if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
  346. widget->id == snd_soc_dapm_line)
  347. return 1;
  348. }
  349. list_for_each_entry(path, &widget->sinks, list_source) {
  350. if (path->walked)
  351. continue;
  352. if (path->sink && path->connect) {
  353. path->walked = 1;
  354. con += is_connected_output_ep(path->sink);
  355. }
  356. }
  357. return con;
  358. }
  359. /*
  360. * Recursively check for a completed path to an active or physically connected
  361. * input widget. Returns number of complete paths.
  362. */
  363. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
  364. {
  365. struct snd_soc_dapm_path *path;
  366. int con = 0;
  367. /* active stream ? */
  368. if (widget->id == snd_soc_dapm_dac && widget->active)
  369. return 1;
  370. if (widget->connected) {
  371. /* connected pin ? */
  372. if (widget->id == snd_soc_dapm_input && !widget->ext)
  373. return 1;
  374. /* connected VMID/Bias for lower pops */
  375. if (widget->id == snd_soc_dapm_vmid)
  376. return 1;
  377. /* connected jack ? */
  378. if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
  379. return 1;
  380. }
  381. list_for_each_entry(path, &widget->sources, list_sink) {
  382. if (path->walked)
  383. continue;
  384. if (path->source && path->connect) {
  385. path->walked = 1;
  386. con += is_connected_input_ep(path->source);
  387. }
  388. }
  389. return con;
  390. }
  391. /*
  392. * Scan each dapm widget for complete audio path.
  393. * A complete path is a route that has valid endpoints i.e.:-
  394. *
  395. * o DAC to output pin.
  396. * o Input Pin to ADC.
  397. * o Input pin to Output pin (bypass, sidetone)
  398. * o DAC to ADC (loopback).
  399. */
  400. static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
  401. {
  402. struct snd_soc_dapm_widget *w;
  403. int in, out, i, c = 1, *seq = NULL, ret = 0, power_change, power;
  404. /* do we have a sequenced stream event */
  405. if (event == SND_SOC_DAPM_STREAM_START) {
  406. c = ARRAY_SIZE(dapm_up_seq);
  407. seq = dapm_up_seq;
  408. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  409. c = ARRAY_SIZE(dapm_down_seq);
  410. seq = dapm_down_seq;
  411. }
  412. for(i = 0; i < c; i++) {
  413. list_for_each_entry(w, &codec->dapm_widgets, list) {
  414. /* is widget in stream order */
  415. if (seq && seq[i] && w->id != seq[i])
  416. continue;
  417. /* vmid - no action */
  418. if (w->id == snd_soc_dapm_vmid)
  419. continue;
  420. /* active ADC */
  421. if (w->id == snd_soc_dapm_adc && w->active) {
  422. in = is_connected_input_ep(w);
  423. dapm_clear_walk(w->codec);
  424. w->power = (in != 0) ? 1 : 0;
  425. dapm_update_bits(w);
  426. continue;
  427. }
  428. /* active DAC */
  429. if (w->id == snd_soc_dapm_dac && w->active) {
  430. out = is_connected_output_ep(w);
  431. dapm_clear_walk(w->codec);
  432. w->power = (out != 0) ? 1 : 0;
  433. dapm_update_bits(w);
  434. continue;
  435. }
  436. /* programmable gain/attenuation */
  437. if (w->id == snd_soc_dapm_pga) {
  438. int on;
  439. in = is_connected_input_ep(w);
  440. dapm_clear_walk(w->codec);
  441. out = is_connected_output_ep(w);
  442. dapm_clear_walk(w->codec);
  443. w->power = on = (out != 0 && in != 0) ? 1 : 0;
  444. if (!on)
  445. dapm_set_pga(w, on); /* lower volume to reduce pops */
  446. dapm_update_bits(w);
  447. if (on)
  448. dapm_set_pga(w, on); /* restore volume from zero */
  449. continue;
  450. }
  451. /* pre and post event widgets */
  452. if (w->id == snd_soc_dapm_pre) {
  453. if (!w->event)
  454. continue;
  455. if (event == SND_SOC_DAPM_STREAM_START) {
  456. ret = w->event(w,
  457. NULL, SND_SOC_DAPM_PRE_PMU);
  458. if (ret < 0)
  459. return ret;
  460. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  461. ret = w->event(w,
  462. NULL, SND_SOC_DAPM_PRE_PMD);
  463. if (ret < 0)
  464. return ret;
  465. }
  466. continue;
  467. }
  468. if (w->id == snd_soc_dapm_post) {
  469. if (!w->event)
  470. continue;
  471. if (event == SND_SOC_DAPM_STREAM_START) {
  472. ret = w->event(w,
  473. NULL, SND_SOC_DAPM_POST_PMU);
  474. if (ret < 0)
  475. return ret;
  476. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  477. ret = w->event(w,
  478. NULL, SND_SOC_DAPM_POST_PMD);
  479. if (ret < 0)
  480. return ret;
  481. }
  482. continue;
  483. }
  484. /* all other widgets */
  485. in = is_connected_input_ep(w);
  486. dapm_clear_walk(w->codec);
  487. out = is_connected_output_ep(w);
  488. dapm_clear_walk(w->codec);
  489. power = (out != 0 && in != 0) ? 1 : 0;
  490. power_change = (w->power == power) ? 0: 1;
  491. w->power = power;
  492. /* call any power change event handlers */
  493. if (power_change) {
  494. if (w->event) {
  495. dbg("power %s event for %s flags %x\n",
  496. w->power ? "on" : "off", w->name, w->event_flags);
  497. if (power) {
  498. /* power up event */
  499. if (w->event_flags & SND_SOC_DAPM_PRE_PMU) {
  500. ret = w->event(w,
  501. NULL, SND_SOC_DAPM_PRE_PMU);
  502. if (ret < 0)
  503. return ret;
  504. }
  505. dapm_update_bits(w);
  506. if (w->event_flags & SND_SOC_DAPM_POST_PMU){
  507. ret = w->event(w,
  508. NULL, SND_SOC_DAPM_POST_PMU);
  509. if (ret < 0)
  510. return ret;
  511. }
  512. } else {
  513. /* power down event */
  514. if (w->event_flags & SND_SOC_DAPM_PRE_PMD) {
  515. ret = w->event(w,
  516. NULL, SND_SOC_DAPM_PRE_PMD);
  517. if (ret < 0)
  518. return ret;
  519. }
  520. dapm_update_bits(w);
  521. if (w->event_flags & SND_SOC_DAPM_POST_PMD) {
  522. ret = w->event(w,
  523. NULL, SND_SOC_DAPM_POST_PMD);
  524. if (ret < 0)
  525. return ret;
  526. }
  527. }
  528. } else
  529. /* no event handler */
  530. dapm_update_bits(w);
  531. }
  532. }
  533. }
  534. return ret;
  535. }
  536. #if DAPM_DEBUG
  537. static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
  538. {
  539. struct snd_soc_dapm_widget *w;
  540. struct snd_soc_dapm_path *p = NULL;
  541. int in, out;
  542. printk("DAPM %s %s\n", codec->name, action);
  543. list_for_each_entry(w, &codec->dapm_widgets, list) {
  544. /* only display widgets that effect routing */
  545. switch (w->id) {
  546. case snd_soc_dapm_pre:
  547. case snd_soc_dapm_post:
  548. case snd_soc_dapm_vmid:
  549. continue;
  550. case snd_soc_dapm_mux:
  551. case snd_soc_dapm_output:
  552. case snd_soc_dapm_input:
  553. case snd_soc_dapm_switch:
  554. case snd_soc_dapm_hp:
  555. case snd_soc_dapm_mic:
  556. case snd_soc_dapm_spk:
  557. case snd_soc_dapm_line:
  558. case snd_soc_dapm_micbias:
  559. case snd_soc_dapm_dac:
  560. case snd_soc_dapm_adc:
  561. case snd_soc_dapm_pga:
  562. case snd_soc_dapm_mixer:
  563. if (w->name) {
  564. in = is_connected_input_ep(w);
  565. dapm_clear_walk(w->codec);
  566. out = is_connected_output_ep(w);
  567. dapm_clear_walk(w->codec);
  568. printk("%s: %s in %d out %d\n", w->name,
  569. w->power ? "On":"Off",in, out);
  570. list_for_each_entry(p, &w->sources, list_sink) {
  571. if (p->connect)
  572. printk(" in %s %s\n", p->name ? p->name : "static",
  573. p->source->name);
  574. }
  575. list_for_each_entry(p, &w->sinks, list_source) {
  576. if (p->connect)
  577. printk(" out %s %s\n", p->name ? p->name : "static",
  578. p->sink->name);
  579. }
  580. }
  581. break;
  582. }
  583. }
  584. }
  585. #endif
  586. /* test and update the power status of a mux widget */
  587. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  588. struct snd_kcontrol *kcontrol, int mask,
  589. int val, struct soc_enum* e)
  590. {
  591. struct snd_soc_dapm_path *path;
  592. int found = 0;
  593. if (widget->id != snd_soc_dapm_mux)
  594. return -ENODEV;
  595. if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
  596. return 0;
  597. /* find dapm widget path assoc with kcontrol */
  598. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  599. if (path->kcontrol != kcontrol)
  600. continue;
  601. if (!path->name || ! e->texts[val])
  602. continue;
  603. found = 1;
  604. /* we now need to match the string in the enum to the path */
  605. if (!(strcmp(path->name, e->texts[val])))
  606. path->connect = 1; /* new connection */
  607. else
  608. path->connect = 0; /* old connection must be powered down */
  609. }
  610. if (found)
  611. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  612. return 0;
  613. }
  614. /* test and update the power status of a mixer or switch widget */
  615. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  616. struct snd_kcontrol *kcontrol, int reg,
  617. int val_mask, int val, int invert)
  618. {
  619. struct snd_soc_dapm_path *path;
  620. int found = 0;
  621. if (widget->id != snd_soc_dapm_mixer &&
  622. widget->id != snd_soc_dapm_switch)
  623. return -ENODEV;
  624. if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
  625. return 0;
  626. /* find dapm widget path assoc with kcontrol */
  627. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  628. if (path->kcontrol != kcontrol)
  629. continue;
  630. /* found, now check type */
  631. found = 1;
  632. if (val)
  633. /* new connection */
  634. path->connect = invert ? 0:1;
  635. else
  636. /* old connection must be powered down */
  637. path->connect = invert ? 1:0;
  638. break;
  639. }
  640. if (found)
  641. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  642. return 0;
  643. }
  644. /* show dapm widget status in sys fs */
  645. static ssize_t dapm_widget_show(struct device *dev,
  646. struct device_attribute *attr, char *buf)
  647. {
  648. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  649. struct snd_soc_codec *codec = devdata->codec;
  650. struct snd_soc_dapm_widget *w;
  651. int count = 0;
  652. char *state = "not set";
  653. list_for_each_entry(w, &codec->dapm_widgets, list) {
  654. /* only display widgets that burnm power */
  655. switch (w->id) {
  656. case snd_soc_dapm_hp:
  657. case snd_soc_dapm_mic:
  658. case snd_soc_dapm_spk:
  659. case snd_soc_dapm_line:
  660. case snd_soc_dapm_micbias:
  661. case snd_soc_dapm_dac:
  662. case snd_soc_dapm_adc:
  663. case snd_soc_dapm_pga:
  664. case snd_soc_dapm_mixer:
  665. if (w->name)
  666. count += sprintf(buf + count, "%s: %s\n",
  667. w->name, w->power ? "On":"Off");
  668. break;
  669. default:
  670. break;
  671. }
  672. }
  673. switch(codec->dapm_state){
  674. case SNDRV_CTL_POWER_D0:
  675. state = "D0";
  676. break;
  677. case SNDRV_CTL_POWER_D1:
  678. state = "D1";
  679. break;
  680. case SNDRV_CTL_POWER_D2:
  681. state = "D2";
  682. break;
  683. case SNDRV_CTL_POWER_D3hot:
  684. state = "D3hot";
  685. break;
  686. case SNDRV_CTL_POWER_D3cold:
  687. state = "D3cold";
  688. break;
  689. }
  690. count += sprintf(buf + count, "PM State: %s\n", state);
  691. return count;
  692. }
  693. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  694. int snd_soc_dapm_sys_add(struct device *dev)
  695. {
  696. int ret = 0;
  697. if (dapm_status)
  698. ret = device_create_file(dev, &dev_attr_dapm_widget);
  699. return ret;
  700. }
  701. static void snd_soc_dapm_sys_remove(struct device *dev)
  702. {
  703. if (dapm_status)
  704. device_remove_file(dev, &dev_attr_dapm_widget);
  705. }
  706. /* free all dapm widgets and resources */
  707. static void dapm_free_widgets(struct snd_soc_codec *codec)
  708. {
  709. struct snd_soc_dapm_widget *w, *next_w;
  710. struct snd_soc_dapm_path *p, *next_p;
  711. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  712. list_del(&w->list);
  713. kfree(w);
  714. }
  715. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  716. list_del(&p->list);
  717. kfree(p->long_name);
  718. kfree(p);
  719. }
  720. }
  721. /**
  722. * snd_soc_dapm_sync_endpoints - scan and power dapm paths
  723. * @codec: audio codec
  724. *
  725. * Walks all dapm audio paths and powers widgets according to their
  726. * stream or path usage.
  727. *
  728. * Returns 0 for success.
  729. */
  730. int snd_soc_dapm_sync_endpoints(struct snd_soc_codec *codec)
  731. {
  732. return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  733. }
  734. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_endpoints);
  735. /**
  736. * snd_soc_dapm_connect_input - connect dapm widgets
  737. * @codec: audio codec
  738. * @sink: name of target widget
  739. * @control: mixer control name
  740. * @source: name of source name
  741. *
  742. * Connects 2 dapm widgets together via a named audio path. The sink is
  743. * the widget receiving the audio signal, whilst the source is the sender
  744. * of the audio signal.
  745. *
  746. * Returns 0 for success else error.
  747. */
  748. int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
  749. const char * control, const char *source)
  750. {
  751. struct snd_soc_dapm_path *path;
  752. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  753. int ret = 0;
  754. /* find src and dest widgets */
  755. list_for_each_entry(w, &codec->dapm_widgets, list) {
  756. if (!wsink && !(strcmp(w->name, sink))) {
  757. wsink = w;
  758. continue;
  759. }
  760. if (!wsource && !(strcmp(w->name, source))) {
  761. wsource = w;
  762. }
  763. }
  764. if (wsource == NULL || wsink == NULL)
  765. return -ENODEV;
  766. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  767. if (!path)
  768. return -ENOMEM;
  769. path->source = wsource;
  770. path->sink = wsink;
  771. INIT_LIST_HEAD(&path->list);
  772. INIT_LIST_HEAD(&path->list_source);
  773. INIT_LIST_HEAD(&path->list_sink);
  774. /* check for external widgets */
  775. if (wsink->id == snd_soc_dapm_input) {
  776. if (wsource->id == snd_soc_dapm_micbias ||
  777. wsource->id == snd_soc_dapm_mic ||
  778. wsink->id == snd_soc_dapm_line ||
  779. wsink->id == snd_soc_dapm_output)
  780. wsink->ext = 1;
  781. }
  782. if (wsource->id == snd_soc_dapm_output) {
  783. if (wsink->id == snd_soc_dapm_spk ||
  784. wsink->id == snd_soc_dapm_hp ||
  785. wsink->id == snd_soc_dapm_line ||
  786. wsink->id == snd_soc_dapm_input)
  787. wsource->ext = 1;
  788. }
  789. /* connect static paths */
  790. if (control == NULL) {
  791. list_add(&path->list, &codec->dapm_paths);
  792. list_add(&path->list_sink, &wsink->sources);
  793. list_add(&path->list_source, &wsource->sinks);
  794. path->connect = 1;
  795. return 0;
  796. }
  797. /* connect dynamic paths */
  798. switch(wsink->id) {
  799. case snd_soc_dapm_adc:
  800. case snd_soc_dapm_dac:
  801. case snd_soc_dapm_pga:
  802. case snd_soc_dapm_input:
  803. case snd_soc_dapm_output:
  804. case snd_soc_dapm_micbias:
  805. case snd_soc_dapm_vmid:
  806. case snd_soc_dapm_pre:
  807. case snd_soc_dapm_post:
  808. list_add(&path->list, &codec->dapm_paths);
  809. list_add(&path->list_sink, &wsink->sources);
  810. list_add(&path->list_source, &wsource->sinks);
  811. path->connect = 1;
  812. return 0;
  813. case snd_soc_dapm_mux:
  814. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  815. &wsink->kcontrols[0]);
  816. if (ret != 0)
  817. goto err;
  818. break;
  819. case snd_soc_dapm_switch:
  820. case snd_soc_dapm_mixer:
  821. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  822. if (ret != 0)
  823. goto err;
  824. break;
  825. case snd_soc_dapm_hp:
  826. case snd_soc_dapm_mic:
  827. case snd_soc_dapm_line:
  828. case snd_soc_dapm_spk:
  829. list_add(&path->list, &codec->dapm_paths);
  830. list_add(&path->list_sink, &wsink->sources);
  831. list_add(&path->list_source, &wsource->sinks);
  832. path->connect = 0;
  833. return 0;
  834. }
  835. return 0;
  836. err:
  837. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  838. control, sink);
  839. kfree(path);
  840. return ret;
  841. }
  842. EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
  843. /**
  844. * snd_soc_dapm_new_widgets - add new dapm widgets
  845. * @codec: audio codec
  846. *
  847. * Checks the codec for any new dapm widgets and creates them if found.
  848. *
  849. * Returns 0 for success.
  850. */
  851. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  852. {
  853. struct snd_soc_dapm_widget *w;
  854. list_for_each_entry(w, &codec->dapm_widgets, list)
  855. {
  856. if (w->new)
  857. continue;
  858. switch(w->id) {
  859. case snd_soc_dapm_switch:
  860. case snd_soc_dapm_mixer:
  861. dapm_new_mixer(codec, w);
  862. break;
  863. case snd_soc_dapm_mux:
  864. dapm_new_mux(codec, w);
  865. break;
  866. case snd_soc_dapm_adc:
  867. case snd_soc_dapm_dac:
  868. case snd_soc_dapm_pga:
  869. dapm_new_pga(codec, w);
  870. break;
  871. case snd_soc_dapm_input:
  872. case snd_soc_dapm_output:
  873. case snd_soc_dapm_micbias:
  874. case snd_soc_dapm_spk:
  875. case snd_soc_dapm_hp:
  876. case snd_soc_dapm_mic:
  877. case snd_soc_dapm_line:
  878. case snd_soc_dapm_vmid:
  879. case snd_soc_dapm_pre:
  880. case snd_soc_dapm_post:
  881. break;
  882. }
  883. w->new = 1;
  884. }
  885. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  886. return 0;
  887. }
  888. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  889. /**
  890. * snd_soc_dapm_get_volsw - dapm mixer get callback
  891. * @kcontrol: mixer control
  892. * @uinfo: control element information
  893. *
  894. * Callback to get the value of a dapm mixer control.
  895. *
  896. * Returns 0 for success.
  897. */
  898. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  899. struct snd_ctl_elem_value *ucontrol)
  900. {
  901. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  902. int reg = kcontrol->private_value & 0xff;
  903. int shift = (kcontrol->private_value >> 8) & 0x0f;
  904. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  905. int max = (kcontrol->private_value >> 16) & 0xff;
  906. int invert = (kcontrol->private_value >> 24) & 0x01;
  907. int mask = (1 << fls(max)) - 1;
  908. /* return the saved value if we are powered down */
  909. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  910. ucontrol->value.integer.value[0] = widget->saved_value;
  911. return 0;
  912. }
  913. ucontrol->value.integer.value[0] =
  914. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  915. if (shift != rshift)
  916. ucontrol->value.integer.value[1] =
  917. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  918. if (invert) {
  919. ucontrol->value.integer.value[0] =
  920. max - ucontrol->value.integer.value[0];
  921. if (shift != rshift)
  922. ucontrol->value.integer.value[1] =
  923. max - ucontrol->value.integer.value[1];
  924. }
  925. return 0;
  926. }
  927. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  928. /**
  929. * snd_soc_dapm_put_volsw - dapm mixer set callback
  930. * @kcontrol: mixer control
  931. * @uinfo: control element information
  932. *
  933. * Callback to set the value of a dapm mixer control.
  934. *
  935. * Returns 0 for success.
  936. */
  937. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  938. struct snd_ctl_elem_value *ucontrol)
  939. {
  940. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  941. int reg = kcontrol->private_value & 0xff;
  942. int shift = (kcontrol->private_value >> 8) & 0x0f;
  943. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  944. int max = (kcontrol->private_value >> 16) & 0xff;
  945. int mask = (1 << fls(max)) - 1;
  946. int invert = (kcontrol->private_value >> 24) & 0x01;
  947. unsigned short val, val2, val_mask;
  948. int ret;
  949. val = (ucontrol->value.integer.value[0] & mask);
  950. if (invert)
  951. val = max - val;
  952. val_mask = mask << shift;
  953. val = val << shift;
  954. if (shift != rshift) {
  955. val2 = (ucontrol->value.integer.value[1] & mask);
  956. if (invert)
  957. val2 = max - val2;
  958. val_mask |= mask << rshift;
  959. val |= val2 << rshift;
  960. }
  961. mutex_lock(&widget->codec->mutex);
  962. widget->value = val;
  963. /* save volume value if the widget is powered down */
  964. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  965. widget->saved_value = val;
  966. mutex_unlock(&widget->codec->mutex);
  967. return 1;
  968. }
  969. dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
  970. if (widget->event) {
  971. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  972. ret = widget->event(widget, kcontrol,
  973. SND_SOC_DAPM_PRE_REG);
  974. if (ret < 0) {
  975. ret = 1;
  976. goto out;
  977. }
  978. }
  979. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  980. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  981. ret = widget->event(widget, kcontrol,
  982. SND_SOC_DAPM_POST_REG);
  983. } else
  984. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  985. out:
  986. mutex_unlock(&widget->codec->mutex);
  987. return ret;
  988. }
  989. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  990. /**
  991. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  992. * @kcontrol: mixer control
  993. * @uinfo: control element information
  994. *
  995. * Callback to get the value of a dapm enumerated double mixer control.
  996. *
  997. * Returns 0 for success.
  998. */
  999. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1000. struct snd_ctl_elem_value *ucontrol)
  1001. {
  1002. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1003. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1004. unsigned short val, bitmask;
  1005. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1006. ;
  1007. val = snd_soc_read(widget->codec, e->reg);
  1008. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1009. if (e->shift_l != e->shift_r)
  1010. ucontrol->value.enumerated.item[1] =
  1011. (val >> e->shift_r) & (bitmask - 1);
  1012. return 0;
  1013. }
  1014. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1015. /**
  1016. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1017. * @kcontrol: mixer control
  1018. * @uinfo: control element information
  1019. *
  1020. * Callback to set the value of a dapm enumerated double mixer control.
  1021. *
  1022. * Returns 0 for success.
  1023. */
  1024. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1025. struct snd_ctl_elem_value *ucontrol)
  1026. {
  1027. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1028. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1029. unsigned short val, mux;
  1030. unsigned short mask, bitmask;
  1031. int ret = 0;
  1032. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1033. ;
  1034. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1035. return -EINVAL;
  1036. mux = ucontrol->value.enumerated.item[0];
  1037. val = mux << e->shift_l;
  1038. mask = (bitmask - 1) << e->shift_l;
  1039. if (e->shift_l != e->shift_r) {
  1040. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1041. return -EINVAL;
  1042. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1043. mask |= (bitmask - 1) << e->shift_r;
  1044. }
  1045. mutex_lock(&widget->codec->mutex);
  1046. widget->value = val;
  1047. dapm_mux_update_power(widget, kcontrol, mask, mux, e);
  1048. if (widget->event) {
  1049. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1050. ret = widget->event(widget,
  1051. kcontrol, SND_SOC_DAPM_PRE_REG);
  1052. if (ret < 0)
  1053. goto out;
  1054. }
  1055. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1056. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1057. ret = widget->event(widget,
  1058. kcontrol, SND_SOC_DAPM_POST_REG);
  1059. } else
  1060. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1061. out:
  1062. mutex_unlock(&widget->codec->mutex);
  1063. return ret;
  1064. }
  1065. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1066. /**
  1067. * snd_soc_dapm_new_control - create new dapm control
  1068. * @codec: audio codec
  1069. * @widget: widget template
  1070. *
  1071. * Creates a new dapm control based upon the template.
  1072. *
  1073. * Returns 0 for success else error.
  1074. */
  1075. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1076. const struct snd_soc_dapm_widget *widget)
  1077. {
  1078. struct snd_soc_dapm_widget *w;
  1079. if ((w = dapm_cnew_widget(widget)) == NULL)
  1080. return -ENOMEM;
  1081. w->codec = codec;
  1082. INIT_LIST_HEAD(&w->sources);
  1083. INIT_LIST_HEAD(&w->sinks);
  1084. INIT_LIST_HEAD(&w->list);
  1085. list_add(&w->list, &codec->dapm_widgets);
  1086. /* machine layer set ups unconnected pins and insertions */
  1087. w->connected = 1;
  1088. return 0;
  1089. }
  1090. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1091. /**
  1092. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1093. * @codec: audio codec
  1094. * @stream: stream name
  1095. * @event: stream event
  1096. *
  1097. * Sends a stream event to the dapm core. The core then makes any
  1098. * necessary widget power changes.
  1099. *
  1100. * Returns 0 for success else error.
  1101. */
  1102. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1103. char *stream, int event)
  1104. {
  1105. struct snd_soc_dapm_widget *w;
  1106. if (stream == NULL)
  1107. return 0;
  1108. mutex_lock(&codec->mutex);
  1109. list_for_each_entry(w, &codec->dapm_widgets, list)
  1110. {
  1111. if (!w->sname)
  1112. continue;
  1113. dbg("widget %s\n %s stream %s event %d\n", w->name, w->sname,
  1114. stream, event);
  1115. if (strstr(w->sname, stream)) {
  1116. switch(event) {
  1117. case SND_SOC_DAPM_STREAM_START:
  1118. w->active = 1;
  1119. break;
  1120. case SND_SOC_DAPM_STREAM_STOP:
  1121. w->active = 0;
  1122. break;
  1123. case SND_SOC_DAPM_STREAM_SUSPEND:
  1124. if (w->active)
  1125. w->suspend = 1;
  1126. w->active = 0;
  1127. break;
  1128. case SND_SOC_DAPM_STREAM_RESUME:
  1129. if (w->suspend) {
  1130. w->active = 1;
  1131. w->suspend = 0;
  1132. }
  1133. break;
  1134. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1135. break;
  1136. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1137. break;
  1138. }
  1139. }
  1140. }
  1141. mutex_unlock(&codec->mutex);
  1142. dapm_power_widgets(codec, event);
  1143. dump_dapm(codec, __func__);
  1144. return 0;
  1145. }
  1146. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1147. /**
  1148. * snd_soc_dapm_device_event - send a device event to the dapm core
  1149. * @socdev: audio device
  1150. * @event: device event
  1151. *
  1152. * Sends a device event to the dapm core. The core then makes any
  1153. * necessary machine or codec power changes..
  1154. *
  1155. * Returns 0 for success else error.
  1156. */
  1157. int snd_soc_dapm_device_event(struct snd_soc_device *socdev, int event)
  1158. {
  1159. struct snd_soc_codec *codec = socdev->codec;
  1160. struct snd_soc_machine *machine = socdev->machine;
  1161. if (machine->dapm_event)
  1162. machine->dapm_event(machine, event);
  1163. if (codec->dapm_event)
  1164. codec->dapm_event(codec, event);
  1165. return 0;
  1166. }
  1167. EXPORT_SYMBOL_GPL(snd_soc_dapm_device_event);
  1168. /**
  1169. * snd_soc_dapm_set_endpoint - set audio endpoint status
  1170. * @codec: audio codec
  1171. * @endpoint: audio signal endpoint (or start point)
  1172. * @status: point status
  1173. *
  1174. * Set audio endpoint status - connected or disconnected.
  1175. *
  1176. * Returns 0 for success else error.
  1177. */
  1178. int snd_soc_dapm_set_endpoint(struct snd_soc_codec *codec,
  1179. char *endpoint, int status)
  1180. {
  1181. struct snd_soc_dapm_widget *w;
  1182. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1183. if (!strcmp(w->name, endpoint)) {
  1184. w->connected = status;
  1185. return 0;
  1186. }
  1187. }
  1188. return -ENODEV;
  1189. }
  1190. EXPORT_SYMBOL_GPL(snd_soc_dapm_set_endpoint);
  1191. /**
  1192. * snd_soc_dapm_free - free dapm resources
  1193. * @socdev: SoC device
  1194. *
  1195. * Free all dapm widgets and resources.
  1196. */
  1197. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1198. {
  1199. struct snd_soc_codec *codec = socdev->codec;
  1200. snd_soc_dapm_sys_remove(socdev->dev);
  1201. dapm_free_widgets(codec);
  1202. }
  1203. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1204. /* Module information */
  1205. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1206. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1207. MODULE_LICENSE("GPL");