xref: /openbmc/linux/sound/soc/atmel/sam9x5_wm8731.c (revision 8dde5715)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * sam9x5_wm8731   --	SoC audio for AT91SAM9X5-based boards
4  *			that are using WM8731 as codec.
5  *
6  *  Copyright (C) 2011 Atmel,
7  *		  Nicolas Ferre <nicolas.ferre@atmel.com>
8  *
9  *  Copyright (C) 2013 Paratronic,
10  *		  Richard Genoud <richard.genoud@gmail.com>
11  *
12  * Based on sam9g20_wm8731.c by:
13  * Sedji Gaouaou <sedji.gaouaou@atmel.com>
14  */
15 #include <linux/of.h>
16 #include <linux/export.h>
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/platform_device.h>
20 #include <linux/device.h>
21 
22 #include <sound/soc.h>
23 #include <sound/soc-dai.h>
24 #include <sound/soc-dapm.h>
25 
26 #include "../codecs/wm8731.h"
27 #include "atmel_ssc_dai.h"
28 
29 
30 #define MCLK_RATE 12288000
31 
32 #define DRV_NAME "sam9x5-snd-wm8731"
33 
34 struct sam9x5_drvdata {
35 	int ssc_id;
36 };
37 
38 /*
39  * Logic for a wm8731 as connected on a at91sam9x5ek based board.
40  */
41 static int sam9x5_wm8731_init(struct snd_soc_pcm_runtime *rtd)
42 {
43 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
44 	struct device *dev = rtd->dev;
45 	int ret;
46 
47 	dev_dbg(dev, "%s called\n", __func__);
48 
49 	/* set the codec system clock for DAC and ADC */
50 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
51 				     MCLK_RATE, SND_SOC_CLOCK_IN);
52 	if (ret < 0) {
53 		dev_err(dev, "Failed to set WM8731 SYSCLK: %d\n", ret);
54 		return ret;
55 	}
56 
57 	return 0;
58 }
59 
60 /*
61  * Audio paths on at91sam9x5ek board:
62  *
63  *  |A| ------------> |      | ---R----> Headphone Jack
64  *  |T| <----\        |  WM  | ---L--/
65  *  |9| ---> CLK <--> | 8731 | <--R----- Line In Jack
66  *  |1| <------------ |      | <--L--/
67  */
68 static const struct snd_soc_dapm_widget sam9x5_dapm_widgets[] = {
69 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
70 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
71 };
72 
73 static int sam9x5_wm8731_driver_probe(struct platform_device *pdev)
74 {
75 	struct device_node *np = pdev->dev.of_node;
76 	struct device_node *codec_np, *cpu_np;
77 	struct snd_soc_card *card;
78 	struct snd_soc_dai_link *dai;
79 	struct sam9x5_drvdata *priv;
80 	int ret;
81 
82 	if (!np) {
83 		dev_err(&pdev->dev, "No device node supplied\n");
84 		return -EINVAL;
85 	}
86 
87 	card = devm_kzalloc(&pdev->dev, sizeof(*card), GFP_KERNEL);
88 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
89 	dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL);
90 	if (!dai || !card || !priv) {
91 		ret = -ENOMEM;
92 		goto out;
93 	}
94 
95 	snd_soc_card_set_drvdata(card, priv);
96 
97 	card->dev = &pdev->dev;
98 	card->owner = THIS_MODULE;
99 	card->dai_link = dai;
100 	card->num_links = 1;
101 	card->dapm_widgets = sam9x5_dapm_widgets;
102 	card->num_dapm_widgets = ARRAY_SIZE(sam9x5_dapm_widgets);
103 	dai->name = "WM8731";
104 	dai->stream_name = "WM8731 PCM";
105 	dai->codec_dai_name = "wm8731-hifi";
106 	dai->init = sam9x5_wm8731_init;
107 	dai->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF
108 		| SND_SOC_DAIFMT_CBM_CFM;
109 
110 	ret = snd_soc_of_parse_card_name(card, "atmel,model");
111 	if (ret) {
112 		dev_err(&pdev->dev, "atmel,model node missing\n");
113 		goto out;
114 	}
115 
116 	ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
117 	if (ret) {
118 		dev_err(&pdev->dev, "atmel,audio-routing node missing\n");
119 		goto out;
120 	}
121 
122 	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
123 	if (!codec_np) {
124 		dev_err(&pdev->dev, "atmel,audio-codec node missing\n");
125 		ret = -EINVAL;
126 		goto out;
127 	}
128 
129 	dai->codec_of_node = codec_np;
130 
131 	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
132 	if (!cpu_np) {
133 		dev_err(&pdev->dev, "atmel,ssc-controller node missing\n");
134 		ret = -EINVAL;
135 		goto out;
136 	}
137 	dai->cpu_of_node = cpu_np;
138 	dai->platform_of_node = cpu_np;
139 
140 	priv->ssc_id = of_alias_get_id(cpu_np, "ssc");
141 
142 	ret = atmel_ssc_set_audio(priv->ssc_id);
143 	if (ret != 0) {
144 		dev_err(&pdev->dev, "Failed to set SSC %d for audio: %d\n",
145 			ret, priv->ssc_id);
146 		goto out;
147 	}
148 
149 	of_node_put(codec_np);
150 	of_node_put(cpu_np);
151 
152 	ret = snd_soc_register_card(card);
153 	if (ret) {
154 		dev_err(&pdev->dev, "Platform device allocation failed\n");
155 		goto out_put_audio;
156 	}
157 
158 	dev_dbg(&pdev->dev, "%s ok\n", __func__);
159 
160 	return ret;
161 
162 out_put_audio:
163 	atmel_ssc_put_audio(priv->ssc_id);
164 out:
165 	return ret;
166 }
167 
168 static int sam9x5_wm8731_driver_remove(struct platform_device *pdev)
169 {
170 	struct snd_soc_card *card = platform_get_drvdata(pdev);
171 	struct sam9x5_drvdata *priv = card->drvdata;
172 
173 	snd_soc_unregister_card(card);
174 	atmel_ssc_put_audio(priv->ssc_id);
175 
176 	return 0;
177 }
178 
179 static const struct of_device_id sam9x5_wm8731_of_match[] = {
180 	{ .compatible = "atmel,sam9x5-wm8731-audio", },
181 	{},
182 };
183 MODULE_DEVICE_TABLE(of, sam9x5_wm8731_of_match);
184 
185 static struct platform_driver sam9x5_wm8731_driver = {
186 	.driver = {
187 		.name = DRV_NAME,
188 		.of_match_table = of_match_ptr(sam9x5_wm8731_of_match),
189 	},
190 	.probe = sam9x5_wm8731_driver_probe,
191 	.remove = sam9x5_wm8731_driver_remove,
192 };
193 module_platform_driver(sam9x5_wm8731_driver);
194 
195 /* Module information */
196 MODULE_AUTHOR("Nicolas Ferre <nicolas.ferre@atmel.com>");
197 MODULE_AUTHOR("Richard Genoud <richard.genoud@gmail.com>");
198 MODULE_DESCRIPTION("ALSA SoC machine driver for AT91SAM9x5 - WM8731");
199 MODULE_LICENSE("GPL");
200 MODULE_ALIAS("platform:" DRV_NAME);
201