soc-dapm.c 65 KB

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