soc-dapm.c 46 KB

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