soc-dapm.c 94 KB

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