soc-dapm.c 124 KB

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