soc-dapm.c 37 KB

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