soc-dapm.c 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271
  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 int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  1326. {
  1327. file->private_data = inode->i_private;
  1328. return 0;
  1329. }
  1330. static ssize_t dapm_widget_power_read_file(struct file *file,
  1331. char __user *user_buf,
  1332. size_t count, loff_t *ppos)
  1333. {
  1334. struct snd_soc_dapm_widget *w = file->private_data;
  1335. char *buf;
  1336. int in, out;
  1337. ssize_t ret;
  1338. struct snd_soc_dapm_path *p = NULL;
  1339. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  1340. if (!buf)
  1341. return -ENOMEM;
  1342. in = is_connected_input_ep(w);
  1343. dapm_clear_walk(w->dapm);
  1344. out = is_connected_output_ep(w);
  1345. dapm_clear_walk(w->dapm);
  1346. ret = snprintf(buf, PAGE_SIZE, "%s: %s%s in %d out %d",
  1347. w->name, w->power ? "On" : "Off",
  1348. w->force ? " (forced)" : "", in, out);
  1349. if (w->reg >= 0)
  1350. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1351. " - R%d(0x%x) bit %d",
  1352. w->reg, w->reg, w->shift);
  1353. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  1354. if (w->sname)
  1355. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  1356. w->sname,
  1357. w->active ? "active" : "inactive");
  1358. list_for_each_entry(p, &w->sources, list_sink) {
  1359. if (p->connected && !p->connected(w, p->sink))
  1360. continue;
  1361. if (p->connect)
  1362. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1363. " in \"%s\" \"%s\"\n",
  1364. p->name ? p->name : "static",
  1365. p->source->name);
  1366. }
  1367. list_for_each_entry(p, &w->sinks, list_source) {
  1368. if (p->connected && !p->connected(w, p->sink))
  1369. continue;
  1370. if (p->connect)
  1371. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  1372. " out \"%s\" \"%s\"\n",
  1373. p->name ? p->name : "static",
  1374. p->sink->name);
  1375. }
  1376. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  1377. kfree(buf);
  1378. return ret;
  1379. }
  1380. static const struct file_operations dapm_widget_power_fops = {
  1381. .open = dapm_widget_power_open_file,
  1382. .read = dapm_widget_power_read_file,
  1383. .llseek = default_llseek,
  1384. };
  1385. static int dapm_bias_open_file(struct inode *inode, struct file *file)
  1386. {
  1387. file->private_data = inode->i_private;
  1388. return 0;
  1389. }
  1390. static ssize_t dapm_bias_read_file(struct file *file, char __user *user_buf,
  1391. size_t count, loff_t *ppos)
  1392. {
  1393. struct snd_soc_dapm_context *dapm = file->private_data;
  1394. char *level;
  1395. switch (dapm->bias_level) {
  1396. case SND_SOC_BIAS_ON:
  1397. level = "On\n";
  1398. break;
  1399. case SND_SOC_BIAS_PREPARE:
  1400. level = "Prepare\n";
  1401. break;
  1402. case SND_SOC_BIAS_STANDBY:
  1403. level = "Standby\n";
  1404. break;
  1405. case SND_SOC_BIAS_OFF:
  1406. level = "Off\n";
  1407. break;
  1408. default:
  1409. BUG();
  1410. level = "Unknown\n";
  1411. break;
  1412. }
  1413. return simple_read_from_buffer(user_buf, count, ppos, level,
  1414. strlen(level));
  1415. }
  1416. static const struct file_operations dapm_bias_fops = {
  1417. .open = dapm_bias_open_file,
  1418. .read = dapm_bias_read_file,
  1419. .llseek = default_llseek,
  1420. };
  1421. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1422. struct dentry *parent)
  1423. {
  1424. struct dentry *d;
  1425. dapm->debugfs_dapm = debugfs_create_dir("dapm", parent);
  1426. if (!dapm->debugfs_dapm) {
  1427. dev_warn(dapm->dev,
  1428. "Failed to create DAPM debugfs directory\n");
  1429. return;
  1430. }
  1431. d = debugfs_create_file("bias_level", 0444,
  1432. dapm->debugfs_dapm, dapm,
  1433. &dapm_bias_fops);
  1434. if (!d)
  1435. dev_warn(dapm->dev,
  1436. "ASoC: Failed to create bias level debugfs file\n");
  1437. }
  1438. static void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1439. {
  1440. struct snd_soc_dapm_context *dapm = w->dapm;
  1441. struct dentry *d;
  1442. if (!dapm->debugfs_dapm || !w->name)
  1443. return;
  1444. d = debugfs_create_file(w->name, 0444,
  1445. dapm->debugfs_dapm, w,
  1446. &dapm_widget_power_fops);
  1447. if (!d)
  1448. dev_warn(w->dapm->dev,
  1449. "ASoC: Failed to create %s debugfs file\n",
  1450. w->name);
  1451. }
  1452. static void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1453. {
  1454. debugfs_remove_recursive(dapm->debugfs_dapm);
  1455. }
  1456. #else
  1457. void snd_soc_dapm_debugfs_init(struct snd_soc_dapm_context *dapm,
  1458. struct dentry *parent)
  1459. {
  1460. }
  1461. static inline void dapm_debugfs_add_widget(struct snd_soc_dapm_widget *w)
  1462. {
  1463. }
  1464. static inline void dapm_debugfs_cleanup(struct snd_soc_dapm_context *dapm)
  1465. {
  1466. }
  1467. #endif
  1468. /* test and update the power status of a mux widget */
  1469. int snd_soc_dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  1470. struct snd_kcontrol *kcontrol, int mux, struct soc_enum *e)
  1471. {
  1472. struct snd_soc_dapm_path *path;
  1473. int found = 0;
  1474. if (widget->id != snd_soc_dapm_mux &&
  1475. widget->id != snd_soc_dapm_virt_mux &&
  1476. widget->id != snd_soc_dapm_value_mux)
  1477. return -ENODEV;
  1478. /* find dapm widget path assoc with kcontrol */
  1479. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1480. if (path->kcontrol != kcontrol)
  1481. continue;
  1482. if (!path->name || !e->texts[mux])
  1483. continue;
  1484. found = 1;
  1485. /* we now need to match the string in the enum to the path */
  1486. if (!(strcmp(path->name, e->texts[mux]))) {
  1487. path->connect = 1; /* new connection */
  1488. dapm_mark_dirty(path->source, "mux connection");
  1489. } else {
  1490. if (path->connect)
  1491. dapm_mark_dirty(path->source,
  1492. "mux disconnection");
  1493. path->connect = 0; /* old connection must be powered down */
  1494. }
  1495. }
  1496. if (found) {
  1497. dapm_mark_dirty(widget, "mux change");
  1498. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1499. }
  1500. return 0;
  1501. }
  1502. EXPORT_SYMBOL_GPL(snd_soc_dapm_mux_update_power);
  1503. /* test and update the power status of a mixer or switch widget */
  1504. int snd_soc_dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  1505. struct snd_kcontrol *kcontrol, int connect)
  1506. {
  1507. struct snd_soc_dapm_path *path;
  1508. int found = 0;
  1509. if (widget->id != snd_soc_dapm_mixer &&
  1510. widget->id != snd_soc_dapm_mixer_named_ctl &&
  1511. widget->id != snd_soc_dapm_switch)
  1512. return -ENODEV;
  1513. /* find dapm widget path assoc with kcontrol */
  1514. list_for_each_entry(path, &widget->dapm->card->paths, list) {
  1515. if (path->kcontrol != kcontrol)
  1516. continue;
  1517. /* found, now check type */
  1518. found = 1;
  1519. path->connect = connect;
  1520. dapm_mark_dirty(path->source, "mixer connection");
  1521. }
  1522. if (found) {
  1523. dapm_mark_dirty(widget, "mixer update");
  1524. dapm_power_widgets(widget->dapm, SND_SOC_DAPM_STREAM_NOP);
  1525. }
  1526. return 0;
  1527. }
  1528. EXPORT_SYMBOL_GPL(snd_soc_dapm_mixer_update_power);
  1529. /* show dapm widget status in sys fs */
  1530. static ssize_t dapm_widget_show(struct device *dev,
  1531. struct device_attribute *attr, char *buf)
  1532. {
  1533. struct snd_soc_pcm_runtime *rtd = dev_get_drvdata(dev);
  1534. struct snd_soc_codec *codec =rtd->codec;
  1535. struct snd_soc_dapm_widget *w;
  1536. int count = 0;
  1537. char *state = "not set";
  1538. list_for_each_entry(w, &codec->card->widgets, list) {
  1539. if (w->dapm != &codec->dapm)
  1540. continue;
  1541. /* only display widgets that burnm power */
  1542. switch (w->id) {
  1543. case snd_soc_dapm_hp:
  1544. case snd_soc_dapm_mic:
  1545. case snd_soc_dapm_spk:
  1546. case snd_soc_dapm_line:
  1547. case snd_soc_dapm_micbias:
  1548. case snd_soc_dapm_dac:
  1549. case snd_soc_dapm_adc:
  1550. case snd_soc_dapm_pga:
  1551. case snd_soc_dapm_out_drv:
  1552. case snd_soc_dapm_mixer:
  1553. case snd_soc_dapm_mixer_named_ctl:
  1554. case snd_soc_dapm_supply:
  1555. case snd_soc_dapm_regulator_supply:
  1556. if (w->name)
  1557. count += sprintf(buf + count, "%s: %s\n",
  1558. w->name, w->power ? "On":"Off");
  1559. break;
  1560. default:
  1561. break;
  1562. }
  1563. }
  1564. switch (codec->dapm.bias_level) {
  1565. case SND_SOC_BIAS_ON:
  1566. state = "On";
  1567. break;
  1568. case SND_SOC_BIAS_PREPARE:
  1569. state = "Prepare";
  1570. break;
  1571. case SND_SOC_BIAS_STANDBY:
  1572. state = "Standby";
  1573. break;
  1574. case SND_SOC_BIAS_OFF:
  1575. state = "Off";
  1576. break;
  1577. }
  1578. count += sprintf(buf + count, "PM State: %s\n", state);
  1579. return count;
  1580. }
  1581. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1582. int snd_soc_dapm_sys_add(struct device *dev)
  1583. {
  1584. return device_create_file(dev, &dev_attr_dapm_widget);
  1585. }
  1586. static void snd_soc_dapm_sys_remove(struct device *dev)
  1587. {
  1588. device_remove_file(dev, &dev_attr_dapm_widget);
  1589. }
  1590. /* free all dapm widgets and resources */
  1591. static void dapm_free_widgets(struct snd_soc_dapm_context *dapm)
  1592. {
  1593. struct snd_soc_dapm_widget *w, *next_w;
  1594. struct snd_soc_dapm_path *p, *next_p;
  1595. list_for_each_entry_safe(w, next_w, &dapm->card->widgets, list) {
  1596. if (w->dapm != dapm)
  1597. continue;
  1598. list_del(&w->list);
  1599. /*
  1600. * remove source and sink paths associated to this widget.
  1601. * While removing the path, remove reference to it from both
  1602. * source and sink widgets so that path is removed only once.
  1603. */
  1604. list_for_each_entry_safe(p, next_p, &w->sources, list_sink) {
  1605. list_del(&p->list_sink);
  1606. list_del(&p->list_source);
  1607. list_del(&p->list);
  1608. kfree(p->long_name);
  1609. kfree(p);
  1610. }
  1611. list_for_each_entry_safe(p, next_p, &w->sinks, list_source) {
  1612. list_del(&p->list_sink);
  1613. list_del(&p->list_source);
  1614. list_del(&p->list);
  1615. kfree(p->long_name);
  1616. kfree(p);
  1617. }
  1618. kfree(w->kcontrols);
  1619. kfree(w->name);
  1620. kfree(w);
  1621. }
  1622. }
  1623. static struct snd_soc_dapm_widget *dapm_find_widget(
  1624. struct snd_soc_dapm_context *dapm, const char *pin,
  1625. bool search_other_contexts)
  1626. {
  1627. struct snd_soc_dapm_widget *w;
  1628. struct snd_soc_dapm_widget *fallback = NULL;
  1629. list_for_each_entry(w, &dapm->card->widgets, list) {
  1630. if (!strcmp(w->name, pin)) {
  1631. if (w->dapm == dapm)
  1632. return w;
  1633. else
  1634. fallback = w;
  1635. }
  1636. }
  1637. if (search_other_contexts)
  1638. return fallback;
  1639. return NULL;
  1640. }
  1641. static int snd_soc_dapm_set_pin(struct snd_soc_dapm_context *dapm,
  1642. const char *pin, int status)
  1643. {
  1644. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  1645. if (!w) {
  1646. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  1647. return -EINVAL;
  1648. }
  1649. if (w->connected != status)
  1650. dapm_mark_dirty(w, "pin configuration");
  1651. w->connected = status;
  1652. if (status == 0)
  1653. w->force = 0;
  1654. return 0;
  1655. }
  1656. /**
  1657. * snd_soc_dapm_sync - scan and power dapm paths
  1658. * @dapm: DAPM context
  1659. *
  1660. * Walks all dapm audio paths and powers widgets according to their
  1661. * stream or path usage.
  1662. *
  1663. * Returns 0 for success.
  1664. */
  1665. int snd_soc_dapm_sync(struct snd_soc_dapm_context *dapm)
  1666. {
  1667. /*
  1668. * Suppress early reports (eg, jacks syncing their state) to avoid
  1669. * silly DAPM runs during card startup.
  1670. */
  1671. if (!dapm->card || !dapm->card->instantiated)
  1672. return 0;
  1673. return dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1674. }
  1675. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1676. static int snd_soc_dapm_add_route(struct snd_soc_dapm_context *dapm,
  1677. const struct snd_soc_dapm_route *route)
  1678. {
  1679. struct snd_soc_dapm_path *path;
  1680. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1681. struct snd_soc_dapm_widget *wtsource = NULL, *wtsink = NULL;
  1682. const char *sink;
  1683. const char *control = route->control;
  1684. const char *source;
  1685. char prefixed_sink[80];
  1686. char prefixed_source[80];
  1687. int ret = 0;
  1688. if (dapm->codec && dapm->codec->name_prefix) {
  1689. snprintf(prefixed_sink, sizeof(prefixed_sink), "%s %s",
  1690. dapm->codec->name_prefix, route->sink);
  1691. sink = prefixed_sink;
  1692. snprintf(prefixed_source, sizeof(prefixed_source), "%s %s",
  1693. dapm->codec->name_prefix, route->source);
  1694. source = prefixed_source;
  1695. } else {
  1696. sink = route->sink;
  1697. source = route->source;
  1698. }
  1699. /*
  1700. * find src and dest widgets over all widgets but favor a widget from
  1701. * current DAPM context
  1702. */
  1703. list_for_each_entry(w, &dapm->card->widgets, list) {
  1704. if (!wsink && !(strcmp(w->name, sink))) {
  1705. wtsink = w;
  1706. if (w->dapm == dapm)
  1707. wsink = w;
  1708. continue;
  1709. }
  1710. if (!wsource && !(strcmp(w->name, source))) {
  1711. wtsource = w;
  1712. if (w->dapm == dapm)
  1713. wsource = w;
  1714. }
  1715. }
  1716. /* use widget from another DAPM context if not found from this */
  1717. if (!wsink)
  1718. wsink = wtsink;
  1719. if (!wsource)
  1720. wsource = wtsource;
  1721. if (wsource == NULL || wsink == NULL)
  1722. return -ENODEV;
  1723. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1724. if (!path)
  1725. return -ENOMEM;
  1726. path->source = wsource;
  1727. path->sink = wsink;
  1728. path->connected = route->connected;
  1729. INIT_LIST_HEAD(&path->list);
  1730. INIT_LIST_HEAD(&path->list_source);
  1731. INIT_LIST_HEAD(&path->list_sink);
  1732. /* check for external widgets */
  1733. if (wsink->id == snd_soc_dapm_input) {
  1734. if (wsource->id == snd_soc_dapm_micbias ||
  1735. wsource->id == snd_soc_dapm_mic ||
  1736. wsource->id == snd_soc_dapm_line ||
  1737. wsource->id == snd_soc_dapm_output)
  1738. wsink->ext = 1;
  1739. }
  1740. if (wsource->id == snd_soc_dapm_output) {
  1741. if (wsink->id == snd_soc_dapm_spk ||
  1742. wsink->id == snd_soc_dapm_hp ||
  1743. wsink->id == snd_soc_dapm_line ||
  1744. wsink->id == snd_soc_dapm_input)
  1745. wsource->ext = 1;
  1746. }
  1747. /* connect static paths */
  1748. if (control == NULL) {
  1749. list_add(&path->list, &dapm->card->paths);
  1750. list_add(&path->list_sink, &wsink->sources);
  1751. list_add(&path->list_source, &wsource->sinks);
  1752. path->connect = 1;
  1753. return 0;
  1754. }
  1755. /* connect dynamic paths */
  1756. switch (wsink->id) {
  1757. case snd_soc_dapm_adc:
  1758. case snd_soc_dapm_dac:
  1759. case snd_soc_dapm_pga:
  1760. case snd_soc_dapm_out_drv:
  1761. case snd_soc_dapm_input:
  1762. case snd_soc_dapm_output:
  1763. case snd_soc_dapm_siggen:
  1764. case snd_soc_dapm_micbias:
  1765. case snd_soc_dapm_vmid:
  1766. case snd_soc_dapm_pre:
  1767. case snd_soc_dapm_post:
  1768. case snd_soc_dapm_supply:
  1769. case snd_soc_dapm_regulator_supply:
  1770. case snd_soc_dapm_aif_in:
  1771. case snd_soc_dapm_aif_out:
  1772. case snd_soc_dapm_dai:
  1773. list_add(&path->list, &dapm->card->paths);
  1774. list_add(&path->list_sink, &wsink->sources);
  1775. list_add(&path->list_source, &wsource->sinks);
  1776. path->connect = 1;
  1777. return 0;
  1778. case snd_soc_dapm_mux:
  1779. case snd_soc_dapm_virt_mux:
  1780. case snd_soc_dapm_value_mux:
  1781. ret = dapm_connect_mux(dapm, wsource, wsink, path, control,
  1782. &wsink->kcontrol_news[0]);
  1783. if (ret != 0)
  1784. goto err;
  1785. break;
  1786. case snd_soc_dapm_switch:
  1787. case snd_soc_dapm_mixer:
  1788. case snd_soc_dapm_mixer_named_ctl:
  1789. ret = dapm_connect_mixer(dapm, wsource, wsink, path, control);
  1790. if (ret != 0)
  1791. goto err;
  1792. break;
  1793. case snd_soc_dapm_hp:
  1794. case snd_soc_dapm_mic:
  1795. case snd_soc_dapm_line:
  1796. case snd_soc_dapm_spk:
  1797. list_add(&path->list, &dapm->card->paths);
  1798. list_add(&path->list_sink, &wsink->sources);
  1799. list_add(&path->list_source, &wsource->sinks);
  1800. path->connect = 0;
  1801. return 0;
  1802. }
  1803. return 0;
  1804. err:
  1805. dev_warn(dapm->dev, "asoc: no dapm match for %s --> %s --> %s\n",
  1806. source, control, sink);
  1807. kfree(path);
  1808. return ret;
  1809. }
  1810. /**
  1811. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1812. * @dapm: DAPM context
  1813. * @route: audio routes
  1814. * @num: number of routes
  1815. *
  1816. * Connects 2 dapm widgets together via a named audio path. The sink is
  1817. * the widget receiving the audio signal, whilst the source is the sender
  1818. * of the audio signal.
  1819. *
  1820. * Returns 0 for success else error. On error all resources can be freed
  1821. * with a call to snd_soc_card_free().
  1822. */
  1823. int snd_soc_dapm_add_routes(struct snd_soc_dapm_context *dapm,
  1824. const struct snd_soc_dapm_route *route, int num)
  1825. {
  1826. int i, ret;
  1827. for (i = 0; i < num; i++) {
  1828. ret = snd_soc_dapm_add_route(dapm, route);
  1829. if (ret < 0) {
  1830. dev_err(dapm->dev, "Failed to add route %s->%s\n",
  1831. route->source, route->sink);
  1832. return ret;
  1833. }
  1834. route++;
  1835. }
  1836. return 0;
  1837. }
  1838. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1839. static int snd_soc_dapm_weak_route(struct snd_soc_dapm_context *dapm,
  1840. const struct snd_soc_dapm_route *route)
  1841. {
  1842. struct snd_soc_dapm_widget *source = dapm_find_widget(dapm,
  1843. route->source,
  1844. true);
  1845. struct snd_soc_dapm_widget *sink = dapm_find_widget(dapm,
  1846. route->sink,
  1847. true);
  1848. struct snd_soc_dapm_path *path;
  1849. int count = 0;
  1850. if (!source) {
  1851. dev_err(dapm->dev, "Unable to find source %s for weak route\n",
  1852. route->source);
  1853. return -ENODEV;
  1854. }
  1855. if (!sink) {
  1856. dev_err(dapm->dev, "Unable to find sink %s for weak route\n",
  1857. route->sink);
  1858. return -ENODEV;
  1859. }
  1860. if (route->control || route->connected)
  1861. dev_warn(dapm->dev, "Ignoring control for weak route %s->%s\n",
  1862. route->source, route->sink);
  1863. list_for_each_entry(path, &source->sinks, list_source) {
  1864. if (path->sink == sink) {
  1865. path->weak = 1;
  1866. count++;
  1867. }
  1868. }
  1869. if (count == 0)
  1870. dev_err(dapm->dev, "No path found for weak route %s->%s\n",
  1871. route->source, route->sink);
  1872. if (count > 1)
  1873. dev_warn(dapm->dev, "%d paths found for weak route %s->%s\n",
  1874. count, route->source, route->sink);
  1875. return 0;
  1876. }
  1877. /**
  1878. * snd_soc_dapm_weak_routes - Mark routes between DAPM widgets as weak
  1879. * @dapm: DAPM context
  1880. * @route: audio routes
  1881. * @num: number of routes
  1882. *
  1883. * Mark existing routes matching those specified in the passed array
  1884. * as being weak, meaning that they are ignored for the purpose of
  1885. * power decisions. The main intended use case is for sidetone paths
  1886. * which couple audio between other independent paths if they are both
  1887. * active in order to make the combination work better at the user
  1888. * level but which aren't intended to be "used".
  1889. *
  1890. * Note that CODEC drivers should not use this as sidetone type paths
  1891. * can frequently also be used as bypass paths.
  1892. */
  1893. int snd_soc_dapm_weak_routes(struct snd_soc_dapm_context *dapm,
  1894. const struct snd_soc_dapm_route *route, int num)
  1895. {
  1896. int i, err;
  1897. int ret = 0;
  1898. for (i = 0; i < num; i++) {
  1899. err = snd_soc_dapm_weak_route(dapm, route);
  1900. if (err)
  1901. ret = err;
  1902. route++;
  1903. }
  1904. return ret;
  1905. }
  1906. EXPORT_SYMBOL_GPL(snd_soc_dapm_weak_routes);
  1907. /**
  1908. * snd_soc_dapm_new_widgets - add new dapm widgets
  1909. * @dapm: DAPM context
  1910. *
  1911. * Checks the codec for any new dapm widgets and creates them if found.
  1912. *
  1913. * Returns 0 for success.
  1914. */
  1915. int snd_soc_dapm_new_widgets(struct snd_soc_dapm_context *dapm)
  1916. {
  1917. struct snd_soc_dapm_widget *w;
  1918. unsigned int val;
  1919. list_for_each_entry(w, &dapm->card->widgets, list)
  1920. {
  1921. if (w->new)
  1922. continue;
  1923. if (w->num_kcontrols) {
  1924. w->kcontrols = kzalloc(w->num_kcontrols *
  1925. sizeof(struct snd_kcontrol *),
  1926. GFP_KERNEL);
  1927. if (!w->kcontrols)
  1928. return -ENOMEM;
  1929. }
  1930. switch(w->id) {
  1931. case snd_soc_dapm_switch:
  1932. case snd_soc_dapm_mixer:
  1933. case snd_soc_dapm_mixer_named_ctl:
  1934. dapm_new_mixer(w);
  1935. break;
  1936. case snd_soc_dapm_mux:
  1937. case snd_soc_dapm_virt_mux:
  1938. case snd_soc_dapm_value_mux:
  1939. dapm_new_mux(w);
  1940. break;
  1941. case snd_soc_dapm_pga:
  1942. case snd_soc_dapm_out_drv:
  1943. dapm_new_pga(w);
  1944. break;
  1945. default:
  1946. break;
  1947. }
  1948. /* Read the initial power state from the device */
  1949. if (w->reg >= 0) {
  1950. val = soc_widget_read(w, w->reg);
  1951. val &= 1 << w->shift;
  1952. if (w->invert)
  1953. val = !val;
  1954. if (val)
  1955. w->power = 1;
  1956. }
  1957. w->new = 1;
  1958. dapm_mark_dirty(w, "new widget");
  1959. dapm_debugfs_add_widget(w);
  1960. }
  1961. dapm_power_widgets(dapm, SND_SOC_DAPM_STREAM_NOP);
  1962. return 0;
  1963. }
  1964. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1965. /**
  1966. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1967. * @kcontrol: mixer control
  1968. * @ucontrol: control element information
  1969. *
  1970. * Callback to get the value of a dapm mixer control.
  1971. *
  1972. * Returns 0 for success.
  1973. */
  1974. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1975. struct snd_ctl_elem_value *ucontrol)
  1976. {
  1977. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  1978. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  1979. struct soc_mixer_control *mc =
  1980. (struct soc_mixer_control *)kcontrol->private_value;
  1981. unsigned int reg = mc->reg;
  1982. unsigned int shift = mc->shift;
  1983. unsigned int rshift = mc->rshift;
  1984. int max = mc->max;
  1985. unsigned int invert = mc->invert;
  1986. unsigned int mask = (1 << fls(max)) - 1;
  1987. ucontrol->value.integer.value[0] =
  1988. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1989. if (shift != rshift)
  1990. ucontrol->value.integer.value[1] =
  1991. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1992. if (invert) {
  1993. ucontrol->value.integer.value[0] =
  1994. max - ucontrol->value.integer.value[0];
  1995. if (shift != rshift)
  1996. ucontrol->value.integer.value[1] =
  1997. max - ucontrol->value.integer.value[1];
  1998. }
  1999. return 0;
  2000. }
  2001. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  2002. /**
  2003. * snd_soc_dapm_put_volsw - dapm mixer set callback
  2004. * @kcontrol: mixer control
  2005. * @ucontrol: control element information
  2006. *
  2007. * Callback to set the value of a dapm mixer control.
  2008. *
  2009. * Returns 0 for success.
  2010. */
  2011. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  2012. struct snd_ctl_elem_value *ucontrol)
  2013. {
  2014. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2015. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2016. struct snd_soc_codec *codec = widget->codec;
  2017. struct soc_mixer_control *mc =
  2018. (struct soc_mixer_control *)kcontrol->private_value;
  2019. unsigned int reg = mc->reg;
  2020. unsigned int shift = mc->shift;
  2021. int max = mc->max;
  2022. unsigned int mask = (1 << fls(max)) - 1;
  2023. unsigned int invert = mc->invert;
  2024. unsigned int val;
  2025. int connect, change;
  2026. struct snd_soc_dapm_update update;
  2027. int wi;
  2028. val = (ucontrol->value.integer.value[0] & mask);
  2029. if (invert)
  2030. val = max - val;
  2031. mask = mask << shift;
  2032. val = val << shift;
  2033. if (val)
  2034. /* new connection */
  2035. connect = invert ? 0 : 1;
  2036. else
  2037. /* old connection must be powered down */
  2038. connect = invert ? 1 : 0;
  2039. mutex_lock(&codec->mutex);
  2040. change = snd_soc_test_bits(widget->codec, reg, mask, val);
  2041. if (change) {
  2042. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2043. widget = wlist->widgets[wi];
  2044. widget->value = val;
  2045. update.kcontrol = kcontrol;
  2046. update.widget = widget;
  2047. update.reg = reg;
  2048. update.mask = mask;
  2049. update.val = val;
  2050. widget->dapm->update = &update;
  2051. snd_soc_dapm_mixer_update_power(widget, kcontrol, connect);
  2052. widget->dapm->update = NULL;
  2053. }
  2054. }
  2055. mutex_unlock(&codec->mutex);
  2056. return 0;
  2057. }
  2058. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  2059. /**
  2060. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  2061. * @kcontrol: mixer control
  2062. * @ucontrol: control element information
  2063. *
  2064. * Callback to get the value of a dapm enumerated double mixer control.
  2065. *
  2066. * Returns 0 for success.
  2067. */
  2068. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  2069. struct snd_ctl_elem_value *ucontrol)
  2070. {
  2071. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2072. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2073. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2074. unsigned int val, bitmask;
  2075. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  2076. ;
  2077. val = snd_soc_read(widget->codec, e->reg);
  2078. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  2079. if (e->shift_l != e->shift_r)
  2080. ucontrol->value.enumerated.item[1] =
  2081. (val >> e->shift_r) & (bitmask - 1);
  2082. return 0;
  2083. }
  2084. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  2085. /**
  2086. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  2087. * @kcontrol: mixer control
  2088. * @ucontrol: control element information
  2089. *
  2090. * Callback to set the value of a dapm enumerated double mixer control.
  2091. *
  2092. * Returns 0 for success.
  2093. */
  2094. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  2095. struct snd_ctl_elem_value *ucontrol)
  2096. {
  2097. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2098. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2099. struct snd_soc_codec *codec = widget->codec;
  2100. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2101. unsigned int val, mux, change;
  2102. unsigned int mask, bitmask;
  2103. struct snd_soc_dapm_update update;
  2104. int wi;
  2105. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  2106. ;
  2107. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2108. return -EINVAL;
  2109. mux = ucontrol->value.enumerated.item[0];
  2110. val = mux << e->shift_l;
  2111. mask = (bitmask - 1) << e->shift_l;
  2112. if (e->shift_l != e->shift_r) {
  2113. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2114. return -EINVAL;
  2115. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  2116. mask |= (bitmask - 1) << e->shift_r;
  2117. }
  2118. mutex_lock(&codec->mutex);
  2119. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2120. if (change) {
  2121. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2122. widget = wlist->widgets[wi];
  2123. widget->value = val;
  2124. update.kcontrol = kcontrol;
  2125. update.widget = widget;
  2126. update.reg = e->reg;
  2127. update.mask = mask;
  2128. update.val = val;
  2129. widget->dapm->update = &update;
  2130. snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2131. widget->dapm->update = NULL;
  2132. }
  2133. }
  2134. mutex_unlock(&codec->mutex);
  2135. return change;
  2136. }
  2137. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  2138. /**
  2139. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  2140. * @kcontrol: mixer control
  2141. * @ucontrol: control element information
  2142. *
  2143. * Returns 0 for success.
  2144. */
  2145. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  2146. struct snd_ctl_elem_value *ucontrol)
  2147. {
  2148. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2149. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2150. ucontrol->value.enumerated.item[0] = widget->value;
  2151. return 0;
  2152. }
  2153. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  2154. /**
  2155. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  2156. * @kcontrol: mixer control
  2157. * @ucontrol: control element information
  2158. *
  2159. * Returns 0 for success.
  2160. */
  2161. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  2162. struct snd_ctl_elem_value *ucontrol)
  2163. {
  2164. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2165. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2166. struct snd_soc_codec *codec = widget->codec;
  2167. struct soc_enum *e =
  2168. (struct soc_enum *)kcontrol->private_value;
  2169. int change;
  2170. int ret = 0;
  2171. int wi;
  2172. if (ucontrol->value.enumerated.item[0] >= e->max)
  2173. return -EINVAL;
  2174. mutex_lock(&codec->mutex);
  2175. change = widget->value != ucontrol->value.enumerated.item[0];
  2176. if (change) {
  2177. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2178. widget = wlist->widgets[wi];
  2179. widget->value = ucontrol->value.enumerated.item[0];
  2180. snd_soc_dapm_mux_update_power(widget, kcontrol, widget->value, e);
  2181. }
  2182. }
  2183. mutex_unlock(&codec->mutex);
  2184. return ret;
  2185. }
  2186. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  2187. /**
  2188. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  2189. * callback
  2190. * @kcontrol: mixer control
  2191. * @ucontrol: control element information
  2192. *
  2193. * Callback to get the value of a dapm semi enumerated double mixer control.
  2194. *
  2195. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2196. * used for handling bitfield coded enumeration for example.
  2197. *
  2198. * Returns 0 for success.
  2199. */
  2200. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  2201. struct snd_ctl_elem_value *ucontrol)
  2202. {
  2203. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2204. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2205. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2206. unsigned int reg_val, val, mux;
  2207. reg_val = snd_soc_read(widget->codec, e->reg);
  2208. val = (reg_val >> e->shift_l) & e->mask;
  2209. for (mux = 0; mux < e->max; mux++) {
  2210. if (val == e->values[mux])
  2211. break;
  2212. }
  2213. ucontrol->value.enumerated.item[0] = mux;
  2214. if (e->shift_l != e->shift_r) {
  2215. val = (reg_val >> e->shift_r) & e->mask;
  2216. for (mux = 0; mux < e->max; mux++) {
  2217. if (val == e->values[mux])
  2218. break;
  2219. }
  2220. ucontrol->value.enumerated.item[1] = mux;
  2221. }
  2222. return 0;
  2223. }
  2224. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  2225. /**
  2226. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  2227. * callback
  2228. * @kcontrol: mixer control
  2229. * @ucontrol: control element information
  2230. *
  2231. * Callback to set the value of a dapm semi enumerated double mixer control.
  2232. *
  2233. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  2234. * used for handling bitfield coded enumeration for example.
  2235. *
  2236. * Returns 0 for success.
  2237. */
  2238. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  2239. struct snd_ctl_elem_value *ucontrol)
  2240. {
  2241. struct snd_soc_dapm_widget_list *wlist = snd_kcontrol_chip(kcontrol);
  2242. struct snd_soc_dapm_widget *widget = wlist->widgets[0];
  2243. struct snd_soc_codec *codec = widget->codec;
  2244. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  2245. unsigned int val, mux, change;
  2246. unsigned int mask;
  2247. struct snd_soc_dapm_update update;
  2248. int wi;
  2249. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  2250. return -EINVAL;
  2251. mux = ucontrol->value.enumerated.item[0];
  2252. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  2253. mask = e->mask << e->shift_l;
  2254. if (e->shift_l != e->shift_r) {
  2255. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  2256. return -EINVAL;
  2257. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  2258. mask |= e->mask << e->shift_r;
  2259. }
  2260. mutex_lock(&codec->mutex);
  2261. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  2262. if (change) {
  2263. for (wi = 0; wi < wlist->num_widgets; wi++) {
  2264. widget = wlist->widgets[wi];
  2265. widget->value = val;
  2266. update.kcontrol = kcontrol;
  2267. update.widget = widget;
  2268. update.reg = e->reg;
  2269. update.mask = mask;
  2270. update.val = val;
  2271. widget->dapm->update = &update;
  2272. snd_soc_dapm_mux_update_power(widget, kcontrol, mux, e);
  2273. widget->dapm->update = NULL;
  2274. }
  2275. }
  2276. mutex_unlock(&codec->mutex);
  2277. return change;
  2278. }
  2279. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  2280. /**
  2281. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  2282. *
  2283. * @kcontrol: mixer control
  2284. * @uinfo: control element information
  2285. *
  2286. * Callback to provide information about a pin switch control.
  2287. */
  2288. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  2289. struct snd_ctl_elem_info *uinfo)
  2290. {
  2291. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2292. uinfo->count = 1;
  2293. uinfo->value.integer.min = 0;
  2294. uinfo->value.integer.max = 1;
  2295. return 0;
  2296. }
  2297. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  2298. /**
  2299. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  2300. *
  2301. * @kcontrol: mixer control
  2302. * @ucontrol: Value
  2303. */
  2304. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  2305. struct snd_ctl_elem_value *ucontrol)
  2306. {
  2307. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2308. const char *pin = (const char *)kcontrol->private_value;
  2309. mutex_lock(&card->mutex);
  2310. ucontrol->value.integer.value[0] =
  2311. snd_soc_dapm_get_pin_status(&card->dapm, pin);
  2312. mutex_unlock(&card->mutex);
  2313. return 0;
  2314. }
  2315. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  2316. /**
  2317. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  2318. *
  2319. * @kcontrol: mixer control
  2320. * @ucontrol: Value
  2321. */
  2322. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  2323. struct snd_ctl_elem_value *ucontrol)
  2324. {
  2325. struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
  2326. const char *pin = (const char *)kcontrol->private_value;
  2327. mutex_lock(&card->mutex);
  2328. if (ucontrol->value.integer.value[0])
  2329. snd_soc_dapm_enable_pin(&card->dapm, pin);
  2330. else
  2331. snd_soc_dapm_disable_pin(&card->dapm, pin);
  2332. snd_soc_dapm_sync(&card->dapm);
  2333. mutex_unlock(&card->mutex);
  2334. return 0;
  2335. }
  2336. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  2337. static struct snd_soc_dapm_widget *
  2338. snd_soc_dapm_new_control(struct snd_soc_dapm_context *dapm,
  2339. const struct snd_soc_dapm_widget *widget)
  2340. {
  2341. struct snd_soc_dapm_widget *w;
  2342. size_t name_len;
  2343. int ret;
  2344. if ((w = dapm_cnew_widget(widget)) == NULL)
  2345. return NULL;
  2346. switch (w->id) {
  2347. case snd_soc_dapm_regulator_supply:
  2348. w->priv = devm_regulator_get(dapm->dev, w->name);
  2349. if (IS_ERR(w->priv)) {
  2350. ret = PTR_ERR(w->priv);
  2351. dev_err(dapm->dev, "Failed to request %s: %d\n",
  2352. w->name, ret);
  2353. return NULL;
  2354. }
  2355. break;
  2356. default:
  2357. break;
  2358. }
  2359. name_len = strlen(widget->name) + 1;
  2360. if (dapm->codec && dapm->codec->name_prefix)
  2361. name_len += 1 + strlen(dapm->codec->name_prefix);
  2362. w->name = kmalloc(name_len, GFP_KERNEL);
  2363. if (w->name == NULL) {
  2364. kfree(w);
  2365. return NULL;
  2366. }
  2367. if (dapm->codec && dapm->codec->name_prefix)
  2368. snprintf((char *)w->name, name_len, "%s %s",
  2369. dapm->codec->name_prefix, widget->name);
  2370. else
  2371. snprintf((char *)w->name, name_len, "%s", widget->name);
  2372. switch (w->id) {
  2373. case snd_soc_dapm_switch:
  2374. case snd_soc_dapm_mixer:
  2375. case snd_soc_dapm_mixer_named_ctl:
  2376. w->power_check = dapm_generic_check_power;
  2377. break;
  2378. case snd_soc_dapm_mux:
  2379. case snd_soc_dapm_virt_mux:
  2380. case snd_soc_dapm_value_mux:
  2381. w->power_check = dapm_generic_check_power;
  2382. break;
  2383. case snd_soc_dapm_adc:
  2384. case snd_soc_dapm_aif_out:
  2385. w->power_check = dapm_adc_check_power;
  2386. break;
  2387. case snd_soc_dapm_dac:
  2388. case snd_soc_dapm_aif_in:
  2389. w->power_check = dapm_dac_check_power;
  2390. break;
  2391. case snd_soc_dapm_pga:
  2392. case snd_soc_dapm_out_drv:
  2393. case snd_soc_dapm_input:
  2394. case snd_soc_dapm_output:
  2395. case snd_soc_dapm_micbias:
  2396. case snd_soc_dapm_spk:
  2397. case snd_soc_dapm_hp:
  2398. case snd_soc_dapm_mic:
  2399. case snd_soc_dapm_line:
  2400. w->power_check = dapm_generic_check_power;
  2401. break;
  2402. case snd_soc_dapm_supply:
  2403. case snd_soc_dapm_regulator_supply:
  2404. w->power_check = dapm_supply_check_power;
  2405. break;
  2406. case snd_soc_dapm_dai:
  2407. w->power_check = dapm_dai_check_power;
  2408. break;
  2409. default:
  2410. w->power_check = dapm_always_on_check_power;
  2411. break;
  2412. }
  2413. dapm->n_widgets++;
  2414. w->dapm = dapm;
  2415. w->codec = dapm->codec;
  2416. w->platform = dapm->platform;
  2417. INIT_LIST_HEAD(&w->sources);
  2418. INIT_LIST_HEAD(&w->sinks);
  2419. INIT_LIST_HEAD(&w->list);
  2420. INIT_LIST_HEAD(&w->dirty);
  2421. list_add(&w->list, &dapm->card->widgets);
  2422. /* machine layer set ups unconnected pins and insertions */
  2423. w->connected = 1;
  2424. return w;
  2425. }
  2426. /**
  2427. * snd_soc_dapm_new_controls - create new dapm controls
  2428. * @dapm: DAPM context
  2429. * @widget: widget array
  2430. * @num: number of widgets
  2431. *
  2432. * Creates new DAPM controls based upon the templates.
  2433. *
  2434. * Returns 0 for success else error.
  2435. */
  2436. int snd_soc_dapm_new_controls(struct snd_soc_dapm_context *dapm,
  2437. const struct snd_soc_dapm_widget *widget,
  2438. int num)
  2439. {
  2440. struct snd_soc_dapm_widget *w;
  2441. int i;
  2442. for (i = 0; i < num; i++) {
  2443. w = snd_soc_dapm_new_control(dapm, widget);
  2444. if (!w) {
  2445. dev_err(dapm->dev,
  2446. "ASoC: Failed to create DAPM control %s\n",
  2447. widget->name);
  2448. return -ENOMEM;
  2449. }
  2450. widget++;
  2451. }
  2452. return 0;
  2453. }
  2454. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  2455. int snd_soc_dapm_new_dai_widgets(struct snd_soc_dapm_context *dapm,
  2456. struct snd_soc_dai *dai)
  2457. {
  2458. struct snd_soc_dapm_widget template;
  2459. struct snd_soc_dapm_widget *w;
  2460. WARN_ON(dapm->dev != dai->dev);
  2461. memset(&template, 0, sizeof(template));
  2462. template.reg = SND_SOC_NOPM;
  2463. if (dai->driver->playback.stream_name) {
  2464. template.id = snd_soc_dapm_dai;
  2465. template.name = dai->driver->playback.stream_name;
  2466. template.sname = dai->driver->playback.stream_name;
  2467. dev_dbg(dai->dev, "adding %s widget\n",
  2468. template.name);
  2469. w = snd_soc_dapm_new_control(dapm, &template);
  2470. if (!w) {
  2471. dev_err(dapm->dev, "Failed to create %s widget\n",
  2472. dai->driver->playback.stream_name);
  2473. }
  2474. w->priv = dai;
  2475. dai->playback_widget = w;
  2476. }
  2477. if (dai->driver->capture.stream_name) {
  2478. template.id = snd_soc_dapm_dai;
  2479. template.name = dai->driver->capture.stream_name;
  2480. template.sname = dai->driver->capture.stream_name;
  2481. dev_dbg(dai->dev, "adding %s widget\n",
  2482. template.name);
  2483. w = snd_soc_dapm_new_control(dapm, &template);
  2484. if (!w) {
  2485. dev_err(dapm->dev, "Failed to create %s widget\n",
  2486. dai->driver->capture.stream_name);
  2487. }
  2488. w->priv = dai;
  2489. dai->capture_widget = w;
  2490. }
  2491. return 0;
  2492. }
  2493. int snd_soc_dapm_link_dai_widgets(struct snd_soc_card *card)
  2494. {
  2495. struct snd_soc_dapm_widget *dai_w, *w;
  2496. struct snd_soc_dai *dai;
  2497. struct snd_soc_dapm_route r;
  2498. memset(&r, 0, sizeof(r));
  2499. /* For each DAI widget... */
  2500. list_for_each_entry(dai_w, &card->widgets, list) {
  2501. if (dai_w->id != snd_soc_dapm_dai)
  2502. continue;
  2503. dai = dai_w->priv;
  2504. /* ...find all widgets with the same stream and link them */
  2505. list_for_each_entry(w, &card->widgets, list) {
  2506. if (w->dapm != dai_w->dapm)
  2507. continue;
  2508. if (w->id == snd_soc_dapm_dai)
  2509. continue;
  2510. if (!w->sname)
  2511. continue;
  2512. if (dai->driver->playback.stream_name &&
  2513. strstr(w->sname,
  2514. dai->driver->playback.stream_name)) {
  2515. r.source = dai->playback_widget->name;
  2516. r.sink = w->name;
  2517. dev_dbg(dai->dev, "%s -> %s\n",
  2518. r.source, r.sink);
  2519. snd_soc_dapm_add_route(w->dapm, &r);
  2520. }
  2521. if (dai->driver->capture.stream_name &&
  2522. strstr(w->sname,
  2523. dai->driver->capture.stream_name)) {
  2524. r.source = w->name;
  2525. r.sink = dai->capture_widget->name;
  2526. dev_dbg(dai->dev, "%s -> %s\n",
  2527. r.source, r.sink);
  2528. snd_soc_dapm_add_route(w->dapm, &r);
  2529. }
  2530. }
  2531. }
  2532. return 0;
  2533. }
  2534. static void soc_dapm_stream_event(struct snd_soc_dapm_context *dapm,
  2535. int stream, struct snd_soc_dai *dai,
  2536. int event)
  2537. {
  2538. struct snd_soc_dapm_widget *w;
  2539. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  2540. w = dai->playback_widget;
  2541. else
  2542. w = dai->capture_widget;
  2543. if (!w)
  2544. return;
  2545. dapm_mark_dirty(w, "stream event");
  2546. switch (event) {
  2547. case SND_SOC_DAPM_STREAM_START:
  2548. w->active = 1;
  2549. break;
  2550. case SND_SOC_DAPM_STREAM_STOP:
  2551. w->active = 0;
  2552. break;
  2553. case SND_SOC_DAPM_STREAM_SUSPEND:
  2554. case SND_SOC_DAPM_STREAM_RESUME:
  2555. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  2556. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  2557. break;
  2558. }
  2559. dapm_power_widgets(dapm, event);
  2560. }
  2561. /**
  2562. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  2563. * @rtd: PCM runtime data
  2564. * @stream: stream name
  2565. * @event: stream event
  2566. *
  2567. * Sends a stream event to the dapm core. The core then makes any
  2568. * necessary widget power changes.
  2569. *
  2570. * Returns 0 for success else error.
  2571. */
  2572. int snd_soc_dapm_stream_event(struct snd_soc_pcm_runtime *rtd, int stream,
  2573. struct snd_soc_dai *dai, int event)
  2574. {
  2575. struct snd_soc_codec *codec = rtd->codec;
  2576. mutex_lock(&codec->mutex);
  2577. soc_dapm_stream_event(&codec->dapm, stream, dai, event);
  2578. mutex_unlock(&codec->mutex);
  2579. return 0;
  2580. }
  2581. /**
  2582. * snd_soc_dapm_enable_pin - enable pin.
  2583. * @dapm: DAPM context
  2584. * @pin: pin name
  2585. *
  2586. * Enables input/output pin and its parents or children widgets iff there is
  2587. * a valid audio route and active audio stream.
  2588. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2589. * do any widget power switching.
  2590. */
  2591. int snd_soc_dapm_enable_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2592. {
  2593. return snd_soc_dapm_set_pin(dapm, pin, 1);
  2594. }
  2595. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  2596. /**
  2597. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  2598. * @dapm: DAPM context
  2599. * @pin: pin name
  2600. *
  2601. * Enables input/output pin regardless of any other state. This is
  2602. * intended for use with microphone bias supplies used in microphone
  2603. * jack detection.
  2604. *
  2605. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2606. * do any widget power switching.
  2607. */
  2608. int snd_soc_dapm_force_enable_pin(struct snd_soc_dapm_context *dapm,
  2609. const char *pin)
  2610. {
  2611. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2612. if (!w) {
  2613. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2614. return -EINVAL;
  2615. }
  2616. dev_dbg(w->dapm->dev, "dapm: force enable pin %s\n", pin);
  2617. w->connected = 1;
  2618. w->force = 1;
  2619. dapm_mark_dirty(w, "force enable");
  2620. return 0;
  2621. }
  2622. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  2623. /**
  2624. * snd_soc_dapm_disable_pin - disable pin.
  2625. * @dapm: DAPM context
  2626. * @pin: pin name
  2627. *
  2628. * Disables input/output pin and its parents or children widgets.
  2629. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2630. * do any widget power switching.
  2631. */
  2632. int snd_soc_dapm_disable_pin(struct snd_soc_dapm_context *dapm,
  2633. const char *pin)
  2634. {
  2635. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2636. }
  2637. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  2638. /**
  2639. * snd_soc_dapm_nc_pin - permanently disable pin.
  2640. * @dapm: DAPM context
  2641. * @pin: pin name
  2642. *
  2643. * Marks the specified pin as being not connected, disabling it along
  2644. * any parent or child widgets. At present this is identical to
  2645. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  2646. * additional things such as disabling controls which only affect
  2647. * paths through the pin.
  2648. *
  2649. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  2650. * do any widget power switching.
  2651. */
  2652. int snd_soc_dapm_nc_pin(struct snd_soc_dapm_context *dapm, const char *pin)
  2653. {
  2654. return snd_soc_dapm_set_pin(dapm, pin, 0);
  2655. }
  2656. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  2657. /**
  2658. * snd_soc_dapm_get_pin_status - get audio pin status
  2659. * @dapm: DAPM context
  2660. * @pin: audio signal pin endpoint (or start point)
  2661. *
  2662. * Get audio pin status - connected or disconnected.
  2663. *
  2664. * Returns 1 for connected otherwise 0.
  2665. */
  2666. int snd_soc_dapm_get_pin_status(struct snd_soc_dapm_context *dapm,
  2667. const char *pin)
  2668. {
  2669. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, true);
  2670. if (w)
  2671. return w->connected;
  2672. return 0;
  2673. }
  2674. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  2675. /**
  2676. * snd_soc_dapm_ignore_suspend - ignore suspend status for DAPM endpoint
  2677. * @dapm: DAPM context
  2678. * @pin: audio signal pin endpoint (or start point)
  2679. *
  2680. * Mark the given endpoint or pin as ignoring suspend. When the
  2681. * system is disabled a path between two endpoints flagged as ignoring
  2682. * suspend will not be disabled. The path must already be enabled via
  2683. * normal means at suspend time, it will not be turned on if it was not
  2684. * already enabled.
  2685. */
  2686. int snd_soc_dapm_ignore_suspend(struct snd_soc_dapm_context *dapm,
  2687. const char *pin)
  2688. {
  2689. struct snd_soc_dapm_widget *w = dapm_find_widget(dapm, pin, false);
  2690. if (!w) {
  2691. dev_err(dapm->dev, "dapm: unknown pin %s\n", pin);
  2692. return -EINVAL;
  2693. }
  2694. w->ignore_suspend = 1;
  2695. return 0;
  2696. }
  2697. EXPORT_SYMBOL_GPL(snd_soc_dapm_ignore_suspend);
  2698. static bool snd_soc_dapm_widget_in_card_paths(struct snd_soc_card *card,
  2699. struct snd_soc_dapm_widget *w)
  2700. {
  2701. struct snd_soc_dapm_path *p;
  2702. list_for_each_entry(p, &card->paths, list) {
  2703. if ((p->source == w) || (p->sink == w)) {
  2704. dev_dbg(card->dev,
  2705. "... Path %s(id:%d dapm:%p) - %s(id:%d dapm:%p)\n",
  2706. p->source->name, p->source->id, p->source->dapm,
  2707. p->sink->name, p->sink->id, p->sink->dapm);
  2708. /* Connected to something other than the codec */
  2709. if (p->source->dapm != p->sink->dapm)
  2710. return true;
  2711. /*
  2712. * Loopback connection from codec external pin to
  2713. * codec external pin
  2714. */
  2715. if (p->sink->id == snd_soc_dapm_input) {
  2716. switch (p->source->id) {
  2717. case snd_soc_dapm_output:
  2718. case snd_soc_dapm_micbias:
  2719. return true;
  2720. default:
  2721. break;
  2722. }
  2723. }
  2724. }
  2725. }
  2726. return false;
  2727. }
  2728. /**
  2729. * snd_soc_dapm_auto_nc_codec_pins - call snd_soc_dapm_nc_pin for unused pins
  2730. * @codec: The codec whose pins should be processed
  2731. *
  2732. * Automatically call snd_soc_dapm_nc_pin() for any external pins in the codec
  2733. * which are unused. Pins are used if they are connected externally to the
  2734. * codec, whether that be to some other device, or a loop-back connection to
  2735. * the codec itself.
  2736. */
  2737. void snd_soc_dapm_auto_nc_codec_pins(struct snd_soc_codec *codec)
  2738. {
  2739. struct snd_soc_card *card = codec->card;
  2740. struct snd_soc_dapm_context *dapm = &codec->dapm;
  2741. struct snd_soc_dapm_widget *w;
  2742. dev_dbg(codec->dev, "Auto NC: DAPMs: card:%p codec:%p\n",
  2743. &card->dapm, &codec->dapm);
  2744. list_for_each_entry(w, &card->widgets, list) {
  2745. if (w->dapm != dapm)
  2746. continue;
  2747. switch (w->id) {
  2748. case snd_soc_dapm_input:
  2749. case snd_soc_dapm_output:
  2750. case snd_soc_dapm_micbias:
  2751. dev_dbg(codec->dev, "Auto NC: Checking widget %s\n",
  2752. w->name);
  2753. if (!snd_soc_dapm_widget_in_card_paths(card, w)) {
  2754. dev_dbg(codec->dev,
  2755. "... Not in map; disabling\n");
  2756. snd_soc_dapm_nc_pin(dapm, w->name);
  2757. }
  2758. break;
  2759. default:
  2760. break;
  2761. }
  2762. }
  2763. }
  2764. /**
  2765. * snd_soc_dapm_free - free dapm resources
  2766. * @dapm: DAPM context
  2767. *
  2768. * Free all dapm widgets and resources.
  2769. */
  2770. void snd_soc_dapm_free(struct snd_soc_dapm_context *dapm)
  2771. {
  2772. snd_soc_dapm_sys_remove(dapm->dev);
  2773. dapm_debugfs_cleanup(dapm);
  2774. dapm_free_widgets(dapm);
  2775. list_del(&dapm->list);
  2776. }
  2777. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  2778. static void soc_dapm_shutdown_codec(struct snd_soc_dapm_context *dapm)
  2779. {
  2780. struct snd_soc_dapm_widget *w;
  2781. LIST_HEAD(down_list);
  2782. int powerdown = 0;
  2783. list_for_each_entry(w, &dapm->card->widgets, list) {
  2784. if (w->dapm != dapm)
  2785. continue;
  2786. if (w->power) {
  2787. dapm_seq_insert(w, &down_list, false);
  2788. w->power = 0;
  2789. powerdown = 1;
  2790. }
  2791. }
  2792. /* If there were no widgets to power down we're already in
  2793. * standby.
  2794. */
  2795. if (powerdown) {
  2796. if (dapm->bias_level == SND_SOC_BIAS_ON)
  2797. snd_soc_dapm_set_bias_level(dapm,
  2798. SND_SOC_BIAS_PREPARE);
  2799. dapm_seq_run(dapm, &down_list, 0, false);
  2800. if (dapm->bias_level == SND_SOC_BIAS_PREPARE)
  2801. snd_soc_dapm_set_bias_level(dapm,
  2802. SND_SOC_BIAS_STANDBY);
  2803. }
  2804. }
  2805. /*
  2806. * snd_soc_dapm_shutdown - callback for system shutdown
  2807. */
  2808. void snd_soc_dapm_shutdown(struct snd_soc_card *card)
  2809. {
  2810. struct snd_soc_codec *codec;
  2811. list_for_each_entry(codec, &card->codec_dev_list, list) {
  2812. soc_dapm_shutdown_codec(&codec->dapm);
  2813. if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY)
  2814. snd_soc_dapm_set_bias_level(&codec->dapm,
  2815. SND_SOC_BIAS_OFF);
  2816. }
  2817. }
  2818. /* Module information */
  2819. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  2820. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  2821. MODULE_LICENSE("GPL");