GNU Linux-libre 4.19.263-gnu1
[releases.git] / sound / soc / mediatek / mt2701 / mt2701-cs42448.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt2701-cs42448.c  --  MT2701 CS42448 ALSA SoC machine driver
4  *
5  * Copyright (c) 2016 MediaTek Inc.
6  * Author: Ir Lian <ir.lian@mediatek.com>
7  *         Garlic Tseng <garlic.tseng@mediatek.com>
8  */
9
10 #include <linux/module.h>
11 #include <sound/soc.h>
12 #include <linux/delay.h>
13 #include <linux/gpio.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/of_gpio.h>
16
17 #include "mt2701-afe-common.h"
18
19 struct mt2701_cs42448_private {
20         int i2s1_in_mux;
21         int i2s1_in_mux_gpio_sel_1;
22         int i2s1_in_mux_gpio_sel_2;
23 };
24
25 static const char * const i2sin_mux_switch_text[] = {
26         "ADC_SDOUT2",
27         "ADC_SDOUT3",
28         "I2S_IN_1",
29         "I2S_IN_2",
30 };
31
32 static const struct soc_enum i2sin_mux_enum =
33         SOC_ENUM_SINGLE_EXT(4, i2sin_mux_switch_text);
34
35 static int mt2701_cs42448_i2sin1_mux_get(struct snd_kcontrol *kcontrol,
36                                          struct snd_ctl_elem_value *ucontrol)
37 {
38         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
39         struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
40
41         ucontrol->value.integer.value[0] = priv->i2s1_in_mux;
42         return 0;
43 }
44
45 static int mt2701_cs42448_i2sin1_mux_set(struct snd_kcontrol *kcontrol,
46                                          struct snd_ctl_elem_value *ucontrol)
47 {
48         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
49         struct mt2701_cs42448_private *priv = snd_soc_card_get_drvdata(card);
50
51         if (ucontrol->value.integer.value[0] == priv->i2s1_in_mux)
52                 return 0;
53
54         switch (ucontrol->value.integer.value[0]) {
55         case 0:
56                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 0);
57                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 0);
58                 break;
59         case 1:
60                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
61                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 0);
62                 break;
63         case 2:
64                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 0);
65                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
66                 break;
67         case 3:
68                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_1, 1);
69                 gpio_set_value(priv->i2s1_in_mux_gpio_sel_2, 1);
70                 break;
71         default:
72                 dev_warn(card->dev, "%s invalid setting\n", __func__);
73         }
74
75         priv->i2s1_in_mux = ucontrol->value.integer.value[0];
76         return 0;
77 }
78
79 static const struct snd_soc_dapm_widget
80                         mt2701_cs42448_asoc_card_dapm_widgets[] = {
81         SND_SOC_DAPM_LINE("Line Out Jack", NULL),
82         SND_SOC_DAPM_MIC("AMIC", NULL),
83         SND_SOC_DAPM_LINE("Tuner In", NULL),
84         SND_SOC_DAPM_LINE("Satellite Tuner In", NULL),
85         SND_SOC_DAPM_LINE("AUX In", NULL),
86 };
87
88 static const struct snd_kcontrol_new mt2701_cs42448_controls[] = {
89         SOC_DAPM_PIN_SWITCH("Line Out Jack"),
90         SOC_DAPM_PIN_SWITCH("AMIC"),
91         SOC_DAPM_PIN_SWITCH("Tuner In"),
92         SOC_DAPM_PIN_SWITCH("Satellite Tuner In"),
93         SOC_DAPM_PIN_SWITCH("AUX In"),
94         SOC_ENUM_EXT("I2SIN1_MUX_Switch", i2sin_mux_enum,
95                      mt2701_cs42448_i2sin1_mux_get,
96                      mt2701_cs42448_i2sin1_mux_set),
97 };
98
99 static const unsigned int mt2701_cs42448_sampling_rates[] = {48000};
100
101 static const struct snd_pcm_hw_constraint_list mt2701_cs42448_constraints_rates = {
102                 .count = ARRAY_SIZE(mt2701_cs42448_sampling_rates),
103                 .list = mt2701_cs42448_sampling_rates,
104                 .mask = 0,
105 };
106
107 static int mt2701_cs42448_fe_ops_startup(struct snd_pcm_substream *substream)
108 {
109         int err;
110
111         err = snd_pcm_hw_constraint_list(substream->runtime, 0,
112                                          SNDRV_PCM_HW_PARAM_RATE,
113                                          &mt2701_cs42448_constraints_rates);
114         if (err < 0) {
115                 dev_err(substream->pcm->card->dev,
116                         "%s snd_pcm_hw_constraint_list failed: 0x%x\n",
117                         __func__, err);
118                 return err;
119         }
120         return 0;
121 }
122
123 static const struct snd_soc_ops mt2701_cs42448_48k_fe_ops = {
124         .startup = mt2701_cs42448_fe_ops_startup,
125 };
126
127 static int mt2701_cs42448_be_ops_hw_params(struct snd_pcm_substream *substream,
128                                            struct snd_pcm_hw_params *params)
129 {
130         struct snd_soc_pcm_runtime *rtd = substream->private_data;
131         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
132         struct snd_soc_dai *codec_dai = rtd->codec_dai;
133         unsigned int mclk_rate;
134         unsigned int rate = params_rate(params);
135         unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
136         unsigned int div_bck_over_lrck = 64;
137
138         mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
139
140         /* mt2701 mclk */
141         snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
142
143         /* codec mclk */
144         snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
145
146         return 0;
147 }
148
149 static struct snd_soc_ops mt2701_cs42448_be_ops = {
150         .hw_params = mt2701_cs42448_be_ops_hw_params
151 };
152
153 enum {
154         DAI_LINK_FE_MULTI_CH_OUT,
155         DAI_LINK_FE_PCM0_IN,
156         DAI_LINK_FE_PCM1_IN,
157         DAI_LINK_FE_BT_OUT,
158         DAI_LINK_FE_BT_IN,
159         DAI_LINK_BE_I2S0,
160         DAI_LINK_BE_I2S1,
161         DAI_LINK_BE_I2S2,
162         DAI_LINK_BE_I2S3,
163         DAI_LINK_BE_MRG_BT,
164 };
165
166 static struct snd_soc_dai_link mt2701_cs42448_dai_links[] = {
167         /* FE */
168         [DAI_LINK_FE_MULTI_CH_OUT] = {
169                 .name = "mt2701-cs42448-multi-ch-out",
170                 .stream_name = "mt2701-cs42448-multi-ch-out",
171                 .cpu_dai_name = "PCM_multi",
172                 .codec_name = "snd-soc-dummy",
173                 .codec_dai_name = "snd-soc-dummy-dai",
174                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
175                             SND_SOC_DPCM_TRIGGER_POST},
176                 .ops = &mt2701_cs42448_48k_fe_ops,
177                 .dynamic = 1,
178                 .dpcm_playback = 1,
179         },
180         [DAI_LINK_FE_PCM0_IN] = {
181                 .name = "mt2701-cs42448-pcm0",
182                 .stream_name = "mt2701-cs42448-pcm0-data-UL",
183                 .cpu_dai_name = "PCM0",
184                 .codec_name = "snd-soc-dummy",
185                 .codec_dai_name = "snd-soc-dummy-dai",
186                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
187                             SND_SOC_DPCM_TRIGGER_POST},
188                 .ops = &mt2701_cs42448_48k_fe_ops,
189                 .dynamic = 1,
190                 .dpcm_capture = 1,
191         },
192         [DAI_LINK_FE_PCM1_IN] = {
193                 .name = "mt2701-cs42448-pcm1-data-UL",
194                 .stream_name = "mt2701-cs42448-pcm1-data-UL",
195                 .cpu_dai_name = "PCM1",
196                 .codec_name = "snd-soc-dummy",
197                 .codec_dai_name = "snd-soc-dummy-dai",
198                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
199                             SND_SOC_DPCM_TRIGGER_POST},
200                 .ops = &mt2701_cs42448_48k_fe_ops,
201                 .dynamic = 1,
202                 .dpcm_capture = 1,
203         },
204         [DAI_LINK_FE_BT_OUT] = {
205                 .name = "mt2701-cs42448-pcm-BT-out",
206                 .stream_name = "mt2701-cs42448-pcm-BT",
207                 .cpu_dai_name = "PCM_BT_DL",
208                 .codec_name = "snd-soc-dummy",
209                 .codec_dai_name = "snd-soc-dummy-dai",
210                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
211                             SND_SOC_DPCM_TRIGGER_POST},
212                 .dynamic = 1,
213                 .dpcm_playback = 1,
214         },
215         [DAI_LINK_FE_BT_IN] = {
216                 .name = "mt2701-cs42448-pcm-BT-in",
217                 .stream_name = "mt2701-cs42448-pcm-BT",
218                 .cpu_dai_name = "PCM_BT_UL",
219                 .codec_name = "snd-soc-dummy",
220                 .codec_dai_name = "snd-soc-dummy-dai",
221                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
222                             SND_SOC_DPCM_TRIGGER_POST},
223                 .dynamic = 1,
224                 .dpcm_capture = 1,
225         },
226         /* BE */
227         [DAI_LINK_BE_I2S0] = {
228                 .name = "mt2701-cs42448-I2S0",
229                 .cpu_dai_name = "I2S0",
230                 .no_pcm = 1,
231                 .codec_dai_name = "cs42448",
232                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
233                          | SND_SOC_DAIFMT_GATED,
234                 .ops = &mt2701_cs42448_be_ops,
235                 .dpcm_playback = 1,
236                 .dpcm_capture = 1,
237         },
238         [DAI_LINK_BE_I2S1] = {
239                 .name = "mt2701-cs42448-I2S1",
240                 .cpu_dai_name = "I2S1",
241                 .no_pcm = 1,
242                 .codec_dai_name = "cs42448",
243                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
244                          | SND_SOC_DAIFMT_GATED,
245                 .ops = &mt2701_cs42448_be_ops,
246                 .dpcm_playback = 1,
247                 .dpcm_capture = 1,
248         },
249         [DAI_LINK_BE_I2S2] = {
250                 .name = "mt2701-cs42448-I2S2",
251                 .cpu_dai_name = "I2S2",
252                 .no_pcm = 1,
253                 .codec_dai_name = "cs42448",
254                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
255                          | SND_SOC_DAIFMT_GATED,
256                 .ops = &mt2701_cs42448_be_ops,
257                 .dpcm_playback = 1,
258                 .dpcm_capture = 1,
259         },
260         [DAI_LINK_BE_I2S3] = {
261                 .name = "mt2701-cs42448-I2S3",
262                 .cpu_dai_name = "I2S3",
263                 .no_pcm = 1,
264                 .codec_dai_name = "cs42448",
265                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
266                          | SND_SOC_DAIFMT_GATED,
267                 .ops = &mt2701_cs42448_be_ops,
268                 .dpcm_playback = 1,
269                 .dpcm_capture = 1,
270         },
271         [DAI_LINK_BE_MRG_BT] = {
272                 .name = "mt2701-cs42448-MRG-BT",
273                 .cpu_dai_name = "MRG BT",
274                 .no_pcm = 1,
275                 .codec_dai_name = "bt-sco-pcm-wb",
276                 .dpcm_playback = 1,
277                 .dpcm_capture = 1,
278         },
279 };
280
281 static struct snd_soc_card mt2701_cs42448_soc_card = {
282         .name = "mt2701-cs42448",
283         .owner = THIS_MODULE,
284         .dai_link = mt2701_cs42448_dai_links,
285         .num_links = ARRAY_SIZE(mt2701_cs42448_dai_links),
286         .controls = mt2701_cs42448_controls,
287         .num_controls = ARRAY_SIZE(mt2701_cs42448_controls),
288         .dapm_widgets = mt2701_cs42448_asoc_card_dapm_widgets,
289         .num_dapm_widgets = ARRAY_SIZE(mt2701_cs42448_asoc_card_dapm_widgets),
290 };
291
292 static int mt2701_cs42448_machine_probe(struct platform_device *pdev)
293 {
294         struct snd_soc_card *card = &mt2701_cs42448_soc_card;
295         int ret;
296         int i;
297         struct device_node *platform_node, *codec_node, *codec_node_bt_mrg;
298         struct mt2701_cs42448_private *priv =
299                 devm_kzalloc(&pdev->dev, sizeof(struct mt2701_cs42448_private),
300                              GFP_KERNEL);
301         struct device *dev = &pdev->dev;
302
303         if (!priv)
304                 return -ENOMEM;
305
306         platform_node = of_parse_phandle(pdev->dev.of_node,
307                                          "mediatek,platform", 0);
308         if (!platform_node) {
309                 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
310                 return -EINVAL;
311         }
312         for (i = 0; i < card->num_links; i++) {
313                 if (mt2701_cs42448_dai_links[i].platform_name)
314                         continue;
315                 mt2701_cs42448_dai_links[i].platform_of_node = platform_node;
316         }
317
318         card->dev = dev;
319
320         codec_node = of_parse_phandle(pdev->dev.of_node,
321                                       "mediatek,audio-codec", 0);
322         if (!codec_node) {
323                 dev_err(&pdev->dev,
324                         "Property 'audio-codec' missing or invalid\n");
325                 return -EINVAL;
326         }
327         for (i = 0; i < card->num_links; i++) {
328                 if (mt2701_cs42448_dai_links[i].codec_name)
329                         continue;
330                 mt2701_cs42448_dai_links[i].codec_of_node = codec_node;
331         }
332
333         codec_node_bt_mrg = of_parse_phandle(pdev->dev.of_node,
334                                              "mediatek,audio-codec-bt-mrg", 0);
335         if (!codec_node_bt_mrg) {
336                 dev_err(&pdev->dev,
337                         "Property 'audio-codec-bt-mrg' missing or invalid\n");
338                 return -EINVAL;
339         }
340         mt2701_cs42448_dai_links[DAI_LINK_BE_MRG_BT].codec_of_node
341                                                         = codec_node_bt_mrg;
342
343         ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
344         if (ret) {
345                 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
346                 return ret;
347         }
348
349         priv->i2s1_in_mux_gpio_sel_1 =
350                 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio1", 0);
351         if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_1)) {
352                 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_1,
353                                         "i2s1_in_mux_gpio_sel_1");
354                 if (ret)
355                         dev_warn(&pdev->dev, "%s devm_gpio_request fail %d\n",
356                                  __func__, ret);
357                 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_1, 0);
358         }
359
360         priv->i2s1_in_mux_gpio_sel_2 =
361                 of_get_named_gpio(dev->of_node, "i2s1-in-sel-gpio2", 0);
362         if (gpio_is_valid(priv->i2s1_in_mux_gpio_sel_2)) {
363                 ret = devm_gpio_request(dev, priv->i2s1_in_mux_gpio_sel_2,
364                                         "i2s1_in_mux_gpio_sel_2");
365                 if (ret)
366                         dev_warn(&pdev->dev, "%s devm_gpio_request fail2 %d\n",
367                                  __func__, ret);
368                 gpio_direction_output(priv->i2s1_in_mux_gpio_sel_2, 0);
369         }
370         snd_soc_card_set_drvdata(card, priv);
371
372         ret = devm_snd_soc_register_card(&pdev->dev, card);
373
374         if (ret)
375                 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
376                         __func__, ret);
377         return ret;
378 }
379
380 #ifdef CONFIG_OF
381 static const struct of_device_id mt2701_cs42448_machine_dt_match[] = {
382         {.compatible = "mediatek,mt2701-cs42448-machine",},
383         {}
384 };
385 #endif
386
387 static struct platform_driver mt2701_cs42448_machine = {
388         .driver = {
389                 .name = "mt2701-cs42448",
390                    #ifdef CONFIG_OF
391                    .of_match_table = mt2701_cs42448_machine_dt_match,
392                    #endif
393         },
394         .probe = mt2701_cs42448_machine_probe,
395 };
396
397 module_platform_driver(mt2701_cs42448_machine);
398
399 /* Module information */
400 MODULE_DESCRIPTION("MT2701 CS42448 ALSA SoC machine driver");
401 MODULE_AUTHOR("Ir Lian <ir.lian@mediatek.com>");
402 MODULE_LICENSE("GPL v2");
403 MODULE_ALIAS("mt2701 cs42448 soc card");