soc-dapm.c 70 KB

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