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