soc-dapm.c 44 KB

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