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