soc-dapm.c 71 KB

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