soc-dapm.c 38 KB

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