f77ab539f280e55364b3454cc47b0f598f304c75
[openwrt/staging/jow.git] /
1 From fbea312ab9d1b0edd50eb6947bc4905a9d25617a Mon Sep 17 00:00:00 2001
2 From: Daniel Matuschek <info@crazy-audio.com>
3 Date: Wed, 15 Jan 2014 21:42:08 +0100
4 Subject: [PATCH 067/454] ASoC: BCM:Add support for HiFiBerry Digi. Driver is
5 based on the patched WM8804 driver.
6
7 Signed-off-by: Daniel Matuschek <daniel@matuschek.net>
8
9 Add a parameter to turn off SPDIF output if no audio is playing
10
11 This patch adds the paramater auto_shutdown_output to the kernel module.
12 Default behaviour of the module is the same, but when auto_shutdown_output
13 is set to 1, the SPDIF oputput will shutdown if no stream is playing.
14
15 bugfix for 32kHz sample rate, was missing
16
17 HiFiBerry Digi: set SPDIF status bits for sample rate
18
19 The HiFiBerry Digi driver did not signal the sample rate in the SPDIF status bits.
20 While this is optional, some DACs and receivers do not accept this signal. This patch
21 adds the sample rate bits in the SPDIF status block.
22
23 Added HiFiBerry Digi+ Pro driver
24
25 Signed-off-by: Daniel Matuschek <daniel@hifiberry.com>
26 ---
27 sound/soc/bcm/Kconfig | 7 +
28 sound/soc/bcm/Makefile | 2 +
29 sound/soc/bcm/hifiberry_digi.c | 276 +++++++++++++++++++++++++++++++++
30 3 files changed, 285 insertions(+)
31 create mode 100644 sound/soc/bcm/hifiberry_digi.c
32
33 --- a/sound/soc/bcm/Kconfig
34 +++ b/sound/soc/bcm/Kconfig
35 @@ -25,6 +25,13 @@ config SND_BCM2708_SOC_HIFIBERRY_DAC
36 help
37 Say Y or M if you want to add support for HifiBerry DAC.
38
39 +config SND_BCM2708_SOC_HIFIBERRY_DIGI
40 + tristate "Support for HifiBerry Digi"
41 + depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
42 + select SND_SOC_WM8804
43 + help
44 + Say Y or M if you want to add support for HifiBerry Digi S/PDIF output board.
45 +
46 config SND_BCM2708_SOC_RPI_DAC
47 tristate "Support for RPi-DAC"
48 depends on SND_BCM2708_SOC_I2S || SND_BCM2835_SOC_I2S
49 --- a/sound/soc/bcm/Makefile
50 +++ b/sound/soc/bcm/Makefile
51 @@ -10,7 +10,9 @@ obj-$(CONFIG_SND_SOC_CYGNUS) += snd-soc-
52
53 # BCM2708 Machine Support
54 snd-soc-hifiberry-dac-objs := hifiberry_dac.o
55 +snd-soc-hifiberry-digi-objs := hifiberry_digi.o
56 snd-soc-rpi-dac-objs := rpi-dac.o
57
58 obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DAC) += snd-soc-hifiberry-dac.o
59 +obj-$(CONFIG_SND_BCM2708_SOC_HIFIBERRY_DIGI) += snd-soc-hifiberry-digi.o
60 obj-$(CONFIG_SND_BCM2708_SOC_RPI_DAC) += snd-soc-rpi-dac.o
61 --- /dev/null
62 +++ b/sound/soc/bcm/hifiberry_digi.c
63 @@ -0,0 +1,276 @@
64 +/*
65 + * ASoC Driver for HifiBerry Digi
66 + *
67 + * Author: Daniel Matuschek <info@crazy-audio.com>
68 + * based on the HifiBerry DAC driver by Florian Meier <florian.meier@koalo.de>
69 + * Copyright 2013
70 + *
71 + * This program is free software; you can redistribute it and/or
72 + * modify it under the terms of the GNU General Public License
73 + * version 2 as published by the Free Software Foundation.
74 + *
75 + * This program is distributed in the hope that it will be useful, but
76 + * WITHOUT ANY WARRANTY; without even the implied warranty of
77 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
78 + * General Public License for more details.
79 + */
80 +
81 +#include <linux/module.h>
82 +#include <linux/platform_device.h>
83 +
84 +#include <sound/core.h>
85 +#include <sound/pcm.h>
86 +#include <sound/pcm_params.h>
87 +#include <sound/soc.h>
88 +#include <sound/jack.h>
89 +#include <linux/gpio/consumer.h>
90 +
91 +#include "../codecs/wm8804.h"
92 +
93 +static short int auto_shutdown_output = 0;
94 +module_param(auto_shutdown_output, short, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
95 +MODULE_PARM_DESC(auto_shutdown_output, "Shutdown SP/DIF output if playback is stopped");
96 +
97 +#define CLK_44EN_RATE 22579200UL
98 +#define CLK_48EN_RATE 24576000UL
99 +
100 +static bool snd_rpi_hifiberry_is_digipro;
101 +static struct gpio_desc *snd_rpi_hifiberry_clk44gpio;
102 +static struct gpio_desc *snd_rpi_hifiberry_clk48gpio;
103 +
104 +static int samplerate=44100;
105 +
106 +static uint32_t snd_rpi_hifiberry_digi_enable_clock(int sample_rate)
107 +{
108 + switch (sample_rate) {
109 + case 11025:
110 + case 22050:
111 + case 44100:
112 + case 88200:
113 + case 176400:
114 + gpiod_set_value_cansleep(snd_rpi_hifiberry_clk44gpio, 1);
115 + gpiod_set_value_cansleep(snd_rpi_hifiberry_clk48gpio, 0);
116 + return CLK_44EN_RATE;
117 + default:
118 + gpiod_set_value_cansleep(snd_rpi_hifiberry_clk48gpio, 1);
119 + gpiod_set_value_cansleep(snd_rpi_hifiberry_clk44gpio, 0);
120 + return CLK_48EN_RATE;
121 + }
122 +}
123 +
124 +
125 +static int snd_rpi_hifiberry_digi_init(struct snd_soc_pcm_runtime *rtd)
126 +{
127 + struct snd_soc_codec *codec = rtd->codec;
128 +
129 + /* enable TX output */
130 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
131 +
132 + /* Initialize Digi+ Pro hardware */
133 + if (snd_rpi_hifiberry_is_digipro) {
134 + struct snd_soc_dai_link *dai = rtd->dai_link;
135 +
136 + dai->name = "HiFiBerry Digi+ Pro";
137 + dai->stream_name = "HiFiBerry Digi+ Pro HiFi";
138 + }
139 +
140 + return 0;
141 +}
142 +
143 +static int snd_rpi_hifiberry_digi_startup(struct snd_pcm_substream *substream) {
144 + /* turn on digital output */
145 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
146 + struct snd_soc_codec *codec = rtd->codec;
147 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x00);
148 + return 0;
149 +}
150 +
151 +static void snd_rpi_hifiberry_digi_shutdown(struct snd_pcm_substream *substream) {
152 + /* turn off output */
153 + if (auto_shutdown_output) {
154 + /* turn off output */
155 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
156 + struct snd_soc_codec *codec = rtd->codec;
157 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x3c, 0x3c);
158 + }
159 +}
160 +
161 +
162 +static int snd_rpi_hifiberry_digi_hw_params(struct snd_pcm_substream *substream,
163 + struct snd_pcm_hw_params *params)
164 +{
165 + struct snd_soc_pcm_runtime *rtd = substream->private_data;
166 + struct snd_soc_dai *codec_dai = rtd->codec_dai;
167 + struct snd_soc_codec *codec = rtd->codec;
168 + struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
169 +
170 + int sysclk = 27000000; /* This is fixed on this board */
171 +
172 + long mclk_freq=0;
173 + int mclk_div=1;
174 + int sampling_freq=1;
175 +
176 + int ret;
177 +
178 + samplerate = params_rate(params);
179 +
180 + if (samplerate<=96000) {
181 + mclk_freq=samplerate*256;
182 + mclk_div=WM8804_MCLKDIV_256FS;
183 + } else {
184 + mclk_freq=samplerate*128;
185 + mclk_div=WM8804_MCLKDIV_128FS;
186 + }
187 +
188 + if (snd_rpi_hifiberry_is_digipro)
189 + sysclk = snd_rpi_hifiberry_digi_enable_clock(samplerate);
190 +
191 + switch (samplerate) {
192 + case 32000:
193 + sampling_freq=0x03;
194 + break;
195 + case 44100:
196 + sampling_freq=0x00;
197 + break;
198 + case 48000:
199 + sampling_freq=0x02;
200 + break;
201 + case 88200:
202 + sampling_freq=0x08;
203 + break;
204 + case 96000:
205 + sampling_freq=0x0a;
206 + break;
207 + case 176400:
208 + sampling_freq=0x0c;
209 + break;
210 + case 192000:
211 + sampling_freq=0x0e;
212 + break;
213 + default:
214 + dev_err(codec->dev,
215 + "Failed to set WM8804 SYSCLK, unsupported samplerate %d\n",
216 + samplerate);
217 + }
218 +
219 + snd_soc_dai_set_clkdiv(codec_dai, WM8804_MCLK_DIV, mclk_div);
220 + snd_soc_dai_set_pll(codec_dai, 0, 0, sysclk, mclk_freq);
221 +
222 + ret = snd_soc_dai_set_sysclk(codec_dai, WM8804_TX_CLKSRC_PLL,
223 + sysclk, SND_SOC_CLOCK_OUT);
224 +
225 + if (ret < 0) {
226 + dev_err(codec->dev,
227 + "Failed to set WM8804 SYSCLK: %d\n", ret);
228 + return ret;
229 + }
230 +
231 + /* Enable TX output */
232 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x4, 0x0);
233 +
234 + /* Power on */
235 + snd_soc_update_bits(codec, WM8804_PWRDN, 0x9, 0);
236 +
237 + /* set sampling frequency status bits */
238 + snd_soc_update_bits(codec, WM8804_SPDTX4, 0x0f, sampling_freq);
239 +
240 + return snd_soc_dai_set_bclk_ratio(cpu_dai,64);
241 +}
242 +
243 +/* machine stream operations */
244 +static struct snd_soc_ops snd_rpi_hifiberry_digi_ops = {
245 + .hw_params = snd_rpi_hifiberry_digi_hw_params,
246 + .startup = snd_rpi_hifiberry_digi_startup,
247 + .shutdown = snd_rpi_hifiberry_digi_shutdown,
248 +};
249 +
250 +static struct snd_soc_dai_link snd_rpi_hifiberry_digi_dai[] = {
251 +{
252 + .name = "HifiBerry Digi",
253 + .stream_name = "HifiBerry Digi HiFi",
254 + .cpu_dai_name = "bcm2708-i2s.0",
255 + .codec_dai_name = "wm8804-spdif",
256 + .platform_name = "bcm2708-i2s.0",
257 + .codec_name = "wm8804.1-003b",
258 + .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
259 + SND_SOC_DAIFMT_CBM_CFM,
260 + .ops = &snd_rpi_hifiberry_digi_ops,
261 + .init = snd_rpi_hifiberry_digi_init,
262 +},
263 +};
264 +
265 +/* audio machine driver */
266 +static struct snd_soc_card snd_rpi_hifiberry_digi = {
267 + .name = "snd_rpi_hifiberry_digi",
268 + .driver_name = "HifiberryDigi",
269 + .owner = THIS_MODULE,
270 + .dai_link = snd_rpi_hifiberry_digi_dai,
271 + .num_links = ARRAY_SIZE(snd_rpi_hifiberry_digi_dai),
272 +};
273 +
274 +static int snd_rpi_hifiberry_digi_probe(struct platform_device *pdev)
275 +{
276 + int ret = 0;
277 +
278 + snd_rpi_hifiberry_digi.dev = &pdev->dev;
279 +
280 + if (pdev->dev.of_node) {
281 + struct device_node *i2s_node;
282 + struct snd_soc_dai_link *dai = &snd_rpi_hifiberry_digi_dai[0];
283 + i2s_node = of_parse_phandle(pdev->dev.of_node,
284 + "i2s-controller", 0);
285 +
286 + if (i2s_node) {
287 + dai->cpu_dai_name = NULL;
288 + dai->cpu_of_node = i2s_node;
289 + dai->platform_name = NULL;
290 + dai->platform_of_node = i2s_node;
291 + }
292 +
293 + snd_rpi_hifiberry_is_digipro = 1;
294 +
295 + snd_rpi_hifiberry_clk44gpio =
296 + devm_gpiod_get(&pdev->dev, "clock44", GPIOD_OUT_LOW);
297 + if (IS_ERR(snd_rpi_hifiberry_clk44gpio))
298 + snd_rpi_hifiberry_is_digipro = 0;
299 +
300 + snd_rpi_hifiberry_clk48gpio =
301 + devm_gpiod_get(&pdev->dev, "clock48", GPIOD_OUT_LOW);
302 + if (IS_ERR(snd_rpi_hifiberry_clk48gpio))
303 + snd_rpi_hifiberry_is_digipro = 0;
304 +
305 + }
306 +
307 + ret = snd_soc_register_card(&snd_rpi_hifiberry_digi);
308 + if (ret && ret != -EPROBE_DEFER)
309 + dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", ret);
310 +
311 + return ret;
312 +}
313 +
314 +static int snd_rpi_hifiberry_digi_remove(struct platform_device *pdev)
315 +{
316 + return snd_soc_unregister_card(&snd_rpi_hifiberry_digi);
317 +}
318 +
319 +static const struct of_device_id snd_rpi_hifiberry_digi_of_match[] = {
320 + { .compatible = "hifiberry,hifiberry-digi", },
321 + {},
322 +};
323 +MODULE_DEVICE_TABLE(of, snd_rpi_hifiberry_digi_of_match);
324 +
325 +static struct platform_driver snd_rpi_hifiberry_digi_driver = {
326 + .driver = {
327 + .name = "snd-hifiberry-digi",
328 + .owner = THIS_MODULE,
329 + .of_match_table = snd_rpi_hifiberry_digi_of_match,
330 + },
331 + .probe = snd_rpi_hifiberry_digi_probe,
332 + .remove = snd_rpi_hifiberry_digi_remove,
333 +};
334 +
335 +module_platform_driver(snd_rpi_hifiberry_digi_driver);
336 +
337 +MODULE_AUTHOR("Daniel Matuschek <info@crazy-audio.com>");
338 +MODULE_DESCRIPTION("ASoC Driver for HifiBerry Digi");
339 +MODULE_LICENSE("GPL v2");