omap-mcpdm.c 6.4 KB

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