1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2a45f8853SCodrin Ciubotariu /*
3a45f8853SCodrin Ciubotariu * ASoC driver for PROTO AudioCODEC (with a WM8731)
4a45f8853SCodrin Ciubotariu *
5a45f8853SCodrin Ciubotariu * Author: Florian Meier, <koalo@koalo.de>
6a45f8853SCodrin Ciubotariu * Copyright 2013
7a45f8853SCodrin Ciubotariu */
8a45f8853SCodrin Ciubotariu
9a45f8853SCodrin Ciubotariu #include <linux/module.h>
10a45f8853SCodrin Ciubotariu #include <linux/platform_device.h>
11a45f8853SCodrin Ciubotariu
12a45f8853SCodrin Ciubotariu #include <sound/core.h>
13a45f8853SCodrin Ciubotariu #include <sound/pcm.h>
14a45f8853SCodrin Ciubotariu #include <sound/soc.h>
15a45f8853SCodrin Ciubotariu #include <sound/jack.h>
16a45f8853SCodrin Ciubotariu
17a45f8853SCodrin Ciubotariu #include "../codecs/wm8731.h"
18a45f8853SCodrin Ciubotariu
19a45f8853SCodrin Ciubotariu #define XTAL_RATE 12288000 /* This is fixed on this board */
20a45f8853SCodrin Ciubotariu
snd_proto_init(struct snd_soc_pcm_runtime * rtd)21a45f8853SCodrin Ciubotariu static int snd_proto_init(struct snd_soc_pcm_runtime *rtd)
22a45f8853SCodrin Ciubotariu {
23a45f8853SCodrin Ciubotariu struct snd_soc_card *card = rtd->card;
24*6de2e582SKuninori Morimoto struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
25a45f8853SCodrin Ciubotariu
26a45f8853SCodrin Ciubotariu /* Set proto sysclk */
27a45f8853SCodrin Ciubotariu int ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_XTAL,
28a45f8853SCodrin Ciubotariu XTAL_RATE, SND_SOC_CLOCK_IN);
29a45f8853SCodrin Ciubotariu if (ret < 0) {
30a45f8853SCodrin Ciubotariu dev_err(card->dev, "Failed to set WM8731 SYSCLK: %d\n",
31a45f8853SCodrin Ciubotariu ret);
32a45f8853SCodrin Ciubotariu return ret;
33a45f8853SCodrin Ciubotariu }
34a45f8853SCodrin Ciubotariu
35a45f8853SCodrin Ciubotariu return 0;
36a45f8853SCodrin Ciubotariu }
37a45f8853SCodrin Ciubotariu
38a45f8853SCodrin Ciubotariu static const struct snd_soc_dapm_widget snd_proto_widget[] = {
39a45f8853SCodrin Ciubotariu SND_SOC_DAPM_MIC("Microphone Jack", NULL),
40a45f8853SCodrin Ciubotariu SND_SOC_DAPM_HP("Headphone Jack", NULL),
41a45f8853SCodrin Ciubotariu };
42a45f8853SCodrin Ciubotariu
43a45f8853SCodrin Ciubotariu static const struct snd_soc_dapm_route snd_proto_route[] = {
44a45f8853SCodrin Ciubotariu /* speaker connected to LHPOUT/RHPOUT */
45a45f8853SCodrin Ciubotariu {"Headphone Jack", NULL, "LHPOUT"},
46a45f8853SCodrin Ciubotariu {"Headphone Jack", NULL, "RHPOUT"},
47a45f8853SCodrin Ciubotariu
48a45f8853SCodrin Ciubotariu /* mic is connected to Mic Jack, with WM8731 Mic Bias */
49a45f8853SCodrin Ciubotariu {"MICIN", NULL, "Mic Bias"},
50a45f8853SCodrin Ciubotariu {"Mic Bias", NULL, "Microphone Jack"},
51a45f8853SCodrin Ciubotariu };
52a45f8853SCodrin Ciubotariu
53a45f8853SCodrin Ciubotariu /* audio machine driver */
54a45f8853SCodrin Ciubotariu static struct snd_soc_card snd_proto = {
55a45f8853SCodrin Ciubotariu .name = "snd_mikroe_proto",
56a45f8853SCodrin Ciubotariu .owner = THIS_MODULE,
57a45f8853SCodrin Ciubotariu .dapm_widgets = snd_proto_widget,
58a45f8853SCodrin Ciubotariu .num_dapm_widgets = ARRAY_SIZE(snd_proto_widget),
59a45f8853SCodrin Ciubotariu .dapm_routes = snd_proto_route,
60a45f8853SCodrin Ciubotariu .num_dapm_routes = ARRAY_SIZE(snd_proto_route),
61a45f8853SCodrin Ciubotariu };
62a45f8853SCodrin Ciubotariu
snd_proto_probe(struct platform_device * pdev)63a45f8853SCodrin Ciubotariu static int snd_proto_probe(struct platform_device *pdev)
64a45f8853SCodrin Ciubotariu {
65a45f8853SCodrin Ciubotariu struct snd_soc_dai_link *dai;
66ed00d6ccSKuninori Morimoto struct snd_soc_dai_link_component *comp;
67a45f8853SCodrin Ciubotariu struct device_node *np = pdev->dev.of_node;
68a45f8853SCodrin Ciubotariu struct device_node *codec_np, *cpu_np;
69a45f8853SCodrin Ciubotariu struct device_node *bitclkmaster = NULL;
70a45f8853SCodrin Ciubotariu struct device_node *framemaster = NULL;
71a45f8853SCodrin Ciubotariu unsigned int dai_fmt;
72a45f8853SCodrin Ciubotariu int ret = 0;
73a45f8853SCodrin Ciubotariu
74a45f8853SCodrin Ciubotariu if (!np) {
75a45f8853SCodrin Ciubotariu dev_err(&pdev->dev, "No device node supplied\n");
76a45f8853SCodrin Ciubotariu return -EINVAL;
77a45f8853SCodrin Ciubotariu }
78a45f8853SCodrin Ciubotariu
79a45f8853SCodrin Ciubotariu snd_proto.dev = &pdev->dev;
80a45f8853SCodrin Ciubotariu ret = snd_soc_of_parse_card_name(&snd_proto, "model");
81a45f8853SCodrin Ciubotariu if (ret)
82a45f8853SCodrin Ciubotariu return ret;
83a45f8853SCodrin Ciubotariu
84a45f8853SCodrin Ciubotariu dai = devm_kzalloc(&pdev->dev, sizeof(*dai), GFP_KERNEL);
85a45f8853SCodrin Ciubotariu if (!dai)
86a45f8853SCodrin Ciubotariu return -ENOMEM;
87a45f8853SCodrin Ciubotariu
88716407eaSKuninori Morimoto /* for cpus/codecs/platforms */
89716407eaSKuninori Morimoto comp = devm_kzalloc(&pdev->dev, 3 * sizeof(*comp), GFP_KERNEL);
90ed00d6ccSKuninori Morimoto if (!comp)
91ed00d6ccSKuninori Morimoto return -ENOMEM;
92ed00d6ccSKuninori Morimoto
93a45f8853SCodrin Ciubotariu snd_proto.dai_link = dai;
94a45f8853SCodrin Ciubotariu snd_proto.num_links = 1;
95a45f8853SCodrin Ciubotariu
96ed00d6ccSKuninori Morimoto dai->cpus = &comp[0];
97ed00d6ccSKuninori Morimoto dai->num_cpus = 1;
98ed00d6ccSKuninori Morimoto dai->codecs = &comp[1];
99ed00d6ccSKuninori Morimoto dai->num_codecs = 1;
100716407eaSKuninori Morimoto dai->platforms = &comp[2];
101716407eaSKuninori Morimoto dai->num_platforms = 1;
102ed00d6ccSKuninori Morimoto
103a45f8853SCodrin Ciubotariu dai->name = "WM8731";
104a45f8853SCodrin Ciubotariu dai->stream_name = "WM8731 HiFi";
105ed00d6ccSKuninori Morimoto dai->codecs->dai_name = "wm8731-hifi";
106a45f8853SCodrin Ciubotariu dai->init = &snd_proto_init;
107a45f8853SCodrin Ciubotariu
108a45f8853SCodrin Ciubotariu codec_np = of_parse_phandle(np, "audio-codec", 0);
109a45f8853SCodrin Ciubotariu if (!codec_np) {
110a45f8853SCodrin Ciubotariu dev_err(&pdev->dev, "audio-codec node missing\n");
111a45f8853SCodrin Ciubotariu return -EINVAL;
112a45f8853SCodrin Ciubotariu }
113ed00d6ccSKuninori Morimoto dai->codecs->of_node = codec_np;
114a45f8853SCodrin Ciubotariu
115a45f8853SCodrin Ciubotariu cpu_np = of_parse_phandle(np, "i2s-controller", 0);
116a45f8853SCodrin Ciubotariu if (!cpu_np) {
117a45f8853SCodrin Ciubotariu dev_err(&pdev->dev, "i2s-controller missing\n");
118b0bfaf05SMiaoqian Lin ret = -EINVAL;
119b0bfaf05SMiaoqian Lin goto put_codec_node;
120a45f8853SCodrin Ciubotariu }
121ed00d6ccSKuninori Morimoto dai->cpus->of_node = cpu_np;
122716407eaSKuninori Morimoto dai->platforms->of_node = cpu_np;
123a45f8853SCodrin Ciubotariu
12422108b9cSKuninori Morimoto dai_fmt = snd_soc_daifmt_parse_format(np, NULL);
12522108b9cSKuninori Morimoto snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL,
126a45f8853SCodrin Ciubotariu &bitclkmaster, &framemaster);
127a45f8853SCodrin Ciubotariu if (bitclkmaster != framemaster) {
128a45f8853SCodrin Ciubotariu dev_err(&pdev->dev, "Must be the same bitclock and frame master\n");
129b0bfaf05SMiaoqian Lin ret = -EINVAL;
130b0bfaf05SMiaoqian Lin goto put_cpu_node;
131a45f8853SCodrin Ciubotariu }
132a45f8853SCodrin Ciubotariu if (bitclkmaster) {
133a45f8853SCodrin Ciubotariu if (codec_np == bitclkmaster)
1344a8cf938SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
135a45f8853SCodrin Ciubotariu else
1364a8cf938SMark Brown dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
13722108b9cSKuninori Morimoto } else {
13822108b9cSKuninori Morimoto dai_fmt |= snd_soc_daifmt_parse_clock_provider_as_flag(np, NULL);
139a45f8853SCodrin Ciubotariu }
14022108b9cSKuninori Morimoto
141b0bfaf05SMiaoqian Lin
142a45f8853SCodrin Ciubotariu dai->dai_fmt = dai_fmt;
143a45f8853SCodrin Ciubotariu ret = snd_soc_register_card(&snd_proto);
1440624dafaSKuninori Morimoto if (ret)
1450624dafaSKuninori Morimoto dev_err_probe(&pdev->dev, ret,
1460624dafaSKuninori Morimoto "snd_soc_register_card() failed\n");
147a45f8853SCodrin Ciubotariu
148b0bfaf05SMiaoqian Lin
149b0bfaf05SMiaoqian Lin put_cpu_node:
150b0bfaf05SMiaoqian Lin of_node_put(bitclkmaster);
151b0bfaf05SMiaoqian Lin of_node_put(framemaster);
152b0bfaf05SMiaoqian Lin of_node_put(cpu_np);
153b0bfaf05SMiaoqian Lin put_codec_node:
154b0bfaf05SMiaoqian Lin of_node_put(codec_np);
155a45f8853SCodrin Ciubotariu return ret;
156a45f8853SCodrin Ciubotariu }
157a45f8853SCodrin Ciubotariu
snd_proto_remove(struct platform_device * pdev)1582328c487SUwe Kleine-König static void snd_proto_remove(struct platform_device *pdev)
159a45f8853SCodrin Ciubotariu {
1601892a991SUwe Kleine-König snd_soc_unregister_card(&snd_proto);
161a45f8853SCodrin Ciubotariu }
162a45f8853SCodrin Ciubotariu
163a45f8853SCodrin Ciubotariu static const struct of_device_id snd_proto_of_match[] = {
164a45f8853SCodrin Ciubotariu { .compatible = "mikroe,mikroe-proto", },
165a45f8853SCodrin Ciubotariu {},
166a45f8853SCodrin Ciubotariu };
167a45f8853SCodrin Ciubotariu MODULE_DEVICE_TABLE(of, snd_proto_of_match);
168a45f8853SCodrin Ciubotariu
169a45f8853SCodrin Ciubotariu static struct platform_driver snd_proto_driver = {
170a45f8853SCodrin Ciubotariu .driver = {
171a45f8853SCodrin Ciubotariu .name = "snd-mikroe-proto",
172a45f8853SCodrin Ciubotariu .of_match_table = snd_proto_of_match,
173a45f8853SCodrin Ciubotariu },
174a45f8853SCodrin Ciubotariu .probe = snd_proto_probe,
1752328c487SUwe Kleine-König .remove_new = snd_proto_remove,
176a45f8853SCodrin Ciubotariu };
177a45f8853SCodrin Ciubotariu
178a45f8853SCodrin Ciubotariu module_platform_driver(snd_proto_driver);
179a45f8853SCodrin Ciubotariu
180a45f8853SCodrin Ciubotariu MODULE_AUTHOR("Florian Meier");
181a45f8853SCodrin Ciubotariu MODULE_DESCRIPTION("ASoC Driver for PROTO board (WM8731)");
182a45f8853SCodrin Ciubotariu MODULE_LICENSE("GPL");
183