soc-dapm.c 56 KB

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