soc-dapm.c 55 KB

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