bcm27xx: update patches from RPi foundation
[oweals/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0147-Added-IQaudIO-Pi-Codec-board-support-2969.patch
1 From 1f912d3042032514c33736723300b3ed618ddd08 Mon Sep 17 00:00:00 2001
2 From: IQaudIO <gordon@iqaudio.com>
3 Date: Mon, 13 May 2019 21:53:05 +0100
4 Subject: [PATCH] Added IQaudIO Pi-Codec board support (#2969)
5
6 Add support for the IQaudIO Pi-Codec board.
7
8 Signed-off-by: Gordon <gordon@iqaudio.com>
9
10 Fixed 48k timing issue
11
12 ASoC: iqaudio-codec: use modern dai_link style
13
14 Signed-off-by: Hui Wang <hui.wang@canonical.com>
15 ---
16  sound/soc/bcm/Kconfig         |   7 +
17  sound/soc/bcm/Makefile        |   2 +
18  sound/soc/bcm/iqaudio-codec.c | 274 ++++++++++++++++++++++++++++++++++
19  3 files changed, 283 insertions(+)
20  create mode 100644 sound/soc/bcm/iqaudio-codec.c
21
22 --- a/sound/soc/bcm/Kconfig
23 +++ b/sound/soc/bcm/Kconfig
24 @@ -104,6 +104,13 @@ config SND_BCM2708_SOC_JUSTBOOM_DIGI
25         help
26           Say Y or M if you want to add support for JustBoom Digi.
27  
28 +config SND_BCM2708_SOC_IQAUDIO_CODEC
29 +       tristate "Support for IQaudIO-CODEC"
30 +       depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
31 +       select SND_SOC_DA7213
32 +       help
33 +         Say Y or M if you want to add support for IQaudIO-CODEC.
34 +
35  config SND_BCM2708_SOC_IQAUDIO_DAC
36         tristate "Support for IQaudIO-DAC"
37         depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
38 --- a/sound/soc/bcm/Makefile
39 +++ b/sound/soc/bcm/Makefile
40 @@ -18,6 +18,7 @@ snd-soc-hifiberry-dacplusadc-objs := hif
41  snd-soc-justboom-dac-objs := justboom-dac.o
42  snd-soc-rpi-cirrus-objs := rpi-cirrus.o
43  snd-soc-rpi-proto-objs := rpi-proto.o
44 +snd-soc-iqaudio-codec-objs := iqaudio-codec.o
45  snd-soc-iqaudio-dac-objs := iqaudio-dac.o
46   snd-soc-i-sabre-q2m-objs := i-sabre-q2m.o
47  snd-soc-audioinjector-pi-soundcard-objs := audioinjector-pi-soundcard.o
48 @@ -41,6 +42,7 @@ obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_D
49  obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
50  obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
51  obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
52 +obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_CODEC) += snd-soc-iqaudio-codec.o
53  obj-$(CONFIG_SND_BCM2708_SOC_IQAUDIO_DAC) += snd-soc-iqaudio-dac.o
54   obj-$(CONFIG_SND_BCM2708_SOC_I_SABRE_Q2M) += snd-soc-i-sabre-q2m.o
55  obj-$(CONFIG_SND_AUDIOINJECTOR_PI_SOUNDCARD) += snd-soc-audioinjector-pi-soundcard.o
56 --- /dev/null
57 +++ b/sound/soc/bcm/iqaudio-codec.c
58 @@ -0,0 +1,274 @@
59 +/*
60 + * ASoC Driver for IQaudIO Raspberry Pi Codec board
61 + *
62 + * Author:     Gordon Garrity <gordon@iqaudio.com>
63 + *             (C) Copyright IQaudio Limited, 2017-2019
64 + *
65 + * This program is free software; you can redistribute it and/or
66 + * modify it under the terms of the GNU General Public License
67 + * version 2 as published by the Free Software Foundation.
68 + *
69 + * This program is distributed in the hope that it will be useful, but
70 + * WITHOUT ANY WARRANTY; without even the implied warranty of
71 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
72 + * General Public License for more details.
73 + */
74 +
75 +#include <linux/module.h>
76 +#include <linux/gpio/consumer.h>
77 +#include <linux/platform_device.h>
78 +
79 +#include <sound/core.h>
80 +#include <sound/pcm.h>
81 +#include <sound/pcm_params.h>
82 +#include <sound/soc.h>
83 +#include <sound/jack.h>
84 +
85 +#include <linux/acpi.h>
86 +#include <linux/slab.h>
87 +#include "../codecs/da7213.h"
88 +
89 +static int pll_out = DA7213_PLL_FREQ_OUT_90316800;
90 +
91 +static int snd_rpi_iqaudio_pll_control(struct snd_soc_dapm_widget *w,
92 +                                      struct snd_kcontrol *k, int  event)
93 +{
94 +       int ret = 0;
95 +       struct snd_soc_dapm_context *dapm = w->dapm;
96 +       struct snd_soc_card *card = dapm->card;
97 +       struct snd_soc_pcm_runtime *rtd =
98 +               snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
99 +       struct snd_soc_dai *codec_dai = rtd->codec_dai;
100 +
101 +       if (SND_SOC_DAPM_EVENT_OFF(event)) {
102 +               ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_MCLK, 0,
103 +                                         0);
104 +               if (ret)
105 +                       dev_err(card->dev, "Failed to bypass PLL: %d\n", ret);
106 +               /* Allow PLL time to bypass */
107 +               msleep(100);
108 +       } else if (SND_SOC_DAPM_EVENT_ON(event)) {
109 +               ret = snd_soc_dai_set_pll(codec_dai, 0, DA7213_SYSCLK_PLL, 0,
110 +                                         pll_out);
111 +               if (ret)
112 +                       dev_err(card->dev, "Failed to enable PLL: %d\n", ret);
113 +               /* Allow PLL time to lock */
114 +               msleep(100);
115 +       }
116 +
117 +       return ret;
118 +}
119 +
120 +static int snd_rpi_iqaudio_post_dapm_event(struct snd_soc_dapm_widget *w,
121 +                              struct snd_kcontrol *kcontrol,
122 +                              int event)
123 +{
124 +     switch (event) {
125 +     case SND_SOC_DAPM_POST_PMU:
126 +           /* Delay for mic bias ramp */
127 +           msleep(1000);
128 +           break;
129 +     default:
130 +           break;
131 +     }
132 +
133 +     return 0;
134 +}
135 +
136 +static const struct snd_kcontrol_new dapm_controls[] = {
137 +       SOC_DAPM_PIN_SWITCH("HP Jack"),
138 +       SOC_DAPM_PIN_SWITCH("MIC Jack"),
139 +       SOC_DAPM_PIN_SWITCH("Onboard MIC"),
140 +       SOC_DAPM_PIN_SWITCH("AUX Jack"),
141 +};
142 +
143 +static const struct snd_soc_dapm_widget dapm_widgets[] = {
144 +       SND_SOC_DAPM_HP("HP Jack", NULL),
145 +       SND_SOC_DAPM_MIC("MIC Jack", NULL),
146 +       SND_SOC_DAPM_MIC("Onboard MIC", NULL),
147 +       SND_SOC_DAPM_LINE("AUX Jack", NULL),
148 +       SND_SOC_DAPM_SUPPLY("PLL Control", SND_SOC_NOPM, 0, 0,
149 +                           snd_rpi_iqaudio_pll_control,
150 +                           SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
151 +       SND_SOC_DAPM_POST("Post Power Up Event", snd_rpi_iqaudio_post_dapm_event),
152 +};
153 +
154 +static const struct snd_soc_dapm_route audio_map[] = {
155 +       {"HP Jack", NULL, "HPL"},
156 +       {"HP Jack", NULL, "HPR"},
157 +       {"HP Jack", NULL, "PLL Control"},
158 +
159 +       {"AUXR", NULL, "AUX Jack"},
160 +       {"AUXL", NULL, "AUX Jack"},
161 +       {"AUX Jack", NULL, "PLL Control"},
162 +
163 +       /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
164 +       {"MIC1", NULL, "MIC Jack"},
165 +       {"MIC Jack", NULL, "PLL Control"},
166 +       {"MIC2", NULL, "Onboard MIC"},
167 +       {"Onboard MIC", NULL, "PLL Control"},
168 +};
169 +
170 +/* machine stream operations */
171 +
172 +static int snd_rpi_iqaudio_codec_init(struct snd_soc_pcm_runtime *rtd)
173 +{
174 +       struct snd_soc_dai *codec_dai = rtd->codec_dai;
175 +       struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
176 +       int ret;
177 +
178 +       /*
179 +        * Disable AUX Jack Pin by default to prevent PLL being enabled at
180 +        * startup. This avoids holding the PLL to a fixed SR config for
181 +        * subsequent streams.
182 +        *
183 +        * This pin can still be enabled later, as required by user-space.
184 +        */
185 +       snd_soc_dapm_disable_pin(&rtd->card->dapm, "AUX Jack");
186 +       snd_soc_dapm_sync(&rtd->card->dapm);
187 +
188 +       /* Set bclk ratio to align with codec's BCLK rate */
189 +       ret = snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
190 +       if (ret) {
191 +               dev_err(rtd->dev, "Failed to set CPU BLCK ratio\n");
192 +               return ret;
193 +       }
194 +
195 +       /* Set MCLK frequency to codec, onboard 11.2896MHz clock */
196 +       return snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK, 11289600,
197 +                                     SND_SOC_CLOCK_OUT);
198 +}
199 +
200 +static int snd_rpi_iqaudio_codec_hw_params(struct snd_pcm_substream *substream,
201 +                                          struct snd_pcm_hw_params *params)
202 +{
203 +       struct snd_soc_pcm_runtime *rtd = substream->private_data;
204 +       unsigned int samplerate = params_rate(params);
205 +
206 +       switch (samplerate) {
207 +       case  8000:
208 +       case 16000:
209 +       case 32000:
210 +       case 48000:
211 +       case 96000:
212 +               pll_out = DA7213_PLL_FREQ_OUT_98304000;
213 +               return 0;
214 +       case 44100:
215 +       case 88200:
216 +               pll_out = DA7213_PLL_FREQ_OUT_90316800;
217 +               return 0;
218 +       default:
219 +               dev_err(rtd->dev,"Unsupported samplerate %d\n", samplerate);
220 +               return -EINVAL;
221 +       }
222 +}
223 +
224 +static const struct snd_soc_ops snd_rpi_iqaudio_codec_ops = {
225 +       .hw_params = snd_rpi_iqaudio_codec_hw_params,
226 +};
227 +
228 +SND_SOC_DAILINK_DEFS(rpi_iqaudio,
229 +       DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
230 +       DAILINK_COMP_ARRAY(COMP_CODEC("da7213.1-001a", "da7213-hifi")),
231 +       DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2835-i2s.0")));
232 +
233 +static struct snd_soc_dai_link snd_rpi_iqaudio_codec_dai[] = {
234 +{
235 +       .dai_fmt                = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
236 +                                 SND_SOC_DAIFMT_CBM_CFM,
237 +       .init                   = snd_rpi_iqaudio_codec_init,
238 +       .ops                    = &snd_rpi_iqaudio_codec_ops,
239 +       .symmetric_rates        = 1,
240 +       .symmetric_channels     = 1,
241 +       .symmetric_samplebits   = 1,
242 +       SND_SOC_DAILINK_REG(rpi_iqaudio),
243 +},
244 +};
245 +
246 +/* audio machine driver */
247 +static struct snd_soc_card snd_rpi_iqaudio_codec = {
248 +       .owner                  = THIS_MODULE,
249 +       .dai_link               = snd_rpi_iqaudio_codec_dai,
250 +       .num_links              = ARRAY_SIZE(snd_rpi_iqaudio_codec_dai),
251 +       .controls               = dapm_controls,
252 +       .num_controls           = ARRAY_SIZE(dapm_controls),
253 +       .dapm_widgets           = dapm_widgets,
254 +       .num_dapm_widgets       = ARRAY_SIZE(dapm_widgets),
255 +       .dapm_routes            = audio_map,
256 +       .num_dapm_routes        = ARRAY_SIZE(audio_map),
257 +};
258 +
259 +static int snd_rpi_iqaudio_codec_probe(struct platform_device *pdev)
260 +{
261 +       int ret = 0;
262 +
263 +       snd_rpi_iqaudio_codec.dev = &pdev->dev;
264 +
265 +       if (pdev->dev.of_node) {
266 +               struct device_node *i2s_node;
267 +               struct snd_soc_card *card = &snd_rpi_iqaudio_codec;
268 +               struct snd_soc_dai_link *dai = &snd_rpi_iqaudio_codec_dai[0];
269 +
270 +               i2s_node = of_parse_phandle(pdev->dev.of_node,
271 +                                           "i2s-controller", 0);
272 +               if (i2s_node) {
273 +                       dai->cpus->dai_name = NULL;
274 +                       dai->cpus->of_node = i2s_node;
275 +                       dai->platforms->name = NULL;
276 +                       dai->platforms->of_node = i2s_node;
277 +               }
278 +
279 +               if (of_property_read_string(pdev->dev.of_node, "card_name",
280 +                                           &card->name))
281 +                       card->name = "IQaudIOCODEC";
282 +
283 +               if (of_property_read_string(pdev->dev.of_node, "dai_name",
284 +                                           &dai->name))
285 +                       dai->name = "IQaudIO CODEC";
286 +
287 +               if (of_property_read_string(pdev->dev.of_node,
288 +                                       "dai_stream_name", &dai->stream_name))
289 +                       dai->stream_name = "IQaudIO CODEC HiFi v1.2";
290 +
291 +       }
292 +
293 +       ret = snd_soc_register_card(&snd_rpi_iqaudio_codec);
294 +       if (ret) {
295 +               if (ret != -EPROBE_DEFER)
296 +                       dev_err(&pdev->dev,
297 +                               "snd_soc_register_card() failed: %d\n", ret);
298 +               return ret;
299 +       }
300 +
301 +       return 0;
302 +}
303 +
304 +static int snd_rpi_iqaudio_codec_remove(struct platform_device *pdev)
305 +{
306 +       return snd_soc_unregister_card(&snd_rpi_iqaudio_codec);
307 +}
308 +
309 +static const struct of_device_id iqaudio_of_match[] = {
310 +       { .compatible = "iqaudio,iqaudio-codec", },
311 +       {},
312 +};
313 +
314 +MODULE_DEVICE_TABLE(of, iqaudio_of_match);
315 +
316 +static struct platform_driver snd_rpi_iqaudio_codec_driver = {
317 +       .driver = {
318 +               .name   = "snd-rpi-iqaudio-codec",
319 +               .owner  = THIS_MODULE,
320 +               .of_match_table = iqaudio_of_match,
321 +       },
322 +       .probe          = snd_rpi_iqaudio_codec_probe,
323 +       .remove         = snd_rpi_iqaudio_codec_remove,
324 +};
325 +
326 +
327 +
328 +module_platform_driver(snd_rpi_iqaudio_codec_driver);
329 +
330 +MODULE_AUTHOR("Gordon Garrity <gordon@iqaudio.com>");
331 +MODULE_DESCRIPTION("ASoC Driver for IQaudIO CODEC");
332 +MODULE_LICENSE("GPL v2");