soc-dapm.c 38 KB

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