omap-mcpdm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * omap-mcpdm.c -- OMAP ALSA SoC DAI driver using McPDM port
  3. *
  4. * Copyright (C) 2009 Texas Instruments
  5. *
  6. * Author: Misael Lopez Cruz <x0052729@ti.com>
  7. * Contact: Jorge Eduardo Candelaria <x0107209@ti.com>
  8. * Margarita Olaya <magi.olaya@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  22. * 02110-1301 USA
  23. *
  24. */
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <sound/core.h>
  29. #include <sound/pcm.h>
  30. #include <sound/pcm_params.h>
  31. #include <sound/initval.h>
  32. #include <sound/soc.h>
  33. #include <plat/dma.h>
  34. #include <plat/mcbsp.h>
  35. #include "mcpdm.h"
  36. #include "omap-pcm.h"
  37. struct omap_mcpdm_data {
  38. struct omap_mcpdm_link *links;
  39. int active;
  40. };
  41. static struct omap_mcpdm_link omap_mcpdm_links[] = {
  42. /* downlink */
  43. {
  44. .irq_mask = MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL,
  45. .threshold = 1,
  46. .format = PDMOUTFORMAT_LJUST,
  47. },
  48. /* uplink */
  49. {
  50. .irq_mask = MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL,
  51. .threshold = 1,
  52. .format = PDMOUTFORMAT_LJUST,
  53. },
  54. };
  55. static struct omap_mcpdm_data mcpdm_data = {
  56. .links = omap_mcpdm_links,
  57. .active = 0,
  58. };
  59. /*
  60. * Stream DMA parameters
  61. */
  62. static struct omap_pcm_dma_data omap_mcpdm_dai_dma_params[] = {
  63. {
  64. .name = "Audio playback",
  65. .dma_req = OMAP44XX_DMA_MCPDM_DL,
  66. .data_type = OMAP_DMA_DATA_TYPE_S32,
  67. .sync_mode = OMAP_DMA_SYNC_PACKET,
  68. .packet_size = 16,
  69. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_DN_DATA,
  70. },
  71. {
  72. .name = "Audio capture",
  73. .dma_req = OMAP44XX_DMA_MCPDM_UP,
  74. .data_type = OMAP_DMA_DATA_TYPE_S32,
  75. .sync_mode = OMAP_DMA_SYNC_PACKET,
  76. .packet_size = 16,
  77. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_UP_DATA,
  78. },
  79. };
  80. static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream,
  81. struct snd_soc_dai *dai)
  82. {
  83. int err = 0;
  84. if (!dai->active)
  85. err = omap_mcpdm_request();
  86. return err;
  87. }
  88. static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream,
  89. struct snd_soc_dai *dai)
  90. {
  91. if (!dai->active)
  92. omap_mcpdm_free();
  93. }
  94. static int omap_mcpdm_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  95. struct snd_soc_dai *dai)
  96. {
  97. struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
  98. int stream = substream->stream;
  99. int err = 0;
  100. switch (cmd) {
  101. case SNDRV_PCM_TRIGGER_START:
  102. case SNDRV_PCM_TRIGGER_RESUME:
  103. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  104. if (!mcpdm_priv->active++)
  105. omap_mcpdm_start(stream);
  106. break;
  107. case SNDRV_PCM_TRIGGER_STOP:
  108. case SNDRV_PCM_TRIGGER_SUSPEND:
  109. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  110. if (!--mcpdm_priv->active)
  111. omap_mcpdm_stop(stream);
  112. break;
  113. default:
  114. err = -EINVAL;
  115. }
  116. return err;
  117. }
  118. static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream,
  119. struct snd_pcm_hw_params *params,
  120. struct snd_soc_dai *dai)
  121. {
  122. struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
  123. struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
  124. int stream = substream->stream;
  125. int channels, err, link_mask = 0;
  126. snd_soc_dai_set_dma_data(dai, substream,
  127. &omap_mcpdm_dai_dma_params[stream]);
  128. channels = params_channels(params);
  129. switch (channels) {
  130. case 4:
  131. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  132. /* up to 2 channels for capture */
  133. return -EINVAL;
  134. link_mask |= 1 << 3;
  135. case 3:
  136. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  137. /* up to 2 channels for capture */
  138. return -EINVAL;
  139. link_mask |= 1 << 2;
  140. case 2:
  141. link_mask |= 1 << 1;
  142. case 1:
  143. link_mask |= 1 << 0;
  144. break;
  145. default:
  146. /* unsupported number of channels */
  147. return -EINVAL;
  148. }
  149. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  150. mcpdm_links[stream].channels = link_mask << 3;
  151. err = omap_mcpdm_playback_open(&mcpdm_links[stream]);
  152. } else {
  153. mcpdm_links[stream].channels = link_mask << 0;
  154. err = omap_mcpdm_capture_open(&mcpdm_links[stream]);
  155. }
  156. return err;
  157. }
  158. static int omap_mcpdm_dai_hw_free(struct snd_pcm_substream *substream,
  159. struct snd_soc_dai *dai)
  160. {
  161. struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
  162. struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
  163. int stream = substream->stream;
  164. int err;
  165. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  166. err = omap_mcpdm_playback_close(&mcpdm_links[stream]);
  167. else
  168. err = omap_mcpdm_capture_close(&mcpdm_links[stream]);
  169. return err;
  170. }
  171. static struct snd_soc_dai_ops omap_mcpdm_dai_ops = {
  172. .startup = omap_mcpdm_dai_startup,
  173. .shutdown = omap_mcpdm_dai_shutdown,
  174. .trigger = omap_mcpdm_dai_trigger,
  175. .hw_params = omap_mcpdm_dai_hw_params,
  176. .hw_free = omap_mcpdm_dai_hw_free,
  177. };
  178. #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  179. #define OMAP_MCPDM_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  180. static int omap_mcpdm_dai_probe(struct snd_soc_dai *dai)
  181. {
  182. snd_soc_dai_set_drvdata(dai, &mcpdm_data);
  183. return 0;
  184. }
  185. static struct snd_soc_dai_driver omap_mcpdm_dai = {
  186. .probe = omap_mcpdm_dai_probe,
  187. .playback = {
  188. .channels_min = 1,
  189. .channels_max = 4,
  190. .rates = OMAP_MCPDM_RATES,
  191. .formats = OMAP_MCPDM_FORMATS,
  192. },
  193. .capture = {
  194. .channels_min = 1,
  195. .channels_max = 2,
  196. .rates = OMAP_MCPDM_RATES,
  197. .formats = OMAP_MCPDM_FORMATS,
  198. },
  199. .ops = &omap_mcpdm_dai_ops,
  200. };
  201. static __devinit int asoc_mcpdm_probe(struct platform_device *pdev)
  202. {
  203. int ret;
  204. ret = omap_mcpdm_probe(pdev);
  205. if (ret < 0)
  206. return ret;
  207. ret = snd_soc_register_dai(&pdev->dev, &omap_mcpdm_dai);
  208. if (ret < 0)
  209. omap_mcpdm_remove(pdev);
  210. return ret;
  211. }
  212. static int __devexit asoc_mcpdm_remove(struct platform_device *pdev)
  213. {
  214. snd_soc_unregister_dai(&pdev->dev);
  215. omap_mcpdm_remove(pdev);
  216. return 0;
  217. }
  218. static struct platform_driver asoc_mcpdm_driver = {
  219. .driver = {
  220. .name = "omap-mcpdm-dai",
  221. .owner = THIS_MODULE,
  222. },
  223. .probe = asoc_mcpdm_probe,
  224. .remove = __devexit_p(asoc_mcpdm_remove),
  225. };
  226. static int __init snd_omap_mcpdm_init(void)
  227. {
  228. return platform_driver_register(&asoc_mcpdm_driver);
  229. }
  230. module_init(snd_omap_mcpdm_init);
  231. static void __exit snd_omap_mcpdm_exit(void)
  232. {
  233. platform_driver_unregister(&asoc_mcpdm_driver);
  234. }
  235. module_exit(snd_omap_mcpdm_exit);
  236. MODULE_AUTHOR("Misael Lopez Cruz <x0052729@ti.com>");
  237. MODULE_DESCRIPTION("OMAP PDM SoC Interface");
  238. MODULE_LICENSE("GPL");