soc-dapm.c 60 KB

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