soc-dapm.c 96 KB

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