soc-dapm.c 63 KB

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