soc-dapm.c 93 KB

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