]> Pileus Git - ~andy/linux/blob - sound/soc/omap/omap-mcpdm.c
7727de0c998e9652dc8c9f64bdfb22f94678132f
[~andy/linux] / sound / soc / omap / omap-mcpdm.c
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
26 #include <linux/init.h>
27 #include <linux/module.h>
28 #include <linux/device.h>
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/initval.h>
33 #include <sound/soc.h>
34
35 #include <plat/dma.h>
36 #include <plat/mcbsp.h>
37 #include "mcpdm.h"
38 #include "omap-pcm.h"
39
40 struct omap_mcpdm_data {
41         struct omap_mcpdm_link *links;
42         int active;
43 };
44
45 static struct omap_mcpdm_link omap_mcpdm_links[] = {
46         /* downlink */
47         {
48                 .irq_mask = MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL,
49                 .threshold = 2,
50                 .format = PDMOUTFORMAT_LJUST,
51         },
52         /* uplink */
53         {
54                 .irq_mask = MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL,
55                 .threshold = UP_THRES_MAX - 3,
56                 .format = PDMOUTFORMAT_LJUST,
57         },
58 };
59
60 static struct omap_mcpdm_data mcpdm_data = {
61         .links = omap_mcpdm_links,
62         .active = 0,
63 };
64
65 /*
66  * Stream DMA parameters
67  */
68 static struct omap_pcm_dma_data omap_mcpdm_dai_dma_params[] = {
69         {
70                 .name = "Audio playback",
71                 .dma_req = OMAP44XX_DMA_MCPDM_DL,
72                 .data_type = OMAP_DMA_DATA_TYPE_S32,
73                 .sync_mode = OMAP_DMA_SYNC_PACKET,
74                 .packet_size = 16,
75                 .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_DN_DATA,
76         },
77         {
78                 .name = "Audio capture",
79                 .dma_req = OMAP44XX_DMA_MCPDM_UP,
80                 .data_type = OMAP_DMA_DATA_TYPE_S32,
81                 .sync_mode = OMAP_DMA_SYNC_PACKET,
82                 .packet_size = 16,
83                 .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_UP_DATA,
84         },
85 };
86
87 static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream,
88                                   struct snd_soc_dai *dai)
89 {
90         int err = 0;
91
92         if (!dai->active)
93                 err = omap_mcpdm_request();
94
95         return err;
96 }
97
98 static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream,
99                                     struct snd_soc_dai *dai)
100 {
101         if (!dai->active)
102                 omap_mcpdm_free();
103 }
104
105 static int omap_mcpdm_dai_trigger(struct snd_pcm_substream *substream, int cmd,
106                                   struct snd_soc_dai *dai)
107 {
108         struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
109         int stream = substream->stream;
110         int err = 0;
111
112         switch (cmd) {
113         case SNDRV_PCM_TRIGGER_START:
114         case SNDRV_PCM_TRIGGER_RESUME:
115         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
116                 if (!mcpdm_priv->active++)
117                         omap_mcpdm_start(stream);
118                 break;
119
120         case SNDRV_PCM_TRIGGER_STOP:
121         case SNDRV_PCM_TRIGGER_SUSPEND:
122         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
123                 if (!--mcpdm_priv->active)
124                         omap_mcpdm_stop(stream);
125                 break;
126         default:
127                 err = -EINVAL;
128         }
129
130         return err;
131 }
132
133 static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream,
134                                     struct snd_pcm_hw_params *params,
135                                     struct snd_soc_dai *dai)
136 {
137         struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
138         struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
139         struct omap_pcm_dma_data *dma_data;
140         int threshold;
141         int stream = substream->stream;
142         int channels, err, link_mask = 0;
143
144         channels = params_channels(params);
145         switch (channels) {
146         case 4:
147                 if (stream == SNDRV_PCM_STREAM_CAPTURE)
148                         /* up to 2 channels for capture */
149                         return -EINVAL;
150                 link_mask |= 1 << 3;
151         case 3:
152                 if (stream == SNDRV_PCM_STREAM_CAPTURE)
153                         /* up to 2 channels for capture */
154                         return -EINVAL;
155                 link_mask |= 1 << 2;
156         case 2:
157                 link_mask |= 1 << 1;
158         case 1:
159                 link_mask |= 1 << 0;
160                 break;
161         default:
162                 /* unsupported number of channels */
163                 return -EINVAL;
164         }
165
166         dma_data = &omap_mcpdm_dai_dma_params[stream];
167         threshold = mcpdm_links[stream].threshold;
168
169         if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
170                 mcpdm_links[stream].channels = link_mask << 3;
171                 dma_data->packet_size = (DN_THRES_MAX - threshold) * channels;
172
173                 err = omap_mcpdm_playback_open(&mcpdm_links[stream]);
174         } else {
175                 mcpdm_links[stream].channels = link_mask << 0;
176                 dma_data->packet_size = threshold * channels;
177
178                 err = omap_mcpdm_capture_open(&mcpdm_links[stream]);
179         }
180
181         snd_soc_dai_set_dma_data(dai, substream, dma_data);
182         return err;
183 }
184
185 static int omap_mcpdm_dai_hw_free(struct snd_pcm_substream *substream,
186                                   struct snd_soc_dai *dai)
187 {
188         struct omap_mcpdm_data *mcpdm_priv = snd_soc_dai_get_drvdata(dai);
189         struct omap_mcpdm_link *mcpdm_links = mcpdm_priv->links;
190         int stream = substream->stream;
191         int err;
192
193         if (substream->stream ==  SNDRV_PCM_STREAM_PLAYBACK)
194                 err = omap_mcpdm_playback_close(&mcpdm_links[stream]);
195         else
196                 err = omap_mcpdm_capture_close(&mcpdm_links[stream]);
197
198         return err;
199 }
200
201 static struct snd_soc_dai_ops omap_mcpdm_dai_ops = {
202         .startup        = omap_mcpdm_dai_startup,
203         .shutdown       = omap_mcpdm_dai_shutdown,
204         .trigger        = omap_mcpdm_dai_trigger,
205         .hw_params      = omap_mcpdm_dai_hw_params,
206         .hw_free        = omap_mcpdm_dai_hw_free,
207 };
208
209 #define OMAP_MCPDM_RATES        (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
210 #define OMAP_MCPDM_FORMATS      (SNDRV_PCM_FMTBIT_S32_LE)
211
212 static int omap_mcpdm_dai_probe(struct snd_soc_dai *dai)
213 {
214         snd_soc_dai_set_drvdata(dai, &mcpdm_data);
215         return 0;
216 }
217
218 static struct snd_soc_dai_driver omap_mcpdm_dai = {
219         .probe = omap_mcpdm_dai_probe,
220         .playback = {
221                 .channels_min = 1,
222                 .channels_max = 4,
223                 .rates = OMAP_MCPDM_RATES,
224                 .formats = OMAP_MCPDM_FORMATS,
225         },
226         .capture = {
227                 .channels_min = 1,
228                 .channels_max = 2,
229                 .rates = OMAP_MCPDM_RATES,
230                 .formats = OMAP_MCPDM_FORMATS,
231         },
232         .ops = &omap_mcpdm_dai_ops,
233 };
234
235 static __devinit int asoc_mcpdm_probe(struct platform_device *pdev)
236 {
237         int ret;
238
239         ret = omap_mcpdm_probe(pdev);
240         if (ret < 0)
241                 return ret;
242         ret = snd_soc_register_dai(&pdev->dev, &omap_mcpdm_dai);
243         if (ret < 0)
244                 omap_mcpdm_remove(pdev);
245         return ret;
246 }
247
248 static int __devexit asoc_mcpdm_remove(struct platform_device *pdev)
249 {
250         snd_soc_unregister_dai(&pdev->dev);
251         omap_mcpdm_remove(pdev);
252         return 0;
253 }
254
255 static struct platform_driver asoc_mcpdm_driver = {
256         .driver = {
257                         .name = "omap-mcpdm-dai",
258                         .owner = THIS_MODULE,
259         },
260
261         .probe = asoc_mcpdm_probe,
262         .remove = __devexit_p(asoc_mcpdm_remove),
263 };
264
265 static int __init snd_omap_mcpdm_init(void)
266 {
267         return platform_driver_register(&asoc_mcpdm_driver);
268 }
269 module_init(snd_omap_mcpdm_init);
270
271 static void __exit snd_omap_mcpdm_exit(void)
272 {
273         platform_driver_unregister(&asoc_mcpdm_driver);
274 }
275 module_exit(snd_omap_mcpdm_exit);
276
277 MODULE_AUTHOR("Misael Lopez Cruz <x0052729@ti.com>");
278 MODULE_DESCRIPTION("OMAP PDM SoC Interface");
279 MODULE_LICENSE("GPL");