xref: /linux/sound/soc/atmel/sam9g20_wm8731.c (revision 4a8cf938d5b6ee22c5a0bec84efb4b8068410ff4)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
25b99e6ccSSedji Gaouaou /*
35b99e6ccSSedji Gaouaou  * sam9g20_wm8731  --  SoC audio for AT91SAM9G20-based
45b99e6ccSSedji Gaouaou  * 			ATMEL AT91SAM9G20ek board.
55b99e6ccSSedji Gaouaou  *
65b99e6ccSSedji Gaouaou  *  Copyright (C) 2005 SAN People
75b99e6ccSSedji Gaouaou  *  Copyright (C) 2008 Atmel
85b99e6ccSSedji Gaouaou  *
95b99e6ccSSedji Gaouaou  * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
105b99e6ccSSedji Gaouaou  *
115b99e6ccSSedji Gaouaou  * Based on ati_b1_wm8731.c by:
125b99e6ccSSedji Gaouaou  * Frank Mandarino <fmandarino@endrelia.com>
135b99e6ccSSedji Gaouaou  * Copyright 2006 Endrelia Technologies Inc.
145b99e6ccSSedji Gaouaou  * Based on corgi.c by:
155b99e6ccSSedji Gaouaou  * Copyright 2005 Wolfson Microelectronics PLC.
165b99e6ccSSedji Gaouaou  * Copyright 2005 Openedhand Ltd.
175b99e6ccSSedji Gaouaou  */
185b99e6ccSSedji Gaouaou 
195b99e6ccSSedji Gaouaou #include <linux/module.h>
205b99e6ccSSedji Gaouaou #include <linux/moduleparam.h>
215b99e6ccSSedji Gaouaou #include <linux/kernel.h>
225b99e6ccSSedji Gaouaou #include <linux/clk.h>
235b99e6ccSSedji Gaouaou #include <linux/timer.h>
245b99e6ccSSedji Gaouaou #include <linux/interrupt.h>
255b99e6ccSSedji Gaouaou #include <linux/platform_device.h>
265998102bSMark Brown #include <linux/i2c.h>
278aa99652SSachin Kamat #include <linux/of.h>
285b99e6ccSSedji Gaouaou 
295b99e6ccSSedji Gaouaou #include <linux/atmel-ssc.h>
305b99e6ccSSedji Gaouaou 
315b99e6ccSSedji Gaouaou #include <sound/core.h>
325b99e6ccSSedji Gaouaou #include <sound/pcm.h>
335b99e6ccSSedji Gaouaou #include <sound/pcm_params.h>
345b99e6ccSSedji Gaouaou #include <sound/soc.h>
355b99e6ccSSedji Gaouaou 
365b99e6ccSSedji Gaouaou #include "../codecs/wm8731.h"
375b99e6ccSSedji Gaouaou #include "atmel-pcm.h"
385b99e6ccSSedji Gaouaou #include "atmel_ssc_dai.h"
395b99e6ccSSedji Gaouaou 
405de7f9b2SMark Brown #define MCLK_RATE 12000000
415de7f9b2SMark Brown 
4222df8eb4SMark Brown /*
4322df8eb4SMark Brown  * As shipped the board does not have inputs.  However, it is relatively
4422df8eb4SMark Brown  * straightforward to modify the board to hook them up so support is left
4522df8eb4SMark Brown  * in the driver.
4622df8eb4SMark Brown  */
4722df8eb4SMark Brown #undef ENABLE_MIC_INPUT
4822df8eb4SMark Brown 
495de7f9b2SMark Brown static struct clk *mclk;
505b99e6ccSSedji Gaouaou 
515de7f9b2SMark Brown static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
52d4c6005fSMark Brown 					struct snd_soc_dapm_context *dapm,
535de7f9b2SMark Brown 					enum snd_soc_bias_level level)
545de7f9b2SMark Brown {
555de7f9b2SMark Brown 	static int mclk_on;
565de7f9b2SMark Brown 	int ret = 0;
575de7f9b2SMark Brown 
585de7f9b2SMark Brown 	switch (level) {
595de7f9b2SMark Brown 	case SND_SOC_BIAS_ON:
605de7f9b2SMark Brown 	case SND_SOC_BIAS_PREPARE:
615de7f9b2SMark Brown 		if (!mclk_on)
625de7f9b2SMark Brown 			ret = clk_enable(mclk);
635de7f9b2SMark Brown 		if (ret == 0)
645de7f9b2SMark Brown 			mclk_on = 1;
655de7f9b2SMark Brown 		break;
665de7f9b2SMark Brown 
675de7f9b2SMark Brown 	case SND_SOC_BIAS_OFF:
685de7f9b2SMark Brown 	case SND_SOC_BIAS_STANDBY:
695de7f9b2SMark Brown 		if (mclk_on)
705de7f9b2SMark Brown 			clk_disable(mclk);
715de7f9b2SMark Brown 		mclk_on = 0;
725de7f9b2SMark Brown 		break;
735de7f9b2SMark Brown 	}
745de7f9b2SMark Brown 
755de7f9b2SMark Brown 	return ret;
765de7f9b2SMark Brown }
775b99e6ccSSedji Gaouaou 
785b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
795b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_MIC("Int Mic", NULL),
805b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
815b99e6ccSSedji Gaouaou };
825b99e6ccSSedji Gaouaou 
835b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_route intercon[] = {
845b99e6ccSSedji Gaouaou 
85a1677e39SLars-Peter Clausen 	/* speaker connected to LHPOUT/RHPOUT */
865b99e6ccSSedji Gaouaou 	{"Ext Spk", NULL, "LHPOUT"},
87a1677e39SLars-Peter Clausen 	{"Ext Spk", NULL, "RHPOUT"},
885b99e6ccSSedji Gaouaou 
895b99e6ccSSedji Gaouaou 	/* mic is connected to Mic Jack, with WM8731 Mic Bias */
905b99e6ccSSedji Gaouaou 	{"MICIN", NULL, "Mic Bias"},
915b99e6ccSSedji Gaouaou 	{"Mic Bias", NULL, "Int Mic"},
925b99e6ccSSedji Gaouaou };
935b99e6ccSSedji Gaouaou 
945b99e6ccSSedji Gaouaou /*
955b99e6ccSSedji Gaouaou  * Logic for a wm8731 as connected on a at91sam9g20ek board.
965b99e6ccSSedji Gaouaou  */
97f0fba2adSLiam Girdwood static int at91sam9g20ek_wm8731_init(struct snd_soc_pcm_runtime *rtd)
985b99e6ccSSedji Gaouaou {
99b434d707SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
100edefc8f3SLadislav Michl 	struct device *dev = rtd->dev;
10196fd6d47SMark Brown 	int ret;
10296fd6d47SMark Brown 
103edefc8f3SLadislav Michl 	dev_dbg(dev, "%s called\n", __func__);
1045b99e6ccSSedji Gaouaou 
1056bb74a72SNicolas Ferre 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_MCLK,
10696fd6d47SMark Brown 				     MCLK_RATE, SND_SOC_CLOCK_IN);
10796fd6d47SMark Brown 	if (ret < 0) {
108edefc8f3SLadislav Michl 		dev_err(dev, "Failed to set WM8731 SYSCLK: %d\n", ret);
10996fd6d47SMark Brown 		return ret;
11096fd6d47SMark Brown 	}
11196fd6d47SMark Brown 
112b0ffe9b1SLars-Peter Clausen #ifndef ENABLE_MIC_INPUT
113b0ffe9b1SLars-Peter Clausen 	snd_soc_dapm_nc_pin(&rtd->card->dapm, "Int Mic");
11422df8eb4SMark Brown #endif
11522df8eb4SMark Brown 
1165b99e6ccSSedji Gaouaou 	return 0;
1175b99e6ccSSedji Gaouaou }
1185b99e6ccSSedji Gaouaou 
119d83923c5SKuninori Morimoto SND_SOC_DAILINK_DEFS(pcm,
120d83923c5SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("at91rm9200_ssc.0")),
1210efd1d7cSKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
1220efd1d7cSKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_PLATFORM("at91rm9200_ssc.0")));
123d83923c5SKuninori Morimoto 
1245b99e6ccSSedji Gaouaou static struct snd_soc_dai_link at91sam9g20ek_dai = {
1255b99e6ccSSedji Gaouaou 	.name = "WM8731",
1265b99e6ccSSedji Gaouaou 	.stream_name = "WM8731 PCM",
1275b99e6ccSSedji Gaouaou 	.init = at91sam9g20ek_wm8731_init,
128059d3dc6SLars-Peter Clausen 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
129*4a8cf938SMark Brown 		   SND_SOC_DAIFMT_CBP_CFP,
130d83923c5SKuninori Morimoto 	SND_SOC_DAILINK_REG(pcm),
1315b99e6ccSSedji Gaouaou };
1325b99e6ccSSedji Gaouaou 
13387506549SMark Brown static struct snd_soc_card snd_soc_at91sam9g20ek = {
1347ee75380SMark Brown 	.name = "AT91SAMG20-EK",
135338d68dbSAxel Lin 	.owner = THIS_MODULE,
1365b99e6ccSSedji Gaouaou 	.dai_link = &at91sam9g20ek_dai,
1375b99e6ccSSedji Gaouaou 	.num_links = 1,
1385de7f9b2SMark Brown 	.set_bias_level = at91sam9g20ek_set_bias_level,
139b0ffe9b1SLars-Peter Clausen 
140b0ffe9b1SLars-Peter Clausen 	.dapm_widgets = at91sam9g20ek_dapm_widgets,
141b0ffe9b1SLars-Peter Clausen 	.num_dapm_widgets = ARRAY_SIZE(at91sam9g20ek_dapm_widgets),
142b0ffe9b1SLars-Peter Clausen 	.dapm_routes = intercon,
143b0ffe9b1SLars-Peter Clausen 	.num_dapm_routes = ARRAY_SIZE(intercon),
144a1677e39SLars-Peter Clausen 	.fully_routed = true,
1455b99e6ccSSedji Gaouaou };
1465b99e6ccSSedji Gaouaou 
14771d14ea6SBill Pemberton static int at91sam9g20ek_audio_probe(struct platform_device *pdev)
1485b99e6ccSSedji Gaouaou {
149531f67e4SBo Shen 	struct device_node *np = pdev->dev.of_node;
150531f67e4SBo Shen 	struct device_node *codec_np, *cpu_np;
1515de7f9b2SMark Brown 	struct clk *pllb;
152242b9bb8SBo Shen 	struct snd_soc_card *card = &snd_soc_at91sam9g20ek;
1535b99e6ccSSedji Gaouaou 	int ret;
1545b99e6ccSSedji Gaouaou 
155531f67e4SBo Shen 	if (!np) {
15640135ea0SMark Brown 		return -ENODEV;
157531f67e4SBo Shen 	}
15840135ea0SMark Brown 
1593310b57dSBo Shen 	ret = atmel_ssc_set_audio(0);
1603310b57dSBo Shen 	if (ret) {
1613310b57dSBo Shen 		dev_err(&pdev->dev, "ssc channel is not valid\n");
1623310b57dSBo Shen 		return -EINVAL;
1633310b57dSBo Shen 	}
1643310b57dSBo Shen 
1655b99e6ccSSedji Gaouaou 	/*
1665de7f9b2SMark Brown 	 * Codec MCLK is supplied by PCK0 - set it up.
1675de7f9b2SMark Brown 	 */
1685de7f9b2SMark Brown 	mclk = clk_get(NULL, "pck0");
1695de7f9b2SMark Brown 	if (IS_ERR(mclk)) {
170edefc8f3SLadislav Michl 		dev_err(&pdev->dev, "Failed to get MCLK\n");
1715de7f9b2SMark Brown 		ret = PTR_ERR(mclk);
1725de7f9b2SMark Brown 		goto err;
1735de7f9b2SMark Brown 	}
1745de7f9b2SMark Brown 
1755de7f9b2SMark Brown 	pllb = clk_get(NULL, "pllb");
176b3915d1fSVasiliy Kulikov 	if (IS_ERR(pllb)) {
177edefc8f3SLadislav Michl 		dev_err(&pdev->dev, "Failed to get PLLB\n");
178b3915d1fSVasiliy Kulikov 		ret = PTR_ERR(pllb);
1795de7f9b2SMark Brown 		goto err_mclk;
1805de7f9b2SMark Brown 	}
1815de7f9b2SMark Brown 	ret = clk_set_parent(mclk, pllb);
1825de7f9b2SMark Brown 	clk_put(pllb);
1835de7f9b2SMark Brown 	if (ret != 0) {
184edefc8f3SLadislav Michl 		dev_err(&pdev->dev, "Failed to set MCLK parent\n");
1855de7f9b2SMark Brown 		goto err_mclk;
1865de7f9b2SMark Brown 	}
1875de7f9b2SMark Brown 
1885de7f9b2SMark Brown 	clk_set_rate(mclk, MCLK_RATE);
1895de7f9b2SMark Brown 
190242b9bb8SBo Shen 	card->dev = &pdev->dev;
191531f67e4SBo Shen 
192531f67e4SBo Shen 	/* Parse device node info */
193531f67e4SBo Shen 	ret = snd_soc_of_parse_card_name(card, "atmel,model");
194531f67e4SBo Shen 	if (ret)
195531f67e4SBo Shen 		goto err;
196531f67e4SBo Shen 
197531f67e4SBo Shen 	ret = snd_soc_of_parse_audio_routing(card,
198531f67e4SBo Shen 		"atmel,audio-routing");
199531f67e4SBo Shen 	if (ret)
200531f67e4SBo Shen 		goto err;
201531f67e4SBo Shen 
202531f67e4SBo Shen 	/* Parse codec info */
203d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.codecs->name = NULL;
204531f67e4SBo Shen 	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
205531f67e4SBo Shen 	if (!codec_np) {
206531f67e4SBo Shen 		dev_err(&pdev->dev, "codec info missing\n");
207531f67e4SBo Shen 		return -EINVAL;
208531f67e4SBo Shen 	}
209d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.codecs->of_node = codec_np;
210531f67e4SBo Shen 
211531f67e4SBo Shen 	/* Parse dai and platform info */
212d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.cpus->dai_name = NULL;
2130efd1d7cSKuninori Morimoto 	at91sam9g20ek_dai.platforms->name = NULL;
214531f67e4SBo Shen 	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
215531f67e4SBo Shen 	if (!cpu_np) {
216531f67e4SBo Shen 		dev_err(&pdev->dev, "dai and pcm info missing\n");
217531f67e4SBo Shen 		return -EINVAL;
218531f67e4SBo Shen 	}
219d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.cpus->of_node = cpu_np;
2200efd1d7cSKuninori Morimoto 	at91sam9g20ek_dai.platforms->of_node = cpu_np;
221531f67e4SBo Shen 
222531f67e4SBo Shen 	of_node_put(codec_np);
223531f67e4SBo Shen 	of_node_put(cpu_np);
224531f67e4SBo Shen 
225242b9bb8SBo Shen 	ret = snd_soc_register_card(card);
2265b99e6ccSSedji Gaouaou 	if (ret) {
227edefc8f3SLadislav Michl 		dev_err(&pdev->dev, "snd_soc_register_card() failed\n");
2285b99e6ccSSedji Gaouaou 	}
2295b99e6ccSSedji Gaouaou 
2305b99e6ccSSedji Gaouaou 	return ret;
2315b99e6ccSSedji Gaouaou 
2325de7f9b2SMark Brown err_mclk:
2335de7f9b2SMark Brown 	clk_put(mclk);
2345de7f9b2SMark Brown 	mclk = NULL;
2355de7f9b2SMark Brown err:
2363310b57dSBo Shen 	atmel_ssc_put_audio(0);
2375b99e6ccSSedji Gaouaou 	return ret;
2385b99e6ccSSedji Gaouaou }
2395b99e6ccSSedji Gaouaou 
24071d14ea6SBill Pemberton static int at91sam9g20ek_audio_remove(struct platform_device *pdev)
2415b99e6ccSSedji Gaouaou {
242242b9bb8SBo Shen 	struct snd_soc_card *card = platform_get_drvdata(pdev);
243242b9bb8SBo Shen 
244151edfc7SBo Shen 	clk_disable(mclk);
2455de7f9b2SMark Brown 	mclk = NULL;
246151edfc7SBo Shen 	snd_soc_unregister_card(card);
247151edfc7SBo Shen 	atmel_ssc_put_audio(0);
248242b9bb8SBo Shen 
249242b9bb8SBo Shen 	return 0;
2505b99e6ccSSedji Gaouaou }
2515b99e6ccSSedji Gaouaou 
252531f67e4SBo Shen #ifdef CONFIG_OF
253531f67e4SBo Shen static const struct of_device_id at91sam9g20ek_wm8731_dt_ids[] = {
254531f67e4SBo Shen 	{ .compatible = "atmel,at91sam9g20ek-wm8731-audio", },
255531f67e4SBo Shen 	{ }
256531f67e4SBo Shen };
257531f67e4SBo Shen MODULE_DEVICE_TABLE(of, at91sam9g20ek_wm8731_dt_ids);
258531f67e4SBo Shen #endif
259531f67e4SBo Shen 
260242b9bb8SBo Shen static struct platform_driver at91sam9g20ek_audio_driver = {
261242b9bb8SBo Shen 	.driver = {
262242b9bb8SBo Shen 		.name	= "at91sam9g20ek-audio",
263531f67e4SBo Shen 		.of_match_table = of_match_ptr(at91sam9g20ek_wm8731_dt_ids),
264242b9bb8SBo Shen 	},
265242b9bb8SBo Shen 	.probe	= at91sam9g20ek_audio_probe,
26671d14ea6SBill Pemberton 	.remove	= at91sam9g20ek_audio_remove,
267242b9bb8SBo Shen };
268242b9bb8SBo Shen 
269242b9bb8SBo Shen module_platform_driver(at91sam9g20ek_audio_driver);
2705b99e6ccSSedji Gaouaou 
2715b99e6ccSSedji Gaouaou /* Module information */
2725b99e6ccSSedji Gaouaou MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
2735b99e6ccSSedji Gaouaou MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
274242b9bb8SBo Shen MODULE_ALIAS("platform:at91sam9g20ek-audio");
2755b99e6ccSSedji Gaouaou MODULE_LICENSE("GPL");
276