soc-dapm.c 57 KB

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