soc-dapm.c 38 KB

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