brcm2708: add kernel 4.14 support
[openwrt/staging/lynxis.git] / target / linux / brcm2708 / patches-4.14 / 950-0088-Allo-Digione-Driver-2048.patch
1 From 525cdff530e74a8428a60859f9308bca825463fd Mon Sep 17 00:00:00 2001
2 From: sandeepal <alsandeep@cem-solutions.net>
3 Date: Fri, 2 Jun 2017 18:59:46 +0530
4 Subject: [PATCH 088/454] Allo Digione Driver (#2048)
5
6 Driver for the Allo Digione soundcard
7
8 allo-digione: 192kHz clicking sound fix
9
10 See: https://github.com/raspberrypi/linux/pull/2149
11 ---
12 sound/soc/bcm/Kconfig | 7 +
13 sound/soc/bcm/Makefile | 2 +
14 sound/soc/bcm/allo-digione.c | 265 +++++++++++++++++++++++++++++++++++
15 3 files changed, 274 insertions(+)
16 create mode 100644 sound/soc/bcm/allo-digione.c
17
18 --- a/sound/soc/bcm/Kconfig
19 +++ b/sound/soc/bcm/Kconfig
20 @@ -176,6 +176,13 @@ config SND_BCM2708_SOC_ALLO_BOSS_DAC
21 help
22 Say Y or M if you want to add support for Allo Boss DAC.
23
24 +config SND_BCM2708_SOC_ALLO_DIGIONE
25 + tristate "Support for Allo DigiOne"
26 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
27 + select SND_SOC_PCM512x_I2C
28 + help
29 + Say Y or M if you want to add support for Allo DigiOne.
30 +
31 config SND_BCM2708_SOC_FE_PI_AUDIO
32 tristate "Support for Fe-Pi-Audio"
33 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
34 --- a/sound/soc/bcm/Makefile
35 +++ b/sound/soc/bcm/Makefile
36 @@ -34,6 +34,7 @@ snd-soc-dionaudio-loco-v2-objs := dionau
37 snd-soc-allo-boss-dac-objs := allo-boss-dac.o
38 snd-soc-allo-piano-dac-objs := allo-piano-dac.o
39 snd-soc-allo-piano-dac-plus-objs := allo-piano-dac-plus.o
40 +snd-soc-allo-digione-objs := allo-digione.o
41 snd-soc-pisound-objs := pisound.o
42 snd-soc-fe-pi-audio-objs := fe-pi-audio.o
43
44 @@ -60,5 +61,6 @@ obj-$(CONFIG_SND_BCM2708_SOC_DIONAUDIO_L
45 obj-$(CONFIG_SND_BCM2708_SOC_ALLO_BOSS_DAC) += snd-soc-allo-boss-dac.o
46 obj-$(CONFIG_SND_BCM2708_SOC_ALLO_PIANO_DAC) += snd-soc-allo-piano-dac.o
47 obj-$(CONFIG_SND_BCM2708_SOC_ALLO_PIANO_DAC_PLUS) += snd-soc-allo-piano-dac-plus.o
48 +obj-$(CONFIG_SND_BCM2708_SOC_ALLO_DIGIONE) += snd-soc-allo-digione.o
49 obj-$(CONFIG_SND_PISOUND) += snd-soc-pisound.o
50 obj-$(CONFIG_SND_BCM2708_SOC_FE_PI_AUDIO) += snd-soc-fe-pi-audio.o
51 --- /dev/null
52 +++ b/sound/soc/bcm/allo-digione.c
53 @@ -0,0 +1,265 @@
54 +/*
55 + * ASoC Driver for Allo DigiOne
56 + *
57 + * Author: Baswaraj <jaikumar@cemsolutions.net>
58 + * Copyright 2017
59 + * based on code by Daniel Matuschek <info@crazy-audio.com>
60 + * based on code by Florian Meier <florian.meier@koalo.de>
61 + *
62 + * This program is free software; you can redistribute it and/or
63 + * modify it under the terms of the GNU General Public License
64 + * version 2 as published by the Free Software Foundation.
65 + *
66 + * This program is distributed in the hope that it will be useful, but
67 + * WITHOUT ANY WARRANTY; without even the implied warranty of
68 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
69 + * General Public License for more details.
70 + */
71 +
72 +#include <linux/module.h>
73 +#include <linux/platform_device.h>
74 +
75 +#include <sound/core.h>
76 +#include <sound/pcm.h>
77 +#include <sound/pcm_params.h>
78 +#include <sound/soc.h>
79 +#include <sound/jack.h>
80 +#include <linux/gpio/consumer.h>
81 +
82 +#include "../codecs/wm8804.h"
83 +
84 +static short int auto_shutdown_output;
85 +module_param(auto_shutdown_output, short,
86 + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
87 +MODULE_PARM_DESC(auto_shutdown_output, "Shutdown SP/DIF output if playback is stopped");
88 +
89 +#define CLK_44EN_RATE 22579200UL
90 +#define CLK_48EN_RATE 24576000UL
91 +
92 +static struct gpio_desc *snd_allo_clk44gpio;
93 +static struct gpio_desc *snd_allo_clk48gpio;
94 +
95 +static int samplerate = 44100;
96 +
97 +static uint32_t snd_allo_digione_enable_clock(int sample_rate)
98 +{
99 + switch (sample_rate) {
100 + case 11025:
101 + case 22050:
102 + case 44100:
103 + case 88200:
104 + case 176400:
105 + gpiod_set_value_cansleep(snd_allo_clk44gpio, 1);
106 + gpiod_set_value_cansleep(snd_allo_clk48gpio, 0);
107 + return CLK_44EN_RATE;
108 + default:
109 + gpiod_set_value_cansleep(snd_allo_clk48gpio, 1);
110 + gpiod_set_value_cansleep(snd_allo_clk44gpio, 0);
111 + return CLK_48EN_RATE;
112 + }
113 +}
114 +
115 +
116 +static int snd_allo_digione_init(struct snd_soc_pcm_runtime *rtd)
117 +{
118 + struct snd_soc_codec *codec = rtd->codec;
119 +
120 + /* enable TX output */
121 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
122 +
123 + return 0;
124 +}
125 +
126 +static int snd_allo_digione_startup(struct snd_pcm_substream *substream)
127 +{
128 + /* turn on digital output */
129 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 + struct snd_soc_codec *codec = rtd->codec;
131 +
132 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
133 + return 0;
134 +}
135 +
136 +static void snd_allo_digione_shutdown(struct snd_pcm_substream *substream)
137 +{
138 + /* turn off output */
139 + if (auto_shutdown_output) {
140 + /* turn off output */
141 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
142 + struct snd_soc_codec *codec = rtd->codec;
143 +
144 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
145 + }
146 +}
147 +
148 +static int snd_allo_digione_hw_params(struct snd_pcm_substream *substream,
149 + struct snd_pcm_hw_params *params)
150 +{
151 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
152 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
153 + struct snd_soc_codec *codec = rtd->codec;
154 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
155 +
156 + int sysclk = 27000000; /* This is fixed on this board */
157 +
158 + long mclk_freq = 0;
159 + int mclk_div = 1;
160 + int sampling_freq = 1;
161 +
162 + int ret;
163 +
164 + samplerate = params_rate(params);
165 + mclk_freq = samplerate * 256;
166 + mclk_div = WM8804_MCLKDIV_256FS;
167 +
168 + sysclk = snd_allo_digione_enable_clock(samplerate);
169 +
170 + switch (samplerate) {
171 + case 32000:
172 + sampling_freq = 0x03;
173 + break;
174 + case 44100:
175 + sampling_freq = 0x00;
176 + break;
177 + case 48000:
178 + sampling_freq = 0x02;
179 + break;
180 + case 88200:
181 + sampling_freq = 0x08;
182 + break;
183 + case 96000:
184 + sampling_freq = 0x0a;
185 + break;
186 + case 176400:
187 + sampling_freq = 0x0c;
188 + break;
189 + case 192000:
190 + sampling_freq = 0x0e;
191 + break;
192 + default:
193 + dev_err(codec->dev,
194 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
195 + samplerate);
196 + }
197 +
198 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
199 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
200 +
201 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
202 + sysclk, SND_SOC_CLOCK_OUT);
203 +
204 + if (ret < 0) {
205 + dev_err(codec->dev,
206 + "Failed to set WM8804 SYSCLK: %d\n", ret);
207 + return ret;
208 + }
209 +
210 + /* Enable TX output */
211 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
212 +
213 + /* Power on */
214 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
215 +
216 + /* set sampling frequency status bits */
217 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
218 +
219 + return snd_soc_dai_set_bclk_ratio(cpu_dai, 64);
220 +}
221 +
222 +/* machine stream operations */
223 +static struct snd_soc_ops snd_allo_digione_ops = {
224 + .hw_params = snd_allo_digione_hw_params,
225 + .startup = snd_allo_digione_startup,
226 + .shutdown = snd_allo_digione_shutdown,
227 +};
228 +
229 +static struct snd_soc_dai_link snd_allo_digione_dai[] = {
230 +{
231 + .name = "Allo DigiOne",
232 + .stream_name = "Allo DigiOne HiFi",
233 + .cpu_dai_name = "bcm2708-i2s.0",
234 + .codec_dai_name = "wm8804-spdif",
235 + .platform_name = "bcm2708-i2s.0",
236 + .codec_name = "wm8804.1-003b",
237 + .dai_fmt = SND_SOC_DAIFMT_I2S |
238 + SND_SOC_DAIFMT_NB_NF |
239 + SND_SOC_DAIFMT_CBM_CFM,
240 + .ops = &snd_allo_digione_ops,
241 + .init = snd_allo_digione_init,
242 +},
243 +};
244 +
245 +/* audio machine driver */
246 +static struct snd_soc_card snd_allo_digione = {
247 + .name = "snd_allo_digione",
248 + .driver_name = "AlloDigiOne",
249 + .owner = THIS_MODULE,
250 + .dai_link = snd_allo_digione_dai,
251 + .num_links = ARRAY_SIZE(snd_allo_digione_dai),
252 +};
253 +
254 +static int snd_allo_digione_probe(struct platform_device *pdev)
255 +{
256 + int ret = 0;
257 +
258 + snd_allo_digione.dev = &pdev->dev;
259 +
260 + if (pdev->dev.of_node) {
261 + struct device_node *i2s_node;
262 + struct snd_soc_dai_link *dai = &snd_allo_digione_dai[0];
263 +
264 + i2s_node = of_parse_phandle(pdev->dev.of_node,
265 + "i2s-controller", 0);
266 +
267 + if (i2s_node) {
268 + dai->cpu_dai_name = NULL;
269 + dai->cpu_of_node = i2s_node;
270 + dai->platform_name = NULL;
271 + dai->platform_of_node = i2s_node;
272 + }
273 +
274 + snd_allo_clk44gpio =
275 + devm_gpiod_get(&pdev->dev, "clock44", GPIOD_OUT_LOW);
276 + if (IS_ERR(snd_allo_clk44gpio))
277 + dev_err(&pdev->dev, "devm_gpiod_get() failed\n");
278 +
279 + snd_allo_clk48gpio =
280 + devm_gpiod_get(&pdev->dev, "clock48", GPIOD_OUT_LOW);
281 + if (IS_ERR(snd_allo_clk48gpio))
282 + dev_err(&pdev->dev, "devm_gpiod_get() failed\n");
283 + }
284 +
285 + ret = snd_soc_register_card(&snd_allo_digione);
286 + if (ret && ret != -EPROBE_DEFER)
287 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
288 + ret);
289 +
290 + return ret;
291 +}
292 +
293 +static int snd_allo_digione_remove(struct platform_device *pdev)
294 +{
295 + return snd_soc_unregister_card(&snd_allo_digione);
296 +}
297 +
298 +static const struct of_device_id snd_allo_digione_of_match[] = {
299 + { .compatible = "allo,allo-digione", },
300 + {},
301 +};
302 +MODULE_DEVICE_TABLE(of, snd_allo_digione_of_match);
303 +
304 +static struct platform_driver snd_allo_digione_driver = {
305 + .driver = {
306 + .name = "snd-allo-digione",
307 + .owner = THIS_MODULE,
308 + .of_match_table = snd_allo_digione_of_match,
309 + },
310 + .probe = snd_allo_digione_probe,
311 + .remove = snd_allo_digione_remove,
312 +};
313 +
314 +module_platform_driver(snd_allo_digione_driver);
315 +
316 +MODULE_AUTHOR("Baswaraj <jaikumar@cem-solutions.net>");
317 +MODULE_DESCRIPTION("ASoC Driver for Allo DigiOne");
318 +MODULE_LICENSE("GPL v2");