soc-dapm.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464
  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)
  620. continue;
  621. if (path->sink->force) {
  622. power = 1;
  623. break;
  624. }
  625. if (path->sink->power_check &&
  626. path->sink->power_check(path->sink)) {
  627. power = 1;
  628. break;
  629. }
  630. }
  631. dapm_clear_walk(w->dapm);
  632. return power;
  633. }
  634. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  635. struct snd_soc_dapm_widget *b,
  636. bool power_up)
  637. {
  638. int *sort;
  639. if (power_up)
  640. sort = dapm_up_seq;
  641. else
  642. sort = dapm_down_seq;
  643. if (sort[a->id] != sort[b->id])
  644. return sort[a->id] - sort[b->id];
  645. if (a->subseq != b->subseq) {
  646. if (power_up)
  647. return a->subseq - b->subseq;
  648. else
  649. return b->subseq - a->subseq;
  650. }
  651. if (a->reg != b->reg)
  652. return a->reg - b->reg;
  653. if (a->dapm != b->dapm)
  654. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  655. return 0;
  656. }
  657. /* Insert a widget in order into a DAPM power sequence. */
  658. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  659. struct list_head *list,
  660. bool power_up)
  661. {
  662. struct snd_soc_dapm_widget *w;
  663. list_for_each_entry(w, list, power_list)
  664. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  665. list_add_tail(&new_widget->power_list, &w->power_list);
  666. return;
  667. }
  668. list_add_tail(&new_widget->power_list, list);
  669. }
  670. static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
  671. struct snd_soc_dapm_widget *w, int event)
  672. {
  673. struct snd_soc_card *card = dapm->card;
  674. const char *ev_name;
  675. int power, ret;
  676. switch (event) {
  677. case SND_SOC_DAPM_PRE_PMU:
  678. ev_name = "PRE_PMU";
  679. power = 1;
  680. break;
  681. case SND_SOC_DAPM_POST_PMU:
  682. ev_name = "POST_PMU";
  683. power = 1;
  684. break;
  685. case SND_SOC_DAPM_PRE_PMD:
  686. ev_name = "PRE_PMD";
  687. power = 0;
  688. break;
  689. case SND_SOC_DAPM_POST_PMD:
  690. ev_name = "POST_PMD";
  691. power = 0;
  692. break;
  693. default:
  694. BUG();
  695. return;
  696. }
  697. if (w->power != power)
  698. return;
  699. if (w->event && (w->event_flags & event)) {
  700. pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
  701. w->name, ev_name);
  702. trace_snd_soc_dapm_widget_event_start(w, event);
  703. ret = w->event(w, NULL, event);
  704. trace_snd_soc_dapm_widget_event_done(w, event);
  705. if (ret < 0)
  706. pr_err("%s: %s event failed: %d\n",
  707. ev_name, w->name, ret);
  708. }
  709. }
  710. /* Apply the coalesced changes from a DAPM sequence */
  711. static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
  712. struct list_head *pending)
  713. {
  714. struct snd_soc_card *card = dapm->card;
  715. struct snd_soc_dapm_widget *w;
  716. int reg, power;
  717. unsigned int value = 0;
  718. unsigned int mask = 0;
  719. unsigned int cur_mask;
  720. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  721. power_list)->reg;
  722. list_for_each_entry(w, pending, power_list) {
  723. cur_mask = 1 << w->shift;
  724. BUG_ON(reg != w->reg);
  725. if (w->invert)
  726. power = !w->power;
  727. else
  728. power = w->power;
  729. mask |= cur_mask;
  730. if (power)
  731. value |= cur_mask;
  732. pop_dbg(dapm->dev, card->pop_time,
  733. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  734. w->name, reg, value, mask);
  735. /* Check for events */
  736. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
  737. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
  738. }
  739. if (reg >= 0) {
  740. pop_dbg(dapm->dev, card->pop_time,
  741. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  742. value, mask, reg, card->pop_time);
  743. pop_wait(card->pop_time);
  744. snd_soc_update_bits(dapm->codec, reg, mask, value);
  745. }
  746. list_for_each_entry(w, pending, power_list) {
  747. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
  748. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
  749. }
  750. }
  751. /* Apply a DAPM power sequence.
  752. *
  753. * We walk over a pre-sorted list of widgets to apply power to. In
  754. * order to minimise the number of writes to the device required
  755. * multiple widgets will be updated in a single write where possible.
  756. * Currently anything that requires more than a single write is not
  757. * handled.
  758. */
  759. static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
  760. struct list_head *list, int event, bool power_up)
  761. {
  762. struct snd_soc_dapm_widget *w, *n;
  763. LIST_HEAD(pending);
  764. int cur_sort = -1;
  765. int cur_subseq = -1;
  766. int cur_reg = SND_SOC_NOPM;
  767. struct snd_soc_dapm_context *cur_dapm = NULL;
  768. int ret, i;
  769. int *sort;
  770. if (power_up)
  771. sort = dapm_up_seq;
  772. else
  773. sort = dapm_down_seq;
  774. list_for_each_entry_safe(w, n, list, power_list) {
  775. ret = 0;
  776. /* Do we need to apply any queued changes? */
  777. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  778. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  779. if (!list_empty(&pending))
  780. dapm_seq_run_coalesced(cur_dapm, &pending);
  781. if (cur_dapm && cur_dapm->seq_notifier) {
  782. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  783. if (sort[i] == cur_sort)
  784. cur_dapm->seq_notifier(cur_dapm,
  785. i,
  786. cur_subseq);
  787. }
  788. INIT_LIST_HEAD(&pending);
  789. cur_sort = -1;
  790. cur_subseq = -1;
  791. cur_reg = SND_SOC_NOPM;
  792. cur_dapm = NULL;
  793. }
  794. switch (w->id) {
  795. case snd_soc_dapm_pre:
  796. if (!w->event)
  797. list_for_each_entry_safe_continue(w, n, list,
  798. power_list);
  799. if (event == SND_SOC_DAPM_STREAM_START)
  800. ret = w->event(w,
  801. NULL, SND_SOC_DAPM_PRE_PMU);
  802. else if (event == SND_SOC_DAPM_STREAM_STOP)
  803. ret = w->event(w,
  804. NULL, SND_SOC_DAPM_PRE_PMD);
  805. break;
  806. case snd_soc_dapm_post:
  807. if (!w->event)
  808. list_for_each_entry_safe_continue(w, n, list,
  809. power_list);
  810. if (event == SND_SOC_DAPM_STREAM_START)
  811. ret = w->event(w,
  812. NULL, SND_SOC_DAPM_POST_PMU);
  813. else if (event == SND_SOC_DAPM_STREAM_STOP)
  814. ret = w->event(w,
  815. NULL, SND_SOC_DAPM_POST_PMD);
  816. break;
  817. case snd_soc_dapm_input:
  818. case snd_soc_dapm_output:
  819. case snd_soc_dapm_hp:
  820. case snd_soc_dapm_mic:
  821. case snd_soc_dapm_line:
  822. case snd_soc_dapm_spk:
  823. /* No register support currently */
  824. ret = dapm_generic_apply_power(w);
  825. break;
  826. default:
  827. /* Queue it up for application */
  828. cur_sort = sort[w->id];
  829. cur_subseq = w->subseq;
  830. cur_reg = w->reg;
  831. cur_dapm = w->dapm;
  832. list_move(&w->power_list, &pending);
  833. break;
  834. }
  835. if (ret < 0)
  836. dev_err(w->dapm->dev,
  837. "Failed to apply widget power: %d\n", ret);
  838. }
  839. if (!list_empty(&pending))
  840. dapm_seq_run_coalesced(dapm, &pending);
  841. if (cur_dapm && cur_dapm->seq_notifier) {
  842. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  843. if (sort[i] == cur_sort)
  844. cur_dapm->seq_notifier(cur_dapm,
  845. i, cur_subseq);
  846. }
  847. }
  848. static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
  849. {
  850. struct snd_soc_dapm_update *update = dapm->update;
  851. struct snd_soc_dapm_widget *w;
  852. int ret;
  853. if (!update)
  854. return;
  855. w = update->widget;
  856. if (w->event &&
  857. (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  858. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  859. if (ret != 0)
  860. pr_err("%s DAPM pre-event failed: %d\n",
  861. w->name, ret);
  862. }
  863. ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
  864. update->val);
  865. if (ret < 0)
  866. pr_err("%s DAPM update failed: %d\n", w->name, ret);
  867. if (w->event &&
  868. (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  869. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  870. if (ret != 0)
  871. pr_err("%s DAPM post-event failed: %d\n",
  872. w->name, ret);
  873. }
  874. }
  875. /*
  876. * Scan each dapm widget for complete audio path.
  877. * A complete path is a route that has valid endpoints i.e.:-
  878. *
  879. * o DAC to output pin.
  880. * o Input Pin to ADC.
  881. * o Input pin to Output pin (bypass, sidetone)
  882. * o DAC to ADC (loopback).
  883. */
  884. static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
  885. {
  886. struct snd_soc_card *card = dapm->codec->card;
  887. struct snd_soc_dapm_widget *w;
  888. struct snd_soc_dapm_context *d;
  889. LIST_HEAD(up_list);
  890. LIST_HEAD(down_list);
  891. int ret = 0;
  892. int power;
  893. trace_snd_soc_dapm_start(card);
  894. list_for_each_entry(d, &card->dapm_list, list)
  895. if (d->n_widgets)
  896. d->dev_power = 0;
  897. /* Check which widgets we need to power and store them in
  898. * lists indicating if they should be powered up or down.
  899. */
  900. list_for_each_entry(w, &card->widgets, list) {
  901. switch (w->id) {
  902. case snd_soc_dapm_pre:
  903. dapm_seq_insert(w, &down_list, false);
  904. break;
  905. case snd_soc_dapm_post:
  906. dapm_seq_insert(w, &up_list, true);
  907. break;
  908. default:
  909. if (!w->power_check)
  910. continue;
  911. if (!w->force)
  912. power = w->power_check(w);
  913. else
  914. power = 1;
  915. if (power)
  916. w->dapm->dev_power = 1;
  917. if (w->power == power)
  918. continue;
  919. trace_snd_soc_dapm_widget_power(w, power);
  920. if (power)
  921. dapm_seq_insert(w, &up_list, true);
  922. else
  923. dapm_seq_insert(w, &down_list, false);
  924. w->power = power;
  925. break;
  926. }
  927. }
  928. /* If there are no DAPM widgets then try to figure out power from the
  929. * event type.
  930. */
  931. if (!dapm->n_widgets) {
  932. switch (event) {
  933. case SND_SOC_DAPM_STREAM_START:
  934. case SND_SOC_DAPM_STREAM_RESUME:
  935. dapm->dev_power = 1;
  936. break;
  937. case SND_SOC_DAPM_STREAM_STOP:
  938. dapm->dev_power = !!dapm->codec->active;
  939. break;
  940. case SND_SOC_DAPM_STREAM_SUSPEND:
  941. dapm->dev_power = 0;
  942. break;
  943. case SND_SOC_DAPM_STREAM_NOP:
  944. switch (dapm->bias_level) {
  945. case SND_SOC_BIAS_STANDBY:
  946. case SND_SOC_BIAS_OFF:
  947. dapm->dev_power = 0;
  948. break;
  949. default:
  950. dapm->dev_power = 1;
  951. break;
  952. }
  953. break;
  954. default:
  955. break;
  956. }
  957. }
  958. list_for_each_entry(d, &dapm->card->dapm_list, list) {
  959. if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
  960. ret = snd_soc_dapm_set_bias_level(card, d,
  961. SND_SOC_BIAS_STANDBY);
  962. if (ret != 0)
  963. dev_err(d->dev,
  964. "Failed to turn on bias: %d\n", ret);
  965. }
  966. /* If we're changing to all on or all off then prepare */
  967. if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
  968. (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
  969. ret = snd_soc_dapm_set_bias_level(card, d,
  970. SND_SOC_BIAS_PREPARE);
  971. if (ret != 0)
  972. dev_err(d->dev,
  973. "Failed to prepare bias: %d\n", ret);
  974. }
  975. }
  976. /* Power down widgets first; try to avoid amplifying pops. */
  977. dapm_seq_run(dapm, &down_list, event, false);
  978. dapm_widget_update(dapm);
  979. /* Now power up. */
  980. dapm_seq_run(dapm, &up_list, event, true);
  981. list_for_each_entry(d, &dapm->card->dapm_list, list) {
  982. /* If we just powered the last thing off drop to standby bias */
  983. if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
  984. ret = snd_soc_dapm_set_bias_level(card, d,
  985. SND_SOC_BIAS_STANDBY);
  986. if (ret != 0)
  987. dev_err(d->dev,
  988. "Failed to apply standby bias: %d\n",
  989. ret);
  990. }
  991. /* If we're in standby and can support bias off then do that */
  992. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  993. d->idle_bias_off) {
  994. ret = snd_soc_dapm_set_bias_level(card, d,
  995. SND_SOC_BIAS_OFF);
  996. if (ret != 0)
  997. dev_err(d->dev,
  998. "Failed to turn off bias: %d\n", ret);
  999. }
  1000. /* If we just powered up then move to active bias */
  1001. if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
  1002. ret = snd_soc_dapm_set_bias_level(card, d,
  1003. SND_SOC_BIAS_ON);
  1004. if (ret != 0)
  1005. dev_err(d->dev,
  1006. "Failed to apply active bias: %d\n",
  1007. ret);
  1008. }
  1009. }
  1010. pop_dbg(dapm->dev, card->pop_time,
  1011. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1012. pop_wait(card->pop_time);
  1013. trace_snd_soc_dapm_done(card);
  1014. return 0;
  1015. }
  1016. #ifdef CONFIG_DEBUG_FS
  1017. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  1018. {
  1019. file->private_data = inode->i_private;
  1020. return 0;
  1021. }
  1022. static ssize_t dapm_widget_power_read_file(struct file *file,
  1023. char __user *user_buf,
  1024. size_t count, loff_t *ppos)
  1025. {
  1026. struct snd_soc_dapm_widget *w = file->private_data;
  1027. char *buf;
  1028. int in, out;
  1029. ssize_t ret;
  1030. struct snd_soc_dapm_path *p = NULL;
  1031. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1032. if (!buf)
  1033. return -ENOMEM;
  1034. in = is_connected_input_ep(w);
  1035. dapm_clear_walk(w->dapm);
  1036. out = is_connected_output_ep(w);
  1037. dapm_clear_walk(w->dapm);
  1038. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  1039. w->name, w->power ? "On" : "Off", in, out);
  1040. if (w->reg >= 0)
  1041. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1042. " - R%d(0x%x) bit %d",
  1043. w->reg, w->reg, w->shift);
  1044. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1045. if (w->sname)
  1046. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1047. w->sname,
  1048. w->active ? "active" : "inactive");
  1049. list_for_each_entry(p, &w->sources, list_sink) {
  1050. if (p->connected && !p->connected(w, p->sink))
  1051. continue;
  1052. if (p->connect)
  1053. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1054. " in %s %s\n",
  1055. p->name ? p->name : "static",
  1056. p->source->name);
  1057. }
  1058. list_for_each_entry(p, &w->sinks, list_source) {
  1059. if (p->connected && !p->connected(w, p->sink))
  1060. continue;
  1061. if (p->connect)
  1062. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1063. " out %s %s\n",
  1064. p->name ? p->name : "static",
  1065. p->sink->name);
  1066. }
  1067. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1068. kfree(buf);
  1069. return ret;
  1070. }
  1071. static const struct file_operations dapm_widget_power_fops = {
  1072. .open = dapm_widget_power_open_file,
  1073. .read = dapm_widget_power_read_file,
  1074. .llseek = default_llseek,
  1075. };
  1076. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  1077. {
  1078. struct snd_soc_dapm_widget *w;
  1079. struct dentry *d;
  1080. if (!dapm->debugfs_dapm)
  1081. return;
  1082. list_for_each_entry(w, &dapm->card->widgets, list) {
  1083. if (!w->name || w->dapm != dapm)
  1084. continue;
  1085. d = debugfs_create_file(w->name, 0444,
  1086. dapm->debugfs_dapm, w,
  1087. &dapm_widget_power_fops);
  1088. if (!d)
  1089. dev_warn(w->dapm->dev,
  1090. "ASoC: Failed to create %s debugfs file\n",
  1091. w->name);
  1092. }
  1093. }
  1094. #else
  1095. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  1096. {
  1097. }
  1098. #endif
  1099. /* test and update the power status of a mux widget */
  1100. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1101. struct snd_kcontrol *kcontrol, int change,
  1102. int mux, struct soc_enum *e)
  1103. {
  1104. struct snd_soc_dapm_path *path;
  1105. int found = 0;
  1106. if (widget->id != snd_soc_dapm_mux &&
  1107. widget->id != snd_soc_dapm_virt_mux &&
  1108. widget->id != snd_soc_dapm_value_mux)
  1109. return -ENODEV;
  1110. if (!change)
  1111. return 0;
  1112. /* find dapm widget path assoc with kcontrol */
  1113. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1114. if (path->kcontrol != kcontrol)
  1115. continue;
  1116. if (!path->name || !e->texts[mux])
  1117. continue;
  1118. found = 1;
  1119. /* we now need to match the string in the enum to the path */
  1120. if (!(strcmp(path->name, e->texts[mux])))
  1121. path->connect = 1; /* new connection */
  1122. else
  1123. path->connect = 0; /* old connection must be powered down */
  1124. }
  1125. if (found)
  1126. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1127. return 0;
  1128. }
  1129. /* test and update the power status of a mixer or switch widget */
  1130. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1131. struct snd_kcontrol *kcontrol, int connect)
  1132. {
  1133. struct snd_soc_dapm_path *path;
  1134. int found = 0;
  1135. if (widget->id != snd_soc_dapm_mixer &&
  1136. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1137. widget->id != snd_soc_dapm_switch)
  1138. return -ENODEV;
  1139. /* find dapm widget path assoc with kcontrol */
  1140. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1141. if (path->kcontrol != kcontrol)
  1142. continue;
  1143. /* found, now check type */
  1144. found = 1;
  1145. path->connect = connect;
  1146. break;
  1147. }
  1148. if (found)
  1149. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1150. return 0;
  1151. }
  1152. /* show dapm widget status in sys fs */
  1153. static ssize_t dapm_widget_show(struct device *dev,
  1154. struct device_attribute *attr, char *buf)
  1155. {
  1156. struct snd_soc_pcm_runtime *rtd =
  1157. container_of(dev, struct snd_soc_pcm_runtime, dev);
  1158. struct snd_soc_codec *codec =rtd->codec;
  1159. struct snd_soc_dapm_widget *w;
  1160. int count = 0;
  1161. char *state = "not set";
  1162. list_for_each_entry(w, &codec->card->widgets, list) {
  1163. if (w->dapm != &codec->dapm)
  1164. continue;
  1165. /* only display widgets that burnm power */
  1166. switch (w->id) {
  1167. case snd_soc_dapm_hp:
  1168. case snd_soc_dapm_mic:
  1169. case snd_soc_dapm_spk:
  1170. case snd_soc_dapm_line:
  1171. case snd_soc_dapm_micbias:
  1172. case snd_soc_dapm_dac:
  1173. case snd_soc_dapm_adc:
  1174. case snd_soc_dapm_pga:
  1175. case snd_soc_dapm_out_drv:
  1176. case snd_soc_dapm_mixer:
  1177. case snd_soc_dapm_mixer_named_ctl:
  1178. case snd_soc_dapm_supply:
  1179. if (w->name)
  1180. count += sprintf(buf + count, "%s: %s\n",
  1181. w->name, w->power ? "On":"Off");
  1182. break;
  1183. default:
  1184. break;
  1185. }
  1186. }
  1187. switch (codec->dapm.bias_level) {
  1188. case SND_SOC_BIAS_ON:
  1189. state = "On";
  1190. break;
  1191. case SND_SOC_BIAS_PREPARE:
  1192. state = "Prepare";
  1193. break;
  1194. case SND_SOC_BIAS_STANDBY:
  1195. state = "Standby";
  1196. break;
  1197. case SND_SOC_BIAS_OFF:
  1198. state = "Off";
  1199. break;
  1200. }
  1201. count += sprintf(buf + count, "PM State: %s\n", state);
  1202. return count;
  1203. }
  1204. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1205. int snd_soc_dapm_sys_add(struct device *dev)
  1206. {
  1207. return device_create_file(dev, &dev_attr_dapm_widget);
  1208. }
  1209. static void snd_soc_dapm_sys_remove(struct device *dev)
  1210. {
  1211. device_remove_file(dev, &dev_attr_dapm_widget);
  1212. }
  1213. /* free all dapm widgets and resources */
  1214. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1215. {
  1216. struct snd_soc_dapm_widget *w, *next_w;
  1217. struct snd_soc_dapm_path *p, *next_p;
  1218. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1219. if (w->dapm != dapm)
  1220. continue;
  1221. list_del(&w->list);
  1222. /*
  1223. * remove source and sink paths associated to this widget.
  1224. * While removing the path, remove reference to it from both
  1225. * source and sink widgets so that path is removed only once.
  1226. */
  1227. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1228. list_del(&p->list_sink);
  1229. list_del(&p->list_source);
  1230. list_del(&p->list);
  1231. kfree(p->long_name);
  1232. kfree(p);
  1233. }
  1234. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1235. list_del(&p->list_sink);
  1236. list_del(&p->list_source);
  1237. list_del(&p->list);
  1238. kfree(p->long_name);
  1239. kfree(p);
  1240. }
  1241. kfree(w->name);
  1242. kfree(w);
  1243. }
  1244. }
  1245. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1246. const char *pin, int status)
  1247. {
  1248. struct snd_soc_dapm_widget *w;
  1249. list_for_each_entry(w, &dapm->card->widgets, list) {
  1250. if (w->dapm != dapm)
  1251. continue;
  1252. if (!strcmp(w->name, pin)) {
  1253. dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
  1254. pin, status);
  1255. w->connected = status;
  1256. /* Allow disabling of forced pins */
  1257. if (status == 0)
  1258. w->force = 0;
  1259. return 0;
  1260. }
  1261. }
  1262. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1263. return -EINVAL;
  1264. }
  1265. /**
  1266. * snd_soc_dapm_sync - scan and power dapm paths
  1267. * @dapm: DAPM context
  1268. *
  1269. * Walks all dapm audio paths and powers widgets according to their
  1270. * stream or path usage.
  1271. *
  1272. * Returns 0 for success.
  1273. */
  1274. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1275. {
  1276. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1277. }
  1278. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1279. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1280. const struct snd_soc_dapm_route *route)
  1281. {
  1282. struct snd_soc_dapm_path *path;
  1283. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1284. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1285. const char *sink;
  1286. const char *control = route->control;
  1287. const char *source;
  1288. char prefixed_sink[80];
  1289. char prefixed_source[80];
  1290. int ret = 0;
  1291. if (dapm->codec->name_prefix) {
  1292. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1293. dapm->codec->name_prefix, route->sink);
  1294. sink = prefixed_sink;
  1295. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1296. dapm->codec->name_prefix, route->source);
  1297. source = prefixed_source;
  1298. } else {
  1299. sink = route->sink;
  1300. source = route->source;
  1301. }
  1302. /*
  1303. * find src and dest widgets over all widgets but favor a widget from
  1304. * current DAPM context
  1305. */
  1306. list_for_each_entry(w, &dapm->card->widgets, list) {
  1307. if (!wsink && !(strcmp(w->name, sink))) {
  1308. wtsink = w;
  1309. if (w->dapm == dapm)
  1310. wsink = w;
  1311. continue;
  1312. }
  1313. if (!wsource && !(strcmp(w->name, source))) {
  1314. wtsource = w;
  1315. if (w->dapm == dapm)
  1316. wsource = w;
  1317. }
  1318. }
  1319. /* use widget from another DAPM context if not found from this */
  1320. if (!wsink)
  1321. wsink = wtsink;
  1322. if (!wsource)
  1323. wsource = wtsource;
  1324. if (wsource == NULL || wsink == NULL)
  1325. return -ENODEV;
  1326. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1327. if (!path)
  1328. return -ENOMEM;
  1329. path->source = wsource;
  1330. path->sink = wsink;
  1331. path->connected = route->connected;
  1332. INIT_LIST_HEAD(&path->list);
  1333. INIT_LIST_HEAD(&path->list_source);
  1334. INIT_LIST_HEAD(&path->list_sink);
  1335. /* check for external widgets */
  1336. if (wsink->id == snd_soc_dapm_input) {
  1337. if (wsource->id == snd_soc_dapm_micbias ||
  1338. wsource->id == snd_soc_dapm_mic ||
  1339. wsource->id == snd_soc_dapm_line ||
  1340. wsource->id == snd_soc_dapm_output)
  1341. wsink->ext = 1;
  1342. }
  1343. if (wsource->id == snd_soc_dapm_output) {
  1344. if (wsink->id == snd_soc_dapm_spk ||
  1345. wsink->id == snd_soc_dapm_hp ||
  1346. wsink->id == snd_soc_dapm_line ||
  1347. wsink->id == snd_soc_dapm_input)
  1348. wsource->ext = 1;
  1349. }
  1350. /* connect static paths */
  1351. if (control == NULL) {
  1352. list_add(&path->list, &dapm->card->paths);
  1353. list_add(&path->list_sink, &wsink->sources);
  1354. list_add(&path->list_source, &wsource->sinks);
  1355. path->connect = 1;
  1356. return 0;
  1357. }
  1358. /* connect dynamic paths */
  1359. switch(wsink->id) {
  1360. case snd_soc_dapm_adc:
  1361. case snd_soc_dapm_dac:
  1362. case snd_soc_dapm_pga:
  1363. case snd_soc_dapm_out_drv:
  1364. case snd_soc_dapm_input:
  1365. case snd_soc_dapm_output:
  1366. case snd_soc_dapm_micbias:
  1367. case snd_soc_dapm_vmid:
  1368. case snd_soc_dapm_pre:
  1369. case snd_soc_dapm_post:
  1370. case snd_soc_dapm_supply:
  1371. case snd_soc_dapm_aif_in:
  1372. case snd_soc_dapm_aif_out:
  1373. list_add(&path->list, &dapm->card->paths);
  1374. list_add(&path->list_sink, &wsink->sources);
  1375. list_add(&path->list_source, &wsource->sinks);
  1376. path->connect = 1;
  1377. return 0;
  1378. case snd_soc_dapm_mux:
  1379. case snd_soc_dapm_virt_mux:
  1380. case snd_soc_dapm_value_mux:
  1381. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1382. &wsink->kcontrols[0]);
  1383. if (ret != 0)
  1384. goto err;
  1385. break;
  1386. case snd_soc_dapm_switch:
  1387. case snd_soc_dapm_mixer:
  1388. case snd_soc_dapm_mixer_named_ctl:
  1389. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1390. if (ret != 0)
  1391. goto err;
  1392. break;
  1393. case snd_soc_dapm_hp:
  1394. case snd_soc_dapm_mic:
  1395. case snd_soc_dapm_line:
  1396. case snd_soc_dapm_spk:
  1397. list_add(&path->list, &dapm->card->paths);
  1398. list_add(&path->list_sink, &wsink->sources);
  1399. list_add(&path->list_source, &wsource->sinks);
  1400. path->connect = 0;
  1401. return 0;
  1402. }
  1403. return 0;
  1404. err:
  1405. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1406. source, control, sink);
  1407. kfree(path);
  1408. return ret;
  1409. }
  1410. /**
  1411. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1412. * @dapm: DAPM context
  1413. * @route: audio routes
  1414. * @num: number of routes
  1415. *
  1416. * Connects 2 dapm widgets together via a named audio path. The sink is
  1417. * the widget receiving the audio signal, whilst the source is the sender
  1418. * of the audio signal.
  1419. *
  1420. * Returns 0 for success else error. On error all resources can be freed
  1421. * with a call to snd_soc_card_free().
  1422. */
  1423. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1424. const struct snd_soc_dapm_route *route, int num)
  1425. {
  1426. int i, ret;
  1427. for (i = 0; i < num; i++) {
  1428. ret = snd_soc_dapm_add_route(dapm, route);
  1429. if (ret < 0) {
  1430. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1431. route->source, route->sink);
  1432. return ret;
  1433. }
  1434. route++;
  1435. }
  1436. return 0;
  1437. }
  1438. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1439. /**
  1440. * snd_soc_dapm_new_widgets - add new dapm widgets
  1441. * @dapm: DAPM context
  1442. *
  1443. * Checks the codec for any new dapm widgets and creates them if found.
  1444. *
  1445. * Returns 0 for success.
  1446. */
  1447. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1448. {
  1449. struct snd_soc_dapm_widget *w;
  1450. unsigned int val;
  1451. list_for_each_entry(w, &dapm->card->widgets, list)
  1452. {
  1453. if (w->new)
  1454. continue;
  1455. switch(w->id) {
  1456. case snd_soc_dapm_switch:
  1457. case snd_soc_dapm_mixer:
  1458. case snd_soc_dapm_mixer_named_ctl:
  1459. w->power_check = dapm_generic_check_power;
  1460. dapm_new_mixer(dapm, w);
  1461. break;
  1462. case snd_soc_dapm_mux:
  1463. case snd_soc_dapm_virt_mux:
  1464. case snd_soc_dapm_value_mux:
  1465. w->power_check = dapm_generic_check_power;
  1466. dapm_new_mux(dapm, w);
  1467. break;
  1468. case snd_soc_dapm_adc:
  1469. case snd_soc_dapm_aif_out:
  1470. w->power_check = dapm_adc_check_power;
  1471. break;
  1472. case snd_soc_dapm_dac:
  1473. case snd_soc_dapm_aif_in:
  1474. w->power_check = dapm_dac_check_power;
  1475. break;
  1476. case snd_soc_dapm_pga:
  1477. case snd_soc_dapm_out_drv:
  1478. w->power_check = dapm_generic_check_power;
  1479. dapm_new_pga(dapm, w);
  1480. break;
  1481. case snd_soc_dapm_input:
  1482. case snd_soc_dapm_output:
  1483. case snd_soc_dapm_micbias:
  1484. case snd_soc_dapm_spk:
  1485. case snd_soc_dapm_hp:
  1486. case snd_soc_dapm_mic:
  1487. case snd_soc_dapm_line:
  1488. w->power_check = dapm_generic_check_power;
  1489. break;
  1490. case snd_soc_dapm_supply:
  1491. w->power_check = dapm_supply_check_power;
  1492. case snd_soc_dapm_vmid:
  1493. case snd_soc_dapm_pre:
  1494. case snd_soc_dapm_post:
  1495. break;
  1496. }
  1497. /* Read the initial power state from the device */
  1498. if (w->reg >= 0) {
  1499. val = snd_soc_read(w->codec, w->reg);
  1500. val &= 1 << w->shift;
  1501. if (w->invert)
  1502. val = !val;
  1503. if (val)
  1504. w->power = 1;
  1505. }
  1506. w->new = 1;
  1507. }
  1508. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1509. return 0;
  1510. }
  1511. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1512. /**
  1513. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1514. * @kcontrol: mixer control
  1515. * @ucontrol: control element information
  1516. *
  1517. * Callback to get the value of a dapm mixer control.
  1518. *
  1519. * Returns 0 for success.
  1520. */
  1521. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1522. struct snd_ctl_elem_value *ucontrol)
  1523. {
  1524. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1525. struct soc_mixer_control *mc =
  1526. (struct soc_mixer_control *)kcontrol->private_value;
  1527. unsigned int reg = mc->reg;
  1528. unsigned int shift = mc->shift;
  1529. unsigned int rshift = mc->rshift;
  1530. int max = mc->max;
  1531. unsigned int invert = mc->invert;
  1532. unsigned int mask = (1 << fls(max)) - 1;
  1533. ucontrol->value.integer.value[0] =
  1534. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1535. if (shift != rshift)
  1536. ucontrol->value.integer.value[1] =
  1537. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1538. if (invert) {
  1539. ucontrol->value.integer.value[0] =
  1540. max - ucontrol->value.integer.value[0];
  1541. if (shift != rshift)
  1542. ucontrol->value.integer.value[1] =
  1543. max - ucontrol->value.integer.value[1];
  1544. }
  1545. return 0;
  1546. }
  1547. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1548. /**
  1549. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1550. * @kcontrol: mixer control
  1551. * @ucontrol: control element information
  1552. *
  1553. * Callback to set the value of a dapm mixer control.
  1554. *
  1555. * Returns 0 for success.
  1556. */
  1557. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1558. struct snd_ctl_elem_value *ucontrol)
  1559. {
  1560. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1561. struct soc_mixer_control *mc =
  1562. (struct soc_mixer_control *)kcontrol->private_value;
  1563. unsigned int reg = mc->reg;
  1564. unsigned int shift = mc->shift;
  1565. int max = mc->max;
  1566. unsigned int mask = (1 << fls(max)) - 1;
  1567. unsigned int invert = mc->invert;
  1568. unsigned int val;
  1569. int connect, change;
  1570. struct snd_soc_dapm_update update;
  1571. val = (ucontrol->value.integer.value[0] & mask);
  1572. if (invert)
  1573. val = max - val;
  1574. mask = mask << shift;
  1575. val = val << shift;
  1576. mutex_lock(&widget->codec->mutex);
  1577. widget->value = val;
  1578. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  1579. if (change) {
  1580. if (val)
  1581. /* new connection */
  1582. connect = invert ? 0:1;
  1583. else
  1584. /* old connection must be powered down */
  1585. connect = invert ? 1:0;
  1586. update.kcontrol = kcontrol;
  1587. update.widget = widget;
  1588. update.reg = reg;
  1589. update.mask = mask;
  1590. update.val = val;
  1591. widget->dapm->update = &update;
  1592. dapm_mixer_update_power(widget, kcontrol, connect);
  1593. widget->dapm->update = NULL;
  1594. }
  1595. mutex_unlock(&widget->codec->mutex);
  1596. return 0;
  1597. }
  1598. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1599. /**
  1600. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1601. * @kcontrol: mixer control
  1602. * @ucontrol: control element information
  1603. *
  1604. * Callback to get the value of a dapm enumerated double mixer control.
  1605. *
  1606. * Returns 0 for success.
  1607. */
  1608. int snd_soc_dapm_get_enum_double(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_enum *e = (struct soc_enum *)kcontrol->private_value;
  1613. unsigned int val, bitmask;
  1614. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1615. ;
  1616. val = snd_soc_read(widget->codec, e->reg);
  1617. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1618. if (e->shift_l != e->shift_r)
  1619. ucontrol->value.enumerated.item[1] =
  1620. (val >> e->shift_r) & (bitmask - 1);
  1621. return 0;
  1622. }
  1623. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1624. /**
  1625. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1626. * @kcontrol: mixer control
  1627. * @ucontrol: control element information
  1628. *
  1629. * Callback to set the value of a dapm enumerated double mixer control.
  1630. *
  1631. * Returns 0 for success.
  1632. */
  1633. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1634. struct snd_ctl_elem_value *ucontrol)
  1635. {
  1636. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1637. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1638. unsigned int val, mux, change;
  1639. unsigned int mask, bitmask;
  1640. struct snd_soc_dapm_update update;
  1641. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1642. ;
  1643. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1644. return -EINVAL;
  1645. mux = ucontrol->value.enumerated.item[0];
  1646. val = mux << e->shift_l;
  1647. mask = (bitmask - 1) << e->shift_l;
  1648. if (e->shift_l != e->shift_r) {
  1649. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1650. return -EINVAL;
  1651. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1652. mask |= (bitmask - 1) << e->shift_r;
  1653. }
  1654. mutex_lock(&widget->codec->mutex);
  1655. widget->value = val;
  1656. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1657. update.kcontrol = kcontrol;
  1658. update.widget = widget;
  1659. update.reg = e->reg;
  1660. update.mask = mask;
  1661. update.val = val;
  1662. widget->dapm->update = &update;
  1663. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1664. widget->dapm->update = NULL;
  1665. mutex_unlock(&widget->codec->mutex);
  1666. return change;
  1667. }
  1668. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1669. /**
  1670. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1671. * @kcontrol: mixer control
  1672. * @ucontrol: control element information
  1673. *
  1674. * Returns 0 for success.
  1675. */
  1676. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1677. struct snd_ctl_elem_value *ucontrol)
  1678. {
  1679. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1680. ucontrol->value.enumerated.item[0] = widget->value;
  1681. return 0;
  1682. }
  1683. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1684. /**
  1685. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  1686. * @kcontrol: mixer control
  1687. * @ucontrol: control element information
  1688. *
  1689. * Returns 0 for success.
  1690. */
  1691. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  1692. struct snd_ctl_elem_value *ucontrol)
  1693. {
  1694. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1695. struct soc_enum *e =
  1696. (struct soc_enum *)kcontrol->private_value;
  1697. int change;
  1698. int ret = 0;
  1699. if (ucontrol->value.enumerated.item[0] >= e->max)
  1700. return -EINVAL;
  1701. mutex_lock(&widget->codec->mutex);
  1702. change = widget->value != ucontrol->value.enumerated.item[0];
  1703. widget->value = ucontrol->value.enumerated.item[0];
  1704. dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
  1705. mutex_unlock(&widget->codec->mutex);
  1706. return ret;
  1707. }
  1708. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  1709. /**
  1710. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1711. * callback
  1712. * @kcontrol: mixer control
  1713. * @ucontrol: control element information
  1714. *
  1715. * Callback to get the value of a dapm semi enumerated double mixer control.
  1716. *
  1717. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1718. * used for handling bitfield coded enumeration for example.
  1719. *
  1720. * Returns 0 for success.
  1721. */
  1722. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1723. struct snd_ctl_elem_value *ucontrol)
  1724. {
  1725. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1726. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1727. unsigned int reg_val, val, mux;
  1728. reg_val = snd_soc_read(widget->codec, e->reg);
  1729. val = (reg_val >> e->shift_l) & e->mask;
  1730. for (mux = 0; mux < e->max; mux++) {
  1731. if (val == e->values[mux])
  1732. break;
  1733. }
  1734. ucontrol->value.enumerated.item[0] = mux;
  1735. if (e->shift_l != e->shift_r) {
  1736. val = (reg_val >> e->shift_r) & e->mask;
  1737. for (mux = 0; mux < e->max; mux++) {
  1738. if (val == e->values[mux])
  1739. break;
  1740. }
  1741. ucontrol->value.enumerated.item[1] = mux;
  1742. }
  1743. return 0;
  1744. }
  1745. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1746. /**
  1747. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1748. * callback
  1749. * @kcontrol: mixer control
  1750. * @ucontrol: control element information
  1751. *
  1752. * Callback to set the value of a dapm semi enumerated double mixer control.
  1753. *
  1754. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1755. * used for handling bitfield coded enumeration for example.
  1756. *
  1757. * Returns 0 for success.
  1758. */
  1759. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1760. struct snd_ctl_elem_value *ucontrol)
  1761. {
  1762. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1763. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1764. unsigned int val, mux, change;
  1765. unsigned int mask;
  1766. struct snd_soc_dapm_update update;
  1767. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1768. return -EINVAL;
  1769. mux = ucontrol->value.enumerated.item[0];
  1770. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1771. mask = e->mask << e->shift_l;
  1772. if (e->shift_l != e->shift_r) {
  1773. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1774. return -EINVAL;
  1775. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1776. mask |= e->mask << e->shift_r;
  1777. }
  1778. mutex_lock(&widget->codec->mutex);
  1779. widget->value = val;
  1780. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1781. update.kcontrol = kcontrol;
  1782. update.widget = widget;
  1783. update.reg = e->reg;
  1784. update.mask = mask;
  1785. update.val = val;
  1786. widget->dapm->update = &update;
  1787. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1788. widget->dapm->update = NULL;
  1789. mutex_unlock(&widget->codec->mutex);
  1790. return change;
  1791. }
  1792. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1793. /**
  1794. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1795. *
  1796. * @kcontrol: mixer control
  1797. * @uinfo: control element information
  1798. *
  1799. * Callback to provide information about a pin switch control.
  1800. */
  1801. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1802. struct snd_ctl_elem_info *uinfo)
  1803. {
  1804. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1805. uinfo->count = 1;
  1806. uinfo->value.integer.min = 0;
  1807. uinfo->value.integer.max = 1;
  1808. return 0;
  1809. }
  1810. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1811. /**
  1812. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1813. *
  1814. * @kcontrol: mixer control
  1815. * @ucontrol: Value
  1816. */
  1817. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1818. struct snd_ctl_elem_value *ucontrol)
  1819. {
  1820. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1821. const char *pin = (const char *)kcontrol->private_value;
  1822. mutex_lock(&codec->mutex);
  1823. ucontrol->value.integer.value[0] =
  1824. snd_soc_dapm_get_pin_status(&codec->dapm, pin);
  1825. mutex_unlock(&codec->mutex);
  1826. return 0;
  1827. }
  1828. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1829. /**
  1830. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1831. *
  1832. * @kcontrol: mixer control
  1833. * @ucontrol: Value
  1834. */
  1835. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1836. struct snd_ctl_elem_value *ucontrol)
  1837. {
  1838. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1839. const char *pin = (const char *)kcontrol->private_value;
  1840. mutex_lock(&codec->mutex);
  1841. if (ucontrol->value.integer.value[0])
  1842. snd_soc_dapm_enable_pin(&codec->dapm, pin);
  1843. else
  1844. snd_soc_dapm_disable_pin(&codec->dapm, pin);
  1845. snd_soc_dapm_sync(&codec->dapm);
  1846. mutex_unlock(&codec->mutex);
  1847. return 0;
  1848. }
  1849. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1850. /**
  1851. * snd_soc_dapm_new_control - create new dapm control
  1852. * @dapm: DAPM context
  1853. * @widget: widget template
  1854. *
  1855. * Creates a new dapm control based upon the template.
  1856. *
  1857. * Returns 0 for success else error.
  1858. */
  1859. int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  1860. const struct snd_soc_dapm_widget *widget)
  1861. {
  1862. struct snd_soc_dapm_widget *w;
  1863. size_t name_len;
  1864. if ((w = dapm_cnew_widget(widget)) == NULL)
  1865. return -ENOMEM;
  1866. name_len = strlen(widget->name) + 1;
  1867. if (dapm->codec->name_prefix)
  1868. name_len += 1 + strlen(dapm->codec->name_prefix);
  1869. w->name = kmalloc(name_len, GFP_KERNEL);
  1870. if (w->name == NULL) {
  1871. kfree(w);
  1872. return -ENOMEM;
  1873. }
  1874. if (dapm->codec->name_prefix)
  1875. snprintf(w->name, name_len, "%s %s",
  1876. dapm->codec->name_prefix, widget->name);
  1877. else
  1878. snprintf(w->name, name_len, "%s", widget->name);
  1879. dapm->n_widgets++;
  1880. w->dapm = dapm;
  1881. w->codec = dapm->codec;
  1882. INIT_LIST_HEAD(&w->sources);
  1883. INIT_LIST_HEAD(&w->sinks);
  1884. INIT_LIST_HEAD(&w->list);
  1885. list_add(&w->list, &dapm->card->widgets);
  1886. /* machine layer set ups unconnected pins and insertions */
  1887. w->connected = 1;
  1888. return 0;
  1889. }
  1890. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1891. /**
  1892. * snd_soc_dapm_new_controls - create new dapm controls
  1893. * @dapm: DAPM context
  1894. * @widget: widget array
  1895. * @num: number of widgets
  1896. *
  1897. * Creates new DAPM controls based upon the templates.
  1898. *
  1899. * Returns 0 for success else error.
  1900. */
  1901. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  1902. const struct snd_soc_dapm_widget *widget,
  1903. int num)
  1904. {
  1905. int i, ret;
  1906. for (i = 0; i < num; i++) {
  1907. ret = snd_soc_dapm_new_control(dapm, widget);
  1908. if (ret < 0) {
  1909. dev_err(dapm->dev,
  1910. "ASoC: Failed to create DAPM control %s: %d\n",
  1911. widget->name, ret);
  1912. return ret;
  1913. }
  1914. widget++;
  1915. }
  1916. return 0;
  1917. }
  1918. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1919. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  1920. const char *stream, int event)
  1921. {
  1922. struct snd_soc_dapm_widget *w;
  1923. list_for_each_entry(w, &dapm->card->widgets, list)
  1924. {
  1925. if (!w->sname || w->dapm != dapm)
  1926. continue;
  1927. dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
  1928. w->name, w->sname, stream, event);
  1929. if (strstr(w->sname, stream)) {
  1930. switch(event) {
  1931. case SND_SOC_DAPM_STREAM_START:
  1932. w->active = 1;
  1933. break;
  1934. case SND_SOC_DAPM_STREAM_STOP:
  1935. w->active = 0;
  1936. break;
  1937. case SND_SOC_DAPM_STREAM_SUSPEND:
  1938. case SND_SOC_DAPM_STREAM_RESUME:
  1939. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1940. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1941. break;
  1942. }
  1943. }
  1944. }
  1945. dapm_power_widgets(dapm, event);
  1946. }
  1947. /**
  1948. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1949. * @rtd: PCM runtime data
  1950. * @stream: stream name
  1951. * @event: stream event
  1952. *
  1953. * Sends a stream event to the dapm core. The core then makes any
  1954. * necessary widget power changes.
  1955. *
  1956. * Returns 0 for success else error.
  1957. */
  1958. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
  1959. const char *stream, int event)
  1960. {
  1961. struct snd_soc_codec *codec = rtd->codec;
  1962. if (stream == NULL)
  1963. return 0;
  1964. mutex_lock(&codec->mutex);
  1965. soc_dapm_stream_event(&codec->dapm, stream, event);
  1966. mutex_unlock(&codec->mutex);
  1967. return 0;
  1968. }
  1969. /**
  1970. * snd_soc_dapm_enable_pin - enable pin.
  1971. * @dapm: DAPM context
  1972. * @pin: pin name
  1973. *
  1974. * Enables input/output pin and its parents or children widgets iff there is
  1975. * a valid audio route and active audio stream.
  1976. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1977. * do any widget power switching.
  1978. */
  1979. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  1980. {
  1981. return snd_soc_dapm_set_pin(dapm, pin, 1);
  1982. }
  1983. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1984. /**
  1985. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  1986. * @dapm: DAPM context
  1987. * @pin: pin name
  1988. *
  1989. * Enables input/output pin regardless of any other state. This is
  1990. * intended for use with microphone bias supplies used in microphone
  1991. * jack detection.
  1992. *
  1993. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1994. * do any widget power switching.
  1995. */
  1996. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  1997. const char *pin)
  1998. {
  1999. struct snd_soc_dapm_widget *w;
  2000. list_for_each_entry(w, &dapm->card->widgets, list) {
  2001. if (w->dapm != dapm)
  2002. continue;
  2003. if (!strcmp(w->name, pin)) {
  2004. dev_dbg(w->dapm->dev,
  2005. "dapm: force enable pin %s\n", pin);
  2006. w->connected = 1;
  2007. w->force = 1;
  2008. return 0;
  2009. }
  2010. }
  2011. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2012. return -EINVAL;
  2013. }
  2014. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  2015. /**
  2016. * snd_soc_dapm_disable_pin - disable pin.
  2017. * @dapm: DAPM context
  2018. * @pin: pin name
  2019. *
  2020. * Disables input/output pin and its parents or children widgets.
  2021. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2022. * do any widget power switching.
  2023. */
  2024. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  2025. const char *pin)
  2026. {
  2027. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2028. }
  2029. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  2030. /**
  2031. * snd_soc_dapm_nc_pin - permanently disable pin.
  2032. * @dapm: DAPM context
  2033. * @pin: pin name
  2034. *
  2035. * Marks the specified pin as being not connected, disabling it along
  2036. * any parent or child widgets. At present this is identical to
  2037. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  2038. * additional things such as disabling controls which only affect
  2039. * paths through the pin.
  2040. *
  2041. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2042. * do any widget power switching.
  2043. */
  2044. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2045. {
  2046. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2047. }
  2048. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  2049. /**
  2050. * snd_soc_dapm_get_pin_status - get audio pin status
  2051. * @dapm: DAPM context
  2052. * @pin: audio signal pin endpoint (or start point)
  2053. *
  2054. * Get audio pin status - connected or disconnected.
  2055. *
  2056. * Returns 1 for connected otherwise 0.
  2057. */
  2058. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  2059. const char *pin)
  2060. {
  2061. struct snd_soc_dapm_widget *w;
  2062. list_for_each_entry(w, &dapm->card->widgets, list) {
  2063. if (w->dapm != dapm)
  2064. continue;
  2065. if (!strcmp(w->name, pin))
  2066. return w->connected;
  2067. }
  2068. return 0;
  2069. }
  2070. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  2071. /**
  2072. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  2073. * @dapm: DAPM context
  2074. * @pin: audio signal pin endpoint (or start point)
  2075. *
  2076. * Mark the given endpoint or pin as ignoring suspend. When the
  2077. * system is disabled a path between two endpoints flagged as ignoring
  2078. * suspend will not be disabled. The path must already be enabled via
  2079. * normal means at suspend time, it will not be turned on if it was not
  2080. * already enabled.
  2081. */
  2082. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  2083. const char *pin)
  2084. {
  2085. struct snd_soc_dapm_widget *w;
  2086. list_for_each_entry(w, &dapm->card->widgets, list) {
  2087. if (w->dapm != dapm)
  2088. continue;
  2089. if (!strcmp(w->name, pin)) {
  2090. w->ignore_suspend = 1;
  2091. return 0;
  2092. }
  2093. }
  2094. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2095. return -EINVAL;
  2096. }
  2097. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  2098. /**
  2099. * snd_soc_dapm_free - free dapm resources
  2100. * @card: SoC device
  2101. *
  2102. * Free all dapm widgets and resources.
  2103. */
  2104. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  2105. {
  2106. snd_soc_dapm_sys_remove(dapm->dev);
  2107. dapm_free_widgets(dapm);
  2108. list_del(&dapm->list);
  2109. }
  2110. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  2111. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  2112. {
  2113. struct snd_soc_dapm_widget *w;
  2114. LIST_HEAD(down_list);
  2115. int powerdown = 0;
  2116. list_for_each_entry(w, &dapm->card->widgets, list) {
  2117. if (w->dapm != dapm)
  2118. continue;
  2119. if (w->power) {
  2120. dapm_seq_insert(w, &down_list, false);
  2121. w->power = 0;
  2122. powerdown = 1;
  2123. }
  2124. }
  2125. /* If there were no widgets to power down we're already in
  2126. * standby.
  2127. */
  2128. if (powerdown) {
  2129. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
  2130. dapm_seq_run(dapm, &down_list, 0, false);
  2131. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
  2132. }
  2133. }
  2134. /*
  2135. * snd_soc_dapm_shutdown - callback for system shutdown
  2136. */
  2137. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2138. {
  2139. struct snd_soc_codec *codec;
  2140. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2141. soc_dapm_shutdown_codec(&codec->dapm);
  2142. snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
  2143. }
  2144. }
  2145. /* Module information */
  2146. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2147. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2148. MODULE_LICENSE("GPL");