soc-dapm.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. * DAC's/ADC's.
  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 <sound/core.h>
  40. #include <sound/pcm.h>
  41. #include <sound/pcm_params.h>
  42. #include <sound/soc-dapm.h>
  43. #include <sound/initval.h>
  44. /* debug */
  45. #ifdef DEBUG
  46. #define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
  47. #else
  48. #define dump_dapm(codec, action)
  49. #endif
  50. /* dapm power sequences - make this per codec in the future */
  51. static int dapm_up_seq[] = {
  52. snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
  53. snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac,
  54. snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga,
  55. snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
  56. };
  57. static int dapm_down_seq[] = {
  58. snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
  59. snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
  60. snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
  61. snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post
  62. };
  63. static int dapm_status = 1;
  64. module_param(dapm_status, int, 0);
  65. MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
  66. static void pop_wait(u32 pop_time)
  67. {
  68. if (pop_time)
  69. schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  70. }
  71. static void pop_dbg(u32 pop_time, const char *fmt, ...)
  72. {
  73. va_list args;
  74. va_start(args, fmt);
  75. if (pop_time) {
  76. vprintk(fmt, args);
  77. pop_wait(pop_time);
  78. }
  79. va_end(args);
  80. }
  81. /* create a new dapm widget */
  82. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  83. const struct snd_soc_dapm_widget *_widget)
  84. {
  85. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  86. }
  87. /* set up initial codec paths */
  88. static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
  89. struct snd_soc_dapm_path *p, int i)
  90. {
  91. switch (w->id) {
  92. case snd_soc_dapm_switch:
  93. case snd_soc_dapm_mixer:
  94. case snd_soc_dapm_mixer_named_ctl: {
  95. int val;
  96. struct soc_mixer_control *mc = (struct soc_mixer_control *)
  97. w->kcontrols[i].private_value;
  98. unsigned int reg = mc->reg;
  99. unsigned int shift = mc->shift;
  100. int max = mc->max;
  101. unsigned int mask = (1 << fls(max)) - 1;
  102. unsigned int invert = mc->invert;
  103. val = snd_soc_read(w->codec, reg);
  104. val = (val >> shift) & mask;
  105. if ((invert && !val) || (!invert && val))
  106. p->connect = 1;
  107. else
  108. p->connect = 0;
  109. }
  110. break;
  111. case snd_soc_dapm_mux: {
  112. struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
  113. int val, item, bitmask;
  114. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  115. ;
  116. val = snd_soc_read(w->codec, e->reg);
  117. item = (val >> e->shift_l) & (bitmask - 1);
  118. p->connect = 0;
  119. for (i = 0; i < e->max; i++) {
  120. if (!(strcmp(p->name, e->texts[i])) && item == i)
  121. p->connect = 1;
  122. }
  123. }
  124. break;
  125. case snd_soc_dapm_value_mux: {
  126. struct soc_enum *e = (struct soc_enum *)
  127. w->kcontrols[i].private_value;
  128. int val, item;
  129. val = snd_soc_read(w->codec, e->reg);
  130. val = (val >> e->shift_l) & e->mask;
  131. for (item = 0; item < e->max; item++) {
  132. if (val == e->values[item])
  133. break;
  134. }
  135. p->connect = 0;
  136. for (i = 0; i < e->max; i++) {
  137. if (!(strcmp(p->name, e->texts[i])) && item == i)
  138. p->connect = 1;
  139. }
  140. }
  141. break;
  142. /* does not effect routing - always connected */
  143. case snd_soc_dapm_pga:
  144. case snd_soc_dapm_output:
  145. case snd_soc_dapm_adc:
  146. case snd_soc_dapm_input:
  147. case snd_soc_dapm_dac:
  148. case snd_soc_dapm_micbias:
  149. case snd_soc_dapm_vmid:
  150. p->connect = 1;
  151. break;
  152. /* does effect routing - dynamically connected */
  153. case snd_soc_dapm_hp:
  154. case snd_soc_dapm_mic:
  155. case snd_soc_dapm_spk:
  156. case snd_soc_dapm_line:
  157. case snd_soc_dapm_pre:
  158. case snd_soc_dapm_post:
  159. p->connect = 0;
  160. break;
  161. }
  162. }
  163. /* connect mux widget to it's interconnecting audio paths */
  164. static int dapm_connect_mux(struct snd_soc_codec *codec,
  165. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  166. struct snd_soc_dapm_path *path, const char *control_name,
  167. const struct snd_kcontrol_new *kcontrol)
  168. {
  169. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  170. int i;
  171. for (i = 0; i < e->max; i++) {
  172. if (!(strcmp(control_name, e->texts[i]))) {
  173. list_add(&path->list, &codec->dapm_paths);
  174. list_add(&path->list_sink, &dest->sources);
  175. list_add(&path->list_source, &src->sinks);
  176. path->name = (char*)e->texts[i];
  177. dapm_set_path_status(dest, path, 0);
  178. return 0;
  179. }
  180. }
  181. return -ENODEV;
  182. }
  183. /* connect mixer widget to it's interconnecting audio paths */
  184. static int dapm_connect_mixer(struct snd_soc_codec *codec,
  185. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  186. struct snd_soc_dapm_path *path, const char *control_name)
  187. {
  188. int i;
  189. /* search for mixer kcontrol */
  190. for (i = 0; i < dest->num_kcontrols; i++) {
  191. if (!strcmp(control_name, dest->kcontrols[i].name)) {
  192. list_add(&path->list, &codec->dapm_paths);
  193. list_add(&path->list_sink, &dest->sources);
  194. list_add(&path->list_source, &src->sinks);
  195. path->name = dest->kcontrols[i].name;
  196. dapm_set_path_status(dest, path, i);
  197. return 0;
  198. }
  199. }
  200. return -ENODEV;
  201. }
  202. /* update dapm codec register bits */
  203. static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
  204. {
  205. int change, power;
  206. unsigned short old, new;
  207. struct snd_soc_codec *codec = widget->codec;
  208. /* check for valid widgets */
  209. if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
  210. widget->id == snd_soc_dapm_output ||
  211. widget->id == snd_soc_dapm_hp ||
  212. widget->id == snd_soc_dapm_mic ||
  213. widget->id == snd_soc_dapm_line ||
  214. widget->id == snd_soc_dapm_spk)
  215. return 0;
  216. power = widget->power;
  217. if (widget->invert)
  218. power = (power ? 0:1);
  219. old = snd_soc_read(codec, widget->reg);
  220. new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
  221. change = old != new;
  222. if (change) {
  223. pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
  224. widget->name, widget->power ? "on" : "off",
  225. codec->pop_time);
  226. snd_soc_write(codec, widget->reg, new);
  227. pop_wait(codec->pop_time);
  228. }
  229. pr_debug("reg %x old %x new %x change %d\n", widget->reg,
  230. old, new, change);
  231. return change;
  232. }
  233. /* ramps the volume up or down to minimise pops before or after a
  234. * DAPM power event */
  235. static int dapm_set_pga(struct snd_soc_dapm_widget *widget, int power)
  236. {
  237. const struct snd_kcontrol_new *k = widget->kcontrols;
  238. if (widget->muted && !power)
  239. return 0;
  240. if (!widget->muted && power)
  241. return 0;
  242. if (widget->num_kcontrols && k) {
  243. struct soc_mixer_control *mc =
  244. (struct soc_mixer_control *)k->private_value;
  245. unsigned int reg = mc->reg;
  246. unsigned int shift = mc->shift;
  247. int max = mc->max;
  248. unsigned int mask = (1 << fls(max)) - 1;
  249. unsigned int invert = mc->invert;
  250. if (power) {
  251. int i;
  252. /* power up has happended, increase volume to last level */
  253. if (invert) {
  254. for (i = max; i > widget->saved_value; i--)
  255. snd_soc_update_bits(widget->codec, reg, mask, i);
  256. } else {
  257. for (i = 0; i < widget->saved_value; i++)
  258. snd_soc_update_bits(widget->codec, reg, mask, i);
  259. }
  260. widget->muted = 0;
  261. } else {
  262. /* power down is about to occur, decrease volume to mute */
  263. int val = snd_soc_read(widget->codec, reg);
  264. int i = widget->saved_value = (val >> shift) & mask;
  265. if (invert) {
  266. for (; i < mask; i++)
  267. snd_soc_update_bits(widget->codec, reg, mask, i);
  268. } else {
  269. for (; i > 0; i--)
  270. snd_soc_update_bits(widget->codec, reg, mask, i);
  271. }
  272. widget->muted = 1;
  273. }
  274. }
  275. return 0;
  276. }
  277. /* create new dapm mixer control */
  278. static int dapm_new_mixer(struct snd_soc_codec *codec,
  279. struct snd_soc_dapm_widget *w)
  280. {
  281. int i, ret = 0;
  282. size_t name_len;
  283. struct snd_soc_dapm_path *path;
  284. /* add kcontrol */
  285. for (i = 0; i < w->num_kcontrols; i++) {
  286. /* match name */
  287. list_for_each_entry(path, &w->sources, list_sink) {
  288. /* mixer/mux paths name must match control name */
  289. if (path->name != (char*)w->kcontrols[i].name)
  290. continue;
  291. /* add dapm control with long name.
  292. * for dapm_mixer this is the concatenation of the
  293. * mixer and kcontrol name.
  294. * for dapm_mixer_named_ctl this is simply the
  295. * kcontrol name.
  296. */
  297. name_len = strlen(w->kcontrols[i].name) + 1;
  298. if (w->id != snd_soc_dapm_mixer_named_ctl)
  299. name_len += 1 + strlen(w->name);
  300. path->long_name = kmalloc(name_len, GFP_KERNEL);
  301. if (path->long_name == NULL)
  302. return -ENOMEM;
  303. switch (w->id) {
  304. default:
  305. snprintf(path->long_name, name_len, "%s %s",
  306. w->name, w->kcontrols[i].name);
  307. break;
  308. case snd_soc_dapm_mixer_named_ctl:
  309. snprintf(path->long_name, name_len, "%s",
  310. w->kcontrols[i].name);
  311. break;
  312. }
  313. path->long_name[name_len - 1] = '\0';
  314. path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
  315. path->long_name);
  316. ret = snd_ctl_add(codec->card, path->kcontrol);
  317. if (ret < 0) {
  318. printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
  319. path->long_name,
  320. ret);
  321. kfree(path->long_name);
  322. path->long_name = NULL;
  323. return ret;
  324. }
  325. }
  326. }
  327. return ret;
  328. }
  329. /* create new dapm mux control */
  330. static int dapm_new_mux(struct snd_soc_codec *codec,
  331. struct snd_soc_dapm_widget *w)
  332. {
  333. struct snd_soc_dapm_path *path = NULL;
  334. struct snd_kcontrol *kcontrol;
  335. int ret = 0;
  336. if (!w->num_kcontrols) {
  337. printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
  338. return -EINVAL;
  339. }
  340. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  341. ret = snd_ctl_add(codec->card, kcontrol);
  342. if (ret < 0)
  343. goto err;
  344. list_for_each_entry(path, &w->sources, list_sink)
  345. path->kcontrol = kcontrol;
  346. return ret;
  347. err:
  348. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  349. return ret;
  350. }
  351. /* create new dapm volume control */
  352. static int dapm_new_pga(struct snd_soc_codec *codec,
  353. struct snd_soc_dapm_widget *w)
  354. {
  355. struct snd_kcontrol *kcontrol;
  356. int ret = 0;
  357. if (!w->num_kcontrols)
  358. return -EINVAL;
  359. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  360. ret = snd_ctl_add(codec->card, kcontrol);
  361. if (ret < 0) {
  362. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  363. return ret;
  364. }
  365. return ret;
  366. }
  367. /* reset 'walked' bit for each dapm path */
  368. static inline void dapm_clear_walk(struct snd_soc_codec *codec)
  369. {
  370. struct snd_soc_dapm_path *p;
  371. list_for_each_entry(p, &codec->dapm_paths, list)
  372. p->walked = 0;
  373. }
  374. /*
  375. * Recursively check for a completed path to an active or physically connected
  376. * output widget. Returns number of complete paths.
  377. */
  378. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
  379. {
  380. struct snd_soc_dapm_path *path;
  381. int con = 0;
  382. if (widget->id == snd_soc_dapm_adc && widget->active)
  383. return 1;
  384. if (widget->connected) {
  385. /* connected pin ? */
  386. if (widget->id == snd_soc_dapm_output && !widget->ext)
  387. return 1;
  388. /* connected jack or spk ? */
  389. if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
  390. widget->id == snd_soc_dapm_line)
  391. return 1;
  392. }
  393. list_for_each_entry(path, &widget->sinks, list_source) {
  394. if (path->walked)
  395. continue;
  396. if (path->sink && path->connect) {
  397. path->walked = 1;
  398. con += is_connected_output_ep(path->sink);
  399. }
  400. }
  401. return con;
  402. }
  403. /*
  404. * Recursively check for a completed path to an active or physically connected
  405. * input widget. Returns number of complete paths.
  406. */
  407. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
  408. {
  409. struct snd_soc_dapm_path *path;
  410. int con = 0;
  411. /* active stream ? */
  412. if (widget->id == snd_soc_dapm_dac && widget->active)
  413. return 1;
  414. if (widget->connected) {
  415. /* connected pin ? */
  416. if (widget->id == snd_soc_dapm_input && !widget->ext)
  417. return 1;
  418. /* connected VMID/Bias for lower pops */
  419. if (widget->id == snd_soc_dapm_vmid)
  420. return 1;
  421. /* connected jack ? */
  422. if (widget->id == snd_soc_dapm_mic || widget->id == snd_soc_dapm_line)
  423. return 1;
  424. }
  425. list_for_each_entry(path, &widget->sources, list_sink) {
  426. if (path->walked)
  427. continue;
  428. if (path->source && path->connect) {
  429. path->walked = 1;
  430. con += is_connected_input_ep(path->source);
  431. }
  432. }
  433. return con;
  434. }
  435. /*
  436. * Handler for generic register modifier widget.
  437. */
  438. int dapm_reg_event(struct snd_soc_dapm_widget *w,
  439. struct snd_kcontrol *kcontrol, int event)
  440. {
  441. unsigned int val;
  442. if (SND_SOC_DAPM_EVENT_ON(event))
  443. val = w->on_val;
  444. else
  445. val = w->off_val;
  446. snd_soc_update_bits(w->codec, -(w->reg + 1),
  447. w->mask << w->shift, val << w->shift);
  448. return 0;
  449. }
  450. EXPORT_SYMBOL_GPL(dapm_reg_event);
  451. /* Standard power change method, used to apply power changes to most
  452. * widgets.
  453. */
  454. static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
  455. {
  456. int ret;
  457. /* call any power change event handlers */
  458. if (w->event)
  459. pr_debug("power %s event for %s flags %x\n",
  460. w->power ? "on" : "off",
  461. w->name, w->event_flags);
  462. /* power up pre event */
  463. if (w->power && w->event &&
  464. (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
  465. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
  466. if (ret < 0)
  467. return ret;
  468. }
  469. /* power down pre event */
  470. if (!w->power && w->event &&
  471. (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
  472. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
  473. if (ret < 0)
  474. return ret;
  475. }
  476. /* Lower PGA volume to reduce pops */
  477. if (w->id == snd_soc_dapm_pga && !w->power)
  478. dapm_set_pga(w, w->power);
  479. dapm_update_bits(w);
  480. /* Raise PGA volume to reduce pops */
  481. if (w->id == snd_soc_dapm_pga && w->power)
  482. dapm_set_pga(w, w->power);
  483. /* power up post event */
  484. if (w->power && w->event &&
  485. (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
  486. ret = w->event(w,
  487. NULL, SND_SOC_DAPM_POST_PMU);
  488. if (ret < 0)
  489. return ret;
  490. }
  491. /* power down post event */
  492. if (!w->power && w->event &&
  493. (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
  494. ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
  495. if (ret < 0)
  496. return ret;
  497. }
  498. return 0;
  499. }
  500. /* Generic check to see if a widget should be powered.
  501. */
  502. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  503. {
  504. int in, out;
  505. in = is_connected_input_ep(w);
  506. dapm_clear_walk(w->codec);
  507. out = is_connected_output_ep(w);
  508. dapm_clear_walk(w->codec);
  509. return out != 0 && in != 0;
  510. }
  511. /* Check to see if an ADC has power */
  512. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  513. {
  514. int in;
  515. if (w->active) {
  516. in = is_connected_input_ep(w);
  517. dapm_clear_walk(w->codec);
  518. return in != 0;
  519. } else {
  520. return dapm_generic_check_power(w);
  521. }
  522. }
  523. /* Check to see if a DAC has power */
  524. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  525. {
  526. int out;
  527. if (w->active) {
  528. out = is_connected_output_ep(w);
  529. dapm_clear_walk(w->codec);
  530. return out != 0;
  531. } else {
  532. return dapm_generic_check_power(w);
  533. }
  534. }
  535. /*
  536. * Scan a single DAPM widget for a complete audio path and update the
  537. * power status appropriately.
  538. */
  539. static int dapm_power_widget(struct snd_soc_codec *codec, int event,
  540. struct snd_soc_dapm_widget *w)
  541. {
  542. int power, ret;
  543. /* Work out the new power state */
  544. switch (w->id) {
  545. case snd_soc_dapm_vmid:
  546. /* No action required */
  547. return 0;
  548. case snd_soc_dapm_adc:
  549. power = dapm_adc_check_power(w);
  550. break;
  551. case snd_soc_dapm_dac:
  552. power = dapm_dac_check_power(w);
  553. break;
  554. case snd_soc_dapm_pre:
  555. if (!w->event)
  556. return 0;
  557. if (event == SND_SOC_DAPM_STREAM_START) {
  558. ret = w->event(w,
  559. NULL, SND_SOC_DAPM_PRE_PMU);
  560. if (ret < 0)
  561. return ret;
  562. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  563. ret = w->event(w,
  564. NULL, SND_SOC_DAPM_PRE_PMD);
  565. if (ret < 0)
  566. return ret;
  567. }
  568. return 0;
  569. case snd_soc_dapm_post:
  570. if (!w->event)
  571. return 0;
  572. if (event == SND_SOC_DAPM_STREAM_START) {
  573. ret = w->event(w,
  574. NULL, SND_SOC_DAPM_POST_PMU);
  575. if (ret < 0)
  576. return ret;
  577. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  578. ret = w->event(w,
  579. NULL, SND_SOC_DAPM_POST_PMD);
  580. if (ret < 0)
  581. return ret;
  582. }
  583. return 0;
  584. default:
  585. power = dapm_generic_check_power(w);
  586. break;
  587. }
  588. if (w->power == power)
  589. return 0;
  590. w->power = power;
  591. return dapm_generic_apply_power(w);
  592. }
  593. /*
  594. * Scan each dapm widget for complete audio path.
  595. * A complete path is a route that has valid endpoints i.e.:-
  596. *
  597. * o DAC to output pin.
  598. * o Input Pin to ADC.
  599. * o Input pin to Output pin (bypass, sidetone)
  600. * o DAC to ADC (loopback).
  601. */
  602. static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
  603. {
  604. struct snd_soc_dapm_widget *w;
  605. int i, c = 1, *seq = NULL, ret = 0;
  606. /* do we have a sequenced stream event */
  607. if (event == SND_SOC_DAPM_STREAM_START) {
  608. c = ARRAY_SIZE(dapm_up_seq);
  609. seq = dapm_up_seq;
  610. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  611. c = ARRAY_SIZE(dapm_down_seq);
  612. seq = dapm_down_seq;
  613. }
  614. for (i = 0; i < c; i++) {
  615. list_for_each_entry(w, &codec->dapm_widgets, list) {
  616. /* is widget in stream order */
  617. if (seq && seq[i] && w->id != seq[i])
  618. continue;
  619. ret = dapm_power_widget(codec, event, w);
  620. if (ret != 0)
  621. return ret;
  622. }
  623. }
  624. return 0;
  625. }
  626. #ifdef DEBUG
  627. static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
  628. {
  629. struct snd_soc_dapm_widget *w;
  630. struct snd_soc_dapm_path *p = NULL;
  631. int in, out;
  632. printk("DAPM %s %s\n", codec->name, action);
  633. list_for_each_entry(w, &codec->dapm_widgets, list) {
  634. /* only display widgets that effect routing */
  635. switch (w->id) {
  636. case snd_soc_dapm_pre:
  637. case snd_soc_dapm_post:
  638. case snd_soc_dapm_vmid:
  639. continue;
  640. case snd_soc_dapm_mux:
  641. case snd_soc_dapm_value_mux:
  642. case snd_soc_dapm_output:
  643. case snd_soc_dapm_input:
  644. case snd_soc_dapm_switch:
  645. case snd_soc_dapm_hp:
  646. case snd_soc_dapm_mic:
  647. case snd_soc_dapm_spk:
  648. case snd_soc_dapm_line:
  649. case snd_soc_dapm_micbias:
  650. case snd_soc_dapm_dac:
  651. case snd_soc_dapm_adc:
  652. case snd_soc_dapm_pga:
  653. case snd_soc_dapm_mixer:
  654. case snd_soc_dapm_mixer_named_ctl:
  655. if (w->name) {
  656. in = is_connected_input_ep(w);
  657. dapm_clear_walk(w->codec);
  658. out = is_connected_output_ep(w);
  659. dapm_clear_walk(w->codec);
  660. printk("%s: %s in %d out %d\n", w->name,
  661. w->power ? "On":"Off",in, out);
  662. list_for_each_entry(p, &w->sources, list_sink) {
  663. if (p->connect)
  664. printk(" in %s %s\n", p->name ? p->name : "static",
  665. p->source->name);
  666. }
  667. list_for_each_entry(p, &w->sinks, list_source) {
  668. if (p->connect)
  669. printk(" out %s %s\n", p->name ? p->name : "static",
  670. p->sink->name);
  671. }
  672. }
  673. break;
  674. }
  675. }
  676. }
  677. #endif
  678. /* test and update the power status of a mux widget */
  679. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  680. struct snd_kcontrol *kcontrol, int mask,
  681. int mux, int val, struct soc_enum *e)
  682. {
  683. struct snd_soc_dapm_path *path;
  684. int found = 0;
  685. if (widget->id != snd_soc_dapm_mux &&
  686. widget->id != snd_soc_dapm_value_mux)
  687. return -ENODEV;
  688. if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
  689. return 0;
  690. /* find dapm widget path assoc with kcontrol */
  691. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  692. if (path->kcontrol != kcontrol)
  693. continue;
  694. if (!path->name || !e->texts[mux])
  695. continue;
  696. found = 1;
  697. /* we now need to match the string in the enum to the path */
  698. if (!(strcmp(path->name, e->texts[mux])))
  699. path->connect = 1; /* new connection */
  700. else
  701. path->connect = 0; /* old connection must be powered down */
  702. }
  703. if (found) {
  704. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  705. dump_dapm(widget->codec, "mux power update");
  706. }
  707. return 0;
  708. }
  709. /* test and update the power status of a mixer or switch widget */
  710. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  711. struct snd_kcontrol *kcontrol, int reg,
  712. int val_mask, int val, int invert)
  713. {
  714. struct snd_soc_dapm_path *path;
  715. int found = 0;
  716. if (widget->id != snd_soc_dapm_mixer &&
  717. widget->id != snd_soc_dapm_mixer_named_ctl &&
  718. widget->id != snd_soc_dapm_switch)
  719. return -ENODEV;
  720. if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
  721. return 0;
  722. /* find dapm widget path assoc with kcontrol */
  723. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  724. if (path->kcontrol != kcontrol)
  725. continue;
  726. /* found, now check type */
  727. found = 1;
  728. if (val)
  729. /* new connection */
  730. path->connect = invert ? 0:1;
  731. else
  732. /* old connection must be powered down */
  733. path->connect = invert ? 1:0;
  734. break;
  735. }
  736. if (found) {
  737. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  738. dump_dapm(widget->codec, "mixer power update");
  739. }
  740. return 0;
  741. }
  742. /* show dapm widget status in sys fs */
  743. static ssize_t dapm_widget_show(struct device *dev,
  744. struct device_attribute *attr, char *buf)
  745. {
  746. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  747. struct snd_soc_codec *codec = devdata->card->codec;
  748. struct snd_soc_dapm_widget *w;
  749. int count = 0;
  750. char *state = "not set";
  751. list_for_each_entry(w, &codec->dapm_widgets, list) {
  752. /* only display widgets that burnm power */
  753. switch (w->id) {
  754. case snd_soc_dapm_hp:
  755. case snd_soc_dapm_mic:
  756. case snd_soc_dapm_spk:
  757. case snd_soc_dapm_line:
  758. case snd_soc_dapm_micbias:
  759. case snd_soc_dapm_dac:
  760. case snd_soc_dapm_adc:
  761. case snd_soc_dapm_pga:
  762. case snd_soc_dapm_mixer:
  763. case snd_soc_dapm_mixer_named_ctl:
  764. if (w->name)
  765. count += sprintf(buf + count, "%s: %s\n",
  766. w->name, w->power ? "On":"Off");
  767. break;
  768. default:
  769. break;
  770. }
  771. }
  772. switch (codec->bias_level) {
  773. case SND_SOC_BIAS_ON:
  774. state = "On";
  775. break;
  776. case SND_SOC_BIAS_PREPARE:
  777. state = "Prepare";
  778. break;
  779. case SND_SOC_BIAS_STANDBY:
  780. state = "Standby";
  781. break;
  782. case SND_SOC_BIAS_OFF:
  783. state = "Off";
  784. break;
  785. }
  786. count += sprintf(buf + count, "PM State: %s\n", state);
  787. return count;
  788. }
  789. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  790. int snd_soc_dapm_sys_add(struct device *dev)
  791. {
  792. if (!dapm_status)
  793. return 0;
  794. return device_create_file(dev, &dev_attr_dapm_widget);
  795. }
  796. static void snd_soc_dapm_sys_remove(struct device *dev)
  797. {
  798. if (dapm_status) {
  799. device_remove_file(dev, &dev_attr_dapm_widget);
  800. }
  801. }
  802. /* free all dapm widgets and resources */
  803. static void dapm_free_widgets(struct snd_soc_codec *codec)
  804. {
  805. struct snd_soc_dapm_widget *w, *next_w;
  806. struct snd_soc_dapm_path *p, *next_p;
  807. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  808. list_del(&w->list);
  809. kfree(w);
  810. }
  811. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  812. list_del(&p->list);
  813. kfree(p->long_name);
  814. kfree(p);
  815. }
  816. }
  817. static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  818. const char *pin, int status)
  819. {
  820. struct snd_soc_dapm_widget *w;
  821. list_for_each_entry(w, &codec->dapm_widgets, list) {
  822. if (!strcmp(w->name, pin)) {
  823. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  824. w->connected = status;
  825. return 0;
  826. }
  827. }
  828. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  829. return -EINVAL;
  830. }
  831. /**
  832. * snd_soc_dapm_sync - scan and power dapm paths
  833. * @codec: audio codec
  834. *
  835. * Walks all dapm audio paths and powers widgets according to their
  836. * stream or path usage.
  837. *
  838. * Returns 0 for success.
  839. */
  840. int snd_soc_dapm_sync(struct snd_soc_codec *codec)
  841. {
  842. int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  843. dump_dapm(codec, "sync");
  844. return ret;
  845. }
  846. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  847. static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
  848. const char *sink, const char *control, const char *source)
  849. {
  850. struct snd_soc_dapm_path *path;
  851. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  852. int ret = 0;
  853. /* find src and dest widgets */
  854. list_for_each_entry(w, &codec->dapm_widgets, list) {
  855. if (!wsink && !(strcmp(w->name, sink))) {
  856. wsink = w;
  857. continue;
  858. }
  859. if (!wsource && !(strcmp(w->name, source))) {
  860. wsource = w;
  861. }
  862. }
  863. if (wsource == NULL || wsink == NULL)
  864. return -ENODEV;
  865. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  866. if (!path)
  867. return -ENOMEM;
  868. path->source = wsource;
  869. path->sink = wsink;
  870. INIT_LIST_HEAD(&path->list);
  871. INIT_LIST_HEAD(&path->list_source);
  872. INIT_LIST_HEAD(&path->list_sink);
  873. /* check for external widgets */
  874. if (wsink->id == snd_soc_dapm_input) {
  875. if (wsource->id == snd_soc_dapm_micbias ||
  876. wsource->id == snd_soc_dapm_mic ||
  877. wsink->id == snd_soc_dapm_line ||
  878. wsink->id == snd_soc_dapm_output)
  879. wsink->ext = 1;
  880. }
  881. if (wsource->id == snd_soc_dapm_output) {
  882. if (wsink->id == snd_soc_dapm_spk ||
  883. wsink->id == snd_soc_dapm_hp ||
  884. wsink->id == snd_soc_dapm_line ||
  885. wsink->id == snd_soc_dapm_input)
  886. wsource->ext = 1;
  887. }
  888. /* connect static paths */
  889. if (control == NULL) {
  890. list_add(&path->list, &codec->dapm_paths);
  891. list_add(&path->list_sink, &wsink->sources);
  892. list_add(&path->list_source, &wsource->sinks);
  893. path->connect = 1;
  894. return 0;
  895. }
  896. /* connect dynamic paths */
  897. switch(wsink->id) {
  898. case snd_soc_dapm_adc:
  899. case snd_soc_dapm_dac:
  900. case snd_soc_dapm_pga:
  901. case snd_soc_dapm_input:
  902. case snd_soc_dapm_output:
  903. case snd_soc_dapm_micbias:
  904. case snd_soc_dapm_vmid:
  905. case snd_soc_dapm_pre:
  906. case snd_soc_dapm_post:
  907. list_add(&path->list, &codec->dapm_paths);
  908. list_add(&path->list_sink, &wsink->sources);
  909. list_add(&path->list_source, &wsource->sinks);
  910. path->connect = 1;
  911. return 0;
  912. case snd_soc_dapm_mux:
  913. case snd_soc_dapm_value_mux:
  914. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  915. &wsink->kcontrols[0]);
  916. if (ret != 0)
  917. goto err;
  918. break;
  919. case snd_soc_dapm_switch:
  920. case snd_soc_dapm_mixer:
  921. case snd_soc_dapm_mixer_named_ctl:
  922. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  923. if (ret != 0)
  924. goto err;
  925. break;
  926. case snd_soc_dapm_hp:
  927. case snd_soc_dapm_mic:
  928. case snd_soc_dapm_line:
  929. case snd_soc_dapm_spk:
  930. list_add(&path->list, &codec->dapm_paths);
  931. list_add(&path->list_sink, &wsink->sources);
  932. list_add(&path->list_source, &wsource->sinks);
  933. path->connect = 0;
  934. return 0;
  935. }
  936. return 0;
  937. err:
  938. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  939. control, sink);
  940. kfree(path);
  941. return ret;
  942. }
  943. /**
  944. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  945. * @codec: codec
  946. * @route: audio routes
  947. * @num: number of routes
  948. *
  949. * Connects 2 dapm widgets together via a named audio path. The sink is
  950. * the widget receiving the audio signal, whilst the source is the sender
  951. * of the audio signal.
  952. *
  953. * Returns 0 for success else error. On error all resources can be freed
  954. * with a call to snd_soc_card_free().
  955. */
  956. int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
  957. const struct snd_soc_dapm_route *route, int num)
  958. {
  959. int i, ret;
  960. for (i = 0; i < num; i++) {
  961. ret = snd_soc_dapm_add_route(codec, route->sink,
  962. route->control, route->source);
  963. if (ret < 0) {
  964. printk(KERN_ERR "Failed to add route %s->%s\n",
  965. route->source,
  966. route->sink);
  967. return ret;
  968. }
  969. route++;
  970. }
  971. return 0;
  972. }
  973. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  974. /**
  975. * snd_soc_dapm_new_widgets - add new dapm widgets
  976. * @codec: audio codec
  977. *
  978. * Checks the codec for any new dapm widgets and creates them if found.
  979. *
  980. * Returns 0 for success.
  981. */
  982. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  983. {
  984. struct snd_soc_dapm_widget *w;
  985. list_for_each_entry(w, &codec->dapm_widgets, list)
  986. {
  987. if (w->new)
  988. continue;
  989. switch(w->id) {
  990. case snd_soc_dapm_switch:
  991. case snd_soc_dapm_mixer:
  992. case snd_soc_dapm_mixer_named_ctl:
  993. dapm_new_mixer(codec, w);
  994. break;
  995. case snd_soc_dapm_mux:
  996. case snd_soc_dapm_value_mux:
  997. dapm_new_mux(codec, w);
  998. break;
  999. case snd_soc_dapm_adc:
  1000. case snd_soc_dapm_dac:
  1001. case snd_soc_dapm_pga:
  1002. dapm_new_pga(codec, w);
  1003. break;
  1004. case snd_soc_dapm_input:
  1005. case snd_soc_dapm_output:
  1006. case snd_soc_dapm_micbias:
  1007. case snd_soc_dapm_spk:
  1008. case snd_soc_dapm_hp:
  1009. case snd_soc_dapm_mic:
  1010. case snd_soc_dapm_line:
  1011. case snd_soc_dapm_vmid:
  1012. case snd_soc_dapm_pre:
  1013. case snd_soc_dapm_post:
  1014. break;
  1015. }
  1016. w->new = 1;
  1017. }
  1018. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1022. /**
  1023. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1024. * @kcontrol: mixer control
  1025. * @ucontrol: control element information
  1026. *
  1027. * Callback to get the value of a dapm mixer control.
  1028. *
  1029. * Returns 0 for success.
  1030. */
  1031. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1032. struct snd_ctl_elem_value *ucontrol)
  1033. {
  1034. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1035. struct soc_mixer_control *mc =
  1036. (struct soc_mixer_control *)kcontrol->private_value;
  1037. unsigned int reg = mc->reg;
  1038. unsigned int shift = mc->shift;
  1039. unsigned int rshift = mc->rshift;
  1040. int max = mc->max;
  1041. unsigned int invert = mc->invert;
  1042. unsigned int mask = (1 << fls(max)) - 1;
  1043. /* return the saved value if we are powered down */
  1044. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  1045. ucontrol->value.integer.value[0] = widget->saved_value;
  1046. return 0;
  1047. }
  1048. ucontrol->value.integer.value[0] =
  1049. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1050. if (shift != rshift)
  1051. ucontrol->value.integer.value[1] =
  1052. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1053. if (invert) {
  1054. ucontrol->value.integer.value[0] =
  1055. max - ucontrol->value.integer.value[0];
  1056. if (shift != rshift)
  1057. ucontrol->value.integer.value[1] =
  1058. max - ucontrol->value.integer.value[1];
  1059. }
  1060. return 0;
  1061. }
  1062. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1063. /**
  1064. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1065. * @kcontrol: mixer control
  1066. * @ucontrol: control element information
  1067. *
  1068. * Callback to set the value of a dapm mixer control.
  1069. *
  1070. * Returns 0 for success.
  1071. */
  1072. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1073. struct snd_ctl_elem_value *ucontrol)
  1074. {
  1075. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1076. struct soc_mixer_control *mc =
  1077. (struct soc_mixer_control *)kcontrol->private_value;
  1078. unsigned int reg = mc->reg;
  1079. unsigned int shift = mc->shift;
  1080. unsigned int rshift = mc->rshift;
  1081. int max = mc->max;
  1082. unsigned int mask = (1 << fls(max)) - 1;
  1083. unsigned int invert = mc->invert;
  1084. unsigned short val, val2, val_mask;
  1085. int ret;
  1086. val = (ucontrol->value.integer.value[0] & mask);
  1087. if (invert)
  1088. val = max - val;
  1089. val_mask = mask << shift;
  1090. val = val << shift;
  1091. if (shift != rshift) {
  1092. val2 = (ucontrol->value.integer.value[1] & mask);
  1093. if (invert)
  1094. val2 = max - val2;
  1095. val_mask |= mask << rshift;
  1096. val |= val2 << rshift;
  1097. }
  1098. mutex_lock(&widget->codec->mutex);
  1099. widget->value = val;
  1100. /* save volume value if the widget is powered down */
  1101. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  1102. widget->saved_value = val;
  1103. mutex_unlock(&widget->codec->mutex);
  1104. return 1;
  1105. }
  1106. dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
  1107. if (widget->event) {
  1108. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1109. ret = widget->event(widget, kcontrol,
  1110. SND_SOC_DAPM_PRE_REG);
  1111. if (ret < 0) {
  1112. ret = 1;
  1113. goto out;
  1114. }
  1115. }
  1116. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1117. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1118. ret = widget->event(widget, kcontrol,
  1119. SND_SOC_DAPM_POST_REG);
  1120. } else
  1121. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1122. out:
  1123. mutex_unlock(&widget->codec->mutex);
  1124. return ret;
  1125. }
  1126. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1127. /**
  1128. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1129. * @kcontrol: mixer control
  1130. * @ucontrol: control element information
  1131. *
  1132. * Callback to get the value of a dapm enumerated double mixer control.
  1133. *
  1134. * Returns 0 for success.
  1135. */
  1136. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1137. struct snd_ctl_elem_value *ucontrol)
  1138. {
  1139. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1140. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1141. unsigned short val, bitmask;
  1142. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1143. ;
  1144. val = snd_soc_read(widget->codec, e->reg);
  1145. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1146. if (e->shift_l != e->shift_r)
  1147. ucontrol->value.enumerated.item[1] =
  1148. (val >> e->shift_r) & (bitmask - 1);
  1149. return 0;
  1150. }
  1151. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1152. /**
  1153. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1154. * @kcontrol: mixer control
  1155. * @ucontrol: control element information
  1156. *
  1157. * Callback to set the value of a dapm enumerated double mixer control.
  1158. *
  1159. * Returns 0 for success.
  1160. */
  1161. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1162. struct snd_ctl_elem_value *ucontrol)
  1163. {
  1164. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1165. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1166. unsigned short val, mux;
  1167. unsigned short mask, bitmask;
  1168. int ret = 0;
  1169. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1170. ;
  1171. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1172. return -EINVAL;
  1173. mux = ucontrol->value.enumerated.item[0];
  1174. val = mux << e->shift_l;
  1175. mask = (bitmask - 1) << e->shift_l;
  1176. if (e->shift_l != e->shift_r) {
  1177. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1178. return -EINVAL;
  1179. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1180. mask |= (bitmask - 1) << e->shift_r;
  1181. }
  1182. mutex_lock(&widget->codec->mutex);
  1183. widget->value = val;
  1184. dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
  1185. if (widget->event) {
  1186. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1187. ret = widget->event(widget,
  1188. kcontrol, SND_SOC_DAPM_PRE_REG);
  1189. if (ret < 0)
  1190. goto out;
  1191. }
  1192. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1193. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1194. ret = widget->event(widget,
  1195. kcontrol, SND_SOC_DAPM_POST_REG);
  1196. } else
  1197. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1198. out:
  1199. mutex_unlock(&widget->codec->mutex);
  1200. return ret;
  1201. }
  1202. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1203. /**
  1204. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1205. * callback
  1206. * @kcontrol: mixer control
  1207. * @ucontrol: control element information
  1208. *
  1209. * Callback to get the value of a dapm semi enumerated double mixer control.
  1210. *
  1211. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1212. * used for handling bitfield coded enumeration for example.
  1213. *
  1214. * Returns 0 for success.
  1215. */
  1216. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1217. struct snd_ctl_elem_value *ucontrol)
  1218. {
  1219. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1220. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1221. unsigned short reg_val, val, mux;
  1222. reg_val = snd_soc_read(widget->codec, e->reg);
  1223. val = (reg_val >> e->shift_l) & e->mask;
  1224. for (mux = 0; mux < e->max; mux++) {
  1225. if (val == e->values[mux])
  1226. break;
  1227. }
  1228. ucontrol->value.enumerated.item[0] = mux;
  1229. if (e->shift_l != e->shift_r) {
  1230. val = (reg_val >> e->shift_r) & e->mask;
  1231. for (mux = 0; mux < e->max; mux++) {
  1232. if (val == e->values[mux])
  1233. break;
  1234. }
  1235. ucontrol->value.enumerated.item[1] = mux;
  1236. }
  1237. return 0;
  1238. }
  1239. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1240. /**
  1241. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1242. * callback
  1243. * @kcontrol: mixer control
  1244. * @ucontrol: control element information
  1245. *
  1246. * Callback to set the value of a dapm semi enumerated double mixer control.
  1247. *
  1248. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1249. * used for handling bitfield coded enumeration for example.
  1250. *
  1251. * Returns 0 for success.
  1252. */
  1253. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1254. struct snd_ctl_elem_value *ucontrol)
  1255. {
  1256. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1257. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1258. unsigned short val, mux;
  1259. unsigned short mask;
  1260. int ret = 0;
  1261. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1262. return -EINVAL;
  1263. mux = ucontrol->value.enumerated.item[0];
  1264. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1265. mask = e->mask << e->shift_l;
  1266. if (e->shift_l != e->shift_r) {
  1267. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1268. return -EINVAL;
  1269. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1270. mask |= e->mask << e->shift_r;
  1271. }
  1272. mutex_lock(&widget->codec->mutex);
  1273. widget->value = val;
  1274. dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
  1275. if (widget->event) {
  1276. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1277. ret = widget->event(widget,
  1278. kcontrol, SND_SOC_DAPM_PRE_REG);
  1279. if (ret < 0)
  1280. goto out;
  1281. }
  1282. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1283. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1284. ret = widget->event(widget,
  1285. kcontrol, SND_SOC_DAPM_POST_REG);
  1286. } else
  1287. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1288. out:
  1289. mutex_unlock(&widget->codec->mutex);
  1290. return ret;
  1291. }
  1292. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1293. /**
  1294. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1295. *
  1296. * @kcontrol: mixer control
  1297. * @uinfo: control element information
  1298. *
  1299. * Callback to provide information about a pin switch control.
  1300. */
  1301. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1302. struct snd_ctl_elem_info *uinfo)
  1303. {
  1304. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1305. uinfo->count = 1;
  1306. uinfo->value.integer.min = 0;
  1307. uinfo->value.integer.max = 1;
  1308. return 0;
  1309. }
  1310. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1311. /**
  1312. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1313. *
  1314. * @kcontrol: mixer control
  1315. * @ucontrol: Value
  1316. */
  1317. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1318. struct snd_ctl_elem_value *ucontrol)
  1319. {
  1320. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1321. const char *pin = (const char *)kcontrol->private_value;
  1322. mutex_lock(&codec->mutex);
  1323. ucontrol->value.integer.value[0] =
  1324. snd_soc_dapm_get_pin_status(codec, pin);
  1325. mutex_unlock(&codec->mutex);
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1329. /**
  1330. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1331. *
  1332. * @kcontrol: mixer control
  1333. * @ucontrol: Value
  1334. */
  1335. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1336. struct snd_ctl_elem_value *ucontrol)
  1337. {
  1338. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1339. const char *pin = (const char *)kcontrol->private_value;
  1340. mutex_lock(&codec->mutex);
  1341. if (ucontrol->value.integer.value[0])
  1342. snd_soc_dapm_enable_pin(codec, pin);
  1343. else
  1344. snd_soc_dapm_disable_pin(codec, pin);
  1345. snd_soc_dapm_sync(codec);
  1346. mutex_unlock(&codec->mutex);
  1347. return 0;
  1348. }
  1349. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1350. /**
  1351. * snd_soc_dapm_new_control - create new dapm control
  1352. * @codec: audio codec
  1353. * @widget: widget template
  1354. *
  1355. * Creates a new dapm control based upon the template.
  1356. *
  1357. * Returns 0 for success else error.
  1358. */
  1359. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1360. const struct snd_soc_dapm_widget *widget)
  1361. {
  1362. struct snd_soc_dapm_widget *w;
  1363. if ((w = dapm_cnew_widget(widget)) == NULL)
  1364. return -ENOMEM;
  1365. w->codec = codec;
  1366. INIT_LIST_HEAD(&w->sources);
  1367. INIT_LIST_HEAD(&w->sinks);
  1368. INIT_LIST_HEAD(&w->list);
  1369. list_add(&w->list, &codec->dapm_widgets);
  1370. /* machine layer set ups unconnected pins and insertions */
  1371. w->connected = 1;
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1375. /**
  1376. * snd_soc_dapm_new_controls - create new dapm controls
  1377. * @codec: audio codec
  1378. * @widget: widget array
  1379. * @num: number of widgets
  1380. *
  1381. * Creates new DAPM controls based upon the templates.
  1382. *
  1383. * Returns 0 for success else error.
  1384. */
  1385. int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
  1386. const struct snd_soc_dapm_widget *widget,
  1387. int num)
  1388. {
  1389. int i, ret;
  1390. for (i = 0; i < num; i++) {
  1391. ret = snd_soc_dapm_new_control(codec, widget);
  1392. if (ret < 0) {
  1393. printk(KERN_ERR
  1394. "ASoC: Failed to create DAPM control %s: %d\n",
  1395. widget->name, ret);
  1396. return ret;
  1397. }
  1398. widget++;
  1399. }
  1400. return 0;
  1401. }
  1402. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1403. /**
  1404. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1405. * @codec: audio codec
  1406. * @stream: stream name
  1407. * @event: stream event
  1408. *
  1409. * Sends a stream event to the dapm core. The core then makes any
  1410. * necessary widget power changes.
  1411. *
  1412. * Returns 0 for success else error.
  1413. */
  1414. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1415. char *stream, int event)
  1416. {
  1417. struct snd_soc_dapm_widget *w;
  1418. if (stream == NULL)
  1419. return 0;
  1420. mutex_lock(&codec->mutex);
  1421. list_for_each_entry(w, &codec->dapm_widgets, list)
  1422. {
  1423. if (!w->sname)
  1424. continue;
  1425. pr_debug("widget %s\n %s stream %s event %d\n",
  1426. w->name, w->sname, stream, event);
  1427. if (strstr(w->sname, stream)) {
  1428. switch(event) {
  1429. case SND_SOC_DAPM_STREAM_START:
  1430. w->active = 1;
  1431. break;
  1432. case SND_SOC_DAPM_STREAM_STOP:
  1433. w->active = 0;
  1434. break;
  1435. case SND_SOC_DAPM_STREAM_SUSPEND:
  1436. if (w->active)
  1437. w->suspend = 1;
  1438. w->active = 0;
  1439. break;
  1440. case SND_SOC_DAPM_STREAM_RESUME:
  1441. if (w->suspend) {
  1442. w->active = 1;
  1443. w->suspend = 0;
  1444. }
  1445. break;
  1446. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1447. break;
  1448. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1449. break;
  1450. }
  1451. }
  1452. }
  1453. mutex_unlock(&codec->mutex);
  1454. dapm_power_widgets(codec, event);
  1455. dump_dapm(codec, __func__);
  1456. return 0;
  1457. }
  1458. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1459. /**
  1460. * snd_soc_dapm_set_bias_level - set the bias level for the system
  1461. * @socdev: audio device
  1462. * @level: level to configure
  1463. *
  1464. * Configure the bias (power) levels for the SoC audio device.
  1465. *
  1466. * Returns 0 for success else error.
  1467. */
  1468. int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
  1469. enum snd_soc_bias_level level)
  1470. {
  1471. struct snd_soc_card *card = socdev->card;
  1472. struct snd_soc_codec *codec = socdev->card->codec;
  1473. int ret = 0;
  1474. if (card->set_bias_level)
  1475. ret = card->set_bias_level(card, level);
  1476. if (ret == 0 && codec->set_bias_level)
  1477. ret = codec->set_bias_level(codec, level);
  1478. return ret;
  1479. }
  1480. /**
  1481. * snd_soc_dapm_enable_pin - enable pin.
  1482. * @codec: SoC codec
  1483. * @pin: pin name
  1484. *
  1485. * Enables input/output pin and it's parents or children widgets iff there is
  1486. * a valid audio route and active audio stream.
  1487. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1488. * do any widget power switching.
  1489. */
  1490. int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1491. {
  1492. return snd_soc_dapm_set_pin(codec, pin, 1);
  1493. }
  1494. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1495. /**
  1496. * snd_soc_dapm_disable_pin - disable pin.
  1497. * @codec: SoC codec
  1498. * @pin: pin name
  1499. *
  1500. * Disables input/output pin and it's parents or children widgets.
  1501. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1502. * do any widget power switching.
  1503. */
  1504. int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
  1505. {
  1506. return snd_soc_dapm_set_pin(codec, pin, 0);
  1507. }
  1508. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1509. /**
  1510. * snd_soc_dapm_nc_pin - permanently disable pin.
  1511. * @codec: SoC codec
  1512. * @pin: pin name
  1513. *
  1514. * Marks the specified pin as being not connected, disabling it along
  1515. * any parent or child widgets. At present this is identical to
  1516. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1517. * additional things such as disabling controls which only affect
  1518. * paths through the pin.
  1519. *
  1520. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1521. * do any widget power switching.
  1522. */
  1523. int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
  1524. {
  1525. return snd_soc_dapm_set_pin(codec, pin, 0);
  1526. }
  1527. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1528. /**
  1529. * snd_soc_dapm_get_pin_status - get audio pin status
  1530. * @codec: audio codec
  1531. * @pin: audio signal pin endpoint (or start point)
  1532. *
  1533. * Get audio pin status - connected or disconnected.
  1534. *
  1535. * Returns 1 for connected otherwise 0.
  1536. */
  1537. int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
  1538. {
  1539. struct snd_soc_dapm_widget *w;
  1540. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1541. if (!strcmp(w->name, pin))
  1542. return w->connected;
  1543. }
  1544. return 0;
  1545. }
  1546. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1547. /**
  1548. * snd_soc_dapm_free - free dapm resources
  1549. * @socdev: SoC device
  1550. *
  1551. * Free all dapm widgets and resources.
  1552. */
  1553. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1554. {
  1555. struct snd_soc_codec *codec = socdev->card->codec;
  1556. snd_soc_dapm_sys_remove(socdev->dev);
  1557. dapm_free_widgets(codec);
  1558. }
  1559. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1560. /* Module information */
  1561. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1562. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1563. MODULE_LICENSE("GPL");