soc-dapm.c 69 KB

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