soc-core.c 43 KB

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