Merge branch 'asoc-5.3' into asoc-5.4
[sfrench/cifs-2.6.git] / sound / soc / rockchip / rockchip_max98090.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
4  *
5  * Copyright (c) 2014, ROCKCHIP CORPORATION.  All rights reserved.
6  */
7
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/slab.h>
11 #include <linux/gpio.h>
12 #include <linux/of_gpio.h>
13 #include <sound/core.h>
14 #include <sound/jack.h>
15 #include <sound/pcm.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18
19 #include "rockchip_i2s.h"
20 #include "../codecs/ts3a227e.h"
21
22 #define DRV_NAME "rockchip-snd-max98090"
23
24 static struct snd_soc_jack headset_jack;
25
26 /* Headset jack detection DAPM pins */
27 static struct snd_soc_jack_pin headset_jack_pins[] = {
28         {
29                 .pin = "Headphone",
30                 .mask = SND_JACK_HEADPHONE,
31         },
32         {
33                 .pin = "Headset Mic",
34                 .mask = SND_JACK_MICROPHONE,
35         },
36
37 };
38
39 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
40         SND_SOC_DAPM_HP("Headphone", NULL),
41         SND_SOC_DAPM_MIC("Headset Mic", NULL),
42         SND_SOC_DAPM_MIC("Int Mic", NULL),
43         SND_SOC_DAPM_SPK("Speaker", NULL),
44 };
45
46 static const struct snd_soc_dapm_route rk_audio_map[] = {
47         {"IN34", NULL, "Headset Mic"},
48         {"Headset Mic", NULL, "MICBIAS"},
49         {"DMICL", NULL, "Int Mic"},
50         {"Headphone", NULL, "HPL"},
51         {"Headphone", NULL, "HPR"},
52         {"Speaker", NULL, "SPKL"},
53         {"Speaker", NULL, "SPKR"},
54 };
55
56 static const struct snd_kcontrol_new rk_mc_controls[] = {
57         SOC_DAPM_PIN_SWITCH("Headphone"),
58         SOC_DAPM_PIN_SWITCH("Headset Mic"),
59         SOC_DAPM_PIN_SWITCH("Int Mic"),
60         SOC_DAPM_PIN_SWITCH("Speaker"),
61 };
62
63 static int rk_jack_event(struct notifier_block *nb, unsigned long event,
64                          void *data)
65 {
66         struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
67         struct snd_soc_dapm_context *dapm = &jack->card->dapm;
68
69         if (event & SND_JACK_MICROPHONE)
70                 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
71         else
72                 snd_soc_dapm_disable_pin(dapm, "MICBIAS");
73
74         snd_soc_dapm_sync(dapm);
75
76         return 0;
77 }
78
79 static struct notifier_block rk_jack_nb = {
80         .notifier_call = rk_jack_event,
81 };
82
83 static int rk_init(struct snd_soc_pcm_runtime *runtime)
84 {
85         /*
86          * The jack has already been created in the rk_98090_headset_init()
87          * function.
88          */
89         snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb);
90
91         return 0;
92 }
93
94 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
95                              struct snd_pcm_hw_params *params)
96 {
97         int ret = 0;
98         struct snd_soc_pcm_runtime *rtd = substream->private_data;
99         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
100         struct snd_soc_dai *codec_dai = rtd->codec_dai;
101         int mclk;
102
103         switch (params_rate(params)) {
104         case 8000:
105         case 16000:
106         case 24000:
107         case 32000:
108         case 48000:
109         case 64000:
110         case 96000:
111                 mclk = 12288000;
112                 break;
113         case 11025:
114         case 22050:
115         case 44100:
116         case 88200:
117                 mclk = 11289600;
118                 break;
119         default:
120                 return -EINVAL;
121         }
122
123         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
124                                      SND_SOC_CLOCK_OUT);
125         if (ret < 0) {
126                 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
127                 return ret;
128         }
129
130         ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
131                                      SND_SOC_CLOCK_IN);
132         if (ret < 0) {
133                 dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
134                 return ret;
135         }
136
137         return ret;
138 }
139
140 static const struct snd_soc_ops rk_aif1_ops = {
141         .hw_params = rk_aif1_hw_params,
142 };
143
144 SND_SOC_DAILINK_DEFS(hifi,
145         DAILINK_COMP_ARRAY(COMP_EMPTY()),
146         DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
147         DAILINK_COMP_ARRAY(COMP_EMPTY()));
148
149 static struct snd_soc_dai_link rk_dailink = {
150         .name = "max98090",
151         .stream_name = "Audio",
152         .init = rk_init,
153         .ops = &rk_aif1_ops,
154         /* set max98090 as slave */
155         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
156                 SND_SOC_DAIFMT_CBS_CFS,
157         SND_SOC_DAILINK_REG(hifi),
158 };
159
160 static int rk_98090_headset_init(struct snd_soc_component *component);
161
162 static struct snd_soc_aux_dev rk_98090_headset_dev = {
163         .name = "Headset Chip",
164         .init = rk_98090_headset_init,
165 };
166
167 static struct snd_soc_card snd_soc_card_rk = {
168         .name = "ROCKCHIP-I2S",
169         .owner = THIS_MODULE,
170         .dai_link = &rk_dailink,
171         .num_links = 1,
172         .aux_dev = &rk_98090_headset_dev,
173         .num_aux_devs = 1,
174         .dapm_widgets = rk_dapm_widgets,
175         .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
176         .dapm_routes = rk_audio_map,
177         .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
178         .controls = rk_mc_controls,
179         .num_controls = ARRAY_SIZE(rk_mc_controls),
180 };
181
182 static int rk_98090_headset_init(struct snd_soc_component *component)
183 {
184         int ret;
185
186         /* Enable Headset and 4 Buttons Jack detection */
187         ret = snd_soc_card_jack_new(&snd_soc_card_rk, "Headset Jack",
188                                     SND_JACK_HEADSET |
189                                     SND_JACK_BTN_0 | SND_JACK_BTN_1 |
190                                     SND_JACK_BTN_2 | SND_JACK_BTN_3,
191                                     &headset_jack,
192                                     headset_jack_pins,
193                                     ARRAY_SIZE(headset_jack_pins));
194         if (ret)
195                 return ret;
196
197         ret = ts3a227e_enable_jack_detect(component, &headset_jack);
198
199         return ret;
200 }
201
202 static int snd_rk_mc_probe(struct platform_device *pdev)
203 {
204         int ret = 0;
205         struct snd_soc_card *card = &snd_soc_card_rk;
206         struct device_node *np = pdev->dev.of_node;
207
208         /* register the soc card */
209         card->dev = &pdev->dev;
210
211         rk_dailink.codecs->of_node = of_parse_phandle(np,
212                         "rockchip,audio-codec", 0);
213         if (!rk_dailink.codecs->of_node) {
214                 dev_err(&pdev->dev,
215                         "Property 'rockchip,audio-codec' missing or invalid\n");
216                 return -EINVAL;
217         }
218
219         rk_dailink.cpus->of_node = of_parse_phandle(np,
220                         "rockchip,i2s-controller", 0);
221         if (!rk_dailink.cpus->of_node) {
222                 dev_err(&pdev->dev,
223                         "Property 'rockchip,i2s-controller' missing or invalid\n");
224                 return -EINVAL;
225         }
226
227         rk_dailink.platforms->of_node = rk_dailink.cpus->of_node;
228
229         rk_98090_headset_dev.codec_of_node = of_parse_phandle(np,
230                         "rockchip,headset-codec", 0);
231         if (!rk_98090_headset_dev.codec_of_node) {
232                 dev_err(&pdev->dev,
233                         "Property 'rockchip,headset-codec' missing/invalid\n");
234                 return -EINVAL;
235         }
236
237         ret = snd_soc_of_parse_card_name(card, "rockchip,model");
238         if (ret) {
239                 dev_err(&pdev->dev,
240                         "Soc parse card name failed %d\n", ret);
241                 return ret;
242         }
243
244         ret = devm_snd_soc_register_card(&pdev->dev, card);
245         if (ret) {
246                 dev_err(&pdev->dev,
247                         "Soc register card failed %d\n", ret);
248                 return ret;
249         }
250
251         return ret;
252 }
253
254 static const struct of_device_id rockchip_max98090_of_match[] = {
255         { .compatible = "rockchip,rockchip-audio-max98090", },
256         {},
257 };
258
259 MODULE_DEVICE_TABLE(of, rockchip_max98090_of_match);
260
261 static struct platform_driver snd_rk_mc_driver = {
262         .probe = snd_rk_mc_probe,
263         .driver = {
264                 .name = DRV_NAME,
265                 .pm = &snd_soc_pm_ops,
266                 .of_match_table = rockchip_max98090_of_match,
267         },
268 };
269
270 module_platform_driver(snd_rk_mc_driver);
271
272 MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>");
273 MODULE_DESCRIPTION("Rockchip max98090 machine ASoC driver");
274 MODULE_LICENSE("GPL v2");
275 MODULE_ALIAS("platform:" DRV_NAME);