soc-dapm.c 93 KB

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