xref: /linux/sound/soc/atmel/sam9g20_wm8731.c (revision 6547effc3aea50cc3c60874f9a65a19f4919ef9d)
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>
268aa99652SSachin Kamat #include <linux/of.h>
275b99e6ccSSedji Gaouaou 
285b99e6ccSSedji Gaouaou #include <linux/atmel-ssc.h>
295b99e6ccSSedji Gaouaou 
305b99e6ccSSedji Gaouaou #include <sound/core.h>
315b99e6ccSSedji Gaouaou #include <sound/pcm.h>
325b99e6ccSSedji Gaouaou #include <sound/pcm_params.h>
335b99e6ccSSedji Gaouaou #include <sound/soc.h>
345b99e6ccSSedji Gaouaou 
355b99e6ccSSedji Gaouaou #include "../codecs/wm8731.h"
365b99e6ccSSedji Gaouaou #include "atmel-pcm.h"
375b99e6ccSSedji Gaouaou #include "atmel_ssc_dai.h"
385b99e6ccSSedji Gaouaou 
395de7f9b2SMark Brown #define MCLK_RATE 12000000
405de7f9b2SMark Brown 
4122df8eb4SMark Brown /*
4222df8eb4SMark Brown  * As shipped the board does not have inputs.  However, it is relatively
4322df8eb4SMark Brown  * straightforward to modify the board to hook them up so support is left
4422df8eb4SMark Brown  * in the driver.
4522df8eb4SMark Brown  */
4622df8eb4SMark Brown #undef ENABLE_MIC_INPUT
4722df8eb4SMark Brown 
485b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
495b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_MIC("Int Mic", NULL),
505b99e6ccSSedji Gaouaou 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
515b99e6ccSSedji Gaouaou };
525b99e6ccSSedji Gaouaou 
535b99e6ccSSedji Gaouaou static const struct snd_soc_dapm_route intercon[] = {
545b99e6ccSSedji Gaouaou 
55a1677e39SLars-Peter Clausen 	/* speaker connected to LHPOUT/RHPOUT */
565b99e6ccSSedji Gaouaou 	{"Ext Spk", NULL, "LHPOUT"},
57a1677e39SLars-Peter Clausen 	{"Ext Spk", NULL, "RHPOUT"},
585b99e6ccSSedji Gaouaou 
595b99e6ccSSedji Gaouaou 	/* mic is connected to Mic Jack, with WM8731 Mic Bias */
605b99e6ccSSedji Gaouaou 	{"MICIN", NULL, "Mic Bias"},
615b99e6ccSSedji Gaouaou 	{"Mic Bias", NULL, "Int Mic"},
625b99e6ccSSedji Gaouaou };
635b99e6ccSSedji Gaouaou 
645b99e6ccSSedji Gaouaou /*
655b99e6ccSSedji Gaouaou  * Logic for a wm8731 as connected on a at91sam9g20ek board.
665b99e6ccSSedji Gaouaou  */
67f0fba2adSLiam Girdwood static int at91sam9g20ek_wm8731_init(struct snd_soc_pcm_runtime *rtd)
685b99e6ccSSedji Gaouaou {
69*6547effcSKuninori Morimoto 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
70edefc8f3SLadislav Michl 	struct device *dev = rtd->dev;
7196fd6d47SMark Brown 	int ret;
7296fd6d47SMark Brown 
73edefc8f3SLadislav Michl 	dev_dbg(dev, "%s called\n", __func__);
745b99e6ccSSedji Gaouaou 
756bb74a72SNicolas Ferre 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_MCLK,
7696fd6d47SMark Brown 				     MCLK_RATE, SND_SOC_CLOCK_IN);
7796fd6d47SMark Brown 	if (ret < 0) {
78edefc8f3SLadislav Michl 		dev_err(dev, "Failed to set WM8731 SYSCLK: %d\n", ret);
7996fd6d47SMark Brown 		return ret;
8096fd6d47SMark Brown 	}
8196fd6d47SMark Brown 
82b0ffe9b1SLars-Peter Clausen #ifndef ENABLE_MIC_INPUT
83b0ffe9b1SLars-Peter Clausen 	snd_soc_dapm_nc_pin(&rtd->card->dapm, "Int Mic");
8422df8eb4SMark Brown #endif
8522df8eb4SMark Brown 
865b99e6ccSSedji Gaouaou 	return 0;
875b99e6ccSSedji Gaouaou }
885b99e6ccSSedji Gaouaou 
89d83923c5SKuninori Morimoto SND_SOC_DAILINK_DEFS(pcm,
90d83923c5SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CPU("at91rm9200_ssc.0")),
910efd1d7cSKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC("wm8731.0-001b", "wm8731-hifi")),
920efd1d7cSKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_PLATFORM("at91rm9200_ssc.0")));
93d83923c5SKuninori Morimoto 
945b99e6ccSSedji Gaouaou static struct snd_soc_dai_link at91sam9g20ek_dai = {
955b99e6ccSSedji Gaouaou 	.name = "WM8731",
965b99e6ccSSedji Gaouaou 	.stream_name = "WM8731 PCM",
975b99e6ccSSedji Gaouaou 	.init = at91sam9g20ek_wm8731_init,
98059d3dc6SLars-Peter Clausen 	.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
994a8cf938SMark Brown 		   SND_SOC_DAIFMT_CBP_CFP,
100c769fb6bSMark Brown #ifndef ENABLE_MIC_INPUT
101c769fb6bSMark Brown 	.playback_only = true,
102c769fb6bSMark Brown #endif
103d83923c5SKuninori Morimoto 	SND_SOC_DAILINK_REG(pcm),
1045b99e6ccSSedji Gaouaou };
1055b99e6ccSSedji Gaouaou 
10687506549SMark Brown static struct snd_soc_card snd_soc_at91sam9g20ek = {
1077ee75380SMark Brown 	.name = "AT91SAMG20-EK",
108338d68dbSAxel Lin 	.owner = THIS_MODULE,
1095b99e6ccSSedji Gaouaou 	.dai_link = &at91sam9g20ek_dai,
1105b99e6ccSSedji Gaouaou 	.num_links = 1,
111b0ffe9b1SLars-Peter Clausen 
112b0ffe9b1SLars-Peter Clausen 	.dapm_widgets = at91sam9g20ek_dapm_widgets,
113b0ffe9b1SLars-Peter Clausen 	.num_dapm_widgets = ARRAY_SIZE(at91sam9g20ek_dapm_widgets),
114b0ffe9b1SLars-Peter Clausen 	.dapm_routes = intercon,
115b0ffe9b1SLars-Peter Clausen 	.num_dapm_routes = ARRAY_SIZE(intercon),
116a1677e39SLars-Peter Clausen 	.fully_routed = true,
1175b99e6ccSSedji Gaouaou };
1185b99e6ccSSedji Gaouaou 
11971d14ea6SBill Pemberton static int at91sam9g20ek_audio_probe(struct platform_device *pdev)
1205b99e6ccSSedji Gaouaou {
121531f67e4SBo Shen 	struct device_node *np = pdev->dev.of_node;
122531f67e4SBo Shen 	struct device_node *codec_np, *cpu_np;
123242b9bb8SBo Shen 	struct snd_soc_card *card = &snd_soc_at91sam9g20ek;
1245b99e6ccSSedji Gaouaou 	int ret;
1255b99e6ccSSedji Gaouaou 
126531f67e4SBo Shen 	if (!np) {
12740135ea0SMark Brown 		return -ENODEV;
128531f67e4SBo Shen 	}
12940135ea0SMark Brown 
1303310b57dSBo Shen 	ret = atmel_ssc_set_audio(0);
1313310b57dSBo Shen 	if (ret) {
13201251dd0SMark Brown 		dev_err(&pdev->dev, "ssc channel is not valid: %d\n", ret);
13301251dd0SMark Brown 		return ret;
1343310b57dSBo Shen 	}
1353310b57dSBo Shen 
136242b9bb8SBo Shen 	card->dev = &pdev->dev;
137531f67e4SBo Shen 
138531f67e4SBo Shen 	/* Parse device node info */
139531f67e4SBo Shen 	ret = snd_soc_of_parse_card_name(card, "atmel,model");
140531f67e4SBo Shen 	if (ret)
141531f67e4SBo Shen 		goto err;
142531f67e4SBo Shen 
143531f67e4SBo Shen 	ret = snd_soc_of_parse_audio_routing(card,
144531f67e4SBo Shen 		"atmel,audio-routing");
145531f67e4SBo Shen 	if (ret)
146531f67e4SBo Shen 		goto err;
147531f67e4SBo Shen 
148531f67e4SBo Shen 	/* Parse codec info */
149d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.codecs->name = NULL;
150531f67e4SBo Shen 	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
151531f67e4SBo Shen 	if (!codec_np) {
152531f67e4SBo Shen 		dev_err(&pdev->dev, "codec info missing\n");
15328103509SMark Brown 		ret = -EINVAL;
15428103509SMark Brown 		goto err;
155531f67e4SBo Shen 	}
156d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.codecs->of_node = codec_np;
157531f67e4SBo Shen 
158531f67e4SBo Shen 	/* Parse dai and platform info */
159d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.cpus->dai_name = NULL;
1600efd1d7cSKuninori Morimoto 	at91sam9g20ek_dai.platforms->name = NULL;
161531f67e4SBo Shen 	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
162531f67e4SBo Shen 	if (!cpu_np) {
163531f67e4SBo Shen 		dev_err(&pdev->dev, "dai and pcm info missing\n");
164f590797fSMiaoqian Lin 		of_node_put(codec_np);
16528103509SMark Brown 		ret = -EINVAL;
16628103509SMark Brown 		goto err;
167531f67e4SBo Shen 	}
168d83923c5SKuninori Morimoto 	at91sam9g20ek_dai.cpus->of_node = cpu_np;
1690efd1d7cSKuninori Morimoto 	at91sam9g20ek_dai.platforms->of_node = cpu_np;
170531f67e4SBo Shen 
171531f67e4SBo Shen 	of_node_put(codec_np);
172531f67e4SBo Shen 	of_node_put(cpu_np);
173531f67e4SBo Shen 
174242b9bb8SBo Shen 	ret = snd_soc_register_card(card);
1755b99e6ccSSedji Gaouaou 	if (ret) {
17629f4078fSMark Brown 		dev_err_probe(&pdev->dev, ret,
1777d67657cSChristophe JAILLET 			      "snd_soc_register_card() failed\n");
17828103509SMark Brown 		goto err;
1795b99e6ccSSedji Gaouaou 	}
1805b99e6ccSSedji Gaouaou 
18128103509SMark Brown 	return 0;
1825b99e6ccSSedji Gaouaou 
1835de7f9b2SMark Brown err:
1843310b57dSBo Shen 	atmel_ssc_put_audio(0);
1855b99e6ccSSedji Gaouaou 	return ret;
1865b99e6ccSSedji Gaouaou }
1875b99e6ccSSedji Gaouaou 
188a186762bSUwe Kleine-König static void at91sam9g20ek_audio_remove(struct platform_device *pdev)
1895b99e6ccSSedji Gaouaou {
190242b9bb8SBo Shen 	struct snd_soc_card *card = platform_get_drvdata(pdev);
191242b9bb8SBo Shen 
192151edfc7SBo Shen 	snd_soc_unregister_card(card);
193151edfc7SBo Shen 	atmel_ssc_put_audio(0);
1945b99e6ccSSedji Gaouaou }
1955b99e6ccSSedji Gaouaou 
196531f67e4SBo Shen #ifdef CONFIG_OF
197531f67e4SBo Shen static const struct of_device_id at91sam9g20ek_wm8731_dt_ids[] = {
198531f67e4SBo Shen 	{ .compatible = "atmel,at91sam9g20ek-wm8731-audio", },
199531f67e4SBo Shen 	{ }
200531f67e4SBo Shen };
201531f67e4SBo Shen MODULE_DEVICE_TABLE(of, at91sam9g20ek_wm8731_dt_ids);
202531f67e4SBo Shen #endif
203531f67e4SBo Shen 
204242b9bb8SBo Shen static struct platform_driver at91sam9g20ek_audio_driver = {
205242b9bb8SBo Shen 	.driver = {
206242b9bb8SBo Shen 		.name	= "at91sam9g20ek-audio",
207531f67e4SBo Shen 		.of_match_table = of_match_ptr(at91sam9g20ek_wm8731_dt_ids),
208242b9bb8SBo Shen 	},
209242b9bb8SBo Shen 	.probe	= at91sam9g20ek_audio_probe,
210a186762bSUwe Kleine-König 	.remove_new = at91sam9g20ek_audio_remove,
211242b9bb8SBo Shen };
212242b9bb8SBo Shen 
213242b9bb8SBo Shen module_platform_driver(at91sam9g20ek_audio_driver);
2145b99e6ccSSedji Gaouaou 
2155b99e6ccSSedji Gaouaou /* Module information */
2165b99e6ccSSedji Gaouaou MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
2175b99e6ccSSedji Gaouaou MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
218242b9bb8SBo Shen MODULE_ALIAS("platform:at91sam9g20ek-audio");
2195b99e6ccSSedji Gaouaou MODULE_LICENSE("GPL");
220