soc-dapm.c 73 KB

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