1 From e17c3e2ccb04657ca00984477854a6a700fef01d 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
6 Driver chipdip-dac.c added into sound/soc/bcm/, files
7 sound/soc/bcm/Kconfig and sound/soc/bcm/Makefile updated.
9 Signed-off-by: Evgenij Sapunov <evgenij.sapunov@chipdip.ru>
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
17 --- a/sound/soc/bcm/Kconfig
18 +++ b/sound/soc/bcm/Kconfig
19 @@ -27,6 +27,12 @@ config SND_BCM63XX_I2S_WHISTLER
21 If you don't know what to do here, say N
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
27 + Say Y or M if you want to add support for the ChipDip DAC soundcard
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
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
47 +obj-$(CONFIG_SND_BCM2708_SOC_CHIPDIP_DAC) += snd-soc-chipdip-dac.o
49 +++ b/sound/soc/bcm/chipdip-dac.c
52 + * ASoC Driver for ChipDip DAC
54 + * Author: Evgenij Sapunov
56 + * based on code by Milan Neskovic <info@justboom.co>
57 + * based on code by Jaikumar <jaikumar@cem-solutions.net>
59 + * Thanks to Phil Elwell (pelwell) for help.
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.
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.
71 +#include <linux/module.h>
72 +#include <linux/gpio/consumer.h>
73 +#include <linux/platform_device.h>
74 +#include <linux/delay.h>
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>
82 +#define SR_BIT_0 0 //sample rate bits
85 +#define BD_BIT_0 3 //bit depth bits
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))
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))
101 +#define MUTE_ACTIVE 0
102 +#define MUTE_NOT_ACTIVE 1
104 +#define HW_PARAMS_GPIO_COUNT 5
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;
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);
115 +static void snd_rpi_chipdip_dac_gpio_array_set(int value)
119 + for (i = 0; i < HW_PARAMS_GPIO_COUNT; i++)
120 + snd_rpi_chipdip_dac_gpio_set(hw_params_gpios[i], ((value >> i) & 1));
123 +static void snd_rpi_chipdip_dac_gpio_set(struct gpio_desc *gpio_item, int value)
126 + gpiod_set_value_cansleep(gpio_item, value);
129 +static int snd_rpi_chipdip_dac_init(struct snd_soc_pcm_runtime *rtd)
134 +static int snd_rpi_chipdip_dac_hw_params(struct snd_pcm_substream *substream,
135 + struct snd_pcm_hw_params *params)
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;
144 + ret = snd_soc_dai_set_bclk_ratio(asoc_rtd_to_cpu(rtd, 0), 2 * 32);
146 + if (current_width != param_value) {
147 + current_width = param_value;
148 + gpio_change_pending = 1;
150 + switch (param_value) {
152 + bit_depth_state = BIT_DEPTH_MASK_16;
155 + bit_depth_state = BIT_DEPTH_MASK_24;
158 + bit_depth_state = BIT_DEPTH_MASK_32;
165 + param_value = params_rate(params);
166 + if (current_rate != param_value) {
167 + current_rate = param_value;
168 + gpio_change_pending = 1;
170 + switch (param_value) {
172 + sample_rate_state = SAMPLE_RATE_MASK_44_1;
175 + sample_rate_state = SAMPLE_RATE_MASK_48;
178 + sample_rate_state = SAMPLE_RATE_MASK_88_2;
181 + sample_rate_state = SAMPLE_RATE_MASK_96;
184 + sample_rate_state = SAMPLE_RATE_MASK_176_4;
187 + sample_rate_state = SAMPLE_RATE_MASK_192;
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);
198 + snd_rpi_chipdip_dac_gpio_set(mute_gpio, MUTE_NOT_ACTIVE);
204 +static int snd_rpi_chipdip_dac_startup(struct snd_pcm_substream *substream)
209 +static void snd_rpi_chipdip_dac_shutdown(struct snd_pcm_substream *substream)
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,
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")));
226 +static struct snd_soc_dai_link snd_rpi_chipdip_dac_dai[] = {
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),
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),
247 +static int snd_rpi_chipdip_dac_probe(struct platform_device *pdev)
252 + snd_rpi_chipdip_dac.dev = &pdev->dev;
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);
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;
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);
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);
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);
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);
296 + snd_rpi_chipdip_dac_gpio_set(sdwn_gpio, 1);
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);
306 +static const struct of_device_id snd_rpi_chipdip_dac_of_match[] = {
307 + { .compatible = "chipdip,chipdip-dac", },
310 +MODULE_DEVICE_TABLE(of, snd_rpi_chipdip_dac_of_match);
312 +static struct platform_driver snd_rpi_chipdip_dac_driver = {
314 + .name = "snd-rpi-chipdip-dac",
315 + .owner = THIS_MODULE,
316 + .of_match_table = snd_rpi_chipdip_dac_of_match,
318 + .probe = snd_rpi_chipdip_dac_probe,
321 +module_platform_driver(snd_rpi_chipdip_dac_driver);
323 +MODULE_AUTHOR("Evgenij Sapunov <evgenij.sapunov@chipdip.ru>");
324 +MODULE_DESCRIPTION("ASoC Driver for ChipDip DAC");
325 +MODULE_LICENSE("GPL v2");