aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/bcm27xx/patches-5.10/950-0723-ASoC-bcm-Add-chipdip-dac-driver.patch
blob: 25dbc74d20e2265e6a4afc3b8edb5ac96f779738 (plain)
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
From e17c3e2ccb04657ca00984477854a6a700fef01d Mon Sep 17 00:00:00 2001
From: "chipdip.lab" <43340836+chipdipru@users.noreply.github.com>
Date: Mon, 26 Jul 2021 14:45:59 +0300
Subject: [PATCH] ASoC: bcm: Add chipdip-dac driver

Driver chipdip-dac.c added into sound/soc/bcm/, files
sound/soc/bcm/Kconfig and sound/soc/bcm/Makefile updated.

Signed-off-by: Evgenij Sapunov <evgenij.sapunov@chipdip.ru>
---
 sound/soc/bcm/Kconfig       |   6 +
 sound/soc/bcm/Makefile      |   3 +-
 sound/soc/bcm/chipdip-dac.c | 275 ++++++++++++++++++++++++++++++++++++
 3 files changed, 283 insertions(+), 1 deletion(-)
 create mode 100644 sound/soc/bcm/chipdip-dac.c

--- a/sound/soc/bcm/Kconfig
+++ b/sound/soc/bcm/Kconfig
@@ -27,6 +27,12 @@ config SND_BCM63XX_I2S_WHISTLER
 
 	  If you don't know what to do here, say N
 
+config SND_BCM2708_SOC_CHIPDIP_DAC
+         tristate "Support for the ChipDip DAC"
+         depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
+         help
+          Say Y or M if you want to add support for the ChipDip DAC soundcard
+
 config SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD
 	tristate "Support for Google voiceHAT soundcard"
 	depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
--- a/sound/soc/bcm/Makefile
+++ b/sound/soc/bcm/Makefile
@@ -47,6 +47,7 @@ snd-soc-fe-pi-audio-objs := fe-pi-audio.
 snd-soc-rpi-simple-soundcard-objs := rpi-simple-soundcard.o
 snd-soc-rpi-wm8804-soundcard-objs := rpi-wm8804-soundcard.o
 snd-soc-pifi-40-objs := pifi-40.o
+snd-soc-chipdip-dac-objs := chipdip-dac.o
 
 obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD)  += snd-soc-googlevoicehat-codec.o
 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
@@ -78,4 +79,4 @@ obj-$(CONFIG_SND_BCM2708_SOC_FE_PI_AUDIO
 obj-$(CONFIG_SND_RPI_SIMPLE_SOUNDCARD) += snd-soc-rpi-simple-soundcard.o
 obj-$(CONFIG_SND_RPI_WM8804_SOUNDCARD) += snd-soc-rpi-wm8804-soundcard.o
 obj-$(CONFIG_SND_BCM2708_SOC_PIFI_40) += snd-soc-pifi-40.o
-
+obj-$(CONFIG_SND_BCM2708_SOC_CHIPDIP_DAC) += snd-soc-chipdip-dac.o
--- /dev/null
+++ b/sound/soc/bcm/chipdip-dac.c
@@ -0,0 +1,275 @@
+/*
+ * ASoC Driver for ChipDip DAC
+ *
+ * Author:	Evgenij Sapunov
+ *		Copyright 2021
+ *		based on code by Milan Neskovic <info@justboom.co>
+ *		based on code by Jaikumar <jaikumar@cem-solutions.net>
+ *
+ * Thanks to Phil Elwell (pelwell) for help.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ */
+
+#include <linux/module.h>
+#include <linux/gpio/consumer.h>
+#include <linux/platform_device.h>
+#include <linux/delay.h>
+
+#include <sound/core.h>
+#include <sound/pcm.h>
+#include <sound/pcm_params.h>
+#include <sound/soc.h>
+#include <sound/jack.h>
+
+#define SR_BIT_0                  0 //sample rate bits
+#define SR_BIT_1                  1
+#define SR_BIT_2                  2
+#define BD_BIT_0                  3 //bit depth bits
+#define BD_BIT_1                  4
+
+#define SAMPLE_RATE_MASK_44_1     0
+#define SAMPLE_RATE_MASK_48       (1 << SR_BIT_0)
+#define SAMPLE_RATE_MASK_88_2     ((1 << SR_BIT_2) | (1 << SR_BIT_1))
+#define SAMPLE_RATE_MASK_96       (1 << SR_BIT_1)
+#define SAMPLE_RATE_MASK_176_4    ((1 << SR_BIT_2) | (1 << SR_BIT_1) | (1 << SR_BIT_0))
+#define SAMPLE_RATE_MASK_192      ((1 << SR_BIT_1) | (1 << SR_BIT_0))
+#define SAMPLE_RATE_MASK          ((1 << SR_BIT_2) | (1 << SR_BIT_1) | (1 << SR_BIT_0))
+
+#define BIT_DEPTH_MASK_16         0
+#define BIT_DEPTH_MASK_24         (1 << BD_BIT_0)
+#define BIT_DEPTH_MASK_32         (1 << BD_BIT_1)
+#define BIT_DEPTH_MASK            ((1 << BD_BIT_1) | (1 << BD_BIT_0))
+
+#define MUTE_ACTIVE               0
+#define MUTE_NOT_ACTIVE           1
+
+#define HW_PARAMS_GPIO_COUNT      5
+
+static struct gpio_desc *mute_gpio;
+static struct gpio_desc *sdwn_gpio;
+static struct gpio_desc *hw_params_gpios[HW_PARAMS_GPIO_COUNT];
+static int current_width;
+static int current_rate;
+
+static void snd_rpi_chipdip_dac_gpio_array_set(int value);
+static void snd_rpi_chipdip_dac_gpio_set(struct gpio_desc *gpio_item, int value);
+
+static void snd_rpi_chipdip_dac_gpio_array_set(int value)
+{
+	int i = 0;
+
+	for (i = 0; i < HW_PARAMS_GPIO_COUNT; i++)
+		snd_rpi_chipdip_dac_gpio_set(hw_params_gpios[i], ((value >> i) & 1));
+}
+
+static void snd_rpi_chipdip_dac_gpio_set(struct gpio_desc *gpio_item, int value)
+{
+	if (gpio_item)
+		gpiod_set_value_cansleep(gpio_item, value);
+}
+
+static int snd_rpi_chipdip_dac_init(struct snd_soc_pcm_runtime *rtd)
+{
+	return 0;
+}
+
+static int snd_rpi_chipdip_dac_hw_params(struct snd_pcm_substream *substream,
+					 struct snd_pcm_hw_params *params)
+{
+	int ret = 0;
+	int gpio_change_pending = 0;
+	int sample_rate_state = 0;
+	int bit_depth_state = 0;
+	int param_value = params_width(params);
+	struct snd_soc_pcm_runtime *rtd = substream->private_data;
+
+	ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_cpu(rtd, 0), 2 * 32);
+
+	if (current_width != param_value) {
+		current_width = param_value;
+		gpio_change_pending = 1;
+
+		switch (param_value) {
+		case 16:
+			bit_depth_state = BIT_DEPTH_MASK_16;
+			break;
+		case 24:
+			bit_depth_state = BIT_DEPTH_MASK_24;
+			break;
+		case 32:
+			bit_depth_state = BIT_DEPTH_MASK_32;
+			break;
+		default:
+			return -EINVAL;
+		}
+	}
+
+	param_value = params_rate(params);
+	if (current_rate != param_value) {
+		current_rate = param_value;
+		gpio_change_pending = 1;
+
+		switch (param_value) {
+		case 44100:
+			sample_rate_state = SAMPLE_RATE_MASK_44_1;
+			break;
+		case 48000:
+			sample_rate_state = SAMPLE_RATE_MASK_48;
+			break;
+		case 88200:
+			sample_rate_state = SAMPLE_RATE_MASK_88_2;
+			break;
+		case 96000:
+			sample_rate_state = SAMPLE_RATE_MASK_96;
+			break;
+		case 176400:
+			sample_rate_state = SAMPLE_RATE_MASK_176_4;
+			break;
+		case 192000:
+			sample_rate_state = SAMPLE_RATE_MASK_192;
+			break;
+		default:
+			return -EINVAL;
+		}
+	}
+
+	if (gpio_change_pending) {
+		snd_rpi_chipdip_dac_gpio_set(mute_gpio, MUTE_ACTIVE);
+		snd_rpi_chipdip_dac_gpio_array_set(bit_depth_state | sample_rate_state);
+		msleep(300);
+		snd_rpi_chipdip_dac_gpio_set(mute_gpio, MUTE_NOT_ACTIVE);
+	}
+
+	return ret;
+}
+
+static int snd_rpi_chipdip_dac_startup(struct snd_pcm_substream *substream)
+{
+	return 0;
+}
+
+static void snd_rpi_chipdip_dac_shutdown(struct snd_pcm_substream *substream)
+{
+
+}
+
+/* machine stream operations */
+static struct snd_soc_ops snd_rpi_chipdip_dac_ops = {
+	.hw_params = snd_rpi_chipdip_dac_hw_params,
+	.startup = snd_rpi_chipdip_dac_startup,
+	.shutdown = snd_rpi_chipdip_dac_shutdown,
+};
+
+SND_SOC_DAILINK_DEFS(hifi,
+	DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
+	DAILINK_COMP_ARRAY(COMP_CODEC("spdif-transmitter", "dit-hifi")),
+	DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
+
+static struct snd_soc_dai_link snd_rpi_chipdip_dac_dai[] = {
+{
+	.name		= "ChipDip DAC",
+	.stream_name	= "ChipDip DAC HiFi",
+	.dai_fmt	= SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
+				SND_SOC_DAIFMT_CBM_CFM,
+	.ops		= &snd_rpi_chipdip_dac_ops,
+	.init		= snd_rpi_chipdip_dac_init,
+	SND_SOC_DAILINK_REG(hifi),
+},
+};
+
+/* audio machine driver */
+static struct snd_soc_card snd_rpi_chipdip_dac = {
+	.name         = "ChipDipDAC",
+	.driver_name  = "ChipdipDac",
+	.owner        = THIS_MODULE,
+	.dai_link     = snd_rpi_chipdip_dac_dai,
+	.num_links    = ARRAY_SIZE(snd_rpi_chipdip_dac_dai),
+};
+
+static int snd_rpi_chipdip_dac_probe(struct platform_device *pdev)
+{
+	int ret = 0;
+	int i = 0;
+
+	snd_rpi_chipdip_dac.dev = &pdev->dev;
+
+	if (pdev->dev.of_node) {
+		struct device_node *i2s_node;
+		struct snd_soc_dai_link *dai = &snd_rpi_chipdip_dac_dai[0];
+		i2s_node = of_parse_phandle(pdev->dev.of_node,
+					"i2s-controller", 0);
+
+		if (i2s_node) {
+			dai->cpus->dai_name = NULL;
+			dai->cpus->of_node = i2s_node;
+			dai->platforms->name = NULL;
+			dai->platforms->of_node = i2s_node;
+		}
+	}
+
+	hw_params_gpios[SR_BIT_0] = devm_gpiod_get_optional(&pdev->dev, "sr0", GPIOD_OUT_LOW);
+	hw_params_gpios[SR_BIT_1] = devm_gpiod_get_optional(&pdev->dev, "sr1", GPIOD_OUT_LOW);
+	hw_params_gpios[SR_BIT_2] = devm_gpiod_get_optional(&pdev->dev, "sr2", GPIOD_OUT_LOW);
+	hw_params_gpios[BD_BIT_0] = devm_gpiod_get_optional(&pdev->dev, "res0", GPIOD_OUT_LOW);
+	hw_params_gpios[BD_BIT_1] = devm_gpiod_get_optional(&pdev->dev, "res1", GPIOD_OUT_LOW);
+	mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute", GPIOD_OUT_LOW);
+	sdwn_gpio = devm_gpiod_get_optional(&pdev->dev, "sdwn", GPIOD_OUT_HIGH);
+
+	for (i = 0; i < HW_PARAMS_GPIO_COUNT; i++) {
+		if (IS_ERR(hw_params_gpios[i])) {
+			ret = PTR_ERR(hw_params_gpios[i]);
+			dev_err(&pdev->dev, "failed to get hw_params gpio: %d\n", ret);
+			return ret;
+		}
+	}
+
+	if (IS_ERR(mute_gpio)) {
+		ret = PTR_ERR(mute_gpio);
+		dev_err(&pdev->dev, "failed to get mute gpio: %d\n", ret);
+		return ret;
+	}
+
+	if (IS_ERR(sdwn_gpio)) {
+		ret = PTR_ERR(sdwn_gpio);
+		dev_err(&pdev->dev, "failed to get sdwn gpio: %d\n", ret);
+		return ret;
+	}
+
+	snd_rpi_chipdip_dac_gpio_set(sdwn_gpio, 1);
+
+	ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_chipdip_dac);
+	if (ret && ret != -EPROBE_DEFER)
+		dev_err(&pdev->dev,
+			"snd_soc_register_card() failed: %d\n", ret);
+
+	return ret;
+}
+
+static const struct of_device_id snd_rpi_chipdip_dac_of_match[] = {
+	{ .compatible = "chipdip,chipdip-dac", },
+	{},
+};
+MODULE_DEVICE_TABLE(of, snd_rpi_chipdip_dac_of_match);
+
+static struct platform_driver snd_rpi_chipdip_dac_driver = {
+	.driver = {
+		.name   = "snd-rpi-chipdip-dac",
+		.owner  = THIS_MODULE,
+		.of_match_table = snd_rpi_chipdip_dac_of_match,
+	},
+	.probe          = snd_rpi_chipdip_dac_probe,
+};
+
+module_platform_driver(snd_rpi_chipdip_dac_driver);
+
+MODULE_AUTHOR("Evgenij Sapunov <evgenij.sapunov@chipdip.ru>");
+MODULE_DESCRIPTION("ASoC Driver for ChipDip DAC");
+MODULE_LICENSE("GPL v2");