soc-dapm.c 96 KB

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