xref: /openbmc/linux/sound/soc/ti/omap-abe-twl6040.c (revision 023e4163)
1 /*
2  * omap-abe-twl6040.c  --  SoC audio for TI OMAP based boards with ABE and
3  *			   twl6040 codec
4  *
5  * Author: Misael Lopez Cruz <misael.lopez@ti.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19  * 02110-1301 USA
20  *
21  */
22 
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/mfd/twl6040.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 
29 #include <sound/core.h>
30 #include <sound/pcm.h>
31 #include <sound/soc.h>
32 #include <sound/jack.h>
33 
34 #include "omap-dmic.h"
35 #include "omap-mcpdm.h"
36 #include "../codecs/twl6040.h"
37 
38 struct abe_twl6040 {
39 	struct snd_soc_card card;
40 	struct snd_soc_dai_link dai_links[2];
41 	int	jack_detection;	/* board can detect jack events */
42 	int	mclk_freq;	/* MCLK frequency speed for twl6040 */
43 };
44 
45 static struct platform_device *dmic_codec_dev;
46 
47 static int omap_abe_hw_params(struct snd_pcm_substream *substream,
48 	struct snd_pcm_hw_params *params)
49 {
50 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
51 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
52 	struct snd_soc_card *card = rtd->card;
53 	struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
54 	int clk_id, freq;
55 	int ret;
56 
57 	clk_id = twl6040_get_clk_id(codec_dai->component);
58 	if (clk_id == TWL6040_SYSCLK_SEL_HPPLL)
59 		freq = priv->mclk_freq;
60 	else if (clk_id == TWL6040_SYSCLK_SEL_LPPLL)
61 		freq = 32768;
62 	else
63 		return -EINVAL;
64 
65 	/* set the codec mclk */
66 	ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, freq,
67 				SND_SOC_CLOCK_IN);
68 	if (ret) {
69 		printk(KERN_ERR "can't set codec system clock\n");
70 		return ret;
71 	}
72 	return ret;
73 }
74 
75 static const struct snd_soc_ops omap_abe_ops = {
76 	.hw_params = omap_abe_hw_params,
77 };
78 
79 static int omap_abe_dmic_hw_params(struct snd_pcm_substream *substream,
80 	struct snd_pcm_hw_params *params)
81 {
82 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
83 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
84 	int ret = 0;
85 
86 	ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_SYSCLK_PAD_CLKS,
87 				     19200000, SND_SOC_CLOCK_IN);
88 	if (ret < 0) {
89 		printk(KERN_ERR "can't set DMIC cpu system clock\n");
90 		return ret;
91 	}
92 	ret = snd_soc_dai_set_sysclk(cpu_dai, OMAP_DMIC_ABE_DMIC_CLK, 2400000,
93 				     SND_SOC_CLOCK_OUT);
94 	if (ret < 0) {
95 		printk(KERN_ERR "can't set DMIC output clock\n");
96 		return ret;
97 	}
98 	return 0;
99 }
100 
101 static struct snd_soc_ops omap_abe_dmic_ops = {
102 	.hw_params = omap_abe_dmic_hw_params,
103 };
104 
105 /* Headset jack */
106 static struct snd_soc_jack hs_jack;
107 
108 /*Headset jack detection DAPM pins */
109 static struct snd_soc_jack_pin hs_jack_pins[] = {
110 	{
111 		.pin = "Headset Mic",
112 		.mask = SND_JACK_MICROPHONE,
113 	},
114 	{
115 		.pin = "Headset Stereophone",
116 		.mask = SND_JACK_HEADPHONE,
117 	},
118 };
119 
120 /* SDP4430 machine DAPM */
121 static const struct snd_soc_dapm_widget twl6040_dapm_widgets[] = {
122 	/* Outputs */
123 	SND_SOC_DAPM_HP("Headset Stereophone", NULL),
124 	SND_SOC_DAPM_SPK("Earphone Spk", NULL),
125 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
126 	SND_SOC_DAPM_LINE("Line Out", NULL),
127 	SND_SOC_DAPM_SPK("Vibrator", NULL),
128 
129 	/* Inputs */
130 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
131 	SND_SOC_DAPM_MIC("Main Handset Mic", NULL),
132 	SND_SOC_DAPM_MIC("Sub Handset Mic", NULL),
133 	SND_SOC_DAPM_LINE("Line In", NULL),
134 
135 	/* Digital microphones */
136 	SND_SOC_DAPM_MIC("Digital Mic", NULL),
137 };
138 
139 static const struct snd_soc_dapm_route audio_map[] = {
140 	/* Routings for outputs */
141 	{"Headset Stereophone", NULL, "HSOL"},
142 	{"Headset Stereophone", NULL, "HSOR"},
143 
144 	{"Earphone Spk", NULL, "EP"},
145 
146 	{"Ext Spk", NULL, "HFL"},
147 	{"Ext Spk", NULL, "HFR"},
148 
149 	{"Line Out", NULL, "AUXL"},
150 	{"Line Out", NULL, "AUXR"},
151 
152 	{"Vibrator", NULL, "VIBRAL"},
153 	{"Vibrator", NULL, "VIBRAR"},
154 
155 	/* Routings for inputs */
156 	{"HSMIC", NULL, "Headset Mic"},
157 	{"Headset Mic", NULL, "Headset Mic Bias"},
158 
159 	{"MAINMIC", NULL, "Main Handset Mic"},
160 	{"Main Handset Mic", NULL, "Main Mic Bias"},
161 
162 	{"SUBMIC", NULL, "Sub Handset Mic"},
163 	{"Sub Handset Mic", NULL, "Main Mic Bias"},
164 
165 	{"AFML", NULL, "Line In"},
166 	{"AFMR", NULL, "Line In"},
167 };
168 
169 static int omap_abe_twl6040_init(struct snd_soc_pcm_runtime *rtd)
170 {
171 	struct snd_soc_component *component = rtd->codec_dai->component;
172 	struct snd_soc_card *card = rtd->card;
173 	struct abe_twl6040 *priv = snd_soc_card_get_drvdata(card);
174 	int hs_trim;
175 	int ret = 0;
176 
177 	/*
178 	 * Configure McPDM offset cancellation based on the HSOTRIM value from
179 	 * twl6040.
180 	 */
181 	hs_trim = twl6040_get_trim_value(component, TWL6040_TRIM_HSOTRIM);
182 	omap_mcpdm_configure_dn_offsets(rtd, TWL6040_HSF_TRIM_LEFT(hs_trim),
183 					TWL6040_HSF_TRIM_RIGHT(hs_trim));
184 
185 	/* Headset jack detection only if it is supported */
186 	if (priv->jack_detection) {
187 		ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
188 					    SND_JACK_HEADSET, &hs_jack,
189 					    hs_jack_pins,
190 					    ARRAY_SIZE(hs_jack_pins));
191 		if (ret)
192 			return ret;
193 
194 		twl6040_hs_jack_detect(component, &hs_jack, SND_JACK_HEADSET);
195 	}
196 
197 	return 0;
198 }
199 
200 static const struct snd_soc_dapm_route dmic_audio_map[] = {
201 	{"DMic", NULL, "Digital Mic"},
202 	{"Digital Mic", NULL, "Digital Mic1 Bias"},
203 };
204 
205 static int omap_abe_dmic_init(struct snd_soc_pcm_runtime *rtd)
206 {
207 	struct snd_soc_dapm_context *dapm = &rtd->card->dapm;
208 
209 	return snd_soc_dapm_add_routes(dapm, dmic_audio_map,
210 				ARRAY_SIZE(dmic_audio_map));
211 }
212 
213 static int omap_abe_probe(struct platform_device *pdev)
214 {
215 	struct device_node *node = pdev->dev.of_node;
216 	struct snd_soc_card *card;
217 	struct device_node *dai_node;
218 	struct abe_twl6040 *priv;
219 	int num_links = 0;
220 	int ret = 0;
221 
222 	if (!node) {
223 		dev_err(&pdev->dev, "of node is missing.\n");
224 		return -ENODEV;
225 	}
226 
227 	priv = devm_kzalloc(&pdev->dev, sizeof(struct abe_twl6040), GFP_KERNEL);
228 	if (priv == NULL)
229 		return -ENOMEM;
230 
231 	card = &priv->card;
232 	card->dev = &pdev->dev;
233 	card->owner = THIS_MODULE;
234 	card->dapm_widgets = twl6040_dapm_widgets;
235 	card->num_dapm_widgets = ARRAY_SIZE(twl6040_dapm_widgets);
236 	card->dapm_routes = audio_map;
237 	card->num_dapm_routes = ARRAY_SIZE(audio_map);
238 
239 	if (snd_soc_of_parse_card_name(card, "ti,model")) {
240 		dev_err(&pdev->dev, "Card name is not provided\n");
241 		return -ENODEV;
242 	}
243 
244 	ret = snd_soc_of_parse_audio_routing(card, "ti,audio-routing");
245 	if (ret) {
246 		dev_err(&pdev->dev, "Error while parsing DAPM routing\n");
247 		return ret;
248 	}
249 
250 	dai_node = of_parse_phandle(node, "ti,mcpdm", 0);
251 	if (!dai_node) {
252 		dev_err(&pdev->dev, "McPDM node is not provided\n");
253 		return -EINVAL;
254 	}
255 
256 	priv->dai_links[0].name = "DMIC";
257 	priv->dai_links[0].stream_name = "TWL6040";
258 	priv->dai_links[0].cpu_of_node = dai_node;
259 	priv->dai_links[0].platform_of_node = dai_node;
260 	priv->dai_links[0].codec_dai_name = "twl6040-legacy";
261 	priv->dai_links[0].codec_name = "twl6040-codec";
262 	priv->dai_links[0].init = omap_abe_twl6040_init;
263 	priv->dai_links[0].ops = &omap_abe_ops;
264 
265 	dai_node = of_parse_phandle(node, "ti,dmic", 0);
266 	if (dai_node) {
267 		num_links = 2;
268 		priv->dai_links[1].name = "TWL6040";
269 		priv->dai_links[1].stream_name = "DMIC Capture";
270 		priv->dai_links[1].cpu_of_node = dai_node;
271 		priv->dai_links[1].platform_of_node = dai_node;
272 		priv->dai_links[1].codec_dai_name = "dmic-hifi";
273 		priv->dai_links[1].codec_name = "dmic-codec";
274 		priv->dai_links[1].init = omap_abe_dmic_init;
275 		priv->dai_links[1].ops = &omap_abe_dmic_ops;
276 	} else {
277 		num_links = 1;
278 	}
279 
280 	priv->jack_detection = of_property_read_bool(node, "ti,jack-detection");
281 	of_property_read_u32(node, "ti,mclk-freq", &priv->mclk_freq);
282 	if (!priv->mclk_freq) {
283 		dev_err(&pdev->dev, "MCLK frequency not provided\n");
284 		return -EINVAL;
285 	}
286 
287 	card->fully_routed = 1;
288 
289 	if (!priv->mclk_freq) {
290 		dev_err(&pdev->dev, "MCLK frequency missing\n");
291 		return -ENODEV;
292 	}
293 
294 	card->dai_link = priv->dai_links;
295 	card->num_links = num_links;
296 
297 	snd_soc_card_set_drvdata(card, priv);
298 
299 	ret = devm_snd_soc_register_card(&pdev->dev, card);
300 	if (ret)
301 		dev_err(&pdev->dev, "devm_snd_soc_register_card() failed: %d\n",
302 			ret);
303 
304 	return ret;
305 }
306 
307 static const struct of_device_id omap_abe_of_match[] = {
308 	{.compatible = "ti,abe-twl6040", },
309 	{ },
310 };
311 MODULE_DEVICE_TABLE(of, omap_abe_of_match);
312 
313 static struct platform_driver omap_abe_driver = {
314 	.driver = {
315 		.name = "omap-abe-twl6040",
316 		.pm = &snd_soc_pm_ops,
317 		.of_match_table = omap_abe_of_match,
318 	},
319 	.probe = omap_abe_probe,
320 };
321 
322 static int __init omap_abe_init(void)
323 {
324 	int ret;
325 
326 	dmic_codec_dev = platform_device_register_simple("dmic-codec", -1, NULL,
327 							 0);
328 	if (IS_ERR(dmic_codec_dev)) {
329 		pr_err("%s: dmic-codec device registration failed\n", __func__);
330 		return PTR_ERR(dmic_codec_dev);
331 	}
332 
333 	ret = platform_driver_register(&omap_abe_driver);
334 	if (ret) {
335 		pr_err("%s: platform driver registration failed\n", __func__);
336 		platform_device_unregister(dmic_codec_dev);
337 	}
338 
339 	return ret;
340 }
341 module_init(omap_abe_init);
342 
343 static void __exit omap_abe_exit(void)
344 {
345 	platform_driver_unregister(&omap_abe_driver);
346 	platform_device_unregister(dmic_codec_dev);
347 }
348 module_exit(omap_abe_exit);
349 
350 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
351 MODULE_DESCRIPTION("ALSA SoC for OMAP boards with ABE and twl6040 codec");
352 MODULE_LICENSE("GPL");
353 MODULE_ALIAS("platform:omap-abe-twl6040");
354