blob: fc4815a6efb1f312cebf09b50d0508204af4fc7c [file] [log] [blame]
Ricardo Neribca2e412011-06-02 15:44:45 -05001/*
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>
Ricardo Neri7e6aa222012-05-18 01:42:38 -050033#include <sound/asound.h>
34#include <sound/asoundef.h>
Ricardo Neria48d9b72012-05-18 01:42:37 -050035#include <video/omapdss.h>
Ricardo Neribca2e412011-06-02 15:44:45 -050036
37#include <plat/dma.h>
38#include "omap-pcm.h"
39#include "omap-hdmi.h"
40
Ricardo Nerie412ec62012-05-18 01:42:34 -050041#define DRV_NAME "omap-hdmi-audio-dai"
Ricardo Neribca2e412011-06-02 15:44:45 -050042
Ricardo Neri26039152012-05-18 01:42:36 -050043struct hdmi_priv {
44 struct omap_pcm_dma_data dma_params;
Ricardo Neri7e6aa222012-05-18 01:42:38 -050045 struct omap_dss_audio dss_audio;
46 struct snd_aes_iec958 iec;
47 struct snd_cea_861_aud_if cea;
Ricardo Neria48d9b72012-05-18 01:42:37 -050048 struct omap_dss_device *dssdev;
Ricardo Neribca2e412011-06-02 15:44:45 -050049};
50
51static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
52 struct snd_soc_dai *dai)
53{
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 return err;
63
64 return 0;
65}
66
Ricardo Neria48d9b72012-05-18 01:42:37 -050067static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
68 struct snd_soc_dai *dai)
69{
70 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
71
72 return priv->dssdev->driver->audio_enable(priv->dssdev);
73}
74
Ricardo Neribca2e412011-06-02 15:44:45 -050075static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
76 struct snd_pcm_hw_params *params,
77 struct snd_soc_dai *dai)
78{
Ricardo Neri26039152012-05-18 01:42:36 -050079 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
Ricardo Neri7e6aa222012-05-18 01:42:38 -050080 struct snd_aes_iec958 *iec = &priv->iec;
81 struct snd_cea_861_aud_if *cea = &priv->cea;
Ricardo Neribca2e412011-06-02 15:44:45 -050082 int err = 0;
83
84 switch (params_format(params)) {
85 case SNDRV_PCM_FORMAT_S16_LE:
Ricardo Neri26039152012-05-18 01:42:36 -050086 priv->dma_params.packet_size = 16;
Ricardo Neribca2e412011-06-02 15:44:45 -050087 break;
88 case SNDRV_PCM_FORMAT_S24_LE:
Ricardo Neri26039152012-05-18 01:42:36 -050089 priv->dma_params.packet_size = 32;
Ricardo Neribca2e412011-06-02 15:44:45 -050090 break;
91 default:
Ricardo Neri7e6aa222012-05-18 01:42:38 -050092 dev_err(dai->dev, "format not supported!\n");
93 return -EINVAL;
Ricardo Neribca2e412011-06-02 15:44:45 -050094 }
95
Ricardo Neri26039152012-05-18 01:42:36 -050096 priv->dma_params.data_type = OMAP_DMA_DATA_TYPE_S32;
Ricardo Neribca2e412011-06-02 15:44:45 -050097
98 snd_soc_dai_set_dma_data(dai, substream,
Ricardo Neri26039152012-05-18 01:42:36 -050099 &priv->dma_params);
Ricardo Neribca2e412011-06-02 15:44:45 -0500100
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500101 /*
102 * fill the IEC-60958 channel status word
103 */
104
105 /* specify IEC-60958-3 (commercial use) */
106 iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
107
108 /* specify that the audio is LPCM*/
109 iec->status[0] &= ~IEC958_AES0_NONAUDIO;
110
111 iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
112
113 iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
114
115 iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
116
117 iec->status[1] = IEC958_AES1_CON_GENERAL;
118
119 iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
120
121 iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
122
123 switch (params_rate(params)) {
124 case 32000:
125 iec->status[3] |= IEC958_AES3_CON_FS_32000;
126 break;
127 case 44100:
128 iec->status[3] |= IEC958_AES3_CON_FS_44100;
129 break;
130 case 48000:
131 iec->status[3] |= IEC958_AES3_CON_FS_48000;
132 break;
133 case 88200:
134 iec->status[3] |= IEC958_AES3_CON_FS_88200;
135 break;
136 case 96000:
137 iec->status[3] |= IEC958_AES3_CON_FS_96000;
138 break;
139 case 176400:
140 iec->status[3] |= IEC958_AES3_CON_FS_176400;
141 break;
142 case 192000:
143 iec->status[3] |= IEC958_AES3_CON_FS_192000;
144 break;
145 default:
146 dev_err(dai->dev, "rate not supported!\n");
147 return -EINVAL;
148 }
149
150 /* specify the clock accuracy */
151 iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
152
153 /*
154 * specify the word length. The same word length value can mean
155 * two different lengths. Hence, we need to specify the maximum
156 * word length as well.
157 */
158 switch (params_format(params)) {
159 case SNDRV_PCM_FORMAT_S16_LE:
160 iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
161 iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
162 break;
163 case SNDRV_PCM_FORMAT_S24_LE:
164 iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
165 iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
166 break;
167 default:
168 dev_err(dai->dev, "format not supported!\n");
169 return -EINVAL;
170 }
171
172 /*
173 * Fill the CEA-861 audio infoframe (see spec for details)
174 */
175
176 cea->db1_ct_cc = (params_channels(params) - 1)
177 & CEA861_AUDIO_INFOFRAME_DB1CC;
178 cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
179
180 cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
181 cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
182
183 cea->db3 = 0; /* not used, all zeros */
184
185 /*
186 * The OMAP HDMI IP requires to use the 8-channel channel code when
187 * transmitting more than two channels.
188 */
189 if (params_channels(params) == 2)
190 cea->db4_ca = 0x0;
191 else
192 cea->db4_ca = 0x13;
193
194 cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
195 /* the expression is trivial but makes clear what we are doing */
196 cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
197
198 priv->dss_audio.iec = iec;
199 priv->dss_audio.cea = cea;
200
201 err = priv->dssdev->driver->audio_config(priv->dssdev,
202 &priv->dss_audio);
203
Ricardo Neribca2e412011-06-02 15:44:45 -0500204 return err;
205}
206
Ricardo Neria48d9b72012-05-18 01:42:37 -0500207static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
208 struct snd_soc_dai *dai)
209{
210 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
211 int err = 0;
212
213 switch (cmd) {
214 case SNDRV_PCM_TRIGGER_START:
215 case SNDRV_PCM_TRIGGER_RESUME:
216 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
217 err = priv->dssdev->driver->audio_start(priv->dssdev);
218 break;
219 case SNDRV_PCM_TRIGGER_STOP:
220 case SNDRV_PCM_TRIGGER_SUSPEND:
221 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
222 priv->dssdev->driver->audio_stop(priv->dssdev);
223 break;
224 default:
225 err = -EINVAL;
226 }
227 return err;
228}
229
230static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
231 struct snd_soc_dai *dai)
232{
233 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
234
235 priv->dssdev->driver->audio_disable(priv->dssdev);
236}
237
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100238static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
Ricardo Neribca2e412011-06-02 15:44:45 -0500239 .startup = omap_hdmi_dai_startup,
240 .hw_params = omap_hdmi_dai_hw_params,
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500241 .prepare = omap_hdmi_dai_prepare,
242 .trigger = omap_hdmi_dai_trigger,
243 .shutdown = omap_hdmi_dai_shutdown,
Ricardo Neribca2e412011-06-02 15:44:45 -0500244};
245
246static struct snd_soc_dai_driver omap_hdmi_dai = {
247 .playback = {
248 .channels_min = 2,
249 .channels_max = 2,
250 .rates = OMAP_HDMI_RATES,
251 .formats = OMAP_HDMI_FORMATS,
252 },
253 .ops = &omap_hdmi_dai_ops,
254};
255
256static __devinit int omap_hdmi_probe(struct platform_device *pdev)
257{
258 int ret;
259 struct resource *hdmi_rsrc;
Ricardo Neri26039152012-05-18 01:42:36 -0500260 struct hdmi_priv *hdmi_data;
Ricardo Neria48d9b72012-05-18 01:42:37 -0500261 bool hdmi_dev_found = false;
Ricardo Neri26039152012-05-18 01:42:36 -0500262
263 hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
264 if (hdmi_data == NULL) {
265 dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
266 return -ENOMEM;
267 }
Ricardo Neribca2e412011-06-02 15:44:45 -0500268
269 hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
270 if (!hdmi_rsrc) {
271 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM HDMI\n");
Ricardo Neri0ff5ee82012-05-18 01:42:35 -0500272 return -ENODEV;
Ricardo Neribca2e412011-06-02 15:44:45 -0500273 }
274
Ricardo Neri26039152012-05-18 01:42:36 -0500275 hdmi_data->dma_params.port_addr = hdmi_rsrc->start
Ricardo Neribca2e412011-06-02 15:44:45 -0500276 + OMAP_HDMI_AUDIO_DMA_PORT;
277
278 hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
279 if (!hdmi_rsrc) {
280 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
Ricardo Neri0ff5ee82012-05-18 01:42:35 -0500281 return -ENODEV;
Ricardo Neribca2e412011-06-02 15:44:45 -0500282 }
283
Ricardo Neri26039152012-05-18 01:42:36 -0500284 hdmi_data->dma_params.dma_req = hdmi_rsrc->start;
285 hdmi_data->dma_params.name = "HDMI playback";
286 hdmi_data->dma_params.sync_mode = OMAP_DMA_SYNC_PACKET;
Ricardo Neribca2e412011-06-02 15:44:45 -0500287
Ricardo Neria48d9b72012-05-18 01:42:37 -0500288 /*
289 * TODO: We assume that there is only one DSS HDMI device. Future
290 * OMAP implementations may support more than one HDMI devices and
291 * we should provided separate audio support for all of them.
292 */
293 /* Find an HDMI device. */
294 for_each_dss_dev(hdmi_data->dssdev) {
295 omap_dss_get_device(hdmi_data->dssdev);
296
297 if (!hdmi_data->dssdev->driver) {
298 omap_dss_put_device(hdmi_data->dssdev);
299 continue;
300 }
301
302 if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
303 hdmi_dev_found = true;
304 break;
305 }
306 }
307
308 if (!hdmi_dev_found) {
309 dev_err(&pdev->dev, "no driver for HDMI display found\n");
310 return -ENODEV;
311 }
312
Ricardo Neri26039152012-05-18 01:42:36 -0500313 dev_set_drvdata(&pdev->dev, hdmi_data);
Ricardo Neribca2e412011-06-02 15:44:45 -0500314 ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500315
Ricardo Neribca2e412011-06-02 15:44:45 -0500316 return ret;
317}
318
319static int __devexit omap_hdmi_remove(struct platform_device *pdev)
320{
Ricardo Neria48d9b72012-05-18 01:42:37 -0500321 struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
322
Ricardo Neribca2e412011-06-02 15:44:45 -0500323 snd_soc_unregister_dai(&pdev->dev);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500324
325 if (hdmi_data == NULL) {
326 dev_err(&pdev->dev, "cannot obtain HDMi data\n");
327 return -ENODEV;
328 }
329
330 omap_dss_put_device(hdmi_data->dssdev);
Ricardo Neribca2e412011-06-02 15:44:45 -0500331 return 0;
332}
333
334static struct platform_driver hdmi_dai_driver = {
335 .driver = {
336 .name = DRV_NAME,
337 .owner = THIS_MODULE,
338 },
339 .probe = omap_hdmi_probe,
340 .remove = __devexit_p(omap_hdmi_remove),
341};
342
Axel Linbeda5bf52011-11-25 10:12:16 +0800343module_platform_driver(hdmi_dai_driver);
Ricardo Neribca2e412011-06-02 15:44:45 -0500344
345MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
346MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
347MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
348MODULE_LICENSE("GPL");
349MODULE_ALIAS("platform:" DRV_NAME);