hc
2023-12-11 6778948f9de86c3cfaf36725a7c87dcff9ba247f
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// SPDX-License-Identifier: GPL-2.0
/*
 * ALSA SoC Audio Layer - Rockchip Multi-DAIS  driver
 *
 * Copyright (c) 2018 Rockchip Electronics Co. Ltd.
 * Author: Sugar Zhang <sugar.zhang@rock-chips.com>
 *
 */
 
#include <linux/module.h>
#include <linux/mfd/syscon.h>
#include <linux/of_device.h>
#include <linux/pm_runtime.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
 
#include "rockchip_multi_dais.h"
 
#define BITCLOCK_INV_STR    "bitclock-inversion"
#define FRAME_INV_STR        "frame-inversion"
#define BITCLOCK_MASTER_STR    "bitclock-master"
#define FRAME_MASTER_STR    "frame-master"
#define DAIS_DRV_NAME        "rockchip-mdais"
#define RK3308_GRF_SOC_CON2    0x308
 
#define SOUND_NAME_PREFIX    "sound-name-prefix"
 
static inline struct rk_mdais_dev *to_info(struct snd_soc_dai *dai)
{
   return snd_soc_dai_get_drvdata(dai);
}
 
static void hw_refine_channels(struct snd_pcm_hw_params *params,
                  unsigned int channel)
{
   struct snd_interval *c =
       hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
 
   c->min = channel;
   c->max = channel;
}
 
static int rockchip_mdais_hw_params(struct snd_pcm_substream *substream,
                   struct snd_pcm_hw_params *params,
                   struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_pcm_hw_params *cparams;
   struct snd_soc_dai *child;
   unsigned int *channel_maps;
   int ret = 0, i = 0;
 
   cparams = kmemdup(params, sizeof(*params), GFP_KERNEL);
   if (IS_ERR(cparams))
       return PTR_ERR(cparams);
 
   if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
       channel_maps = mdais->playback_channel_maps;
   else
       channel_maps = mdais->capture_channel_maps;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       if (channel_maps[i])
           hw_refine_channels(cparams, channel_maps[i]);
       if (child->driver->ops && child->driver->ops->hw_params) {
           ret = child->driver->ops->hw_params(substream, cparams, child);
           if (ret < 0) {
               dev_err(dai->dev, "ASoC: can't set %s hw params: %d\n",
                   dai->name, ret);
               return ret;
           }
       }
   }
 
   kfree(cparams);
   return 0;
}
 
static int rockchip_mdais_trigger(struct snd_pcm_substream *substream,
                 int cmd, struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_dai *child;
   unsigned int *channel_maps;
   int ret = 0, i = 0;
 
   if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
       channel_maps = mdais->playback_channel_maps;
   else
       channel_maps = mdais->capture_channel_maps;
 
   for (i = 0; i < mdais->num_dais; i++) {
       /* skip DAIs which have no channel mapping */
       if (!channel_maps[i])
           continue;
 
       child = mdais->dais[i].dai;
       if (child->driver->ops && child->driver->ops->trigger) {
           ret = child->driver->ops->trigger(substream,
                             cmd, child);
           if (ret < 0)
               return ret;
       }
   }
 
   return 0;
}
 
static int rockchip_mdais_startup(struct snd_pcm_substream *substream,
                 struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_dai *child;
   int ret = 0, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       if (child->driver->ops && child->driver->ops->startup) {
           ret = child->driver->ops->startup(substream, child);
           if (ret < 0)
               return ret;
       }
   }
 
   return 0;
}
 
static void rockchip_mdais_shutdown(struct snd_pcm_substream *substream,
                 struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_dai *child;
   int i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       if (child->driver->ops && child->driver->ops->shutdown) {
           child->driver->ops->shutdown(substream, child);
       }
   }
}
 
static int rockchip_mdais_prepare(struct snd_pcm_substream *substream,
                 struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_dai *child;
   int ret = 0, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       if (child->driver->ops && child->driver->ops->prepare) {
           ret = child->driver->ops->prepare(substream, child);
           if (ret < 0)
               return ret;
       }
   }
 
   return 0;
}
 
static int rockchip_mdais_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id,
                    unsigned int freq, int dir)
{
   struct rk_mdais_dev *mdais = to_info(cpu_dai);
   struct snd_soc_dai *child;
   int ret, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       ret = snd_soc_dai_set_sysclk(child, clk_id, freq, dir);
       if (ret && ret != -ENOTSUPP)
           return ret;
   }
 
   return 0;
}
 
static int rockchip_mdais_set_fmt(struct snd_soc_dai *cpu_dai,
                 unsigned int fmt)
{
   struct rk_mdais_dev *mdais = to_info(cpu_dai);
   struct snd_soc_dai *child;
   unsigned int dai_fmt;
   int ret, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       dai_fmt = fmt;
       if (mdais->dais[i].fmt_msk) {
           dai_fmt &= ~(mdais->dais[i].fmt_msk);
           dai_fmt |= mdais->dais[i].fmt;
       }
       ret = snd_soc_dai_set_fmt(child, dai_fmt);
       if (ret && ret != -ENOTSUPP)
           return ret;
   }
 
   return 0;
}
 
static int rockchip_mdais_tdm_slot(struct snd_soc_dai *dai,
                  unsigned int tx_mask, unsigned int rx_mask,
                  int slots, int slot_width)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_dai *child;
   int ret, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       ret = snd_soc_dai_set_tdm_slot(child, tx_mask, rx_mask,
                          slots, slot_width);
       if (ret && ret != -ENOTSUPP)
           return ret;
   }
 
   return 0;
}
 
static int rockchip_mdais_dai_probe(struct snd_soc_dai *dai)
{
   struct rk_mdais_dev *mdais = to_info(dai);
   struct snd_soc_component *comp;
   struct snd_soc_dai *child;
   const char *str;
   int ret, i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       comp = child->component;
       if (!child->probed && child->driver->probe) {
           if (!comp->name_prefix) {
               ret = device_property_read_string(child->dev,
                                 SOUND_NAME_PREFIX, &str);
               if (!ret)
                   comp->name_prefix = str;
           }
 
           comp->card = dai->component->card;
           ret = child->driver->probe(child);
           if (ret < 0) {
               dev_err(child->dev,
                   "ASoC: failed to probe DAI %s: %d\n",
                   child->name, ret);
               return ret;
           }
 
           ret = snd_soc_add_component_controls(comp,
                                comp->driver->controls,
                                comp->driver->num_controls);
           if (ret)
               dev_err(dai->dev, "%s: Failed to add controls, should add '%s' in DT\n",
                   dev_name(child->dev), SOUND_NAME_PREFIX);
 
           dai->probed = 1;
       }
   }
 
   return 0;
}
 
static const struct snd_soc_dai_ops rockchip_mdais_dai_ops = {
   .hw_params = rockchip_mdais_hw_params,
   .set_sysclk = rockchip_mdais_set_sysclk,
   .set_fmt = rockchip_mdais_set_fmt,
   .set_tdm_slot = rockchip_mdais_tdm_slot,
   .trigger = rockchip_mdais_trigger,
   .startup = rockchip_mdais_startup,
   .shutdown = rockchip_mdais_shutdown,
   .prepare = rockchip_mdais_prepare,
};
 
static const struct snd_soc_component_driver rockchip_mdais_component = {
   .name = DAIS_DRV_NAME,
};
 
static const struct of_device_id rockchip_mdais_match[] = {
   { .compatible = "rockchip,multi-dais", },
   { .compatible = "rockchip,rk3308-multi-dais", },
   {},
};
 
static struct snd_soc_dai *rockchip_mdais_find_dai(struct device_node *np)
{
   struct snd_soc_dai_link_component dai_component = { 0 };
 
   dai_component.of_node = np;
 
   return snd_soc_find_dai_with_mutex(&dai_component);
}
 
static int mdais_runtime_suspend(struct device *dev)
{
   struct rk_mdais_dev *mdais = dev_get_drvdata(dev);
   struct snd_soc_dai *child;
   int i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       pm_runtime_put(child->dev);
   }
 
   return 0;
}
 
static int mdais_runtime_resume(struct device *dev)
{
   struct rk_mdais_dev *mdais = dev_get_drvdata(dev);
   struct snd_soc_dai *child;
   int i = 0;
 
   for (i = 0; i < mdais->num_dais; i++) {
       child = mdais->dais[i].dai;
       pm_runtime_get_sync(child->dev);
   }
 
   return 0;
}
 
static int mdais_read_prop_array(struct device_node *node,
                const char *propname,
                unsigned int *array, int num)
{
   int ret = 0;
 
   memset(array, 0, sizeof(*array) * num);
   if (of_property_read_bool(node, propname)) {
       ret = of_property_read_u32_array(node, propname, array, num);
       if (ret)
           ret = -EINVAL;
   } else {
       ret = -EINVAL;
   }
 
   return ret;
}
 
static void mdais_parse_daifmt(struct device_node *node, struct rk_dai *dais,
                  int num_dai)
{
   unsigned int cinv[MAX_DAIS], finv[MAX_DAIS];
   unsigned int cmst[MAX_DAIS], fmst[MAX_DAIS];
   unsigned int format = 0, format_mask = 0;
   int i = 0, ret = 0;
 
   ret = mdais_read_prop_array(node, BITCLOCK_INV_STR, cinv, num_dai);
   if (!ret)
       format_mask |= SND_SOC_DAIFMT_INV_MASK;
   ret = mdais_read_prop_array(node, FRAME_INV_STR, finv, num_dai);
   if (!ret)
       format_mask |= SND_SOC_DAIFMT_INV_MASK;
   ret = mdais_read_prop_array(node, BITCLOCK_MASTER_STR, cmst, num_dai);
   if (!ret)
       format_mask |= SND_SOC_DAIFMT_MASTER_MASK;
   ret = mdais_read_prop_array(node, FRAME_MASTER_STR, fmst, num_dai);
   if (!ret)
       format_mask |= SND_SOC_DAIFMT_MASTER_MASK;
 
   for (i = 0; i < num_dai; i++) {
       format = 0;
 
       switch ((cinv[i] << 4) + finv[i]) {
       case 0x11:
           format |= SND_SOC_DAIFMT_IB_IF;
           break;
       case 0x10:
           format |= SND_SOC_DAIFMT_IB_NF;
           break;
       case 0x01:
           format |= SND_SOC_DAIFMT_NB_IF;
           break;
       default:
           /* SND_SOC_DAIFMT_NB_NF is default */
           break;
       }
 
       switch ((!cmst[i] << 4) + !fmst[i]) {
       case 0x11:
           format |= SND_SOC_DAIFMT_CBM_CFM;
           break;
       case 0x10:
           format |= SND_SOC_DAIFMT_CBM_CFS;
           break;
       case 0x01:
           format |= SND_SOC_DAIFMT_CBS_CFM;
           break;
       default:
           format |= SND_SOC_DAIFMT_CBS_CFS;
           break;
       }
 
       dais[i].fmt = format & format_mask;
       dais[i].fmt_msk = format_mask;
   }
}
 
static int rockchip_mdais_dai_prepare(struct platform_device *pdev,
                     struct snd_soc_dai_driver **soc_dai)
{
   struct snd_soc_dai_driver rockchip_mdais_dai = {
       .probe = rockchip_mdais_dai_probe,
       .playback = {
           .stream_name = "Playback",
           .channels_min = 1,
           .channels_max = 512,
           .rates = SNDRV_PCM_RATE_8000_384000,
           .formats = (SNDRV_PCM_FMTBIT_S8 |
                   SNDRV_PCM_FMTBIT_S16_LE |
                   SNDRV_PCM_FMTBIT_S20_3LE |
                   SNDRV_PCM_FMTBIT_S24_LE |
                   SNDRV_PCM_FMTBIT_S32_LE),
       },
       .capture = {
           .stream_name = "Capture",
           .channels_min = 1,
           .channels_max = 512,
           .rates = SNDRV_PCM_RATE_8000_384000,
           .formats = (SNDRV_PCM_FMTBIT_S8 |
                   SNDRV_PCM_FMTBIT_S16_LE |
                   SNDRV_PCM_FMTBIT_S20_3LE |
                   SNDRV_PCM_FMTBIT_S24_LE |
                   SNDRV_PCM_FMTBIT_S32_LE),
       },
       .ops = &rockchip_mdais_dai_ops,
   };
 
   *soc_dai = devm_kmemdup(&pdev->dev, &rockchip_mdais_dai,
               sizeof(rockchip_mdais_dai), GFP_KERNEL);
   if (!(*soc_dai))
       return -ENOMEM;
 
   return 0;
}
 
static void mdais_fixup_dai(struct snd_soc_dai_driver *soc_dai,
               struct rk_mdais_dev *mdais)
{
   int i, tch, rch;
   unsigned int *tx_maps, *rx_maps;
 
   tch = 0;
   rch = 0;
   tx_maps = mdais->playback_channel_maps;
   rx_maps = mdais->capture_channel_maps;
   for (i = 0; i < mdais->num_dais; i++) {
       tch += tx_maps[i];
       rch += rx_maps[i];
   }
 
   soc_dai->playback.channels_min = tch;
   soc_dai->playback.channels_max = tch;
   soc_dai->capture.channels_min = rch;
   soc_dai->capture.channels_max = rch;
}
 
static int rockchip_mdais_probe(struct platform_device *pdev)
{
   struct device_node *np = pdev->dev.of_node;
   struct platform_device  *sub_pdev;
   struct rk_mdais_dev *mdais;
   struct device_node *node;
   struct snd_soc_dai_driver *soc_dai;
   struct rk_dai *dais;
   unsigned int *map;
   int count, mp_count;
   int ret = 0, i = 0;
 
   ret = rockchip_mdais_dai_prepare(pdev, &soc_dai);
   if (ret < 0)
       return ret;
 
   mdais = devm_kzalloc(&pdev->dev, sizeof(*mdais), GFP_KERNEL);
   if (!mdais)
       return -ENOMEM;
 
   count = of_count_phandle_with_args(np, "dais", NULL);
   if (count < 0 || count > MAX_DAIS)
       return -EINVAL;
 
   mp_count = of_property_count_u32_elems(np, "capture,channel-mapping");
   if (mp_count != count)
       return -EINVAL;
   mp_count = of_property_count_u32_elems(np, "playback,channel-mapping");
   if (mp_count != count)
       return -EINVAL;
 
   mdais->num_dais = count;
   dais = devm_kcalloc(&pdev->dev, count,
               sizeof(*dais), GFP_KERNEL);
   if (!dais)
       return -ENOMEM;
 
   map = devm_kcalloc(&pdev->dev, count,
              sizeof(*map), GFP_KERNEL);
   if (!map)
       return -ENOMEM;
   ret = of_property_read_u32_array(np, "capture,channel-mapping",
                    map, count);
   if (ret)
       return -EINVAL;
   mdais->capture_channel_maps = map;
   map = devm_kcalloc(&pdev->dev, count,
              sizeof(*map), GFP_KERNEL);
   if (!map)
       return -ENOMEM;
   ret = of_property_read_u32_array(np, "playback,channel-mapping",
                    map, count);
   if (ret)
       return -EINVAL;
   mdais->playback_channel_maps = map;
 
   for (i = 0; i < count; i++) {
       node = of_parse_phandle(np, "dais", i);
       sub_pdev = of_find_device_by_node(node);
       if (!sub_pdev) {
           dev_err(&pdev->dev, "fail to find subnode dev\n");
           return -ENODEV;
       }
       dais[i].of_node = node;
       dais[i].dev = &sub_pdev->dev;
       dais[i].dai = rockchip_mdais_find_dai(node);
       if (!dais[i].dai)
           return -EPROBE_DEFER;
   }
 
   mdais_parse_daifmt(np, dais, count);
   mdais_fixup_dai(soc_dai, mdais);
 
   if (of_device_is_compatible(np, "rockchip,rk3308-multi-dais")) {
       struct regmap *grf;
       const char *name;
       unsigned int i2s0_fmt = 0, i2s1_fmt = 0;
 
       for (i = 0; i < count; i++) {
           name = dev_name(dais[i].dev);
           if (strstr(name, "ff300000"))
               i2s0_fmt = dais[i].fmt;
           else if (strstr(name, "ff310000"))
               i2s1_fmt = dais[i].fmt;
       }
       i2s0_fmt &= SND_SOC_DAIFMT_MASTER_MASK;
       i2s1_fmt &= SND_SOC_DAIFMT_MASTER_MASK;
 
       if ((i2s0_fmt == SND_SOC_DAIFMT_CBS_CFS &&
            i2s1_fmt == SND_SOC_DAIFMT_CBM_CFM) ||
           (i2s0_fmt == SND_SOC_DAIFMT_CBM_CFM &&
            i2s1_fmt == SND_SOC_DAIFMT_CBS_CFS)) {
           grf = syscon_regmap_lookup_by_phandle(np,
                                 "rockchip,grf");
           if (IS_ERR(grf))
               return PTR_ERR(grf);
 
           dev_info(&pdev->dev, "enable i2s 16ch ctrl en\n");
           regmap_write(grf, RK3308_GRF_SOC_CON2,
                    BIT(14) << 16 | BIT(14));
       }
   }
 
   mdais->dais = dais;
   mdais->dev = &pdev->dev;
   dev_set_drvdata(&pdev->dev, mdais);
 
   pm_runtime_enable(&pdev->dev);
   if (!pm_runtime_enabled(&pdev->dev)) {
       ret = mdais_runtime_resume(&pdev->dev);
       if (ret)
           goto err_pm_disable;
   }
 
   ret = devm_snd_soc_register_component(&pdev->dev,
                         &rockchip_mdais_component,
                         soc_dai, 1);
 
   if (ret) {
       dev_err(&pdev->dev, "could not register dai: %d\n", ret);
       goto err_suspend;
   }
 
   ret = snd_dmaengine_mpcm_register(mdais);
   if (ret) {
       dev_err(&pdev->dev, "Could not register PCM\n");
       return ret;
   }
 
   return 0;
 
err_suspend:
   if (!pm_runtime_status_suspended(&pdev->dev))
       mdais_runtime_resume(&pdev->dev);
err_pm_disable:
   pm_runtime_disable(&pdev->dev);
 
   return ret;
}
 
static int rockchip_mdais_remove(struct platform_device *pdev)
{
   snd_dmaengine_mpcm_unregister(&pdev->dev);
   pm_runtime_disable(&pdev->dev);
   if (!pm_runtime_status_suspended(&pdev->dev))
       mdais_runtime_suspend(&pdev->dev);
 
   return 0;
}
 
static const struct dev_pm_ops rockchip_mdais_pm_ops = {
   SET_RUNTIME_PM_OPS(mdais_runtime_suspend, mdais_runtime_resume,
              NULL)
};
 
static struct platform_driver rockchip_mdais_driver = {
   .probe = rockchip_mdais_probe,
   .remove = rockchip_mdais_remove,
   .driver = {
       .name = DAIS_DRV_NAME,
       .of_match_table = of_match_ptr(rockchip_mdais_match),
       .pm = &rockchip_mdais_pm_ops,
   },
};
module_platform_driver(rockchip_mdais_driver);
 
MODULE_DESCRIPTION("ROCKCHIP MULTI-DAIS ASoC Interface");
MODULE_AUTHOR("Sugar Zhang <sugar.zhang@rock-chips.com>");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DAIS_DRV_NAME);
MODULE_DEVICE_TABLE(of, rockchip_mdais_match);