soc-dapm.c 94 KB

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