soc-core.c 52 KB

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