2 * imx-hdmi-dma.c -- HDMI DMA driver for ALSA Soc Audio Layer
4 * Copyright (C) 2011-2014 Freescale Semiconductor, Inc.
6 * based on imx-pcm-dma-mx2.c
7 * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
9 * This code is based on code copyrighted by Freescale,
10 * Liam Girdwood, Javier Martin and probably others.
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/delay.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/mfd/mxc-hdmi-core.h>
22 #include <linux/platform_data/dma-imx.h>
24 #include <video/mxc_hdmi.h>
28 #define HDMI_DMA_BURST_UNSPECIFIED_LEGNTH 0
29 #define HDMI_DMA_BURST_INCR4 1
30 #define HDMI_DMA_BURST_INCR8 2
31 #define HDMI_DMA_BURST_INCR16 3
33 #define HDMI_BASE_ADDR 0x00120000
35 struct hdmi_sdma_script {
42 struct hdmi_dma_priv {
43 struct snd_pcm_substream *substream;
44 struct platform_device *pdev;
46 struct snd_dma_buffer hw_buffer;
47 unsigned long buffer_bytes;
48 unsigned long appl_bytes;
58 snd_pcm_format_t format;
70 dma_addr_t phy_hdmi_sdma_t;
71 struct hdmi_sdma_script *hdmi_sdma_t;
72 struct dma_chan *dma_channel;
73 struct imx_dma_data dma_data;
74 struct dma_async_tx_descriptor *desc;
75 struct imx_hdmi_sdma_params sdma_params;
78 /* bit 0:0:0:b:p(0):c:(u)0:(v)0 */
79 /* max 8 channels supported; channels are interleaved */
80 static u8 g_packet_head_table[48 * 8];
82 void hdmi_dma_copy_16_neon_lut(unsigned short *src, unsigned int *dst,
83 int samples, unsigned char *lookup_table);
84 void hdmi_dma_copy_16_neon_fast(unsigned short *src, unsigned int *dst,
86 void hdmi_dma_copy_24_neon_lut(unsigned int *src, unsigned int *dst,
87 int samples, unsigned char *lookup_table);
88 void hdmi_dma_copy_24_neon_fast(unsigned int *src, unsigned int *dst,
90 static void hdmi_dma_irq_enable(struct hdmi_dma_priv *priv);
91 static void hdmi_dma_irq_disable(struct hdmi_dma_priv *priv);
93 union hdmi_audio_header_t iec_header;
94 EXPORT_SYMBOL(iec_header);
97 * Note that the period size for DMA != period size for ALSA because the
98 * driver adds iec frame info to the audio samples (in hdmi_dma_copy).
100 * Each 4 byte subframe = 1 byte of iec data + 3 byte audio sample.
102 * A 16 bit audio sample becomes 32 bits including the frame info. Ratio=2
103 * A 24 bit audio sample becomes 32 bits including the frame info. Ratio=3:4
104 * If the 24 bit raw audio is in 32 bit words, the
106 * Original Packed into subframe Ratio of size Format
107 * sample how many size of DMA buffer
108 * (bits) bits to ALSA buffer
109 * -------- ----------- -------- -------------- ------------------------
110 * 16 16 32 2 SNDRV_PCM_FORMAT_S16_LE
111 * 24 24 32 1.33 SNDRV_PCM_FORMAT_S24_3LE*
112 * 24 32 32 1 SNDRV_PCM_FORMAT_S24_LE
114 * *so SNDRV_PCM_FORMAT_S24_3LE is not supported.
118 * The minimum dma period is one IEC audio frame (192 * 4 * channels).
119 * The maximum dma period for the HDMI DMA is 8K.
121 * channels minimum maximum
122 * dma period dma period
123 * -------- ------------------ ----------
124 * 2 192 * 4 * 2 = 1536 * 4 = 6144
125 * 4 192 * 4 * 4 = 3072 * 2 = 6144
126 * 6 192 * 4 * 6 = 4608 * 1 = 4608
127 * 8 192 * 4 * 8 = 6144 * 1 = 6144
130 * 1. Must keep the ratio of DMA buffer to ALSA buffer consistent.
131 * 2. frame_idx is saved in the private data, so even if a frame cannot be
132 * transmitted in a period, it can be continued in the next period. This
133 * is necessary for 6 ch.
135 #define HDMI_DMA_PERIOD_BYTES (12288)
136 #define HDMI_DMA_BUF_SIZE (128 * 1024)
137 #define HDMI_PCM_BUF_SIZE (128 * 1024)
139 #define hdmi_audio_debug(dev, reg) \
140 dev_dbg(dev, #reg ": 0x%02x\n", hdmi_readb(reg))
143 static void dumpregs(struct device *dev)
145 hdmi_audio_debug(dev, HDMI_AHB_DMA_CONF0);
146 hdmi_audio_debug(dev, HDMI_AHB_DMA_START);
147 hdmi_audio_debug(dev, HDMI_AHB_DMA_STOP);
148 hdmi_audio_debug(dev, HDMI_AHB_DMA_THRSLD);
149 hdmi_audio_debug(dev, HDMI_AHB_DMA_STRADDR0);
150 hdmi_audio_debug(dev, HDMI_AHB_DMA_STPADDR0);
151 hdmi_audio_debug(dev, HDMI_AHB_DMA_BSTADDR0);
152 hdmi_audio_debug(dev, HDMI_AHB_DMA_MBLENGTH0);
153 hdmi_audio_debug(dev, HDMI_AHB_DMA_MBLENGTH1);
154 hdmi_audio_debug(dev, HDMI_AHB_DMA_STAT);
155 hdmi_audio_debug(dev, HDMI_AHB_DMA_INT);
156 hdmi_audio_debug(dev, HDMI_AHB_DMA_MASK);
157 hdmi_audio_debug(dev, HDMI_AHB_DMA_POL);
158 hdmi_audio_debug(dev, HDMI_AHB_DMA_CONF1);
159 hdmi_audio_debug(dev, HDMI_AHB_DMA_BUFFSTAT);
160 hdmi_audio_debug(dev, HDMI_AHB_DMA_BUFFINT);
161 hdmi_audio_debug(dev, HDMI_AHB_DMA_BUFFMASK);
162 hdmi_audio_debug(dev, HDMI_AHB_DMA_BUFFPOL);
163 hdmi_audio_debug(dev, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
164 hdmi_audio_debug(dev, HDMI_IH_AHBDMAAUD_STAT0);
165 hdmi_audio_debug(dev, HDMI_IH_MUTE);
168 static void dumppriv(struct device *dev, struct hdmi_dma_priv *priv)
170 dev_dbg(dev, "channels = %d\n", priv->channels);
171 dev_dbg(dev, "periods = %d\n", priv->periods);
172 dev_dbg(dev, "period_bytes = %d\n", priv->period_bytes);
173 dev_dbg(dev, "dma period_bytes = %d\n", priv->dma_period_bytes);
174 dev_dbg(dev, "buffer_ratio = %d\n", priv->buffer_ratio);
175 dev_dbg(dev, "hw dma buffer = 0x%08x\n", (int)priv->hw_buffer.addr);
176 dev_dbg(dev, "dma buf size = %d\n", (int)priv->buffer_bytes);
177 dev_dbg(dev, "sample_rate = %d\n", (int)priv->rate);
180 static void dumpregs(struct device *dev) {}
181 static void dumppriv(struct device *dev, struct hdmi_dma_priv *priv) {}
185 * Conditions for DMA to work:
186 * ((final_addr - initial_addr)>>2)+1) < 2k. So max period is 8k.
187 * (inital_addr & 0x3) == 0
188 * (final_addr & 0x3) == 0x3
190 * The DMA Period should be an integer multiple of the IEC 60958 audio
191 * frame size, which is 768 bytes (192 * 4).
193 static void hdmi_dma_set_addr(int start_addr, int dma_period_bytes)
195 int final_addr = start_addr + dma_period_bytes - 1;
197 hdmi_write4(start_addr, HDMI_AHB_DMA_STRADDR0);
198 hdmi_write4(final_addr, HDMI_AHB_DMA_STPADDR0);
201 static void hdmi_dma_irq_set(bool set)
203 u8 val = hdmi_readb(HDMI_AHB_DMA_MASK);
206 val |= HDMI_AHB_DMA_DONE;
208 val &= (u8)~HDMI_AHB_DMA_DONE;
210 hdmi_writeb(val, HDMI_AHB_DMA_MASK);
213 static void hdmi_mask(int mask)
215 u8 regval = hdmi_readb(HDMI_AHB_DMA_MASK);
218 regval |= HDMI_AHB_DMA_ERROR | HDMI_AHB_DMA_FIFO_EMPTY;
220 regval &= (u8)~(HDMI_AHB_DMA_ERROR | HDMI_AHB_DMA_FIFO_EMPTY);
222 hdmi_writeb(regval, HDMI_AHB_DMA_MASK);
225 int odd_ones(unsigned a)
235 /* Add frame information for one pcm subframe */
236 static u32 hdmi_dma_add_frame_info(struct hdmi_dma_priv *priv,
237 u32 pcm_data, int subframe_idx)
239 union hdmi_audio_dma_data_t subframe;
242 iec_header.B.channel = subframe_idx;
244 /* fill b (start-of-block) */
245 subframe.B.b = (priv->frame_idx == 0) ? 1 : 0;
247 /* fill c (channel status) */
248 if (priv->frame_idx < 42)
249 subframe.B.c = (iec_header.U >> priv->frame_idx) & 0x1;
253 subframe.B.p = odd_ones(pcm_data);
254 subframe.B.p ^= subframe.B.c;
255 subframe.B.p ^= subframe.B.u;
256 subframe.B.p ^= subframe.B.v;
259 if (priv->sample_bits == 16)
260 subframe.B.data = pcm_data << 8;
262 subframe.B.data = pcm_data;
267 static void init_table(int channels)
269 unsigned char *p = g_packet_head_table;
272 for (i = 0; i < 48; i++) {
277 for (ch = 0; ch < channels; ch++) {
280 iec_header.B.channel = ch+1;
281 c = (iec_header.U >> i) & 0x1;
283 /* preset bit p as c */
284 *p++ = (b << 4) | (c << 2) | (c << 3);
289 #ifdef HDMI_DMA_NO_NEON
290 /* Optimization for IEC head */
291 static void hdmi_dma_copy_16_c_lut(u16 *src, u32 *dst, int samples,
297 for (i = 0; i < samples; i++) {
298 /* get source sample */
302 p = sample ^ (sample >> 8);
306 p &= 1; /* only want last bit */
309 /* get packet header */
310 head = *lookup_table++;
316 *dst++ = (head << 24) | (sample << 8);
320 static void hdmi_dma_copy_16_c_fast(u16 *src, u32 *dst, int samples)
325 for (i = 0; i < samples; i++) {
326 /* get source sample */
330 p = sample ^ (sample >> 8);
334 p &= 1; /* only want last bit */
338 *dst++ = (p << 24) | (sample << 8);
342 static void hdmi_dma_copy_16(u16 *src, u32 *dst, int framecnt, int channelcnt)
344 /* split input frames into 192-frame each */
345 int count_in_192 = (framecnt + 191) / 192;
348 for (i = 0; i < count_in_192; i++) {
351 /* handles frame index [0, 48) */
352 count = (framecnt < 48) ? framecnt : 48;
353 samples = count * channelcnt;
354 hdmi_dma_copy_16_c_lut(src, dst, samples, g_packet_head_table);
362 /* handles frame index [48, 192) */
363 count = (framecnt < 192 - 48) ? framecnt : 192 - 48;
364 samples = count * channelcnt;
365 hdmi_dma_copy_16_c_fast(src, dst, samples);
372 /* NEON optimization for IEC head*/
375 * Convert pcm samples to iec samples suitable for HDMI transfer.
376 * PCM sample is 16 bits length.
377 * Frame index always starts from 0.
378 * Channel count can be 1, 2, 4, 6, or 8
379 * Sample count (frame_count * channel_count) is multipliable by 8.
381 static void hdmi_dma_copy_16(u16 *src, u32 *dst, int framecount, int channelcount)
383 /* split input frames into 192-frame each */
384 int i, count_in_192 = (framecount + 191) / 192;
386 for (i = 0; i < count_in_192; i++) {
389 /* handles frame index [0, 48) */
390 count = (framecount < 48) ? framecount : 48;
391 samples = count * channelcount;
392 hdmi_dma_copy_16_neon_lut(src, dst, samples, g_packet_head_table);
400 /* handles frame index [48, 192) */
401 count = (framecount < 192 - 48) ? framecount : (192 - 48);
402 samples = count * channelcount;
403 hdmi_dma_copy_16_neon_fast(src, dst, samples);
411 static void hdmi_dma_mmap_copy(struct snd_pcm_substream *substream,
412 int offset, int count)
414 struct snd_soc_pcm_runtime *rtd = substream->private_data;
415 struct snd_pcm_runtime *runtime = substream->runtime;
416 struct hdmi_dma_priv *priv = runtime->private_data;
417 struct device *dev = rtd->platform->dev;
418 u32 framecount, *dst;
421 framecount = count / (priv->sample_align * priv->channels);
423 /* hw_buffer is the destination for pcm data plus frame info. */
424 dst = (u32 *)(priv->hw_buffer.area + (offset * priv->buffer_ratio));
426 switch (priv->format) {
427 case SNDRV_PCM_FORMAT_S16_LE:
428 /* dma_buffer is the mmapped buffer we are copying pcm from. */
429 src16 = (u16 *)(runtime->dma_area + offset);
430 hdmi_dma_copy_16(src16, dst, framecount, priv->channels);
433 dev_err(dev, "unsupported sample format %s\n",
434 snd_pcm_format_name(priv->format));
439 static void hdmi_dma_data_copy(struct snd_pcm_substream *substream,
440 struct hdmi_dma_priv *priv, char type)
442 struct snd_pcm_runtime *runtime = substream->runtime;
443 unsigned long offset, count, appl_bytes, space_to_end;
445 if (runtime->access != SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
448 appl_bytes = frames_to_bytes(runtime, runtime->status->hw_ptr);
450 appl_bytes += 2 * priv->period_bytes;
451 offset = appl_bytes % priv->buffer_bytes;
455 count = priv->period_bytes;
456 space_to_end = priv->period_bytes;
459 count = priv->buffer_bytes;
460 space_to_end = priv->buffer_bytes - offset;
467 if (count <= space_to_end) {
468 hdmi_dma_mmap_copy(substream, offset, count);
470 hdmi_dma_mmap_copy(substream, offset, space_to_end);
471 hdmi_dma_mmap_copy(substream, 0, count - space_to_end);
475 static void hdmi_sdma_callback(void *data)
477 struct hdmi_dma_priv *priv = (struct hdmi_dma_priv *)data;
478 struct snd_pcm_substream *substream = priv->substream;
479 struct snd_pcm_runtime *runtime = substream->runtime;
482 spin_lock_irqsave(&priv->irq_lock, flags);
484 if (runtime && runtime->dma_area && priv->tx_active) {
485 priv->offset += priv->period_bytes;
486 priv->offset %= priv->period_bytes * priv->periods;
488 /* Copy data by period_bytes */
489 hdmi_dma_data_copy(substream, priv, 'p');
491 snd_pcm_period_elapsed(substream);
494 spin_unlock_irqrestore(&priv->irq_lock, flags);
499 static int hdmi_dma_set_thrsld_incrtype(struct device *dev, int channels)
501 u8 mask = HDMI_AHB_DMA_CONF0_BURST_MODE | HDMI_AHB_DMA_CONF0_INCR_TYPE_MASK;
502 u8 val = hdmi_readb(HDMI_AHB_DMA_CONF0) & ~mask;
503 int incr_type, threshold;
505 switch (hdmi_readb(HDMI_REVISION_ID)) {
507 incr_type = HDMI_DMA_BURST_INCR4;
514 incr_type = HDMI_DMA_BURST_INCR8;
518 dev_err(dev, "unknown hdmi controller!\n");
522 hdmi_writeb(threshold, HDMI_AHB_DMA_THRSLD);
525 case HDMI_DMA_BURST_UNSPECIFIED_LEGNTH:
527 case HDMI_DMA_BURST_INCR4:
528 val |= HDMI_AHB_DMA_CONF0_BURST_MODE;
530 case HDMI_DMA_BURST_INCR8:
531 val |= HDMI_AHB_DMA_CONF0_BURST_MODE |
532 HDMI_AHB_DMA_CONF0_INCR8;
534 case HDMI_DMA_BURST_INCR16:
535 val |= HDMI_AHB_DMA_CONF0_BURST_MODE |
536 HDMI_AHB_DMA_CONF0_INCR16;
539 dev_err(dev, "invalid increment type: %d!", incr_type);
543 hdmi_writeb(val, HDMI_AHB_DMA_CONF0);
545 hdmi_audio_debug(dev, HDMI_AHB_DMA_THRSLD);
550 static int hdmi_dma_configure_dma(struct device *dev, int channels)
555 if (channels <= 0 || channels > 8 || channels % 2 != 0) {
556 dev_err(dev, "unsupported channel number: %d\n", channels);
560 hdmi_audio_writeb(AHB_DMA_CONF0, EN_HLOCK, 0x1);
562 ret = hdmi_dma_set_thrsld_incrtype(dev, channels);
566 for (i = 0; i < channels; i += 2)
569 hdmi_writeb(val, HDMI_AHB_DMA_CONF1);
574 static void hdmi_dma_init_iec_header(void)
578 iec_header.B.consumer = 0; /* Consumer use */
579 iec_header.B.linear_pcm = 0; /* linear pcm audio */
580 iec_header.B.copyright = 1; /* no copyright */
581 iec_header.B.pre_emphasis = 0; /* 2 channels without pre-emphasis */
582 iec_header.B.mode = 0; /* Mode 0 */
584 iec_header.B.category_code = 0;
586 iec_header.B.source = 2; /* stereo */
587 iec_header.B.channel = 0;
589 iec_header.B.sample_freq = 0x02; /* 48 KHz */
590 iec_header.B.clock_acc = 0; /* Level II */
592 iec_header.B.word_length = 0x02; /* 16 bits */
593 iec_header.B.org_sample_freq = 0x0D; /* 48 KHz */
595 iec_header.B.cgms_a = 0; /* Copying is permitted without restriction */
598 static int hdmi_dma_update_iec_header(struct snd_pcm_substream *substream)
600 struct snd_soc_pcm_runtime *rtd = substream->private_data;
601 struct snd_pcm_runtime *runtime = substream->runtime;
602 struct hdmi_dma_priv *priv = runtime->private_data;
603 struct device *dev = rtd->platform->dev;
605 iec_header.B.source = priv->channels;
607 switch (priv->rate) {
609 iec_header.B.sample_freq = 0x03;
610 iec_header.B.org_sample_freq = 0x0C;
613 iec_header.B.sample_freq = 0x00;
614 iec_header.B.org_sample_freq = 0x0F;
617 iec_header.B.sample_freq = 0x02;
618 iec_header.B.org_sample_freq = 0x0D;
621 iec_header.B.sample_freq = 0x08;
622 iec_header.B.org_sample_freq = 0x07;
625 iec_header.B.sample_freq = 0x0A;
626 iec_header.B.org_sample_freq = 0x05;
629 iec_header.B.sample_freq = 0x0C;
630 iec_header.B.org_sample_freq = 0x03;
633 iec_header.B.sample_freq = 0x0E;
634 iec_header.B.org_sample_freq = 0x01;
637 dev_err(dev, "unsupported sample rate\n");
641 switch (priv->format) {
642 case SNDRV_PCM_FORMAT_S16_LE:
643 iec_header.B.word_length = 0x02;
645 case SNDRV_PCM_FORMAT_S24_LE:
646 iec_header.B.word_length = 0x0b;
656 * The HDMI block transmits the audio data without adding any of the audio
657 * frame bits. So we have to copy the raw dma data from the ALSA buffer
658 * to the DMA buffer, adding the frame information.
660 static int hdmi_dma_copy(struct snd_pcm_substream *substream, int channel,
661 snd_pcm_uframes_t pos, void __user *buf,
662 snd_pcm_uframes_t frames)
664 struct snd_pcm_runtime *runtime = substream->runtime;
665 struct hdmi_dma_priv *priv = runtime->private_data;
666 unsigned int count = frames_to_bytes(runtime, frames);
667 unsigned int pos_bytes = frames_to_bytes(runtime, pos);
672 /* Adding frame info to pcm data from userspace and copy to hw_buffer */
673 hw_buf = (u32 *)(priv->hw_buffer.area + (pos_bytes * priv->buffer_ratio));
676 for (subframe_idx = 1 ; subframe_idx <= priv->channels ; subframe_idx++) {
677 if (copy_from_user(&pcm_data, buf, priv->sample_align))
680 buf += priv->sample_align;
681 count -= priv->sample_align;
683 /* Save the header info to the audio dma buffer */
684 *hw_buf++ = hdmi_dma_add_frame_info(priv, pcm_data, subframe_idx);
688 if (priv->frame_idx == 192)
695 static int hdmi_sdma_initbuf(struct device *dev, struct hdmi_dma_priv *priv)
697 struct hdmi_sdma_script *hdmi_sdma_t = priv->hdmi_sdma_t;
701 dev_err(dev, "hdmi private addr invalid!!!\n");
705 hdmi_sdma_t->control_reg_addr = HDMI_BASE_ADDR + HDMI_AHB_DMA_START;
706 hdmi_sdma_t->status_reg_addr = HDMI_BASE_ADDR + HDMI_IH_AHBDMAAUD_STAT0;
707 hdmi_sdma_t->dma_start_addr = HDMI_BASE_ADDR + HDMI_AHB_DMA_STRADDR0;
709 head = &hdmi_sdma_t->buffer[0];
710 tail = &hdmi_sdma_t->buffer[1];
712 for (i = 0; i < priv->sdma_params.buffer_num; i++) {
713 *head = priv->hw_buffer.addr + i * priv->period_bytes * priv->buffer_ratio;
714 *tail = *head + priv->dma_period_bytes - 1;
722 static int hdmi_sdma_config(struct snd_pcm_substream *substream,
723 struct hdmi_dma_priv *priv)
725 struct snd_soc_pcm_runtime *rtd = substream->private_data;
726 struct device *dai_dev = &priv->pdev->dev;
727 struct device *dev = rtd->platform->dev;
728 struct dma_slave_config slave_config;
731 priv->dma_channel = dma_request_slave_channel(dai_dev, "tx");
732 if (priv->dma_channel == NULL) {
733 dev_err(dev, "failed to alloc dma channel\n");
737 priv->dma_data.data_addr1 = &priv->sdma_params.buffer_num;
738 priv->dma_data.data_addr2 = &priv->sdma_params.phyaddr;
739 priv->dma_channel->private = &priv->dma_data;
741 slave_config.direction = DMA_TRANS_NONE;
742 slave_config.dma_request0 = 0;
743 slave_config.dma_request1 = 0;
745 ret = dmaengine_slave_config(priv->dma_channel, &slave_config);
747 dev_err(dev, "failed to config slave dma\n");
754 static int hdmi_dma_hw_free(struct snd_pcm_substream *substream)
756 struct snd_pcm_runtime *runtime = substream->runtime;
757 struct hdmi_dma_priv *priv = runtime->private_data;
759 if (priv->dma_channel) {
760 dma_release_channel(priv->dma_channel);
761 priv->dma_channel = NULL;
767 static int hdmi_dma_hw_params(struct snd_pcm_substream *substream,
768 struct snd_pcm_hw_params *params)
770 struct snd_pcm_runtime *runtime = substream->runtime;
771 struct hdmi_dma_priv *priv = runtime->private_data;
772 struct snd_soc_pcm_runtime *rtd = substream->private_data;
773 struct device *dev = rtd->platform->dev;
776 priv->buffer_bytes = params_buffer_bytes(params);
777 priv->periods = params_periods(params);
778 priv->period_bytes = params_period_bytes(params);
779 priv->channels = params_channels(params);
780 priv->format = params_format(params);
781 priv->rate = params_rate(params);
784 priv->period_time = HZ / (priv->rate / params_period_size(params));
786 switch (priv->format) {
787 case SNDRV_PCM_FORMAT_S16_LE:
788 priv->buffer_ratio = 2;
789 priv->sample_align = 2;
790 priv->sample_bits = 16;
792 case SNDRV_PCM_FORMAT_S24_LE:
793 /* 24 bit audio in 32 bit word */
794 priv->buffer_ratio = 1;
795 priv->sample_align = 4;
796 priv->sample_bits = 24;
799 dev_err(dev, "unsupported sample format: %d\n", priv->format);
803 priv->dma_period_bytes = priv->period_bytes * priv->buffer_ratio;
804 priv->sdma_params.buffer_num = priv->periods;
805 priv->sdma_params.phyaddr = priv->phy_hdmi_sdma_t;
807 ret = hdmi_sdma_initbuf(dev, priv);
811 ret = hdmi_sdma_config(substream, priv);
815 snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
817 ret = hdmi_dma_configure_dma(dev, priv->channels);
821 hdmi_dma_set_addr(priv->hw_buffer.addr, priv->dma_period_bytes);
825 hdmi_dma_update_iec_header(substream);
827 /* Init par for mmap optimizate */
828 init_table(priv->channels);
830 priv->appl_bytes = 0;
835 static void hdmi_dma_trigger_init(struct snd_pcm_substream *substream,
836 struct hdmi_dma_priv *priv)
838 unsigned long status;
843 /* Copy data by buffer_bytes */
844 hdmi_dma_data_copy(substream, priv, 'b');
846 hdmi_audio_writeb(AHB_DMA_CONF0, SW_FIFO_RST, 0x1);
848 /* Delay after reset */
851 status = hdmi_readb(HDMI_IH_AHBDMAAUD_STAT0);
852 hdmi_writeb(status, HDMI_IH_AHBDMAAUD_STAT0);
855 static int hdmi_dma_prepare_and_submit(struct snd_pcm_substream *substream,
856 struct hdmi_dma_priv *priv)
858 struct snd_soc_pcm_runtime *rtd = substream->private_data;
859 struct device *dev = rtd->platform->dev;
861 priv->desc = dmaengine_prep_dma_cyclic(priv->dma_channel, 0, 0, 0,
864 dev_err(dev, "failed to prepare slave dma\n");
868 priv->desc->callback = hdmi_sdma_callback;
869 priv->desc->callback_param = (void *)priv;
870 dmaengine_submit(priv->desc);
875 static int hdmi_dma_trigger(struct snd_pcm_substream *substream, int cmd)
877 struct snd_pcm_runtime *runtime = substream->runtime;
878 struct snd_soc_pcm_runtime *rtd = substream->private_data;
879 struct hdmi_dma_priv *priv = runtime->private_data;
880 struct device *dev = rtd->platform->dev;
884 case SNDRV_PCM_TRIGGER_START:
885 case SNDRV_PCM_TRIGGER_RESUME:
886 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
887 if (!check_hdmi_state())
889 hdmi_dma_trigger_init(substream, priv);
893 priv->tx_active = true;
894 hdmi_audio_writeb(AHB_DMA_START, START, 0x1);
895 hdmi_dma_irq_set(false);
896 hdmi_set_dma_mode(1);
897 ret = hdmi_dma_prepare_and_submit(substream, priv);
900 dma_async_issue_pending(priv->desc->chan);
902 case SNDRV_PCM_TRIGGER_STOP:
903 case SNDRV_PCM_TRIGGER_SUSPEND:
904 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
905 dmaengine_terminate_all(priv->dma_channel);
906 hdmi_set_dma_mode(0);
907 hdmi_dma_irq_set(true);
908 hdmi_audio_writeb(AHB_DMA_STOP, STOP, 0x1);
909 priv->tx_active = false;
918 static snd_pcm_uframes_t hdmi_dma_pointer(struct snd_pcm_substream *substream)
920 struct snd_pcm_runtime *runtime = substream->runtime;
921 struct hdmi_dma_priv *priv = runtime->private_data;
923 return bytes_to_frames(runtime, priv->offset);
926 static struct snd_pcm_hardware snd_imx_hardware = {
927 .info = SNDRV_PCM_INFO_INTERLEAVED |
928 SNDRV_PCM_INFO_BLOCK_TRANSFER |
929 SNDRV_PCM_INFO_MMAP |
930 SNDRV_PCM_INFO_MMAP_VALID |
931 SNDRV_PCM_INFO_PAUSE |
932 SNDRV_PCM_INFO_RESUME,
933 .formats = MXC_HDMI_FORMATS_PLAYBACK,
937 .buffer_bytes_max = HDMI_PCM_BUF_SIZE,
938 .period_bytes_min = HDMI_DMA_PERIOD_BYTES / 2,
939 .period_bytes_max = HDMI_DMA_PERIOD_BYTES / 2,
945 static void hdmi_dma_irq_enable(struct hdmi_dma_priv *priv)
949 hdmi_writeb(0xff, HDMI_AHB_DMA_POL);
950 hdmi_writeb(0xff, HDMI_AHB_DMA_BUFFPOL);
952 spin_lock_irqsave(&priv->irq_lock, flags);
954 hdmi_writeb(0xff, HDMI_IH_AHBDMAAUD_STAT0);
955 hdmi_writeb(0xff, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
956 hdmi_dma_irq_set(false);
959 spin_unlock_irqrestore(&priv->irq_lock, flags);
962 static void hdmi_dma_irq_disable(struct hdmi_dma_priv *priv)
966 spin_lock_irqsave(&priv->irq_lock, flags);
968 hdmi_dma_irq_set(true);
969 hdmi_writeb(0x0, HDMI_IH_MUTE_AHBDMAAUD_STAT0);
970 hdmi_writeb(0xff, HDMI_IH_AHBDMAAUD_STAT0);
973 spin_unlock_irqrestore(&priv->irq_lock, flags);
976 static int hdmi_dma_open(struct snd_pcm_substream *substream)
978 struct snd_pcm_runtime *runtime = substream->runtime;
979 struct snd_soc_pcm_runtime *rtd = substream->private_data;
980 struct device *dev = rtd->platform->dev;
981 struct hdmi_dma_priv *priv = dev_get_drvdata(dev);
984 runtime->private_data = priv;
986 ret = mxc_hdmi_register_audio(substream);
988 dev_err(dev, "HDMI Video is not ready!\n");
992 hdmi_audio_writeb(AHB_DMA_CONF0, SW_FIFO_RST, 0x1);
994 ret = snd_pcm_hw_constraint_integer(substream->runtime,
995 SNDRV_PCM_HW_PARAM_PERIODS);
999 snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
1001 hdmi_dma_irq_enable(priv);
1006 static int hdmi_dma_close(struct snd_pcm_substream *substream)
1008 struct snd_pcm_runtime *runtime = substream->runtime;
1009 struct hdmi_dma_priv *priv = runtime->private_data;
1011 hdmi_dma_irq_disable(priv);
1012 mxc_hdmi_unregister_audio(substream);
1017 static struct snd_pcm_ops imx_hdmi_dma_pcm_ops = {
1018 .open = hdmi_dma_open,
1019 .close = hdmi_dma_close,
1020 .ioctl = snd_pcm_lib_ioctl,
1021 .hw_params = hdmi_dma_hw_params,
1022 .hw_free = hdmi_dma_hw_free,
1023 .trigger = hdmi_dma_trigger,
1024 .pointer = hdmi_dma_pointer,
1025 .copy = hdmi_dma_copy,
1028 static int imx_hdmi_dma_pcm_new(struct snd_soc_pcm_runtime *rtd)
1030 struct hdmi_dma_priv *priv = dev_get_drvdata(rtd->platform->dev);
1031 struct snd_card *card = rtd->card->snd_card;
1032 struct snd_pcm_substream *substream;
1033 struct snd_pcm *pcm = rtd->pcm;
1034 u64 dma_mask = DMA_BIT_MASK(32);
1037 if (!card->dev->dma_mask)
1038 card->dev->dma_mask = &dma_mask;
1039 if (!card->dev->coherent_dma_mask)
1040 card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
1042 substream = pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1044 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
1045 HDMI_PCM_BUF_SIZE, &substream->dma_buffer);
1047 dev_err(card->dev, "failed to alloc playback dma buffer\n");
1051 priv->substream = substream;
1053 /* Alloc the hw_buffer */
1054 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, pcm->card->dev,
1055 HDMI_DMA_BUF_SIZE, &priv->hw_buffer);
1057 dev_err(card->dev, "failed to alloc hw dma buffer\n");
1064 static void imx_hdmi_dma_pcm_free(struct snd_pcm *pcm)
1066 int stream = SNDRV_PCM_STREAM_PLAYBACK;
1067 struct snd_pcm_substream *substream = pcm->streams[stream].substream;
1068 struct snd_soc_pcm_runtime *rtd = pcm->private_data;
1069 struct hdmi_dma_priv *priv = dev_get_drvdata(rtd->platform->dev);
1072 snd_dma_free_pages(&substream->dma_buffer);
1073 substream->dma_buffer.area = NULL;
1074 substream->dma_buffer.addr = 0;
1077 /* Free the hw_buffer */
1078 snd_dma_free_pages(&priv->hw_buffer);
1079 priv->hw_buffer.area = NULL;
1080 priv->hw_buffer.addr = 0;
1083 static struct snd_soc_platform_driver imx_hdmi_platform = {
1084 .ops = &imx_hdmi_dma_pcm_ops,
1085 .pcm_new = imx_hdmi_dma_pcm_new,
1086 .pcm_free = imx_hdmi_dma_pcm_free,
1089 static int imx_soc_platform_probe(struct platform_device *pdev)
1091 struct imx_hdmi *hdmi_drvdata = platform_get_drvdata(pdev);
1092 struct hdmi_dma_priv *priv;
1095 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1097 dev_err(&pdev->dev, "Failed to alloc hdmi_dma\n");
1101 priv->hdmi_sdma_t = dma_alloc_coherent(NULL,
1102 sizeof(struct hdmi_sdma_script),
1103 &priv->phy_hdmi_sdma_t, GFP_KERNEL);
1104 if (!priv->hdmi_sdma_t) {
1105 dev_err(&pdev->dev, "Failed to alloc hdmi_sdma_t\n");
1109 priv->tx_active = false;
1110 spin_lock_init(&priv->irq_lock);
1112 priv->pdev = hdmi_drvdata->pdev;
1114 hdmi_dma_init_iec_header();
1116 dev_set_drvdata(&pdev->dev, priv);
1118 switch (hdmi_readb(HDMI_REVISION_ID)) {
1120 snd_imx_hardware.period_bytes_max = HDMI_DMA_PERIOD_BYTES / 4;
1121 snd_imx_hardware.period_bytes_min = HDMI_DMA_PERIOD_BYTES / 4;
1127 ret = snd_soc_register_platform(&pdev->dev, &imx_hdmi_platform);
1134 dma_free_coherent(NULL, sizeof(struct hdmi_sdma_script),
1135 priv->hdmi_sdma_t, priv->phy_hdmi_sdma_t);
1140 static int imx_soc_platform_remove(struct platform_device *pdev)
1142 struct hdmi_dma_priv *priv = dev_get_drvdata(&pdev->dev);
1144 dma_free_coherent(NULL, sizeof(struct hdmi_sdma_script),
1145 priv->hdmi_sdma_t, priv->phy_hdmi_sdma_t);
1147 snd_soc_unregister_platform(&pdev->dev);
1152 static struct platform_driver imx_hdmi_dma_driver = {
1154 .name = "imx-hdmi-audio",
1155 .owner = THIS_MODULE,
1157 .probe = imx_soc_platform_probe,
1158 .remove = imx_soc_platform_remove,
1161 module_platform_driver(imx_hdmi_dma_driver);
1163 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1164 MODULE_DESCRIPTION("i.MX HDMI audio DMA");
1165 MODULE_LICENSE("GPL");