soc-core.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587
  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
  8. * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
  9. * with code, comments and ideas from :-
  10. * Richard Purdie <richard@openedhand.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. *
  17. * Revision history
  18. * 12th Aug 2005 Initial version.
  19. * 25th Oct 2005 Working Codec, Interface and Platform registration.
  20. *
  21. * TODO:
  22. * o Add hw rules to enforce rates, etc.
  23. * o More testing with other codecs/machines.
  24. * o Add more codecs and platforms to ensure good API coverage.
  25. * o Support TDM on PCM and I2S
  26. */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/delay.h>
  31. #include <linux/pm.h>
  32. #include <linux/bitops.h>
  33. #include <linux/platform_device.h>
  34. #include <sound/core.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/soc.h>
  38. #include <sound/soc-dapm.h>
  39. #include <sound/initval.h>
  40. /* debug */
  41. #define SOC_DEBUG 0
  42. #if SOC_DEBUG
  43. #define dbg(format, arg...) printk(format, ## arg)
  44. #else
  45. #define dbg(format, arg...)
  46. #endif
  47. static DEFINE_MUTEX(pcm_mutex);
  48. static DEFINE_MUTEX(io_mutex);
  49. static DECLARE_WAIT_QUEUE_HEAD(soc_pm_waitq);
  50. /*
  51. * This is a timeout to do a DAPM powerdown after a stream is closed().
  52. * It can be used to eliminate pops between different playback streams, e.g.
  53. * between two audio tracks.
  54. */
  55. static int pmdown_time = 5000;
  56. module_param(pmdown_time, int, 0);
  57. MODULE_PARM_DESC(pmdown_time, "DAPM stream powerdown time (msecs)");
  58. /*
  59. * This function forces any delayed work to be queued and run.
  60. */
  61. static int run_delayed_work(struct delayed_work *dwork)
  62. {
  63. int ret;
  64. /* cancel any work waiting to be queued. */
  65. ret = cancel_delayed_work(dwork);
  66. /* if there was any work waiting then we run it now and
  67. * wait for it's completion */
  68. if (ret) {
  69. schedule_delayed_work(dwork, 0);
  70. flush_scheduled_work();
  71. }
  72. return ret;
  73. }
  74. #ifdef CONFIG_SND_SOC_AC97_BUS
  75. /* unregister ac97 codec */
  76. static int soc_ac97_dev_unregister(struct snd_soc_codec *codec)
  77. {
  78. if (codec->ac97->dev.bus)
  79. device_unregister(&codec->ac97->dev);
  80. return 0;
  81. }
  82. /* stop no dev release warning */
  83. static void soc_ac97_device_release(struct device *dev){}
  84. /* register ac97 codec to bus */
  85. static int soc_ac97_dev_register(struct snd_soc_codec *codec)
  86. {
  87. int err;
  88. codec->ac97->dev.bus = &ac97_bus_type;
  89. codec->ac97->dev.parent = NULL;
  90. codec->ac97->dev.release = soc_ac97_device_release;
  91. snprintf(codec->ac97->dev.bus_id, BUS_ID_SIZE, "%d-%d:%s",
  92. codec->card->number, 0, codec->name);
  93. err = device_register(&codec->ac97->dev);
  94. if (err < 0) {
  95. snd_printk(KERN_ERR "Can't register ac97 bus\n");
  96. codec->ac97->dev.bus = NULL;
  97. return err;
  98. }
  99. return 0;
  100. }
  101. #endif
  102. static inline const char* get_dai_name(int type)
  103. {
  104. switch(type) {
  105. case SND_SOC_DAI_AC97_BUS:
  106. case SND_SOC_DAI_AC97:
  107. return "AC97";
  108. case SND_SOC_DAI_I2S:
  109. return "I2S";
  110. case SND_SOC_DAI_PCM:
  111. return "PCM";
  112. }
  113. return NULL;
  114. }
  115. /*
  116. * Called by ALSA when a PCM substream is opened, the runtime->hw record is
  117. * then initialized and any private data can be allocated. This also calls
  118. * startup for the cpu DAI, platform, machine and codec DAI.
  119. */
  120. static int soc_pcm_open(struct snd_pcm_substream *substream)
  121. {
  122. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  123. struct snd_soc_device *socdev = rtd->socdev;
  124. struct snd_pcm_runtime *runtime = substream->runtime;
  125. struct snd_soc_dai_link *machine = rtd->dai;
  126. struct snd_soc_platform *platform = socdev->platform;
  127. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  128. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  129. int ret = 0;
  130. mutex_lock(&pcm_mutex);
  131. /* startup the audio subsystem */
  132. if (cpu_dai->ops.startup) {
  133. ret = cpu_dai->ops.startup(substream);
  134. if (ret < 0) {
  135. printk(KERN_ERR "asoc: can't open interface %s\n",
  136. cpu_dai->name);
  137. goto out;
  138. }
  139. }
  140. if (platform->pcm_ops->open) {
  141. ret = platform->pcm_ops->open(substream);
  142. if (ret < 0) {
  143. printk(KERN_ERR "asoc: can't open platform %s\n", platform->name);
  144. goto platform_err;
  145. }
  146. }
  147. if (codec_dai->ops.startup) {
  148. ret = codec_dai->ops.startup(substream);
  149. if (ret < 0) {
  150. printk(KERN_ERR "asoc: can't open codec %s\n",
  151. codec_dai->name);
  152. goto codec_dai_err;
  153. }
  154. }
  155. if (machine->ops && machine->ops->startup) {
  156. ret = machine->ops->startup(substream);
  157. if (ret < 0) {
  158. printk(KERN_ERR "asoc: %s startup failed\n", machine->name);
  159. goto machine_err;
  160. }
  161. }
  162. /* Check that the codec and cpu DAI's are compatible */
  163. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  164. runtime->hw.rate_min =
  165. max(codec_dai->playback.rate_min, cpu_dai->playback.rate_min);
  166. runtime->hw.rate_max =
  167. min(codec_dai->playback.rate_max, cpu_dai->playback.rate_max);
  168. runtime->hw.channels_min =
  169. max(codec_dai->playback.channels_min,
  170. cpu_dai->playback.channels_min);
  171. runtime->hw.channels_max =
  172. min(codec_dai->playback.channels_max,
  173. cpu_dai->playback.channels_max);
  174. runtime->hw.formats =
  175. codec_dai->playback.formats & cpu_dai->playback.formats;
  176. runtime->hw.rates =
  177. codec_dai->playback.rates & cpu_dai->playback.rates;
  178. } else {
  179. runtime->hw.rate_min =
  180. max(codec_dai->capture.rate_min, cpu_dai->capture.rate_min);
  181. runtime->hw.rate_max =
  182. min(codec_dai->capture.rate_max, 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_codec_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. codec_dai->pop_wait = 0;
  260. snd_soc_dapm_stream_event(codec,
  261. codec_dai->playback.stream_name,
  262. SND_SOC_DAPM_STREAM_STOP);
  263. /* power down the codec power domain if no longer active */
  264. if (codec->active == 0) {
  265. dbg("pop wq D3 %s %s\n", codec->name,
  266. codec_dai->playback.stream_name);
  267. snd_soc_dapm_device_event(socdev,
  268. SNDRV_CTL_POWER_D3hot);
  269. }
  270. }
  271. }
  272. mutex_unlock(&pcm_mutex);
  273. }
  274. /*
  275. * Called by ALSA when a PCM substream is closed. Private data can be
  276. * freed here. The cpu DAI, codec DAI, machine and platform are also
  277. * shutdown.
  278. */
  279. static int soc_codec_close(struct snd_pcm_substream *substream)
  280. {
  281. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  282. struct snd_soc_device *socdev = rtd->socdev;
  283. struct snd_soc_dai_link *machine = rtd->dai;
  284. struct snd_soc_platform *platform = socdev->platform;
  285. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  286. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  287. struct snd_soc_codec *codec = socdev->codec;
  288. mutex_lock(&pcm_mutex);
  289. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  290. cpu_dai->playback.active = codec_dai->playback.active = 0;
  291. else
  292. cpu_dai->capture.active = codec_dai->capture.active = 0;
  293. if (codec_dai->playback.active == 0 &&
  294. codec_dai->capture.active == 0) {
  295. cpu_dai->active = codec_dai->active = 0;
  296. }
  297. codec->active--;
  298. if (cpu_dai->ops.shutdown)
  299. cpu_dai->ops.shutdown(substream);
  300. if (codec_dai->ops.shutdown)
  301. codec_dai->ops.shutdown(substream);
  302. if (machine->ops && machine->ops->shutdown)
  303. machine->ops->shutdown(substream);
  304. if (platform->pcm_ops->close)
  305. platform->pcm_ops->close(substream);
  306. cpu_dai->runtime = NULL;
  307. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  308. /* start delayed pop wq here for playback streams */
  309. codec_dai->pop_wait = 1;
  310. schedule_delayed_work(&socdev->delayed_work,
  311. msecs_to_jiffies(pmdown_time));
  312. } else {
  313. /* capture streams can be powered down now */
  314. snd_soc_dapm_stream_event(codec,
  315. codec_dai->capture.stream_name,
  316. SND_SOC_DAPM_STREAM_STOP);
  317. if (codec->active == 0 && codec_dai->pop_wait == 0)
  318. snd_soc_dapm_device_event(socdev,
  319. SNDRV_CTL_POWER_D3hot);
  320. }
  321. mutex_unlock(&pcm_mutex);
  322. return 0;
  323. }
  324. /*
  325. * Called by ALSA when the PCM substream is prepared, can set format, sample
  326. * rate, etc. This function is non atomic and can be called multiple times,
  327. * it can refer to the runtime info.
  328. */
  329. static int soc_pcm_prepare(struct snd_pcm_substream *substream)
  330. {
  331. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  332. struct snd_soc_device *socdev = rtd->socdev;
  333. struct snd_soc_dai_link *machine = rtd->dai;
  334. struct snd_soc_platform *platform = socdev->platform;
  335. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  336. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  337. struct snd_soc_codec *codec = socdev->codec;
  338. int ret = 0;
  339. mutex_lock(&pcm_mutex);
  340. if (machine->ops && machine->ops->prepare) {
  341. ret = machine->ops->prepare(substream);
  342. if (ret < 0) {
  343. printk(KERN_ERR "asoc: machine prepare error\n");
  344. goto out;
  345. }
  346. }
  347. if (platform->pcm_ops->prepare) {
  348. ret = platform->pcm_ops->prepare(substream);
  349. if (ret < 0) {
  350. printk(KERN_ERR "asoc: platform prepare error\n");
  351. goto out;
  352. }
  353. }
  354. if (codec_dai->ops.prepare) {
  355. ret = codec_dai->ops.prepare(substream);
  356. if (ret < 0) {
  357. printk(KERN_ERR "asoc: codec DAI prepare error\n");
  358. goto out;
  359. }
  360. }
  361. if (cpu_dai->ops.prepare) {
  362. ret = cpu_dai->ops.prepare(substream);
  363. if (ret < 0) {
  364. printk(KERN_ERR "asoc: cpu DAI prepare error\n");
  365. goto out;
  366. }
  367. }
  368. /* we only want to start a DAPM playback stream if we are not waiting
  369. * on an existing one stopping */
  370. if (codec_dai->pop_wait) {
  371. /* we are waiting for the delayed work to start */
  372. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  373. snd_soc_dapm_stream_event(socdev->codec,
  374. codec_dai->capture.stream_name,
  375. SND_SOC_DAPM_STREAM_START);
  376. else {
  377. codec_dai->pop_wait = 0;
  378. cancel_delayed_work(&socdev->delayed_work);
  379. if (codec_dai->dai_ops.digital_mute)
  380. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  381. }
  382. } else {
  383. /* no delayed work - do we need to power up codec */
  384. if (codec->dapm_state != SNDRV_CTL_POWER_D0) {
  385. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D1);
  386. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  387. snd_soc_dapm_stream_event(codec,
  388. codec_dai->playback.stream_name,
  389. SND_SOC_DAPM_STREAM_START);
  390. else
  391. snd_soc_dapm_stream_event(codec,
  392. codec_dai->capture.stream_name,
  393. SND_SOC_DAPM_STREAM_START);
  394. snd_soc_dapm_device_event(socdev, SNDRV_CTL_POWER_D0);
  395. if (codec_dai->dai_ops.digital_mute)
  396. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  397. } else {
  398. /* codec already powered - power on widgets */
  399. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  400. snd_soc_dapm_stream_event(codec,
  401. codec_dai->playback.stream_name,
  402. SND_SOC_DAPM_STREAM_START);
  403. else
  404. snd_soc_dapm_stream_event(codec,
  405. codec_dai->capture.stream_name,
  406. SND_SOC_DAPM_STREAM_START);
  407. if (codec_dai->dai_ops.digital_mute)
  408. codec_dai->dai_ops.digital_mute(codec_dai, 0);
  409. }
  410. }
  411. out:
  412. mutex_unlock(&pcm_mutex);
  413. return ret;
  414. }
  415. /*
  416. * Called by ALSA when the hardware params are set by application. This
  417. * function can also be called multiple times and can allocate buffers
  418. * (using snd_pcm_lib_* ). It's non-atomic.
  419. */
  420. static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
  421. struct snd_pcm_hw_params *params)
  422. {
  423. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  424. struct snd_soc_device *socdev = rtd->socdev;
  425. struct snd_soc_dai_link *machine = rtd->dai;
  426. struct snd_soc_platform *platform = socdev->platform;
  427. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  428. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  429. int ret = 0;
  430. mutex_lock(&pcm_mutex);
  431. if (machine->ops && machine->ops->hw_params) {
  432. ret = machine->ops->hw_params(substream, params);
  433. if (ret < 0) {
  434. printk(KERN_ERR "asoc: machine hw_params failed\n");
  435. goto out;
  436. }
  437. }
  438. if (codec_dai->ops.hw_params) {
  439. ret = codec_dai->ops.hw_params(substream, params);
  440. if (ret < 0) {
  441. printk(KERN_ERR "asoc: can't set codec %s hw params\n",
  442. codec_dai->name);
  443. goto codec_err;
  444. }
  445. }
  446. if (cpu_dai->ops.hw_params) {
  447. ret = cpu_dai->ops.hw_params(substream, params);
  448. if (ret < 0) {
  449. printk(KERN_ERR "asoc: can't set interface %s hw params\n",
  450. cpu_dai->name);
  451. goto interface_err;
  452. }
  453. }
  454. if (platform->pcm_ops->hw_params) {
  455. ret = platform->pcm_ops->hw_params(substream, params);
  456. if (ret < 0) {
  457. printk(KERN_ERR "asoc: can't set platform %s hw params\n",
  458. platform->name);
  459. goto platform_err;
  460. }
  461. }
  462. out:
  463. mutex_unlock(&pcm_mutex);
  464. return ret;
  465. platform_err:
  466. if (cpu_dai->ops.hw_free)
  467. cpu_dai->ops.hw_free(substream);
  468. interface_err:
  469. if (codec_dai->ops.hw_free)
  470. codec_dai->ops.hw_free(substream);
  471. codec_err:
  472. if(machine->ops && machine->ops->hw_free)
  473. machine->ops->hw_free(substream);
  474. mutex_unlock(&pcm_mutex);
  475. return ret;
  476. }
  477. /*
  478. * Free's resources allocated by hw_params, can be called multiple times
  479. */
  480. static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
  481. {
  482. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  483. struct snd_soc_device *socdev = rtd->socdev;
  484. struct snd_soc_dai_link *machine = rtd->dai;
  485. struct snd_soc_platform *platform = socdev->platform;
  486. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  487. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  488. struct snd_soc_codec *codec = socdev->codec;
  489. mutex_lock(&pcm_mutex);
  490. /* apply codec digital mute */
  491. if (!codec->active && codec_dai->dai_ops.digital_mute)
  492. codec_dai->dai_ops.digital_mute(codec_dai, 1);
  493. /* free any machine hw params */
  494. if (machine->ops && machine->ops->hw_free)
  495. machine->ops->hw_free(substream);
  496. /* free any DMA resources */
  497. if (platform->pcm_ops->hw_free)
  498. platform->pcm_ops->hw_free(substream);
  499. /* now free hw params for the DAI's */
  500. if (codec_dai->ops.hw_free)
  501. codec_dai->ops.hw_free(substream);
  502. if (cpu_dai->ops.hw_free)
  503. cpu_dai->ops.hw_free(substream);
  504. mutex_unlock(&pcm_mutex);
  505. return 0;
  506. }
  507. static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  508. {
  509. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  510. struct snd_soc_device *socdev = rtd->socdev;
  511. struct snd_soc_dai_link *machine = rtd->dai;
  512. struct snd_soc_platform *platform = socdev->platform;
  513. struct snd_soc_cpu_dai *cpu_dai = machine->cpu_dai;
  514. struct snd_soc_codec_dai *codec_dai = machine->codec_dai;
  515. int ret;
  516. if (codec_dai->ops.trigger) {
  517. ret = codec_dai->ops.trigger(substream, cmd);
  518. if (ret < 0)
  519. return ret;
  520. }
  521. if (platform->pcm_ops->trigger) {
  522. ret = platform->pcm_ops->trigger(substream, cmd);
  523. if (ret < 0)
  524. return ret;
  525. }
  526. if (cpu_dai->ops.trigger) {
  527. ret = cpu_dai->ops.trigger(substream, cmd);
  528. if (ret < 0)
  529. return ret;
  530. }
  531. return 0;
  532. }
  533. /* ASoC PCM operations */
  534. static struct snd_pcm_ops soc_pcm_ops = {
  535. .open = soc_pcm_open,
  536. .close = soc_codec_close,
  537. .hw_params = soc_pcm_hw_params,
  538. .hw_free = soc_pcm_hw_free,
  539. .prepare = soc_pcm_prepare,
  540. .trigger = soc_pcm_trigger,
  541. };
  542. #ifdef CONFIG_PM
  543. /* powers down audio subsystem for suspend */
  544. static int soc_suspend(struct platform_device *pdev, pm_message_t state)
  545. {
  546. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  547. struct snd_soc_machine *machine = socdev->machine;
  548. struct snd_soc_platform *platform = socdev->platform;
  549. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  550. struct snd_soc_codec *codec = socdev->codec;
  551. int i;
  552. /* mute any active DAC's */
  553. for(i = 0; i < machine->num_links; i++) {
  554. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  555. if (dai->dai_ops.digital_mute && dai->playback.active)
  556. dai->dai_ops.digital_mute(dai, 1);
  557. }
  558. if (machine->suspend_pre)
  559. machine->suspend_pre(pdev, state);
  560. for(i = 0; i < machine->num_links; i++) {
  561. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  562. if (cpu_dai->suspend && cpu_dai->type != SND_SOC_DAI_AC97)
  563. cpu_dai->suspend(pdev, cpu_dai);
  564. if (platform->suspend)
  565. platform->suspend(pdev, cpu_dai);
  566. }
  567. /* close any waiting streams and save state */
  568. run_delayed_work(&socdev->delayed_work);
  569. codec->suspend_dapm_state = codec->dapm_state;
  570. for(i = 0; i < codec->num_dai; i++) {
  571. char *stream = codec->dai[i].playback.stream_name;
  572. if (stream != NULL)
  573. snd_soc_dapm_stream_event(codec, stream,
  574. SND_SOC_DAPM_STREAM_SUSPEND);
  575. stream = codec->dai[i].capture.stream_name;
  576. if (stream != NULL)
  577. snd_soc_dapm_stream_event(codec, stream,
  578. SND_SOC_DAPM_STREAM_SUSPEND);
  579. }
  580. if (codec_dev->suspend)
  581. codec_dev->suspend(pdev, state);
  582. for(i = 0; i < machine->num_links; i++) {
  583. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  584. if (cpu_dai->suspend && cpu_dai->type == SND_SOC_DAI_AC97)
  585. cpu_dai->suspend(pdev, cpu_dai);
  586. }
  587. if (machine->suspend_post)
  588. machine->suspend_post(pdev, state);
  589. return 0;
  590. }
  591. /* powers up audio subsystem after a suspend */
  592. static int soc_resume(struct platform_device *pdev)
  593. {
  594. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  595. struct snd_soc_machine *machine = socdev->machine;
  596. struct snd_soc_platform *platform = socdev->platform;
  597. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  598. struct snd_soc_codec *codec = socdev->codec;
  599. int i;
  600. if (machine->resume_pre)
  601. machine->resume_pre(pdev);
  602. for(i = 0; i < machine->num_links; i++) {
  603. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  604. if (cpu_dai->resume && cpu_dai->type == SND_SOC_DAI_AC97)
  605. cpu_dai->resume(pdev, cpu_dai);
  606. }
  607. if (codec_dev->resume)
  608. codec_dev->resume(pdev);
  609. for(i = 0; i < codec->num_dai; i++) {
  610. char* stream = codec->dai[i].playback.stream_name;
  611. if (stream != NULL)
  612. snd_soc_dapm_stream_event(codec, stream,
  613. SND_SOC_DAPM_STREAM_RESUME);
  614. stream = codec->dai[i].capture.stream_name;
  615. if (stream != NULL)
  616. snd_soc_dapm_stream_event(codec, stream,
  617. SND_SOC_DAPM_STREAM_RESUME);
  618. }
  619. /* unmute any active DAC's */
  620. for(i = 0; i < machine->num_links; i++) {
  621. struct snd_soc_codec_dai *dai = machine->dai_link[i].codec_dai;
  622. if (dai->dai_ops.digital_mute && dai->playback.active)
  623. dai->dai_ops.digital_mute(dai, 0);
  624. }
  625. for(i = 0; i < machine->num_links; i++) {
  626. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  627. if (cpu_dai->resume && cpu_dai->type != SND_SOC_DAI_AC97)
  628. cpu_dai->resume(pdev, cpu_dai);
  629. if (platform->resume)
  630. platform->resume(pdev, cpu_dai);
  631. }
  632. if (machine->resume_post)
  633. machine->resume_post(pdev);
  634. return 0;
  635. }
  636. #else
  637. #define soc_suspend NULL
  638. #define soc_resume NULL
  639. #endif
  640. /* probes a new socdev */
  641. static int soc_probe(struct platform_device *pdev)
  642. {
  643. int ret = 0, i;
  644. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  645. struct snd_soc_machine *machine = socdev->machine;
  646. struct snd_soc_platform *platform = socdev->platform;
  647. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  648. if (machine->probe) {
  649. ret = machine->probe(pdev);
  650. if(ret < 0)
  651. return ret;
  652. }
  653. for (i = 0; i < machine->num_links; i++) {
  654. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  655. if (cpu_dai->probe) {
  656. ret = cpu_dai->probe(pdev);
  657. if(ret < 0)
  658. goto cpu_dai_err;
  659. }
  660. }
  661. if (codec_dev->probe) {
  662. ret = codec_dev->probe(pdev);
  663. if(ret < 0)
  664. goto cpu_dai_err;
  665. }
  666. if (platform->probe) {
  667. ret = platform->probe(pdev);
  668. if(ret < 0)
  669. goto platform_err;
  670. }
  671. /* DAPM stream work */
  672. INIT_DELAYED_WORK(&socdev->delayed_work, close_delayed_work);
  673. return 0;
  674. platform_err:
  675. if (codec_dev->remove)
  676. codec_dev->remove(pdev);
  677. cpu_dai_err:
  678. for (i--; i >= 0; i--) {
  679. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  680. if (cpu_dai->remove)
  681. cpu_dai->remove(pdev);
  682. }
  683. if (machine->remove)
  684. machine->remove(pdev);
  685. return ret;
  686. }
  687. /* removes a socdev */
  688. static int soc_remove(struct platform_device *pdev)
  689. {
  690. int i;
  691. struct snd_soc_device *socdev = platform_get_drvdata(pdev);
  692. struct snd_soc_machine *machine = socdev->machine;
  693. struct snd_soc_platform *platform = socdev->platform;
  694. struct snd_soc_codec_device *codec_dev = socdev->codec_dev;
  695. run_delayed_work(&socdev->delayed_work);
  696. if (platform->remove)
  697. platform->remove(pdev);
  698. if (codec_dev->remove)
  699. codec_dev->remove(pdev);
  700. for (i = 0; i < machine->num_links; i++) {
  701. struct snd_soc_cpu_dai *cpu_dai = machine->dai_link[i].cpu_dai;
  702. if (cpu_dai->remove)
  703. cpu_dai->remove(pdev);
  704. }
  705. if (machine->remove)
  706. machine->remove(pdev);
  707. return 0;
  708. }
  709. /* ASoC platform driver */
  710. static struct platform_driver soc_driver = {
  711. .driver = {
  712. .name = "soc-audio",
  713. },
  714. .probe = soc_probe,
  715. .remove = soc_remove,
  716. .suspend = soc_suspend,
  717. .resume = soc_resume,
  718. };
  719. /* create a new pcm */
  720. static int soc_new_pcm(struct snd_soc_device *socdev,
  721. struct snd_soc_dai_link *dai_link, int num)
  722. {
  723. struct snd_soc_codec *codec = socdev->codec;
  724. struct snd_soc_codec_dai *codec_dai = dai_link->codec_dai;
  725. struct snd_soc_cpu_dai *cpu_dai = dai_link->cpu_dai;
  726. struct snd_soc_pcm_runtime *rtd;
  727. struct snd_pcm *pcm;
  728. char new_name[64];
  729. int ret = 0, playback = 0, capture = 0;
  730. rtd = kzalloc(sizeof(struct snd_soc_pcm_runtime), GFP_KERNEL);
  731. if (rtd == NULL)
  732. return -ENOMEM;
  733. rtd->dai = dai_link;
  734. rtd->socdev = socdev;
  735. codec_dai->codec = socdev->codec;
  736. /* check client and interface hw capabilities */
  737. sprintf(new_name, "%s %s-%s-%d",dai_link->stream_name, codec_dai->name,
  738. get_dai_name(cpu_dai->type), num);
  739. if (codec_dai->playback.channels_min)
  740. playback = 1;
  741. if (codec_dai->capture.channels_min)
  742. capture = 1;
  743. ret = snd_pcm_new(codec->card, new_name, codec->pcm_devs++, playback,
  744. capture, &pcm);
  745. if (ret < 0) {
  746. printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
  747. kfree(rtd);
  748. return ret;
  749. }
  750. pcm->private_data = rtd;
  751. soc_pcm_ops.mmap = socdev->platform->pcm_ops->mmap;
  752. soc_pcm_ops.pointer = socdev->platform->pcm_ops->pointer;
  753. soc_pcm_ops.ioctl = socdev->platform->pcm_ops->ioctl;
  754. soc_pcm_ops.copy = socdev->platform->pcm_ops->copy;
  755. soc_pcm_ops.silence = socdev->platform->pcm_ops->silence;
  756. soc_pcm_ops.ack = socdev->platform->pcm_ops->ack;
  757. soc_pcm_ops.page = socdev->platform->pcm_ops->page;
  758. if (playback)
  759. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &soc_pcm_ops);
  760. if (capture)
  761. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &soc_pcm_ops);
  762. ret = socdev->platform->pcm_new(codec->card, codec_dai, pcm);
  763. if (ret < 0) {
  764. printk(KERN_ERR "asoc: platform pcm constructor failed\n");
  765. kfree(rtd);
  766. return ret;
  767. }
  768. pcm->private_free = socdev->platform->pcm_free;
  769. printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
  770. cpu_dai->name);
  771. return ret;
  772. }
  773. /* codec register dump */
  774. static ssize_t codec_reg_show(struct device *dev,
  775. struct device_attribute *attr, char *buf)
  776. {
  777. struct snd_soc_device *devdata = dev_get_drvdata(dev);
  778. struct snd_soc_codec *codec = devdata->codec;
  779. int i, step = 1, count = 0;
  780. if (!codec->reg_cache_size)
  781. return 0;
  782. if (codec->reg_cache_step)
  783. step = codec->reg_cache_step;
  784. count += sprintf(buf, "%s registers\n", codec->name);
  785. for(i = 0; i < codec->reg_cache_size; i += step)
  786. count += sprintf(buf + count, "%2x: %4x\n", i, codec->read(codec, i));
  787. return count;
  788. }
  789. static DEVICE_ATTR(codec_reg, 0444, codec_reg_show, NULL);
  790. /**
  791. * snd_soc_new_ac97_codec - initailise AC97 device
  792. * @codec: audio codec
  793. * @ops: AC97 bus operations
  794. * @num: AC97 codec number
  795. *
  796. * Initialises AC97 codec resources for use by ad-hoc devices only.
  797. */
  798. int snd_soc_new_ac97_codec(struct snd_soc_codec *codec,
  799. struct snd_ac97_bus_ops *ops, int num)
  800. {
  801. mutex_lock(&codec->mutex);
  802. codec->ac97 = kzalloc(sizeof(struct snd_ac97), GFP_KERNEL);
  803. if (codec->ac97 == NULL) {
  804. mutex_unlock(&codec->mutex);
  805. return -ENOMEM;
  806. }
  807. codec->ac97->bus = kzalloc(sizeof(struct snd_ac97_bus), GFP_KERNEL);
  808. if (codec->ac97->bus == NULL) {
  809. kfree(codec->ac97);
  810. codec->ac97 = NULL;
  811. mutex_unlock(&codec->mutex);
  812. return -ENOMEM;
  813. }
  814. codec->ac97->bus->ops = ops;
  815. codec->ac97->num = num;
  816. mutex_unlock(&codec->mutex);
  817. return 0;
  818. }
  819. EXPORT_SYMBOL_GPL(snd_soc_new_ac97_codec);
  820. /**
  821. * snd_soc_free_ac97_codec - free AC97 codec device
  822. * @codec: audio codec
  823. *
  824. * Frees AC97 codec device resources.
  825. */
  826. void snd_soc_free_ac97_codec(struct snd_soc_codec *codec)
  827. {
  828. mutex_lock(&codec->mutex);
  829. kfree(codec->ac97->bus);
  830. kfree(codec->ac97);
  831. codec->ac97 = NULL;
  832. mutex_unlock(&codec->mutex);
  833. }
  834. EXPORT_SYMBOL_GPL(snd_soc_free_ac97_codec);
  835. /**
  836. * snd_soc_update_bits - update codec register bits
  837. * @codec: audio codec
  838. * @reg: codec register
  839. * @mask: register mask
  840. * @value: new value
  841. *
  842. * Writes new register value.
  843. *
  844. * Returns 1 for change else 0.
  845. */
  846. int snd_soc_update_bits(struct snd_soc_codec *codec, unsigned short reg,
  847. unsigned short mask, unsigned short value)
  848. {
  849. int change;
  850. unsigned short old, new;
  851. mutex_lock(&io_mutex);
  852. old = snd_soc_read(codec, reg);
  853. new = (old & ~mask) | value;
  854. change = old != new;
  855. if (change)
  856. snd_soc_write(codec, reg, new);
  857. mutex_unlock(&io_mutex);
  858. return change;
  859. }
  860. EXPORT_SYMBOL_GPL(snd_soc_update_bits);
  861. /**
  862. * snd_soc_test_bits - test register for change
  863. * @codec: audio codec
  864. * @reg: codec register
  865. * @mask: register mask
  866. * @value: new value
  867. *
  868. * Tests a register with a new value and checks if the new value is
  869. * different from the old value.
  870. *
  871. * Returns 1 for change else 0.
  872. */
  873. int snd_soc_test_bits(struct snd_soc_codec *codec, unsigned short reg,
  874. unsigned short mask, unsigned short value)
  875. {
  876. int change;
  877. unsigned short old, new;
  878. mutex_lock(&io_mutex);
  879. old = snd_soc_read(codec, reg);
  880. new = (old & ~mask) | value;
  881. change = old != new;
  882. mutex_unlock(&io_mutex);
  883. return change;
  884. }
  885. EXPORT_SYMBOL_GPL(snd_soc_test_bits);
  886. /**
  887. * snd_soc_new_pcms - create new sound card and pcms
  888. * @socdev: the SoC audio device
  889. *
  890. * Create a new sound card based upon the codec and interface pcms.
  891. *
  892. * Returns 0 for success, else error.
  893. */
  894. int snd_soc_new_pcms(struct snd_soc_device *socdev, int idx, const char *xid)
  895. {
  896. struct snd_soc_codec *codec = socdev->codec;
  897. struct snd_soc_machine *machine = socdev->machine;
  898. int ret = 0, i;
  899. mutex_lock(&codec->mutex);
  900. /* register a sound card */
  901. codec->card = snd_card_new(idx, xid, codec->owner, 0);
  902. if (!codec->card) {
  903. printk(KERN_ERR "asoc: can't create sound card for codec %s\n",
  904. codec->name);
  905. mutex_unlock(&codec->mutex);
  906. return -ENODEV;
  907. }
  908. codec->card->dev = socdev->dev;
  909. codec->card->private_data = codec;
  910. strncpy(codec->card->driver, codec->name, sizeof(codec->card->driver));
  911. /* create the pcms */
  912. for(i = 0; i < machine->num_links; i++) {
  913. ret = soc_new_pcm(socdev, &machine->dai_link[i], i);
  914. if (ret < 0) {
  915. printk(KERN_ERR "asoc: can't create pcm %s\n",
  916. machine->dai_link[i].stream_name);
  917. mutex_unlock(&codec->mutex);
  918. return ret;
  919. }
  920. }
  921. mutex_unlock(&codec->mutex);
  922. return ret;
  923. }
  924. EXPORT_SYMBOL_GPL(snd_soc_new_pcms);
  925. /**
  926. * snd_soc_register_card - register sound card
  927. * @socdev: the SoC audio device
  928. *
  929. * Register a SoC sound card. Also registers an AC97 device if the
  930. * codec is AC97 for ad hoc devices.
  931. *
  932. * Returns 0 for success, else error.
  933. */
  934. int snd_soc_register_card(struct snd_soc_device *socdev)
  935. {
  936. struct snd_soc_codec *codec = socdev->codec;
  937. struct snd_soc_machine *machine = socdev->machine;
  938. int ret = 0, i, ac97 = 0, err = 0;
  939. mutex_lock(&codec->mutex);
  940. for(i = 0; i < machine->num_links; i++) {
  941. if (socdev->machine->dai_link[i].init) {
  942. err = socdev->machine->dai_link[i].init(codec);
  943. if (err < 0) {
  944. printk(KERN_ERR "asoc: failed to init %s\n",
  945. socdev->machine->dai_link[i].stream_name);
  946. continue;
  947. }
  948. }
  949. if (socdev->machine->dai_link[i].codec_dai->type ==
  950. SND_SOC_DAI_AC97_BUS)
  951. ac97 = 1;
  952. }
  953. snprintf(codec->card->shortname, sizeof(codec->card->shortname),
  954. "%s", machine->name);
  955. snprintf(codec->card->longname, sizeof(codec->card->longname),
  956. "%s (%s)", machine->name, codec->name);
  957. ret = snd_card_register(codec->card);
  958. if (ret < 0) {
  959. printk(KERN_ERR "asoc: failed to register soundcard for codec %s\n",
  960. codec->name);
  961. goto out;
  962. }
  963. #ifdef CONFIG_SND_SOC_AC97_BUS
  964. if (ac97) {
  965. ret = soc_ac97_dev_register(codec);
  966. if (ret < 0) {
  967. printk(KERN_ERR "asoc: AC97 device register failed\n");
  968. snd_card_free(codec->card);
  969. goto out;
  970. }
  971. }
  972. #endif
  973. err = snd_soc_dapm_sys_add(socdev->dev);
  974. if (err < 0)
  975. printk(KERN_WARNING "asoc: failed to add dapm sysfs entries\n");
  976. err = device_create_file(socdev->dev, &dev_attr_codec_reg);
  977. if (err < 0)
  978. printk(KERN_WARNING "asoc: failed to add codec sysfs entries\n");
  979. out:
  980. mutex_unlock(&codec->mutex);
  981. return ret;
  982. }
  983. EXPORT_SYMBOL_GPL(snd_soc_register_card);
  984. /**
  985. * snd_soc_free_pcms - free sound card and pcms
  986. * @socdev: the SoC audio device
  987. *
  988. * Frees sound card and pcms associated with the socdev.
  989. * Also unregister the codec if it is an AC97 device.
  990. */
  991. void snd_soc_free_pcms(struct snd_soc_device *socdev)
  992. {
  993. struct snd_soc_codec *codec = socdev->codec;
  994. #ifdef CONFIG_SND_SOC_AC97_BUS
  995. struct snd_soc_codec_dai *codec_dai;
  996. int i;
  997. #endif
  998. mutex_lock(&codec->mutex);
  999. #ifdef CONFIG_SND_SOC_AC97_BUS
  1000. for(i = 0; i < codec->num_dai; i++) {
  1001. codec_dai = &codec->dai[i];
  1002. if (codec_dai->type == SND_SOC_DAI_AC97_BUS && codec->ac97) {
  1003. soc_ac97_dev_unregister(codec);
  1004. goto free_card;
  1005. }
  1006. }
  1007. free_card:
  1008. #endif
  1009. if (codec->card)
  1010. snd_card_free(codec->card);
  1011. device_remove_file(socdev->dev, &dev_attr_codec_reg);
  1012. mutex_unlock(&codec->mutex);
  1013. }
  1014. EXPORT_SYMBOL_GPL(snd_soc_free_pcms);
  1015. /**
  1016. * snd_soc_set_runtime_hwparams - set the runtime hardware parameters
  1017. * @substream: the pcm substream
  1018. * @hw: the hardware parameters
  1019. *
  1020. * Sets the substream runtime hardware parameters.
  1021. */
  1022. int snd_soc_set_runtime_hwparams(struct snd_pcm_substream *substream,
  1023. const struct snd_pcm_hardware *hw)
  1024. {
  1025. struct snd_pcm_runtime *runtime = substream->runtime;
  1026. runtime->hw.info = hw->info;
  1027. runtime->hw.formats = hw->formats;
  1028. runtime->hw.period_bytes_min = hw->period_bytes_min;
  1029. runtime->hw.period_bytes_max = hw->period_bytes_max;
  1030. runtime->hw.periods_min = hw->periods_min;
  1031. runtime->hw.periods_max = hw->periods_max;
  1032. runtime->hw.buffer_bytes_max = hw->buffer_bytes_max;
  1033. runtime->hw.fifo_size = hw->fifo_size;
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL_GPL(snd_soc_set_runtime_hwparams);
  1037. /**
  1038. * snd_soc_cnew - create new control
  1039. * @_template: control template
  1040. * @data: control private data
  1041. * @lnng_name: control long name
  1042. *
  1043. * Create a new mixer control from a template control.
  1044. *
  1045. * Returns 0 for success, else error.
  1046. */
  1047. struct snd_kcontrol *snd_soc_cnew(const struct snd_kcontrol_new *_template,
  1048. void *data, char *long_name)
  1049. {
  1050. struct snd_kcontrol_new template;
  1051. memcpy(&template, _template, sizeof(template));
  1052. if (long_name)
  1053. template.name = long_name;
  1054. template.index = 0;
  1055. return snd_ctl_new1(&template, data);
  1056. }
  1057. EXPORT_SYMBOL_GPL(snd_soc_cnew);
  1058. /**
  1059. * snd_soc_info_enum_double - enumerated double mixer info callback
  1060. * @kcontrol: mixer control
  1061. * @uinfo: control element information
  1062. *
  1063. * Callback to provide information about a double enumerated
  1064. * mixer control.
  1065. *
  1066. * Returns 0 for success.
  1067. */
  1068. int snd_soc_info_enum_double(struct snd_kcontrol *kcontrol,
  1069. struct snd_ctl_elem_info *uinfo)
  1070. {
  1071. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1072. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1073. uinfo->count = e->shift_l == e->shift_r ? 1 : 2;
  1074. uinfo->value.enumerated.items = e->mask;
  1075. if (uinfo->value.enumerated.item > e->mask - 1)
  1076. uinfo->value.enumerated.item = e->mask - 1;
  1077. strcpy(uinfo->value.enumerated.name,
  1078. e->texts[uinfo->value.enumerated.item]);
  1079. return 0;
  1080. }
  1081. EXPORT_SYMBOL_GPL(snd_soc_info_enum_double);
  1082. /**
  1083. * snd_soc_get_enum_double - enumerated double mixer get callback
  1084. * @kcontrol: mixer control
  1085. * @uinfo: control element information
  1086. *
  1087. * Callback to get the value of a double enumerated mixer.
  1088. *
  1089. * Returns 0 for success.
  1090. */
  1091. int snd_soc_get_enum_double(struct snd_kcontrol *kcontrol,
  1092. struct snd_ctl_elem_value *ucontrol)
  1093. {
  1094. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1095. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1096. unsigned short val, bitmask;
  1097. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1098. ;
  1099. val = snd_soc_read(codec, e->reg);
  1100. ucontrol->value.enumerated.item[0] = (val >> e->shift_l) & (bitmask - 1);
  1101. if (e->shift_l != e->shift_r)
  1102. ucontrol->value.enumerated.item[1] =
  1103. (val >> e->shift_r) & (bitmask - 1);
  1104. return 0;
  1105. }
  1106. EXPORT_SYMBOL_GPL(snd_soc_get_enum_double);
  1107. /**
  1108. * snd_soc_put_enum_double - enumerated double mixer put callback
  1109. * @kcontrol: mixer control
  1110. * @uinfo: control element information
  1111. *
  1112. * Callback to set the value of a double enumerated mixer.
  1113. *
  1114. * Returns 0 for success.
  1115. */
  1116. int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
  1117. struct snd_ctl_elem_value *ucontrol)
  1118. {
  1119. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1120. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1121. unsigned short val;
  1122. unsigned short mask, bitmask;
  1123. for (bitmask = 1; bitmask < e->mask; bitmask <<= 1)
  1124. ;
  1125. if (ucontrol->value.enumerated.item[0] > e->mask - 1)
  1126. return -EINVAL;
  1127. val = ucontrol->value.enumerated.item[0] << e->shift_l;
  1128. mask = (bitmask - 1) << e->shift_l;
  1129. if (e->shift_l != e->shift_r) {
  1130. if (ucontrol->value.enumerated.item[1] > e->mask - 1)
  1131. return -EINVAL;
  1132. val |= ucontrol->value.enumerated.item[1] << e->shift_r;
  1133. mask |= (bitmask - 1) << e->shift_r;
  1134. }
  1135. return snd_soc_update_bits(codec, e->reg, mask, val);
  1136. }
  1137. EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
  1138. /**
  1139. * snd_soc_info_enum_ext - external enumerated single mixer info callback
  1140. * @kcontrol: mixer control
  1141. * @uinfo: control element information
  1142. *
  1143. * Callback to provide information about an external enumerated
  1144. * single mixer.
  1145. *
  1146. * Returns 0 for success.
  1147. */
  1148. int snd_soc_info_enum_ext(struct snd_kcontrol *kcontrol,
  1149. struct snd_ctl_elem_info *uinfo)
  1150. {
  1151. struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
  1152. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1153. uinfo->count = 1;
  1154. uinfo->value.enumerated.items = e->mask;
  1155. if (uinfo->value.enumerated.item > e->mask - 1)
  1156. uinfo->value.enumerated.item = e->mask - 1;
  1157. strcpy(uinfo->value.enumerated.name,
  1158. e->texts[uinfo->value.enumerated.item]);
  1159. return 0;
  1160. }
  1161. EXPORT_SYMBOL_GPL(snd_soc_info_enum_ext);
  1162. /**
  1163. * snd_soc_info_volsw_ext - external single mixer info callback
  1164. * @kcontrol: mixer control
  1165. * @uinfo: control element information
  1166. *
  1167. * Callback to provide information about a single external mixer control.
  1168. *
  1169. * Returns 0 for success.
  1170. */
  1171. int snd_soc_info_volsw_ext(struct snd_kcontrol *kcontrol,
  1172. struct snd_ctl_elem_info *uinfo)
  1173. {
  1174. int max = kcontrol->private_value;
  1175. if (max == 1)
  1176. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1177. else
  1178. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1179. uinfo->count = 1;
  1180. uinfo->value.integer.min = 0;
  1181. uinfo->value.integer.max = max;
  1182. return 0;
  1183. }
  1184. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_ext);
  1185. /**
  1186. * snd_soc_info_volsw - single mixer info callback
  1187. * @kcontrol: mixer control
  1188. * @uinfo: control element information
  1189. *
  1190. * Callback to provide information about a single mixer control.
  1191. *
  1192. * Returns 0 for success.
  1193. */
  1194. int snd_soc_info_volsw(struct snd_kcontrol *kcontrol,
  1195. struct snd_ctl_elem_info *uinfo)
  1196. {
  1197. int max = (kcontrol->private_value >> 16) & 0xff;
  1198. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1199. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1200. if (max == 1)
  1201. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1202. else
  1203. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1204. uinfo->count = shift == rshift ? 1 : 2;
  1205. uinfo->value.integer.min = 0;
  1206. uinfo->value.integer.max = max;
  1207. return 0;
  1208. }
  1209. EXPORT_SYMBOL_GPL(snd_soc_info_volsw);
  1210. /**
  1211. * snd_soc_get_volsw - single mixer get callback
  1212. * @kcontrol: mixer control
  1213. * @uinfo: control element information
  1214. *
  1215. * Callback to get the value of a single mixer control.
  1216. *
  1217. * Returns 0 for success.
  1218. */
  1219. int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
  1220. struct snd_ctl_elem_value *ucontrol)
  1221. {
  1222. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1223. int reg = kcontrol->private_value & 0xff;
  1224. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1225. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1226. int max = (kcontrol->private_value >> 16) & 0xff;
  1227. int mask = (1 << fls(max)) - 1;
  1228. int invert = (kcontrol->private_value >> 24) & 0x01;
  1229. ucontrol->value.integer.value[0] =
  1230. (snd_soc_read(codec, reg) >> shift) & mask;
  1231. if (shift != rshift)
  1232. ucontrol->value.integer.value[1] =
  1233. (snd_soc_read(codec, reg) >> rshift) & mask;
  1234. if (invert) {
  1235. ucontrol->value.integer.value[0] =
  1236. max - ucontrol->value.integer.value[0];
  1237. if (shift != rshift)
  1238. ucontrol->value.integer.value[1] =
  1239. max - ucontrol->value.integer.value[1];
  1240. }
  1241. return 0;
  1242. }
  1243. EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
  1244. /**
  1245. * snd_soc_put_volsw - single mixer put callback
  1246. * @kcontrol: mixer control
  1247. * @uinfo: control element information
  1248. *
  1249. * Callback to set the value of a single mixer control.
  1250. *
  1251. * Returns 0 for success.
  1252. */
  1253. int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
  1254. struct snd_ctl_elem_value *ucontrol)
  1255. {
  1256. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1257. int reg = kcontrol->private_value & 0xff;
  1258. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1259. int rshift = (kcontrol->private_value >> 12) & 0x0f;
  1260. int max = (kcontrol->private_value >> 16) & 0xff;
  1261. int mask = (1 << fls(max)) - 1;
  1262. int invert = (kcontrol->private_value >> 24) & 0x01;
  1263. unsigned short val, val2, val_mask;
  1264. val = (ucontrol->value.integer.value[0] & mask);
  1265. if (invert)
  1266. val = max - val;
  1267. val_mask = mask << shift;
  1268. val = val << shift;
  1269. if (shift != rshift) {
  1270. val2 = (ucontrol->value.integer.value[1] & mask);
  1271. if (invert)
  1272. val2 = max - val2;
  1273. val_mask |= mask << rshift;
  1274. val |= val2 << rshift;
  1275. }
  1276. return snd_soc_update_bits(codec, reg, val_mask, val);
  1277. }
  1278. EXPORT_SYMBOL_GPL(snd_soc_put_volsw);
  1279. /**
  1280. * snd_soc_info_volsw_2r - double mixer info callback
  1281. * @kcontrol: mixer control
  1282. * @uinfo: control element information
  1283. *
  1284. * Callback to provide information about a double mixer control that
  1285. * spans 2 codec registers.
  1286. *
  1287. * Returns 0 for success.
  1288. */
  1289. int snd_soc_info_volsw_2r(struct snd_kcontrol *kcontrol,
  1290. struct snd_ctl_elem_info *uinfo)
  1291. {
  1292. int max = (kcontrol->private_value >> 12) & 0xff;
  1293. if (max == 1)
  1294. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  1295. else
  1296. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1297. uinfo->count = 2;
  1298. uinfo->value.integer.min = 0;
  1299. uinfo->value.integer.max = max;
  1300. return 0;
  1301. }
  1302. EXPORT_SYMBOL_GPL(snd_soc_info_volsw_2r);
  1303. /**
  1304. * snd_soc_get_volsw_2r - double mixer get callback
  1305. * @kcontrol: mixer control
  1306. * @uinfo: control element information
  1307. *
  1308. * Callback to get the value of a double mixer control that spans 2 registers.
  1309. *
  1310. * Returns 0 for success.
  1311. */
  1312. int snd_soc_get_volsw_2r(struct snd_kcontrol *kcontrol,
  1313. struct snd_ctl_elem_value *ucontrol)
  1314. {
  1315. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1316. int reg = kcontrol->private_value & 0xff;
  1317. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1318. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1319. int max = (kcontrol->private_value >> 12) & 0xff;
  1320. int mask = (1<<fls(max))-1;
  1321. int invert = (kcontrol->private_value >> 20) & 0x01;
  1322. ucontrol->value.integer.value[0] =
  1323. (snd_soc_read(codec, reg) >> shift) & mask;
  1324. ucontrol->value.integer.value[1] =
  1325. (snd_soc_read(codec, reg2) >> shift) & mask;
  1326. if (invert) {
  1327. ucontrol->value.integer.value[0] =
  1328. max - ucontrol->value.integer.value[0];
  1329. ucontrol->value.integer.value[1] =
  1330. max - ucontrol->value.integer.value[1];
  1331. }
  1332. return 0;
  1333. }
  1334. EXPORT_SYMBOL_GPL(snd_soc_get_volsw_2r);
  1335. /**
  1336. * snd_soc_put_volsw_2r - double mixer set callback
  1337. * @kcontrol: mixer control
  1338. * @uinfo: control element information
  1339. *
  1340. * Callback to set the value of a double mixer control that spans 2 registers.
  1341. *
  1342. * Returns 0 for success.
  1343. */
  1344. int snd_soc_put_volsw_2r(struct snd_kcontrol *kcontrol,
  1345. struct snd_ctl_elem_value *ucontrol)
  1346. {
  1347. struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
  1348. int reg = kcontrol->private_value & 0xff;
  1349. int reg2 = (kcontrol->private_value >> 24) & 0xff;
  1350. int shift = (kcontrol->private_value >> 8) & 0x0f;
  1351. int max = (kcontrol->private_value >> 12) & 0xff;
  1352. int mask = (1 << fls(max)) - 1;
  1353. int invert = (kcontrol->private_value >> 20) & 0x01;
  1354. int err;
  1355. unsigned short val, val2, val_mask;
  1356. val_mask = mask << shift;
  1357. val = (ucontrol->value.integer.value[0] & mask);
  1358. val2 = (ucontrol->value.integer.value[1] & mask);
  1359. if (invert) {
  1360. val = max - val;
  1361. val2 = max - val2;
  1362. }
  1363. val = val << shift;
  1364. val2 = val2 << shift;
  1365. if ((err = snd_soc_update_bits(codec, reg, val_mask, val)) < 0)
  1366. return err;
  1367. err = snd_soc_update_bits(codec, reg2, val_mask, val2);
  1368. return err;
  1369. }
  1370. EXPORT_SYMBOL_GPL(snd_soc_put_volsw_2r);
  1371. static int __devinit snd_soc_init(void)
  1372. {
  1373. printk(KERN_INFO "ASoC version %s\n", SND_SOC_VERSION);
  1374. return platform_driver_register(&soc_driver);
  1375. }
  1376. static void snd_soc_exit(void)
  1377. {
  1378. platform_driver_unregister(&soc_driver);
  1379. }
  1380. module_init(snd_soc_init);
  1381. module_exit(snd_soc_exit);
  1382. /* Module information */
  1383. MODULE_AUTHOR("Liam Girdwood, liam.girdwood@wolfsonmicro.com, www.wolfsonmicro.com");
  1384. MODULE_DESCRIPTION("ALSA SoC Core");
  1385. MODULE_LICENSE("GPL");