soc-core.c 43 KB

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