soc-dapm.c 73 KB

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