ASoC: amd: Adding DAI LINK for rt1015 codec
[linux-2.6-microblaze.git] / sound / soc / amd / acp3x-rt5682-max9836.c
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec.
4 //
5 //Copyright 2016 Advanced Micro Devices, Inc.
6
7 #include <sound/core.h>
8 #include <sound/soc.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc-dapm.h>
12 #include <sound/jack.h>
13 #include <linux/clk.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/io.h>
20 #include <linux/acpi.h>
21
22 #include "raven/acp3x.h"
23 #include "../codecs/rt5682.h"
24 #include "../codecs/rt1015.h"
25
26 #define PCO_PLAT_CLK 48000000
27 #define RT5682_PLL_FREQ (48000 * 512)
28 #define DUAL_CHANNEL            2
29
30 static struct snd_soc_jack pco_jack;
31 static struct clk *rt5682_dai_wclk;
32 static struct clk *rt5682_dai_bclk;
33 static struct gpio_desc *dmic_sel;
34 void *soc_is_rltk_max(struct device *dev);
35
36 enum {
37         RT5682 = 0,
38         MAX,
39         EC,
40 };
41
42 static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd)
43 {
44         int ret;
45         struct snd_soc_card *card = rtd->card;
46         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
47         struct snd_soc_component *component = codec_dai->component;
48
49         dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
50
51         /* set rt5682 dai fmt */
52         ret =  snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
53                         | SND_SOC_DAIFMT_NB_NF
54                         | SND_SOC_DAIFMT_CBM_CFM);
55         if (ret < 0) {
56                 dev_err(rtd->card->dev,
57                                 "Failed to set rt5682 dai fmt: %d\n", ret);
58                 return ret;
59         }
60
61         /* set codec PLL */
62         ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK,
63                                   PCO_PLAT_CLK, RT5682_PLL_FREQ);
64         if (ret < 0) {
65                 dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret);
66                 return ret;
67         }
68
69         /* Set codec sysclk */
70         ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2,
71                         RT5682_PLL_FREQ, SND_SOC_CLOCK_IN);
72         if (ret < 0) {
73                 dev_err(rtd->dev,
74                         "Failed to set rt5682 SYSCLK: %d\n", ret);
75                 return ret;
76         }
77
78         /* Set tdm/i2s1 master bclk ratio */
79         ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
80         if (ret < 0) {
81                 dev_err(rtd->dev,
82                         "Failed to set rt5682 tdm bclk ratio: %d\n", ret);
83                 return ret;
84         }
85
86         rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk");
87         rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk");
88
89         ret = snd_soc_card_jack_new(card, "Headset Jack",
90                                 SND_JACK_HEADSET | SND_JACK_LINEOUT |
91                                 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
92                                 SND_JACK_BTN_2 | SND_JACK_BTN_3,
93                                 &pco_jack, NULL, 0);
94         if (ret) {
95                 dev_err(card->dev, "HP jack creation failed %d\n", ret);
96                 return ret;
97         }
98
99         snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
100         snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
101         snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
102         snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
103
104         ret = snd_soc_component_set_jack(component, &pco_jack, NULL);
105         if (ret) {
106                 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
107                 return ret;
108         }
109
110         return ret;
111 }
112
113 static int rt5682_clk_enable(struct snd_pcm_substream *substream)
114 {
115         int ret = 0;
116         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
117
118         /* RT5682 will support only 48K output with 48M mclk */
119         clk_set_rate(rt5682_dai_wclk, 48000);
120         clk_set_rate(rt5682_dai_bclk, 48000 * 64);
121         ret = clk_prepare_enable(rt5682_dai_wclk);
122         if (ret < 0) {
123                 dev_err(rtd->dev, "can't enable wclk %d\n", ret);
124                 return ret;
125         }
126
127         return ret;
128 }
129
130 static void rt5682_clk_disable(void)
131 {
132         clk_disable_unprepare(rt5682_dai_wclk);
133 }
134
135 static const unsigned int channels[] = {
136         DUAL_CHANNEL,
137 };
138
139 static const unsigned int rates[] = {
140         48000,
141 };
142
143 static const struct snd_pcm_hw_constraint_list constraints_rates = {
144         .count = ARRAY_SIZE(rates),
145         .list  = rates,
146         .mask = 0,
147 };
148
149 static const struct snd_pcm_hw_constraint_list constraints_channels = {
150         .count = ARRAY_SIZE(channels),
151         .list = channels,
152         .mask = 0,
153 };
154
155 static int acp3x_5682_startup(struct snd_pcm_substream *substream)
156 {
157         struct snd_pcm_runtime *runtime = substream->runtime;
158         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
159         struct snd_soc_card *card = rtd->card;
160         struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
161
162         machine->play_i2s_instance = I2S_SP_INSTANCE;
163         machine->cap_i2s_instance = I2S_SP_INSTANCE;
164
165         runtime->hw.channels_max = DUAL_CHANNEL;
166         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
167                                    &constraints_channels);
168         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
169                                    &constraints_rates);
170         return rt5682_clk_enable(substream);
171 }
172
173 static int acp3x_max_startup(struct snd_pcm_substream *substream)
174 {
175         struct snd_pcm_runtime *runtime = substream->runtime;
176         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
177         struct snd_soc_card *card = rtd->card;
178         struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
179
180         machine->play_i2s_instance = I2S_BT_INSTANCE;
181
182         runtime->hw.channels_max = DUAL_CHANNEL;
183         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
184                                    &constraints_channels);
185         snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
186                                    &constraints_rates);
187         return rt5682_clk_enable(substream);
188 }
189
190 static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
191 {
192         struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
193         struct snd_soc_card *card = rtd->card;
194         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
195         struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
196
197         machine->cap_i2s_instance = I2S_BT_INSTANCE;
198         snd_soc_dai_set_bclk_ratio(codec_dai, 64);
199
200         return rt5682_clk_enable(substream);
201 }
202
203 static int dmic_switch;
204
205 static int dmic_get(struct snd_kcontrol *kcontrol,
206                          struct snd_ctl_elem_value *ucontrol)
207 {
208         ucontrol->value.integer.value[0] = dmic_switch;
209         return 0;
210 }
211
212 static int dmic_set(struct snd_kcontrol *kcontrol,
213                          struct snd_ctl_elem_value *ucontrol)
214 {
215         if (dmic_sel) {
216                 dmic_switch = ucontrol->value.integer.value[0];
217                 gpiod_set_value(dmic_sel, dmic_switch);
218         }
219         return 0;
220 }
221
222 static void rt5682_shutdown(struct snd_pcm_substream *substream)
223 {
224         rt5682_clk_disable();
225 }
226
227 static const struct snd_soc_ops acp3x_5682_ops = {
228         .startup = acp3x_5682_startup,
229         .shutdown = rt5682_shutdown,
230 };
231
232 static const struct snd_soc_ops acp3x_max_play_ops = {
233         .startup = acp3x_max_startup,
234         .shutdown = rt5682_shutdown,
235 };
236
237 static const struct snd_soc_ops acp3x_ec_cap0_ops = {
238         .startup = acp3x_ec_dmic0_startup,
239         .shutdown = rt5682_shutdown,
240 };
241
242 SND_SOC_DAILINK_DEF(acp3x_i2s,
243         DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
244 SND_SOC_DAILINK_DEF(acp3x_bt,
245         DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
246
247 SND_SOC_DAILINK_DEF(rt5682,
248         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
249 SND_SOC_DAILINK_DEF(max,
250         DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
251 SND_SOC_DAILINK_DEF(rt1015,
252         DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"),
253                         COMP_CODEC("i2c-10EC1015:01", "rt1015-aif")));
254 SND_SOC_DAILINK_DEF(cros_ec,
255         DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
256
257 SND_SOC_DAILINK_DEF(platform,
258         DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
259
260 static struct snd_soc_codec_conf rt1015_conf[] = {
261         {
262                 .dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
263                 .name_prefix = "Left",
264         },
265         {
266                 .dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
267                 .name_prefix = "Right",
268         },
269 };
270
271 static struct snd_soc_dai_link acp3x_dai[] = {
272         [RT5682] = {
273                 .name = "acp3x-5682-play",
274                 .stream_name = "Playback",
275                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
276                                 | SND_SOC_DAIFMT_CBM_CFM,
277                 .init = acp3x_5682_init,
278                 .dpcm_playback = 1,
279                 .dpcm_capture = 1,
280                 .ops = &acp3x_5682_ops,
281                 SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
282         },
283         [MAX] = {
284                 .name = "acp3x-max98357-play",
285                 .stream_name = "HiFi Playback",
286                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
287                                 | SND_SOC_DAIFMT_CBS_CFS,
288                 .dpcm_playback = 1,
289                 .ops = &acp3x_max_play_ops,
290                 .cpus = acp3x_bt,
291                 .num_cpus = ARRAY_SIZE(acp3x_bt),
292                 .platforms = platform,
293                 .num_platforms = ARRAY_SIZE(platform),
294         },
295         [EC] = {
296                 .name = "acp3x-ec-dmic0-capture",
297                 .stream_name = "Capture DMIC0",
298                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
299                                 | SND_SOC_DAIFMT_CBS_CFS,
300                 .dpcm_capture = 1,
301                 .ops = &acp3x_ec_cap0_ops,
302                 SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
303         },
304 };
305
306 static const char * const dmic_mux_text[] = {
307         "Front Mic",
308         "Rear Mic",
309 };
310
311 static SOC_ENUM_SINGLE_DECL(
312                 acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text);
313
314 static const struct snd_kcontrol_new acp3x_dmic_mux_control =
315         SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum,
316                           dmic_get, dmic_set);
317
318 static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = {
319         SND_SOC_DAPM_HP("Headphone Jack", NULL),
320         SND_SOC_DAPM_SPK("Spk", NULL),
321         SND_SOC_DAPM_MIC("Headset Mic", NULL),
322         SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
323                          &acp3x_dmic_mux_control),
324 };
325
326 static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = {
327         {"Headphone Jack", NULL, "HPOL"},
328         {"Headphone Jack", NULL, "HPOR"},
329         {"IN1P", NULL, "Headset Mic"},
330         {"Spk", NULL, "Speaker"},
331         {"Dmic Mux", "Front Mic", "DMIC"},
332         {"Dmic Mux", "Rear Mic", "DMIC"},
333 };
334
335 static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = {
336         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
337         SOC_DAPM_PIN_SWITCH("Spk"),
338         SOC_DAPM_PIN_SWITCH("Headset Mic"),
339 };
340
341 static struct snd_soc_card acp3x_5682 = {
342         .name = "acp3xalc5682m98357",
343         .owner = THIS_MODULE,
344         .dai_link = acp3x_dai,
345         .num_links = ARRAY_SIZE(acp3x_dai),
346         .dapm_widgets = acp3x_5682_widgets,
347         .num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets),
348         .dapm_routes = acp3x_5682_audio_route,
349         .num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route),
350         .controls = acp3x_5682_mc_controls,
351         .num_controls = ARRAY_SIZE(acp3x_5682_mc_controls),
352 };
353
354 static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = {
355         SND_SOC_DAPM_HP("Headphone Jack", NULL),
356         SND_SOC_DAPM_MIC("Headset Mic", NULL),
357         SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
358                          &acp3x_dmic_mux_control),
359         SND_SOC_DAPM_SPK("Left Spk", NULL),
360         SND_SOC_DAPM_SPK("Right Spk", NULL),
361 };
362
363 static const struct snd_soc_dapm_route acp3x_1015_route[] = {
364         {"Headphone Jack", NULL, "HPOL"},
365         {"Headphone Jack", NULL, "HPOR"},
366         {"IN1P", NULL, "Headset Mic"},
367         {"Dmic Mux", "Front Mic", "DMIC"},
368         {"Dmic Mux", "Rear Mic", "DMIC"},
369         {"Left Spk", NULL, "Left SPO"},
370         {"Right Spk", NULL, "Right SPO"},
371 };
372
373 static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = {
374         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
375         SOC_DAPM_PIN_SWITCH("Headset Mic"),
376         SOC_DAPM_PIN_SWITCH("Left Spk"),
377         SOC_DAPM_PIN_SWITCH("Right Spk"),
378 };
379
380 static struct snd_soc_card acp3x_1015 = {
381         .name = "acp3xalc56821015",
382         .owner = THIS_MODULE,
383         .dai_link = acp3x_dai,
384         .num_links = ARRAY_SIZE(acp3x_dai),
385         .dapm_widgets = acp3x_1015_widgets,
386         .num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets),
387         .dapm_routes = acp3x_1015_route,
388         .num_dapm_routes = ARRAY_SIZE(acp3x_1015_route),
389         .codec_conf = rt1015_conf,
390         .num_configs = ARRAY_SIZE(rt1015_conf),
391         .controls = acp3x_mc_1015_controls,
392         .num_controls = ARRAY_SIZE(acp3x_mc_1015_controls),
393 };
394
395 void *soc_is_rltk_max(struct device *dev)
396 {
397         const struct acpi_device_id *match;
398
399         match = acpi_match_device(dev->driver->acpi_match_table, dev);
400         if (!match)
401                 return NULL;
402         return (void *)match->driver_data;
403 }
404
405 static void card_spk_dai_link_present(struct snd_soc_dai_link *links,
406                                                 const char *card_name)
407 {
408         if (!strcmp(card_name, "acp3xalc56821015")) {
409                 links[1].codecs = rt1015;
410                 links[1].num_codecs = ARRAY_SIZE(rt1015);
411         } else {
412                 links[1].codecs = max;
413                 links[1].num_codecs = ARRAY_SIZE(max);
414         }
415 }
416
417 static int acp3x_probe(struct platform_device *pdev)
418 {
419         int ret;
420         struct snd_soc_card *card;
421         struct acp3x_platform_info *machine;
422         struct device *dev = &pdev->dev;
423
424         card = (struct snd_soc_card *)soc_is_rltk_max(dev);
425         if (!card)
426                 return -ENODEV;
427
428         machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
429         if (!machine)
430                 return -ENOMEM;
431
432         card_spk_dai_link_present(card->dai_link, card->name);
433         card->dev = &pdev->dev;
434         platform_set_drvdata(pdev, card);
435         snd_soc_card_set_drvdata(card, machine);
436
437         dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
438         if (IS_ERR(dmic_sel)) {
439                 dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
440                         PTR_ERR(dmic_sel));
441                 return PTR_ERR(dmic_sel);
442         }
443
444         ret = devm_snd_soc_register_card(&pdev->dev, card);
445         if (ret) {
446                 dev_err(&pdev->dev,
447                                 "devm_snd_soc_register_card(%s) failed: %d\n",
448                                 card->name, ret);
449                 return ret;
450         }
451         return 0;
452 }
453
454 static const struct acpi_device_id acp3x_audio_acpi_match[] = {
455         { "AMDI5682", (unsigned long)&acp3x_5682},
456         { "AMDI1015", (unsigned long)&acp3x_1015},
457         {},
458 };
459 MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
460
461 static struct platform_driver acp3x_audio = {
462         .driver = {
463                 .name = "acp3x-alc5682-max98357",
464                 .acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
465                 .pm = &snd_soc_pm_ops,
466         },
467         .probe = acp3x_probe,
468 };
469
470 module_platform_driver(acp3x_audio);
471
472 MODULE_AUTHOR("akshu.agrawal@amd.com");
473 MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
474 MODULE_DESCRIPTION("ALC5682 ALC1015 & MAX98357 audio support");
475 MODULE_LICENSE("GPL v2");