soc-dapm.c 55 KB

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