soc-dapm.c 69 KB

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