soc-dapm.c 79 KB

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