soc-dapm.c 41 KB

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