soc-dapm.c 55 KB

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