soc-dapm.c 52 KB

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