soc-dapm.c 42 KB

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