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