soc-dapm.c 93 KB

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