soc-dapm.c 70 KB

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