xref: /openbmc/linux/sound/soc/qcom/apq8016_sbc.c (revision 93032e31)
1 /*
2  * Copyright (c) 2015 The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  */
14 
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/io.h>
19 #include <linux/of.h>
20 #include <linux/clk.h>
21 #include <linux/platform_device.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <dt-bindings/sound/apq8016-lpass.h>
26 
27 struct apq8016_sbc_data {
28 	void __iomem *mic_iomux;
29 	void __iomem *spkr_iomux;
30 	struct snd_soc_dai_link dai_link[];	/* dynamically allocated */
31 };
32 
33 #define MIC_CTRL_TER_WS_SLAVE_SEL	BIT(21)
34 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10	BIT(17)
35 #define MIC_CTRL_TLMM_SCLK_EN		BIT(1)
36 #define	SPKR_CTL_PRI_WS_SLAVE_SEL_11	(BIT(17) | BIT(16))
37 
38 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
39 {
40 	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
41 	struct snd_soc_card *card = rtd->card;
42 	struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
43 	int rval = 0;
44 
45 	switch (cpu_dai->id) {
46 	case MI2S_PRIMARY:
47 		writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
48 			pdata->spkr_iomux);
49 		break;
50 
51 	case MI2S_QUATERNARY:
52 		/* Configure the Quat MI2S to TLMM */
53 		writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
54 			MIC_CTRL_TLMM_SCLK_EN,
55 			pdata->mic_iomux);
56 		break;
57 	case MI2S_TERTIARY:
58 		writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
59 			MIC_CTRL_TLMM_SCLK_EN,
60 			pdata->mic_iomux);
61 
62 		break;
63 
64 	default:
65 		dev_err(card->dev, "unsupported cpu dai configuration\n");
66 		rval = -EINVAL;
67 		break;
68 
69 	}
70 
71 	return rval;
72 }
73 
74 static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
75 {
76 	struct device *dev = card->dev;
77 	struct snd_soc_dai_link *link;
78 	struct device_node *np, *codec, *cpu, *node  = dev->of_node;
79 	struct apq8016_sbc_data *data;
80 	int ret, num_links;
81 
82 	ret = snd_soc_of_parse_card_name(card, "qcom,model");
83 	if (ret) {
84 		dev_err(dev, "Error parsing card name: %d\n", ret);
85 		return ERR_PTR(ret);
86 	}
87 
88 	/* DAPM routes */
89 	if (of_property_read_bool(node, "qcom,audio-routing")) {
90 		ret = snd_soc_of_parse_audio_routing(card,
91 					"qcom,audio-routing");
92 		if (ret)
93 			return ERR_PTR(ret);
94 	}
95 
96 
97 	/* Populate links */
98 	num_links = of_get_child_count(node);
99 
100 	/* Allocate the private data and the DAI link array */
101 	data = devm_kzalloc(dev, sizeof(*data) + sizeof(*link) * num_links,
102 			    GFP_KERNEL);
103 	if (!data)
104 		return ERR_PTR(-ENOMEM);
105 
106 	card->dai_link	= &data->dai_link[0];
107 	card->num_links	= num_links;
108 
109 	link = data->dai_link;
110 
111 	for_each_child_of_node(node, np) {
112 		cpu = of_get_child_by_name(np, "cpu");
113 		codec = of_get_child_by_name(np, "codec");
114 
115 		if (!cpu || !codec) {
116 			dev_err(dev, "Can't find cpu/codec DT node\n");
117 			return ERR_PTR(-EINVAL);
118 		}
119 
120 		link->cpu_of_node = of_parse_phandle(cpu, "sound-dai", 0);
121 		if (!link->cpu_of_node) {
122 			dev_err(card->dev, "error getting cpu phandle\n");
123 			return ERR_PTR(-EINVAL);
124 		}
125 
126 		link->codec_of_node = of_parse_phandle(codec, "sound-dai", 0);
127 		if (!link->codec_of_node) {
128 			dev_err(card->dev, "error getting codec phandle\n");
129 			return ERR_PTR(-EINVAL);
130 		}
131 
132 		ret = snd_soc_of_get_dai_name(cpu, &link->cpu_dai_name);
133 		if (ret) {
134 			dev_err(card->dev, "error getting cpu dai name\n");
135 			return ERR_PTR(ret);
136 		}
137 
138 		ret = snd_soc_of_get_dai_name(codec, &link->codec_dai_name);
139 		if (ret) {
140 			dev_err(card->dev, "error getting codec dai name\n");
141 			return ERR_PTR(ret);
142 		}
143 
144 		link->platform_of_node = link->cpu_of_node;
145 		ret = of_property_read_string(np, "link-name", &link->name);
146 		if (ret) {
147 			dev_err(card->dev, "error getting codec dai_link name\n");
148 			return ERR_PTR(ret);
149 		}
150 
151 		link->stream_name = link->name;
152 		link->init = apq8016_sbc_dai_init;
153 		link++;
154 	}
155 
156 	return data;
157 }
158 
159 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
160 
161 	SND_SOC_DAPM_MIC("Handset Mic", NULL),
162 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
163 	SND_SOC_DAPM_MIC("Secondary Mic", NULL),
164 	SND_SOC_DAPM_MIC("Digital Mic1", NULL),
165 	SND_SOC_DAPM_MIC("Digital Mic2", NULL),
166 };
167 
168 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
169 {
170 	struct device *dev = &pdev->dev;
171 	struct snd_soc_card *card;
172 	struct apq8016_sbc_data *data;
173 	struct resource *res;
174 
175 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
176 	if (!card)
177 		return -ENOMEM;
178 
179 	card->dev = dev;
180 	card->dapm_widgets = apq8016_sbc_dapm_widgets;
181 	card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
182 	data = apq8016_sbc_parse_of(card);
183 	if (IS_ERR(data)) {
184 		dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
185 			PTR_ERR(data));
186 		return PTR_ERR(data);
187 	}
188 
189 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
190 	data->mic_iomux = devm_ioremap_resource(dev, res);
191 	if (IS_ERR(data->mic_iomux))
192 		return PTR_ERR(data->mic_iomux);
193 
194 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
195 	data->spkr_iomux = devm_ioremap_resource(dev, res);
196 	if (IS_ERR(data->spkr_iomux))
197 		return PTR_ERR(data->spkr_iomux);
198 
199 	platform_set_drvdata(pdev, data);
200 	snd_soc_card_set_drvdata(card, data);
201 
202 	return devm_snd_soc_register_card(&pdev->dev, card);
203 }
204 
205 static const struct of_device_id apq8016_sbc_device_id[]  = {
206 	{ .compatible = "qcom,apq8016-sbc-sndcard" },
207 	{},
208 };
209 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
210 
211 static struct platform_driver apq8016_sbc_platform_driver = {
212 	.driver = {
213 		.name = "qcom-apq8016-sbc",
214 		.of_match_table = of_match_ptr(apq8016_sbc_device_id),
215 	},
216 	.probe = apq8016_sbc_platform_probe,
217 };
218 module_platform_driver(apq8016_sbc_platform_driver);
219 
220 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
221 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
222 MODULE_LICENSE("GPL v2");
223