sam9g20_wm8731.c 6.7 KB
Newer Older
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
/*
 * sam9g20_wm8731  --  SoC audio for AT91SAM9G20-based
 * 			ATMEL AT91SAM9G20ek board.
 *
 *  Copyright (C) 2005 SAN People
 *  Copyright (C) 2008 Atmel
 *
 * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
 *
 * Based on ati_b1_wm8731.c by:
 * Frank Mandarino <fmandarino@endrelia.com>
 * Copyright 2006 Endrelia Technologies Inc.
 * Based on corgi.c by:
 * Copyright 2005 Wolfson Microelectronics PLC.
 * Copyright 2005 Openedhand Ltd.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
39
#include <linux/i2c.h>
40 41 42 43 44 45 46 47

#include <linux/atmel-ssc.h>

#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>

48
#include <asm/mach-types.h>
49 50 51 52 53 54 55
#include <mach/hardware.h>
#include <mach/gpio.h>

#include "../codecs/wm8731.h"
#include "atmel-pcm.h"
#include "atmel_ssc_dai.h"

56 57
#define MCLK_RATE 12000000

58 59 60 61 62 63 64
/*
 * As shipped the board does not have inputs.  However, it is relatively
 * straightforward to modify the board to hook them up so support is left
 * in the driver.
 */
#undef ENABLE_MIC_INPUT

65
static struct clk *mclk;
66 67 68 69 70

static int at91sam9g20ek_hw_params(struct snd_pcm_substream *substream,
	struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
71 72
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
73 74 75 76
	int ret;

	/* set codec DAI configuration */
	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
77
		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
78 79 80 81 82
	if (ret < 0)
		return ret;

	/* set cpu DAI configuration */
	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
83
		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
84 85 86 87 88 89 90 91 92 93
	if (ret < 0)
		return ret;

	return 0;
}

static struct snd_soc_ops at91sam9g20ek_ops = {
	.hw_params = at91sam9g20ek_hw_params,
};

94
static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
95
					struct snd_soc_dapm_context *dapm,
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
					enum snd_soc_bias_level level)
{
	static int mclk_on;
	int ret = 0;

	switch (level) {
	case SND_SOC_BIAS_ON:
	case SND_SOC_BIAS_PREPARE:
		if (!mclk_on)
			ret = clk_enable(mclk);
		if (ret == 0)
			mclk_on = 1;
		break;

	case SND_SOC_BIAS_OFF:
	case SND_SOC_BIAS_STANDBY:
		if (mclk_on)
			clk_disable(mclk);
		mclk_on = 0;
		break;
	}

	return ret;
}
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
	SND_SOC_DAPM_MIC("Int Mic", NULL),
	SND_SOC_DAPM_SPK("Ext Spk", NULL),
};

static const struct snd_soc_dapm_route intercon[] = {

	/* speaker connected to LHPOUT */
	{"Ext Spk", NULL, "LHPOUT"},

	/* mic is connected to Mic Jack, with WM8731 Mic Bias */
	{"MICIN", NULL, "Mic Bias"},
	{"Mic Bias", NULL, "Int Mic"},
};

/*
 * Logic for a wm8731 as connected on a at91sam9g20ek board.
 */
139
static int at91sam9g20ek_wm8731_init(struct snd_soc_pcm_runtime *rtd)
140
{
141 142
	struct snd_soc_codec *codec = rtd->codec;
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
L
Liam Girdwood 已提交
143
	struct snd_soc_dapm_context *dapm = &codec->dapm;
144 145
	int ret;

146 147 148 149
	printk(KERN_DEBUG
			"at91sam9g20ek_wm8731 "
			": at91sam9g20ek_wm8731_init() called\n");

150
	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_MCLK,
151 152 153 154 155 156
		MCLK_RATE, SND_SOC_CLOCK_IN);
	if (ret < 0) {
		printk(KERN_ERR "Failed to set WM8731 SYSCLK: %d\n", ret);
		return ret;
	}

157
	/* Add specific widgets */
L
Liam Girdwood 已提交
158
	snd_soc_dapm_new_controls(dapm, at91sam9g20ek_dapm_widgets,
159 160
				  ARRAY_SIZE(at91sam9g20ek_dapm_widgets));
	/* Set up specific audio path interconnects */
L
Liam Girdwood 已提交
161
	snd_soc_dapm_add_routes(dapm, intercon, ARRAY_SIZE(intercon));
162 163

	/* not connected */
L
Liam Girdwood 已提交
164 165
	snd_soc_dapm_nc_pin(dapm, "RLINEIN");
	snd_soc_dapm_nc_pin(dapm, "LLINEIN");
166

167
#ifdef ENABLE_MIC_INPUT
L
Liam Girdwood 已提交
168
	snd_soc_dapm_enable_pin(dapm, "Int Mic");
169
#else
L
Liam Girdwood 已提交
170
	snd_soc_dapm_nc_pin(dapm, "Int Mic");
171 172 173
#endif

	/* always connected */
L
Liam Girdwood 已提交
174
	snd_soc_dapm_enable_pin(dapm, "Ext Spk");
175 176 177 178 179 180 181

	return 0;
}

static struct snd_soc_dai_link at91sam9g20ek_dai = {
	.name = "WM8731",
	.stream_name = "WM8731 PCM",
182 183
	.cpu_dai_name = "atmel-ssc-dai.0",
	.codec_dai_name = "wm8731-hifi",
184
	.init = at91sam9g20ek_wm8731_init,
185
	.platform_name = "atmel-pcm-audio",
186
	.codec_name = "wm8731.0-001b",
187 188 189
	.ops = &at91sam9g20ek_ops,
};

190
static struct snd_soc_card snd_soc_at91sam9g20ek = {
191
	.name = "AT91SAMG20-EK",
192
	.owner = THIS_MODULE,
193 194
	.dai_link = &at91sam9g20ek_dai,
	.num_links = 1,
195
	.set_bias_level = at91sam9g20ek_set_bias_level,
196 197
};

198
static int __devinit at91sam9g20ek_audio_probe(struct platform_device *pdev)
199
{
200
	struct clk *pllb;
201
	struct snd_soc_card *card = &snd_soc_at91sam9g20ek;
202 203
	int ret;

204
	if (!(machine_is_at91sam9g20ek() || machine_is_at91sam9g20ek_2mmc()))
205 206
		return -ENODEV;

207 208 209 210 211 212 213 214 215 216 217
	/*
	 * Codec MCLK is supplied by PCK0 - set it up.
	 */
	mclk = clk_get(NULL, "pck0");
	if (IS_ERR(mclk)) {
		printk(KERN_ERR "ASoC: Failed to get MCLK\n");
		ret = PTR_ERR(mclk);
		goto err;
	}

	pllb = clk_get(NULL, "pllb");
V
Vasiliy Kulikov 已提交
218
	if (IS_ERR(pllb)) {
219
		printk(KERN_ERR "ASoC: Failed to get PLLB\n");
V
Vasiliy Kulikov 已提交
220
		ret = PTR_ERR(pllb);
221 222 223 224 225 226 227 228 229 230 231
		goto err_mclk;
	}
	ret = clk_set_parent(mclk, pllb);
	clk_put(pllb);
	if (ret != 0) {
		printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
		goto err_mclk;
	}

	clk_set_rate(mclk, MCLK_RATE);

232 233
	card->dev = &pdev->dev;
	ret = snd_soc_register_card(card);
234
	if (ret) {
235
		printk(KERN_ERR "ASoC: snd_soc_register_card() failed\n");
236 237 238 239
	}

	return ret;

240 241 242 243
err_mclk:
	clk_put(mclk);
	mclk = NULL;
err:
244 245 246
	return ret;
}

247
static int __devexit at91sam9g20ek_audio_remove(struct platform_device *pdev)
248
{
249 250 251
	struct snd_soc_card *card = platform_get_drvdata(pdev);

	snd_soc_unregister_card(card);
252 253
	clk_put(mclk);
	mclk = NULL;
254 255

	return 0;
256 257
}

258 259 260 261 262 263 264 265 266 267
static struct platform_driver at91sam9g20ek_audio_driver = {
	.driver = {
		.name	= "at91sam9g20ek-audio",
		.owner	= THIS_MODULE,
	},
	.probe	= at91sam9g20ek_audio_probe,
	.remove	= __devexit_p(at91sam9g20ek_audio_remove),
};

module_platform_driver(at91sam9g20ek_audio_driver);
268 269 270 271

/* Module information */
MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
272
MODULE_ALIAS("platform:at91sam9g20ek-audio");
273
MODULE_LICENSE("GPL");