soc-dapm.c 94 KB

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