soc-dapm.c 44 KB

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