soc-dapm.c 38 KB

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