soc-dapm.c 65 KB

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