xref: /linux/sound/soc/atmel/sam9g20_wm8731.c (revision 7ee753804185eb0a46ac964fd6a6564bd67290c9)
15b99e6ccSSedji Gaouaou /*
25b99e6ccSSedji Gaouaou  * sam9g20_wm8731  --  SoC audio for AT91SAM9G20-based
35b99e6ccSSedji Gaouaou  * 			ATMEL AT91SAM9G20ek board.
45b99e6ccSSedji Gaouaou  *
55b99e6ccSSedji Gaouaou  *  Copyright (C) 2005 SAN People
65b99e6ccSSedji Gaouaou  *  Copyright (C) 2008 Atmel
75b99e6ccSSedji Gaouaou  *
85b99e6ccSSedji Gaouaou  * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
95b99e6ccSSedji Gaouaou  *
105b99e6ccSSedji Gaouaou  * Based on ati_b1_wm8731.c by:
115b99e6ccSSedji Gaouaou  * Frank Mandarino <fmandarino@endrelia.com>
125b99e6ccSSedji Gaouaou  * Copyright 2006 Endrelia Technologies Inc.
135b99e6ccSSedji Gaouaou  * Based on corgi.c by:
145b99e6ccSSedji Gaouaou  * Copyright 2005 Wolfson Microelectronics PLC.
155b99e6ccSSedji Gaouaou  * Copyright 2005 Openedhand Ltd.
165b99e6ccSSedji Gaouaou  *
175b99e6ccSSedji Gaouaou  * This program is free software; you can redistribute it and/or modify
185b99e6ccSSedji Gaouaou  * it under the terms of the GNU General Public License as published by
195b99e6ccSSedji Gaouaou  * the Free Software Foundation; either version 2 of the License, or
205b99e6ccSSedji Gaouaou  * (at your option) any later version.
215b99e6ccSSedji Gaouaou  *
225b99e6ccSSedji Gaouaou  * This program is distributed in the hope that it will be useful,
235b99e6ccSSedji Gaouaou  * but WITHOUT ANY WARRANTY; without even the implied warranty of
245b99e6ccSSedji Gaouaou  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
255b99e6ccSSedji Gaouaou  * GNU General Public License for more details.
265b99e6ccSSedji Gaouaou  *
275b99e6ccSSedji Gaouaou  * You should have received a copy of the GNU General Public License
285b99e6ccSSedji Gaouaou  * along with this program; if not, write to the Free Software
295b99e6ccSSedji Gaouaou  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
305b99e6ccSSedji Gaouaou  */
315b99e6ccSSedji Gaouaou 
325b99e6ccSSedji Gaouaou #include <linux/module.h>
335b99e6ccSSedji Gaouaou #include <linux/moduleparam.h>
345b99e6ccSSedji Gaouaou #include <linux/kernel.h>
355b99e6ccSSedji Gaouaou #include <linux/clk.h>
365b99e6ccSSedji Gaouaou #include <linux/timer.h>
375b99e6ccSSedji Gaouaou #include <linux/interrupt.h>
385b99e6ccSSedji Gaouaou #include <linux/platform_device.h>
395b99e6ccSSedji Gaouaou 
405b99e6ccSSedji Gaouaou #include <linux/atmel-ssc.h>
415b99e6ccSSedji Gaouaou 
425b99e6ccSSedji Gaouaou #include <sound/core.h>
435b99e6ccSSedji Gaouaou #include <sound/pcm.h>
445b99e6ccSSedji Gaouaou #include <sound/pcm_params.h>
455b99e6ccSSedji Gaouaou #include <sound/soc.h>
465b99e6ccSSedji Gaouaou #include <sound/soc-dapm.h>
475b99e6ccSSedji Gaouaou 
4840135ea0SMark Brown #include <asm/mach-types.h>
495b99e6ccSSedji Gaouaou #include <mach/hardware.h>
505b99e6ccSSedji Gaouaou #include <mach/gpio.h>
515b99e6ccSSedji Gaouaou 
525b99e6ccSSedji Gaouaou #include "../codecs/wm8731.h"
535b99e6ccSSedji Gaouaou #include "atmel-pcm.h"
545b99e6ccSSedji Gaouaou #include "atmel_ssc_dai.h"
555b99e6ccSSedji Gaouaou 
565de7f9b2SMark Brown #define MCLK_RATE 12000000
575de7f9b2SMark Brown 
585de7f9b2SMark Brown static struct clk *mclk;
595b99e6ccSSedji Gaouaou 
605b99e6ccSSedji Gaouaou static int at91sam9g20ek_startup(struct snd_pcm_substream *substream)
615b99e6ccSSedji Gaouaou {
625b99e6ccSSedji Gaouaou 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
635b99e6ccSSedji Gaouaou 	struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
645b99e6ccSSedji Gaouaou 	int ret;
655b99e6ccSSedji Gaouaou 
665b99e6ccSSedji Gaouaou 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK,
675de7f9b2SMark Brown 		MCLK_RATE, SND_SOC_CLOCK_IN);
685de7f9b2SMark Brown 	if (ret < 0) {
695de7f9b2SMark Brown 		clk_disable(mclk);
705b99e6ccSSedji Gaouaou 		return ret;
715de7f9b2SMark Brown 	}
725b99e6ccSSedji Gaouaou 
735b99e6ccSSedji Gaouaou 	return 0;
745b99e6ccSSedji Gaouaou }
755b99e6ccSSedji Gaouaou 
765b99e6ccSSedji Gaouaou static void at91sam9g20ek_shutdown(struct snd_pcm_substream *substream)
775b99e6ccSSedji Gaouaou {
785b99e6ccSSedji Gaouaou 	struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
795b99e6ccSSedji Gaouaou 
805b99e6ccSSedji Gaouaou 	dev_dbg(rtd->socdev->dev, "shutdown");
815b99e6ccSSedji Gaouaou }
825b99e6ccSSedji Gaouaou 
835b99e6ccSSedji Gaouaou static int at91sam9g20ek_hw_params(struct snd_pcm_substream *substream,
845b99e6ccSSedji Gaouaou 	struct snd_pcm_hw_params *params)
855b99e6ccSSedji Gaouaou {
865b99e6ccSSedji Gaouaou 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
875b99e6ccSSedji Gaouaou 	struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
885b99e6ccSSedji Gaouaou 	struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
895b99e6ccSSedji Gaouaou 	struct atmel_ssc_info *ssc_p = cpu_dai->private_data;
905b99e6ccSSedji Gaouaou 	struct ssc_device *ssc = ssc_p->ssc;
915b99e6ccSSedji Gaouaou 	int ret;
925b99e6ccSSedji Gaouaou 
935b99e6ccSSedji Gaouaou 	unsigned int rate;
945b99e6ccSSedji Gaouaou 	int cmr_div, period;
955b99e6ccSSedji Gaouaou 
965b99e6ccSSedji Gaouaou 	if (ssc == NULL) {
975b99e6ccSSedji Gaouaou 		printk(KERN_INFO "at91sam9g20ek_hw_params: ssc is NULL!\n");
985b99e6ccSSedji Gaouaou 		return -EINVAL;
995b99e6ccSSedji Gaouaou 	}
1005b99e6ccSSedji Gaouaou 
1015b99e6ccSSedji Gaouaou 	/* set codec DAI configuration */
1025b99e6ccSSedji Gaouaou 	ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
1035b99e6ccSSedji Gaouaou 		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
1045b99e6ccSSedji Gaouaou 	if (ret < 0)
1055b99e6ccSSedji Gaouaou 		return ret;
1065b99e6ccSSedji Gaouaou 
1075b99e6ccSSedji Gaouaou 	/* set cpu DAI configuration */
1085b99e6ccSSedji Gaouaou 	ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
1095b99e6ccSSedji Gaouaou 		SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
1105b99e6ccSSedji Gaouaou 	if (ret < 0)
1115b99e6ccSSedji Gaouaou 		return ret;
1125b99e6ccSSedji Gaouaou 
1135b99e6ccSSedji Gaouaou 	/*
1145b99e6ccSSedji Gaouaou 	 * The SSC clock dividers depend on the sample rate.  The CMR.DIV
1155b99e6ccSSedji Gaouaou 	 * field divides the system master clock MCK to drive the SSC TK
1165b99e6ccSSedji Gaouaou 	 * signal which provides the codec BCLK.  The TCMR.PERIOD and
1175b99e6ccSSedji Gaouaou 	 * RCMR.PERIOD fields further divide the BCLK signal to drive
1185b99e6ccSSedji Gaouaou 	 * the SSC TF and RF signals which provide the codec DACLRC and
1195b99e6ccSSedji Gaouaou 	 * ADCLRC clocks.
1205b99e6ccSSedji Gaouaou 	 *
1215b99e6ccSSedji Gaouaou 	 * The dividers were determined through trial and error, where a
1225b99e6ccSSedji Gaouaou 	 * CMR.DIV value is chosen such that the resulting BCLK value is
1235b99e6ccSSedji Gaouaou 	 * divisible, or almost divisible, by (2 * sample rate), and then
1245b99e6ccSSedji Gaouaou 	 * the TCMR.PERIOD or RCMR.PERIOD is BCLK / (2 * sample rate) - 1.
1255b99e6ccSSedji Gaouaou 	 */
1265b99e6ccSSedji Gaouaou 	rate = params_rate(params);
1275b99e6ccSSedji Gaouaou 
1285b99e6ccSSedji Gaouaou 	switch (rate) {
1295b99e6ccSSedji Gaouaou 	case 8000:
1305b99e6ccSSedji Gaouaou 		cmr_div = 55;	/* BCLK = 133MHz/(2*55) = 1.209MHz */
1315b99e6ccSSedji Gaouaou 		period = 74;	/* LRC = BCLK/(2*(74+1)) ~= 8060,6Hz */
1325b99e6ccSSedji Gaouaou 		break;
1335b99e6ccSSedji Gaouaou 	case 11025:
1345b99e6ccSSedji Gaouaou 		cmr_div = 67;	/* BCLK = 133MHz/(2*60) = 1.108MHz */
1355b99e6ccSSedji Gaouaou 		period = 45;	/* LRC = BCLK/(2*(49+1)) = 11083,3Hz */
1365b99e6ccSSedji Gaouaou 		break;
1375b99e6ccSSedji Gaouaou 	case 16000:
1385b99e6ccSSedji Gaouaou 		cmr_div = 63;	/* BCLK = 133MHz/(2*63) = 1.055MHz */
1395b99e6ccSSedji Gaouaou 		period = 32;	/* LRC = BCLK/(2*(32+1)) = 15993,2Hz */
1405b99e6ccSSedji Gaouaou 		break;
1415b99e6ccSSedji Gaouaou 	case 22050:
1425b99e6ccSSedji Gaouaou 		cmr_div = 52;	/* BCLK = 133MHz/(2*52) = 1.278MHz */
1435b99e6ccSSedji Gaouaou 		period = 28;	/* LRC = BCLK/(2*(28+1)) = 22049Hz */
1445b99e6ccSSedji Gaouaou 		break;
1455b99e6ccSSedji Gaouaou 	case 32000:
1465b99e6ccSSedji Gaouaou 		cmr_div = 66;	/* BCLK = 133MHz/(2*66) = 1.007MHz */
1475b99e6ccSSedji Gaouaou 		period = 15;	/* LRC = BCLK/(2*(15+1)) = 31486,742Hz */
1485b99e6ccSSedji Gaouaou 		break;
1495b99e6ccSSedji Gaouaou 	case 44100:
1505b99e6ccSSedji Gaouaou 		cmr_div = 29;	/* BCLK = 133MHz/(2*29) = 2.293MHz */
1515b99e6ccSSedji Gaouaou 		period = 25;	/* LRC = BCLK/(2*(25+1)) = 44098Hz */
1525b99e6ccSSedji Gaouaou 		break;
1535b99e6ccSSedji Gaouaou 	case 48000:
1545b99e6ccSSedji Gaouaou 		cmr_div = 33;	/* BCLK = 133MHz/(2*33) = 2.015MHz */
1555b99e6ccSSedji Gaouaou 		period = 20;	/* LRC = BCLK/(2*(20+1)) = 47979,79Hz */
1565b99e6ccSSedji Gaouaou 		break;
1575b99e6ccSSedji Gaouaou 	case 88200:
1585b99e6ccSSedji Gaouaou 		cmr_div = 29;	/* BCLK = 133MHz/(2*29) = 2.293MHz */
1595b99e6ccSSedji Gaouaou 		period = 12;	/* LRC = BCLK/(2*(12+1)) = 88196Hz */
1605b99e6ccSSedji Gaouaou 		break;
1615b99e6ccSSedji Gaouaou 	case 96000:
1625b99e6ccSSedji Gaouaou 		cmr_div = 23;	/* BCLK = 133MHz/(2*23) = 2.891MHz */
1635b99e6ccSSedji Gaouaou 		period = 14;	/* LRC = BCLK/(2*(14+1)) = 96376Hz */
1645b99e6ccSSedji Gaouaou 		break;
1655b99e6ccSSedji Gaouaou 	default:
1665b99e6ccSSedji Gaouaou 		printk(KERN_WARNING "unsupported rate %d"
1675b99e6ccSSedji Gaouaou 				" on at91sam9g20ek board\n", rate);
1685b99e6ccSSedji Gaouaou 		return -EINVAL;
1695b99e6ccSSedji Gaouaou 	}
1705b99e6ccSSedji Gaouaou 
1715b99e6ccSSedji Gaouaou 	/* set the MCK divider for BCLK */
1725b99e6ccSSedji Gaouaou 	ret = snd_soc_dai_set_clkdiv(cpu_dai, ATMEL_SSC_CMR_DIV, cmr_div);
1735b99e6ccSSedji Gaouaou 	if (ret < 0)
1745b99e6ccSSedji Gaouaou 		return ret;
1755b99e6ccSSedji Gaouaou 
1765b99e6ccSSedji Gaouaou 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1775b99e6ccSSedji Gaouaou 		/* set the BCLK divider for DACLRC */
1785b99e6ccSSedji Gaouaou 		ret = snd_soc_dai_set_clkdiv(cpu_dai,
1795b99e6ccSSedji Gaouaou 						ATMEL_SSC_TCMR_PERIOD, period);
1805b99e6ccSSedji Gaouaou 	} else {
1815b99e6ccSSedji Gaouaou 		/* set the BCLK divider for ADCLRC */
1825b99e6ccSSedji Gaouaou 		ret = snd_soc_dai_set_clkdiv(cpu_dai,
1835b99e6ccSSedji Gaouaou 						ATMEL_SSC_RCMR_PERIOD, period);
1845b99e6ccSSedji Gaouaou 	}
1855b99e6ccSSedji Gaouaou 	if (ret < 0)
1865b99e6ccSSedji Gaouaou 		return ret;
1875b99e6ccSSedji Gaouaou 
1885b99e6ccSSedji Gaouaou 	return 0;
1895b99e6ccSSedji Gaouaou }
1905b99e6ccSSedji Gaouaou 
1915b99e6ccSSedji Gaouaou static struct snd_soc_ops at91sam9g20ek_ops = {
1925b99e6ccSSedji Gaouaou 	.startup = at91sam9g20ek_startup,
1935b99e6ccSSedji Gaouaou 	.hw_params = at91sam9g20ek_hw_params,
1945b99e6ccSSedji Gaouaou 	.shutdown = at91sam9g20ek_shutdown,
1955b99e6ccSSedji Gaouaou };
1965b99e6ccSSedji Gaouaou 
1975de7f9b2SMark Brown static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
1985de7f9b2SMark Brown 					enum snd_soc_bias_level level)
1995de7f9b2SMark Brown {
2005de7f9b2SMark Brown 	static int mclk_on;
2015de7f9b2SMark Brown 	int ret = 0;
2025de7f9b2SMark Brown 
2035de7f9b2SMark Brown 	switch (level) {
2045de7f9b2SMark Brown 	case SND_SOC_BIAS_ON:
2055de7f9b2SMark Brown 	case SND_SOC_BIAS_PREPARE:
2065de7f9b2SMark Brown 		if (!mclk_on)
2075de7f9b2SMark Brown 			ret = clk_enable(mclk);
2085de7f9b2SMark Brown 		if (ret == 0)
2095de7f9b2SMark Brown 			mclk_on = 1;
2105de7f9b2SMark Brown 		break;
2115de7f9b2SMark Brown 
2125de7f9b2SMark Brown 	case SND_SOC_BIAS_OFF:
2135de7f9b2SMark Brown 	case SND_SOC_BIAS_STANDBY:
2145de7f9b2SMark Brown 		if (mclk_on)
2155de7f9b2SMark Brown 			clk_disable(mclk);
2165de7f9b2SMark Brown 		mclk_on = 0;
2175de7f9b2SMark Brown 		break;
2185de7f9b2SMark Brown 	}
2195de7f9b2SMark Brown 
2205de7f9b2SMark Brown 	return ret;
2215de7f9b2SMark Brown }
2225b99e6ccSSedji Gaouaou 
2235b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
2245b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_MIC("Int Mic", NULL),
2255b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
2265b99e6ccSSedji Gaouaou };
2275b99e6ccSSedji Gaouaou 
2285b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_route intercon[] = {
2295b99e6ccSSedji Gaouaou 
2305b99e6ccSSedji Gaouaou 	/* speaker connected to LHPOUT */
2315b99e6ccSSedji Gaouaou 	{"Ext Spk", NULL, "LHPOUT"},
2325b99e6ccSSedji Gaouaou 
2335b99e6ccSSedji Gaouaou 	/* mic is connected to Mic Jack, with WM8731 Mic Bias */
2345b99e6ccSSedji Gaouaou 	{"MICIN", NULL, "Mic Bias"},
2355b99e6ccSSedji Gaouaou 	{"Mic Bias", NULL, "Int Mic"},
2365b99e6ccSSedji Gaouaou };
2375b99e6ccSSedji Gaouaou 
2385b99e6ccSSedji Gaouaou /*
2395b99e6ccSSedji Gaouaou  * Logic for a wm8731 as connected on a at91sam9g20ek board.
2405b99e6ccSSedji Gaouaou  */
2415b99e6ccSSedji Gaouaou static int at91sam9g20ek_wm8731_init(struct snd_soc_codec *codec)
2425b99e6ccSSedji Gaouaou {
2435b99e6ccSSedji Gaouaou 	printk(KERN_DEBUG
2445b99e6ccSSedji Gaouaou 			"at91sam9g20ek_wm8731 "
2455b99e6ccSSedji Gaouaou 			": at91sam9g20ek_wm8731_init() called\n");
2465b99e6ccSSedji Gaouaou 
2475b99e6ccSSedji Gaouaou 	/* Add specific widgets */
2485b99e6ccSSedji Gaouaou 	snd_soc_dapm_new_controls(codec, at91sam9g20ek_dapm_widgets,
2495b99e6ccSSedji Gaouaou 				  ARRAY_SIZE(at91sam9g20ek_dapm_widgets));
2505b99e6ccSSedji Gaouaou 	/* Set up specific audio path interconnects */
2515b99e6ccSSedji Gaouaou 	snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
2525b99e6ccSSedji Gaouaou 
2535b99e6ccSSedji Gaouaou 	/* not connected */
2548c0bad7fSMark Brown 	snd_soc_dapm_nc_pin(codec, "RLINEIN");
2558c0bad7fSMark Brown 	snd_soc_dapm_nc_pin(codec, "LLINEIN");
2565b99e6ccSSedji Gaouaou 
2575b99e6ccSSedji Gaouaou 	/* always connected */
2585b99e6ccSSedji Gaouaou 	snd_soc_dapm_enable_pin(codec, "Int Mic");
2595b99e6ccSSedji Gaouaou 	snd_soc_dapm_enable_pin(codec, "Ext Spk");
2605b99e6ccSSedji Gaouaou 
2615b99e6ccSSedji Gaouaou 	snd_soc_dapm_sync(codec);
2625b99e6ccSSedji Gaouaou 
2635b99e6ccSSedji Gaouaou 	return 0;
2645b99e6ccSSedji Gaouaou }
2655b99e6ccSSedji Gaouaou 
2665b99e6ccSSedji Gaouaou static struct snd_soc_dai_link at91sam9g20ek_dai = {
2675b99e6ccSSedji Gaouaou 	.name = "WM8731",
2685b99e6ccSSedji Gaouaou 	.stream_name = "WM8731 PCM",
2695b99e6ccSSedji Gaouaou 	.cpu_dai = &atmel_ssc_dai[0],
2705b99e6ccSSedji Gaouaou 	.codec_dai = &wm8731_dai,
2715b99e6ccSSedji Gaouaou 	.init = at91sam9g20ek_wm8731_init,
2725b99e6ccSSedji Gaouaou 	.ops = &at91sam9g20ek_ops,
2735b99e6ccSSedji Gaouaou };
2745b99e6ccSSedji Gaouaou 
27587506549SMark Brown static struct snd_soc_card snd_soc_at91sam9g20ek = {
276*7ee75380SMark Brown 	.name = "AT91SAMG20-EK",
27787689d56SMark Brown 	.platform = &atmel_soc_platform,
2785b99e6ccSSedji Gaouaou 	.dai_link = &at91sam9g20ek_dai,
2795b99e6ccSSedji Gaouaou 	.num_links = 1,
2805de7f9b2SMark Brown 	.set_bias_level = at91sam9g20ek_set_bias_level,
2815b99e6ccSSedji Gaouaou };
2825b99e6ccSSedji Gaouaou 
2835b99e6ccSSedji Gaouaou static struct wm8731_setup_data at91sam9g20ek_wm8731_setup = {
2845b99e6ccSSedji Gaouaou 	.i2c_bus = 0,
2855b99e6ccSSedji Gaouaou 	.i2c_address = 0x1b,
2865b99e6ccSSedji Gaouaou };
2875b99e6ccSSedji Gaouaou 
2885b99e6ccSSedji Gaouaou static struct snd_soc_device at91sam9g20ek_snd_devdata = {
28987506549SMark Brown 	.card = &snd_soc_at91sam9g20ek,
2905b99e6ccSSedji Gaouaou 	.codec_dev = &soc_codec_dev_wm8731,
2915b99e6ccSSedji Gaouaou 	.codec_data = &at91sam9g20ek_wm8731_setup,
2925b99e6ccSSedji Gaouaou };
2935b99e6ccSSedji Gaouaou 
2945b99e6ccSSedji Gaouaou static struct platform_device *at91sam9g20ek_snd_device;
2955b99e6ccSSedji Gaouaou 
2965b99e6ccSSedji Gaouaou static int __init at91sam9g20ek_init(void)
2975b99e6ccSSedji Gaouaou {
2985b99e6ccSSedji Gaouaou 	struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
2995b99e6ccSSedji Gaouaou 	struct ssc_device *ssc = NULL;
3005de7f9b2SMark Brown 	struct clk *pllb;
3015b99e6ccSSedji Gaouaou 	int ret;
3025b99e6ccSSedji Gaouaou 
30340135ea0SMark Brown 	if (!machine_is_at91sam9g20ek())
30440135ea0SMark Brown 		return -ENODEV;
30540135ea0SMark Brown 
3065b99e6ccSSedji Gaouaou 	/*
3075de7f9b2SMark Brown 	 * Codec MCLK is supplied by PCK0 - set it up.
3085de7f9b2SMark Brown 	 */
3095de7f9b2SMark Brown 	mclk = clk_get(NULL, "pck0");
3105de7f9b2SMark Brown 	if (IS_ERR(mclk)) {
3115de7f9b2SMark Brown 		printk(KERN_ERR "ASoC: Failed to get MCLK\n");
3125de7f9b2SMark Brown 		ret = PTR_ERR(mclk);
3135de7f9b2SMark Brown 		goto err;
3145de7f9b2SMark Brown 	}
3155de7f9b2SMark Brown 
3165de7f9b2SMark Brown 	pllb = clk_get(NULL, "pllb");
3175de7f9b2SMark Brown 	if (IS_ERR(mclk)) {
3185de7f9b2SMark Brown 		printk(KERN_ERR "ASoC: Failed to get PLLB\n");
3195de7f9b2SMark Brown 		ret = PTR_ERR(mclk);
3205de7f9b2SMark Brown 		goto err_mclk;
3215de7f9b2SMark Brown 	}
3225de7f9b2SMark Brown 	ret = clk_set_parent(mclk, pllb);
3235de7f9b2SMark Brown 	clk_put(pllb);
3245de7f9b2SMark Brown 	if (ret != 0) {
3255de7f9b2SMark Brown 		printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
3265de7f9b2SMark Brown 		goto err_mclk;
3275de7f9b2SMark Brown 	}
3285de7f9b2SMark Brown 
3295de7f9b2SMark Brown 	clk_set_rate(mclk, MCLK_RATE);
3305de7f9b2SMark Brown 
3315de7f9b2SMark Brown 	/*
3325b99e6ccSSedji Gaouaou 	 * Request SSC device
3335b99e6ccSSedji Gaouaou 	 */
3345b99e6ccSSedji Gaouaou 	ssc = ssc_request(0);
3355b99e6ccSSedji Gaouaou 	if (IS_ERR(ssc)) {
336d6943541SMark Brown 		printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
3375b99e6ccSSedji Gaouaou 		ret = PTR_ERR(ssc);
3385b99e6ccSSedji Gaouaou 		ssc = NULL;
3395b99e6ccSSedji Gaouaou 		goto err_ssc;
3405b99e6ccSSedji Gaouaou 	}
3415b99e6ccSSedji Gaouaou 	ssc_p->ssc = ssc;
3425b99e6ccSSedji Gaouaou 
3435b99e6ccSSedji Gaouaou 	at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
3445b99e6ccSSedji Gaouaou 	if (!at91sam9g20ek_snd_device) {
345d6943541SMark Brown 		printk(KERN_ERR "ASoC: Platform device allocation failed\n");
3465b99e6ccSSedji Gaouaou 		ret = -ENOMEM;
3475b99e6ccSSedji Gaouaou 	}
3485b99e6ccSSedji Gaouaou 
3495b99e6ccSSedji Gaouaou 	platform_set_drvdata(at91sam9g20ek_snd_device,
3505b99e6ccSSedji Gaouaou 			&at91sam9g20ek_snd_devdata);
3515b99e6ccSSedji Gaouaou 	at91sam9g20ek_snd_devdata.dev = &at91sam9g20ek_snd_device->dev;
3525b99e6ccSSedji Gaouaou 
3535b99e6ccSSedji Gaouaou 	ret = platform_device_add(at91sam9g20ek_snd_device);
3545b99e6ccSSedji Gaouaou 	if (ret) {
355d6943541SMark Brown 		printk(KERN_ERR "ASoC: Platform device allocation failed\n");
3565b99e6ccSSedji Gaouaou 		platform_device_put(at91sam9g20ek_snd_device);
3575b99e6ccSSedji Gaouaou 	}
3585b99e6ccSSedji Gaouaou 
3595b99e6ccSSedji Gaouaou 	return ret;
3605b99e6ccSSedji Gaouaou 
3615b99e6ccSSedji Gaouaou err_ssc:
3625de7f9b2SMark Brown err_mclk:
3635de7f9b2SMark Brown 	clk_put(mclk);
3645de7f9b2SMark Brown 	mclk = NULL;
3655de7f9b2SMark Brown err:
3665b99e6ccSSedji Gaouaou 	return ret;
3675b99e6ccSSedji Gaouaou }
3685b99e6ccSSedji Gaouaou 
3695b99e6ccSSedji Gaouaou static void __exit at91sam9g20ek_exit(void)
3705b99e6ccSSedji Gaouaou {
3715b99e6ccSSedji Gaouaou 	struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
3725b99e6ccSSedji Gaouaou 	struct ssc_device *ssc;
3735b99e6ccSSedji Gaouaou 
3745b99e6ccSSedji Gaouaou 	if (ssc_p != NULL) {
3755b99e6ccSSedji Gaouaou 		ssc = ssc_p->ssc;
3765b99e6ccSSedji Gaouaou 		if (ssc != NULL)
3775b99e6ccSSedji Gaouaou 			ssc_free(ssc);
3785b99e6ccSSedji Gaouaou 		ssc_p->ssc = NULL;
3795b99e6ccSSedji Gaouaou 	}
3805b99e6ccSSedji Gaouaou 
3815b99e6ccSSedji Gaouaou 	platform_device_unregister(at91sam9g20ek_snd_device);
3825b99e6ccSSedji Gaouaou 	at91sam9g20ek_snd_device = NULL;
3835de7f9b2SMark Brown 	clk_put(mclk);
3845de7f9b2SMark Brown 	mclk = NULL;
3855b99e6ccSSedji Gaouaou }
3865b99e6ccSSedji Gaouaou 
3875b99e6ccSSedji Gaouaou module_init(at91sam9g20ek_init);
3885b99e6ccSSedji Gaouaou module_exit(at91sam9g20ek_exit);
3895b99e6ccSSedji Gaouaou 
3905b99e6ccSSedji Gaouaou /* Module information */
3915b99e6ccSSedji Gaouaou MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
3925b99e6ccSSedji Gaouaou MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
3935b99e6ccSSedji Gaouaou MODULE_LICENSE("GPL");
394