Mention branches and keyring.
[releases.git] / meson / axg-tdm-interface.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 //
3 // Copyright (c) 2018 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
5
6 #include <linux/clk.h>
7 #include <linux/module.h>
8 #include <linux/of_platform.h>
9 #include <sound/pcm_params.h>
10 #include <sound/soc.h>
11 #include <sound/soc-dai.h>
12
13 #include "axg-tdm.h"
14
15 /* Maximum bit clock frequency according the datasheets */
16 #define MAX_SCLK 100000000 /* Hz */
17
18 enum {
19         TDM_IFACE_PAD,
20         TDM_IFACE_LOOPBACK,
21 };
22
23 static unsigned int axg_tdm_slots_total(u32 *mask)
24 {
25         unsigned int slots = 0;
26         int i;
27
28         if (!mask)
29                 return 0;
30
31         /* Count the total number of slots provided by all 4 lanes */
32         for (i = 0; i < AXG_TDM_NUM_LANES; i++)
33                 slots += hweight32(mask[i]);
34
35         return slots;
36 }
37
38 int axg_tdm_set_tdm_slots(struct snd_soc_dai *dai, u32 *tx_mask,
39                           u32 *rx_mask, unsigned int slots,
40                           unsigned int slot_width)
41 {
42         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
43         struct axg_tdm_stream *tx = (struct axg_tdm_stream *)
44                 dai->playback_dma_data;
45         struct axg_tdm_stream *rx = (struct axg_tdm_stream *)
46                 dai->capture_dma_data;
47         unsigned int tx_slots, rx_slots;
48         unsigned int fmt = 0;
49
50         tx_slots = axg_tdm_slots_total(tx_mask);
51         rx_slots = axg_tdm_slots_total(rx_mask);
52
53         /* We should at least have a slot for a valid interface */
54         if (!tx_slots && !rx_slots) {
55                 dev_err(dai->dev, "interface has no slot\n");
56                 return -EINVAL;
57         }
58
59         iface->slots = slots;
60
61         switch (slot_width) {
62         case 0:
63                 slot_width = 32;
64                 fallthrough;
65         case 32:
66                 fmt |= SNDRV_PCM_FMTBIT_S32_LE;
67                 fallthrough;
68         case 24:
69                 fmt |= SNDRV_PCM_FMTBIT_S24_LE;
70                 fmt |= SNDRV_PCM_FMTBIT_S20_LE;
71                 fallthrough;
72         case 16:
73                 fmt |= SNDRV_PCM_FMTBIT_S16_LE;
74                 fallthrough;
75         case 8:
76                 fmt |= SNDRV_PCM_FMTBIT_S8;
77                 break;
78         default:
79                 dev_err(dai->dev, "unsupported slot width: %d\n", slot_width);
80                 return -EINVAL;
81         }
82
83         iface->slot_width = slot_width;
84
85         /* Amend the dai driver and let dpcm merge do its job */
86         if (tx) {
87                 tx->mask = tx_mask;
88                 dai->driver->playback.channels_max = tx_slots;
89                 dai->driver->playback.formats = fmt;
90         }
91
92         if (rx) {
93                 rx->mask = rx_mask;
94                 dai->driver->capture.channels_max = rx_slots;
95                 dai->driver->capture.formats = fmt;
96         }
97
98         return 0;
99 }
100 EXPORT_SYMBOL_GPL(axg_tdm_set_tdm_slots);
101
102 static int axg_tdm_iface_set_sysclk(struct snd_soc_dai *dai, int clk_id,
103                                     unsigned int freq, int dir)
104 {
105         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
106         int ret = -ENOTSUPP;
107
108         if (dir == SND_SOC_CLOCK_OUT && clk_id == 0) {
109                 if (!iface->mclk) {
110                         dev_warn(dai->dev, "master clock not provided\n");
111                 } else {
112                         ret = clk_set_rate(iface->mclk, freq);
113                         if (!ret)
114                                 iface->mclk_rate = freq;
115                 }
116         }
117
118         return ret;
119 }
120
121 static int axg_tdm_iface_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
122 {
123         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
124
125         switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
126         case SND_SOC_DAIFMT_BP_FP:
127                 if (!iface->mclk) {
128                         dev_err(dai->dev, "cpu clock master: mclk missing\n");
129                         return -ENODEV;
130                 }
131                 break;
132
133         case SND_SOC_DAIFMT_BC_FC:
134                 break;
135
136         case SND_SOC_DAIFMT_BP_FC:
137         case SND_SOC_DAIFMT_BC_FP:
138                 dev_err(dai->dev, "only CBS_CFS and CBM_CFM are supported\n");
139                 fallthrough;
140         default:
141                 return -EINVAL;
142         }
143
144         iface->fmt = fmt;
145         return 0;
146 }
147
148 static int axg_tdm_iface_startup(struct snd_pcm_substream *substream,
149                                  struct snd_soc_dai *dai)
150 {
151         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
152         struct axg_tdm_stream *ts =
153                 snd_soc_dai_get_dma_data(dai, substream);
154         int ret;
155
156         if (!axg_tdm_slots_total(ts->mask)) {
157                 dev_err(dai->dev, "interface has not slots\n");
158                 return -EINVAL;
159         }
160
161         if (snd_soc_component_active(dai->component)) {
162                 /* Apply component wide rate symmetry */
163                 ret = snd_pcm_hw_constraint_single(substream->runtime,
164                                                    SNDRV_PCM_HW_PARAM_RATE,
165                                                    iface->rate);
166
167         } else {
168                 /* Limit rate according to the slot number and width */
169                 unsigned int max_rate =
170                         MAX_SCLK / (iface->slots * iface->slot_width);
171                 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
172                                                    SNDRV_PCM_HW_PARAM_RATE,
173                                                    0, max_rate);
174         }
175
176         if (ret < 0)
177                 dev_err(dai->dev, "can't set iface rate constraint\n");
178         else
179                 ret = 0;
180
181         return ret;
182 }
183
184 static int axg_tdm_iface_set_stream(struct snd_pcm_substream *substream,
185                                     struct snd_pcm_hw_params *params,
186                                     struct snd_soc_dai *dai)
187 {
188         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
189         struct axg_tdm_stream *ts = snd_soc_dai_get_dma_data(dai, substream);
190         unsigned int channels = params_channels(params);
191         unsigned int width = params_width(params);
192
193         /* Save rate and sample_bits for component symmetry */
194         iface->rate = params_rate(params);
195
196         /* Make sure this interface can cope with the stream */
197         if (axg_tdm_slots_total(ts->mask) < channels) {
198                 dev_err(dai->dev, "not enough slots for channels\n");
199                 return -EINVAL;
200         }
201
202         if (iface->slot_width < width) {
203                 dev_err(dai->dev, "incompatible slots width for stream\n");
204                 return -EINVAL;
205         }
206
207         /* Save the parameter for tdmout/tdmin widgets */
208         ts->physical_width = params_physical_width(params);
209         ts->width = params_width(params);
210         ts->channels = params_channels(params);
211
212         return 0;
213 }
214
215 static int axg_tdm_iface_set_lrclk(struct snd_soc_dai *dai,
216                                    struct snd_pcm_hw_params *params)
217 {
218         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
219         unsigned int ratio_num;
220         int ret;
221
222         ret = clk_set_rate(iface->lrclk, params_rate(params));
223         if (ret) {
224                 dev_err(dai->dev, "setting sample clock failed: %d\n", ret);
225                 return ret;
226         }
227
228         switch (iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
229         case SND_SOC_DAIFMT_I2S:
230         case SND_SOC_DAIFMT_LEFT_J:
231         case SND_SOC_DAIFMT_RIGHT_J:
232                 /* 50% duty cycle ratio */
233                 ratio_num = 1;
234                 break;
235
236         case SND_SOC_DAIFMT_DSP_A:
237         case SND_SOC_DAIFMT_DSP_B:
238                 /*
239                  * A zero duty cycle ratio will result in setting the mininum
240                  * ratio possible which, for this clock, is 1 cycle of the
241                  * parent bclk clock high and the rest low, This is exactly
242                  * what we want here.
243                  */
244                 ratio_num = 0;
245                 break;
246
247         default:
248                 return -EINVAL;
249         }
250
251         ret = clk_set_duty_cycle(iface->lrclk, ratio_num, 2);
252         if (ret) {
253                 dev_err(dai->dev,
254                         "setting sample clock duty cycle failed: %d\n", ret);
255                 return ret;
256         }
257
258         /* Set sample clock inversion */
259         ret = clk_set_phase(iface->lrclk,
260                             axg_tdm_lrclk_invert(iface->fmt) ? 180 : 0);
261         if (ret) {
262                 dev_err(dai->dev,
263                         "setting sample clock phase failed: %d\n", ret);
264                 return ret;
265         }
266
267         return 0;
268 }
269
270 static int axg_tdm_iface_set_sclk(struct snd_soc_dai *dai,
271                                   struct snd_pcm_hw_params *params)
272 {
273         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
274         unsigned long srate;
275         int ret;
276
277         srate = iface->slots * iface->slot_width * params_rate(params);
278
279         if (!iface->mclk_rate) {
280                 /* If no specific mclk is requested, default to bit clock * 2 */
281                 clk_set_rate(iface->mclk, 2 * srate);
282         } else {
283                 /* Check if we can actually get the bit clock from mclk */
284                 if (iface->mclk_rate % srate) {
285                         dev_err(dai->dev,
286                                 "can't derive sclk %lu from mclk %lu\n",
287                                 srate, iface->mclk_rate);
288                         return -EINVAL;
289                 }
290         }
291
292         ret = clk_set_rate(iface->sclk, srate);
293         if (ret) {
294                 dev_err(dai->dev, "setting bit clock failed: %d\n", ret);
295                 return ret;
296         }
297
298         /* Set the bit clock inversion */
299         ret = clk_set_phase(iface->sclk,
300                             axg_tdm_sclk_invert(iface->fmt) ? 0 : 180);
301         if (ret) {
302                 dev_err(dai->dev, "setting bit clock phase failed: %d\n", ret);
303                 return ret;
304         }
305
306         return ret;
307 }
308
309 static int axg_tdm_iface_hw_params(struct snd_pcm_substream *substream,
310                                    struct snd_pcm_hw_params *params,
311                                    struct snd_soc_dai *dai)
312 {
313         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
314         int ret;
315
316         switch (iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
317         case SND_SOC_DAIFMT_I2S:
318         case SND_SOC_DAIFMT_LEFT_J:
319         case SND_SOC_DAIFMT_RIGHT_J:
320                 if (iface->slots > 2) {
321                         dev_err(dai->dev, "bad slot number for format: %d\n",
322                                 iface->slots);
323                         return -EINVAL;
324                 }
325                 break;
326
327         case SND_SOC_DAIFMT_DSP_A:
328         case SND_SOC_DAIFMT_DSP_B:
329                 break;
330
331         default:
332                 dev_err(dai->dev, "unsupported dai format\n");
333                 return -EINVAL;
334         }
335
336         ret = axg_tdm_iface_set_stream(substream, params, dai);
337         if (ret)
338                 return ret;
339
340         if ((iface->fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) ==
341             SND_SOC_DAIFMT_BP_FP) {
342                 ret = axg_tdm_iface_set_sclk(dai, params);
343                 if (ret)
344                         return ret;
345
346                 ret = axg_tdm_iface_set_lrclk(dai, params);
347                 if (ret)
348                         return ret;
349         }
350
351         return 0;
352 }
353
354 static int axg_tdm_iface_trigger(struct snd_pcm_substream *substream,
355                                  int cmd,
356                                  struct snd_soc_dai *dai)
357 {
358         struct axg_tdm_stream *ts =
359                 snd_soc_dai_get_dma_data(dai, substream);
360
361         switch (cmd) {
362         case SNDRV_PCM_TRIGGER_START:
363         case SNDRV_PCM_TRIGGER_RESUME:
364         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
365                 axg_tdm_stream_start(ts);
366                 break;
367         case SNDRV_PCM_TRIGGER_SUSPEND:
368         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
369         case SNDRV_PCM_TRIGGER_STOP:
370                 axg_tdm_stream_stop(ts);
371                 break;
372         default:
373                 return -EINVAL;
374         }
375
376         return 0;
377 }
378
379 static int axg_tdm_iface_remove_dai(struct snd_soc_dai *dai)
380 {
381         if (dai->capture_dma_data)
382                 axg_tdm_stream_free(dai->capture_dma_data);
383
384         if (dai->playback_dma_data)
385                 axg_tdm_stream_free(dai->playback_dma_data);
386
387         return 0;
388 }
389
390 static int axg_tdm_iface_probe_dai(struct snd_soc_dai *dai)
391 {
392         struct axg_tdm_iface *iface = snd_soc_dai_get_drvdata(dai);
393
394         if (dai->capture_widget) {
395                 dai->capture_dma_data = axg_tdm_stream_alloc(iface);
396                 if (!dai->capture_dma_data)
397                         return -ENOMEM;
398         }
399
400         if (dai->playback_widget) {
401                 dai->playback_dma_data = axg_tdm_stream_alloc(iface);
402                 if (!dai->playback_dma_data) {
403                         axg_tdm_iface_remove_dai(dai);
404                         return -ENOMEM;
405                 }
406         }
407
408         return 0;
409 }
410
411 static const struct snd_soc_dai_ops axg_tdm_iface_ops = {
412         .set_sysclk     = axg_tdm_iface_set_sysclk,
413         .set_fmt        = axg_tdm_iface_set_fmt,
414         .startup        = axg_tdm_iface_startup,
415         .hw_params      = axg_tdm_iface_hw_params,
416         .trigger        = axg_tdm_iface_trigger,
417 };
418
419 /* TDM Backend DAIs */
420 static const struct snd_soc_dai_driver axg_tdm_iface_dai_drv[] = {
421         [TDM_IFACE_PAD] = {
422                 .name = "TDM Pad",
423                 .playback = {
424                         .stream_name    = "Playback",
425                         .channels_min   = 1,
426                         .channels_max   = AXG_TDM_CHANNEL_MAX,
427                         .rates          = AXG_TDM_RATES,
428                         .formats        = AXG_TDM_FORMATS,
429                 },
430                 .capture = {
431                         .stream_name    = "Capture",
432                         .channels_min   = 1,
433                         .channels_max   = AXG_TDM_CHANNEL_MAX,
434                         .rates          = AXG_TDM_RATES,
435                         .formats        = AXG_TDM_FORMATS,
436                 },
437                 .id = TDM_IFACE_PAD,
438                 .ops = &axg_tdm_iface_ops,
439                 .probe = axg_tdm_iface_probe_dai,
440                 .remove = axg_tdm_iface_remove_dai,
441         },
442         [TDM_IFACE_LOOPBACK] = {
443                 .name = "TDM Loopback",
444                 .capture = {
445                         .stream_name    = "Loopback",
446                         .channels_min   = 1,
447                         .channels_max   = AXG_TDM_CHANNEL_MAX,
448                         .rates          = AXG_TDM_RATES,
449                         .formats        = AXG_TDM_FORMATS,
450                 },
451                 .id = TDM_IFACE_LOOPBACK,
452                 .ops = &axg_tdm_iface_ops,
453                 .probe = axg_tdm_iface_probe_dai,
454                 .remove = axg_tdm_iface_remove_dai,
455         },
456 };
457
458 static int axg_tdm_iface_set_bias_level(struct snd_soc_component *component,
459                                         enum snd_soc_bias_level level)
460 {
461         struct axg_tdm_iface *iface = snd_soc_component_get_drvdata(component);
462         enum snd_soc_bias_level now =
463                 snd_soc_component_get_bias_level(component);
464         int ret = 0;
465
466         switch (level) {
467         case SND_SOC_BIAS_PREPARE:
468                 if (now == SND_SOC_BIAS_STANDBY)
469                         ret = clk_prepare_enable(iface->mclk);
470                 break;
471
472         case SND_SOC_BIAS_STANDBY:
473                 if (now == SND_SOC_BIAS_PREPARE)
474                         clk_disable_unprepare(iface->mclk);
475                 break;
476
477         case SND_SOC_BIAS_OFF:
478         case SND_SOC_BIAS_ON:
479                 break;
480         }
481
482         return ret;
483 }
484
485 static const struct snd_soc_dapm_widget axg_tdm_iface_dapm_widgets[] = {
486         SND_SOC_DAPM_SIGGEN("Playback Signal"),
487 };
488
489 static const struct snd_soc_dapm_route axg_tdm_iface_dapm_routes[] = {
490         { "Loopback", NULL, "Playback Signal" },
491 };
492
493 static const struct snd_soc_component_driver axg_tdm_iface_component_drv = {
494         .dapm_widgets           = axg_tdm_iface_dapm_widgets,
495         .num_dapm_widgets       = ARRAY_SIZE(axg_tdm_iface_dapm_widgets),
496         .dapm_routes            = axg_tdm_iface_dapm_routes,
497         .num_dapm_routes        = ARRAY_SIZE(axg_tdm_iface_dapm_routes),
498         .set_bias_level         = axg_tdm_iface_set_bias_level,
499 };
500
501 static const struct of_device_id axg_tdm_iface_of_match[] = {
502         { .compatible = "amlogic,axg-tdm-iface", },
503         {}
504 };
505 MODULE_DEVICE_TABLE(of, axg_tdm_iface_of_match);
506
507 static int axg_tdm_iface_probe(struct platform_device *pdev)
508 {
509         struct device *dev = &pdev->dev;
510         struct snd_soc_dai_driver *dai_drv;
511         struct axg_tdm_iface *iface;
512         int ret, i;
513
514         iface = devm_kzalloc(dev, sizeof(*iface), GFP_KERNEL);
515         if (!iface)
516                 return -ENOMEM;
517         platform_set_drvdata(pdev, iface);
518
519         /*
520          * Duplicate dai driver: depending on the slot masks configuration
521          * We'll change the number of channel provided by DAI stream, so dpcm
522          * channel merge can be done properly
523          */
524         dai_drv = devm_kcalloc(dev, ARRAY_SIZE(axg_tdm_iface_dai_drv),
525                                sizeof(*dai_drv), GFP_KERNEL);
526         if (!dai_drv)
527                 return -ENOMEM;
528
529         for (i = 0; i < ARRAY_SIZE(axg_tdm_iface_dai_drv); i++)
530                 memcpy(&dai_drv[i], &axg_tdm_iface_dai_drv[i],
531                        sizeof(*dai_drv));
532
533         /* Bit clock provided on the pad */
534         iface->sclk = devm_clk_get(dev, "sclk");
535         if (IS_ERR(iface->sclk))
536                 return dev_err_probe(dev, PTR_ERR(iface->sclk), "failed to get sclk\n");
537
538         /* Sample clock provided on the pad */
539         iface->lrclk = devm_clk_get(dev, "lrclk");
540         if (IS_ERR(iface->lrclk))
541                 return dev_err_probe(dev, PTR_ERR(iface->lrclk), "failed to get lrclk\n");
542
543         /*
544          * mclk maybe be missing when the cpu dai is in slave mode and
545          * the codec does not require it to provide a master clock.
546          * At this point, ignore the error if mclk is missing. We'll
547          * throw an error if the cpu dai is master and mclk is missing
548          */
549         iface->mclk = devm_clk_get(dev, "mclk");
550         if (IS_ERR(iface->mclk)) {
551                 ret = PTR_ERR(iface->mclk);
552                 if (ret == -ENOENT)
553                         iface->mclk = NULL;
554                 else
555                         return dev_err_probe(dev, ret, "failed to get mclk\n");
556         }
557
558         return devm_snd_soc_register_component(dev,
559                                         &axg_tdm_iface_component_drv, dai_drv,
560                                         ARRAY_SIZE(axg_tdm_iface_dai_drv));
561 }
562
563 static struct platform_driver axg_tdm_iface_pdrv = {
564         .probe = axg_tdm_iface_probe,
565         .driver = {
566                 .name = "axg-tdm-iface",
567                 .of_match_table = axg_tdm_iface_of_match,
568         },
569 };
570 module_platform_driver(axg_tdm_iface_pdrv);
571
572 MODULE_DESCRIPTION("Amlogic AXG TDM interface driver");
573 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
574 MODULE_LICENSE("GPL v2");