xref: /openbmc/linux/sound/soc/qcom/apq8016_sbc.c (revision 2326f3cd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 The Linux Foundation. All rights reserved.
4  */
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>
15 #include <sound/jack.h>
16 #include <sound/soc.h>
17 #include <uapi/linux/input-event-codes.h>
18 #include <dt-bindings/sound/apq8016-lpass.h>
19 #include "common.h"
20 
21 struct apq8016_sbc_data {
22 	struct snd_soc_card card;
23 	void __iomem *mic_iomux;
24 	void __iomem *spkr_iomux;
25 	struct snd_soc_jack jack;
26 	bool jack_setup;
27 };
28 
29 #define MIC_CTRL_TER_WS_SLAVE_SEL	BIT(21)
30 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10	BIT(17)
31 #define MIC_CTRL_TLMM_SCLK_EN		BIT(1)
32 #define	SPKR_CTL_PRI_WS_SLAVE_SEL_11	(BIT(17) | BIT(16))
33 #define SPKR_CTL_TLMM_MCLK_EN		BIT(1)
34 #define SPKR_CTL_TLMM_SCLK_EN		BIT(2)
35 #define SPKR_CTL_TLMM_DATA1_EN		BIT(3)
36 #define SPKR_CTL_TLMM_WS_OUT_SEL_MASK	GENMASK(7, 6)
37 #define SPKR_CTL_TLMM_WS_OUT_SEL_SEC	BIT(6)
38 #define SPKR_CTL_TLMM_WS_EN_SEL_MASK	GENMASK(19, 18)
39 #define SPKR_CTL_TLMM_WS_EN_SEL_SEC	BIT(18)
40 #define DEFAULT_MCLK_RATE		9600000
41 
42 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
43 {
44 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
45 	struct snd_soc_dai *codec_dai;
46 	struct snd_soc_component *component;
47 	struct snd_soc_card *card = rtd->card;
48 	struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
49 	int i, rval;
50 	u32 value;
51 
52 	switch (cpu_dai->id) {
53 	case MI2S_PRIMARY:
54 		writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
55 			pdata->spkr_iomux);
56 		break;
57 
58 	case MI2S_QUATERNARY:
59 		/* Configure the Quat MI2S to TLMM */
60 		writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
61 			MIC_CTRL_TLMM_SCLK_EN,
62 			pdata->mic_iomux);
63 		break;
64 	case MI2S_SECONDARY:
65 		/* Clear TLMM_WS_OUT_SEL and TLMM_WS_EN_SEL fields */
66 		value = readl(pdata->spkr_iomux) &
67 			~(SPKR_CTL_TLMM_WS_OUT_SEL_MASK | SPKR_CTL_TLMM_WS_EN_SEL_MASK);
68 		/* Configure the Sec MI2S to TLMM */
69 		writel(value | SPKR_CTL_TLMM_MCLK_EN | SPKR_CTL_TLMM_SCLK_EN |
70 			SPKR_CTL_TLMM_DATA1_EN | SPKR_CTL_TLMM_WS_OUT_SEL_SEC |
71 			SPKR_CTL_TLMM_WS_EN_SEL_SEC, pdata->spkr_iomux);
72 		break;
73 	case MI2S_TERTIARY:
74 		writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
75 			MIC_CTRL_TLMM_SCLK_EN,
76 			pdata->mic_iomux);
77 
78 		break;
79 
80 	default:
81 		dev_err(card->dev, "unsupported cpu dai configuration\n");
82 		return -EINVAL;
83 
84 	}
85 
86 	if (!pdata->jack_setup) {
87 		struct snd_jack *jack;
88 
89 		rval = snd_soc_card_jack_new(card, "Headset Jack",
90 					     SND_JACK_HEADSET |
91 					     SND_JACK_HEADPHONE |
92 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
93 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
94 					     SND_JACK_BTN_4,
95 					     &pdata->jack, NULL, 0);
96 
97 		if (rval < 0) {
98 			dev_err(card->dev, "Unable to add Headphone Jack\n");
99 			return rval;
100 		}
101 
102 		jack = pdata->jack.jack;
103 
104 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
105 		snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
106 		snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
107 		snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
108 		pdata->jack_setup = true;
109 	}
110 
111 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
112 
113 		component = codec_dai->component;
114 		/* Set default mclk for internal codec */
115 		rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
116 				       SND_SOC_CLOCK_IN);
117 		if (rval != 0 && rval != -ENOTSUPP) {
118 			dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
119 			return rval;
120 		}
121 		rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
122 		if (rval != 0 && rval != -ENOTSUPP) {
123 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
124 			return rval;
125 		}
126 	}
127 
128 	return 0;
129 }
130 
131 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
132 {
133 	struct snd_soc_dai_link *link;
134 	int i;
135 
136 	for_each_card_prelinks(card, i, link)
137 		link->init = apq8016_sbc_dai_init;
138 }
139 
140 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
141 
142 	SND_SOC_DAPM_MIC("Handset Mic", NULL),
143 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
144 	SND_SOC_DAPM_MIC("Secondary Mic", NULL),
145 	SND_SOC_DAPM_MIC("Digital Mic1", NULL),
146 	SND_SOC_DAPM_MIC("Digital Mic2", NULL),
147 };
148 
149 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
150 {
151 	struct device *dev = &pdev->dev;
152 	struct snd_soc_card *card;
153 	struct apq8016_sbc_data *data;
154 	int ret;
155 
156 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
157 	if (!data)
158 		return -ENOMEM;
159 
160 	card = &data->card;
161 	card->dev = dev;
162 	card->owner = THIS_MODULE;
163 	card->dapm_widgets = apq8016_sbc_dapm_widgets;
164 	card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
165 
166 	ret = qcom_snd_parse_of(card);
167 	if (ret)
168 		return ret;
169 
170 	data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
171 	if (IS_ERR(data->mic_iomux))
172 		return PTR_ERR(data->mic_iomux);
173 
174 	data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
175 	if (IS_ERR(data->spkr_iomux))
176 		return PTR_ERR(data->spkr_iomux);
177 
178 	snd_soc_card_set_drvdata(card, data);
179 
180 	apq8016_sbc_add_ops(card);
181 	return devm_snd_soc_register_card(&pdev->dev, card);
182 }
183 
184 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
185 	{ .compatible = "qcom,apq8016-sbc-sndcard" },
186 	{},
187 };
188 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
189 
190 static struct platform_driver apq8016_sbc_platform_driver = {
191 	.driver = {
192 		.name = "qcom-apq8016-sbc",
193 		.of_match_table = of_match_ptr(apq8016_sbc_device_id),
194 	},
195 	.probe = apq8016_sbc_platform_probe,
196 };
197 module_platform_driver(apq8016_sbc_platform_driver);
198 
199 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
200 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
201 MODULE_LICENSE("GPL v2");
202