blob: bf379310008adebd704b5a1ca0671c382f7ad071 [file] [log] [blame]
Russell King7ed6c662013-11-07 16:01:45 +00001/*
2 * DesignWare HDMI audio driver
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Written and tested against the Designware HDMI Tx found in iMX6.
9 */
10#include <linux/io.h>
11#include <linux/interrupt.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
14#include <drm/bridge/dw_hdmi.h>
15
16#include <sound/asoundef.h>
17#include <sound/core.h>
18#include <sound/initval.h>
19#include <sound/pcm.h>
20#include <sound/pcm_iec958.h>
21
22#include "dw_hdmi-audio.h"
23
24#define DRIVER_NAME "dw-hdmi-ahb-audio"
25
26/* Provide some bits rather than bit offsets */
27enum {
28 HDMI_AHB_DMA_CONF0_SW_FIFO_RST = BIT(7),
29 HDMI_AHB_DMA_CONF0_EN_HLOCK = BIT(3),
30 HDMI_AHB_DMA_START_START = BIT(0),
31 HDMI_AHB_DMA_STOP_STOP = BIT(0),
32 HDMI_IH_MUTE_AHBDMAAUD_STAT0_ERROR = BIT(5),
33 HDMI_IH_MUTE_AHBDMAAUD_STAT0_LOST = BIT(4),
34 HDMI_IH_MUTE_AHBDMAAUD_STAT0_RETRY = BIT(3),
35 HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE = BIT(2),
36 HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFFULL = BIT(1),
37 HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFEMPTY = BIT(0),
38 HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL =
39 HDMI_IH_MUTE_AHBDMAAUD_STAT0_ERROR |
40 HDMI_IH_MUTE_AHBDMAAUD_STAT0_LOST |
41 HDMI_IH_MUTE_AHBDMAAUD_STAT0_RETRY |
42 HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE |
43 HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFFULL |
44 HDMI_IH_MUTE_AHBDMAAUD_STAT0_BUFFEMPTY,
45 HDMI_IH_AHBDMAAUD_STAT0_ERROR = BIT(5),
46 HDMI_IH_AHBDMAAUD_STAT0_LOST = BIT(4),
47 HDMI_IH_AHBDMAAUD_STAT0_RETRY = BIT(3),
48 HDMI_IH_AHBDMAAUD_STAT0_DONE = BIT(2),
49 HDMI_IH_AHBDMAAUD_STAT0_BUFFFULL = BIT(1),
50 HDMI_IH_AHBDMAAUD_STAT0_BUFFEMPTY = BIT(0),
51 HDMI_IH_AHBDMAAUD_STAT0_ALL =
52 HDMI_IH_AHBDMAAUD_STAT0_ERROR |
53 HDMI_IH_AHBDMAAUD_STAT0_LOST |
54 HDMI_IH_AHBDMAAUD_STAT0_RETRY |
55 HDMI_IH_AHBDMAAUD_STAT0_DONE |
56 HDMI_IH_AHBDMAAUD_STAT0_BUFFFULL |
57 HDMI_IH_AHBDMAAUD_STAT0_BUFFEMPTY,
58 HDMI_AHB_DMA_CONF0_INCR16 = 2 << 1,
59 HDMI_AHB_DMA_CONF0_INCR8 = 1 << 1,
60 HDMI_AHB_DMA_CONF0_INCR4 = 0,
61 HDMI_AHB_DMA_CONF0_BURST_MODE = BIT(0),
62 HDMI_AHB_DMA_MASK_DONE = BIT(7),
63 HDMI_REVISION_ID = 0x0001,
64 HDMI_IH_AHBDMAAUD_STAT0 = 0x0109,
65 HDMI_IH_MUTE_AHBDMAAUD_STAT0 = 0x0189,
66 HDMI_AHB_DMA_CONF0 = 0x3600,
67 HDMI_AHB_DMA_START = 0x3601,
68 HDMI_AHB_DMA_STOP = 0x3602,
69 HDMI_AHB_DMA_THRSLD = 0x3603,
70 HDMI_AHB_DMA_STRADDR0 = 0x3604,
71 HDMI_AHB_DMA_STPADDR0 = 0x3608,
72 HDMI_AHB_DMA_MASK = 0x3614,
73 HDMI_AHB_DMA_POL = 0x3615,
74 HDMI_AHB_DMA_CONF1 = 0x3616,
75 HDMI_AHB_DMA_BUFFPOL = 0x361a,
76};
77
78struct snd_dw_hdmi {
79 struct snd_card *card;
80 struct snd_pcm *pcm;
81 spinlock_t lock;
82 struct dw_hdmi_audio_data data;
83 struct snd_pcm_substream *substream;
84 void (*reformat)(struct snd_dw_hdmi *, size_t, size_t);
85 void *buf_src;
86 void *buf_dst;
87 dma_addr_t buf_addr;
88 unsigned buf_offset;
89 unsigned buf_period;
90 unsigned buf_size;
91 unsigned channels;
92 u8 revision;
93 u8 iec_offset;
94 u8 cs[192][8];
95};
96
97static void dw_hdmi_writel(u32 val, void __iomem *ptr)
98{
99 writeb_relaxed(val, ptr);
100 writeb_relaxed(val >> 8, ptr + 1);
101 writeb_relaxed(val >> 16, ptr + 2);
102 writeb_relaxed(val >> 24, ptr + 3);
103}
104
105/*
106 * Convert to hardware format: The userspace buffer contains IEC958 samples,
107 * with the PCUV bits in bits 31..28 and audio samples in bits 27..4. We
108 * need these to be in bits 27..24, with the IEC B bit in bit 28, and audio
109 * samples in 23..0.
110 *
111 * Default preamble in bits 3..0: 8 = block start, 4 = even 2 = odd
112 *
113 * Ideally, we could do with having the data properly formatted in userspace.
114 */
115static void dw_hdmi_reformat_iec958(struct snd_dw_hdmi *dw,
116 size_t offset, size_t bytes)
117{
118 u32 *src = dw->buf_src + offset;
119 u32 *dst = dw->buf_dst + offset;
120 u32 *end = dw->buf_src + offset + bytes;
121
122 do {
123 u32 b, sample = *src++;
124
125 b = (sample & 8) << (28 - 3);
126
127 sample >>= 4;
128
129 *dst++ = sample | b;
130 } while (src < end);
131}
132
133static u32 parity(u32 sample)
134{
135 sample ^= sample >> 16;
136 sample ^= sample >> 8;
137 sample ^= sample >> 4;
138 sample ^= sample >> 2;
139 sample ^= sample >> 1;
140 return (sample & 1) << 27;
141}
142
143static void dw_hdmi_reformat_s24(struct snd_dw_hdmi *dw,
144 size_t offset, size_t bytes)
145{
146 u32 *src = dw->buf_src + offset;
147 u32 *dst = dw->buf_dst + offset;
148 u32 *end = dw->buf_src + offset + bytes;
149
150 do {
151 unsigned i;
152 u8 *cs;
153
154 cs = dw->cs[dw->iec_offset++];
155 if (dw->iec_offset >= 192)
156 dw->iec_offset = 0;
157
158 i = dw->channels;
159 do {
160 u32 sample = *src++;
161
162 sample &= ~0xff000000;
163 sample |= *cs++ << 24;
164 sample |= parity(sample & ~0xf8000000);
165
166 *dst++ = sample;
167 } while (--i);
168 } while (src < end);
169}
170
171static void dw_hdmi_create_cs(struct snd_dw_hdmi *dw,
172 struct snd_pcm_runtime *runtime)
173{
174 u8 cs[4];
175 unsigned ch, i, j;
176
177 snd_pcm_create_iec958_consumer(runtime, cs, sizeof(cs));
178
179 memset(dw->cs, 0, sizeof(dw->cs));
180
181 for (ch = 0; ch < 8; ch++) {
182 cs[2] &= ~IEC958_AES2_CON_CHANNEL;
183 cs[2] |= (ch + 1) << 4;
184
185 for (i = 0; i < ARRAY_SIZE(cs); i++) {
186 unsigned c = cs[i];
187
188 for (j = 0; j < 8; j++, c >>= 1)
189 dw->cs[i * 8 + j][ch] = (c & 1) << 2;
190 }
191 }
192 dw->cs[0][0] |= BIT(4);
193}
194
195static void dw_hdmi_start_dma(struct snd_dw_hdmi *dw)
196{
197 void __iomem *base = dw->data.base;
198 unsigned offset = dw->buf_offset;
199 unsigned period = dw->buf_period;
200 u32 start, stop;
201
202 dw->reformat(dw, offset, period);
203
204 /* Clear all irqs before enabling irqs and starting DMA */
205 writeb_relaxed(HDMI_IH_AHBDMAAUD_STAT0_ALL,
206 base + HDMI_IH_AHBDMAAUD_STAT0);
207
208 start = dw->buf_addr + offset;
209 stop = start + period - 1;
210
211 /* Setup the hardware start/stop addresses */
212 dw_hdmi_writel(start, base + HDMI_AHB_DMA_STRADDR0);
213 dw_hdmi_writel(stop, base + HDMI_AHB_DMA_STPADDR0);
214
215 writeb_relaxed((u8)~HDMI_AHB_DMA_MASK_DONE, base + HDMI_AHB_DMA_MASK);
216 writeb(HDMI_AHB_DMA_START_START, base + HDMI_AHB_DMA_START);
217
218 offset += period;
219 if (offset >= dw->buf_size)
220 offset = 0;
221 dw->buf_offset = offset;
222}
223
224static void dw_hdmi_stop_dma(struct snd_dw_hdmi *dw)
225{
226 /* Disable interrupts before disabling DMA */
227 writeb_relaxed(~0, dw->data.base + HDMI_AHB_DMA_MASK);
228 writeb_relaxed(HDMI_AHB_DMA_STOP_STOP, dw->data.base + HDMI_AHB_DMA_STOP);
229}
230
231static irqreturn_t snd_dw_hdmi_irq(int irq, void *data)
232{
233 struct snd_dw_hdmi *dw = data;
234 struct snd_pcm_substream *substream;
235 unsigned stat;
236
237 stat = readb_relaxed(dw->data.base + HDMI_IH_AHBDMAAUD_STAT0);
238 if (!stat)
239 return IRQ_NONE;
240
241 writeb_relaxed(stat, dw->data.base + HDMI_IH_AHBDMAAUD_STAT0);
242
243 substream = dw->substream;
244 if (stat & HDMI_IH_AHBDMAAUD_STAT0_DONE && substream) {
245 snd_pcm_period_elapsed(substream);
246
247 spin_lock(&dw->lock);
248 if (dw->substream)
249 dw_hdmi_start_dma(dw);
250 spin_unlock(&dw->lock);
251 }
252
253 return IRQ_HANDLED;
254}
255
256static struct snd_pcm_hardware dw_hdmi_hw = {
257 .info = SNDRV_PCM_INFO_INTERLEAVED |
258 SNDRV_PCM_INFO_BLOCK_TRANSFER |
259 SNDRV_PCM_INFO_MMAP |
260 SNDRV_PCM_INFO_MMAP_VALID,
261 .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE |
262 SNDRV_PCM_FMTBIT_S24_LE,
263 .rates = SNDRV_PCM_RATE_32000 |
264 SNDRV_PCM_RATE_44100 |
265 SNDRV_PCM_RATE_48000 |
266 SNDRV_PCM_RATE_88200 |
267 SNDRV_PCM_RATE_96000 |
268 SNDRV_PCM_RATE_176400 |
269 SNDRV_PCM_RATE_192000,
270 .channels_min = 2,
271 .channels_max = 8,
272 .buffer_bytes_max = 64 * 1024,
273 .period_bytes_min = 256,
274 .period_bytes_max = 8192, /* ERR004323: must limit to 8k */
275 .periods_min = 2,
276 .periods_max = 16,
277 .fifo_size = 0,
278};
279
280static int dw_hdmi_open(struct snd_pcm_substream *substream)
281{
282 struct snd_pcm_runtime *runtime = substream->runtime;
283 struct snd_dw_hdmi *dw = substream->private_data;
284 void __iomem *base = dw->data.base;
285 int ret;
286
287 runtime->hw = dw_hdmi_hw;
288
289 ret = snd_pcm_limit_hw_rates(runtime);
290 if (ret < 0)
291 return ret;
292
293 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
294 if (ret < 0)
295 return ret;
296
297 /* Clear FIFO */
298 writeb_relaxed(HDMI_AHB_DMA_CONF0_SW_FIFO_RST,
299 base + HDMI_AHB_DMA_CONF0);
300
301 /* Configure interrupt polarities */
302 writeb_relaxed(~0, base + HDMI_AHB_DMA_POL);
303 writeb_relaxed(~0, base + HDMI_AHB_DMA_BUFFPOL);
304
305 /* Keep interrupts masked, and clear any pending */
306 writeb_relaxed(~0, base + HDMI_AHB_DMA_MASK);
307 writeb_relaxed(~0, base + HDMI_IH_AHBDMAAUD_STAT0);
308
309 ret = request_irq(dw->data.irq, snd_dw_hdmi_irq, IRQF_SHARED,
310 "dw-hdmi-audio", dw);
311 if (ret)
312 return ret;
313
314 /* Un-mute done interrupt */
315 writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL &
316 ~HDMI_IH_MUTE_AHBDMAAUD_STAT0_DONE,
317 base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
318
319 return 0;
320}
321
322static int dw_hdmi_close(struct snd_pcm_substream *substream)
323{
324 struct snd_dw_hdmi *dw = substream->private_data;
325
326 /* Mute all interrupts */
327 writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL,
328 dw->data.base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
329
330 free_irq(dw->data.irq, dw);
331
332 return 0;
333}
334
335static int dw_hdmi_hw_free(struct snd_pcm_substream *substream)
336{
337 return snd_pcm_lib_free_vmalloc_buffer(substream);
338}
339
340static int dw_hdmi_hw_params(struct snd_pcm_substream *substream,
341 struct snd_pcm_hw_params *params)
342{
343 return snd_pcm_lib_alloc_vmalloc_buffer(substream,
344 params_buffer_bytes(params));
345}
346
347static int dw_hdmi_prepare(struct snd_pcm_substream *substream)
348{
349 struct snd_pcm_runtime *runtime = substream->runtime;
350 struct snd_dw_hdmi *dw = substream->private_data;
351 u8 threshold, conf0, conf1;
352
353 /* Setup as per 3.0.5 FSL 4.1.0 BSP */
354 switch (dw->revision) {
355 case 0x0a:
356 conf0 = HDMI_AHB_DMA_CONF0_BURST_MODE |
357 HDMI_AHB_DMA_CONF0_INCR4;
358 if (runtime->channels == 2)
359 threshold = 126;
360 else
361 threshold = 124;
362 break;
363 case 0x1a:
364 conf0 = HDMI_AHB_DMA_CONF0_BURST_MODE |
365 HDMI_AHB_DMA_CONF0_INCR8;
366 threshold = 128;
367 break;
368 default:
369 /* NOTREACHED */
370 return -EINVAL;
371 }
372
373 dw_hdmi_set_sample_rate(dw->data.hdmi, runtime->rate);
374
375 /* Minimum number of bytes in the fifo. */
376 runtime->hw.fifo_size = threshold * 32;
377
378 conf0 |= HDMI_AHB_DMA_CONF0_EN_HLOCK;
379 conf1 = (1 << runtime->channels) - 1;
380
381 writeb_relaxed(threshold, dw->data.base + HDMI_AHB_DMA_THRSLD);
382 writeb_relaxed(conf0, dw->data.base + HDMI_AHB_DMA_CONF0);
383 writeb_relaxed(conf1, dw->data.base + HDMI_AHB_DMA_CONF1);
384
385 switch (runtime->format) {
386 case SNDRV_PCM_FORMAT_IEC958_SUBFRAME_LE:
387 dw->reformat = dw_hdmi_reformat_iec958;
388 break;
389 case SNDRV_PCM_FORMAT_S24_LE:
390 dw_hdmi_create_cs(dw, runtime);
391 dw->reformat = dw_hdmi_reformat_s24;
392 break;
393 }
394 dw->iec_offset = 0;
395 dw->channels = runtime->channels;
396 dw->buf_src = runtime->dma_area;
397 dw->buf_dst = substream->dma_buffer.area;
398 dw->buf_addr = substream->dma_buffer.addr;
399 dw->buf_period = snd_pcm_lib_period_bytes(substream);
400 dw->buf_size = snd_pcm_lib_buffer_bytes(substream);
401
402 return 0;
403}
404
405static int dw_hdmi_trigger(struct snd_pcm_substream *substream, int cmd)
406{
407 struct snd_dw_hdmi *dw = substream->private_data;
408 unsigned long flags;
409 int ret = 0;
410
411 switch (cmd) {
412 case SNDRV_PCM_TRIGGER_START:
413 spin_lock_irqsave(&dw->lock, flags);
414 dw->buf_offset = 0;
415 dw->substream = substream;
416 dw_hdmi_start_dma(dw);
417 dw_hdmi_audio_enable(dw->data.hdmi);
418 spin_unlock_irqrestore(&dw->lock, flags);
419 substream->runtime->delay = substream->runtime->period_size;
420 break;
421
422 case SNDRV_PCM_TRIGGER_STOP:
423 spin_lock_irqsave(&dw->lock, flags);
424 dw->substream = NULL;
425 dw_hdmi_stop_dma(dw);
426 dw_hdmi_audio_disable(dw->data.hdmi);
427 spin_unlock_irqrestore(&dw->lock, flags);
428 break;
429
430 default:
431 ret = -EINVAL;
432 break;
433 }
434
435 return ret;
436}
437
438static snd_pcm_uframes_t dw_hdmi_pointer(struct snd_pcm_substream *substream)
439{
440 struct snd_pcm_runtime *runtime = substream->runtime;
441 struct snd_dw_hdmi *dw = substream->private_data;
442
443 /*
444 * We are unable to report the exact hardware position as
445 * reading the 32-bit DMA position using 8-bit reads is racy.
446 */
447 return bytes_to_frames(runtime, dw->buf_offset);
448}
449
450static struct snd_pcm_ops snd_dw_hdmi_ops = {
451 .open = dw_hdmi_open,
452 .close = dw_hdmi_close,
453 .ioctl = snd_pcm_lib_ioctl,
454 .hw_params = dw_hdmi_hw_params,
455 .hw_free = dw_hdmi_hw_free,
456 .prepare = dw_hdmi_prepare,
457 .trigger = dw_hdmi_trigger,
458 .pointer = dw_hdmi_pointer,
459 .page = snd_pcm_lib_get_vmalloc_page,
460};
461
462static int snd_dw_hdmi_probe(struct platform_device *pdev)
463{
464 const struct dw_hdmi_audio_data *data = pdev->dev.platform_data;
465 struct device *dev = pdev->dev.parent;
466 struct snd_dw_hdmi *dw;
467 struct snd_card *card;
468 struct snd_pcm *pcm;
469 unsigned revision;
470 int ret;
471
472 writeb_relaxed(HDMI_IH_MUTE_AHBDMAAUD_STAT0_ALL,
473 data->base + HDMI_IH_MUTE_AHBDMAAUD_STAT0);
474 revision = readb_relaxed(data->base + HDMI_REVISION_ID);
475 if (revision != 0x0a && revision != 0x1a) {
476 dev_err(dev, "dw-hdmi-audio: unknown revision 0x%02x\n",
477 revision);
478 return -ENXIO;
479 }
480
481 ret = snd_card_new(dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
482 THIS_MODULE, sizeof(struct snd_dw_hdmi), &card);
483 if (ret < 0)
484 return ret;
485
486 strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
487 strlcpy(card->shortname, "DW-HDMI", sizeof(card->shortname));
488 snprintf(card->longname, sizeof(card->longname),
489 "%s rev 0x%02x, irq %d", card->shortname, revision,
490 data->irq);
491
492 dw = card->private_data;
493 dw->card = card;
494 dw->data = *data;
495 dw->revision = revision;
496
497 spin_lock_init(&dw->lock);
498
499 ret = snd_pcm_new(card, "DW HDMI", 0, 1, 0, &pcm);
500 if (ret < 0)
501 goto err;
502
503 dw->pcm = pcm;
504 pcm->private_data = dw;
505 strlcpy(pcm->name, DRIVER_NAME, sizeof(pcm->name));
506 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_dw_hdmi_ops);
507
508 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
509 dev, 64 * 1024, 64 * 1024);
510
511 ret = snd_card_register(card);
512 if (ret < 0)
513 goto err;
514
515 platform_set_drvdata(pdev, dw);
516
517 return 0;
518
519err:
520 snd_card_free(card);
521 return ret;
522}
523
524static int snd_dw_hdmi_remove(struct platform_device *pdev)
525{
526 struct snd_dw_hdmi *dw = platform_get_drvdata(pdev);
527
528 snd_card_free(dw->card);
529
530 return 0;
531}
532
533#if defined(CONFIG_PM_SLEEP) && defined(IS_NOT_BROKEN)
534/*
535 * This code is fine, but requires implementation in the dw_hdmi_trigger()
536 * method which is currently missing as I have no way to test this.
537 */
538static int snd_dw_hdmi_suspend(struct device *dev)
539{
540 struct snd_dw_hdmi *dw = dev_get_drvdata(dev);
541
542 snd_power_change_state(dw->card, SNDRV_CTL_POWER_D3cold);
543 snd_pcm_suspend_all(dw->pcm);
544
545 return 0;
546}
547
548static int snd_dw_hdmi_resume(struct device *dev)
549{
550 struct snd_dw_hdmi *dw = dev_get_drvdata(dev);
551
552 snd_power_change_state(dw->card, SNDRV_CTL_POWER_D0);
553
554 return 0;
555}
556
557static SIMPLE_DEV_PM_OPS(snd_dw_hdmi_pm, snd_dw_hdmi_suspend,
558 snd_dw_hdmi_resume);
559#define PM_OPS &snd_dw_hdmi_pm
560#else
561#define PM_OPS NULL
562#endif
563
564static struct platform_driver snd_dw_hdmi_driver = {
565 .probe = snd_dw_hdmi_probe,
566 .remove = snd_dw_hdmi_remove,
567 .driver = {
568 .name = DRIVER_NAME,
569 .owner = THIS_MODULE,
570 .pm = PM_OPS,
571 },
572};
573
574module_platform_driver(snd_dw_hdmi_driver);
575
576MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
577MODULE_DESCRIPTION("Synopsis Designware HDMI AHB ALSA interface");
578MODULE_LICENSE("GPL v2");
579MODULE_ALIAS("platform:" DRIVER_NAME);