soc-dapm.c 75 KB

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