soc-dapm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 old %x new %x change %d\n", 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, SND_SOC_DAPM_PRE_PMU);
  457. if (ret < 0)
  458. return ret;
  459. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  460. ret = w->event(w, SND_SOC_DAPM_PRE_PMD);
  461. if (ret < 0)
  462. return ret;
  463. }
  464. continue;
  465. }
  466. if (w->id == snd_soc_dapm_post) {
  467. if (!w->event)
  468. continue;
  469. if (event == SND_SOC_DAPM_STREAM_START) {
  470. ret = w->event(w, SND_SOC_DAPM_POST_PMU);
  471. if (ret < 0)
  472. return ret;
  473. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  474. ret = w->event(w, SND_SOC_DAPM_POST_PMD);
  475. if (ret < 0)
  476. return ret;
  477. }
  478. continue;
  479. }
  480. /* all other widgets */
  481. in = is_connected_input_ep(w);
  482. dapm_clear_walk(w->codec);
  483. out = is_connected_output_ep(w);
  484. dapm_clear_walk(w->codec);
  485. power = (out != 0 && in != 0) ? 1 : 0;
  486. power_change = (w->power == power) ? 0: 1;
  487. w->power = power;
  488. /* call any power change event handlers */
  489. if (power_change) {
  490. if (w->event) {
  491. dbg("power %s event for %s flags %x\n",
  492. w->power ? "on" : "off", w->name, w->event_flags);
  493. if (power) {
  494. /* power up event */
  495. if (w->event_flags & SND_SOC_DAPM_PRE_PMU) {
  496. ret = w->event(w, SND_SOC_DAPM_PRE_PMU);
  497. if (ret < 0)
  498. return ret;
  499. }
  500. dapm_update_bits(w);
  501. if (w->event_flags & SND_SOC_DAPM_POST_PMU){
  502. ret = w->event(w, SND_SOC_DAPM_POST_PMU);
  503. if (ret < 0)
  504. return ret;
  505. }
  506. } else {
  507. /* power down event */
  508. if (w->event_flags & SND_SOC_DAPM_PRE_PMD) {
  509. ret = w->event(w, SND_SOC_DAPM_PRE_PMD);
  510. if (ret < 0)
  511. return ret;
  512. }
  513. dapm_update_bits(w);
  514. if (w->event_flags & SND_SOC_DAPM_POST_PMD) {
  515. ret = w->event(w, SND_SOC_DAPM_POST_PMD);
  516. if (ret < 0)
  517. return ret;
  518. }
  519. }
  520. } else
  521. /* no event handler */
  522. dapm_update_bits(w);
  523. }
  524. }
  525. }
  526. return ret;
  527. }
  528. #if DAPM_DEBUG
  529. static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
  530. {
  531. struct snd_soc_dapm_widget *w;
  532. struct snd_soc_dapm_path *p = NULL;
  533. int in, out;
  534. printk("DAPM %s %s\n", codec->name, action);
  535. list_for_each_entry(w, &codec->dapm_widgets, list) {
  536. /* only display widgets that effect routing */
  537. switch (w->id) {
  538. case snd_soc_dapm_pre:
  539. case snd_soc_dapm_post:
  540. case snd_soc_dapm_vmid:
  541. continue;
  542. case snd_soc_dapm_mux:
  543. case snd_soc_dapm_output:
  544. case snd_soc_dapm_input:
  545. case snd_soc_dapm_switch:
  546. case snd_soc_dapm_hp:
  547. case snd_soc_dapm_mic:
  548. case snd_soc_dapm_spk:
  549. case snd_soc_dapm_line:
  550. case snd_soc_dapm_micbias:
  551. case snd_soc_dapm_dac:
  552. case snd_soc_dapm_adc:
  553. case snd_soc_dapm_pga:
  554. case snd_soc_dapm_mixer:
  555. if (w->name) {
  556. in = is_connected_input_ep(w);
  557. dapm_clear_walk(w->codec);
  558. out = is_connected_output_ep(w);
  559. dapm_clear_walk(w->codec);
  560. printk("%s: %s in %d out %d\n", w->name,
  561. w->power ? "On":"Off",in, out);
  562. list_for_each_entry(p, &w->sources, list_sink) {
  563. if (p->connect)
  564. printk(" in %s %s\n", p->name ? p->name : "static",
  565. p->source->name);
  566. }
  567. list_for_each_entry(p, &w->sinks, list_source) {
  568. if (p->connect)
  569. printk(" out %s %s\n", p->name ? p->name : "static",
  570. p->sink->name);
  571. }
  572. }
  573. break;
  574. }
  575. }
  576. }
  577. #endif
  578. /* test and update the power status of a mux widget */
  579. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  580. struct snd_kcontrol *kcontrol, int mask,
  581. int val, struct soc_enum* e)
  582. {
  583. struct snd_soc_dapm_path *path;
  584. int found = 0;
  585. if (widget->id != snd_soc_dapm_mux)
  586. return -ENODEV;
  587. if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
  588. return 0;
  589. /* find dapm widget path assoc with kcontrol */
  590. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  591. if (path->kcontrol != kcontrol)
  592. continue;
  593. if (!path->name || ! e->texts[val])
  594. continue;
  595. found = 1;
  596. /* we now need to match the string in the enum to the path */
  597. if (!(strcmp(path->name, e->texts[val])))
  598. path->connect = 1; /* new connection */
  599. else
  600. path->connect = 0; /* old connection must be powered down */
  601. }
  602. if (found)
  603. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  604. return 0;
  605. }
  606. /* test and update the power status of a mixer or switch widget */
  607. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  608. struct snd_kcontrol *kcontrol, int reg,
  609. int val_mask, int val, int invert)
  610. {
  611. struct snd_soc_dapm_path *path;
  612. int found = 0;
  613. if (widget->id != snd_soc_dapm_mixer &&
  614. widget->id != snd_soc_dapm_switch)
  615. return -ENODEV;
  616. if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
  617. return 0;
  618. /* find dapm widget path assoc with kcontrol */
  619. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  620. if (path->kcontrol != kcontrol)
  621. continue;
  622. /* found, now check type */
  623. found = 1;
  624. if (val)
  625. /* new connection */
  626. path->connect = invert ? 0:1;
  627. else
  628. /* old connection must be powered down */
  629. path->connect = invert ? 1:0;
  630. break;
  631. }
  632. if (found)
  633. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  634. return 0;
  635. }
  636. /* show dapm widget status in sys fs */
  637. static ssize_t dapm_widget_show(struct device *dev,
  638. struct device_attribute *attr, char *buf)
  639. {
  640. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  641. struct snd_soc_codec *codec = devdata->codec;
  642. struct snd_soc_dapm_widget *w;
  643. int count = 0;
  644. char *state = "not set";
  645. list_for_each_entry(w, &codec->dapm_widgets, list) {
  646. /* only display widgets that burnm power */
  647. switch (w->id) {
  648. case snd_soc_dapm_hp:
  649. case snd_soc_dapm_mic:
  650. case snd_soc_dapm_spk:
  651. case snd_soc_dapm_line:
  652. case snd_soc_dapm_micbias:
  653. case snd_soc_dapm_dac:
  654. case snd_soc_dapm_adc:
  655. case snd_soc_dapm_pga:
  656. case snd_soc_dapm_mixer:
  657. if (w->name)
  658. count += sprintf(buf + count, "%s: %s\n",
  659. w->name, w->power ? "On":"Off");
  660. break;
  661. default:
  662. break;
  663. }
  664. }
  665. switch(codec->dapm_state){
  666. case SNDRV_CTL_POWER_D0:
  667. state = "D0";
  668. break;
  669. case SNDRV_CTL_POWER_D1:
  670. state = "D1";
  671. break;
  672. case SNDRV_CTL_POWER_D2:
  673. state = "D2";
  674. break;
  675. case SNDRV_CTL_POWER_D3hot:
  676. state = "D3hot";
  677. break;
  678. case SNDRV_CTL_POWER_D3cold:
  679. state = "D3cold";
  680. break;
  681. }
  682. count += sprintf(buf + count, "PM State: %s\n", state);
  683. return count;
  684. }
  685. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  686. int snd_soc_dapm_sys_add(struct device *dev)
  687. {
  688. int ret = 0;
  689. if (dapm_status)
  690. ret = device_create_file(dev, &dev_attr_dapm_widget);
  691. return ret;
  692. }
  693. static void snd_soc_dapm_sys_remove(struct device *dev)
  694. {
  695. if (dapm_status)
  696. device_remove_file(dev, &dev_attr_dapm_widget);
  697. }
  698. /* free all dapm widgets and resources */
  699. static void dapm_free_widgets(struct snd_soc_codec *codec)
  700. {
  701. struct snd_soc_dapm_widget *w, *next_w;
  702. struct snd_soc_dapm_path *p, *next_p;
  703. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  704. list_del(&w->list);
  705. kfree(w);
  706. }
  707. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  708. list_del(&p->list);
  709. kfree(p->long_name);
  710. kfree(p);
  711. }
  712. }
  713. /**
  714. * snd_soc_dapm_sync_endpoints - scan and power dapm paths
  715. * @codec: audio codec
  716. *
  717. * Walks all dapm audio paths and powers widgets according to their
  718. * stream or path usage.
  719. *
  720. * Returns 0 for success.
  721. */
  722. int snd_soc_dapm_sync_endpoints(struct snd_soc_codec *codec)
  723. {
  724. return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  725. }
  726. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_endpoints);
  727. /**
  728. * snd_soc_dapm_connect_input - connect dapm widgets
  729. * @codec: audio codec
  730. * @sink: name of target widget
  731. * @control: mixer control name
  732. * @source: name of source name
  733. *
  734. * Connects 2 dapm widgets together via a named audio path. The sink is
  735. * the widget receiving the audio signal, whilst the source is the sender
  736. * of the audio signal.
  737. *
  738. * Returns 0 for success else error.
  739. */
  740. int snd_soc_dapm_connect_input(struct snd_soc_codec *codec, const char *sink,
  741. const char * control, const char *source)
  742. {
  743. struct snd_soc_dapm_path *path;
  744. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  745. int ret = 0;
  746. /* find src and dest widgets */
  747. list_for_each_entry(w, &codec->dapm_widgets, list) {
  748. if (!wsink && !(strcmp(w->name, sink))) {
  749. wsink = w;
  750. continue;
  751. }
  752. if (!wsource && !(strcmp(w->name, source))) {
  753. wsource = w;
  754. }
  755. }
  756. if (wsource == NULL || wsink == NULL)
  757. return -ENODEV;
  758. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  759. if (!path)
  760. return -ENOMEM;
  761. path->source = wsource;
  762. path->sink = wsink;
  763. INIT_LIST_HEAD(&path->list);
  764. INIT_LIST_HEAD(&path->list_source);
  765. INIT_LIST_HEAD(&path->list_sink);
  766. /* check for external widgets */
  767. if (wsink->id == snd_soc_dapm_input) {
  768. if (wsource->id == snd_soc_dapm_micbias ||
  769. wsource->id == snd_soc_dapm_mic ||
  770. wsink->id == snd_soc_dapm_line ||
  771. wsink->id == snd_soc_dapm_output)
  772. wsink->ext = 1;
  773. }
  774. if (wsource->id == snd_soc_dapm_output) {
  775. if (wsink->id == snd_soc_dapm_spk ||
  776. wsink->id == snd_soc_dapm_hp ||
  777. wsink->id == snd_soc_dapm_line ||
  778. wsink->id == snd_soc_dapm_input)
  779. wsource->ext = 1;
  780. }
  781. /* connect static paths */
  782. if (control == NULL) {
  783. list_add(&path->list, &codec->dapm_paths);
  784. list_add(&path->list_sink, &wsink->sources);
  785. list_add(&path->list_source, &wsource->sinks);
  786. path->connect = 1;
  787. return 0;
  788. }
  789. /* connect dynamic paths */
  790. switch(wsink->id) {
  791. case snd_soc_dapm_adc:
  792. case snd_soc_dapm_dac:
  793. case snd_soc_dapm_pga:
  794. case snd_soc_dapm_input:
  795. case snd_soc_dapm_output:
  796. case snd_soc_dapm_micbias:
  797. case snd_soc_dapm_vmid:
  798. case snd_soc_dapm_pre:
  799. case snd_soc_dapm_post:
  800. list_add(&path->list, &codec->dapm_paths);
  801. list_add(&path->list_sink, &wsink->sources);
  802. list_add(&path->list_source, &wsource->sinks);
  803. path->connect = 1;
  804. return 0;
  805. case snd_soc_dapm_mux:
  806. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  807. &wsink->kcontrols[0]);
  808. if (ret != 0)
  809. goto err;
  810. break;
  811. case snd_soc_dapm_switch:
  812. case snd_soc_dapm_mixer:
  813. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  814. if (ret != 0)
  815. goto err;
  816. break;
  817. case snd_soc_dapm_hp:
  818. case snd_soc_dapm_mic:
  819. case snd_soc_dapm_line:
  820. case snd_soc_dapm_spk:
  821. list_add(&path->list, &codec->dapm_paths);
  822. list_add(&path->list_sink, &wsink->sources);
  823. list_add(&path->list_source, &wsource->sinks);
  824. path->connect = 0;
  825. return 0;
  826. }
  827. return 0;
  828. err:
  829. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  830. control, sink);
  831. kfree(path);
  832. return ret;
  833. }
  834. EXPORT_SYMBOL_GPL(snd_soc_dapm_connect_input);
  835. /**
  836. * snd_soc_dapm_new_widgets - add new dapm widgets
  837. * @codec: audio codec
  838. *
  839. * Checks the codec for any new dapm widgets and creates them if found.
  840. *
  841. * Returns 0 for success.
  842. */
  843. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  844. {
  845. struct snd_soc_dapm_widget *w;
  846. mutex_lock(&codec->mutex);
  847. list_for_each_entry(w, &codec->dapm_widgets, list)
  848. {
  849. if (w->new)
  850. continue;
  851. switch(w->id) {
  852. case snd_soc_dapm_switch:
  853. case snd_soc_dapm_mixer:
  854. dapm_new_mixer(codec, w);
  855. break;
  856. case snd_soc_dapm_mux:
  857. dapm_new_mux(codec, w);
  858. break;
  859. case snd_soc_dapm_adc:
  860. case snd_soc_dapm_dac:
  861. case snd_soc_dapm_pga:
  862. dapm_new_pga(codec, w);
  863. break;
  864. case snd_soc_dapm_input:
  865. case snd_soc_dapm_output:
  866. case snd_soc_dapm_micbias:
  867. case snd_soc_dapm_spk:
  868. case snd_soc_dapm_hp:
  869. case snd_soc_dapm_mic:
  870. case snd_soc_dapm_line:
  871. case snd_soc_dapm_vmid:
  872. case snd_soc_dapm_pre:
  873. case snd_soc_dapm_post:
  874. break;
  875. }
  876. w->new = 1;
  877. }
  878. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  879. mutex_unlock(&codec->mutex);
  880. return 0;
  881. }
  882. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  883. /**
  884. * snd_soc_dapm_get_volsw - dapm mixer get callback
  885. * @kcontrol: mixer control
  886. * @uinfo: control element information
  887. *
  888. * Callback to get the value of a dapm mixer control.
  889. *
  890. * Returns 0 for success.
  891. */
  892. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  893. struct snd_ctl_elem_value *ucontrol)
  894. {
  895. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  896. int reg = kcontrol->private_value & 0xff;
  897. int shift = (kcontrol->private_value >> 8) & 0x0f;
  898. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  899. int max = (kcontrol->private_value >> 16) & 0xff;
  900. int invert = (kcontrol->private_value >> 24) & 0x01;
  901. int mask = (1 << fls(max)) - 1;
  902. /* return the saved value if we are powered down */
  903. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  904. ucontrol->value.integer.value[0] = widget->saved_value;
  905. return 0;
  906. }
  907. ucontrol->value.integer.value[0] =
  908. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  909. if (shift != rshift)
  910. ucontrol->value.integer.value[1] =
  911. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  912. if (invert) {
  913. ucontrol->value.integer.value[0] =
  914. max - ucontrol->value.integer.value[0];
  915. if (shift != rshift)
  916. ucontrol->value.integer.value[1] =
  917. max - ucontrol->value.integer.value[1];
  918. }
  919. return 0;
  920. }
  921. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  922. /**
  923. * snd_soc_dapm_put_volsw - dapm mixer set callback
  924. * @kcontrol: mixer control
  925. * @uinfo: control element information
  926. *
  927. * Callback to set the value of a dapm mixer control.
  928. *
  929. * Returns 0 for success.
  930. */
  931. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  932. struct snd_ctl_elem_value *ucontrol)
  933. {
  934. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  935. int reg = kcontrol->private_value & 0xff;
  936. int shift = (kcontrol->private_value >> 8) & 0x0f;
  937. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  938. int max = (kcontrol->private_value >> 16) & 0xff;
  939. int mask = (1 << fls(max)) - 1;
  940. int invert = (kcontrol->private_value >> 24) & 0x01;
  941. unsigned short val, val2, val_mask;
  942. int ret;
  943. val = (ucontrol->value.integer.value[0] & mask);
  944. if (invert)
  945. val = max - val;
  946. val_mask = mask << shift;
  947. val = val << shift;
  948. if (shift != rshift) {
  949. val2 = (ucontrol->value.integer.value[1] & mask);
  950. if (invert)
  951. val2 = max - val2;
  952. val_mask |= mask << rshift;
  953. val |= val2 << rshift;
  954. }
  955. mutex_lock(&widget->codec->mutex);
  956. widget->value = val;
  957. /* save volume value if the widget is powered down */
  958. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  959. widget->saved_value = val;
  960. mutex_unlock(&widget->codec->mutex);
  961. return 1;
  962. }
  963. dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
  964. if (widget->event) {
  965. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  966. ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
  967. if (ret < 0)
  968. goto out;
  969. }
  970. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  971. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  972. ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
  973. } else
  974. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  975. out:
  976. mutex_unlock(&widget->codec->mutex);
  977. return ret;
  978. }
  979. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  980. /**
  981. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  982. * @kcontrol: mixer control
  983. * @uinfo: control element information
  984. *
  985. * Callback to get the value of a dapm enumerated double mixer control.
  986. *
  987. * Returns 0 for success.
  988. */
  989. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  990. struct snd_ctl_elem_value *ucontrol)
  991. {
  992. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  993. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  994. unsigned short val, bitmask;
  995. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  996. ;
  997. val = snd_soc_read(widget->codec, e->reg);
  998. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  999. if (e->shift_l != e->shift_r)
  1000. ucontrol->value.enumerated.item[1] =
  1001. (val >> e->shift_r) & (bitmask - 1);
  1002. return 0;
  1003. }
  1004. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1005. /**
  1006. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1007. * @kcontrol: mixer control
  1008. * @uinfo: control element information
  1009. *
  1010. * Callback to set the value of a dapm enumerated double mixer control.
  1011. *
  1012. * Returns 0 for success.
  1013. */
  1014. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1015. struct snd_ctl_elem_value *ucontrol)
  1016. {
  1017. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1018. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1019. unsigned short val, mux;
  1020. unsigned short mask, bitmask;
  1021. int ret = 0;
  1022. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1023. ;
  1024. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1025. return -EINVAL;
  1026. mux = ucontrol->value.enumerated.item[0];
  1027. val = mux << e->shift_l;
  1028. mask = (bitmask - 1) << e->shift_l;
  1029. if (e->shift_l != e->shift_r) {
  1030. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1031. return -EINVAL;
  1032. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1033. mask |= (bitmask - 1) << e->shift_r;
  1034. }
  1035. mutex_lock(&widget->codec->mutex);
  1036. widget->value = val;
  1037. dapm_mux_update_power(widget, kcontrol, mask, mux, e);
  1038. if (widget->event) {
  1039. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1040. ret = widget->event(widget, SND_SOC_DAPM_PRE_REG);
  1041. if (ret < 0)
  1042. goto out;
  1043. }
  1044. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1045. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1046. ret = widget->event(widget, SND_SOC_DAPM_POST_REG);
  1047. } else
  1048. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1049. out:
  1050. mutex_unlock(&widget->codec->mutex);
  1051. return ret;
  1052. }
  1053. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1054. /**
  1055. * snd_soc_dapm_new_control - create new dapm control
  1056. * @codec: audio codec
  1057. * @widget: widget template
  1058. *
  1059. * Creates a new dapm control based upon the template.
  1060. *
  1061. * Returns 0 for success else error.
  1062. */
  1063. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1064. const struct snd_soc_dapm_widget *widget)
  1065. {
  1066. struct snd_soc_dapm_widget *w;
  1067. if ((w = dapm_cnew_widget(widget)) == NULL)
  1068. return -ENOMEM;
  1069. w->codec = codec;
  1070. INIT_LIST_HEAD(&w->sources);
  1071. INIT_LIST_HEAD(&w->sinks);
  1072. INIT_LIST_HEAD(&w->list);
  1073. list_add(&w->list, &codec->dapm_widgets);
  1074. /* machine layer set ups unconnected pins and insertions */
  1075. w->connected = 1;
  1076. return 0;
  1077. }
  1078. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1079. /**
  1080. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1081. * @codec: audio codec
  1082. * @stream: stream name
  1083. * @event: stream event
  1084. *
  1085. * Sends a stream event to the dapm core. The core then makes any
  1086. * necessary widget power changes.
  1087. *
  1088. * Returns 0 for success else error.
  1089. */
  1090. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1091. char *stream, int event)
  1092. {
  1093. struct snd_soc_dapm_widget *w;
  1094. if (stream == NULL)
  1095. return 0;
  1096. mutex_lock(&codec->mutex);
  1097. list_for_each_entry(w, &codec->dapm_widgets, list)
  1098. {
  1099. if (!w->sname)
  1100. continue;
  1101. dbg("widget %s\n %s stream %s event %d\n", w->name, w->sname,
  1102. stream, event);
  1103. if (strstr(w->sname, stream)) {
  1104. switch(event) {
  1105. case SND_SOC_DAPM_STREAM_START:
  1106. w->active = 1;
  1107. break;
  1108. case SND_SOC_DAPM_STREAM_STOP:
  1109. w->active = 0;
  1110. break;
  1111. case SND_SOC_DAPM_STREAM_SUSPEND:
  1112. if (w->active)
  1113. w->suspend = 1;
  1114. w->active = 0;
  1115. break;
  1116. case SND_SOC_DAPM_STREAM_RESUME:
  1117. if (w->suspend) {
  1118. w->active = 1;
  1119. w->suspend = 0;
  1120. }
  1121. break;
  1122. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1123. break;
  1124. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. mutex_unlock(&codec->mutex);
  1130. dapm_power_widgets(codec, event);
  1131. dump_dapm(codec, __FUNCTION__);
  1132. return 0;
  1133. }
  1134. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1135. /**
  1136. * snd_soc_dapm_device_event - send a device event to the dapm core
  1137. * @socdev: audio device
  1138. * @event: device event
  1139. *
  1140. * Sends a device event to the dapm core. The core then makes any
  1141. * necessary machine or codec power changes..
  1142. *
  1143. * Returns 0 for success else error.
  1144. */
  1145. int snd_soc_dapm_device_event(struct snd_soc_device *socdev, int event)
  1146. {
  1147. struct snd_soc_codec *codec = socdev->codec;
  1148. struct snd_soc_machine *machine = socdev->machine;
  1149. if (machine->dapm_event)
  1150. machine->dapm_event(machine, event);
  1151. if (codec->dapm_event)
  1152. codec->dapm_event(codec, event);
  1153. return 0;
  1154. }
  1155. EXPORT_SYMBOL_GPL(snd_soc_dapm_device_event);
  1156. /**
  1157. * snd_soc_dapm_set_endpoint - set audio endpoint status
  1158. * @codec: audio codec
  1159. * @endpoint: audio signal endpoint (or start point)
  1160. * @status: point status
  1161. *
  1162. * Set audio endpoint status - connected or disconnected.
  1163. *
  1164. * Returns 0 for success else error.
  1165. */
  1166. int snd_soc_dapm_set_endpoint(struct snd_soc_codec *codec,
  1167. char *endpoint, int status)
  1168. {
  1169. struct snd_soc_dapm_widget *w;
  1170. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1171. if (!strcmp(w->name, endpoint)) {
  1172. w->connected = status;
  1173. }
  1174. }
  1175. return 0;
  1176. }
  1177. EXPORT_SYMBOL_GPL(snd_soc_dapm_set_endpoint);
  1178. /**
  1179. * snd_soc_dapm_free - free dapm resources
  1180. * @socdev: SoC device
  1181. *
  1182. * Free all dapm widgets and resources.
  1183. */
  1184. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1185. {
  1186. struct snd_soc_codec *codec = socdev->codec;
  1187. snd_soc_dapm_sys_remove(socdev->dev);
  1188. dapm_free_widgets(codec);
  1189. }
  1190. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1191. /* Module information */
  1192. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1193. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1194. MODULE_LICENSE("GPL");