soc-dapm.c 56 KB

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