de69367552715a588892667505699c0a1a0538c5
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0456-ASoC-bcm-Add-chipdip-dac-driver.patch
1 From 7da0da344225ec00f3070f9b4f6cb9dda06093ab Mon Sep 17 00:00:00 2001
2 From: "chipdip.lab" <43340836+chipdipru@users.noreply.github.com>
3 Date: Mon, 26 Jul 2021 14:45:59 +0300
4 Subject: [PATCH] ASoC: bcm: Add chipdip-dac driver
5
6 Driver chipdip-dac.c added into sound/soc/bcm/, files
7 sound/soc/bcm/Kconfig and sound/soc/bcm/Makefile updated.
8
9 Signed-off-by: Evgenij Sapunov <evgenij.sapunov@chipdip.ru>
10 ---
11 sound/soc/bcm/Kconfig | 6 +
12 sound/soc/bcm/Makefile | 3 +-
13 sound/soc/bcm/chipdip-dac.c | 275 ++++++++++++++++++++++++++++++++++++
14 3 files changed, 283 insertions(+), 1 deletion(-)
15 create mode 100644 sound/soc/bcm/chipdip-dac.c
16
17 --- a/sound/soc/bcm/Kconfig
18 +++ b/sound/soc/bcm/Kconfig
19 @@ -27,6 +27,12 @@ config SND_BCM63XX_I2S_WHISTLER
20
21 If you don't know what to do here, say N
22
23 +config SND_BCM2708_SOC_CHIPDIP_DAC
24 + tristate "Support for the ChipDip DAC"
25 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
26 + help
27 + Say Y or M if you want to add support for the ChipDip DAC soundcard
28 +
29 config SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD
30 tristate "Support for Google voiceHAT soundcard"
31 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
32 --- a/sound/soc/bcm/Makefile
33 +++ b/sound/soc/bcm/Makefile
34 @@ -47,6 +47,7 @@ snd-soc-fe-pi-audio-objs := fe-pi-audio.
35 snd-soc-rpi-simple-soundcard-objs := rpi-simple-soundcard.o
36 snd-soc-rpi-wm8804-soundcard-objs := rpi-wm8804-soundcard.o
37 snd-soc-pifi-40-objs := pifi-40.o
38 +snd-soc-chipdip-dac-objs := chipdip-dac.o
39
40 obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-codec.o
41 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
42 @@ -78,4 +79,4 @@ obj-$(CONFIG_SND_BCM2708_SOC_FE_PI_AUDIO
43 obj-$(CONFIG_SND_RPI_SIMPLE_SOUNDCARD) += snd-soc-rpi-simple-soundcard.o
44 obj-$(CONFIG_SND_RPI_WM8804_SOUNDCARD) += snd-soc-rpi-wm8804-soundcard.o
45 obj-$(CONFIG_SND_BCM2708_SOC_PIFI_40) += snd-soc-pifi-40.o
46 -
47 +obj-$(CONFIG_SND_BCM2708_SOC_CHIPDIP_DAC) += snd-soc-chipdip-dac.o
48 --- /dev/null
49 +++ b/sound/soc/bcm/chipdip-dac.c
50 @@ -0,0 +1,275 @@
51 +/*
52 + * ASoC Driver for ChipDip DAC
53 + *
54 + * Author: Evgenij Sapunov
55 + * Copyright 2021
56 + * based on code by Milan Neskovic <info@justboom.co>
57 + * based on code by Jaikumar <jaikumar@cem-solutions.net>
58 + *
59 + * Thanks to Phil Elwell (pelwell) for help.
60 + *
61 + * This program is free software; you can redistribute it and/or
62 + * modify it under the terms of the GNU General Public License
63 + * version 2 as published by the Free Software Foundation.
64 + *
65 + * This program is distributed in the hope that it will be useful, but
66 + * WITHOUT ANY WARRANTY; without even the implied warranty of
67 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
68 + * General Public License for more details.
69 + */
70 +
71 +#include <linux/module.h>
72 +#include <linux/gpio/consumer.h>
73 +#include <linux/platform_device.h>
74 +#include <linux/delay.h>
75 +
76 +#include <sound/core.h>
77 +#include <sound/pcm.h>
78 +#include <sound/pcm_params.h>
79 +#include <sound/soc.h>
80 +#include <sound/jack.h>
81 +
82 +#define SR_BIT_0 0 //sample rate bits
83 +#define SR_BIT_1 1
84 +#define SR_BIT_2 2
85 +#define BD_BIT_0 3 //bit depth bits
86 +#define BD_BIT_1 4
87 +
88 +#define SAMPLE_RATE_MASK_44_1 0
89 +#define SAMPLE_RATE_MASK_48 (1 << SR_BIT_0)
90 +#define SAMPLE_RATE_MASK_88_2 ((1 << SR_BIT_2) | (1 << SR_BIT_1))
91 +#define SAMPLE_RATE_MASK_96 (1 << SR_BIT_1)
92 +#define SAMPLE_RATE_MASK_176_4 ((1 << SR_BIT_2) | (1 << SR_BIT_1) | (1 << SR_BIT_0))
93 +#define SAMPLE_RATE_MASK_192 ((1 << SR_BIT_1) | (1 << SR_BIT_0))
94 +#define SAMPLE_RATE_MASK ((1 << SR_BIT_2) | (1 << SR_BIT_1) | (1 << SR_BIT_0))
95 +
96 +#define BIT_DEPTH_MASK_16 0
97 +#define BIT_DEPTH_MASK_24 (1 << BD_BIT_0)
98 +#define BIT_DEPTH_MASK_32 (1 << BD_BIT_1)
99 +#define BIT_DEPTH_MASK ((1 << BD_BIT_1) | (1 << BD_BIT_0))
100 +
101 +#define MUTE_ACTIVE 0
102 +#define MUTE_NOT_ACTIVE 1
103 +
104 +#define HW_PARAMS_GPIO_COUNT 5
105 +
106 +static struct gpio_desc *mute_gpio;
107 +static struct gpio_desc *sdwn_gpio;
108 +static struct gpio_desc *hw_params_gpios[HW_PARAMS_GPIO_COUNT];
109 +static int current_width;
110 +static int current_rate;
111 +
112 +static void snd_rpi_chipdip_dac_gpio_array_set(int value);
113 +static void snd_rpi_chipdip_dac_gpio_set(struct gpio_desc *gpio_item, int value);
114 +
115 +static void snd_rpi_chipdip_dac_gpio_array_set(int value)
116 +{
117 + int i = 0;
118 +
119 + for (i = 0; i < HW_PARAMS_GPIO_COUNT; i++)
120 + snd_rpi_chipdip_dac_gpio_set(hw_params_gpios[i], ((value >> i) & 1));
121 +}
122 +
123 +static void snd_rpi_chipdip_dac_gpio_set(struct gpio_desc *gpio_item, int value)
124 +{
125 + if (gpio_item)
126 + gpiod_set_value_cansleep(gpio_item, value);
127 +}
128 +
129 +static int snd_rpi_chipdip_dac_init(struct snd_soc_pcm_runtime *rtd)
130 +{
131 + return 0;
132 +}
133 +
134 +static int snd_rpi_chipdip_dac_hw_params(struct snd_pcm_substream *substream,
135 + struct snd_pcm_hw_params *params)
136 +{
137 + int ret = 0;
138 + int gpio_change_pending = 0;
139 + int sample_rate_state = 0;
140 + int bit_depth_state = 0;
141 + int param_value = params_width(params);
142 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
143 +
144 + ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_cpu(rtd, 0), 2 * 32);
145 +
146 + if (current_width != param_value) {
147 + current_width = param_value;
148 + gpio_change_pending = 1;
149 +
150 + switch (param_value) {
151 + case 16:
152 + bit_depth_state = BIT_DEPTH_MASK_16;
153 + break;
154 + case 24:
155 + bit_depth_state = BIT_DEPTH_MASK_24;
156 + break;
157 + case 32:
158 + bit_depth_state = BIT_DEPTH_MASK_32;
159 + break;
160 + default:
161 + return -EINVAL;
162 + }
163 + }
164 +
165 + param_value = params_rate(params);
166 + if (current_rate != param_value) {
167 + current_rate = param_value;
168 + gpio_change_pending = 1;
169 +
170 + switch (param_value) {
171 + case 44100:
172 + sample_rate_state = SAMPLE_RATE_MASK_44_1;
173 + break;
174 + case 48000:
175 + sample_rate_state = SAMPLE_RATE_MASK_48;
176 + break;
177 + case 88200:
178 + sample_rate_state = SAMPLE_RATE_MASK_88_2;
179 + break;
180 + case 96000:
181 + sample_rate_state = SAMPLE_RATE_MASK_96;
182 + break;
183 + case 176400:
184 + sample_rate_state = SAMPLE_RATE_MASK_176_4;
185 + break;
186 + case 192000:
187 + sample_rate_state = SAMPLE_RATE_MASK_192;
188 + break;
189 + default:
190 + return -EINVAL;
191 + }
192 + }
193 +
194 + if (gpio_change_pending) {
195 + snd_rpi_chipdip_dac_gpio_set(mute_gpio, MUTE_ACTIVE);
196 + snd_rpi_chipdip_dac_gpio_array_set(bit_depth_state | sample_rate_state);
197 + msleep(300);
198 + snd_rpi_chipdip_dac_gpio_set(mute_gpio, MUTE_NOT_ACTIVE);
199 + }
200 +
201 + return ret;
202 +}
203 +
204 +static int snd_rpi_chipdip_dac_startup(struct snd_pcm_substream *substream)
205 +{
206 + return 0;
207 +}
208 +
209 +static void snd_rpi_chipdip_dac_shutdown(struct snd_pcm_substream *substream)
210 +{
211 +
212 +}
213 +
214 +/* machine stream operations */
215 +static struct snd_soc_ops snd_rpi_chipdip_dac_ops = {
216 + .hw_params = snd_rpi_chipdip_dac_hw_params,
217 + .startup = snd_rpi_chipdip_dac_startup,
218 + .shutdown = snd_rpi_chipdip_dac_shutdown,
219 +};
220 +
221 +SND_SOC_DAILINK_DEFS(hifi,
222 + DAILINK_COMP_ARRAY(COMP_CPU("bcm2708-i2s.0")),
223 + DAILINK_COMP_ARRAY(COMP_CODEC("spdif-transmitter", "dit-hifi")),
224 + DAILINK_COMP_ARRAY(COMP_PLATFORM("bcm2708-i2s.0")));
225 +
226 +static struct snd_soc_dai_link snd_rpi_chipdip_dac_dai[] = {
227 +{
228 + .name = "ChipDip DAC",
229 + .stream_name = "ChipDip DAC HiFi",
230 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
231 + SND_SOC_DAIFMT_CBM_CFM,
232 + .ops = &snd_rpi_chipdip_dac_ops,
233 + .init = snd_rpi_chipdip_dac_init,
234 + SND_SOC_DAILINK_REG(hifi),
235 +},
236 +};
237 +
238 +/* audio machine driver */
239 +static struct snd_soc_card snd_rpi_chipdip_dac = {
240 + .name = "ChipDipDAC",
241 + .driver_name = "ChipdipDac",
242 + .owner = THIS_MODULE,
243 + .dai_link = snd_rpi_chipdip_dac_dai,
244 + .num_links = ARRAY_SIZE(snd_rpi_chipdip_dac_dai),
245 +};
246 +
247 +static int snd_rpi_chipdip_dac_probe(struct platform_device *pdev)
248 +{
249 + int ret = 0;
250 + int i = 0;
251 +
252 + snd_rpi_chipdip_dac.dev = &pdev->dev;
253 +
254 + if (pdev->dev.of_node) {
255 + struct device_node *i2s_node;
256 + struct snd_soc_dai_link *dai = &snd_rpi_chipdip_dac_dai[0];
257 + i2s_node = of_parse_phandle(pdev->dev.of_node,
258 + "i2s-controller", 0);
259 +
260 + if (i2s_node) {
261 + dai->cpus->dai_name = NULL;
262 + dai->cpus->of_node = i2s_node;
263 + dai->platforms->name = NULL;
264 + dai->platforms->of_node = i2s_node;
265 + }
266 + }
267 +
268 + hw_params_gpios[SR_BIT_0] = devm_gpiod_get_optional(&pdev->dev, "sr0", GPIOD_OUT_LOW);
269 + hw_params_gpios[SR_BIT_1] = devm_gpiod_get_optional(&pdev->dev, "sr1", GPIOD_OUT_LOW);
270 + hw_params_gpios[SR_BIT_2] = devm_gpiod_get_optional(&pdev->dev, "sr2", GPIOD_OUT_LOW);
271 + hw_params_gpios[BD_BIT_0] = devm_gpiod_get_optional(&pdev->dev, "res0", GPIOD_OUT_LOW);
272 + hw_params_gpios[BD_BIT_1] = devm_gpiod_get_optional(&pdev->dev, "res1", GPIOD_OUT_LOW);
273 + mute_gpio = devm_gpiod_get_optional(&pdev->dev, "mute", GPIOD_OUT_LOW);
274 + sdwn_gpio = devm_gpiod_get_optional(&pdev->dev, "sdwn", GPIOD_OUT_HIGH);
275 +
276 + for (i = 0; i < HW_PARAMS_GPIO_COUNT; i++) {
277 + if (IS_ERR(hw_params_gpios[i])) {
278 + ret = PTR_ERR(hw_params_gpios[i]);
279 + dev_err(&pdev->dev, "failed to get hw_params gpio: %d\n", ret);
280 + return ret;
281 + }
282 + }
283 +
284 + if (IS_ERR(mute_gpio)) {
285 + ret = PTR_ERR(mute_gpio);
286 + dev_err(&pdev->dev, "failed to get mute gpio: %d\n", ret);
287 + return ret;
288 + }
289 +
290 + if (IS_ERR(sdwn_gpio)) {
291 + ret = PTR_ERR(sdwn_gpio);
292 + dev_err(&pdev->dev, "failed to get sdwn gpio: %d\n", ret);
293 + return ret;
294 + }
295 +
296 + snd_rpi_chipdip_dac_gpio_set(sdwn_gpio, 1);
297 +
298 + ret = devm_snd_soc_register_card(&pdev->dev, &snd_rpi_chipdip_dac);
299 + if (ret && ret != -EPROBE_DEFER)
300 + dev_err(&pdev->dev,
301 + "snd_soc_register_card() failed: %d\n", ret);
302 +
303 + return ret;
304 +}
305 +
306 +static const struct of_device_id snd_rpi_chipdip_dac_of_match[] = {
307 + { .compatible = "chipdip,chipdip-dac", },
308 + {},
309 +};
310 +MODULE_DEVICE_TABLE(of, snd_rpi_chipdip_dac_of_match);
311 +
312 +static struct platform_driver snd_rpi_chipdip_dac_driver = {
313 + .driver = {
314 + .name = "snd-rpi-chipdip-dac",
315 + .owner = THIS_MODULE,
316 + .of_match_table = snd_rpi_chipdip_dac_of_match,
317 + },
318 + .probe = snd_rpi_chipdip_dac_probe,
319 +};
320 +
321 +module_platform_driver(snd_rpi_chipdip_dac_driver);
322 +
323 +MODULE_AUTHOR("Evgenij Sapunov <evgenij.sapunov@chipdip.ru>");
324 +MODULE_DESCRIPTION("ASoC Driver for ChipDip DAC");
325 +MODULE_LICENSE("GPL v2");