soc-dapm.c 60 KB

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