soc-dapm.c 96 KB

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