xref: /openbmc/linux/sound/soc/qcom/apq8016_sbc.c (revision 3a35093a)
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 DEFAULT_MCLK_RATE		9600000
34 
35 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
36 {
37 	struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
38 	struct snd_soc_dai *codec_dai;
39 	struct snd_soc_component *component;
40 	struct snd_soc_card *card = rtd->card;
41 	struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
42 	int i, rval;
43 
44 	switch (cpu_dai->id) {
45 	case MI2S_PRIMARY:
46 		writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
47 			pdata->spkr_iomux);
48 		break;
49 
50 	case MI2S_QUATERNARY:
51 		/* Configure the Quat MI2S to TLMM */
52 		writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
53 			MIC_CTRL_TLMM_SCLK_EN,
54 			pdata->mic_iomux);
55 		break;
56 	case MI2S_TERTIARY:
57 		writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
58 			MIC_CTRL_TLMM_SCLK_EN,
59 			pdata->mic_iomux);
60 
61 		break;
62 
63 	default:
64 		dev_err(card->dev, "unsupported cpu dai configuration\n");
65 		return -EINVAL;
66 
67 	}
68 
69 	if (!pdata->jack_setup) {
70 		struct snd_jack *jack;
71 
72 		rval = snd_soc_card_jack_new(card, "Headset Jack",
73 					     SND_JACK_HEADSET |
74 					     SND_JACK_HEADPHONE |
75 					     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
76 					     SND_JACK_BTN_2 | SND_JACK_BTN_3 |
77 					     SND_JACK_BTN_4,
78 					     &pdata->jack, NULL, 0);
79 
80 		if (rval < 0) {
81 			dev_err(card->dev, "Unable to add Headphone Jack\n");
82 			return rval;
83 		}
84 
85 		jack = pdata->jack.jack;
86 
87 		snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
88 		snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
89 		snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
90 		snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
91 		pdata->jack_setup = true;
92 	}
93 
94 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
95 
96 		component = codec_dai->component;
97 		/* Set default mclk for internal codec */
98 		rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
99 				       SND_SOC_CLOCK_IN);
100 		if (rval != 0 && rval != -ENOTSUPP) {
101 			dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
102 			return rval;
103 		}
104 		rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
105 		if (rval != 0 && rval != -ENOTSUPP) {
106 			dev_warn(card->dev, "Failed to set jack: %d\n", rval);
107 			return rval;
108 		}
109 	}
110 
111 	return 0;
112 }
113 
114 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
115 {
116 	struct snd_soc_dai_link *link;
117 	int i;
118 
119 	for_each_card_prelinks(card, i, link)
120 		link->init = apq8016_sbc_dai_init;
121 }
122 
123 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
124 
125 	SND_SOC_DAPM_MIC("Handset Mic", NULL),
126 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
127 	SND_SOC_DAPM_MIC("Secondary Mic", NULL),
128 	SND_SOC_DAPM_MIC("Digital Mic1", NULL),
129 	SND_SOC_DAPM_MIC("Digital Mic2", NULL),
130 };
131 
132 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
133 {
134 	struct device *dev = &pdev->dev;
135 	struct snd_soc_card *card;
136 	struct apq8016_sbc_data *data;
137 	struct resource *res;
138 	int ret;
139 
140 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
141 	if (!data)
142 		return -ENOMEM;
143 
144 	card = &data->card;
145 	card->dev = dev;
146 	card->owner = THIS_MODULE;
147 	card->dapm_widgets = apq8016_sbc_dapm_widgets;
148 	card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
149 
150 	ret = qcom_snd_parse_of(card);
151 	if (ret)
152 		return ret;
153 
154 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
155 	data->mic_iomux = devm_ioremap_resource(dev, res);
156 	if (IS_ERR(data->mic_iomux))
157 		return PTR_ERR(data->mic_iomux);
158 
159 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
160 	data->spkr_iomux = devm_ioremap_resource(dev, res);
161 	if (IS_ERR(data->spkr_iomux))
162 		return PTR_ERR(data->spkr_iomux);
163 
164 	snd_soc_card_set_drvdata(card, data);
165 
166 	apq8016_sbc_add_ops(card);
167 	return devm_snd_soc_register_card(&pdev->dev, card);
168 }
169 
170 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
171 	{ .compatible = "qcom,apq8016-sbc-sndcard" },
172 	{},
173 };
174 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
175 
176 static struct platform_driver apq8016_sbc_platform_driver = {
177 	.driver = {
178 		.name = "qcom-apq8016-sbc",
179 		.of_match_table = of_match_ptr(apq8016_sbc_device_id),
180 	},
181 	.probe = apq8016_sbc_platform_probe,
182 };
183 module_platform_driver(apq8016_sbc_platform_driver);
184 
185 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
186 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
187 MODULE_LICENSE("GPL v2");
188