soc-core.c 52 KB

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