soc-dapm.c 34 KB

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