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