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