GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / mediatek / mt2701 / mt2701-wm8960.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt2701-wm8960.c  --  MT2701 WM8960 ALSA SoC machine driver
4  *
5  * Copyright (c) 2017 MediaTek Inc.
6  * Author: Ryder Lee <ryder.lee@mediatek.com>
7  */
8
9 #include <linux/module.h>
10 #include <sound/soc.h>
11
12 #include "mt2701-afe-common.h"
13
14 static const struct snd_soc_dapm_widget mt2701_wm8960_widgets[] = {
15         SND_SOC_DAPM_HP("Headphone", NULL),
16         SND_SOC_DAPM_MIC("AMIC", NULL),
17 };
18
19 static const struct snd_kcontrol_new mt2701_wm8960_controls[] = {
20         SOC_DAPM_PIN_SWITCH("Headphone"),
21         SOC_DAPM_PIN_SWITCH("AMIC"),
22 };
23
24 static int mt2701_wm8960_be_ops_hw_params(struct snd_pcm_substream *substream,
25                                           struct snd_pcm_hw_params *params)
26 {
27         struct snd_soc_pcm_runtime *rtd = substream->private_data;
28         struct snd_soc_dai *codec_dai = rtd->codec_dai;
29         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
30         unsigned int mclk_rate;
31         unsigned int rate = params_rate(params);
32         unsigned int div_mclk_over_bck = rate > 192000 ? 2 : 4;
33         unsigned int div_bck_over_lrck = 64;
34
35         mclk_rate = rate * div_bck_over_lrck * div_mclk_over_bck;
36
37         snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate, SND_SOC_CLOCK_OUT);
38         snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate, SND_SOC_CLOCK_IN);
39
40         return 0;
41 }
42
43 static struct snd_soc_ops mt2701_wm8960_be_ops = {
44         .hw_params = mt2701_wm8960_be_ops_hw_params
45 };
46
47 static struct snd_soc_dai_link mt2701_wm8960_dai_links[] = {
48         /* FE */
49         {
50                 .name = "wm8960-playback",
51                 .stream_name = "wm8960-playback",
52                 .cpu_dai_name = "PCMO0",
53                 .codec_name = "snd-soc-dummy",
54                 .codec_dai_name = "snd-soc-dummy-dai",
55                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
56                             SND_SOC_DPCM_TRIGGER_POST},
57                 .dynamic = 1,
58                 .dpcm_playback = 1,
59         },
60         {
61                 .name = "wm8960-capture",
62                 .stream_name = "wm8960-capture",
63                 .cpu_dai_name = "PCM0",
64                 .codec_name = "snd-soc-dummy",
65                 .codec_dai_name = "snd-soc-dummy-dai",
66                 .trigger = {SND_SOC_DPCM_TRIGGER_POST,
67                             SND_SOC_DPCM_TRIGGER_POST},
68                 .dynamic = 1,
69                 .dpcm_capture = 1,
70         },
71         /* BE */
72         {
73                 .name = "wm8960-codec",
74                 .cpu_dai_name = "I2S0",
75                 .no_pcm = 1,
76                 .codec_dai_name = "wm8960-hifi",
77                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS
78                         | SND_SOC_DAIFMT_GATED,
79                 .ops = &mt2701_wm8960_be_ops,
80                 .dpcm_playback = 1,
81                 .dpcm_capture = 1,
82         },
83 };
84
85 static struct snd_soc_card mt2701_wm8960_card = {
86         .name = "mt2701-wm8960",
87         .owner = THIS_MODULE,
88         .dai_link = mt2701_wm8960_dai_links,
89         .num_links = ARRAY_SIZE(mt2701_wm8960_dai_links),
90         .controls = mt2701_wm8960_controls,
91         .num_controls = ARRAY_SIZE(mt2701_wm8960_controls),
92         .dapm_widgets = mt2701_wm8960_widgets,
93         .num_dapm_widgets = ARRAY_SIZE(mt2701_wm8960_widgets),
94 };
95
96 static int mt2701_wm8960_machine_probe(struct platform_device *pdev)
97 {
98         struct snd_soc_card *card = &mt2701_wm8960_card;
99         struct device_node *platform_node, *codec_node;
100         int ret, i;
101
102         platform_node = of_parse_phandle(pdev->dev.of_node,
103                                          "mediatek,platform", 0);
104         if (!platform_node) {
105                 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
106                 return -EINVAL;
107         }
108         for (i = 0; i < card->num_links; i++) {
109                 if (mt2701_wm8960_dai_links[i].platform_name)
110                         continue;
111                 mt2701_wm8960_dai_links[i].platform_of_node = platform_node;
112         }
113
114         card->dev = &pdev->dev;
115
116         codec_node = of_parse_phandle(pdev->dev.of_node,
117                                       "mediatek,audio-codec", 0);
118         if (!codec_node) {
119                 dev_err(&pdev->dev,
120                         "Property 'audio-codec' missing or invalid\n");
121                 ret = -EINVAL;
122                 goto put_platform_node;
123         }
124         for (i = 0; i < card->num_links; i++) {
125                 if (mt2701_wm8960_dai_links[i].codec_name)
126                         continue;
127                 mt2701_wm8960_dai_links[i].codec_of_node = codec_node;
128         }
129
130         ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
131         if (ret) {
132                 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
133                 goto put_codec_node;
134         }
135
136         ret = devm_snd_soc_register_card(&pdev->dev, card);
137         if (ret)
138                 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
139                         __func__, ret);
140
141 put_codec_node:
142         of_node_put(codec_node);
143 put_platform_node:
144         of_node_put(platform_node);
145         return ret;
146 }
147
148 #ifdef CONFIG_OF
149 static const struct of_device_id mt2701_wm8960_machine_dt_match[] = {
150         {.compatible = "mediatek,mt2701-wm8960-machine",},
151         {}
152 };
153 #endif
154
155 static struct platform_driver mt2701_wm8960_machine = {
156         .driver = {
157                 .name = "mt2701-wm8960",
158                 .owner = THIS_MODULE,
159 #ifdef CONFIG_OF
160                 .of_match_table = mt2701_wm8960_machine_dt_match,
161 #endif
162         },
163         .probe = mt2701_wm8960_machine_probe,
164 };
165
166 module_platform_driver(mt2701_wm8960_machine);
167
168 /* Module information */
169 MODULE_DESCRIPTION("MT2701 WM8960 ALSA SoC machine driver");
170 MODULE_AUTHOR("Ryder Lee <ryder.lee@mediatek.com>");
171 MODULE_LICENSE("GPL v2");
172 MODULE_ALIAS("mt2701 wm8960 soc card");
173