soc-dapm.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  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/delay.h>
  35. #include <linux/pm.h>
  36. #include <linux/bitops.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/jiffies.h>
  39. #include <linux/debugfs.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc-dapm.h>
  44. #include <sound/initval.h>
  45. /* dapm power sequences - make this per codec in the future */
  46. static int dapm_up_seq[] = {
  47. [snd_soc_dapm_pre] = 0,
  48. [snd_soc_dapm_supply] = 1,
  49. [snd_soc_dapm_micbias] = 2,
  50. [snd_soc_dapm_aif_in] = 3,
  51. [snd_soc_dapm_aif_out] = 3,
  52. [snd_soc_dapm_mic] = 4,
  53. [snd_soc_dapm_mux] = 5,
  54. [snd_soc_dapm_value_mux] = 5,
  55. [snd_soc_dapm_dac] = 6,
  56. [snd_soc_dapm_mixer] = 7,
  57. [snd_soc_dapm_mixer_named_ctl] = 7,
  58. [snd_soc_dapm_pga] = 8,
  59. [snd_soc_dapm_adc] = 9,
  60. [snd_soc_dapm_hp] = 10,
  61. [snd_soc_dapm_spk] = 10,
  62. [snd_soc_dapm_post] = 11,
  63. };
  64. static int dapm_down_seq[] = {
  65. [snd_soc_dapm_pre] = 0,
  66. [snd_soc_dapm_adc] = 1,
  67. [snd_soc_dapm_hp] = 2,
  68. [snd_soc_dapm_spk] = 2,
  69. [snd_soc_dapm_pga] = 4,
  70. [snd_soc_dapm_mixer_named_ctl] = 5,
  71. [snd_soc_dapm_mixer] = 5,
  72. [snd_soc_dapm_dac] = 6,
  73. [snd_soc_dapm_mic] = 7,
  74. [snd_soc_dapm_micbias] = 8,
  75. [snd_soc_dapm_mux] = 9,
  76. [snd_soc_dapm_value_mux] = 9,
  77. [snd_soc_dapm_aif_in] = 10,
  78. [snd_soc_dapm_aif_out] = 10,
  79. [snd_soc_dapm_supply] = 11,
  80. [snd_soc_dapm_post] = 12,
  81. };
  82. static void pop_wait(u32 pop_time)
  83. {
  84. if (pop_time)
  85. schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  86. }
  87. static void pop_dbg(u32 pop_time, const char *fmt, ...)
  88. {
  89. va_list args;
  90. va_start(args, fmt);
  91. if (pop_time) {
  92. vprintk(fmt, args);
  93. }
  94. va_end(args);
  95. }
  96. /* create a new dapm widget */
  97. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  98. const struct snd_soc_dapm_widget *_widget)
  99. {
  100. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  101. }
  102. /**
  103. * snd_soc_dapm_set_bias_level - set the bias level for the system
  104. * @socdev: audio device
  105. * @level: level to configure
  106. *
  107. * Configure the bias (power) levels for the SoC audio device.
  108. *
  109. * Returns 0 for success else error.
  110. */
  111. static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
  112. enum snd_soc_bias_level level)
  113. {
  114. struct snd_soc_card *card = socdev->card;
  115. struct snd_soc_codec *codec = socdev->card->codec;
  116. int ret = 0;
  117. switch (level) {
  118. case SND_SOC_BIAS_ON:
  119. dev_dbg(socdev->dev, "Setting full bias\n");
  120. break;
  121. case SND_SOC_BIAS_PREPARE:
  122. dev_dbg(socdev->dev, "Setting bias prepare\n");
  123. break;
  124. case SND_SOC_BIAS_STANDBY:
  125. dev_dbg(socdev->dev, "Setting standby bias\n");
  126. break;
  127. case SND_SOC_BIAS_OFF:
  128. dev_dbg(socdev->dev, "Setting bias off\n");
  129. break;
  130. default:
  131. dev_err(socdev->dev, "Setting invalid bias %d\n", level);
  132. return -EINVAL;
  133. }
  134. if (card->set_bias_level)
  135. ret = card->set_bias_level(card, level);
  136. if (ret == 0) {
  137. if (codec->set_bias_level)
  138. ret = codec->set_bias_level(codec, level);
  139. else
  140. codec->bias_level = level;
  141. }
  142. return ret;
  143. }
  144. /* set up initial codec paths */
  145. static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
  146. struct snd_soc_dapm_path *p, int i)
  147. {
  148. switch (w->id) {
  149. case snd_soc_dapm_switch:
  150. case snd_soc_dapm_mixer:
  151. case snd_soc_dapm_mixer_named_ctl: {
  152. int val;
  153. struct soc_mixer_control *mc = (struct soc_mixer_control *)
  154. w->kcontrols[i].private_value;
  155. unsigned int reg = mc->reg;
  156. unsigned int shift = mc->shift;
  157. int max = mc->max;
  158. unsigned int mask = (1 << fls(max)) - 1;
  159. unsigned int invert = mc->invert;
  160. val = snd_soc_read(w->codec, reg);
  161. val = (val >> shift) & mask;
  162. if ((invert && !val) || (!invert && val))
  163. p->connect = 1;
  164. else
  165. p->connect = 0;
  166. }
  167. break;
  168. case snd_soc_dapm_mux: {
  169. struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
  170. int val, item, bitmask;
  171. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  172. ;
  173. val = snd_soc_read(w->codec, e->reg);
  174. item = (val >> e->shift_l) & (bitmask - 1);
  175. p->connect = 0;
  176. for (i = 0; i < e->max; i++) {
  177. if (!(strcmp(p->name, e->texts[i])) && item == i)
  178. p->connect = 1;
  179. }
  180. }
  181. break;
  182. case snd_soc_dapm_value_mux: {
  183. struct soc_enum *e = (struct soc_enum *)
  184. w->kcontrols[i].private_value;
  185. int val, item;
  186. val = snd_soc_read(w->codec, e->reg);
  187. val = (val >> e->shift_l) & e->mask;
  188. for (item = 0; item < e->max; item++) {
  189. if (val == e->values[item])
  190. break;
  191. }
  192. p->connect = 0;
  193. for (i = 0; i < e->max; i++) {
  194. if (!(strcmp(p->name, e->texts[i])) && item == i)
  195. p->connect = 1;
  196. }
  197. }
  198. break;
  199. /* does not effect routing - always connected */
  200. case snd_soc_dapm_pga:
  201. case snd_soc_dapm_output:
  202. case snd_soc_dapm_adc:
  203. case snd_soc_dapm_input:
  204. case snd_soc_dapm_dac:
  205. case snd_soc_dapm_micbias:
  206. case snd_soc_dapm_vmid:
  207. case snd_soc_dapm_supply:
  208. case snd_soc_dapm_aif_in:
  209. case snd_soc_dapm_aif_out:
  210. p->connect = 1;
  211. break;
  212. /* does effect routing - dynamically connected */
  213. case snd_soc_dapm_hp:
  214. case snd_soc_dapm_mic:
  215. case snd_soc_dapm_spk:
  216. case snd_soc_dapm_line:
  217. case snd_soc_dapm_pre:
  218. case snd_soc_dapm_post:
  219. p->connect = 0;
  220. break;
  221. }
  222. }
  223. /* connect mux widget to its interconnecting audio paths */
  224. static int dapm_connect_mux(struct snd_soc_codec *codec,
  225. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  226. struct snd_soc_dapm_path *path, const char *control_name,
  227. const struct snd_kcontrol_new *kcontrol)
  228. {
  229. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  230. int i;
  231. for (i = 0; i < e->max; i++) {
  232. if (!(strcmp(control_name, e->texts[i]))) {
  233. list_add(&path->list, &codec->dapm_paths);
  234. list_add(&path->list_sink, &dest->sources);
  235. list_add(&path->list_source, &src->sinks);
  236. path->name = (char*)e->texts[i];
  237. dapm_set_path_status(dest, path, 0);
  238. return 0;
  239. }
  240. }
  241. return -ENODEV;
  242. }
  243. /* connect mixer widget to its interconnecting audio paths */
  244. static int dapm_connect_mixer(struct snd_soc_codec *codec,
  245. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  246. struct snd_soc_dapm_path *path, const char *control_name)
  247. {
  248. int i;
  249. /* search for mixer kcontrol */
  250. for (i = 0; i < dest->num_kcontrols; i++) {
  251. if (!strcmp(control_name, dest->kcontrols[i].name)) {
  252. list_add(&path->list, &codec->dapm_paths);
  253. list_add(&path->list_sink, &dest->sources);
  254. list_add(&path->list_source, &src->sinks);
  255. path->name = dest->kcontrols[i].name;
  256. dapm_set_path_status(dest, path, i);
  257. return 0;
  258. }
  259. }
  260. return -ENODEV;
  261. }
  262. /* update dapm codec register bits */
  263. static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
  264. {
  265. int change, power;
  266. unsigned int old, new;
  267. struct snd_soc_codec *codec = widget->codec;
  268. /* check for valid widgets */
  269. if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
  270. widget->id == snd_soc_dapm_output ||
  271. widget->id == snd_soc_dapm_hp ||
  272. widget->id == snd_soc_dapm_mic ||
  273. widget->id == snd_soc_dapm_line ||
  274. widget->id == snd_soc_dapm_spk)
  275. return 0;
  276. power = widget->power;
  277. if (widget->invert)
  278. power = (power ? 0:1);
  279. old = snd_soc_read(codec, widget->reg);
  280. new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
  281. change = old != new;
  282. if (change) {
  283. pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
  284. widget->name, widget->power ? "on" : "off",
  285. codec->pop_time);
  286. pop_wait(codec->pop_time);
  287. snd_soc_write(codec, widget->reg, new);
  288. }
  289. pr_debug("reg %x old %x new %x change %d\n", widget->reg,
  290. old, new, change);
  291. return change;
  292. }
  293. /* create new dapm mixer control */
  294. static int dapm_new_mixer(struct snd_soc_codec *codec,
  295. struct snd_soc_dapm_widget *w)
  296. {
  297. int i, ret = 0;
  298. size_t name_len;
  299. struct snd_soc_dapm_path *path;
  300. /* add kcontrol */
  301. for (i = 0; i < w->num_kcontrols; i++) {
  302. /* match name */
  303. list_for_each_entry(path, &w->sources, list_sink) {
  304. /* mixer/mux paths name must match control name */
  305. if (path->name != (char*)w->kcontrols[i].name)
  306. continue;
  307. /* add dapm control with long name.
  308. * for dapm_mixer this is the concatenation of the
  309. * mixer and kcontrol name.
  310. * for dapm_mixer_named_ctl this is simply the
  311. * kcontrol name.
  312. */
  313. name_len = strlen(w->kcontrols[i].name) + 1;
  314. if (w->id != snd_soc_dapm_mixer_named_ctl)
  315. name_len += 1 + strlen(w->name);
  316. path->long_name = kmalloc(name_len, GFP_KERNEL);
  317. if (path->long_name == NULL)
  318. return -ENOMEM;
  319. switch (w->id) {
  320. default:
  321. snprintf(path->long_name, name_len, "%s %s",
  322. w->name, w->kcontrols[i].name);
  323. break;
  324. case snd_soc_dapm_mixer_named_ctl:
  325. snprintf(path->long_name, name_len, "%s",
  326. w->kcontrols[i].name);
  327. break;
  328. }
  329. path->long_name[name_len - 1] = '\0';
  330. path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
  331. path->long_name);
  332. ret = snd_ctl_add(codec->card, path->kcontrol);
  333. if (ret < 0) {
  334. printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
  335. path->long_name,
  336. ret);
  337. kfree(path->long_name);
  338. path->long_name = NULL;
  339. return ret;
  340. }
  341. }
  342. }
  343. return ret;
  344. }
  345. /* create new dapm mux control */
  346. static int dapm_new_mux(struct snd_soc_codec *codec,
  347. struct snd_soc_dapm_widget *w)
  348. {
  349. struct snd_soc_dapm_path *path = NULL;
  350. struct snd_kcontrol *kcontrol;
  351. int ret = 0;
  352. if (!w->num_kcontrols) {
  353. printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
  354. return -EINVAL;
  355. }
  356. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  357. ret = snd_ctl_add(codec->card, kcontrol);
  358. if (ret < 0)
  359. goto err;
  360. list_for_each_entry(path, &w->sources, list_sink)
  361. path->kcontrol = kcontrol;
  362. return ret;
  363. err:
  364. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  365. return ret;
  366. }
  367. /* create new dapm volume control */
  368. static int dapm_new_pga(struct snd_soc_codec *codec,
  369. struct snd_soc_dapm_widget *w)
  370. {
  371. if (w->num_kcontrols)
  372. pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
  373. return 0;
  374. }
  375. /* reset 'walked' bit for each dapm path */
  376. static inline void dapm_clear_walk(struct snd_soc_codec *codec)
  377. {
  378. struct snd_soc_dapm_path *p;
  379. list_for_each_entry(p, &codec->dapm_paths, list)
  380. p->walked = 0;
  381. }
  382. /* We implement power down on suspend by checking the power state of
  383. * the ALSA card - when we are suspending the ALSA state for the card
  384. * is set to D3.
  385. */
  386. static int snd_soc_dapm_suspend_check(struct snd_soc_dapm_widget *widget)
  387. {
  388. struct snd_soc_codec *codec = widget->codec;
  389. switch (snd_power_get_state(codec->card)) {
  390. case SNDRV_CTL_POWER_D3hot:
  391. case SNDRV_CTL_POWER_D3cold:
  392. return 0;
  393. default:
  394. return 1;
  395. }
  396. }
  397. /*
  398. * Recursively check for a completed path to an active or physically connected
  399. * output widget. Returns number of complete paths.
  400. */
  401. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
  402. {
  403. struct snd_soc_dapm_path *path;
  404. int con = 0;
  405. if (widget->id == snd_soc_dapm_supply)
  406. return 0;
  407. switch (widget->id) {
  408. case snd_soc_dapm_adc:
  409. case snd_soc_dapm_aif_out:
  410. if (widget->active)
  411. return snd_soc_dapm_suspend_check(widget);
  412. default:
  413. break;
  414. }
  415. if (widget->connected) {
  416. /* connected pin ? */
  417. if (widget->id == snd_soc_dapm_output && !widget->ext)
  418. return snd_soc_dapm_suspend_check(widget);
  419. /* connected jack or spk ? */
  420. if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
  421. (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
  422. return snd_soc_dapm_suspend_check(widget);
  423. }
  424. list_for_each_entry(path, &widget->sinks, list_source) {
  425. if (path->walked)
  426. continue;
  427. if (path->sink && path->connect) {
  428. path->walked = 1;
  429. con += is_connected_output_ep(path->sink);
  430. }
  431. }
  432. return con;
  433. }
  434. /*
  435. * Recursively check for a completed path to an active or physically connected
  436. * input widget. Returns number of complete paths.
  437. */
  438. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
  439. {
  440. struct snd_soc_dapm_path *path;
  441. int con = 0;
  442. if (widget->id == snd_soc_dapm_supply)
  443. return 0;
  444. /* active stream ? */
  445. switch (widget->id) {
  446. case snd_soc_dapm_dac:
  447. case snd_soc_dapm_aif_in:
  448. if (widget->active)
  449. return snd_soc_dapm_suspend_check(widget);
  450. default:
  451. break;
  452. }
  453. if (widget->connected) {
  454. /* connected pin ? */
  455. if (widget->id == snd_soc_dapm_input && !widget->ext)
  456. return snd_soc_dapm_suspend_check(widget);
  457. /* connected VMID/Bias for lower pops */
  458. if (widget->id == snd_soc_dapm_vmid)
  459. return snd_soc_dapm_suspend_check(widget);
  460. /* connected jack ? */
  461. if (widget->id == snd_soc_dapm_mic ||
  462. (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
  463. return snd_soc_dapm_suspend_check(widget);
  464. }
  465. list_for_each_entry(path, &widget->sources, list_sink) {
  466. if (path->walked)
  467. continue;
  468. if (path->source && path->connect) {
  469. path->walked = 1;
  470. con += is_connected_input_ep(path->source);
  471. }
  472. }
  473. return con;
  474. }
  475. /*
  476. * Handler for generic register modifier widget.
  477. */
  478. int dapm_reg_event(struct snd_soc_dapm_widget *w,
  479. struct snd_kcontrol *kcontrol, int event)
  480. {
  481. unsigned int val;
  482. if (SND_SOC_DAPM_EVENT_ON(event))
  483. val = w->on_val;
  484. else
  485. val = w->off_val;
  486. snd_soc_update_bits(w->codec, -(w->reg + 1),
  487. w->mask << w->shift, val << w->shift);
  488. return 0;
  489. }
  490. EXPORT_SYMBOL_GPL(dapm_reg_event);
  491. /* Standard power change method, used to apply power changes to most
  492. * widgets.
  493. */
  494. static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
  495. {
  496. int ret;
  497. /* call any power change event handlers */
  498. if (w->event)
  499. pr_debug("power %s event for %s flags %x\n",
  500. w->power ? "on" : "off",
  501. w->name, w->event_flags);
  502. /* power up pre event */
  503. if (w->power && w->event &&
  504. (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
  505. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
  506. if (ret < 0)
  507. return ret;
  508. }
  509. /* power down pre event */
  510. if (!w->power && w->event &&
  511. (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
  512. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
  513. if (ret < 0)
  514. return ret;
  515. }
  516. dapm_update_bits(w);
  517. /* power up post event */
  518. if (w->power && w->event &&
  519. (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
  520. ret = w->event(w,
  521. NULL, SND_SOC_DAPM_POST_PMU);
  522. if (ret < 0)
  523. return ret;
  524. }
  525. /* power down post event */
  526. if (!w->power && w->event &&
  527. (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
  528. ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
  529. if (ret < 0)
  530. return ret;
  531. }
  532. return 0;
  533. }
  534. /* Generic check to see if a widget should be powered.
  535. */
  536. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  537. {
  538. int in, out;
  539. in = is_connected_input_ep(w);
  540. dapm_clear_walk(w->codec);
  541. out = is_connected_output_ep(w);
  542. dapm_clear_walk(w->codec);
  543. return out != 0 && in != 0;
  544. }
  545. /* Check to see if an ADC has power */
  546. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  547. {
  548. int in;
  549. if (w->active) {
  550. in = is_connected_input_ep(w);
  551. dapm_clear_walk(w->codec);
  552. return in != 0;
  553. } else {
  554. return dapm_generic_check_power(w);
  555. }
  556. }
  557. /* Check to see if a DAC has power */
  558. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  559. {
  560. int out;
  561. if (w->active) {
  562. out = is_connected_output_ep(w);
  563. dapm_clear_walk(w->codec);
  564. return out != 0;
  565. } else {
  566. return dapm_generic_check_power(w);
  567. }
  568. }
  569. /* Check to see if a power supply is needed */
  570. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  571. {
  572. struct snd_soc_dapm_path *path;
  573. int power = 0;
  574. /* Check if one of our outputs is connected */
  575. list_for_each_entry(path, &w->sinks, list_source) {
  576. if (path->connected &&
  577. !path->connected(path->source, path->sink))
  578. continue;
  579. if (path->sink && path->sink->power_check &&
  580. path->sink->power_check(path->sink)) {
  581. power = 1;
  582. break;
  583. }
  584. }
  585. dapm_clear_walk(w->codec);
  586. return power;
  587. }
  588. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  589. struct snd_soc_dapm_widget *b,
  590. int sort[])
  591. {
  592. if (a->codec != b->codec)
  593. return (unsigned long)a - (unsigned long)b;
  594. if (sort[a->id] != sort[b->id])
  595. return sort[a->id] - sort[b->id];
  596. if (a->reg != b->reg)
  597. return a->reg - b->reg;
  598. return 0;
  599. }
  600. /* Insert a widget in order into a DAPM power sequence. */
  601. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  602. struct list_head *list,
  603. int sort[])
  604. {
  605. struct snd_soc_dapm_widget *w;
  606. list_for_each_entry(w, list, power_list)
  607. if (dapm_seq_compare(new_widget, w, sort) < 0) {
  608. list_add_tail(&new_widget->power_list, &w->power_list);
  609. return;
  610. }
  611. list_add_tail(&new_widget->power_list, list);
  612. }
  613. /* Apply the coalesced changes from a DAPM sequence */
  614. static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
  615. struct list_head *pending)
  616. {
  617. struct snd_soc_dapm_widget *w;
  618. int reg, power, ret;
  619. unsigned int value = 0;
  620. unsigned int mask = 0;
  621. unsigned int cur_mask;
  622. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  623. power_list)->reg;
  624. list_for_each_entry(w, pending, power_list) {
  625. cur_mask = 1 << w->shift;
  626. BUG_ON(reg != w->reg);
  627. if (w->invert)
  628. power = !w->power;
  629. else
  630. power = w->power;
  631. mask |= cur_mask;
  632. if (power)
  633. value |= cur_mask;
  634. pop_dbg(codec->pop_time,
  635. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  636. w->name, reg, value, mask);
  637. /* power up pre event */
  638. if (w->power && w->event &&
  639. (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
  640. pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
  641. w->name);
  642. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
  643. if (ret < 0)
  644. pr_err("%s: pre event failed: %d\n",
  645. w->name, ret);
  646. }
  647. /* power down pre event */
  648. if (!w->power && w->event &&
  649. (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
  650. pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
  651. w->name);
  652. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
  653. if (ret < 0)
  654. pr_err("%s: pre event failed: %d\n",
  655. w->name, ret);
  656. }
  657. }
  658. if (reg >= 0) {
  659. pop_dbg(codec->pop_time,
  660. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  661. value, mask, reg, codec->pop_time);
  662. pop_wait(codec->pop_time);
  663. snd_soc_update_bits(codec, reg, mask, value);
  664. }
  665. list_for_each_entry(w, pending, power_list) {
  666. /* power up post event */
  667. if (w->power && w->event &&
  668. (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
  669. pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
  670. w->name);
  671. ret = w->event(w,
  672. NULL, SND_SOC_DAPM_POST_PMU);
  673. if (ret < 0)
  674. pr_err("%s: post event failed: %d\n",
  675. w->name, ret);
  676. }
  677. /* power down post event */
  678. if (!w->power && w->event &&
  679. (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
  680. pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
  681. w->name);
  682. ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
  683. if (ret < 0)
  684. pr_err("%s: post event failed: %d\n",
  685. w->name, ret);
  686. }
  687. }
  688. }
  689. /* Apply a DAPM power sequence.
  690. *
  691. * We walk over a pre-sorted list of widgets to apply power to. In
  692. * order to minimise the number of writes to the device required
  693. * multiple widgets will be updated in a single write where possible.
  694. * Currently anything that requires more than a single write is not
  695. * handled.
  696. */
  697. static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
  698. int event, int sort[])
  699. {
  700. struct snd_soc_dapm_widget *w, *n;
  701. LIST_HEAD(pending);
  702. int cur_sort = -1;
  703. int cur_reg = SND_SOC_NOPM;
  704. int ret;
  705. list_for_each_entry_safe(w, n, list, power_list) {
  706. ret = 0;
  707. /* Do we need to apply any queued changes? */
  708. if (sort[w->id] != cur_sort || w->reg != cur_reg) {
  709. if (!list_empty(&pending))
  710. dapm_seq_run_coalesced(codec, &pending);
  711. INIT_LIST_HEAD(&pending);
  712. cur_sort = -1;
  713. cur_reg = SND_SOC_NOPM;
  714. }
  715. switch (w->id) {
  716. case snd_soc_dapm_pre:
  717. if (!w->event)
  718. list_for_each_entry_safe_continue(w, n, list,
  719. power_list);
  720. if (event == SND_SOC_DAPM_STREAM_START)
  721. ret = w->event(w,
  722. NULL, SND_SOC_DAPM_PRE_PMU);
  723. else if (event == SND_SOC_DAPM_STREAM_STOP)
  724. ret = w->event(w,
  725. NULL, SND_SOC_DAPM_PRE_PMD);
  726. break;
  727. case snd_soc_dapm_post:
  728. if (!w->event)
  729. list_for_each_entry_safe_continue(w, n, list,
  730. power_list);
  731. if (event == SND_SOC_DAPM_STREAM_START)
  732. ret = w->event(w,
  733. NULL, SND_SOC_DAPM_POST_PMU);
  734. else if (event == SND_SOC_DAPM_STREAM_STOP)
  735. ret = w->event(w,
  736. NULL, SND_SOC_DAPM_POST_PMD);
  737. break;
  738. case snd_soc_dapm_input:
  739. case snd_soc_dapm_output:
  740. case snd_soc_dapm_hp:
  741. case snd_soc_dapm_mic:
  742. case snd_soc_dapm_line:
  743. case snd_soc_dapm_spk:
  744. /* No register support currently */
  745. ret = dapm_generic_apply_power(w);
  746. break;
  747. default:
  748. /* Queue it up for application */
  749. cur_sort = sort[w->id];
  750. cur_reg = w->reg;
  751. list_move(&w->power_list, &pending);
  752. break;
  753. }
  754. if (ret < 0)
  755. pr_err("Failed to apply widget power: %d\n",
  756. ret);
  757. }
  758. if (!list_empty(&pending))
  759. dapm_seq_run_coalesced(codec, &pending);
  760. }
  761. /*
  762. * Scan each dapm widget for complete audio path.
  763. * A complete path is a route that has valid endpoints i.e.:-
  764. *
  765. * o DAC to output pin.
  766. * o Input Pin to ADC.
  767. * o Input pin to Output pin (bypass, sidetone)
  768. * o DAC to ADC (loopback).
  769. */
  770. static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
  771. {
  772. struct snd_soc_device *socdev = codec->socdev;
  773. struct snd_soc_dapm_widget *w;
  774. LIST_HEAD(up_list);
  775. LIST_HEAD(down_list);
  776. int ret = 0;
  777. int power;
  778. int sys_power = 0;
  779. /* Check which widgets we need to power and store them in
  780. * lists indicating if they should be powered up or down.
  781. */
  782. list_for_each_entry(w, &codec->dapm_widgets, list) {
  783. switch (w->id) {
  784. case snd_soc_dapm_pre:
  785. dapm_seq_insert(w, &down_list, dapm_down_seq);
  786. break;
  787. case snd_soc_dapm_post:
  788. dapm_seq_insert(w, &up_list, dapm_up_seq);
  789. break;
  790. default:
  791. if (!w->power_check)
  792. continue;
  793. if (!w->force)
  794. power = w->power_check(w);
  795. else
  796. power = 1;
  797. if (power)
  798. sys_power = 1;
  799. if (w->power == power)
  800. continue;
  801. if (power)
  802. dapm_seq_insert(w, &up_list, dapm_up_seq);
  803. else
  804. dapm_seq_insert(w, &down_list, dapm_down_seq);
  805. w->power = power;
  806. break;
  807. }
  808. }
  809. /* If there are no DAPM widgets then try to figure out power from the
  810. * event type.
  811. */
  812. if (list_empty(&codec->dapm_widgets)) {
  813. switch (event) {
  814. case SND_SOC_DAPM_STREAM_START:
  815. case SND_SOC_DAPM_STREAM_RESUME:
  816. sys_power = 1;
  817. break;
  818. case SND_SOC_DAPM_STREAM_SUSPEND:
  819. sys_power = 0;
  820. break;
  821. case SND_SOC_DAPM_STREAM_NOP:
  822. switch (codec->bias_level) {
  823. case SND_SOC_BIAS_STANDBY:
  824. case SND_SOC_BIAS_OFF:
  825. sys_power = 0;
  826. break;
  827. default:
  828. sys_power = 1;
  829. break;
  830. }
  831. break;
  832. default:
  833. break;
  834. }
  835. }
  836. if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
  837. ret = snd_soc_dapm_set_bias_level(socdev,
  838. SND_SOC_BIAS_STANDBY);
  839. if (ret != 0)
  840. pr_err("Failed to turn on bias: %d\n", ret);
  841. }
  842. /* If we're changing to all on or all off then prepare */
  843. if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
  844. (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
  845. ret = snd_soc_dapm_set_bias_level(socdev,
  846. SND_SOC_BIAS_PREPARE);
  847. if (ret != 0)
  848. pr_err("Failed to prepare bias: %d\n", ret);
  849. }
  850. /* Power down widgets first; try to avoid amplifying pops. */
  851. dapm_seq_run(codec, &down_list, event, dapm_down_seq);
  852. /* Now power up. */
  853. dapm_seq_run(codec, &up_list, event, dapm_up_seq);
  854. /* If we just powered the last thing off drop to standby bias */
  855. if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
  856. ret = snd_soc_dapm_set_bias_level(socdev,
  857. SND_SOC_BIAS_STANDBY);
  858. if (ret != 0)
  859. pr_err("Failed to apply standby bias: %d\n", ret);
  860. }
  861. /* If we're in standby and can support bias off then do that */
  862. if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
  863. codec->idle_bias_off) {
  864. ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
  865. if (ret != 0)
  866. pr_err("Failed to turn off bias: %d\n", ret);
  867. }
  868. /* If we just powered up then move to active bias */
  869. if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
  870. ret = snd_soc_dapm_set_bias_level(socdev,
  871. SND_SOC_BIAS_ON);
  872. if (ret != 0)
  873. pr_err("Failed to apply active bias: %d\n", ret);
  874. }
  875. pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
  876. codec->pop_time);
  877. pop_wait(codec->pop_time);
  878. return 0;
  879. }
  880. #ifdef CONFIG_DEBUG_FS
  881. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  882. {
  883. file->private_data = inode->i_private;
  884. return 0;
  885. }
  886. static ssize_t dapm_widget_power_read_file(struct file *file,
  887. char __user *user_buf,
  888. size_t count, loff_t *ppos)
  889. {
  890. struct snd_soc_dapm_widget *w = file->private_data;
  891. char *buf;
  892. int in, out;
  893. ssize_t ret;
  894. struct snd_soc_dapm_path *p = NULL;
  895. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  896. if (!buf)
  897. return -ENOMEM;
  898. in = is_connected_input_ep(w);
  899. dapm_clear_walk(w->codec);
  900. out = is_connected_output_ep(w);
  901. dapm_clear_walk(w->codec);
  902. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  903. w->name, w->power ? "On" : "Off", in, out);
  904. if (w->reg >= 0)
  905. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  906. " - R%d(0x%x) bit %d",
  907. w->reg, w->reg, w->shift);
  908. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  909. if (w->sname)
  910. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  911. w->sname,
  912. w->active ? "active" : "inactive");
  913. list_for_each_entry(p, &w->sources, list_sink) {
  914. if (p->connected && !p->connected(w, p->sink))
  915. continue;
  916. if (p->connect)
  917. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  918. " in %s %s\n",
  919. p->name ? p->name : "static",
  920. p->source->name);
  921. }
  922. list_for_each_entry(p, &w->sinks, list_source) {
  923. if (p->connected && !p->connected(w, p->sink))
  924. continue;
  925. if (p->connect)
  926. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  927. " out %s %s\n",
  928. p->name ? p->name : "static",
  929. p->sink->name);
  930. }
  931. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  932. kfree(buf);
  933. return ret;
  934. }
  935. static const struct file_operations dapm_widget_power_fops = {
  936. .open = dapm_widget_power_open_file,
  937. .read = dapm_widget_power_read_file,
  938. };
  939. void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
  940. {
  941. struct snd_soc_dapm_widget *w;
  942. struct dentry *d;
  943. if (!codec->debugfs_dapm)
  944. return;
  945. list_for_each_entry(w, &codec->dapm_widgets, list) {
  946. if (!w->name)
  947. continue;
  948. d = debugfs_create_file(w->name, 0444,
  949. codec->debugfs_dapm, w,
  950. &dapm_widget_power_fops);
  951. if (!d)
  952. printk(KERN_WARNING
  953. "ASoC: Failed to create %s debugfs file\n",
  954. w->name);
  955. }
  956. }
  957. #else
  958. void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
  959. {
  960. }
  961. #endif
  962. /* test and update the power status of a mux widget */
  963. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  964. struct snd_kcontrol *kcontrol, int change,
  965. int mux, struct soc_enum *e)
  966. {
  967. struct snd_soc_dapm_path *path;
  968. int found = 0;
  969. if (widget->id != snd_soc_dapm_mux &&
  970. widget->id != snd_soc_dapm_value_mux)
  971. return -ENODEV;
  972. if (!change)
  973. return 0;
  974. /* find dapm widget path assoc with kcontrol */
  975. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  976. if (path->kcontrol != kcontrol)
  977. continue;
  978. if (!path->name || !e->texts[mux])
  979. continue;
  980. found = 1;
  981. /* we now need to match the string in the enum to the path */
  982. if (!(strcmp(path->name, e->texts[mux])))
  983. path->connect = 1; /* new connection */
  984. else
  985. path->connect = 0; /* old connection must be powered down */
  986. }
  987. if (found)
  988. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  989. return 0;
  990. }
  991. /* test and update the power status of a mixer or switch widget */
  992. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  993. struct snd_kcontrol *kcontrol, int connect)
  994. {
  995. struct snd_soc_dapm_path *path;
  996. int found = 0;
  997. if (widget->id != snd_soc_dapm_mixer &&
  998. widget->id != snd_soc_dapm_mixer_named_ctl &&
  999. widget->id != snd_soc_dapm_switch)
  1000. return -ENODEV;
  1001. /* find dapm widget path assoc with kcontrol */
  1002. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  1003. if (path->kcontrol != kcontrol)
  1004. continue;
  1005. /* found, now check type */
  1006. found = 1;
  1007. path->connect = connect;
  1008. break;
  1009. }
  1010. if (found)
  1011. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  1012. return 0;
  1013. }
  1014. /* show dapm widget status in sys fs */
  1015. static ssize_t dapm_widget_show(struct device *dev,
  1016. struct device_attribute *attr, char *buf)
  1017. {
  1018. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  1019. struct snd_soc_codec *codec = devdata->card->codec;
  1020. struct snd_soc_dapm_widget *w;
  1021. int count = 0;
  1022. char *state = "not set";
  1023. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1024. /* only display widgets that burnm power */
  1025. switch (w->id) {
  1026. case snd_soc_dapm_hp:
  1027. case snd_soc_dapm_mic:
  1028. case snd_soc_dapm_spk:
  1029. case snd_soc_dapm_line:
  1030. case snd_soc_dapm_micbias:
  1031. case snd_soc_dapm_dac:
  1032. case snd_soc_dapm_adc:
  1033. case snd_soc_dapm_pga:
  1034. case snd_soc_dapm_mixer:
  1035. case snd_soc_dapm_mixer_named_ctl:
  1036. case snd_soc_dapm_supply:
  1037. if (w->name)
  1038. count += sprintf(buf + count, "%s: %s\n",
  1039. w->name, w->power ? "On":"Off");
  1040. break;
  1041. default:
  1042. break;
  1043. }
  1044. }
  1045. switch (codec->bias_level) {
  1046. case SND_SOC_BIAS_ON:
  1047. state = "On";
  1048. break;
  1049. case SND_SOC_BIAS_PREPARE:
  1050. state = "Prepare";
  1051. break;
  1052. case SND_SOC_BIAS_STANDBY:
  1053. state = "Standby";
  1054. break;
  1055. case SND_SOC_BIAS_OFF:
  1056. state = "Off";
  1057. break;
  1058. }
  1059. count += sprintf(buf + count, "PM State: %s\n", state);
  1060. return count;
  1061. }
  1062. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1063. int snd_soc_dapm_sys_add(struct device *dev)
  1064. {
  1065. return device_create_file(dev, &dev_attr_dapm_widget);
  1066. }
  1067. static void snd_soc_dapm_sys_remove(struct device *dev)
  1068. {
  1069. device_remove_file(dev, &dev_attr_dapm_widget);
  1070. }
  1071. /* free all dapm widgets and resources */
  1072. static void dapm_free_widgets(struct snd_soc_codec *codec)
  1073. {
  1074. struct snd_soc_dapm_widget *w, *next_w;
  1075. struct snd_soc_dapm_path *p, *next_p;
  1076. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  1077. list_del(&w->list);
  1078. kfree(w);
  1079. }
  1080. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  1081. list_del(&p->list);
  1082. kfree(p->long_name);
  1083. kfree(p);
  1084. }
  1085. }
  1086. static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  1087. const char *pin, int status)
  1088. {
  1089. struct snd_soc_dapm_widget *w;
  1090. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1091. if (!strcmp(w->name, pin)) {
  1092. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  1093. w->connected = status;
  1094. /* Allow disabling of forced pins */
  1095. if (status == 0)
  1096. w->force = 0;
  1097. return 0;
  1098. }
  1099. }
  1100. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  1101. return -EINVAL;
  1102. }
  1103. /**
  1104. * snd_soc_dapm_sync - scan and power dapm paths
  1105. * @codec: audio codec
  1106. *
  1107. * Walks all dapm audio paths and powers widgets according to their
  1108. * stream or path usage.
  1109. *
  1110. * Returns 0 for success.
  1111. */
  1112. int snd_soc_dapm_sync(struct snd_soc_codec *codec)
  1113. {
  1114. return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1115. }
  1116. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1117. static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
  1118. const struct snd_soc_dapm_route *route)
  1119. {
  1120. struct snd_soc_dapm_path *path;
  1121. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1122. const char *sink = route->sink;
  1123. const char *control = route->control;
  1124. const char *source = route->source;
  1125. int ret = 0;
  1126. /* find src and dest widgets */
  1127. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1128. if (!wsink && !(strcmp(w->name, sink))) {
  1129. wsink = w;
  1130. continue;
  1131. }
  1132. if (!wsource && !(strcmp(w->name, source))) {
  1133. wsource = w;
  1134. }
  1135. }
  1136. if (wsource == NULL || wsink == NULL)
  1137. return -ENODEV;
  1138. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1139. if (!path)
  1140. return -ENOMEM;
  1141. path->source = wsource;
  1142. path->sink = wsink;
  1143. path->connected = route->connected;
  1144. INIT_LIST_HEAD(&path->list);
  1145. INIT_LIST_HEAD(&path->list_source);
  1146. INIT_LIST_HEAD(&path->list_sink);
  1147. /* check for external widgets */
  1148. if (wsink->id == snd_soc_dapm_input) {
  1149. if (wsource->id == snd_soc_dapm_micbias ||
  1150. wsource->id == snd_soc_dapm_mic ||
  1151. wsource->id == snd_soc_dapm_line ||
  1152. wsource->id == snd_soc_dapm_output)
  1153. wsink->ext = 1;
  1154. }
  1155. if (wsource->id == snd_soc_dapm_output) {
  1156. if (wsink->id == snd_soc_dapm_spk ||
  1157. wsink->id == snd_soc_dapm_hp ||
  1158. wsink->id == snd_soc_dapm_line ||
  1159. wsink->id == snd_soc_dapm_input)
  1160. wsource->ext = 1;
  1161. }
  1162. /* connect static paths */
  1163. if (control == NULL) {
  1164. list_add(&path->list, &codec->dapm_paths);
  1165. list_add(&path->list_sink, &wsink->sources);
  1166. list_add(&path->list_source, &wsource->sinks);
  1167. path->connect = 1;
  1168. return 0;
  1169. }
  1170. /* connect dynamic paths */
  1171. switch(wsink->id) {
  1172. case snd_soc_dapm_adc:
  1173. case snd_soc_dapm_dac:
  1174. case snd_soc_dapm_pga:
  1175. case snd_soc_dapm_input:
  1176. case snd_soc_dapm_output:
  1177. case snd_soc_dapm_micbias:
  1178. case snd_soc_dapm_vmid:
  1179. case snd_soc_dapm_pre:
  1180. case snd_soc_dapm_post:
  1181. case snd_soc_dapm_supply:
  1182. case snd_soc_dapm_aif_in:
  1183. case snd_soc_dapm_aif_out:
  1184. list_add(&path->list, &codec->dapm_paths);
  1185. list_add(&path->list_sink, &wsink->sources);
  1186. list_add(&path->list_source, &wsource->sinks);
  1187. path->connect = 1;
  1188. return 0;
  1189. case snd_soc_dapm_mux:
  1190. case snd_soc_dapm_value_mux:
  1191. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  1192. &wsink->kcontrols[0]);
  1193. if (ret != 0)
  1194. goto err;
  1195. break;
  1196. case snd_soc_dapm_switch:
  1197. case snd_soc_dapm_mixer:
  1198. case snd_soc_dapm_mixer_named_ctl:
  1199. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  1200. if (ret != 0)
  1201. goto err;
  1202. break;
  1203. case snd_soc_dapm_hp:
  1204. case snd_soc_dapm_mic:
  1205. case snd_soc_dapm_line:
  1206. case snd_soc_dapm_spk:
  1207. list_add(&path->list, &codec->dapm_paths);
  1208. list_add(&path->list_sink, &wsink->sources);
  1209. list_add(&path->list_source, &wsource->sinks);
  1210. path->connect = 0;
  1211. return 0;
  1212. }
  1213. return 0;
  1214. err:
  1215. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  1216. control, sink);
  1217. kfree(path);
  1218. return ret;
  1219. }
  1220. /**
  1221. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1222. * @codec: codec
  1223. * @route: audio routes
  1224. * @num: number of routes
  1225. *
  1226. * Connects 2 dapm widgets together via a named audio path. The sink is
  1227. * the widget receiving the audio signal, whilst the source is the sender
  1228. * of the audio signal.
  1229. *
  1230. * Returns 0 for success else error. On error all resources can be freed
  1231. * with a call to snd_soc_card_free().
  1232. */
  1233. int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
  1234. const struct snd_soc_dapm_route *route, int num)
  1235. {
  1236. int i, ret;
  1237. for (i = 0; i < num; i++) {
  1238. ret = snd_soc_dapm_add_route(codec, route);
  1239. if (ret < 0) {
  1240. printk(KERN_ERR "Failed to add route %s->%s\n",
  1241. route->source,
  1242. route->sink);
  1243. return ret;
  1244. }
  1245. route++;
  1246. }
  1247. return 0;
  1248. }
  1249. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1250. /**
  1251. * snd_soc_dapm_new_widgets - add new dapm widgets
  1252. * @codec: audio codec
  1253. *
  1254. * Checks the codec for any new dapm widgets and creates them if found.
  1255. *
  1256. * Returns 0 for success.
  1257. */
  1258. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  1259. {
  1260. struct snd_soc_dapm_widget *w;
  1261. list_for_each_entry(w, &codec->dapm_widgets, list)
  1262. {
  1263. if (w->new)
  1264. continue;
  1265. switch(w->id) {
  1266. case snd_soc_dapm_switch:
  1267. case snd_soc_dapm_mixer:
  1268. case snd_soc_dapm_mixer_named_ctl:
  1269. w->power_check = dapm_generic_check_power;
  1270. dapm_new_mixer(codec, w);
  1271. break;
  1272. case snd_soc_dapm_mux:
  1273. case snd_soc_dapm_value_mux:
  1274. w->power_check = dapm_generic_check_power;
  1275. dapm_new_mux(codec, w);
  1276. break;
  1277. case snd_soc_dapm_adc:
  1278. case snd_soc_dapm_aif_out:
  1279. w->power_check = dapm_adc_check_power;
  1280. break;
  1281. case snd_soc_dapm_dac:
  1282. case snd_soc_dapm_aif_in:
  1283. w->power_check = dapm_dac_check_power;
  1284. break;
  1285. case snd_soc_dapm_pga:
  1286. w->power_check = dapm_generic_check_power;
  1287. dapm_new_pga(codec, w);
  1288. break;
  1289. case snd_soc_dapm_input:
  1290. case snd_soc_dapm_output:
  1291. case snd_soc_dapm_micbias:
  1292. case snd_soc_dapm_spk:
  1293. case snd_soc_dapm_hp:
  1294. case snd_soc_dapm_mic:
  1295. case snd_soc_dapm_line:
  1296. w->power_check = dapm_generic_check_power;
  1297. break;
  1298. case snd_soc_dapm_supply:
  1299. w->power_check = dapm_supply_check_power;
  1300. case snd_soc_dapm_vmid:
  1301. case snd_soc_dapm_pre:
  1302. case snd_soc_dapm_post:
  1303. break;
  1304. }
  1305. w->new = 1;
  1306. }
  1307. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1308. return 0;
  1309. }
  1310. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1311. /**
  1312. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1313. * @kcontrol: mixer control
  1314. * @ucontrol: control element information
  1315. *
  1316. * Callback to get the value of a dapm mixer control.
  1317. *
  1318. * Returns 0 for success.
  1319. */
  1320. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1321. struct snd_ctl_elem_value *ucontrol)
  1322. {
  1323. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1324. struct soc_mixer_control *mc =
  1325. (struct soc_mixer_control *)kcontrol->private_value;
  1326. unsigned int reg = mc->reg;
  1327. unsigned int shift = mc->shift;
  1328. unsigned int rshift = mc->rshift;
  1329. int max = mc->max;
  1330. unsigned int invert = mc->invert;
  1331. unsigned int mask = (1 << fls(max)) - 1;
  1332. ucontrol->value.integer.value[0] =
  1333. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1334. if (shift != rshift)
  1335. ucontrol->value.integer.value[1] =
  1336. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1337. if (invert) {
  1338. ucontrol->value.integer.value[0] =
  1339. max - ucontrol->value.integer.value[0];
  1340. if (shift != rshift)
  1341. ucontrol->value.integer.value[1] =
  1342. max - ucontrol->value.integer.value[1];
  1343. }
  1344. return 0;
  1345. }
  1346. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1347. /**
  1348. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1349. * @kcontrol: mixer control
  1350. * @ucontrol: control element information
  1351. *
  1352. * Callback to set the value of a dapm mixer control.
  1353. *
  1354. * Returns 0 for success.
  1355. */
  1356. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1357. struct snd_ctl_elem_value *ucontrol)
  1358. {
  1359. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1360. struct soc_mixer_control *mc =
  1361. (struct soc_mixer_control *)kcontrol->private_value;
  1362. unsigned int reg = mc->reg;
  1363. unsigned int shift = mc->shift;
  1364. unsigned int rshift = mc->rshift;
  1365. int max = mc->max;
  1366. unsigned int mask = (1 << fls(max)) - 1;
  1367. unsigned int invert = mc->invert;
  1368. unsigned int val, val2, val_mask;
  1369. int connect;
  1370. int ret;
  1371. val = (ucontrol->value.integer.value[0] & mask);
  1372. if (invert)
  1373. val = max - val;
  1374. val_mask = mask << shift;
  1375. val = val << shift;
  1376. if (shift != rshift) {
  1377. val2 = (ucontrol->value.integer.value[1] & mask);
  1378. if (invert)
  1379. val2 = max - val2;
  1380. val_mask |= mask << rshift;
  1381. val |= val2 << rshift;
  1382. }
  1383. mutex_lock(&widget->codec->mutex);
  1384. widget->value = val;
  1385. if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
  1386. if (val)
  1387. /* new connection */
  1388. connect = invert ? 0:1;
  1389. else
  1390. /* old connection must be powered down */
  1391. connect = invert ? 1:0;
  1392. dapm_mixer_update_power(widget, kcontrol, connect);
  1393. }
  1394. if (widget->event) {
  1395. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1396. ret = widget->event(widget, kcontrol,
  1397. SND_SOC_DAPM_PRE_REG);
  1398. if (ret < 0) {
  1399. ret = 1;
  1400. goto out;
  1401. }
  1402. }
  1403. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1404. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1405. ret = widget->event(widget, kcontrol,
  1406. SND_SOC_DAPM_POST_REG);
  1407. } else
  1408. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1409. out:
  1410. mutex_unlock(&widget->codec->mutex);
  1411. return ret;
  1412. }
  1413. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1414. /**
  1415. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1416. * @kcontrol: mixer control
  1417. * @ucontrol: control element information
  1418. *
  1419. * Callback to get the value of a dapm enumerated double mixer control.
  1420. *
  1421. * Returns 0 for success.
  1422. */
  1423. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1424. struct snd_ctl_elem_value *ucontrol)
  1425. {
  1426. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1427. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1428. unsigned int val, bitmask;
  1429. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1430. ;
  1431. val = snd_soc_read(widget->codec, e->reg);
  1432. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1433. if (e->shift_l != e->shift_r)
  1434. ucontrol->value.enumerated.item[1] =
  1435. (val >> e->shift_r) & (bitmask - 1);
  1436. return 0;
  1437. }
  1438. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1439. /**
  1440. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1441. * @kcontrol: mixer control
  1442. * @ucontrol: control element information
  1443. *
  1444. * Callback to set the value of a dapm enumerated double mixer control.
  1445. *
  1446. * Returns 0 for success.
  1447. */
  1448. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1449. struct snd_ctl_elem_value *ucontrol)
  1450. {
  1451. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1452. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1453. unsigned int val, mux, change;
  1454. unsigned int mask, bitmask;
  1455. int ret = 0;
  1456. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1457. ;
  1458. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1459. return -EINVAL;
  1460. mux = ucontrol->value.enumerated.item[0];
  1461. val = mux << e->shift_l;
  1462. mask = (bitmask - 1) << e->shift_l;
  1463. if (e->shift_l != e->shift_r) {
  1464. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1465. return -EINVAL;
  1466. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1467. mask |= (bitmask - 1) << e->shift_r;
  1468. }
  1469. mutex_lock(&widget->codec->mutex);
  1470. widget->value = val;
  1471. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1472. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1473. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1474. ret = widget->event(widget,
  1475. kcontrol, SND_SOC_DAPM_PRE_REG);
  1476. if (ret < 0)
  1477. goto out;
  1478. }
  1479. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1480. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1481. ret = widget->event(widget,
  1482. kcontrol, SND_SOC_DAPM_POST_REG);
  1483. out:
  1484. mutex_unlock(&widget->codec->mutex);
  1485. return ret;
  1486. }
  1487. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1488. /**
  1489. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1490. * @kcontrol: mixer control
  1491. * @ucontrol: control element information
  1492. *
  1493. * Returns 0 for success.
  1494. */
  1495. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1496. struct snd_ctl_elem_value *ucontrol)
  1497. {
  1498. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1499. ucontrol->value.enumerated.item[0] = widget->value;
  1500. return 0;
  1501. }
  1502. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1503. /**
  1504. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  1505. * @kcontrol: mixer control
  1506. * @ucontrol: control element information
  1507. *
  1508. * Returns 0 for success.
  1509. */
  1510. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  1511. struct snd_ctl_elem_value *ucontrol)
  1512. {
  1513. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1514. struct soc_enum *e =
  1515. (struct soc_enum *)kcontrol->private_value;
  1516. int change;
  1517. int ret = 0;
  1518. if (ucontrol->value.enumerated.item[0] >= e->max)
  1519. return -EINVAL;
  1520. mutex_lock(&widget->codec->mutex);
  1521. change = widget->value != ucontrol->value.enumerated.item[0];
  1522. widget->value = ucontrol->value.enumerated.item[0];
  1523. dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
  1524. mutex_unlock(&widget->codec->mutex);
  1525. return ret;
  1526. }
  1527. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  1528. /**
  1529. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1530. * callback
  1531. * @kcontrol: mixer control
  1532. * @ucontrol: control element information
  1533. *
  1534. * Callback to get the value of a dapm semi enumerated double mixer control.
  1535. *
  1536. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1537. * used for handling bitfield coded enumeration for example.
  1538. *
  1539. * Returns 0 for success.
  1540. */
  1541. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1542. struct snd_ctl_elem_value *ucontrol)
  1543. {
  1544. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1545. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1546. unsigned int reg_val, val, mux;
  1547. reg_val = snd_soc_read(widget->codec, e->reg);
  1548. val = (reg_val >> e->shift_l) & e->mask;
  1549. for (mux = 0; mux < e->max; mux++) {
  1550. if (val == e->values[mux])
  1551. break;
  1552. }
  1553. ucontrol->value.enumerated.item[0] = mux;
  1554. if (e->shift_l != e->shift_r) {
  1555. val = (reg_val >> e->shift_r) & e->mask;
  1556. for (mux = 0; mux < e->max; mux++) {
  1557. if (val == e->values[mux])
  1558. break;
  1559. }
  1560. ucontrol->value.enumerated.item[1] = mux;
  1561. }
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1565. /**
  1566. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1567. * callback
  1568. * @kcontrol: mixer control
  1569. * @ucontrol: control element information
  1570. *
  1571. * Callback to set the value of a dapm semi enumerated double mixer control.
  1572. *
  1573. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1574. * used for handling bitfield coded enumeration for example.
  1575. *
  1576. * Returns 0 for success.
  1577. */
  1578. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1579. struct snd_ctl_elem_value *ucontrol)
  1580. {
  1581. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1582. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1583. unsigned int val, mux, change;
  1584. unsigned int mask;
  1585. int ret = 0;
  1586. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1587. return -EINVAL;
  1588. mux = ucontrol->value.enumerated.item[0];
  1589. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1590. mask = e->mask << e->shift_l;
  1591. if (e->shift_l != e->shift_r) {
  1592. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1593. return -EINVAL;
  1594. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1595. mask |= e->mask << e->shift_r;
  1596. }
  1597. mutex_lock(&widget->codec->mutex);
  1598. widget->value = val;
  1599. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1600. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1601. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1602. ret = widget->event(widget,
  1603. kcontrol, SND_SOC_DAPM_PRE_REG);
  1604. if (ret < 0)
  1605. goto out;
  1606. }
  1607. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1608. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1609. ret = widget->event(widget,
  1610. kcontrol, SND_SOC_DAPM_POST_REG);
  1611. out:
  1612. mutex_unlock(&widget->codec->mutex);
  1613. return ret;
  1614. }
  1615. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1616. /**
  1617. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1618. *
  1619. * @kcontrol: mixer control
  1620. * @uinfo: control element information
  1621. *
  1622. * Callback to provide information about a pin switch control.
  1623. */
  1624. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1625. struct snd_ctl_elem_info *uinfo)
  1626. {
  1627. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1628. uinfo->count = 1;
  1629. uinfo->value.integer.min = 0;
  1630. uinfo->value.integer.max = 1;
  1631. return 0;
  1632. }
  1633. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1634. /**
  1635. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1636. *
  1637. * @kcontrol: mixer control
  1638. * @ucontrol: Value
  1639. */
  1640. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1641. struct snd_ctl_elem_value *ucontrol)
  1642. {
  1643. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1644. const char *pin = (const char *)kcontrol->private_value;
  1645. mutex_lock(&codec->mutex);
  1646. ucontrol->value.integer.value[0] =
  1647. snd_soc_dapm_get_pin_status(codec, pin);
  1648. mutex_unlock(&codec->mutex);
  1649. return 0;
  1650. }
  1651. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1652. /**
  1653. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1654. *
  1655. * @kcontrol: mixer control
  1656. * @ucontrol: Value
  1657. */
  1658. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1659. struct snd_ctl_elem_value *ucontrol)
  1660. {
  1661. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1662. const char *pin = (const char *)kcontrol->private_value;
  1663. mutex_lock(&codec->mutex);
  1664. if (ucontrol->value.integer.value[0])
  1665. snd_soc_dapm_enable_pin(codec, pin);
  1666. else
  1667. snd_soc_dapm_disable_pin(codec, pin);
  1668. snd_soc_dapm_sync(codec);
  1669. mutex_unlock(&codec->mutex);
  1670. return 0;
  1671. }
  1672. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1673. /**
  1674. * snd_soc_dapm_new_control - create new dapm control
  1675. * @codec: audio codec
  1676. * @widget: widget template
  1677. *
  1678. * Creates a new dapm control based upon the template.
  1679. *
  1680. * Returns 0 for success else error.
  1681. */
  1682. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1683. const struct snd_soc_dapm_widget *widget)
  1684. {
  1685. struct snd_soc_dapm_widget *w;
  1686. if ((w = dapm_cnew_widget(widget)) == NULL)
  1687. return -ENOMEM;
  1688. w->codec = codec;
  1689. INIT_LIST_HEAD(&w->sources);
  1690. INIT_LIST_HEAD(&w->sinks);
  1691. INIT_LIST_HEAD(&w->list);
  1692. list_add(&w->list, &codec->dapm_widgets);
  1693. /* machine layer set ups unconnected pins and insertions */
  1694. w->connected = 1;
  1695. return 0;
  1696. }
  1697. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1698. /**
  1699. * snd_soc_dapm_new_controls - create new dapm controls
  1700. * @codec: audio codec
  1701. * @widget: widget array
  1702. * @num: number of widgets
  1703. *
  1704. * Creates new DAPM controls based upon the templates.
  1705. *
  1706. * Returns 0 for success else error.
  1707. */
  1708. int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
  1709. const struct snd_soc_dapm_widget *widget,
  1710. int num)
  1711. {
  1712. int i, ret;
  1713. for (i = 0; i < num; i++) {
  1714. ret = snd_soc_dapm_new_control(codec, widget);
  1715. if (ret < 0) {
  1716. printk(KERN_ERR
  1717. "ASoC: Failed to create DAPM control %s: %d\n",
  1718. widget->name, ret);
  1719. return ret;
  1720. }
  1721. widget++;
  1722. }
  1723. return 0;
  1724. }
  1725. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1726. /**
  1727. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1728. * @codec: audio codec
  1729. * @stream: stream name
  1730. * @event: stream event
  1731. *
  1732. * Sends a stream event to the dapm core. The core then makes any
  1733. * necessary widget power changes.
  1734. *
  1735. * Returns 0 for success else error.
  1736. */
  1737. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1738. char *stream, int event)
  1739. {
  1740. struct snd_soc_dapm_widget *w;
  1741. if (stream == NULL)
  1742. return 0;
  1743. mutex_lock(&codec->mutex);
  1744. list_for_each_entry(w, &codec->dapm_widgets, list)
  1745. {
  1746. if (!w->sname)
  1747. continue;
  1748. pr_debug("widget %s\n %s stream %s event %d\n",
  1749. w->name, w->sname, stream, event);
  1750. if (strstr(w->sname, stream)) {
  1751. switch(event) {
  1752. case SND_SOC_DAPM_STREAM_START:
  1753. w->active = 1;
  1754. break;
  1755. case SND_SOC_DAPM_STREAM_STOP:
  1756. w->active = 0;
  1757. break;
  1758. case SND_SOC_DAPM_STREAM_SUSPEND:
  1759. case SND_SOC_DAPM_STREAM_RESUME:
  1760. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1761. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1762. break;
  1763. }
  1764. }
  1765. }
  1766. dapm_power_widgets(codec, event);
  1767. mutex_unlock(&codec->mutex);
  1768. return 0;
  1769. }
  1770. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1771. /**
  1772. * snd_soc_dapm_enable_pin - enable pin.
  1773. * @codec: SoC codec
  1774. * @pin: pin name
  1775. *
  1776. * Enables input/output pin and its parents or children widgets iff there is
  1777. * a valid audio route and active audio stream.
  1778. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1779. * do any widget power switching.
  1780. */
  1781. int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1782. {
  1783. return snd_soc_dapm_set_pin(codec, pin, 1);
  1784. }
  1785. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1786. /**
  1787. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  1788. * @codec: SoC codec
  1789. * @pin: pin name
  1790. *
  1791. * Enables input/output pin regardless of any other state. This is
  1792. * intended for use with microphone bias supplies used in microphone
  1793. * jack detection.
  1794. *
  1795. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1796. * do any widget power switching.
  1797. */
  1798. int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1799. {
  1800. struct snd_soc_dapm_widget *w;
  1801. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1802. if (!strcmp(w->name, pin)) {
  1803. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  1804. w->connected = 1;
  1805. w->force = 1;
  1806. return 0;
  1807. }
  1808. }
  1809. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  1810. return -EINVAL;
  1811. }
  1812. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  1813. /**
  1814. * snd_soc_dapm_disable_pin - disable pin.
  1815. * @codec: SoC codec
  1816. * @pin: pin name
  1817. *
  1818. * Disables input/output pin and its parents or children widgets.
  1819. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1820. * do any widget power switching.
  1821. */
  1822. int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
  1823. {
  1824. return snd_soc_dapm_set_pin(codec, pin, 0);
  1825. }
  1826. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1827. /**
  1828. * snd_soc_dapm_nc_pin - permanently disable pin.
  1829. * @codec: SoC codec
  1830. * @pin: pin name
  1831. *
  1832. * Marks the specified pin as being not connected, disabling it along
  1833. * any parent or child widgets. At present this is identical to
  1834. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1835. * additional things such as disabling controls which only affect
  1836. * paths through the pin.
  1837. *
  1838. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1839. * do any widget power switching.
  1840. */
  1841. int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
  1842. {
  1843. return snd_soc_dapm_set_pin(codec, pin, 0);
  1844. }
  1845. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1846. /**
  1847. * snd_soc_dapm_get_pin_status - get audio pin status
  1848. * @codec: audio codec
  1849. * @pin: audio signal pin endpoint (or start point)
  1850. *
  1851. * Get audio pin status - connected or disconnected.
  1852. *
  1853. * Returns 1 for connected otherwise 0.
  1854. */
  1855. int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
  1856. {
  1857. struct snd_soc_dapm_widget *w;
  1858. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1859. if (!strcmp(w->name, pin))
  1860. return w->connected;
  1861. }
  1862. return 0;
  1863. }
  1864. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1865. /**
  1866. * snd_soc_dapm_free - free dapm resources
  1867. * @socdev: SoC device
  1868. *
  1869. * Free all dapm widgets and resources.
  1870. */
  1871. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1872. {
  1873. struct snd_soc_codec *codec = socdev->card->codec;
  1874. snd_soc_dapm_sys_remove(socdev->dev);
  1875. dapm_free_widgets(codec);
  1876. }
  1877. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1878. /*
  1879. * snd_soc_dapm_shutdown - callback for system shutdown
  1880. */
  1881. void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
  1882. {
  1883. struct snd_soc_codec *codec = socdev->card->codec;
  1884. struct snd_soc_dapm_widget *w;
  1885. LIST_HEAD(down_list);
  1886. int powerdown = 0;
  1887. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1888. if (w->power) {
  1889. dapm_seq_insert(w, &down_list, dapm_down_seq);
  1890. w->power = 0;
  1891. powerdown = 1;
  1892. }
  1893. }
  1894. /* If there were no widgets to power down we're already in
  1895. * standby.
  1896. */
  1897. if (powerdown) {
  1898. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
  1899. dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
  1900. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
  1901. }
  1902. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
  1903. }
  1904. /* Module information */
  1905. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1906. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1907. MODULE_LICENSE("GPL");