soc-core.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /*
  2. * soc-core.c -- ALSA SoC Audio Layer
  3. *
  4. * Copyright 2005 Wolfson Microelectronics PLC.
  5. * Copyright 2005 Openedhand Ltd.
  6. *
  7. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  8. * with code, comments and ideas from :-
  9. * Richard Purdie <richard@openedhand.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * TODO:
  17. * o Add hw rules to enforce rates, etc.
  18. * o More testing with other codecs/machines.
  19. * o Add more codecs and platforms to ensure good API coverage.
  20. * o Support TDM on PCM and I2S
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/pm.h>
  27. #include <linux/bitops.h>
  28. #include <linux/debugfs.h>
  29. #include <linux/platform_device.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/pcm_params.h>
  33. #include <sound/soc.h>
  34. #include <sound/soc-dapm.h>
  35. #include <sound/initval.h>
  36. static DEFINE_MUTEX(pcm_mutex);
  37. static DEFINE_MUTEX(io_mutex);
  38. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  39. /*
  40. * This is a timeout to do a DAPM powerdown after a stream is closed().
  41. * It can be used to eliminate pops between different playback streams, e.g.
  42. * between two audio tracks.
  43. */
  44. static int pmdown_time = 5000;
  45. module_param(pmdown_time, int, 0);
  46. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  47. /*
  48. * This function forces any delayed work to be queued and run.
  49. */
  50. static int run_delayed_work(struct delayed_work *dwork)
  51. {
  52. int ret;
  53. /* cancel any work waiting to be queued. */
  54. ret = cancel_delayed_work(dwork);
  55. /* if there was any work waiting then we run it now and
  56. * wait for it's completion */
  57. if (ret) {
  58. schedule_delayed_work(dwork, 0);
  59. flush_scheduled_work();
  60. }
  61. return ret;
  62. }
  63. #ifdef CONFIG_SND_SOC_AC97_BUS
  64. /* unregister ac97 codec */
  65. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  66. {
  67. if (codec->ac97->dev.bus)
  68. device_unregister(&codec->ac97->dev);
  69. return 0;
  70. }
  71. /* stop no dev release warning */
  72. static void soc_ac97_device_release(struct device *dev){}
  73. /* register ac97 codec to bus */
  74. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  75. {
  76. int err;
  77. codec->ac97->dev.bus = &ac97_bus_type;
  78. codec->ac97->dev.parent = NULL;
  79. codec->ac97->dev.release = soc_ac97_device_release;
  80. snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
  81. codec->card->number, 0, codec->name);
  82. err = device_register(&codec->ac97->dev);
  83. if (err < 0) {
  84. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  85. codec->ac97->dev.bus = NULL;
  86. return err;
  87. }
  88. return 0;
  89. }
  90. #endif
  91. static inline const char *get_dai_name(int type)
  92. {
  93. switch (type) {
  94. case SND_SOC_DAI_AC97_BUS:
  95. case SND_SOC_DAI_AC97:
  96. return "AC97";
  97. case SND_SOC_DAI_I2S:
  98. return "I2S";
  99. case SND_SOC_DAI_PCM:
  100. return "PCM";
  101. }
  102. return NULL;
  103. }
  104. /*
  105. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  106. * then initialized and any private data can be allocated. This also calls
  107. * startup for the cpu DAI, platform, machine and codec DAI.
  108. */
  109. static int soc_pcm_open(struct snd_pcm_substream *substream)
  110. {
  111. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  112. struct snd_soc_device *socdev = rtd->socdev;
  113. struct snd_pcm_runtime *runtime = substream->runtime;
  114. struct snd_soc_dai_link *machine = rtd->dai;
  115. struct snd_soc_platform *platform = socdev->platform;
  116. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  117. struct snd_soc_dai *codec_dai = machine->codec_dai;
  118. int ret = 0;
  119. mutex_lock(&pcm_mutex);
  120. /* startup the audio subsystem */
  121. if (cpu_dai->ops.startup) {
  122. ret = cpu_dai->ops.startup(substream);
  123. if (ret < 0) {
  124. printk(KERN_ERR "asoc: can't open interface %s\n",
  125. cpu_dai->name);
  126. goto out;
  127. }
  128. }
  129. if (platform->pcm_ops->open) {
  130. ret = platform->pcm_ops->open(substream);
  131. if (ret < 0) {
  132. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  133. goto platform_err;
  134. }
  135. }
  136. if (codec_dai->ops.startup) {
  137. ret = codec_dai->ops.startup(substream);
  138. if (ret < 0) {
  139. printk(KERN_ERR "asoc: can't open codec %s\n",
  140. codec_dai->name);
  141. goto codec_dai_err;
  142. }
  143. }
  144. if (machine->ops && machine->ops->startup) {
  145. ret = machine->ops->startup(substream);
  146. if (ret < 0) {
  147. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  148. goto machine_err;
  149. }
  150. }
  151. /* Check that the codec and cpu DAI's are compatible */
  152. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  153. runtime->hw.rate_min =
  154. max(codec_dai->playback.rate_min,
  155. cpu_dai->playback.rate_min);
  156. runtime->hw.rate_max =
  157. min(codec_dai->playback.rate_max,
  158. cpu_dai->playback.rate_max);
  159. runtime->hw.channels_min =
  160. max(codec_dai->playback.channels_min,
  161. cpu_dai->playback.channels_min);
  162. runtime->hw.channels_max =
  163. min(codec_dai->playback.channels_max,
  164. cpu_dai->playback.channels_max);
  165. runtime->hw.formats =
  166. codec_dai->playback.formats & cpu_dai->playback.formats;
  167. runtime->hw.rates =
  168. codec_dai->playback.rates & cpu_dai->playback.rates;
  169. } else {
  170. runtime->hw.rate_min =
  171. max(codec_dai->capture.rate_min,
  172. cpu_dai->capture.rate_min);
  173. runtime->hw.rate_max =
  174. min(codec_dai->capture.rate_max,
  175. cpu_dai->capture.rate_max);
  176. runtime->hw.channels_min =
  177. max(codec_dai->capture.channels_min,
  178. cpu_dai->capture.channels_min);
  179. runtime->hw.channels_max =
  180. min(codec_dai->capture.channels_max,
  181. cpu_dai->capture.channels_max);
  182. runtime->hw.formats =
  183. codec_dai->capture.formats & cpu_dai->capture.formats;
  184. runtime->hw.rates =
  185. codec_dai->capture.rates & cpu_dai->capture.rates;
  186. }
  187. snd_pcm_limit_hw_rates(runtime);
  188. if (!runtime->hw.rates) {
  189. printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
  190. codec_dai->name, cpu_dai->name);
  191. goto machine_err;
  192. }
  193. if (!runtime->hw.formats) {
  194. printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
  195. codec_dai->name, cpu_dai->name);
  196. goto machine_err;
  197. }
  198. if (!runtime->hw.channels_min || !runtime->hw.channels_max) {
  199. printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
  200. codec_dai->name, cpu_dai->name);
  201. goto machine_err;
  202. }
  203. pr_debug("asoc: %s <-> %s info:\n", codec_dai->name, cpu_dai->name);
  204. pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
  205. pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
  206. runtime->hw.channels_max);
  207. pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
  208. runtime->hw.rate_max);
  209. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  210. cpu_dai->playback.active = codec_dai->playback.active = 1;
  211. else
  212. cpu_dai->capture.active = codec_dai->capture.active = 1;
  213. cpu_dai->active = codec_dai->active = 1;
  214. cpu_dai->runtime = runtime;
  215. socdev->codec->active++;
  216. mutex_unlock(&pcm_mutex);
  217. return 0;
  218. machine_err:
  219. if (machine->ops && machine->ops->shutdown)
  220. machine->ops->shutdown(substream);
  221. codec_dai_err:
  222. if (platform->pcm_ops->close)
  223. platform->pcm_ops->close(substream);
  224. platform_err:
  225. if (cpu_dai->ops.shutdown)
  226. cpu_dai->ops.shutdown(substream);
  227. out:
  228. mutex_unlock(&pcm_mutex);
  229. return ret;
  230. }
  231. /*
  232. * Power down the audio subsystem pmdown_time msecs after close is called.
  233. * This is to ensure there are no pops or clicks in between any music tracks
  234. * due to DAPM power cycling.
  235. */
  236. static void close_delayed_work(struct work_struct *work)
  237. {
  238. struct snd_soc_device *socdev =
  239. container_of(work, struct snd_soc_device, delayed_work.work);
  240. struct snd_soc_codec *codec = socdev->codec;
  241. struct snd_soc_dai *codec_dai;
  242. int i;
  243. mutex_lock(&pcm_mutex);
  244. for (i = 0; i < codec->num_dai; i++) {
  245. codec_dai = &codec->dai[i];
  246. pr_debug("pop wq checking: %s status: %s waiting: %s\n",
  247. codec_dai->playback.stream_name,
  248. codec_dai->playback.active ? "active" : "inactive",
  249. codec_dai->pop_wait ? "yes" : "no");
  250. /* are we waiting on this codec DAI stream */
  251. if (codec_dai->pop_wait == 1) {
  252. /* Reduce power if no longer active */
  253. if (codec->active == 0) {
  254. pr_debug("pop wq D1 %s %s\n", codec->name,
  255. codec_dai->playback.stream_name);
  256. snd_soc_dapm_set_bias_level(socdev,
  257. SND_SOC_BIAS_PREPARE);
  258. }
  259. codec_dai->pop_wait = 0;
  260. snd_soc_dapm_stream_event(codec,
  261. codec_dai->playback.stream_name,
  262. SND_SOC_DAPM_STREAM_STOP);
  263. /* Fall into standby if no longer active */
  264. if (codec->active == 0) {
  265. pr_debug("pop wq D3 %s %s\n", codec->name,
  266. codec_dai->playback.stream_name);
  267. snd_soc_dapm_set_bias_level(socdev,
  268. SND_SOC_BIAS_STANDBY);
  269. }
  270. }
  271. }
  272. mutex_unlock(&pcm_mutex);
  273. }
  274. /*
  275. * Called by ALSA when a PCM substream is closed. Private data can be
  276. * freed here. The cpu DAI, codec DAI, machine and platform are also
  277. * shutdown.
  278. */
  279. static int soc_codec_close(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  282. struct snd_soc_device *socdev = rtd->socdev;
  283. struct snd_soc_dai_link *machine = rtd->dai;
  284. struct snd_soc_platform *platform = socdev->platform;
  285. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  286. struct snd_soc_dai *codec_dai = machine->codec_dai;
  287. struct snd_soc_codec *codec = socdev->codec;
  288. mutex_lock(&pcm_mutex);
  289. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  290. cpu_dai->playback.active = codec_dai->playback.active = 0;
  291. else
  292. cpu_dai->capture.active = codec_dai->capture.active = 0;
  293. if (codec_dai->playback.active == 0 &&
  294. codec_dai->capture.active == 0) {
  295. cpu_dai->active = codec_dai->active = 0;
  296. }
  297. codec->active--;
  298. /* Muting the DAC suppresses artifacts caused during digital
  299. * shutdown, for example from stopping clocks.
  300. */
  301. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  302. snd_soc_dai_digital_mute(codec_dai, 1);
  303. if (cpu_dai->ops.shutdown)
  304. cpu_dai->ops.shutdown(substream);
  305. if (codec_dai->ops.shutdown)
  306. codec_dai->ops.shutdown(substream);
  307. if (machine->ops && machine->ops->shutdown)
  308. machine->ops->shutdown(substream);
  309. if (platform->pcm_ops->close)
  310. platform->pcm_ops->close(substream);
  311. cpu_dai->runtime = NULL;
  312. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  313. /* start delayed pop wq here for playback streams */
  314. codec_dai->pop_wait = 1;
  315. schedule_delayed_work(&socdev->delayed_work,
  316. msecs_to_jiffies(pmdown_time));
  317. } else {
  318. /* capture streams can be powered down now */
  319. snd_soc_dapm_stream_event(codec,
  320. codec_dai->capture.stream_name,
  321. SND_SOC_DAPM_STREAM_STOP);
  322. if (codec->active == 0 && codec_dai->pop_wait == 0)
  323. snd_soc_dapm_set_bias_level(socdev,
  324. SND_SOC_BIAS_STANDBY);
  325. }
  326. mutex_unlock(&pcm_mutex);
  327. return 0;
  328. }
  329. /*
  330. * Called by ALSA when the PCM substream is prepared, can set format, sample
  331. * rate, etc. This function is non atomic and can be called multiple times,
  332. * it can refer to the runtime info.
  333. */
  334. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  335. {
  336. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  337. struct snd_soc_device *socdev = rtd->socdev;
  338. struct snd_soc_dai_link *machine = rtd->dai;
  339. struct snd_soc_platform *platform = socdev->platform;
  340. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  341. struct snd_soc_dai *codec_dai = machine->codec_dai;
  342. struct snd_soc_codec *codec = socdev->codec;
  343. int ret = 0;
  344. mutex_lock(&pcm_mutex);
  345. if (machine->ops && machine->ops->prepare) {
  346. ret = machine->ops->prepare(substream);
  347. if (ret < 0) {
  348. printk(KERN_ERR "asoc: machine prepare error\n");
  349. goto out;
  350. }
  351. }
  352. if (platform->pcm_ops->prepare) {
  353. ret = platform->pcm_ops->prepare(substream);
  354. if (ret < 0) {
  355. printk(KERN_ERR "asoc: platform prepare error\n");
  356. goto out;
  357. }
  358. }
  359. if (codec_dai->ops.prepare) {
  360. ret = codec_dai->ops.prepare(substream);
  361. if (ret < 0) {
  362. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  363. goto out;
  364. }
  365. }
  366. if (cpu_dai->ops.prepare) {
  367. ret = cpu_dai->ops.prepare(substream);
  368. if (ret < 0) {
  369. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  370. goto out;
  371. }
  372. }
  373. /* cancel any delayed stream shutdown that is pending */
  374. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
  375. codec_dai->pop_wait) {
  376. codec_dai->pop_wait = 0;
  377. cancel_delayed_work(&socdev->delayed_work);
  378. }
  379. /* do we need to power up codec */
  380. if (codec->bias_level != SND_SOC_BIAS_ON) {
  381. snd_soc_dapm_set_bias_level(socdev,
  382. SND_SOC_BIAS_PREPARE);
  383. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  384. snd_soc_dapm_stream_event(codec,
  385. codec_dai->playback.stream_name,
  386. SND_SOC_DAPM_STREAM_START);
  387. else
  388. snd_soc_dapm_stream_event(codec,
  389. codec_dai->capture.stream_name,
  390. SND_SOC_DAPM_STREAM_START);
  391. snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_ON);
  392. snd_soc_dai_digital_mute(codec_dai, 0);
  393. } else {
  394. /* codec already powered - power on widgets */
  395. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  396. snd_soc_dapm_stream_event(codec,
  397. codec_dai->playback.stream_name,
  398. SND_SOC_DAPM_STREAM_START);
  399. else
  400. snd_soc_dapm_stream_event(codec,
  401. codec_dai->capture.stream_name,
  402. SND_SOC_DAPM_STREAM_START);
  403. snd_soc_dai_digital_mute(codec_dai, 0);
  404. }
  405. out:
  406. mutex_unlock(&pcm_mutex);
  407. return ret;
  408. }
  409. /*
  410. * Called by ALSA when the hardware params are set by application. This
  411. * function can also be called multiple times and can allocate buffers
  412. * (using snd_pcm_lib_* ). It's non-atomic.
  413. */
  414. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  415. struct snd_pcm_hw_params *params)
  416. {
  417. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  418. struct snd_soc_device *socdev = rtd->socdev;
  419. struct snd_soc_dai_link *machine = rtd->dai;
  420. struct snd_soc_platform *platform = socdev->platform;
  421. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  422. struct snd_soc_dai *codec_dai = machine->codec_dai;
  423. int ret = 0;
  424. mutex_lock(&pcm_mutex);
  425. if (machine->ops && machine->ops->hw_params) {
  426. ret = machine->ops->hw_params(substream, params);
  427. if (ret < 0) {
  428. printk(KERN_ERR "asoc: machine hw_params failed\n");
  429. goto out;
  430. }
  431. }
  432. if (codec_dai->ops.hw_params) {
  433. ret = codec_dai->ops.hw_params(substream, params);
  434. if (ret < 0) {
  435. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  436. codec_dai->name);
  437. goto codec_err;
  438. }
  439. }
  440. if (cpu_dai->ops.hw_params) {
  441. ret = cpu_dai->ops.hw_params(substream, params);
  442. if (ret < 0) {
  443. printk(KERN_ERR "asoc: interface %s hw params failed\n",
  444. cpu_dai->name);
  445. goto interface_err;
  446. }
  447. }
  448. if (platform->pcm_ops->hw_params) {
  449. ret = platform->pcm_ops->hw_params(substream, params);
  450. if (ret < 0) {
  451. printk(KERN_ERR "asoc: platform %s hw params failed\n",
  452. platform->name);
  453. goto platform_err;
  454. }
  455. }
  456. out:
  457. mutex_unlock(&pcm_mutex);
  458. return ret;
  459. platform_err:
  460. if (cpu_dai->ops.hw_free)
  461. cpu_dai->ops.hw_free(substream);
  462. interface_err:
  463. if (codec_dai->ops.hw_free)
  464. codec_dai->ops.hw_free(substream);
  465. codec_err:
  466. if (machine->ops && machine->ops->hw_free)
  467. machine->ops->hw_free(substream);
  468. mutex_unlock(&pcm_mutex);
  469. return ret;
  470. }
  471. /*
  472. * Free's resources allocated by hw_params, can be called multiple times
  473. */
  474. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  475. {
  476. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  477. struct snd_soc_device *socdev = rtd->socdev;
  478. struct snd_soc_dai_link *machine = rtd->dai;
  479. struct snd_soc_platform *platform = socdev->platform;
  480. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  481. struct snd_soc_dai *codec_dai = machine->codec_dai;
  482. struct snd_soc_codec *codec = socdev->codec;
  483. mutex_lock(&pcm_mutex);
  484. /* apply codec digital mute */
  485. if (!codec->active)
  486. snd_soc_dai_digital_mute(codec_dai, 1);
  487. /* free any machine hw params */
  488. if (machine->ops && machine->ops->hw_free)
  489. machine->ops->hw_free(substream);
  490. /* free any DMA resources */
  491. if (platform->pcm_ops->hw_free)
  492. platform->pcm_ops->hw_free(substream);
  493. /* now free hw params for the DAI's */
  494. if (codec_dai->ops.hw_free)
  495. codec_dai->ops.hw_free(substream);
  496. if (cpu_dai->ops.hw_free)
  497. cpu_dai->ops.hw_free(substream);
  498. mutex_unlock(&pcm_mutex);
  499. return 0;
  500. }
  501. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  502. {
  503. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  504. struct snd_soc_device *socdev = rtd->socdev;
  505. struct snd_soc_dai_link *machine = rtd->dai;
  506. struct snd_soc_platform *platform = socdev->platform;
  507. struct snd_soc_dai *cpu_dai = machine->cpu_dai;
  508. struct snd_soc_dai *codec_dai = machine->codec_dai;
  509. int ret;
  510. if (codec_dai->ops.trigger) {
  511. ret = codec_dai->ops.trigger(substream, cmd);
  512. if (ret < 0)
  513. return ret;
  514. }
  515. if (platform->pcm_ops->trigger) {
  516. ret = platform->pcm_ops->trigger(substream, cmd);
  517. if (ret < 0)
  518. return ret;
  519. }
  520. if (cpu_dai->ops.trigger) {
  521. ret = cpu_dai->ops.trigger(substream, cmd);
  522. if (ret < 0)
  523. return ret;
  524. }
  525. return 0;
  526. }
  527. /* ASoC PCM operations */
  528. static struct snd_pcm_ops soc_pcm_ops = {
  529. .open = soc_pcm_open,
  530. .close = soc_codec_close,
  531. .hw_params = soc_pcm_hw_params,
  532. .hw_free = soc_pcm_hw_free,
  533. .prepare = soc_pcm_prepare,
  534. .trigger = soc_pcm_trigger,
  535. };
  536. #ifdef CONFIG_PM
  537. /* powers down audio subsystem for suspend */
  538. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  539. {
  540. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  541. struct snd_soc_machine *machine = socdev->machine;
  542. struct snd_soc_platform *platform = socdev->platform;
  543. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  544. struct snd_soc_codec *codec = socdev->codec;
  545. int i;
  546. /* Due to the resume being scheduled into a workqueue we could
  547. * suspend before that's finished - wait for it to complete.
  548. */
  549. snd_power_lock(codec->card);
  550. snd_power_wait(codec->card, SNDRV_CTL_POWER_D0);
  551. snd_power_unlock(codec->card);
  552. /* we're going to block userspace touching us until resume completes */
  553. snd_power_change_state(codec->card, SNDRV_CTL_POWER_D3hot);
  554. /* mute any active DAC's */
  555. for (i = 0; i < machine->num_links; i++) {
  556. struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
  557. if (dai->dai_ops.digital_mute && dai->playback.active)
  558. dai->dai_ops.digital_mute(dai, 1);
  559. }
  560. /* suspend all pcms */
  561. for (i = 0; i < machine->num_links; i++)
  562. snd_pcm_suspend_all(machine->dai_link[i].pcm);
  563. if (machine->suspend_pre)
  564. machine->suspend_pre(pdev, state);
  565. for (i = 0; i < machine->num_links; i++) {
  566. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  567. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  568. cpu_dai->suspend(pdev, cpu_dai);
  569. if (platform->suspend)
  570. platform->suspend(pdev, cpu_dai);
  571. }
  572. /* close any waiting streams and save state */
  573. run_delayed_work(&socdev->delayed_work);
  574. codec->suspend_bias_level = codec->bias_level;
  575. for (i = 0; i < codec->num_dai; i++) {
  576. char *stream = codec->dai[i].playback.stream_name;
  577. if (stream != NULL)
  578. snd_soc_dapm_stream_event(codec, stream,
  579. SND_SOC_DAPM_STREAM_SUSPEND);
  580. stream = codec->dai[i].capture.stream_name;
  581. if (stream != NULL)
  582. snd_soc_dapm_stream_event(codec, stream,
  583. SND_SOC_DAPM_STREAM_SUSPEND);
  584. }
  585. if (codec_dev->suspend)
  586. codec_dev->suspend(pdev, state);
  587. for (i = 0; i < machine->num_links; i++) {
  588. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  589. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  590. cpu_dai->suspend(pdev, cpu_dai);
  591. }
  592. if (machine->suspend_post)
  593. machine->suspend_post(pdev, state);
  594. return 0;
  595. }
  596. /* deferred resume work, so resume can complete before we finished
  597. * setting our codec back up, which can be very slow on I2C
  598. */
  599. static void soc_resume_deferred(struct work_struct *work)
  600. {
  601. struct snd_soc_device *socdev = container_of(work,
  602. struct snd_soc_device,
  603. deferred_resume_work);
  604. struct snd_soc_machine *machine = socdev->machine;
  605. struct snd_soc_platform *platform = socdev->platform;
  606. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  607. struct snd_soc_codec *codec = socdev->codec;
  608. struct platform_device *pdev = to_platform_device(socdev->dev);
  609. int i;
  610. /* our power state is still SNDRV_CTL_POWER_D3hot from suspend time,
  611. * so userspace apps are blocked from touching us
  612. */
  613. dev_info(socdev->dev, "starting resume work\n");
  614. if (machine->resume_pre)
  615. machine->resume_pre(pdev);
  616. for (i = 0; i < machine->num_links; i++) {
  617. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  618. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  619. cpu_dai->resume(pdev, cpu_dai);
  620. }
  621. if (codec_dev->resume)
  622. codec_dev->resume(pdev);
  623. for (i = 0; i < codec->num_dai; i++) {
  624. char *stream = codec->dai[i].playback.stream_name;
  625. if (stream != NULL)
  626. snd_soc_dapm_stream_event(codec, stream,
  627. SND_SOC_DAPM_STREAM_RESUME);
  628. stream = codec->dai[i].capture.stream_name;
  629. if (stream != NULL)
  630. snd_soc_dapm_stream_event(codec, stream,
  631. SND_SOC_DAPM_STREAM_RESUME);
  632. }
  633. /* unmute any active DACs */
  634. for (i = 0; i < machine->num_links; i++) {
  635. struct snd_soc_dai *dai = machine->dai_link[i].codec_dai;
  636. if (dai->dai_ops.digital_mute && dai->playback.active)
  637. dai->dai_ops.digital_mute(dai, 0);
  638. }
  639. for (i = 0; i < machine->num_links; i++) {
  640. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  641. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  642. cpu_dai->resume(pdev, cpu_dai);
  643. if (platform->resume)
  644. platform->resume(pdev, cpu_dai);
  645. }
  646. if (machine->resume_post)
  647. machine->resume_post(pdev);
  648. dev_info(socdev->dev, "resume work completed\n");
  649. /* userspace can access us now we are back as we were before */
  650. snd_power_change_state(codec->card, SNDRV_CTL_POWER_D0);
  651. }
  652. /* powers up audio subsystem after a suspend */
  653. static int soc_resume(struct platform_device *pdev)
  654. {
  655. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  656. dev_info(socdev->dev, "scheduling resume work\n");
  657. if (!schedule_work(&socdev->deferred_resume_work))
  658. dev_err(socdev->dev, "work item may be lost\n");
  659. return 0;
  660. }
  661. #else
  662. #define soc_suspend NULL
  663. #define soc_resume NULL
  664. #endif
  665. /* probes a new socdev */
  666. static int soc_probe(struct platform_device *pdev)
  667. {
  668. int ret = 0, i;
  669. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  670. struct snd_soc_machine *machine = socdev->machine;
  671. struct snd_soc_platform *platform = socdev->platform;
  672. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  673. if (machine->probe) {
  674. ret = machine->probe(pdev);
  675. if (ret < 0)
  676. return ret;
  677. }
  678. for (i = 0; i < machine->num_links; i++) {
  679. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  680. if (cpu_dai->probe) {
  681. ret = cpu_dai->probe(pdev, cpu_dai);
  682. if (ret < 0)
  683. goto cpu_dai_err;
  684. }
  685. }
  686. if (codec_dev->probe) {
  687. ret = codec_dev->probe(pdev);
  688. if (ret < 0)
  689. goto cpu_dai_err;
  690. }
  691. if (platform->probe) {
  692. ret = platform->probe(pdev);
  693. if (ret < 0)
  694. goto platform_err;
  695. }
  696. /* DAPM stream work */
  697. INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
  698. #ifdef CONFIG_PM
  699. /* deferred resume work */
  700. INIT_WORK(&socdev->deferred_resume_work, soc_resume_deferred);
  701. #endif
  702. return 0;
  703. platform_err:
  704. if (codec_dev->remove)
  705. codec_dev->remove(pdev);
  706. cpu_dai_err:
  707. for (i--; i >= 0; i--) {
  708. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  709. if (cpu_dai->remove)
  710. cpu_dai->remove(pdev, cpu_dai);
  711. }
  712. if (machine->remove)
  713. machine->remove(pdev);
  714. return ret;
  715. }
  716. /* removes a socdev */
  717. static int soc_remove(struct platform_device *pdev)
  718. {
  719. int i;
  720. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  721. struct snd_soc_machine *machine = socdev->machine;
  722. struct snd_soc_platform *platform = socdev->platform;
  723. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  724. run_delayed_work(&socdev->delayed_work);
  725. if (platform->remove)
  726. platform->remove(pdev);
  727. if (codec_dev->remove)
  728. codec_dev->remove(pdev);
  729. for (i = 0; i < machine->num_links; i++) {
  730. struct snd_soc_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  731. if (cpu_dai->remove)
  732. cpu_dai->remove(pdev, cpu_dai);
  733. }
  734. if (machine->remove)
  735. machine->remove(pdev);
  736. return 0;
  737. }
  738. /* ASoC platform driver */
  739. static struct platform_driver soc_driver = {
  740. .driver = {
  741. .name = "soc-audio",
  742. .owner = THIS_MODULE,
  743. },
  744. .probe = soc_probe,
  745. .remove = soc_remove,
  746. .suspend = soc_suspend,
  747. .resume = soc_resume,
  748. };
  749. /* create a new pcm */
  750. static int soc_new_pcm(struct snd_soc_device *socdev,
  751. struct snd_soc_dai_link *dai_link, int num)
  752. {
  753. struct snd_soc_codec *codec = socdev->codec;
  754. struct snd_soc_dai *codec_dai = dai_link->codec_dai;
  755. struct snd_soc_dai *cpu_dai = dai_link->cpu_dai;
  756. struct snd_soc_pcm_runtime *rtd;
  757. struct snd_pcm *pcm;
  758. char new_name[64];
  759. int ret = 0, playback = 0, capture = 0;
  760. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  761. if (rtd == NULL)
  762. return -ENOMEM;
  763. rtd->dai = dai_link;
  764. rtd->socdev = socdev;
  765. codec_dai->codec = socdev->codec;
  766. /* check client and interface hw capabilities */
  767. sprintf(new_name, "%s %s-%s-%d", dai_link->stream_name, codec_dai->name,
  768. get_dai_name(cpu_dai->type), num);
  769. if (codec_dai->playback.channels_min)
  770. playback = 1;
  771. if (codec_dai->capture.channels_min)
  772. capture = 1;
  773. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  774. capture, &pcm);
  775. if (ret < 0) {
  776. printk(KERN_ERR "asoc: can't create pcm for codec %s\n",
  777. codec->name);
  778. kfree(rtd);
  779. return ret;
  780. }
  781. dai_link->pcm = pcm;
  782. pcm->private_data = rtd;
  783. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  784. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  785. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  786. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  787. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  788. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  789. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  790. if (playback)
  791. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  792. if (capture)
  793. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  794. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  795. if (ret < 0) {
  796. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  797. kfree(rtd);
  798. return ret;
  799. }
  800. pcm->private_free = socdev->platform->pcm_free;
  801. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  802. cpu_dai->name);
  803. return ret;
  804. }
  805. /* codec register dump */
  806. static ssize_t soc_codec_reg_show(struct snd_soc_device *devdata, char *buf)
  807. {
  808. struct snd_soc_codec *codec = devdata->codec;
  809. int i, step = 1, count = 0;
  810. if (!codec->reg_cache_size)
  811. return 0;
  812. if (codec->reg_cache_step)
  813. step = codec->reg_cache_step;
  814. count += sprintf(buf, "%s registers\n", codec->name);
  815. for (i = 0; i < codec->reg_cache_size; i += step) {
  816. count += sprintf(buf + count, "%2x: ", i);
  817. if (count >= PAGE_SIZE - 1)
  818. break;
  819. if (codec->display_register)
  820. count += codec->display_register(codec, buf + count,
  821. PAGE_SIZE - count, i);
  822. else
  823. count += snprintf(buf + count, PAGE_SIZE - count,
  824. "%4x", codec->read(codec, i));
  825. if (count >= PAGE_SIZE - 1)
  826. break;
  827. count += snprintf(buf + count, PAGE_SIZE - count, "\n");
  828. if (count >= PAGE_SIZE - 1)
  829. break;
  830. }
  831. /* Truncate count; min() would cause a warning */
  832. if (count >= PAGE_SIZE)
  833. count = PAGE_SIZE - 1;
  834. return count;
  835. }
  836. static ssize_t codec_reg_show(struct device *dev,
  837. struct device_attribute *attr, char *buf)
  838. {
  839. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  840. return soc_codec_reg_show(devdata, buf);
  841. }
  842. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  843. #ifdef CONFIG_DEBUG_FS
  844. static int codec_reg_open_file(struct inode *inode, struct file *file)
  845. {
  846. file->private_data = inode->i_private;
  847. return 0;
  848. }
  849. static ssize_t codec_reg_read_file(struct file *file, char __user *user_buf,
  850. size_t count, loff_t *ppos)
  851. {
  852. ssize_t ret;
  853. struct snd_soc_device *devdata = file->private_data;
  854. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  855. if (!buf)
  856. return -ENOMEM;
  857. ret = soc_codec_reg_show(devdata, buf);
  858. if (ret >= 0)
  859. ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
  860. kfree(buf);
  861. return ret;
  862. }
  863. static ssize_t codec_reg_write_file(struct file *file,
  864. const char __user *user_buf, size_t count, loff_t *ppos)
  865. {
  866. char buf[32];
  867. int buf_size;
  868. char *start = buf;
  869. unsigned long reg, value;
  870. int step = 1;
  871. struct snd_soc_device *devdata = file->private_data;
  872. struct snd_soc_codec *codec = devdata->codec;
  873. buf_size = min(count, (sizeof(buf)-1));
  874. if (copy_from_user(buf, user_buf, buf_size))
  875. return -EFAULT;
  876. buf[buf_size] = 0;
  877. if (codec->reg_cache_step)
  878. step = codec->reg_cache_step;
  879. while (*start == ' ')
  880. start++;
  881. reg = simple_strtoul(start, &start, 16);
  882. if ((reg >= codec->reg_cache_size) || (reg % step))
  883. return -EINVAL;
  884. while (*start == ' ')
  885. start++;
  886. if (strict_strtoul(start, 16, &value))
  887. return -EINVAL;
  888. codec->write(codec, reg, value);
  889. return buf_size;
  890. }
  891. static const struct file_operations codec_reg_fops = {
  892. .open = codec_reg_open_file,
  893. .read = codec_reg_read_file,
  894. .write = codec_reg_write_file,
  895. };
  896. static void soc_init_debugfs(struct snd_soc_device *socdev)
  897. {
  898. struct dentry *root, *file;
  899. struct snd_soc_codec *codec = socdev->codec;
  900. root = debugfs_create_dir(dev_name(socdev->dev), NULL);
  901. if (IS_ERR(root) || !root)
  902. goto exit1;
  903. file = debugfs_create_file("codec_reg", 0644,
  904. root, socdev, &codec_reg_fops);
  905. if (!file)
  906. goto exit2;
  907. file = debugfs_create_u32("dapm_pop_time", 0744,
  908. root, &codec->pop_time);
  909. if (!file)
  910. goto exit2;
  911. socdev->debugfs_root = root;
  912. return;
  913. exit2:
  914. debugfs_remove_recursive(root);
  915. exit1:
  916. dev_err(socdev->dev, "debugfs is not available\n");
  917. }
  918. static void soc_cleanup_debugfs(struct snd_soc_device *socdev)
  919. {
  920. debugfs_remove_recursive(socdev->debugfs_root);
  921. socdev->debugfs_root = NULL;
  922. }
  923. #else
  924. static inline void soc_init_debugfs(struct snd_soc_device *socdev)
  925. {
  926. }
  927. static inline void soc_cleanup_debugfs(struct snd_soc_device *socdev)
  928. {
  929. }
  930. #endif
  931. /**
  932. * snd_soc_new_ac97_codec - initailise AC97 device
  933. * @codec: audio codec
  934. * @ops: AC97 bus operations
  935. * @num: AC97 codec number
  936. *
  937. * Initialises AC97 codec resources for use by ad-hoc devices only.
  938. */
  939. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  940. struct snd_ac97_bus_ops *ops, int num)
  941. {
  942. mutex_lock(&codec->mutex);
  943. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  944. if (codec->ac97 == NULL) {
  945. mutex_unlock(&codec->mutex);
  946. return -ENOMEM;
  947. }
  948. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  949. if (codec->ac97->bus == NULL) {
  950. kfree(codec->ac97);
  951. codec->ac97 = NULL;
  952. mutex_unlock(&codec->mutex);
  953. return -ENOMEM;
  954. }
  955. codec->ac97->bus->ops = ops;
  956. codec->ac97->num = num;
  957. mutex_unlock(&codec->mutex);
  958. return 0;
  959. }
  960. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  961. /**
  962. * snd_soc_free_ac97_codec - free AC97 codec device
  963. * @codec: audio codec
  964. *
  965. * Frees AC97 codec device resources.
  966. */
  967. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  968. {
  969. mutex_lock(&codec->mutex);
  970. kfree(codec->ac97->bus);
  971. kfree(codec->ac97);
  972. codec->ac97 = NULL;
  973. mutex_unlock(&codec->mutex);
  974. }
  975. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  976. /**
  977. * snd_soc_update_bits - update codec register bits
  978. * @codec: audio codec
  979. * @reg: codec register
  980. * @mask: register mask
  981. * @value: new value
  982. *
  983. * Writes new register value.
  984. *
  985. * Returns 1 for change else 0.
  986. */
  987. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  988. unsigned short mask, unsigned short value)
  989. {
  990. int change;
  991. unsigned short old, new;
  992. mutex_lock(&io_mutex);
  993. old = snd_soc_read(codec, reg);
  994. new = (old & ~mask) | value;
  995. change = old != new;
  996. if (change)
  997. snd_soc_write(codec, reg, new);
  998. mutex_unlock(&io_mutex);
  999. return change;
  1000. }
  1001. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  1002. /**
  1003. * snd_soc_test_bits - test register for change
  1004. * @codec: audio codec
  1005. * @reg: codec register
  1006. * @mask: register mask
  1007. * @value: new value
  1008. *
  1009. * Tests a register with a new value and checks if the new value is
  1010. * different from the old value.
  1011. *
  1012. * Returns 1 for change else 0.
  1013. */
  1014. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  1015. unsigned short mask, unsigned short value)
  1016. {
  1017. int change;
  1018. unsigned short old, new;
  1019. mutex_lock(&io_mutex);
  1020. old = snd_soc_read(codec, reg);
  1021. new = (old & ~mask) | value;
  1022. change = old != new;
  1023. mutex_unlock(&io_mutex);
  1024. return change;
  1025. }
  1026. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  1027. /**
  1028. * snd_soc_new_pcms - create new sound card and pcms
  1029. * @socdev: the SoC audio device
  1030. *
  1031. * Create a new sound card based upon the codec and interface pcms.
  1032. *
  1033. * Returns 0 for success, else error.
  1034. */
  1035. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  1036. {
  1037. struct snd_soc_codec *codec = socdev->codec;
  1038. struct snd_soc_machine *machine = socdev->machine;
  1039. int ret = 0, i;
  1040. mutex_lock(&codec->mutex);
  1041. /* register a sound card */
  1042. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  1043. if (!codec->card) {
  1044. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  1045. codec->name);
  1046. mutex_unlock(&codec->mutex);
  1047. return -ENODEV;
  1048. }
  1049. codec->card->dev = socdev->dev;
  1050. codec->card->private_data = codec;
  1051. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  1052. /* create the pcms */
  1053. for (i = 0; i < machine->num_links; i++) {
  1054. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  1055. if (ret < 0) {
  1056. printk(KERN_ERR "asoc: can't create pcm %s\n",
  1057. machine->dai_link[i].stream_name);
  1058. mutex_unlock(&codec->mutex);
  1059. return ret;
  1060. }
  1061. }
  1062. mutex_unlock(&codec->mutex);
  1063. return ret;
  1064. }
  1065. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  1066. /**
  1067. * snd_soc_register_card - register sound card
  1068. * @socdev: the SoC audio device
  1069. *
  1070. * Register a SoC sound card. Also registers an AC97 device if the
  1071. * codec is AC97 for ad hoc devices.
  1072. *
  1073. * Returns 0 for success, else error.
  1074. */
  1075. int snd_soc_register_card(struct snd_soc_device *socdev)
  1076. {
  1077. struct snd_soc_codec *codec = socdev->codec;
  1078. struct snd_soc_machine *machine = socdev->machine;
  1079. int ret = 0, i, ac97 = 0, err = 0;
  1080. for (i = 0; i < machine->num_links; i++) {
  1081. if (socdev->machine->dai_link[i].init) {
  1082. err = socdev->machine->dai_link[i].init(codec);
  1083. if (err < 0) {
  1084. printk(KERN_ERR "asoc: failed to init %s\n",
  1085. socdev->machine->dai_link[i].stream_name);
  1086. continue;
  1087. }
  1088. }
  1089. if (socdev->machine->dai_link[i].codec_dai->type ==
  1090. SND_SOC_DAI_AC97_BUS)
  1091. ac97 = 1;
  1092. }
  1093. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  1094. "%s", machine->name);
  1095. snprintf(codec->card->longname, sizeof(codec->card->longname),
  1096. "%s (%s)", machine->name, codec->name);
  1097. ret = snd_card_register(codec->card);
  1098. if (ret < 0) {
  1099. printk(KERN_ERR "asoc: failed to register soundcard for %s\n",
  1100. codec->name);
  1101. goto out;
  1102. }
  1103. mutex_lock(&codec->mutex);
  1104. #ifdef CONFIG_SND_SOC_AC97_BUS
  1105. if (ac97) {
  1106. ret = soc_ac97_dev_register(codec);
  1107. if (ret < 0) {
  1108. printk(KERN_ERR "asoc: AC97 device register failed\n");
  1109. snd_card_free(codec->card);
  1110. mutex_unlock(&codec->mutex);
  1111. goto out;
  1112. }
  1113. }
  1114. #endif
  1115. err = snd_soc_dapm_sys_add(socdev->dev);
  1116. if (err < 0)
  1117. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  1118. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  1119. if (err < 0)
  1120. printk(KERN_WARNING "asoc: failed to add codec sysfs files\n");
  1121. soc_init_debugfs(socdev);
  1122. mutex_unlock(&codec->mutex);
  1123. out:
  1124. return ret;
  1125. }
  1126. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  1127. /**
  1128. * snd_soc_free_pcms - free sound card and pcms
  1129. * @socdev: the SoC audio device
  1130. *
  1131. * Frees sound card and pcms associated with the socdev.
  1132. * Also unregister the codec if it is an AC97 device.
  1133. */
  1134. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  1135. {
  1136. struct snd_soc_codec *codec = socdev->codec;
  1137. #ifdef CONFIG_SND_SOC_AC97_BUS
  1138. struct snd_soc_dai *codec_dai;
  1139. int i;
  1140. #endif
  1141. mutex_lock(&codec->mutex);
  1142. soc_cleanup_debugfs(socdev);
  1143. #ifdef CONFIG_SND_SOC_AC97_BUS
  1144. for (i = 0; i < codec->num_dai; i++) {
  1145. codec_dai = &codec->dai[i];
  1146. if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
  1147. soc_ac97_dev_unregister(codec);
  1148. goto free_card;
  1149. }
  1150. }
  1151. free_card:
  1152. #endif
  1153. if (codec->card)
  1154. snd_card_free(codec->card);
  1155. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1156. mutex_unlock(&codec->mutex);
  1157. }
  1158. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1159. /**
  1160. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1161. * @substream: the pcm substream
  1162. * @hw: the hardware parameters
  1163. *
  1164. * Sets the substream runtime hardware parameters.
  1165. */
  1166. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1167. const struct snd_pcm_hardware *hw)
  1168. {
  1169. struct snd_pcm_runtime *runtime = substream->runtime;
  1170. runtime->hw.info = hw->info;
  1171. runtime->hw.formats = hw->formats;
  1172. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1173. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1174. runtime->hw.periods_min = hw->periods_min;
  1175. runtime->hw.periods_max = hw->periods_max;
  1176. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1177. runtime->hw.fifo_size = hw->fifo_size;
  1178. return 0;
  1179. }
  1180. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1181. /**
  1182. * snd_soc_cnew - create new control
  1183. * @_template: control template
  1184. * @data: control private data
  1185. * @lnng_name: control long name
  1186. *
  1187. * Create a new mixer control from a template control.
  1188. *
  1189. * Returns 0 for success, else error.
  1190. */
  1191. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1192. void *data, char *long_name)
  1193. {
  1194. struct snd_kcontrol_new template;
  1195. memcpy(&template, _template, sizeof(template));
  1196. if (long_name)
  1197. template.name = long_name;
  1198. template.index = 0;
  1199. return snd_ctl_new1(&template, data);
  1200. }
  1201. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1202. /**
  1203. * snd_soc_info_enum_double - enumerated double mixer info callback
  1204. * @kcontrol: mixer control
  1205. * @uinfo: control element information
  1206. *
  1207. * Callback to provide information about a double enumerated
  1208. * mixer control.
  1209. *
  1210. * Returns 0 for success.
  1211. */
  1212. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1213. struct snd_ctl_elem_info *uinfo)
  1214. {
  1215. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1216. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1217. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1218. uinfo->value.enumerated.items = e->max;
  1219. if (uinfo->value.enumerated.item > e->max - 1)
  1220. uinfo->value.enumerated.item = e->max - 1;
  1221. strcpy(uinfo->value.enumerated.name,
  1222. e->texts[uinfo->value.enumerated.item]);
  1223. return 0;
  1224. }
  1225. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1226. /**
  1227. * snd_soc_get_enum_double - enumerated double mixer get callback
  1228. * @kcontrol: mixer control
  1229. * @uinfo: control element information
  1230. *
  1231. * Callback to get the value of a double enumerated mixer.
  1232. *
  1233. * Returns 0 for success.
  1234. */
  1235. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1236. struct snd_ctl_elem_value *ucontrol)
  1237. {
  1238. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1239. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1240. unsigned short val, bitmask;
  1241. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1242. ;
  1243. val = snd_soc_read(codec, e->reg);
  1244. ucontrol->value.enumerated.item[0]
  1245. = (val >> e->shift_l) & (bitmask - 1);
  1246. if (e->shift_l != e->shift_r)
  1247. ucontrol->value.enumerated.item[1] =
  1248. (val >> e->shift_r) & (bitmask - 1);
  1249. return 0;
  1250. }
  1251. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1252. /**
  1253. * snd_soc_put_enum_double - enumerated double mixer put callback
  1254. * @kcontrol: mixer control
  1255. * @uinfo: control element information
  1256. *
  1257. * Callback to set the value of a double enumerated mixer.
  1258. *
  1259. * Returns 0 for success.
  1260. */
  1261. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1262. struct snd_ctl_elem_value *ucontrol)
  1263. {
  1264. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1265. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1266. unsigned short val;
  1267. unsigned short mask, bitmask;
  1268. for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
  1269. ;
  1270. if (ucontrol->value.enumerated.item[0] > e->max - 1)
  1271. return -EINVAL;
  1272. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1273. mask = (bitmask - 1) << e->shift_l;
  1274. if (e->shift_l != e->shift_r) {
  1275. if (ucontrol->value.enumerated.item[1] > e->max - 1)
  1276. return -EINVAL;
  1277. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1278. mask |= (bitmask - 1) << e->shift_r;
  1279. }
  1280. return snd_soc_update_bits(codec, e->reg, mask, val);
  1281. }
  1282. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1283. /**
  1284. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1285. * @kcontrol: mixer control
  1286. * @uinfo: control element information
  1287. *
  1288. * Callback to provide information about an external enumerated
  1289. * single mixer.
  1290. *
  1291. * Returns 0 for success.
  1292. */
  1293. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1294. struct snd_ctl_elem_info *uinfo)
  1295. {
  1296. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1297. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1298. uinfo->count = 1;
  1299. uinfo->value.enumerated.items = e->max;
  1300. if (uinfo->value.enumerated.item > e->max - 1)
  1301. uinfo->value.enumerated.item = e->max - 1;
  1302. strcpy(uinfo->value.enumerated.name,
  1303. e->texts[uinfo->value.enumerated.item]);
  1304. return 0;
  1305. }
  1306. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1307. /**
  1308. * snd_soc_info_volsw_ext - external single mixer info callback
  1309. * @kcontrol: mixer control
  1310. * @uinfo: control element information
  1311. *
  1312. * Callback to provide information about a single external mixer control.
  1313. *
  1314. * Returns 0 for success.
  1315. */
  1316. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1317. struct snd_ctl_elem_info *uinfo)
  1318. {
  1319. int max = kcontrol->private_value;
  1320. if (max == 1)
  1321. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1322. else
  1323. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1324. uinfo->count = 1;
  1325. uinfo->value.integer.min = 0;
  1326. uinfo->value.integer.max = max;
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1330. /**
  1331. * snd_soc_info_volsw - single mixer info callback
  1332. * @kcontrol: mixer control
  1333. * @uinfo: control element information
  1334. *
  1335. * Callback to provide information about a single mixer control.
  1336. *
  1337. * Returns 0 for success.
  1338. */
  1339. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1340. struct snd_ctl_elem_info *uinfo)
  1341. {
  1342. struct soc_mixer_control *mc =
  1343. (struct soc_mixer_control *)kcontrol->private_value;
  1344. int max = mc->max;
  1345. unsigned int shift = mc->shift;
  1346. unsigned int rshift = mc->rshift;
  1347. if (max == 1)
  1348. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1349. else
  1350. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1351. uinfo->count = shift == rshift ? 1 : 2;
  1352. uinfo->value.integer.min = 0;
  1353. uinfo->value.integer.max = max;
  1354. return 0;
  1355. }
  1356. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1357. /**
  1358. * snd_soc_get_volsw - single mixer get callback
  1359. * @kcontrol: mixer control
  1360. * @uinfo: control element information
  1361. *
  1362. * Callback to get the value of a single mixer control.
  1363. *
  1364. * Returns 0 for success.
  1365. */
  1366. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1367. struct snd_ctl_elem_value *ucontrol)
  1368. {
  1369. struct soc_mixer_control *mc =
  1370. (struct soc_mixer_control *)kcontrol->private_value;
  1371. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1372. unsigned int reg = mc->reg;
  1373. unsigned int shift = mc->shift;
  1374. unsigned int rshift = mc->rshift;
  1375. int max = mc->max;
  1376. unsigned int mask = (1 << fls(max)) - 1;
  1377. unsigned int invert = mc->invert;
  1378. ucontrol->value.integer.value[0] =
  1379. (snd_soc_read(codec, reg) >> shift) & mask;
  1380. if (shift != rshift)
  1381. ucontrol->value.integer.value[1] =
  1382. (snd_soc_read(codec, reg) >> rshift) & mask;
  1383. if (invert) {
  1384. ucontrol->value.integer.value[0] =
  1385. max - ucontrol->value.integer.value[0];
  1386. if (shift != rshift)
  1387. ucontrol->value.integer.value[1] =
  1388. max - ucontrol->value.integer.value[1];
  1389. }
  1390. return 0;
  1391. }
  1392. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1393. /**
  1394. * snd_soc_put_volsw - single mixer put callback
  1395. * @kcontrol: mixer control
  1396. * @uinfo: control element information
  1397. *
  1398. * Callback to set the value of a single mixer control.
  1399. *
  1400. * Returns 0 for success.
  1401. */
  1402. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1403. struct snd_ctl_elem_value *ucontrol)
  1404. {
  1405. struct soc_mixer_control *mc =
  1406. (struct soc_mixer_control *)kcontrol->private_value;
  1407. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1408. unsigned int reg = mc->reg;
  1409. unsigned int shift = mc->shift;
  1410. unsigned int rshift = mc->rshift;
  1411. int max = mc->max;
  1412. unsigned int mask = (1 << fls(max)) - 1;
  1413. unsigned int invert = mc->invert;
  1414. unsigned short val, val2, val_mask;
  1415. val = (ucontrol->value.integer.value[0] & mask);
  1416. if (invert)
  1417. val = max - val;
  1418. val_mask = mask << shift;
  1419. val = val << shift;
  1420. if (shift != rshift) {
  1421. val2 = (ucontrol->value.integer.value[1] & mask);
  1422. if (invert)
  1423. val2 = max - val2;
  1424. val_mask |= mask << rshift;
  1425. val |= val2 << rshift;
  1426. }
  1427. return snd_soc_update_bits(codec, reg, val_mask, val);
  1428. }
  1429. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1430. /**
  1431. * snd_soc_info_volsw_2r - double mixer info callback
  1432. * @kcontrol: mixer control
  1433. * @uinfo: control element information
  1434. *
  1435. * Callback to provide information about a double mixer control that
  1436. * spans 2 codec registers.
  1437. *
  1438. * Returns 0 for success.
  1439. */
  1440. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1441. struct snd_ctl_elem_info *uinfo)
  1442. {
  1443. struct soc_mixer_control *mc =
  1444. (struct soc_mixer_control *)kcontrol->private_value;
  1445. int max = mc->max;
  1446. if (max == 1)
  1447. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1448. else
  1449. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1450. uinfo->count = 2;
  1451. uinfo->value.integer.min = 0;
  1452. uinfo->value.integer.max = max;
  1453. return 0;
  1454. }
  1455. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1456. /**
  1457. * snd_soc_get_volsw_2r - double mixer get callback
  1458. * @kcontrol: mixer control
  1459. * @uinfo: control element information
  1460. *
  1461. * Callback to get the value of a double mixer control that spans 2 registers.
  1462. *
  1463. * Returns 0 for success.
  1464. */
  1465. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1466. struct snd_ctl_elem_value *ucontrol)
  1467. {
  1468. struct soc_mixer_control *mc =
  1469. (struct soc_mixer_control *)kcontrol->private_value;
  1470. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1471. unsigned int reg = mc->reg;
  1472. unsigned int reg2 = mc->rreg;
  1473. unsigned int shift = mc->shift;
  1474. int max = mc->max;
  1475. unsigned int mask = (1<<fls(max))-1;
  1476. unsigned int invert = mc->invert;
  1477. ucontrol->value.integer.value[0] =
  1478. (snd_soc_read(codec, reg) >> shift) & mask;
  1479. ucontrol->value.integer.value[1] =
  1480. (snd_soc_read(codec, reg2) >> shift) & mask;
  1481. if (invert) {
  1482. ucontrol->value.integer.value[0] =
  1483. max - ucontrol->value.integer.value[0];
  1484. ucontrol->value.integer.value[1] =
  1485. max - ucontrol->value.integer.value[1];
  1486. }
  1487. return 0;
  1488. }
  1489. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1490. /**
  1491. * snd_soc_put_volsw_2r - double mixer set callback
  1492. * @kcontrol: mixer control
  1493. * @uinfo: control element information
  1494. *
  1495. * Callback to set the value of a double mixer control that spans 2 registers.
  1496. *
  1497. * Returns 0 for success.
  1498. */
  1499. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1500. struct snd_ctl_elem_value *ucontrol)
  1501. {
  1502. struct soc_mixer_control *mc =
  1503. (struct soc_mixer_control *)kcontrol->private_value;
  1504. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1505. unsigned int reg = mc->reg;
  1506. unsigned int reg2 = mc->rreg;
  1507. unsigned int shift = mc->shift;
  1508. int max = mc->max;
  1509. unsigned int mask = (1 << fls(max)) - 1;
  1510. unsigned int invert = mc->invert;
  1511. int err;
  1512. unsigned short val, val2, val_mask;
  1513. val_mask = mask << shift;
  1514. val = (ucontrol->value.integer.value[0] & mask);
  1515. val2 = (ucontrol->value.integer.value[1] & mask);
  1516. if (invert) {
  1517. val = max - val;
  1518. val2 = max - val2;
  1519. }
  1520. val = val << shift;
  1521. val2 = val2 << shift;
  1522. err = snd_soc_update_bits(codec, reg, val_mask, val);
  1523. if (err < 0)
  1524. return err;
  1525. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1526. return err;
  1527. }
  1528. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1529. /**
  1530. * snd_soc_info_volsw_s8 - signed mixer info callback
  1531. * @kcontrol: mixer control
  1532. * @uinfo: control element information
  1533. *
  1534. * Callback to provide information about a signed mixer control.
  1535. *
  1536. * Returns 0 for success.
  1537. */
  1538. int snd_soc_info_volsw_s8(struct snd_kcontrol *kcontrol,
  1539. struct snd_ctl_elem_info *uinfo)
  1540. {
  1541. struct soc_mixer_control *mc =
  1542. (struct soc_mixer_control *)kcontrol->private_value;
  1543. int max = mc->max;
  1544. int min = mc->min;
  1545. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1546. uinfo->count = 2;
  1547. uinfo->value.integer.min = 0;
  1548. uinfo->value.integer.max = max-min;
  1549. return 0;
  1550. }
  1551. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_s8);
  1552. /**
  1553. * snd_soc_get_volsw_s8 - signed mixer get callback
  1554. * @kcontrol: mixer control
  1555. * @uinfo: control element information
  1556. *
  1557. * Callback to get the value of a signed mixer control.
  1558. *
  1559. * Returns 0 for success.
  1560. */
  1561. int snd_soc_get_volsw_s8(struct snd_kcontrol *kcontrol,
  1562. struct snd_ctl_elem_value *ucontrol)
  1563. {
  1564. struct soc_mixer_control *mc =
  1565. (struct soc_mixer_control *)kcontrol->private_value;
  1566. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1567. unsigned int reg = mc->reg;
  1568. int min = mc->min;
  1569. int val = snd_soc_read(codec, reg);
  1570. ucontrol->value.integer.value[0] =
  1571. ((signed char)(val & 0xff))-min;
  1572. ucontrol->value.integer.value[1] =
  1573. ((signed char)((val >> 8) & 0xff))-min;
  1574. return 0;
  1575. }
  1576. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_s8);
  1577. /**
  1578. * snd_soc_put_volsw_sgn - signed mixer put callback
  1579. * @kcontrol: mixer control
  1580. * @uinfo: control element information
  1581. *
  1582. * Callback to set the value of a signed mixer control.
  1583. *
  1584. * Returns 0 for success.
  1585. */
  1586. int snd_soc_put_volsw_s8(struct snd_kcontrol *kcontrol,
  1587. struct snd_ctl_elem_value *ucontrol)
  1588. {
  1589. struct soc_mixer_control *mc =
  1590. (struct soc_mixer_control *)kcontrol->private_value;
  1591. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1592. unsigned int reg = mc->reg;
  1593. int min = mc->min;
  1594. unsigned short val;
  1595. val = (ucontrol->value.integer.value[0]+min) & 0xff;
  1596. val |= ((ucontrol->value.integer.value[1]+min) & 0xff) << 8;
  1597. return snd_soc_update_bits(codec, reg, 0xffff, val);
  1598. }
  1599. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_s8);
  1600. /**
  1601. * snd_soc_dai_set_sysclk - configure DAI system or master clock.
  1602. * @dai: DAI
  1603. * @clk_id: DAI specific clock ID
  1604. * @freq: new clock frequency in Hz
  1605. * @dir: new clock direction - input/output.
  1606. *
  1607. * Configures the DAI master (MCLK) or system (SYSCLK) clocking.
  1608. */
  1609. int snd_soc_dai_set_sysclk(struct snd_soc_dai *dai, int clk_id,
  1610. unsigned int freq, int dir)
  1611. {
  1612. if (dai->dai_ops.set_sysclk)
  1613. return dai->dai_ops.set_sysclk(dai, clk_id, freq, dir);
  1614. else
  1615. return -EINVAL;
  1616. }
  1617. EXPORT_SYMBOL_GPL(snd_soc_dai_set_sysclk);
  1618. /**
  1619. * snd_soc_dai_set_clkdiv - configure DAI clock dividers.
  1620. * @dai: DAI
  1621. * @clk_id: DAI specific clock divider ID
  1622. * @div: new clock divisor.
  1623. *
  1624. * Configures the clock dividers. This is used to derive the best DAI bit and
  1625. * frame clocks from the system or master clock. It's best to set the DAI bit
  1626. * and frame clocks as low as possible to save system power.
  1627. */
  1628. int snd_soc_dai_set_clkdiv(struct snd_soc_dai *dai,
  1629. int div_id, int div)
  1630. {
  1631. if (dai->dai_ops.set_clkdiv)
  1632. return dai->dai_ops.set_clkdiv(dai, div_id, div);
  1633. else
  1634. return -EINVAL;
  1635. }
  1636. EXPORT_SYMBOL_GPL(snd_soc_dai_set_clkdiv);
  1637. /**
  1638. * snd_soc_dai_set_pll - configure DAI PLL.
  1639. * @dai: DAI
  1640. * @pll_id: DAI specific PLL ID
  1641. * @freq_in: PLL input clock frequency in Hz
  1642. * @freq_out: requested PLL output clock frequency in Hz
  1643. *
  1644. * Configures and enables PLL to generate output clock based on input clock.
  1645. */
  1646. int snd_soc_dai_set_pll(struct snd_soc_dai *dai,
  1647. int pll_id, unsigned int freq_in, unsigned int freq_out)
  1648. {
  1649. if (dai->dai_ops.set_pll)
  1650. return dai->dai_ops.set_pll(dai, pll_id, freq_in, freq_out);
  1651. else
  1652. return -EINVAL;
  1653. }
  1654. EXPORT_SYMBOL_GPL(snd_soc_dai_set_pll);
  1655. /**
  1656. * snd_soc_dai_set_fmt - configure DAI hardware audio format.
  1657. * @dai: DAI
  1658. * @clk_id: DAI specific clock ID
  1659. * @fmt: SND_SOC_DAIFMT_ format value.
  1660. *
  1661. * Configures the DAI hardware format and clocking.
  1662. */
  1663. int snd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
  1664. {
  1665. if (dai->dai_ops.set_fmt)
  1666. return dai->dai_ops.set_fmt(dai, fmt);
  1667. else
  1668. return -EINVAL;
  1669. }
  1670. EXPORT_SYMBOL_GPL(snd_soc_dai_set_fmt);
  1671. /**
  1672. * snd_soc_dai_set_tdm_slot - configure DAI TDM.
  1673. * @dai: DAI
  1674. * @mask: DAI specific mask representing used slots.
  1675. * @slots: Number of slots in use.
  1676. *
  1677. * Configures a DAI for TDM operation. Both mask and slots are codec and DAI
  1678. * specific.
  1679. */
  1680. int snd_soc_dai_set_tdm_slot(struct snd_soc_dai *dai,
  1681. unsigned int mask, int slots)
  1682. {
  1683. if (dai->dai_ops.set_sysclk)
  1684. return dai->dai_ops.set_tdm_slot(dai, mask, slots);
  1685. else
  1686. return -EINVAL;
  1687. }
  1688. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tdm_slot);
  1689. /**
  1690. * snd_soc_dai_set_tristate - configure DAI system or master clock.
  1691. * @dai: DAI
  1692. * @tristate: tristate enable
  1693. *
  1694. * Tristates the DAI so that others can use it.
  1695. */
  1696. int snd_soc_dai_set_tristate(struct snd_soc_dai *dai, int tristate)
  1697. {
  1698. if (dai->dai_ops.set_sysclk)
  1699. return dai->dai_ops.set_tristate(dai, tristate);
  1700. else
  1701. return -EINVAL;
  1702. }
  1703. EXPORT_SYMBOL_GPL(snd_soc_dai_set_tristate);
  1704. /**
  1705. * snd_soc_dai_digital_mute - configure DAI system or master clock.
  1706. * @dai: DAI
  1707. * @mute: mute enable
  1708. *
  1709. * Mutes the DAI DAC.
  1710. */
  1711. int snd_soc_dai_digital_mute(struct snd_soc_dai *dai, int mute)
  1712. {
  1713. if (dai->dai_ops.digital_mute)
  1714. return dai->dai_ops.digital_mute(dai, mute);
  1715. else
  1716. return -EINVAL;
  1717. }
  1718. EXPORT_SYMBOL_GPL(snd_soc_dai_digital_mute);
  1719. static int __devinit snd_soc_init(void)
  1720. {
  1721. return platform_driver_register(&soc_driver);
  1722. }
  1723. static void snd_soc_exit(void)
  1724. {
  1725. platform_driver_unregister(&soc_driver);
  1726. }
  1727. module_init(snd_soc_init);
  1728. module_exit(snd_soc_exit);
  1729. /* Module information */
  1730. MODULE_AUTHOR("Liam Girdwood, lrg@slimlogic.co.uk");
  1731. MODULE_DESCRIPTION("ALSA SoC Core");
  1732. MODULE_LICENSE("GPL");
  1733. MODULE_ALIAS("platform:soc-audio");