summaryrefslogtreecommitdiff
path: root/sound/soc/mxs/mxs-devb.c
blob: 6847c54a64850a6338e7c769223d988bcf0b72a0 (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
/*
 * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/pm.h>
#include <linux/bitops.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/clk.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-dapm.h>
#include <sound/initval.h>

#include <mach/dma.h>
#include <mach/device.h>

#include "mxs-dai.h"
#include "mxs-pcm.h"
#include "../codecs/sgtl5000.h"

struct mxs_evk_priv {
	int sysclk;
	int hw;
	struct platform_device *pdev;
};

static struct mxs_evk_priv card_priv;

static int mxs_evk_audio_hw_params(struct snd_pcm_substream *substream,
				      struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai_link *machine = rtd->dai;
	struct snd_soc_dai *cpu_dai = machine->cpu_dai;
	struct snd_soc_dai *codec_dai = machine->codec_dai;
	struct mxs_evk_priv *priv = &card_priv;
	unsigned int rate = params_rate(params);
	int ret = 0;

	u32 dai_format;

	/* only need to do this once as capture and playback are sync */
	if (priv->hw)
		return 0;
	priv->hw = 1;
	priv->sysclk = 512 * rate;

	snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, (priv->sysclk)/2, 0);
	snd_soc_dai_set_sysclk(codec_dai, SGTL5000_LRCLK, rate, 0);

	snd_soc_dai_set_clkdiv(cpu_dai, IMX_SSP_SYS_MCLK, 256);
	/* set codec to slave mode */
	dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
	    SND_SOC_DAIFMT_CBS_CFS;

	/* set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
	if (ret < 0)
		return ret;
	/* set cpu_dai to master mode for playback, slave mode for record */
	dai_format = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
		SND_SOC_DAIFMT_CBM_CFM;

	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
	if (ret < 0)
		return ret;

	/* set the SAIF system clock as output */
	snd_soc_dai_set_sysclk(cpu_dai, IMX_SSP_SYS_CLK, priv->sysclk, \
		SND_SOC_CLOCK_OUT);

	return 0;
}

static int mxs_evk_startup(struct snd_pcm_substream *substream)
{
	return 0;
}

static void mxs_evk_shutdown(struct snd_pcm_substream *substream)
{
	struct mxs_evk_priv *priv = &card_priv;
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai_link *machine = rtd->dai;
	struct snd_soc_dai *cpu_dai = machine->cpu_dai;

	if (cpu_dai->playback.active || cpu_dai->capture.active)
		priv->hw = 1;
	else
		priv->hw = 0;
}

/*
 * mxs_evk SGTL5000 audio DAI opserations.
 */
static struct snd_soc_ops mxs_evk_ops = {
	.startup = mxs_evk_startup,
	.shutdown = mxs_evk_shutdown,
	.hw_params = mxs_evk_audio_hw_params,
};

/* mxs_evk machine connections to the codec pins */
static const struct snd_soc_dapm_route audio_map[] = {

	/* Mic Jack --> MIC_IN (with automatic bias) */
	{"MIC_IN", NULL, "Mic Jack"},

	/* Line in Jack --> LINE_IN */
	{"LINE_IN", NULL, "Line In Jack"},

	/* HP_OUT --> Headphone Jack */
	{"Headphone Jack", NULL, "HP_OUT"},

	/* LINE_OUT --> Ext Speaker */
	{"Ext Spk", NULL, "LINE_OUT"},
};

static const char *jack_function[] = { "off", "on"};

static const char *spk_function[] = { "off", "on" };

static const char *line_in_function[] = { "off", "on" };

static const struct soc_enum sgtl5000_enum[] = {
	SOC_ENUM_SINGLE_EXT(2, jack_function),
	SOC_ENUM_SINGLE_EXT(2, spk_function),
	SOC_ENUM_SINGLE_EXT(2, line_in_function),
};

/* mxs_evk card dapm widgets */
static const struct snd_soc_dapm_widget mxs_evk_dapm_widgets[] = {
	SND_SOC_DAPM_MIC("Mic Jack", NULL),
	SND_SOC_DAPM_LINE("Line In Jack", NULL),
	SND_SOC_DAPM_HP("Headphone Jack", NULL),
};

static int mxs_evk_sgtl5000_init(struct snd_soc_codec *codec)
{
	/* Add mxs_evk specific widgets */
	snd_soc_dapm_new_controls(codec, mxs_evk_dapm_widgets,
				  ARRAY_SIZE(mxs_evk_dapm_widgets));

	/* Set up mxs_evk specific audio path audio_map */
	snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));

	snd_soc_dapm_sync(codec);

	return 0;
}

/* mxs_evk digital audio interface glue - connects codec <--> CPU */
static struct snd_soc_dai_link mxs_evk_dai = {
	.name = "SGTL5000",
	.stream_name = "SGTL5000",
	.codec_dai = &sgtl5000_dai,
	.init = mxs_evk_sgtl5000_init,
	.ops = &mxs_evk_ops,
};

static int mxs_evk_card_remove(struct platform_device *pdev)
{
	struct mxs_evk_priv *priv = &card_priv;
	struct mxs_audio_platform_data *plat;
	if (priv->pdev) {
		plat = priv->pdev->dev.platform_data;
		if (plat->finit)
			plat->finit();
	}

	return 0;
}

static struct snd_soc_card snd_soc_card_mxs_evk = {
	.name = "mxs-evk",
	.platform = &mxs_soc_platform,
	.dai_link = &mxs_evk_dai,
	.num_links = 1,
	.remove = mxs_evk_card_remove,
};

static struct snd_soc_device mxs_evk_snd_devdata = {
	.card = &snd_soc_card_mxs_evk,
	.codec_dev = &soc_codec_dev_sgtl5000,
};

static int __devinit mxs_evk_sgtl5000_probe(struct platform_device *pdev)
{
	struct mxs_audio_platform_data *plat = pdev->dev.platform_data;
	struct mxs_saif *saif_select;
	int ret = -EINVAL;
	if (plat->init && plat->init())
		goto err_plat_init;
	mxs_evk_dai.cpu_dai = &mxs_saif_dai[0];
	saif_select = (struct mxs_saif *)mxs_evk_dai.cpu_dai->private_data;
	saif_select->stream_mapping = PLAYBACK_SAIF0_CAPTURE_SAIF1;
	saif_select->saif_mclk = plat->saif_mclock;
	saif_select->saif_clk = SAIF0;
	return 0;
err_plat_init:
	if (plat->finit)
		plat->finit();
	return ret;
}

static int mxs_evk_sgtl5000_remove(struct platform_device *pdev)
{
	struct mxs_audio_platform_data *plat = pdev->dev.platform_data;

	if (plat->finit)
		plat->finit();
	return 0;
}

static struct platform_driver mxs_evk_sgtl5000_audio_driver = {
	.probe = mxs_evk_sgtl5000_probe,
	.remove = mxs_evk_sgtl5000_remove,
	.driver = {
		   .name = "mxs-sgtl5000",
		   },
};

static struct platform_device *mxs_evk_snd_device;

static int __init mxs_evk_init(void)
{
	int ret;

	ret = platform_driver_register(&mxs_evk_sgtl5000_audio_driver);
	if (ret)
		return -ENOMEM;

	mxs_evk_snd_device = platform_device_alloc("soc-audio", 1);
	if (!mxs_evk_snd_device)
		return -ENOMEM;

	platform_set_drvdata(mxs_evk_snd_device, &mxs_evk_snd_devdata);
	mxs_evk_snd_devdata.dev = &mxs_evk_snd_device->dev;
	ret = platform_device_add(mxs_evk_snd_device);

	if (ret)
		platform_device_put(mxs_evk_snd_device);

	return ret;
}

static void __exit mxs_evk_exit(void)
{
	platform_driver_unregister(&mxs_evk_sgtl5000_audio_driver);
	platform_device_unregister(mxs_evk_snd_device);
}

module_init(mxs_evk_init);
module_exit(mxs_evk_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("SGTL5000 Driver for MXS EVK");
MODULE_LICENSE("GPL");