Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / iqaudio-codec.c
1 /*
2  * ASoC Driver for IQaudIO Raspberry Pi Codec board
3  *
4  * Author:      Gordon Garrity <gordon@iqaudio.com>
5  *              (C) Copyright IQaudio Limited, 2017-2019
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  */
16
17 #include <linux/module.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/platform_device.h>
20
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/jack.h>
26
27 #include <linux/acpi.h>
28 #include <linux/slab.h>
29 #include "../codecs/da7213.h"
30
31 static int pll_out = DA7213_PLL_FREQ_OUT_90316800;
32
33 static int snd_rpi_iqaudio_pll_control(struct snd_soc_dapm_widget *w,
34                                        struct snd_kcontrol *k, int  event)
35 {
36         int ret = 0;
37         struct snd_soc_dapm_context *dapm = w->dapm;
38         struct snd_soc_card *card = dapm->card;
39         struct snd_soc_pcm_runtime *rtd =
40                 snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
41         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
42
43         if (SND_SOC_DAPM_EVENT_OFF(event)) {
44                 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_MCLK, 0,
45                                           0);
46                 if (ret)
47                         dev_err(card->dev, "Failed to bypass PLL: %d\n", ret);
48                 /* Allow PLL time to bypass */
49                 msleep(100);
50         } else if (SND_SOC_DAPM_EVENT_ON(event)) {
51                 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_PLL, 0,
52                                           pll_out);
53                 if (ret)
54                         dev_err(card->dev, "Failed to enable PLL: %d\n", ret);
55                 /* Allow PLL time to lock */
56                 msleep(100);
57         }
58
59         return ret;
60 }
61
62 static int snd_rpi_iqaudio_post_dapm_event(struct snd_soc_dapm_widget *w,
63                               struct snd_kcontrol *kcontrol,
64                               int event)
65 {
66      switch (event) {
67      case SND_SOC_DAPM_POST_PMU:
68            /* Delay for mic bias ramp */
69            msleep(1000);
70            break;
71      default:
72            break;
73      }
74
75      return 0;
76 }
77
78 static const struct snd_kcontrol_new dapm_controls[] = {
79         SOC_DAPM_PIN_SWITCH("HP Jack"),
80         SOC_DAPM_PIN_SWITCH("MIC Jack"),
81         SOC_DAPM_PIN_SWITCH("Onboard MIC"),
82         SOC_DAPM_PIN_SWITCH("AUX Jack"),
83 };
84
85 static const struct snd_soc_dapm_widget dapm_widgets[] = {
86         SND_SOC_DAPM_HP("HP Jack", NULL),
87         SND_SOC_DAPM_MIC("MIC Jack", NULL),
88         SND_SOC_DAPM_MIC("Onboard MIC", NULL),
89         SND_SOC_DAPM_LINE("AUX Jack", NULL),
90         SND_SOC_DAPM_SUPPLY("PLL Control", SND_SOC_NOPM, 0, 0,
91                             snd_rpi_iqaudio_pll_control,
92                             SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
93         SND_SOC_DAPM_POST("Post Power Up Event", snd_rpi_iqaudio_post_dapm_event),
94 };
95
96 static const struct snd_soc_dapm_route audio_map[] = {
97         {"HP Jack", NULL, "HPL"},
98         {"HP Jack", NULL, "HPR"},
99         {"HP Jack", NULL, "PLL Control"},
100
101         {"AUXR", NULL, "AUX Jack"},
102         {"AUXL", NULL, "AUX Jack"},
103         {"AUX Jack", NULL, "PLL Control"},
104
105         /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
106         {"MIC1", NULL, "MIC Jack"},
107         {"MIC Jack", NULL, "PLL Control"},
108         {"MIC2", NULL, "Onboard MIC"},
109         {"Onboard MIC", NULL, "PLL Control"},
110 };
111
112 /* machine stream operations */
113
114 static int snd_rpi_iqaudio_codec_init(struct snd_soc_pcm_runtime *rtd)
115 {
116         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
117         struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
118         int ret;
119
120         /*
121          * Disable AUX Jack Pin by default to prevent PLL being enabled at
122          * startup. This avoids holding the PLL to a fixed SR config for
123          * subsequent streams.
124          *
125          * This pin can still be enabled later, as required by user-space.
126          */
127         snd_soc_dapm_disable_pin(&rtd->card->dapm, "AUX Jack");
128         snd_soc_dapm_sync(&rtd->card->dapm);
129
130         /* Set bclk ratio to align with codec's BCLK rate */
131         ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
132         if (ret) {
133                 dev_err(rtd->dev, "Failed to set CPU BLCK ratio\n");
134                 return ret;
135         }
136
137         /* Set MCLK frequency to codec, onboard 11.2896MHz clock */
138         return snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK, 11289600,
139                                       SND_SOC_CLOCK_OUT);
140 }
141
142 static int snd_rpi_iqaudio_codec_hw_params(struct snd_pcm_substream *substream,
143                                            struct snd_pcm_hw_params *params)
144 {
145         struct snd_soc_pcm_runtime *rtd = substream->private_data;
146         unsigned int samplerate = params_rate(params);
147
148         switch (samplerate) {
149         case  8000:
150         case 16000:
151         case 32000:
152         case 48000:
153         case 96000:
154                 pll_out = DA7213_PLL_FREQ_OUT_98304000;
155                 return 0;
156         case 44100:
157         case 88200:
158                 pll_out = DA7213_PLL_FREQ_OUT_90316800;
159                 return 0;
160         default:
161                 dev_err(rtd->dev,"Unsupported samplerate %d\n", samplerate);
162                 return -EINVAL;
163         }
164 }
165
166 static const struct snd_soc_ops snd_rpi_iqaudio_codec_ops = {
167         .hw_params = snd_rpi_iqaudio_codec_hw_params,
168 };
169
170 SND_SOC_DAILINK_DEFS(rpi_iqaudio,
171         DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
172         DAILINK_COMP_ARRAY(COMP_CODEC("da7213.1-001a", "da7213-hifi")),
173         DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
174
175 static struct snd_soc_dai_link snd_rpi_iqaudio_codec_dai[] = {
176 {
177         .dai_fmt                = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
178                                   SND_SOC_DAIFMT_CBM_CFM,
179         .init                   = snd_rpi_iqaudio_codec_init,
180         .ops                    = &snd_rpi_iqaudio_codec_ops,
181         .symmetric_rate = 1,
182         .symmetric_channels     = 1,
183         .symmetric_sample_bits  = 1,
184         SND_SOC_DAILINK_REG(rpi_iqaudio),
185 },
186 };
187
188 /* audio machine driver */
189 static struct snd_soc_card snd_rpi_iqaudio_codec = {
190         .owner                  = THIS_MODULE,
191         .dai_link               = snd_rpi_iqaudio_codec_dai,
192         .num_links              = ARRAY_SIZE(snd_rpi_iqaudio_codec_dai),
193         .controls               = dapm_controls,
194         .num_controls           = ARRAY_SIZE(dapm_controls),
195         .dapm_widgets           = dapm_widgets,
196         .num_dapm_widgets       = ARRAY_SIZE(dapm_widgets),
197         .dapm_routes            = audio_map,
198         .num_dapm_routes        = ARRAY_SIZE(audio_map),
199 };
200
201 static int snd_rpi_iqaudio_codec_probe(struct platform_device *pdev)
202 {
203         int ret = 0;
204
205         snd_rpi_iqaudio_codec.dev = &pdev->dev;
206
207         if (pdev->dev.of_node) {
208                 struct device_node *i2s_node;
209                 struct snd_soc_card *card = &snd_rpi_iqaudio_codec;
210                 struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_codec_dai[0];
211
212                 i2s_node = of_parse_phandle(pdev->dev.of_node,
213                                             "i2s-controller", 0);
214                 if (i2s_node) {
215                         dai->cpus->dai_name = NULL;
216                         dai->cpus->of_node = i2s_node;
217                         dai->platforms->name = NULL;
218                         dai->platforms->of_node = i2s_node;
219                 }
220
221                 if (of_property_read_string(pdev->dev.of_node, "card_name",
222                                             &card->name))
223                         card->name = "IQaudIOCODEC";
224
225                 if (of_property_read_string(pdev->dev.of_node, "dai_name",
226                                             &dai->name))
227                         dai->name = "IQaudIO CODEC";
228
229                 if (of_property_read_string(pdev->dev.of_node,
230                                         "dai_stream_name", &dai->stream_name))
231                         dai->stream_name = "IQaudIO CODEC HiFi v1.2";
232
233         }
234
235         ret = snd_soc_register_card(&snd_rpi_iqaudio_codec);
236         if (ret) {
237                 if (ret != -EPROBE_DEFER)
238                         dev_err(&pdev->dev,
239                                 "snd_soc_register_card() failed: %d\n", ret);
240                 return ret;
241         }
242
243         return 0;
244 }
245
246 static int snd_rpi_iqaudio_codec_remove(struct platform_device *pdev)
247 {
248         return snd_soc_unregister_card(&snd_rpi_iqaudio_codec);
249 }
250
251 static const struct of_device_id iqaudio_of_match[] = {
252         { .compatible = "iqaudio,iqaudio-codec", },
253         {},
254 };
255
256 MODULE_DEVICE_TABLE(of, iqaudio_of_match);
257
258 static struct platform_driver snd_rpi_iqaudio_codec_driver = {
259         .driver = {
260                 .name   = "snd-rpi-iqaudio-codec",
261                 .owner  = THIS_MODULE,
262                 .of_match_table = iqaudio_of_match,
263         },
264         .probe          = snd_rpi_iqaudio_codec_probe,
265         .remove         = snd_rpi_iqaudio_codec_remove,
266 };
267
268
269
270 module_platform_driver(snd_rpi_iqaudio_codec_driver);
271
272 MODULE_AUTHOR("Gordon Garrity <gordon@iqaudio.com>");
273 MODULE_DESCRIPTION("ASoC Driver for IQaudIO CODEC");
274 MODULE_LICENSE("GPL v2");