soc-core.c 52 KB

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