soc-dapm.c 38 KB

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