soc-dapm.c 66 KB

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