soc-dapm.c 44 KB

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