soc-dapm.c 90 KB

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