soc-dapm.c 112 KB

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