soc-dapm.c 68 KB

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