soc-dapm.c 94 KB

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