soc-dapm.c 122 KB

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