soc-dapm.c 124 KB

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