soc-dapm.c 94 KB

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