GNU Linux-libre 4.19.286-gnu1
[releases.git] / sound / soc / mediatek / mt8173 / mt8173-max98090.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * mt8173-max98090.c  --  MT8173 MAX98090 ALSA SoC machine driver
4  *
5  * Copyright (c) 2015 MediaTek Inc.
6  * Author: Koro Chen <koro.chen@mediatek.com>
7  */
8
9 #include <linux/module.h>
10 #include <sound/soc.h>
11 #include <sound/jack.h>
12 #include <linux/gpio.h>
13 #include "../../codecs/max98090.h"
14
15 static struct snd_soc_jack mt8173_max98090_jack;
16
17 static struct snd_soc_jack_pin mt8173_max98090_jack_pins[] = {
18         {
19                 .pin    = "Headphone",
20                 .mask   = SND_JACK_HEADPHONE,
21         },
22         {
23                 .pin    = "Headset Mic",
24                 .mask   = SND_JACK_MICROPHONE,
25         },
26 };
27
28 static const struct snd_soc_dapm_widget mt8173_max98090_widgets[] = {
29         SND_SOC_DAPM_SPK("Speaker", NULL),
30         SND_SOC_DAPM_MIC("Int Mic", NULL),
31         SND_SOC_DAPM_HP("Headphone", NULL),
32         SND_SOC_DAPM_MIC("Headset Mic", NULL),
33 };
34
35 static const struct snd_soc_dapm_route mt8173_max98090_routes[] = {
36         {"Speaker", NULL, "SPKL"},
37         {"Speaker", NULL, "SPKR"},
38         {"DMICL", NULL, "Int Mic"},
39         {"Headphone", NULL, "HPL"},
40         {"Headphone", NULL, "HPR"},
41         {"Headset Mic", NULL, "MICBIAS"},
42         {"IN34", NULL, "Headset Mic"},
43 };
44
45 static const struct snd_kcontrol_new mt8173_max98090_controls[] = {
46         SOC_DAPM_PIN_SWITCH("Speaker"),
47         SOC_DAPM_PIN_SWITCH("Int Mic"),
48         SOC_DAPM_PIN_SWITCH("Headphone"),
49         SOC_DAPM_PIN_SWITCH("Headset Mic"),
50 };
51
52 static int mt8173_max98090_hw_params(struct snd_pcm_substream *substream,
53                                      struct snd_pcm_hw_params *params)
54 {
55         struct snd_soc_pcm_runtime *rtd = substream->private_data;
56         struct snd_soc_dai *codec_dai = rtd->codec_dai;
57
58         return snd_soc_dai_set_sysclk(codec_dai, 0, params_rate(params) * 256,
59                                       SND_SOC_CLOCK_IN);
60 }
61
62 static const struct snd_soc_ops mt8173_max98090_ops = {
63         .hw_params = mt8173_max98090_hw_params,
64 };
65
66 static int mt8173_max98090_init(struct snd_soc_pcm_runtime *runtime)
67 {
68         int ret;
69         struct snd_soc_card *card = runtime->card;
70         struct snd_soc_component *component = runtime->codec_dai->component;
71
72         /* enable jack detection */
73         ret = snd_soc_card_jack_new(card, "Headphone", SND_JACK_HEADPHONE,
74                                     &mt8173_max98090_jack,
75                                     mt8173_max98090_jack_pins,
76                                     ARRAY_SIZE(mt8173_max98090_jack_pins));
77         if (ret) {
78                 dev_err(card->dev, "Can't create a new Jack %d\n", ret);
79                 return ret;
80         }
81
82         return max98090_mic_detect(component, &mt8173_max98090_jack);
83 }
84
85 /* Digital audio interface glue - connects codec <---> CPU */
86 static struct snd_soc_dai_link mt8173_max98090_dais[] = {
87         /* Front End DAI links */
88         {
89                 .name = "MAX98090 Playback",
90                 .stream_name = "MAX98090 Playback",
91                 .cpu_dai_name = "DL1",
92                 .codec_name = "snd-soc-dummy",
93                 .codec_dai_name = "snd-soc-dummy-dai",
94                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
95                 .dynamic = 1,
96                 .dpcm_playback = 1,
97         },
98         {
99                 .name = "MAX98090 Capture",
100                 .stream_name = "MAX98090 Capture",
101                 .cpu_dai_name = "VUL",
102                 .codec_name = "snd-soc-dummy",
103                 .codec_dai_name = "snd-soc-dummy-dai",
104                 .trigger = {SND_SOC_DPCM_TRIGGER_POST, SND_SOC_DPCM_TRIGGER_POST},
105                 .dynamic = 1,
106                 .dpcm_capture = 1,
107         },
108         /* Back End DAI links */
109         {
110                 .name = "Codec",
111                 .cpu_dai_name = "I2S",
112                 .no_pcm = 1,
113                 .codec_dai_name = "HiFi",
114                 .init = mt8173_max98090_init,
115                 .ops = &mt8173_max98090_ops,
116                 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
117                            SND_SOC_DAIFMT_CBS_CFS,
118                 .dpcm_playback = 1,
119                 .dpcm_capture = 1,
120         },
121 };
122
123 static struct snd_soc_card mt8173_max98090_card = {
124         .name = "mt8173-max98090",
125         .owner = THIS_MODULE,
126         .dai_link = mt8173_max98090_dais,
127         .num_links = ARRAY_SIZE(mt8173_max98090_dais),
128         .controls = mt8173_max98090_controls,
129         .num_controls = ARRAY_SIZE(mt8173_max98090_controls),
130         .dapm_widgets = mt8173_max98090_widgets,
131         .num_dapm_widgets = ARRAY_SIZE(mt8173_max98090_widgets),
132         .dapm_routes = mt8173_max98090_routes,
133         .num_dapm_routes = ARRAY_SIZE(mt8173_max98090_routes),
134 };
135
136 static int mt8173_max98090_dev_probe(struct platform_device *pdev)
137 {
138         struct snd_soc_card *card = &mt8173_max98090_card;
139         struct device_node *codec_node, *platform_node;
140         int ret, i;
141
142         platform_node = of_parse_phandle(pdev->dev.of_node,
143                                          "mediatek,platform", 0);
144         if (!platform_node) {
145                 dev_err(&pdev->dev, "Property 'platform' missing or invalid\n");
146                 return -EINVAL;
147         }
148         for (i = 0; i < card->num_links; i++) {
149                 if (mt8173_max98090_dais[i].platform_name)
150                         continue;
151                 mt8173_max98090_dais[i].platform_of_node = platform_node;
152         }
153
154         codec_node = of_parse_phandle(pdev->dev.of_node,
155                                       "mediatek,audio-codec", 0);
156         if (!codec_node) {
157                 dev_err(&pdev->dev,
158                         "Property 'audio-codec' missing or invalid\n");
159                 ret = -EINVAL;
160                 goto put_platform_node;
161         }
162         for (i = 0; i < card->num_links; i++) {
163                 if (mt8173_max98090_dais[i].codec_name)
164                         continue;
165                 mt8173_max98090_dais[i].codec_of_node = codec_node;
166         }
167         card->dev = &pdev->dev;
168
169         ret = devm_snd_soc_register_card(&pdev->dev, card);
170         if (ret)
171                 dev_err(&pdev->dev, "%s snd_soc_register_card fail %d\n",
172                         __func__, ret);
173
174         of_node_put(codec_node);
175
176 put_platform_node:
177         of_node_put(platform_node);
178         return ret;
179 }
180
181 static const struct of_device_id mt8173_max98090_dt_match[] = {
182         { .compatible = "mediatek,mt8173-max98090", },
183         { }
184 };
185 MODULE_DEVICE_TABLE(of, mt8173_max98090_dt_match);
186
187 static struct platform_driver mt8173_max98090_driver = {
188         .driver = {
189                    .name = "mt8173-max98090",
190                    .of_match_table = mt8173_max98090_dt_match,
191 #ifdef CONFIG_PM
192                    .pm = &snd_soc_pm_ops,
193 #endif
194         },
195         .probe = mt8173_max98090_dev_probe,
196 };
197
198 module_platform_driver(mt8173_max98090_driver);
199
200 /* Module information */
201 MODULE_DESCRIPTION("MT8173 MAX98090 ALSA SoC machine driver");
202 MODULE_AUTHOR("Koro Chen <koro.chen@mediatek.com>");
203 MODULE_LICENSE("GPL v2");
204 MODULE_ALIAS("platform:mt8173-max98090");
205