soc-dapm.c 56 KB

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