soc-dapm.c 74 KB

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