soc-dapm.c 69 KB

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