blob: 083413abc2f694bedc98237bc6d3e45ebe0958ca [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>
Stephan Gerhold118205d22020-07-23 20:39:03 +020019#include "common.h"
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010020
21struct apq8016_sbc_data {
Stephan Gerhold118205d22020-07-23 20:39:03 +020022 struct snd_soc_card card;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010023 void __iomem *mic_iomux;
24 void __iomem *spkr_iomux;
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020025 struct snd_soc_jack jack;
26 bool jack_setup;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010027};
28
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000029#define MIC_CTRL_TER_WS_SLAVE_SEL BIT(21)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010030#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))
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020033#define DEFAULT_MCLK_RATE 9600000
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010034
35static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
36{
Kuninori Morimoto6e3a98b2020-03-23 14:20:01 +090037 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
Kuninori Morimotocf4dae02020-02-19 15:56:09 +090038 struct snd_soc_dai *codec_dai;
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +000039 struct snd_soc_component *component;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010040 struct snd_soc_card *card = rtd->card;
41 struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020042 int i, rval;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010043
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;
Srinivas Kandagatlabbedefb2016-02-11 12:18:45 +000056 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;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010062
63 default:
64 dev_err(card->dev, "unsupported cpu dai configuration\n");
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020065 return -EINVAL;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +010066
67 }
68
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020069 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
Benson Leung5f6d1df2017-11-22 12:56:43 -080087 snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +020088 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
Kuninori Morimotoc998ee32020-03-09 13:07:57 +090094 for_each_rtd_codec_dais(rtd, i, codec_dai) {
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020095
Kuninori Morimotocf4dae02020-02-19 15:56:09 +090096 component = codec_dai->component;
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020097 /* Set default mclk for internal codec */
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +000098 rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
Srinivas Kandagatla334822a2017-08-02 15:17:47 +020099 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 }
Kuninori Morimoto96e1b9e2017-12-05 04:24:13 +0000104 rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
Srinivas Kandagatlab47b91c2017-08-17 10:02:11 +0200105 if (rval != 0 && rval != -ENOTSUPP) {
106 dev_warn(card->dev, "Failed to set jack: %d\n", rval);
107 return rval;
108 }
Srinivas Kandagatla334822a2017-08-02 15:17:47 +0200109 }
110
111 return 0;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100112}
113
Stephan Gerhold118205d22020-07-23 20:39:03 +0200114static void apq8016_sbc_add_ops(struct snd_soc_card *card)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100115{
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100116 struct snd_soc_dai_link *link;
Stephan Gerhold118205d22020-07-23 20:39:03 +0200117 int i;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100118
Stephan Gerhold118205d22020-07-23 20:39:03 +0200119 for_each_card_prelinks(card, i, link)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100120 link->init = apq8016_sbc_dai_init;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100121}
122
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100123static 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
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100132static 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;
Stephan Gerhold118205d22020-07-23 20:39:03 +0200138 int ret;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100139
Stephan Gerhold118205d22020-07-23 20:39:03 +0200140 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
141 if (!data)
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100142 return -ENOMEM;
143
Stephan Gerhold118205d22020-07-23 20:39:03 +0200144 card = &data->card;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100145 card->dev = dev;
Srinivas Kandagatla11903002016-09-06 10:57:42 +0100146 card->dapm_widgets = apq8016_sbc_dapm_widgets;
147 card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
Stephan Gerhold118205d22020-07-23 20:39:03 +0200148
149 ret = qcom_snd_parse_of(card);
150 if (ret)
151 return ret;
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100152
153 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mic-iomux");
154 data->mic_iomux = devm_ioremap_resource(dev, res);
155 if (IS_ERR(data->mic_iomux))
156 return PTR_ERR(data->mic_iomux);
157
158 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "spkr-iomux");
159 data->spkr_iomux = devm_ioremap_resource(dev, res);
160 if (IS_ERR(data->spkr_iomux))
161 return PTR_ERR(data->spkr_iomux);
162
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100163 snd_soc_card_set_drvdata(card, data);
164
Stephan Gerhold118205d22020-07-23 20:39:03 +0200165 apq8016_sbc_add_ops(card);
Srinivas Kandagatlabdb052e2015-06-10 13:15:54 +0100166 return devm_snd_soc_register_card(&pdev->dev, card);
167}
168
169static const struct of_device_id apq8016_sbc_device_id[] = {
170 { .compatible = "qcom,apq8016-sbc-sndcard" },
171 {},
172};
173MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
174
175static struct platform_driver apq8016_sbc_platform_driver = {
176 .driver = {
177 .name = "qcom-apq8016-sbc",
178 .of_match_table = of_match_ptr(apq8016_sbc_device_id),
179 },
180 .probe = apq8016_sbc_platform_probe,
181};
182module_platform_driver(apq8016_sbc_platform_driver);
183
184MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
185MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
186MODULE_LICENSE("GPL v2");