summaryrefslogtreecommitdiff
path: root/sound/soc/mxs/mxs-adc.c
blob: e8bb4255fff5fd80d6dc19f6c96f526015f4fb30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * ASoC Audio Layer for Freescale MXS ADC/DAC
 *
 * Author: Vladislav Buzov <vbuzov@embeddedalley.com>
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/dma-mapping.h>
#include <linux/clk.h>
#include <linux/delay.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <mach/dma.h>
#include <asm/mach-types.h>
#include <mach/hardware.h>
#include <mach/regs-audioin.h>
#include <mach/regs-audioout.h>

#include "mxs-pcm.h"

#define MXS_ADC_RATES	SNDRV_PCM_RATE_8000_192000
#define MXS_ADC_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
				SNDRV_PCM_FMTBIT_S32_LE)

struct mxs_pcm_dma_params mxs_audio_in = {
	.name = "mxs-audio-in",
	.dma_ch	= MXS_DMA_CHANNEL_AHB_APBX_AUDIOADC,
	.irq = IRQ_ADC_DMA,
};

struct mxs_pcm_dma_params mxs_audio_out = {
	.name = "mxs-audio-out",
	.dma_ch	= MXS_DMA_CHANNEL_AHB_APBX_AUDIODAC,
	.irq = IRQ_DAC_DMA,
};

static irqreturn_t mxs_err_irq(int irq, void *dev_id)
{
	struct snd_pcm_substream *substream = dev_id;
	int playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0;
	u32 ctrl_reg;
	u32 overflow_mask;
	u32 underflow_mask;

	if (playback) {
		ctrl_reg = __raw_readl(REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL);
		underflow_mask = BM_AUDIOOUT_CTRL_FIFO_UNDERFLOW_IRQ;
		overflow_mask = BM_AUDIOOUT_CTRL_FIFO_OVERFLOW_IRQ;
	} else {
		ctrl_reg = __raw_readl(REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL);
		underflow_mask = BM_AUDIOIN_CTRL_FIFO_UNDERFLOW_IRQ;
		overflow_mask = BM_AUDIOIN_CTRL_FIFO_OVERFLOW_IRQ;
	}

	if (ctrl_reg & underflow_mask) {
		printk(KERN_DEBUG "%s underflow detected\n",
		       playback ? "DAC" : "ADC");

		if (playback)
			__raw_writel(
				BM_AUDIOOUT_CTRL_FIFO_UNDERFLOW_IRQ,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		else
			__raw_writel(
				BM_AUDIOIN_CTRL_FIFO_UNDERFLOW_IRQ,
				REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);

	} else if (ctrl_reg & overflow_mask) {
		printk(KERN_DEBUG "%s overflow detected\n",
		       playback ? "DAC" : "ADC");

		if (playback)
			__raw_writel(
				BM_AUDIOOUT_CTRL_FIFO_OVERFLOW_IRQ,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		else
			__raw_writel(BM_AUDIOIN_CTRL_FIFO_OVERFLOW_IRQ,
				REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);
	} else
		printk(KERN_WARNING "Unknown DAC error interrupt\n");

	return IRQ_HANDLED;
}

static int mxs_adc_trigger(struct snd_pcm_substream *substream,
				int cmd,
				struct snd_soc_dai *dai)
{
	int playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0;
	int ret = 0;
	u32 reg = 0;
	u32 reg1 = 0;
	u32 l, r;
	u32 ll, rr;
	int i;

	switch (cmd) {
	case SNDRV_PCM_TRIGGER_START:

		if (playback) {
			reg = __raw_readl(REGS_AUDIOOUT_BASE + \
					HW_AUDIOOUT_HPVOL);
			reg1 = BM_AUDIOOUT_HPVOL_VOL_LEFT | \
				BM_AUDIOOUT_HPVOL_VOL_RIGHT;
			__raw_writel(reg1, REGS_AUDIOOUT_BASE + \
				HW_AUDIOOUT_HPVOL);

			__raw_writel(BM_AUDIOOUT_CTRL_RUN,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_SET);
			__raw_writel(BM_AUDIOOUT_ANACTRL_HP_HOLD_GND,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_ANACTRL_CLR);

			reg1 = reg & ~BM_AUDIOOUT_HPVOL_VOL_LEFT;
			reg1 = reg1 & ~BM_AUDIOOUT_HPVOL_VOL_RIGHT;

			l = (reg & BM_AUDIOOUT_HPVOL_VOL_LEFT) >>
				BP_AUDIOOUT_HPVOL_VOL_LEFT;
			r = (reg & BM_AUDIOOUT_HPVOL_VOL_RIGHT) >>
				BP_AUDIOOUT_HPVOL_VOL_RIGHT;
			for (i = 0x7f; i > 0 ; i -= 0x8) {
				ll = i > l ? i : l;
				rr = i > r ? i : r;
				/* fade in hp vol */
				reg = reg1 | BF_AUDIOOUT_HPVOL_VOL_LEFT(ll)
					| BF_AUDIOOUT_HPVOL_VOL_RIGHT(rr);
				__raw_writel(reg,
					REGS_AUDIOOUT_BASE + HW_AUDIOOUT_HPVOL);
				udelay(100);
			}
			__raw_writel(BM_AUDIOOUT_SPEAKERCTRL_MUTE,
			    REGS_AUDIOIN_BASE + HW_AUDIOOUT_SPEAKERCTRL_CLR);
		}
		else
			__raw_writel(BM_AUDIOIN_CTRL_RUN,
				REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_SET);

		break;

	case SNDRV_PCM_TRIGGER_STOP:

		if (playback) {
			__raw_writel(BM_AUDIOOUT_ANACTRL_HP_HOLD_GND,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_ANACTRL_SET);
			__raw_writel(BM_AUDIOOUT_SPEAKERCTRL_MUTE,
			    REGS_AUDIOOUT_BASE + HW_AUDIOOUT_SPEAKERCTRL_SET);

			__raw_writel(BM_AUDIOOUT_CTRL_RUN,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		}
		else
			__raw_writel(BM_AUDIOIN_CTRL_RUN,
				REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);
		break;

	case SNDRV_PCM_TRIGGER_RESUME:
	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
	case SNDRV_PCM_TRIGGER_SUSPEND:
	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}

static int mxs_adc_startup(struct snd_pcm_substream *substream,
				struct snd_soc_dai *dai)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
	int playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0;
	int irq;
	int ret;

	if (playback) {
		irq = IRQ_DAC_ERROR;
		cpu_dai->dma_data = &mxs_audio_out;
	} else {
		irq = IRQ_ADC_ERROR;
		cpu_dai->dma_data = &mxs_audio_in;
	}

	ret = request_irq(irq, mxs_err_irq, 0, "MXS DAC/ADC Error",
			  substream);
	if (ret) {
		printk(KERN_ERR "%s: Unable to request ADC/DAC error irq %d\n",
		       __func__, IRQ_DAC_ERROR);
		return ret;
	}

	/* Enable error interrupt */
	if (playback) {
		__raw_writel(BM_AUDIOOUT_CTRL_FIFO_OVERFLOW_IRQ,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		__raw_writel(BM_AUDIOOUT_CTRL_FIFO_UNDERFLOW_IRQ,
				REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		__raw_writel(BM_AUDIOOUT_CTRL_FIFO_ERROR_IRQ_EN,
			REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_SET);
	} else {
		__raw_writel(BM_AUDIOIN_CTRL_FIFO_OVERFLOW_IRQ,
			REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);
		__raw_writel(BM_AUDIOIN_CTRL_FIFO_UNDERFLOW_IRQ,
			REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);
		__raw_writel(BM_AUDIOIN_CTRL_FIFO_ERROR_IRQ_EN,
			REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_SET);
	}

	return 0;
}

static void mxs_adc_shutdown(struct snd_pcm_substream *substream,
				  struct snd_soc_dai *dai)
{
	int playback = substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? 1 : 0;

	/* Disable error interrupt */
	if (playback) {
		__raw_writel(BM_AUDIOOUT_CTRL_FIFO_ERROR_IRQ_EN,
			REGS_AUDIOOUT_BASE + HW_AUDIOOUT_CTRL_CLR);
		free_irq(IRQ_DAC_ERROR, substream);
	} else {
		__raw_writel(BM_AUDIOIN_CTRL_FIFO_ERROR_IRQ_EN,
			REGS_AUDIOIN_BASE + HW_AUDIOIN_CTRL_CLR);
		free_irq(IRQ_ADC_ERROR, substream);
	}
}

#ifdef CONFIG_PM
static int mxs_adc_suspend(struct snd_soc_dai *cpu_dai)
{
	return 0;
}

static int mxs_adc_resume(struct snd_soc_dai *cpu_dai)
{
	return 0;
}
#else
#define mxs_adc_suspend	NULL
#define mxs_adc_resume	NULL
#endif /* CONFIG_PM */

struct snd_soc_dai_ops mxs_adc_dai_ops = {
	.startup = mxs_adc_startup,
	.shutdown = mxs_adc_shutdown,
	.trigger = mxs_adc_trigger,
};

struct snd_soc_dai mxs_adc_dai = {
	.name = "mxs adc/dac",
	.id = 0,
	.suspend = mxs_adc_suspend,
	.resume = mxs_adc_resume,
	.playback = {
		.channels_min = 2,
		.channels_max = 2,
		.rates = MXS_ADC_RATES,
		.formats = MXS_ADC_FORMATS,
	},
	.capture = {
		.channels_min = 2,
		.channels_max = 2,
		.rates = MXS_ADC_RATES,
		.formats = MXS_ADC_FORMATS,
	},
	.ops = &mxs_adc_dai_ops,
};
EXPORT_SYMBOL_GPL(mxs_adc_dai);

static int __init mxs_adc_dai_init(void)
{
	return snd_soc_register_dai(&mxs_adc_dai);
}

static void __exit mxs_adc_dai_exit(void)
{
	snd_soc_unregister_dai(&mxs_adc_dai);
}
module_init(mxs_adc_dai_init);
module_exit(mxs_adc_dai_exit);

MODULE_AUTHOR("Vladislav Buzov");
MODULE_DESCRIPTION("MXS ADC/DAC DAI");
MODULE_LICENSE("GPL");