soc-dapm.c 94 KB

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