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
|
// SPDX-License-Identifier: (GPL-2.0 OR MIT)
//
// Copyright (c) 2018 BayLibre, SAS.
// Author: Jerome Brunet <jbrunet@baylibre.com>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <sound/soc.h>
#include <sound/soc-dai.h>
#include "axg-tdm.h"
#include "meson-card.h"
struct axg_dai_link_tdm_mask {
u32 tx;
u32 rx;
};
struct axg_dai_link_tdm_data {
unsigned int mclk_fs;
unsigned int slots;
unsigned int slot_width;
u32 *tx_mask;
u32 *rx_mask;
struct axg_dai_link_tdm_mask *codec_masks;
};
/*
* Base params for the codec to codec links
* Those will be over-written by the CPU side of the link
*/
static const struct snd_soc_pcm_stream codec_params = {
.formats = SNDRV_PCM_FMTBIT_S24_LE,
.rate_min = 5525,
.rate_max = 192000,
.channels_min = 1,
.channels_max = 8,
};
static int axg_card_tdm_be_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
struct axg_dai_link_tdm_data *be =
(struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
return meson_card_i2s_set_sysclk(substream, params, be->mclk_fs);
}
static const struct snd_soc_ops axg_card_tdm_be_ops = {
.hw_params = axg_card_tdm_be_hw_params,
};
static int axg_card_tdm_dai_init(struct snd_soc_pcm_runtime *rtd)
{
struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
struct axg_dai_link_tdm_data *be =
(struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
struct snd_soc_dai *codec_dai;
int ret, i;
for_each_rtd_codec_dais(rtd, i, codec_dai) {
ret = snd_soc_dai_set_tdm_slot(codec_dai,
be->codec_masks[i].tx,
be->codec_masks[i].rx,
be->slots, be->slot_width);
if (ret && ret != -ENOTSUPP) {
dev_err(codec_dai->dev,
"setting tdm link slots failed\n");
return ret;
}
}
ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), be->tx_mask, be->rx_mask,
be->slots, be->slot_width);
if (ret) {
dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
return ret;
}
return 0;
}
static int axg_card_tdm_dai_lb_init(struct snd_soc_pcm_runtime *rtd)
{
struct meson_card *priv = snd_soc_card_get_drvdata(rtd->card);
struct axg_dai_link_tdm_data *be =
(struct axg_dai_link_tdm_data *)priv->link_data[rtd->num];
int ret;
/* The loopback rx_mask is the pad tx_mask */
ret = axg_tdm_set_tdm_slots(asoc_rtd_to_cpu(rtd, 0), NULL, be->tx_mask,
be->slots, be->slot_width);
if (ret) {
dev_err(asoc_rtd_to_cpu(rtd, 0)->dev, "setting tdm link slots failed\n");
return ret;
}
return 0;
}
static int axg_card_add_tdm_loopback(struct snd_soc_card *card,
int *index)
{
struct meson_card *priv = snd_soc_card_get_drvdata(card);
struct snd_soc_dai_link *pad = &card->dai_link[*index];
struct snd_soc_dai_link *lb;
struct snd_soc_dai_link_component *dlc;
int ret;
/* extend links */
ret = meson_card_reallocate_links(card, card->num_links + 1);
if (ret)
return ret;
lb = &card->dai_link[*index + 1];
lb->name = kasprintf(GFP_KERNEL, "%s-lb", pad->name);
if (!lb->name)
return -ENOMEM;
dlc = devm_kzalloc(card->dev, 2 * sizeof(*dlc), GFP_KERNEL);
if (!dlc)
return -ENOMEM;
lb->cpus = &dlc[0];
lb->codecs = &dlc[1];
lb->num_cpus = 1;
lb->num_codecs = 1;
lb->stream_name = lb->name;
lb->cpus->of_node = pad->cpus->of_node;
lb->cpus->dai_name = "TDM Loopback";
lb->codecs->name = "snd-soc-dummy";
lb->codecs->dai_name = "snd-soc-dummy-dai";
lb->dpcm_capture = 1;
lb->no_pcm = 1;
lb->ops = &axg_card_tdm_be_ops;
lb->init = axg_card_tdm_dai_lb_init;
/* Provide the same link data to the loopback */
priv->link_data[*index + 1] = priv->link_data[*index];
/*
* axg_card_clean_references() will iterate over this link,
* make sure the node count is balanced
*/
of_node_get(lb->cpus->of_node);
/* Let add_links continue where it should */
*index += 1;
return 0;
}
static int axg_card_parse_cpu_tdm_slots(struct snd_soc_card *card,
struct snd_soc_dai_link *link,
struct device_node *node,
struct axg_dai_link_tdm_data *be)
{
char propname[32];
u32 tx, rx;
int i;
be->tx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
sizeof(*be->tx_mask), GFP_KERNEL);
be->rx_mask = devm_kcalloc(card->dev, AXG_TDM_NUM_LANES,
sizeof(*be->rx_mask), GFP_KERNEL);
if (!be->tx_mask || !be->rx_mask)
return -ENOMEM;
for (i = 0, tx = 0; i < AXG_TDM_NUM_LANES; i++) {
snprintf(propname, 32, "dai-tdm-slot-tx-mask-%d", i);
snd_soc_of_get_slot_mask(node, propname, &be->tx_mask[i]);
tx = max(tx, be->tx_mask[i]);
}
/* Disable playback is the interface has no tx slots */
if (!tx)
link->dpcm_playback = 0;
for (i = 0, rx = 0; i < AXG_TDM_NUM_LANES; i++) {
snprintf(propname, 32, "dai-tdm-slot-rx-mask-%d", i);
snd_soc_of_get_slot_mask(node, propname, &be->rx_mask[i]);
rx = max(rx, be->rx_mask[i]);
}
/* Disable capture is the interface has no rx slots */
if (!rx)
link->dpcm_capture = 0;
/* ... but the interface should at least have one of them */
if (!tx && !rx) {
dev_err(card->dev, "tdm link has no cpu slots\n");
return -EINVAL;
}
of_property_read_u32(node, "dai-tdm-slot-num", &be->slots);
if (!be->slots) {
/*
* If the slot number is not provided, set it such as it
* accommodates the largest mask
*/
be->slots = fls(max(tx, rx));
} else if (be->slots < fls(max(tx, rx)) || be->slots > 32) {
/*
* Error if the slots can't accommodate the largest mask or
* if it is just too big
*/
dev_err(card->dev, "bad slot number\n");
return -EINVAL;
}
of_property_read_u32(node, "dai-tdm-slot-width", &be->slot_width);
return 0;
}
static int axg_card_parse_codecs_masks(struct snd_soc_card *card,
struct snd_soc_dai_link *link,
struct device_node *node,
struct axg_dai_link_tdm_data *be)
{
struct axg_dai_link_tdm_mask *codec_mask;
struct device_node *np;
codec_mask = devm_kcalloc(card->dev, link->num_codecs,
sizeof(*codec_mask), GFP_KERNEL);
if (!codec_mask)
return -ENOMEM;
be->codec_masks = codec_mask;
for_each_child_of_node(node, np) {
snd_soc_of_get_slot_mask(np, "dai-tdm-slot-rx-mask",
&codec_mask->rx);
snd_soc_of_get_slot_mask(np, "dai-tdm-slot-tx-mask",
&codec_mask->tx);
codec_mask++;
}
return 0;
}
static int axg_card_parse_tdm(struct snd_soc_card *card,
struct device_node *node,
int *index)
{
struct meson_card *priv = snd_soc_card_get_drvdata(card);
struct snd_soc_dai_link *link = &card->dai_link[*index];
struct axg_dai_link_tdm_data *be;
int ret;
/* Allocate tdm link parameters */
be = devm_kzalloc(card->dev, sizeof(*be), GFP_KERNEL);
if (!be)
return -ENOMEM;
priv->link_data[*index] = be;
/* Setup tdm link */
link->ops = &axg_card_tdm_be_ops;
link->init = axg_card_tdm_dai_init;
link->dai_fmt = meson_card_parse_daifmt(node, link->cpus->of_node);
of_property_read_u32(node, "mclk-fs", &be->mclk_fs);
ret = axg_card_parse_cpu_tdm_slots(card, link, node, be);
if (ret) {
dev_err(card->dev, "error parsing tdm link slots\n");
return ret;
}
ret = axg_card_parse_codecs_masks(card, link, node, be);
if (ret)
return ret;
/* Add loopback if the pad dai has playback */
if (link->dpcm_playback) {
ret = axg_card_add_tdm_loopback(card, index);
if (ret)
return ret;
}
return 0;
}
static int axg_card_cpu_is_capture_fe(struct device_node *np)
{
return of_device_is_compatible(np, DT_PREFIX "axg-toddr");
}
static int axg_card_cpu_is_playback_fe(struct device_node *np)
{
return of_device_is_compatible(np, DT_PREFIX "axg-frddr");
}
static int axg_card_cpu_is_tdm_iface(struct device_node *np)
{
return of_device_is_compatible(np, DT_PREFIX "axg-tdm-iface");
}
static int axg_card_cpu_is_codec(struct device_node *np)
{
return of_device_is_compatible(np, DT_PREFIX "g12a-tohdmitx") ||
of_device_is_compatible(np, DT_PREFIX "g12a-toacodec");
}
static int axg_card_add_link(struct snd_soc_card *card, struct device_node *np,
int *index)
{
struct snd_soc_dai_link *dai_link = &card->dai_link[*index];
struct snd_soc_dai_link_component *cpu;
int ret;
cpu = devm_kzalloc(card->dev, sizeof(*cpu), GFP_KERNEL);
if (!cpu)
return -ENOMEM;
dai_link->cpus = cpu;
dai_link->num_cpus = 1;
ret = meson_card_parse_dai(card, np, &dai_link->cpus->of_node,
&dai_link->cpus->dai_name);
if (ret)
return ret;
if (axg_card_cpu_is_playback_fe(dai_link->cpus->of_node))
ret = meson_card_set_fe_link(card, dai_link, np, true);
else if (axg_card_cpu_is_capture_fe(dai_link->cpus->of_node))
ret = meson_card_set_fe_link(card, dai_link, np, false);
else
ret = meson_card_set_be_link(card, dai_link, np);
if (ret)
return ret;
if (axg_card_cpu_is_tdm_iface(dai_link->cpus->of_node))
ret = axg_card_parse_tdm(card, np, index);
else if (axg_card_cpu_is_codec(dai_link->cpus->of_node))
dai_link->params = &codec_params;
return ret;
}
static const struct meson_card_match_data axg_card_match_data = {
.add_link = axg_card_add_link,
};
static const struct of_device_id axg_card_of_match[] = {
{
.compatible = "amlogic,axg-sound-card",
.data = &axg_card_match_data,
}, {}
};
MODULE_DEVICE_TABLE(of, axg_card_of_match);
static struct platform_driver axg_card_pdrv = {
.probe = meson_card_probe,
.remove = meson_card_remove,
.driver = {
.name = "axg-sound-card",
.of_match_table = axg_card_of_match,
},
};
module_platform_driver(axg_card_pdrv);
MODULE_DESCRIPTION("Amlogic AXG ALSA machine driver");
MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
MODULE_LICENSE("GPL v2");
|