soc-dapm.c 70 KB

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