hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/sound/soc/codecs/rk3328_codec.c
....@@ -1,33 +1,21 @@
1
-/*
2
- * rk3328_codec.c -- rk3328 ALSA Soc Audio driver
3
- *
4
- * Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd 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
- *
18
- */
1
+// SPDX-License-Identifier: GPL-2.0
2
+//
3
+// rk3328 ALSA SoC Audio driver
4
+//
5
+// Copyright (c) 2017, Fuzhou Rockchip Electronics Co., Ltd All rights reserved.
196
20
-#include <linux/module.h>
21
-#include <linux/device.h>
22
-#include <linux/delay.h>
23
-#include <linux/of.h>
247 #include <linux/clk.h>
25
-#include <linux/mfd/syscon.h>
8
+#include <linux/delay.h>
9
+#include <linux/device.h>
10
+#include <linux/gpio/consumer.h>
11
+#include <linux/module.h>
12
+#include <linux/of.h>
2613 #include <linux/platform_device.h>
2714 #include <linux/pm_runtime.h>
2815 #include <linux/regmap.h>
29
-#include <sound/pcm_params.h>
16
+#include <linux/mfd/syscon.h>
3017 #include <sound/dmaengine_pcm.h>
18
+#include <sound/pcm_params.h>
3119 #include "rk3328_codec.h"
3220
3321 /*
....@@ -44,7 +32,7 @@
4432
4533 struct rk3328_codec_priv {
4634 struct regmap *regmap;
47
- struct regmap *grf;
35
+ struct gpio_desc *mute;
4836 struct clk *mclk;
4937 struct clk *pclk;
5038 unsigned int sclk;
....@@ -66,29 +54,27 @@
6654 { HPOUT_POP_CTRL, 0x11 },
6755 };
6856
69
-static int rk3328_codec_reset(struct snd_soc_component *component)
57
+static int rk3328_codec_reset(struct rk3328_codec_priv *rk3328)
7058 {
71
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(component);
72
-
73
- regmap_write(rk3328->regmap, CODEC_RESET, 0);
59
+ regmap_write(rk3328->regmap, CODEC_RESET, 0x00);
7460 mdelay(10);
7561 regmap_write(rk3328->regmap, CODEC_RESET, 0x03);
7662
7763 return 0;
7864 }
7965
80
-static int rk3328_set_dai_fmt(struct snd_soc_dai *dai,
81
- unsigned int fmt)
66
+static int rk3328_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
8267 {
83
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(dai->component);
84
- unsigned int val = 0;
68
+ struct rk3328_codec_priv *rk3328 =
69
+ snd_soc_component_get_drvdata(dai->component);
70
+ unsigned int val;
8571
8672 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
8773 case SND_SOC_DAIFMT_CBS_CFS:
88
- val |= PIN_DIRECTION_IN | DAC_I2S_MODE_SLAVE;
74
+ val = PIN_DIRECTION_IN | DAC_I2S_MODE_SLAVE;
8975 break;
9076 case SND_SOC_DAIFMT_CBM_CFM:
91
- val |= PIN_DIRECTION_OUT | DAC_I2S_MODE_MASTER;
77
+ val = PIN_DIRECTION_OUT | DAC_I2S_MODE_MASTER;
9278 break;
9379 default:
9480 return -EINVAL;
....@@ -97,20 +83,19 @@
9783 regmap_update_bits(rk3328->regmap, DAC_INIT_CTRL1,
9884 PIN_DIRECTION_MASK | DAC_I2S_MODE_MASK, val);
9985
100
- val = 0;
10186 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
10287 case SND_SOC_DAIFMT_DSP_A:
10388 case SND_SOC_DAIFMT_DSP_B:
104
- val |= DAC_MODE_PCM;
89
+ val = DAC_MODE_PCM;
10590 break;
10691 case SND_SOC_DAIFMT_I2S:
107
- val |= DAC_MODE_I2S;
92
+ val = DAC_MODE_I2S;
10893 break;
10994 case SND_SOC_DAIFMT_RIGHT_J:
110
- val |= DAC_MODE_RJM;
95
+ val = DAC_MODE_RJM;
11196 break;
11297 case SND_SOC_DAIFMT_LEFT_J:
113
- val |= DAC_MODE_LJM;
98
+ val = DAC_MODE_LJM;
11499 break;
115100 default:
116101 return -EINVAL;
....@@ -118,19 +103,15 @@
118103
119104 regmap_update_bits(rk3328->regmap, DAC_INIT_CTRL2,
120105 DAC_MODE_MASK, val);
106
+
121107 return 0;
122108 }
123109
124
-static void rk3328_analog_output(struct rk3328_codec_priv *rk3328, int mute)
110
+static int rk3328_mute_stream(struct snd_soc_dai *dai, int mute, int direction)
125111 {
126
- regmap_write(rk3328->grf, RK3328_GRF_SOC_CON10,
127
- (BIT(1) << 16) | (mute << 1));
128
-}
129
-
130
-static int rk3328_digital_mute(struct snd_soc_dai *dai, int mute)
131
-{
132
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(dai->component);
133
- unsigned int val = 0;
112
+ struct rk3328_codec_priv *rk3328 =
113
+ snd_soc_component_get_drvdata(dai->component);
114
+ unsigned int val;
134115
135116 if (mute)
136117 val = HPOUTL_MUTE | HPOUTR_MUTE;
....@@ -139,46 +120,41 @@
139120
140121 regmap_update_bits(rk3328->regmap, HPOUT_CTRL,
141122 HPOUTL_MUTE_MASK | HPOUTR_MUTE_MASK, val);
123
+
142124 return 0;
143125 }
144126
145
-static int rk3328_codec_power_on(struct snd_soc_component *component, int wait_ms)
127
+static int rk3328_codec_power_on(struct rk3328_codec_priv *rk3328, int wait_ms)
146128 {
147
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(component);
148
-
149129 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
150130 DAC_CHARGE_XCHARGE_MASK, DAC_CHARGE_PRECHARGE);
151131 mdelay(10);
152132 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
153133 DAC_CHARGE_CURRENT_ALL_MASK,
154134 DAC_CHARGE_CURRENT_ALL_ON);
155
-
156135 mdelay(wait_ms);
157136
158137 return 0;
159138 }
160139
161
-static int rk3328_codec_power_off(struct snd_soc_component *component, int wait_ms)
140
+static int rk3328_codec_power_off(struct rk3328_codec_priv *rk3328, int wait_ms)
162141 {
163
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(component);
164
-
165142 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
166143 DAC_CHARGE_XCHARGE_MASK, DAC_CHARGE_DISCHARGE);
167144 mdelay(10);
168145 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
169146 DAC_CHARGE_CURRENT_ALL_MASK,
170147 DAC_CHARGE_CURRENT_ALL_ON);
171
-
172148 mdelay(wait_ms);
173149
174150 return 0;
175151 }
176152
177
-static struct rk3328_reg_msk_val playback_open_list[] = {
153
+static const struct rk3328_reg_msk_val playback_open_list[] = {
178154 { DAC_PWR_CTRL, DAC_PWR_MASK, DAC_PWR_ON },
179155 { DAC_PWR_CTRL, DACL_PATH_REFV_MASK | DACR_PATH_REFV_MASK,
180156 DACL_PATH_REFV_ON | DACR_PATH_REFV_ON },
181
- { DAC_PWR_CTRL, HPOUTL_ZERO_CROSSING_ON | HPOUTR_ZERO_CROSSING_ON,
157
+ { DAC_PWR_CTRL, HPOUTL_ZERO_CROSSING_MASK | HPOUTR_ZERO_CROSSING_MASK,
182158 HPOUTL_ZERO_CROSSING_ON | HPOUTR_ZERO_CROSSING_ON },
183159 { HPOUT_POP_CTRL, HPOUTR_POP_MASK | HPOUTL_POP_MASK,
184160 HPOUTR_POP_WORK | HPOUTL_POP_WORK },
....@@ -203,18 +179,15 @@
203179 HPOUTL_UNMUTE | HPOUTR_UNMUTE },
204180 };
205181
206
-#define PLAYBACK_OPEN_LIST_LEN ARRAY_SIZE(playback_open_list)
207
-
208
-static int rk3328_codec_open_playback(struct snd_soc_component *component)
182
+static int rk3328_codec_open_playback(struct rk3328_codec_priv *rk3328)
209183 {
210
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(component);
211
- int i = 0;
184
+ int i;
212185
213186 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
214187 DAC_CHARGE_CURRENT_ALL_MASK,
215188 DAC_CHARGE_CURRENT_I);
216189
217
- for (i = 0; i < PLAYBACK_OPEN_LIST_LEN; i++) {
190
+ for (i = 0; i < ARRAY_SIZE(playback_open_list); i++) {
218191 regmap_update_bits(rk3328->regmap,
219192 playback_open_list[i].reg,
220193 playback_open_list[i].msk,
....@@ -223,16 +196,17 @@
223196 }
224197
225198 msleep(rk3328->spk_depop_time);
226
- rk3328_analog_output(rk3328, 1);
199
+ gpiod_set_value(rk3328->mute, 0);
227200
228201 regmap_update_bits(rk3328->regmap, HPOUTL_GAIN_CTRL,
229202 HPOUTL_GAIN_MASK, OUT_VOLUME);
230203 regmap_update_bits(rk3328->regmap, HPOUTR_GAIN_CTRL,
231204 HPOUTR_GAIN_MASK, OUT_VOLUME);
205
+
232206 return 0;
233207 }
234208
235
-static struct rk3328_reg_msk_val playback_close_list[] = {
209
+static const struct rk3328_reg_msk_val playback_close_list[] = {
236210 { HPMIX_CTRL, HPMIXL_INIT2_MASK | HPMIXR_INIT2_MASK,
237211 HPMIXL_INIT2_DIS | HPMIXR_INIT2_DIS },
238212 { DAC_SELECT, DACL_SELECT_MASK | DACR_SELECT_MASK,
....@@ -259,21 +233,18 @@
259233 DACL_INIT_OFF | DACR_INIT_OFF },
260234 };
261235
262
-#define PLAYBACK_CLOSE_LIST_LEN ARRAY_SIZE(playback_close_list)
263
-
264
-static int rk3328_codec_close_playback(struct snd_soc_component *component)
236
+static int rk3328_codec_close_playback(struct rk3328_codec_priv *rk3328)
265237 {
266
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(component);
267
- int i = 0;
238
+ size_t i;
268239
269
- rk3328_analog_output(rk3328, 0);
240
+ gpiod_set_value(rk3328->mute, 1);
270241
271242 regmap_update_bits(rk3328->regmap, HPOUTL_GAIN_CTRL,
272243 HPOUTL_GAIN_MASK, 0);
273244 regmap_update_bits(rk3328->regmap, HPOUTR_GAIN_CTRL,
274245 HPOUTR_GAIN_MASK, 0);
275246
276
- for (i = 0; i < PLAYBACK_CLOSE_LIST_LEN; i++) {
247
+ for (i = 0; i < ARRAY_SIZE(playback_close_list); i++) {
277248 regmap_update_bits(rk3328->regmap,
278249 playback_close_list[i].reg,
279250 playback_close_list[i].msk,
....@@ -281,9 +252,13 @@
281252 mdelay(1);
282253 }
283254
255
+ /* Workaround for silence when changed Fs 48 -> 44.1kHz */
256
+ rk3328_codec_reset(rk3328);
257
+
284258 regmap_update_bits(rk3328->regmap, DAC_PRECHARGE_CTRL,
285259 DAC_CHARGE_CURRENT_ALL_MASK,
286
- DAC_CHARGE_CURRENT_I);
260
+ DAC_CHARGE_CURRENT_ALL_ON);
261
+
287262 return 0;
288263 }
289264
....@@ -291,27 +266,28 @@
291266 struct snd_pcm_hw_params *params,
292267 struct snd_soc_dai *dai)
293268 {
294
- struct rk3328_codec_priv *rk3328 = snd_soc_component_get_drvdata(dai->component);
269
+ struct rk3328_codec_priv *rk3328 =
270
+ snd_soc_component_get_drvdata(dai->component);
295271 unsigned int val = 0;
296272
297273 switch (params_format(params)) {
298274 case SNDRV_PCM_FORMAT_S16_LE:
299
- val |= DAC_VDL_16BITS;
275
+ val = DAC_VDL_16BITS;
300276 break;
301277 case SNDRV_PCM_FORMAT_S20_3LE:
302
- val |= DAC_VDL_20BITS;
278
+ val = DAC_VDL_20BITS;
303279 break;
304280 case SNDRV_PCM_FORMAT_S24_LE:
305
- val |= DAC_VDL_24BITS;
281
+ val = DAC_VDL_24BITS;
306282 break;
307283 case SNDRV_PCM_FORMAT_S32_LE:
308
- val |= DAC_VDL_32BITS;
284
+ val = DAC_VDL_32BITS;
309285 break;
310286 default:
311287 return -EINVAL;
312288 }
313
-
314289 regmap_update_bits(rk3328->regmap, DAC_INIT_CTRL2, DAC_VDL_MASK, val);
290
+
315291 val = DAC_WL_32BITS | DAC_RST_DIS;
316292 regmap_update_bits(rk3328->regmap, DAC_INIT_CTRL3,
317293 DAC_WL_MASK | DAC_RST_MASK, val);
....@@ -322,21 +298,28 @@
322298 static int rk3328_pcm_startup(struct snd_pcm_substream *substream,
323299 struct snd_soc_dai *dai)
324300 {
325
- return rk3328_codec_open_playback(dai->component);
301
+ struct rk3328_codec_priv *rk3328 =
302
+ snd_soc_component_get_drvdata(dai->component);
303
+
304
+ return rk3328_codec_open_playback(rk3328);
326305 }
327306
328307 static void rk3328_pcm_shutdown(struct snd_pcm_substream *substream,
329308 struct snd_soc_dai *dai)
330309 {
331
- rk3328_codec_close_playback(dai->component);
310
+ struct rk3328_codec_priv *rk3328 =
311
+ snd_soc_component_get_drvdata(dai->component);
312
+
313
+ rk3328_codec_close_playback(rk3328);
332314 }
333315
334316 static const struct snd_soc_dai_ops rk3328_dai_ops = {
335317 .hw_params = rk3328_hw_params,
336318 .set_fmt = rk3328_set_dai_fmt,
337
- .digital_mute = rk3328_digital_mute,
319
+ .mute_stream = rk3328_mute_stream,
338320 .startup = rk3328_pcm_startup,
339321 .shutdown = rk3328_pcm_shutdown,
322
+ .no_capture_mute = 1,
340323 };
341324
342325 static struct snd_soc_dai_driver rk3328_dai[] = {
....@@ -369,19 +352,25 @@
369352
370353 static int rk3328_codec_probe(struct snd_soc_component *component)
371354 {
372
- rk3328_codec_reset(component);
373
- rk3328_codec_power_on(component, 0);
355
+ struct rk3328_codec_priv *rk3328 =
356
+ snd_soc_component_get_drvdata(component);
357
+
358
+ rk3328_codec_reset(rk3328);
359
+ rk3328_codec_power_on(rk3328, 0);
374360
375361 return 0;
376362 }
377363
378364 static void rk3328_codec_remove(struct snd_soc_component *component)
379365 {
380
- rk3328_codec_close_playback(component);
381
- rk3328_codec_power_off(component, 0);
366
+ struct rk3328_codec_priv *rk3328 =
367
+ snd_soc_component_get_drvdata(component);
368
+
369
+ rk3328_codec_close_playback(rk3328);
370
+ rk3328_codec_power_off(rk3328, 0);
382371 }
383372
384
-static const struct snd_soc_component_driver soc_codec_dev_rk3328 = {
373
+static const struct snd_soc_component_driver soc_codec_rk3328 = {
385374 .probe = rk3328_codec_probe,
386375 .remove = rk3328_codec_remove,
387376 };
....@@ -431,19 +420,10 @@
431420 .cache_type = REGCACHE_FLAT,
432421 };
433422
434
-#ifdef CONFIG_OF
435
-static const struct of_device_id rk3328codec_of_match[] = {
436
- { .compatible = "rockchip,rk3328-codec", },
437
- {},
438
-};
439
-MODULE_DEVICE_TABLE(of, rk3328codec_of_match);
440
-#endif
441
-
442423 static int rk3328_platform_probe(struct platform_device *pdev)
443424 {
444425 struct device_node *rk3328_np = pdev->dev.of_node;
445426 struct rk3328_codec_priv *rk3328;
446
- struct resource *res;
447427 struct regmap *grf;
448428 void __iomem *base;
449429 int ret = 0;
....@@ -458,7 +438,6 @@
458438 dev_err(&pdev->dev, "missing 'rockchip,grf'\n");
459439 return PTR_ERR(grf);
460440 }
461
- rk3328->grf = grf;
462441 /* enable i2s_acodec_en */
463442 regmap_write(grf, RK3328_GRF_SOC_CON2,
464443 (BIT(14) << 16 | BIT(14)));
....@@ -467,10 +446,21 @@
467446 &rk3328->spk_depop_time);
468447 if (ret < 0) {
469448 dev_info(&pdev->dev, "spk_depop_time use default value.\n");
470
- rk3328->spk_depop_time = 100;
449
+ rk3328->spk_depop_time = 200;
471450 }
472451
473
- rk3328_analog_output(rk3328, 0);
452
+ rk3328->mute = gpiod_get_optional(&pdev->dev, "mute", GPIOD_OUT_HIGH);
453
+ if (IS_ERR(rk3328->mute))
454
+ return PTR_ERR(rk3328->mute);
455
+ /*
456
+ * Rock64 is the only supported platform to have widely relied on
457
+ * this; if we do happen to come across an old DTB, just leave the
458
+ * external mute forced off.
459
+ */
460
+ if (!rk3328->mute && of_machine_is_compatible("pine64,rock64")) {
461
+ dev_warn(&pdev->dev, "assuming implicit control of GPIO_MUTE; update devicetree if possible\n");
462
+ regmap_write(grf, RK3328_GRF_SOC_CON10, BIT(17) | BIT(1));
463
+ }
474464
475465 rk3328->mclk = devm_clk_get(&pdev->dev, "mclk");
476466 if (IS_ERR(rk3328->mclk))
....@@ -484,35 +474,57 @@
484474 rk3328->pclk = devm_clk_get(&pdev->dev, "pclk");
485475 if (IS_ERR(rk3328->pclk)) {
486476 dev_err(&pdev->dev, "can't get acodec pclk\n");
487
- return PTR_ERR(rk3328->pclk);
477
+ ret = PTR_ERR(rk3328->pclk);
478
+ goto err_unprepare_mclk;
488479 }
489480
490481 ret = clk_prepare_enable(rk3328->pclk);
491482 if (ret < 0) {
492483 dev_err(&pdev->dev, "failed to enable acodec pclk\n");
493
- return ret;
484
+ goto err_unprepare_mclk;
494485 }
495486
496
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
497
- base = devm_ioremap_resource(&pdev->dev, res);
498
- if (IS_ERR(base))
499
- return PTR_ERR(base);
487
+ base = devm_platform_ioremap_resource(pdev, 0);
488
+ if (IS_ERR(base)) {
489
+ ret = PTR_ERR(base);
490
+ goto err_unprepare_pclk;
491
+ }
500492
501493 rk3328->regmap = devm_regmap_init_mmio(&pdev->dev, base,
502494 &rk3328_codec_regmap_config);
503
- if (IS_ERR(rk3328->regmap))
504
- return PTR_ERR(rk3328->regmap);
495
+ if (IS_ERR(rk3328->regmap)) {
496
+ ret = PTR_ERR(rk3328->regmap);
497
+ goto err_unprepare_pclk;
498
+ }
505499
506500 platform_set_drvdata(pdev, rk3328);
507501
508
- return devm_snd_soc_register_component(&pdev->dev, &soc_codec_dev_rk3328,
509
- rk3328_dai, ARRAY_SIZE(rk3328_dai));
502
+ ret = devm_snd_soc_register_component(&pdev->dev, &soc_codec_rk3328,
503
+ rk3328_dai,
504
+ ARRAY_SIZE(rk3328_dai));
505
+ if (ret)
506
+ goto err_unprepare_pclk;
507
+
508
+ return 0;
509
+
510
+err_unprepare_pclk:
511
+ clk_disable_unprepare(rk3328->pclk);
512
+
513
+err_unprepare_mclk:
514
+ clk_disable_unprepare(rk3328->mclk);
515
+ return ret;
510516 }
517
+
518
+static const struct of_device_id rk3328_codec_of_match[] = {
519
+ { .compatible = "rockchip,rk3328-codec", },
520
+ {},
521
+};
522
+MODULE_DEVICE_TABLE(of, rk3328_codec_of_match);
511523
512524 static struct platform_driver rk3328_codec_driver = {
513525 .driver = {
514526 .name = "rk3328-codec",
515
- .of_match_table = of_match_ptr(rk3328codec_of_match),
527
+ .of_match_table = of_match_ptr(rk3328_codec_of_match),
516528 },
517529 .probe = rk3328_platform_probe,
518530 };