soc-dapm.c 61 KB

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