soc-dapm.c 110 KB

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