soc-dapm.c 76 KB

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