soc-dapm.c 59 KB

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