soc-dapm.c 55 KB

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