soc-dapm.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  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. * DACs/ADCs.
  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 <linux/debugfs.h>
  40. #include <sound/core.h>
  41. #include <sound/pcm.h>
  42. #include <sound/pcm_params.h>
  43. #include <sound/soc-dapm.h>
  44. #include <sound/initval.h>
  45. /* dapm power sequences - make this per codec in the future */
  46. static int dapm_up_seq[] = {
  47. [snd_soc_dapm_pre] = 0,
  48. [snd_soc_dapm_supply] = 1,
  49. [snd_soc_dapm_micbias] = 2,
  50. [snd_soc_dapm_aif_in] = 3,
  51. [snd_soc_dapm_aif_out] = 3,
  52. [snd_soc_dapm_mic] = 4,
  53. [snd_soc_dapm_mux] = 5,
  54. [snd_soc_dapm_value_mux] = 5,
  55. [snd_soc_dapm_dac] = 6,
  56. [snd_soc_dapm_mixer] = 7,
  57. [snd_soc_dapm_mixer_named_ctl] = 7,
  58. [snd_soc_dapm_pga] = 8,
  59. [snd_soc_dapm_adc] = 9,
  60. [snd_soc_dapm_hp] = 10,
  61. [snd_soc_dapm_spk] = 10,
  62. [snd_soc_dapm_post] = 11,
  63. };
  64. static int dapm_down_seq[] = {
  65. [snd_soc_dapm_pre] = 0,
  66. [snd_soc_dapm_adc] = 1,
  67. [snd_soc_dapm_hp] = 2,
  68. [snd_soc_dapm_spk] = 2,
  69. [snd_soc_dapm_pga] = 4,
  70. [snd_soc_dapm_mixer_named_ctl] = 5,
  71. [snd_soc_dapm_mixer] = 5,
  72. [snd_soc_dapm_dac] = 6,
  73. [snd_soc_dapm_mic] = 7,
  74. [snd_soc_dapm_micbias] = 8,
  75. [snd_soc_dapm_mux] = 9,
  76. [snd_soc_dapm_value_mux] = 9,
  77. [snd_soc_dapm_aif_in] = 10,
  78. [snd_soc_dapm_aif_out] = 10,
  79. [snd_soc_dapm_supply] = 11,
  80. [snd_soc_dapm_post] = 12,
  81. };
  82. static void pop_wait(u32 pop_time)
  83. {
  84. if (pop_time)
  85. schedule_timeout_uninterruptible(msecs_to_jiffies(pop_time));
  86. }
  87. static void pop_dbg(u32 pop_time, const char *fmt, ...)
  88. {
  89. va_list args;
  90. va_start(args, fmt);
  91. if (pop_time) {
  92. vprintk(fmt, args);
  93. }
  94. va_end(args);
  95. }
  96. /* create a new dapm widget */
  97. static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
  98. const struct snd_soc_dapm_widget *_widget)
  99. {
  100. return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
  101. }
  102. /**
  103. * snd_soc_dapm_set_bias_level - set the bias level for the system
  104. * @socdev: audio device
  105. * @level: level to configure
  106. *
  107. * Configure the bias (power) levels for the SoC audio device.
  108. *
  109. * Returns 0 for success else error.
  110. */
  111. static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
  112. enum snd_soc_bias_level level)
  113. {
  114. struct snd_soc_card *card = socdev->card;
  115. struct snd_soc_codec *codec = socdev->card->codec;
  116. int ret = 0;
  117. switch (level) {
  118. case SND_SOC_BIAS_ON:
  119. dev_dbg(socdev->dev, "Setting full bias\n");
  120. break;
  121. case SND_SOC_BIAS_PREPARE:
  122. dev_dbg(socdev->dev, "Setting bias prepare\n");
  123. break;
  124. case SND_SOC_BIAS_STANDBY:
  125. dev_dbg(socdev->dev, "Setting standby bias\n");
  126. break;
  127. case SND_SOC_BIAS_OFF:
  128. dev_dbg(socdev->dev, "Setting bias off\n");
  129. break;
  130. default:
  131. dev_err(socdev->dev, "Setting invalid bias %d\n", level);
  132. return -EINVAL;
  133. }
  134. if (card->set_bias_level)
  135. ret = card->set_bias_level(card, level);
  136. if (ret == 0) {
  137. if (codec->set_bias_level)
  138. ret = codec->set_bias_level(codec, level);
  139. else
  140. codec->bias_level = level;
  141. }
  142. return ret;
  143. }
  144. /* set up initial codec paths */
  145. static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
  146. struct snd_soc_dapm_path *p, int i)
  147. {
  148. switch (w->id) {
  149. case snd_soc_dapm_switch:
  150. case snd_soc_dapm_mixer:
  151. case snd_soc_dapm_mixer_named_ctl: {
  152. int val;
  153. struct soc_mixer_control *mc = (struct soc_mixer_control *)
  154. w->kcontrols[i].private_value;
  155. unsigned int reg = mc->reg;
  156. unsigned int shift = mc->shift;
  157. int max = mc->max;
  158. unsigned int mask = (1 << fls(max)) - 1;
  159. unsigned int invert = mc->invert;
  160. val = snd_soc_read(w->codec, reg);
  161. val = (val >> shift) & mask;
  162. if ((invert && !val) || (!invert && val))
  163. p->connect = 1;
  164. else
  165. p->connect = 0;
  166. }
  167. break;
  168. case snd_soc_dapm_mux: {
  169. struct soc_enum *e = (struct soc_enum *)w->kcontrols[i].private_value;
  170. int val, item, bitmask;
  171. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  172. ;
  173. val = snd_soc_read(w->codec, e->reg);
  174. item = (val >> e->shift_l) & (bitmask - 1);
  175. p->connect = 0;
  176. for (i = 0; i < e->max; i++) {
  177. if (!(strcmp(p->name, e->texts[i])) && item == i)
  178. p->connect = 1;
  179. }
  180. }
  181. break;
  182. case snd_soc_dapm_value_mux: {
  183. struct soc_enum *e = (struct soc_enum *)
  184. w->kcontrols[i].private_value;
  185. int val, item;
  186. val = snd_soc_read(w->codec, e->reg);
  187. val = (val >> e->shift_l) & e->mask;
  188. for (item = 0; item < e->max; item++) {
  189. if (val == e->values[item])
  190. break;
  191. }
  192. p->connect = 0;
  193. for (i = 0; i < e->max; i++) {
  194. if (!(strcmp(p->name, e->texts[i])) && item == i)
  195. p->connect = 1;
  196. }
  197. }
  198. break;
  199. /* does not effect routing - always connected */
  200. case snd_soc_dapm_pga:
  201. case snd_soc_dapm_output:
  202. case snd_soc_dapm_adc:
  203. case snd_soc_dapm_input:
  204. case snd_soc_dapm_dac:
  205. case snd_soc_dapm_micbias:
  206. case snd_soc_dapm_vmid:
  207. case snd_soc_dapm_supply:
  208. case snd_soc_dapm_aif_in:
  209. case snd_soc_dapm_aif_out:
  210. p->connect = 1;
  211. break;
  212. /* does effect routing - dynamically connected */
  213. case snd_soc_dapm_hp:
  214. case snd_soc_dapm_mic:
  215. case snd_soc_dapm_spk:
  216. case snd_soc_dapm_line:
  217. case snd_soc_dapm_pre:
  218. case snd_soc_dapm_post:
  219. p->connect = 0;
  220. break;
  221. }
  222. }
  223. /* connect mux widget to its interconnecting audio paths */
  224. static int dapm_connect_mux(struct snd_soc_codec *codec,
  225. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  226. struct snd_soc_dapm_path *path, const char *control_name,
  227. const struct snd_kcontrol_new *kcontrol)
  228. {
  229. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  230. int i;
  231. for (i = 0; i < e->max; i++) {
  232. if (!(strcmp(control_name, e->texts[i]))) {
  233. list_add(&path->list, &codec->dapm_paths);
  234. list_add(&path->list_sink, &dest->sources);
  235. list_add(&path->list_source, &src->sinks);
  236. path->name = (char*)e->texts[i];
  237. dapm_set_path_status(dest, path, 0);
  238. return 0;
  239. }
  240. }
  241. return -ENODEV;
  242. }
  243. /* connect mixer widget to its interconnecting audio paths */
  244. static int dapm_connect_mixer(struct snd_soc_codec *codec,
  245. struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
  246. struct snd_soc_dapm_path *path, const char *control_name)
  247. {
  248. int i;
  249. /* search for mixer kcontrol */
  250. for (i = 0; i < dest->num_kcontrols; i++) {
  251. if (!strcmp(control_name, dest->kcontrols[i].name)) {
  252. list_add(&path->list, &codec->dapm_paths);
  253. list_add(&path->list_sink, &dest->sources);
  254. list_add(&path->list_source, &src->sinks);
  255. path->name = dest->kcontrols[i].name;
  256. dapm_set_path_status(dest, path, i);
  257. return 0;
  258. }
  259. }
  260. return -ENODEV;
  261. }
  262. /* update dapm codec register bits */
  263. static int dapm_update_bits(struct snd_soc_dapm_widget *widget)
  264. {
  265. int change, power;
  266. unsigned int old, new;
  267. struct snd_soc_codec *codec = widget->codec;
  268. /* check for valid widgets */
  269. if (widget->reg < 0 || widget->id == snd_soc_dapm_input ||
  270. widget->id == snd_soc_dapm_output ||
  271. widget->id == snd_soc_dapm_hp ||
  272. widget->id == snd_soc_dapm_mic ||
  273. widget->id == snd_soc_dapm_line ||
  274. widget->id == snd_soc_dapm_spk)
  275. return 0;
  276. power = widget->power;
  277. if (widget->invert)
  278. power = (power ? 0:1);
  279. old = snd_soc_read(codec, widget->reg);
  280. new = (old & ~(0x1 << widget->shift)) | (power << widget->shift);
  281. change = old != new;
  282. if (change) {
  283. pop_dbg(codec->pop_time, "pop test %s : %s in %d ms\n",
  284. widget->name, widget->power ? "on" : "off",
  285. codec->pop_time);
  286. pop_wait(codec->pop_time);
  287. snd_soc_write(codec, widget->reg, new);
  288. }
  289. pr_debug("reg %x old %x new %x change %d\n", widget->reg,
  290. old, new, change);
  291. return change;
  292. }
  293. /* create new dapm mixer control */
  294. static int dapm_new_mixer(struct snd_soc_codec *codec,
  295. struct snd_soc_dapm_widget *w)
  296. {
  297. int i, ret = 0;
  298. size_t name_len;
  299. struct snd_soc_dapm_path *path;
  300. /* add kcontrol */
  301. for (i = 0; i < w->num_kcontrols; i++) {
  302. /* match name */
  303. list_for_each_entry(path, &w->sources, list_sink) {
  304. /* mixer/mux paths name must match control name */
  305. if (path->name != (char*)w->kcontrols[i].name)
  306. continue;
  307. /* add dapm control with long name.
  308. * for dapm_mixer this is the concatenation of the
  309. * mixer and kcontrol name.
  310. * for dapm_mixer_named_ctl this is simply the
  311. * kcontrol name.
  312. */
  313. name_len = strlen(w->kcontrols[i].name) + 1;
  314. if (w->id != snd_soc_dapm_mixer_named_ctl)
  315. name_len += 1 + strlen(w->name);
  316. path->long_name = kmalloc(name_len, GFP_KERNEL);
  317. if (path->long_name == NULL)
  318. return -ENOMEM;
  319. switch (w->id) {
  320. default:
  321. snprintf(path->long_name, name_len, "%s %s",
  322. w->name, w->kcontrols[i].name);
  323. break;
  324. case snd_soc_dapm_mixer_named_ctl:
  325. snprintf(path->long_name, name_len, "%s",
  326. w->kcontrols[i].name);
  327. break;
  328. }
  329. path->long_name[name_len - 1] = '\0';
  330. path->kcontrol = snd_soc_cnew(&w->kcontrols[i], w,
  331. path->long_name);
  332. ret = snd_ctl_add(codec->card, path->kcontrol);
  333. if (ret < 0) {
  334. printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
  335. path->long_name,
  336. ret);
  337. kfree(path->long_name);
  338. path->long_name = NULL;
  339. return ret;
  340. }
  341. }
  342. }
  343. return ret;
  344. }
  345. /* create new dapm mux control */
  346. static int dapm_new_mux(struct snd_soc_codec *codec,
  347. struct snd_soc_dapm_widget *w)
  348. {
  349. struct snd_soc_dapm_path *path = NULL;
  350. struct snd_kcontrol *kcontrol;
  351. int ret = 0;
  352. if (!w->num_kcontrols) {
  353. printk(KERN_ERR "asoc: mux %s has no controls\n", w->name);
  354. return -EINVAL;
  355. }
  356. kcontrol = snd_soc_cnew(&w->kcontrols[0], w, w->name);
  357. ret = snd_ctl_add(codec->card, kcontrol);
  358. if (ret < 0)
  359. goto err;
  360. list_for_each_entry(path, &w->sources, list_sink)
  361. path->kcontrol = kcontrol;
  362. return ret;
  363. err:
  364. printk(KERN_ERR "asoc: failed to add kcontrol %s\n", w->name);
  365. return ret;
  366. }
  367. /* create new dapm volume control */
  368. static int dapm_new_pga(struct snd_soc_codec *codec,
  369. struct snd_soc_dapm_widget *w)
  370. {
  371. if (w->num_kcontrols)
  372. pr_err("asoc: PGA controls not supported: '%s'\n", w->name);
  373. return 0;
  374. }
  375. /* reset 'walked' bit for each dapm path */
  376. static inline void dapm_clear_walk(struct snd_soc_codec *codec)
  377. {
  378. struct snd_soc_dapm_path *p;
  379. list_for_each_entry(p, &codec->dapm_paths, list)
  380. p->walked = 0;
  381. }
  382. /*
  383. * Recursively check for a completed path to an active or physically connected
  384. * output widget. Returns number of complete paths.
  385. */
  386. static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
  387. {
  388. struct snd_soc_dapm_path *path;
  389. int con = 0;
  390. if (widget->id == snd_soc_dapm_supply)
  391. return 0;
  392. switch (widget->id) {
  393. case snd_soc_dapm_adc:
  394. case snd_soc_dapm_aif_out:
  395. if (widget->active)
  396. return 1;
  397. default:
  398. break;
  399. }
  400. if (widget->connected) {
  401. /* connected pin ? */
  402. if (widget->id == snd_soc_dapm_output && !widget->ext)
  403. return 1;
  404. /* connected jack or spk ? */
  405. if (widget->id == snd_soc_dapm_hp || widget->id == snd_soc_dapm_spk ||
  406. (widget->id == snd_soc_dapm_line && !list_empty(&widget->sources)))
  407. return 1;
  408. }
  409. list_for_each_entry(path, &widget->sinks, list_source) {
  410. if (path->walked)
  411. continue;
  412. if (path->sink && path->connect) {
  413. path->walked = 1;
  414. con += is_connected_output_ep(path->sink);
  415. }
  416. }
  417. return con;
  418. }
  419. /*
  420. * Recursively check for a completed path to an active or physically connected
  421. * input widget. Returns number of complete paths.
  422. */
  423. static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
  424. {
  425. struct snd_soc_dapm_path *path;
  426. int con = 0;
  427. if (widget->id == snd_soc_dapm_supply)
  428. return 0;
  429. /* active stream ? */
  430. switch (widget->id) {
  431. case snd_soc_dapm_dac:
  432. case snd_soc_dapm_aif_in:
  433. if (widget->active)
  434. return 1;
  435. default:
  436. break;
  437. }
  438. if (widget->connected) {
  439. /* connected pin ? */
  440. if (widget->id == snd_soc_dapm_input && !widget->ext)
  441. return 1;
  442. /* connected VMID/Bias for lower pops */
  443. if (widget->id == snd_soc_dapm_vmid)
  444. return 1;
  445. /* connected jack ? */
  446. if (widget->id == snd_soc_dapm_mic ||
  447. (widget->id == snd_soc_dapm_line && !list_empty(&widget->sinks)))
  448. return 1;
  449. }
  450. list_for_each_entry(path, &widget->sources, list_sink) {
  451. if (path->walked)
  452. continue;
  453. if (path->source && path->connect) {
  454. path->walked = 1;
  455. con += is_connected_input_ep(path->source);
  456. }
  457. }
  458. return con;
  459. }
  460. /*
  461. * Handler for generic register modifier widget.
  462. */
  463. int dapm_reg_event(struct snd_soc_dapm_widget *w,
  464. struct snd_kcontrol *kcontrol, int event)
  465. {
  466. unsigned int val;
  467. if (SND_SOC_DAPM_EVENT_ON(event))
  468. val = w->on_val;
  469. else
  470. val = w->off_val;
  471. snd_soc_update_bits(w->codec, -(w->reg + 1),
  472. w->mask << w->shift, val << w->shift);
  473. return 0;
  474. }
  475. EXPORT_SYMBOL_GPL(dapm_reg_event);
  476. /* Standard power change method, used to apply power changes to most
  477. * widgets.
  478. */
  479. static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
  480. {
  481. int ret;
  482. /* call any power change event handlers */
  483. if (w->event)
  484. pr_debug("power %s event for %s flags %x\n",
  485. w->power ? "on" : "off",
  486. w->name, w->event_flags);
  487. /* power up pre event */
  488. if (w->power && w->event &&
  489. (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
  490. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
  491. if (ret < 0)
  492. return ret;
  493. }
  494. /* power down pre event */
  495. if (!w->power && w->event &&
  496. (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
  497. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
  498. if (ret < 0)
  499. return ret;
  500. }
  501. dapm_update_bits(w);
  502. /* power up post event */
  503. if (w->power && w->event &&
  504. (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
  505. ret = w->event(w,
  506. NULL, SND_SOC_DAPM_POST_PMU);
  507. if (ret < 0)
  508. return ret;
  509. }
  510. /* power down post event */
  511. if (!w->power && w->event &&
  512. (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
  513. ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
  514. if (ret < 0)
  515. return ret;
  516. }
  517. return 0;
  518. }
  519. /* Generic check to see if a widget should be powered.
  520. */
  521. static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
  522. {
  523. int in, out;
  524. in = is_connected_input_ep(w);
  525. dapm_clear_walk(w->codec);
  526. out = is_connected_output_ep(w);
  527. dapm_clear_walk(w->codec);
  528. return out != 0 && in != 0;
  529. }
  530. /* Check to see if an ADC has power */
  531. static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
  532. {
  533. int in;
  534. if (w->active) {
  535. in = is_connected_input_ep(w);
  536. dapm_clear_walk(w->codec);
  537. return in != 0;
  538. } else {
  539. return dapm_generic_check_power(w);
  540. }
  541. }
  542. /* Check to see if a DAC has power */
  543. static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
  544. {
  545. int out;
  546. if (w->active) {
  547. out = is_connected_output_ep(w);
  548. dapm_clear_walk(w->codec);
  549. return out != 0;
  550. } else {
  551. return dapm_generic_check_power(w);
  552. }
  553. }
  554. /* Check to see if a power supply is needed */
  555. static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
  556. {
  557. struct snd_soc_dapm_path *path;
  558. int power = 0;
  559. /* Check if one of our outputs is connected */
  560. list_for_each_entry(path, &w->sinks, list_source) {
  561. if (path->connected &&
  562. !path->connected(path->source, path->sink))
  563. continue;
  564. if (path->sink && path->sink->power_check &&
  565. path->sink->power_check(path->sink)) {
  566. power = 1;
  567. break;
  568. }
  569. }
  570. dapm_clear_walk(w->codec);
  571. return power;
  572. }
  573. static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
  574. struct snd_soc_dapm_widget *b,
  575. int sort[])
  576. {
  577. if (a->codec != b->codec)
  578. return (unsigned long)a - (unsigned long)b;
  579. if (sort[a->id] != sort[b->id])
  580. return sort[a->id] - sort[b->id];
  581. if (a->reg != b->reg)
  582. return a->reg - b->reg;
  583. return 0;
  584. }
  585. /* Insert a widget in order into a DAPM power sequence. */
  586. static void dapm_seq_insert(struct snd_soc_dapm_widget *new_widget,
  587. struct list_head *list,
  588. int sort[])
  589. {
  590. struct snd_soc_dapm_widget *w;
  591. list_for_each_entry(w, list, power_list)
  592. if (dapm_seq_compare(new_widget, w, sort) < 0) {
  593. list_add_tail(&new_widget->power_list, &w->power_list);
  594. return;
  595. }
  596. list_add_tail(&new_widget->power_list, list);
  597. }
  598. /* Apply the coalesced changes from a DAPM sequence */
  599. static void dapm_seq_run_coalesced(struct snd_soc_codec *codec,
  600. struct list_head *pending)
  601. {
  602. struct snd_soc_dapm_widget *w;
  603. int reg, power, ret;
  604. unsigned int value = 0;
  605. unsigned int mask = 0;
  606. unsigned int cur_mask;
  607. reg = list_first_entry(pending, struct snd_soc_dapm_widget,
  608. power_list)->reg;
  609. list_for_each_entry(w, pending, power_list) {
  610. cur_mask = 1 << w->shift;
  611. BUG_ON(reg != w->reg);
  612. if (w->invert)
  613. power = !w->power;
  614. else
  615. power = w->power;
  616. mask |= cur_mask;
  617. if (power)
  618. value |= cur_mask;
  619. pop_dbg(codec->pop_time,
  620. "pop test : Queue %s: reg=0x%x, 0x%x/0x%x\n",
  621. w->name, reg, value, mask);
  622. /* power up pre event */
  623. if (w->power && w->event &&
  624. (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
  625. pop_dbg(codec->pop_time, "pop test : %s PRE_PMU\n",
  626. w->name);
  627. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
  628. if (ret < 0)
  629. pr_err("%s: pre event failed: %d\n",
  630. w->name, ret);
  631. }
  632. /* power down pre event */
  633. if (!w->power && w->event &&
  634. (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
  635. pop_dbg(codec->pop_time, "pop test : %s PRE_PMD\n",
  636. w->name);
  637. ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
  638. if (ret < 0)
  639. pr_err("%s: pre event failed: %d\n",
  640. w->name, ret);
  641. }
  642. }
  643. if (reg >= 0) {
  644. pop_dbg(codec->pop_time,
  645. "pop test : Applying 0x%x/0x%x to %x in %dms\n",
  646. value, mask, reg, codec->pop_time);
  647. pop_wait(codec->pop_time);
  648. snd_soc_update_bits(codec, reg, mask, value);
  649. }
  650. list_for_each_entry(w, pending, power_list) {
  651. /* power up post event */
  652. if (w->power && w->event &&
  653. (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
  654. pop_dbg(codec->pop_time, "pop test : %s POST_PMU\n",
  655. w->name);
  656. ret = w->event(w,
  657. NULL, SND_SOC_DAPM_POST_PMU);
  658. if (ret < 0)
  659. pr_err("%s: post event failed: %d\n",
  660. w->name, ret);
  661. }
  662. /* power down post event */
  663. if (!w->power && w->event &&
  664. (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
  665. pop_dbg(codec->pop_time, "pop test : %s POST_PMD\n",
  666. w->name);
  667. ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
  668. if (ret < 0)
  669. pr_err("%s: post event failed: %d\n",
  670. w->name, ret);
  671. }
  672. }
  673. }
  674. /* Apply a DAPM power sequence.
  675. *
  676. * We walk over a pre-sorted list of widgets to apply power to. In
  677. * order to minimise the number of writes to the device required
  678. * multiple widgets will be updated in a single write where possible.
  679. * Currently anything that requires more than a single write is not
  680. * handled.
  681. */
  682. static void dapm_seq_run(struct snd_soc_codec *codec, struct list_head *list,
  683. int event, int sort[])
  684. {
  685. struct snd_soc_dapm_widget *w, *n;
  686. LIST_HEAD(pending);
  687. int cur_sort = -1;
  688. int cur_reg = SND_SOC_NOPM;
  689. int ret;
  690. list_for_each_entry_safe(w, n, list, power_list) {
  691. ret = 0;
  692. /* Do we need to apply any queued changes? */
  693. if (sort[w->id] != cur_sort || w->reg != cur_reg) {
  694. if (!list_empty(&pending))
  695. dapm_seq_run_coalesced(codec, &pending);
  696. INIT_LIST_HEAD(&pending);
  697. cur_sort = -1;
  698. cur_reg = SND_SOC_NOPM;
  699. }
  700. switch (w->id) {
  701. case snd_soc_dapm_pre:
  702. if (!w->event)
  703. list_for_each_entry_safe_continue(w, n, list,
  704. power_list);
  705. if (event == SND_SOC_DAPM_STREAM_START)
  706. ret = w->event(w,
  707. NULL, SND_SOC_DAPM_PRE_PMU);
  708. else if (event == SND_SOC_DAPM_STREAM_STOP)
  709. ret = w->event(w,
  710. NULL, SND_SOC_DAPM_PRE_PMD);
  711. break;
  712. case snd_soc_dapm_post:
  713. if (!w->event)
  714. list_for_each_entry_safe_continue(w, n, list,
  715. power_list);
  716. if (event == SND_SOC_DAPM_STREAM_START)
  717. ret = w->event(w,
  718. NULL, SND_SOC_DAPM_POST_PMU);
  719. else if (event == SND_SOC_DAPM_STREAM_STOP)
  720. ret = w->event(w,
  721. NULL, SND_SOC_DAPM_POST_PMD);
  722. break;
  723. case snd_soc_dapm_input:
  724. case snd_soc_dapm_output:
  725. case snd_soc_dapm_hp:
  726. case snd_soc_dapm_mic:
  727. case snd_soc_dapm_line:
  728. case snd_soc_dapm_spk:
  729. /* No register support currently */
  730. ret = dapm_generic_apply_power(w);
  731. break;
  732. default:
  733. /* Queue it up for application */
  734. cur_sort = sort[w->id];
  735. cur_reg = w->reg;
  736. list_move(&w->power_list, &pending);
  737. break;
  738. }
  739. if (ret < 0)
  740. pr_err("Failed to apply widget power: %d\n",
  741. ret);
  742. }
  743. if (!list_empty(&pending))
  744. dapm_seq_run_coalesced(codec, &pending);
  745. }
  746. /*
  747. * Scan each dapm widget for complete audio path.
  748. * A complete path is a route that has valid endpoints i.e.:-
  749. *
  750. * o DAC to output pin.
  751. * o Input Pin to ADC.
  752. * o Input pin to Output pin (bypass, sidetone)
  753. * o DAC to ADC (loopback).
  754. */
  755. static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
  756. {
  757. struct snd_soc_device *socdev = codec->socdev;
  758. struct snd_soc_dapm_widget *w;
  759. LIST_HEAD(up_list);
  760. LIST_HEAD(down_list);
  761. int ret = 0;
  762. int power;
  763. int sys_power = 0;
  764. /* Check which widgets we need to power and store them in
  765. * lists indicating if they should be powered up or down.
  766. */
  767. list_for_each_entry(w, &codec->dapm_widgets, list) {
  768. switch (w->id) {
  769. case snd_soc_dapm_pre:
  770. dapm_seq_insert(w, &down_list, dapm_down_seq);
  771. break;
  772. case snd_soc_dapm_post:
  773. dapm_seq_insert(w, &up_list, dapm_up_seq);
  774. break;
  775. default:
  776. if (!w->power_check)
  777. continue;
  778. /* If we're suspending then pull down all the
  779. * power. */
  780. switch (event) {
  781. case SND_SOC_DAPM_STREAM_SUSPEND:
  782. power = 0;
  783. break;
  784. default:
  785. if (!w->force)
  786. power = w->power_check(w);
  787. else
  788. power = 1;
  789. if (power)
  790. sys_power = 1;
  791. break;
  792. }
  793. if (w->power == power)
  794. continue;
  795. if (power)
  796. dapm_seq_insert(w, &up_list, dapm_up_seq);
  797. else
  798. dapm_seq_insert(w, &down_list, dapm_down_seq);
  799. w->power = power;
  800. break;
  801. }
  802. }
  803. /* If there are no DAPM widgets then try to figure out power from the
  804. * event type.
  805. */
  806. if (list_empty(&codec->dapm_widgets)) {
  807. switch (event) {
  808. case SND_SOC_DAPM_STREAM_START:
  809. case SND_SOC_DAPM_STREAM_RESUME:
  810. sys_power = 1;
  811. break;
  812. case SND_SOC_DAPM_STREAM_SUSPEND:
  813. sys_power = 0;
  814. break;
  815. case SND_SOC_DAPM_STREAM_NOP:
  816. switch (codec->bias_level) {
  817. case SND_SOC_BIAS_STANDBY:
  818. case SND_SOC_BIAS_OFF:
  819. sys_power = 0;
  820. break;
  821. default:
  822. sys_power = 1;
  823. break;
  824. }
  825. break;
  826. default:
  827. break;
  828. }
  829. }
  830. if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
  831. ret = snd_soc_dapm_set_bias_level(socdev,
  832. SND_SOC_BIAS_STANDBY);
  833. if (ret != 0)
  834. pr_err("Failed to turn on bias: %d\n", ret);
  835. }
  836. /* If we're changing to all on or all off then prepare */
  837. if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
  838. (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
  839. ret = snd_soc_dapm_set_bias_level(socdev,
  840. SND_SOC_BIAS_PREPARE);
  841. if (ret != 0)
  842. pr_err("Failed to prepare bias: %d\n", ret);
  843. }
  844. /* Power down widgets first; try to avoid amplifying pops. */
  845. dapm_seq_run(codec, &down_list, event, dapm_down_seq);
  846. /* Now power up. */
  847. dapm_seq_run(codec, &up_list, event, dapm_up_seq);
  848. /* If we just powered the last thing off drop to standby bias */
  849. if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
  850. ret = snd_soc_dapm_set_bias_level(socdev,
  851. SND_SOC_BIAS_STANDBY);
  852. if (ret != 0)
  853. pr_err("Failed to apply standby bias: %d\n", ret);
  854. }
  855. /* If we're in standby and can support bias off then do that */
  856. if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
  857. codec->idle_bias_off) {
  858. ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
  859. if (ret != 0)
  860. pr_err("Failed to turn off bias: %d\n", ret);
  861. }
  862. /* If we just powered up then move to active bias */
  863. if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
  864. ret = snd_soc_dapm_set_bias_level(socdev,
  865. SND_SOC_BIAS_ON);
  866. if (ret != 0)
  867. pr_err("Failed to apply active bias: %d\n", ret);
  868. }
  869. pop_dbg(codec->pop_time, "DAPM sequencing finished, waiting %dms\n",
  870. codec->pop_time);
  871. pop_wait(codec->pop_time);
  872. return 0;
  873. }
  874. #ifdef CONFIG_DEBUG_FS
  875. static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
  876. {
  877. file->private_data = inode->i_private;
  878. return 0;
  879. }
  880. static ssize_t dapm_widget_power_read_file(struct file *file,
  881. char __user *user_buf,
  882. size_t count, loff_t *ppos)
  883. {
  884. struct snd_soc_dapm_widget *w = file->private_data;
  885. char *buf;
  886. int in, out;
  887. ssize_t ret;
  888. struct snd_soc_dapm_path *p = NULL;
  889. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  890. if (!buf)
  891. return -ENOMEM;
  892. in = is_connected_input_ep(w);
  893. dapm_clear_walk(w->codec);
  894. out = is_connected_output_ep(w);
  895. dapm_clear_walk(w->codec);
  896. ret = snprintf(buf, PAGE_SIZE, "%s: %s in %d out %d",
  897. w->name, w->power ? "On" : "Off", in, out);
  898. if (w->reg >= 0)
  899. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  900. " - R%d(0x%x) bit %d",
  901. w->reg, w->reg, w->shift);
  902. ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
  903. if (w->sname)
  904. ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
  905. w->sname,
  906. w->active ? "active" : "inactive");
  907. list_for_each_entry(p, &w->sources, list_sink) {
  908. if (p->connected && !p->connected(w, p->sink))
  909. continue;
  910. if (p->connect)
  911. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  912. " in %s %s\n",
  913. p->name ? p->name : "static",
  914. p->source->name);
  915. }
  916. list_for_each_entry(p, &w->sinks, list_source) {
  917. if (p->connected && !p->connected(w, p->sink))
  918. continue;
  919. if (p->connect)
  920. ret += snprintf(buf + ret, PAGE_SIZE - ret,
  921. " out %s %s\n",
  922. p->name ? p->name : "static",
  923. p->sink->name);
  924. }
  925. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  926. kfree(buf);
  927. return ret;
  928. }
  929. static const struct file_operations dapm_widget_power_fops = {
  930. .open = dapm_widget_power_open_file,
  931. .read = dapm_widget_power_read_file,
  932. };
  933. void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
  934. {
  935. struct snd_soc_dapm_widget *w;
  936. struct dentry *d;
  937. if (!codec->debugfs_dapm)
  938. return;
  939. list_for_each_entry(w, &codec->dapm_widgets, list) {
  940. if (!w->name)
  941. continue;
  942. d = debugfs_create_file(w->name, 0444,
  943. codec->debugfs_dapm, w,
  944. &dapm_widget_power_fops);
  945. if (!d)
  946. printk(KERN_WARNING
  947. "ASoC: Failed to create %s debugfs file\n",
  948. w->name);
  949. }
  950. }
  951. #else
  952. void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
  953. {
  954. }
  955. #endif
  956. /* test and update the power status of a mux widget */
  957. static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
  958. struct snd_kcontrol *kcontrol, int change,
  959. int mux, struct soc_enum *e)
  960. {
  961. struct snd_soc_dapm_path *path;
  962. int found = 0;
  963. if (widget->id != snd_soc_dapm_mux &&
  964. widget->id != snd_soc_dapm_value_mux)
  965. return -ENODEV;
  966. if (!change)
  967. return 0;
  968. /* find dapm widget path assoc with kcontrol */
  969. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  970. if (path->kcontrol != kcontrol)
  971. continue;
  972. if (!path->name || !e->texts[mux])
  973. continue;
  974. found = 1;
  975. /* we now need to match the string in the enum to the path */
  976. if (!(strcmp(path->name, e->texts[mux])))
  977. path->connect = 1; /* new connection */
  978. else
  979. path->connect = 0; /* old connection must be powered down */
  980. }
  981. if (found)
  982. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  983. return 0;
  984. }
  985. /* test and update the power status of a mixer or switch widget */
  986. static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
  987. struct snd_kcontrol *kcontrol, int connect)
  988. {
  989. struct snd_soc_dapm_path *path;
  990. int found = 0;
  991. if (widget->id != snd_soc_dapm_mixer &&
  992. widget->id != snd_soc_dapm_mixer_named_ctl &&
  993. widget->id != snd_soc_dapm_switch)
  994. return -ENODEV;
  995. /* find dapm widget path assoc with kcontrol */
  996. list_for_each_entry(path, &widget->codec->dapm_paths, list) {
  997. if (path->kcontrol != kcontrol)
  998. continue;
  999. /* found, now check type */
  1000. found = 1;
  1001. path->connect = connect;
  1002. break;
  1003. }
  1004. if (found)
  1005. dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
  1006. return 0;
  1007. }
  1008. /* show dapm widget status in sys fs */
  1009. static ssize_t dapm_widget_show(struct device *dev,
  1010. struct device_attribute *attr, char *buf)
  1011. {
  1012. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  1013. struct snd_soc_codec *codec = devdata->card->codec;
  1014. struct snd_soc_dapm_widget *w;
  1015. int count = 0;
  1016. char *state = "not set";
  1017. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1018. /* only display widgets that burnm power */
  1019. switch (w->id) {
  1020. case snd_soc_dapm_hp:
  1021. case snd_soc_dapm_mic:
  1022. case snd_soc_dapm_spk:
  1023. case snd_soc_dapm_line:
  1024. case snd_soc_dapm_micbias:
  1025. case snd_soc_dapm_dac:
  1026. case snd_soc_dapm_adc:
  1027. case snd_soc_dapm_pga:
  1028. case snd_soc_dapm_mixer:
  1029. case snd_soc_dapm_mixer_named_ctl:
  1030. case snd_soc_dapm_supply:
  1031. if (w->name)
  1032. count += sprintf(buf + count, "%s: %s\n",
  1033. w->name, w->power ? "On":"Off");
  1034. break;
  1035. default:
  1036. break;
  1037. }
  1038. }
  1039. switch (codec->bias_level) {
  1040. case SND_SOC_BIAS_ON:
  1041. state = "On";
  1042. break;
  1043. case SND_SOC_BIAS_PREPARE:
  1044. state = "Prepare";
  1045. break;
  1046. case SND_SOC_BIAS_STANDBY:
  1047. state = "Standby";
  1048. break;
  1049. case SND_SOC_BIAS_OFF:
  1050. state = "Off";
  1051. break;
  1052. }
  1053. count += sprintf(buf + count, "PM State: %s\n", state);
  1054. return count;
  1055. }
  1056. static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
  1057. int snd_soc_dapm_sys_add(struct device *dev)
  1058. {
  1059. return device_create_file(dev, &dev_attr_dapm_widget);
  1060. }
  1061. static void snd_soc_dapm_sys_remove(struct device *dev)
  1062. {
  1063. device_remove_file(dev, &dev_attr_dapm_widget);
  1064. }
  1065. /* free all dapm widgets and resources */
  1066. static void dapm_free_widgets(struct snd_soc_codec *codec)
  1067. {
  1068. struct snd_soc_dapm_widget *w, *next_w;
  1069. struct snd_soc_dapm_path *p, *next_p;
  1070. list_for_each_entry_safe(w, next_w, &codec->dapm_widgets, list) {
  1071. list_del(&w->list);
  1072. kfree(w);
  1073. }
  1074. list_for_each_entry_safe(p, next_p, &codec->dapm_paths, list) {
  1075. list_del(&p->list);
  1076. kfree(p->long_name);
  1077. kfree(p);
  1078. }
  1079. }
  1080. static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  1081. const char *pin, int status)
  1082. {
  1083. struct snd_soc_dapm_widget *w;
  1084. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1085. if (!strcmp(w->name, pin)) {
  1086. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  1087. w->connected = status;
  1088. /* Allow disabling of forced pins */
  1089. if (status == 0)
  1090. w->force = 0;
  1091. return 0;
  1092. }
  1093. }
  1094. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  1095. return -EINVAL;
  1096. }
  1097. /**
  1098. * snd_soc_dapm_sync - scan and power dapm paths
  1099. * @codec: audio codec
  1100. *
  1101. * Walks all dapm audio paths and powers widgets according to their
  1102. * stream or path usage.
  1103. *
  1104. * Returns 0 for success.
  1105. */
  1106. int snd_soc_dapm_sync(struct snd_soc_codec *codec)
  1107. {
  1108. return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1109. }
  1110. EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
  1111. static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
  1112. const struct snd_soc_dapm_route *route)
  1113. {
  1114. struct snd_soc_dapm_path *path;
  1115. struct snd_soc_dapm_widget *wsource = NULL, *wsink = NULL, *w;
  1116. const char *sink = route->sink;
  1117. const char *control = route->control;
  1118. const char *source = route->source;
  1119. int ret = 0;
  1120. /* find src and dest widgets */
  1121. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1122. if (!wsink && !(strcmp(w->name, sink))) {
  1123. wsink = w;
  1124. continue;
  1125. }
  1126. if (!wsource && !(strcmp(w->name, source))) {
  1127. wsource = w;
  1128. }
  1129. }
  1130. if (wsource == NULL || wsink == NULL)
  1131. return -ENODEV;
  1132. path = kzalloc(sizeof(struct snd_soc_dapm_path), GFP_KERNEL);
  1133. if (!path)
  1134. return -ENOMEM;
  1135. path->source = wsource;
  1136. path->sink = wsink;
  1137. path->connected = route->connected;
  1138. INIT_LIST_HEAD(&path->list);
  1139. INIT_LIST_HEAD(&path->list_source);
  1140. INIT_LIST_HEAD(&path->list_sink);
  1141. /* check for external widgets */
  1142. if (wsink->id == snd_soc_dapm_input) {
  1143. if (wsource->id == snd_soc_dapm_micbias ||
  1144. wsource->id == snd_soc_dapm_mic ||
  1145. wsource->id == snd_soc_dapm_line ||
  1146. wsource->id == snd_soc_dapm_output)
  1147. wsink->ext = 1;
  1148. }
  1149. if (wsource->id == snd_soc_dapm_output) {
  1150. if (wsink->id == snd_soc_dapm_spk ||
  1151. wsink->id == snd_soc_dapm_hp ||
  1152. wsink->id == snd_soc_dapm_line ||
  1153. wsink->id == snd_soc_dapm_input)
  1154. wsource->ext = 1;
  1155. }
  1156. /* connect static paths */
  1157. if (control == NULL) {
  1158. list_add(&path->list, &codec->dapm_paths);
  1159. list_add(&path->list_sink, &wsink->sources);
  1160. list_add(&path->list_source, &wsource->sinks);
  1161. path->connect = 1;
  1162. return 0;
  1163. }
  1164. /* connect dynamic paths */
  1165. switch(wsink->id) {
  1166. case snd_soc_dapm_adc:
  1167. case snd_soc_dapm_dac:
  1168. case snd_soc_dapm_pga:
  1169. case snd_soc_dapm_input:
  1170. case snd_soc_dapm_output:
  1171. case snd_soc_dapm_micbias:
  1172. case snd_soc_dapm_vmid:
  1173. case snd_soc_dapm_pre:
  1174. case snd_soc_dapm_post:
  1175. case snd_soc_dapm_supply:
  1176. case snd_soc_dapm_aif_in:
  1177. case snd_soc_dapm_aif_out:
  1178. list_add(&path->list, &codec->dapm_paths);
  1179. list_add(&path->list_sink, &wsink->sources);
  1180. list_add(&path->list_source, &wsource->sinks);
  1181. path->connect = 1;
  1182. return 0;
  1183. case snd_soc_dapm_mux:
  1184. case snd_soc_dapm_value_mux:
  1185. ret = dapm_connect_mux(codec, wsource, wsink, path, control,
  1186. &wsink->kcontrols[0]);
  1187. if (ret != 0)
  1188. goto err;
  1189. break;
  1190. case snd_soc_dapm_switch:
  1191. case snd_soc_dapm_mixer:
  1192. case snd_soc_dapm_mixer_named_ctl:
  1193. ret = dapm_connect_mixer(codec, wsource, wsink, path, control);
  1194. if (ret != 0)
  1195. goto err;
  1196. break;
  1197. case snd_soc_dapm_hp:
  1198. case snd_soc_dapm_mic:
  1199. case snd_soc_dapm_line:
  1200. case snd_soc_dapm_spk:
  1201. list_add(&path->list, &codec->dapm_paths);
  1202. list_add(&path->list_sink, &wsink->sources);
  1203. list_add(&path->list_source, &wsource->sinks);
  1204. path->connect = 0;
  1205. return 0;
  1206. }
  1207. return 0;
  1208. err:
  1209. printk(KERN_WARNING "asoc: no dapm match for %s --> %s --> %s\n", source,
  1210. control, sink);
  1211. kfree(path);
  1212. return ret;
  1213. }
  1214. /**
  1215. * snd_soc_dapm_add_routes - Add routes between DAPM widgets
  1216. * @codec: codec
  1217. * @route: audio routes
  1218. * @num: number of routes
  1219. *
  1220. * Connects 2 dapm widgets together via a named audio path. The sink is
  1221. * the widget receiving the audio signal, whilst the source is the sender
  1222. * of the audio signal.
  1223. *
  1224. * Returns 0 for success else error. On error all resources can be freed
  1225. * with a call to snd_soc_card_free().
  1226. */
  1227. int snd_soc_dapm_add_routes(struct snd_soc_codec *codec,
  1228. const struct snd_soc_dapm_route *route, int num)
  1229. {
  1230. int i, ret;
  1231. for (i = 0; i < num; i++) {
  1232. ret = snd_soc_dapm_add_route(codec, route);
  1233. if (ret < 0) {
  1234. printk(KERN_ERR "Failed to add route %s->%s\n",
  1235. route->source,
  1236. route->sink);
  1237. return ret;
  1238. }
  1239. route++;
  1240. }
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL_GPL(snd_soc_dapm_add_routes);
  1244. /**
  1245. * snd_soc_dapm_new_widgets - add new dapm widgets
  1246. * @codec: audio codec
  1247. *
  1248. * Checks the codec for any new dapm widgets and creates them if found.
  1249. *
  1250. * Returns 0 for success.
  1251. */
  1252. int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
  1253. {
  1254. struct snd_soc_dapm_widget *w;
  1255. list_for_each_entry(w, &codec->dapm_widgets, list)
  1256. {
  1257. if (w->new)
  1258. continue;
  1259. switch(w->id) {
  1260. case snd_soc_dapm_switch:
  1261. case snd_soc_dapm_mixer:
  1262. case snd_soc_dapm_mixer_named_ctl:
  1263. w->power_check = dapm_generic_check_power;
  1264. dapm_new_mixer(codec, w);
  1265. break;
  1266. case snd_soc_dapm_mux:
  1267. case snd_soc_dapm_value_mux:
  1268. w->power_check = dapm_generic_check_power;
  1269. dapm_new_mux(codec, w);
  1270. break;
  1271. case snd_soc_dapm_adc:
  1272. case snd_soc_dapm_aif_out:
  1273. w->power_check = dapm_adc_check_power;
  1274. break;
  1275. case snd_soc_dapm_dac:
  1276. case snd_soc_dapm_aif_in:
  1277. w->power_check = dapm_dac_check_power;
  1278. break;
  1279. case snd_soc_dapm_pga:
  1280. w->power_check = dapm_generic_check_power;
  1281. dapm_new_pga(codec, w);
  1282. break;
  1283. case snd_soc_dapm_input:
  1284. case snd_soc_dapm_output:
  1285. case snd_soc_dapm_micbias:
  1286. case snd_soc_dapm_spk:
  1287. case snd_soc_dapm_hp:
  1288. case snd_soc_dapm_mic:
  1289. case snd_soc_dapm_line:
  1290. w->power_check = dapm_generic_check_power;
  1291. break;
  1292. case snd_soc_dapm_supply:
  1293. w->power_check = dapm_supply_check_power;
  1294. case snd_soc_dapm_vmid:
  1295. case snd_soc_dapm_pre:
  1296. case snd_soc_dapm_post:
  1297. break;
  1298. }
  1299. w->new = 1;
  1300. }
  1301. dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
  1302. return 0;
  1303. }
  1304. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_widgets);
  1305. /**
  1306. * snd_soc_dapm_get_volsw - dapm mixer get callback
  1307. * @kcontrol: mixer control
  1308. * @ucontrol: control element information
  1309. *
  1310. * Callback to get the value of a dapm mixer control.
  1311. *
  1312. * Returns 0 for success.
  1313. */
  1314. int snd_soc_dapm_get_volsw(struct snd_kcontrol *kcontrol,
  1315. struct snd_ctl_elem_value *ucontrol)
  1316. {
  1317. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1318. struct soc_mixer_control *mc =
  1319. (struct soc_mixer_control *)kcontrol->private_value;
  1320. unsigned int reg = mc->reg;
  1321. unsigned int shift = mc->shift;
  1322. unsigned int rshift = mc->rshift;
  1323. int max = mc->max;
  1324. unsigned int invert = mc->invert;
  1325. unsigned int mask = (1 << fls(max)) - 1;
  1326. ucontrol->value.integer.value[0] =
  1327. (snd_soc_read(widget->codec, reg) >> shift) & mask;
  1328. if (shift != rshift)
  1329. ucontrol->value.integer.value[1] =
  1330. (snd_soc_read(widget->codec, reg) >> rshift) & mask;
  1331. if (invert) {
  1332. ucontrol->value.integer.value[0] =
  1333. max - ucontrol->value.integer.value[0];
  1334. if (shift != rshift)
  1335. ucontrol->value.integer.value[1] =
  1336. max - ucontrol->value.integer.value[1];
  1337. }
  1338. return 0;
  1339. }
  1340. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_volsw);
  1341. /**
  1342. * snd_soc_dapm_put_volsw - dapm mixer set callback
  1343. * @kcontrol: mixer control
  1344. * @ucontrol: control element information
  1345. *
  1346. * Callback to set the value of a dapm mixer control.
  1347. *
  1348. * Returns 0 for success.
  1349. */
  1350. int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
  1351. struct snd_ctl_elem_value *ucontrol)
  1352. {
  1353. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1354. struct soc_mixer_control *mc =
  1355. (struct soc_mixer_control *)kcontrol->private_value;
  1356. unsigned int reg = mc->reg;
  1357. unsigned int shift = mc->shift;
  1358. unsigned int rshift = mc->rshift;
  1359. int max = mc->max;
  1360. unsigned int mask = (1 << fls(max)) - 1;
  1361. unsigned int invert = mc->invert;
  1362. unsigned int val, val2, val_mask;
  1363. int connect;
  1364. int ret;
  1365. val = (ucontrol->value.integer.value[0] & mask);
  1366. if (invert)
  1367. val = max - val;
  1368. val_mask = mask << shift;
  1369. val = val << shift;
  1370. if (shift != rshift) {
  1371. val2 = (ucontrol->value.integer.value[1] & mask);
  1372. if (invert)
  1373. val2 = max - val2;
  1374. val_mask |= mask << rshift;
  1375. val |= val2 << rshift;
  1376. }
  1377. mutex_lock(&widget->codec->mutex);
  1378. widget->value = val;
  1379. if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
  1380. if (val)
  1381. /* new connection */
  1382. connect = invert ? 0:1;
  1383. else
  1384. /* old connection must be powered down */
  1385. connect = invert ? 1:0;
  1386. dapm_mixer_update_power(widget, kcontrol, connect);
  1387. }
  1388. if (widget->event) {
  1389. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1390. ret = widget->event(widget, kcontrol,
  1391. SND_SOC_DAPM_PRE_REG);
  1392. if (ret < 0) {
  1393. ret = 1;
  1394. goto out;
  1395. }
  1396. }
  1397. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1398. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1399. ret = widget->event(widget, kcontrol,
  1400. SND_SOC_DAPM_POST_REG);
  1401. } else
  1402. ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
  1403. out:
  1404. mutex_unlock(&widget->codec->mutex);
  1405. return ret;
  1406. }
  1407. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_volsw);
  1408. /**
  1409. * snd_soc_dapm_get_enum_double - dapm enumerated double mixer get callback
  1410. * @kcontrol: mixer control
  1411. * @ucontrol: control element information
  1412. *
  1413. * Callback to get the value of a dapm enumerated double mixer control.
  1414. *
  1415. * Returns 0 for success.
  1416. */
  1417. int snd_soc_dapm_get_enum_double(struct snd_kcontrol *kcontrol,
  1418. struct snd_ctl_elem_value *ucontrol)
  1419. {
  1420. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1421. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1422. unsigned int val, bitmask;
  1423. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1424. ;
  1425. val = snd_soc_read(widget->codec, e->reg);
  1426. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1427. if (e->shift_l != e->shift_r)
  1428. ucontrol->value.enumerated.item[1] =
  1429. (val >> e->shift_r) & (bitmask - 1);
  1430. return 0;
  1431. }
  1432. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_double);
  1433. /**
  1434. * snd_soc_dapm_put_enum_double - dapm enumerated double mixer set callback
  1435. * @kcontrol: mixer control
  1436. * @ucontrol: control element information
  1437. *
  1438. * Callback to set the value of a dapm enumerated double mixer control.
  1439. *
  1440. * Returns 0 for success.
  1441. */
  1442. int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
  1443. struct snd_ctl_elem_value *ucontrol)
  1444. {
  1445. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1446. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1447. unsigned int val, mux, change;
  1448. unsigned int mask, bitmask;
  1449. int ret = 0;
  1450. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1451. ;
  1452. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1453. return -EINVAL;
  1454. mux = ucontrol->value.enumerated.item[0];
  1455. val = mux << e->shift_l;
  1456. mask = (bitmask - 1) << e->shift_l;
  1457. if (e->shift_l != e->shift_r) {
  1458. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1459. return -EINVAL;
  1460. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1461. mask |= (bitmask - 1) << e->shift_r;
  1462. }
  1463. mutex_lock(&widget->codec->mutex);
  1464. widget->value = val;
  1465. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1466. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1467. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1468. ret = widget->event(widget,
  1469. kcontrol, SND_SOC_DAPM_PRE_REG);
  1470. if (ret < 0)
  1471. goto out;
  1472. }
  1473. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1474. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1475. ret = widget->event(widget,
  1476. kcontrol, SND_SOC_DAPM_POST_REG);
  1477. out:
  1478. mutex_unlock(&widget->codec->mutex);
  1479. return ret;
  1480. }
  1481. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
  1482. /**
  1483. * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
  1484. * @kcontrol: mixer control
  1485. * @ucontrol: control element information
  1486. *
  1487. * Returns 0 for success.
  1488. */
  1489. int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
  1490. struct snd_ctl_elem_value *ucontrol)
  1491. {
  1492. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1493. ucontrol->value.enumerated.item[0] = widget->value;
  1494. return 0;
  1495. }
  1496. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
  1497. /**
  1498. * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
  1499. * @kcontrol: mixer control
  1500. * @ucontrol: control element information
  1501. *
  1502. * Returns 0 for success.
  1503. */
  1504. int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
  1505. struct snd_ctl_elem_value *ucontrol)
  1506. {
  1507. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1508. struct soc_enum *e =
  1509. (struct soc_enum *)kcontrol->private_value;
  1510. int change;
  1511. int ret = 0;
  1512. if (ucontrol->value.enumerated.item[0] >= e->max)
  1513. return -EINVAL;
  1514. mutex_lock(&widget->codec->mutex);
  1515. change = widget->value != ucontrol->value.enumerated.item[0];
  1516. widget->value = ucontrol->value.enumerated.item[0];
  1517. dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
  1518. mutex_unlock(&widget->codec->mutex);
  1519. return ret;
  1520. }
  1521. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
  1522. /**
  1523. * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  1524. * callback
  1525. * @kcontrol: mixer control
  1526. * @ucontrol: control element information
  1527. *
  1528. * Callback to get the value of a dapm semi enumerated double mixer control.
  1529. *
  1530. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1531. * used for handling bitfield coded enumeration for example.
  1532. *
  1533. * Returns 0 for success.
  1534. */
  1535. int snd_soc_dapm_get_value_enum_double(struct snd_kcontrol *kcontrol,
  1536. struct snd_ctl_elem_value *ucontrol)
  1537. {
  1538. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1539. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1540. unsigned int reg_val, val, mux;
  1541. reg_val = snd_soc_read(widget->codec, e->reg);
  1542. val = (reg_val >> e->shift_l) & e->mask;
  1543. for (mux = 0; mux < e->max; mux++) {
  1544. if (val == e->values[mux])
  1545. break;
  1546. }
  1547. ucontrol->value.enumerated.item[0] = mux;
  1548. if (e->shift_l != e->shift_r) {
  1549. val = (reg_val >> e->shift_r) & e->mask;
  1550. for (mux = 0; mux < e->max; mux++) {
  1551. if (val == e->values[mux])
  1552. break;
  1553. }
  1554. ucontrol->value.enumerated.item[1] = mux;
  1555. }
  1556. return 0;
  1557. }
  1558. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_value_enum_double);
  1559. /**
  1560. * snd_soc_dapm_put_value_enum_double - dapm semi enumerated double mixer set
  1561. * callback
  1562. * @kcontrol: mixer control
  1563. * @ucontrol: control element information
  1564. *
  1565. * Callback to set the value of a dapm semi enumerated double mixer control.
  1566. *
  1567. * Semi enumerated mixer: the enumerated items are referred as values. Can be
  1568. * used for handling bitfield coded enumeration for example.
  1569. *
  1570. * Returns 0 for success.
  1571. */
  1572. int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
  1573. struct snd_ctl_elem_value *ucontrol)
  1574. {
  1575. struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
  1576. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1577. unsigned int val, mux, change;
  1578. unsigned int mask;
  1579. int ret = 0;
  1580. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1581. return -EINVAL;
  1582. mux = ucontrol->value.enumerated.item[0];
  1583. val = e->values[ucontrol->value.enumerated.item[0]] << e->shift_l;
  1584. mask = e->mask << e->shift_l;
  1585. if (e->shift_l != e->shift_r) {
  1586. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1587. return -EINVAL;
  1588. val |= e->values[ucontrol->value.enumerated.item[1]] << e->shift_r;
  1589. mask |= e->mask << e->shift_r;
  1590. }
  1591. mutex_lock(&widget->codec->mutex);
  1592. widget->value = val;
  1593. change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
  1594. dapm_mux_update_power(widget, kcontrol, change, mux, e);
  1595. if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
  1596. ret = widget->event(widget,
  1597. kcontrol, SND_SOC_DAPM_PRE_REG);
  1598. if (ret < 0)
  1599. goto out;
  1600. }
  1601. ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
  1602. if (widget->event_flags & SND_SOC_DAPM_POST_REG)
  1603. ret = widget->event(widget,
  1604. kcontrol, SND_SOC_DAPM_POST_REG);
  1605. out:
  1606. mutex_unlock(&widget->codec->mutex);
  1607. return ret;
  1608. }
  1609. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_value_enum_double);
  1610. /**
  1611. * snd_soc_dapm_info_pin_switch - Info for a pin switch
  1612. *
  1613. * @kcontrol: mixer control
  1614. * @uinfo: control element information
  1615. *
  1616. * Callback to provide information about a pin switch control.
  1617. */
  1618. int snd_soc_dapm_info_pin_switch(struct snd_kcontrol *kcontrol,
  1619. struct snd_ctl_elem_info *uinfo)
  1620. {
  1621. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1622. uinfo->count = 1;
  1623. uinfo->value.integer.min = 0;
  1624. uinfo->value.integer.max = 1;
  1625. return 0;
  1626. }
  1627. EXPORT_SYMBOL_GPL(snd_soc_dapm_info_pin_switch);
  1628. /**
  1629. * snd_soc_dapm_get_pin_switch - Get information for a pin switch
  1630. *
  1631. * @kcontrol: mixer control
  1632. * @ucontrol: Value
  1633. */
  1634. int snd_soc_dapm_get_pin_switch(struct snd_kcontrol *kcontrol,
  1635. struct snd_ctl_elem_value *ucontrol)
  1636. {
  1637. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1638. const char *pin = (const char *)kcontrol->private_value;
  1639. mutex_lock(&codec->mutex);
  1640. ucontrol->value.integer.value[0] =
  1641. snd_soc_dapm_get_pin_status(codec, pin);
  1642. mutex_unlock(&codec->mutex);
  1643. return 0;
  1644. }
  1645. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_switch);
  1646. /**
  1647. * snd_soc_dapm_put_pin_switch - Set information for a pin switch
  1648. *
  1649. * @kcontrol: mixer control
  1650. * @ucontrol: Value
  1651. */
  1652. int snd_soc_dapm_put_pin_switch(struct snd_kcontrol *kcontrol,
  1653. struct snd_ctl_elem_value *ucontrol)
  1654. {
  1655. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1656. const char *pin = (const char *)kcontrol->private_value;
  1657. mutex_lock(&codec->mutex);
  1658. if (ucontrol->value.integer.value[0])
  1659. snd_soc_dapm_enable_pin(codec, pin);
  1660. else
  1661. snd_soc_dapm_disable_pin(codec, pin);
  1662. snd_soc_dapm_sync(codec);
  1663. mutex_unlock(&codec->mutex);
  1664. return 0;
  1665. }
  1666. EXPORT_SYMBOL_GPL(snd_soc_dapm_put_pin_switch);
  1667. /**
  1668. * snd_soc_dapm_new_control - create new dapm control
  1669. * @codec: audio codec
  1670. * @widget: widget template
  1671. *
  1672. * Creates a new dapm control based upon the template.
  1673. *
  1674. * Returns 0 for success else error.
  1675. */
  1676. int snd_soc_dapm_new_control(struct snd_soc_codec *codec,
  1677. const struct snd_soc_dapm_widget *widget)
  1678. {
  1679. struct snd_soc_dapm_widget *w;
  1680. if ((w = dapm_cnew_widget(widget)) == NULL)
  1681. return -ENOMEM;
  1682. w->codec = codec;
  1683. INIT_LIST_HEAD(&w->sources);
  1684. INIT_LIST_HEAD(&w->sinks);
  1685. INIT_LIST_HEAD(&w->list);
  1686. list_add(&w->list, &codec->dapm_widgets);
  1687. /* machine layer set ups unconnected pins and insertions */
  1688. w->connected = 1;
  1689. return 0;
  1690. }
  1691. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_control);
  1692. /**
  1693. * snd_soc_dapm_new_controls - create new dapm controls
  1694. * @codec: audio codec
  1695. * @widget: widget array
  1696. * @num: number of widgets
  1697. *
  1698. * Creates new DAPM controls based upon the templates.
  1699. *
  1700. * Returns 0 for success else error.
  1701. */
  1702. int snd_soc_dapm_new_controls(struct snd_soc_codec *codec,
  1703. const struct snd_soc_dapm_widget *widget,
  1704. int num)
  1705. {
  1706. int i, ret;
  1707. for (i = 0; i < num; i++) {
  1708. ret = snd_soc_dapm_new_control(codec, widget);
  1709. if (ret < 0) {
  1710. printk(KERN_ERR
  1711. "ASoC: Failed to create DAPM control %s: %d\n",
  1712. widget->name, ret);
  1713. return ret;
  1714. }
  1715. widget++;
  1716. }
  1717. return 0;
  1718. }
  1719. EXPORT_SYMBOL_GPL(snd_soc_dapm_new_controls);
  1720. /**
  1721. * snd_soc_dapm_stream_event - send a stream event to the dapm core
  1722. * @codec: audio codec
  1723. * @stream: stream name
  1724. * @event: stream event
  1725. *
  1726. * Sends a stream event to the dapm core. The core then makes any
  1727. * necessary widget power changes.
  1728. *
  1729. * Returns 0 for success else error.
  1730. */
  1731. int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
  1732. char *stream, int event)
  1733. {
  1734. struct snd_soc_dapm_widget *w;
  1735. if (stream == NULL)
  1736. return 0;
  1737. mutex_lock(&codec->mutex);
  1738. list_for_each_entry(w, &codec->dapm_widgets, list)
  1739. {
  1740. if (!w->sname)
  1741. continue;
  1742. pr_debug("widget %s\n %s stream %s event %d\n",
  1743. w->name, w->sname, stream, event);
  1744. if (strstr(w->sname, stream)) {
  1745. switch(event) {
  1746. case SND_SOC_DAPM_STREAM_START:
  1747. w->active = 1;
  1748. break;
  1749. case SND_SOC_DAPM_STREAM_STOP:
  1750. w->active = 0;
  1751. break;
  1752. case SND_SOC_DAPM_STREAM_SUSPEND:
  1753. if (w->active)
  1754. w->suspend = 1;
  1755. w->active = 0;
  1756. break;
  1757. case SND_SOC_DAPM_STREAM_RESUME:
  1758. if (w->suspend) {
  1759. w->active = 1;
  1760. w->suspend = 0;
  1761. }
  1762. break;
  1763. case SND_SOC_DAPM_STREAM_PAUSE_PUSH:
  1764. break;
  1765. case SND_SOC_DAPM_STREAM_PAUSE_RELEASE:
  1766. break;
  1767. }
  1768. }
  1769. }
  1770. dapm_power_widgets(codec, event);
  1771. mutex_unlock(&codec->mutex);
  1772. return 0;
  1773. }
  1774. EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
  1775. /**
  1776. * snd_soc_dapm_enable_pin - enable pin.
  1777. * @codec: SoC codec
  1778. * @pin: pin name
  1779. *
  1780. * Enables input/output pin and its parents or children widgets iff there is
  1781. * a valid audio route and active audio stream.
  1782. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1783. * do any widget power switching.
  1784. */
  1785. int snd_soc_dapm_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1786. {
  1787. return snd_soc_dapm_set_pin(codec, pin, 1);
  1788. }
  1789. EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  1790. /**
  1791. * snd_soc_dapm_force_enable_pin - force a pin to be enabled
  1792. * @codec: SoC codec
  1793. * @pin: pin name
  1794. *
  1795. * Enables input/output pin regardless of any other state. This is
  1796. * intended for use with microphone bias supplies used in microphone
  1797. * jack detection.
  1798. *
  1799. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1800. * do any widget power switching.
  1801. */
  1802. int snd_soc_dapm_force_enable_pin(struct snd_soc_codec *codec, const char *pin)
  1803. {
  1804. struct snd_soc_dapm_widget *w;
  1805. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1806. if (!strcmp(w->name, pin)) {
  1807. pr_debug("dapm: %s: pin %s\n", codec->name, pin);
  1808. w->connected = 1;
  1809. w->force = 1;
  1810. return 0;
  1811. }
  1812. }
  1813. pr_err("dapm: %s: configuring unknown pin %s\n", codec->name, pin);
  1814. return -EINVAL;
  1815. }
  1816. EXPORT_SYMBOL_GPL(snd_soc_dapm_force_enable_pin);
  1817. /**
  1818. * snd_soc_dapm_disable_pin - disable pin.
  1819. * @codec: SoC codec
  1820. * @pin: pin name
  1821. *
  1822. * Disables input/output pin and its parents or children widgets.
  1823. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1824. * do any widget power switching.
  1825. */
  1826. int snd_soc_dapm_disable_pin(struct snd_soc_codec *codec, const char *pin)
  1827. {
  1828. return snd_soc_dapm_set_pin(codec, pin, 0);
  1829. }
  1830. EXPORT_SYMBOL_GPL(snd_soc_dapm_disable_pin);
  1831. /**
  1832. * snd_soc_dapm_nc_pin - permanently disable pin.
  1833. * @codec: SoC codec
  1834. * @pin: pin name
  1835. *
  1836. * Marks the specified pin as being not connected, disabling it along
  1837. * any parent or child widgets. At present this is identical to
  1838. * snd_soc_dapm_disable_pin() but in future it will be extended to do
  1839. * additional things such as disabling controls which only affect
  1840. * paths through the pin.
  1841. *
  1842. * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  1843. * do any widget power switching.
  1844. */
  1845. int snd_soc_dapm_nc_pin(struct snd_soc_codec *codec, const char *pin)
  1846. {
  1847. return snd_soc_dapm_set_pin(codec, pin, 0);
  1848. }
  1849. EXPORT_SYMBOL_GPL(snd_soc_dapm_nc_pin);
  1850. /**
  1851. * snd_soc_dapm_get_pin_status - get audio pin status
  1852. * @codec: audio codec
  1853. * @pin: audio signal pin endpoint (or start point)
  1854. *
  1855. * Get audio pin status - connected or disconnected.
  1856. *
  1857. * Returns 1 for connected otherwise 0.
  1858. */
  1859. int snd_soc_dapm_get_pin_status(struct snd_soc_codec *codec, const char *pin)
  1860. {
  1861. struct snd_soc_dapm_widget *w;
  1862. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1863. if (!strcmp(w->name, pin))
  1864. return w->connected;
  1865. }
  1866. return 0;
  1867. }
  1868. EXPORT_SYMBOL_GPL(snd_soc_dapm_get_pin_status);
  1869. /**
  1870. * snd_soc_dapm_free - free dapm resources
  1871. * @socdev: SoC device
  1872. *
  1873. * Free all dapm widgets and resources.
  1874. */
  1875. void snd_soc_dapm_free(struct snd_soc_device *socdev)
  1876. {
  1877. struct snd_soc_codec *codec = socdev->card->codec;
  1878. snd_soc_dapm_sys_remove(socdev->dev);
  1879. dapm_free_widgets(codec);
  1880. }
  1881. EXPORT_SYMBOL_GPL(snd_soc_dapm_free);
  1882. /*
  1883. * snd_soc_dapm_shutdown - callback for system shutdown
  1884. */
  1885. void snd_soc_dapm_shutdown(struct snd_soc_device *socdev)
  1886. {
  1887. struct snd_soc_codec *codec = socdev->card->codec;
  1888. struct snd_soc_dapm_widget *w;
  1889. LIST_HEAD(down_list);
  1890. int powerdown = 0;
  1891. list_for_each_entry(w, &codec->dapm_widgets, list) {
  1892. if (w->power) {
  1893. dapm_seq_insert(w, &down_list, dapm_down_seq);
  1894. w->power = 0;
  1895. powerdown = 1;
  1896. }
  1897. }
  1898. /* If there were no widgets to power down we're already in
  1899. * standby.
  1900. */
  1901. if (powerdown) {
  1902. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_PREPARE);
  1903. dapm_seq_run(codec, &down_list, 0, dapm_down_seq);
  1904. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_STANDBY);
  1905. }
  1906. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
  1907. }
  1908. /* Module information */
  1909. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1910. MODULE_DESCRIPTION("Dynamic Audio Power Management core for ALSA SoC");
  1911. MODULE_LICENSE("GPL");