soc-dapm.c 92 KB

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