soc-dapm.c 96 KB

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