soc-dapm.c 46 KB

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