soc-dapm.c 76 KB

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