soc-dapm.c 65 KB

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