brcm2708: update to latest patches from the RPi foundation
[openwrt/staging/wigyori.git] / target / linux / brcm2708 / patches-4.19 / 950-0724-adds-the-Hifiberry-DAC-ADC-PRO-version.patch
1 From 9404c34f9d9aef6f1c5a8f8c223865a5adb81518 Mon Sep 17 00:00:00 2001
2 From: Joerg Schambacher <joscha@schambacher.com>
3 Date: Tue, 23 Jul 2019 16:57:35 +0200
4 Subject: [PATCH 724/725] adds the Hifiberry DAC+ADC PRO version
5
6 This adds the driver for the DAC+ADC PRO version of the Hifiberry soundcard with software controlled PCM1863 ADC
7 Signed-off-by: Joerg Schambacher joerg@i2audio.com
8 ---
9 arch/arm/boot/dts/overlays/Makefile | 1 +
10 arch/arm/boot/dts/overlays/README | 21 +
11 .../hifiberry-dacplusadcpro-overlay.dts | 64 +++
12 arch/arm/configs/bcm2709_defconfig | 1 +
13 arch/arm/configs/bcm2711_defconfig | 1 +
14 arch/arm/configs/bcmrpi_defconfig | 1 +
15 sound/soc/bcm/Kconfig | 8 +
16 sound/soc/bcm/Makefile | 2 +
17 sound/soc/bcm/hifiberry_dacplusadcpro.c | 538 ++++++++++++++++++
18 9 files changed, 637 insertions(+)
19 create mode 100644 arch/arm/boot/dts/overlays/hifiberry-dacplusadcpro-overlay.dts
20 create mode 100644 sound/soc/bcm/hifiberry_dacplusadcpro.c
21
22 --- a/arch/arm/boot/dts/overlays/Makefile
23 +++ b/arch/arm/boot/dts/overlays/Makefile
24 @@ -53,6 +53,7 @@ dtbo-$(CONFIG_ARCH_BCM2835) += \
25 hifiberry-dac.dtbo \
26 hifiberry-dacplus.dtbo \
27 hifiberry-dacplusadc.dtbo \
28 + hifiberry-dacplusadcpro.dtbo \
29 hifiberry-digi.dtbo \
30 hifiberry-digi-pro.dtbo \
31 hy28a.dtbo \
32 --- a/arch/arm/boot/dts/overlays/README
33 +++ b/arch/arm/boot/dts/overlays/README
34 @@ -883,6 +883,27 @@ Params: 24db_digital_gain Allow ga
35 master for bit clock and frame clock.
36
37
38 +Name: hifiberry-dacplusadcpro
39 +Info: Configures the HifiBerry DAC+ADC PRO audio card
40 +Load: dtoverlay=hifiberry-dacplusadcpro,<param>=<val>
41 +Params: 24db_digital_gain Allow gain to be applied via the PCM512x codec
42 + Digital volume control. Enable with
43 + "dtoverlay=hifiberry-dacplusadcpro,24db_digital_gain"
44 + (The default behaviour is that the Digital
45 + volume control is limited to a maximum of
46 + 0dB. ie. it can attenuate but not provide
47 + gain. For most users, this will be desired
48 + as it will prevent clipping. By appending
49 + the 24dB_digital_gain parameter, the Digital
50 + volume control will allow up to 24dB of
51 + gain. If this parameter is enabled, it is the
52 + responsibility of the user to ensure that
53 + the Digital volume control is set to a value
54 + that does not result in clipping/distortion!)
55 + slave Force DAC+ADC Pro into slave mode, using Pi as
56 + master for bit clock and frame clock.
57 +
58 +
59 Name: hifiberry-digi
60 Info: Configures the HifiBerry Digi and Digi+ audio card
61 Load: dtoverlay=hifiberry-digi
62 --- /dev/null
63 +++ b/arch/arm/boot/dts/overlays/hifiberry-dacplusadcpro-overlay.dts
64 @@ -0,0 +1,64 @@
65 +// Definitions for HiFiBerry DAC+ADC PRO
66 +/dts-v1/;
67 +/plugin/;
68 +
69 +/ {
70 + compatible = "brcm,bcm2708";
71 +
72 + fragment@0 {
73 + target-path = "/clocks";
74 + __overlay__ {
75 + dacpro_osc: dacpro_osc {
76 + compatible = "hifiberry,dacpro-clk";
77 + #clock-cells = <0>;
78 + };
79 + };
80 + };
81 +
82 + fragment@1 {
83 + target = <&i2s>;
84 + __overlay__ {
85 + status = "okay";
86 + };
87 + };
88 +
89 + fragment@2 {
90 + target = <&i2c1>;
91 + __overlay__ {
92 + #address-cells = <1>;
93 + #size-cells = <0>;
94 + status = "okay";
95 +
96 + hb_dac: pcm5122@4d {
97 + #sound-dai-cells = <0>;
98 + compatible = "ti,pcm5122";
99 + reg = <0x4d>;
100 + clocks = <&dacpro_osc>;
101 + status = "okay";
102 + };
103 + hb_adc: pcm186x@4a {
104 + #sound-dai-cells = <0>;
105 + compatible = "ti,pcm1863";
106 + reg = <0x4a>;
107 + clocks = <&dacpro_osc>;
108 + status = "okay";
109 + };
110 + };
111 + };
112 +
113 + fragment@3 {
114 + target = <&sound>;
115 + hifiberry_dacplusadcpro: __overlay__ {
116 + compatible = "hifiberry,hifiberry-dacplusadcpro";
117 + audio-codec = <&hb_dac &hb_adc>;
118 + i2s-controller = <&i2s>;
119 + status = "okay";
120 + };
121 + };
122 +
123 + __overrides__ {
124 + 24db_digital_gain =
125 + <&hifiberry_dacplusadcpro>,"hifiberry-dacplusadcpro,24db_digital_gain?";
126 + slave = <&hifiberry_dacplusadcpro>,"hifiberry-dacplusadcpro,slave?";
127 + };
128 +};
129 --- a/arch/arm/configs/bcm2709_defconfig
130 +++ b/arch/arm/configs/bcm2709_defconfig
131 @@ -961,6 +961,7 @@ CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SO
132 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
133 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
134 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC=m
135 +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADCPRO=m
136 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
137 CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP=m
138 CONFIG_SND_BCM2708_SOC_RPI_CIRRUS=m
139 --- a/arch/arm/configs/bcm2711_defconfig
140 +++ b/arch/arm/configs/bcm2711_defconfig
141 @@ -972,6 +972,7 @@ CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SO
142 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
143 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
144 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC=m
145 +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADCPRO=m
146 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
147 CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP=m
148 CONFIG_SND_BCM2708_SOC_RPI_CIRRUS=m
149 --- a/arch/arm/configs/bcmrpi_defconfig
150 +++ b/arch/arm/configs/bcmrpi_defconfig
151 @@ -953,6 +953,7 @@ CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SO
152 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC=m
153 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS=m
154 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC=m
155 +CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADCPRO=m
156 CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI=m
157 CONFIG_SND_BCM2708_SOC_HIFIBERRY_AMP=m
158 CONFIG_SND_BCM2708_SOC_RPI_CIRRUS=m
159 --- a/sound/soc/bcm/Kconfig
160 +++ b/sound/soc/bcm/Kconfig
161 @@ -48,6 +48,14 @@ config SND_BCM2708_SOC_HIFIBERRY_DACPLUS
162 help
163 Say Y or M if you want to add support for HifiBerry DAC+ADC.
164
165 +config SND_BCM2708_SOC_HIFIBERRY_DACPLUSADCPRO
166 + tristate "Support for HifiBerry DAC+ADC PRO"
167 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
168 + select SND_SOC_PCM512x_I2C
169 + select SND_SOC_PCM186X_I2C
170 + help
171 + Say Y or M if you want to add support for HifiBerry DAC+ADC PRO.
172 +
173 config SND_BCM2708_SOC_HIFIBERRY_DIGI
174 tristate "Support for HifiBerry Digi"
175 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
176 --- a/sound/soc/bcm/Makefile
177 +++ b/sound/soc/bcm/Makefile
178 @@ -14,6 +14,7 @@ snd-soc-googlevoicehat-codec-objs := goo
179 # BCM2708 Machine Support
180 snd-soc-hifiberry-dacplus-objs := hifiberry_dacplus.o
181 snd-soc-hifiberry-dacplusadc-objs := hifiberry_dacplusadc.o
182 +snd-soc-hifiberry-dacplusadcpro-objs := hifiberry_dacplusadcpro.o
183 snd-soc-justboom-dac-objs := justboom-dac.o
184 snd-soc-rpi-cirrus-objs := rpi-cirrus.o
185 snd-soc-rpi-proto-objs := rpi-proto.o
186 @@ -38,6 +39,7 @@ snd-soc-rpi-wm8804-soundcard-objs := rpi
187 obj-$(CONFIG_SND_BCM2708_SOC_GOOGLEVOICEHAT_SOUNDCARD) += snd-soc-googlevoicehat-codec.o
188 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUS) += snd-soc-hifiberry-dacplus.o
189 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADC) += snd-soc-hifiberry-dacplusadc.o
190 +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DACPLUSADCPRO) += snd-soc-hifiberry-dacplusadcpro.o
191 obj-$(CONFIG_SND_BCM2708_SOC_JUSTBOOM_DAC) += snd-soc-justboom-dac.o
192 obj-$(CONFIG_SND_BCM2708_SOC_RPI_CIRRUS) += snd-soc-rpi-cirrus.o
193 obj-$(CONFIG_SND_BCM2708_SOC_RPI_PROTO) += snd-soc-rpi-proto.o
194 --- /dev/null
195 +++ b/sound/soc/bcm/hifiberry_dacplusadcpro.c
196 @@ -0,0 +1,538 @@
197 +/*
198 + * ASoC Driver for HiFiBerry DAC+ / DAC Pro with ADC PRO Version (SW control)
199 + *
200 + * Author: Daniel Matuschek, Stuart MacLean <stuart@hifiberry.com>
201 + * Copyright 2014-2015
202 + * based on code by Florian Meier <florian.meier@koalo.de>
203 + * ADC added by Joerg Schambacher <joerg@i2audio.com>
204 + * Copyright 2018-19
205 + *
206 + * This program is free software; you can redistribute it and/or
207 + * modify it under the terms of the GNU General Public License
208 + * version 2 as published by the Free Software Foundation.
209 + *
210 + * This program is distributed in the hope that it will be useful, but
211 + * WITHOUT ANY WARRANTY; without even the implied warranty of
212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
213 + * General Public License for more details.
214 + */
215 +
216 +#include <linux/module.h>
217 +#include <linux/platform_device.h>
218 +#include <linux/kernel.h>
219 +#include <linux/clk.h>
220 +#include <linux/kernel.h>
221 +#include <linux/module.h>
222 +#include <linux/of.h>
223 +#include <linux/slab.h>
224 +#include <linux/delay.h>
225 +
226 +#include <sound/core.h>
227 +#include <sound/pcm.h>
228 +#include <sound/pcm_params.h>
229 +#include <sound/soc.h>
230 +#include <sound/jack.h>
231 +#include <sound/tlv.h>
232 +
233 +#include "../codecs/pcm512x.h"
234 +#include "../codecs/pcm186x.h"
235 +
236 +#define HIFIBERRY_DACPRO_NOCLOCK 0
237 +#define HIFIBERRY_DACPRO_CLK44EN 1
238 +#define HIFIBERRY_DACPRO_CLK48EN 2
239 +
240 +struct pcm512x_priv {
241 + struct regmap *regmap;
242 + struct clk *sclk;
243 +};
244 +
245 +/* Clock rate of CLK44EN attached to GPIO6 pin */
246 +#define CLK_44EN_RATE 22579200UL
247 +/* Clock rate of CLK48EN attached to GPIO3 pin */
248 +#define CLK_48EN_RATE 24576000UL
249 +
250 +static bool slave;
251 +static bool snd_rpi_hifiberry_is_dacpro;
252 +static bool digital_gain_0db_limit = true;
253 +
254 +static const unsigned int pcm186x_adc_input_channel_sel_value[] = {
255 + 0x00, 0x01, 0x02, 0x03, 0x10
256 +};
257 +
258 +static const char * const pcm186x_adcl_input_channel_sel_text[] = {
259 + "No Select",
260 + "VINL1[SE]", /* Default for ADCL */
261 + "VINL2[SE]",
262 + "VINL2[SE] + VINL1[SE]",
263 + "{VIN1P, VIN1M}[DIFF]"
264 +};
265 +
266 +static const char * const pcm186x_adcr_input_channel_sel_text[] = {
267 + "No Select",
268 + "VINR1[SE]", /* Default for ADCR */
269 + "VINR2[SE]",
270 + "VINR2[SE] + VINR1[SE]",
271 + "{VIN2P, VIN2M}[DIFF]"
272 +};
273 +
274 +static const struct soc_enum pcm186x_adc_input_channel_sel[] = {
275 + SOC_VALUE_ENUM_SINGLE(PCM186X_ADC1_INPUT_SEL_L, 0,
276 + PCM186X_ADC_INPUT_SEL_MASK,
277 + ARRAY_SIZE(pcm186x_adcl_input_channel_sel_text),
278 + pcm186x_adcl_input_channel_sel_text,
279 + pcm186x_adc_input_channel_sel_value),
280 + SOC_VALUE_ENUM_SINGLE(PCM186X_ADC1_INPUT_SEL_R, 0,
281 + PCM186X_ADC_INPUT_SEL_MASK,
282 + ARRAY_SIZE(pcm186x_adcr_input_channel_sel_text),
283 + pcm186x_adcr_input_channel_sel_text,
284 + pcm186x_adc_input_channel_sel_value),
285 +};
286 +
287 +static const unsigned int pcm186x_mic_bias_sel_value[] = {
288 + 0x00, 0x01, 0x11
289 +};
290 +
291 +static const char * const pcm186x_mic_bias_sel_text[] = {
292 + "Mic Bias off",
293 + "Mic Bias on",
294 + "Mic Bias with Bypass Resistor"
295 +};
296 +
297 +static const struct soc_enum pcm186x_mic_bias_sel[] = {
298 + SOC_VALUE_ENUM_SINGLE(PCM186X_MIC_BIAS_CTRL, 0,
299 + GENMASK(4, 0),
300 + ARRAY_SIZE(pcm186x_mic_bias_sel_text),
301 + pcm186x_mic_bias_sel_text,
302 + pcm186x_mic_bias_sel_value),
303 +};
304 +
305 +static const unsigned int pcm186x_gain_sel_value[] = {
306 + 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
307 + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
308 + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
309 + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
310 + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
311 + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
312 + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
313 + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
314 + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
315 + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
316 + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
317 + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
318 + 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
319 + 0x50
320 +};
321 +
322 +static const char * const pcm186x_gain_sel_text[] = {
323 + "-12.0dB", "-11.5dB", "-11.0dB", "-10.5dB", "-10.0dB", "-9.5dB",
324 + "-9.0dB", "-8.5dB", "-8.0dB", "-7.5dB", "-7.0dB", "-6.5dB",
325 + "-6.0dB", "-5.5dB", "-5.0dB", "-4.5dB", "-4.0dB", "-3.5dB",
326 + "-3.0dB", "-2.5dB", "-2.0dB", "-1.5dB", "-1.0dB", "-0.5dB",
327 + "0.0dB", "0.5dB", "1.0dB", "1.5dB", "2.0dB", "2.5dB",
328 + "3.0dB", "3.5dB", "4.0dB", "4.5dB", "5.0dB", "5.5dB",
329 + "6.0dB", "6.5dB", "7.0dB", "7.5dB", "8.0dB", "8.5dB",
330 + "9.0dB", "9.5dB", "10.0dB", "10.5dB", "11.0dB", "11.5dB",
331 + "12.0dB", "12.5dB", "13.0dB", "13.5dB", "14.0dB", "14.5dB",
332 + "15.0dB", "15.5dB", "16.0dB", "16.5dB", "17.0dB", "17.5dB",
333 + "18.0dB", "18.5dB", "19.0dB", "19.5dB", "20.0dB", "20.5dB",
334 + "21.0dB", "21.5dB", "22.0dB", "22.5dB", "23.0dB", "23.5dB",
335 + "24.0dB", "24.5dB", "25.0dB", "25.5dB", "26.0dB", "26.5dB",
336 + "27.0dB", "27.5dB", "28.0dB", "28.5dB", "29.0dB", "29.5dB",
337 + "30.0dB", "30.5dB", "31.0dB", "31.5dB", "32.0dB", "32.5dB",
338 + "33.0dB", "33.5dB", "34.0dB", "34.5dB", "35.0dB", "35.5dB",
339 + "36.0dB", "36.5dB", "37.0dB", "37.5dB", "38.0dB", "38.5dB",
340 + "39.0dB", "39.5dB", "40.0dB"};
341 +
342 +static const struct soc_enum pcm186x_gain_sel[] = {
343 + SOC_VALUE_ENUM_SINGLE(PCM186X_PGA_VAL_CH1_L, 0,
344 + 0xff,
345 + ARRAY_SIZE(pcm186x_gain_sel_text),
346 + pcm186x_gain_sel_text,
347 + pcm186x_gain_sel_value),
348 + SOC_VALUE_ENUM_SINGLE(PCM186X_PGA_VAL_CH1_R, 0,
349 + 0xff,
350 + ARRAY_SIZE(pcm186x_gain_sel_text),
351 + pcm186x_gain_sel_text,
352 + pcm186x_gain_sel_value),
353 +};
354 +
355 +static const struct snd_kcontrol_new pcm1863_snd_controls_card[] = {
356 + SOC_ENUM("ADC Left Input", pcm186x_adc_input_channel_sel[0]),
357 + SOC_ENUM("ADC Right Input", pcm186x_adc_input_channel_sel[1]),
358 + SOC_ENUM("ADC Mic Bias", pcm186x_mic_bias_sel),
359 + SOC_ENUM("PGA Gain Left", pcm186x_gain_sel[0]),
360 + SOC_ENUM("PGA Gain Right", pcm186x_gain_sel[1]),
361 +};
362 +
363 +static int pcm1863_add_controls(struct snd_soc_component *component)
364 +{
365 + snd_soc_add_component_controls(component,
366 + pcm1863_snd_controls_card,
367 + ARRAY_SIZE(pcm1863_snd_controls_card));
368 + return 0;
369 +}
370 +
371 +static void snd_rpi_hifiberry_dacplusadcpro_select_clk(
372 + struct snd_soc_component *component, int clk_id)
373 +{
374 + switch (clk_id) {
375 + case HIFIBERRY_DACPRO_NOCLOCK:
376 + snd_soc_component_update_bits(component,
377 + PCM512x_GPIO_CONTROL_1, 0x24, 0x00);
378 + break;
379 + case HIFIBERRY_DACPRO_CLK44EN:
380 + snd_soc_component_update_bits(component,
381 + PCM512x_GPIO_CONTROL_1, 0x24, 0x20);
382 + break;
383 + case HIFIBERRY_DACPRO_CLK48EN:
384 + snd_soc_component_update_bits(component,
385 + PCM512x_GPIO_CONTROL_1, 0x24, 0x04);
386 + break;
387 + }
388 +}
389 +
390 +static void snd_rpi_hifiberry_dacplusadcpro_clk_gpio(struct snd_soc_component *component)
391 +{
392 + snd_soc_component_update_bits(component, PCM512x_GPIO_EN, 0x24, 0x24);
393 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_3, 0x0f, 0x02);
394 + snd_soc_component_update_bits(component, PCM512x_GPIO_OUTPUT_6, 0x0f, 0x02);
395 +}
396 +
397 +static bool snd_rpi_hifiberry_dacplusadcpro_is_sclk(struct snd_soc_component *component)
398 +{
399 + unsigned int sck;
400 +
401 + snd_soc_component_read(component, PCM512x_RATE_DET_4, &sck);
402 + return (!(sck & 0x40));
403 +}
404 +
405 +static bool snd_rpi_hifiberry_dacplusadcpro_is_sclk_sleep(
406 + struct snd_soc_component *component)
407 +{
408 + msleep(2);
409 + return snd_rpi_hifiberry_dacplusadcpro_is_sclk(component);
410 +}
411 +
412 +static bool snd_rpi_hifiberry_dacplusadcpro_is_pro_card(struct snd_soc_component *component)
413 +{
414 + bool isClk44EN, isClk48En, isNoClk;
415 +
416 + snd_rpi_hifiberry_dacplusadcpro_clk_gpio(component);
417 +
418 + snd_rpi_hifiberry_dacplusadcpro_select_clk(component, HIFIBERRY_DACPRO_CLK44EN);
419 + isClk44EN = snd_rpi_hifiberry_dacplusadcpro_is_sclk_sleep(component);
420 +
421 + snd_rpi_hifiberry_dacplusadcpro_select_clk(component, HIFIBERRY_DACPRO_NOCLOCK);
422 + isNoClk = snd_rpi_hifiberry_dacplusadcpro_is_sclk_sleep(component);
423 +
424 + snd_rpi_hifiberry_dacplusadcpro_select_clk(component, HIFIBERRY_DACPRO_CLK48EN);
425 + isClk48En = snd_rpi_hifiberry_dacplusadcpro_is_sclk_sleep(component);
426 +
427 + return (isClk44EN && isClk48En && !isNoClk);
428 +}
429 +
430 +static int snd_rpi_hifiberry_dacplusadcpro_clk_for_rate(int sample_rate)
431 +{
432 + int type;
433 +
434 + switch (sample_rate) {
435 + case 11025:
436 + case 22050:
437 + case 44100:
438 + case 88200:
439 + case 176400:
440 + case 352800:
441 + type = HIFIBERRY_DACPRO_CLK44EN;
442 + break;
443 + default:
444 + type = HIFIBERRY_DACPRO_CLK48EN;
445 + break;
446 + }
447 + return type;
448 +}
449 +
450 +static void snd_rpi_hifiberry_dacplusadcpro_set_sclk(struct snd_soc_component *component,
451 + int sample_rate)
452 +{
453 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
454 +
455 + if (!IS_ERR(pcm512x->sclk)) {
456 + int ctype;
457 +
458 + ctype = snd_rpi_hifiberry_dacplusadcpro_clk_for_rate(sample_rate);
459 + clk_set_rate(pcm512x->sclk, (ctype == HIFIBERRY_DACPRO_CLK44EN)
460 + ? CLK_44EN_RATE : CLK_48EN_RATE);
461 + snd_rpi_hifiberry_dacplusadcpro_select_clk(component, ctype);
462 + }
463 +}
464 +
465 +static int snd_rpi_hifiberry_dacplusadcpro_init(struct snd_soc_pcm_runtime *rtd)
466 +{
467 + struct snd_soc_component *dac = rtd->codec_dais[0]->component;
468 + struct snd_soc_component *adc = rtd->codec_dais[1]->component;
469 + struct snd_soc_dai_driver *adc_driver = rtd->codec_dais[1]->driver;
470 + struct pcm512x_priv *priv;
471 + int ret;
472 +
473 + if (slave)
474 + snd_rpi_hifiberry_is_dacpro = false;
475 + else
476 + snd_rpi_hifiberry_is_dacpro =
477 + snd_rpi_hifiberry_dacplusadcpro_is_pro_card(dac);
478 +
479 + if (snd_rpi_hifiberry_is_dacpro) {
480 + struct snd_soc_dai_link *dai = rtd->dai_link;
481 +
482 + dai->name = "HiFiBerry DAC+ADC Pro";
483 + dai->stream_name = "HiFiBerry DAC+ADC Pro HiFi";
484 +
485 + // set DAC DAI configuration
486 + ret = snd_soc_dai_set_fmt(rtd->codec_dais[0],
487 + SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
488 + | SND_SOC_DAIFMT_CBM_CFM);
489 + if (ret < 0)
490 + return ret;
491 +
492 + // set ADC DAI configuration
493 + ret = snd_soc_dai_set_fmt(rtd->codec_dais[1],
494 + SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
495 + | SND_SOC_DAIFMT_CBS_CFS);
496 + if (ret < 0)
497 + return ret;
498 +
499 + // set CPU DAI configuration
500 + ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
501 + SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
502 + if (ret < 0)
503 + return ret;
504 +
505 + snd_soc_component_update_bits(dac, PCM512x_BCLK_LRCLK_CFG, 0x31, 0x11);
506 + snd_soc_component_update_bits(dac, PCM512x_MASTER_MODE, 0x03, 0x03);
507 + snd_soc_component_update_bits(dac, PCM512x_MASTER_CLKDIV_2, 0x7f, 63);
508 + } else {
509 + priv = snd_soc_component_get_drvdata(dac);
510 + priv->sclk = ERR_PTR(-ENOENT);
511 + }
512 +
513 + /* disable 24bit mode as long as I2S module does not have sign extension fixed */
514 + adc_driver->capture.formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE;
515 +
516 + snd_soc_component_update_bits(dac, PCM512x_GPIO_EN, 0x08, 0x08);
517 + snd_soc_component_update_bits(dac, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
518 + snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
519 +
520 + ret = pcm1863_add_controls(adc);
521 + if (ret < 0)
522 + dev_warn(rtd->dev, "Failed to add pcm1863 controls: %d\n",
523 + ret);
524 +
525 + /* set GPIO2 to output, GPIO3 input */
526 + snd_soc_component_write(adc, PCM186X_GPIO3_2_CTRL, 0x00);
527 + snd_soc_component_write(adc, PCM186X_GPIO3_2_DIR_CTRL, 0x04);
528 + snd_soc_component_update_bits(adc, PCM186X_GPIO_IN_OUT, 0x40, 0x40);
529 +
530 + if (digital_gain_0db_limit) {
531 + int ret;
532 + struct snd_soc_card *card = rtd->card;
533 +
534 + ret = snd_soc_limit_volume(card, "Digital Playback Volume", 207);
535 + if (ret < 0)
536 + dev_warn(card->dev, "Failed to set volume limit: %d\n", ret);
537 + }
538 +
539 + return 0;
540 +}
541 +
542 +static int snd_rpi_hifiberry_dacplusadcpro_update_rate_den(
543 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
544 +{
545 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 + struct snd_soc_component *component = rtd->codec_dais[0]->component; /* only use DAC */
547 + struct pcm512x_priv *pcm512x = snd_soc_component_get_drvdata(component);
548 + struct snd_ratnum *rats_no_pll;
549 + unsigned int num = 0, den = 0;
550 + int err;
551 +
552 + rats_no_pll = devm_kzalloc(rtd->dev, sizeof(*rats_no_pll), GFP_KERNEL);
553 + if (!rats_no_pll)
554 + return -ENOMEM;
555 +
556 + rats_no_pll->num = clk_get_rate(pcm512x->sclk) / 64;
557 + rats_no_pll->den_min = 1;
558 + rats_no_pll->den_max = 128;
559 + rats_no_pll->den_step = 1;
560 +
561 + err = snd_interval_ratnum(hw_param_interval(params,
562 + SNDRV_PCM_HW_PARAM_RATE), 1, rats_no_pll, &num, &den);
563 + if (err >= 0 && den) {
564 + params->rate_num = num;
565 + params->rate_den = den;
566 + }
567 +
568 + devm_kfree(rtd->dev, rats_no_pll);
569 + return 0;
570 +}
571 +
572 +static int snd_rpi_hifiberry_dacplusadcpro_hw_params(
573 + struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
574 +{
575 + int ret = 0;
576 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
577 + int channels = params_channels(params);
578 + int width = 32;
579 + struct snd_soc_component *dac = rtd->codec_dais[0]->component;
580 +
581 + if (snd_rpi_hifiberry_is_dacpro) {
582 +
583 + width = snd_pcm_format_physical_width(params_format(params));
584 +
585 + snd_rpi_hifiberry_dacplusadcpro_set_sclk(dac,
586 + params_rate(params));
587 +
588 + ret = snd_rpi_hifiberry_dacplusadcpro_update_rate_den(
589 + substream, params);
590 + if (ret)
591 + return ret;
592 + }
593 +
594 + ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x03, 0x03,
595 + channels, width);
596 + if (ret)
597 + return ret;
598 + ret = snd_soc_dai_set_tdm_slot(rtd->codec_dais[0], 0x03, 0x03,
599 + channels, width);
600 + if (ret)
601 + return ret;
602 + ret = snd_soc_dai_set_tdm_slot(rtd->codec_dais[1], 0x03, 0x03,
603 + channels, width);
604 + return ret;
605 +}
606 +
607 +static int snd_rpi_hifiberry_dacplusadcpro_startup(
608 + struct snd_pcm_substream *substream)
609 +{
610 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
611 + struct snd_soc_component *dac = rtd->codec_dais[0]->component;
612 + struct snd_soc_component *adc = rtd->codec_dais[1]->component;
613 +
614 + /* switch on respective LED */
615 + if (!substream->stream)
616 + snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x08);
617 + else
618 + snd_soc_component_update_bits(adc, PCM186X_GPIO_IN_OUT, 0x40, 0x40);
619 + return 0;
620 +}
621 +
622 +static void snd_rpi_hifiberry_dacplusadcpro_shutdown(
623 + struct snd_pcm_substream *substream)
624 +{
625 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
626 + struct snd_soc_component *dac = rtd->codec_dais[0]->component;
627 + struct snd_soc_component *adc = rtd->codec_dais[1]->component;
628 +
629 + /* switch off respective LED */
630 + if (!substream->stream)
631 + snd_soc_component_update_bits(dac, PCM512x_GPIO_CONTROL_1, 0x08, 0x00);
632 + else
633 + snd_soc_component_update_bits(adc, PCM186X_GPIO_IN_OUT, 0x40, 0x00);
634 +}
635 +
636 +
637 +/* machine stream operations */
638 +static struct snd_soc_ops snd_rpi_hifiberry_dacplusadcpro_ops = {
639 + .hw_params = snd_rpi_hifiberry_dacplusadcpro_hw_params,
640 + .startup = snd_rpi_hifiberry_dacplusadcpro_startup,
641 + .shutdown = snd_rpi_hifiberry_dacplusadcpro_shutdown,
642 +};
643 +
644 +static struct snd_soc_dai_link_component snd_rpi_hifiberry_dacplusadcpro_codecs[] = {
645 + {
646 + .name = "pcm512x.1-004d",
647 + .dai_name = "pcm512x-hifi",
648 + },
649 + {
650 + .name = "pcm186x.1-004a",
651 + .dai_name = "pcm1863-aif",
652 + },
653 +};
654 +
655 +static struct snd_soc_dai_link snd_rpi_hifiberry_dacplusadcpro_dai[] = {
656 +{
657 + .name = "HiFiBerry DAC+ADC PRO",
658 + .stream_name = "HiFiBerry DAC+ADC PRO HiFi",
659 + .cpu_dai_name = "bcm2708-i2s.0",
660 + .platform_name = "bcm2708-i2s.0",
661 + .codecs = snd_rpi_hifiberry_dacplusadcpro_codecs,
662 + .num_codecs = 2,
663 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
664 + SND_SOC_DAIFMT_CBS_CFS,
665 + .ops = &snd_rpi_hifiberry_dacplusadcpro_ops,
666 + .init = snd_rpi_hifiberry_dacplusadcpro_init,
667 +},
668 +};
669 +
670 +/* audio machine driver */
671 +static struct snd_soc_card snd_rpi_hifiberry_dacplusadcpro = {
672 + .name = "snd_rpi_hifiberry_dacplusadcpro",
673 + .driver_name = "HifiberryDacpAdcPro",
674 + .owner = THIS_MODULE,
675 + .dai_link = snd_rpi_hifiberry_dacplusadcpro_dai,
676 + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_dacplusadcpro_dai),
677 +};
678 +
679 +static int snd_rpi_hifiberry_dacplusadcpro_probe(struct platform_device *pdev)
680 +{
681 + int ret = 0, i = 0;
682 + struct snd_soc_card *card = &snd_rpi_hifiberry_dacplusadcpro;
683 +
684 + snd_rpi_hifiberry_dacplusadcpro.dev = &pdev->dev;
685 + if (pdev->dev.of_node) {
686 + struct device_node *i2s_node;
687 + struct snd_soc_dai_link *dai;
688 +
689 + dai = &snd_rpi_hifiberry_dacplusadcpro_dai[0];
690 + i2s_node = of_parse_phandle(pdev->dev.of_node,
691 + "i2s-controller", 0);
692 + if (i2s_node) {
693 + for (i = 0; i < card->num_links; i++) {
694 + dai->cpu_dai_name = NULL;
695 + dai->cpu_of_node = i2s_node;
696 + dai->platform_name = NULL;
697 + dai->platform_of_node = i2s_node;
698 + }
699 + }
700 + }
701 + digital_gain_0db_limit = !of_property_read_bool(
702 + pdev->dev.of_node, "hifiberry-dacplusadcpro,24db_digital_gain");
703 + slave = of_property_read_bool(pdev->dev.of_node,
704 + "hifiberry-dacplusadcpro,slave");
705 + ret = snd_soc_register_card(&snd_rpi_hifiberry_dacplusadcpro);
706 + if (ret && ret != -EPROBE_DEFER)
707 + dev_err(&pdev->dev,
708 + "snd_soc_register_card() failed: %d\n", ret);
709 +
710 + return ret;
711 +}
712 +
713 +static const struct of_device_id snd_rpi_hifiberry_dacplusadcpro_of_match[] = {
714 + { .compatible = "hifiberry,hifiberry-dacplusadcpro", },
715 + {},
716 +};
717 +
718 +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_dacplusadcpro_of_match);
719 +
720 +static struct platform_driver snd_rpi_hifiberry_dacplusadcpro_driver = {
721 + .driver = {
722 + .name = "snd-rpi-hifiberry-dacplusadcpro",
723 + .owner = THIS_MODULE,
724 + .of_match_table = snd_rpi_hifiberry_dacplusadcpro_of_match,
725 + },
726 + .probe = snd_rpi_hifiberry_dacplusadcpro_probe,
727 +};
728 +
729 +module_platform_driver(snd_rpi_hifiberry_dacplusadcpro_driver);
730 +
731 +MODULE_AUTHOR("Joerg Schambacher <joerg@i2audio.com>");
732 +MODULE_AUTHOR("Daniel Matuschek <daniel@hifiberry.com>");
733 +MODULE_DESCRIPTION("ASoC Driver for HiFiBerry DAC+ADC");
734 +MODULE_LICENSE("GPL v2");