soc-core.c 43 KB

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