soc-dapm.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870
  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. /* Generic check to see if a widget should be powered.
  670. */
  671. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  672. {
  673. int in, out;
  674. DAPM_UPDATE_STAT(w, power_checks);
  675. in = is_connected_input_ep(w);
  676. dapm_clear_walk(w->dapm);
  677. out = is_connected_output_ep(w);
  678. dapm_clear_walk(w->dapm);
  679. return out != 0 && in != 0;
  680. }
  681. /* Check to see if an ADC has power */
  682. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  683. {
  684. int in;
  685. DAPM_UPDATE_STAT(w, power_checks);
  686. if (w->active) {
  687. in = is_connected_input_ep(w);
  688. dapm_clear_walk(w->dapm);
  689. return in != 0;
  690. } else {
  691. return dapm_generic_check_power(w);
  692. }
  693. }
  694. /* Check to see if a DAC has power */
  695. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  696. {
  697. int out;
  698. DAPM_UPDATE_STAT(w, power_checks);
  699. if (w->active) {
  700. out = is_connected_output_ep(w);
  701. dapm_clear_walk(w->dapm);
  702. return out != 0;
  703. } else {
  704. return dapm_generic_check_power(w);
  705. }
  706. }
  707. /* Check to see if a power supply is needed */
  708. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  709. {
  710. struct snd_soc_dapm_path *path;
  711. int power = 0;
  712. DAPM_UPDATE_STAT(w, power_checks);
  713. /* Check if one of our outputs is connected */
  714. list_for_each_entry(path, &w->sinks, list_source) {
  715. DAPM_UPDATE_STAT(w, neighbour_checks);
  716. if (path->weak)
  717. continue;
  718. if (path->connected &&
  719. !path->connected(path->source, path->sink))
  720. continue;
  721. if (!path->sink)
  722. continue;
  723. if (path->sink->force) {
  724. power = 1;
  725. break;
  726. }
  727. if (path->sink->power_check &&
  728. path->sink->power_check(path->sink)) {
  729. power = 1;
  730. break;
  731. }
  732. }
  733. dapm_clear_walk(w->dapm);
  734. return power;
  735. }
  736. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  737. struct snd_soc_dapm_widget *b,
  738. bool power_up)
  739. {
  740. int *sort;
  741. if (power_up)
  742. sort = dapm_up_seq;
  743. else
  744. sort = dapm_down_seq;
  745. if (sort[a->id] != sort[b->id])
  746. return sort[a->id] - sort[b->id];
  747. if (a->subseq != b->subseq) {
  748. if (power_up)
  749. return a->subseq - b->subseq;
  750. else
  751. return b->subseq - a->subseq;
  752. }
  753. if (a->reg != b->reg)
  754. return a->reg - b->reg;
  755. if (a->dapm != b->dapm)
  756. return (unsigned long)a->dapm - (unsigned long)b->dapm;
  757. return 0;
  758. }
  759. /* Insert a widget in order into a DAPM power sequence. */
  760. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  761. struct list_head *list,
  762. bool power_up)
  763. {
  764. struct snd_soc_dapm_widget *w;
  765. list_for_each_entry(w, list, power_list)
  766. if (dapm_seq_compare(new_widget, w, power_up) < 0) {
  767. list_add_tail(&new_widget->power_list, &w->power_list);
  768. return;
  769. }
  770. list_add_tail(&new_widget->power_list, list);
  771. }
  772. static void dapm_seq_check_event(struct snd_soc_dapm_context *dapm,
  773. struct snd_soc_dapm_widget *w, int event)
  774. {
  775. struct snd_soc_card *card = dapm->card;
  776. const char *ev_name;
  777. int power, ret;
  778. switch (event) {
  779. case SND_SOC_DAPM_PRE_PMU:
  780. ev_name = "PRE_PMU";
  781. power = 1;
  782. break;
  783. case SND_SOC_DAPM_POST_PMU:
  784. ev_name = "POST_PMU";
  785. power = 1;
  786. break;
  787. case SND_SOC_DAPM_PRE_PMD:
  788. ev_name = "PRE_PMD";
  789. power = 0;
  790. break;
  791. case SND_SOC_DAPM_POST_PMD:
  792. ev_name = "POST_PMD";
  793. power = 0;
  794. break;
  795. default:
  796. BUG();
  797. return;
  798. }
  799. if (w->power != power)
  800. return;
  801. if (w->event && (w->event_flags & event)) {
  802. pop_dbg(dapm->dev, card->pop_time, "pop test : %s %s\n",
  803. w->name, ev_name);
  804. trace_snd_soc_dapm_widget_event_start(w, event);
  805. ret = w->event(w, NULL, event);
  806. trace_snd_soc_dapm_widget_event_done(w, event);
  807. if (ret < 0)
  808. pr_err("%s: %s event failed: %d\n",
  809. ev_name, w->name, ret);
  810. }
  811. }
  812. /* Apply the coalesced changes from a DAPM sequence */
  813. static void dapm_seq_run_coalesced(struct snd_soc_dapm_context *dapm,
  814. struct list_head *pending)
  815. {
  816. struct snd_soc_card *card = dapm->card;
  817. struct snd_soc_dapm_widget *w;
  818. int reg, power;
  819. unsigned int value = 0;
  820. unsigned int mask = 0;
  821. unsigned int cur_mask;
  822. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  823. power_list)->reg;
  824. list_for_each_entry(w, pending, power_list) {
  825. cur_mask = 1 << w->shift;
  826. BUG_ON(reg != w->reg);
  827. if (w->invert)
  828. power = !w->power;
  829. else
  830. power = w->power;
  831. mask |= cur_mask;
  832. if (power)
  833. value |= cur_mask;
  834. pop_dbg(dapm->dev, card->pop_time,
  835. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  836. w->name, reg, value, mask);
  837. /* Check for events */
  838. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMU);
  839. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_PRE_PMD);
  840. }
  841. if (reg >= 0) {
  842. /* Any widget will do, they should all be updating the
  843. * same register.
  844. */
  845. w = list_first_entry(pending, struct snd_soc_dapm_widget,
  846. power_list);
  847. pop_dbg(dapm->dev, card->pop_time,
  848. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  849. value, mask, reg, card->pop_time);
  850. pop_wait(card->pop_time);
  851. soc_widget_update_bits(w, reg, mask, value);
  852. }
  853. list_for_each_entry(w, pending, power_list) {
  854. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMU);
  855. dapm_seq_check_event(dapm, w, SND_SOC_DAPM_POST_PMD);
  856. }
  857. }
  858. /* Apply a DAPM power sequence.
  859. *
  860. * We walk over a pre-sorted list of widgets to apply power to. In
  861. * order to minimise the number of writes to the device required
  862. * multiple widgets will be updated in a single write where possible.
  863. * Currently anything that requires more than a single write is not
  864. * handled.
  865. */
  866. static void dapm_seq_run(struct snd_soc_dapm_context *dapm,
  867. struct list_head *list, int event, bool power_up)
  868. {
  869. struct snd_soc_dapm_widget *w, *n;
  870. LIST_HEAD(pending);
  871. int cur_sort = -1;
  872. int cur_subseq = -1;
  873. int cur_reg = SND_SOC_NOPM;
  874. struct snd_soc_dapm_context *cur_dapm = NULL;
  875. int ret, i;
  876. int *sort;
  877. if (power_up)
  878. sort = dapm_up_seq;
  879. else
  880. sort = dapm_down_seq;
  881. list_for_each_entry_safe(w, n, list, power_list) {
  882. ret = 0;
  883. /* Do we need to apply any queued changes? */
  884. if (sort[w->id] != cur_sort || w->reg != cur_reg ||
  885. w->dapm != cur_dapm || w->subseq != cur_subseq) {
  886. if (!list_empty(&pending))
  887. dapm_seq_run_coalesced(cur_dapm, &pending);
  888. if (cur_dapm && cur_dapm->seq_notifier) {
  889. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  890. if (sort[i] == cur_sort)
  891. cur_dapm->seq_notifier(cur_dapm,
  892. i,
  893. cur_subseq);
  894. }
  895. INIT_LIST_HEAD(&pending);
  896. cur_sort = -1;
  897. cur_subseq = INT_MIN;
  898. cur_reg = SND_SOC_NOPM;
  899. cur_dapm = NULL;
  900. }
  901. switch (w->id) {
  902. case snd_soc_dapm_pre:
  903. if (!w->event)
  904. list_for_each_entry_safe_continue(w, n, list,
  905. power_list);
  906. if (event == SND_SOC_DAPM_STREAM_START)
  907. ret = w->event(w,
  908. NULL, SND_SOC_DAPM_PRE_PMU);
  909. else if (event == SND_SOC_DAPM_STREAM_STOP)
  910. ret = w->event(w,
  911. NULL, SND_SOC_DAPM_PRE_PMD);
  912. break;
  913. case snd_soc_dapm_post:
  914. if (!w->event)
  915. list_for_each_entry_safe_continue(w, n, list,
  916. power_list);
  917. if (event == SND_SOC_DAPM_STREAM_START)
  918. ret = w->event(w,
  919. NULL, SND_SOC_DAPM_POST_PMU);
  920. else if (event == SND_SOC_DAPM_STREAM_STOP)
  921. ret = w->event(w,
  922. NULL, SND_SOC_DAPM_POST_PMD);
  923. break;
  924. default:
  925. /* Queue it up for application */
  926. cur_sort = sort[w->id];
  927. cur_subseq = w->subseq;
  928. cur_reg = w->reg;
  929. cur_dapm = w->dapm;
  930. list_move(&w->power_list, &pending);
  931. break;
  932. }
  933. if (ret < 0)
  934. dev_err(w->dapm->dev,
  935. "Failed to apply widget power: %d\n", ret);
  936. }
  937. if (!list_empty(&pending))
  938. dapm_seq_run_coalesced(cur_dapm, &pending);
  939. if (cur_dapm && cur_dapm->seq_notifier) {
  940. for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++)
  941. if (sort[i] == cur_sort)
  942. cur_dapm->seq_notifier(cur_dapm,
  943. i, cur_subseq);
  944. }
  945. }
  946. static void dapm_widget_update(struct snd_soc_dapm_context *dapm)
  947. {
  948. struct snd_soc_dapm_update *update = dapm->update;
  949. struct snd_soc_dapm_widget *w;
  950. int ret;
  951. if (!update)
  952. return;
  953. w = update->widget;
  954. if (w->event &&
  955. (w->event_flags & SND_SOC_DAPM_PRE_REG)) {
  956. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_PRE_REG);
  957. if (ret != 0)
  958. pr_err("%s DAPM pre-event failed: %d\n",
  959. w->name, ret);
  960. }
  961. ret = snd_soc_update_bits(w->codec, update->reg, update->mask,
  962. update->val);
  963. if (ret < 0)
  964. pr_err("%s DAPM update failed: %d\n", w->name, ret);
  965. if (w->event &&
  966. (w->event_flags & SND_SOC_DAPM_POST_REG)) {
  967. ret = w->event(w, update->kcontrol, SND_SOC_DAPM_POST_REG);
  968. if (ret != 0)
  969. pr_err("%s DAPM post-event failed: %d\n",
  970. w->name, ret);
  971. }
  972. }
  973. /* Async callback run prior to DAPM sequences - brings to _PREPARE if
  974. * they're changing state.
  975. */
  976. static void dapm_pre_sequence_async(void *data, async_cookie_t cookie)
  977. {
  978. struct snd_soc_dapm_context *d = data;
  979. int ret;
  980. /* If we're off and we're not supposed to be go into STANDBY */
  981. if (d->bias_level == SND_SOC_BIAS_OFF &&
  982. d->target_bias_level != SND_SOC_BIAS_OFF) {
  983. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  984. if (ret != 0)
  985. dev_err(d->dev,
  986. "Failed to turn on bias: %d\n", ret);
  987. }
  988. /* Prepare for a STADDBY->ON or ON->STANDBY transition */
  989. if (d->bias_level != d->target_bias_level) {
  990. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_PREPARE);
  991. if (ret != 0)
  992. dev_err(d->dev,
  993. "Failed to prepare bias: %d\n", ret);
  994. }
  995. }
  996. /* Async callback run prior to DAPM sequences - brings to their final
  997. * state.
  998. */
  999. static void dapm_post_sequence_async(void *data, async_cookie_t cookie)
  1000. {
  1001. struct snd_soc_dapm_context *d = data;
  1002. int ret;
  1003. /* If we just powered the last thing off drop to standby bias */
  1004. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1005. (d->target_bias_level == SND_SOC_BIAS_STANDBY ||
  1006. d->target_bias_level == SND_SOC_BIAS_OFF)) {
  1007. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_STANDBY);
  1008. if (ret != 0)
  1009. dev_err(d->dev, "Failed to apply standby bias: %d\n",
  1010. ret);
  1011. }
  1012. /* If we're in standby and can support bias off then do that */
  1013. if (d->bias_level == SND_SOC_BIAS_STANDBY &&
  1014. d->target_bias_level == SND_SOC_BIAS_OFF) {
  1015. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_OFF);
  1016. if (ret != 0)
  1017. dev_err(d->dev, "Failed to turn off bias: %d\n", ret);
  1018. }
  1019. /* If we just powered up then move to active bias */
  1020. if (d->bias_level == SND_SOC_BIAS_PREPARE &&
  1021. d->target_bias_level == SND_SOC_BIAS_ON) {
  1022. ret = snd_soc_dapm_set_bias_level(d, SND_SOC_BIAS_ON);
  1023. if (ret != 0)
  1024. dev_err(d->dev, "Failed to apply active bias: %d\n",
  1025. ret);
  1026. }
  1027. }
  1028. static void dapm_widget_set_power(struct snd_soc_dapm_widget *w, bool power,
  1029. struct list_head *up_list,
  1030. struct list_head *down_list)
  1031. {
  1032. if (w->power == power)
  1033. return;
  1034. trace_snd_soc_dapm_widget_power(w, power);
  1035. if (power)
  1036. dapm_seq_insert(w, up_list, true);
  1037. else
  1038. dapm_seq_insert(w, down_list, false);
  1039. w->power = power;
  1040. }
  1041. static void dapm_power_one_widget(struct snd_soc_dapm_widget *w,
  1042. struct list_head *up_list,
  1043. struct list_head *down_list)
  1044. {
  1045. struct snd_soc_dapm_context *d;
  1046. int power;
  1047. switch (w->id) {
  1048. case snd_soc_dapm_pre:
  1049. dapm_seq_insert(w, down_list, false);
  1050. break;
  1051. case snd_soc_dapm_post:
  1052. dapm_seq_insert(w, up_list, true);
  1053. break;
  1054. default:
  1055. if (!w->power_check)
  1056. break;
  1057. if (!w->force)
  1058. power = w->power_check(w);
  1059. else
  1060. power = 1;
  1061. if (power) {
  1062. d = w->dapm;
  1063. /* Supplies and micbiases only bring the
  1064. * context up to STANDBY as unless something
  1065. * else is active and passing audio they
  1066. * generally don't require full power.
  1067. */
  1068. switch (w->id) {
  1069. case snd_soc_dapm_supply:
  1070. case snd_soc_dapm_micbias:
  1071. if (d->target_bias_level < SND_SOC_BIAS_STANDBY)
  1072. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1073. break;
  1074. default:
  1075. d->target_bias_level = SND_SOC_BIAS_ON;
  1076. break;
  1077. }
  1078. }
  1079. dapm_widget_set_power(w, power, up_list, down_list);
  1080. break;
  1081. }
  1082. }
  1083. /*
  1084. * Scan each dapm widget for complete audio path.
  1085. * A complete path is a route that has valid endpoints i.e.:-
  1086. *
  1087. * o DAC to output pin.
  1088. * o Input Pin to ADC.
  1089. * o Input pin to Output pin (bypass, sidetone)
  1090. * o DAC to ADC (loopback).
  1091. */
  1092. static int dapm_power_widgets(struct snd_soc_dapm_context *dapm, int event)
  1093. {
  1094. struct snd_soc_card *card = dapm->card;
  1095. struct snd_soc_dapm_widget *w;
  1096. struct snd_soc_dapm_context *d;
  1097. LIST_HEAD(up_list);
  1098. LIST_HEAD(down_list);
  1099. LIST_HEAD(async_domain);
  1100. enum snd_soc_bias_level bias;
  1101. trace_snd_soc_dapm_start(card);
  1102. list_for_each_entry(d, &card->dapm_list, list) {
  1103. if (d->n_widgets || d->codec == NULL) {
  1104. if (d->idle_bias_off)
  1105. d->target_bias_level = SND_SOC_BIAS_OFF;
  1106. else
  1107. d->target_bias_level = SND_SOC_BIAS_STANDBY;
  1108. }
  1109. }
  1110. memset(&card->dapm_stats, 0, sizeof(card->dapm_stats));
  1111. /* Check which widgets we need to power and store them in
  1112. * lists indicating if they should be powered up or down.
  1113. */
  1114. list_for_each_entry(w, &card->widgets, list) {
  1115. dapm_power_one_widget(w, &up_list, &down_list);
  1116. }
  1117. /* If there are no DAPM widgets then try to figure out power from the
  1118. * event type.
  1119. */
  1120. if (!dapm->n_widgets) {
  1121. switch (event) {
  1122. case SND_SOC_DAPM_STREAM_START:
  1123. case SND_SOC_DAPM_STREAM_RESUME:
  1124. dapm->target_bias_level = SND_SOC_BIAS_ON;
  1125. break;
  1126. case SND_SOC_DAPM_STREAM_STOP:
  1127. if (dapm->codec->active)
  1128. dapm->target_bias_level = SND_SOC_BIAS_ON;
  1129. else
  1130. dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
  1131. break;
  1132. case SND_SOC_DAPM_STREAM_SUSPEND:
  1133. dapm->target_bias_level = SND_SOC_BIAS_STANDBY;
  1134. break;
  1135. case SND_SOC_DAPM_STREAM_NOP:
  1136. dapm->target_bias_level = dapm->bias_level;
  1137. break;
  1138. default:
  1139. break;
  1140. }
  1141. }
  1142. /* Force all contexts in the card to the same bias state if
  1143. * they're not ground referenced.
  1144. */
  1145. bias = SND_SOC_BIAS_OFF;
  1146. list_for_each_entry(d, &card->dapm_list, list)
  1147. if (d->target_bias_level > bias)
  1148. bias = d->target_bias_level;
  1149. list_for_each_entry(d, &card->dapm_list, list)
  1150. if (!d->idle_bias_off)
  1151. d->target_bias_level = bias;
  1152. trace_snd_soc_dapm_walk_done(card);
  1153. /* Run all the bias changes in parallel */
  1154. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1155. async_schedule_domain(dapm_pre_sequence_async, d,
  1156. &async_domain);
  1157. async_synchronize_full_domain(&async_domain);
  1158. /* Power down widgets first; try to avoid amplifying pops. */
  1159. dapm_seq_run(dapm, &down_list, event, false);
  1160. dapm_widget_update(dapm);
  1161. /* Now power up. */
  1162. dapm_seq_run(dapm, &up_list, event, true);
  1163. /* Run all the bias changes in parallel */
  1164. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1165. async_schedule_domain(dapm_post_sequence_async, d,
  1166. &async_domain);
  1167. async_synchronize_full_domain(&async_domain);
  1168. pop_dbg(dapm->dev, card->pop_time,
  1169. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1170. pop_wait(card->pop_time);
  1171. trace_snd_soc_dapm_done(card);
  1172. return 0;
  1173. }
  1174. #ifdef CONFIG_DEBUG_FS
  1175. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  1176. {
  1177. file->private_data = inode->i_private;
  1178. return 0;
  1179. }
  1180. static ssize_t dapm_widget_power_read_file(struct file *file,
  1181. char __user *user_buf,
  1182. size_t count, loff_t *ppos)
  1183. {
  1184. struct snd_soc_dapm_widget *w = file->private_data;
  1185. char *buf;
  1186. int in, out;
  1187. ssize_t ret;
  1188. struct snd_soc_dapm_path *p = NULL;
  1189. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1190. if (!buf)
  1191. return -ENOMEM;
  1192. in = is_connected_input_ep(w);
  1193. dapm_clear_walk(w->dapm);
  1194. out = is_connected_output_ep(w);
  1195. dapm_clear_walk(w->dapm);
  1196. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  1197. w->name, w->power ? "On" : "Off", in, out);
  1198. if (w->reg >= 0)
  1199. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1200. " - R%d(0x%x) bit %d",
  1201. w->reg, w->reg, w->shift);
  1202. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1203. if (w->sname)
  1204. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1205. w->sname,
  1206. w->active ? "active" : "inactive");
  1207. list_for_each_entry(p, &w->sources, list_sink) {
  1208. if (p->connected && !p->connected(w, p->sink))
  1209. continue;
  1210. if (p->connect)
  1211. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1212. " in \"%s\" \"%s\"\n",
  1213. p->name ? p->name : "static",
  1214. p->source->name);
  1215. }
  1216. list_for_each_entry(p, &w->sinks, list_source) {
  1217. if (p->connected && !p->connected(w, p->sink))
  1218. continue;
  1219. if (p->connect)
  1220. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1221. " out \"%s\" \"%s\"\n",
  1222. p->name ? p->name : "static",
  1223. p->sink->name);
  1224. }
  1225. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1226. kfree(buf);
  1227. return ret;
  1228. }
  1229. static const struct file_operations dapm_widget_power_fops = {
  1230. .open = dapm_widget_power_open_file,
  1231. .read = dapm_widget_power_read_file,
  1232. .llseek = default_llseek,
  1233. };
  1234. static int dapm_bias_open_file(struct inode *inode, struct file *file)
  1235. {
  1236. file->private_data = inode->i_private;
  1237. return 0;
  1238. }
  1239. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1240. size_t count, loff_t *ppos)
  1241. {
  1242. struct snd_soc_dapm_context *dapm = file->private_data;
  1243. char *level;
  1244. switch (dapm->bias_level) {
  1245. case SND_SOC_BIAS_ON:
  1246. level = "On\n";
  1247. break;
  1248. case SND_SOC_BIAS_PREPARE:
  1249. level = "Prepare\n";
  1250. break;
  1251. case SND_SOC_BIAS_STANDBY:
  1252. level = "Standby\n";
  1253. break;
  1254. case SND_SOC_BIAS_OFF:
  1255. level = "Off\n";
  1256. break;
  1257. default:
  1258. BUG();
  1259. level = "Unknown\n";
  1260. break;
  1261. }
  1262. return simple_read_from_buffer(user_buf, count, ppos, level,
  1263. strlen(level));
  1264. }
  1265. static const struct file_operations dapm_bias_fops = {
  1266. .open = dapm_bias_open_file,
  1267. .read = dapm_bias_read_file,
  1268. .llseek = default_llseek,
  1269. };
  1270. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1271. struct dentry *parent)
  1272. {
  1273. struct dentry *d;
  1274. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1275. if (!dapm->debugfs_dapm) {
  1276. printk(KERN_WARNING
  1277. "Failed to create DAPM debugfs directory\n");
  1278. return;
  1279. }
  1280. d = debugfs_create_file("bias_level", 0444,
  1281. dapm->debugfs_dapm, dapm,
  1282. &dapm_bias_fops);
  1283. if (!d)
  1284. dev_warn(dapm->dev,
  1285. "ASoC: Failed to create bias level debugfs file\n");
  1286. }
  1287. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1288. {
  1289. struct snd_soc_dapm_context *dapm = w->dapm;
  1290. struct dentry *d;
  1291. if (!dapm->debugfs_dapm || !w->name)
  1292. return;
  1293. d = debugfs_create_file(w->name, 0444,
  1294. dapm->debugfs_dapm, w,
  1295. &dapm_widget_power_fops);
  1296. if (!d)
  1297. dev_warn(w->dapm->dev,
  1298. "ASoC: Failed to create %s debugfs file\n",
  1299. w->name);
  1300. }
  1301. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1302. {
  1303. debugfs_remove_recursive(dapm->debugfs_dapm);
  1304. }
  1305. #else
  1306. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1307. struct dentry *parent)
  1308. {
  1309. }
  1310. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1311. {
  1312. }
  1313. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1314. {
  1315. }
  1316. #endif
  1317. /* test and update the power status of a mux widget */
  1318. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1319. struct snd_kcontrol *kcontrol, int change,
  1320. int mux, struct soc_enum *e)
  1321. {
  1322. struct snd_soc_dapm_path *path;
  1323. int found = 0;
  1324. if (widget->id != snd_soc_dapm_mux &&
  1325. widget->id != snd_soc_dapm_virt_mux &&
  1326. widget->id != snd_soc_dapm_value_mux)
  1327. return -ENODEV;
  1328. if (!change)
  1329. return 0;
  1330. /* find dapm widget path assoc with kcontrol */
  1331. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1332. if (path->kcontrol != kcontrol)
  1333. continue;
  1334. if (!path->name || !e->texts[mux])
  1335. continue;
  1336. found = 1;
  1337. /* we now need to match the string in the enum to the path */
  1338. if (!(strcmp(path->name, e->texts[mux])))
  1339. path->connect = 1; /* new connection */
  1340. else
  1341. path->connect = 0; /* old connection must be powered down */
  1342. }
  1343. if (found)
  1344. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1345. return 0;
  1346. }
  1347. /* test and update the power status of a mixer or switch widget */
  1348. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1349. struct snd_kcontrol *kcontrol, int connect)
  1350. {
  1351. struct snd_soc_dapm_path *path;
  1352. int found = 0;
  1353. if (widget->id != snd_soc_dapm_mixer &&
  1354. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1355. widget->id != snd_soc_dapm_switch)
  1356. return -ENODEV;
  1357. /* find dapm widget path assoc with kcontrol */
  1358. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1359. if (path->kcontrol != kcontrol)
  1360. continue;
  1361. /* found, now check type */
  1362. found = 1;
  1363. path->connect = connect;
  1364. }
  1365. if (found)
  1366. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1367. return 0;
  1368. }
  1369. /* show dapm widget status in sys fs */
  1370. static ssize_t dapm_widget_show(struct device *dev,
  1371. struct device_attribute *attr, char *buf)
  1372. {
  1373. struct snd_soc_pcm_runtime *rtd =
  1374. container_of(dev, struct snd_soc_pcm_runtime, dev);
  1375. struct snd_soc_codec *codec =rtd->codec;
  1376. struct snd_soc_dapm_widget *w;
  1377. int count = 0;
  1378. char *state = "not set";
  1379. list_for_each_entry(w, &codec->card->widgets, list) {
  1380. if (w->dapm != &codec->dapm)
  1381. continue;
  1382. /* only display widgets that burnm power */
  1383. switch (w->id) {
  1384. case snd_soc_dapm_hp:
  1385. case snd_soc_dapm_mic:
  1386. case snd_soc_dapm_spk:
  1387. case snd_soc_dapm_line:
  1388. case snd_soc_dapm_micbias:
  1389. case snd_soc_dapm_dac:
  1390. case snd_soc_dapm_adc:
  1391. case snd_soc_dapm_pga:
  1392. case snd_soc_dapm_out_drv:
  1393. case snd_soc_dapm_mixer:
  1394. case snd_soc_dapm_mixer_named_ctl:
  1395. case snd_soc_dapm_supply:
  1396. if (w->name)
  1397. count += sprintf(buf + count, "%s: %s\n",
  1398. w->name, w->power ? "On":"Off");
  1399. break;
  1400. default:
  1401. break;
  1402. }
  1403. }
  1404. switch (codec->dapm.bias_level) {
  1405. case SND_SOC_BIAS_ON:
  1406. state = "On";
  1407. break;
  1408. case SND_SOC_BIAS_PREPARE:
  1409. state = "Prepare";
  1410. break;
  1411. case SND_SOC_BIAS_STANDBY:
  1412. state = "Standby";
  1413. break;
  1414. case SND_SOC_BIAS_OFF:
  1415. state = "Off";
  1416. break;
  1417. }
  1418. count += sprintf(buf + count, "PM State: %s\n", state);
  1419. return count;
  1420. }
  1421. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1422. int snd_soc_dapm_sys_add(struct device *dev)
  1423. {
  1424. return device_create_file(dev, &dev_attr_dapm_widget);
  1425. }
  1426. static void snd_soc_dapm_sys_remove(struct device *dev)
  1427. {
  1428. device_remove_file(dev, &dev_attr_dapm_widget);
  1429. }
  1430. /* free all dapm widgets and resources */
  1431. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1432. {
  1433. struct snd_soc_dapm_widget *w, *next_w;
  1434. struct snd_soc_dapm_path *p, *next_p;
  1435. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1436. if (w->dapm != dapm)
  1437. continue;
  1438. list_del(&w->list);
  1439. /*
  1440. * remove source and sink paths associated to this widget.
  1441. * While removing the path, remove reference to it from both
  1442. * source and sink widgets so that path is removed only once.
  1443. */
  1444. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1445. list_del(&p->list_sink);
  1446. list_del(&p->list_source);
  1447. list_del(&p->list);
  1448. kfree(p->long_name);
  1449. kfree(p);
  1450. }
  1451. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1452. list_del(&p->list_sink);
  1453. list_del(&p->list_source);
  1454. list_del(&p->list);
  1455. kfree(p->long_name);
  1456. kfree(p);
  1457. }
  1458. kfree(w->kcontrols);
  1459. kfree(w->name);
  1460. kfree(w);
  1461. }
  1462. }
  1463. static struct snd_soc_dapm_widget *dapm_find_widget(
  1464. struct snd_soc_dapm_context *dapm, const char *pin,
  1465. bool search_other_contexts)
  1466. {
  1467. struct snd_soc_dapm_widget *w;
  1468. struct snd_soc_dapm_widget *fallback = NULL;
  1469. list_for_each_entry(w, &dapm->card->widgets, list) {
  1470. if (!strcmp(w->name, pin)) {
  1471. if (w->dapm == dapm)
  1472. return w;
  1473. else
  1474. fallback = w;
  1475. }
  1476. }
  1477. if (search_other_contexts)
  1478. return fallback;
  1479. return NULL;
  1480. }
  1481. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1482. const char *pin, int status)
  1483. {
  1484. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  1485. if (!w) {
  1486. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1487. return -EINVAL;
  1488. }
  1489. w->connected = status;
  1490. if (status == 0)
  1491. w->force = 0;
  1492. return 0;
  1493. }
  1494. /**
  1495. * snd_soc_dapm_sync - scan and power dapm paths
  1496. * @dapm: DAPM context
  1497. *
  1498. * Walks all dapm audio paths and powers widgets according to their
  1499. * stream or path usage.
  1500. *
  1501. * Returns 0 for success.
  1502. */
  1503. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1504. {
  1505. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1506. }
  1507. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1508. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1509. const struct snd_soc_dapm_route *route)
  1510. {
  1511. struct snd_soc_dapm_path *path;
  1512. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1513. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1514. const char *sink;
  1515. const char *control = route->control;
  1516. const char *source;
  1517. char prefixed_sink[80];
  1518. char prefixed_source[80];
  1519. int ret = 0;
  1520. if (dapm->codec && dapm->codec->name_prefix) {
  1521. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1522. dapm->codec->name_prefix, route->sink);
  1523. sink = prefixed_sink;
  1524. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1525. dapm->codec->name_prefix, route->source);
  1526. source = prefixed_source;
  1527. } else {
  1528. sink = route->sink;
  1529. source = route->source;
  1530. }
  1531. /*
  1532. * find src and dest widgets over all widgets but favor a widget from
  1533. * current DAPM context
  1534. */
  1535. list_for_each_entry(w, &dapm->card->widgets, list) {
  1536. if (!wsink && !(strcmp(w->name, sink))) {
  1537. wtsink = w;
  1538. if (w->dapm == dapm)
  1539. wsink = w;
  1540. continue;
  1541. }
  1542. if (!wsource && !(strcmp(w->name, source))) {
  1543. wtsource = w;
  1544. if (w->dapm == dapm)
  1545. wsource = w;
  1546. }
  1547. }
  1548. /* use widget from another DAPM context if not found from this */
  1549. if (!wsink)
  1550. wsink = wtsink;
  1551. if (!wsource)
  1552. wsource = wtsource;
  1553. if (wsource == NULL || wsink == NULL)
  1554. return -ENODEV;
  1555. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1556. if (!path)
  1557. return -ENOMEM;
  1558. path->source = wsource;
  1559. path->sink = wsink;
  1560. path->connected = route->connected;
  1561. INIT_LIST_HEAD(&path->list);
  1562. INIT_LIST_HEAD(&path->list_source);
  1563. INIT_LIST_HEAD(&path->list_sink);
  1564. /* check for external widgets */
  1565. if (wsink->id == snd_soc_dapm_input) {
  1566. if (wsource->id == snd_soc_dapm_micbias ||
  1567. wsource->id == snd_soc_dapm_mic ||
  1568. wsource->id == snd_soc_dapm_line ||
  1569. wsource->id == snd_soc_dapm_output)
  1570. wsink->ext = 1;
  1571. }
  1572. if (wsource->id == snd_soc_dapm_output) {
  1573. if (wsink->id == snd_soc_dapm_spk ||
  1574. wsink->id == snd_soc_dapm_hp ||
  1575. wsink->id == snd_soc_dapm_line ||
  1576. wsink->id == snd_soc_dapm_input)
  1577. wsource->ext = 1;
  1578. }
  1579. /* connect static paths */
  1580. if (control == NULL) {
  1581. list_add(&path->list, &dapm->card->paths);
  1582. list_add(&path->list_sink, &wsink->sources);
  1583. list_add(&path->list_source, &wsource->sinks);
  1584. path->connect = 1;
  1585. return 0;
  1586. }
  1587. /* connect dynamic paths */
  1588. switch (wsink->id) {
  1589. case snd_soc_dapm_adc:
  1590. case snd_soc_dapm_dac:
  1591. case snd_soc_dapm_pga:
  1592. case snd_soc_dapm_out_drv:
  1593. case snd_soc_dapm_input:
  1594. case snd_soc_dapm_output:
  1595. case snd_soc_dapm_micbias:
  1596. case snd_soc_dapm_vmid:
  1597. case snd_soc_dapm_pre:
  1598. case snd_soc_dapm_post:
  1599. case snd_soc_dapm_supply:
  1600. case snd_soc_dapm_aif_in:
  1601. case snd_soc_dapm_aif_out:
  1602. list_add(&path->list, &dapm->card->paths);
  1603. list_add(&path->list_sink, &wsink->sources);
  1604. list_add(&path->list_source, &wsource->sinks);
  1605. path->connect = 1;
  1606. return 0;
  1607. case snd_soc_dapm_mux:
  1608. case snd_soc_dapm_virt_mux:
  1609. case snd_soc_dapm_value_mux:
  1610. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1611. &wsink->kcontrol_news[0]);
  1612. if (ret != 0)
  1613. goto err;
  1614. break;
  1615. case snd_soc_dapm_switch:
  1616. case snd_soc_dapm_mixer:
  1617. case snd_soc_dapm_mixer_named_ctl:
  1618. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1619. if (ret != 0)
  1620. goto err;
  1621. break;
  1622. case snd_soc_dapm_hp:
  1623. case snd_soc_dapm_mic:
  1624. case snd_soc_dapm_line:
  1625. case snd_soc_dapm_spk:
  1626. list_add(&path->list, &dapm->card->paths);
  1627. list_add(&path->list_sink, &wsink->sources);
  1628. list_add(&path->list_source, &wsource->sinks);
  1629. path->connect = 0;
  1630. return 0;
  1631. }
  1632. return 0;
  1633. err:
  1634. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1635. source, control, sink);
  1636. kfree(path);
  1637. return ret;
  1638. }
  1639. /**
  1640. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1641. * @dapm: DAPM context
  1642. * @route: audio routes
  1643. * @num: number of routes
  1644. *
  1645. * Connects 2 dapm widgets together via a named audio path. The sink is
  1646. * the widget receiving the audio signal, whilst the source is the sender
  1647. * of the audio signal.
  1648. *
  1649. * Returns 0 for success else error. On error all resources can be freed
  1650. * with a call to snd_soc_card_free().
  1651. */
  1652. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1653. const struct snd_soc_dapm_route *route, int num)
  1654. {
  1655. int i, ret;
  1656. for (i = 0; i < num; i++) {
  1657. ret = snd_soc_dapm_add_route(dapm, route);
  1658. if (ret < 0) {
  1659. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1660. route->source, route->sink);
  1661. return ret;
  1662. }
  1663. route++;
  1664. }
  1665. return 0;
  1666. }
  1667. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1668. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  1669. const struct snd_soc_dapm_route *route)
  1670. {
  1671. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  1672. route->source,
  1673. true);
  1674. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  1675. route->sink,
  1676. true);
  1677. struct snd_soc_dapm_path *path;
  1678. int count = 0;
  1679. if (!source) {
  1680. dev_err(dapm->dev, "Unable to find source %s for weak route\n",
  1681. route->source);
  1682. return -ENODEV;
  1683. }
  1684. if (!sink) {
  1685. dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
  1686. route->sink);
  1687. return -ENODEV;
  1688. }
  1689. if (route->control || route->connected)
  1690. dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
  1691. route->source, route->sink);
  1692. list_for_each_entry(path, &source->sinks, list_source) {
  1693. if (path->sink == sink) {
  1694. path->weak = 1;
  1695. count++;
  1696. }
  1697. }
  1698. if (count == 0)
  1699. dev_err(dapm->dev, "No path found for weak route %s->%s\n",
  1700. route->source, route->sink);
  1701. if (count > 1)
  1702. dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
  1703. count, route->source, route->sink);
  1704. return 0;
  1705. }
  1706. /**
  1707. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  1708. * @dapm: DAPM context
  1709. * @route: audio routes
  1710. * @num: number of routes
  1711. *
  1712. * Mark existing routes matching those specified in the passed array
  1713. * as being weak, meaning that they are ignored for the purpose of
  1714. * power decisions. The main intended use case is for sidetone paths
  1715. * which couple audio between other independent paths if they are both
  1716. * active in order to make the combination work better at the user
  1717. * level but which aren't intended to be "used".
  1718. *
  1719. * Note that CODEC drivers should not use this as sidetone type paths
  1720. * can frequently also be used as bypass paths.
  1721. */
  1722. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  1723. const struct snd_soc_dapm_route *route, int num)
  1724. {
  1725. int i, err;
  1726. int ret = 0;
  1727. for (i = 0; i < num; i++) {
  1728. err = snd_soc_dapm_weak_route(dapm, route);
  1729. if (err)
  1730. ret = err;
  1731. route++;
  1732. }
  1733. return ret;
  1734. }
  1735. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  1736. /**
  1737. * snd_soc_dapm_new_widgets - add new dapm widgets
  1738. * @dapm: DAPM context
  1739. *
  1740. * Checks the codec for any new dapm widgets and creates them if found.
  1741. *
  1742. * Returns 0 for success.
  1743. */
  1744. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1745. {
  1746. struct snd_soc_dapm_widget *w;
  1747. unsigned int val;
  1748. list_for_each_entry(w, &dapm->card->widgets, list)
  1749. {
  1750. if (w->new)
  1751. continue;
  1752. if (w->num_kcontrols) {
  1753. w->kcontrols = kzalloc(w->num_kcontrols *
  1754. sizeof(struct snd_kcontrol *),
  1755. GFP_KERNEL);
  1756. if (!w->kcontrols)
  1757. return -ENOMEM;
  1758. }
  1759. switch(w->id) {
  1760. case snd_soc_dapm_switch:
  1761. case snd_soc_dapm_mixer:
  1762. case snd_soc_dapm_mixer_named_ctl:
  1763. w->power_check = dapm_generic_check_power;
  1764. dapm_new_mixer(w);
  1765. break;
  1766. case snd_soc_dapm_mux:
  1767. case snd_soc_dapm_virt_mux:
  1768. case snd_soc_dapm_value_mux:
  1769. w->power_check = dapm_generic_check_power;
  1770. dapm_new_mux(w);
  1771. break;
  1772. case snd_soc_dapm_adc:
  1773. case snd_soc_dapm_aif_out:
  1774. w->power_check = dapm_adc_check_power;
  1775. break;
  1776. case snd_soc_dapm_dac:
  1777. case snd_soc_dapm_aif_in:
  1778. w->power_check = dapm_dac_check_power;
  1779. break;
  1780. case snd_soc_dapm_pga:
  1781. case snd_soc_dapm_out_drv:
  1782. w->power_check = dapm_generic_check_power;
  1783. dapm_new_pga(w);
  1784. break;
  1785. case snd_soc_dapm_input:
  1786. case snd_soc_dapm_output:
  1787. case snd_soc_dapm_micbias:
  1788. case snd_soc_dapm_spk:
  1789. case snd_soc_dapm_hp:
  1790. case snd_soc_dapm_mic:
  1791. case snd_soc_dapm_line:
  1792. w->power_check = dapm_generic_check_power;
  1793. break;
  1794. case snd_soc_dapm_supply:
  1795. w->power_check = dapm_supply_check_power;
  1796. case snd_soc_dapm_vmid:
  1797. case snd_soc_dapm_pre:
  1798. case snd_soc_dapm_post:
  1799. break;
  1800. }
  1801. /* Read the initial power state from the device */
  1802. if (w->reg >= 0) {
  1803. val = soc_widget_read(w, w->reg);
  1804. val &= 1 << w->shift;
  1805. if (w->invert)
  1806. val = !val;
  1807. if (val)
  1808. w->power = 1;
  1809. }
  1810. w->new = 1;
  1811. dapm_debugfs_add_widget(w);
  1812. }
  1813. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1814. return 0;
  1815. }
  1816. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1817. /**
  1818. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1819. * @kcontrol: mixer control
  1820. * @ucontrol: control element information
  1821. *
  1822. * Callback to get the value of a dapm mixer control.
  1823. *
  1824. * Returns 0 for success.
  1825. */
  1826. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1827. struct snd_ctl_elem_value *ucontrol)
  1828. {
  1829. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1830. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1831. struct soc_mixer_control *mc =
  1832. (struct soc_mixer_control *)kcontrol->private_value;
  1833. unsigned int reg = mc->reg;
  1834. unsigned int shift = mc->shift;
  1835. unsigned int rshift = mc->rshift;
  1836. int max = mc->max;
  1837. unsigned int invert = mc->invert;
  1838. unsigned int mask = (1 << fls(max)) - 1;
  1839. ucontrol->value.integer.value[0] =
  1840. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1841. if (shift != rshift)
  1842. ucontrol->value.integer.value[1] =
  1843. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1844. if (invert) {
  1845. ucontrol->value.integer.value[0] =
  1846. max - ucontrol->value.integer.value[0];
  1847. if (shift != rshift)
  1848. ucontrol->value.integer.value[1] =
  1849. max - ucontrol->value.integer.value[1];
  1850. }
  1851. return 0;
  1852. }
  1853. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1854. /**
  1855. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1856. * @kcontrol: mixer control
  1857. * @ucontrol: control element information
  1858. *
  1859. * Callback to set the value of a dapm mixer control.
  1860. *
  1861. * Returns 0 for success.
  1862. */
  1863. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1864. struct snd_ctl_elem_value *ucontrol)
  1865. {
  1866. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1867. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1868. struct snd_soc_codec *codec = widget->codec;
  1869. struct soc_mixer_control *mc =
  1870. (struct soc_mixer_control *)kcontrol->private_value;
  1871. unsigned int reg = mc->reg;
  1872. unsigned int shift = mc->shift;
  1873. int max = mc->max;
  1874. unsigned int mask = (1 << fls(max)) - 1;
  1875. unsigned int invert = mc->invert;
  1876. unsigned int val;
  1877. int connect, change;
  1878. struct snd_soc_dapm_update update;
  1879. int wi;
  1880. val = (ucontrol->value.integer.value[0] & mask);
  1881. if (invert)
  1882. val = max - val;
  1883. mask = mask << shift;
  1884. val = val << shift;
  1885. if (val)
  1886. /* new connection */
  1887. connect = invert ? 0 : 1;
  1888. else
  1889. /* old connection must be powered down */
  1890. connect = invert ? 1 : 0;
  1891. mutex_lock(&codec->mutex);
  1892. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  1893. if (change) {
  1894. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1895. widget = wlist->widgets[wi];
  1896. widget->value = val;
  1897. update.kcontrol = kcontrol;
  1898. update.widget = widget;
  1899. update.reg = reg;
  1900. update.mask = mask;
  1901. update.val = val;
  1902. widget->dapm->update = &update;
  1903. dapm_mixer_update_power(widget, kcontrol, connect);
  1904. widget->dapm->update = NULL;
  1905. }
  1906. }
  1907. mutex_unlock(&codec->mutex);
  1908. return 0;
  1909. }
  1910. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1911. /**
  1912. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1913. * @kcontrol: mixer control
  1914. * @ucontrol: control element information
  1915. *
  1916. * Callback to get the value of a dapm enumerated double mixer control.
  1917. *
  1918. * Returns 0 for success.
  1919. */
  1920. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1921. struct snd_ctl_elem_value *ucontrol)
  1922. {
  1923. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1924. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1925. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1926. unsigned int val, bitmask;
  1927. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1928. ;
  1929. val = snd_soc_read(widget->codec, e->reg);
  1930. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1931. if (e->shift_l != e->shift_r)
  1932. ucontrol->value.enumerated.item[1] =
  1933. (val >> e->shift_r) & (bitmask - 1);
  1934. return 0;
  1935. }
  1936. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1937. /**
  1938. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1939. * @kcontrol: mixer control
  1940. * @ucontrol: control element information
  1941. *
  1942. * Callback to set the value of a dapm enumerated double mixer control.
  1943. *
  1944. * Returns 0 for success.
  1945. */
  1946. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1947. struct snd_ctl_elem_value *ucontrol)
  1948. {
  1949. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1950. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1951. struct snd_soc_codec *codec = widget->codec;
  1952. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1953. unsigned int val, mux, change;
  1954. unsigned int mask, bitmask;
  1955. struct snd_soc_dapm_update update;
  1956. int wi;
  1957. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1958. ;
  1959. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1960. return -EINVAL;
  1961. mux = ucontrol->value.enumerated.item[0];
  1962. val = mux << e->shift_l;
  1963. mask = (bitmask - 1) << e->shift_l;
  1964. if (e->shift_l != e->shift_r) {
  1965. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1966. return -EINVAL;
  1967. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1968. mask |= (bitmask - 1) << e->shift_r;
  1969. }
  1970. mutex_lock(&codec->mutex);
  1971. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1972. if (change) {
  1973. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1974. widget = wlist->widgets[wi];
  1975. widget->value = val;
  1976. update.kcontrol = kcontrol;
  1977. update.widget = widget;
  1978. update.reg = e->reg;
  1979. update.mask = mask;
  1980. update.val = val;
  1981. widget->dapm->update = &update;
  1982. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1983. widget->dapm->update = NULL;
  1984. }
  1985. }
  1986. mutex_unlock(&codec->mutex);
  1987. return change;
  1988. }
  1989. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1990. /**
  1991. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1992. * @kcontrol: mixer control
  1993. * @ucontrol: control element information
  1994. *
  1995. * Returns 0 for success.
  1996. */
  1997. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1998. struct snd_ctl_elem_value *ucontrol)
  1999. {
  2000. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2001. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2002. ucontrol->value.enumerated.item[0] = widget->value;
  2003. return 0;
  2004. }
  2005. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  2006. /**
  2007. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  2008. * @kcontrol: mixer control
  2009. * @ucontrol: control element information
  2010. *
  2011. * Returns 0 for success.
  2012. */
  2013. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  2014. struct snd_ctl_elem_value *ucontrol)
  2015. {
  2016. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2017. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2018. struct snd_soc_codec *codec = widget->codec;
  2019. struct soc_enum *e =
  2020. (struct soc_enum *)kcontrol->private_value;
  2021. int change;
  2022. int ret = 0;
  2023. int wi;
  2024. if (ucontrol->value.enumerated.item[0] >= e->max)
  2025. return -EINVAL;
  2026. mutex_lock(&codec->mutex);
  2027. change = widget->value != ucontrol->value.enumerated.item[0];
  2028. if (change) {
  2029. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2030. widget = wlist->widgets[wi];
  2031. widget->value = ucontrol->value.enumerated.item[0];
  2032. dapm_mux_update_power(widget, kcontrol, change,
  2033. widget->value, e);
  2034. }
  2035. }
  2036. mutex_unlock(&codec->mutex);
  2037. return ret;
  2038. }
  2039. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  2040. /**
  2041. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  2042. * callback
  2043. * @kcontrol: mixer control
  2044. * @ucontrol: control element information
  2045. *
  2046. * Callback to get the value of a dapm semi enumerated double mixer control.
  2047. *
  2048. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2049. * used for handling bitfield coded enumeration for example.
  2050. *
  2051. * Returns 0 for success.
  2052. */
  2053. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2054. struct snd_ctl_elem_value *ucontrol)
  2055. {
  2056. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2057. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2058. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2059. unsigned int reg_val, val, mux;
  2060. reg_val = snd_soc_read(widget->codec, e->reg);
  2061. val = (reg_val >> e->shift_l) & e->mask;
  2062. for (mux = 0; mux < e->max; mux++) {
  2063. if (val == e->values[mux])
  2064. break;
  2065. }
  2066. ucontrol->value.enumerated.item[0] = mux;
  2067. if (e->shift_l != e->shift_r) {
  2068. val = (reg_val >> e->shift_r) & e->mask;
  2069. for (mux = 0; mux < e->max; mux++) {
  2070. if (val == e->values[mux])
  2071. break;
  2072. }
  2073. ucontrol->value.enumerated.item[1] = mux;
  2074. }
  2075. return 0;
  2076. }
  2077. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  2078. /**
  2079. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  2080. * callback
  2081. * @kcontrol: mixer control
  2082. * @ucontrol: control element information
  2083. *
  2084. * Callback to set the value of a dapm semi enumerated double mixer control.
  2085. *
  2086. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2087. * used for handling bitfield coded enumeration for example.
  2088. *
  2089. * Returns 0 for success.
  2090. */
  2091. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2092. struct snd_ctl_elem_value *ucontrol)
  2093. {
  2094. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2095. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2096. struct snd_soc_codec *codec = widget->codec;
  2097. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2098. unsigned int val, mux, change;
  2099. unsigned int mask;
  2100. struct snd_soc_dapm_update update;
  2101. int wi;
  2102. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2103. return -EINVAL;
  2104. mux = ucontrol->value.enumerated.item[0];
  2105. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2106. mask = e->mask << e->shift_l;
  2107. if (e->shift_l != e->shift_r) {
  2108. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2109. return -EINVAL;
  2110. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2111. mask |= e->mask << e->shift_r;
  2112. }
  2113. mutex_lock(&codec->mutex);
  2114. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2115. if (change) {
  2116. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2117. widget = wlist->widgets[wi];
  2118. widget->value = val;
  2119. update.kcontrol = kcontrol;
  2120. update.widget = widget;
  2121. update.reg = e->reg;
  2122. update.mask = mask;
  2123. update.val = val;
  2124. widget->dapm->update = &update;
  2125. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  2126. widget->dapm->update = NULL;
  2127. }
  2128. }
  2129. mutex_unlock(&codec->mutex);
  2130. return change;
  2131. }
  2132. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  2133. /**
  2134. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2135. *
  2136. * @kcontrol: mixer control
  2137. * @uinfo: control element information
  2138. *
  2139. * Callback to provide information about a pin switch control.
  2140. */
  2141. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2142. struct snd_ctl_elem_info *uinfo)
  2143. {
  2144. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2145. uinfo->count = 1;
  2146. uinfo->value.integer.min = 0;
  2147. uinfo->value.integer.max = 1;
  2148. return 0;
  2149. }
  2150. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2151. /**
  2152. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2153. *
  2154. * @kcontrol: mixer control
  2155. * @ucontrol: Value
  2156. */
  2157. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2158. struct snd_ctl_elem_value *ucontrol)
  2159. {
  2160. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2161. const char *pin = (const char *)kcontrol->private_value;
  2162. mutex_lock(&codec->mutex);
  2163. ucontrol->value.integer.value[0] =
  2164. snd_soc_dapm_get_pin_status(&codec->dapm, pin);
  2165. mutex_unlock(&codec->mutex);
  2166. return 0;
  2167. }
  2168. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2169. /**
  2170. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2171. *
  2172. * @kcontrol: mixer control
  2173. * @ucontrol: Value
  2174. */
  2175. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2176. struct snd_ctl_elem_value *ucontrol)
  2177. {
  2178. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2179. const char *pin = (const char *)kcontrol->private_value;
  2180. mutex_lock(&codec->mutex);
  2181. if (ucontrol->value.integer.value[0])
  2182. snd_soc_dapm_enable_pin(&codec->dapm, pin);
  2183. else
  2184. snd_soc_dapm_disable_pin(&codec->dapm, pin);
  2185. snd_soc_dapm_sync(&codec->dapm);
  2186. mutex_unlock(&codec->mutex);
  2187. return 0;
  2188. }
  2189. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2190. /**
  2191. * snd_soc_dapm_new_control - create new dapm control
  2192. * @dapm: DAPM context
  2193. * @widget: widget template
  2194. *
  2195. * Creates a new dapm control based upon the template.
  2196. *
  2197. * Returns 0 for success else error.
  2198. */
  2199. int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  2200. const struct snd_soc_dapm_widget *widget)
  2201. {
  2202. struct snd_soc_dapm_widget *w;
  2203. size_t name_len;
  2204. if ((w = dapm_cnew_widget(widget)) == NULL)
  2205. return -ENOMEM;
  2206. name_len = strlen(widget->name) + 1;
  2207. if (dapm->codec && dapm->codec->name_prefix)
  2208. name_len += 1 + strlen(dapm->codec->name_prefix);
  2209. w->name = kmalloc(name_len, GFP_KERNEL);
  2210. if (w->name == NULL) {
  2211. kfree(w);
  2212. return -ENOMEM;
  2213. }
  2214. if (dapm->codec && dapm->codec->name_prefix)
  2215. snprintf(w->name, name_len, "%s %s",
  2216. dapm->codec->name_prefix, widget->name);
  2217. else
  2218. snprintf(w->name, name_len, "%s", widget->name);
  2219. dapm->n_widgets++;
  2220. w->dapm = dapm;
  2221. w->codec = dapm->codec;
  2222. w->platform = dapm->platform;
  2223. INIT_LIST_HEAD(&w->sources);
  2224. INIT_LIST_HEAD(&w->sinks);
  2225. INIT_LIST_HEAD(&w->list);
  2226. list_add(&w->list, &dapm->card->widgets);
  2227. /* machine layer set ups unconnected pins and insertions */
  2228. w->connected = 1;
  2229. return 0;
  2230. }
  2231. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  2232. /**
  2233. * snd_soc_dapm_new_controls - create new dapm controls
  2234. * @dapm: DAPM context
  2235. * @widget: widget array
  2236. * @num: number of widgets
  2237. *
  2238. * Creates new DAPM controls based upon the templates.
  2239. *
  2240. * Returns 0 for success else error.
  2241. */
  2242. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  2243. const struct snd_soc_dapm_widget *widget,
  2244. int num)
  2245. {
  2246. int i, ret;
  2247. for (i = 0; i < num; i++) {
  2248. ret = snd_soc_dapm_new_control(dapm, widget);
  2249. if (ret < 0) {
  2250. dev_err(dapm->dev,
  2251. "ASoC: Failed to create DAPM control %s: %d\n",
  2252. widget->name, ret);
  2253. return ret;
  2254. }
  2255. widget++;
  2256. }
  2257. return 0;
  2258. }
  2259. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  2260. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  2261. const char *stream, int event)
  2262. {
  2263. struct snd_soc_dapm_widget *w;
  2264. list_for_each_entry(w, &dapm->card->widgets, list)
  2265. {
  2266. if (!w->sname || w->dapm != dapm)
  2267. continue;
  2268. dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
  2269. w->name, w->sname, stream, event);
  2270. if (strstr(w->sname, stream)) {
  2271. switch(event) {
  2272. case SND_SOC_DAPM_STREAM_START:
  2273. w->active = 1;
  2274. break;
  2275. case SND_SOC_DAPM_STREAM_STOP:
  2276. w->active = 0;
  2277. break;
  2278. case SND_SOC_DAPM_STREAM_SUSPEND:
  2279. case SND_SOC_DAPM_STREAM_RESUME:
  2280. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2281. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2282. break;
  2283. }
  2284. }
  2285. }
  2286. dapm_power_widgets(dapm, event);
  2287. /* do we need to notify any clients that DAPM stream is complete */
  2288. if (dapm->stream_event)
  2289. dapm->stream_event(dapm, event);
  2290. }
  2291. /**
  2292. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  2293. * @rtd: PCM runtime data
  2294. * @stream: stream name
  2295. * @event: stream event
  2296. *
  2297. * Sends a stream event to the dapm core. The core then makes any
  2298. * necessary widget power changes.
  2299. *
  2300. * Returns 0 for success else error.
  2301. */
  2302. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
  2303. const char *stream, int event)
  2304. {
  2305. struct snd_soc_codec *codec = rtd->codec;
  2306. if (stream == NULL)
  2307. return 0;
  2308. mutex_lock(&codec->mutex);
  2309. soc_dapm_stream_event(&codec->dapm, stream, event);
  2310. mutex_unlock(&codec->mutex);
  2311. return 0;
  2312. }
  2313. /**
  2314. * snd_soc_dapm_enable_pin - enable pin.
  2315. * @dapm: DAPM context
  2316. * @pin: pin name
  2317. *
  2318. * Enables input/output pin and its parents or children widgets iff there is
  2319. * a valid audio route and active audio stream.
  2320. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2321. * do any widget power switching.
  2322. */
  2323. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2324. {
  2325. return snd_soc_dapm_set_pin(dapm, pin, 1);
  2326. }
  2327. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  2328. /**
  2329. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  2330. * @dapm: DAPM context
  2331. * @pin: pin name
  2332. *
  2333. * Enables input/output pin regardless of any other state. This is
  2334. * intended for use with microphone bias supplies used in microphone
  2335. * jack detection.
  2336. *
  2337. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2338. * do any widget power switching.
  2339. */
  2340. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  2341. const char *pin)
  2342. {
  2343. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2344. if (!w) {
  2345. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2346. return -EINVAL;
  2347. }
  2348. dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
  2349. w->connected = 1;
  2350. w->force = 1;
  2351. return 0;
  2352. }
  2353. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  2354. /**
  2355. * snd_soc_dapm_disable_pin - disable pin.
  2356. * @dapm: DAPM context
  2357. * @pin: pin name
  2358. *
  2359. * Disables input/output pin and its parents or children widgets.
  2360. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2361. * do any widget power switching.
  2362. */
  2363. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  2364. const char *pin)
  2365. {
  2366. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2367. }
  2368. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  2369. /**
  2370. * snd_soc_dapm_nc_pin - permanently disable pin.
  2371. * @dapm: DAPM context
  2372. * @pin: pin name
  2373. *
  2374. * Marks the specified pin as being not connected, disabling it along
  2375. * any parent or child widgets. At present this is identical to
  2376. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  2377. * additional things such as disabling controls which only affect
  2378. * paths through the pin.
  2379. *
  2380. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2381. * do any widget power switching.
  2382. */
  2383. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2384. {
  2385. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2386. }
  2387. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  2388. /**
  2389. * snd_soc_dapm_get_pin_status - get audio pin status
  2390. * @dapm: DAPM context
  2391. * @pin: audio signal pin endpoint (or start point)
  2392. *
  2393. * Get audio pin status - connected or disconnected.
  2394. *
  2395. * Returns 1 for connected otherwise 0.
  2396. */
  2397. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  2398. const char *pin)
  2399. {
  2400. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2401. if (w)
  2402. return w->connected;
  2403. return 0;
  2404. }
  2405. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  2406. /**
  2407. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  2408. * @dapm: DAPM context
  2409. * @pin: audio signal pin endpoint (or start point)
  2410. *
  2411. * Mark the given endpoint or pin as ignoring suspend. When the
  2412. * system is disabled a path between two endpoints flagged as ignoring
  2413. * suspend will not be disabled. The path must already be enabled via
  2414. * normal means at suspend time, it will not be turned on if it was not
  2415. * already enabled.
  2416. */
  2417. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  2418. const char *pin)
  2419. {
  2420. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  2421. if (!w) {
  2422. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2423. return -EINVAL;
  2424. }
  2425. w->ignore_suspend = 1;
  2426. return 0;
  2427. }
  2428. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  2429. /**
  2430. * snd_soc_dapm_free - free dapm resources
  2431. * @dapm: DAPM context
  2432. *
  2433. * Free all dapm widgets and resources.
  2434. */
  2435. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  2436. {
  2437. snd_soc_dapm_sys_remove(dapm->dev);
  2438. dapm_debugfs_cleanup(dapm);
  2439. dapm_free_widgets(dapm);
  2440. list_del(&dapm->list);
  2441. }
  2442. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  2443. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  2444. {
  2445. struct snd_soc_dapm_widget *w;
  2446. LIST_HEAD(down_list);
  2447. int powerdown = 0;
  2448. list_for_each_entry(w, &dapm->card->widgets, list) {
  2449. if (w->dapm != dapm)
  2450. continue;
  2451. if (w->power) {
  2452. dapm_seq_insert(w, &down_list, false);
  2453. w->power = 0;
  2454. powerdown = 1;
  2455. }
  2456. }
  2457. /* If there were no widgets to power down we're already in
  2458. * standby.
  2459. */
  2460. if (powerdown) {
  2461. snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
  2462. dapm_seq_run(dapm, &down_list, 0, false);
  2463. snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
  2464. }
  2465. }
  2466. /*
  2467. * snd_soc_dapm_shutdown - callback for system shutdown
  2468. */
  2469. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2470. {
  2471. struct snd_soc_codec *codec;
  2472. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2473. soc_dapm_shutdown_codec(&codec->dapm);
  2474. snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
  2475. }
  2476. }
  2477. /* Module information */
  2478. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2479. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2480. MODULE_LICENSE("GPL");