soc-dapm.c 69 KB

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