soc-dapm.c 60 KB

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