xref: /openbmc/linux/sound/soc/qcom/apq8016_sbc.c (revision aa0dc6a7)
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 	int ret;
138 
139 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
140 	if (!data)
141 		return -ENOMEM;
142 
143 	card = &data->card;
144 	card->dev = dev;
145 	card->owner = THIS_MODULE;
146 	card->dapm_widgets = apq8016_sbc_dapm_widgets;
147 	card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
148 
149 	ret = qcom_snd_parse_of(card);
150 	if (ret)
151 		return ret;
152 
153 	data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
154 	if (IS_ERR(data->mic_iomux))
155 		return PTR_ERR(data->mic_iomux);
156 
157 	data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
158 	if (IS_ERR(data->spkr_iomux))
159 		return PTR_ERR(data->spkr_iomux);
160 
161 	snd_soc_card_set_drvdata(card, data);
162 
163 	apq8016_sbc_add_ops(card);
164 	return devm_snd_soc_register_card(&pdev->dev, card);
165 }
166 
167 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
168 	{ .compatible = "qcom,apq8016-sbc-sndcard" },
169 	{},
170 };
171 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
172 
173 static struct platform_driver apq8016_sbc_platform_driver = {
174 	.driver = {
175 		.name = "qcom-apq8016-sbc",
176 		.of_match_table = of_match_ptr(apq8016_sbc_device_id),
177 	},
178 	.probe = apq8016_sbc_platform_probe,
179 };
180 module_platform_driver(apq8016_sbc_platform_driver);
181 
182 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
183 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
184 MODULE_LICENSE("GPL v2");
185