soc-dapm.c 73 KB

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