soc-dapm.c 41 KB

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