blob: ced3b88b44d4380880aa7a719f5d1eee4e31e70a [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>
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020035#include <sound/dmaengine_pcm.h>
Ricardo Neria48d9b72012-05-18 01:42:37 -050036#include <video/omapdss.h>
Ricardo Neribca2e412011-06-02 15:44:45 -050037
Ricardo Neribca2e412011-06-02 15:44:45 -050038#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 {
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020043 struct snd_dmaengine_dai_dma_data dma_data;
44 unsigned int dma_req;
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{
Ricardo Neria0534e32012-05-18 01:42:39 -050054 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
Ricardo Neribca2e412011-06-02 15:44:45 -050055 int err;
56 /*
57 * Make sure that the period bytes are multiple of the DMA packet size.
58 * Largest packet size we use is 32 32-bit words = 128 bytes
59 */
60 err = snd_pcm_hw_constraint_step(substream->runtime, 0,
61 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 128);
Ricardo Neria0534e32012-05-18 01:42:39 -050062 if (err < 0) {
63 dev_err(dai->dev, "could not apply constraint\n");
Ricardo Neribca2e412011-06-02 15:44:45 -050064 return err;
Ricardo Neria0534e32012-05-18 01:42:39 -050065 }
Ricardo Neribca2e412011-06-02 15:44:45 -050066
Ricardo Neria0534e32012-05-18 01:42:39 -050067 if (!priv->dssdev->driver->audio_supported(priv->dssdev)) {
68 dev_err(dai->dev, "audio not supported\n");
69 return -ENODEV;
70 }
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030071
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020072 snd_soc_dai_set_dma_data(dai, substream, &priv->dma_data);
Peter Ujfalusibcd6da72012-09-14 15:05:57 +030073
Ricardo Neribca2e412011-06-02 15:44:45 -050074 return 0;
75}
76
Ricardo Neria48d9b72012-05-18 01:42:37 -050077static int omap_hdmi_dai_prepare(struct snd_pcm_substream *substream,
78 struct snd_soc_dai *dai)
79{
80 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
81
82 return priv->dssdev->driver->audio_enable(priv->dssdev);
83}
84
Ricardo Neribca2e412011-06-02 15:44:45 -050085static int omap_hdmi_dai_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params,
87 struct snd_soc_dai *dai)
88{
Ricardo Neri26039152012-05-18 01:42:36 -050089 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
Ricardo Neri7e6aa222012-05-18 01:42:38 -050090 struct snd_aes_iec958 *iec = &priv->iec;
91 struct snd_cea_861_aud_if *cea = &priv->cea;
Ricardo Neribca2e412011-06-02 15:44:45 -050092 int err = 0;
93
94 switch (params_format(params)) {
95 case SNDRV_PCM_FORMAT_S16_LE:
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020096 priv->dma_data.maxburst = 16;
Ricardo Neribca2e412011-06-02 15:44:45 -050097 break;
98 case SNDRV_PCM_FORMAT_S24_LE:
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +020099 priv->dma_data.maxburst = 32;
Ricardo Neribca2e412011-06-02 15:44:45 -0500100 break;
101 default:
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500102 dev_err(dai->dev, "format not supported!\n");
103 return -EINVAL;
Ricardo Neribca2e412011-06-02 15:44:45 -0500104 }
105
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500106 /*
107 * fill the IEC-60958 channel status word
108 */
Ricardo Neria88fedf2013-01-10 18:32:13 -0600109 /* initialize the word bytes */
110 memset(iec->status, 0, sizeof(iec->status));
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500111
112 /* specify IEC-60958-3 (commercial use) */
113 iec->status[0] &= ~IEC958_AES0_PROFESSIONAL;
114
115 /* specify that the audio is LPCM*/
116 iec->status[0] &= ~IEC958_AES0_NONAUDIO;
117
118 iec->status[0] |= IEC958_AES0_CON_NOT_COPYRIGHT;
119
120 iec->status[0] |= IEC958_AES0_CON_EMPHASIS_NONE;
121
122 iec->status[0] |= IEC958_AES1_PRO_MODE_NOTID;
123
124 iec->status[1] = IEC958_AES1_CON_GENERAL;
125
126 iec->status[2] |= IEC958_AES2_CON_SOURCE_UNSPEC;
127
128 iec->status[2] |= IEC958_AES2_CON_CHANNEL_UNSPEC;
129
130 switch (params_rate(params)) {
131 case 32000:
132 iec->status[3] |= IEC958_AES3_CON_FS_32000;
133 break;
134 case 44100:
135 iec->status[3] |= IEC958_AES3_CON_FS_44100;
136 break;
137 case 48000:
138 iec->status[3] |= IEC958_AES3_CON_FS_48000;
139 break;
140 case 88200:
141 iec->status[3] |= IEC958_AES3_CON_FS_88200;
142 break;
143 case 96000:
144 iec->status[3] |= IEC958_AES3_CON_FS_96000;
145 break;
146 case 176400:
147 iec->status[3] |= IEC958_AES3_CON_FS_176400;
148 break;
149 case 192000:
150 iec->status[3] |= IEC958_AES3_CON_FS_192000;
151 break;
152 default:
153 dev_err(dai->dev, "rate not supported!\n");
154 return -EINVAL;
155 }
156
157 /* specify the clock accuracy */
158 iec->status[3] |= IEC958_AES3_CON_CLOCK_1000PPM;
159
160 /*
161 * specify the word length. The same word length value can mean
162 * two different lengths. Hence, we need to specify the maximum
163 * word length as well.
164 */
165 switch (params_format(params)) {
166 case SNDRV_PCM_FORMAT_S16_LE:
167 iec->status[4] |= IEC958_AES4_CON_WORDLEN_20_16;
168 iec->status[4] &= ~IEC958_AES4_CON_MAX_WORDLEN_24;
169 break;
170 case SNDRV_PCM_FORMAT_S24_LE:
171 iec->status[4] |= IEC958_AES4_CON_WORDLEN_24_20;
172 iec->status[4] |= IEC958_AES4_CON_MAX_WORDLEN_24;
173 break;
174 default:
175 dev_err(dai->dev, "format not supported!\n");
176 return -EINVAL;
177 }
178
179 /*
180 * Fill the CEA-861 audio infoframe (see spec for details)
181 */
182
183 cea->db1_ct_cc = (params_channels(params) - 1)
184 & CEA861_AUDIO_INFOFRAME_DB1CC;
185 cea->db1_ct_cc |= CEA861_AUDIO_INFOFRAME_DB1CT_FROM_STREAM;
186
187 cea->db2_sf_ss = CEA861_AUDIO_INFOFRAME_DB2SF_FROM_STREAM;
188 cea->db2_sf_ss |= CEA861_AUDIO_INFOFRAME_DB2SS_FROM_STREAM;
189
190 cea->db3 = 0; /* not used, all zeros */
191
192 /*
193 * The OMAP HDMI IP requires to use the 8-channel channel code when
194 * transmitting more than two channels.
195 */
196 if (params_channels(params) == 2)
197 cea->db4_ca = 0x0;
198 else
199 cea->db4_ca = 0x13;
200
201 cea->db5_dminh_lsv = CEA861_AUDIO_INFOFRAME_DB5_DM_INH_PROHIBITED;
202 /* the expression is trivial but makes clear what we are doing */
203 cea->db5_dminh_lsv |= (0 & CEA861_AUDIO_INFOFRAME_DB5_LSV);
204
205 priv->dss_audio.iec = iec;
206 priv->dss_audio.cea = cea;
207
208 err = priv->dssdev->driver->audio_config(priv->dssdev,
209 &priv->dss_audio);
210
Ricardo Neribca2e412011-06-02 15:44:45 -0500211 return err;
212}
213
Ricardo Neria48d9b72012-05-18 01:42:37 -0500214static int omap_hdmi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
215 struct snd_soc_dai *dai)
216{
217 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
218 int err = 0;
219
220 switch (cmd) {
221 case SNDRV_PCM_TRIGGER_START:
222 case SNDRV_PCM_TRIGGER_RESUME:
223 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
224 err = priv->dssdev->driver->audio_start(priv->dssdev);
225 break;
226 case SNDRV_PCM_TRIGGER_STOP:
227 case SNDRV_PCM_TRIGGER_SUSPEND:
228 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
229 priv->dssdev->driver->audio_stop(priv->dssdev);
230 break;
231 default:
232 err = -EINVAL;
233 }
234 return err;
235}
236
237static void omap_hdmi_dai_shutdown(struct snd_pcm_substream *substream,
238 struct snd_soc_dai *dai)
239{
240 struct hdmi_priv *priv = snd_soc_dai_get_drvdata(dai);
241
242 priv->dssdev->driver->audio_disable(priv->dssdev);
243}
244
Lars-Peter Clausen85e76522011-11-23 11:40:40 +0100245static const struct snd_soc_dai_ops omap_hdmi_dai_ops = {
Ricardo Neribca2e412011-06-02 15:44:45 -0500246 .startup = omap_hdmi_dai_startup,
247 .hw_params = omap_hdmi_dai_hw_params,
Ricardo Neri7e6aa222012-05-18 01:42:38 -0500248 .prepare = omap_hdmi_dai_prepare,
249 .trigger = omap_hdmi_dai_trigger,
250 .shutdown = omap_hdmi_dai_shutdown,
Ricardo Neribca2e412011-06-02 15:44:45 -0500251};
252
253static struct snd_soc_dai_driver omap_hdmi_dai = {
254 .playback = {
255 .channels_min = 2,
Ricardo Neria1ab92d2012-05-18 01:42:40 -0500256 .channels_max = 8,
Ricardo Neribca2e412011-06-02 15:44:45 -0500257 .rates = OMAP_HDMI_RATES,
258 .formats = OMAP_HDMI_FORMATS,
259 },
260 .ops = &omap_hdmi_dai_ops,
261};
262
Kuninori Morimoto0ba7f842013-03-21 03:34:01 -0700263static const struct snd_soc_component_driver omap_hdmi_component = {
264 .name = DRV_NAME,
265};
266
Bill Pemberton7ff60002012-12-07 09:26:29 -0500267static int omap_hdmi_probe(struct platform_device *pdev)
Ricardo Neribca2e412011-06-02 15:44:45 -0500268{
269 int ret;
270 struct resource *hdmi_rsrc;
Ricardo Neri26039152012-05-18 01:42:36 -0500271 struct hdmi_priv *hdmi_data;
Ricardo Neria48d9b72012-05-18 01:42:37 -0500272 bool hdmi_dev_found = false;
Ricardo Neri26039152012-05-18 01:42:36 -0500273
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 }
Ricardo Neribca2e412011-06-02 15:44:45 -0500279
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");
Ricardo Neri0ff5ee82012-05-18 01:42:35 -0500283 return -ENODEV;
Ricardo Neribca2e412011-06-02 15:44:45 -0500284 }
285
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200286 hdmi_data->dma_data.addr = hdmi_rsrc->start + OMAP_HDMI_AUDIO_DMA_PORT;
Ricardo Neribca2e412011-06-02 15:44:45 -0500287
288 hdmi_rsrc = platform_get_resource(pdev, IORESOURCE_DMA, 0);
289 if (!hdmi_rsrc) {
290 dev_err(&pdev->dev, "Cannot obtain IORESOURCE_DMA HDMI\n");
Ricardo Neri0ff5ee82012-05-18 01:42:35 -0500291 return -ENODEV;
Ricardo Neribca2e412011-06-02 15:44:45 -0500292 }
293
Lars-Peter Clausen09ae3aa2013-04-03 11:06:05 +0200294 hdmi_data->dma_req = hdmi_rsrc->start;
295 hdmi_data->dma_data.filter_data = &hdmi_data->dma_req;
296 hdmi_data->dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
Ricardo Neribca2e412011-06-02 15:44:45 -0500297
Ricardo Neria48d9b72012-05-18 01:42:37 -0500298 /*
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
Ricardo Neri26039152012-05-18 01:42:36 -0500323 dev_set_drvdata(&pdev->dev, hdmi_data);
Kuninori Morimoto0ba7f842013-03-21 03:34:01 -0700324 ret = snd_soc_register_component(&pdev->dev, &omap_hdmi_component,
325 &omap_hdmi_dai, 1);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500326
Ricardo Neribca2e412011-06-02 15:44:45 -0500327 return ret;
328}
329
Bill Pemberton7ff60002012-12-07 09:26:29 -0500330static int omap_hdmi_remove(struct platform_device *pdev)
Ricardo Neribca2e412011-06-02 15:44:45 -0500331{
Ricardo Neria48d9b72012-05-18 01:42:37 -0500332 struct hdmi_priv *hdmi_data = dev_get_drvdata(&pdev->dev);
333
Kuninori Morimoto0ba7f842013-03-21 03:34:01 -0700334 snd_soc_unregister_component(&pdev->dev);
Ricardo Neria48d9b72012-05-18 01:42:37 -0500335
336 if (hdmi_data == NULL) {
337 dev_err(&pdev->dev, "cannot obtain HDMi data\n");
338 return -ENODEV;
339 }
340
341 omap_dss_put_device(hdmi_data->dssdev);
Ricardo Neribca2e412011-06-02 15:44:45 -0500342 return 0;
343}
344
345static struct platform_driver hdmi_dai_driver = {
346 .driver = {
347 .name = DRV_NAME,
348 .owner = THIS_MODULE,
349 },
350 .probe = omap_hdmi_probe,
Bill Pemberton7ff60002012-12-07 09:26:29 -0500351 .remove = omap_hdmi_remove,
Ricardo Neribca2e412011-06-02 15:44:45 -0500352};
353
Axel Linbeda5bf52011-11-25 10:12:16 +0800354module_platform_driver(hdmi_dai_driver);
Ricardo Neribca2e412011-06-02 15:44:45 -0500355
356MODULE_AUTHOR("Jorge Candelaria <jorge.candelaria@ti.com>");
357MODULE_AUTHOR("Ricardo Neri <ricardo.neri@ti.com>");
358MODULE_DESCRIPTION("OMAP HDMI SoC Interface");
359MODULE_LICENSE("GPL");
360MODULE_ALIAS("platform:" DRV_NAME);