soc-dapm.c 89 KB

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