blob: 2ef090f4af9e92bbbc39d724eed34543a07ed7c6 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +01002/*
3 * Copyright (c) 2015 The Linux Foundation. All rights reserved.
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +01004 */
5
6#include <linux/device.h>
7#include <linux/module.h>
8#include <linux/kernel.h>
9#include <linux/io.h>
10#include <linux/of.h>
11#include <linux/clk.h>
12#include <linux/platform_device.h>
13#include <sound/pcm.h>
14#include <sound/pcm_params.h>
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020015#include <sound/jack.h>
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010016#include <sound/soc.h>
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020017#include <uapi/linux/input-event-codes.h>
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010018#include <dt-bindings/sound/apq8016-lpass.h>
19
20struct apq8016_sbc_data {
21 void __iomem *mic_iomux;
22 void __iomem *spkr_iomux;
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020023 struct snd_soc_jack jack;
24 bool jack_setup;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010025 struct snd_soc_dai_link dai_link[]; /* dynamically allocated */
26};
27
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000028#define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010029#define MIC_CTRL_QUA_WS_SLAVE_SEL_10 BIT(17)
30#define MIC_CTRL_TLMM_SCLK_EN BIT(1)
31#define SPKR_CTL_PRI_WS_SLAVE_SEL_11 (BIT(17) | BIT(16))
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020032#define DEFAULT_MCLK_RATE 9600000
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010033
34static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
35{
Kuninori Morimoto6e3a98b2020-03-23 14:20:01 +090036 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimotocf4dae02020-02-19 15:56:09 +090037 struct snd_soc_dai *codec_dai;
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +000038 struct snd_soc_component *component;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010039 struct snd_soc_card *card = rtd->card;
40 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020041 int i, rval;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010042
43 switch (cpu_dai->id) {
44 case MI2S_PRIMARY:
45 writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
46 pdata->spkr_iomux);
47 break;
48
49 case MI2S_QUATERNARY:
50 /* Configure the Quat MI2S to TLMM */
51 writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
52 MIC_CTRL_TLMM_SCLK_EN,
53 pdata->mic_iomux);
54 break;
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000055 case MI2S_TERTIARY:
56 writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
57 MIC_CTRL_TLMM_SCLK_EN,
58 pdata->mic_iomux);
59
60 break;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010061
62 default:
63 dev_err(card->dev, "unsupported cpu dai configuration\n");
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020064 return -EINVAL;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010065
66 }
67
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020068 if (!pdata->jack_setup) {
69 struct snd_jack *jack;
70
71 rval = snd_soc_card_jack_new(card, "Headset Jack",
72 SND_JACK_HEADSET |
73 SND_JACK_HEADPHONE |
74 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
75 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
76 SND_JACK_BTN_4,
77 &pdata->jack, NULL, 0);
78
79 if (rval < 0) {
80 dev_err(card->dev, "Unable to add Headphone Jack\n");
81 return rval;
82 }
83
84 jack = pdata->jack.jack;
85
Benson Leung5f6d1df2017-11-22 12:56:43 -080086 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020087 snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
88 snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
89 snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
90 pdata->jack_setup = true;
91 }
92
Kuninori Morimotoc998ee32020-03-09 13:07:57 +090093 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020094
Kuninori Morimotocf4dae02020-02-19 15:56:09 +090095 component = codec_dai->component;
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020096 /* Set default mclk for internal codec */
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +000097 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020098 SND_SOC_CLOCK_IN);
99 if (rval != 0 && rval != -ENOTSUPP) {
100 dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
101 return rval;
102 }
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +0000103 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +0200104 if (rval != 0 && rval != -ENOTSUPP) {
105 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
106 return rval;
107 }
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200108 }
109
110 return 0;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100111}
112
113static struct apq8016_sbc_data *apq8016_sbc_parse_of(struct snd_soc_card *card)
114{
115 struct device *dev = card->dev;
116 struct snd_soc_dai_link *link;
117 struct device_node *np, *codec, *cpu, *node = dev->of_node;
118 struct apq8016_sbc_data *data;
Kuninori Morimoto98b232c2019-06-06 13:17:04 +0900119 struct snd_soc_dai_link_component *dlc;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100120 int ret, num_links;
121
122 ret = snd_soc_of_parse_card_name(card, "qcom,model");
123 if (ret) {
124 dev_err(dev, "Error parsing card name: %d\n", ret);
125 return ERR_PTR(ret);
126 }
127
Srinivas Kandagatla05f90332016-09-06 10:57:43 +0100128 /* DAPM routes */
129 if (of_property_read_bool(node, "qcom,audio-routing")) {
130 ret = snd_soc_of_parse_audio_routing(card,
131 "qcom,audio-routing");
132 if (ret)
133 return ERR_PTR(ret);
134 }
135
136
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100137 /* Populate links */
138 num_links = of_get_child_count(node);
139
140 /* Allocate the private data and the DAI link array */
Kees Cook0ed2dd02018-05-08 16:08:53 -0700141 data = devm_kzalloc(dev,
142 struct_size(data, dai_link, num_links),
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100143 GFP_KERNEL);
144 if (!data)
145 return ERR_PTR(-ENOMEM);
146
147 card->dai_link = &data->dai_link[0];
148 card->num_links = num_links;
149
150 link = data->dai_link;
151
152 for_each_child_of_node(node, np) {
Stephan Gerhold8201f112019-07-22 15:03:52 +0200153 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
154 if (!dlc)
155 return ERR_PTR(-ENOMEM);
156
157 link->cpus = &dlc[0];
158 link->platforms = &dlc[1];
159
160 link->num_cpus = 1;
161 link->num_platforms = 1;
162
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100163 cpu = of_get_child_by_name(np, "cpu");
164 codec = of_get_child_by_name(np, "codec");
165
166 if (!cpu || !codec) {
167 dev_err(dev, "Can't find cpu/codec DT node\n");
Takashi Iwai8d166722019-02-19 16:46:50 +0100168 ret = -EINVAL;
169 goto error;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100170 }
171
Kuninori Morimoto98b232c2019-06-06 13:17:04 +0900172 link->cpus->of_node = of_parse_phandle(cpu, "sound-dai", 0);
173 if (!link->cpus->of_node) {
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100174 dev_err(card->dev, "error getting cpu phandle\n");
Takashi Iwai8d166722019-02-19 16:46:50 +0100175 ret = -EINVAL;
176 goto error;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100177 }
178
Kuninori Morimoto98b232c2019-06-06 13:17:04 +0900179 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100180 if (ret) {
181 dev_err(card->dev, "error getting cpu dai name\n");
Takashi Iwai8d166722019-02-19 16:46:50 +0100182 goto error;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100183 }
184
Srinivas Kandagatla09065f82016-10-20 15:20:48 +0100185 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
186
187 if (ret < 0) {
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100188 dev_err(card->dev, "error getting codec dai name\n");
Takashi Iwai8d166722019-02-19 16:46:50 +0100189 goto error;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100190 }
191
Kuninori Morimoto291728a2019-06-28 10:47:46 +0900192 link->platforms->of_node = link->cpus->of_node;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100193 ret = of_property_read_string(np, "link-name", &link->name);
194 if (ret) {
195 dev_err(card->dev, "error getting codec dai_link name\n");
Takashi Iwai8d166722019-02-19 16:46:50 +0100196 goto error;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100197 }
198
199 link->stream_name = link->name;
200 link->init = apq8016_sbc_dai_init;
201 link++;
Takashi Iwai8d166722019-02-19 16:46:50 +0100202
203 of_node_put(cpu);
204 of_node_put(codec);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100205 }
206
207 return data;
Takashi Iwai8d166722019-02-19 16:46:50 +0100208
209 error:
210 of_node_put(np);
211 of_node_put(cpu);
212 of_node_put(codec);
213 return ERR_PTR(ret);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100214}
215
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100216static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
217
218 SND_SOC_DAPM_MIC("Handset Mic", NULL),
219 SND_SOC_DAPM_MIC("Headset Mic", NULL),
220 SND_SOC_DAPM_MIC("Secondary Mic", NULL),
221 SND_SOC_DAPM_MIC("Digital Mic1", NULL),
222 SND_SOC_DAPM_MIC("Digital Mic2", NULL),
223};
224
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100225static int apq8016_sbc_platform_probe(struct platform_device *pdev)
226{
227 struct device *dev = &pdev->dev;
228 struct snd_soc_card *card;
229 struct apq8016_sbc_data *data;
230 struct resource *res;
231
232 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
233 if (!card)
234 return -ENOMEM;
235
236 card->dev = dev;
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100237 card->dapm_widgets = apq8016_sbc_dapm_widgets;
238 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100239 data = apq8016_sbc_parse_of(card);
240 if (IS_ERR(data)) {
241 dev_err(&pdev->dev, "Error resolving dai links: %ld\n",
242 PTR_ERR(data));
243 return PTR_ERR(data);
244 }
245
246 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
247 data->mic_iomux = devm_ioremap_resource(dev, res);
248 if (IS_ERR(data->mic_iomux))
249 return PTR_ERR(data->mic_iomux);
250
251 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
252 data->spkr_iomux = devm_ioremap_resource(dev, res);
253 if (IS_ERR(data->spkr_iomux))
254 return PTR_ERR(data->spkr_iomux);
255
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100256 snd_soc_card_set_drvdata(card, data);
257
258 return devm_snd_soc_register_card(&pdev->dev, card);
259}
260
261static const struct of_device_id apq8016_sbc_device_id[] = {
262 { .compatible = "qcom,apq8016-sbc-sndcard" },
263 {},
264};
265MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
266
267static struct platform_driver apq8016_sbc_platform_driver = {
268 .driver = {
269 .name = "qcom-apq8016-sbc",
270 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
271 },
272 .probe = apq8016_sbc_platform_probe,
273};
274module_platform_driver(apq8016_sbc_platform_driver);
275
276MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
277MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
278MODULE_LICENSE("GPL v2");