soc-utils.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * soc-util.c -- ALSA SoC Audio Layer utility functions
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. * Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/platform_device.h>
  16. #include <linux/export.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
  22. {
  23. return sample_size * channels * tdm_slots;
  24. }
  25. EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
  26. int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
  27. {
  28. int sample_size;
  29. sample_size = snd_pcm_format_width(params_format(params));
  30. if (sample_size < 0)
  31. return sample_size;
  32. return snd_soc_calc_frame_size(sample_size, params_channels(params),
  33. 1);
  34. }
  35. EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
  36. int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
  37. {
  38. return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
  39. }
  40. EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
  41. int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
  42. {
  43. int ret;
  44. ret = snd_soc_params_to_frame_size(params);
  45. if (ret > 0)
  46. return ret * params_rate(params);
  47. else
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
  51. int snd_soc_component_enable_pin(struct snd_soc_component *component,
  52. const char *pin)
  53. {
  54. struct snd_soc_dapm_context *dapm =
  55. snd_soc_component_get_dapm(component);
  56. char *full_name;
  57. int ret;
  58. if (!component->name_prefix)
  59. return snd_soc_dapm_enable_pin(dapm, pin);
  60. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  61. if (!full_name)
  62. return -ENOMEM;
  63. ret = snd_soc_dapm_enable_pin(dapm, full_name);
  64. kfree(full_name);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);
  68. int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
  69. const char *pin)
  70. {
  71. struct snd_soc_dapm_context *dapm =
  72. snd_soc_component_get_dapm(component);
  73. char *full_name;
  74. int ret;
  75. if (!component->name_prefix)
  76. return snd_soc_dapm_enable_pin_unlocked(dapm, pin);
  77. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  78. if (!full_name)
  79. return -ENOMEM;
  80. ret = snd_soc_dapm_enable_pin_unlocked(dapm, full_name);
  81. kfree(full_name);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);
  85. int snd_soc_component_disable_pin(struct snd_soc_component *component,
  86. const char *pin)
  87. {
  88. struct snd_soc_dapm_context *dapm =
  89. snd_soc_component_get_dapm(component);
  90. char *full_name;
  91. int ret;
  92. if (!component->name_prefix)
  93. return snd_soc_dapm_disable_pin(dapm, pin);
  94. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  95. if (!full_name)
  96. return -ENOMEM;
  97. ret = snd_soc_dapm_disable_pin(dapm, full_name);
  98. kfree(full_name);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);
  102. int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
  103. const char *pin)
  104. {
  105. struct snd_soc_dapm_context *dapm =
  106. snd_soc_component_get_dapm(component);
  107. char *full_name;
  108. int ret;
  109. if (!component->name_prefix)
  110. return snd_soc_dapm_disable_pin_unlocked(dapm, pin);
  111. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  112. if (!full_name)
  113. return -ENOMEM;
  114. ret = snd_soc_dapm_disable_pin_unlocked(dapm, full_name);
  115. kfree(full_name);
  116. return ret;
  117. }
  118. EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);
  119. int snd_soc_component_nc_pin(struct snd_soc_component *component,
  120. const char *pin)
  121. {
  122. struct snd_soc_dapm_context *dapm =
  123. snd_soc_component_get_dapm(component);
  124. char *full_name;
  125. int ret;
  126. if (!component->name_prefix)
  127. return snd_soc_dapm_nc_pin(dapm, pin);
  128. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  129. if (!full_name)
  130. return -ENOMEM;
  131. ret = snd_soc_dapm_nc_pin(dapm, full_name);
  132. kfree(full_name);
  133. return ret;
  134. }
  135. EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);
  136. int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
  137. const char *pin)
  138. {
  139. struct snd_soc_dapm_context *dapm =
  140. snd_soc_component_get_dapm(component);
  141. char *full_name;
  142. int ret;
  143. if (!component->name_prefix)
  144. return snd_soc_dapm_nc_pin_unlocked(dapm, pin);
  145. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  146. if (!full_name)
  147. return -ENOMEM;
  148. ret = snd_soc_dapm_nc_pin_unlocked(dapm, full_name);
  149. kfree(full_name);
  150. return ret;
  151. }
  152. EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);
  153. int snd_soc_component_get_pin_status(struct snd_soc_component *component,
  154. const char *pin)
  155. {
  156. struct snd_soc_dapm_context *dapm =
  157. snd_soc_component_get_dapm(component);
  158. char *full_name;
  159. int ret;
  160. if (!component->name_prefix)
  161. return snd_soc_dapm_get_pin_status(dapm, pin);
  162. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  163. if (!full_name)
  164. return -ENOMEM;
  165. ret = snd_soc_dapm_get_pin_status(dapm, full_name);
  166. kfree(full_name);
  167. return ret;
  168. }
  169. EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);
  170. int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
  171. const char *pin)
  172. {
  173. struct snd_soc_dapm_context *dapm =
  174. snd_soc_component_get_dapm(component);
  175. char *full_name;
  176. int ret;
  177. if (!component->name_prefix)
  178. return snd_soc_dapm_force_enable_pin(dapm, pin);
  179. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  180. if (!full_name)
  181. return -ENOMEM;
  182. ret = snd_soc_dapm_force_enable_pin(dapm, full_name);
  183. kfree(full_name);
  184. return ret;
  185. }
  186. EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);
  187. int snd_soc_component_force_enable_pin_unlocked(
  188. struct snd_soc_component *component,
  189. const char *pin)
  190. {
  191. struct snd_soc_dapm_context *dapm =
  192. snd_soc_component_get_dapm(component);
  193. char *full_name;
  194. int ret;
  195. if (!component->name_prefix)
  196. return snd_soc_dapm_force_enable_pin_unlocked(dapm, pin);
  197. full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
  198. if (!full_name)
  199. return -ENOMEM;
  200. ret = snd_soc_dapm_force_enable_pin_unlocked(dapm, full_name);
  201. kfree(full_name);
  202. return ret;
  203. }
  204. EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);
  205. static const struct snd_pcm_hardware dummy_dma_hardware = {
  206. /* Random values to keep userspace happy when checking constraints */
  207. .info = SNDRV_PCM_INFO_INTERLEAVED |
  208. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  209. .buffer_bytes_max = 128*1024,
  210. .period_bytes_min = PAGE_SIZE,
  211. .period_bytes_max = PAGE_SIZE*2,
  212. .periods_min = 2,
  213. .periods_max = 128,
  214. };
  215. static int dummy_dma_open(struct snd_pcm_substream *substream)
  216. {
  217. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  218. /* BE's dont need dummy params */
  219. if (!rtd->dai_link->no_pcm)
  220. snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
  221. return 0;
  222. }
  223. static const struct snd_pcm_ops dummy_dma_ops = {
  224. .open = dummy_dma_open,
  225. .ioctl = snd_pcm_lib_ioctl,
  226. };
  227. static const struct snd_soc_platform_driver dummy_platform = {
  228. .ops = &dummy_dma_ops,
  229. };
  230. static struct snd_soc_codec_driver dummy_codec;
  231. #define STUB_RATES SNDRV_PCM_RATE_8000_192000
  232. #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  233. SNDRV_PCM_FMTBIT_U8 | \
  234. SNDRV_PCM_FMTBIT_S16_LE | \
  235. SNDRV_PCM_FMTBIT_U16_LE | \
  236. SNDRV_PCM_FMTBIT_S24_LE | \
  237. SNDRV_PCM_FMTBIT_U24_LE | \
  238. SNDRV_PCM_FMTBIT_S32_LE | \
  239. SNDRV_PCM_FMTBIT_U32_LE | \
  240. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
  241. /*
  242. * The dummy CODEC is only meant to be used in situations where there is no
  243. * actual hardware.
  244. *
  245. * If there is actual hardware even if it does not have a control bus
  246. * the hardware will still have constraints like supported samplerates, etc.
  247. * which should be modelled. And the data flow graph also should be modelled
  248. * using DAPM.
  249. */
  250. static struct snd_soc_dai_driver dummy_dai = {
  251. .name = "snd-soc-dummy-dai",
  252. .playback = {
  253. .stream_name = "Playback",
  254. .channels_min = 1,
  255. .channels_max = 384,
  256. .rates = STUB_RATES,
  257. .formats = STUB_FORMATS,
  258. },
  259. .capture = {
  260. .stream_name = "Capture",
  261. .channels_min = 1,
  262. .channels_max = 384,
  263. .rates = STUB_RATES,
  264. .formats = STUB_FORMATS,
  265. },
  266. };
  267. int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
  268. {
  269. if (dai->driver == &dummy_dai)
  270. return 1;
  271. return 0;
  272. }
  273. static int snd_soc_dummy_probe(struct platform_device *pdev)
  274. {
  275. int ret;
  276. ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
  277. if (ret < 0)
  278. return ret;
  279. ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
  280. if (ret < 0) {
  281. snd_soc_unregister_codec(&pdev->dev);
  282. return ret;
  283. }
  284. return ret;
  285. }
  286. static int snd_soc_dummy_remove(struct platform_device *pdev)
  287. {
  288. snd_soc_unregister_platform(&pdev->dev);
  289. snd_soc_unregister_codec(&pdev->dev);
  290. return 0;
  291. }
  292. static struct platform_driver soc_dummy_driver = {
  293. .driver = {
  294. .name = "snd-soc-dummy",
  295. },
  296. .probe = snd_soc_dummy_probe,
  297. .remove = snd_soc_dummy_remove,
  298. };
  299. static struct platform_device *soc_dummy_dev;
  300. int __init snd_soc_util_init(void)
  301. {
  302. int ret;
  303. soc_dummy_dev =
  304. platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
  305. if (IS_ERR(soc_dummy_dev))
  306. return PTR_ERR(soc_dummy_dev);
  307. ret = platform_driver_register(&soc_dummy_driver);
  308. if (ret != 0)
  309. platform_device_unregister(soc_dummy_dev);
  310. return ret;
  311. }
  312. void __exit snd_soc_util_exit(void)
  313. {
  314. platform_device_unregister(soc_dummy_dev);
  315. platform_driver_unregister(&soc_dummy_driver);
  316. }