soc-dapm.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  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->subseq != b->subseq) {
  640. if (power_up)
  641. return a->subseq - b->subseq;
  642. else
  643. return b->subseq - a->subseq;
  644. }
  645. if (a->reg != b->reg)
  646. return a->reg - b->reg;
  647. if (a->dapm != b->dapm)
  648. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  649. return 0;
  650. }
  651. /* Insert a widget in order into a DAPM power sequence. */
  652. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  653. struct list_head *list,
  654. bool power_up)
  655. {
  656. struct snd_soc_dapm_widget *w;
  657. list_for_each_entry(w, list, power_list)
  658. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  659. list_add_tail(&new_widget->power_list, &w->power_list);
  660. return;
  661. }
  662. list_add_tail(&new_widget->power_list, list);
  663. }
  664. static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
  665. struct snd_soc_dapm_widget *w, int event)
  666. {
  667. struct snd_soc_card *card = dapm->card;
  668. const char *ev_name;
  669. int power, ret;
  670. switch (event) {
  671. case SND_SOC_DAPM_PRE_PMU:
  672. ev_name = "PRE_PMU";
  673. power = 1;
  674. break;
  675. case SND_SOC_DAPM_POST_PMU:
  676. ev_name = "POST_PMU";
  677. power = 1;
  678. break;
  679. case SND_SOC_DAPM_PRE_PMD:
  680. ev_name = "PRE_PMD";
  681. power = 0;
  682. break;
  683. case SND_SOC_DAPM_POST_PMD:
  684. ev_name = "POST_PMD";
  685. power = 0;
  686. break;
  687. default:
  688. BUG();
  689. return;
  690. }
  691. if (w->power != power)
  692. return;
  693. if (w->event && (w->event_flags & event)) {
  694. pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
  695. w->name, ev_name);
  696. trace_snd_soc_dapm_widget_event_start(w, event);
  697. ret = w->event(w, NULL, event);
  698. trace_snd_soc_dapm_widget_event_done(w, event);
  699. if (ret < 0)
  700. pr_err("%s: %s event failed: %d\n",
  701. ev_name, w->name, ret);
  702. }
  703. }
  704. /* Apply the coalesced changes from a DAPM sequence */
  705. static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
  706. struct list_head *pending)
  707. {
  708. struct snd_soc_card *card = dapm->card;
  709. struct snd_soc_dapm_widget *w;
  710. int reg, power;
  711. unsigned int value = 0;
  712. unsigned int mask = 0;
  713. unsigned int cur_mask;
  714. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  715. power_list)->reg;
  716. list_for_each_entry(w, pending, power_list) {
  717. cur_mask = 1 << w->shift;
  718. BUG_ON(reg != w->reg);
  719. if (w->invert)
  720. power = !w->power;
  721. else
  722. power = w->power;
  723. mask |= cur_mask;
  724. if (power)
  725. value |= cur_mask;
  726. pop_dbg(dapm->dev, card->pop_time,
  727. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  728. w->name, reg, value, mask);
  729. /* Check for events */
  730. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
  731. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
  732. }
  733. if (reg >= 0) {
  734. pop_dbg(dapm->dev, card->pop_time,
  735. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  736. value, mask, reg, card->pop_time);
  737. pop_wait(card->pop_time);
  738. snd_soc_update_bits(dapm->codec, reg, mask, value);
  739. }
  740. list_for_each_entry(w, pending, power_list) {
  741. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
  742. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
  743. }
  744. }
  745. /* Apply a DAPM power sequence.
  746. *
  747. * We walk over a pre-sorted list of widgets to apply power to. In
  748. * order to minimise the number of writes to the device required
  749. * multiple widgets will be updated in a single write where possible.
  750. * Currently anything that requires more than a single write is not
  751. * handled.
  752. */
  753. static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
  754. struct list_head *list, int event, bool power_up)
  755. {
  756. struct snd_soc_dapm_widget *w, *n;
  757. LIST_HEAD(pending);
  758. int cur_sort = -1;
  759. int cur_subseq = -1;
  760. int cur_reg = SND_SOC_NOPM;
  761. struct snd_soc_dapm_context *cur_dapm = NULL;
  762. int ret, i;
  763. int *sort;
  764. if (power_up)
  765. sort = dapm_up_seq;
  766. else
  767. sort = dapm_down_seq;
  768. list_for_each_entry_safe(w, n, list, power_list) {
  769. ret = 0;
  770. /* Do we need to apply any queued changes? */
  771. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  772. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  773. if (!list_empty(&pending))
  774. dapm_seq_run_coalesced(cur_dapm, &pending);
  775. if (cur_dapm && cur_dapm->seq_notifier) {
  776. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  777. if (sort[i] == cur_sort)
  778. cur_dapm->seq_notifier(cur_dapm,
  779. i,
  780. cur_subseq);
  781. }
  782. INIT_LIST_HEAD(&pending);
  783. cur_sort = -1;
  784. cur_subseq = -1;
  785. cur_reg = SND_SOC_NOPM;
  786. cur_dapm = NULL;
  787. }
  788. switch (w->id) {
  789. case snd_soc_dapm_pre:
  790. if (!w->event)
  791. list_for_each_entry_safe_continue(w, n, list,
  792. power_list);
  793. if (event == SND_SOC_DAPM_STREAM_START)
  794. ret = w->event(w,
  795. NULL, SND_SOC_DAPM_PRE_PMU);
  796. else if (event == SND_SOC_DAPM_STREAM_STOP)
  797. ret = w->event(w,
  798. NULL, SND_SOC_DAPM_PRE_PMD);
  799. break;
  800. case snd_soc_dapm_post:
  801. if (!w->event)
  802. list_for_each_entry_safe_continue(w, n, list,
  803. power_list);
  804. if (event == SND_SOC_DAPM_STREAM_START)
  805. ret = w->event(w,
  806. NULL, SND_SOC_DAPM_POST_PMU);
  807. else if (event == SND_SOC_DAPM_STREAM_STOP)
  808. ret = w->event(w,
  809. NULL, SND_SOC_DAPM_POST_PMD);
  810. break;
  811. case snd_soc_dapm_input:
  812. case snd_soc_dapm_output:
  813. case snd_soc_dapm_hp:
  814. case snd_soc_dapm_mic:
  815. case snd_soc_dapm_line:
  816. case snd_soc_dapm_spk:
  817. /* No register support currently */
  818. ret = dapm_generic_apply_power(w);
  819. break;
  820. default:
  821. /* Queue it up for application */
  822. cur_sort = sort[w->id];
  823. cur_subseq = w->subseq;
  824. cur_reg = w->reg;
  825. cur_dapm = w->dapm;
  826. list_move(&w->power_list, &pending);
  827. break;
  828. }
  829. if (ret < 0)
  830. dev_err(w->dapm->dev,
  831. "Failed to apply widget power: %d\n", ret);
  832. }
  833. if (!list_empty(&pending))
  834. dapm_seq_run_coalesced(dapm, &pending);
  835. if (cur_dapm && cur_dapm->seq_notifier) {
  836. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  837. if (sort[i] == cur_sort)
  838. cur_dapm->seq_notifier(cur_dapm,
  839. i, cur_subseq);
  840. }
  841. }
  842. static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
  843. {
  844. struct snd_soc_dapm_update *update = dapm->update;
  845. struct snd_soc_dapm_widget *w;
  846. int ret;
  847. if (!update)
  848. return;
  849. w = update->widget;
  850. if (w->event &&
  851. (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  852. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  853. if (ret != 0)
  854. pr_err("%s DAPM pre-event failed: %d\n",
  855. w->name, ret);
  856. }
  857. ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
  858. update->val);
  859. if (ret < 0)
  860. pr_err("%s DAPM update failed: %d\n", w->name, ret);
  861. if (w->event &&
  862. (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  863. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  864. if (ret != 0)
  865. pr_err("%s DAPM post-event failed: %d\n",
  866. w->name, ret);
  867. }
  868. }
  869. /*
  870. * Scan each dapm widget for complete audio path.
  871. * A complete path is a route that has valid endpoints i.e.:-
  872. *
  873. * o DAC to output pin.
  874. * o Input Pin to ADC.
  875. * o Input pin to Output pin (bypass, sidetone)
  876. * o DAC to ADC (loopback).
  877. */
  878. static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
  879. {
  880. struct snd_soc_card *card = dapm->codec->card;
  881. struct snd_soc_dapm_widget *w;
  882. struct snd_soc_dapm_context *d;
  883. LIST_HEAD(up_list);
  884. LIST_HEAD(down_list);
  885. int ret = 0;
  886. int power;
  887. trace_snd_soc_dapm_start(card);
  888. list_for_each_entry(d, &card->dapm_list, list)
  889. if (d->n_widgets)
  890. d->dev_power = 0;
  891. /* Check which widgets we need to power and store them in
  892. * lists indicating if they should be powered up or down.
  893. */
  894. list_for_each_entry(w, &card->widgets, list) {
  895. switch (w->id) {
  896. case snd_soc_dapm_pre:
  897. dapm_seq_insert(w, &down_list, false);
  898. break;
  899. case snd_soc_dapm_post:
  900. dapm_seq_insert(w, &up_list, true);
  901. break;
  902. default:
  903. if (!w->power_check)
  904. continue;
  905. if (!w->force)
  906. power = w->power_check(w);
  907. else
  908. power = 1;
  909. if (power)
  910. w->dapm->dev_power = 1;
  911. if (w->power == power)
  912. continue;
  913. trace_snd_soc_dapm_widget_power(w, power);
  914. if (power)
  915. dapm_seq_insert(w, &up_list, true);
  916. else
  917. dapm_seq_insert(w, &down_list, false);
  918. w->power = power;
  919. break;
  920. }
  921. }
  922. /* If there are no DAPM widgets then try to figure out power from the
  923. * event type.
  924. */
  925. if (!dapm->n_widgets) {
  926. switch (event) {
  927. case SND_SOC_DAPM_STREAM_START:
  928. case SND_SOC_DAPM_STREAM_RESUME:
  929. dapm->dev_power = 1;
  930. break;
  931. case SND_SOC_DAPM_STREAM_STOP:
  932. dapm->dev_power = !!dapm->codec->active;
  933. break;
  934. case SND_SOC_DAPM_STREAM_SUSPEND:
  935. dapm->dev_power = 0;
  936. break;
  937. case SND_SOC_DAPM_STREAM_NOP:
  938. switch (dapm->bias_level) {
  939. case SND_SOC_BIAS_STANDBY:
  940. case SND_SOC_BIAS_OFF:
  941. dapm->dev_power = 0;
  942. break;
  943. default:
  944. dapm->dev_power = 1;
  945. break;
  946. }
  947. break;
  948. default:
  949. break;
  950. }
  951. }
  952. list_for_each_entry(d, &dapm->card->dapm_list, list) {
  953. if (d->dev_power && d->bias_level == SND_SOC_BIAS_OFF) {
  954. ret = snd_soc_dapm_set_bias_level(card, d,
  955. SND_SOC_BIAS_STANDBY);
  956. if (ret != 0)
  957. dev_err(d->dev,
  958. "Failed to turn on bias: %d\n", ret);
  959. }
  960. /* If we're changing to all on or all off then prepare */
  961. if ((d->dev_power && d->bias_level == SND_SOC_BIAS_STANDBY) ||
  962. (!d->dev_power && d->bias_level == SND_SOC_BIAS_ON)) {
  963. ret = snd_soc_dapm_set_bias_level(card, d,
  964. SND_SOC_BIAS_PREPARE);
  965. if (ret != 0)
  966. dev_err(d->dev,
  967. "Failed to prepare bias: %d\n", ret);
  968. }
  969. }
  970. /* Power down widgets first; try to avoid amplifying pops. */
  971. dapm_seq_run(dapm, &down_list, event, false);
  972. dapm_widget_update(dapm);
  973. /* Now power up. */
  974. dapm_seq_run(dapm, &up_list, event, true);
  975. list_for_each_entry(d, &dapm->card->dapm_list, list) {
  976. /* If we just powered the last thing off drop to standby bias */
  977. if (d->bias_level == SND_SOC_BIAS_PREPARE && !d->dev_power) {
  978. ret = snd_soc_dapm_set_bias_level(card, d,
  979. SND_SOC_BIAS_STANDBY);
  980. if (ret != 0)
  981. dev_err(d->dev,
  982. "Failed to apply standby bias: %d\n",
  983. ret);
  984. }
  985. /* If we're in standby and can support bias off then do that */
  986. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  987. d->idle_bias_off) {
  988. ret = snd_soc_dapm_set_bias_level(card, d,
  989. SND_SOC_BIAS_OFF);
  990. if (ret != 0)
  991. dev_err(d->dev,
  992. "Failed to turn off bias: %d\n", ret);
  993. }
  994. /* If we just powered up then move to active bias */
  995. if (d->bias_level == SND_SOC_BIAS_PREPARE && d->dev_power) {
  996. ret = snd_soc_dapm_set_bias_level(card, d,
  997. SND_SOC_BIAS_ON);
  998. if (ret != 0)
  999. dev_err(d->dev,
  1000. "Failed to apply active bias: %d\n",
  1001. ret);
  1002. }
  1003. }
  1004. pop_dbg(dapm->dev, card->pop_time,
  1005. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1006. pop_wait(card->pop_time);
  1007. trace_snd_soc_dapm_done(card);
  1008. return 0;
  1009. }
  1010. #ifdef CONFIG_DEBUG_FS
  1011. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  1012. {
  1013. file->private_data = inode->i_private;
  1014. return 0;
  1015. }
  1016. static ssize_t dapm_widget_power_read_file(struct file *file,
  1017. char __user *user_buf,
  1018. size_t count, loff_t *ppos)
  1019. {
  1020. struct snd_soc_dapm_widget *w = file->private_data;
  1021. char *buf;
  1022. int in, out;
  1023. ssize_t ret;
  1024. struct snd_soc_dapm_path *p = NULL;
  1025. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1026. if (!buf)
  1027. return -ENOMEM;
  1028. in = is_connected_input_ep(w);
  1029. dapm_clear_walk(w->dapm);
  1030. out = is_connected_output_ep(w);
  1031. dapm_clear_walk(w->dapm);
  1032. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  1033. w->name, w->power ? "On" : "Off", in, out);
  1034. if (w->reg >= 0)
  1035. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1036. " - R%d(0x%x) bit %d",
  1037. w->reg, w->reg, w->shift);
  1038. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1039. if (w->sname)
  1040. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1041. w->sname,
  1042. w->active ? "active" : "inactive");
  1043. list_for_each_entry(p, &w->sources, list_sink) {
  1044. if (p->connected && !p->connected(w, p->sink))
  1045. continue;
  1046. if (p->connect)
  1047. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1048. " in %s %s\n",
  1049. p->name ? p->name : "static",
  1050. p->source->name);
  1051. }
  1052. list_for_each_entry(p, &w->sinks, list_source) {
  1053. if (p->connected && !p->connected(w, p->sink))
  1054. continue;
  1055. if (p->connect)
  1056. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1057. " out %s %s\n",
  1058. p->name ? p->name : "static",
  1059. p->sink->name);
  1060. }
  1061. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1062. kfree(buf);
  1063. return ret;
  1064. }
  1065. static const struct file_operations dapm_widget_power_fops = {
  1066. .open = dapm_widget_power_open_file,
  1067. .read = dapm_widget_power_read_file,
  1068. .llseek = default_llseek,
  1069. };
  1070. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  1071. {
  1072. struct snd_soc_dapm_widget *w;
  1073. struct dentry *d;
  1074. if (!dapm->debugfs_dapm)
  1075. return;
  1076. list_for_each_entry(w, &dapm->card->widgets, list) {
  1077. if (!w->name || w->dapm != dapm)
  1078. continue;
  1079. d = debugfs_create_file(w->name, 0444,
  1080. dapm->debugfs_dapm, w,
  1081. &dapm_widget_power_fops);
  1082. if (!d)
  1083. dev_warn(w->dapm->dev,
  1084. "ASoC: Failed to create %s debugfs file\n",
  1085. w->name);
  1086. }
  1087. }
  1088. #else
  1089. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  1090. {
  1091. }
  1092. #endif
  1093. /* test and update the power status of a mux widget */
  1094. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1095. struct snd_kcontrol *kcontrol, int change,
  1096. int mux, struct soc_enum *e)
  1097. {
  1098. struct snd_soc_dapm_path *path;
  1099. int found = 0;
  1100. if (widget->id != snd_soc_dapm_mux &&
  1101. widget->id != snd_soc_dapm_virt_mux &&
  1102. widget->id != snd_soc_dapm_value_mux)
  1103. return -ENODEV;
  1104. if (!change)
  1105. return 0;
  1106. /* find dapm widget path assoc with kcontrol */
  1107. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1108. if (path->kcontrol != kcontrol)
  1109. continue;
  1110. if (!path->name || !e->texts[mux])
  1111. continue;
  1112. found = 1;
  1113. /* we now need to match the string in the enum to the path */
  1114. if (!(strcmp(path->name, e->texts[mux])))
  1115. path->connect = 1; /* new connection */
  1116. else
  1117. path->connect = 0; /* old connection must be powered down */
  1118. }
  1119. if (found)
  1120. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1121. return 0;
  1122. }
  1123. /* test and update the power status of a mixer or switch widget */
  1124. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1125. struct snd_kcontrol *kcontrol, int connect)
  1126. {
  1127. struct snd_soc_dapm_path *path;
  1128. int found = 0;
  1129. if (widget->id != snd_soc_dapm_mixer &&
  1130. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1131. widget->id != snd_soc_dapm_switch)
  1132. return -ENODEV;
  1133. /* find dapm widget path assoc with kcontrol */
  1134. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1135. if (path->kcontrol != kcontrol)
  1136. continue;
  1137. /* found, now check type */
  1138. found = 1;
  1139. path->connect = connect;
  1140. break;
  1141. }
  1142. if (found)
  1143. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1144. return 0;
  1145. }
  1146. /* show dapm widget status in sys fs */
  1147. static ssize_t dapm_widget_show(struct device *dev,
  1148. struct device_attribute *attr, char *buf)
  1149. {
  1150. struct snd_soc_pcm_runtime *rtd =
  1151. container_of(dev, struct snd_soc_pcm_runtime, dev);
  1152. struct snd_soc_codec *codec =rtd->codec;
  1153. struct snd_soc_dapm_widget *w;
  1154. int count = 0;
  1155. char *state = "not set";
  1156. list_for_each_entry(w, &codec->card->widgets, list) {
  1157. if (w->dapm != &codec->dapm)
  1158. continue;
  1159. /* only display widgets that burnm power */
  1160. switch (w->id) {
  1161. case snd_soc_dapm_hp:
  1162. case snd_soc_dapm_mic:
  1163. case snd_soc_dapm_spk:
  1164. case snd_soc_dapm_line:
  1165. case snd_soc_dapm_micbias:
  1166. case snd_soc_dapm_dac:
  1167. case snd_soc_dapm_adc:
  1168. case snd_soc_dapm_pga:
  1169. case snd_soc_dapm_out_drv:
  1170. case snd_soc_dapm_mixer:
  1171. case snd_soc_dapm_mixer_named_ctl:
  1172. case snd_soc_dapm_supply:
  1173. if (w->name)
  1174. count += sprintf(buf + count, "%s: %s\n",
  1175. w->name, w->power ? "On":"Off");
  1176. break;
  1177. default:
  1178. break;
  1179. }
  1180. }
  1181. switch (codec->dapm.bias_level) {
  1182. case SND_SOC_BIAS_ON:
  1183. state = "On";
  1184. break;
  1185. case SND_SOC_BIAS_PREPARE:
  1186. state = "Prepare";
  1187. break;
  1188. case SND_SOC_BIAS_STANDBY:
  1189. state = "Standby";
  1190. break;
  1191. case SND_SOC_BIAS_OFF:
  1192. state = "Off";
  1193. break;
  1194. }
  1195. count += sprintf(buf + count, "PM State: %s\n", state);
  1196. return count;
  1197. }
  1198. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1199. int snd_soc_dapm_sys_add(struct device *dev)
  1200. {
  1201. return device_create_file(dev, &dev_attr_dapm_widget);
  1202. }
  1203. static void snd_soc_dapm_sys_remove(struct device *dev)
  1204. {
  1205. device_remove_file(dev, &dev_attr_dapm_widget);
  1206. }
  1207. /* free all dapm widgets and resources */
  1208. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1209. {
  1210. struct snd_soc_dapm_widget *w, *next_w;
  1211. struct snd_soc_dapm_path *p, *next_p;
  1212. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1213. if (w->dapm != dapm)
  1214. continue;
  1215. list_del(&w->list);
  1216. /*
  1217. * remove source and sink paths associated to this widget.
  1218. * While removing the path, remove reference to it from both
  1219. * source and sink widgets so that path is removed only once.
  1220. */
  1221. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1222. list_del(&p->list_sink);
  1223. list_del(&p->list_source);
  1224. list_del(&p->list);
  1225. kfree(p->long_name);
  1226. kfree(p);
  1227. }
  1228. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1229. list_del(&p->list_sink);
  1230. list_del(&p->list_source);
  1231. list_del(&p->list);
  1232. kfree(p->long_name);
  1233. kfree(p);
  1234. }
  1235. kfree(w->name);
  1236. kfree(w);
  1237. }
  1238. }
  1239. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1240. const char *pin, int status)
  1241. {
  1242. struct snd_soc_dapm_widget *w;
  1243. list_for_each_entry(w, &dapm->card->widgets, list) {
  1244. if (w->dapm != dapm)
  1245. continue;
  1246. if (!strcmp(w->name, pin)) {
  1247. dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
  1248. pin, status);
  1249. w->connected = status;
  1250. /* Allow disabling of forced pins */
  1251. if (status == 0)
  1252. w->force = 0;
  1253. return 0;
  1254. }
  1255. }
  1256. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1257. return -EINVAL;
  1258. }
  1259. /**
  1260. * snd_soc_dapm_sync - scan and power dapm paths
  1261. * @dapm: DAPM context
  1262. *
  1263. * Walks all dapm audio paths and powers widgets according to their
  1264. * stream or path usage.
  1265. *
  1266. * Returns 0 for success.
  1267. */
  1268. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1269. {
  1270. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1271. }
  1272. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1273. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1274. const struct snd_soc_dapm_route *route)
  1275. {
  1276. struct snd_soc_dapm_path *path;
  1277. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1278. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1279. const char *sink;
  1280. const char *control = route->control;
  1281. const char *source;
  1282. char prefixed_sink[80];
  1283. char prefixed_source[80];
  1284. int ret = 0;
  1285. if (dapm->codec->name_prefix) {
  1286. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1287. dapm->codec->name_prefix, route->sink);
  1288. sink = prefixed_sink;
  1289. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1290. dapm->codec->name_prefix, route->source);
  1291. source = prefixed_source;
  1292. } else {
  1293. sink = route->sink;
  1294. source = route->source;
  1295. }
  1296. /*
  1297. * find src and dest widgets over all widgets but favor a widget from
  1298. * current DAPM context
  1299. */
  1300. list_for_each_entry(w, &dapm->card->widgets, list) {
  1301. if (!wsink && !(strcmp(w->name, sink))) {
  1302. wtsink = w;
  1303. if (w->dapm == dapm)
  1304. wsink = w;
  1305. continue;
  1306. }
  1307. if (!wsource && !(strcmp(w->name, source))) {
  1308. wtsource = w;
  1309. if (w->dapm == dapm)
  1310. wsource = w;
  1311. }
  1312. }
  1313. /* use widget from another DAPM context if not found from this */
  1314. if (!wsink)
  1315. wsink = wtsink;
  1316. if (!wsource)
  1317. wsource = wtsource;
  1318. if (wsource == NULL || wsink == NULL)
  1319. return -ENODEV;
  1320. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1321. if (!path)
  1322. return -ENOMEM;
  1323. path->source = wsource;
  1324. path->sink = wsink;
  1325. path->connected = route->connected;
  1326. INIT_LIST_HEAD(&path->list);
  1327. INIT_LIST_HEAD(&path->list_source);
  1328. INIT_LIST_HEAD(&path->list_sink);
  1329. /* check for external widgets */
  1330. if (wsink->id == snd_soc_dapm_input) {
  1331. if (wsource->id == snd_soc_dapm_micbias ||
  1332. wsource->id == snd_soc_dapm_mic ||
  1333. wsource->id == snd_soc_dapm_line ||
  1334. wsource->id == snd_soc_dapm_output)
  1335. wsink->ext = 1;
  1336. }
  1337. if (wsource->id == snd_soc_dapm_output) {
  1338. if (wsink->id == snd_soc_dapm_spk ||
  1339. wsink->id == snd_soc_dapm_hp ||
  1340. wsink->id == snd_soc_dapm_line ||
  1341. wsink->id == snd_soc_dapm_input)
  1342. wsource->ext = 1;
  1343. }
  1344. /* connect static paths */
  1345. if (control == NULL) {
  1346. list_add(&path->list, &dapm->card->paths);
  1347. list_add(&path->list_sink, &wsink->sources);
  1348. list_add(&path->list_source, &wsource->sinks);
  1349. path->connect = 1;
  1350. return 0;
  1351. }
  1352. /* connect dynamic paths */
  1353. switch(wsink->id) {
  1354. case snd_soc_dapm_adc:
  1355. case snd_soc_dapm_dac:
  1356. case snd_soc_dapm_pga:
  1357. case snd_soc_dapm_out_drv:
  1358. case snd_soc_dapm_input:
  1359. case snd_soc_dapm_output:
  1360. case snd_soc_dapm_micbias:
  1361. case snd_soc_dapm_vmid:
  1362. case snd_soc_dapm_pre:
  1363. case snd_soc_dapm_post:
  1364. case snd_soc_dapm_supply:
  1365. case snd_soc_dapm_aif_in:
  1366. case snd_soc_dapm_aif_out:
  1367. list_add(&path->list, &dapm->card->paths);
  1368. list_add(&path->list_sink, &wsink->sources);
  1369. list_add(&path->list_source, &wsource->sinks);
  1370. path->connect = 1;
  1371. return 0;
  1372. case snd_soc_dapm_mux:
  1373. case snd_soc_dapm_virt_mux:
  1374. case snd_soc_dapm_value_mux:
  1375. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1376. &wsink->kcontrols[0]);
  1377. if (ret != 0)
  1378. goto err;
  1379. break;
  1380. case snd_soc_dapm_switch:
  1381. case snd_soc_dapm_mixer:
  1382. case snd_soc_dapm_mixer_named_ctl:
  1383. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1384. if (ret != 0)
  1385. goto err;
  1386. break;
  1387. case snd_soc_dapm_hp:
  1388. case snd_soc_dapm_mic:
  1389. case snd_soc_dapm_line:
  1390. case snd_soc_dapm_spk:
  1391. list_add(&path->list, &dapm->card->paths);
  1392. list_add(&path->list_sink, &wsink->sources);
  1393. list_add(&path->list_source, &wsource->sinks);
  1394. path->connect = 0;
  1395. return 0;
  1396. }
  1397. return 0;
  1398. err:
  1399. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1400. source, control, sink);
  1401. kfree(path);
  1402. return ret;
  1403. }
  1404. /**
  1405. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1406. * @dapm: DAPM context
  1407. * @route: audio routes
  1408. * @num: number of routes
  1409. *
  1410. * Connects 2 dapm widgets together via a named audio path. The sink is
  1411. * the widget receiving the audio signal, whilst the source is the sender
  1412. * of the audio signal.
  1413. *
  1414. * Returns 0 for success else error. On error all resources can be freed
  1415. * with a call to snd_soc_card_free().
  1416. */
  1417. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1418. const struct snd_soc_dapm_route *route, int num)
  1419. {
  1420. int i, ret;
  1421. for (i = 0; i < num; i++) {
  1422. ret = snd_soc_dapm_add_route(dapm, route);
  1423. if (ret < 0) {
  1424. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1425. route->source, route->sink);
  1426. return ret;
  1427. }
  1428. route++;
  1429. }
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1433. /**
  1434. * snd_soc_dapm_new_widgets - add new dapm widgets
  1435. * @dapm: DAPM context
  1436. *
  1437. * Checks the codec for any new dapm widgets and creates them if found.
  1438. *
  1439. * Returns 0 for success.
  1440. */
  1441. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1442. {
  1443. struct snd_soc_dapm_widget *w;
  1444. list_for_each_entry(w, &dapm->card->widgets, list)
  1445. {
  1446. if (w->new)
  1447. continue;
  1448. switch(w->id) {
  1449. case snd_soc_dapm_switch:
  1450. case snd_soc_dapm_mixer:
  1451. case snd_soc_dapm_mixer_named_ctl:
  1452. w->power_check = dapm_generic_check_power;
  1453. dapm_new_mixer(dapm, w);
  1454. break;
  1455. case snd_soc_dapm_mux:
  1456. case snd_soc_dapm_virt_mux:
  1457. case snd_soc_dapm_value_mux:
  1458. w->power_check = dapm_generic_check_power;
  1459. dapm_new_mux(dapm, w);
  1460. break;
  1461. case snd_soc_dapm_adc:
  1462. case snd_soc_dapm_aif_out:
  1463. w->power_check = dapm_adc_check_power;
  1464. break;
  1465. case snd_soc_dapm_dac:
  1466. case snd_soc_dapm_aif_in:
  1467. w->power_check = dapm_dac_check_power;
  1468. break;
  1469. case snd_soc_dapm_pga:
  1470. case snd_soc_dapm_out_drv:
  1471. w->power_check = dapm_generic_check_power;
  1472. dapm_new_pga(dapm, w);
  1473. break;
  1474. case snd_soc_dapm_input:
  1475. case snd_soc_dapm_output:
  1476. case snd_soc_dapm_micbias:
  1477. case snd_soc_dapm_spk:
  1478. case snd_soc_dapm_hp:
  1479. case snd_soc_dapm_mic:
  1480. case snd_soc_dapm_line:
  1481. w->power_check = dapm_generic_check_power;
  1482. break;
  1483. case snd_soc_dapm_supply:
  1484. w->power_check = dapm_supply_check_power;
  1485. case snd_soc_dapm_vmid:
  1486. case snd_soc_dapm_pre:
  1487. case snd_soc_dapm_post:
  1488. break;
  1489. }
  1490. w->new = 1;
  1491. }
  1492. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1493. return 0;
  1494. }
  1495. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1496. /**
  1497. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1498. * @kcontrol: mixer control
  1499. * @ucontrol: control element information
  1500. *
  1501. * Callback to get the value of a dapm mixer control.
  1502. *
  1503. * Returns 0 for success.
  1504. */
  1505. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1506. struct snd_ctl_elem_value *ucontrol)
  1507. {
  1508. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1509. struct soc_mixer_control *mc =
  1510. (struct soc_mixer_control *)kcontrol->private_value;
  1511. unsigned int reg = mc->reg;
  1512. unsigned int shift = mc->shift;
  1513. unsigned int rshift = mc->rshift;
  1514. int max = mc->max;
  1515. unsigned int invert = mc->invert;
  1516. unsigned int mask = (1 << fls(max)) - 1;
  1517. ucontrol->value.integer.value[0] =
  1518. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1519. if (shift != rshift)
  1520. ucontrol->value.integer.value[1] =
  1521. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1522. if (invert) {
  1523. ucontrol->value.integer.value[0] =
  1524. max - ucontrol->value.integer.value[0];
  1525. if (shift != rshift)
  1526. ucontrol->value.integer.value[1] =
  1527. max - ucontrol->value.integer.value[1];
  1528. }
  1529. return 0;
  1530. }
  1531. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1532. /**
  1533. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1534. * @kcontrol: mixer control
  1535. * @ucontrol: control element information
  1536. *
  1537. * Callback to set the value of a dapm mixer control.
  1538. *
  1539. * Returns 0 for success.
  1540. */
  1541. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1542. struct snd_ctl_elem_value *ucontrol)
  1543. {
  1544. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1545. struct soc_mixer_control *mc =
  1546. (struct soc_mixer_control *)kcontrol->private_value;
  1547. unsigned int reg = mc->reg;
  1548. unsigned int shift = mc->shift;
  1549. int max = mc->max;
  1550. unsigned int mask = (1 << fls(max)) - 1;
  1551. unsigned int invert = mc->invert;
  1552. unsigned int val;
  1553. int connect, change;
  1554. struct snd_soc_dapm_update update;
  1555. val = (ucontrol->value.integer.value[0] & mask);
  1556. if (invert)
  1557. val = max - val;
  1558. mask = mask << shift;
  1559. val = val << shift;
  1560. mutex_lock(&widget->codec->mutex);
  1561. widget->value = val;
  1562. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  1563. if (change) {
  1564. if (val)
  1565. /* new connection */
  1566. connect = invert ? 0:1;
  1567. else
  1568. /* old connection must be powered down */
  1569. connect = invert ? 1:0;
  1570. update.kcontrol = kcontrol;
  1571. update.widget = widget;
  1572. update.reg = reg;
  1573. update.mask = mask;
  1574. update.val = val;
  1575. widget->dapm->update = &update;
  1576. dapm_mixer_update_power(widget, kcontrol, connect);
  1577. widget->dapm->update = NULL;
  1578. }
  1579. mutex_unlock(&widget->codec->mutex);
  1580. return 0;
  1581. }
  1582. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1583. /**
  1584. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1585. * @kcontrol: mixer control
  1586. * @ucontrol: control element information
  1587. *
  1588. * Callback to get the value of a dapm enumerated double mixer control.
  1589. *
  1590. * Returns 0 for success.
  1591. */
  1592. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1593. struct snd_ctl_elem_value *ucontrol)
  1594. {
  1595. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1596. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1597. unsigned int val, bitmask;
  1598. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1599. ;
  1600. val = snd_soc_read(widget->codec, e->reg);
  1601. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1602. if (e->shift_l != e->shift_r)
  1603. ucontrol->value.enumerated.item[1] =
  1604. (val >> e->shift_r) & (bitmask - 1);
  1605. return 0;
  1606. }
  1607. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1608. /**
  1609. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1610. * @kcontrol: mixer control
  1611. * @ucontrol: control element information
  1612. *
  1613. * Callback to set the value of a dapm enumerated double mixer control.
  1614. *
  1615. * Returns 0 for success.
  1616. */
  1617. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1618. struct snd_ctl_elem_value *ucontrol)
  1619. {
  1620. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1621. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1622. unsigned int val, mux, change;
  1623. unsigned int mask, bitmask;
  1624. struct snd_soc_dapm_update update;
  1625. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1626. ;
  1627. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1628. return -EINVAL;
  1629. mux = ucontrol->value.enumerated.item[0];
  1630. val = mux << e->shift_l;
  1631. mask = (bitmask - 1) << e->shift_l;
  1632. if (e->shift_l != e->shift_r) {
  1633. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1634. return -EINVAL;
  1635. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1636. mask |= (bitmask - 1) << e->shift_r;
  1637. }
  1638. mutex_lock(&widget->codec->mutex);
  1639. widget->value = val;
  1640. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1641. update.kcontrol = kcontrol;
  1642. update.widget = widget;
  1643. update.reg = e->reg;
  1644. update.mask = mask;
  1645. update.val = val;
  1646. widget->dapm->update = &update;
  1647. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1648. widget->dapm->update = NULL;
  1649. mutex_unlock(&widget->codec->mutex);
  1650. return change;
  1651. }
  1652. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1653. /**
  1654. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1655. * @kcontrol: mixer control
  1656. * @ucontrol: control element information
  1657. *
  1658. * Returns 0 for success.
  1659. */
  1660. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1661. struct snd_ctl_elem_value *ucontrol)
  1662. {
  1663. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1664. ucontrol->value.enumerated.item[0] = widget->value;
  1665. return 0;
  1666. }
  1667. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1668. /**
  1669. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  1670. * @kcontrol: mixer control
  1671. * @ucontrol: control element information
  1672. *
  1673. * Returns 0 for success.
  1674. */
  1675. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  1676. struct snd_ctl_elem_value *ucontrol)
  1677. {
  1678. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1679. struct soc_enum *e =
  1680. (struct soc_enum *)kcontrol->private_value;
  1681. int change;
  1682. int ret = 0;
  1683. if (ucontrol->value.enumerated.item[0] >= e->max)
  1684. return -EINVAL;
  1685. mutex_lock(&widget->codec->mutex);
  1686. change = widget->value != ucontrol->value.enumerated.item[0];
  1687. widget->value = ucontrol->value.enumerated.item[0];
  1688. dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
  1689. mutex_unlock(&widget->codec->mutex);
  1690. return ret;
  1691. }
  1692. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  1693. /**
  1694. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1695. * callback
  1696. * @kcontrol: mixer control
  1697. * @ucontrol: control element information
  1698. *
  1699. * Callback to get the value of a dapm semi enumerated double mixer control.
  1700. *
  1701. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1702. * used for handling bitfield coded enumeration for example.
  1703. *
  1704. * Returns 0 for success.
  1705. */
  1706. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1707. struct snd_ctl_elem_value *ucontrol)
  1708. {
  1709. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1710. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1711. unsigned int reg_val, val, mux;
  1712. reg_val = snd_soc_read(widget->codec, e->reg);
  1713. val = (reg_val >> e->shift_l) & e->mask;
  1714. for (mux = 0; mux < e->max; mux++) {
  1715. if (val == e->values[mux])
  1716. break;
  1717. }
  1718. ucontrol->value.enumerated.item[0] = mux;
  1719. if (e->shift_l != e->shift_r) {
  1720. val = (reg_val >> e->shift_r) & e->mask;
  1721. for (mux = 0; mux < e->max; mux++) {
  1722. if (val == e->values[mux])
  1723. break;
  1724. }
  1725. ucontrol->value.enumerated.item[1] = mux;
  1726. }
  1727. return 0;
  1728. }
  1729. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1730. /**
  1731. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1732. * callback
  1733. * @kcontrol: mixer control
  1734. * @ucontrol: control element information
  1735. *
  1736. * Callback to set the value of a dapm semi enumerated double mixer control.
  1737. *
  1738. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1739. * used for handling bitfield coded enumeration for example.
  1740. *
  1741. * Returns 0 for success.
  1742. */
  1743. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1744. struct snd_ctl_elem_value *ucontrol)
  1745. {
  1746. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1747. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1748. unsigned int val, mux, change;
  1749. unsigned int mask;
  1750. struct snd_soc_dapm_update update;
  1751. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1752. return -EINVAL;
  1753. mux = ucontrol->value.enumerated.item[0];
  1754. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1755. mask = e->mask << e->shift_l;
  1756. if (e->shift_l != e->shift_r) {
  1757. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1758. return -EINVAL;
  1759. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1760. mask |= e->mask << e->shift_r;
  1761. }
  1762. mutex_lock(&widget->codec->mutex);
  1763. widget->value = val;
  1764. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1765. update.kcontrol = kcontrol;
  1766. update.widget = widget;
  1767. update.reg = e->reg;
  1768. update.mask = mask;
  1769. update.val = val;
  1770. widget->dapm->update = &update;
  1771. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1772. widget->dapm->update = NULL;
  1773. mutex_unlock(&widget->codec->mutex);
  1774. return change;
  1775. }
  1776. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1777. /**
  1778. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1779. *
  1780. * @kcontrol: mixer control
  1781. * @uinfo: control element information
  1782. *
  1783. * Callback to provide information about a pin switch control.
  1784. */
  1785. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1786. struct snd_ctl_elem_info *uinfo)
  1787. {
  1788. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1789. uinfo->count = 1;
  1790. uinfo->value.integer.min = 0;
  1791. uinfo->value.integer.max = 1;
  1792. return 0;
  1793. }
  1794. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1795. /**
  1796. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1797. *
  1798. * @kcontrol: mixer control
  1799. * @ucontrol: Value
  1800. */
  1801. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1802. struct snd_ctl_elem_value *ucontrol)
  1803. {
  1804. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1805. const char *pin = (const char *)kcontrol->private_value;
  1806. mutex_lock(&codec->mutex);
  1807. ucontrol->value.integer.value[0] =
  1808. snd_soc_dapm_get_pin_status(&codec->dapm, pin);
  1809. mutex_unlock(&codec->mutex);
  1810. return 0;
  1811. }
  1812. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1813. /**
  1814. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1815. *
  1816. * @kcontrol: mixer control
  1817. * @ucontrol: Value
  1818. */
  1819. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1820. struct snd_ctl_elem_value *ucontrol)
  1821. {
  1822. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1823. const char *pin = (const char *)kcontrol->private_value;
  1824. mutex_lock(&codec->mutex);
  1825. if (ucontrol->value.integer.value[0])
  1826. snd_soc_dapm_enable_pin(&codec->dapm, pin);
  1827. else
  1828. snd_soc_dapm_disable_pin(&codec->dapm, pin);
  1829. snd_soc_dapm_sync(&codec->dapm);
  1830. mutex_unlock(&codec->mutex);
  1831. return 0;
  1832. }
  1833. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1834. /**
  1835. * snd_soc_dapm_new_control - create new dapm control
  1836. * @dapm: DAPM context
  1837. * @widget: widget template
  1838. *
  1839. * Creates a new dapm control based upon the template.
  1840. *
  1841. * Returns 0 for success else error.
  1842. */
  1843. int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  1844. const struct snd_soc_dapm_widget *widget)
  1845. {
  1846. struct snd_soc_dapm_widget *w;
  1847. size_t name_len;
  1848. if ((w = dapm_cnew_widget(widget)) == NULL)
  1849. return -ENOMEM;
  1850. name_len = strlen(widget->name) + 1;
  1851. if (dapm->codec->name_prefix)
  1852. name_len += 1 + strlen(dapm->codec->name_prefix);
  1853. w->name = kmalloc(name_len, GFP_KERNEL);
  1854. if (w->name == NULL) {
  1855. kfree(w);
  1856. return -ENOMEM;
  1857. }
  1858. if (dapm->codec->name_prefix)
  1859. snprintf(w->name, name_len, "%s %s",
  1860. dapm->codec->name_prefix, widget->name);
  1861. else
  1862. snprintf(w->name, name_len, "%s", widget->name);
  1863. dapm->n_widgets++;
  1864. w->dapm = dapm;
  1865. w->codec = dapm->codec;
  1866. INIT_LIST_HEAD(&w->sources);
  1867. INIT_LIST_HEAD(&w->sinks);
  1868. INIT_LIST_HEAD(&w->list);
  1869. list_add(&w->list, &dapm->card->widgets);
  1870. /* machine layer set ups unconnected pins and insertions */
  1871. w->connected = 1;
  1872. return 0;
  1873. }
  1874. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1875. /**
  1876. * snd_soc_dapm_new_controls - create new dapm controls
  1877. * @dapm: DAPM context
  1878. * @widget: widget array
  1879. * @num: number of widgets
  1880. *
  1881. * Creates new DAPM controls based upon the templates.
  1882. *
  1883. * Returns 0 for success else error.
  1884. */
  1885. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  1886. const struct snd_soc_dapm_widget *widget,
  1887. int num)
  1888. {
  1889. int i, ret;
  1890. for (i = 0; i < num; i++) {
  1891. ret = snd_soc_dapm_new_control(dapm, widget);
  1892. if (ret < 0) {
  1893. dev_err(dapm->dev,
  1894. "ASoC: Failed to create DAPM control %s: %d\n",
  1895. widget->name, ret);
  1896. return ret;
  1897. }
  1898. widget++;
  1899. }
  1900. return 0;
  1901. }
  1902. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1903. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  1904. const char *stream, int event)
  1905. {
  1906. struct snd_soc_dapm_widget *w;
  1907. list_for_each_entry(w, &dapm->card->widgets, list)
  1908. {
  1909. if (!w->sname || w->dapm != dapm)
  1910. continue;
  1911. dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
  1912. w->name, w->sname, stream, event);
  1913. if (strstr(w->sname, stream)) {
  1914. switch(event) {
  1915. case SND_SOC_DAPM_STREAM_START:
  1916. w->active = 1;
  1917. break;
  1918. case SND_SOC_DAPM_STREAM_STOP:
  1919. w->active = 0;
  1920. break;
  1921. case SND_SOC_DAPM_STREAM_SUSPEND:
  1922. case SND_SOC_DAPM_STREAM_RESUME:
  1923. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1924. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1925. break;
  1926. }
  1927. }
  1928. }
  1929. dapm_power_widgets(dapm, event);
  1930. }
  1931. /**
  1932. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1933. * @rtd: PCM runtime data
  1934. * @stream: stream name
  1935. * @event: stream event
  1936. *
  1937. * Sends a stream event to the dapm core. The core then makes any
  1938. * necessary widget power changes.
  1939. *
  1940. * Returns 0 for success else error.
  1941. */
  1942. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
  1943. const char *stream, int event)
  1944. {
  1945. struct snd_soc_codec *codec = rtd->codec;
  1946. if (stream == NULL)
  1947. return 0;
  1948. mutex_lock(&codec->mutex);
  1949. soc_dapm_stream_event(&codec->dapm, stream, event);
  1950. mutex_unlock(&codec->mutex);
  1951. return 0;
  1952. }
  1953. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1954. /**
  1955. * snd_soc_dapm_enable_pin - enable pin.
  1956. * @dapm: DAPM context
  1957. * @pin: pin name
  1958. *
  1959. * Enables input/output pin and its parents or children widgets iff there is
  1960. * a valid audio route and active audio stream.
  1961. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1962. * do any widget power switching.
  1963. */
  1964. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  1965. {
  1966. return snd_soc_dapm_set_pin(dapm, pin, 1);
  1967. }
  1968. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1969. /**
  1970. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  1971. * @dapm: DAPM context
  1972. * @pin: pin name
  1973. *
  1974. * Enables input/output pin regardless of any other state. This is
  1975. * intended for use with microphone bias supplies used in microphone
  1976. * jack detection.
  1977. *
  1978. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1979. * do any widget power switching.
  1980. */
  1981. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  1982. const char *pin)
  1983. {
  1984. struct snd_soc_dapm_widget *w;
  1985. list_for_each_entry(w, &dapm->card->widgets, list) {
  1986. if (w->dapm != dapm)
  1987. continue;
  1988. if (!strcmp(w->name, pin)) {
  1989. dev_dbg(w->dapm->dev,
  1990. "dapm: force enable pin %s\n", pin);
  1991. w->connected = 1;
  1992. w->force = 1;
  1993. return 0;
  1994. }
  1995. }
  1996. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1997. return -EINVAL;
  1998. }
  1999. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  2000. /**
  2001. * snd_soc_dapm_disable_pin - disable pin.
  2002. * @dapm: DAPM context
  2003. * @pin: pin name
  2004. *
  2005. * Disables input/output pin and its parents or children widgets.
  2006. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2007. * do any widget power switching.
  2008. */
  2009. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  2010. const char *pin)
  2011. {
  2012. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2013. }
  2014. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  2015. /**
  2016. * snd_soc_dapm_nc_pin - permanently disable pin.
  2017. * @dapm: DAPM context
  2018. * @pin: pin name
  2019. *
  2020. * Marks the specified pin as being not connected, disabling it along
  2021. * any parent or child widgets. At present this is identical to
  2022. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  2023. * additional things such as disabling controls which only affect
  2024. * paths through the pin.
  2025. *
  2026. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2027. * do any widget power switching.
  2028. */
  2029. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2030. {
  2031. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2032. }
  2033. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  2034. /**
  2035. * snd_soc_dapm_get_pin_status - get audio pin status
  2036. * @dapm: DAPM context
  2037. * @pin: audio signal pin endpoint (or start point)
  2038. *
  2039. * Get audio pin status - connected or disconnected.
  2040. *
  2041. * Returns 1 for connected otherwise 0.
  2042. */
  2043. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  2044. const char *pin)
  2045. {
  2046. struct snd_soc_dapm_widget *w;
  2047. list_for_each_entry(w, &dapm->card->widgets, list) {
  2048. if (w->dapm != dapm)
  2049. continue;
  2050. if (!strcmp(w->name, pin))
  2051. return w->connected;
  2052. }
  2053. return 0;
  2054. }
  2055. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  2056. /**
  2057. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  2058. * @dapm: DAPM context
  2059. * @pin: audio signal pin endpoint (or start point)
  2060. *
  2061. * Mark the given endpoint or pin as ignoring suspend. When the
  2062. * system is disabled a path between two endpoints flagged as ignoring
  2063. * suspend will not be disabled. The path must already be enabled via
  2064. * normal means at suspend time, it will not be turned on if it was not
  2065. * already enabled.
  2066. */
  2067. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  2068. const char *pin)
  2069. {
  2070. struct snd_soc_dapm_widget *w;
  2071. list_for_each_entry(w, &dapm->card->widgets, list) {
  2072. if (w->dapm != dapm)
  2073. continue;
  2074. if (!strcmp(w->name, pin)) {
  2075. w->ignore_suspend = 1;
  2076. return 0;
  2077. }
  2078. }
  2079. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2080. return -EINVAL;
  2081. }
  2082. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  2083. /**
  2084. * snd_soc_dapm_free - free dapm resources
  2085. * @card: SoC device
  2086. *
  2087. * Free all dapm widgets and resources.
  2088. */
  2089. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  2090. {
  2091. snd_soc_dapm_sys_remove(dapm->dev);
  2092. dapm_free_widgets(dapm);
  2093. list_del(&dapm->list);
  2094. }
  2095. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  2096. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  2097. {
  2098. struct snd_soc_dapm_widget *w;
  2099. LIST_HEAD(down_list);
  2100. int powerdown = 0;
  2101. list_for_each_entry(w, &dapm->card->widgets, list) {
  2102. if (w->dapm != dapm)
  2103. continue;
  2104. if (w->power) {
  2105. dapm_seq_insert(w, &down_list, false);
  2106. w->power = 0;
  2107. powerdown = 1;
  2108. }
  2109. }
  2110. /* If there were no widgets to power down we're already in
  2111. * standby.
  2112. */
  2113. if (powerdown) {
  2114. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
  2115. dapm_seq_run(dapm, &down_list, 0, false);
  2116. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
  2117. }
  2118. }
  2119. /*
  2120. * snd_soc_dapm_shutdown - callback for system shutdown
  2121. */
  2122. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2123. {
  2124. struct snd_soc_codec *codec;
  2125. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2126. soc_dapm_shutdown_codec(&codec->dapm);
  2127. snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
  2128. }
  2129. }
  2130. /* Module information */
  2131. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2132. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2133. MODULE_LICENSE("GPL");