soc-dapm.c 116 KB

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