soc-dapm.c 97 KB

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