huangcm
2025-04-26 bff3d3009b0d3a2be3ed92e4817fcabee9e76955
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
/*
 * sound\soc\sunxi\sunxi_snddaudio.c
 * (C) Copyright 2014-2016
 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
 * Wolfgang Huang <huangjinhui@allwinnertech.com>
 *
 * some simple description for this code
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 */
 
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <linux/io.h>
#include <linux/of.h>
#ifdef CONFIG_SND_SUNXI_MAD
#include "sunxi-mad.h"
static struct sunxi_mad_priv sunxi_daudio_priv;
#endif
 
static atomic_t daudio_count = ATOMIC_INIT(-1);
 
static int sunxi_snddaudio_hw_params(struct snd_pcm_substream *substream,
       struct snd_pcm_hw_params *params)
{
   struct snd_soc_pcm_runtime *rtd = substream->private_data;
   struct snd_soc_dai *codec_dai = rtd->codec_dai;
   struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
   struct snd_soc_card *card = rtd->card;
   unsigned int freq, clk_div;
   int ret;
 
   switch (params_rate(params)) {
   case 8000:
   case 16000:
   case 32000:
   case 64000:
   case 12000:
   case 24000:
   case 48000:
   case 96000:
   case 192000:
#ifdef CONFIG_AHUB_FREQ_REQ
   freq = 98304000;
#else
   freq = 24576000;
#endif
       break;
   case 11025:
   case 22050:
   case 44100:
   case 88200:
   case 176400:
#ifdef CONFIG_AHUB_FREQ_REQ
   freq = 90316800;
#else
       freq = 22579200;
#endif
       break;
   default:
       dev_err(card->dev, "unsupport params rate\n");
       return -EINVAL;
   }
 
   /* set platform clk source freq and set the mode as daudio or pcm */
   ret = snd_soc_dai_set_sysclk(cpu_dai, 0, freq, 0);
   if (ret < 0)
       return ret;
 
   /* set codec clk source freq and set the mode as daudio or pcm */
   ret = snd_soc_dai_set_sysclk(codec_dai, 0, freq, 0);
   if (ret < 0)
       dev_warn(card->dev, "codec_dai set sysclk failed\n");
 
   /* set codec dai fmt */
   ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
           SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
   if (ret < 0)
       dev_warn(card->dev, "codec dai set fmt failed\n");
 
   /* set system clk div */
   clk_div = freq / params_rate(params);
 
   ret = snd_soc_dai_set_clkdiv(cpu_dai, 0, clk_div);
   if (ret < 0)
       return ret;
 
   ret = snd_soc_dai_set_clkdiv(codec_dai, 0, clk_div);
   if (ret < 0)
       dev_warn(card->dev, "codec_dai set clkdiv failed\n");
   return 0;
}
 
/* sunxi card initialization */
static int sunxi_snddaudio_init(struct snd_soc_pcm_runtime *rtd)
{
   return 0;
}
 
#ifdef CONFIG_SND_SUNXI_MAD
/*enable the snddaudio suspend */
static int sunxi_snddaudio_startup(struct snd_pcm_substream *substream)
{
    struct snd_soc_pcm_runtime *rtd = substream->private_data;
 
   if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
       if (sunxi_daudio_priv.mad_bind == 1)
           rtd->dai_link->ignore_suspend = 1;
   }
 
    return 0;
}
 
/*disable the snddaudio suspend */
static void sunxi_snddaudio_shutdown(struct snd_pcm_substream *substream)
{
    struct snd_soc_pcm_runtime *rtd = substream->private_data;
   if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
       if (sunxi_daudio_priv.mad_bind == 1)
           rtd->dai_link->ignore_suspend = 0;
   }
}
 
/*mad_bind config*/
static int sunxi_daudio_set_mad_bind(struct snd_kcontrol *kcontrol,
   struct snd_ctl_elem_value *ucontrol)
{
   sunxi_daudio_priv.mad_bind = ucontrol->value.integer.value[0];
   return 0;
}
 
static int sunxi_daudio_get_mad_bind(struct snd_kcontrol *kcontrol,
   struct snd_ctl_elem_value *ucontrol)
{
   ucontrol->value.integer.value[0] = sunxi_daudio_priv.mad_bind;
   return 0;
}
 
static const char *mad_bind_function[] = {"mad_unbind", "mad_bind"};
 
static const struct soc_enum mad_bind_enum[] = {
   SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mad_bind_function),
           mad_bind_function),
};
 
/* daudio machine kcontrols */
static const struct snd_kcontrol_new sunxi_snddaudio_controls[] = {
   SOC_ENUM_EXT("daudio bind mad Function", mad_bind_enum[0],
       sunxi_daudio_get_mad_bind, sunxi_daudio_set_mad_bind),
};
#endif
 
static struct snd_soc_ops sunxi_snddaudio_ops = {
   .hw_params      = sunxi_snddaudio_hw_params,
#ifdef CONFIG_SND_SUNXI_MAD
   .startup = sunxi_snddaudio_startup,
   .shutdown = sunxi_snddaudio_shutdown,
#endif
};
 
static struct snd_soc_dai_link sunxi_snddaudio_dai_link = {
   .name           = "sysvoice",
   .stream_name    = "SUNXI-AUDIO",
   .cpu_dai_name   = "sunxi-daudio",
   .platform_name  = "sunxi-daudio",
    .codec_dai_name = "snd-soc-dummy-dai",
    .codec_name     = "snd-soc-dummy",
   .init           = sunxi_snddaudio_init,
   .ops            = &sunxi_snddaudio_ops,
};
 
static struct snd_soc_card snd_soc_sunxi_snddaudio = {
   .name           = "snddaudio",
   .owner          = THIS_MODULE,
   .dai_link       = &sunxi_snddaudio_dai_link,
   .num_links      = 1,
#ifdef CONFIG_SND_SUNXI_MAD
   .controls = sunxi_snddaudio_controls,
   .num_controls = ARRAY_SIZE(sunxi_snddaudio_controls),
#endif
};
 
#ifdef CONFIG_SND_SUNXI_SOC_AHUB
static int sunxi_snddaudio_dev_probe(struct platform_device *pdev)
{
   struct device_node *np = pdev->dev.of_node;
   struct snd_soc_dai_link    *dai_link;
   struct snd_soc_card *card;
   char name[30] = "snddaudio";
   unsigned int temp_val;
   int ret = 0;
 
   card = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_card),
           GFP_KERNEL);
   if (!card)
       return -ENOMEM;
 
   memcpy(card, &snd_soc_sunxi_snddaudio, sizeof(struct snd_soc_card));
 
   card->dev = &pdev->dev;
 
   dai_link = devm_kzalloc(&pdev->dev,
           sizeof(struct snd_soc_dai_link), GFP_KERNEL);
   if (!dai_link) {
       ret = -ENOMEM;
       goto err_kfree_card;
   }
 
   memcpy(dai_link, &sunxi_snddaudio_dai_link,
           sizeof(struct snd_soc_dai_link));
   card->dai_link = dai_link;
 
   dai_link->cpu_dai_name = NULL;
   dai_link->cpu_of_node = of_parse_phandle(np,
                   "sunxi,cpudai-controller", 0);
   if (!dai_link->cpu_of_node) {
       dev_err(&pdev->dev,
           "Property 'sunxi,cpudai-controller' invalid\n");
       ret = -EINVAL;
       goto err_kfree_link;
   }
   dai_link->platform_name = "snd-soc-dummy";
 
   ret = of_property_read_string(np, "sunxi,snddaudio-codec",
           &dai_link->codec_name);
   /*
    * As we setting codec & codec_dai in dtb, we just setting the
    * codec & codec_dai in the dai_link. But if we just not setting,
    * we then using the snd-soc-dummy as the codec & codec_dai to
    * construct the snd-card for audio playback & capture.
    */
   if (!ret) {
       ret = of_property_read_string(np, "sunxi,snddaudio-codec-dai",
               &dai_link->codec_dai_name);
       if (ret < 0) {
           dev_err(card->dev, "codec_dai name invaild in dtb\n");
           ret = -EINVAL;
           goto err_kfree_link;
       }
       sprintf(name+3, "%s", dai_link->codec_name);
   } else {
       if (dai_link->cpu_of_node && of_property_read_u32(
           dai_link->cpu_of_node, "tdm_num", &temp_val) >= 0)
           sprintf(name+9, "%u", temp_val);
       else
           sprintf(name+9, "%d", atomic_inc_return(&daudio_count));
   }
 
   card->name = name;
   dev_info(card->dev, "codec: %s, codec_dai: %s.\n",
           dai_link->codec_name,
           dai_link->codec_dai_name);
 
   ret = snd_soc_register_card(card);
   if (ret) {
       dev_err(card->dev, "snd_soc_register_card failed\n");
       goto err_kfree_link;
   }
 
   return ret;
err_kfree_link:
   devm_kfree(&pdev->dev, card->dai_link);
err_kfree_card:
   devm_kfree(&pdev->dev, card);
   return ret;
}
#else
static int sunxi_snddaudio_dev_probe(struct platform_device *pdev)
{
   struct device_node *np = pdev->dev.of_node;
   struct snd_soc_dai_link    *dai_link;
   struct snd_soc_card *card;
   char name[30] = "snddaudio";
   unsigned int temp_val;
   int ret = 0;
 
   card = devm_kzalloc(&pdev->dev, sizeof(struct snd_soc_card),
           GFP_KERNEL);
   if (!card)
       return -ENOMEM;
 
   memcpy(card, &snd_soc_sunxi_snddaudio, sizeof(struct snd_soc_card));
 
   card->dev = &pdev->dev;
 
   dai_link = devm_kzalloc(&pdev->dev,
           sizeof(struct snd_soc_dai_link), GFP_KERNEL);
   if (!dai_link) {
       ret = -ENOMEM;
       goto err_kfree_card;
   }
   memcpy(dai_link, &sunxi_snddaudio_dai_link,
           sizeof(struct snd_soc_dai_link));
   card->dai_link = dai_link;
 
   dai_link->cpu_dai_name = NULL;
   dai_link->cpu_of_node = of_parse_phandle(np,
           "sunxi,daudio0-controller", 0);
   if (dai_link->cpu_of_node)
       goto cpu_node_find;
 
   dai_link->cpu_of_node = of_parse_phandle(np,
           "sunxi,daudio1-controller", 0);
   if (dai_link->cpu_of_node)
       goto cpu_node_find;
 
   dai_link->cpu_of_node = of_parse_phandle(np,
           "sunxi,daudio-controller", 0);
   if (dai_link->cpu_of_node)
       goto cpu_node_find;
 
   dev_err(card->dev, "Perperty 'sunxi,daudio-controller' missing\n");
 
   goto err_kfree_link;
 
cpu_node_find:
   dai_link->platform_name = NULL;
   dai_link->platform_of_node =
               dai_link->cpu_of_node;
 
   ret = of_property_read_string(np, "sunxi,snddaudio-codec",
           &dai_link->codec_name);
   /*
    * As we setting codec & codec_dai in dtb, we just setting the
    * codec & codec_dai in the dai_link. But if we just not setting,
    * we then using the snd-soc-dummy as the codec & codec_dai to
    * construct the snd-card for audio playback & capture.
    */
   if (!ret) {
       ret = of_property_read_string(np, "sunxi,snddaudio-codec-dai",
               &dai_link->codec_dai_name);
       if (ret < 0) {
           dev_err(card->dev, "codec_dai name invaild in dtb\n");
           ret = -EINVAL;
           goto err_kfree_link;
       }
       sprintf(name+3, "%s", dai_link->codec_name);
   } else {
       if (dai_link->cpu_of_node && of_property_read_u32(
           dai_link->cpu_of_node, "tdm_num", &temp_val) >= 0)
           sprintf(name+9, "%u", temp_val);
       else
           sprintf(name+9, "%d", atomic_inc_return(&daudio_count));
   }
 
   card->name = name;
   dev_info(card->dev, "codec: %s, codec_dai: %s.\n",
           dai_link->codec_name,
           dai_link->codec_dai_name);
 
#ifdef CONFIG_SND_SUNXI_MAD
    sunxi_daudio_priv.mad_bind = 0;
   snd_soc_card_set_drvdata(card, &sunxi_daudio_priv);
#endif
 
   ret = snd_soc_register_card(card);
   if (ret) {
       dev_err(card->dev, "snd_soc_register_card failed\n");
       goto err_kfree_link;
   }
 
   return ret;
 
err_kfree_link:
   devm_kfree(&pdev->dev, card->dai_link);
err_kfree_card:
   devm_kfree(&pdev->dev, card);
   return ret;
}
#endif
static int sunxi_snddaudio_dev_remove(struct platform_device *pdev)
{
   struct snd_soc_card *card = platform_get_drvdata(pdev);
 
   devm_kfree(&pdev->dev, card->dai_link);
   snd_soc_unregister_card(card);
   devm_kfree(&pdev->dev, card);
   return 0;
}
 
static const struct of_device_id sunxi_snddaudio_of_match[] = {
   { .compatible = "allwinner,sunxi-daudio0-machine", },
   { .compatible = "allwinner,sunxi-daudio1-machine", },
   { .compatible = "allwinner,sunxi-daudio2-machine", },
   { .compatible = "allwinner,sunxi-daudio3-machine", },
   {},
};
 
static struct platform_driver sunxi_snddaudio_driver = {
   .driver = {
       .name   = "snddaudio",
       .owner  = THIS_MODULE,
       .pm     = &snd_soc_pm_ops,
       .of_match_table = sunxi_snddaudio_of_match,
   },
   .probe  = sunxi_snddaudio_dev_probe,
   .remove = sunxi_snddaudio_dev_remove,
};
 
static int __init sunxi_snddaudio_driver_init(void)
{
   return platform_driver_register(&sunxi_snddaudio_driver);
}
 
static void __exit sunxi_snddaudio_driver_exit(void)
{
   platform_driver_unregister(&sunxi_snddaudio_driver);
}
 
late_initcall(sunxi_snddaudio_driver_init);
module_exit(sunxi_snddaudio_driver_exit);
 
MODULE_AUTHOR("Wolfgang Huang");
MODULE_DESCRIPTION("SUNXI snddaudio ALSA SoC Audio Card Driver");
MODULE_LICENSE("GPL");