soc-dapm.c 115 KB

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