soc-dapm.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // soc-dapm.c -- ALSA SoC Dynamic Audio Power Management
  4. //
  5. // Copyright 2005 Wolfson Microelectronics PLC.
  6. // Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. //
  8. // Features:
  9. // o Changes power status of internal codec blocks depending on the
  10. // dynamic configuration of codec internal audio paths and active
  11. // DACs/ADCs.
  12. // o Platform power domain - can support external components i.e. amps and
  13. // mic/headphone insertion events.
  14. // o Automatic Mic Bias support
  15. // o Jack insertion power event initiation - e.g. hp insertion will enable
  16. // sinks, dacs, etc
  17. // o Delayed power down of audio subsystem to reduce pops between a quick
  18. // device reopen.
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/async.h>
  22. #include <linux/delay.h>
  23. #include <linux/pm.h>
  24. #include <linux/bitops.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/debugfs.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <linux/pinctrl/consumer.h>
  31. #include <linux/clk.h>
  32. #include <linux/slab.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/pcm_params.h>
  36. #include <sound/soc.h>
  37. #include <sound/initval.h>
  38. #include <trace/events/asoc.h>
  39. #define DAPM_UPDATE_STAT(widget, val) widget->dapm->card->dapm_stats.val++;
  40. #define SND_SOC_DAPM_DIR_REVERSE(x) ((x == SND_SOC_DAPM_DIR_IN) ? \
  41. SND_SOC_DAPM_DIR_OUT : SND_SOC_DAPM_DIR_IN)
  42. #define snd_soc_dapm_for_each_direction(dir) \
  43. for ((dir) = SND_SOC_DAPM_DIR_IN; (dir) <= SND_SOC_DAPM_DIR_OUT; \
  44. (dir)++)
  45. static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
  46. struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
  47. const char *control,
  48. int (*connected)(struct snd_soc_dapm_widget *source,
  49. struct snd_soc_dapm_widget *sink));
  50. struct snd_soc_dapm_widget *
  51. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  52. const struct snd_soc_dapm_widget *widget);
  53. struct snd_soc_dapm_widget *
  54. snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
  55. const struct snd_soc_dapm_widget *widget);
  56. /* dapm power sequences - make this per codec in the future */
  57. static int dapm_up_seq[] = {
  58. [snd_soc_dapm_pre] = 0,
  59. [snd_soc_dapm_regulator_supply] = 1,
  60. [snd_soc_dapm_pinctrl] = 1,
  61. [snd_soc_dapm_clock_supply] = 1,
  62. [snd_soc_dapm_supply] = 2,
  63. [snd_soc_dapm_micbias] = 3,
  64. [snd_soc_dapm_dai_link] = 2,
  65. [snd_soc_dapm_dai_in] = 4,
  66. [snd_soc_dapm_dai_out] = 4,
  67. [snd_soc_dapm_aif_in] = 4,
  68. [snd_soc_dapm_aif_out] = 4,
  69. [snd_soc_dapm_mic] = 5,
  70. [snd_soc_dapm_mux] = 6,
  71. [snd_soc_dapm_demux] = 6,
  72. [snd_soc_dapm_dac] = 7,
  73. [snd_soc_dapm_switch] = 8,
  74. [snd_soc_dapm_mixer] = 8,
  75. [snd_soc_dapm_mixer_named_ctl] = 8,
  76. [snd_soc_dapm_pga] = 9,
  77. [snd_soc_dapm_adc] = 10,
  78. [snd_soc_dapm_out_drv] = 11,
  79. [snd_soc_dapm_hp] = 11,
  80. [snd_soc_dapm_spk] = 11,
  81. [snd_soc_dapm_line] = 11,
  82. [snd_soc_dapm_kcontrol] = 12,
  83. [snd_soc_dapm_post] = 13,
  84. };
  85. static int dapm_down_seq[] = {
  86. [snd_soc_dapm_pre] = 0,
  87. [snd_soc_dapm_kcontrol] = 1,
  88. [snd_soc_dapm_adc] = 2,
  89. [snd_soc_dapm_hp] = 3,
  90. [snd_soc_dapm_spk] = 3,
  91. [snd_soc_dapm_line] = 3,
  92. [snd_soc_dapm_out_drv] = 3,
  93. [snd_soc_dapm_pga] = 4,
  94. [snd_soc_dapm_switch] = 5,
  95. [snd_soc_dapm_mixer_named_ctl] = 5,
  96. [snd_soc_dapm_mixer] = 5,
  97. [snd_soc_dapm_dac] = 6,
  98. [snd_soc_dapm_mic] = 7,
  99. [snd_soc_dapm_micbias] = 8,
  100. [snd_soc_dapm_mux] = 9,
  101. [snd_soc_dapm_demux] = 9,
  102. [snd_soc_dapm_aif_in] = 10,
  103. [snd_soc_dapm_aif_out] = 10,
  104. [snd_soc_dapm_dai_in] = 10,
  105. [snd_soc_dapm_dai_out] = 10,
  106. [snd_soc_dapm_dai_link] = 11,
  107. [snd_soc_dapm_supply] = 12,
  108. [snd_soc_dapm_clock_supply] = 13,
  109. [snd_soc_dapm_pinctrl] = 13,
  110. [snd_soc_dapm_regulator_supply] = 13,
  111. [snd_soc_dapm_post] = 14,
  112. };
  113. static void dapm_assert_locked(struct snd_soc_dapm_context *dapm)
  114. {
  115. if (dapm->card && dapm->card->instantiated)
  116. lockdep_assert_held(&dapm->card->dapm_mutex);
  117. }
  118. static void pop_wait(u32 pop_time)
  119. {
  120. if (pop_time)
  121. schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  122. }
  123. static void pop_dbg(struct device *dev, u32 pop_time, const char *fmt, ...)
  124. {
  125. va_list args;
  126. char *buf;
  127. if (!pop_time)
  128. return;
  129. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  130. if (buf == NULL)
  131. return;
  132. va_start(args, fmt);
  133. vsnprintf(buf, PAGE_SIZE, fmt, args);
  134. dev_info(dev, "%s", buf);
  135. va_end(args);
  136. kfree(buf);
  137. }
  138. static bool dapm_dirty_widget(struct snd_soc_dapm_widget *w)
  139. {
  140. return !list_empty(&w->dirty);
  141. }
  142. static void dapm_mark_dirty(struct snd_soc_dapm_widget *w, const char *reason)
  143. {
  144. dapm_assert_locked(w->dapm);
  145. if (!dapm_dirty_widget(w)) {
  146. dev_vdbg(w->dapm->dev, "Marking %s dirty due to %s\n",
  147. w->name, reason);
  148. list_add_tail(&w->dirty, &w->dapm->card->dapm_dirty);
  149. }
  150. }
  151. /*
  152. * Common implementation for dapm_widget_invalidate_input_paths() and
  153. * dapm_widget_invalidate_output_paths(). The function is inlined since the
  154. * combined size of the two specialized functions is only marginally larger then
  155. * the size of the generic function and at the same time the fast path of the
  156. * specialized functions is significantly smaller than the generic function.
  157. */
  158. static __always_inline void dapm_widget_invalidate_paths(
  159. struct snd_soc_dapm_widget *w, enum snd_soc_dapm_direction dir)
  160. {
  161. enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
  162. struct snd_soc_dapm_widget *node;
  163. struct snd_soc_dapm_path *p;
  164. LIST_HEAD(list);
  165. dapm_assert_locked(w->dapm);
  166. if (w->endpoints[dir] == -1)
  167. return;
  168. list_add_tail(&w->work_list, &list);
  169. w->endpoints[dir] = -1;
  170. list_for_each_entry(w, &list, work_list) {
  171. snd_soc_dapm_widget_for_each_path(w, dir, p) {
  172. if (p->is_supply || p->weak || !p->connect)
  173. continue;
  174. node = p->node[rdir];
  175. if (node->endpoints[dir] != -1) {
  176. node->endpoints[dir] = -1;
  177. list_add_tail(&node->work_list, &list);
  178. }
  179. }
  180. }
  181. }
  182. /*
  183. * dapm_widget_invalidate_input_paths() - Invalidate the cached number of
  184. * input paths
  185. * @w: The widget for which to invalidate the cached number of input paths
  186. *
  187. * Resets the cached number of inputs for the specified widget and all widgets
  188. * that can be reached via outcoming paths from the widget.
  189. *
  190. * This function must be called if the number of output paths for a widget might
  191. * have changed. E.g. if the source state of a widget changes or a path is added
  192. * or activated with the widget as the sink.
  193. */
  194. static void dapm_widget_invalidate_input_paths(struct snd_soc_dapm_widget *w)
  195. {
  196. dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_IN);
  197. }
  198. /*
  199. * dapm_widget_invalidate_output_paths() - Invalidate the cached number of
  200. * output paths
  201. * @w: The widget for which to invalidate the cached number of output paths
  202. *
  203. * Resets the cached number of outputs for the specified widget and all widgets
  204. * that can be reached via incoming paths from the widget.
  205. *
  206. * This function must be called if the number of output paths for a widget might
  207. * have changed. E.g. if the sink state of a widget changes or a path is added
  208. * or activated with the widget as the source.
  209. */
  210. static void dapm_widget_invalidate_output_paths(struct snd_soc_dapm_widget *w)
  211. {
  212. dapm_widget_invalidate_paths(w, SND_SOC_DAPM_DIR_OUT);
  213. }
  214. /*
  215. * dapm_path_invalidate() - Invalidates the cached number of inputs and outputs
  216. * for the widgets connected to a path
  217. * @p: The path to invalidate
  218. *
  219. * Resets the cached number of inputs for the sink of the path and the cached
  220. * number of outputs for the source of the path.
  221. *
  222. * This function must be called when a path is added, removed or the connected
  223. * state changes.
  224. */
  225. static void dapm_path_invalidate(struct snd_soc_dapm_path *p)
  226. {
  227. /*
  228. * Weak paths or supply paths do not influence the number of input or
  229. * output paths of their neighbors.
  230. */
  231. if (p->weak || p->is_supply)
  232. return;
  233. /*
  234. * The number of connected endpoints is the sum of the number of
  235. * connected endpoints of all neighbors. If a node with 0 connected
  236. * endpoints is either connected or disconnected that sum won't change,
  237. * so there is no need to re-check the path.
  238. */
  239. if (p->source->endpoints[SND_SOC_DAPM_DIR_IN] != 0)
  240. dapm_widget_invalidate_input_paths(p->sink);
  241. if (p->sink->endpoints[SND_SOC_DAPM_DIR_OUT] != 0)
  242. dapm_widget_invalidate_output_paths(p->source);
  243. }
  244. void dapm_mark_endpoints_dirty(struct snd_soc_card *card)
  245. {
  246. struct snd_soc_dapm_widget *w;
  247. mutex_lock(&card->dapm_mutex);
  248. list_for_each_entry(w, &card->widgets, list) {
  249. if (w->is_ep) {
  250. dapm_mark_dirty(w, "Rechecking endpoints");
  251. if (w->is_ep & SND_SOC_DAPM_EP_SINK)
  252. dapm_widget_invalidate_output_paths(w);
  253. if (w->is_ep & SND_SOC_DAPM_EP_SOURCE)
  254. dapm_widget_invalidate_input_paths(w);
  255. }
  256. }
  257. mutex_unlock(&card->dapm_mutex);
  258. }
  259. EXPORT_SYMBOL_GPL(dapm_mark_endpoints_dirty);
  260. /* create a new dapm widget */
  261. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  262. const struct snd_soc_dapm_widget *_widget)
  263. {
  264. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  265. }
  266. struct dapm_kcontrol_data {
  267. unsigned int value;
  268. struct snd_soc_dapm_widget *widget;
  269. struct list_head paths;
  270. struct snd_soc_dapm_widget_list *wlist;
  271. };
  272. static int dapm_kcontrol_data_alloc(struct snd_soc_dapm_widget *widget,
  273. struct snd_kcontrol *kcontrol, const char *ctrl_name)
  274. {
  275. struct dapm_kcontrol_data *data;
  276. struct soc_mixer_control *mc;
  277. struct soc_enum *e;
  278. const char *name;
  279. int ret;
  280. data = kzalloc(sizeof(*data), GFP_KERNEL);
  281. if (!data)
  282. return -ENOMEM;
  283. INIT_LIST_HEAD(&data->paths);
  284. switch (widget->id) {
  285. case snd_soc_dapm_switch:
  286. case snd_soc_dapm_mixer:
  287. case snd_soc_dapm_mixer_named_ctl:
  288. mc = (struct soc_mixer_control *)kcontrol->private_value;
  289. if (mc->autodisable && snd_soc_volsw_is_stereo(mc))
  290. dev_warn(widget->dapm->dev,
  291. "ASoC: Unsupported stereo autodisable control '%s'\n",
  292. ctrl_name);
  293. if (mc->autodisable) {
  294. struct snd_soc_dapm_widget template;
  295. name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
  296. "Autodisable");
  297. if (!name) {
  298. ret = -ENOMEM;
  299. goto err_data;
  300. }
  301. memset(&template, 0, sizeof(template));
  302. template.reg = mc->reg;
  303. template.mask = (1 << fls(mc->max)) - 1;
  304. template.shift = mc->shift;
  305. if (mc->invert)
  306. template.off_val = mc->max;
  307. else
  308. template.off_val = 0;
  309. template.on_val = template.off_val;
  310. template.id = snd_soc_dapm_kcontrol;
  311. template.name = name;
  312. data->value = template.on_val;
  313. data->widget =
  314. snd_soc_dapm_new_control_unlocked(widget->dapm,
  315. &template);
  316. kfree(name);
  317. if (IS_ERR(data->widget)) {
  318. ret = PTR_ERR(data->widget);
  319. goto err_data;
  320. }
  321. }
  322. break;
  323. case snd_soc_dapm_demux:
  324. case snd_soc_dapm_mux:
  325. e = (struct soc_enum *)kcontrol->private_value;
  326. if (e->autodisable) {
  327. struct snd_soc_dapm_widget template;
  328. name = kasprintf(GFP_KERNEL, "%s %s", ctrl_name,
  329. "Autodisable");
  330. if (!name) {
  331. ret = -ENOMEM;
  332. goto err_data;
  333. }
  334. memset(&template, 0, sizeof(template));
  335. template.reg = e->reg;
  336. template.mask = e->mask << e->shift_l;
  337. template.shift = e->shift_l;
  338. template.off_val = snd_soc_enum_item_to_val(e, 0);
  339. template.on_val = template.off_val;
  340. template.id = snd_soc_dapm_kcontrol;
  341. template.name = name;
  342. data->value = template.on_val;
  343. data->widget = snd_soc_dapm_new_control_unlocked(
  344. widget->dapm, &template);
  345. kfree(name);
  346. if (IS_ERR(data->widget)) {
  347. ret = PTR_ERR(data->widget);
  348. goto err_data;
  349. }
  350. snd_soc_dapm_add_path(widget->dapm, data->widget,
  351. widget, NULL, NULL);
  352. }
  353. break;
  354. default:
  355. break;
  356. }
  357. kcontrol->private_data = data;
  358. return 0;
  359. err_data:
  360. kfree(data);
  361. return ret;
  362. }
  363. static void dapm_kcontrol_free(struct snd_kcontrol *kctl)
  364. {
  365. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kctl);
  366. list_del(&data->paths);
  367. kfree(data->wlist);
  368. kfree(data);
  369. }
  370. static struct snd_soc_dapm_widget_list *dapm_kcontrol_get_wlist(
  371. const struct snd_kcontrol *kcontrol)
  372. {
  373. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  374. return data->wlist;
  375. }
  376. static int dapm_kcontrol_add_widget(struct snd_kcontrol *kcontrol,
  377. struct snd_soc_dapm_widget *widget)
  378. {
  379. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  380. struct snd_soc_dapm_widget_list *new_wlist;
  381. unsigned int n;
  382. if (data->wlist)
  383. n = data->wlist->num_widgets + 1;
  384. else
  385. n = 1;
  386. new_wlist = krealloc(data->wlist,
  387. sizeof(*new_wlist) + sizeof(widget) * n, GFP_KERNEL);
  388. if (!new_wlist)
  389. return -ENOMEM;
  390. new_wlist->widgets[n - 1] = widget;
  391. new_wlist->num_widgets = n;
  392. data->wlist = new_wlist;
  393. return 0;
  394. }
  395. static void dapm_kcontrol_add_path(const struct snd_kcontrol *kcontrol,
  396. struct snd_soc_dapm_path *path)
  397. {
  398. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  399. list_add_tail(&path->list_kcontrol, &data->paths);
  400. }
  401. static bool dapm_kcontrol_is_powered(const struct snd_kcontrol *kcontrol)
  402. {
  403. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  404. if (!data->widget)
  405. return true;
  406. return data->widget->power;
  407. }
  408. static struct list_head *dapm_kcontrol_get_path_list(
  409. const struct snd_kcontrol *kcontrol)
  410. {
  411. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  412. return &data->paths;
  413. }
  414. #define dapm_kcontrol_for_each_path(path, kcontrol) \
  415. list_for_each_entry(path, dapm_kcontrol_get_path_list(kcontrol), \
  416. list_kcontrol)
  417. unsigned int dapm_kcontrol_get_value(const struct snd_kcontrol *kcontrol)
  418. {
  419. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  420. return data->value;
  421. }
  422. EXPORT_SYMBOL_GPL(dapm_kcontrol_get_value);
  423. static bool dapm_kcontrol_set_value(const struct snd_kcontrol *kcontrol,
  424. unsigned int value)
  425. {
  426. struct dapm_kcontrol_data *data = snd_kcontrol_chip(kcontrol);
  427. if (data->value == value)
  428. return false;
  429. if (data->widget)
  430. data->widget->on_val = value;
  431. data->value = value;
  432. return true;
  433. }
  434. /**
  435. * snd_soc_dapm_kcontrol_widget() - Returns the widget associated to a
  436. * kcontrol
  437. * @kcontrol: The kcontrol
  438. */
  439. struct snd_soc_dapm_widget *snd_soc_dapm_kcontrol_widget(
  440. struct snd_kcontrol *kcontrol)
  441. {
  442. return dapm_kcontrol_get_wlist(kcontrol)->widgets[0];
  443. }
  444. EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_widget);
  445. /**
  446. * snd_soc_dapm_kcontrol_dapm() - Returns the dapm context associated to a
  447. * kcontrol
  448. * @kcontrol: The kcontrol
  449. *
  450. * Note: This function must only be used on kcontrols that are known to have
  451. * been registered for a CODEC. Otherwise the behaviour is undefined.
  452. */
  453. struct snd_soc_dapm_context *snd_soc_dapm_kcontrol_dapm(
  454. struct snd_kcontrol *kcontrol)
  455. {
  456. return dapm_kcontrol_get_wlist(kcontrol)->widgets[0]->dapm;
  457. }
  458. EXPORT_SYMBOL_GPL(snd_soc_dapm_kcontrol_dapm);
  459. static void dapm_reset(struct snd_soc_card *card)
  460. {
  461. struct snd_soc_dapm_widget *w;
  462. lockdep_assert_held(&card->dapm_mutex);
  463. memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
  464. list_for_each_entry(w, &card->widgets, list) {
  465. w->new_power = w->power;
  466. w->power_checked = false;
  467. }
  468. }
  469. static const char *soc_dapm_prefix(struct snd_soc_dapm_context *dapm)
  470. {
  471. if (!dapm->component)
  472. return NULL;
  473. return dapm->component->name_prefix;
  474. }
  475. static int soc_dapm_read(struct snd_soc_dapm_context *dapm, int reg,
  476. unsigned int *value)
  477. {
  478. if (!dapm->component)
  479. return -EIO;
  480. return snd_soc_component_read(dapm->component, reg, value);
  481. }
  482. static int soc_dapm_update_bits(struct snd_soc_dapm_context *dapm,
  483. int reg, unsigned int mask, unsigned int value)
  484. {
  485. if (!dapm->component)
  486. return -EIO;
  487. return snd_soc_component_update_bits(dapm->component, reg,
  488. mask, value);
  489. }
  490. static int soc_dapm_test_bits(struct snd_soc_dapm_context *dapm,
  491. int reg, unsigned int mask, unsigned int value)
  492. {
  493. if (!dapm->component)
  494. return -EIO;
  495. return snd_soc_component_test_bits(dapm->component, reg, mask, value);
  496. }
  497. static void soc_dapm_async_complete(struct snd_soc_dapm_context *dapm)
  498. {
  499. if (dapm->component)
  500. snd_soc_component_async_complete(dapm->component);
  501. }
  502. static struct snd_soc_dapm_widget *
  503. dapm_wcache_lookup(struct snd_soc_dapm_wcache *wcache, const char *name)
  504. {
  505. struct snd_soc_dapm_widget *w = wcache->widget;
  506. struct list_head *wlist;
  507. const int depth = 2;
  508. int i = 0;
  509. if (w) {
  510. wlist = &w->dapm->card->widgets;
  511. list_for_each_entry_from(w, wlist, list) {
  512. if (!strcmp(name, w->name))
  513. return w;
  514. if (++i == depth)
  515. break;
  516. }
  517. }
  518. return NULL;
  519. }
  520. static inline void dapm_wcache_update(struct snd_soc_dapm_wcache *wcache,
  521. struct snd_soc_dapm_widget *w)
  522. {
  523. wcache->widget = w;
  524. }
  525. /**
  526. * snd_soc_dapm_force_bias_level() - Sets the DAPM bias level
  527. * @dapm: The DAPM context for which to set the level
  528. * @level: The level to set
  529. *
  530. * Forces the DAPM bias level to a specific state. It will call the bias level
  531. * callback of DAPM context with the specified level. This will even happen if
  532. * the context is already at the same level. Furthermore it will not go through
  533. * the normal bias level sequencing, meaning any intermediate states between the
  534. * current and the target state will not be entered.
  535. *
  536. * Note that the change in bias level is only temporary and the next time
  537. * snd_soc_dapm_sync() is called the state will be set to the level as
  538. * determined by the DAPM core. The function is mainly intended to be used to
  539. * used during probe or resume from suspend to power up the device so
  540. * initialization can be done, before the DAPM core takes over.
  541. */
  542. int snd_soc_dapm_force_bias_level(struct snd_soc_dapm_context *dapm,
  543. enum snd_soc_bias_level level)
  544. {
  545. int ret = 0;
  546. if (dapm->set_bias_level)
  547. ret = dapm->set_bias_level(dapm, level);
  548. if (ret == 0)
  549. dapm->bias_level = level;
  550. return ret;
  551. }
  552. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_bias_level);
  553. /**
  554. * snd_soc_dapm_set_bias_level - set the bias level for the system
  555. * @dapm: DAPM context
  556. * @level: level to configure
  557. *
  558. * Configure the bias (power) levels for the SoC audio device.
  559. *
  560. * Returns 0 for success else error.
  561. */
  562. static int snd_soc_dapm_set_bias_level(struct snd_soc_dapm_context *dapm,
  563. enum snd_soc_bias_level level)
  564. {
  565. struct snd_soc_card *card = dapm->card;
  566. int ret = 0;
  567. trace_snd_soc_bias_level_start(card, level);
  568. if (card && card->set_bias_level)
  569. ret = card->set_bias_level(card, dapm, level);
  570. if (ret != 0)
  571. goto out;
  572. if (!card || dapm != &card->dapm)
  573. ret = snd_soc_dapm_force_bias_level(dapm, level);
  574. if (ret != 0)
  575. goto out;
  576. if (card && card->set_bias_level_post)
  577. ret = card->set_bias_level_post(card, dapm, level);
  578. out:
  579. trace_snd_soc_bias_level_done(card, level);
  580. return ret;
  581. }
  582. /* connect mux widget to its interconnecting audio paths */
  583. static int dapm_connect_mux(struct snd_soc_dapm_context *dapm,
  584. struct snd_soc_dapm_path *path, const char *control_name,
  585. struct snd_soc_dapm_widget *w)
  586. {
  587. const struct snd_kcontrol_new *kcontrol = &w->kcontrol_news[0];
  588. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  589. unsigned int val, item;
  590. int i;
  591. if (e->reg != SND_SOC_NOPM) {
  592. soc_dapm_read(dapm, e->reg, &val);
  593. val = (val >> e->shift_l) & e->mask;
  594. item = snd_soc_enum_val_to_item(e, val);
  595. } else {
  596. /* since a virtual mux has no backing registers to
  597. * decide which path to connect, it will try to match
  598. * with the first enumeration. This is to ensure
  599. * that the default mux choice (the first) will be
  600. * correctly powered up during initialization.
  601. */
  602. item = 0;
  603. }
  604. i = match_string(e->texts, e->items, control_name);
  605. if (i < 0)
  606. return -ENODEV;
  607. path->name = e->texts[i];
  608. path->connect = (i == item);
  609. return 0;
  610. }
  611. /* set up initial codec paths */
  612. static void dapm_set_mixer_path_status(struct snd_soc_dapm_path *p, int i,
  613. int nth_path)
  614. {
  615. struct soc_mixer_control *mc = (struct soc_mixer_control *)
  616. p->sink->kcontrol_news[i].private_value;
  617. unsigned int reg = mc->reg;
  618. unsigned int shift = mc->shift;
  619. unsigned int max = mc->max;
  620. unsigned int mask = (1 << fls(max)) - 1;
  621. unsigned int invert = mc->invert;
  622. unsigned int val;
  623. if (reg != SND_SOC_NOPM) {
  624. soc_dapm_read(p->sink->dapm, reg, &val);
  625. /*
  626. * The nth_path argument allows this function to know
  627. * which path of a kcontrol it is setting the initial
  628. * status for. Ideally this would support any number
  629. * of paths and channels. But since kcontrols only come
  630. * in mono and stereo variants, we are limited to 2
  631. * channels.
  632. *
  633. * The following code assumes for stereo controls the
  634. * first path is the left channel, and all remaining
  635. * paths are the right channel.
  636. */
  637. if (snd_soc_volsw_is_stereo(mc) && nth_path > 0) {
  638. if (reg != mc->rreg)
  639. soc_dapm_read(p->sink->dapm, mc->rreg, &val);
  640. val = (val >> mc->rshift) & mask;
  641. } else {
  642. val = (val >> shift) & mask;
  643. }
  644. if (invert)
  645. val = max - val;
  646. p->connect = !!val;
  647. } else {
  648. p->connect = 0;
  649. }
  650. }
  651. /* connect mixer widget to its interconnecting audio paths */
  652. static int dapm_connect_mixer(struct snd_soc_dapm_context *dapm,
  653. struct snd_soc_dapm_path *path, const char *control_name)
  654. {
  655. int i, nth_path = 0;
  656. /* search for mixer kcontrol */
  657. for (i = 0; i < path->sink->num_kcontrols; i++) {
  658. if (!strcmp(control_name, path->sink->kcontrol_news[i].name)) {
  659. path->name = path->sink->kcontrol_news[i].name;
  660. dapm_set_mixer_path_status(path, i, nth_path++);
  661. return 0;
  662. }
  663. }
  664. return -ENODEV;
  665. }
  666. static int dapm_is_shared_kcontrol(struct snd_soc_dapm_context *dapm,
  667. struct snd_soc_dapm_widget *kcontrolw,
  668. const struct snd_kcontrol_new *kcontrol_new,
  669. struct snd_kcontrol **kcontrol)
  670. {
  671. struct snd_soc_dapm_widget *w;
  672. int i;
  673. *kcontrol = NULL;
  674. list_for_each_entry(w, &dapm->card->widgets, list) {
  675. if (w == kcontrolw || w->dapm != kcontrolw->dapm)
  676. continue;
  677. for (i = 0; i < w->num_kcontrols; i++) {
  678. if (&w->kcontrol_news[i] == kcontrol_new) {
  679. if (w->kcontrols)
  680. *kcontrol = w->kcontrols[i];
  681. return 1;
  682. }
  683. }
  684. }
  685. return 0;
  686. }
  687. /*
  688. * Determine if a kcontrol is shared. If it is, look it up. If it isn't,
  689. * create it. Either way, add the widget into the control's widget list
  690. */
  691. static int dapm_create_or_share_kcontrol(struct snd_soc_dapm_widget *w,
  692. int kci)
  693. {
  694. struct snd_soc_dapm_context *dapm = w->dapm;
  695. struct snd_card *card = dapm->card->snd_card;
  696. const char *prefix;
  697. size_t prefix_len;
  698. int shared;
  699. struct snd_kcontrol *kcontrol;
  700. bool wname_in_long_name, kcname_in_long_name;
  701. char *long_name = NULL;
  702. const char *name;
  703. int ret = 0;
  704. prefix = soc_dapm_prefix(dapm);
  705. if (prefix)
  706. prefix_len = strlen(prefix) + 1;
  707. else
  708. prefix_len = 0;
  709. shared = dapm_is_shared_kcontrol(dapm, w, &w->kcontrol_news[kci],
  710. &kcontrol);
  711. if (!kcontrol) {
  712. if (shared) {
  713. wname_in_long_name = false;
  714. kcname_in_long_name = true;
  715. } else {
  716. switch (w->id) {
  717. case snd_soc_dapm_switch:
  718. case snd_soc_dapm_mixer:
  719. case snd_soc_dapm_pga:
  720. case snd_soc_dapm_out_drv:
  721. wname_in_long_name = true;
  722. kcname_in_long_name = true;
  723. break;
  724. case snd_soc_dapm_mixer_named_ctl:
  725. wname_in_long_name = false;
  726. kcname_in_long_name = true;
  727. break;
  728. case snd_soc_dapm_demux:
  729. case snd_soc_dapm_mux:
  730. wname_in_long_name = true;
  731. kcname_in_long_name = false;
  732. break;
  733. default:
  734. return -EINVAL;
  735. }
  736. }
  737. if (wname_in_long_name && kcname_in_long_name) {
  738. /*
  739. * The control will get a prefix from the control
  740. * creation process but we're also using the same
  741. * prefix for widgets so cut the prefix off the
  742. * front of the widget name.
  743. */
  744. long_name = kasprintf(GFP_KERNEL, "%s %s",
  745. w->name + prefix_len,
  746. w->kcontrol_news[kci].name);
  747. if (long_name == NULL)
  748. return -ENOMEM;
  749. name = long_name;
  750. } else if (wname_in_long_name) {
  751. long_name = NULL;
  752. name = w->name + prefix_len;
  753. } else {
  754. long_name = NULL;
  755. name = w->kcontrol_news[kci].name;
  756. }
  757. kcontrol = snd_soc_cnew(&w->kcontrol_news[kci], NULL, name,
  758. prefix);
  759. if (!kcontrol) {
  760. ret = -ENOMEM;
  761. goto exit_free;
  762. }
  763. kcontrol->private_free = dapm_kcontrol_free;
  764. ret = dapm_kcontrol_data_alloc(w, kcontrol, name);
  765. if (ret) {
  766. snd_ctl_free_one(kcontrol);
  767. goto exit_free;
  768. }
  769. ret = snd_ctl_add(card, kcontrol);
  770. if (ret < 0) {
  771. dev_err(dapm->dev,
  772. "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
  773. w->name, name, ret);
  774. goto exit_free;
  775. }
  776. }
  777. ret = dapm_kcontrol_add_widget(kcontrol, w);
  778. if (ret == 0)
  779. w->kcontrols[kci] = kcontrol;
  780. exit_free:
  781. kfree(long_name);
  782. return ret;
  783. }
  784. /* create new dapm mixer control */
  785. static int dapm_new_mixer(struct snd_soc_dapm_widget *w)
  786. {
  787. int i, ret;
  788. struct snd_soc_dapm_path *path;
  789. struct dapm_kcontrol_data *data;
  790. /* add kcontrol */
  791. for (i = 0; i < w->num_kcontrols; i++) {
  792. /* match name */
  793. snd_soc_dapm_widget_for_each_source_path(w, path) {
  794. /* mixer/mux paths name must match control name */
  795. if (path->name != (char *)w->kcontrol_news[i].name)
  796. continue;
  797. if (!w->kcontrols[i]) {
  798. ret = dapm_create_or_share_kcontrol(w, i);
  799. if (ret < 0)
  800. return ret;
  801. }
  802. dapm_kcontrol_add_path(w->kcontrols[i], path);
  803. data = snd_kcontrol_chip(w->kcontrols[i]);
  804. if (data->widget)
  805. snd_soc_dapm_add_path(data->widget->dapm,
  806. data->widget,
  807. path->source,
  808. NULL, NULL);
  809. }
  810. }
  811. return 0;
  812. }
  813. /* create new dapm mux control */
  814. static int dapm_new_mux(struct snd_soc_dapm_widget *w)
  815. {
  816. struct snd_soc_dapm_context *dapm = w->dapm;
  817. enum snd_soc_dapm_direction dir;
  818. struct snd_soc_dapm_path *path;
  819. const char *type;
  820. int ret;
  821. switch (w->id) {
  822. case snd_soc_dapm_mux:
  823. dir = SND_SOC_DAPM_DIR_OUT;
  824. type = "mux";
  825. break;
  826. case snd_soc_dapm_demux:
  827. dir = SND_SOC_DAPM_DIR_IN;
  828. type = "demux";
  829. break;
  830. default:
  831. return -EINVAL;
  832. }
  833. if (w->num_kcontrols != 1) {
  834. dev_err(dapm->dev,
  835. "ASoC: %s %s has incorrect number of controls\n", type,
  836. w->name);
  837. return -EINVAL;
  838. }
  839. if (list_empty(&w->edges[dir])) {
  840. dev_err(dapm->dev, "ASoC: %s %s has no paths\n", type, w->name);
  841. return -EINVAL;
  842. }
  843. ret = dapm_create_or_share_kcontrol(w, 0);
  844. if (ret < 0)
  845. return ret;
  846. snd_soc_dapm_widget_for_each_path(w, dir, path) {
  847. if (path->name)
  848. dapm_kcontrol_add_path(w->kcontrols[0], path);
  849. }
  850. return 0;
  851. }
  852. /* create new dapm volume control */
  853. static int dapm_new_pga(struct snd_soc_dapm_widget *w)
  854. {
  855. int i, ret;
  856. for (i = 0; i < w->num_kcontrols; i++) {
  857. ret = dapm_create_or_share_kcontrol(w, i);
  858. if (ret < 0)
  859. return ret;
  860. }
  861. return 0;
  862. }
  863. /* create new dapm dai link control */
  864. static int dapm_new_dai_link(struct snd_soc_dapm_widget *w)
  865. {
  866. int i, ret;
  867. struct snd_kcontrol *kcontrol;
  868. struct snd_soc_dapm_context *dapm = w->dapm;
  869. struct snd_card *card = dapm->card->snd_card;
  870. struct snd_soc_pcm_runtime *rtd = w->priv;
  871. /* create control for links with > 1 config */
  872. if (rtd->dai_link->num_params <= 1)
  873. return 0;
  874. /* add kcontrol */
  875. for (i = 0; i < w->num_kcontrols; i++) {
  876. kcontrol = snd_soc_cnew(&w->kcontrol_news[i], w,
  877. w->name, NULL);
  878. ret = snd_ctl_add(card, kcontrol);
  879. if (ret < 0) {
  880. dev_err(dapm->dev,
  881. "ASoC: failed to add widget %s dapm kcontrol %s: %d\n",
  882. w->name, w->kcontrol_news[i].name, ret);
  883. return ret;
  884. }
  885. kcontrol->private_data = w;
  886. w->kcontrols[i] = kcontrol;
  887. }
  888. return 0;
  889. }
  890. /* We implement power down on suspend by checking the power state of
  891. * the ALSA card - when we are suspending the ALSA state for the card
  892. * is set to D3.
  893. */
  894. static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
  895. {
  896. int level = snd_power_get_state(widget->dapm->card->snd_card);
  897. switch (level) {
  898. case SNDRV_CTL_POWER_D3hot:
  899. case SNDRV_CTL_POWER_D3cold:
  900. if (widget->ignore_suspend)
  901. dev_dbg(widget->dapm->dev, "ASoC: %s ignoring suspend\n",
  902. widget->name);
  903. return widget->ignore_suspend;
  904. default:
  905. return 1;
  906. }
  907. }
  908. static int dapm_widget_list_create(struct snd_soc_dapm_widget_list **list,
  909. struct list_head *widgets)
  910. {
  911. struct snd_soc_dapm_widget *w;
  912. struct list_head *it;
  913. unsigned int size = 0;
  914. unsigned int i = 0;
  915. list_for_each(it, widgets)
  916. size++;
  917. *list = kzalloc(struct_size(*list, widgets, size), GFP_KERNEL);
  918. if (*list == NULL)
  919. return -ENOMEM;
  920. list_for_each_entry(w, widgets, work_list)
  921. (*list)->widgets[i++] = w;
  922. (*list)->num_widgets = i;
  923. return 0;
  924. }
  925. /*
  926. * Common implementation for is_connected_output_ep() and
  927. * is_connected_input_ep(). The function is inlined since the combined size of
  928. * the two specialized functions is only marginally larger then the size of the
  929. * generic function and at the same time the fast path of the specialized
  930. * functions is significantly smaller than the generic function.
  931. */
  932. static __always_inline int is_connected_ep(struct snd_soc_dapm_widget *widget,
  933. struct list_head *list, enum snd_soc_dapm_direction dir,
  934. int (*fn)(struct snd_soc_dapm_widget *, struct list_head *,
  935. bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
  936. enum snd_soc_dapm_direction)),
  937. bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
  938. enum snd_soc_dapm_direction))
  939. {
  940. enum snd_soc_dapm_direction rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
  941. struct snd_soc_dapm_path *path;
  942. int con = 0;
  943. if (widget->endpoints[dir] >= 0)
  944. return widget->endpoints[dir];
  945. DAPM_UPDATE_STAT(widget, path_checks);
  946. /* do we need to add this widget to the list ? */
  947. if (list)
  948. list_add_tail(&widget->work_list, list);
  949. if (custom_stop_condition && custom_stop_condition(widget, dir)) {
  950. widget->endpoints[dir] = 1;
  951. return widget->endpoints[dir];
  952. }
  953. if ((widget->is_ep & SND_SOC_DAPM_DIR_TO_EP(dir)) && widget->connected) {
  954. widget->endpoints[dir] = snd_soc_dapm_suspend_check(widget);
  955. return widget->endpoints[dir];
  956. }
  957. snd_soc_dapm_widget_for_each_path(widget, rdir, path) {
  958. DAPM_UPDATE_STAT(widget, neighbour_checks);
  959. if (path->weak || path->is_supply)
  960. continue;
  961. if (path->walking)
  962. return 1;
  963. trace_snd_soc_dapm_path(widget, dir, path);
  964. if (path->connect) {
  965. path->walking = 1;
  966. con += fn(path->node[dir], list, custom_stop_condition);
  967. path->walking = 0;
  968. }
  969. }
  970. widget->endpoints[dir] = con;
  971. return con;
  972. }
  973. /*
  974. * Recursively check for a completed path to an active or physically connected
  975. * output widget. Returns number of complete paths.
  976. *
  977. * Optionally, can be supplied with a function acting as a stopping condition.
  978. * This function takes the dapm widget currently being examined and the walk
  979. * direction as an arguments, it should return true if the walk should be
  980. * stopped and false otherwise.
  981. */
  982. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget,
  983. struct list_head *list,
  984. bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
  985. enum snd_soc_dapm_direction))
  986. {
  987. return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_OUT,
  988. is_connected_output_ep, custom_stop_condition);
  989. }
  990. /*
  991. * Recursively check for a completed path to an active or physically connected
  992. * input widget. Returns number of complete paths.
  993. *
  994. * Optionally, can be supplied with a function acting as a stopping condition.
  995. * This function takes the dapm widget currently being examined and the walk
  996. * direction as an arguments, it should return true if the walk should be
  997. * stopped and false otherwise.
  998. */
  999. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget,
  1000. struct list_head *list,
  1001. bool (*custom_stop_condition)(struct snd_soc_dapm_widget *i,
  1002. enum snd_soc_dapm_direction))
  1003. {
  1004. return is_connected_ep(widget, list, SND_SOC_DAPM_DIR_IN,
  1005. is_connected_input_ep, custom_stop_condition);
  1006. }
  1007. /**
  1008. * snd_soc_dapm_get_connected_widgets - query audio path and it's widgets.
  1009. * @dai: the soc DAI.
  1010. * @stream: stream direction.
  1011. * @list: list of active widgets for this stream.
  1012. * @custom_stop_condition: (optional) a function meant to stop the widget graph
  1013. * walk based on custom logic.
  1014. *
  1015. * Queries DAPM graph as to whether a valid audio stream path exists for
  1016. * the initial stream specified by name. This takes into account
  1017. * current mixer and mux kcontrol settings. Creates list of valid widgets.
  1018. *
  1019. * Optionally, can be supplied with a function acting as a stopping condition.
  1020. * This function takes the dapm widget currently being examined and the walk
  1021. * direction as an arguments, it should return true if the walk should be
  1022. * stopped and false otherwise.
  1023. *
  1024. * Returns the number of valid paths or negative error.
  1025. */
  1026. int snd_soc_dapm_dai_get_connected_widgets(struct snd_soc_dai *dai, int stream,
  1027. struct snd_soc_dapm_widget_list **list,
  1028. bool (*custom_stop_condition)(struct snd_soc_dapm_widget *,
  1029. enum snd_soc_dapm_direction))
  1030. {
  1031. struct snd_soc_card *card = dai->component->card;
  1032. struct snd_soc_dapm_widget *w;
  1033. LIST_HEAD(widgets);
  1034. int paths;
  1035. int ret;
  1036. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1037. /*
  1038. * For is_connected_{output,input}_ep fully discover the graph we need
  1039. * to reset the cached number of inputs and outputs.
  1040. */
  1041. list_for_each_entry(w, &card->widgets, list) {
  1042. w->endpoints[SND_SOC_DAPM_DIR_IN] = -1;
  1043. w->endpoints[SND_SOC_DAPM_DIR_OUT] = -1;
  1044. }
  1045. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  1046. paths = is_connected_output_ep(dai->playback_widget, &widgets,
  1047. custom_stop_condition);
  1048. else
  1049. paths = is_connected_input_ep(dai->capture_widget, &widgets,
  1050. custom_stop_condition);
  1051. /* Drop starting point */
  1052. list_del(widgets.next);
  1053. ret = dapm_widget_list_create(list, &widgets);
  1054. if (ret)
  1055. paths = ret;
  1056. trace_snd_soc_dapm_connected(paths, stream);
  1057. mutex_unlock(&card->dapm_mutex);
  1058. return paths;
  1059. }
  1060. /*
  1061. * Handler for regulator supply widget.
  1062. */
  1063. int dapm_regulator_event(struct snd_soc_dapm_widget *w,
  1064. struct snd_kcontrol *kcontrol, int event)
  1065. {
  1066. int ret;
  1067. soc_dapm_async_complete(w->dapm);
  1068. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1069. if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
  1070. ret = regulator_allow_bypass(w->regulator, false);
  1071. if (ret != 0)
  1072. dev_warn(w->dapm->dev,
  1073. "ASoC: Failed to unbypass %s: %d\n",
  1074. w->name, ret);
  1075. }
  1076. return regulator_enable(w->regulator);
  1077. } else {
  1078. if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
  1079. ret = regulator_allow_bypass(w->regulator, true);
  1080. if (ret != 0)
  1081. dev_warn(w->dapm->dev,
  1082. "ASoC: Failed to bypass %s: %d\n",
  1083. w->name, ret);
  1084. }
  1085. return regulator_disable_deferred(w->regulator, w->shift);
  1086. }
  1087. }
  1088. EXPORT_SYMBOL_GPL(dapm_regulator_event);
  1089. /*
  1090. * Handler for pinctrl widget.
  1091. */
  1092. int dapm_pinctrl_event(struct snd_soc_dapm_widget *w,
  1093. struct snd_kcontrol *kcontrol, int event)
  1094. {
  1095. struct snd_soc_dapm_pinctrl_priv *priv = w->priv;
  1096. struct pinctrl *p = w->pinctrl;
  1097. struct pinctrl_state *s;
  1098. if (!p || !priv)
  1099. return -EIO;
  1100. if (SND_SOC_DAPM_EVENT_ON(event))
  1101. s = pinctrl_lookup_state(p, priv->active_state);
  1102. else
  1103. s = pinctrl_lookup_state(p, priv->sleep_state);
  1104. if (IS_ERR(s))
  1105. return PTR_ERR(s);
  1106. return pinctrl_select_state(p, s);
  1107. }
  1108. EXPORT_SYMBOL_GPL(dapm_pinctrl_event);
  1109. /*
  1110. * Handler for clock supply widget.
  1111. */
  1112. int dapm_clock_event(struct snd_soc_dapm_widget *w,
  1113. struct snd_kcontrol *kcontrol, int event)
  1114. {
  1115. if (!w->clk)
  1116. return -EIO;
  1117. soc_dapm_async_complete(w->dapm);
  1118. if (SND_SOC_DAPM_EVENT_ON(event)) {
  1119. return clk_prepare_enable(w->clk);
  1120. } else {
  1121. clk_disable_unprepare(w->clk);
  1122. return 0;
  1123. }
  1124. return 0;
  1125. }
  1126. EXPORT_SYMBOL_GPL(dapm_clock_event);
  1127. static int dapm_widget_power_check(struct snd_soc_dapm_widget *w)
  1128. {
  1129. if (w->power_checked)
  1130. return w->new_power;
  1131. if (w->force)
  1132. w->new_power = 1;
  1133. else
  1134. w->new_power = w->power_check(w);
  1135. w->power_checked = true;
  1136. return w->new_power;
  1137. }
  1138. /* Generic check to see if a widget should be powered. */
  1139. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  1140. {
  1141. int in, out;
  1142. DAPM_UPDATE_STAT(w, power_checks);
  1143. in = is_connected_input_ep(w, NULL, NULL);
  1144. out = is_connected_output_ep(w, NULL, NULL);
  1145. return out != 0 && in != 0;
  1146. }
  1147. /* Check to see if a power supply is needed */
  1148. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  1149. {
  1150. struct snd_soc_dapm_path *path;
  1151. DAPM_UPDATE_STAT(w, power_checks);
  1152. /* Check if one of our outputs is connected */
  1153. snd_soc_dapm_widget_for_each_sink_path(w, path) {
  1154. DAPM_UPDATE_STAT(w, neighbour_checks);
  1155. if (path->weak)
  1156. continue;
  1157. if (path->connected &&
  1158. !path->connected(path->source, path->sink))
  1159. continue;
  1160. if (dapm_widget_power_check(path->sink))
  1161. return 1;
  1162. }
  1163. return 0;
  1164. }
  1165. static int dapm_always_on_check_power(struct snd_soc_dapm_widget *w)
  1166. {
  1167. return w->connected;
  1168. }
  1169. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  1170. struct snd_soc_dapm_widget *b,
  1171. bool power_up)
  1172. {
  1173. int *sort;
  1174. if (power_up)
  1175. sort = dapm_up_seq;
  1176. else
  1177. sort = dapm_down_seq;
  1178. if (sort[a->id] != sort[b->id])
  1179. return sort[a->id] - sort[b->id];
  1180. if (a->subseq != b->subseq) {
  1181. if (power_up)
  1182. return a->subseq - b->subseq;
  1183. else
  1184. return b->subseq - a->subseq;
  1185. }
  1186. if (a->reg != b->reg)
  1187. return a->reg - b->reg;
  1188. if (a->dapm != b->dapm)
  1189. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  1190. return 0;
  1191. }
  1192. /* Insert a widget in order into a DAPM power sequence. */
  1193. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  1194. struct list_head *list,
  1195. bool power_up)
  1196. {
  1197. struct snd_soc_dapm_widget *w;
  1198. list_for_each_entry(w, list, power_list)
  1199. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  1200. list_add_tail(&new_widget->power_list, &w->power_list);
  1201. return;
  1202. }
  1203. list_add_tail(&new_widget->power_list, list);
  1204. }
  1205. static void dapm_seq_check_event(struct snd_soc_card *card,
  1206. struct snd_soc_dapm_widget *w, int event)
  1207. {
  1208. const char *ev_name;
  1209. int power, ret;
  1210. switch (event) {
  1211. case SND_SOC_DAPM_PRE_PMU:
  1212. ev_name = "PRE_PMU";
  1213. power = 1;
  1214. break;
  1215. case SND_SOC_DAPM_POST_PMU:
  1216. ev_name = "POST_PMU";
  1217. power = 1;
  1218. break;
  1219. case SND_SOC_DAPM_PRE_PMD:
  1220. ev_name = "PRE_PMD";
  1221. power = 0;
  1222. break;
  1223. case SND_SOC_DAPM_POST_PMD:
  1224. ev_name = "POST_PMD";
  1225. power = 0;
  1226. break;
  1227. case SND_SOC_DAPM_WILL_PMU:
  1228. ev_name = "WILL_PMU";
  1229. power = 1;
  1230. break;
  1231. case SND_SOC_DAPM_WILL_PMD:
  1232. ev_name = "WILL_PMD";
  1233. power = 0;
  1234. break;
  1235. default:
  1236. WARN(1, "Unknown event %d\n", event);
  1237. return;
  1238. }
  1239. if (w->new_power != power)
  1240. return;
  1241. if (w->event && (w->event_flags & event)) {
  1242. pop_dbg(w->dapm->dev, card->pop_time, "pop test : %s %s\n",
  1243. w->name, ev_name);
  1244. soc_dapm_async_complete(w->dapm);
  1245. trace_snd_soc_dapm_widget_event_start(w, event);
  1246. ret = w->event(w, NULL, event);
  1247. trace_snd_soc_dapm_widget_event_done(w, event);
  1248. if (ret < 0)
  1249. dev_err(w->dapm->dev, "ASoC: %s: %s event failed: %d\n",
  1250. ev_name, w->name, ret);
  1251. }
  1252. }
  1253. /* Apply the coalesced changes from a DAPM sequence */
  1254. static void dapm_seq_run_coalesced(struct snd_soc_card *card,
  1255. struct list_head *pending)
  1256. {
  1257. struct snd_soc_dapm_context *dapm;
  1258. struct snd_soc_dapm_widget *w;
  1259. int reg;
  1260. unsigned int value = 0;
  1261. unsigned int mask = 0;
  1262. w = list_first_entry(pending, struct snd_soc_dapm_widget, power_list);
  1263. reg = w->reg;
  1264. dapm = w->dapm;
  1265. list_for_each_entry(w, pending, power_list) {
  1266. WARN_ON(reg != w->reg || dapm != w->dapm);
  1267. w->power = w->new_power;
  1268. mask |= w->mask << w->shift;
  1269. if (w->power)
  1270. value |= w->on_val << w->shift;
  1271. else
  1272. value |= w->off_val << w->shift;
  1273. pop_dbg(dapm->dev, card->pop_time,
  1274. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  1275. w->name, reg, value, mask);
  1276. /* Check for events */
  1277. dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMU);
  1278. dapm_seq_check_event(card, w, SND_SOC_DAPM_PRE_PMD);
  1279. }
  1280. if (reg >= 0) {
  1281. /* Any widget will do, they should all be updating the
  1282. * same register.
  1283. */
  1284. pop_dbg(dapm->dev, card->pop_time,
  1285. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  1286. value, mask, reg, card->pop_time);
  1287. pop_wait(card->pop_time);
  1288. soc_dapm_update_bits(dapm, reg, mask, value);
  1289. }
  1290. list_for_each_entry(w, pending, power_list) {
  1291. dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMU);
  1292. dapm_seq_check_event(card, w, SND_SOC_DAPM_POST_PMD);
  1293. }
  1294. }
  1295. /* Apply a DAPM power sequence.
  1296. *
  1297. * We walk over a pre-sorted list of widgets to apply power to. In
  1298. * order to minimise the number of writes to the device required
  1299. * multiple widgets will be updated in a single write where possible.
  1300. * Currently anything that requires more than a single write is not
  1301. * handled.
  1302. */
  1303. static void dapm_seq_run(struct snd_soc_card *card,
  1304. struct list_head *list, int event, bool power_up)
  1305. {
  1306. struct snd_soc_dapm_widget *w, *n;
  1307. struct snd_soc_dapm_context *d;
  1308. LIST_HEAD(pending);
  1309. int cur_sort = -1;
  1310. int cur_subseq = -1;
  1311. int cur_reg = SND_SOC_NOPM;
  1312. struct snd_soc_dapm_context *cur_dapm = NULL;
  1313. int ret, i;
  1314. int *sort;
  1315. if (power_up)
  1316. sort = dapm_up_seq;
  1317. else
  1318. sort = dapm_down_seq;
  1319. list_for_each_entry_safe(w, n, list, power_list) {
  1320. ret = 0;
  1321. /* Do we need to apply any queued changes? */
  1322. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  1323. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  1324. if (!list_empty(&pending))
  1325. dapm_seq_run_coalesced(card, &pending);
  1326. if (cur_dapm && cur_dapm->seq_notifier) {
  1327. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1328. if (sort[i] == cur_sort)
  1329. cur_dapm->seq_notifier(cur_dapm,
  1330. i,
  1331. cur_subseq);
  1332. }
  1333. if (cur_dapm && w->dapm != cur_dapm)
  1334. soc_dapm_async_complete(cur_dapm);
  1335. INIT_LIST_HEAD(&pending);
  1336. cur_sort = -1;
  1337. cur_subseq = INT_MIN;
  1338. cur_reg = SND_SOC_NOPM;
  1339. cur_dapm = NULL;
  1340. }
  1341. switch (w->id) {
  1342. case snd_soc_dapm_pre:
  1343. if (!w->event)
  1344. list_for_each_entry_safe_continue(w, n, list,
  1345. power_list);
  1346. if (event == SND_SOC_DAPM_STREAM_START)
  1347. ret = w->event(w,
  1348. NULL, SND_SOC_DAPM_PRE_PMU);
  1349. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1350. ret = w->event(w,
  1351. NULL, SND_SOC_DAPM_PRE_PMD);
  1352. break;
  1353. case snd_soc_dapm_post:
  1354. if (!w->event)
  1355. list_for_each_entry_safe_continue(w, n, list,
  1356. power_list);
  1357. if (event == SND_SOC_DAPM_STREAM_START)
  1358. ret = w->event(w,
  1359. NULL, SND_SOC_DAPM_POST_PMU);
  1360. else if (event == SND_SOC_DAPM_STREAM_STOP)
  1361. ret = w->event(w,
  1362. NULL, SND_SOC_DAPM_POST_PMD);
  1363. break;
  1364. default:
  1365. /* Queue it up for application */
  1366. cur_sort = sort[w->id];
  1367. cur_subseq = w->subseq;
  1368. cur_reg = w->reg;
  1369. cur_dapm = w->dapm;
  1370. list_move(&w->power_list, &pending);
  1371. break;
  1372. }
  1373. if (ret < 0)
  1374. dev_err(w->dapm->dev,
  1375. "ASoC: Failed to apply widget power: %d\n", ret);
  1376. }
  1377. if (!list_empty(&pending))
  1378. dapm_seq_run_coalesced(card, &pending);
  1379. if (cur_dapm && cur_dapm->seq_notifier) {
  1380. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  1381. if (sort[i] == cur_sort)
  1382. cur_dapm->seq_notifier(cur_dapm,
  1383. i, cur_subseq);
  1384. }
  1385. list_for_each_entry(d, &card->dapm_list, list) {
  1386. soc_dapm_async_complete(d);
  1387. }
  1388. }
  1389. static void dapm_widget_update(struct snd_soc_card *card)
  1390. {
  1391. struct snd_soc_dapm_update *update = card->update;
  1392. struct snd_soc_dapm_widget_list *wlist;
  1393. struct snd_soc_dapm_widget *w = NULL;
  1394. unsigned int wi;
  1395. int ret;
  1396. if (!update || !dapm_kcontrol_is_powered(update->kcontrol))
  1397. return;
  1398. wlist = dapm_kcontrol_get_wlist(update->kcontrol);
  1399. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1400. w = wlist->widgets[wi];
  1401. if (w->event && (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  1402. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  1403. if (ret != 0)
  1404. dev_err(w->dapm->dev, "ASoC: %s DAPM pre-event failed: %d\n",
  1405. w->name, ret);
  1406. }
  1407. }
  1408. if (!w)
  1409. return;
  1410. ret = soc_dapm_update_bits(w->dapm, update->reg, update->mask,
  1411. update->val);
  1412. if (ret < 0)
  1413. dev_err(w->dapm->dev, "ASoC: %s DAPM update failed: %d\n",
  1414. w->name, ret);
  1415. if (update->has_second_set) {
  1416. ret = soc_dapm_update_bits(w->dapm, update->reg2,
  1417. update->mask2, update->val2);
  1418. if (ret < 0)
  1419. dev_err(w->dapm->dev,
  1420. "ASoC: %s DAPM update failed: %d\n",
  1421. w->name, ret);
  1422. }
  1423. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1424. w = wlist->widgets[wi];
  1425. if (w->event && (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  1426. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  1427. if (ret != 0)
  1428. dev_err(w->dapm->dev, "ASoC: %s DAPM post-event failed: %d\n",
  1429. w->name, ret);
  1430. }
  1431. }
  1432. }
  1433. /* Async callback run prior to DAPM sequences - brings to _PREPARE if
  1434. * they're changing state.
  1435. */
  1436. static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
  1437. {
  1438. struct snd_soc_dapm_context *d = data;
  1439. int ret;
  1440. /* If we're off and we're not supposed to go into STANDBY */
  1441. if (d->bias_level == SND_SOC_BIAS_OFF &&
  1442. d->target_bias_level != SND_SOC_BIAS_OFF) {
  1443. if (d->dev)
  1444. pm_runtime_get_sync(d->dev);
  1445. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1446. if (ret != 0)
  1447. dev_err(d->dev,
  1448. "ASoC: Failed to turn on bias: %d\n", ret);
  1449. }
  1450. /* Prepare for a transition to ON or away from ON */
  1451. if ((d->target_bias_level == SND_SOC_BIAS_ON &&
  1452. d->bias_level != SND_SOC_BIAS_ON) ||
  1453. (d->target_bias_level != SND_SOC_BIAS_ON &&
  1454. d->bias_level == SND_SOC_BIAS_ON)) {
  1455. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
  1456. if (ret != 0)
  1457. dev_err(d->dev,
  1458. "ASoC: Failed to prepare bias: %d\n", ret);
  1459. }
  1460. }
  1461. /* Async callback run prior to DAPM sequences - brings to their final
  1462. * state.
  1463. */
  1464. static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
  1465. {
  1466. struct snd_soc_dapm_context *d = data;
  1467. int ret;
  1468. /* If we just powered the last thing off drop to standby bias */
  1469. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1470. (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
  1471. d->target_bias_level == SND_SOC_BIAS_OFF)) {
  1472. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1473. if (ret != 0)
  1474. dev_err(d->dev, "ASoC: Failed to apply standby bias: %d\n",
  1475. ret);
  1476. }
  1477. /* If we're in standby and can support bias off then do that */
  1478. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  1479. d->target_bias_level == SND_SOC_BIAS_OFF) {
  1480. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
  1481. if (ret != 0)
  1482. dev_err(d->dev, "ASoC: Failed to turn off bias: %d\n",
  1483. ret);
  1484. if (d->dev)
  1485. pm_runtime_put(d->dev);
  1486. }
  1487. /* If we just powered up then move to active bias */
  1488. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1489. d->target_bias_level == SND_SOC_BIAS_ON) {
  1490. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
  1491. if (ret != 0)
  1492. dev_err(d->dev, "ASoC: Failed to apply active bias: %d\n",
  1493. ret);
  1494. }
  1495. }
  1496. static void dapm_widget_set_peer_power(struct snd_soc_dapm_widget *peer,
  1497. bool power, bool connect)
  1498. {
  1499. /* If a connection is being made or broken then that update
  1500. * will have marked the peer dirty, otherwise the widgets are
  1501. * not connected and this update has no impact. */
  1502. if (!connect)
  1503. return;
  1504. /* If the peer is already in the state we're moving to then we
  1505. * won't have an impact on it. */
  1506. if (power != peer->power)
  1507. dapm_mark_dirty(peer, "peer state change");
  1508. }
  1509. static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
  1510. struct list_head *up_list,
  1511. struct list_head *down_list)
  1512. {
  1513. struct snd_soc_dapm_path *path;
  1514. if (w->power == power)
  1515. return;
  1516. trace_snd_soc_dapm_widget_power(w, power);
  1517. /* If we changed our power state perhaps our neigbours changed
  1518. * also.
  1519. */
  1520. snd_soc_dapm_widget_for_each_source_path(w, path)
  1521. dapm_widget_set_peer_power(path->source, power, path->connect);
  1522. /* Supplies can't affect their outputs, only their inputs */
  1523. if (!w->is_supply) {
  1524. snd_soc_dapm_widget_for_each_sink_path(w, path)
  1525. dapm_widget_set_peer_power(path->sink, power,
  1526. path->connect);
  1527. }
  1528. if (power)
  1529. dapm_seq_insert(w, up_list, true);
  1530. else
  1531. dapm_seq_insert(w, down_list, false);
  1532. }
  1533. static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
  1534. struct list_head *up_list,
  1535. struct list_head *down_list)
  1536. {
  1537. int power;
  1538. switch (w->id) {
  1539. case snd_soc_dapm_pre:
  1540. dapm_seq_insert(w, down_list, false);
  1541. break;
  1542. case snd_soc_dapm_post:
  1543. dapm_seq_insert(w, up_list, true);
  1544. break;
  1545. default:
  1546. power = dapm_widget_power_check(w);
  1547. dapm_widget_set_power(w, power, up_list, down_list);
  1548. break;
  1549. }
  1550. }
  1551. static bool dapm_idle_bias_off(struct snd_soc_dapm_context *dapm)
  1552. {
  1553. if (dapm->idle_bias_off)
  1554. return true;
  1555. switch (snd_power_get_state(dapm->card->snd_card)) {
  1556. case SNDRV_CTL_POWER_D3hot:
  1557. case SNDRV_CTL_POWER_D3cold:
  1558. return dapm->suspend_bias_off;
  1559. default:
  1560. break;
  1561. }
  1562. return false;
  1563. }
  1564. /*
  1565. * Scan each dapm widget for complete audio path.
  1566. * A complete path is a route that has valid endpoints i.e.:-
  1567. *
  1568. * o DAC to output pin.
  1569. * o Input pin to ADC.
  1570. * o Input pin to Output pin (bypass, sidetone)
  1571. * o DAC to ADC (loopback).
  1572. */
  1573. static int dapm_power_widgets(struct snd_soc_card *card, int event)
  1574. {
  1575. struct snd_soc_dapm_widget *w;
  1576. struct snd_soc_dapm_context *d;
  1577. LIST_HEAD(up_list);
  1578. LIST_HEAD(down_list);
  1579. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  1580. enum snd_soc_bias_level bias;
  1581. lockdep_assert_held(&card->dapm_mutex);
  1582. trace_snd_soc_dapm_start(card);
  1583. list_for_each_entry(d, &card->dapm_list, list) {
  1584. if (dapm_idle_bias_off(d))
  1585. d->target_bias_level = SND_SOC_BIAS_OFF;
  1586. else
  1587. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1588. }
  1589. dapm_reset(card);
  1590. /* Check which widgets we need to power and store them in
  1591. * lists indicating if they should be powered up or down. We
  1592. * only check widgets that have been flagged as dirty but note
  1593. * that new widgets may be added to the dirty list while we
  1594. * iterate.
  1595. */
  1596. list_for_each_entry(w, &card->dapm_dirty, dirty) {
  1597. dapm_power_one_widget(w, &up_list, &down_list);
  1598. }
  1599. list_for_each_entry(w, &card->widgets, list) {
  1600. switch (w->id) {
  1601. case snd_soc_dapm_pre:
  1602. case snd_soc_dapm_post:
  1603. /* These widgets always need to be powered */
  1604. break;
  1605. default:
  1606. list_del_init(&w->dirty);
  1607. break;
  1608. }
  1609. if (w->new_power) {
  1610. d = w->dapm;
  1611. /* Supplies and micbiases only bring the
  1612. * context up to STANDBY as unless something
  1613. * else is active and passing audio they
  1614. * generally don't require full power. Signal
  1615. * generators are virtual pins and have no
  1616. * power impact themselves.
  1617. */
  1618. switch (w->id) {
  1619. case snd_soc_dapm_siggen:
  1620. case snd_soc_dapm_vmid:
  1621. break;
  1622. case snd_soc_dapm_supply:
  1623. case snd_soc_dapm_regulator_supply:
  1624. case snd_soc_dapm_pinctrl:
  1625. case snd_soc_dapm_clock_supply:
  1626. case snd_soc_dapm_micbias:
  1627. if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
  1628. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1629. break;
  1630. default:
  1631. d->target_bias_level = SND_SOC_BIAS_ON;
  1632. break;
  1633. }
  1634. }
  1635. }
  1636. /* Force all contexts in the card to the same bias state if
  1637. * they're not ground referenced.
  1638. */
  1639. bias = SND_SOC_BIAS_OFF;
  1640. list_for_each_entry(d, &card->dapm_list, list)
  1641. if (d->target_bias_level > bias)
  1642. bias = d->target_bias_level;
  1643. list_for_each_entry(d, &card->dapm_list, list)
  1644. if (!dapm_idle_bias_off(d))
  1645. d->target_bias_level = bias;
  1646. trace_snd_soc_dapm_walk_done(card);
  1647. /* Run card bias changes at first */
  1648. dapm_pre_sequence_async(&card->dapm, 0);
  1649. /* Run other bias changes in parallel */
  1650. list_for_each_entry(d, &card->dapm_list, list) {
  1651. if (d != &card->dapm && d->bias_level != d->target_bias_level)
  1652. async_schedule_domain(dapm_pre_sequence_async, d,
  1653. &async_domain);
  1654. }
  1655. async_synchronize_full_domain(&async_domain);
  1656. list_for_each_entry(w, &down_list, power_list) {
  1657. dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMD);
  1658. }
  1659. list_for_each_entry(w, &up_list, power_list) {
  1660. dapm_seq_check_event(card, w, SND_SOC_DAPM_WILL_PMU);
  1661. }
  1662. /* Power down widgets first; try to avoid amplifying pops. */
  1663. dapm_seq_run(card, &down_list, event, false);
  1664. dapm_widget_update(card);
  1665. /* Now power up. */
  1666. dapm_seq_run(card, &up_list, event, true);
  1667. /* Run all the bias changes in parallel */
  1668. list_for_each_entry(d, &card->dapm_list, list) {
  1669. if (d != &card->dapm && d->bias_level != d->target_bias_level)
  1670. async_schedule_domain(dapm_post_sequence_async, d,
  1671. &async_domain);
  1672. }
  1673. async_synchronize_full_domain(&async_domain);
  1674. /* Run card bias changes at last */
  1675. dapm_post_sequence_async(&card->dapm, 0);
  1676. /* do we need to notify any clients that DAPM event is complete */
  1677. list_for_each_entry(d, &card->dapm_list, list) {
  1678. if (d->stream_event)
  1679. d->stream_event(d, event);
  1680. }
  1681. pop_dbg(card->dev, card->pop_time,
  1682. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1683. pop_wait(card->pop_time);
  1684. trace_snd_soc_dapm_done(card);
  1685. return 0;
  1686. }
  1687. #ifdef CONFIG_DEBUG_FS
  1688. static ssize_t dapm_widget_power_read_file(struct file *file,
  1689. char __user *user_buf,
  1690. size_t count, loff_t *ppos)
  1691. {
  1692. struct snd_soc_dapm_widget *w = file->private_data;
  1693. struct snd_soc_card *card = w->dapm->card;
  1694. enum snd_soc_dapm_direction dir, rdir;
  1695. char *buf;
  1696. int in, out;
  1697. ssize_t ret;
  1698. struct snd_soc_dapm_path *p = NULL;
  1699. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1700. if (!buf)
  1701. return -ENOMEM;
  1702. mutex_lock(&card->dapm_mutex);
  1703. /* Supply widgets are not handled by is_connected_{input,output}_ep() */
  1704. if (w->is_supply) {
  1705. in = 0;
  1706. out = 0;
  1707. } else {
  1708. in = is_connected_input_ep(w, NULL, NULL);
  1709. out = is_connected_output_ep(w, NULL, NULL);
  1710. }
  1711. ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
  1712. w->name, w->power ? "On" : "Off",
  1713. w->force ? " (forced)" : "", in, out);
  1714. if (w->reg >= 0)
  1715. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1716. " - R%d(0x%x) mask 0x%x",
  1717. w->reg, w->reg, w->mask << w->shift);
  1718. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1719. if (w->sname)
  1720. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1721. w->sname,
  1722. w->active ? "active" : "inactive");
  1723. snd_soc_dapm_for_each_direction(dir) {
  1724. rdir = SND_SOC_DAPM_DIR_REVERSE(dir);
  1725. snd_soc_dapm_widget_for_each_path(w, dir, p) {
  1726. if (p->connected && !p->connected(p->source, p->sink))
  1727. continue;
  1728. if (!p->connect)
  1729. continue;
  1730. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1731. " %s \"%s\" \"%s\"\n",
  1732. (rdir == SND_SOC_DAPM_DIR_IN) ? "in" : "out",
  1733. p->name ? p->name : "static",
  1734. p->node[rdir]->name);
  1735. }
  1736. }
  1737. mutex_unlock(&card->dapm_mutex);
  1738. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1739. kfree(buf);
  1740. return ret;
  1741. }
  1742. static const struct file_operations dapm_widget_power_fops = {
  1743. .open = simple_open,
  1744. .read = dapm_widget_power_read_file,
  1745. .llseek = default_llseek,
  1746. };
  1747. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1748. size_t count, loff_t *ppos)
  1749. {
  1750. struct snd_soc_dapm_context *dapm = file->private_data;
  1751. char *level;
  1752. switch (dapm->bias_level) {
  1753. case SND_SOC_BIAS_ON:
  1754. level = "On\n";
  1755. break;
  1756. case SND_SOC_BIAS_PREPARE:
  1757. level = "Prepare\n";
  1758. break;
  1759. case SND_SOC_BIAS_STANDBY:
  1760. level = "Standby\n";
  1761. break;
  1762. case SND_SOC_BIAS_OFF:
  1763. level = "Off\n";
  1764. break;
  1765. default:
  1766. WARN(1, "Unknown bias_level %d\n", dapm->bias_level);
  1767. level = "Unknown\n";
  1768. break;
  1769. }
  1770. return simple_read_from_buffer(user_buf, count, ppos, level,
  1771. strlen(level));
  1772. }
  1773. static const struct file_operations dapm_bias_fops = {
  1774. .open = simple_open,
  1775. .read = dapm_bias_read_file,
  1776. .llseek = default_llseek,
  1777. };
  1778. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1779. struct dentry *parent)
  1780. {
  1781. struct dentry *d;
  1782. if (!parent)
  1783. return;
  1784. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1785. if (!dapm->debugfs_dapm) {
  1786. dev_warn(dapm->dev,
  1787. "ASoC: Failed to create DAPM debugfs directory\n");
  1788. return;
  1789. }
  1790. d = debugfs_create_file("bias_level", 0444,
  1791. dapm->debugfs_dapm, dapm,
  1792. &dapm_bias_fops);
  1793. if (!d)
  1794. dev_warn(dapm->dev,
  1795. "ASoC: Failed to create bias level debugfs file\n");
  1796. }
  1797. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1798. {
  1799. struct snd_soc_dapm_context *dapm = w->dapm;
  1800. struct dentry *d;
  1801. if (!dapm->debugfs_dapm || !w->name)
  1802. return;
  1803. d = debugfs_create_file(w->name, 0444,
  1804. dapm->debugfs_dapm, w,
  1805. &dapm_widget_power_fops);
  1806. if (!d)
  1807. dev_warn(w->dapm->dev,
  1808. "ASoC: Failed to create %s debugfs file\n",
  1809. w->name);
  1810. }
  1811. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1812. {
  1813. debugfs_remove_recursive(dapm->debugfs_dapm);
  1814. }
  1815. #else
  1816. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1817. struct dentry *parent)
  1818. {
  1819. }
  1820. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1821. {
  1822. }
  1823. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1824. {
  1825. }
  1826. #endif
  1827. /*
  1828. * soc_dapm_connect_path() - Connects or disconnects a path
  1829. * @path: The path to update
  1830. * @connect: The new connect state of the path. True if the path is connected,
  1831. * false if it is disconnected.
  1832. * @reason: The reason why the path changed (for debugging only)
  1833. */
  1834. static void soc_dapm_connect_path(struct snd_soc_dapm_path *path,
  1835. bool connect, const char *reason)
  1836. {
  1837. if (path->connect == connect)
  1838. return;
  1839. path->connect = connect;
  1840. dapm_mark_dirty(path->source, reason);
  1841. dapm_mark_dirty(path->sink, reason);
  1842. dapm_path_invalidate(path);
  1843. }
  1844. /* test and update the power status of a mux widget */
  1845. static int soc_dapm_mux_update_power(struct snd_soc_card *card,
  1846. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1847. {
  1848. struct snd_soc_dapm_path *path;
  1849. int found = 0;
  1850. bool connect;
  1851. lockdep_assert_held(&card->dapm_mutex);
  1852. /* find dapm widget path assoc with kcontrol */
  1853. dapm_kcontrol_for_each_path(path, kcontrol) {
  1854. found = 1;
  1855. /* we now need to match the string in the enum to the path */
  1856. if (!(strcmp(path->name, e->texts[mux])))
  1857. connect = true;
  1858. else
  1859. connect = false;
  1860. soc_dapm_connect_path(path, connect, "mux update");
  1861. }
  1862. if (found)
  1863. dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
  1864. return found;
  1865. }
  1866. int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_context *dapm,
  1867. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e,
  1868. struct snd_soc_dapm_update *update)
  1869. {
  1870. struct snd_soc_card *card = dapm->card;
  1871. int ret;
  1872. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1873. card->update = update;
  1874. ret = soc_dapm_mux_update_power(card, kcontrol, mux, e);
  1875. card->update = NULL;
  1876. mutex_unlock(&card->dapm_mutex);
  1877. if (ret > 0)
  1878. soc_dpcm_runtime_update(card);
  1879. return ret;
  1880. }
  1881. EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
  1882. /* test and update the power status of a mixer or switch widget */
  1883. static int soc_dapm_mixer_update_power(struct snd_soc_card *card,
  1884. struct snd_kcontrol *kcontrol,
  1885. int connect, int rconnect)
  1886. {
  1887. struct snd_soc_dapm_path *path;
  1888. int found = 0;
  1889. lockdep_assert_held(&card->dapm_mutex);
  1890. /* find dapm widget path assoc with kcontrol */
  1891. dapm_kcontrol_for_each_path(path, kcontrol) {
  1892. /*
  1893. * Ideally this function should support any number of
  1894. * paths and channels. But since kcontrols only come
  1895. * in mono and stereo variants, we are limited to 2
  1896. * channels.
  1897. *
  1898. * The following code assumes for stereo controls the
  1899. * first path (when 'found == 0') is the left channel,
  1900. * and all remaining paths (when 'found == 1') are the
  1901. * right channel.
  1902. *
  1903. * A stereo control is signified by a valid 'rconnect'
  1904. * value, either 0 for unconnected, or >= 0 for connected.
  1905. * This is chosen instead of using snd_soc_volsw_is_stereo,
  1906. * so that the behavior of snd_soc_dapm_mixer_update_power
  1907. * doesn't change even when the kcontrol passed in is
  1908. * stereo.
  1909. *
  1910. * It passes 'connect' as the path connect status for
  1911. * the left channel, and 'rconnect' for the right
  1912. * channel.
  1913. */
  1914. if (found && rconnect >= 0)
  1915. soc_dapm_connect_path(path, rconnect, "mixer update");
  1916. else
  1917. soc_dapm_connect_path(path, connect, "mixer update");
  1918. found = 1;
  1919. }
  1920. if (found)
  1921. dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
  1922. return found;
  1923. }
  1924. int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_context *dapm,
  1925. struct snd_kcontrol *kcontrol, int connect,
  1926. struct snd_soc_dapm_update *update)
  1927. {
  1928. struct snd_soc_card *card = dapm->card;
  1929. int ret;
  1930. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  1931. card->update = update;
  1932. ret = soc_dapm_mixer_update_power(card, kcontrol, connect, -1);
  1933. card->update = NULL;
  1934. mutex_unlock(&card->dapm_mutex);
  1935. if (ret > 0)
  1936. soc_dpcm_runtime_update(card);
  1937. return ret;
  1938. }
  1939. EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
  1940. static ssize_t dapm_widget_show_component(struct snd_soc_component *cmpnt,
  1941. char *buf)
  1942. {
  1943. struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(cmpnt);
  1944. struct snd_soc_dapm_widget *w;
  1945. int count = 0;
  1946. char *state = "not set";
  1947. /* card won't be set for the dummy component, as a spot fix
  1948. * we're checking for that case specifically here but in future
  1949. * we will ensure that the dummy component looks like others.
  1950. */
  1951. if (!cmpnt->card)
  1952. return 0;
  1953. list_for_each_entry(w, &cmpnt->card->widgets, list) {
  1954. if (w->dapm != dapm)
  1955. continue;
  1956. /* only display widgets that burn power */
  1957. switch (w->id) {
  1958. case snd_soc_dapm_hp:
  1959. case snd_soc_dapm_mic:
  1960. case snd_soc_dapm_spk:
  1961. case snd_soc_dapm_line:
  1962. case snd_soc_dapm_micbias:
  1963. case snd_soc_dapm_dac:
  1964. case snd_soc_dapm_adc:
  1965. case snd_soc_dapm_pga:
  1966. case snd_soc_dapm_out_drv:
  1967. case snd_soc_dapm_mixer:
  1968. case snd_soc_dapm_mixer_named_ctl:
  1969. case snd_soc_dapm_supply:
  1970. case snd_soc_dapm_regulator_supply:
  1971. case snd_soc_dapm_pinctrl:
  1972. case snd_soc_dapm_clock_supply:
  1973. if (w->name)
  1974. count += sprintf(buf + count, "%s: %s\n",
  1975. w->name, w->power ? "On":"Off");
  1976. break;
  1977. default:
  1978. break;
  1979. }
  1980. }
  1981. switch (snd_soc_dapm_get_bias_level(dapm)) {
  1982. case SND_SOC_BIAS_ON:
  1983. state = "On";
  1984. break;
  1985. case SND_SOC_BIAS_PREPARE:
  1986. state = "Prepare";
  1987. break;
  1988. case SND_SOC_BIAS_STANDBY:
  1989. state = "Standby";
  1990. break;
  1991. case SND_SOC_BIAS_OFF:
  1992. state = "Off";
  1993. break;
  1994. }
  1995. count += sprintf(buf + count, "PM State: %s\n", state);
  1996. return count;
  1997. }
  1998. /* show dapm widget status in sys fs */
  1999. static ssize_t dapm_widget_show(struct device *dev,
  2000. struct device_attribute *attr, char *buf)
  2001. {
  2002. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  2003. struct snd_soc_dai *codec_dai;
  2004. int i, count = 0;
  2005. mutex_lock(&rtd->card->dapm_mutex);
  2006. for_each_rtd_codec_dai(rtd, i, codec_dai) {
  2007. struct snd_soc_component *cmpnt = codec_dai->component;
  2008. count += dapm_widget_show_component(cmpnt, buf + count);
  2009. }
  2010. mutex_unlock(&rtd->card->dapm_mutex);
  2011. return count;
  2012. }
  2013. static DEVICE_ATTR_RO(dapm_widget);
  2014. struct attribute *soc_dapm_dev_attrs[] = {
  2015. &dev_attr_dapm_widget.attr,
  2016. NULL
  2017. };
  2018. static void dapm_free_path(struct snd_soc_dapm_path *path)
  2019. {
  2020. list_del(&path->list_node[SND_SOC_DAPM_DIR_IN]);
  2021. list_del(&path->list_node[SND_SOC_DAPM_DIR_OUT]);
  2022. list_del(&path->list_kcontrol);
  2023. list_del(&path->list);
  2024. kfree(path);
  2025. }
  2026. void snd_soc_dapm_free_widget(struct snd_soc_dapm_widget *w)
  2027. {
  2028. struct snd_soc_dapm_path *p, *next_p;
  2029. enum snd_soc_dapm_direction dir;
  2030. list_del(&w->list);
  2031. /*
  2032. * remove source and sink paths associated to this widget.
  2033. * While removing the path, remove reference to it from both
  2034. * source and sink widgets so that path is removed only once.
  2035. */
  2036. snd_soc_dapm_for_each_direction(dir) {
  2037. snd_soc_dapm_widget_for_each_path_safe(w, dir, p, next_p)
  2038. dapm_free_path(p);
  2039. }
  2040. kfree(w->kcontrols);
  2041. kfree_const(w->name);
  2042. kfree(w);
  2043. }
  2044. void snd_soc_dapm_reset_cache(struct snd_soc_dapm_context *dapm)
  2045. {
  2046. dapm->path_sink_cache.widget = NULL;
  2047. dapm->path_source_cache.widget = NULL;
  2048. }
  2049. /* free all dapm widgets and resources */
  2050. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  2051. {
  2052. struct snd_soc_dapm_widget *w, *next_w;
  2053. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  2054. if (w->dapm != dapm)
  2055. continue;
  2056. snd_soc_dapm_free_widget(w);
  2057. }
  2058. snd_soc_dapm_reset_cache(dapm);
  2059. }
  2060. static struct snd_soc_dapm_widget *dapm_find_widget(
  2061. struct snd_soc_dapm_context *dapm, const char *pin,
  2062. bool search_other_contexts)
  2063. {
  2064. struct snd_soc_dapm_widget *w;
  2065. struct snd_soc_dapm_widget *fallback = NULL;
  2066. list_for_each_entry(w, &dapm->card->widgets, list) {
  2067. if (!strcmp(w->name, pin)) {
  2068. if (w->dapm == dapm)
  2069. return w;
  2070. else
  2071. fallback = w;
  2072. }
  2073. }
  2074. if (search_other_contexts)
  2075. return fallback;
  2076. return NULL;
  2077. }
  2078. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  2079. const char *pin, int status)
  2080. {
  2081. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2082. dapm_assert_locked(dapm);
  2083. if (!w) {
  2084. dev_err(dapm->dev, "ASoC: DAPM unknown pin %s\n", pin);
  2085. return -EINVAL;
  2086. }
  2087. if (w->connected != status) {
  2088. dapm_mark_dirty(w, "pin configuration");
  2089. dapm_widget_invalidate_input_paths(w);
  2090. dapm_widget_invalidate_output_paths(w);
  2091. }
  2092. w->connected = status;
  2093. if (status == 0)
  2094. w->force = 0;
  2095. return 0;
  2096. }
  2097. /**
  2098. * snd_soc_dapm_sync_unlocked - scan and power dapm paths
  2099. * @dapm: DAPM context
  2100. *
  2101. * Walks all dapm audio paths and powers widgets according to their
  2102. * stream or path usage.
  2103. *
  2104. * Requires external locking.
  2105. *
  2106. * Returns 0 for success.
  2107. */
  2108. int snd_soc_dapm_sync_unlocked(struct snd_soc_dapm_context *dapm)
  2109. {
  2110. /*
  2111. * Suppress early reports (eg, jacks syncing their state) to avoid
  2112. * silly DAPM runs during card startup.
  2113. */
  2114. if (!dapm->card || !dapm->card->instantiated)
  2115. return 0;
  2116. return dapm_power_widgets(dapm->card, SND_SOC_DAPM_STREAM_NOP);
  2117. }
  2118. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync_unlocked);
  2119. /**
  2120. * snd_soc_dapm_sync - scan and power dapm paths
  2121. * @dapm: DAPM context
  2122. *
  2123. * Walks all dapm audio paths and powers widgets according to their
  2124. * stream or path usage.
  2125. *
  2126. * Returns 0 for success.
  2127. */
  2128. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  2129. {
  2130. int ret;
  2131. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2132. ret = snd_soc_dapm_sync_unlocked(dapm);
  2133. mutex_unlock(&dapm->card->dapm_mutex);
  2134. return ret;
  2135. }
  2136. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  2137. /*
  2138. * dapm_update_widget_flags() - Re-compute widget sink and source flags
  2139. * @w: The widget for which to update the flags
  2140. *
  2141. * Some widgets have a dynamic category which depends on which neighbors they
  2142. * are connected to. This function update the category for these widgets.
  2143. *
  2144. * This function must be called whenever a path is added or removed to a widget.
  2145. */
  2146. static void dapm_update_widget_flags(struct snd_soc_dapm_widget *w)
  2147. {
  2148. enum snd_soc_dapm_direction dir;
  2149. struct snd_soc_dapm_path *p;
  2150. unsigned int ep;
  2151. switch (w->id) {
  2152. case snd_soc_dapm_input:
  2153. /* On a fully routed card an input is never a source */
  2154. if (w->dapm->card->fully_routed)
  2155. return;
  2156. ep = SND_SOC_DAPM_EP_SOURCE;
  2157. snd_soc_dapm_widget_for_each_source_path(w, p) {
  2158. if (p->source->id == snd_soc_dapm_micbias ||
  2159. p->source->id == snd_soc_dapm_mic ||
  2160. p->source->id == snd_soc_dapm_line ||
  2161. p->source->id == snd_soc_dapm_output) {
  2162. ep = 0;
  2163. break;
  2164. }
  2165. }
  2166. break;
  2167. case snd_soc_dapm_output:
  2168. /* On a fully routed card a output is never a sink */
  2169. if (w->dapm->card->fully_routed)
  2170. return;
  2171. ep = SND_SOC_DAPM_EP_SINK;
  2172. snd_soc_dapm_widget_for_each_sink_path(w, p) {
  2173. if (p->sink->id == snd_soc_dapm_spk ||
  2174. p->sink->id == snd_soc_dapm_hp ||
  2175. p->sink->id == snd_soc_dapm_line ||
  2176. p->sink->id == snd_soc_dapm_input) {
  2177. ep = 0;
  2178. break;
  2179. }
  2180. }
  2181. break;
  2182. case snd_soc_dapm_line:
  2183. ep = 0;
  2184. snd_soc_dapm_for_each_direction(dir) {
  2185. if (!list_empty(&w->edges[dir]))
  2186. ep |= SND_SOC_DAPM_DIR_TO_EP(dir);
  2187. }
  2188. break;
  2189. default:
  2190. return;
  2191. }
  2192. w->is_ep = ep;
  2193. }
  2194. static int snd_soc_dapm_check_dynamic_path(struct snd_soc_dapm_context *dapm,
  2195. struct snd_soc_dapm_widget *source, struct snd_soc_dapm_widget *sink,
  2196. const char *control)
  2197. {
  2198. bool dynamic_source = false;
  2199. bool dynamic_sink = false;
  2200. if (!control)
  2201. return 0;
  2202. switch (source->id) {
  2203. case snd_soc_dapm_demux:
  2204. dynamic_source = true;
  2205. break;
  2206. default:
  2207. break;
  2208. }
  2209. switch (sink->id) {
  2210. case snd_soc_dapm_mux:
  2211. case snd_soc_dapm_switch:
  2212. case snd_soc_dapm_mixer:
  2213. case snd_soc_dapm_mixer_named_ctl:
  2214. dynamic_sink = true;
  2215. break;
  2216. default:
  2217. break;
  2218. }
  2219. if (dynamic_source && dynamic_sink) {
  2220. dev_err(dapm->dev,
  2221. "Direct connection between demux and mixer/mux not supported for path %s -> [%s] -> %s\n",
  2222. source->name, control, sink->name);
  2223. return -EINVAL;
  2224. } else if (!dynamic_source && !dynamic_sink) {
  2225. dev_err(dapm->dev,
  2226. "Control not supported for path %s -> [%s] -> %s\n",
  2227. source->name, control, sink->name);
  2228. return -EINVAL;
  2229. }
  2230. return 0;
  2231. }
  2232. static int snd_soc_dapm_add_path(struct snd_soc_dapm_context *dapm,
  2233. struct snd_soc_dapm_widget *wsource, struct snd_soc_dapm_widget *wsink,
  2234. const char *control,
  2235. int (*connected)(struct snd_soc_dapm_widget *source,
  2236. struct snd_soc_dapm_widget *sink))
  2237. {
  2238. struct snd_soc_dapm_widget *widgets[2];
  2239. enum snd_soc_dapm_direction dir;
  2240. struct snd_soc_dapm_path *path;
  2241. int ret;
  2242. if (wsink->is_supply && !wsource->is_supply) {
  2243. dev_err(dapm->dev,
  2244. "Connecting non-supply widget to supply widget is not supported (%s -> %s)\n",
  2245. wsource->name, wsink->name);
  2246. return -EINVAL;
  2247. }
  2248. if (connected && !wsource->is_supply) {
  2249. dev_err(dapm->dev,
  2250. "connected() callback only supported for supply widgets (%s -> %s)\n",
  2251. wsource->name, wsink->name);
  2252. return -EINVAL;
  2253. }
  2254. if (wsource->is_supply && control) {
  2255. dev_err(dapm->dev,
  2256. "Conditional paths are not supported for supply widgets (%s -> [%s] -> %s)\n",
  2257. wsource->name, control, wsink->name);
  2258. return -EINVAL;
  2259. }
  2260. ret = snd_soc_dapm_check_dynamic_path(dapm, wsource, wsink, control);
  2261. if (ret)
  2262. return ret;
  2263. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  2264. if (!path)
  2265. return -ENOMEM;
  2266. path->node[SND_SOC_DAPM_DIR_IN] = wsource;
  2267. path->node[SND_SOC_DAPM_DIR_OUT] = wsink;
  2268. widgets[SND_SOC_DAPM_DIR_IN] = wsource;
  2269. widgets[SND_SOC_DAPM_DIR_OUT] = wsink;
  2270. path->connected = connected;
  2271. INIT_LIST_HEAD(&path->list);
  2272. INIT_LIST_HEAD(&path->list_kcontrol);
  2273. if (wsource->is_supply || wsink->is_supply)
  2274. path->is_supply = 1;
  2275. /* connect static paths */
  2276. if (control == NULL) {
  2277. path->connect = 1;
  2278. } else {
  2279. switch (wsource->id) {
  2280. case snd_soc_dapm_demux:
  2281. ret = dapm_connect_mux(dapm, path, control, wsource);
  2282. if (ret)
  2283. goto err;
  2284. break;
  2285. default:
  2286. break;
  2287. }
  2288. switch (wsink->id) {
  2289. case snd_soc_dapm_mux:
  2290. ret = dapm_connect_mux(dapm, path, control, wsink);
  2291. if (ret != 0)
  2292. goto err;
  2293. break;
  2294. case snd_soc_dapm_switch:
  2295. case snd_soc_dapm_mixer:
  2296. case snd_soc_dapm_mixer_named_ctl:
  2297. ret = dapm_connect_mixer(dapm, path, control);
  2298. if (ret != 0)
  2299. goto err;
  2300. break;
  2301. default:
  2302. break;
  2303. }
  2304. }
  2305. list_add(&path->list, &dapm->card->paths);
  2306. snd_soc_dapm_for_each_direction(dir)
  2307. list_add(&path->list_node[dir], &widgets[dir]->edges[dir]);
  2308. snd_soc_dapm_for_each_direction(dir) {
  2309. dapm_update_widget_flags(widgets[dir]);
  2310. dapm_mark_dirty(widgets[dir], "Route added");
  2311. }
  2312. if (dapm->card->instantiated && path->connect)
  2313. dapm_path_invalidate(path);
  2314. return 0;
  2315. err:
  2316. kfree(path);
  2317. return ret;
  2318. }
  2319. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  2320. const struct snd_soc_dapm_route *route)
  2321. {
  2322. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  2323. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  2324. const char *sink;
  2325. const char *source;
  2326. char prefixed_sink[80];
  2327. char prefixed_source[80];
  2328. const char *prefix;
  2329. int ret;
  2330. prefix = soc_dapm_prefix(dapm);
  2331. if (prefix) {
  2332. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  2333. prefix, route->sink);
  2334. sink = prefixed_sink;
  2335. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  2336. prefix, route->source);
  2337. source = prefixed_source;
  2338. } else {
  2339. sink = route->sink;
  2340. source = route->source;
  2341. }
  2342. wsource = dapm_wcache_lookup(&dapm->path_source_cache, source);
  2343. wsink = dapm_wcache_lookup(&dapm->path_sink_cache, sink);
  2344. if (wsink && wsource)
  2345. goto skip_search;
  2346. /*
  2347. * find src and dest widgets over all widgets but favor a widget from
  2348. * current DAPM context
  2349. */
  2350. list_for_each_entry(w, &dapm->card->widgets, list) {
  2351. if (!wsink && !(strcmp(w->name, sink))) {
  2352. wtsink = w;
  2353. if (w->dapm == dapm) {
  2354. wsink = w;
  2355. if (wsource)
  2356. break;
  2357. }
  2358. continue;
  2359. }
  2360. if (!wsource && !(strcmp(w->name, source))) {
  2361. wtsource = w;
  2362. if (w->dapm == dapm) {
  2363. wsource = w;
  2364. if (wsink)
  2365. break;
  2366. }
  2367. }
  2368. }
  2369. /* use widget from another DAPM context if not found from this */
  2370. if (!wsink)
  2371. wsink = wtsink;
  2372. if (!wsource)
  2373. wsource = wtsource;
  2374. if (wsource == NULL) {
  2375. dev_err(dapm->dev, "ASoC: no source widget found for %s\n",
  2376. route->source);
  2377. return -ENODEV;
  2378. }
  2379. if (wsink == NULL) {
  2380. dev_err(dapm->dev, "ASoC: no sink widget found for %s\n",
  2381. route->sink);
  2382. return -ENODEV;
  2383. }
  2384. skip_search:
  2385. dapm_wcache_update(&dapm->path_sink_cache, wsink);
  2386. dapm_wcache_update(&dapm->path_source_cache, wsource);
  2387. ret = snd_soc_dapm_add_path(dapm, wsource, wsink, route->control,
  2388. route->connected);
  2389. if (ret)
  2390. goto err;
  2391. return 0;
  2392. err:
  2393. dev_warn(dapm->dev, "ASoC: no dapm match for %s --> %s --> %s\n",
  2394. source, route->control, sink);
  2395. return ret;
  2396. }
  2397. static int snd_soc_dapm_del_route(struct snd_soc_dapm_context *dapm,
  2398. const struct snd_soc_dapm_route *route)
  2399. {
  2400. struct snd_soc_dapm_widget *wsource, *wsink;
  2401. struct snd_soc_dapm_path *path, *p;
  2402. const char *sink;
  2403. const char *source;
  2404. char prefixed_sink[80];
  2405. char prefixed_source[80];
  2406. const char *prefix;
  2407. if (route->control) {
  2408. dev_err(dapm->dev,
  2409. "ASoC: Removal of routes with controls not supported\n");
  2410. return -EINVAL;
  2411. }
  2412. prefix = soc_dapm_prefix(dapm);
  2413. if (prefix) {
  2414. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  2415. prefix, route->sink);
  2416. sink = prefixed_sink;
  2417. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  2418. prefix, route->source);
  2419. source = prefixed_source;
  2420. } else {
  2421. sink = route->sink;
  2422. source = route->source;
  2423. }
  2424. path = NULL;
  2425. list_for_each_entry(p, &dapm->card->paths, list) {
  2426. if (strcmp(p->source->name, source) != 0)
  2427. continue;
  2428. if (strcmp(p->sink->name, sink) != 0)
  2429. continue;
  2430. path = p;
  2431. break;
  2432. }
  2433. if (path) {
  2434. wsource = path->source;
  2435. wsink = path->sink;
  2436. dapm_mark_dirty(wsource, "Route removed");
  2437. dapm_mark_dirty(wsink, "Route removed");
  2438. if (path->connect)
  2439. dapm_path_invalidate(path);
  2440. dapm_free_path(path);
  2441. /* Update any path related flags */
  2442. dapm_update_widget_flags(wsource);
  2443. dapm_update_widget_flags(wsink);
  2444. } else {
  2445. dev_warn(dapm->dev, "ASoC: Route %s->%s does not exist\n",
  2446. source, sink);
  2447. }
  2448. return 0;
  2449. }
  2450. /**
  2451. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  2452. * @dapm: DAPM context
  2453. * @route: audio routes
  2454. * @num: number of routes
  2455. *
  2456. * Connects 2 dapm widgets together via a named audio path. The sink is
  2457. * the widget receiving the audio signal, whilst the source is the sender
  2458. * of the audio signal.
  2459. *
  2460. * Returns 0 for success else error. On error all resources can be freed
  2461. * with a call to snd_soc_card_free().
  2462. */
  2463. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  2464. const struct snd_soc_dapm_route *route, int num)
  2465. {
  2466. int i, r, ret = 0;
  2467. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2468. for (i = 0; i < num; i++) {
  2469. r = snd_soc_dapm_add_route(dapm, route);
  2470. if (r < 0) {
  2471. dev_err(dapm->dev, "ASoC: Failed to add route %s -> %s -> %s\n",
  2472. route->source,
  2473. route->control ? route->control : "direct",
  2474. route->sink);
  2475. ret = r;
  2476. }
  2477. route++;
  2478. }
  2479. mutex_unlock(&dapm->card->dapm_mutex);
  2480. return ret;
  2481. }
  2482. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  2483. /**
  2484. * snd_soc_dapm_del_routes - Remove routes between DAPM widgets
  2485. * @dapm: DAPM context
  2486. * @route: audio routes
  2487. * @num: number of routes
  2488. *
  2489. * Removes routes from the DAPM context.
  2490. */
  2491. int snd_soc_dapm_del_routes(struct snd_soc_dapm_context *dapm,
  2492. const struct snd_soc_dapm_route *route, int num)
  2493. {
  2494. int i;
  2495. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2496. for (i = 0; i < num; i++) {
  2497. snd_soc_dapm_del_route(dapm, route);
  2498. route++;
  2499. }
  2500. mutex_unlock(&dapm->card->dapm_mutex);
  2501. return 0;
  2502. }
  2503. EXPORT_SYMBOL_GPL(snd_soc_dapm_del_routes);
  2504. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  2505. const struct snd_soc_dapm_route *route)
  2506. {
  2507. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  2508. route->source,
  2509. true);
  2510. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  2511. route->sink,
  2512. true);
  2513. struct snd_soc_dapm_path *path;
  2514. int count = 0;
  2515. if (!source) {
  2516. dev_err(dapm->dev, "ASoC: Unable to find source %s for weak route\n",
  2517. route->source);
  2518. return -ENODEV;
  2519. }
  2520. if (!sink) {
  2521. dev_err(dapm->dev, "ASoC: Unable to find sink %s for weak route\n",
  2522. route->sink);
  2523. return -ENODEV;
  2524. }
  2525. if (route->control || route->connected)
  2526. dev_warn(dapm->dev, "ASoC: Ignoring control for weak route %s->%s\n",
  2527. route->source, route->sink);
  2528. snd_soc_dapm_widget_for_each_sink_path(source, path) {
  2529. if (path->sink == sink) {
  2530. path->weak = 1;
  2531. count++;
  2532. }
  2533. }
  2534. if (count == 0)
  2535. dev_err(dapm->dev, "ASoC: No path found for weak route %s->%s\n",
  2536. route->source, route->sink);
  2537. if (count > 1)
  2538. dev_warn(dapm->dev, "ASoC: %d paths found for weak route %s->%s\n",
  2539. count, route->source, route->sink);
  2540. return 0;
  2541. }
  2542. /**
  2543. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  2544. * @dapm: DAPM context
  2545. * @route: audio routes
  2546. * @num: number of routes
  2547. *
  2548. * Mark existing routes matching those specified in the passed array
  2549. * as being weak, meaning that they are ignored for the purpose of
  2550. * power decisions. The main intended use case is for sidetone paths
  2551. * which couple audio between other independent paths if they are both
  2552. * active in order to make the combination work better at the user
  2553. * level but which aren't intended to be "used".
  2554. *
  2555. * Note that CODEC drivers should not use this as sidetone type paths
  2556. * can frequently also be used as bypass paths.
  2557. */
  2558. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  2559. const struct snd_soc_dapm_route *route, int num)
  2560. {
  2561. int i, err;
  2562. int ret = 0;
  2563. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2564. for (i = 0; i < num; i++) {
  2565. err = snd_soc_dapm_weak_route(dapm, route);
  2566. if (err)
  2567. ret = err;
  2568. route++;
  2569. }
  2570. mutex_unlock(&dapm->card->dapm_mutex);
  2571. return ret;
  2572. }
  2573. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  2574. /**
  2575. * snd_soc_dapm_new_widgets - add new dapm widgets
  2576. * @card: card to be checked for new dapm widgets
  2577. *
  2578. * Checks the codec for any new dapm widgets and creates them if found.
  2579. *
  2580. * Returns 0 for success.
  2581. */
  2582. int snd_soc_dapm_new_widgets(struct snd_soc_card *card)
  2583. {
  2584. struct snd_soc_dapm_widget *w;
  2585. unsigned int val;
  2586. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  2587. list_for_each_entry(w, &card->widgets, list)
  2588. {
  2589. if (w->new)
  2590. continue;
  2591. if (w->num_kcontrols) {
  2592. w->kcontrols = kcalloc(w->num_kcontrols,
  2593. sizeof(struct snd_kcontrol *),
  2594. GFP_KERNEL);
  2595. if (!w->kcontrols) {
  2596. mutex_unlock(&card->dapm_mutex);
  2597. return -ENOMEM;
  2598. }
  2599. }
  2600. switch(w->id) {
  2601. case snd_soc_dapm_switch:
  2602. case snd_soc_dapm_mixer:
  2603. case snd_soc_dapm_mixer_named_ctl:
  2604. dapm_new_mixer(w);
  2605. break;
  2606. case snd_soc_dapm_mux:
  2607. case snd_soc_dapm_demux:
  2608. dapm_new_mux(w);
  2609. break;
  2610. case snd_soc_dapm_pga:
  2611. case snd_soc_dapm_out_drv:
  2612. dapm_new_pga(w);
  2613. break;
  2614. case snd_soc_dapm_dai_link:
  2615. dapm_new_dai_link(w);
  2616. break;
  2617. default:
  2618. break;
  2619. }
  2620. /* Read the initial power state from the device */
  2621. if (w->reg >= 0) {
  2622. soc_dapm_read(w->dapm, w->reg, &val);
  2623. val = val >> w->shift;
  2624. val &= w->mask;
  2625. if (val == w->on_val)
  2626. w->power = 1;
  2627. }
  2628. w->new = 1;
  2629. dapm_mark_dirty(w, "new widget");
  2630. dapm_debugfs_add_widget(w);
  2631. }
  2632. dapm_power_widgets(card, SND_SOC_DAPM_STREAM_NOP);
  2633. mutex_unlock(&card->dapm_mutex);
  2634. return 0;
  2635. }
  2636. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  2637. /**
  2638. * snd_soc_dapm_get_volsw - dapm mixer get callback
  2639. * @kcontrol: mixer control
  2640. * @ucontrol: control element information
  2641. *
  2642. * Callback to get the value of a dapm mixer control.
  2643. *
  2644. * Returns 0 for success.
  2645. */
  2646. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  2647. struct snd_ctl_elem_value *ucontrol)
  2648. {
  2649. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  2650. struct snd_soc_card *card = dapm->card;
  2651. struct soc_mixer_control *mc =
  2652. (struct soc_mixer_control *)kcontrol->private_value;
  2653. int reg = mc->reg;
  2654. unsigned int shift = mc->shift;
  2655. int max = mc->max;
  2656. unsigned int width = fls(max);
  2657. unsigned int mask = (1 << fls(max)) - 1;
  2658. unsigned int invert = mc->invert;
  2659. unsigned int reg_val, val, rval = 0;
  2660. int ret = 0;
  2661. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2662. if (dapm_kcontrol_is_powered(kcontrol) && reg != SND_SOC_NOPM) {
  2663. ret = soc_dapm_read(dapm, reg, &reg_val);
  2664. val = (reg_val >> shift) & mask;
  2665. if (ret == 0 && reg != mc->rreg)
  2666. ret = soc_dapm_read(dapm, mc->rreg, &reg_val);
  2667. if (snd_soc_volsw_is_stereo(mc))
  2668. rval = (reg_val >> mc->rshift) & mask;
  2669. } else {
  2670. reg_val = dapm_kcontrol_get_value(kcontrol);
  2671. val = reg_val & mask;
  2672. if (snd_soc_volsw_is_stereo(mc))
  2673. rval = (reg_val >> width) & mask;
  2674. }
  2675. mutex_unlock(&card->dapm_mutex);
  2676. if (ret)
  2677. return ret;
  2678. if (invert)
  2679. ucontrol->value.integer.value[0] = max - val;
  2680. else
  2681. ucontrol->value.integer.value[0] = val;
  2682. if (snd_soc_volsw_is_stereo(mc)) {
  2683. if (invert)
  2684. ucontrol->value.integer.value[1] = max - rval;
  2685. else
  2686. ucontrol->value.integer.value[1] = rval;
  2687. }
  2688. return ret;
  2689. }
  2690. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  2691. /**
  2692. * snd_soc_dapm_put_volsw - dapm mixer set callback
  2693. * @kcontrol: mixer control
  2694. * @ucontrol: control element information
  2695. *
  2696. * Callback to set the value of a dapm mixer control.
  2697. *
  2698. * Returns 0 for success.
  2699. */
  2700. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  2701. struct snd_ctl_elem_value *ucontrol)
  2702. {
  2703. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  2704. struct snd_soc_card *card = dapm->card;
  2705. struct soc_mixer_control *mc =
  2706. (struct soc_mixer_control *)kcontrol->private_value;
  2707. int reg = mc->reg;
  2708. unsigned int shift = mc->shift;
  2709. int max = mc->max;
  2710. unsigned int width = fls(max);
  2711. unsigned int mask = (1 << width) - 1;
  2712. unsigned int invert = mc->invert;
  2713. unsigned int val, rval = 0;
  2714. int connect, rconnect = -1, change, reg_change = 0;
  2715. struct snd_soc_dapm_update update = {};
  2716. int ret = 0;
  2717. val = (ucontrol->value.integer.value[0] & mask);
  2718. connect = !!val;
  2719. if (invert)
  2720. val = max - val;
  2721. if (snd_soc_volsw_is_stereo(mc)) {
  2722. rval = (ucontrol->value.integer.value[1] & mask);
  2723. rconnect = !!rval;
  2724. if (invert)
  2725. rval = max - rval;
  2726. }
  2727. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2728. /* This assumes field width < (bits in unsigned int / 2) */
  2729. if (width > sizeof(unsigned int) * 8 / 2)
  2730. dev_warn(dapm->dev,
  2731. "ASoC: control %s field width limit exceeded\n",
  2732. kcontrol->id.name);
  2733. change = dapm_kcontrol_set_value(kcontrol, val | (rval << width));
  2734. if (reg != SND_SOC_NOPM) {
  2735. val = val << shift;
  2736. rval = rval << mc->rshift;
  2737. reg_change = soc_dapm_test_bits(dapm, reg, mask << shift, val);
  2738. if (snd_soc_volsw_is_stereo(mc))
  2739. reg_change |= soc_dapm_test_bits(dapm, mc->rreg,
  2740. mask << mc->rshift,
  2741. rval);
  2742. }
  2743. if (change || reg_change) {
  2744. if (reg_change) {
  2745. if (snd_soc_volsw_is_stereo(mc)) {
  2746. update.has_second_set = true;
  2747. update.reg2 = mc->rreg;
  2748. update.mask2 = mask << mc->rshift;
  2749. update.val2 = rval;
  2750. }
  2751. update.kcontrol = kcontrol;
  2752. update.reg = reg;
  2753. update.mask = mask << shift;
  2754. update.val = val;
  2755. card->update = &update;
  2756. }
  2757. change |= reg_change;
  2758. ret = soc_dapm_mixer_update_power(card, kcontrol, connect,
  2759. rconnect);
  2760. card->update = NULL;
  2761. }
  2762. mutex_unlock(&card->dapm_mutex);
  2763. if (ret > 0)
  2764. soc_dpcm_runtime_update(card);
  2765. return change;
  2766. }
  2767. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  2768. /**
  2769. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  2770. * @kcontrol: mixer control
  2771. * @ucontrol: control element information
  2772. *
  2773. * Callback to get the value of a dapm enumerated double mixer control.
  2774. *
  2775. * Returns 0 for success.
  2776. */
  2777. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  2778. struct snd_ctl_elem_value *ucontrol)
  2779. {
  2780. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  2781. struct snd_soc_card *card = dapm->card;
  2782. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2783. unsigned int reg_val, val;
  2784. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2785. if (e->reg != SND_SOC_NOPM && dapm_kcontrol_is_powered(kcontrol)) {
  2786. int ret = soc_dapm_read(dapm, e->reg, &reg_val);
  2787. if (ret) {
  2788. mutex_unlock(&card->dapm_mutex);
  2789. return ret;
  2790. }
  2791. } else {
  2792. reg_val = dapm_kcontrol_get_value(kcontrol);
  2793. }
  2794. mutex_unlock(&card->dapm_mutex);
  2795. val = (reg_val >> e->shift_l) & e->mask;
  2796. ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(e, val);
  2797. if (e->shift_l != e->shift_r) {
  2798. val = (reg_val >> e->shift_r) & e->mask;
  2799. val = snd_soc_enum_val_to_item(e, val);
  2800. ucontrol->value.enumerated.item[1] = val;
  2801. }
  2802. return 0;
  2803. }
  2804. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  2805. /**
  2806. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  2807. * @kcontrol: mixer control
  2808. * @ucontrol: control element information
  2809. *
  2810. * Callback to set the value of a dapm enumerated double mixer control.
  2811. *
  2812. * Returns 0 for success.
  2813. */
  2814. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  2815. struct snd_ctl_elem_value *ucontrol)
  2816. {
  2817. struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
  2818. struct snd_soc_card *card = dapm->card;
  2819. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2820. unsigned int *item = ucontrol->value.enumerated.item;
  2821. unsigned int val, change, reg_change = 0;
  2822. unsigned int mask;
  2823. struct snd_soc_dapm_update update = {};
  2824. int ret = 0;
  2825. if (item[0] >= e->items)
  2826. return -EINVAL;
  2827. val = snd_soc_enum_item_to_val(e, item[0]) << e->shift_l;
  2828. mask = e->mask << e->shift_l;
  2829. if (e->shift_l != e->shift_r) {
  2830. if (item[1] > e->items)
  2831. return -EINVAL;
  2832. val |= snd_soc_enum_item_to_val(e, item[1]) << e->shift_r;
  2833. mask |= e->mask << e->shift_r;
  2834. }
  2835. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2836. change = dapm_kcontrol_set_value(kcontrol, val);
  2837. if (e->reg != SND_SOC_NOPM)
  2838. reg_change = soc_dapm_test_bits(dapm, e->reg, mask, val);
  2839. if (change || reg_change) {
  2840. if (reg_change) {
  2841. update.kcontrol = kcontrol;
  2842. update.reg = e->reg;
  2843. update.mask = mask;
  2844. update.val = val;
  2845. card->update = &update;
  2846. }
  2847. change |= reg_change;
  2848. ret = soc_dapm_mux_update_power(card, kcontrol, item[0], e);
  2849. card->update = NULL;
  2850. }
  2851. mutex_unlock(&card->dapm_mutex);
  2852. if (ret > 0)
  2853. soc_dpcm_runtime_update(card);
  2854. return change;
  2855. }
  2856. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  2857. /**
  2858. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2859. *
  2860. * @kcontrol: mixer control
  2861. * @uinfo: control element information
  2862. *
  2863. * Callback to provide information about a pin switch control.
  2864. */
  2865. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2866. struct snd_ctl_elem_info *uinfo)
  2867. {
  2868. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2869. uinfo->count = 1;
  2870. uinfo->value.integer.min = 0;
  2871. uinfo->value.integer.max = 1;
  2872. return 0;
  2873. }
  2874. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2875. /**
  2876. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2877. *
  2878. * @kcontrol: mixer control
  2879. * @ucontrol: Value
  2880. */
  2881. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2882. struct snd_ctl_elem_value *ucontrol)
  2883. {
  2884. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2885. const char *pin = (const char *)kcontrol->private_value;
  2886. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  2887. ucontrol->value.integer.value[0] =
  2888. snd_soc_dapm_get_pin_status(&card->dapm, pin);
  2889. mutex_unlock(&card->dapm_mutex);
  2890. return 0;
  2891. }
  2892. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2893. /**
  2894. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2895. *
  2896. * @kcontrol: mixer control
  2897. * @ucontrol: Value
  2898. */
  2899. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2900. struct snd_ctl_elem_value *ucontrol)
  2901. {
  2902. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2903. const char *pin = (const char *)kcontrol->private_value;
  2904. if (ucontrol->value.integer.value[0])
  2905. snd_soc_dapm_enable_pin(&card->dapm, pin);
  2906. else
  2907. snd_soc_dapm_disable_pin(&card->dapm, pin);
  2908. snd_soc_dapm_sync(&card->dapm);
  2909. return 0;
  2910. }
  2911. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2912. struct snd_soc_dapm_widget *
  2913. snd_soc_dapm_new_control_unlocked(struct snd_soc_dapm_context *dapm,
  2914. const struct snd_soc_dapm_widget *widget)
  2915. {
  2916. enum snd_soc_dapm_direction dir;
  2917. struct snd_soc_dapm_widget *w;
  2918. const char *prefix;
  2919. int ret;
  2920. if ((w = dapm_cnew_widget(widget)) == NULL)
  2921. return ERR_PTR(-ENOMEM);
  2922. switch (w->id) {
  2923. case snd_soc_dapm_regulator_supply:
  2924. w->regulator = devm_regulator_get(dapm->dev, w->name);
  2925. if (IS_ERR(w->regulator)) {
  2926. ret = PTR_ERR(w->regulator);
  2927. goto request_failed;
  2928. }
  2929. if (w->on_val & SND_SOC_DAPM_REGULATOR_BYPASS) {
  2930. ret = regulator_allow_bypass(w->regulator, true);
  2931. if (ret != 0)
  2932. dev_warn(dapm->dev,
  2933. "ASoC: Failed to bypass %s: %d\n",
  2934. w->name, ret);
  2935. }
  2936. break;
  2937. case snd_soc_dapm_pinctrl:
  2938. w->pinctrl = devm_pinctrl_get(dapm->dev);
  2939. if (IS_ERR(w->pinctrl)) {
  2940. ret = PTR_ERR(w->pinctrl);
  2941. goto request_failed;
  2942. }
  2943. break;
  2944. case snd_soc_dapm_clock_supply:
  2945. w->clk = devm_clk_get(dapm->dev, w->name);
  2946. if (IS_ERR(w->clk)) {
  2947. ret = PTR_ERR(w->clk);
  2948. goto request_failed;
  2949. }
  2950. break;
  2951. default:
  2952. break;
  2953. }
  2954. prefix = soc_dapm_prefix(dapm);
  2955. if (prefix)
  2956. w->name = kasprintf(GFP_KERNEL, "%s %s", prefix, widget->name);
  2957. else
  2958. w->name = kstrdup_const(widget->name, GFP_KERNEL);
  2959. if (w->name == NULL) {
  2960. kfree(w);
  2961. return ERR_PTR(-ENOMEM);
  2962. }
  2963. switch (w->id) {
  2964. case snd_soc_dapm_mic:
  2965. w->is_ep = SND_SOC_DAPM_EP_SOURCE;
  2966. w->power_check = dapm_generic_check_power;
  2967. break;
  2968. case snd_soc_dapm_input:
  2969. if (!dapm->card->fully_routed)
  2970. w->is_ep = SND_SOC_DAPM_EP_SOURCE;
  2971. w->power_check = dapm_generic_check_power;
  2972. break;
  2973. case snd_soc_dapm_spk:
  2974. case snd_soc_dapm_hp:
  2975. w->is_ep = SND_SOC_DAPM_EP_SINK;
  2976. w->power_check = dapm_generic_check_power;
  2977. break;
  2978. case snd_soc_dapm_output:
  2979. if (!dapm->card->fully_routed)
  2980. w->is_ep = SND_SOC_DAPM_EP_SINK;
  2981. w->power_check = dapm_generic_check_power;
  2982. break;
  2983. case snd_soc_dapm_vmid:
  2984. case snd_soc_dapm_siggen:
  2985. w->is_ep = SND_SOC_DAPM_EP_SOURCE;
  2986. w->power_check = dapm_always_on_check_power;
  2987. break;
  2988. case snd_soc_dapm_sink:
  2989. w->is_ep = SND_SOC_DAPM_EP_SINK;
  2990. w->power_check = dapm_always_on_check_power;
  2991. break;
  2992. case snd_soc_dapm_mux:
  2993. case snd_soc_dapm_demux:
  2994. case snd_soc_dapm_switch:
  2995. case snd_soc_dapm_mixer:
  2996. case snd_soc_dapm_mixer_named_ctl:
  2997. case snd_soc_dapm_adc:
  2998. case snd_soc_dapm_aif_out:
  2999. case snd_soc_dapm_dac:
  3000. case snd_soc_dapm_aif_in:
  3001. case snd_soc_dapm_pga:
  3002. case snd_soc_dapm_out_drv:
  3003. case snd_soc_dapm_micbias:
  3004. case snd_soc_dapm_line:
  3005. case snd_soc_dapm_dai_link:
  3006. case snd_soc_dapm_dai_out:
  3007. case snd_soc_dapm_dai_in:
  3008. w->power_check = dapm_generic_check_power;
  3009. break;
  3010. case snd_soc_dapm_supply:
  3011. case snd_soc_dapm_regulator_supply:
  3012. case snd_soc_dapm_pinctrl:
  3013. case snd_soc_dapm_clock_supply:
  3014. case snd_soc_dapm_kcontrol:
  3015. w->is_supply = 1;
  3016. w->power_check = dapm_supply_check_power;
  3017. break;
  3018. default:
  3019. w->power_check = dapm_always_on_check_power;
  3020. break;
  3021. }
  3022. w->dapm = dapm;
  3023. INIT_LIST_HEAD(&w->list);
  3024. INIT_LIST_HEAD(&w->dirty);
  3025. list_add_tail(&w->list, &dapm->card->widgets);
  3026. snd_soc_dapm_for_each_direction(dir) {
  3027. INIT_LIST_HEAD(&w->edges[dir]);
  3028. w->endpoints[dir] = -1;
  3029. }
  3030. /* machine layer sets up unconnected pins and insertions */
  3031. w->connected = 1;
  3032. return w;
  3033. request_failed:
  3034. if (ret != -EPROBE_DEFER)
  3035. dev_err(dapm->dev, "ASoC: Failed to request %s: %d\n",
  3036. w->name, ret);
  3037. return ERR_PTR(ret);
  3038. }
  3039. /**
  3040. * snd_soc_dapm_new_control - create new dapm control
  3041. * @dapm: DAPM context
  3042. * @widget: widget template
  3043. *
  3044. * Creates new DAPM control based upon a template.
  3045. *
  3046. * Returns a widget pointer on success or an error pointer on failure
  3047. */
  3048. struct snd_soc_dapm_widget *
  3049. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  3050. const struct snd_soc_dapm_widget *widget)
  3051. {
  3052. struct snd_soc_dapm_widget *w;
  3053. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3054. w = snd_soc_dapm_new_control_unlocked(dapm, widget);
  3055. mutex_unlock(&dapm->card->dapm_mutex);
  3056. return w;
  3057. }
  3058. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  3059. /**
  3060. * snd_soc_dapm_new_controls - create new dapm controls
  3061. * @dapm: DAPM context
  3062. * @widget: widget array
  3063. * @num: number of widgets
  3064. *
  3065. * Creates new DAPM controls based upon the templates.
  3066. *
  3067. * Returns 0 for success else error.
  3068. */
  3069. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  3070. const struct snd_soc_dapm_widget *widget,
  3071. int num)
  3072. {
  3073. struct snd_soc_dapm_widget *w;
  3074. int i;
  3075. int ret = 0;
  3076. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_INIT);
  3077. for (i = 0; i < num; i++) {
  3078. w = snd_soc_dapm_new_control_unlocked(dapm, widget);
  3079. if (IS_ERR(w)) {
  3080. ret = PTR_ERR(w);
  3081. break;
  3082. }
  3083. widget++;
  3084. }
  3085. mutex_unlock(&dapm->card->dapm_mutex);
  3086. return ret;
  3087. }
  3088. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  3089. static int snd_soc_dai_link_event(struct snd_soc_dapm_widget *w,
  3090. struct snd_kcontrol *kcontrol, int event)
  3091. {
  3092. struct snd_soc_dapm_path *path;
  3093. struct snd_soc_dai *source, *sink;
  3094. struct snd_soc_pcm_runtime *rtd = w->priv;
  3095. const struct snd_soc_pcm_stream *config;
  3096. struct snd_pcm_substream substream;
  3097. struct snd_pcm_hw_params *params = NULL;
  3098. struct snd_pcm_runtime *runtime = NULL;
  3099. unsigned int fmt;
  3100. int ret = 0;
  3101. config = rtd->dai_link->params + rtd->params_select;
  3102. if (WARN_ON(!config) ||
  3103. WARN_ON(list_empty(&w->edges[SND_SOC_DAPM_DIR_OUT]) ||
  3104. list_empty(&w->edges[SND_SOC_DAPM_DIR_IN])))
  3105. return -EINVAL;
  3106. /* Be a little careful as we don't want to overflow the mask array */
  3107. if (config->formats) {
  3108. fmt = ffs(config->formats) - 1;
  3109. } else {
  3110. dev_warn(w->dapm->dev, "ASoC: Invalid format %llx specified\n",
  3111. config->formats);
  3112. fmt = 0;
  3113. }
  3114. /* Currently very limited parameter selection */
  3115. params = kzalloc(sizeof(*params), GFP_KERNEL);
  3116. if (!params) {
  3117. ret = -ENOMEM;
  3118. goto out;
  3119. }
  3120. snd_mask_set(hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT), fmt);
  3121. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min =
  3122. config->rate_min;
  3123. hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max =
  3124. config->rate_max;
  3125. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->min
  3126. = config->channels_min;
  3127. hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS)->max
  3128. = config->channels_max;
  3129. memset(&substream, 0, sizeof(substream));
  3130. /* Allocate a dummy snd_pcm_runtime for startup() and other ops() */
  3131. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  3132. if (!runtime) {
  3133. ret = -ENOMEM;
  3134. goto out;
  3135. }
  3136. substream.runtime = runtime;
  3137. substream.private_data = rtd;
  3138. switch (event) {
  3139. case SND_SOC_DAPM_PRE_PMU:
  3140. substream.stream = SNDRV_PCM_STREAM_CAPTURE;
  3141. snd_soc_dapm_widget_for_each_source_path(w, path) {
  3142. source = path->source->priv;
  3143. if (source->driver->ops->startup) {
  3144. ret = source->driver->ops->startup(&substream,
  3145. source);
  3146. if (ret < 0) {
  3147. dev_err(source->dev,
  3148. "ASoC: startup() failed: %d\n",
  3149. ret);
  3150. goto out;
  3151. }
  3152. source->active++;
  3153. }
  3154. ret = soc_dai_hw_params(&substream, params, source);
  3155. if (ret < 0)
  3156. goto out;
  3157. }
  3158. substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
  3159. snd_soc_dapm_widget_for_each_sink_path(w, path) {
  3160. sink = path->sink->priv;
  3161. if (sink->driver->ops->startup) {
  3162. ret = sink->driver->ops->startup(&substream,
  3163. sink);
  3164. if (ret < 0) {
  3165. dev_err(sink->dev,
  3166. "ASoC: startup() failed: %d\n",
  3167. ret);
  3168. goto out;
  3169. }
  3170. sink->active++;
  3171. }
  3172. ret = soc_dai_hw_params(&substream, params, sink);
  3173. if (ret < 0)
  3174. goto out;
  3175. }
  3176. break;
  3177. case SND_SOC_DAPM_POST_PMU:
  3178. snd_soc_dapm_widget_for_each_sink_path(w, path) {
  3179. sink = path->sink->priv;
  3180. ret = snd_soc_dai_digital_mute(sink, 0,
  3181. SNDRV_PCM_STREAM_PLAYBACK);
  3182. if (ret != 0 && ret != -ENOTSUPP)
  3183. dev_warn(sink->dev,
  3184. "ASoC: Failed to unmute: %d\n", ret);
  3185. ret = 0;
  3186. }
  3187. break;
  3188. case SND_SOC_DAPM_PRE_PMD:
  3189. snd_soc_dapm_widget_for_each_sink_path(w, path) {
  3190. sink = path->sink->priv;
  3191. ret = snd_soc_dai_digital_mute(sink, 1,
  3192. SNDRV_PCM_STREAM_PLAYBACK);
  3193. if (ret != 0 && ret != -ENOTSUPP)
  3194. dev_warn(sink->dev,
  3195. "ASoC: Failed to mute: %d\n", ret);
  3196. ret = 0;
  3197. }
  3198. substream.stream = SNDRV_PCM_STREAM_CAPTURE;
  3199. snd_soc_dapm_widget_for_each_source_path(w, path) {
  3200. source = path->source->priv;
  3201. if (source->driver->ops->hw_free)
  3202. source->driver->ops->hw_free(&substream,
  3203. source);
  3204. source->active--;
  3205. if (source->driver->ops->shutdown)
  3206. source->driver->ops->shutdown(&substream,
  3207. source);
  3208. }
  3209. substream.stream = SNDRV_PCM_STREAM_PLAYBACK;
  3210. snd_soc_dapm_widget_for_each_sink_path(w, path) {
  3211. sink = path->sink->priv;
  3212. if (sink->driver->ops->hw_free)
  3213. sink->driver->ops->hw_free(&substream, sink);
  3214. sink->active--;
  3215. if (sink->driver->ops->shutdown)
  3216. sink->driver->ops->shutdown(&substream, sink);
  3217. }
  3218. break;
  3219. default:
  3220. WARN(1, "Unknown event %d\n", event);
  3221. ret = -EINVAL;
  3222. }
  3223. out:
  3224. kfree(runtime);
  3225. kfree(params);
  3226. return ret;
  3227. }
  3228. static int snd_soc_dapm_dai_link_get(struct snd_kcontrol *kcontrol,
  3229. struct snd_ctl_elem_value *ucontrol)
  3230. {
  3231. struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
  3232. struct snd_soc_pcm_runtime *rtd = w->priv;
  3233. ucontrol->value.enumerated.item[0] = rtd->params_select;
  3234. return 0;
  3235. }
  3236. static int snd_soc_dapm_dai_link_put(struct snd_kcontrol *kcontrol,
  3237. struct snd_ctl_elem_value *ucontrol)
  3238. {
  3239. struct snd_soc_dapm_widget *w = snd_kcontrol_chip(kcontrol);
  3240. struct snd_soc_pcm_runtime *rtd = w->priv;
  3241. /* Can't change the config when widget is already powered */
  3242. if (w->power)
  3243. return -EBUSY;
  3244. if (ucontrol->value.enumerated.item[0] == rtd->params_select)
  3245. return 0;
  3246. if (ucontrol->value.enumerated.item[0] >= rtd->dai_link->num_params)
  3247. return -EINVAL;
  3248. rtd->params_select = ucontrol->value.enumerated.item[0];
  3249. return 0;
  3250. }
  3251. static void
  3252. snd_soc_dapm_free_kcontrol(struct snd_soc_card *card,
  3253. unsigned long *private_value,
  3254. int num_params,
  3255. const char **w_param_text)
  3256. {
  3257. int count;
  3258. devm_kfree(card->dev, (void *)*private_value);
  3259. for (count = 0 ; count < num_params; count++)
  3260. devm_kfree(card->dev, (void *)w_param_text[count]);
  3261. devm_kfree(card->dev, w_param_text);
  3262. }
  3263. static struct snd_kcontrol_new *
  3264. snd_soc_dapm_alloc_kcontrol(struct snd_soc_card *card,
  3265. char *link_name,
  3266. const struct snd_soc_pcm_stream *params,
  3267. int num_params, const char **w_param_text,
  3268. unsigned long *private_value)
  3269. {
  3270. struct soc_enum w_param_enum[] = {
  3271. SOC_ENUM_SINGLE(0, 0, 0, NULL),
  3272. };
  3273. struct snd_kcontrol_new kcontrol_dai_link[] = {
  3274. SOC_ENUM_EXT(NULL, w_param_enum[0],
  3275. snd_soc_dapm_dai_link_get,
  3276. snd_soc_dapm_dai_link_put),
  3277. };
  3278. struct snd_kcontrol_new *kcontrol_news;
  3279. const struct snd_soc_pcm_stream *config = params;
  3280. int count;
  3281. for (count = 0 ; count < num_params; count++) {
  3282. if (!config->stream_name) {
  3283. dev_warn(card->dapm.dev,
  3284. "ASoC: anonymous config %d for dai link %s\n",
  3285. count, link_name);
  3286. w_param_text[count] =
  3287. devm_kasprintf(card->dev, GFP_KERNEL,
  3288. "Anonymous Configuration %d",
  3289. count);
  3290. } else {
  3291. w_param_text[count] = devm_kmemdup(card->dev,
  3292. config->stream_name,
  3293. strlen(config->stream_name) + 1,
  3294. GFP_KERNEL);
  3295. }
  3296. if (!w_param_text[count])
  3297. goto outfree_w_param;
  3298. config++;
  3299. }
  3300. w_param_enum[0].items = num_params;
  3301. w_param_enum[0].texts = w_param_text;
  3302. *private_value =
  3303. (unsigned long) devm_kmemdup(card->dev,
  3304. (void *)(kcontrol_dai_link[0].private_value),
  3305. sizeof(struct soc_enum), GFP_KERNEL);
  3306. if (!*private_value) {
  3307. dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
  3308. link_name);
  3309. goto outfree_w_param;
  3310. }
  3311. kcontrol_dai_link[0].private_value = *private_value;
  3312. /* duplicate kcontrol_dai_link on heap so that memory persists */
  3313. kcontrol_news = devm_kmemdup(card->dev, &kcontrol_dai_link[0],
  3314. sizeof(struct snd_kcontrol_new),
  3315. GFP_KERNEL);
  3316. if (!kcontrol_news) {
  3317. dev_err(card->dev, "ASoC: Failed to create control for %s widget\n",
  3318. link_name);
  3319. goto outfree_w_param;
  3320. }
  3321. return kcontrol_news;
  3322. outfree_w_param:
  3323. snd_soc_dapm_free_kcontrol(card, private_value, num_params, w_param_text);
  3324. return NULL;
  3325. }
  3326. static struct snd_soc_dapm_widget *
  3327. snd_soc_dapm_new_dai(struct snd_soc_card *card, struct snd_soc_pcm_runtime *rtd,
  3328. struct snd_soc_dapm_widget *source,
  3329. struct snd_soc_dapm_widget *sink)
  3330. {
  3331. struct snd_soc_dapm_widget template;
  3332. struct snd_soc_dapm_widget *w;
  3333. const char **w_param_text;
  3334. unsigned long private_value;
  3335. char *link_name;
  3336. int ret;
  3337. link_name = devm_kasprintf(card->dev, GFP_KERNEL, "%s-%s",
  3338. source->name, sink->name);
  3339. if (!link_name)
  3340. return ERR_PTR(-ENOMEM);
  3341. memset(&template, 0, sizeof(template));
  3342. template.reg = SND_SOC_NOPM;
  3343. template.id = snd_soc_dapm_dai_link;
  3344. template.name = link_name;
  3345. template.event = snd_soc_dai_link_event;
  3346. template.event_flags = SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMU |
  3347. SND_SOC_DAPM_PRE_PMD;
  3348. template.kcontrol_news = NULL;
  3349. /* allocate memory for control, only in case of multiple configs */
  3350. if (rtd->dai_link->num_params > 1) {
  3351. w_param_text = devm_kcalloc(card->dev,
  3352. rtd->dai_link->num_params,
  3353. sizeof(char *), GFP_KERNEL);
  3354. if (!w_param_text) {
  3355. ret = -ENOMEM;
  3356. goto param_fail;
  3357. }
  3358. template.num_kcontrols = 1;
  3359. template.kcontrol_news =
  3360. snd_soc_dapm_alloc_kcontrol(card,
  3361. link_name,
  3362. rtd->dai_link->params,
  3363. rtd->dai_link->num_params,
  3364. w_param_text, &private_value);
  3365. if (!template.kcontrol_news) {
  3366. ret = -ENOMEM;
  3367. goto param_fail;
  3368. }
  3369. } else {
  3370. w_param_text = NULL;
  3371. }
  3372. dev_dbg(card->dev, "ASoC: adding %s widget\n", link_name);
  3373. w = snd_soc_dapm_new_control_unlocked(&card->dapm, &template);
  3374. if (IS_ERR(w)) {
  3375. ret = PTR_ERR(w);
  3376. goto outfree_kcontrol_news;
  3377. }
  3378. w->priv = rtd;
  3379. return w;
  3380. outfree_kcontrol_news:
  3381. devm_kfree(card->dev, (void *)template.kcontrol_news);
  3382. snd_soc_dapm_free_kcontrol(card, &private_value,
  3383. rtd->dai_link->num_params, w_param_text);
  3384. param_fail:
  3385. devm_kfree(card->dev, link_name);
  3386. return ERR_PTR(ret);
  3387. }
  3388. int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
  3389. struct snd_soc_dai *dai)
  3390. {
  3391. struct snd_soc_dapm_widget template;
  3392. struct snd_soc_dapm_widget *w;
  3393. WARN_ON(dapm->dev != dai->dev);
  3394. memset(&template, 0, sizeof(template));
  3395. template.reg = SND_SOC_NOPM;
  3396. if (dai->driver->playback.stream_name) {
  3397. template.id = snd_soc_dapm_dai_in;
  3398. template.name = dai->driver->playback.stream_name;
  3399. template.sname = dai->driver->playback.stream_name;
  3400. dev_dbg(dai->dev, "ASoC: adding %s widget\n",
  3401. template.name);
  3402. w = snd_soc_dapm_new_control_unlocked(dapm, &template);
  3403. if (IS_ERR(w))
  3404. return PTR_ERR(w);
  3405. w->priv = dai;
  3406. dai->playback_widget = w;
  3407. }
  3408. if (dai->driver->capture.stream_name) {
  3409. template.id = snd_soc_dapm_dai_out;
  3410. template.name = dai->driver->capture.stream_name;
  3411. template.sname = dai->driver->capture.stream_name;
  3412. dev_dbg(dai->dev, "ASoC: adding %s widget\n",
  3413. template.name);
  3414. w = snd_soc_dapm_new_control_unlocked(dapm, &template);
  3415. if (IS_ERR(w))
  3416. return PTR_ERR(w);
  3417. w->priv = dai;
  3418. dai->capture_widget = w;
  3419. }
  3420. return 0;
  3421. }
  3422. int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
  3423. {
  3424. struct snd_soc_dapm_widget *dai_w, *w;
  3425. struct snd_soc_dapm_widget *src, *sink;
  3426. struct snd_soc_dai *dai;
  3427. /* For each DAI widget... */
  3428. list_for_each_entry(dai_w, &card->widgets, list) {
  3429. switch (dai_w->id) {
  3430. case snd_soc_dapm_dai_in:
  3431. case snd_soc_dapm_dai_out:
  3432. break;
  3433. default:
  3434. continue;
  3435. }
  3436. /* let users know there is no DAI to link */
  3437. if (!dai_w->priv) {
  3438. dev_dbg(card->dev, "dai widget %s has no DAI\n",
  3439. dai_w->name);
  3440. continue;
  3441. }
  3442. dai = dai_w->priv;
  3443. /* ...find all widgets with the same stream and link them */
  3444. list_for_each_entry(w, &card->widgets, list) {
  3445. if (w->dapm != dai_w->dapm)
  3446. continue;
  3447. switch (w->id) {
  3448. case snd_soc_dapm_dai_in:
  3449. case snd_soc_dapm_dai_out:
  3450. continue;
  3451. default:
  3452. break;
  3453. }
  3454. if (!w->sname || !strstr(w->sname, dai_w->sname))
  3455. continue;
  3456. if (dai_w->id == snd_soc_dapm_dai_in) {
  3457. src = dai_w;
  3458. sink = w;
  3459. } else {
  3460. src = w;
  3461. sink = dai_w;
  3462. }
  3463. dev_dbg(dai->dev, "%s -> %s\n", src->name, sink->name);
  3464. snd_soc_dapm_add_path(w->dapm, src, sink, NULL, NULL);
  3465. }
  3466. }
  3467. return 0;
  3468. }
  3469. static void dapm_connect_dai_link_widgets(struct snd_soc_card *card,
  3470. struct snd_soc_pcm_runtime *rtd)
  3471. {
  3472. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  3473. struct snd_soc_dai *codec_dai;
  3474. struct snd_soc_dapm_widget *playback = NULL, *capture = NULL;
  3475. struct snd_soc_dapm_widget *codec, *playback_cpu, *capture_cpu;
  3476. int i;
  3477. if (rtd->dai_link->params) {
  3478. playback_cpu = cpu_dai->capture_widget;
  3479. capture_cpu = cpu_dai->playback_widget;
  3480. } else {
  3481. playback = cpu_dai->playback_widget;
  3482. capture = cpu_dai->capture_widget;
  3483. playback_cpu = playback;
  3484. capture_cpu = capture;
  3485. }
  3486. for_each_rtd_codec_dai(rtd, i, codec_dai) {
  3487. /* connect BE DAI playback if widgets are valid */
  3488. codec = codec_dai->playback_widget;
  3489. if (playback_cpu && codec) {
  3490. if (!playback) {
  3491. playback = snd_soc_dapm_new_dai(card, rtd,
  3492. playback_cpu,
  3493. codec);
  3494. if (IS_ERR(playback)) {
  3495. dev_err(rtd->dev,
  3496. "ASoC: Failed to create DAI %s: %ld\n",
  3497. codec_dai->name,
  3498. PTR_ERR(playback));
  3499. continue;
  3500. }
  3501. snd_soc_dapm_add_path(&card->dapm, playback_cpu,
  3502. playback, NULL, NULL);
  3503. }
  3504. dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
  3505. cpu_dai->component->name, playback_cpu->name,
  3506. codec_dai->component->name, codec->name);
  3507. snd_soc_dapm_add_path(&card->dapm, playback, codec,
  3508. NULL, NULL);
  3509. }
  3510. }
  3511. for_each_rtd_codec_dai(rtd, i, codec_dai) {
  3512. /* connect BE DAI capture if widgets are valid */
  3513. codec = codec_dai->capture_widget;
  3514. if (codec && capture_cpu) {
  3515. if (!capture) {
  3516. capture = snd_soc_dapm_new_dai(card, rtd,
  3517. codec,
  3518. capture_cpu);
  3519. if (IS_ERR(capture)) {
  3520. dev_err(rtd->dev,
  3521. "ASoC: Failed to create DAI %s: %ld\n",
  3522. codec_dai->name,
  3523. PTR_ERR(capture));
  3524. continue;
  3525. }
  3526. snd_soc_dapm_add_path(&card->dapm, capture,
  3527. capture_cpu, NULL, NULL);
  3528. }
  3529. dev_dbg(rtd->dev, "connected DAI link %s:%s -> %s:%s\n",
  3530. codec_dai->component->name, codec->name,
  3531. cpu_dai->component->name, capture_cpu->name);
  3532. snd_soc_dapm_add_path(&card->dapm, codec, capture,
  3533. NULL, NULL);
  3534. }
  3535. }
  3536. }
  3537. static void soc_dapm_dai_stream_event(struct snd_soc_dai *dai, int stream,
  3538. int event)
  3539. {
  3540. struct snd_soc_dapm_widget *w;
  3541. unsigned int ep;
  3542. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  3543. w = dai->playback_widget;
  3544. else
  3545. w = dai->capture_widget;
  3546. if (w) {
  3547. dapm_mark_dirty(w, "stream event");
  3548. if (w->id == snd_soc_dapm_dai_in) {
  3549. ep = SND_SOC_DAPM_EP_SOURCE;
  3550. dapm_widget_invalidate_input_paths(w);
  3551. } else {
  3552. ep = SND_SOC_DAPM_EP_SINK;
  3553. dapm_widget_invalidate_output_paths(w);
  3554. }
  3555. switch (event) {
  3556. case SND_SOC_DAPM_STREAM_START:
  3557. w->active = 1;
  3558. w->is_ep = ep;
  3559. break;
  3560. case SND_SOC_DAPM_STREAM_STOP:
  3561. w->active = 0;
  3562. w->is_ep = 0;
  3563. break;
  3564. case SND_SOC_DAPM_STREAM_SUSPEND:
  3565. case SND_SOC_DAPM_STREAM_RESUME:
  3566. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  3567. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  3568. break;
  3569. }
  3570. }
  3571. }
  3572. void snd_soc_dapm_connect_dai_link_widgets(struct snd_soc_card *card)
  3573. {
  3574. struct snd_soc_pcm_runtime *rtd;
  3575. /* for each BE DAI link... */
  3576. for_each_card_rtds(card, rtd) {
  3577. /*
  3578. * dynamic FE links have no fixed DAI mapping.
  3579. * CODEC<->CODEC links have no direct connection.
  3580. */
  3581. if (rtd->dai_link->dynamic)
  3582. continue;
  3583. dapm_connect_dai_link_widgets(card, rtd);
  3584. }
  3585. }
  3586. static void soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  3587. int event)
  3588. {
  3589. struct snd_soc_dai *codec_dai;
  3590. int i;
  3591. soc_dapm_dai_stream_event(rtd->cpu_dai, stream, event);
  3592. for_each_rtd_codec_dai(rtd, i, codec_dai)
  3593. soc_dapm_dai_stream_event(codec_dai, stream, event);
  3594. dapm_power_widgets(rtd->card, event);
  3595. }
  3596. /**
  3597. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  3598. * @rtd: PCM runtime data
  3599. * @stream: stream name
  3600. * @event: stream event
  3601. *
  3602. * Sends a stream event to the dapm core. The core then makes any
  3603. * necessary widget power changes.
  3604. *
  3605. * Returns 0 for success else error.
  3606. */
  3607. void snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  3608. int event)
  3609. {
  3610. struct snd_soc_card *card = rtd->card;
  3611. mutex_lock_nested(&card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3612. soc_dapm_stream_event(rtd, stream, event);
  3613. mutex_unlock(&card->dapm_mutex);
  3614. }
  3615. /**
  3616. * snd_soc_dapm_enable_pin_unlocked - enable pin.
  3617. * @dapm: DAPM context
  3618. * @pin: pin name
  3619. *
  3620. * Enables input/output pin and its parents or children widgets iff there is
  3621. * a valid audio route and active audio stream.
  3622. *
  3623. * Requires external locking.
  3624. *
  3625. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3626. * do any widget power switching.
  3627. */
  3628. int snd_soc_dapm_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
  3629. const char *pin)
  3630. {
  3631. return snd_soc_dapm_set_pin(dapm, pin, 1);
  3632. }
  3633. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin_unlocked);
  3634. /**
  3635. * snd_soc_dapm_enable_pin - enable pin.
  3636. * @dapm: DAPM context
  3637. * @pin: pin name
  3638. *
  3639. * Enables input/output pin and its parents or children widgets iff there is
  3640. * a valid audio route and active audio stream.
  3641. *
  3642. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3643. * do any widget power switching.
  3644. */
  3645. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3646. {
  3647. int ret;
  3648. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3649. ret = snd_soc_dapm_set_pin(dapm, pin, 1);
  3650. mutex_unlock(&dapm->card->dapm_mutex);
  3651. return ret;
  3652. }
  3653. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  3654. /**
  3655. * snd_soc_dapm_force_enable_pin_unlocked - force a pin to be enabled
  3656. * @dapm: DAPM context
  3657. * @pin: pin name
  3658. *
  3659. * Enables input/output pin regardless of any other state. This is
  3660. * intended for use with microphone bias supplies used in microphone
  3661. * jack detection.
  3662. *
  3663. * Requires external locking.
  3664. *
  3665. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3666. * do any widget power switching.
  3667. */
  3668. int snd_soc_dapm_force_enable_pin_unlocked(struct snd_soc_dapm_context *dapm,
  3669. const char *pin)
  3670. {
  3671. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3672. if (!w) {
  3673. dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
  3674. return -EINVAL;
  3675. }
  3676. dev_dbg(w->dapm->dev, "ASoC: force enable pin %s\n", pin);
  3677. if (!w->connected) {
  3678. /*
  3679. * w->force does not affect the number of input or output paths,
  3680. * so we only have to recheck if w->connected is changed
  3681. */
  3682. dapm_widget_invalidate_input_paths(w);
  3683. dapm_widget_invalidate_output_paths(w);
  3684. w->connected = 1;
  3685. }
  3686. w->force = 1;
  3687. dapm_mark_dirty(w, "force enable");
  3688. return 0;
  3689. }
  3690. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin_unlocked);
  3691. /**
  3692. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  3693. * @dapm: DAPM context
  3694. * @pin: pin name
  3695. *
  3696. * Enables input/output pin regardless of any other state. This is
  3697. * intended for use with microphone bias supplies used in microphone
  3698. * jack detection.
  3699. *
  3700. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3701. * do any widget power switching.
  3702. */
  3703. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  3704. const char *pin)
  3705. {
  3706. int ret;
  3707. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3708. ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
  3709. mutex_unlock(&dapm->card->dapm_mutex);
  3710. return ret;
  3711. }
  3712. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  3713. /**
  3714. * snd_soc_dapm_disable_pin_unlocked - disable pin.
  3715. * @dapm: DAPM context
  3716. * @pin: pin name
  3717. *
  3718. * Disables input/output pin and its parents or children widgets.
  3719. *
  3720. * Requires external locking.
  3721. *
  3722. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3723. * do any widget power switching.
  3724. */
  3725. int snd_soc_dapm_disable_pin_unlocked(struct snd_soc_dapm_context *dapm,
  3726. const char *pin)
  3727. {
  3728. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3729. }
  3730. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin_unlocked);
  3731. /**
  3732. * snd_soc_dapm_disable_pin - disable pin.
  3733. * @dapm: DAPM context
  3734. * @pin: pin name
  3735. *
  3736. * Disables input/output pin and its parents or children widgets.
  3737. *
  3738. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3739. * do any widget power switching.
  3740. */
  3741. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  3742. const char *pin)
  3743. {
  3744. int ret;
  3745. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3746. ret = snd_soc_dapm_set_pin(dapm, pin, 0);
  3747. mutex_unlock(&dapm->card->dapm_mutex);
  3748. return ret;
  3749. }
  3750. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  3751. /**
  3752. * snd_soc_dapm_nc_pin_unlocked - permanently disable pin.
  3753. * @dapm: DAPM context
  3754. * @pin: pin name
  3755. *
  3756. * Marks the specified pin as being not connected, disabling it along
  3757. * any parent or child widgets. At present this is identical to
  3758. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  3759. * additional things such as disabling controls which only affect
  3760. * paths through the pin.
  3761. *
  3762. * Requires external locking.
  3763. *
  3764. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3765. * do any widget power switching.
  3766. */
  3767. int snd_soc_dapm_nc_pin_unlocked(struct snd_soc_dapm_context *dapm,
  3768. const char *pin)
  3769. {
  3770. return snd_soc_dapm_set_pin(dapm, pin, 0);
  3771. }
  3772. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin_unlocked);
  3773. /**
  3774. * snd_soc_dapm_nc_pin - permanently disable pin.
  3775. * @dapm: DAPM context
  3776. * @pin: pin name
  3777. *
  3778. * Marks the specified pin as being not connected, disabling it along
  3779. * any parent or child widgets. At present this is identical to
  3780. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  3781. * additional things such as disabling controls which only affect
  3782. * paths through the pin.
  3783. *
  3784. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  3785. * do any widget power switching.
  3786. */
  3787. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  3788. {
  3789. int ret;
  3790. mutex_lock_nested(&dapm->card->dapm_mutex, SND_SOC_DAPM_CLASS_RUNTIME);
  3791. ret = snd_soc_dapm_set_pin(dapm, pin, 0);
  3792. mutex_unlock(&dapm->card->dapm_mutex);
  3793. return ret;
  3794. }
  3795. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  3796. /**
  3797. * snd_soc_dapm_get_pin_status - get audio pin status
  3798. * @dapm: DAPM context
  3799. * @pin: audio signal pin endpoint (or start point)
  3800. *
  3801. * Get audio pin status - connected or disconnected.
  3802. *
  3803. * Returns 1 for connected otherwise 0.
  3804. */
  3805. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  3806. const char *pin)
  3807. {
  3808. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  3809. if (w)
  3810. return w->connected;
  3811. return 0;
  3812. }
  3813. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  3814. /**
  3815. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  3816. * @dapm: DAPM context
  3817. * @pin: audio signal pin endpoint (or start point)
  3818. *
  3819. * Mark the given endpoint or pin as ignoring suspend. When the
  3820. * system is disabled a path between two endpoints flagged as ignoring
  3821. * suspend will not be disabled. The path must already be enabled via
  3822. * normal means at suspend time, it will not be turned on if it was not
  3823. * already enabled.
  3824. */
  3825. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  3826. const char *pin)
  3827. {
  3828. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  3829. if (!w) {
  3830. dev_err(dapm->dev, "ASoC: unknown pin %s\n", pin);
  3831. return -EINVAL;
  3832. }
  3833. w->ignore_suspend = 1;
  3834. return 0;
  3835. }
  3836. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  3837. /**
  3838. * snd_soc_dapm_free - free dapm resources
  3839. * @dapm: DAPM context
  3840. *
  3841. * Free all dapm widgets and resources.
  3842. */
  3843. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  3844. {
  3845. dapm_debugfs_cleanup(dapm);
  3846. dapm_free_widgets(dapm);
  3847. list_del(&dapm->list);
  3848. }
  3849. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  3850. static void soc_dapm_shutdown_dapm(struct snd_soc_dapm_context *dapm)
  3851. {
  3852. struct snd_soc_card *card = dapm->card;
  3853. struct snd_soc_dapm_widget *w;
  3854. LIST_HEAD(down_list);
  3855. int powerdown = 0;
  3856. mutex_lock(&card->dapm_mutex);
  3857. list_for_each_entry(w, &dapm->card->widgets, list) {
  3858. if (w->dapm != dapm)
  3859. continue;
  3860. if (w->power) {
  3861. dapm_seq_insert(w, &down_list, false);
  3862. w->power = 0;
  3863. powerdown = 1;
  3864. }
  3865. }
  3866. /* If there were no widgets to power down we're already in
  3867. * standby.
  3868. */
  3869. if (powerdown) {
  3870. if (dapm->bias_level == SND_SOC_BIAS_ON)
  3871. snd_soc_dapm_set_bias_level(dapm,
  3872. SND_SOC_BIAS_PREPARE);
  3873. dapm_seq_run(card, &down_list, 0, false);
  3874. if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
  3875. snd_soc_dapm_set_bias_level(dapm,
  3876. SND_SOC_BIAS_STANDBY);
  3877. }
  3878. mutex_unlock(&card->dapm_mutex);
  3879. }
  3880. /*
  3881. * snd_soc_dapm_shutdown - callback for system shutdown
  3882. */
  3883. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  3884. {
  3885. struct snd_soc_dapm_context *dapm;
  3886. list_for_each_entry(dapm, &card->dapm_list, list) {
  3887. if (dapm != &card->dapm) {
  3888. soc_dapm_shutdown_dapm(dapm);
  3889. if (dapm->bias_level == SND_SOC_BIAS_STANDBY)
  3890. snd_soc_dapm_set_bias_level(dapm,
  3891. SND_SOC_BIAS_OFF);
  3892. }
  3893. }
  3894. soc_dapm_shutdown_dapm(&card->dapm);
  3895. if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  3896. snd_soc_dapm_set_bias_level(&card->dapm,
  3897. SND_SOC_BIAS_OFF);
  3898. }
  3899. /* Module information */
  3900. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  3901. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  3902. MODULE_LICENSE("GPL");