soc-dapm.c 59 KB

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