blob: f59c69fb400ee60861bf334067aa1ee29df00d12 [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
Ricardo Neribca2e412011-06-02 15:44:45 -050037#include "omap-pcm.h"
38#include "omap-hdmi.h"
39
Ricardo Nerie412ec62012-05-18 01:42:34 -050040#define DRV_NAME "omap-hdmi-audio-dai"
Ricardo Neribca2e412011-06-02 15:44:45 -050041
Ricardo Neri26039152012-05-18 01:42:36 -050042struct hdmi_priv {
43 struct omap_pcm_dma_data dma_params;
Ricardo Neri7e6aa222012-05-18 01:42:38 -050044 struct omap_dss_audio dss_audio;
45 struct snd_aes_iec958 iec;
46 struct snd_cea_861_aud_if cea;
Ricardo Neria48d9b72012-05-18 01:42:37 -050047 struct omap_dss_device *dssdev;
Ricardo Neribca2e412011-06-02 15:44:45 -050048};
49
50static int omap_hdmi_dai_startup(struct snd_pcm_substream *substream,
51 struct snd_soc_dai *dai)
52{
Ricardo Neria0534e32012-05-18 01:42:39 -050053 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
Ricardo Neribca2e412011-06-02 15:44:45 -050054 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);
Ricardo Neria0534e32012-05-18 01:42:39 -050061 if (err < 0) {
62 dev_err(dai->dev, "could not apply constraint\n");
Ricardo Neribca2e412011-06-02 15:44:45 -050063 return err;
Ricardo Neria0534e32012-05-18 01:42:39 -050064 }
Ricardo Neribca2e412011-06-02 15:44:45 -050065
Ricardo Neria0534e32012-05-18 01:42:39 -050066 if (!priv->dssdev->driver->audio_supported(priv->dssdev)) {
67 dev_err(dai->dev, "audio not supported\n");
68 return -ENODEV;
69 }
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030070
71 snd_soc_dai_set_dma_data(dai, substream, &priv->dma_params);
72
Ricardo Neribca2e412011-06-02 15:44:45 -050073 return 0;
74}
75
Ricardo Neria48d9b72012-05-18 01:42:37 -050076static 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
Ricardo Neribca2e412011-06-02 15:44:45 -050084static 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{
Ricardo Neri26039152012-05-18 01:42:36 -050088 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
Ricardo Neri7e6aa222012-05-18 01:42:38 -050089 struct snd_aes_iec958 *iec = &priv->iec;
90 struct snd_cea_861_aud_if *cea = &priv->cea;
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030091 struct omap_pcm_dma_data *dma_data;
Ricardo Neribca2e412011-06-02 15:44:45 -050092 int err = 0;
93
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030094 dma_data = snd_soc_dai_get_dma_data(dai, substream);
95
Ricardo Neribca2e412011-06-02 15:44:45 -050096 switch (params_format(params)) {
97 case SNDRV_PCM_FORMAT_S16_LE:
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030098 dma_data->packet_size = 16;
Ricardo Neribca2e412011-06-02 15:44:45 -050099 break;
100 case SNDRV_PCM_FORMAT_S24_LE:
Peter Ujfalusibcd6da72012-09-14 15:05:57 +0300101 dma_data->packet_size = 32;
Ricardo Neribca2e412011-06-02 15:44:45 -0500102 break;
103 default:
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500104 dev_err(dai->dev, "format not supported!\n");
105 return -EINVAL;
Ricardo Neribca2e412011-06-02 15:44:45 -0500106 }
107
Peter Ujfalusibcd6da72012-09-14 15:05:57 +0300108 dma_data->data_type = 32;
Ricardo Neribca2e412011-06-02 15:44:45 -0500109
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500110 /*
111 * fill the IEC-60958 channel status word
112 */
113
114 /* specify IEC-60958-3 (commercial use) */
115 iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
116
117 /* specify that the audio is LPCM*/
118 iec->status[0] &= ~IEC958_AES0_NONAUDIO;
119
120 iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
121
122 iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
123
124 iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
125
126 iec->status[1] = IEC958_AES1_CON_GENERAL;
127
128 iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
129
130 iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
131
132 switch (params_rate(params)) {
133 case 32000:
134 iec->status[3] |= IEC958_AES3_CON_FS_32000;
135 break;
136 case 44100:
137 iec->status[3] |= IEC958_AES3_CON_FS_44100;
138 break;
139 case 48000:
140 iec->status[3] |= IEC958_AES3_CON_FS_48000;
141 break;
142 case 88200:
143 iec->status[3] |= IEC958_AES3_CON_FS_88200;
144 break;
145 case 96000:
146 iec->status[3] |= IEC958_AES3_CON_FS_96000;
147 break;
148 case 176400:
149 iec->status[3] |= IEC958_AES3_CON_FS_176400;
150 break;
151 case 192000:
152 iec->status[3] |= IEC958_AES3_CON_FS_192000;
153 break;
154 default:
155 dev_err(dai->dev, "rate not supported!\n");
156 return -EINVAL;
157 }
158
159 /* specify the clock accuracy */
160 iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
161
162 /*
163 * specify the word length. The same word length value can mean
164 * two different lengths. Hence, we need to specify the maximum
165 * word length as well.
166 */
167 switch (params_format(params)) {
168 case SNDRV_PCM_FORMAT_S16_LE:
169 iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
170 iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
171 break;
172 case SNDRV_PCM_FORMAT_S24_LE:
173 iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
174 iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
175 break;
176 default:
177 dev_err(dai->dev, "format not supported!\n");
178 return -EINVAL;
179 }
180
181 /*
182 * Fill the CEA-861 audio infoframe (see spec for details)
183 */
184
185 cea->db1_ct_cc = (params_channels(params) - 1)
186 & CEA861_AUDIO_INFOFRAME_DB1CC;
187 cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
188
189 cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
190 cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
191
192 cea->db3 = 0; /* not used, all zeros */
193
194 /*
195 * The OMAP HDMI IP requires to use the 8-channel channel code when
196 * transmitting more than two channels.
197 */
198 if (params_channels(params) == 2)
199 cea->db4_ca = 0x0;
200 else
201 cea->db4_ca = 0x13;
202
203 cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
204 /* the expression is trivial but makes clear what we are doing */
205 cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
206
207 priv->dss_audio.iec = iec;
208 priv->dss_audio.cea = cea;
209
210 err = priv->dssdev->driver->audio_config(priv->dssdev,
211 &priv->dss_audio);
212
Ricardo Neribca2e412011-06-02 15:44:45 -0500213 return err;
214}
215
Ricardo Neria48d9b72012-05-18 01:42:37 -0500216static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
217 struct snd_soc_dai *dai)
218{
219 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
220 int err = 0;
221
222 switch (cmd) {
223 case SNDRV_PCM_TRIGGER_START:
224 case SNDRV_PCM_TRIGGER_RESUME:
225 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
226 err = priv->dssdev->driver->audio_start(priv->dssdev);
227 break;
228 case SNDRV_PCM_TRIGGER_STOP:
229 case SNDRV_PCM_TRIGGER_SUSPEND:
230 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
231 priv->dssdev->driver->audio_stop(priv->dssdev);
232 break;
233 default:
234 err = -EINVAL;
235 }
236 return err;
237}
238
239static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
240 struct snd_soc_dai *dai)
241{
242 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
243
244 priv->dssdev->driver->audio_disable(priv->dssdev);
245}
246
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100247static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
Ricardo Neribca2e412011-06-02 15:44:45 -0500248 .startup = omap_hdmi_dai_startup,
249 .hw_params = omap_hdmi_dai_hw_params,
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500250 .prepare = omap_hdmi_dai_prepare,
251 .trigger = omap_hdmi_dai_trigger,
252 .shutdown = omap_hdmi_dai_shutdown,
Ricardo Neribca2e412011-06-02 15:44:45 -0500253};
254
255static struct snd_soc_dai_driver omap_hdmi_dai = {
256 .playback = {
257 .channels_min = 2,
Ricardo Neria1ab92d2012-05-18 01:42:40 -0500258 .channels_max = 8,
Ricardo Neribca2e412011-06-02 15:44:45 -0500259 .rates = OMAP_HDMI_RATES,
260 .formats = OMAP_HDMI_FORMATS,
261 },
262 .ops = &omap_hdmi_dai_ops,
263};
264
265static __devinit int omap_hdmi_probe(struct platform_device *pdev)
266{
267 int ret;
268 struct resource *hdmi_rsrc;
Ricardo Neri26039152012-05-18 01:42:36 -0500269 struct hdmi_priv *hdmi_data;
Ricardo Neria48d9b72012-05-18 01:42:37 -0500270 bool hdmi_dev_found = false;
Ricardo Neri26039152012-05-18 01:42:36 -0500271
272 hdmi_data = devm_kzalloc(&pdev->dev, sizeof(*hdmi_data), GFP_KERNEL);
273 if (hdmi_data == NULL) {
274 dev_err(&pdev->dev, "Cannot allocate memory for HDMI data\n");
275 return -ENOMEM;
276 }
Ricardo Neribca2e412011-06-02 15:44:45 -0500277
278 hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
279 if (!hdmi_rsrc) {
280 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_MEM 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.port_addr = hdmi_rsrc->start
Ricardo Neribca2e412011-06-02 15:44:45 -0500285 + OMAP_HDMI_AUDIO_DMA_PORT;
286
287 hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
288 if (!hdmi_rsrc) {
289 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
Ricardo Neri0ff5ee82012-05-18 01:42:35 -0500290 return -ENODEV;
Ricardo Neribca2e412011-06-02 15:44:45 -0500291 }
292
Ricardo Neri26039152012-05-18 01:42:36 -0500293 hdmi_data->dma_params.dma_req = hdmi_rsrc->start;
294 hdmi_data->dma_params.name = "HDMI playback";
Ricardo Neribca2e412011-06-02 15:44:45 -0500295
Ricardo Neria48d9b72012-05-18 01:42:37 -0500296 /*
297 * TODO: We assume that there is only one DSS HDMI device. Future
298 * OMAP implementations may support more than one HDMI devices and
299 * we should provided separate audio support for all of them.
300 */
301 /* Find an HDMI device. */
302 for_each_dss_dev(hdmi_data->dssdev) {
303 omap_dss_get_device(hdmi_data->dssdev);
304
305 if (!hdmi_data->dssdev->driver) {
306 omap_dss_put_device(hdmi_data->dssdev);
307 continue;
308 }
309
310 if (hdmi_data->dssdev->type == OMAP_DISPLAY_TYPE_HDMI) {
311 hdmi_dev_found = true;
312 break;
313 }
314 }
315
316 if (!hdmi_dev_found) {
317 dev_err(&pdev->dev, "no driver for HDMI display found\n");
318 return -ENODEV;
319 }
320
Ricardo Neri26039152012-05-18 01:42:36 -0500321 dev_set_drvdata(&pdev->dev, hdmi_data);
Ricardo Neribca2e412011-06-02 15:44:45 -0500322 ret = snd_soc_register_dai(&pdev->dev, &omap_hdmi_dai);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500323
Ricardo Neribca2e412011-06-02 15:44:45 -0500324 return ret;
325}
326
327static int __devexit omap_hdmi_remove(struct platform_device *pdev)
328{
Ricardo Neria48d9b72012-05-18 01:42:37 -0500329 struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
330
Ricardo Neribca2e412011-06-02 15:44:45 -0500331 snd_soc_unregister_dai(&pdev->dev);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500332
333 if (hdmi_data == NULL) {
334 dev_err(&pdev->dev, "cannot obtain HDMi data\n");
335 return -ENODEV;
336 }
337
338 omap_dss_put_device(hdmi_data->dssdev);
Ricardo Neribca2e412011-06-02 15:44:45 -0500339 return 0;
340}
341
342static struct platform_driver hdmi_dai_driver = {
343 .driver = {
344 .name = DRV_NAME,
345 .owner = THIS_MODULE,
346 },
347 .probe = omap_hdmi_probe,
348 .remove = __devexit_p(omap_hdmi_remove),
349};
350
Axel Linbeda5bf52011-11-25 10:12:16 +0800351module_platform_driver(hdmi_dai_driver);
Ricardo Neribca2e412011-06-02 15:44:45 -0500352
353MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
354MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
355MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
356MODULE_LICENSE("GPL");
357MODULE_ALIAS("platform:" DRV_NAME);