soc-dapm.c 65 KB

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