ASoC: qcom: sc7180: initialize the "no_headphone" variable
[linux-2.6-microblaze.git] / sound / soc / qcom / sc7180.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
4 //
5 // sc7180.c -- ALSA SoC Machine driver for SC7180
6
7 #include <dt-bindings/sound/sc7180-lpass.h>
8 #include <linux/gpio.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/module.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/soc.h>
17 #include <uapi/linux/input-event-codes.h>
18
19 #include "../codecs/rt5682.h"
20 #include "common.h"
21 #include "lpass.h"
22
23 #define DEFAULT_MCLK_RATE               19200000
24 #define RT5682_PLL1_FREQ (48000 * 512)
25
26 #define DRIVER_NAME "SC7180"
27
28 struct sc7180_snd_data {
29         struct snd_soc_card card;
30         u32 pri_mi2s_clk_count;
31         struct snd_soc_jack hs_jack;
32         struct snd_soc_jack hdmi_jack;
33         struct gpio_desc *dmic_sel;
34         int dmic_switch;
35 };
36
37 static void sc7180_jack_free(struct snd_jack *jack)
38 {
39         struct snd_soc_component *component = jack->private_data;
40
41         snd_soc_component_set_jack(component, NULL, NULL);
42 }
43
44 static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd)
45 {
46         struct snd_soc_card *card = rtd->card;
47         struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
48         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
49         struct snd_soc_component *component = codec_dai->component;
50         struct snd_jack *jack;
51         int rval;
52
53         rval = snd_soc_card_jack_new(
54                         card, "Headset Jack",
55                         SND_JACK_HEADSET |
56                         SND_JACK_HEADPHONE |
57                         SND_JACK_BTN_0 | SND_JACK_BTN_1 |
58                         SND_JACK_BTN_2 | SND_JACK_BTN_3,
59                         &pdata->hs_jack, NULL, 0);
60
61         if (rval < 0) {
62                 dev_err(card->dev, "Unable to add Headset Jack\n");
63                 return rval;
64         }
65
66         jack = pdata->hs_jack.jack;
67
68         snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
69         snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
70         snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
71         snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
72
73         jack->private_data = component;
74         jack->private_free = sc7180_jack_free;
75
76         return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
77 }
78
79 static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd)
80 {
81         struct snd_soc_card *card = rtd->card;
82         struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
83         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
84         struct snd_soc_component *component = codec_dai->component;
85         struct snd_jack *jack;
86         int rval;
87
88         rval = snd_soc_card_jack_new(
89                         card, "HDMI Jack",
90                         SND_JACK_LINEOUT,
91                         &pdata->hdmi_jack, NULL, 0);
92
93         if (rval < 0) {
94                 dev_err(card->dev, "Unable to add HDMI Jack\n");
95                 return rval;
96         }
97
98         jack = pdata->hdmi_jack.jack;
99         jack->private_data = component;
100         jack->private_free = sc7180_jack_free;
101
102         return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
103 }
104
105 static int sc7180_init(struct snd_soc_pcm_runtime *rtd)
106 {
107         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
108
109         switch (cpu_dai->id) {
110         case MI2S_PRIMARY:
111                 return sc7180_headset_init(rtd);
112         case MI2S_SECONDARY:
113                 return 0;
114         case LPASS_DP_RX:
115                 return sc7180_hdmi_init(rtd);
116         default:
117                 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
118                         cpu_dai->id);
119                 return -EINVAL;
120         }
121         return 0;
122 }
123
124 static int sc7180_snd_startup(struct snd_pcm_substream *substream)
125 {
126         struct snd_soc_pcm_runtime *rtd = substream->private_data;
127         struct snd_soc_card *card = rtd->card;
128         struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
129         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
130         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
131         int ret;
132
133         switch (cpu_dai->id) {
134         case MI2S_PRIMARY:
135                 if (++data->pri_mi2s_clk_count == 1) {
136                         snd_soc_dai_set_sysclk(cpu_dai,
137                                                LPASS_MCLK0,
138                                                DEFAULT_MCLK_RATE,
139                                                SNDRV_PCM_STREAM_PLAYBACK);
140                 }
141
142                 snd_soc_dai_set_fmt(codec_dai,
143                                     SND_SOC_DAIFMT_CBS_CFS |
144                                     SND_SOC_DAIFMT_NB_NF |
145                                     SND_SOC_DAIFMT_I2S);
146
147                 /* Configure PLL1 for codec */
148                 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5682_PLL1_S_MCLK,
149                                           DEFAULT_MCLK_RATE, RT5682_PLL1_FREQ);
150                 if (ret) {
151                         dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
152                         return ret;
153                 }
154
155                 /* Configure sysclk for codec */
156                 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1,
157                                              RT5682_PLL1_FREQ,
158                                              SND_SOC_CLOCK_IN);
159                 if (ret)
160                         dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
161                                 ret);
162
163                 break;
164         case MI2S_SECONDARY:
165                 break;
166         case LPASS_DP_RX:
167                 break;
168         default:
169                 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
170                         cpu_dai->id);
171                 return -EINVAL;
172         }
173         return 0;
174 }
175
176 static int dmic_get(struct snd_kcontrol *kcontrol,
177                     struct snd_ctl_elem_value *ucontrol)
178 {
179         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
180         struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
181
182         ucontrol->value.integer.value[0] = data->dmic_switch;
183         return 0;
184 }
185
186 static int dmic_set(struct snd_kcontrol *kcontrol,
187                     struct snd_ctl_elem_value *ucontrol)
188 {
189         struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
190         struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
191
192         data->dmic_switch = ucontrol->value.integer.value[0];
193         gpiod_set_value(data->dmic_sel, data->dmic_switch);
194         return 0;
195 }
196
197 static void sc7180_snd_shutdown(struct snd_pcm_substream *substream)
198 {
199         struct snd_soc_pcm_runtime *rtd = substream->private_data;
200         struct snd_soc_card *card = rtd->card;
201         struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
202         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
203
204         switch (cpu_dai->id) {
205         case MI2S_PRIMARY:
206                 if (--data->pri_mi2s_clk_count == 0) {
207                         snd_soc_dai_set_sysclk(cpu_dai,
208                                                LPASS_MCLK0,
209                                                0,
210                                                SNDRV_PCM_STREAM_PLAYBACK);
211                 }
212                 break;
213         case MI2S_SECONDARY:
214                 break;
215         case LPASS_DP_RX:
216                 break;
217         default:
218                 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
219                         cpu_dai->id);
220                 break;
221         }
222 }
223
224 static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd)
225 {
226         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
227
228         switch (cpu_dai->id) {
229         case MI2S_PRIMARY:
230                 return 0;
231         case MI2S_SECONDARY:
232                 return 0;
233         case LPASS_DP_RX:
234                 return sc7180_hdmi_init(rtd);
235         default:
236                 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
237                         cpu_dai->id);
238                 return -EINVAL;
239         }
240         return 0;
241 }
242
243 static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream)
244 {
245         struct snd_soc_pcm_runtime *rtd = substream->private_data;
246         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
247         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
248
249         switch (cpu_dai->id) {
250         case MI2S_PRIMARY:
251                 snd_soc_dai_set_fmt(codec_dai,
252                                     SND_SOC_DAIFMT_CBS_CFS |
253                                     SND_SOC_DAIFMT_NB_NF |
254                                     SND_SOC_DAIFMT_I2S);
255
256                 break;
257         case MI2S_SECONDARY:
258                 break;
259         case LPASS_DP_RX:
260                 break;
261         default:
262                 dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
263                         cpu_dai->id);
264                 return -EINVAL;
265         }
266         return 0;
267 }
268
269 static const struct snd_soc_ops sc7180_ops = {
270         .startup = sc7180_snd_startup,
271         .shutdown = sc7180_snd_shutdown,
272 };
273
274 static const struct snd_soc_ops sc7180_adau7002_ops = {
275         .startup = sc7180_adau7002_snd_startup,
276 };
277
278 static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = {
279         SND_SOC_DAPM_HP("Headphone Jack", NULL),
280         SND_SOC_DAPM_MIC("Headset Mic", NULL),
281 };
282
283 static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = {
284         SND_SOC_DAPM_MIC("DMIC", NULL),
285 };
286
287 static const char * const dmic_mux_text[] = {
288         "Front Mic",
289         "Rear Mic",
290 };
291
292 static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum,
293                             SND_SOC_NOPM, 0, dmic_mux_text);
294
295 static const struct snd_kcontrol_new sc7180_dmic_mux_control =
296         SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum,
297                           dmic_get, dmic_set);
298
299 static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = {
300         SND_SOC_DAPM_HP("Headphone Jack", NULL),
301         SND_SOC_DAPM_MIC("Headset Mic", NULL),
302         SND_SOC_DAPM_MIC("DMIC", NULL),
303         SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control),
304 };
305
306 static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = {
307         {"Dmic Mux", "Front Mic", "DMIC"},
308         {"Dmic Mux", "Rear Mic", "DMIC"},
309 };
310
311 static int sc7180_snd_platform_probe(struct platform_device *pdev)
312 {
313         struct snd_soc_card *card;
314         struct sc7180_snd_data *data;
315         struct device *dev = &pdev->dev;
316         struct snd_soc_dai_link *link;
317         int ret;
318         int i;
319         bool no_headphone = false;
320
321         /* Allocate the private data */
322         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
323         if (!data)
324                 return -ENOMEM;
325
326         card = &data->card;
327         snd_soc_card_set_drvdata(card, data);
328
329         card->owner = THIS_MODULE;
330         card->driver_name = DRIVER_NAME;
331         card->dev = dev;
332         card->dapm_widgets = sc7180_snd_widgets;
333         card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets);
334
335         if (of_property_read_bool(dev->of_node, "dmic-gpios")) {
336                 card->dapm_widgets = sc7180_snd_dual_mic_widgets,
337                 card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets),
338                 card->dapm_routes = sc7180_snd_dual_mic_audio_route,
339                 card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route),
340                 data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
341                 if (IS_ERR(data->dmic_sel)) {
342                         dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel));
343                         return PTR_ERR(data->dmic_sel);
344                 }
345         }
346
347         if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) {
348                 no_headphone = true;
349                 card->dapm_widgets = sc7180_adau7002_snd_widgets;
350                 card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets);
351         }
352
353         ret = qcom_snd_parse_of(card);
354         if (ret)
355                 return ret;
356
357         for_each_card_prelinks(card, i, link) {
358                 if (no_headphone) {
359                         link->ops = &sc7180_adau7002_ops;
360                         link->init = sc7180_adau7002_init;
361                 } else {
362                         link->ops = &sc7180_ops;
363                         link->init = sc7180_init;
364                 }
365         }
366
367         return devm_snd_soc_register_card(dev, card);
368 }
369
370 static const struct of_device_id sc7180_snd_device_id[]  = {
371         {.compatible = "google,sc7180-trogdor"},
372         {.compatible = "google,sc7180-coachz"},
373         {},
374 };
375 MODULE_DEVICE_TABLE(of, sc7180_snd_device_id);
376
377 static struct platform_driver sc7180_snd_driver = {
378         .probe = sc7180_snd_platform_probe,
379         .driver = {
380                 .name = "msm-snd-sc7180",
381                 .of_match_table = sc7180_snd_device_id,
382                 .pm = &snd_soc_pm_ops,
383         },
384 };
385 module_platform_driver(sc7180_snd_driver);
386
387 MODULE_DESCRIPTION("sc7180 ASoC Machine Driver");
388 MODULE_LICENSE("GPL v2");