soc-dapm.c 89 KB

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