omap-mcpdm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-mcpdm.h"
  37. #include "omap-pcm.h"
  38. struct omap_mcpdm_data {
  39. struct omap_mcpdm_link *links;
  40. int active;
  41. };
  42. static struct omap_mcpdm_link omap_mcpdm_links[] = {
  43. /* downlink */
  44. {
  45. .irq_mask = MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL,
  46. .threshold = 1,
  47. .format = PDMOUTFORMAT_LJUST,
  48. },
  49. /* uplink */
  50. {
  51. .irq_mask = MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL,
  52. .threshold = 1,
  53. .format = PDMOUTFORMAT_LJUST,
  54. },
  55. };
  56. static struct omap_mcpdm_data mcpdm_data = {
  57. .links = omap_mcpdm_links,
  58. .active = 0,
  59. };
  60. /*
  61. * Stream DMA parameters
  62. */
  63. static struct omap_pcm_dma_data omap_mcpdm_dai_dma_params[] = {
  64. {
  65. .name = "Audio playback",
  66. .dma_req = OMAP44XX_DMA_MCPDM_DL,
  67. .data_type = OMAP_DMA_DATA_TYPE_S32,
  68. .sync_mode = OMAP_DMA_SYNC_PACKET,
  69. .packet_size = 16,
  70. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_DN_DATA,
  71. },
  72. {
  73. .name = "Audio capture",
  74. .dma_req = OMAP44XX_DMA_MCPDM_UP,
  75. .data_type = OMAP_DMA_DATA_TYPE_S32,
  76. .sync_mode = OMAP_DMA_SYNC_PACKET,
  77. .packet_size = 16,
  78. .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_UP_DATA,
  79. },
  80. };
  81. static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream,
  82. struct snd_soc_dai *dai)
  83. {
  84. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  85. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  86. int err = 0;
  87. if (!cpu_dai->active)
  88. err = omap_mcpdm_request();
  89. return err;
  90. }
  91. static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream,
  92. struct snd_soc_dai *dai)
  93. {
  94. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  95. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  96. if (!cpu_dai->active)
  97. omap_mcpdm_free();
  98. }
  99. static int omap_mcpdm_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  100. struct snd_soc_dai *dai)
  101. {
  102. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  103. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  104. struct omap_mcpdm_data *mcpdm_priv = cpu_dai->private_data;
  105. int stream = substream->stream;
  106. int err = 0;
  107. switch (cmd) {
  108. case SNDRV_PCM_TRIGGER_START:
  109. case SNDRV_PCM_TRIGGER_RESUME:
  110. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  111. if (!mcpdm_priv->active++)
  112. omap_mcpdm_start(stream);
  113. break;
  114. case SNDRV_PCM_TRIGGER_STOP:
  115. case SNDRV_PCM_TRIGGER_SUSPEND:
  116. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  117. if (!--mcpdm_priv->active)
  118. omap_mcpdm_stop(stream);
  119. break;
  120. default:
  121. err = -EINVAL;
  122. }
  123. return err;
  124. }
  125. static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream,
  126. struct snd_pcm_hw_params *params,
  127. struct snd_soc_dai *dai)
  128. {
  129. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  130. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  131. struct omap_mcpdm_data *mcpdm_priv = cpu_dai->private_data;
  132. struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
  133. int stream = substream->stream;
  134. int channels, err, link_mask = 0;
  135. snd_soc_dai_set_dma_data(cpu_dai, substream,
  136. &omap_mcpdm_dai_dma_params[stream]);
  137. channels = params_channels(params);
  138. switch (channels) {
  139. case 4:
  140. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  141. /* up to 2 channels for capture */
  142. return -EINVAL;
  143. link_mask |= 1 << 3;
  144. case 3:
  145. if (stream == SNDRV_PCM_STREAM_CAPTURE)
  146. /* up to 2 channels for capture */
  147. return -EINVAL;
  148. link_mask |= 1 << 2;
  149. case 2:
  150. link_mask |= 1 << 1;
  151. case 1:
  152. link_mask |= 1 << 0;
  153. break;
  154. default:
  155. /* unsupported number of channels */
  156. return -EINVAL;
  157. }
  158. if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
  159. mcpdm_links[stream].channels = link_mask << 3;
  160. err = omap_mcpdm_playback_open(&mcpdm_links[stream]);
  161. } else {
  162. mcpdm_links[stream].channels = link_mask << 0;
  163. err = omap_mcpdm_capture_open(&mcpdm_links[stream]);
  164. }
  165. return err;
  166. }
  167. static int omap_mcpdm_dai_hw_free(struct snd_pcm_substream *substream,
  168. struct snd_soc_dai *dai)
  169. {
  170. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  171. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  172. struct omap_mcpdm_data *mcpdm_priv = cpu_dai->private_data;
  173. struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
  174. int stream = substream->stream;
  175. int err;
  176. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  177. err = omap_mcpdm_playback_close(&mcpdm_links[stream]);
  178. else
  179. err = omap_mcpdm_capture_close(&mcpdm_links[stream]);
  180. return err;
  181. }
  182. static struct snd_soc_dai_ops omap_mcpdm_dai_ops = {
  183. .startup = omap_mcpdm_dai_startup,
  184. .shutdown = omap_mcpdm_dai_shutdown,
  185. .trigger = omap_mcpdm_dai_trigger,
  186. .hw_params = omap_mcpdm_dai_hw_params,
  187. .hw_free = omap_mcpdm_dai_hw_free,
  188. };
  189. #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
  190. #define OMAP_MCPDM_FORMATS (SNDRV_PCM_FMTBIT_S32_LE)
  191. struct snd_soc_dai omap_mcpdm_dai = {
  192. .name = "omap-mcpdm",
  193. .id = -1,
  194. .playback = {
  195. .channels_min = 1,
  196. .channels_max = 4,
  197. .rates = OMAP_MCPDM_RATES,
  198. .formats = OMAP_MCPDM_FORMATS,
  199. },
  200. .capture = {
  201. .channels_min = 1,
  202. .channels_max = 2,
  203. .rates = OMAP_MCPDM_RATES,
  204. .formats = OMAP_MCPDM_FORMATS,
  205. },
  206. .ops = &omap_mcpdm_dai_ops,
  207. .private_data = &mcpdm_data,
  208. };
  209. EXPORT_SYMBOL_GPL(omap_mcpdm_dai);
  210. static int __init snd_omap_mcpdm_init(void)
  211. {
  212. return snd_soc_register_dai(&omap_mcpdm_dai);
  213. }
  214. module_init(snd_omap_mcpdm_init);
  215. static void __exit snd_omap_mcpdm_exit(void)
  216. {
  217. snd_soc_unregister_dai(&omap_mcpdm_dai);
  218. }
  219. module_exit(snd_omap_mcpdm_exit);
  220. MODULE_AUTHOR("Misael Lopez Cruz <x0052729@ti.com>");
  221. MODULE_DESCRIPTION("OMAP PDM SoC Interface");
  222. MODULE_LICENSE("GPL");