soc-dapm.c 57 KB

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