soc-dapm.c 38 KB

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