Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / sound / soc / bcm / rpi-cirrus.c
1 /*
2  * ASoC machine driver for Cirrus Logic Audio Card
3  * (with WM5102 and WM8804 codecs)
4  *
5  * Copyright 2015-2017 Matthias Reichl <hias@horus.com>
6  *
7  * Based on rpi-cirrus-sound-pi driver (c) Wolfson / Cirrus Logic Inc.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/list.h>
18 #include <linux/delay.h>
19 #include <sound/pcm_params.h>
20
21 #include <linux/mfd/arizona/registers.h>
22
23 #include "../codecs/wm5102.h"
24 #include "../codecs/wm8804.h"
25
26 #define WM8804_CLKOUT_HZ 12000000
27
28 #define RPI_CIRRUS_DEFAULT_RATE 44100
29 #define WM5102_MAX_SYSCLK_1 49152000 /* max sysclk for 4K family */
30 #define WM5102_MAX_SYSCLK_2 45158400 /* max sysclk for 11.025K family */
31
32 static inline unsigned int calc_sysclk(unsigned int rate)
33 {
34         return (rate % 4000) ? WM5102_MAX_SYSCLK_2 : WM5102_MAX_SYSCLK_1;
35 }
36
37 enum {
38         DAI_WM5102 = 0,
39         DAI_WM8804,
40 };
41
42 struct rpi_cirrus_priv {
43         /* mutex for synchronzing FLL1 access with DAPM */
44         struct mutex lock;
45         unsigned int card_rate;
46         int sync_path_enable;
47         int fll1_freq; /* negative means RefClock in spdif rx case */
48
49         /* track hw params/free for substreams */
50         unsigned int params_set;
51         unsigned int min_rate_idx, max_rate_idx;
52         unsigned char iec958_status[4];
53 };
54
55 /* helper functions */
56 static inline struct snd_soc_pcm_runtime *get_wm5102_runtime(
57         struct snd_soc_card *card) {
58         return snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_WM5102]);
59 }
60
61 static inline struct snd_soc_pcm_runtime *get_wm8804_runtime(
62         struct snd_soc_card *card) {
63         return snd_soc_get_pcm_runtime(card, &card->dai_link[DAI_WM8804]);
64 }
65
66
67 struct rate_info {
68         unsigned int value;
69         char *text;
70 };
71
72 static struct rate_info min_rates[] = {
73         {     0, "off"},
74         { 32000, "32kHz"},
75         { 44100, "44.1kHz"}
76 };
77
78 #define NUM_MIN_RATES ARRAY_SIZE(min_rates)
79
80 static int rpi_cirrus_min_rate_info(struct snd_kcontrol *kcontrol,
81         struct snd_ctl_elem_info *uinfo)
82 {
83         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
84         uinfo->count = 1;
85         uinfo->value.enumerated.items = NUM_MIN_RATES;
86
87         if (uinfo->value.enumerated.item >= NUM_MIN_RATES)
88                 uinfo->value.enumerated.item = NUM_MIN_RATES - 1;
89         strcpy(uinfo->value.enumerated.name,
90                 min_rates[uinfo->value.enumerated.item].text);
91         return 0;
92 }
93
94 static int rpi_cirrus_min_rate_get(struct snd_kcontrol *kcontrol,
95         struct snd_ctl_elem_value *ucontrol)
96 {
97         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
98         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
99
100         ucontrol->value.enumerated.item[0] = priv->min_rate_idx;
101         return 0;
102 }
103
104 static int rpi_cirrus_min_rate_put(struct snd_kcontrol *kcontrol,
105         struct snd_ctl_elem_value *ucontrol)
106 {
107         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
108         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
109         int changed = 0;
110
111         if (priv->min_rate_idx != ucontrol->value.enumerated.item[0]) {
112                 changed = 1;
113                 priv->min_rate_idx = ucontrol->value.enumerated.item[0];
114         }
115
116         return changed;
117 }
118
119 static struct rate_info max_rates[] = {
120         {     0, "off"},
121         { 48000, "48kHz"},
122         { 96000, "96kHz"}
123 };
124
125 #define NUM_MAX_RATES ARRAY_SIZE(max_rates)
126
127 static int rpi_cirrus_max_rate_info(struct snd_kcontrol *kcontrol,
128         struct snd_ctl_elem_info *uinfo)
129 {
130         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
131         uinfo->count = 1;
132         uinfo->value.enumerated.items = NUM_MAX_RATES;
133         if (uinfo->value.enumerated.item >= NUM_MAX_RATES)
134                 uinfo->value.enumerated.item = NUM_MAX_RATES - 1;
135         strcpy(uinfo->value.enumerated.name,
136                 max_rates[uinfo->value.enumerated.item].text);
137         return 0;
138 }
139
140 static int rpi_cirrus_max_rate_get(struct snd_kcontrol *kcontrol,
141         struct snd_ctl_elem_value *ucontrol)
142 {
143         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
144         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
145
146         ucontrol->value.enumerated.item[0] = priv->max_rate_idx;
147         return 0;
148 }
149
150 static int rpi_cirrus_max_rate_put(struct snd_kcontrol *kcontrol,
151         struct snd_ctl_elem_value *ucontrol)
152 {
153         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
154         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
155         int changed = 0;
156
157         if (priv->max_rate_idx != ucontrol->value.enumerated.item[0]) {
158                 changed = 1;
159                 priv->max_rate_idx = ucontrol->value.enumerated.item[0];
160         }
161
162         return changed;
163 }
164
165 static int rpi_cirrus_spdif_info(struct snd_kcontrol *kcontrol,
166         struct snd_ctl_elem_info *uinfo)
167 {
168         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
169         uinfo->count = 1;
170         return 0;
171 }
172
173 static int rpi_cirrus_spdif_playback_get(struct snd_kcontrol *kcontrol,
174         struct snd_ctl_elem_value *ucontrol)
175 {
176         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
177         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
178         int i;
179
180         for (i = 0; i < 4; i++)
181                 ucontrol->value.iec958.status[i] = priv->iec958_status[i];
182
183         return 0;
184 }
185
186 static int rpi_cirrus_spdif_playback_put(struct snd_kcontrol *kcontrol,
187         struct snd_ctl_elem_value *ucontrol)
188 {
189         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
190         struct snd_soc_component *wm8804_component =
191                 asoc_rtd_to_codec(get_wm8804_runtime(card), 0)->component;
192         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
193         unsigned char *stat = priv->iec958_status;
194         unsigned char *ctrl_stat = ucontrol->value.iec958.status;
195         unsigned int mask;
196         int i, changed = 0;
197
198         for (i = 0; i < 4; i++) {
199                 mask = (i == 3) ? 0x3f : 0xff;
200                 if ((ctrl_stat[i] & mask) != (stat[i] & mask)) {
201                         changed = 1;
202                         stat[i] = ctrl_stat[i] & mask;
203                         snd_soc_component_update_bits(wm8804_component,
204                                 WM8804_SPDTX1 + i, mask, stat[i]);
205                 }
206         }
207
208         return changed;
209 }
210
211 static int rpi_cirrus_spdif_mask_get(struct snd_kcontrol *kcontrol,
212         struct snd_ctl_elem_value *ucontrol)
213 {
214         ucontrol->value.iec958.status[0] = 0xff;
215         ucontrol->value.iec958.status[1] = 0xff;
216         ucontrol->value.iec958.status[2] = 0xff;
217         ucontrol->value.iec958.status[3] = 0x3f;
218
219         return 0;
220 }
221
222 static int rpi_cirrus_spdif_capture_get(struct snd_kcontrol *kcontrol,
223         struct snd_ctl_elem_value *ucontrol)
224 {
225         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
226         struct snd_soc_component *wm8804_component =
227                 asoc_rtd_to_codec(get_wm8804_runtime(card), 0)->component;
228         unsigned int val, mask;
229         int i;
230
231         for (i = 0; i < 4; i++) {
232                 val = snd_soc_component_read(wm8804_component,
233                         WM8804_RXCHAN1 + i);
234                 mask = (i == 3) ? 0x3f : 0xff;
235                 ucontrol->value.iec958.status[i] = val & mask;
236         }
237
238         return 0;
239 }
240
241 #define SPDIF_FLAG_CTRL(desc, reg, bit, invert) \
242 { \
243                 .access =  SNDRV_CTL_ELEM_ACCESS_READ \
244                            | SNDRV_CTL_ELEM_ACCESS_VOLATILE, \
245                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER, \
246                 .name =    SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE) \
247                                 desc " Flag", \
248                 .info =    snd_ctl_boolean_mono_info, \
249                 .get =     rpi_cirrus_spdif_status_flag_get, \
250                 .private_value = \
251                         (bit) | ((reg) << 8) | ((invert) << 16) \
252 }
253
254 static int rpi_cirrus_spdif_status_flag_get(struct snd_kcontrol *kcontrol,
255         struct snd_ctl_elem_value *ucontrol)
256 {
257         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
258         struct snd_soc_component *wm8804_component =
259                 asoc_rtd_to_codec(get_wm8804_runtime(card), 0)->component;
260
261         unsigned int bit = kcontrol->private_value & 0xff;
262         unsigned int reg = (kcontrol->private_value >> 8) & 0xff;
263         unsigned int invert = (kcontrol->private_value >> 16) & 0xff;
264         unsigned int val;
265         bool flag;
266
267         val = snd_soc_component_read(wm8804_component, reg);
268
269         flag = val & (1 << bit);
270
271         ucontrol->value.integer.value[0] = invert ? !flag : flag;
272
273         return 0;
274 }
275
276 static const char * const recovered_frequency_texts[] = {
277         "176.4/192 kHz",
278         "88.2/96 kHz",
279         "44.1/48 kHz",
280         "32 kHz"
281 };
282
283 #define NUM_RECOVERED_FREQUENCIES \
284         ARRAY_SIZE(recovered_frequency_texts)
285
286 static int rpi_cirrus_recovered_frequency_info(struct snd_kcontrol *kcontrol,
287         struct snd_ctl_elem_info *uinfo)
288 {
289         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
290         uinfo->count = 1;
291         uinfo->value.enumerated.items = NUM_RECOVERED_FREQUENCIES;
292         if (uinfo->value.enumerated.item >= NUM_RECOVERED_FREQUENCIES)
293                 uinfo->value.enumerated.item = NUM_RECOVERED_FREQUENCIES - 1;
294         strcpy(uinfo->value.enumerated.name,
295                 recovered_frequency_texts[uinfo->value.enumerated.item]);
296         return 0;
297 }
298
299 static int rpi_cirrus_recovered_frequency_get(struct snd_kcontrol *kcontrol,
300         struct snd_ctl_elem_value *ucontrol)
301 {
302         struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
303         struct snd_soc_component *wm8804_component =
304                 asoc_rtd_to_codec(get_wm8804_runtime(card), 0)->component;
305         unsigned int val;
306
307         val = snd_soc_component_read(wm8804_component, WM8804_SPDSTAT);
308
309         ucontrol->value.enumerated.item[0] = (val >> 4) & 0x03;
310         return 0;
311 }
312
313 static const struct snd_kcontrol_new rpi_cirrus_controls[] = {
314         {
315                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
316                 .name =    "Min Sample Rate",
317                 .info =    rpi_cirrus_min_rate_info,
318                 .get =     rpi_cirrus_min_rate_get,
319                 .put =     rpi_cirrus_min_rate_put,
320         },
321         {
322                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
323                 .name =    "Max Sample Rate",
324                 .info =    rpi_cirrus_max_rate_info,
325                 .get =     rpi_cirrus_max_rate_get,
326                 .put =     rpi_cirrus_max_rate_put,
327         },
328         {
329                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
330                 .name =    SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
331                 .info =    rpi_cirrus_spdif_info,
332                 .get =     rpi_cirrus_spdif_playback_get,
333                 .put =     rpi_cirrus_spdif_playback_put,
334         },
335         {
336                 .access =  SNDRV_CTL_ELEM_ACCESS_READ
337                            | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
338                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
339                 .name =    SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
340                 .info =    rpi_cirrus_spdif_info,
341                 .get =     rpi_cirrus_spdif_capture_get,
342         },
343         {
344                 .access =  SNDRV_CTL_ELEM_ACCESS_READ,
345                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
346                 .name =    SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
347                 .info =    rpi_cirrus_spdif_info,
348                 .get =     rpi_cirrus_spdif_mask_get,
349         },
350         {
351                 .access =  SNDRV_CTL_ELEM_ACCESS_READ
352                            | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
353                 .iface =   SNDRV_CTL_ELEM_IFACE_MIXER,
354                 .name =    SNDRV_CTL_NAME_IEC958("", CAPTURE, NONE)
355                                 "Recovered Frequency",
356                 .info =    rpi_cirrus_recovered_frequency_info,
357                 .get =     rpi_cirrus_recovered_frequency_get,
358         },
359         SPDIF_FLAG_CTRL("Audio", WM8804_SPDSTAT, 0, 1),
360         SPDIF_FLAG_CTRL("Non-PCM", WM8804_SPDSTAT, 1, 0),
361         SPDIF_FLAG_CTRL("Copyright", WM8804_SPDSTAT, 2, 1),
362         SPDIF_FLAG_CTRL("De-Emphasis", WM8804_SPDSTAT, 3, 0),
363         SPDIF_FLAG_CTRL("Lock", WM8804_SPDSTAT, 6, 1),
364         SPDIF_FLAG_CTRL("Invalid", WM8804_INTSTAT, 1, 0),
365         SPDIF_FLAG_CTRL("TransErr", WM8804_INTSTAT, 3, 0),
366 };
367
368 static const char * const linein_micbias_texts[] = {
369         "off", "on",
370 };
371
372 static SOC_ENUM_SINGLE_VIRT_DECL(linein_micbias_enum,
373         linein_micbias_texts);
374
375 static const struct snd_kcontrol_new linein_micbias_mux =
376         SOC_DAPM_ENUM("Route", linein_micbias_enum);
377
378 static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
379         struct snd_kcontrol *kcontrol, int event);
380
381 const struct snd_soc_dapm_widget rpi_cirrus_dapm_widgets[] = {
382         SND_SOC_DAPM_MIC("DMIC", NULL),
383         SND_SOC_DAPM_MIC("Headset Mic", NULL),
384         SND_SOC_DAPM_INPUT("Line Input"),
385         SND_SOC_DAPM_MIC("Line Input with Micbias", NULL),
386         SND_SOC_DAPM_MUX("Line Input Micbias", SND_SOC_NOPM, 0, 0,
387                 &linein_micbias_mux),
388         SND_SOC_DAPM_INPUT("dummy SPDIF in"),
389         SND_SOC_DAPM_PGA_E("dummy SPDIFRX", SND_SOC_NOPM, 0, 0, NULL, 0,
390                 rpi_cirrus_spdif_rx_enable_event,
391                 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD),
392         SND_SOC_DAPM_INPUT("Dummy Input"),
393         SND_SOC_DAPM_OUTPUT("Dummy Output"),
394 };
395
396 const struct snd_soc_dapm_route rpi_cirrus_dapm_routes[] = {
397         { "IN1L", NULL, "Headset Mic" },
398         { "IN1R", NULL, "Headset Mic" },
399         { "Headset Mic", NULL, "MICBIAS1" },
400
401         { "IN2L", NULL, "DMIC" },
402         { "IN2R", NULL, "DMIC" },
403         { "DMIC", NULL, "MICBIAS2" },
404
405         { "IN3L", NULL, "Line Input Micbias" },
406         { "IN3R", NULL, "Line Input Micbias" },
407
408         { "Line Input Micbias", "off", "Line Input" },
409         { "Line Input Micbias", "on", "Line Input with Micbias" },
410
411         /* Make sure MICVDD is enabled, otherwise we get noise */
412         { "Line Input", NULL, "MICVDD" },
413         { "Line Input with Micbias", NULL, "MICBIAS3" },
414
415         /* Dummy routes to check whether SPDIF RX is enabled or not */
416         {"dummy SPDIFRX", NULL, "dummy SPDIF in"},
417         {"AIFTX", NULL, "dummy SPDIFRX"},
418
419         /*
420          * Dummy routes to keep wm5102 from staying off on
421          * playback/capture if all mixers are off.
422          */
423         { "Dummy Output", NULL, "AIF1RX1" },
424         { "Dummy Output", NULL, "AIF1RX2" },
425         { "AIF1TX1", NULL, "Dummy Input" },
426         { "AIF1TX2", NULL, "Dummy Input" },
427 };
428
429 static int rpi_cirrus_clear_flls(struct snd_soc_card *card,
430         struct snd_soc_component *wm5102_component) {
431
432         int ret1, ret2;
433
434         ret1 = snd_soc_component_set_pll(wm5102_component,
435                 WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
436         ret2 = snd_soc_component_set_pll(wm5102_component,
437                 WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
438
439         if (ret1) {
440                 dev_warn(card->dev,
441                         "setting FLL1 to zero failed: %d\n", ret1);
442                 return ret1;
443         }
444         if (ret2) {
445                 dev_warn(card->dev,
446                         "setting FLL1_REFCLK to zero failed: %d\n", ret2);
447                 return ret2;
448         }
449         return 0;
450 }
451
452 static int rpi_cirrus_set_fll(struct snd_soc_card *card,
453         struct snd_soc_component *wm5102_component, unsigned int clk_freq)
454 {
455         int ret = snd_soc_component_set_pll(wm5102_component,
456                 WM5102_FLL1,
457                 ARIZONA_CLK_SRC_MCLK1,
458                 WM8804_CLKOUT_HZ,
459                 clk_freq);
460         if (ret)
461                 dev_err(card->dev, "Failed to set FLL1 to %d: %d\n",
462                         clk_freq, ret);
463
464         usleep_range(1000, 2000);
465         return ret;
466 }
467
468 static int rpi_cirrus_set_fll_refclk(struct snd_soc_card *card,
469         struct snd_soc_component *wm5102_component,
470         unsigned int clk_freq, unsigned int aif2_freq)
471 {
472         int ret = snd_soc_component_set_pll(wm5102_component,
473                 WM5102_FLL1_REFCLK,
474                 ARIZONA_CLK_SRC_MCLK1,
475                 WM8804_CLKOUT_HZ,
476                 clk_freq);
477         if (ret) {
478                 dev_err(card->dev,
479                         "Failed to set FLL1_REFCLK to %d: %d\n",
480                         clk_freq, ret);
481                 return ret;
482         }
483
484         ret = snd_soc_component_set_pll(wm5102_component,
485                 WM5102_FLL1,
486                 ARIZONA_CLK_SRC_AIF2BCLK,
487                 aif2_freq, clk_freq);
488         if (ret)
489                 dev_err(card->dev,
490                         "Failed to set FLL1 with Sync Clock %d to %d: %d\n",
491                         aif2_freq, clk_freq, ret);
492
493         usleep_range(1000, 2000);
494         return ret;
495 }
496
497 static int rpi_cirrus_spdif_rx_enable_event(struct snd_soc_dapm_widget *w,
498         struct snd_kcontrol *kcontrol, int event)
499 {
500         struct snd_soc_card *card = w->dapm->card;
501         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
502         struct snd_soc_component *wm5102_component =
503                 asoc_rtd_to_codec(get_wm5102_runtime(card), 0)->component;
504
505         unsigned int clk_freq, aif2_freq;
506         int ret = 0;
507
508         switch (event) {
509         case SND_SOC_DAPM_POST_PMU:
510                 mutex_lock(&priv->lock);
511
512                 /* Enable sync path in case of SPDIF capture use case */
513
514                 clk_freq = calc_sysclk(priv->card_rate);
515                 aif2_freq = 64 * priv->card_rate;
516
517                 dev_dbg(card->dev,
518                         "spdif_rx: changing FLL1 to use Ref Clock clk: %d spdif: %d\n",
519                         clk_freq, aif2_freq);
520
521                 ret = rpi_cirrus_clear_flls(card, wm5102_component);
522                 if (ret) {
523                         dev_err(card->dev, "spdif_rx: failed to clear FLLs\n");
524                         goto out;
525                 }
526
527                 ret = rpi_cirrus_set_fll_refclk(card, wm5102_component,
528                         clk_freq, aif2_freq);
529
530                 if (ret) {
531                         dev_err(card->dev, "spdif_rx: failed to set FLLs\n");
532                         goto out;
533                 }
534
535                 /* set to negative to indicate we're doing spdif rx */
536                 priv->fll1_freq = -clk_freq;
537                 priv->sync_path_enable = 1;
538                 break;
539
540         case SND_SOC_DAPM_POST_PMD:
541                 mutex_lock(&priv->lock);
542                 priv->sync_path_enable = 0;
543                 break;
544
545         default:
546                 return 0;
547         }
548
549 out:
550         mutex_unlock(&priv->lock);
551         return ret;
552 }
553
554 static int rpi_cirrus_set_bias_level(struct snd_soc_card *card,
555         struct snd_soc_dapm_context *dapm,
556         enum snd_soc_bias_level level)
557 {
558         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
559         struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
560         struct snd_soc_component *wm5102_component =
561                 asoc_rtd_to_codec(wm5102_runtime, 0)->component;
562
563         int ret = 0;
564         unsigned int clk_freq;
565
566         if (dapm->dev != asoc_rtd_to_codec(wm5102_runtime, 0)->dev)
567                 return 0;
568
569         switch (level) {
570         case SND_SOC_BIAS_PREPARE:
571                 if (dapm->bias_level == SND_SOC_BIAS_ON)
572                         break;
573
574                 mutex_lock(&priv->lock);
575
576                 if (!priv->sync_path_enable) {
577                         clk_freq = calc_sysclk(priv->card_rate);
578
579                         dev_dbg(card->dev,
580                                 "set_bias: changing FLL1 from %d to %d\n",
581                                 priv->fll1_freq, clk_freq);
582
583                         ret = rpi_cirrus_set_fll(card,
584                                 wm5102_component, clk_freq);
585                         if (ret)
586                                 dev_err(card->dev,
587                                         "set_bias: Failed to set FLL1\n");
588                         else
589                                 priv->fll1_freq = clk_freq;
590                 }
591                 mutex_unlock(&priv->lock);
592                 break;
593         default:
594                 break;
595         }
596
597         return ret;
598 }
599
600 static int rpi_cirrus_set_bias_level_post(struct snd_soc_card *card,
601         struct snd_soc_dapm_context *dapm,
602         enum snd_soc_bias_level level)
603 {
604         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
605         struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
606         struct snd_soc_component *wm5102_component =
607                 asoc_rtd_to_codec(wm5102_runtime, 0)->component;
608
609         if (dapm->dev != asoc_rtd_to_codec(wm5102_runtime, 0)->dev)
610                 return 0;
611
612         switch (level) {
613         case SND_SOC_BIAS_STANDBY:
614                 mutex_lock(&priv->lock);
615
616                 dev_dbg(card->dev,
617                         "set_bias_post: changing FLL1 from %d to off\n",
618                                 priv->fll1_freq);
619
620                 if (rpi_cirrus_clear_flls(card, wm5102_component))
621                         dev_err(card->dev,
622                                 "set_bias_post: failed to clear FLLs\n");
623                 else
624                         priv->fll1_freq = 0;
625
626                 mutex_unlock(&priv->lock);
627
628                 break;
629         default:
630                 break;
631         }
632
633         return 0;
634 }
635
636 static int rpi_cirrus_set_wm8804_pll(struct snd_soc_card *card,
637         struct snd_soc_dai *wm8804_dai, unsigned int rate)
638 {
639         int ret;
640
641         /* use 256fs */
642         unsigned int clk_freq = rate * 256;
643
644         ret = snd_soc_dai_set_pll(wm8804_dai, 0, 0,
645                 WM8804_CLKOUT_HZ, clk_freq);
646         if (ret) {
647                 dev_err(card->dev,
648                         "Failed to set WM8804 PLL to %d: %d\n", clk_freq, ret);
649                 return ret;
650         }
651
652         /* Set MCLK as PLL Output */
653         ret = snd_soc_dai_set_sysclk(wm8804_dai,
654                 WM8804_TX_CLKSRC_PLL, clk_freq, 0);
655         if (ret) {
656                 dev_err(card->dev,
657                         "Failed to set MCLK as PLL Output: %d\n", ret);
658                 return ret;
659         }
660
661         return ret;
662 }
663
664 static int rpi_cirrus_startup(struct snd_pcm_substream *substream)
665 {
666         struct snd_soc_pcm_runtime *rtd = substream->private_data;
667         struct snd_soc_card *card = rtd->card;
668         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
669         unsigned int min_rate = min_rates[priv->min_rate_idx].value;
670         unsigned int max_rate = max_rates[priv->max_rate_idx].value;
671
672         if (min_rate || max_rate) {
673                 if (max_rate == 0)
674                         max_rate = UINT_MAX;
675
676                 dev_dbg(card->dev,
677                         "startup: limiting rate to %u-%u\n",
678                         min_rate, max_rate);
679
680                 snd_pcm_hw_constraint_minmax(substream->runtime,
681                         SNDRV_PCM_HW_PARAM_RATE, min_rate, max_rate);
682         }
683
684         return 0;
685 }
686
687 static struct snd_soc_pcm_stream rpi_cirrus_dai_link2_params = {
688         .formats = SNDRV_PCM_FMTBIT_S24_LE,
689         .channels_min = 2,
690         .channels_max = 2,
691         .rate_min = RPI_CIRRUS_DEFAULT_RATE,
692         .rate_max = RPI_CIRRUS_DEFAULT_RATE,
693 };
694
695 static int rpi_cirrus_hw_params(struct snd_pcm_substream *substream,
696         struct snd_pcm_hw_params *params)
697 {
698         struct snd_soc_pcm_runtime *rtd = substream->private_data;
699         struct snd_soc_card *card = rtd->card;
700         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
701         struct snd_soc_dai *bcm_i2s_dai = asoc_rtd_to_cpu(rtd, 0);
702         struct snd_soc_component *wm5102_component = asoc_rtd_to_codec(rtd, 0)->component;
703         struct snd_soc_dai *wm8804_dai = asoc_rtd_to_codec(get_wm8804_runtime(card), 0);
704
705         int ret;
706
707         unsigned int width = snd_pcm_format_physical_width(
708                 params_format(params));
709         unsigned int rate = params_rate(params);
710         unsigned int clk_freq = calc_sysclk(rate);
711
712         mutex_lock(&priv->lock);
713
714         dev_dbg(card->dev, "hw_params: setting rate to %d\n", rate);
715
716         ret = snd_soc_dai_set_bclk_ratio(bcm_i2s_dai, 2 * width);
717         if (ret) {
718                 dev_err(card->dev, "set_bclk_ratio failed: %d\n", ret);
719                 goto out;
720         }
721
722         ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(rtd, 0), 0x03, 0x03, 2, width);
723         if (ret) {
724                 dev_err(card->dev, "set_tdm_slot failed: %d\n", ret);
725                 goto out;
726         }
727
728         /* WM8804 supports sample rates from 32k only */
729         if (rate >= 32000) {
730                 ret = rpi_cirrus_set_wm8804_pll(card, wm8804_dai, rate);
731                 if (ret)
732                         goto out;
733         }
734
735         ret = snd_soc_component_set_sysclk(wm5102_component,
736                 ARIZONA_CLK_SYSCLK,
737                 ARIZONA_CLK_SRC_FLL1,
738                 clk_freq,
739                 SND_SOC_CLOCK_IN);
740         if (ret) {
741                 dev_err(card->dev, "Failed to set SYSCLK: %d\n", ret);
742                 goto out;
743         }
744
745         if ((priv->fll1_freq > 0) && (priv->fll1_freq != clk_freq)) {
746                 dev_dbg(card->dev,
747                         "hw_params: changing FLL1 from %d to %d\n",
748                         priv->fll1_freq, clk_freq);
749
750                 if (rpi_cirrus_clear_flls(card, wm5102_component)) {
751                         dev_err(card->dev, "hw_params: failed to clear FLLs\n");
752                         goto out;
753                 }
754
755                 if (rpi_cirrus_set_fll(card, wm5102_component, clk_freq)) {
756                         dev_err(card->dev, "hw_params: failed to set FLL\n");
757                         goto out;
758                 }
759
760                 priv->fll1_freq = clk_freq;
761         }
762
763         priv->card_rate = rate;
764         rpi_cirrus_dai_link2_params.rate_min = rate;
765         rpi_cirrus_dai_link2_params.rate_max = rate;
766
767         priv->params_set |= 1 << substream->stream;
768
769 out:
770         mutex_unlock(&priv->lock);
771
772         return ret;
773 }
774
775 static int rpi_cirrus_hw_free(struct snd_pcm_substream *substream)
776 {
777         struct snd_soc_pcm_runtime *rtd = substream->private_data;
778         struct snd_soc_card *card = rtd->card;
779         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
780         struct snd_soc_component *wm5102_component = asoc_rtd_to_codec(rtd, 0)->component;
781         int ret;
782         unsigned int old_params_set = priv->params_set;
783
784         priv->params_set &= ~(1 << substream->stream);
785
786         /* disable sysclk if this was the last open stream */
787         if (priv->params_set == 0 && old_params_set) {
788                 dev_dbg(card->dev,
789                         "hw_free: Setting SYSCLK to Zero\n");
790
791                 ret = snd_soc_component_set_sysclk(wm5102_component,
792                         ARIZONA_CLK_SYSCLK,
793                         ARIZONA_CLK_SRC_FLL1,
794                         0,
795                         SND_SOC_CLOCK_IN);
796                 if (ret)
797                         dev_err(card->dev,
798                                 "hw_free: Failed to set SYSCLK to Zero: %d\n",
799                                 ret);
800         }
801         return 0;
802 }
803
804 static int rpi_cirrus_init_wm5102(struct snd_soc_pcm_runtime *rtd)
805 {
806         struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
807         int ret;
808
809         /* no 32kHz input, derive it from sysclk if needed  */
810         snd_soc_component_update_bits(component,
811                         ARIZONA_CLOCK_32K_1, ARIZONA_CLK_32K_SRC_MASK, 2);
812
813         if (rpi_cirrus_clear_flls(rtd->card, component))
814                 dev_warn(rtd->card->dev,
815                         "init_wm5102: failed to clear FLLs\n");
816
817         ret = snd_soc_component_set_sysclk(component,
818                 ARIZONA_CLK_SYSCLK, ARIZONA_CLK_SRC_FLL1,
819                 0, SND_SOC_CLOCK_IN);
820         if (ret) {
821                 dev_err(rtd->card->dev,
822                         "Failed to set SYSCLK to Zero: %d\n", ret);
823                 return ret;
824         }
825
826         return 0;
827 }
828
829 static int rpi_cirrus_init_wm8804(struct snd_soc_pcm_runtime *rtd)
830 {
831         struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
832         struct snd_soc_component *component = codec_dai->component;
833         struct snd_soc_card *card = rtd->card;
834         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
835         unsigned int val, mask;
836         int i, ret;
837
838         for (i = 0; i < 4; i++) {
839                 val = snd_soc_component_read(component,
840                         WM8804_SPDTX1 + i);
841                 mask = (i == 3) ? 0x3f : 0xff;
842                 priv->iec958_status[i] = val & mask;
843         }
844
845         /* Setup for 256fs */
846         ret = snd_soc_dai_set_clkdiv(codec_dai,
847                 WM8804_MCLK_DIV, WM8804_MCLKDIV_256FS);
848         if (ret) {
849                 dev_err(card->dev,
850                         "init_wm8804: Failed to set MCLK_DIV to 256fs: %d\n",
851                         ret);
852                 return ret;
853         }
854
855         /* Output OSC on CLKOUT */
856         ret = snd_soc_dai_set_sysclk(codec_dai,
857                 WM8804_CLKOUT_SRC_OSCCLK, WM8804_CLKOUT_HZ, 0);
858         if (ret)
859                 dev_err(card->dev,
860                         "init_wm8804: Failed to set CLKOUT as OSC Frequency: %d\n",
861                         ret);
862
863         /* Init PLL with default samplerate */
864         ret = rpi_cirrus_set_wm8804_pll(card, codec_dai,
865                 RPI_CIRRUS_DEFAULT_RATE);
866         if (ret)
867                 dev_err(card->dev,
868                         "init_wm8804: Failed to setup PLL for %dHz: %d\n",
869                         RPI_CIRRUS_DEFAULT_RATE, ret);
870
871         return ret;
872 }
873
874 static struct snd_soc_ops rpi_cirrus_ops = {
875         .startup = rpi_cirrus_startup,
876         .hw_params = rpi_cirrus_hw_params,
877         .hw_free = rpi_cirrus_hw_free,
878 };
879
880 SND_SOC_DAILINK_DEFS(wm5102,
881      DAILINK_COMP_ARRAY(COMP_EMPTY()),
882      DAILINK_COMP_ARRAY(COMP_CODEC("wm5102-codec", "wm5102-aif1")),
883      DAILINK_COMP_ARRAY(COMP_EMPTY()));
884
885 SND_SOC_DAILINK_DEFS(wm8804,
886      DAILINK_COMP_ARRAY(COMP_CPU("wm5102-aif2")),
887      DAILINK_COMP_ARRAY(COMP_CODEC("wm8804.1-003b", "wm8804-spdif")));
888
889 static struct snd_soc_dai_link rpi_cirrus_dai[] = {
890         [DAI_WM5102] = {
891                 .name           = "WM5102",
892                 .stream_name    = "WM5102 AiFi",
893                 .dai_fmt        =   SND_SOC_DAIFMT_I2S
894                                   | SND_SOC_DAIFMT_NB_NF
895                                   | SND_SOC_DAIFMT_CBM_CFM,
896                 .ops            = &rpi_cirrus_ops,
897                 .init           = rpi_cirrus_init_wm5102,
898                 SND_SOC_DAILINK_REG(wm5102),
899         },
900         [DAI_WM8804] = {
901                 .name           = "WM5102 SPDIF",
902                 .stream_name    = "SPDIF Tx/Rx",
903                 .dai_fmt        =   SND_SOC_DAIFMT_I2S
904                                   | SND_SOC_DAIFMT_NB_NF
905                                   | SND_SOC_DAIFMT_CBM_CFM,
906                 .ignore_suspend = 1,
907                 .params         = &rpi_cirrus_dai_link2_params,
908                 .init           = rpi_cirrus_init_wm8804,
909                 SND_SOC_DAILINK_REG(wm8804),
910         },
911 };
912
913
914 static int rpi_cirrus_late_probe(struct snd_soc_card *card)
915 {
916         struct rpi_cirrus_priv *priv = snd_soc_card_get_drvdata(card);
917         struct snd_soc_pcm_runtime *wm5102_runtime = get_wm5102_runtime(card);
918         struct snd_soc_pcm_runtime *wm8804_runtime = get_wm8804_runtime(card);
919         int ret;
920
921         dev_dbg(card->dev, "iec958_bits: %02x %02x %02x %02x\n",
922                 priv->iec958_status[0],
923                 priv->iec958_status[1],
924                 priv->iec958_status[2],
925                 priv->iec958_status[3]);
926
927         ret = snd_soc_dai_set_sysclk(
928                 asoc_rtd_to_codec(wm5102_runtime, 0), ARIZONA_CLK_SYSCLK, 0, 0);
929         if (ret) {
930                 dev_err(card->dev,
931                         "Failed to set WM5102 codec dai clk domain: %d\n", ret);
932                 return ret;
933         }
934
935         ret = snd_soc_dai_set_sysclk(
936                 asoc_rtd_to_cpu(wm8804_runtime, 0), ARIZONA_CLK_SYSCLK, 0, 0);
937         if (ret)
938                 dev_err(card->dev,
939                         "Failed to set WM8804 codec dai clk domain: %d\n", ret);
940
941         return ret;
942 }
943
944 /* audio machine driver */
945 static struct snd_soc_card rpi_cirrus_card = {
946         .name                   = "RPi-Cirrus",
947         .driver_name            = "RPiCirrus",
948         .owner                  = THIS_MODULE,
949         .dai_link               = rpi_cirrus_dai,
950         .num_links              = ARRAY_SIZE(rpi_cirrus_dai),
951         .late_probe             = rpi_cirrus_late_probe,
952         .controls               = rpi_cirrus_controls,
953         .num_controls           = ARRAY_SIZE(rpi_cirrus_controls),
954         .dapm_widgets           = rpi_cirrus_dapm_widgets,
955         .num_dapm_widgets       = ARRAY_SIZE(rpi_cirrus_dapm_widgets),
956         .dapm_routes            = rpi_cirrus_dapm_routes,
957         .num_dapm_routes        = ARRAY_SIZE(rpi_cirrus_dapm_routes),
958         .set_bias_level         = rpi_cirrus_set_bias_level,
959         .set_bias_level_post    = rpi_cirrus_set_bias_level_post,
960 };
961
962 static int rpi_cirrus_probe(struct platform_device *pdev)
963 {
964         int ret = 0;
965         struct rpi_cirrus_priv *priv;
966         struct device_node *i2s_node;
967
968         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
969         if (!priv)
970                 return -ENOMEM;
971
972         priv->min_rate_idx = 1; /* min samplerate 32kHz */
973         priv->card_rate = RPI_CIRRUS_DEFAULT_RATE;
974
975         mutex_init(&priv->lock);
976
977         snd_soc_card_set_drvdata(&rpi_cirrus_card, priv);
978
979         if (!pdev->dev.of_node)
980                 return -ENODEV;
981
982         i2s_node = of_parse_phandle(
983                         pdev->dev.of_node, "i2s-controller", 0);
984         if (!i2s_node) {
985                 dev_err(&pdev->dev, "i2s-controller missing in DT\n");
986                 return -ENODEV;
987         }
988
989         rpi_cirrus_dai[DAI_WM5102].cpus->of_node = i2s_node;
990         rpi_cirrus_dai[DAI_WM5102].platforms->of_node = i2s_node;
991
992         rpi_cirrus_card.dev = &pdev->dev;
993
994         ret = devm_snd_soc_register_card(&pdev->dev, &rpi_cirrus_card);
995         if (ret) {
996                 if (ret == -EPROBE_DEFER)
997                         dev_dbg(&pdev->dev,
998                                 "register card requested probe deferral\n");
999                 else
1000                         dev_err(&pdev->dev,
1001                                 "Failed to register card: %d\n", ret);
1002         }
1003
1004         return ret;
1005 }
1006
1007 static const struct of_device_id rpi_cirrus_of_match[] = {
1008         { .compatible = "wlf,rpi-cirrus", },
1009         {},
1010 };
1011 MODULE_DEVICE_TABLE(of, rpi_cirrus_of_match);
1012
1013 static struct platform_driver rpi_cirrus_driver = {
1014         .driver = {
1015                 .name   = "snd-rpi-cirrus",
1016                 .of_match_table = of_match_ptr(rpi_cirrus_of_match),
1017         },
1018         .probe  = rpi_cirrus_probe,
1019 };
1020
1021 module_platform_driver(rpi_cirrus_driver);
1022
1023 MODULE_AUTHOR("Matthias Reichl <hias@horus.com>");
1024 MODULE_DESCRIPTION("ASoC driver for Cirrus Logic Audio Card");
1025 MODULE_LICENSE("GPL");