hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/sound/soc/rockchip/rockchip_max98090.c
....@@ -1,23 +1,12 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Rockchip machine ASoC driver for boards using a MAX90809 CODEC.
34 *
45 * Copyright (c) 2014, ROCKCHIP CORPORATION. All rights reserved.
5
- *
6
- * This program is free software; you can redistribute it and/or modify it
7
- * under the terms and conditions of the GNU General Public License,
8
- * version 2, as published by the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope it will be useful, but WITHOUT
11
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13
- * more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
- *
186 */
197
208 #include <linux/module.h>
9
+#include <linux/of_device.h>
2110 #include <linux/platform_device.h>
2211 #include <linux/slab.h>
2312 #include <linux/gpio.h>
....@@ -48,38 +37,116 @@
4837
4938 };
5039
51
-static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
52
- SND_SOC_DAPM_HP("Headphone", NULL),
53
- SND_SOC_DAPM_MIC("Headset Mic", NULL),
54
- SND_SOC_DAPM_MIC("Int Mic", NULL),
55
- SND_SOC_DAPM_SPK("Speaker", NULL),
40
+#define RK_MAX98090_WIDGETS \
41
+ SND_SOC_DAPM_HP("Headphone", NULL), \
42
+ SND_SOC_DAPM_MIC("Headset Mic", NULL), \
43
+ SND_SOC_DAPM_MIC("Int Mic", NULL), \
44
+ SND_SOC_DAPM_SPK("Speaker", NULL)
45
+
46
+#define RK_HDMI_WIDGETS \
47
+ SND_SOC_DAPM_LINE("HDMI", NULL)
48
+
49
+static const struct snd_soc_dapm_widget rk_max98090_dapm_widgets[] = {
50
+ RK_MAX98090_WIDGETS,
5651 };
5752
58
-static const struct snd_soc_dapm_route rk_audio_map[] = {
59
- {"IN34", NULL, "Headset Mic"},
60
- {"IN34", NULL, "MICBIAS"},
61
- {"Headset Mic", NULL, "MICBIAS"},
62
- {"DMICL", NULL, "Int Mic"},
63
- {"Headphone", NULL, "HPL"},
64
- {"Headphone", NULL, "HPR"},
65
- {"Speaker", NULL, "SPKL"},
66
- {"Speaker", NULL, "SPKR"},
53
+static const struct snd_soc_dapm_widget rk_hdmi_dapm_widgets[] = {
54
+ RK_HDMI_WIDGETS,
6755 };
6856
69
-static const struct snd_kcontrol_new rk_mc_controls[] = {
70
- SOC_DAPM_PIN_SWITCH("Headphone"),
71
- SOC_DAPM_PIN_SWITCH("Headset Mic"),
72
- SOC_DAPM_PIN_SWITCH("Int Mic"),
73
- SOC_DAPM_PIN_SWITCH("Speaker"),
57
+static const struct snd_soc_dapm_widget rk_max98090_hdmi_dapm_widgets[] = {
58
+ RK_MAX98090_WIDGETS,
59
+ RK_HDMI_WIDGETS,
7460 };
61
+
62
+#define RK_MAX98090_AUDIO_MAP \
63
+ {"IN34", NULL, "Headset Mic"}, \
64
+ {"Headset Mic", NULL, "MICBIAS"}, \
65
+ {"DMICL", NULL, "Int Mic"}, \
66
+ {"Headphone", NULL, "HPL"}, \
67
+ {"Headphone", NULL, "HPR"}, \
68
+ {"Speaker", NULL, "SPKL"}, \
69
+ {"Speaker", NULL, "SPKR"}
70
+
71
+#define RK_HDMI_AUDIO_MAP \
72
+ {"HDMI", NULL, "TX"}
73
+
74
+static const struct snd_soc_dapm_route rk_max98090_audio_map[] = {
75
+ RK_MAX98090_AUDIO_MAP,
76
+};
77
+
78
+static const struct snd_soc_dapm_route rk_hdmi_audio_map[] = {
79
+ RK_HDMI_AUDIO_MAP,
80
+};
81
+
82
+static const struct snd_soc_dapm_route rk_max98090_hdmi_audio_map[] = {
83
+ RK_MAX98090_AUDIO_MAP,
84
+ RK_HDMI_AUDIO_MAP,
85
+};
86
+
87
+#define RK_MAX98090_CONTROLS \
88
+ SOC_DAPM_PIN_SWITCH("Headphone"), \
89
+ SOC_DAPM_PIN_SWITCH("Headset Mic"), \
90
+ SOC_DAPM_PIN_SWITCH("Int Mic"), \
91
+ SOC_DAPM_PIN_SWITCH("Speaker")
92
+
93
+#define RK_HDMI_CONTROLS \
94
+ SOC_DAPM_PIN_SWITCH("HDMI")
95
+
96
+static const struct snd_kcontrol_new rk_max98090_controls[] = {
97
+ RK_MAX98090_CONTROLS,
98
+};
99
+
100
+static const struct snd_kcontrol_new rk_hdmi_controls[] = {
101
+ RK_HDMI_CONTROLS,
102
+};
103
+
104
+static const struct snd_kcontrol_new rk_max98090_hdmi_controls[] = {
105
+ RK_MAX98090_CONTROLS,
106
+ RK_HDMI_CONTROLS,
107
+};
108
+
109
+static int rk_jack_event(struct notifier_block *nb, unsigned long event,
110
+ void *data)
111
+{
112
+ struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
113
+ struct snd_soc_dapm_context *dapm = &jack->card->dapm;
114
+
115
+ if (event & SND_JACK_MICROPHONE) {
116
+ snd_soc_dapm_force_enable_pin(dapm, "MICBIAS");
117
+ snd_soc_dapm_force_enable_pin(dapm, "SHDN");
118
+ } else {
119
+ snd_soc_dapm_disable_pin(dapm, "MICBIAS");
120
+ snd_soc_dapm_disable_pin(dapm, "SHDN");
121
+ }
122
+
123
+ snd_soc_dapm_sync(dapm);
124
+
125
+ return 0;
126
+}
127
+
128
+static struct notifier_block rk_jack_nb = {
129
+ .notifier_call = rk_jack_event,
130
+};
131
+
132
+static int rk_init(struct snd_soc_pcm_runtime *runtime)
133
+{
134
+ /*
135
+ * The jack has already been created in the rk_98090_headset_init()
136
+ * function.
137
+ */
138
+ snd_soc_jack_notifier_register(&headset_jack, &rk_jack_nb);
139
+
140
+ return 0;
141
+}
75142
76143 static int rk_aif1_hw_params(struct snd_pcm_substream *substream,
77144 struct snd_pcm_hw_params *params)
78145 {
79146 int ret = 0;
80
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
81
- struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
82
- struct snd_soc_dai *codec_dai = rtd->codec_dai;
147
+ struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
148
+ struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
149
+ struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
83150 int mclk;
84151
85152 switch (params_rate(params)) {
....@@ -104,55 +171,173 @@
104171
105172 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, mclk,
106173 SND_SOC_CLOCK_OUT);
107
- if (ret < 0) {
108
- dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
174
+ if (ret) {
175
+ dev_err(cpu_dai->dev, "Can't set cpu dai clock %d\n", ret);
109176 return ret;
110177 }
111178
112179 ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk,
113180 SND_SOC_CLOCK_IN);
114
- if (ret < 0) {
115
- dev_err(codec_dai->dev, "Can't set codec clock %d\n", ret);
181
+
182
+ /* HDMI codec dai does not need to set sysclk. */
183
+ if (!strcmp(rtd->dai_link->name, "HDMI"))
184
+ return 0;
185
+
186
+ if (ret) {
187
+ dev_err(codec_dai->dev, "Can't set codec dai clock %d\n", ret);
116188 return ret;
117189 }
118190
119191 return ret;
120192 }
121193
194
+static int rk_aif1_startup(struct snd_pcm_substream *substream)
195
+{
196
+ /*
197
+ * Set period size to 240 because pl330 has issue
198
+ * dealing with larger period in stress testing.
199
+ */
200
+ return snd_pcm_hw_constraint_minmax(substream->runtime,
201
+ SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 240, 240);
202
+}
203
+
122204 static const struct snd_soc_ops rk_aif1_ops = {
123205 .hw_params = rk_aif1_hw_params,
206
+ .startup = rk_aif1_startup,
124207 };
125208
126
-static struct snd_soc_dai_link rk_dailink = {
127
- .name = "max98090",
128
- .stream_name = "Audio",
129
- .codec_dai_name = "HiFi",
130
- .ops = &rk_aif1_ops,
131
- /* set max98090 as slave */
132
- .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
133
- SND_SOC_DAIFMT_CBS_CFS,
209
+SND_SOC_DAILINK_DEFS(analog,
210
+ DAILINK_COMP_ARRAY(COMP_EMPTY()),
211
+ DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "HiFi")),
212
+ DAILINK_COMP_ARRAY(COMP_EMPTY()));
213
+
214
+SND_SOC_DAILINK_DEFS(hdmi,
215
+ DAILINK_COMP_ARRAY(COMP_EMPTY()),
216
+ DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "i2s-hifi")),
217
+ DAILINK_COMP_ARRAY(COMP_EMPTY()));
218
+
219
+enum {
220
+ DAILINK_MAX98090,
221
+ DAILINK_HDMI,
222
+};
223
+
224
+static struct snd_soc_jack rk_hdmi_jack;
225
+
226
+static int rk_hdmi_init(struct snd_soc_pcm_runtime *runtime)
227
+{
228
+ struct snd_soc_card *card = runtime->card;
229
+ struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
230
+ int ret;
231
+
232
+ /* enable jack detection */
233
+ ret = snd_soc_card_jack_new(card, "HDMI Jack", SND_JACK_LINEOUT,
234
+ &rk_hdmi_jack, NULL, 0);
235
+ if (ret) {
236
+ dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
237
+ return ret;
238
+ }
239
+
240
+ return snd_soc_component_set_jack(component, &rk_hdmi_jack, NULL);
241
+}
242
+
243
+/* max98090 dai_link */
244
+static struct snd_soc_dai_link rk_max98090_dailinks[] = {
245
+ {
246
+ .name = "max98090",
247
+ .stream_name = "Analog",
248
+ .init = rk_init,
249
+ .ops = &rk_aif1_ops,
250
+ /* set max98090 as slave */
251
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
252
+ SND_SOC_DAIFMT_CBS_CFS,
253
+ SND_SOC_DAILINK_REG(analog),
254
+ },
255
+};
256
+
257
+/* HDMI codec dai_link */
258
+static struct snd_soc_dai_link rk_hdmi_dailinks[] = {
259
+ {
260
+ .name = "HDMI",
261
+ .stream_name = "HDMI",
262
+ .init = rk_hdmi_init,
263
+ .ops = &rk_aif1_ops,
264
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
265
+ SND_SOC_DAIFMT_CBS_CFS,
266
+ SND_SOC_DAILINK_REG(hdmi),
267
+ }
268
+};
269
+
270
+/* max98090 and HDMI codec dai_link */
271
+static struct snd_soc_dai_link rk_max98090_hdmi_dailinks[] = {
272
+ [DAILINK_MAX98090] = {
273
+ .name = "max98090",
274
+ .stream_name = "Analog",
275
+ .init = rk_init,
276
+ .ops = &rk_aif1_ops,
277
+ /* set max98090 as slave */
278
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
279
+ SND_SOC_DAIFMT_CBS_CFS,
280
+ SND_SOC_DAILINK_REG(analog),
281
+ },
282
+ [DAILINK_HDMI] = {
283
+ .name = "HDMI",
284
+ .stream_name = "HDMI",
285
+ .init = rk_hdmi_init,
286
+ .ops = &rk_aif1_ops,
287
+ .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
288
+ SND_SOC_DAIFMT_CBS_CFS,
289
+ SND_SOC_DAILINK_REG(hdmi),
290
+ }
134291 };
135292
136293 static int rk_98090_headset_init(struct snd_soc_component *component);
137294
138295 static struct snd_soc_aux_dev rk_98090_headset_dev = {
139
- .name = "Headset Chip",
296
+ .dlc = COMP_EMPTY(),
140297 .init = rk_98090_headset_init,
141298 };
142299
143
-static struct snd_soc_card snd_soc_card_rk = {
300
+static struct snd_soc_card rockchip_max98090_card = {
144301 .name = "ROCKCHIP-I2S",
145302 .owner = THIS_MODULE,
146
- .dai_link = &rk_dailink,
147
- .num_links = 1,
303
+ .dai_link = rk_max98090_dailinks,
304
+ .num_links = ARRAY_SIZE(rk_max98090_dailinks),
148305 .aux_dev = &rk_98090_headset_dev,
149306 .num_aux_devs = 1,
150
- .dapm_widgets = rk_dapm_widgets,
151
- .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
152
- .dapm_routes = rk_audio_map,
153
- .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
154
- .controls = rk_mc_controls,
155
- .num_controls = ARRAY_SIZE(rk_mc_controls),
307
+ .dapm_widgets = rk_max98090_dapm_widgets,
308
+ .num_dapm_widgets = ARRAY_SIZE(rk_max98090_dapm_widgets),
309
+ .dapm_routes = rk_max98090_audio_map,
310
+ .num_dapm_routes = ARRAY_SIZE(rk_max98090_audio_map),
311
+ .controls = rk_max98090_controls,
312
+ .num_controls = ARRAY_SIZE(rk_max98090_controls),
313
+};
314
+
315
+static struct snd_soc_card rockchip_hdmi_card = {
316
+ .name = "ROCKCHIP-HDMI",
317
+ .owner = THIS_MODULE,
318
+ .dai_link = rk_hdmi_dailinks,
319
+ .num_links = ARRAY_SIZE(rk_hdmi_dailinks),
320
+ .dapm_widgets = rk_hdmi_dapm_widgets,
321
+ .num_dapm_widgets = ARRAY_SIZE(rk_hdmi_dapm_widgets),
322
+ .dapm_routes = rk_hdmi_audio_map,
323
+ .num_dapm_routes = ARRAY_SIZE(rk_hdmi_audio_map),
324
+ .controls = rk_hdmi_controls,
325
+ .num_controls = ARRAY_SIZE(rk_hdmi_controls),
326
+};
327
+
328
+static struct snd_soc_card rockchip_max98090_hdmi_card = {
329
+ .name = "ROCKCHIP-MAX98090-HDMI",
330
+ .owner = THIS_MODULE,
331
+ .dai_link = rk_max98090_hdmi_dailinks,
332
+ .num_links = ARRAY_SIZE(rk_max98090_hdmi_dailinks),
333
+ .aux_dev = &rk_98090_headset_dev,
334
+ .num_aux_devs = 1,
335
+ .dapm_widgets = rk_max98090_hdmi_dapm_widgets,
336
+ .num_dapm_widgets = ARRAY_SIZE(rk_max98090_hdmi_dapm_widgets),
337
+ .dapm_routes = rk_max98090_hdmi_audio_map,
338
+ .num_dapm_routes = ARRAY_SIZE(rk_max98090_hdmi_audio_map),
339
+ .controls = rk_max98090_hdmi_controls,
340
+ .num_controls = ARRAY_SIZE(rk_max98090_hdmi_controls),
156341 };
157342
158343 static int rk_98090_headset_init(struct snd_soc_component *component)
....@@ -160,7 +345,7 @@
160345 int ret;
161346
162347 /* Enable Headset and 4 Buttons Jack detection */
163
- ret = snd_soc_card_jack_new(&snd_soc_card_rk, "Headset Jack",
348
+ ret = snd_soc_card_jack_new(component->card, "Headset Jack",
164349 SND_JACK_HEADSET |
165350 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
166351 SND_JACK_BTN_2 | SND_JACK_BTN_3,
....@@ -175,41 +360,75 @@
175360 return ret;
176361 }
177362
178
-static int snd_rk_mc_probe(struct platform_device *pdev)
363
+static int rk_parse_headset_from_of(struct device *dev, struct device_node *np)
179364 {
180
- int ret = 0;
181
- struct snd_soc_card *card = &snd_soc_card_rk;
182
- struct device_node *np = pdev->dev.of_node;
183
-
184
- /* register the soc card */
185
- card->dev = &pdev->dev;
186
-
187
- rk_dailink.codec_of_node = of_parse_phandle(np,
188
- "rockchip,audio-codec", 0);
189
- if (!rk_dailink.codec_of_node) {
190
- dev_err(&pdev->dev,
191
- "Property 'rockchip,audio-codec' missing or invalid\n");
192
- return -EINVAL;
193
- }
194
-
195
- rk_dailink.cpu_of_node = of_parse_phandle(np,
196
- "rockchip,i2s-controller", 0);
197
- if (!rk_dailink.cpu_of_node) {
198
- dev_err(&pdev->dev,
199
- "Property 'rockchip,i2s-controller' missing or invalid\n");
200
- return -EINVAL;
201
- }
202
-
203
- rk_dailink.platform_of_node = rk_dailink.cpu_of_node;
204
-
205
- rk_98090_headset_dev.codec_of_node = of_parse_phandle(np,
206
- "rockchip,headset-codec", 0);
207
- if (!rk_98090_headset_dev.codec_of_node) {
208
- dev_err(&pdev->dev,
365
+ rk_98090_headset_dev.dlc.of_node = of_parse_phandle(
366
+ np, "rockchip,headset-codec", 0);
367
+ if (!rk_98090_headset_dev.dlc.of_node) {
368
+ dev_err(dev,
209369 "Property 'rockchip,headset-codec' missing/invalid\n");
210370 return -EINVAL;
211371 }
372
+ return 0;
373
+}
212374
375
+static int snd_rk_mc_probe(struct platform_device *pdev)
376
+{
377
+ int ret = 0;
378
+ struct snd_soc_card *card;
379
+ struct device *dev = &pdev->dev;
380
+ struct device_node *np = pdev->dev.of_node;
381
+ struct device_node *np_cpu;
382
+ struct device_node *np_audio, *np_hdmi;
383
+
384
+ /* Parse DTS for I2S controller. */
385
+ np_cpu = of_parse_phandle(np, "rockchip,i2s-controller", 0);
386
+
387
+ if (!np_cpu) {
388
+ dev_err(&pdev->dev,
389
+ "Property 'rockchip,i2s-controller missing or invalid\n");
390
+ return -EINVAL;
391
+ }
392
+
393
+ /*
394
+ * Find the card to use based on the presences of audio codec
395
+ * and hdmi codec in device property. Set their of_node accordingly.
396
+ */
397
+ np_audio = of_parse_phandle(np, "rockchip,audio-codec", 0);
398
+ np_hdmi = of_parse_phandle(np, "rockchip,hdmi-codec", 0);
399
+ if (np_audio && np_hdmi) {
400
+ card = &rockchip_max98090_hdmi_card;
401
+ card->dai_link[DAILINK_MAX98090].codecs->of_node = np_audio;
402
+ card->dai_link[DAILINK_HDMI].codecs->of_node = np_hdmi;
403
+ card->dai_link[DAILINK_MAX98090].cpus->of_node = np_cpu;
404
+ card->dai_link[DAILINK_MAX98090].platforms->of_node = np_cpu;
405
+ card->dai_link[DAILINK_HDMI].cpus->of_node = np_cpu;
406
+ card->dai_link[DAILINK_HDMI].platforms->of_node = np_cpu;
407
+ } else if (np_audio) {
408
+ card = &rockchip_max98090_card;
409
+ card->dai_link[0].codecs->of_node = np_audio;
410
+ card->dai_link[0].cpus->of_node = np_cpu;
411
+ card->dai_link[0].platforms->of_node = np_cpu;
412
+ } else if (np_hdmi) {
413
+ card = &rockchip_hdmi_card;
414
+ card->dai_link[0].codecs->of_node = np_hdmi;
415
+ card->dai_link[0].cpus->of_node = np_cpu;
416
+ card->dai_link[0].platforms->of_node = np_cpu;
417
+ } else {
418
+ dev_err(dev, "At least one of codecs should be specified\n");
419
+ return -EINVAL;
420
+ }
421
+
422
+ card->dev = dev;
423
+
424
+ /* Parse headset detection codec. */
425
+ if (np_audio) {
426
+ ret = rk_parse_headset_from_of(dev, np);
427
+ if (ret)
428
+ return ret;
429
+ }
430
+
431
+ /* Parse card name. */
213432 ret = snd_soc_of_parse_card_name(card, "rockchip,model");
214433 if (ret) {
215434 dev_err(&pdev->dev,
....@@ -217,6 +436,7 @@
217436 return ret;
218437 }
219438
439
+ /* register the soc card */
220440 ret = devm_snd_soc_register_card(&pdev->dev, card);
221441 if (ret) {
222442 dev_err(&pdev->dev,