soc-dapm.c 76 KB

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