]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/omap/omap-hdmi.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[karo-tx-linux.git] / sound / soc / omap / omap-hdmi.c
1 /*
2  * omap-hdmi.c
3  *
4  * OMAP ALSA SoC DAI driver for HDMI audio on OMAP4 processors.
5  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
6  * Authors: Jorge Candelaria <jorge.candelaria@ti.com>
7  *          Ricardo Neri <ricardo.neri@ti.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * version 2 as published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
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 <sound/asound.h>
34 #include <sound/asoundef.h>
35 #include <video/omapdss.h>
36
37 #include "omap-pcm.h"
38 #include "omap-hdmi.h"
39
40 #define DRV_NAME "omap-hdmi-audio-dai"
41
42 struct hdmi_priv {
43         struct omap_pcm_dma_data dma_params;
44         struct omap_dss_audio dss_audio;
45         struct snd_aes_iec958 iec;
46         struct snd_cea_861_aud_if cea;
47         struct omap_dss_device *dssdev;
48 };
49
50 static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
51                                   struct snd_soc_dai *dai)
52 {
53         struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
54         int err;
55         /*
56          * Make sure that the period bytes are multiple of the DMA packet size.
57          * Largest packet size we use is 32 32-bit words = 128 bytes
58          */
59         err = snd_pcm_hw_constraint_step(substream->runtime, 0,
60                                  SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
61         if (err < 0) {
62                 dev_err(dai->dev, "could not apply constraint\n");
63                 return err;
64         }
65
66         if (!priv->dssdev->driver->audio_supported(priv->dssdev)) {
67                 dev_err(dai->dev, "audio not supported\n");
68                 return -ENODEV;
69         }
70
71         snd_soc_dai_set_dma_data(dai, substream, &priv->dma_params);
72
73         return 0;
74 }
75
76 static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
77                                 struct snd_soc_dai *dai)
78 {
79         struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
80
81         return priv->dssdev->driver->audio_enable(priv->dssdev);
82 }
83
84 static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
85                                     struct snd_pcm_hw_params *params,
86                                     struct snd_soc_dai *dai)
87 {
88         struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
89         struct snd_aes_iec958 *iec = &priv->iec;
90         struct snd_cea_861_aud_if *cea = &priv->cea;
91         struct omap_pcm_dma_data *dma_data;
92         int err = 0;
93
94         dma_data = snd_soc_dai_get_dma_data(dai, substream);
95
96         switch (params_format(params)) {
97         case SNDRV_PCM_FORMAT_S16_LE:
98                 dma_data->packet_size = 16;
99                 break;
100         case SNDRV_PCM_FORMAT_S24_LE:
101                 dma_data->packet_size = 32;
102                 break;
103         default:
104                 dev_err(dai->dev, "format not supported!\n");
105                 return -EINVAL;
106         }
107
108         dma_data->data_type = 32;
109
110         /*
111          * fill the IEC-60958 channel status word
112          */
113         /* initialize the word bytes */
114         memset(iec->status, 0, sizeof(iec->status));
115
116         /* specify IEC-60958-3 (commercial use) */
117         iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
118
119         /* specify that the audio is LPCM*/
120         iec->status[0] &= ~IEC958_AES0_NONAUDIO;
121
122         iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
123
124         iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
125
126         iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
127
128         iec->status[1] = IEC958_AES1_CON_GENERAL;
129
130         iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
131
132         iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
133
134         switch (params_rate(params)) {
135         case 32000:
136                 iec->status[3] |= IEC958_AES3_CON_FS_32000;
137                 break;
138         case 44100:
139                 iec->status[3] |= IEC958_AES3_CON_FS_44100;
140                 break;
141         case 48000:
142                 iec->status[3] |= IEC958_AES3_CON_FS_48000;
143                 break;
144         case 88200:
145                 iec->status[3] |= IEC958_AES3_CON_FS_88200;
146                 break;
147         case 96000:
148                 iec->status[3] |= IEC958_AES3_CON_FS_96000;
149                 break;
150         case 176400:
151                 iec->status[3] |= IEC958_AES3_CON_FS_176400;
152                 break;
153         case 192000:
154                 iec->status[3] |= IEC958_AES3_CON_FS_192000;
155                 break;
156         default:
157                 dev_err(dai->dev, "rate not supported!\n");
158                 return -EINVAL;
159         }
160
161         /* specify the clock accuracy */
162         iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
163
164         /*
165          * specify the word length. The same word length value can mean
166          * two different lengths. Hence, we need to specify the maximum
167          * word length as well.
168          */
169         switch (params_format(params)) {
170         case SNDRV_PCM_FORMAT_S16_LE:
171                 iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
172                 iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
173                 break;
174         case SNDRV_PCM_FORMAT_S24_LE:
175                 iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
176                 iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
177                 break;
178         default:
179                 dev_err(dai->dev, "format not supported!\n");
180                 return -EINVAL;
181         }
182
183         /*
184          * Fill the CEA-861 audio infoframe (see spec for details)
185          */
186
187         cea->db1_ct_cc = (params_channels(params) - 1)
188                 & CEA861_AUDIO_INFOFRAME_DB1CC;
189         cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
190
191         cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
192         cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
193
194         cea->db3 = 0; /* not used, all zeros */
195
196         /*
197          * The OMAP HDMI IP requires to use the 8-channel channel code when
198          * transmitting more than two channels.
199          */
200         if (params_channels(params) == 2)
201                 cea->db4_ca = 0x0;
202         else
203                 cea->db4_ca = 0x13;
204
205         cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
206         /* the expression is trivial but makes clear what we are doing */
207         cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
208
209         priv->dss_audio.iec = iec;
210         priv->dss_audio.cea = cea;
211
212         err = priv->dssdev->driver->audio_config(priv->dssdev,
213                                                  &priv->dss_audio);
214
215         return err;
216 }
217
218 static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
219                                 struct snd_soc_dai *dai)
220 {
221         struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
222         int err = 0;
223
224         switch (cmd) {
225         case SNDRV_PCM_TRIGGER_START:
226         case SNDRV_PCM_TRIGGER_RESUME:
227         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
228                 err = priv->dssdev->driver->audio_start(priv->dssdev);
229                 break;
230         case SNDRV_PCM_TRIGGER_STOP:
231         case SNDRV_PCM_TRIGGER_SUSPEND:
232         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
233                 priv->dssdev->driver->audio_stop(priv->dssdev);
234                 break;
235         default:
236                 err = -EINVAL;
237         }
238         return err;
239 }
240
241 static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
242                                 struct snd_soc_dai *dai)
243 {
244         struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
245
246         priv->dssdev->driver->audio_disable(priv->dssdev);
247 }
248
249 static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
250         .startup        = omap_hdmi_dai_startup,
251         .hw_params      = omap_hdmi_dai_hw_params,
252         .prepare        = omap_hdmi_dai_prepare,
253         .trigger        = omap_hdmi_dai_trigger,
254         .shutdown       = omap_hdmi_dai_shutdown,
255 };
256
257 static struct snd_soc_dai_driver omap_hdmi_dai = {
258         .playback = {
259                 .channels_min = 2,
260                 .channels_max = 8,
261                 .rates = OMAP_HDMI_RATES,
262                 .formats = OMAP_HDMI_FORMATS,
263         },
264         .ops = &omap_hdmi_dai_ops,
265 };
266
267 static int omap_hdmi_probe(struct platform_device *pdev)
268 {
269         int ret;
270         struct resource *hdmi_rsrc;
271         struct hdmi_priv *hdmi_data;
272         bool hdmi_dev_found = false;
273
274         hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
275         if (hdmi_data == NULL) {
276                 dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
277                 return -ENOMEM;
278         }
279
280         hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
281         if (!hdmi_rsrc) {
282                 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM HDMI\n");
283                 return -ENODEV;
284         }
285
286         hdmi_data->dma_params.port_addr =  hdmi_rsrc->start
287                 + OMAP_HDMI_AUDIO_DMA_PORT;
288
289         hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
290         if (!hdmi_rsrc) {
291                 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
292                 return -ENODEV;
293         }
294
295         hdmi_data->dma_params.dma_req =  hdmi_rsrc->start;
296         hdmi_data->dma_params.name = "HDMI playback";
297
298         /*
299          * TODO: We assume that there is only one DSS HDMI device. Future
300          * OMAP implementations may support more than one HDMI devices and
301          * we should provided separate audio support for all of them.
302          */
303         /* Find an HDMI device. */
304         for_each_dss_dev(hdmi_data->dssdev) {
305                 omap_dss_get_device(hdmi_data->dssdev);
306
307                 if (!hdmi_data->dssdev->driver) {
308                         omap_dss_put_device(hdmi_data->dssdev);
309                         continue;
310                 }
311
312                 if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
313                         hdmi_dev_found = true;
314                         break;
315                 }
316         }
317
318         if (!hdmi_dev_found) {
319                 dev_err(&pdev->dev, "no driver for HDMI display found\n");
320                 return -ENODEV;
321         }
322
323         dev_set_drvdata(&pdev->dev, hdmi_data);
324         ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
325
326         return ret;
327 }
328
329 static int omap_hdmi_remove(struct platform_device *pdev)
330 {
331         struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
332
333         snd_soc_unregister_dai(&pdev->dev);
334
335         if (hdmi_data == NULL) {
336                 dev_err(&pdev->dev, "cannot obtain HDMi data\n");
337                 return -ENODEV;
338         }
339
340         omap_dss_put_device(hdmi_data->dssdev);
341         return 0;
342 }
343
344 static struct platform_driver hdmi_dai_driver = {
345         .driver = {
346                 .name = DRV_NAME,
347                 .owner = THIS_MODULE,
348         },
349         .probe = omap_hdmi_probe,
350         .remove = omap_hdmi_remove,
351 };
352
353 module_platform_driver(hdmi_dai_driver);
354
355 MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
356 MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
357 MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
358 MODULE_LICENSE("GPL");
359 MODULE_ALIAS("platform:" DRV_NAME);