soc-dapm.c 56 KB

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