soc-dapm.c 110 KB

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