soc-dapm.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. if (!dapm_status)
  711. return 0;
  712. return device_create_file(dev, &dev_attr_dapm_widget);
  713. }
  714. static void snd_soc_dapm_sys_remove(struct device *dev)
  715. {
  716. if (dapm_status) {
  717. device_remove_file(dev, &dev_attr_dapm_widget);
  718. }
  719. }
  720. /* free all dapm widgets and resources */
  721. static void dapm_free_widgets(struct snd_soc_codec *codec)
  722. {
  723. struct snd_soc_dapm_widget *w, *next_w;
  724. struct snd_soc_dapm_path *p, *next_p;
  725. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  726. list_del(&w->list);
  727. kfree(w);
  728. }
  729. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  730. list_del(&p->list);
  731. kfree(p->long_name);
  732. kfree(p);
  733. }
  734. }
  735. static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  736. char *pin, int status)
  737. {
  738. struct snd_soc_dapm_widget *w;
  739. list_for_each_entry(w, &codec->dapm_widgets, list) {
  740. if (!strcmp(w->name, pin)) {
  741. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  742. w->connected = status;
  743. return 0;
  744. }
  745. }
  746. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  747. return -EINVAL;
  748. }
  749. /**
  750. * snd_soc_dapm_sync - scan and power dapm paths
  751. * @codec: audio codec
  752. *
  753. * Walks all dapm audio paths and powers widgets according to their
  754. * stream or path usage.
  755. *
  756. * Returns 0 for success.
  757. */
  758. int snd_soc_dapm_sync(struct snd_soc_codec *codec)
  759. {
  760. int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  761. dump_dapm(codec, "sync");
  762. return ret;
  763. }
  764. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  765. static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
  766. const char *sink, const char *control, const char *source)
  767. {
  768. struct snd_soc_dapm_path *path;
  769. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  770. int ret = 0;
  771. /* find src and dest widgets */
  772. list_for_each_entry(w, &codec->dapm_widgets, list) {
  773. if (!wsink && !(strcmp(w->name, sink))) {
  774. wsink = w;
  775. continue;
  776. }
  777. if (!wsource && !(strcmp(w->name, source))) {
  778. wsource = w;
  779. }
  780. }
  781. if (wsource == NULL || wsink == NULL)
  782. return -ENODEV;
  783. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  784. if (!path)
  785. return -ENOMEM;
  786. path->source = wsource;
  787. path->sink = wsink;
  788. INIT_LIST_HEAD(&path->list);
  789. INIT_LIST_HEAD(&path->list_source);
  790. INIT_LIST_HEAD(&path->list_sink);
  791. /* check for external widgets */
  792. if (wsink->id == snd_soc_dapm_input) {
  793. if (wsource->id == snd_soc_dapm_micbias ||
  794. wsource->id == snd_soc_dapm_mic ||
  795. wsink->id == snd_soc_dapm_line ||
  796. wsink->id == snd_soc_dapm_output)
  797. wsink->ext = 1;
  798. }
  799. if (wsource->id == snd_soc_dapm_output) {
  800. if (wsink->id == snd_soc_dapm_spk ||
  801. wsink->id == snd_soc_dapm_hp ||
  802. wsink->id == snd_soc_dapm_line ||
  803. wsink->id == snd_soc_dapm_input)
  804. wsource->ext = 1;
  805. }
  806. /* connect static paths */
  807. if (control == NULL) {
  808. list_add(&path->list, &codec->dapm_paths);
  809. list_add(&path->list_sink, &wsink->sources);
  810. list_add(&path->list_source, &wsource->sinks);
  811. path->connect = 1;
  812. return 0;
  813. }
  814. /* connect dynamic paths */
  815. switch(wsink->id) {
  816. case snd_soc_dapm_adc:
  817. case snd_soc_dapm_dac:
  818. case snd_soc_dapm_pga:
  819. case snd_soc_dapm_input:
  820. case snd_soc_dapm_output:
  821. case snd_soc_dapm_micbias:
  822. case snd_soc_dapm_vmid:
  823. case snd_soc_dapm_pre:
  824. case snd_soc_dapm_post:
  825. list_add(&path->list, &codec->dapm_paths);
  826. list_add(&path->list_sink, &wsink->sources);
  827. list_add(&path->list_source, &wsource->sinks);
  828. path->connect = 1;
  829. return 0;
  830. case snd_soc_dapm_mux:
  831. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  832. &wsink->kcontrols[0]);
  833. if (ret != 0)
  834. goto err;
  835. break;
  836. case snd_soc_dapm_switch:
  837. case snd_soc_dapm_mixer:
  838. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  839. if (ret != 0)
  840. goto err;
  841. break;
  842. case snd_soc_dapm_hp:
  843. case snd_soc_dapm_mic:
  844. case snd_soc_dapm_line:
  845. case snd_soc_dapm_spk:
  846. list_add(&path->list, &codec->dapm_paths);
  847. list_add(&path->list_sink, &wsink->sources);
  848. list_add(&path->list_source, &wsource->sinks);
  849. path->connect = 0;
  850. return 0;
  851. }
  852. return 0;
  853. err:
  854. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  855. control, sink);
  856. kfree(path);
  857. return ret;
  858. }
  859. /**
  860. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  861. * @codec: codec
  862. * @route: audio routes
  863. * @num: number of routes
  864. *
  865. * Connects 2 dapm widgets together via a named audio path. The sink is
  866. * the widget receiving the audio signal, whilst the source is the sender
  867. * of the audio signal.
  868. *
  869. * Returns 0 for success else error. On error all resources can be freed
  870. * with a call to snd_soc_card_free().
  871. */
  872. int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
  873. const struct snd_soc_dapm_route *route, int num)
  874. {
  875. int i, ret;
  876. for (i = 0; i < num; i++) {
  877. ret = snd_soc_dapm_add_route(codec, route->sink,
  878. route->control, route->source);
  879. if (ret < 0) {
  880. printk(KERN_ERR "Failed to add route %s->%s\n",
  881. route->source,
  882. route->sink);
  883. return ret;
  884. }
  885. route++;
  886. }
  887. return 0;
  888. }
  889. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  890. /**
  891. * snd_soc_dapm_new_widgets - add new dapm widgets
  892. * @codec: audio codec
  893. *
  894. * Checks the codec for any new dapm widgets and creates them if found.
  895. *
  896. * Returns 0 for success.
  897. */
  898. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  899. {
  900. struct snd_soc_dapm_widget *w;
  901. list_for_each_entry(w, &codec->dapm_widgets, list)
  902. {
  903. if (w->new)
  904. continue;
  905. switch(w->id) {
  906. case snd_soc_dapm_switch:
  907. case snd_soc_dapm_mixer:
  908. dapm_new_mixer(codec, w);
  909. break;
  910. case snd_soc_dapm_mux:
  911. dapm_new_mux(codec, w);
  912. break;
  913. case snd_soc_dapm_adc:
  914. case snd_soc_dapm_dac:
  915. case snd_soc_dapm_pga:
  916. dapm_new_pga(codec, w);
  917. break;
  918. case snd_soc_dapm_input:
  919. case snd_soc_dapm_output:
  920. case snd_soc_dapm_micbias:
  921. case snd_soc_dapm_spk:
  922. case snd_soc_dapm_hp:
  923. case snd_soc_dapm_mic:
  924. case snd_soc_dapm_line:
  925. case snd_soc_dapm_vmid:
  926. case snd_soc_dapm_pre:
  927. case snd_soc_dapm_post:
  928. break;
  929. }
  930. w->new = 1;
  931. }
  932. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  933. return 0;
  934. }
  935. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  936. /**
  937. * snd_soc_dapm_get_volsw - dapm mixer get callback
  938. * @kcontrol: mixer control
  939. * @uinfo: control element information
  940. *
  941. * Callback to get the value of a dapm mixer control.
  942. *
  943. * Returns 0 for success.
  944. */
  945. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  946. struct snd_ctl_elem_value *ucontrol)
  947. {
  948. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  949. struct soc_mixer_control *mc =
  950. (struct soc_mixer_control *)kcontrol->private_value;
  951. unsigned int reg = mc->reg;
  952. unsigned int shift = mc->shift;
  953. unsigned int rshift = mc->rshift;
  954. int max = mc->max;
  955. unsigned int invert = mc->invert;
  956. unsigned int mask = (1 << fls(max)) - 1;
  957. /* return the saved value if we are powered down */
  958. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  959. ucontrol->value.integer.value[0] = widget->saved_value;
  960. return 0;
  961. }
  962. ucontrol->value.integer.value[0] =
  963. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  964. if (shift != rshift)
  965. ucontrol->value.integer.value[1] =
  966. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  967. if (invert) {
  968. ucontrol->value.integer.value[0] =
  969. max - ucontrol->value.integer.value[0];
  970. if (shift != rshift)
  971. ucontrol->value.integer.value[1] =
  972. max - ucontrol->value.integer.value[1];
  973. }
  974. return 0;
  975. }
  976. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  977. /**
  978. * snd_soc_dapm_put_volsw - dapm mixer set callback
  979. * @kcontrol: mixer control
  980. * @uinfo: control element information
  981. *
  982. * Callback to set the value of a dapm mixer control.
  983. *
  984. * Returns 0 for success.
  985. */
  986. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  987. struct snd_ctl_elem_value *ucontrol)
  988. {
  989. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  990. struct soc_mixer_control *mc =
  991. (struct soc_mixer_control *)kcontrol->private_value;
  992. unsigned int reg = mc->reg;
  993. unsigned int shift = mc->shift;
  994. unsigned int rshift = mc->rshift;
  995. int max = mc->max;
  996. unsigned int mask = (1 << fls(max)) - 1;
  997. unsigned int invert = mc->invert;
  998. unsigned short val, val2, val_mask;
  999. int ret;
  1000. val = (ucontrol->value.integer.value[0] & mask);
  1001. if (invert)
  1002. val = max - val;
  1003. val_mask = mask << shift;
  1004. val = val << shift;
  1005. if (shift != rshift) {
  1006. val2 = (ucontrol->value.integer.value[1] & mask);
  1007. if (invert)
  1008. val2 = max - val2;
  1009. val_mask |= mask << rshift;
  1010. val |= val2 << rshift;
  1011. }
  1012. mutex_lock(&widget->codec->mutex);
  1013. widget->value = val;
  1014. /* save volume value if the widget is powered down */
  1015. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  1016. widget->saved_value = val;
  1017. mutex_unlock(&widget->codec->mutex);
  1018. return 1;
  1019. }
  1020. dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
  1021. if (widget->event) {
  1022. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1023. ret = widget->event(widget, kcontrol,
  1024. SND_SOC_DAPM_PRE_REG);
  1025. if (ret < 0) {
  1026. ret = 1;
  1027. goto out;
  1028. }
  1029. }
  1030. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1031. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1032. ret = widget->event(widget, kcontrol,
  1033. SND_SOC_DAPM_POST_REG);
  1034. } else
  1035. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1036. out:
  1037. mutex_unlock(&widget->codec->mutex);
  1038. return ret;
  1039. }
  1040. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1041. /**
  1042. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1043. * @kcontrol: mixer control
  1044. * @uinfo: control element information
  1045. *
  1046. * Callback to get the value of a dapm enumerated double mixer control.
  1047. *
  1048. * Returns 0 for success.
  1049. */
  1050. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1051. struct snd_ctl_elem_value *ucontrol)
  1052. {
  1053. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1054. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1055. unsigned short val, bitmask;
  1056. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1057. ;
  1058. val = snd_soc_read(widget->codec, e->reg);
  1059. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1060. if (e->shift_l != e->shift_r)
  1061. ucontrol->value.enumerated.item[1] =
  1062. (val >> e->shift_r) & (bitmask - 1);
  1063. return 0;
  1064. }
  1065. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1066. /**
  1067. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1068. * @kcontrol: mixer control
  1069. * @uinfo: control element information
  1070. *
  1071. * Callback to set the value of a dapm enumerated double mixer control.
  1072. *
  1073. * Returns 0 for success.
  1074. */
  1075. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1076. struct snd_ctl_elem_value *ucontrol)
  1077. {
  1078. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1079. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1080. unsigned short val, mux;
  1081. unsigned short mask, bitmask;
  1082. int ret = 0;
  1083. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1084. ;
  1085. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1086. return -EINVAL;
  1087. mux = ucontrol->value.enumerated.item[0];
  1088. val = mux << e->shift_l;
  1089. mask = (bitmask - 1) << e->shift_l;
  1090. if (e->shift_l != e->shift_r) {
  1091. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1092. return -EINVAL;
  1093. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1094. mask |= (bitmask - 1) << e->shift_r;
  1095. }
  1096. mutex_lock(&widget->codec->mutex);
  1097. widget->value = val;
  1098. dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
  1099. if (widget->event) {
  1100. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1101. ret = widget->event(widget,
  1102. kcontrol, SND_SOC_DAPM_PRE_REG);
  1103. if (ret < 0)
  1104. goto out;
  1105. }
  1106. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1107. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1108. ret = widget->event(widget,
  1109. kcontrol, SND_SOC_DAPM_POST_REG);
  1110. } else
  1111. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1112. out:
  1113. mutex_unlock(&widget->codec->mutex);
  1114. return ret;
  1115. }
  1116. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1117. /**
  1118. * snd_soc_dapm_new_control - create new dapm control
  1119. * @codec: audio codec
  1120. * @widget: widget template
  1121. *
  1122. * Creates a new dapm control based upon the template.
  1123. *
  1124. * Returns 0 for success else error.
  1125. */
  1126. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1127. const struct snd_soc_dapm_widget *widget)
  1128. {
  1129. struct snd_soc_dapm_widget *w;
  1130. if ((w = dapm_cnew_widget(widget)) == NULL)
  1131. return -ENOMEM;
  1132. w->codec = codec;
  1133. INIT_LIST_HEAD(&w->sources);
  1134. INIT_LIST_HEAD(&w->sinks);
  1135. INIT_LIST_HEAD(&w->list);
  1136. list_add(&w->list, &codec->dapm_widgets);
  1137. /* machine layer set ups unconnected pins and insertions */
  1138. w->connected = 1;
  1139. return 0;
  1140. }
  1141. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1142. /**
  1143. * snd_soc_dapm_new_controls - create new dapm controls
  1144. * @codec: audio codec
  1145. * @widget: widget array
  1146. * @num: number of widgets
  1147. *
  1148. * Creates new DAPM controls based upon the templates.
  1149. *
  1150. * Returns 0 for success else error.
  1151. */
  1152. int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
  1153. const struct snd_soc_dapm_widget *widget,
  1154. int num)
  1155. {
  1156. int i, ret;
  1157. for (i = 0; i < num; i++) {
  1158. ret = snd_soc_dapm_new_control(codec, widget);
  1159. if (ret < 0)
  1160. return ret;
  1161. widget++;
  1162. }
  1163. return 0;
  1164. }
  1165. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1166. /**
  1167. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1168. * @codec: audio codec
  1169. * @stream: stream name
  1170. * @event: stream event
  1171. *
  1172. * Sends a stream event to the dapm core. The core then makes any
  1173. * necessary widget power changes.
  1174. *
  1175. * Returns 0 for success else error.
  1176. */
  1177. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1178. char *stream, int event)
  1179. {
  1180. struct snd_soc_dapm_widget *w;
  1181. if (stream == NULL)
  1182. return 0;
  1183. mutex_lock(&codec->mutex);
  1184. list_for_each_entry(w, &codec->dapm_widgets, list)
  1185. {
  1186. if (!w->sname)
  1187. continue;
  1188. pr_debug("widget %s\n %s stream %s event %d\n",
  1189. w->name, w->sname, stream, event);
  1190. if (strstr(w->sname, stream)) {
  1191. switch(event) {
  1192. case SND_SOC_DAPM_STREAM_START:
  1193. w->active = 1;
  1194. break;
  1195. case SND_SOC_DAPM_STREAM_STOP:
  1196. w->active = 0;
  1197. break;
  1198. case SND_SOC_DAPM_STREAM_SUSPEND:
  1199. if (w->active)
  1200. w->suspend = 1;
  1201. w->active = 0;
  1202. break;
  1203. case SND_SOC_DAPM_STREAM_RESUME:
  1204. if (w->suspend) {
  1205. w->active = 1;
  1206. w->suspend = 0;
  1207. }
  1208. break;
  1209. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1210. break;
  1211. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1212. break;
  1213. }
  1214. }
  1215. }
  1216. mutex_unlock(&codec->mutex);
  1217. dapm_power_widgets(codec, event);
  1218. dump_dapm(codec, __func__);
  1219. return 0;
  1220. }
  1221. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1222. /**
  1223. * snd_soc_dapm_set_bias_level - set the bias level for the system
  1224. * @socdev: audio device
  1225. * @level: level to configure
  1226. *
  1227. * Configure the bias (power) levels for the SoC audio device.
  1228. *
  1229. * Returns 0 for success else error.
  1230. */
  1231. int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
  1232. enum snd_soc_bias_level level)
  1233. {
  1234. struct snd_soc_codec *codec = socdev->codec;
  1235. struct snd_soc_card *card = socdev->card;
  1236. int ret = 0;
  1237. if (card->set_bias_level)
  1238. ret = card->set_bias_level(card, level);
  1239. if (ret == 0 && codec->set_bias_level)
  1240. ret = codec->set_bias_level(codec, level);
  1241. return ret;
  1242. }
  1243. /**
  1244. * snd_soc_dapm_enable_pin - enable pin.
  1245. * @snd_soc_codec: SoC codec
  1246. * @pin: pin name
  1247. *
  1248. * Enables input/output pin and it's parents or children widgets iff there is
  1249. * a valid audio route and active audio stream.
  1250. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1251. * do any widget power switching.
  1252. */
  1253. int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, char *pin)
  1254. {
  1255. return snd_soc_dapm_set_pin(codec, pin, 1);
  1256. }
  1257. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1258. /**
  1259. * snd_soc_dapm_disable_pin - disable pin.
  1260. * @codec: SoC codec
  1261. * @pin: pin name
  1262. *
  1263. * Disables input/output pin and it's parents or children widgets.
  1264. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1265. * do any widget power switching.
  1266. */
  1267. int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, char *pin)
  1268. {
  1269. return snd_soc_dapm_set_pin(codec, pin, 0);
  1270. }
  1271. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1272. /**
  1273. * snd_soc_dapm_nc_pin - permanently disable pin.
  1274. * @codec: SoC codec
  1275. * @pin: pin name
  1276. *
  1277. * Marks the specified pin as being not connected, disabling it along
  1278. * any parent or child widgets. At present this is identical to
  1279. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1280. * additional things such as disabling controls which only affect
  1281. * paths through the pin.
  1282. *
  1283. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1284. * do any widget power switching.
  1285. */
  1286. int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, char *pin)
  1287. {
  1288. return snd_soc_dapm_set_pin(codec, pin, 0);
  1289. }
  1290. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1291. /**
  1292. * snd_soc_dapm_get_pin_status - get audio pin status
  1293. * @codec: audio codec
  1294. * @pin: audio signal pin endpoint (or start point)
  1295. *
  1296. * Get audio pin status - connected or disconnected.
  1297. *
  1298. * Returns 1 for connected otherwise 0.
  1299. */
  1300. int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, char *pin)
  1301. {
  1302. struct snd_soc_dapm_widget *w;
  1303. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1304. if (!strcmp(w->name, pin))
  1305. return w->connected;
  1306. }
  1307. return 0;
  1308. }
  1309. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1310. /**
  1311. * snd_soc_dapm_free - free dapm resources
  1312. * @socdev: SoC device
  1313. *
  1314. * Free all dapm widgets and resources.
  1315. */
  1316. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1317. {
  1318. struct snd_soc_codec *codec = socdev->codec;
  1319. snd_soc_dapm_sys_remove(socdev->dev);
  1320. dapm_free_widgets(codec);
  1321. }
  1322. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1323. /* Module information */
  1324. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1325. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1326. MODULE_LICENSE("GPL");