soc-dapm.c 79 KB

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