soc-dapm.c 88 KB

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