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
/*
 * sound\soc\sunxi\sunx8iw11_sndcodec.c
 * (C) Copyright 2014-2017
 * allwinner Technology Co., Ltd. <www.allwinnertech.com>
 * huangxin <huangxin@allwinnertech.com>
 * wolfgang huang <huangjinhui@allwinnetech.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 <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/io.h>
#include <linux/input.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/jack.h>
#include <linux/of.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <linux/delay.h>
 
#include "sunxi_rw_func.h"
 
struct sunxi_card_priv {
   int jack_gpio;
   int jack_invert;    /* 0->high is plug_in, 1->high is plug_out */
   struct snd_soc_jack jack_detect;
};
 
static const struct snd_kcontrol_new sunxi_card_controls[] = {
   SOC_DAPM_PIN_SWITCH("Phoneout Speaker"),
};
 
static const struct snd_soc_dapm_widget sunxi_card_dapm_widgets[] = {
   SND_SOC_DAPM_MIC("Main Mic", NULL),
   SND_SOC_DAPM_SPK("Phoneout Speaker", NULL),
   SND_SOC_DAPM_LINE("LINEIN", NULL),
};
 
static const struct snd_soc_dapm_route sunxi_card_routes[] = {
   {"MainMic Bias", NULL, "Main Mic"},
 
   {"Phoneout Speaker", NULL, "PHONEOUTP"},
   {"Phoneout Speaker", NULL, "PHONEOUTN"},
 
 
   {"LINEINL", NULL, "LINEIN"},
   {"LINEINR", NULL, "LINEIN"},
};
 
/*
 * Card initialization
 */
static int sunxi_card_init(struct snd_soc_pcm_runtime *rtd)
{
   struct snd_soc_codec *codec = rtd->codec;
 
   snd_soc_dapm_disable_pin(&codec->component.dapm, "Phoneout Speaker");
   snd_soc_dapm_enable_pin(&codec->component.dapm, "LINEIN");
   snd_soc_dapm_enable_pin(&codec->component.dapm, "LINEOUT");
 
   snd_soc_dapm_sync(&codec->component.dapm);
   return 0;
}
 
static int sunxi_card_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_card *card = rtd->card;
   unsigned int freq;
   int ret;
 
   switch (params_rate(params)) {
   case    8000:
   case    12000:
   case    16000:
   case    24000:
   case    32000:
   case    48000:
   case    96000:
   case    192000:
       freq = 24576000;
       break;
   case    11025:
   case    22050:
   case    44100:
       freq = 22579200;
       break;
   default:
       dev_err(card->dev, "invalid rate setting\n");
       return -EINVAL;
   }
 
   ret = snd_soc_dai_set_sysclk(codec_dai, 0, freq, 0);
   if (ret < 0) {
       dev_err(card->dev, "set codec dai sysclk faided.\n");
       return ret;
   }
 
   return 0;
}
 
static struct snd_soc_ops sunxi_card_ops = {
   .hw_params    = sunxi_card_hw_params,
};
 
static int sunxi_card_suspend(struct snd_soc_card *card)
{
   return 0;
}
 
static int sunxi_card_resume(struct snd_soc_card *card)
{
   return 0;
}
 
static struct snd_soc_dai_link sunxi_card_dai_link[] = {
   {
       .name        = "audiocodec",
       .stream_name    = "SUNXI-CODEC",
       .cpu_dai_name    = "sunxi-internal-cpudai",
       .codec_dai_name = "sun8iw17codec",
       .platform_name    = "sunxi-internal-cpudai",
       .codec_name    = "sunxi-internal-codec",
       .init        = sunxi_card_init,
       .ops        = &sunxi_card_ops,
   },
};
 
static struct snd_soc_card snd_soc_sunxi_card = {
   .name        = "audiocodec",
   .owner        = THIS_MODULE,
   .dai_link    = sunxi_card_dai_link,
   .num_links    = ARRAY_SIZE(sunxi_card_dai_link),
   .controls    = sunxi_card_controls,
   .num_controls    = ARRAY_SIZE(sunxi_card_controls),
   .dapm_widgets    = sunxi_card_dapm_widgets,
   .num_dapm_widgets = ARRAY_SIZE(sunxi_card_dapm_widgets),
   .dapm_routes = sunxi_card_routes,
   .num_dapm_routes = ARRAY_SIZE(sunxi_card_routes),
   .suspend_post    = sunxi_card_suspend,
   .resume_post    = sunxi_card_resume,
};
 
static int sunxi_card_dev_probe(struct platform_device *pdev)
{
   int ret = 0;
   struct device_node *np = pdev->dev.of_node;
   struct snd_soc_card *card = &snd_soc_sunxi_card;
   struct sunxi_card_priv *priv;
 
   /* register the soc card */
   card->dev = &pdev->dev;
 
   priv = devm_kzalloc(&pdev->dev,
       sizeof(struct sunxi_card_priv), GFP_KERNEL);
   if (!priv)
       return -ENOMEM;
 
   sunxi_card_dai_link[0].cpu_dai_name = NULL;
   sunxi_card_dai_link[0].cpu_of_node = of_parse_phandle(np,
                   "sunxi,cpudai-controller", 0);
   if (!sunxi_card_dai_link[0].cpu_of_node) {
       dev_err(&pdev->dev, "Property 'sunxi,cpudai-controller' missing or invalid\n");
       ret = -EINVAL;
       goto err_devm_kfree;
   } else {
       sunxi_card_dai_link[0].platform_name = NULL;
       sunxi_card_dai_link[0].platform_of_node =
               sunxi_card_dai_link[0].cpu_of_node;
   }
   sunxi_card_dai_link[0].codec_name = NULL;
   sunxi_card_dai_link[0].codec_of_node = of_parse_phandle(np,
                       "sunxi,audio-codec", 0);
   if (!sunxi_card_dai_link[0].codec_of_node) {
       dev_err(&pdev->dev, "Property 'sunxi,audio-codec' missing or invalid\n");
       ret = -EINVAL;
       goto err_devm_kfree;
   }
 
   snd_soc_card_set_drvdata(card, priv);
   ret = snd_soc_register_card(card);
   if (ret) {
       dev_err(&pdev->dev, "snd_soc_register_card failed %d\n", ret);
       goto err_devm_kfree;
   }
 
   return 0;
 
err_devm_kfree:
   devm_kfree(&pdev->dev, priv);
   return ret;
}
 
static int __exit sunxi_card_dev_remove(struct platform_device *pdev)
{
   struct sunxi_card_priv *priv = dev_get_drvdata(&pdev->dev);
 
   devm_kfree(&pdev->dev, priv);
   return 0;
}
 
static const struct of_device_id sunxi_card_of_match[] = {
   { .compatible = "allwinner,sunxi-codec-machine", },
   {},
};
 
static struct platform_driver sunxi_machine_driver = {
   .driver = {
       .name = "sunxi-codec-machine",
       .owner = THIS_MODULE,
       .pm = &snd_soc_pm_ops,
       .of_match_table = sunxi_card_of_match,
   },
   .probe = sunxi_card_dev_probe,
   .remove = __exit_p(sunxi_card_dev_remove),
};
 
module_platform_driver(sunxi_machine_driver);
 
MODULE_AUTHOR("wolfgang huang <huangjinhui@allwinnertech.com>");
MODULE_DESCRIPTION("SUNXI Codec Machine ASoC driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sunxi-codec-machine");