soc-dapm.c 79 KB

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