soc-dapm.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861
  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 if
  1136. * they're not ground referenced.
  1137. */
  1138. bias = SND_SOC_BIAS_OFF;
  1139. list_for_each_entry(d, &card->dapm_list, list)
  1140. if (d->target_bias_level > bias)
  1141. bias = d->target_bias_level;
  1142. list_for_each_entry(d, &card->dapm_list, list)
  1143. if (!d->idle_bias_off)
  1144. d->target_bias_level = bias;
  1145. trace_snd_soc_dapm_walk_done(card);
  1146. /* Run all the bias changes in parallel */
  1147. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1148. async_schedule_domain(dapm_pre_sequence_async, d,
  1149. &async_domain);
  1150. async_synchronize_full_domain(&async_domain);
  1151. /* Power down widgets first; try to avoid amplifying pops. */
  1152. dapm_seq_run(dapm, &down_list, event, false);
  1153. dapm_widget_update(dapm);
  1154. /* Now power up. */
  1155. dapm_seq_run(dapm, &up_list, event, true);
  1156. /* Run all the bias changes in parallel */
  1157. list_for_each_entry(d, &dapm->card->dapm_list, list)
  1158. async_schedule_domain(dapm_post_sequence_async, d,
  1159. &async_domain);
  1160. async_synchronize_full_domain(&async_domain);
  1161. pop_dbg(dapm->dev, card->pop_time,
  1162. "DAPM sequencing finished, waiting %dms\n", card->pop_time);
  1163. pop_wait(card->pop_time);
  1164. trace_snd_soc_dapm_done(card);
  1165. return 0;
  1166. }
  1167. #ifdef CONFIG_DEBUG_FS
  1168. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  1169. {
  1170. file->private_data = inode->i_private;
  1171. return 0;
  1172. }
  1173. static ssize_t dapm_widget_power_read_file(struct file *file,
  1174. char __user *user_buf,
  1175. size_t count, loff_t *ppos)
  1176. {
  1177. struct snd_soc_dapm_widget *w = file->private_data;
  1178. char *buf;
  1179. int in, out;
  1180. ssize_t ret;
  1181. struct snd_soc_dapm_path *p = NULL;
  1182. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1183. if (!buf)
  1184. return -ENOMEM;
  1185. in = is_connected_input_ep(w);
  1186. dapm_clear_walk(w->dapm);
  1187. out = is_connected_output_ep(w);
  1188. dapm_clear_walk(w->dapm);
  1189. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  1190. w->name, w->power ? "On" : "Off", in, out);
  1191. if (w->reg >= 0)
  1192. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1193. " - R%d(0x%x) bit %d",
  1194. w->reg, w->reg, w->shift);
  1195. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1196. if (w->sname)
  1197. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1198. w->sname,
  1199. w->active ? "active" : "inactive");
  1200. list_for_each_entry(p, &w->sources, list_sink) {
  1201. if (p->connected && !p->connected(w, p->sink))
  1202. continue;
  1203. if (p->connect)
  1204. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1205. " in \"%s\" \"%s\"\n",
  1206. p->name ? p->name : "static",
  1207. p->source->name);
  1208. }
  1209. list_for_each_entry(p, &w->sinks, list_source) {
  1210. if (p->connected && !p->connected(w, p->sink))
  1211. continue;
  1212. if (p->connect)
  1213. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1214. " out \"%s\" \"%s\"\n",
  1215. p->name ? p->name : "static",
  1216. p->sink->name);
  1217. }
  1218. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1219. kfree(buf);
  1220. return ret;
  1221. }
  1222. static const struct file_operations dapm_widget_power_fops = {
  1223. .open = dapm_widget_power_open_file,
  1224. .read = dapm_widget_power_read_file,
  1225. .llseek = default_llseek,
  1226. };
  1227. static int dapm_bias_open_file(struct inode *inode, struct file *file)
  1228. {
  1229. file->private_data = inode->i_private;
  1230. return 0;
  1231. }
  1232. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1233. size_t count, loff_t *ppos)
  1234. {
  1235. struct snd_soc_dapm_context *dapm = file->private_data;
  1236. char *level;
  1237. switch (dapm->bias_level) {
  1238. case SND_SOC_BIAS_ON:
  1239. level = "On\n";
  1240. break;
  1241. case SND_SOC_BIAS_PREPARE:
  1242. level = "Prepare\n";
  1243. break;
  1244. case SND_SOC_BIAS_STANDBY:
  1245. level = "Standby\n";
  1246. break;
  1247. case SND_SOC_BIAS_OFF:
  1248. level = "Off\n";
  1249. break;
  1250. default:
  1251. BUG();
  1252. level = "Unknown\n";
  1253. break;
  1254. }
  1255. return simple_read_from_buffer(user_buf, count, ppos, level,
  1256. strlen(level));
  1257. }
  1258. static const struct file_operations dapm_bias_fops = {
  1259. .open = dapm_bias_open_file,
  1260. .read = dapm_bias_read_file,
  1261. .llseek = default_llseek,
  1262. };
  1263. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1264. struct dentry *parent)
  1265. {
  1266. struct dentry *d;
  1267. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1268. if (!dapm->debugfs_dapm) {
  1269. printk(KERN_WARNING
  1270. "Failed to create DAPM debugfs directory\n");
  1271. return;
  1272. }
  1273. d = debugfs_create_file("bias_level", 0444,
  1274. dapm->debugfs_dapm, dapm,
  1275. &dapm_bias_fops);
  1276. if (!d)
  1277. dev_warn(dapm->dev,
  1278. "ASoC: Failed to create bias level debugfs file\n");
  1279. }
  1280. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1281. {
  1282. struct snd_soc_dapm_context *dapm = w->dapm;
  1283. struct dentry *d;
  1284. if (!dapm->debugfs_dapm || !w->name)
  1285. return;
  1286. d = debugfs_create_file(w->name, 0444,
  1287. dapm->debugfs_dapm, w,
  1288. &dapm_widget_power_fops);
  1289. if (!d)
  1290. dev_warn(w->dapm->dev,
  1291. "ASoC: Failed to create %s debugfs file\n",
  1292. w->name);
  1293. }
  1294. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1295. {
  1296. debugfs_remove_recursive(dapm->debugfs_dapm);
  1297. }
  1298. #else
  1299. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1300. struct dentry *parent)
  1301. {
  1302. }
  1303. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1304. {
  1305. }
  1306. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1307. {
  1308. }
  1309. #endif
  1310. /* test and update the power status of a mux widget */
  1311. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1312. struct snd_kcontrol *kcontrol, int change,
  1313. int mux, struct soc_enum *e)
  1314. {
  1315. struct snd_soc_dapm_path *path;
  1316. int found = 0;
  1317. if (widget->id != snd_soc_dapm_mux &&
  1318. widget->id != snd_soc_dapm_virt_mux &&
  1319. widget->id != snd_soc_dapm_value_mux)
  1320. return -ENODEV;
  1321. if (!change)
  1322. return 0;
  1323. /* find dapm widget path assoc with kcontrol */
  1324. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1325. if (path->kcontrol != kcontrol)
  1326. continue;
  1327. if (!path->name || !e->texts[mux])
  1328. continue;
  1329. found = 1;
  1330. /* we now need to match the string in the enum to the path */
  1331. if (!(strcmp(path->name, e->texts[mux])))
  1332. path->connect = 1; /* new connection */
  1333. else
  1334. path->connect = 0; /* old connection must be powered down */
  1335. }
  1336. if (found)
  1337. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1338. return 0;
  1339. }
  1340. /* test and update the power status of a mixer or switch widget */
  1341. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1342. struct snd_kcontrol *kcontrol, int connect)
  1343. {
  1344. struct snd_soc_dapm_path *path;
  1345. int found = 0;
  1346. if (widget->id != snd_soc_dapm_mixer &&
  1347. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1348. widget->id != snd_soc_dapm_switch)
  1349. return -ENODEV;
  1350. /* find dapm widget path assoc with kcontrol */
  1351. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1352. if (path->kcontrol != kcontrol)
  1353. continue;
  1354. /* found, now check type */
  1355. found = 1;
  1356. path->connect = connect;
  1357. }
  1358. if (found)
  1359. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1360. return 0;
  1361. }
  1362. /* show dapm widget status in sys fs */
  1363. static ssize_t dapm_widget_show(struct device *dev,
  1364. struct device_attribute *attr, char *buf)
  1365. {
  1366. struct snd_soc_pcm_runtime *rtd =
  1367. container_of(dev, struct snd_soc_pcm_runtime, dev);
  1368. struct snd_soc_codec *codec =rtd->codec;
  1369. struct snd_soc_dapm_widget *w;
  1370. int count = 0;
  1371. char *state = "not set";
  1372. list_for_each_entry(w, &codec->card->widgets, list) {
  1373. if (w->dapm != &codec->dapm)
  1374. continue;
  1375. /* only display widgets that burnm power */
  1376. switch (w->id) {
  1377. case snd_soc_dapm_hp:
  1378. case snd_soc_dapm_mic:
  1379. case snd_soc_dapm_spk:
  1380. case snd_soc_dapm_line:
  1381. case snd_soc_dapm_micbias:
  1382. case snd_soc_dapm_dac:
  1383. case snd_soc_dapm_adc:
  1384. case snd_soc_dapm_pga:
  1385. case snd_soc_dapm_out_drv:
  1386. case snd_soc_dapm_mixer:
  1387. case snd_soc_dapm_mixer_named_ctl:
  1388. case snd_soc_dapm_supply:
  1389. if (w->name)
  1390. count += sprintf(buf + count, "%s: %s\n",
  1391. w->name, w->power ? "On":"Off");
  1392. break;
  1393. default:
  1394. break;
  1395. }
  1396. }
  1397. switch (codec->dapm.bias_level) {
  1398. case SND_SOC_BIAS_ON:
  1399. state = "On";
  1400. break;
  1401. case SND_SOC_BIAS_PREPARE:
  1402. state = "Prepare";
  1403. break;
  1404. case SND_SOC_BIAS_STANDBY:
  1405. state = "Standby";
  1406. break;
  1407. case SND_SOC_BIAS_OFF:
  1408. state = "Off";
  1409. break;
  1410. }
  1411. count += sprintf(buf + count, "PM State: %s\n", state);
  1412. return count;
  1413. }
  1414. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1415. int snd_soc_dapm_sys_add(struct device *dev)
  1416. {
  1417. return device_create_file(dev, &dev_attr_dapm_widget);
  1418. }
  1419. static void snd_soc_dapm_sys_remove(struct device *dev)
  1420. {
  1421. device_remove_file(dev, &dev_attr_dapm_widget);
  1422. }
  1423. /* free all dapm widgets and resources */
  1424. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1425. {
  1426. struct snd_soc_dapm_widget *w, *next_w;
  1427. struct snd_soc_dapm_path *p, *next_p;
  1428. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1429. if (w->dapm != dapm)
  1430. continue;
  1431. list_del(&w->list);
  1432. /*
  1433. * remove source and sink paths associated to this widget.
  1434. * While removing the path, remove reference to it from both
  1435. * source and sink widgets so that path is removed only once.
  1436. */
  1437. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1438. list_del(&p->list_sink);
  1439. list_del(&p->list_source);
  1440. list_del(&p->list);
  1441. kfree(p->long_name);
  1442. kfree(p);
  1443. }
  1444. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1445. list_del(&p->list_sink);
  1446. list_del(&p->list_source);
  1447. list_del(&p->list);
  1448. kfree(p->long_name);
  1449. kfree(p);
  1450. }
  1451. kfree(w->kcontrols);
  1452. kfree(w->name);
  1453. kfree(w);
  1454. }
  1455. }
  1456. static struct snd_soc_dapm_widget *dapm_find_widget(
  1457. struct snd_soc_dapm_context *dapm, const char *pin,
  1458. bool search_other_contexts)
  1459. {
  1460. struct snd_soc_dapm_widget *w;
  1461. struct snd_soc_dapm_widget *fallback = NULL;
  1462. list_for_each_entry(w, &dapm->card->widgets, list) {
  1463. if (!strcmp(w->name, pin)) {
  1464. if (w->dapm == dapm)
  1465. return w;
  1466. else
  1467. fallback = w;
  1468. }
  1469. }
  1470. if (search_other_contexts)
  1471. return fallback;
  1472. return NULL;
  1473. }
  1474. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1475. const char *pin, int status)
  1476. {
  1477. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  1478. if (!w) {
  1479. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1480. return -EINVAL;
  1481. }
  1482. w->connected = status;
  1483. if (status == 0)
  1484. w->force = 0;
  1485. return 0;
  1486. }
  1487. /**
  1488. * snd_soc_dapm_sync - scan and power dapm paths
  1489. * @dapm: DAPM context
  1490. *
  1491. * Walks all dapm audio paths and powers widgets according to their
  1492. * stream or path usage.
  1493. *
  1494. * Returns 0 for success.
  1495. */
  1496. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1497. {
  1498. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1499. }
  1500. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1501. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1502. const struct snd_soc_dapm_route *route)
  1503. {
  1504. struct snd_soc_dapm_path *path;
  1505. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1506. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1507. const char *sink;
  1508. const char *control = route->control;
  1509. const char *source;
  1510. char prefixed_sink[80];
  1511. char prefixed_source[80];
  1512. int ret = 0;
  1513. if (dapm->codec && dapm->codec->name_prefix) {
  1514. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1515. dapm->codec->name_prefix, route->sink);
  1516. sink = prefixed_sink;
  1517. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1518. dapm->codec->name_prefix, route->source);
  1519. source = prefixed_source;
  1520. } else {
  1521. sink = route->sink;
  1522. source = route->source;
  1523. }
  1524. /*
  1525. * find src and dest widgets over all widgets but favor a widget from
  1526. * current DAPM context
  1527. */
  1528. list_for_each_entry(w, &dapm->card->widgets, list) {
  1529. if (!wsink && !(strcmp(w->name, sink))) {
  1530. wtsink = w;
  1531. if (w->dapm == dapm)
  1532. wsink = w;
  1533. continue;
  1534. }
  1535. if (!wsource && !(strcmp(w->name, source))) {
  1536. wtsource = w;
  1537. if (w->dapm == dapm)
  1538. wsource = w;
  1539. }
  1540. }
  1541. /* use widget from another DAPM context if not found from this */
  1542. if (!wsink)
  1543. wsink = wtsink;
  1544. if (!wsource)
  1545. wsource = wtsource;
  1546. if (wsource == NULL || wsink == NULL)
  1547. return -ENODEV;
  1548. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1549. if (!path)
  1550. return -ENOMEM;
  1551. path->source = wsource;
  1552. path->sink = wsink;
  1553. path->connected = route->connected;
  1554. INIT_LIST_HEAD(&path->list);
  1555. INIT_LIST_HEAD(&path->list_source);
  1556. INIT_LIST_HEAD(&path->list_sink);
  1557. /* check for external widgets */
  1558. if (wsink->id == snd_soc_dapm_input) {
  1559. if (wsource->id == snd_soc_dapm_micbias ||
  1560. wsource->id == snd_soc_dapm_mic ||
  1561. wsource->id == snd_soc_dapm_line ||
  1562. wsource->id == snd_soc_dapm_output)
  1563. wsink->ext = 1;
  1564. }
  1565. if (wsource->id == snd_soc_dapm_output) {
  1566. if (wsink->id == snd_soc_dapm_spk ||
  1567. wsink->id == snd_soc_dapm_hp ||
  1568. wsink->id == snd_soc_dapm_line ||
  1569. wsink->id == snd_soc_dapm_input)
  1570. wsource->ext = 1;
  1571. }
  1572. /* connect static paths */
  1573. if (control == NULL) {
  1574. list_add(&path->list, &dapm->card->paths);
  1575. list_add(&path->list_sink, &wsink->sources);
  1576. list_add(&path->list_source, &wsource->sinks);
  1577. path->connect = 1;
  1578. return 0;
  1579. }
  1580. /* connect dynamic paths */
  1581. switch (wsink->id) {
  1582. case snd_soc_dapm_adc:
  1583. case snd_soc_dapm_dac:
  1584. case snd_soc_dapm_pga:
  1585. case snd_soc_dapm_out_drv:
  1586. case snd_soc_dapm_input:
  1587. case snd_soc_dapm_output:
  1588. case snd_soc_dapm_micbias:
  1589. case snd_soc_dapm_vmid:
  1590. case snd_soc_dapm_pre:
  1591. case snd_soc_dapm_post:
  1592. case snd_soc_dapm_supply:
  1593. case snd_soc_dapm_aif_in:
  1594. case snd_soc_dapm_aif_out:
  1595. list_add(&path->list, &dapm->card->paths);
  1596. list_add(&path->list_sink, &wsink->sources);
  1597. list_add(&path->list_source, &wsource->sinks);
  1598. path->connect = 1;
  1599. return 0;
  1600. case snd_soc_dapm_mux:
  1601. case snd_soc_dapm_virt_mux:
  1602. case snd_soc_dapm_value_mux:
  1603. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1604. &wsink->kcontrol_news[0]);
  1605. if (ret != 0)
  1606. goto err;
  1607. break;
  1608. case snd_soc_dapm_switch:
  1609. case snd_soc_dapm_mixer:
  1610. case snd_soc_dapm_mixer_named_ctl:
  1611. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1612. if (ret != 0)
  1613. goto err;
  1614. break;
  1615. case snd_soc_dapm_hp:
  1616. case snd_soc_dapm_mic:
  1617. case snd_soc_dapm_line:
  1618. case snd_soc_dapm_spk:
  1619. list_add(&path->list, &dapm->card->paths);
  1620. list_add(&path->list_sink, &wsink->sources);
  1621. list_add(&path->list_source, &wsource->sinks);
  1622. path->connect = 0;
  1623. return 0;
  1624. }
  1625. return 0;
  1626. err:
  1627. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1628. source, control, sink);
  1629. kfree(path);
  1630. return ret;
  1631. }
  1632. /**
  1633. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1634. * @dapm: DAPM context
  1635. * @route: audio routes
  1636. * @num: number of routes
  1637. *
  1638. * Connects 2 dapm widgets together via a named audio path. The sink is
  1639. * the widget receiving the audio signal, whilst the source is the sender
  1640. * of the audio signal.
  1641. *
  1642. * Returns 0 for success else error. On error all resources can be freed
  1643. * with a call to snd_soc_card_free().
  1644. */
  1645. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1646. const struct snd_soc_dapm_route *route, int num)
  1647. {
  1648. int i, ret;
  1649. for (i = 0; i < num; i++) {
  1650. ret = snd_soc_dapm_add_route(dapm, route);
  1651. if (ret < 0) {
  1652. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1653. route->source, route->sink);
  1654. return ret;
  1655. }
  1656. route++;
  1657. }
  1658. return 0;
  1659. }
  1660. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1661. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  1662. const struct snd_soc_dapm_route *route)
  1663. {
  1664. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  1665. route->source,
  1666. true);
  1667. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  1668. route->sink,
  1669. true);
  1670. struct snd_soc_dapm_path *path;
  1671. int count = 0;
  1672. if (!source) {
  1673. dev_err(dapm->dev, "Unable to find source %s for weak route\n",
  1674. route->source);
  1675. return -ENODEV;
  1676. }
  1677. if (!sink) {
  1678. dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
  1679. route->sink);
  1680. return -ENODEV;
  1681. }
  1682. if (route->control || route->connected)
  1683. dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
  1684. route->source, route->sink);
  1685. list_for_each_entry(path, &source->sinks, list_source) {
  1686. if (path->sink == sink) {
  1687. path->weak = 1;
  1688. count++;
  1689. }
  1690. }
  1691. if (count == 0)
  1692. dev_err(dapm->dev, "No path found for weak route %s->%s\n",
  1693. route->source, route->sink);
  1694. if (count > 1)
  1695. dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
  1696. count, route->source, route->sink);
  1697. return 0;
  1698. }
  1699. /**
  1700. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  1701. * @dapm: DAPM context
  1702. * @route: audio routes
  1703. * @num: number of routes
  1704. *
  1705. * Mark existing routes matching those specified in the passed array
  1706. * as being weak, meaning that they are ignored for the purpose of
  1707. * power decisions. The main intended use case is for sidetone paths
  1708. * which couple audio between other independent paths if they are both
  1709. * active in order to make the combination work better at the user
  1710. * level but which aren't intended to be "used".
  1711. *
  1712. * Note that CODEC drivers should not use this as sidetone type paths
  1713. * can frequently also be used as bypass paths.
  1714. */
  1715. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  1716. const struct snd_soc_dapm_route *route, int num)
  1717. {
  1718. int i, err;
  1719. int ret = 0;
  1720. for (i = 0; i < num; i++) {
  1721. err = snd_soc_dapm_weak_route(dapm, route);
  1722. if (err)
  1723. ret = err;
  1724. route++;
  1725. }
  1726. return ret;
  1727. }
  1728. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  1729. /**
  1730. * snd_soc_dapm_new_widgets - add new dapm widgets
  1731. * @dapm: DAPM context
  1732. *
  1733. * Checks the codec for any new dapm widgets and creates them if found.
  1734. *
  1735. * Returns 0 for success.
  1736. */
  1737. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1738. {
  1739. struct snd_soc_dapm_widget *w;
  1740. unsigned int val;
  1741. list_for_each_entry(w, &dapm->card->widgets, list)
  1742. {
  1743. if (w->new)
  1744. continue;
  1745. if (w->num_kcontrols) {
  1746. w->kcontrols = kzalloc(w->num_kcontrols *
  1747. sizeof(struct snd_kcontrol *),
  1748. GFP_KERNEL);
  1749. if (!w->kcontrols)
  1750. return -ENOMEM;
  1751. }
  1752. switch(w->id) {
  1753. case snd_soc_dapm_switch:
  1754. case snd_soc_dapm_mixer:
  1755. case snd_soc_dapm_mixer_named_ctl:
  1756. w->power_check = dapm_generic_check_power;
  1757. dapm_new_mixer(w);
  1758. break;
  1759. case snd_soc_dapm_mux:
  1760. case snd_soc_dapm_virt_mux:
  1761. case snd_soc_dapm_value_mux:
  1762. w->power_check = dapm_generic_check_power;
  1763. dapm_new_mux(w);
  1764. break;
  1765. case snd_soc_dapm_adc:
  1766. case snd_soc_dapm_aif_out:
  1767. w->power_check = dapm_adc_check_power;
  1768. break;
  1769. case snd_soc_dapm_dac:
  1770. case snd_soc_dapm_aif_in:
  1771. w->power_check = dapm_dac_check_power;
  1772. break;
  1773. case snd_soc_dapm_pga:
  1774. case snd_soc_dapm_out_drv:
  1775. w->power_check = dapm_generic_check_power;
  1776. dapm_new_pga(w);
  1777. break;
  1778. case snd_soc_dapm_input:
  1779. case snd_soc_dapm_output:
  1780. case snd_soc_dapm_micbias:
  1781. case snd_soc_dapm_spk:
  1782. case snd_soc_dapm_hp:
  1783. case snd_soc_dapm_mic:
  1784. case snd_soc_dapm_line:
  1785. w->power_check = dapm_generic_check_power;
  1786. break;
  1787. case snd_soc_dapm_supply:
  1788. w->power_check = dapm_supply_check_power;
  1789. case snd_soc_dapm_vmid:
  1790. case snd_soc_dapm_pre:
  1791. case snd_soc_dapm_post:
  1792. break;
  1793. }
  1794. /* Read the initial power state from the device */
  1795. if (w->reg >= 0) {
  1796. val = soc_widget_read(w, w->reg);
  1797. val &= 1 << w->shift;
  1798. if (w->invert)
  1799. val = !val;
  1800. if (val)
  1801. w->power = 1;
  1802. }
  1803. w->new = 1;
  1804. dapm_debugfs_add_widget(w);
  1805. }
  1806. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1807. return 0;
  1808. }
  1809. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1810. /**
  1811. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1812. * @kcontrol: mixer control
  1813. * @ucontrol: control element information
  1814. *
  1815. * Callback to get the value of a dapm mixer control.
  1816. *
  1817. * Returns 0 for success.
  1818. */
  1819. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1820. struct snd_ctl_elem_value *ucontrol)
  1821. {
  1822. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1823. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1824. struct soc_mixer_control *mc =
  1825. (struct soc_mixer_control *)kcontrol->private_value;
  1826. unsigned int reg = mc->reg;
  1827. unsigned int shift = mc->shift;
  1828. unsigned int rshift = mc->rshift;
  1829. int max = mc->max;
  1830. unsigned int invert = mc->invert;
  1831. unsigned int mask = (1 << fls(max)) - 1;
  1832. ucontrol->value.integer.value[0] =
  1833. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1834. if (shift != rshift)
  1835. ucontrol->value.integer.value[1] =
  1836. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1837. if (invert) {
  1838. ucontrol->value.integer.value[0] =
  1839. max - ucontrol->value.integer.value[0];
  1840. if (shift != rshift)
  1841. ucontrol->value.integer.value[1] =
  1842. max - ucontrol->value.integer.value[1];
  1843. }
  1844. return 0;
  1845. }
  1846. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1847. /**
  1848. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1849. * @kcontrol: mixer control
  1850. * @ucontrol: control element information
  1851. *
  1852. * Callback to set the value of a dapm mixer control.
  1853. *
  1854. * Returns 0 for success.
  1855. */
  1856. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1857. struct snd_ctl_elem_value *ucontrol)
  1858. {
  1859. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1860. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1861. struct snd_soc_codec *codec = widget->codec;
  1862. struct soc_mixer_control *mc =
  1863. (struct soc_mixer_control *)kcontrol->private_value;
  1864. unsigned int reg = mc->reg;
  1865. unsigned int shift = mc->shift;
  1866. int max = mc->max;
  1867. unsigned int mask = (1 << fls(max)) - 1;
  1868. unsigned int invert = mc->invert;
  1869. unsigned int val;
  1870. int connect, change;
  1871. struct snd_soc_dapm_update update;
  1872. int wi;
  1873. val = (ucontrol->value.integer.value[0] & mask);
  1874. if (invert)
  1875. val = max - val;
  1876. mask = mask << shift;
  1877. val = val << shift;
  1878. if (val)
  1879. /* new connection */
  1880. connect = invert ? 0 : 1;
  1881. else
  1882. /* old connection must be powered down */
  1883. connect = invert ? 1 : 0;
  1884. mutex_lock(&codec->mutex);
  1885. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  1886. if (change) {
  1887. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1888. widget = wlist->widgets[wi];
  1889. widget->value = val;
  1890. update.kcontrol = kcontrol;
  1891. update.widget = widget;
  1892. update.reg = reg;
  1893. update.mask = mask;
  1894. update.val = val;
  1895. widget->dapm->update = &update;
  1896. dapm_mixer_update_power(widget, kcontrol, connect);
  1897. widget->dapm->update = NULL;
  1898. }
  1899. }
  1900. mutex_unlock(&codec->mutex);
  1901. return 0;
  1902. }
  1903. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1904. /**
  1905. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1906. * @kcontrol: mixer control
  1907. * @ucontrol: control element information
  1908. *
  1909. * Callback to get the value of a dapm enumerated double mixer control.
  1910. *
  1911. * Returns 0 for success.
  1912. */
  1913. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1914. struct snd_ctl_elem_value *ucontrol)
  1915. {
  1916. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1917. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1918. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1919. unsigned int val, bitmask;
  1920. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1921. ;
  1922. val = snd_soc_read(widget->codec, e->reg);
  1923. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1924. if (e->shift_l != e->shift_r)
  1925. ucontrol->value.enumerated.item[1] =
  1926. (val >> e->shift_r) & (bitmask - 1);
  1927. return 0;
  1928. }
  1929. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1930. /**
  1931. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1932. * @kcontrol: mixer control
  1933. * @ucontrol: control element information
  1934. *
  1935. * Callback to set the value of a dapm enumerated double mixer control.
  1936. *
  1937. * Returns 0 for success.
  1938. */
  1939. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1940. struct snd_ctl_elem_value *ucontrol)
  1941. {
  1942. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1943. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1944. struct snd_soc_codec *codec = widget->codec;
  1945. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1946. unsigned int val, mux, change;
  1947. unsigned int mask, bitmask;
  1948. struct snd_soc_dapm_update update;
  1949. int wi;
  1950. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1951. ;
  1952. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1953. return -EINVAL;
  1954. mux = ucontrol->value.enumerated.item[0];
  1955. val = mux << e->shift_l;
  1956. mask = (bitmask - 1) << e->shift_l;
  1957. if (e->shift_l != e->shift_r) {
  1958. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1959. return -EINVAL;
  1960. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1961. mask |= (bitmask - 1) << e->shift_r;
  1962. }
  1963. mutex_lock(&codec->mutex);
  1964. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1965. if (change) {
  1966. for (wi = 0; wi < wlist->num_widgets; wi++) {
  1967. widget = wlist->widgets[wi];
  1968. widget->value = val;
  1969. update.kcontrol = kcontrol;
  1970. update.widget = widget;
  1971. update.reg = e->reg;
  1972. update.mask = mask;
  1973. update.val = val;
  1974. widget->dapm->update = &update;
  1975. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1976. widget->dapm->update = NULL;
  1977. }
  1978. }
  1979. mutex_unlock(&codec->mutex);
  1980. return change;
  1981. }
  1982. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1983. /**
  1984. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1985. * @kcontrol: mixer control
  1986. * @ucontrol: control element information
  1987. *
  1988. * Returns 0 for success.
  1989. */
  1990. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1991. struct snd_ctl_elem_value *ucontrol)
  1992. {
  1993. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1994. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1995. ucontrol->value.enumerated.item[0] = widget->value;
  1996. return 0;
  1997. }
  1998. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1999. /**
  2000. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  2001. * @kcontrol: mixer control
  2002. * @ucontrol: control element information
  2003. *
  2004. * Returns 0 for success.
  2005. */
  2006. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  2007. struct snd_ctl_elem_value *ucontrol)
  2008. {
  2009. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2010. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2011. struct snd_soc_codec *codec = widget->codec;
  2012. struct soc_enum *e =
  2013. (struct soc_enum *)kcontrol->private_value;
  2014. int change;
  2015. int ret = 0;
  2016. int wi;
  2017. if (ucontrol->value.enumerated.item[0] >= e->max)
  2018. return -EINVAL;
  2019. mutex_lock(&codec->mutex);
  2020. change = widget->value != ucontrol->value.enumerated.item[0];
  2021. if (change) {
  2022. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2023. widget = wlist->widgets[wi];
  2024. widget->value = ucontrol->value.enumerated.item[0];
  2025. dapm_mux_update_power(widget, kcontrol, change,
  2026. widget->value, e);
  2027. }
  2028. }
  2029. mutex_unlock(&codec->mutex);
  2030. return ret;
  2031. }
  2032. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  2033. /**
  2034. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  2035. * callback
  2036. * @kcontrol: mixer control
  2037. * @ucontrol: control element information
  2038. *
  2039. * Callback to get the value of a dapm semi enumerated double mixer control.
  2040. *
  2041. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2042. * used for handling bitfield coded enumeration for example.
  2043. *
  2044. * Returns 0 for success.
  2045. */
  2046. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2047. struct snd_ctl_elem_value *ucontrol)
  2048. {
  2049. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2050. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2051. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2052. unsigned int reg_val, val, mux;
  2053. reg_val = snd_soc_read(widget->codec, e->reg);
  2054. val = (reg_val >> e->shift_l) & e->mask;
  2055. for (mux = 0; mux < e->max; mux++) {
  2056. if (val == e->values[mux])
  2057. break;
  2058. }
  2059. ucontrol->value.enumerated.item[0] = mux;
  2060. if (e->shift_l != e->shift_r) {
  2061. val = (reg_val >> e->shift_r) & e->mask;
  2062. for (mux = 0; mux < e->max; mux++) {
  2063. if (val == e->values[mux])
  2064. break;
  2065. }
  2066. ucontrol->value.enumerated.item[1] = mux;
  2067. }
  2068. return 0;
  2069. }
  2070. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  2071. /**
  2072. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  2073. * callback
  2074. * @kcontrol: mixer control
  2075. * @ucontrol: control element information
  2076. *
  2077. * Callback to set the value of a dapm semi enumerated double mixer control.
  2078. *
  2079. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2080. * used for handling bitfield coded enumeration for example.
  2081. *
  2082. * Returns 0 for success.
  2083. */
  2084. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2085. struct snd_ctl_elem_value *ucontrol)
  2086. {
  2087. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2088. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2089. struct snd_soc_codec *codec = widget->codec;
  2090. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2091. unsigned int val, mux, change;
  2092. unsigned int mask;
  2093. struct snd_soc_dapm_update update;
  2094. int wi;
  2095. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2096. return -EINVAL;
  2097. mux = ucontrol->value.enumerated.item[0];
  2098. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2099. mask = e->mask << e->shift_l;
  2100. if (e->shift_l != e->shift_r) {
  2101. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2102. return -EINVAL;
  2103. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2104. mask |= e->mask << e->shift_r;
  2105. }
  2106. mutex_lock(&codec->mutex);
  2107. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2108. if (change) {
  2109. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2110. widget = wlist->widgets[wi];
  2111. widget->value = val;
  2112. update.kcontrol = kcontrol;
  2113. update.widget = widget;
  2114. update.reg = e->reg;
  2115. update.mask = mask;
  2116. update.val = val;
  2117. widget->dapm->update = &update;
  2118. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  2119. widget->dapm->update = NULL;
  2120. }
  2121. }
  2122. mutex_unlock(&codec->mutex);
  2123. return change;
  2124. }
  2125. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  2126. /**
  2127. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2128. *
  2129. * @kcontrol: mixer control
  2130. * @uinfo: control element information
  2131. *
  2132. * Callback to provide information about a pin switch control.
  2133. */
  2134. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2135. struct snd_ctl_elem_info *uinfo)
  2136. {
  2137. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2138. uinfo->count = 1;
  2139. uinfo->value.integer.min = 0;
  2140. uinfo->value.integer.max = 1;
  2141. return 0;
  2142. }
  2143. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2144. /**
  2145. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2146. *
  2147. * @kcontrol: mixer control
  2148. * @ucontrol: Value
  2149. */
  2150. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2151. struct snd_ctl_elem_value *ucontrol)
  2152. {
  2153. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2154. const char *pin = (const char *)kcontrol->private_value;
  2155. mutex_lock(&codec->mutex);
  2156. ucontrol->value.integer.value[0] =
  2157. snd_soc_dapm_get_pin_status(&codec->dapm, pin);
  2158. mutex_unlock(&codec->mutex);
  2159. return 0;
  2160. }
  2161. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2162. /**
  2163. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2164. *
  2165. * @kcontrol: mixer control
  2166. * @ucontrol: Value
  2167. */
  2168. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2169. struct snd_ctl_elem_value *ucontrol)
  2170. {
  2171. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  2172. const char *pin = (const char *)kcontrol->private_value;
  2173. mutex_lock(&codec->mutex);
  2174. if (ucontrol->value.integer.value[0])
  2175. snd_soc_dapm_enable_pin(&codec->dapm, pin);
  2176. else
  2177. snd_soc_dapm_disable_pin(&codec->dapm, pin);
  2178. snd_soc_dapm_sync(&codec->dapm);
  2179. mutex_unlock(&codec->mutex);
  2180. return 0;
  2181. }
  2182. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2183. /**
  2184. * snd_soc_dapm_new_control - create new dapm control
  2185. * @dapm: DAPM context
  2186. * @widget: widget template
  2187. *
  2188. * Creates a new dapm control based upon the template.
  2189. *
  2190. * Returns 0 for success else error.
  2191. */
  2192. int snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  2193. const struct snd_soc_dapm_widget *widget)
  2194. {
  2195. struct snd_soc_dapm_widget *w;
  2196. size_t name_len;
  2197. if ((w = dapm_cnew_widget(widget)) == NULL)
  2198. return -ENOMEM;
  2199. name_len = strlen(widget->name) + 1;
  2200. if (dapm->codec && dapm->codec->name_prefix)
  2201. name_len += 1 + strlen(dapm->codec->name_prefix);
  2202. w->name = kmalloc(name_len, GFP_KERNEL);
  2203. if (w->name == NULL) {
  2204. kfree(w);
  2205. return -ENOMEM;
  2206. }
  2207. if (dapm->codec && dapm->codec->name_prefix)
  2208. snprintf(w->name, name_len, "%s %s",
  2209. dapm->codec->name_prefix, widget->name);
  2210. else
  2211. snprintf(w->name, name_len, "%s", widget->name);
  2212. dapm->n_widgets++;
  2213. w->dapm = dapm;
  2214. w->codec = dapm->codec;
  2215. w->platform = dapm->platform;
  2216. INIT_LIST_HEAD(&w->sources);
  2217. INIT_LIST_HEAD(&w->sinks);
  2218. INIT_LIST_HEAD(&w->list);
  2219. list_add(&w->list, &dapm->card->widgets);
  2220. /* machine layer set ups unconnected pins and insertions */
  2221. w->connected = 1;
  2222. return 0;
  2223. }
  2224. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  2225. /**
  2226. * snd_soc_dapm_new_controls - create new dapm controls
  2227. * @dapm: DAPM context
  2228. * @widget: widget array
  2229. * @num: number of widgets
  2230. *
  2231. * Creates new DAPM controls based upon the templates.
  2232. *
  2233. * Returns 0 for success else error.
  2234. */
  2235. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  2236. const struct snd_soc_dapm_widget *widget,
  2237. int num)
  2238. {
  2239. int i, ret;
  2240. for (i = 0; i < num; i++) {
  2241. ret = snd_soc_dapm_new_control(dapm, widget);
  2242. if (ret < 0) {
  2243. dev_err(dapm->dev,
  2244. "ASoC: Failed to create DAPM control %s: %d\n",
  2245. widget->name, ret);
  2246. return ret;
  2247. }
  2248. widget++;
  2249. }
  2250. return 0;
  2251. }
  2252. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  2253. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  2254. const char *stream, int event)
  2255. {
  2256. struct snd_soc_dapm_widget *w;
  2257. list_for_each_entry(w, &dapm->card->widgets, list)
  2258. {
  2259. if (!w->sname || w->dapm != dapm)
  2260. continue;
  2261. dev_vdbg(w->dapm->dev, "widget %s\n %s stream %s event %d\n",
  2262. w->name, w->sname, stream, event);
  2263. if (strstr(w->sname, stream)) {
  2264. switch(event) {
  2265. case SND_SOC_DAPM_STREAM_START:
  2266. w->active = 1;
  2267. break;
  2268. case SND_SOC_DAPM_STREAM_STOP:
  2269. w->active = 0;
  2270. break;
  2271. case SND_SOC_DAPM_STREAM_SUSPEND:
  2272. case SND_SOC_DAPM_STREAM_RESUME:
  2273. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2274. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2275. break;
  2276. }
  2277. }
  2278. }
  2279. dapm_power_widgets(dapm, event);
  2280. /* do we need to notify any clients that DAPM stream is complete */
  2281. if (dapm->stream_event)
  2282. dapm->stream_event(dapm, event);
  2283. }
  2284. /**
  2285. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  2286. * @rtd: PCM runtime data
  2287. * @stream: stream name
  2288. * @event: stream event
  2289. *
  2290. * Sends a stream event to the dapm core. The core then makes any
  2291. * necessary widget power changes.
  2292. *
  2293. * Returns 0 for success else error.
  2294. */
  2295. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd,
  2296. const char *stream, int event)
  2297. {
  2298. struct snd_soc_codec *codec = rtd->codec;
  2299. if (stream == NULL)
  2300. return 0;
  2301. mutex_lock(&codec->mutex);
  2302. soc_dapm_stream_event(&codec->dapm, stream, event);
  2303. mutex_unlock(&codec->mutex);
  2304. return 0;
  2305. }
  2306. /**
  2307. * snd_soc_dapm_enable_pin - enable pin.
  2308. * @dapm: DAPM context
  2309. * @pin: pin name
  2310. *
  2311. * Enables input/output pin and its parents or children widgets iff there is
  2312. * a valid audio route and active audio stream.
  2313. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2314. * do any widget power switching.
  2315. */
  2316. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2317. {
  2318. return snd_soc_dapm_set_pin(dapm, pin, 1);
  2319. }
  2320. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  2321. /**
  2322. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  2323. * @dapm: DAPM context
  2324. * @pin: pin name
  2325. *
  2326. * Enables input/output pin regardless of any other state. This is
  2327. * intended for use with microphone bias supplies used in microphone
  2328. * jack detection.
  2329. *
  2330. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2331. * do any widget power switching.
  2332. */
  2333. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  2334. const char *pin)
  2335. {
  2336. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2337. if (!w) {
  2338. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2339. return -EINVAL;
  2340. }
  2341. dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
  2342. w->connected = 1;
  2343. w->force = 1;
  2344. return 0;
  2345. }
  2346. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  2347. /**
  2348. * snd_soc_dapm_disable_pin - disable pin.
  2349. * @dapm: DAPM context
  2350. * @pin: pin name
  2351. *
  2352. * Disables input/output pin and its parents or children widgets.
  2353. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2354. * do any widget power switching.
  2355. */
  2356. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  2357. const char *pin)
  2358. {
  2359. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2360. }
  2361. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  2362. /**
  2363. * snd_soc_dapm_nc_pin - permanently disable pin.
  2364. * @dapm: DAPM context
  2365. * @pin: pin name
  2366. *
  2367. * Marks the specified pin as being not connected, disabling it along
  2368. * any parent or child widgets. At present this is identical to
  2369. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  2370. * additional things such as disabling controls which only affect
  2371. * paths through the pin.
  2372. *
  2373. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2374. * do any widget power switching.
  2375. */
  2376. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2377. {
  2378. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2379. }
  2380. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  2381. /**
  2382. * snd_soc_dapm_get_pin_status - get audio pin status
  2383. * @dapm: DAPM context
  2384. * @pin: audio signal pin endpoint (or start point)
  2385. *
  2386. * Get audio pin status - connected or disconnected.
  2387. *
  2388. * Returns 1 for connected otherwise 0.
  2389. */
  2390. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  2391. const char *pin)
  2392. {
  2393. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2394. if (w)
  2395. return w->connected;
  2396. return 0;
  2397. }
  2398. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  2399. /**
  2400. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  2401. * @dapm: DAPM context
  2402. * @pin: audio signal pin endpoint (or start point)
  2403. *
  2404. * Mark the given endpoint or pin as ignoring suspend. When the
  2405. * system is disabled a path between two endpoints flagged as ignoring
  2406. * suspend will not be disabled. The path must already be enabled via
  2407. * normal means at suspend time, it will not be turned on if it was not
  2408. * already enabled.
  2409. */
  2410. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  2411. const char *pin)
  2412. {
  2413. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  2414. if (!w) {
  2415. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2416. return -EINVAL;
  2417. }
  2418. w->ignore_suspend = 1;
  2419. return 0;
  2420. }
  2421. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  2422. /**
  2423. * snd_soc_dapm_free - free dapm resources
  2424. * @dapm: DAPM context
  2425. *
  2426. * Free all dapm widgets and resources.
  2427. */
  2428. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  2429. {
  2430. snd_soc_dapm_sys_remove(dapm->dev);
  2431. dapm_debugfs_cleanup(dapm);
  2432. dapm_free_widgets(dapm);
  2433. list_del(&dapm->list);
  2434. }
  2435. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  2436. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  2437. {
  2438. struct snd_soc_dapm_widget *w;
  2439. LIST_HEAD(down_list);
  2440. int powerdown = 0;
  2441. list_for_each_entry(w, &dapm->card->widgets, list) {
  2442. if (w->dapm != dapm)
  2443. continue;
  2444. if (w->power) {
  2445. dapm_seq_insert(w, &down_list, false);
  2446. w->power = 0;
  2447. powerdown = 1;
  2448. }
  2449. }
  2450. /* If there were no widgets to power down we're already in
  2451. * standby.
  2452. */
  2453. if (powerdown) {
  2454. snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_PREPARE);
  2455. dapm_seq_run(dapm, &down_list, 0, false);
  2456. snd_soc_dapm_set_bias_level(dapm, SND_SOC_BIAS_STANDBY);
  2457. }
  2458. }
  2459. /*
  2460. * snd_soc_dapm_shutdown - callback for system shutdown
  2461. */
  2462. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2463. {
  2464. struct snd_soc_codec *codec;
  2465. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2466. soc_dapm_shutdown_codec(&codec->dapm);
  2467. snd_soc_dapm_set_bias_level(&codec->dapm, SND_SOC_BIAS_OFF);
  2468. }
  2469. }
  2470. /* Module information */
  2471. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2472. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2473. MODULE_LICENSE("GPL");