soc-dapm.c 93 KB

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