soc-dapm.c 65 KB

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