1/*
2 * sam9g20_wm8731  --  SoC audio for AT91SAM9G20-based
3 * 			ATMEL AT91SAM9G20ek board.
4 *
5 *  Copyright (C) 2005 SAN People
6 *  Copyright (C) 2008 Atmel
7 *
8 * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
9 *
10 * Based on ati_b1_wm8731.c by:
11 * Frank Mandarino <fmandarino@endrelia.com>
12 * Copyright 2006 Endrelia Technologies Inc.
13 * Based on corgi.c by:
14 * Copyright 2005 Wolfson Microelectronics PLC.
15 * Copyright 2005 Openedhand Ltd.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
30 */
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/kernel.h>
35#include <linux/clk.h>
36#include <linux/timer.h>
37#include <linux/interrupt.h>
38#include <linux/platform_device.h>
39#include <linux/i2c.h>
40#include <linux/of.h>
41
42#include <linux/atmel-ssc.h>
43
44#include <sound/core.h>
45#include <sound/pcm.h>
46#include <sound/pcm_params.h>
47#include <sound/soc.h>
48
49#include "../codecs/wm8731.h"
50#include "atmel-pcm.h"
51#include "atmel_ssc_dai.h"
52
53#define MCLK_RATE 12000000
54
55/*
56 * As shipped the board does not have inputs.  However, it is relatively
57 * straightforward to modify the board to hook them up so support is left
58 * in the driver.
59 */
60#undef ENABLE_MIC_INPUT
61
62static struct clk *mclk;
63
64static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
65					struct snd_soc_dapm_context *dapm,
66					enum snd_soc_bias_level level)
67{
68	static int mclk_on;
69	int ret = 0;
70
71	switch (level) {
72	case SND_SOC_BIAS_ON:
73	case SND_SOC_BIAS_PREPARE:
74		if (!mclk_on)
75			ret = clk_enable(mclk);
76		if (ret == 0)
77			mclk_on = 1;
78		break;
79
80	case SND_SOC_BIAS_OFF:
81	case SND_SOC_BIAS_STANDBY:
82		if (mclk_on)
83			clk_disable(mclk);
84		mclk_on = 0;
85		break;
86	}
87
88	return ret;
89}
90
91static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
92	SND_SOC_DAPM_MIC("Int Mic", NULL),
93	SND_SOC_DAPM_SPK("Ext Spk", NULL),
94};
95
96static const struct snd_soc_dapm_route intercon[] = {
97
98	/* speaker connected to LHPOUT/RHPOUT */
99	{"Ext Spk", NULL, "LHPOUT"},
100	{"Ext Spk", NULL, "RHPOUT"},
101
102	/* mic is connected to Mic Jack, with WM8731 Mic Bias */
103	{"MICIN", NULL, "Mic Bias"},
104	{"Mic Bias", NULL, "Int Mic"},
105};
106
107/*
108 * Logic for a wm8731 as connected on a at91sam9g20ek board.
109 */
110static int at91sam9g20ek_wm8731_init(struct snd_soc_pcm_runtime *rtd)
111{
112	struct snd_soc_dai *codec_dai = rtd->codec_dai;
113	int ret;
114
115	printk(KERN_DEBUG
116			"at91sam9g20ek_wm8731 "
117			": at91sam9g20ek_wm8731_init() called\n");
118
119	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_MCLK,
120		MCLK_RATE, SND_SOC_CLOCK_IN);
121	if (ret < 0) {
122		printk(KERN_ERR "Failed to set WM8731 SYSCLK: %d\n", ret);
123		return ret;
124	}
125
126#ifndef ENABLE_MIC_INPUT
127	snd_soc_dapm_nc_pin(&rtd->card->dapm, "Int Mic");
128#endif
129
130	return 0;
131}
132
133static struct snd_soc_dai_link at91sam9g20ek_dai = {
134	.name = "WM8731",
135	.stream_name = "WM8731 PCM",
136	.cpu_dai_name = "at91rm9200_ssc.0",
137	.codec_dai_name = "wm8731-hifi",
138	.init = at91sam9g20ek_wm8731_init,
139	.platform_name = "at91rm9200_ssc.0",
140	.codec_name = "wm8731.0-001b",
141	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
142		   SND_SOC_DAIFMT_CBM_CFM,
143};
144
145static struct snd_soc_card snd_soc_at91sam9g20ek = {
146	.name = "AT91SAMG20-EK",
147	.owner = THIS_MODULE,
148	.dai_link = &at91sam9g20ek_dai,
149	.num_links = 1,
150	.set_bias_level = at91sam9g20ek_set_bias_level,
151
152	.dapm_widgets = at91sam9g20ek_dapm_widgets,
153	.num_dapm_widgets = ARRAY_SIZE(at91sam9g20ek_dapm_widgets),
154	.dapm_routes = intercon,
155	.num_dapm_routes = ARRAY_SIZE(intercon),
156	.fully_routed = true,
157};
158
159static int at91sam9g20ek_audio_probe(struct platform_device *pdev)
160{
161	struct device_node *np = pdev->dev.of_node;
162	struct device_node *codec_np, *cpu_np;
163	struct clk *pllb;
164	struct snd_soc_card *card = &snd_soc_at91sam9g20ek;
165	int ret;
166
167	if (!np) {
168		return -ENODEV;
169	}
170
171	ret = atmel_ssc_set_audio(0);
172	if (ret) {
173		dev_err(&pdev->dev, "ssc channel is not valid\n");
174		return -EINVAL;
175	}
176
177	/*
178	 * Codec MCLK is supplied by PCK0 - set it up.
179	 */
180	mclk = clk_get(NULL, "pck0");
181	if (IS_ERR(mclk)) {
182		printk(KERN_ERR "ASoC: Failed to get MCLK\n");
183		ret = PTR_ERR(mclk);
184		goto err;
185	}
186
187	pllb = clk_get(NULL, "pllb");
188	if (IS_ERR(pllb)) {
189		printk(KERN_ERR "ASoC: Failed to get PLLB\n");
190		ret = PTR_ERR(pllb);
191		goto err_mclk;
192	}
193	ret = clk_set_parent(mclk, pllb);
194	clk_put(pllb);
195	if (ret != 0) {
196		printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
197		goto err_mclk;
198	}
199
200	clk_set_rate(mclk, MCLK_RATE);
201
202	card->dev = &pdev->dev;
203
204	/* Parse device node info */
205	ret = snd_soc_of_parse_card_name(card, "atmel,model");
206	if (ret)
207		goto err;
208
209	ret = snd_soc_of_parse_audio_routing(card,
210		"atmel,audio-routing");
211	if (ret)
212		goto err;
213
214	/* Parse codec info */
215	at91sam9g20ek_dai.codec_name = NULL;
216	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
217	if (!codec_np) {
218		dev_err(&pdev->dev, "codec info missing\n");
219		return -EINVAL;
220	}
221	at91sam9g20ek_dai.codec_of_node = codec_np;
222
223	/* Parse dai and platform info */
224	at91sam9g20ek_dai.cpu_dai_name = NULL;
225	at91sam9g20ek_dai.platform_name = NULL;
226	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
227	if (!cpu_np) {
228		dev_err(&pdev->dev, "dai and pcm info missing\n");
229		return -EINVAL;
230	}
231	at91sam9g20ek_dai.cpu_of_node = cpu_np;
232	at91sam9g20ek_dai.platform_of_node = cpu_np;
233
234	of_node_put(codec_np);
235	of_node_put(cpu_np);
236
237	ret = snd_soc_register_card(card);
238	if (ret) {
239		printk(KERN_ERR "ASoC: snd_soc_register_card() failed\n");
240	}
241
242	return ret;
243
244err_mclk:
245	clk_put(mclk);
246	mclk = NULL;
247err:
248	atmel_ssc_put_audio(0);
249	return ret;
250}
251
252static int at91sam9g20ek_audio_remove(struct platform_device *pdev)
253{
254	struct snd_soc_card *card = platform_get_drvdata(pdev);
255
256	clk_disable(mclk);
257	mclk = NULL;
258	snd_soc_unregister_card(card);
259	atmel_ssc_put_audio(0);
260
261	return 0;
262}
263
264#ifdef CONFIG_OF
265static const struct of_device_id at91sam9g20ek_wm8731_dt_ids[] = {
266	{ .compatible = "atmel,at91sam9g20ek-wm8731-audio", },
267	{ }
268};
269MODULE_DEVICE_TABLE(of, at91sam9g20ek_wm8731_dt_ids);
270#endif
271
272static struct platform_driver at91sam9g20ek_audio_driver = {
273	.driver = {
274		.name	= "at91sam9g20ek-audio",
275		.of_match_table = of_match_ptr(at91sam9g20ek_wm8731_dt_ids),
276	},
277	.probe	= at91sam9g20ek_audio_probe,
278	.remove	= at91sam9g20ek_audio_remove,
279};
280
281module_platform_driver(at91sam9g20ek_audio_driver);
282
283/* Module information */
284MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
285MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
286MODULE_ALIAS("platform:at91sam9g20ek-audio");
287MODULE_LICENSE("GPL");
288