soc-dapm.c 73 KB

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