soc-dapm.c 38 KB

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