soc-dapm.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309
  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_STOP:
  848. sys_power = !!dapm->codec->active;
  849. break;
  850. case SND_SOC_DAPM_STREAM_SUSPEND:
  851. sys_power = 0;
  852. break;
  853. case SND_SOC_DAPM_STREAM_NOP:
  854. switch (dapm->bias_level) {
  855. case SND_SOC_BIAS_STANDBY:
  856. case SND_SOC_BIAS_OFF:
  857. sys_power = 0;
  858. break;
  859. default:
  860. sys_power = 1;
  861. break;
  862. }
  863. break;
  864. default:
  865. break;
  866. }
  867. }
  868. if (sys_power && dapm->bias_level == SND_SOC_BIAS_OFF) {
  869. ret = snd_soc_dapm_set_bias_level(card, dapm,
  870. SND_SOC_BIAS_STANDBY);
  871. if (ret != 0)
  872. dev_err(dapm->dev,
  873. "Failed to turn on bias: %d\n", ret);
  874. }
  875. /* If we're changing to all on or all off then prepare */
  876. if ((sys_power && dapm->bias_level == SND_SOC_BIAS_STANDBY) ||
  877. (!sys_power && dapm->bias_level == SND_SOC_BIAS_ON)) {
  878. ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_PREPARE);
  879. if (ret != 0)
  880. dev_err(dapm->dev,
  881. "Failed to prepare bias: %d\n", ret);
  882. }
  883. /* Power down widgets first; try to avoid amplifying pops. */
  884. dapm_seq_run(dapm, &down_list, event, dapm_down_seq);
  885. /* Now power up. */
  886. dapm_seq_run(dapm, &up_list, event, dapm_up_seq);
  887. /* If we just powered the last thing off drop to standby bias */
  888. if (dapm->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
  889. ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_STANDBY);
  890. if (ret != 0)
  891. dev_err(dapm->dev,
  892. "Failed to apply standby bias: %d\n", ret);
  893. }
  894. /* If we're in standby and can support bias off then do that */
  895. if (dapm->bias_level == SND_SOC_BIAS_STANDBY &&
  896. dapm->idle_bias_off) {
  897. ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_OFF);
  898. if (ret != 0)
  899. dev_err(dapm->dev,
  900. "Failed to turn off bias: %d\n", ret);
  901. }
  902. /* If we just powered up then move to active bias */
  903. if (dapm->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
  904. ret = snd_soc_dapm_set_bias_level(card, dapm, SND_SOC_BIAS_ON);
  905. if (ret != 0)
  906. dev_err(dapm->dev,
  907. "Failed to apply active bias: %d\n", ret);
  908. }
  909. pop_dbg(dapm->dev, card->pop_time,
  910. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  911. pop_wait(card->pop_time);
  912. trace_snd_soc_dapm_done(card);
  913. return 0;
  914. }
  915. #ifdef CONFIG_DEBUG_FS
  916. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  917. {
  918. file->private_data = inode->i_private;
  919. return 0;
  920. }
  921. static ssize_t dapm_widget_power_read_file(struct file *file,
  922. char __user *user_buf,
  923. size_t count, loff_t *ppos)
  924. {
  925. struct snd_soc_dapm_widget *w = file->private_data;
  926. char *buf;
  927. int in, out;
  928. ssize_t ret;
  929. struct snd_soc_dapm_path *p = NULL;
  930. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  931. if (!buf)
  932. return -ENOMEM;
  933. in = is_connected_input_ep(w);
  934. dapm_clear_walk(w->dapm);
  935. out = is_connected_output_ep(w);
  936. dapm_clear_walk(w->dapm);
  937. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  938. w->name, w->power ? "On" : "Off", in, out);
  939. if (w->reg >= 0)
  940. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  941. " - R%d(0x%x) bit %d",
  942. w->reg, w->reg, w->shift);
  943. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  944. if (w->sname)
  945. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  946. w->sname,
  947. w->active ? "active" : "inactive");
  948. list_for_each_entry(p, &w->sources, list_sink) {
  949. if (p->connected && !p->connected(w, p->sink))
  950. continue;
  951. if (p->connect)
  952. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  953. " in %s %s\n",
  954. p->name ? p->name : "static",
  955. p->source->name);
  956. }
  957. list_for_each_entry(p, &w->sinks, list_source) {
  958. if (p->connected && !p->connected(w, p->sink))
  959. continue;
  960. if (p->connect)
  961. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  962. " out %s %s\n",
  963. p->name ? p->name : "static",
  964. p->sink->name);
  965. }
  966. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  967. kfree(buf);
  968. return ret;
  969. }
  970. static const struct file_operations dapm_widget_power_fops = {
  971. .open = dapm_widget_power_open_file,
  972. .read = dapm_widget_power_read_file,
  973. .llseek = default_llseek,
  974. };
  975. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  976. {
  977. struct snd_soc_dapm_widget *w;
  978. struct dentry *d;
  979. if (!dapm->debugfs_dapm)
  980. return;
  981. list_for_each_entry(w, &dapm->widgets, list) {
  982. if (!w->name)
  983. continue;
  984. d = debugfs_create_file(w->name, 0444,
  985. dapm->debugfs_dapm, w,
  986. &dapm_widget_power_fops);
  987. if (!d)
  988. dev_warn(w->dapm->dev,
  989. "ASoC: Failed to create %s debugfs file\n",
  990. w->name);
  991. }
  992. }
  993. #else
  994. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm)
  995. {
  996. }
  997. #endif
  998. /* test and update the power status of a mux widget */
  999. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1000. struct snd_kcontrol *kcontrol, int change,
  1001. int mux, struct soc_enum *e)
  1002. {
  1003. struct snd_soc_dapm_path *path;
  1004. int found = 0;
  1005. if (widget->id != snd_soc_dapm_mux &&
  1006. widget->id != snd_soc_dapm_value_mux)
  1007. return -ENODEV;
  1008. if (!change)
  1009. return 0;
  1010. /* find dapm widget path assoc with kcontrol */
  1011. list_for_each_entry(path, &widget->dapm->paths, list) {
  1012. if (path->kcontrol != kcontrol)
  1013. continue;
  1014. if (!path->name || !e->texts[mux])
  1015. continue;
  1016. found = 1;
  1017. /* we now need to match the string in the enum to the path */
  1018. if (!(strcmp(path->name, e->texts[mux])))
  1019. path->connect = 1; /* new connection */
  1020. else
  1021. path->connect = 0; /* old connection must be powered down */
  1022. }
  1023. if (found)
  1024. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1025. return 0;
  1026. }
  1027. /* test and update the power status of a mixer or switch widget */
  1028. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1029. struct snd_kcontrol *kcontrol, int connect)
  1030. {
  1031. struct snd_soc_dapm_path *path;
  1032. int found = 0;
  1033. if (widget->id != snd_soc_dapm_mixer &&
  1034. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1035. widget->id != snd_soc_dapm_switch)
  1036. return -ENODEV;
  1037. /* find dapm widget path assoc with kcontrol */
  1038. list_for_each_entry(path, &widget->dapm->paths, list) {
  1039. if (path->kcontrol != kcontrol)
  1040. continue;
  1041. /* found, now check type */
  1042. found = 1;
  1043. path->connect = connect;
  1044. break;
  1045. }
  1046. if (found)
  1047. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1048. return 0;
  1049. }
  1050. /* show dapm widget status in sys fs */
  1051. static ssize_t dapm_widget_show(struct device *dev,
  1052. struct device_attribute *attr, char *buf)
  1053. {
  1054. struct snd_soc_pcm_runtime *rtd =
  1055. container_of(dev, struct snd_soc_pcm_runtime, dev);
  1056. struct snd_soc_codec *codec =rtd->codec;
  1057. struct snd_soc_dapm_widget *w;
  1058. int count = 0;
  1059. char *state = "not set";
  1060. list_for_each_entry(w, &codec->dapm.widgets, list) {
  1061. /* only display widgets that burnm power */
  1062. switch (w->id) {
  1063. case snd_soc_dapm_hp:
  1064. case snd_soc_dapm_mic:
  1065. case snd_soc_dapm_spk:
  1066. case snd_soc_dapm_line:
  1067. case snd_soc_dapm_micbias:
  1068. case snd_soc_dapm_dac:
  1069. case snd_soc_dapm_adc:
  1070. case snd_soc_dapm_pga:
  1071. case snd_soc_dapm_mixer:
  1072. case snd_soc_dapm_mixer_named_ctl:
  1073. case snd_soc_dapm_supply:
  1074. if (w->name)
  1075. count += sprintf(buf + count, "%s: %s\n",
  1076. w->name, w->power ? "On":"Off");
  1077. break;
  1078. default:
  1079. break;
  1080. }
  1081. }
  1082. switch (codec->dapm.bias_level) {
  1083. case SND_SOC_BIAS_ON:
  1084. state = "On";
  1085. break;
  1086. case SND_SOC_BIAS_PREPARE:
  1087. state = "Prepare";
  1088. break;
  1089. case SND_SOC_BIAS_STANDBY:
  1090. state = "Standby";
  1091. break;
  1092. case SND_SOC_BIAS_OFF:
  1093. state = "Off";
  1094. break;
  1095. }
  1096. count += sprintf(buf + count, "PM State: %s\n", state);
  1097. return count;
  1098. }
  1099. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1100. int snd_soc_dapm_sys_add(struct device *dev)
  1101. {
  1102. return device_create_file(dev, &dev_attr_dapm_widget);
  1103. }
  1104. static void snd_soc_dapm_sys_remove(struct device *dev)
  1105. {
  1106. device_remove_file(dev, &dev_attr_dapm_widget);
  1107. }
  1108. /* free all dapm widgets and resources */
  1109. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1110. {
  1111. struct snd_soc_dapm_widget *w, *next_w;
  1112. struct snd_soc_dapm_path *p, *next_p;
  1113. list_for_each_entry_safe(w, next_w, &dapm->widgets, list) {
  1114. list_del(&w->list);
  1115. kfree(w->name);
  1116. kfree(w);
  1117. }
  1118. list_for_each_entry_safe(p, next_p, &dapm->paths, list) {
  1119. list_del(&p->list);
  1120. kfree(p->long_name);
  1121. kfree(p);
  1122. }
  1123. }
  1124. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1125. const char *pin, int status)
  1126. {
  1127. struct snd_soc_dapm_widget *w;
  1128. list_for_each_entry(w, &dapm->widgets, list) {
  1129. if (!strcmp(w->name, pin)) {
  1130. dev_dbg(w->dapm->dev, "dapm: pin %s = %d\n",
  1131. pin, status);
  1132. w->connected = status;
  1133. /* Allow disabling of forced pins */
  1134. if (status == 0)
  1135. w->force = 0;
  1136. return 0;
  1137. }
  1138. }
  1139. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1140. return -EINVAL;
  1141. }
  1142. /**
  1143. * snd_soc_dapm_sync - scan and power dapm paths
  1144. * @dapm: DAPM context
  1145. *
  1146. * Walks all dapm audio paths and powers widgets according to their
  1147. * stream or path usage.
  1148. *
  1149. * Returns 0 for success.
  1150. */
  1151. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1152. {
  1153. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1154. }
  1155. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1156. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1157. const struct snd_soc_dapm_route *route)
  1158. {
  1159. struct snd_soc_dapm_path *path;
  1160. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1161. const char *sink;
  1162. const char *control = route->control;
  1163. const char *source;
  1164. char prefixed_sink[80];
  1165. char prefixed_source[80];
  1166. int ret = 0;
  1167. if (dapm->codec->name_prefix) {
  1168. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1169. dapm->codec->name_prefix, route->sink);
  1170. sink = prefixed_sink;
  1171. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1172. dapm->codec->name_prefix, route->source);
  1173. source = prefixed_source;
  1174. } else {
  1175. sink = route->sink;
  1176. source = route->source;
  1177. }
  1178. /* find src and dest widgets */
  1179. list_for_each_entry(w, &dapm->widgets, list) {
  1180. if (!wsink && !(strcmp(w->name, sink))) {
  1181. wsink = w;
  1182. continue;
  1183. }
  1184. if (!wsource && !(strcmp(w->name, source))) {
  1185. wsource = w;
  1186. }
  1187. }
  1188. if (wsource == NULL || wsink == NULL)
  1189. return -ENODEV;
  1190. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1191. if (!path)
  1192. return -ENOMEM;
  1193. path->source = wsource;
  1194. path->sink = wsink;
  1195. path->connected = route->connected;
  1196. INIT_LIST_HEAD(&path->list);
  1197. INIT_LIST_HEAD(&path->list_source);
  1198. INIT_LIST_HEAD(&path->list_sink);
  1199. /* check for external widgets */
  1200. if (wsink->id == snd_soc_dapm_input) {
  1201. if (wsource->id == snd_soc_dapm_micbias ||
  1202. wsource->id == snd_soc_dapm_mic ||
  1203. wsource->id == snd_soc_dapm_line ||
  1204. wsource->id == snd_soc_dapm_output)
  1205. wsink->ext = 1;
  1206. }
  1207. if (wsource->id == snd_soc_dapm_output) {
  1208. if (wsink->id == snd_soc_dapm_spk ||
  1209. wsink->id == snd_soc_dapm_hp ||
  1210. wsink->id == snd_soc_dapm_line ||
  1211. wsink->id == snd_soc_dapm_input)
  1212. wsource->ext = 1;
  1213. }
  1214. /* connect static paths */
  1215. if (control == NULL) {
  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. }
  1222. /* connect dynamic paths */
  1223. switch(wsink->id) {
  1224. case snd_soc_dapm_adc:
  1225. case snd_soc_dapm_dac:
  1226. case snd_soc_dapm_pga:
  1227. case snd_soc_dapm_input:
  1228. case snd_soc_dapm_output:
  1229. case snd_soc_dapm_micbias:
  1230. case snd_soc_dapm_vmid:
  1231. case snd_soc_dapm_pre:
  1232. case snd_soc_dapm_post:
  1233. case snd_soc_dapm_supply:
  1234. case snd_soc_dapm_aif_in:
  1235. case snd_soc_dapm_aif_out:
  1236. list_add(&path->list, &dapm->paths);
  1237. list_add(&path->list_sink, &wsink->sources);
  1238. list_add(&path->list_source, &wsource->sinks);
  1239. path->connect = 1;
  1240. return 0;
  1241. case snd_soc_dapm_mux:
  1242. case snd_soc_dapm_value_mux:
  1243. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1244. &wsink->kcontrols[0]);
  1245. if (ret != 0)
  1246. goto err;
  1247. break;
  1248. case snd_soc_dapm_switch:
  1249. case snd_soc_dapm_mixer:
  1250. case snd_soc_dapm_mixer_named_ctl:
  1251. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1252. if (ret != 0)
  1253. goto err;
  1254. break;
  1255. case snd_soc_dapm_hp:
  1256. case snd_soc_dapm_mic:
  1257. case snd_soc_dapm_line:
  1258. case snd_soc_dapm_spk:
  1259. list_add(&path->list, &dapm->paths);
  1260. list_add(&path->list_sink, &wsink->sources);
  1261. list_add(&path->list_source, &wsource->sinks);
  1262. path->connect = 0;
  1263. return 0;
  1264. }
  1265. return 0;
  1266. err:
  1267. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1268. source, control, sink);
  1269. kfree(path);
  1270. return ret;
  1271. }
  1272. /**
  1273. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1274. * @dapm: DAPM context
  1275. * @route: audio routes
  1276. * @num: number of routes
  1277. *
  1278. * Connects 2 dapm widgets together via a named audio path. The sink is
  1279. * the widget receiving the audio signal, whilst the source is the sender
  1280. * of the audio signal.
  1281. *
  1282. * Returns 0 for success else error. On error all resources can be freed
  1283. * with a call to snd_soc_card_free().
  1284. */
  1285. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1286. const struct snd_soc_dapm_route *route, int num)
  1287. {
  1288. int i, ret;
  1289. for (i = 0; i < num; i++) {
  1290. ret = snd_soc_dapm_add_route(dapm, route);
  1291. if (ret < 0) {
  1292. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1293. route->source, route->sink);
  1294. return ret;
  1295. }
  1296. route++;
  1297. }
  1298. return 0;
  1299. }
  1300. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1301. /**
  1302. * snd_soc_dapm_new_widgets - add new dapm widgets
  1303. * @dapm: DAPM context
  1304. *
  1305. * Checks the codec for any new dapm widgets and creates them if found.
  1306. *
  1307. * Returns 0 for success.
  1308. */
  1309. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1310. {
  1311. struct snd_soc_dapm_widget *w;
  1312. list_for_each_entry(w, &dapm->widgets, list)
  1313. {
  1314. if (w->new)
  1315. continue;
  1316. switch(w->id) {
  1317. case snd_soc_dapm_switch:
  1318. case snd_soc_dapm_mixer:
  1319. case snd_soc_dapm_mixer_named_ctl:
  1320. w->power_check = dapm_generic_check_power;
  1321. dapm_new_mixer(dapm, w);
  1322. break;
  1323. case snd_soc_dapm_mux:
  1324. case snd_soc_dapm_value_mux:
  1325. w->power_check = dapm_generic_check_power;
  1326. dapm_new_mux(dapm, w);
  1327. break;
  1328. case snd_soc_dapm_adc:
  1329. case snd_soc_dapm_aif_out:
  1330. w->power_check = dapm_adc_check_power;
  1331. break;
  1332. case snd_soc_dapm_dac:
  1333. case snd_soc_dapm_aif_in:
  1334. w->power_check = dapm_dac_check_power;
  1335. break;
  1336. case snd_soc_dapm_pga:
  1337. w->power_check = dapm_generic_check_power;
  1338. dapm_new_pga(dapm, w);
  1339. break;
  1340. case snd_soc_dapm_input:
  1341. case snd_soc_dapm_output:
  1342. case snd_soc_dapm_micbias:
  1343. case snd_soc_dapm_spk:
  1344. case snd_soc_dapm_hp:
  1345. case snd_soc_dapm_mic:
  1346. case snd_soc_dapm_line:
  1347. w->power_check = dapm_generic_check_power;
  1348. break;
  1349. case snd_soc_dapm_supply:
  1350. w->power_check = dapm_supply_check_power;
  1351. case snd_soc_dapm_vmid:
  1352. case snd_soc_dapm_pre:
  1353. case snd_soc_dapm_post:
  1354. break;
  1355. }
  1356. w->new = 1;
  1357. }
  1358. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1359. return 0;
  1360. }
  1361. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1362. /**
  1363. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1364. * @kcontrol: mixer control
  1365. * @ucontrol: control element information
  1366. *
  1367. * Callback to get the value of a dapm mixer control.
  1368. *
  1369. * Returns 0 for success.
  1370. */
  1371. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1372. struct snd_ctl_elem_value *ucontrol)
  1373. {
  1374. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1375. struct soc_mixer_control *mc =
  1376. (struct soc_mixer_control *)kcontrol->private_value;
  1377. unsigned int reg = mc->reg;
  1378. unsigned int shift = mc->shift;
  1379. unsigned int rshift = mc->rshift;
  1380. int max = mc->max;
  1381. unsigned int invert = mc->invert;
  1382. unsigned int mask = (1 << fls(max)) - 1;
  1383. ucontrol->value.integer.value[0] =
  1384. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1385. if (shift != rshift)
  1386. ucontrol->value.integer.value[1] =
  1387. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1388. if (invert) {
  1389. ucontrol->value.integer.value[0] =
  1390. max - ucontrol->value.integer.value[0];
  1391. if (shift != rshift)
  1392. ucontrol->value.integer.value[1] =
  1393. max - ucontrol->value.integer.value[1];
  1394. }
  1395. return 0;
  1396. }
  1397. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1398. /**
  1399. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1400. * @kcontrol: mixer control
  1401. * @ucontrol: control element information
  1402. *
  1403. * Callback to set the value of a dapm mixer control.
  1404. *
  1405. * Returns 0 for success.
  1406. */
  1407. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1408. struct snd_ctl_elem_value *ucontrol)
  1409. {
  1410. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1411. struct soc_mixer_control *mc =
  1412. (struct soc_mixer_control *)kcontrol->private_value;
  1413. unsigned int reg = mc->reg;
  1414. unsigned int shift = mc->shift;
  1415. unsigned int rshift = mc->rshift;
  1416. int max = mc->max;
  1417. unsigned int mask = (1 << fls(max)) - 1;
  1418. unsigned int invert = mc->invert;
  1419. unsigned int val, val2, val_mask;
  1420. int connect;
  1421. int ret;
  1422. val = (ucontrol->value.integer.value[0] & mask);
  1423. if (invert)
  1424. val = max - val;
  1425. val_mask = mask << shift;
  1426. val = val << shift;
  1427. if (shift != rshift) {
  1428. val2 = (ucontrol->value.integer.value[1] & mask);
  1429. if (invert)
  1430. val2 = max - val2;
  1431. val_mask |= mask << rshift;
  1432. val |= val2 << rshift;
  1433. }
  1434. mutex_lock(&widget->codec->mutex);
  1435. widget->value = val;
  1436. if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
  1437. if (val)
  1438. /* new connection */
  1439. connect = invert ? 0:1;
  1440. else
  1441. /* old connection must be powered down */
  1442. connect = invert ? 1:0;
  1443. dapm_mixer_update_power(widget, kcontrol, connect);
  1444. }
  1445. if (widget->event) {
  1446. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1447. ret = widget->event(widget, kcontrol,
  1448. SND_SOC_DAPM_PRE_REG);
  1449. if (ret < 0) {
  1450. ret = 1;
  1451. goto out;
  1452. }
  1453. }
  1454. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1455. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1456. ret = widget->event(widget, kcontrol,
  1457. SND_SOC_DAPM_POST_REG);
  1458. } else
  1459. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1460. out:
  1461. mutex_unlock(&widget->codec->mutex);
  1462. return ret;
  1463. }
  1464. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1465. /**
  1466. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1467. * @kcontrol: mixer control
  1468. * @ucontrol: control element information
  1469. *
  1470. * Callback to get the value of a dapm enumerated double mixer control.
  1471. *
  1472. * Returns 0 for success.
  1473. */
  1474. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1475. struct snd_ctl_elem_value *ucontrol)
  1476. {
  1477. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1478. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1479. unsigned int val, bitmask;
  1480. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1481. ;
  1482. val = snd_soc_read(widget->codec, e->reg);
  1483. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1484. if (e->shift_l != e->shift_r)
  1485. ucontrol->value.enumerated.item[1] =
  1486. (val >> e->shift_r) & (bitmask - 1);
  1487. return 0;
  1488. }
  1489. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1490. /**
  1491. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1492. * @kcontrol: mixer control
  1493. * @ucontrol: control element information
  1494. *
  1495. * Callback to set the value of a dapm enumerated double mixer control.
  1496. *
  1497. * Returns 0 for success.
  1498. */
  1499. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1500. struct snd_ctl_elem_value *ucontrol)
  1501. {
  1502. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1503. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1504. unsigned int val, mux, change;
  1505. unsigned int mask, bitmask;
  1506. int ret = 0;
  1507. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1508. ;
  1509. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1510. return -EINVAL;
  1511. mux = ucontrol->value.enumerated.item[0];
  1512. val = mux << e->shift_l;
  1513. mask = (bitmask - 1) << e->shift_l;
  1514. if (e->shift_l != e->shift_r) {
  1515. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1516. return -EINVAL;
  1517. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1518. mask |= (bitmask - 1) << e->shift_r;
  1519. }
  1520. mutex_lock(&widget->codec->mutex);
  1521. widget->value = val;
  1522. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1523. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1524. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1525. ret = widget->event(widget,
  1526. kcontrol, SND_SOC_DAPM_PRE_REG);
  1527. if (ret < 0)
  1528. goto out;
  1529. }
  1530. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1531. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1532. ret = widget->event(widget,
  1533. kcontrol, SND_SOC_DAPM_POST_REG);
  1534. out:
  1535. mutex_unlock(&widget->codec->mutex);
  1536. return ret;
  1537. }
  1538. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1539. /**
  1540. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1541. * @kcontrol: mixer control
  1542. * @ucontrol: control element information
  1543. *
  1544. * Returns 0 for success.
  1545. */
  1546. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1547. struct snd_ctl_elem_value *ucontrol)
  1548. {
  1549. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1550. ucontrol->value.enumerated.item[0] = widget->value;
  1551. return 0;
  1552. }
  1553. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1554. /**
  1555. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  1556. * @kcontrol: mixer control
  1557. * @ucontrol: control element information
  1558. *
  1559. * Returns 0 for success.
  1560. */
  1561. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  1562. struct snd_ctl_elem_value *ucontrol)
  1563. {
  1564. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1565. struct soc_enum *e =
  1566. (struct soc_enum *)kcontrol->private_value;
  1567. int change;
  1568. int ret = 0;
  1569. if (ucontrol->value.enumerated.item[0] >= e->max)
  1570. return -EINVAL;
  1571. mutex_lock(&widget->codec->mutex);
  1572. change = widget->value != ucontrol->value.enumerated.item[0];
  1573. widget->value = ucontrol->value.enumerated.item[0];
  1574. dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
  1575. mutex_unlock(&widget->codec->mutex);
  1576. return ret;
  1577. }
  1578. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  1579. /**
  1580. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1581. * callback
  1582. * @kcontrol: mixer control
  1583. * @ucontrol: control element information
  1584. *
  1585. * Callback to get the value of a dapm semi enumerated double mixer control.
  1586. *
  1587. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1588. * used for handling bitfield coded enumeration for example.
  1589. *
  1590. * Returns 0 for success.
  1591. */
  1592. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1593. struct snd_ctl_elem_value *ucontrol)
  1594. {
  1595. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1596. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1597. unsigned int reg_val, val, mux;
  1598. reg_val = snd_soc_read(widget->codec, e->reg);
  1599. val = (reg_val >> e->shift_l) & e->mask;
  1600. for (mux = 0; mux < e->max; mux++) {
  1601. if (val == e->values[mux])
  1602. break;
  1603. }
  1604. ucontrol->value.enumerated.item[0] = mux;
  1605. if (e->shift_l != e->shift_r) {
  1606. val = (reg_val >> e->shift_r) & e->mask;
  1607. for (mux = 0; mux < e->max; mux++) {
  1608. if (val == e->values[mux])
  1609. break;
  1610. }
  1611. ucontrol->value.enumerated.item[1] = mux;
  1612. }
  1613. return 0;
  1614. }
  1615. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1616. /**
  1617. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1618. * callback
  1619. * @kcontrol: mixer control
  1620. * @ucontrol: control element information
  1621. *
  1622. * Callback to set the value of a dapm semi enumerated double mixer control.
  1623. *
  1624. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1625. * used for handling bitfield coded enumeration for example.
  1626. *
  1627. * Returns 0 for success.
  1628. */
  1629. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1630. struct snd_ctl_elem_value *ucontrol)
  1631. {
  1632. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1633. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1634. unsigned int val, mux, change;
  1635. unsigned int mask;
  1636. int ret = 0;
  1637. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1638. return -EINVAL;
  1639. mux = ucontrol->value.enumerated.item[0];
  1640. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1641. mask = e->mask << e->shift_l;
  1642. if (e->shift_l != e->shift_r) {
  1643. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1644. return -EINVAL;
  1645. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1646. mask |= e->mask << e->shift_r;
  1647. }
  1648. mutex_lock(&widget->codec->mutex);
  1649. widget->value = val;
  1650. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1651. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1652. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1653. ret = widget->event(widget,
  1654. kcontrol, SND_SOC_DAPM_PRE_REG);
  1655. if (ret < 0)
  1656. goto out;
  1657. }
  1658. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1659. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1660. ret = widget->event(widget,
  1661. kcontrol, SND_SOC_DAPM_POST_REG);
  1662. out:
  1663. mutex_unlock(&widget->codec->mutex);
  1664. return ret;
  1665. }
  1666. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1667. /**
  1668. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1669. *
  1670. * @kcontrol: mixer control
  1671. * @uinfo: control element information
  1672. *
  1673. * Callback to provide information about a pin switch control.
  1674. */
  1675. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1676. struct snd_ctl_elem_info *uinfo)
  1677. {
  1678. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1679. uinfo->count = 1;
  1680. uinfo->value.integer.min = 0;
  1681. uinfo->value.integer.max = 1;
  1682. return 0;
  1683. }
  1684. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1685. /**
  1686. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1687. *
  1688. * @kcontrol: mixer control
  1689. * @ucontrol: Value
  1690. */
  1691. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1692. struct snd_ctl_elem_value *ucontrol)
  1693. {
  1694. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1695. const char *pin = (const char *)kcontrol->private_value;
  1696. mutex_lock(&codec->mutex);
  1697. ucontrol->value.integer.value[0] =
  1698. snd_soc_dapm_get_pin_status(&codec->dapm, pin);
  1699. mutex_unlock(&codec->mutex);
  1700. return 0;
  1701. }
  1702. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1703. /**
  1704. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1705. *
  1706. * @kcontrol: mixer control
  1707. * @ucontrol: Value
  1708. */
  1709. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1710. struct snd_ctl_elem_value *ucontrol)
  1711. {
  1712. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1713. const char *pin = (const char *)kcontrol->private_value;
  1714. mutex_lock(&codec->mutex);
  1715. if (ucontrol->value.integer.value[0])
  1716. snd_soc_dapm_enable_pin(&codec->dapm, pin);
  1717. else
  1718. snd_soc_dapm_disable_pin(&codec->dapm, pin);
  1719. snd_soc_dapm_sync(&codec->dapm);
  1720. mutex_unlock(&codec->mutex);
  1721. return 0;
  1722. }
  1723. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1724. /**
  1725. * snd_soc_dapm_new_control - create new dapm control
  1726. * @dapm: DAPM context
  1727. * @widget: widget template
  1728. *
  1729. * Creates a new dapm control based upon the template.
  1730. *
  1731. * Returns 0 for success else error.
  1732. */
  1733. int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  1734. const struct snd_soc_dapm_widget *widget)
  1735. {
  1736. struct snd_soc_dapm_widget *w;
  1737. size_t name_len;
  1738. if ((w = dapm_cnew_widget(widget)) == NULL)
  1739. return -ENOMEM;
  1740. name_len = strlen(widget->name) + 1;
  1741. if (dapm->codec->name_prefix)
  1742. name_len += 1 + strlen(dapm->codec->name_prefix);
  1743. w->name = kmalloc(name_len, GFP_KERNEL);
  1744. if (w->name == NULL) {
  1745. kfree(w);
  1746. return -ENOMEM;
  1747. }
  1748. if (dapm->codec->name_prefix)
  1749. snprintf(w->name, name_len, "%s %s",
  1750. dapm->codec->name_prefix, widget->name);
  1751. else
  1752. snprintf(w->name, name_len, "%s", widget->name);
  1753. w->dapm = dapm;
  1754. w->codec = dapm->codec;
  1755. INIT_LIST_HEAD(&w->sources);
  1756. INIT_LIST_HEAD(&w->sinks);
  1757. INIT_LIST_HEAD(&w->list);
  1758. list_add(&w->list, &dapm->widgets);
  1759. /* machine layer set ups unconnected pins and insertions */
  1760. w->connected = 1;
  1761. return 0;
  1762. }
  1763. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1764. /**
  1765. * snd_soc_dapm_new_controls - create new dapm controls
  1766. * @dapm: DAPM context
  1767. * @widget: widget array
  1768. * @num: number of widgets
  1769. *
  1770. * Creates new DAPM controls based upon the templates.
  1771. *
  1772. * Returns 0 for success else error.
  1773. */
  1774. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  1775. const struct snd_soc_dapm_widget *widget,
  1776. int num)
  1777. {
  1778. int i, ret;
  1779. for (i = 0; i < num; i++) {
  1780. ret = snd_soc_dapm_new_control(dapm, widget);
  1781. if (ret < 0) {
  1782. dev_err(dapm->dev,
  1783. "ASoC: Failed to create DAPM control %s: %d\n",
  1784. widget->name, ret);
  1785. return ret;
  1786. }
  1787. widget++;
  1788. }
  1789. return 0;
  1790. }
  1791. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1792. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  1793. const char *stream, int event)
  1794. {
  1795. struct snd_soc_dapm_widget *w;
  1796. list_for_each_entry(w, &dapm->widgets, list)
  1797. {
  1798. if (!w->sname)
  1799. continue;
  1800. dev_dbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
  1801. w->name, w->sname, stream, event);
  1802. if (strstr(w->sname, stream)) {
  1803. switch(event) {
  1804. case SND_SOC_DAPM_STREAM_START:
  1805. w->active = 1;
  1806. break;
  1807. case SND_SOC_DAPM_STREAM_STOP:
  1808. w->active = 0;
  1809. break;
  1810. case SND_SOC_DAPM_STREAM_SUSPEND:
  1811. case SND_SOC_DAPM_STREAM_RESUME:
  1812. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1813. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1814. break;
  1815. }
  1816. }
  1817. }
  1818. dapm_power_widgets(dapm, event);
  1819. }
  1820. /**
  1821. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1822. * @rtd: PCM runtime data
  1823. * @stream: stream name
  1824. * @event: stream event
  1825. *
  1826. * Sends a stream event to the dapm core. The core then makes any
  1827. * necessary widget power changes.
  1828. *
  1829. * Returns 0 for success else error.
  1830. */
  1831. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
  1832. const char *stream, int event)
  1833. {
  1834. struct snd_soc_codec *codec = rtd->codec;
  1835. if (stream == NULL)
  1836. return 0;
  1837. mutex_lock(&codec->mutex);
  1838. soc_dapm_stream_event(&codec->dapm, stream, event);
  1839. mutex_unlock(&codec->mutex);
  1840. return 0;
  1841. }
  1842. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1843. /**
  1844. * snd_soc_dapm_enable_pin - enable pin.
  1845. * @dapm: DAPM context
  1846. * @pin: pin name
  1847. *
  1848. * Enables input/output pin and its parents or children widgets iff there is
  1849. * a valid audio route and active audio stream.
  1850. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1851. * do any widget power switching.
  1852. */
  1853. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  1854. {
  1855. return snd_soc_dapm_set_pin(dapm, pin, 1);
  1856. }
  1857. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1858. /**
  1859. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  1860. * @dapm: DAPM context
  1861. * @pin: pin name
  1862. *
  1863. * Enables input/output pin regardless of any other state. This is
  1864. * intended for use with microphone bias supplies used in microphone
  1865. * jack detection.
  1866. *
  1867. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1868. * do any widget power switching.
  1869. */
  1870. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  1871. const char *pin)
  1872. {
  1873. struct snd_soc_dapm_widget *w;
  1874. list_for_each_entry(w, &dapm->widgets, list) {
  1875. if (!strcmp(w->name, pin)) {
  1876. dev_dbg(w->dapm->dev,
  1877. "dapm: force enable pin %s\n", pin);
  1878. w->connected = 1;
  1879. w->force = 1;
  1880. return 0;
  1881. }
  1882. }
  1883. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1884. return -EINVAL;
  1885. }
  1886. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  1887. /**
  1888. * snd_soc_dapm_disable_pin - disable pin.
  1889. * @dapm: DAPM context
  1890. * @pin: pin name
  1891. *
  1892. * Disables input/output pin and its parents or children widgets.
  1893. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1894. * do any widget power switching.
  1895. */
  1896. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  1897. const char *pin)
  1898. {
  1899. return snd_soc_dapm_set_pin(dapm, pin, 0);
  1900. }
  1901. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1902. /**
  1903. * snd_soc_dapm_nc_pin - permanently disable pin.
  1904. * @dapm: DAPM context
  1905. * @pin: pin name
  1906. *
  1907. * Marks the specified pin as being not connected, disabling it along
  1908. * any parent or child widgets. At present this is identical to
  1909. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1910. * additional things such as disabling controls which only affect
  1911. * paths through the pin.
  1912. *
  1913. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1914. * do any widget power switching.
  1915. */
  1916. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  1917. {
  1918. return snd_soc_dapm_set_pin(dapm, pin, 0);
  1919. }
  1920. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1921. /**
  1922. * snd_soc_dapm_get_pin_status - get audio pin status
  1923. * @dapm: DAPM context
  1924. * @pin: audio signal pin endpoint (or start point)
  1925. *
  1926. * Get audio pin status - connected or disconnected.
  1927. *
  1928. * Returns 1 for connected otherwise 0.
  1929. */
  1930. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  1931. const char *pin)
  1932. {
  1933. struct snd_soc_dapm_widget *w;
  1934. list_for_each_entry(w, &dapm->widgets, list) {
  1935. if (!strcmp(w->name, pin))
  1936. return w->connected;
  1937. }
  1938. return 0;
  1939. }
  1940. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1941. /**
  1942. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  1943. * @dapm: DAPM context
  1944. * @pin: audio signal pin endpoint (or start point)
  1945. *
  1946. * Mark the given endpoint or pin as ignoring suspend. When the
  1947. * system is disabled a path between two endpoints flagged as ignoring
  1948. * suspend will not be disabled. The path must already be enabled via
  1949. * normal means at suspend time, it will not be turned on if it was not
  1950. * already enabled.
  1951. */
  1952. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  1953. const char *pin)
  1954. {
  1955. struct snd_soc_dapm_widget *w;
  1956. list_for_each_entry(w, &dapm->widgets, list) {
  1957. if (!strcmp(w->name, pin)) {
  1958. w->ignore_suspend = 1;
  1959. return 0;
  1960. }
  1961. }
  1962. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1963. return -EINVAL;
  1964. }
  1965. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  1966. /**
  1967. * snd_soc_dapm_free - free dapm resources
  1968. * @card: SoC device
  1969. *
  1970. * Free all dapm widgets and resources.
  1971. */
  1972. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  1973. {
  1974. snd_soc_dapm_sys_remove(dapm->dev);
  1975. dapm_free_widgets(dapm);
  1976. }
  1977. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1978. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  1979. {
  1980. struct snd_soc_dapm_widget *w;
  1981. LIST_HEAD(down_list);
  1982. int powerdown = 0;
  1983. list_for_each_entry(w, &dapm->widgets, list) {
  1984. if (w->power) {
  1985. dapm_seq_insert(w, &down_list, dapm_down_seq);
  1986. w->power = 0;
  1987. powerdown = 1;
  1988. }
  1989. }
  1990. /* If there were no widgets to power down we're already in
  1991. * standby.
  1992. */
  1993. if (powerdown) {
  1994. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_PREPARE);
  1995. dapm_seq_run(dapm, &down_list, 0, dapm_down_seq);
  1996. snd_soc_dapm_set_bias_level(NULL, dapm, SND_SOC_BIAS_STANDBY);
  1997. }
  1998. }
  1999. /*
  2000. * snd_soc_dapm_shutdown - callback for system shutdown
  2001. */
  2002. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2003. {
  2004. struct snd_soc_codec *codec;
  2005. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2006. soc_dapm_shutdown_codec(&codec->dapm);
  2007. snd_soc_dapm_set_bias_level(card, &codec->dapm, SND_SOC_BIAS_OFF);
  2008. }
  2009. }
  2010. /* Module information */
  2011. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2012. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2013. MODULE_LICENSE("GPL");