soc-dapm.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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. /* Generic check to see if a widget should be powered.
  501. */
  502. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  503. {
  504. int in, out;
  505. in = is_connected_input_ep(w);
  506. dapm_clear_walk(w->codec);
  507. out = is_connected_output_ep(w);
  508. dapm_clear_walk(w->codec);
  509. return out != 0 && in != 0;
  510. }
  511. /* Check to see if an ADC has power */
  512. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  513. {
  514. int in;
  515. if (w->active) {
  516. in = is_connected_input_ep(w);
  517. dapm_clear_walk(w->codec);
  518. return in != 0;
  519. } else {
  520. return dapm_generic_check_power(w);
  521. }
  522. }
  523. /* Check to see if a DAC has power */
  524. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  525. {
  526. int out;
  527. if (w->active) {
  528. out = is_connected_output_ep(w);
  529. dapm_clear_walk(w->codec);
  530. return out != 0;
  531. } else {
  532. return dapm_generic_check_power(w);
  533. }
  534. }
  535. /*
  536. * Scan a single DAPM widget for a complete audio path and update the
  537. * power status appropriately.
  538. */
  539. static int dapm_power_widget(struct snd_soc_codec *codec, int event,
  540. struct snd_soc_dapm_widget *w)
  541. {
  542. int power, ret;
  543. switch (w->id) {
  544. case snd_soc_dapm_pre:
  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_PRE_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_PRE_PMD);
  555. if (ret < 0)
  556. return ret;
  557. }
  558. return 0;
  559. case snd_soc_dapm_post:
  560. if (!w->event)
  561. return 0;
  562. if (event == SND_SOC_DAPM_STREAM_START) {
  563. ret = w->event(w,
  564. NULL, SND_SOC_DAPM_POST_PMU);
  565. if (ret < 0)
  566. return ret;
  567. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  568. ret = w->event(w,
  569. NULL, SND_SOC_DAPM_POST_PMD);
  570. if (ret < 0)
  571. return ret;
  572. }
  573. return 0;
  574. default:
  575. break;
  576. }
  577. if (!w->power_check)
  578. return 0;
  579. power = w->power_check(w);
  580. if (w->power == power)
  581. return 0;
  582. w->power = power;
  583. return dapm_generic_apply_power(w);
  584. }
  585. /*
  586. * Scan each dapm widget for complete audio path.
  587. * A complete path is a route that has valid endpoints i.e.:-
  588. *
  589. * o DAC to output pin.
  590. * o Input Pin to ADC.
  591. * o Input pin to Output pin (bypass, sidetone)
  592. * o DAC to ADC (loopback).
  593. */
  594. static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
  595. {
  596. struct snd_soc_dapm_widget *w;
  597. int i, c = 1, *seq = NULL, ret = 0;
  598. /* do we have a sequenced stream event */
  599. if (event == SND_SOC_DAPM_STREAM_START) {
  600. c = ARRAY_SIZE(dapm_up_seq);
  601. seq = dapm_up_seq;
  602. } else if (event == SND_SOC_DAPM_STREAM_STOP) {
  603. c = ARRAY_SIZE(dapm_down_seq);
  604. seq = dapm_down_seq;
  605. }
  606. for (i = 0; i < c; i++) {
  607. list_for_each_entry(w, &codec->dapm_widgets, list) {
  608. /* is widget in stream order */
  609. if (seq && seq[i] && w->id != seq[i])
  610. continue;
  611. ret = dapm_power_widget(codec, event, w);
  612. if (ret != 0)
  613. return ret;
  614. }
  615. }
  616. return 0;
  617. }
  618. #ifdef DEBUG
  619. static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
  620. {
  621. struct snd_soc_dapm_widget *w;
  622. struct snd_soc_dapm_path *p = NULL;
  623. int in, out;
  624. printk("DAPM %s %s\n", codec->name, action);
  625. list_for_each_entry(w, &codec->dapm_widgets, list) {
  626. /* only display widgets that effect routing */
  627. switch (w->id) {
  628. case snd_soc_dapm_pre:
  629. case snd_soc_dapm_post:
  630. case snd_soc_dapm_vmid:
  631. continue;
  632. case snd_soc_dapm_mux:
  633. case snd_soc_dapm_value_mux:
  634. case snd_soc_dapm_output:
  635. case snd_soc_dapm_input:
  636. case snd_soc_dapm_switch:
  637. case snd_soc_dapm_hp:
  638. case snd_soc_dapm_mic:
  639. case snd_soc_dapm_spk:
  640. case snd_soc_dapm_line:
  641. case snd_soc_dapm_micbias:
  642. case snd_soc_dapm_dac:
  643. case snd_soc_dapm_adc:
  644. case snd_soc_dapm_pga:
  645. case snd_soc_dapm_mixer:
  646. case snd_soc_dapm_mixer_named_ctl:
  647. if (w->name) {
  648. in = is_connected_input_ep(w);
  649. dapm_clear_walk(w->codec);
  650. out = is_connected_output_ep(w);
  651. dapm_clear_walk(w->codec);
  652. printk("%s: %s in %d out %d\n", w->name,
  653. w->power ? "On":"Off",in, out);
  654. list_for_each_entry(p, &w->sources, list_sink) {
  655. if (p->connect)
  656. printk(" in %s %s\n", p->name ? p->name : "static",
  657. p->source->name);
  658. }
  659. list_for_each_entry(p, &w->sinks, list_source) {
  660. if (p->connect)
  661. printk(" out %s %s\n", p->name ? p->name : "static",
  662. p->sink->name);
  663. }
  664. }
  665. break;
  666. }
  667. }
  668. }
  669. #endif
  670. /* test and update the power status of a mux widget */
  671. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  672. struct snd_kcontrol *kcontrol, int mask,
  673. int mux, int val, struct soc_enum *e)
  674. {
  675. struct snd_soc_dapm_path *path;
  676. int found = 0;
  677. if (widget->id != snd_soc_dapm_mux &&
  678. widget->id != snd_soc_dapm_value_mux)
  679. return -ENODEV;
  680. if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
  681. return 0;
  682. /* find dapm widget path assoc with kcontrol */
  683. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  684. if (path->kcontrol != kcontrol)
  685. continue;
  686. if (!path->name || !e->texts[mux])
  687. continue;
  688. found = 1;
  689. /* we now need to match the string in the enum to the path */
  690. if (!(strcmp(path->name, e->texts[mux])))
  691. path->connect = 1; /* new connection */
  692. else
  693. path->connect = 0; /* old connection must be powered down */
  694. }
  695. if (found) {
  696. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  697. dump_dapm(widget->codec, "mux power update");
  698. }
  699. return 0;
  700. }
  701. /* test and update the power status of a mixer or switch widget */
  702. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  703. struct snd_kcontrol *kcontrol, int reg,
  704. int val_mask, int val, int invert)
  705. {
  706. struct snd_soc_dapm_path *path;
  707. int found = 0;
  708. if (widget->id != snd_soc_dapm_mixer &&
  709. widget->id != snd_soc_dapm_mixer_named_ctl &&
  710. widget->id != snd_soc_dapm_switch)
  711. return -ENODEV;
  712. if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
  713. return 0;
  714. /* find dapm widget path assoc with kcontrol */
  715. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  716. if (path->kcontrol != kcontrol)
  717. continue;
  718. /* found, now check type */
  719. found = 1;
  720. if (val)
  721. /* new connection */
  722. path->connect = invert ? 0:1;
  723. else
  724. /* old connection must be powered down */
  725. path->connect = invert ? 1:0;
  726. break;
  727. }
  728. if (found) {
  729. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  730. dump_dapm(widget->codec, "mixer power update");
  731. }
  732. return 0;
  733. }
  734. /* show dapm widget status in sys fs */
  735. static ssize_t dapm_widget_show(struct device *dev,
  736. struct device_attribute *attr, char *buf)
  737. {
  738. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  739. struct snd_soc_codec *codec = devdata->card->codec;
  740. struct snd_soc_dapm_widget *w;
  741. int count = 0;
  742. char *state = "not set";
  743. list_for_each_entry(w, &codec->dapm_widgets, list) {
  744. /* only display widgets that burnm power */
  745. switch (w->id) {
  746. case snd_soc_dapm_hp:
  747. case snd_soc_dapm_mic:
  748. case snd_soc_dapm_spk:
  749. case snd_soc_dapm_line:
  750. case snd_soc_dapm_micbias:
  751. case snd_soc_dapm_dac:
  752. case snd_soc_dapm_adc:
  753. case snd_soc_dapm_pga:
  754. case snd_soc_dapm_mixer:
  755. case snd_soc_dapm_mixer_named_ctl:
  756. if (w->name)
  757. count += sprintf(buf + count, "%s: %s\n",
  758. w->name, w->power ? "On":"Off");
  759. break;
  760. default:
  761. break;
  762. }
  763. }
  764. switch (codec->bias_level) {
  765. case SND_SOC_BIAS_ON:
  766. state = "On";
  767. break;
  768. case SND_SOC_BIAS_PREPARE:
  769. state = "Prepare";
  770. break;
  771. case SND_SOC_BIAS_STANDBY:
  772. state = "Standby";
  773. break;
  774. case SND_SOC_BIAS_OFF:
  775. state = "Off";
  776. break;
  777. }
  778. count += sprintf(buf + count, "PM State: %s\n", state);
  779. return count;
  780. }
  781. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  782. int snd_soc_dapm_sys_add(struct device *dev)
  783. {
  784. if (!dapm_status)
  785. return 0;
  786. return device_create_file(dev, &dev_attr_dapm_widget);
  787. }
  788. static void snd_soc_dapm_sys_remove(struct device *dev)
  789. {
  790. if (dapm_status) {
  791. device_remove_file(dev, &dev_attr_dapm_widget);
  792. }
  793. }
  794. /* free all dapm widgets and resources */
  795. static void dapm_free_widgets(struct snd_soc_codec *codec)
  796. {
  797. struct snd_soc_dapm_widget *w, *next_w;
  798. struct snd_soc_dapm_path *p, *next_p;
  799. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  800. list_del(&w->list);
  801. kfree(w);
  802. }
  803. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  804. list_del(&p->list);
  805. kfree(p->long_name);
  806. kfree(p);
  807. }
  808. }
  809. static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  810. const char *pin, int status)
  811. {
  812. struct snd_soc_dapm_widget *w;
  813. list_for_each_entry(w, &codec->dapm_widgets, list) {
  814. if (!strcmp(w->name, pin)) {
  815. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  816. w->connected = status;
  817. return 0;
  818. }
  819. }
  820. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  821. return -EINVAL;
  822. }
  823. /**
  824. * snd_soc_dapm_sync - scan and power dapm paths
  825. * @codec: audio codec
  826. *
  827. * Walks all dapm audio paths and powers widgets according to their
  828. * stream or path usage.
  829. *
  830. * Returns 0 for success.
  831. */
  832. int snd_soc_dapm_sync(struct snd_soc_codec *codec)
  833. {
  834. int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  835. dump_dapm(codec, "sync");
  836. return ret;
  837. }
  838. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  839. static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
  840. const char *sink, const char *control, const char *source)
  841. {
  842. struct snd_soc_dapm_path *path;
  843. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  844. int ret = 0;
  845. /* find src and dest widgets */
  846. list_for_each_entry(w, &codec->dapm_widgets, list) {
  847. if (!wsink && !(strcmp(w->name, sink))) {
  848. wsink = w;
  849. continue;
  850. }
  851. if (!wsource && !(strcmp(w->name, source))) {
  852. wsource = w;
  853. }
  854. }
  855. if (wsource == NULL || wsink == NULL)
  856. return -ENODEV;
  857. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  858. if (!path)
  859. return -ENOMEM;
  860. path->source = wsource;
  861. path->sink = wsink;
  862. INIT_LIST_HEAD(&path->list);
  863. INIT_LIST_HEAD(&path->list_source);
  864. INIT_LIST_HEAD(&path->list_sink);
  865. /* check for external widgets */
  866. if (wsink->id == snd_soc_dapm_input) {
  867. if (wsource->id == snd_soc_dapm_micbias ||
  868. wsource->id == snd_soc_dapm_mic ||
  869. wsink->id == snd_soc_dapm_line ||
  870. wsink->id == snd_soc_dapm_output)
  871. wsink->ext = 1;
  872. }
  873. if (wsource->id == snd_soc_dapm_output) {
  874. if (wsink->id == snd_soc_dapm_spk ||
  875. wsink->id == snd_soc_dapm_hp ||
  876. wsink->id == snd_soc_dapm_line ||
  877. wsink->id == snd_soc_dapm_input)
  878. wsource->ext = 1;
  879. }
  880. /* connect static paths */
  881. if (control == NULL) {
  882. list_add(&path->list, &codec->dapm_paths);
  883. list_add(&path->list_sink, &wsink->sources);
  884. list_add(&path->list_source, &wsource->sinks);
  885. path->connect = 1;
  886. return 0;
  887. }
  888. /* connect dynamic paths */
  889. switch(wsink->id) {
  890. case snd_soc_dapm_adc:
  891. case snd_soc_dapm_dac:
  892. case snd_soc_dapm_pga:
  893. case snd_soc_dapm_input:
  894. case snd_soc_dapm_output:
  895. case snd_soc_dapm_micbias:
  896. case snd_soc_dapm_vmid:
  897. case snd_soc_dapm_pre:
  898. case snd_soc_dapm_post:
  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. case snd_soc_dapm_mux:
  905. case snd_soc_dapm_value_mux:
  906. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  907. &wsink->kcontrols[0]);
  908. if (ret != 0)
  909. goto err;
  910. break;
  911. case snd_soc_dapm_switch:
  912. case snd_soc_dapm_mixer:
  913. case snd_soc_dapm_mixer_named_ctl:
  914. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  915. if (ret != 0)
  916. goto err;
  917. break;
  918. case snd_soc_dapm_hp:
  919. case snd_soc_dapm_mic:
  920. case snd_soc_dapm_line:
  921. case snd_soc_dapm_spk:
  922. list_add(&path->list, &codec->dapm_paths);
  923. list_add(&path->list_sink, &wsink->sources);
  924. list_add(&path->list_source, &wsource->sinks);
  925. path->connect = 0;
  926. return 0;
  927. }
  928. return 0;
  929. err:
  930. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  931. control, sink);
  932. kfree(path);
  933. return ret;
  934. }
  935. /**
  936. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  937. * @codec: codec
  938. * @route: audio routes
  939. * @num: number of routes
  940. *
  941. * Connects 2 dapm widgets together via a named audio path. The sink is
  942. * the widget receiving the audio signal, whilst the source is the sender
  943. * of the audio signal.
  944. *
  945. * Returns 0 for success else error. On error all resources can be freed
  946. * with a call to snd_soc_card_free().
  947. */
  948. int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
  949. const struct snd_soc_dapm_route *route, int num)
  950. {
  951. int i, ret;
  952. for (i = 0; i < num; i++) {
  953. ret = snd_soc_dapm_add_route(codec, route->sink,
  954. route->control, route->source);
  955. if (ret < 0) {
  956. printk(KERN_ERR "Failed to add route %s->%s\n",
  957. route->source,
  958. route->sink);
  959. return ret;
  960. }
  961. route++;
  962. }
  963. return 0;
  964. }
  965. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  966. /**
  967. * snd_soc_dapm_new_widgets - add new dapm widgets
  968. * @codec: audio codec
  969. *
  970. * Checks the codec for any new dapm widgets and creates them if found.
  971. *
  972. * Returns 0 for success.
  973. */
  974. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  975. {
  976. struct snd_soc_dapm_widget *w;
  977. list_for_each_entry(w, &codec->dapm_widgets, list)
  978. {
  979. if (w->new)
  980. continue;
  981. switch(w->id) {
  982. case snd_soc_dapm_switch:
  983. case snd_soc_dapm_mixer:
  984. case snd_soc_dapm_mixer_named_ctl:
  985. w->power_check = dapm_generic_check_power;
  986. dapm_new_mixer(codec, w);
  987. break;
  988. case snd_soc_dapm_mux:
  989. case snd_soc_dapm_value_mux:
  990. w->power_check = dapm_generic_check_power;
  991. dapm_new_mux(codec, w);
  992. break;
  993. case snd_soc_dapm_adc:
  994. w->power_check = dapm_adc_check_power;
  995. break;
  996. case snd_soc_dapm_dac:
  997. w->power_check = dapm_dac_check_power;
  998. break;
  999. case snd_soc_dapm_pga:
  1000. w->power_check = dapm_generic_check_power;
  1001. dapm_new_pga(codec, w);
  1002. break;
  1003. case snd_soc_dapm_input:
  1004. case snd_soc_dapm_output:
  1005. case snd_soc_dapm_micbias:
  1006. case snd_soc_dapm_spk:
  1007. case snd_soc_dapm_hp:
  1008. case snd_soc_dapm_mic:
  1009. case snd_soc_dapm_line:
  1010. w->power_check = dapm_generic_check_power;
  1011. break;
  1012. case snd_soc_dapm_vmid:
  1013. case snd_soc_dapm_pre:
  1014. case snd_soc_dapm_post:
  1015. break;
  1016. }
  1017. w->new = 1;
  1018. }
  1019. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1020. return 0;
  1021. }
  1022. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1023. /**
  1024. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1025. * @kcontrol: mixer control
  1026. * @ucontrol: control element information
  1027. *
  1028. * Callback to get the value of a dapm mixer control.
  1029. *
  1030. * Returns 0 for success.
  1031. */
  1032. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1033. struct snd_ctl_elem_value *ucontrol)
  1034. {
  1035. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1036. struct soc_mixer_control *mc =
  1037. (struct soc_mixer_control *)kcontrol->private_value;
  1038. unsigned int reg = mc->reg;
  1039. unsigned int shift = mc->shift;
  1040. unsigned int rshift = mc->rshift;
  1041. int max = mc->max;
  1042. unsigned int invert = mc->invert;
  1043. unsigned int mask = (1 << fls(max)) - 1;
  1044. /* return the saved value if we are powered down */
  1045. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  1046. ucontrol->value.integer.value[0] = widget->saved_value;
  1047. return 0;
  1048. }
  1049. ucontrol->value.integer.value[0] =
  1050. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1051. if (shift != rshift)
  1052. ucontrol->value.integer.value[1] =
  1053. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1054. if (invert) {
  1055. ucontrol->value.integer.value[0] =
  1056. max - ucontrol->value.integer.value[0];
  1057. if (shift != rshift)
  1058. ucontrol->value.integer.value[1] =
  1059. max - ucontrol->value.integer.value[1];
  1060. }
  1061. return 0;
  1062. }
  1063. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1064. /**
  1065. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1066. * @kcontrol: mixer control
  1067. * @ucontrol: control element information
  1068. *
  1069. * Callback to set the value of a dapm mixer control.
  1070. *
  1071. * Returns 0 for success.
  1072. */
  1073. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1074. struct snd_ctl_elem_value *ucontrol)
  1075. {
  1076. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1077. struct soc_mixer_control *mc =
  1078. (struct soc_mixer_control *)kcontrol->private_value;
  1079. unsigned int reg = mc->reg;
  1080. unsigned int shift = mc->shift;
  1081. unsigned int rshift = mc->rshift;
  1082. int max = mc->max;
  1083. unsigned int mask = (1 << fls(max)) - 1;
  1084. unsigned int invert = mc->invert;
  1085. unsigned short val, val2, val_mask;
  1086. int ret;
  1087. val = (ucontrol->value.integer.value[0] & mask);
  1088. if (invert)
  1089. val = max - val;
  1090. val_mask = mask << shift;
  1091. val = val << shift;
  1092. if (shift != rshift) {
  1093. val2 = (ucontrol->value.integer.value[1] & mask);
  1094. if (invert)
  1095. val2 = max - val2;
  1096. val_mask |= mask << rshift;
  1097. val |= val2 << rshift;
  1098. }
  1099. mutex_lock(&widget->codec->mutex);
  1100. widget->value = val;
  1101. /* save volume value if the widget is powered down */
  1102. if (widget->id == snd_soc_dapm_pga && !widget->power) {
  1103. widget->saved_value = val;
  1104. mutex_unlock(&widget->codec->mutex);
  1105. return 1;
  1106. }
  1107. dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
  1108. if (widget->event) {
  1109. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1110. ret = widget->event(widget, kcontrol,
  1111. SND_SOC_DAPM_PRE_REG);
  1112. if (ret < 0) {
  1113. ret = 1;
  1114. goto out;
  1115. }
  1116. }
  1117. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1118. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1119. ret = widget->event(widget, kcontrol,
  1120. SND_SOC_DAPM_POST_REG);
  1121. } else
  1122. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1123. out:
  1124. mutex_unlock(&widget->codec->mutex);
  1125. return ret;
  1126. }
  1127. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1128. /**
  1129. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1130. * @kcontrol: mixer control
  1131. * @ucontrol: control element information
  1132. *
  1133. * Callback to get the value of a dapm enumerated double mixer control.
  1134. *
  1135. * Returns 0 for success.
  1136. */
  1137. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1138. struct snd_ctl_elem_value *ucontrol)
  1139. {
  1140. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1141. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1142. unsigned short val, bitmask;
  1143. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1144. ;
  1145. val = snd_soc_read(widget->codec, e->reg);
  1146. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1147. if (e->shift_l != e->shift_r)
  1148. ucontrol->value.enumerated.item[1] =
  1149. (val >> e->shift_r) & (bitmask - 1);
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1153. /**
  1154. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1155. * @kcontrol: mixer control
  1156. * @ucontrol: control element information
  1157. *
  1158. * Callback to set the value of a dapm enumerated double mixer control.
  1159. *
  1160. * Returns 0 for success.
  1161. */
  1162. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1163. struct snd_ctl_elem_value *ucontrol)
  1164. {
  1165. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1166. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1167. unsigned short val, mux;
  1168. unsigned short mask, bitmask;
  1169. int ret = 0;
  1170. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1171. ;
  1172. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1173. return -EINVAL;
  1174. mux = ucontrol->value.enumerated.item[0];
  1175. val = mux << e->shift_l;
  1176. mask = (bitmask - 1) << e->shift_l;
  1177. if (e->shift_l != e->shift_r) {
  1178. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1179. return -EINVAL;
  1180. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1181. mask |= (bitmask - 1) << e->shift_r;
  1182. }
  1183. mutex_lock(&widget->codec->mutex);
  1184. widget->value = val;
  1185. dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
  1186. if (widget->event) {
  1187. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1188. ret = widget->event(widget,
  1189. kcontrol, SND_SOC_DAPM_PRE_REG);
  1190. if (ret < 0)
  1191. goto out;
  1192. }
  1193. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1194. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1195. ret = widget->event(widget,
  1196. kcontrol, SND_SOC_DAPM_POST_REG);
  1197. } else
  1198. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1199. out:
  1200. mutex_unlock(&widget->codec->mutex);
  1201. return ret;
  1202. }
  1203. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1204. /**
  1205. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1206. * callback
  1207. * @kcontrol: mixer control
  1208. * @ucontrol: control element information
  1209. *
  1210. * Callback to get the value of a dapm semi enumerated double mixer control.
  1211. *
  1212. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1213. * used for handling bitfield coded enumeration for example.
  1214. *
  1215. * Returns 0 for success.
  1216. */
  1217. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1218. struct snd_ctl_elem_value *ucontrol)
  1219. {
  1220. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1221. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1222. unsigned short reg_val, val, mux;
  1223. reg_val = snd_soc_read(widget->codec, e->reg);
  1224. val = (reg_val >> e->shift_l) & e->mask;
  1225. for (mux = 0; mux < e->max; mux++) {
  1226. if (val == e->values[mux])
  1227. break;
  1228. }
  1229. ucontrol->value.enumerated.item[0] = mux;
  1230. if (e->shift_l != e->shift_r) {
  1231. val = (reg_val >> e->shift_r) & e->mask;
  1232. for (mux = 0; mux < e->max; mux++) {
  1233. if (val == e->values[mux])
  1234. break;
  1235. }
  1236. ucontrol->value.enumerated.item[1] = mux;
  1237. }
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1241. /**
  1242. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1243. * callback
  1244. * @kcontrol: mixer control
  1245. * @ucontrol: control element information
  1246. *
  1247. * Callback to set the value of a dapm semi enumerated double mixer control.
  1248. *
  1249. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1250. * used for handling bitfield coded enumeration for example.
  1251. *
  1252. * Returns 0 for success.
  1253. */
  1254. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1255. struct snd_ctl_elem_value *ucontrol)
  1256. {
  1257. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1258. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1259. unsigned short val, mux;
  1260. unsigned short mask;
  1261. int ret = 0;
  1262. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1263. return -EINVAL;
  1264. mux = ucontrol->value.enumerated.item[0];
  1265. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1266. mask = e->mask << e->shift_l;
  1267. if (e->shift_l != e->shift_r) {
  1268. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1269. return -EINVAL;
  1270. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1271. mask |= e->mask << e->shift_r;
  1272. }
  1273. mutex_lock(&widget->codec->mutex);
  1274. widget->value = val;
  1275. dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
  1276. if (widget->event) {
  1277. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1278. ret = widget->event(widget,
  1279. kcontrol, SND_SOC_DAPM_PRE_REG);
  1280. if (ret < 0)
  1281. goto out;
  1282. }
  1283. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1284. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1285. ret = widget->event(widget,
  1286. kcontrol, SND_SOC_DAPM_POST_REG);
  1287. } else
  1288. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1289. out:
  1290. mutex_unlock(&widget->codec->mutex);
  1291. return ret;
  1292. }
  1293. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1294. /**
  1295. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1296. *
  1297. * @kcontrol: mixer control
  1298. * @uinfo: control element information
  1299. *
  1300. * Callback to provide information about a pin switch control.
  1301. */
  1302. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1303. struct snd_ctl_elem_info *uinfo)
  1304. {
  1305. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1306. uinfo->count = 1;
  1307. uinfo->value.integer.min = 0;
  1308. uinfo->value.integer.max = 1;
  1309. return 0;
  1310. }
  1311. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1312. /**
  1313. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1314. *
  1315. * @kcontrol: mixer control
  1316. * @ucontrol: Value
  1317. */
  1318. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1319. struct snd_ctl_elem_value *ucontrol)
  1320. {
  1321. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1322. const char *pin = (const char *)kcontrol->private_value;
  1323. mutex_lock(&codec->mutex);
  1324. ucontrol->value.integer.value[0] =
  1325. snd_soc_dapm_get_pin_status(codec, pin);
  1326. mutex_unlock(&codec->mutex);
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1330. /**
  1331. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1332. *
  1333. * @kcontrol: mixer control
  1334. * @ucontrol: Value
  1335. */
  1336. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1337. struct snd_ctl_elem_value *ucontrol)
  1338. {
  1339. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1340. const char *pin = (const char *)kcontrol->private_value;
  1341. mutex_lock(&codec->mutex);
  1342. if (ucontrol->value.integer.value[0])
  1343. snd_soc_dapm_enable_pin(codec, pin);
  1344. else
  1345. snd_soc_dapm_disable_pin(codec, pin);
  1346. snd_soc_dapm_sync(codec);
  1347. mutex_unlock(&codec->mutex);
  1348. return 0;
  1349. }
  1350. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1351. /**
  1352. * snd_soc_dapm_new_control - create new dapm control
  1353. * @codec: audio codec
  1354. * @widget: widget template
  1355. *
  1356. * Creates a new dapm control based upon the template.
  1357. *
  1358. * Returns 0 for success else error.
  1359. */
  1360. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1361. const struct snd_soc_dapm_widget *widget)
  1362. {
  1363. struct snd_soc_dapm_widget *w;
  1364. if ((w = dapm_cnew_widget(widget)) == NULL)
  1365. return -ENOMEM;
  1366. w->codec = codec;
  1367. INIT_LIST_HEAD(&w->sources);
  1368. INIT_LIST_HEAD(&w->sinks);
  1369. INIT_LIST_HEAD(&w->list);
  1370. list_add(&w->list, &codec->dapm_widgets);
  1371. /* machine layer set ups unconnected pins and insertions */
  1372. w->connected = 1;
  1373. return 0;
  1374. }
  1375. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1376. /**
  1377. * snd_soc_dapm_new_controls - create new dapm controls
  1378. * @codec: audio codec
  1379. * @widget: widget array
  1380. * @num: number of widgets
  1381. *
  1382. * Creates new DAPM controls based upon the templates.
  1383. *
  1384. * Returns 0 for success else error.
  1385. */
  1386. int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
  1387. const struct snd_soc_dapm_widget *widget,
  1388. int num)
  1389. {
  1390. int i, ret;
  1391. for (i = 0; i < num; i++) {
  1392. ret = snd_soc_dapm_new_control(codec, widget);
  1393. if (ret < 0) {
  1394. printk(KERN_ERR
  1395. "ASoC: Failed to create DAPM control %s: %d\n",
  1396. widget->name, ret);
  1397. return ret;
  1398. }
  1399. widget++;
  1400. }
  1401. return 0;
  1402. }
  1403. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1404. /**
  1405. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1406. * @codec: audio codec
  1407. * @stream: stream name
  1408. * @event: stream event
  1409. *
  1410. * Sends a stream event to the dapm core. The core then makes any
  1411. * necessary widget power changes.
  1412. *
  1413. * Returns 0 for success else error.
  1414. */
  1415. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1416. char *stream, int event)
  1417. {
  1418. struct snd_soc_dapm_widget *w;
  1419. if (stream == NULL)
  1420. return 0;
  1421. mutex_lock(&codec->mutex);
  1422. list_for_each_entry(w, &codec->dapm_widgets, list)
  1423. {
  1424. if (!w->sname)
  1425. continue;
  1426. pr_debug("widget %s\n %s stream %s event %d\n",
  1427. w->name, w->sname, stream, event);
  1428. if (strstr(w->sname, stream)) {
  1429. switch(event) {
  1430. case SND_SOC_DAPM_STREAM_START:
  1431. w->active = 1;
  1432. break;
  1433. case SND_SOC_DAPM_STREAM_STOP:
  1434. w->active = 0;
  1435. break;
  1436. case SND_SOC_DAPM_STREAM_SUSPEND:
  1437. if (w->active)
  1438. w->suspend = 1;
  1439. w->active = 0;
  1440. break;
  1441. case SND_SOC_DAPM_STREAM_RESUME:
  1442. if (w->suspend) {
  1443. w->active = 1;
  1444. w->suspend = 0;
  1445. }
  1446. break;
  1447. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1448. break;
  1449. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1450. break;
  1451. }
  1452. }
  1453. }
  1454. mutex_unlock(&codec->mutex);
  1455. dapm_power_widgets(codec, event);
  1456. dump_dapm(codec, __func__);
  1457. return 0;
  1458. }
  1459. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1460. /**
  1461. * snd_soc_dapm_set_bias_level - set the bias level for the system
  1462. * @socdev: audio device
  1463. * @level: level to configure
  1464. *
  1465. * Configure the bias (power) levels for the SoC audio device.
  1466. *
  1467. * Returns 0 for success else error.
  1468. */
  1469. int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
  1470. enum snd_soc_bias_level level)
  1471. {
  1472. struct snd_soc_card *card = socdev->card;
  1473. struct snd_soc_codec *codec = socdev->card->codec;
  1474. int ret = 0;
  1475. if (card->set_bias_level)
  1476. ret = card->set_bias_level(card, level);
  1477. if (ret == 0 && codec->set_bias_level)
  1478. ret = codec->set_bias_level(codec, level);
  1479. return ret;
  1480. }
  1481. /**
  1482. * snd_soc_dapm_enable_pin - enable pin.
  1483. * @codec: SoC codec
  1484. * @pin: pin name
  1485. *
  1486. * Enables input/output pin and it's parents or children widgets iff there is
  1487. * a valid audio route and active audio stream.
  1488. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1489. * do any widget power switching.
  1490. */
  1491. int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1492. {
  1493. return snd_soc_dapm_set_pin(codec, pin, 1);
  1494. }
  1495. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1496. /**
  1497. * snd_soc_dapm_disable_pin - disable pin.
  1498. * @codec: SoC codec
  1499. * @pin: pin name
  1500. *
  1501. * Disables input/output pin and it's parents or children widgets.
  1502. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1503. * do any widget power switching.
  1504. */
  1505. int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
  1506. {
  1507. return snd_soc_dapm_set_pin(codec, pin, 0);
  1508. }
  1509. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1510. /**
  1511. * snd_soc_dapm_nc_pin - permanently disable pin.
  1512. * @codec: SoC codec
  1513. * @pin: pin name
  1514. *
  1515. * Marks the specified pin as being not connected, disabling it along
  1516. * any parent or child widgets. At present this is identical to
  1517. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1518. * additional things such as disabling controls which only affect
  1519. * paths through the pin.
  1520. *
  1521. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1522. * do any widget power switching.
  1523. */
  1524. int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
  1525. {
  1526. return snd_soc_dapm_set_pin(codec, pin, 0);
  1527. }
  1528. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1529. /**
  1530. * snd_soc_dapm_get_pin_status - get audio pin status
  1531. * @codec: audio codec
  1532. * @pin: audio signal pin endpoint (or start point)
  1533. *
  1534. * Get audio pin status - connected or disconnected.
  1535. *
  1536. * Returns 1 for connected otherwise 0.
  1537. */
  1538. int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
  1539. {
  1540. struct snd_soc_dapm_widget *w;
  1541. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1542. if (!strcmp(w->name, pin))
  1543. return w->connected;
  1544. }
  1545. return 0;
  1546. }
  1547. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1548. /**
  1549. * snd_soc_dapm_free - free dapm resources
  1550. * @socdev: SoC device
  1551. *
  1552. * Free all dapm widgets and resources.
  1553. */
  1554. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1555. {
  1556. struct snd_soc_codec *codec = socdev->card->codec;
  1557. snd_soc_dapm_sys_remove(socdev->dev);
  1558. dapm_free_widgets(codec);
  1559. }
  1560. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1561. /* Module information */
  1562. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1563. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1564. MODULE_LICENSE("GPL");